blob: ae7617095dec55956350a384b420d3622ef18bf1 [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 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Tejun Heo0a950f62012-11-19 09:02:12 -0800141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida;
143
Paul Menagee788e062008-07-25 01:46:59 -0700144 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700145 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700146
147 /* The name for this hierarchy - may be empty */
148 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149};
150
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151/*
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
155 */
156static struct cgroupfs_root rootnode;
157
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700158/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
160 */
161struct cfent {
162 struct list_head node;
163 struct dentry *dentry;
164 struct cftype *type;
165};
166
167/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
170 */
171#define CSS_ID_MAX (65535)
172struct css_id {
173 /*
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700179 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100180 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700181 /*
182 * ID of this css.
183 */
184 unsigned short id;
185 /*
186 * Depth in hierarchy which this ID belongs to.
187 */
188 unsigned short depth;
189 /*
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
191 */
192 struct rcu_head rcu_head;
193 /*
194 * Hierarchy of CSS ID belongs to.
195 */
196 unsigned short stack[0]; /* Array of Length (depth+1) */
197};
198
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800199/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300200 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800201 */
202struct cgroup_event {
203 /*
204 * Cgroup which the event belongs to.
205 */
206 struct cgroup *cgrp;
207 /*
208 * Control file which the event associated.
209 */
210 struct cftype *cft;
211 /*
212 * eventfd to signal userspace about the event.
213 */
214 struct eventfd_ctx *eventfd;
215 /*
216 * Each of these stored in a list by the cgroup.
217 */
218 struct list_head list;
219 /*
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
222 */
223 poll_table pt;
224 wait_queue_head_t *wqh;
225 wait_queue_t wait;
226 struct work_struct remove;
227};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* The list of hierarchy roots */
230
231static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700232static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700234static DEFINE_IDA(hierarchy_ida);
235static int next_hierarchy_id;
236static DEFINE_SPINLOCK(hierarchy_id_lock);
237
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239#define dummytop (&rootnode.top_cgroup)
240
Li Zefan65dff752013-03-01 15:01:56 +0800241static struct cgroup_name root_cgroup_name = { .name = "/" };
242
Paul Menageddbcc7e2007-10-18 23:39:30 -0700243/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800244 * check for fork/exit handlers to call. This avoids us having to do
245 * extra work in the fork/exit path if none of the subsystems need to
246 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700248static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249
Tejun Heo42809dd2012-11-19 08:13:37 -0800250static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800251static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
252 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800253
Paul E. McKenneyd11c5632010-02-22 17:04:50 -0800254#ifdef CONFIG_PROVE_LOCKING
255int cgroup_lock_is_held(void)
256{
257 return lockdep_is_held(&cgroup_mutex);
258}
259#else /* #ifdef CONFIG_PROVE_LOCKING */
260int cgroup_lock_is_held(void)
261{
262 return mutex_is_locked(&cgroup_mutex);
263}
264#endif /* #else #ifdef CONFIG_PROVE_LOCKING */
265
266EXPORT_SYMBOL_GPL(cgroup_lock_is_held);
267
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700268static int css_unbias_refcnt(int refcnt)
269{
270 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
271}
272
Tejun Heo28b4c272012-04-01 12:09:56 -0700273/* the current nr of refs, always >= 0 whether @css is deactivated or not */
274static int css_refcnt(struct cgroup_subsys_state *css)
275{
276 int v = atomic_read(&css->refcnt);
277
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700278 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700279}
280
Paul Menageddbcc7e2007-10-18 23:39:30 -0700281/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700282inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283{
Paul Menagebd89aab2007-10-18 23:40:44 -0700284 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700285}
286
287/* bits in struct cgroupfs_root flags field */
288enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400289 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
290 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700291};
292
Adrian Bunke9685a02008-02-07 00:13:46 -0800293static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700294{
295 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700296 (1 << CGRP_RELEASABLE) |
297 (1 << CGRP_NOTIFY_ON_RELEASE);
298 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700299}
300
Adrian Bunke9685a02008-02-07 00:13:46 -0800301static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302{
Paul Menagebd89aab2007-10-18 23:40:44 -0700303 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700304}
305
Paul Menageddbcc7e2007-10-18 23:39:30 -0700306/*
307 * for_each_subsys() allows you to iterate on each subsystem attached to
308 * an active hierarchy
309 */
310#define for_each_subsys(_root, _ss) \
311list_for_each_entry(_ss, &_root->subsys_list, sibling)
312
Li Zefane5f6a862009-01-07 18:07:41 -0800313/* for_each_active_root() allows you to iterate across the active hierarchies */
314#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700315list_for_each_entry(_root, &roots, root_list)
316
Tejun Heof6ea9372012-04-01 12:09:55 -0700317static inline struct cgroup *__d_cgrp(struct dentry *dentry)
318{
319 return dentry->d_fsdata;
320}
321
Tejun Heo05ef1d72012-04-01 12:09:56 -0700322static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700323{
324 return dentry->d_fsdata;
325}
326
Tejun Heo05ef1d72012-04-01 12:09:56 -0700327static inline struct cftype *__d_cft(struct dentry *dentry)
328{
329 return __d_cfe(dentry)->type;
330}
331
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700332/**
333 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
334 * @cgrp: the cgroup to be checked for liveness
335 *
336 * On success, returns true; the lock should be later released with
337 * cgroup_unlock(). On failure returns false with no lock held.
338 */
339bool cgroup_lock_live_group(struct cgroup *cgrp)
340{
341 mutex_lock(&cgroup_mutex);
342 if (cgroup_is_removed(cgrp)) {
343 mutex_unlock(&cgroup_mutex);
344 return false;
345 }
346 return true;
347}
348EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
349
Paul Menage81a6a5c2007-10-18 23:39:38 -0700350/* the list of cgroups eligible for automatic release. Protected by
351 * release_list_lock */
352static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200353static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700354static void cgroup_release_agent(struct work_struct *work);
355static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700356static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700357
Paul Menage817929e2007-10-18 23:39:36 -0700358/* Link structure for associating css_set objects with cgroups */
359struct cg_cgroup_link {
360 /*
361 * List running through cg_cgroup_links associated with a
362 * cgroup, anchored on cgroup->css_sets
363 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700364 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700365 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700366 /*
367 * List running through cg_cgroup_links pointing at a
368 * single css_set object, anchored on css_set->cg_links
369 */
370 struct list_head cg_link_list;
371 struct css_set *cg;
372};
373
374/* The default css_set - used by init and its children prior to any
375 * hierarchies being mounted. It contains a pointer to the root state
376 * for each subsystem. Also used to anchor the list of css_sets. Not
377 * reference-counted, to improve performance when child cgroups
378 * haven't been created.
379 */
380
381static struct css_set init_css_set;
382static struct cg_cgroup_link init_css_set_link;
383
Ben Blume6a11052010-03-10 15:22:09 -0800384static int cgroup_init_idr(struct cgroup_subsys *ss,
385 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700386
Paul Menage817929e2007-10-18 23:39:36 -0700387/* css_set_lock protects the list of css_set objects, and the
388 * chain of tasks off each css_set. Nests outside task->alloc_lock
389 * due to cgroup_iter_start() */
390static DEFINE_RWLOCK(css_set_lock);
391static int css_set_count;
392
Paul Menage7717f7b2009-09-23 15:56:22 -0700393/*
394 * hash table for cgroup groups. This improves the performance to find
395 * an existing css_set. This hash doesn't (currently) take into
396 * account cgroups in empty hierarchies.
397 */
Li Zefan472b1052008-04-29 01:00:11 -0700398#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800399static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700400
Li Zefan0ac801f2013-01-10 11:49:27 +0800401static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700402{
403 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800404 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700405
406 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800407 key += (unsigned long)css[i];
408 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700409
Li Zefan0ac801f2013-01-10 11:49:27 +0800410 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700411}
412
Paul Menage817929e2007-10-18 23:39:36 -0700413/* We don't maintain the lists running through each css_set to its
414 * task until after the first call to cgroup_iter_start(). This
415 * reduces the fork()/exit() overhead for people who have cgroups
416 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700417static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700418
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700420{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700421 struct cg_cgroup_link *link;
422 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700423 /*
424 * Ensure that the refcount doesn't hit zero while any readers
425 * can see it. Similar to atomic_dec_and_lock(), but for an
426 * rwlock
427 */
428 if (atomic_add_unless(&cg->refcount, -1, 1))
429 return;
430 write_lock(&css_set_lock);
431 if (!atomic_dec_and_test(&cg->refcount)) {
432 write_unlock(&css_set_lock);
433 return;
434 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800437 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700438 css_set_count--;
439
440 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
441 cg_link_list) {
442 struct cgroup *cgrp = link->cgrp;
443 list_del(&link->cg_link_list);
444 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800445
446 /*
447 * We may not be holding cgroup_mutex, and if cgrp->count is
448 * dropped to 0 the cgroup can be destroyed at any time, hence
449 * rcu_read_lock is used to keep it alive.
450 */
451 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700452 if (atomic_dec_and_test(&cgrp->count) &&
453 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700455 set_bit(CGRP_RELEASABLE, &cgrp->flags);
456 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700457 }
Li Zefan71b57072013-01-24 14:43:28 +0800458 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700459
460 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700461 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700462
463 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800464 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700465}
466
467/*
468 * refcounted get/put for css_set objects
469 */
470static inline void get_css_set(struct css_set *cg)
471{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700472 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700473}
474
475static inline void put_css_set(struct css_set *cg)
476{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700477 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700478}
479
Paul Menage81a6a5c2007-10-18 23:39:38 -0700480static inline void put_css_set_taskexit(struct css_set *cg)
481{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700482 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700483}
484
Paul Menage817929e2007-10-18 23:39:36 -0700485/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700486 * compare_css_sets - helper function for find_existing_css_set().
487 * @cg: candidate css_set being tested
488 * @old_cg: existing css_set for a task
489 * @new_cgrp: cgroup that's being entered by the task
490 * @template: desired set of css pointers in css_set (pre-calculated)
491 *
492 * Returns true if "cg" matches "old_cg" except for the hierarchy
493 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
494 */
495static bool compare_css_sets(struct css_set *cg,
496 struct css_set *old_cg,
497 struct cgroup *new_cgrp,
498 struct cgroup_subsys_state *template[])
499{
500 struct list_head *l1, *l2;
501
502 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
503 /* Not all subsystems matched */
504 return false;
505 }
506
507 /*
508 * Compare cgroup pointers in order to distinguish between
509 * different cgroups in heirarchies with no subsystems. We
510 * could get by with just this check alone (and skip the
511 * memcmp above) but on most setups the memcmp check will
512 * avoid the need for this more expensive check on almost all
513 * candidates.
514 */
515
516 l1 = &cg->cg_links;
517 l2 = &old_cg->cg_links;
518 while (1) {
519 struct cg_cgroup_link *cgl1, *cgl2;
520 struct cgroup *cg1, *cg2;
521
522 l1 = l1->next;
523 l2 = l2->next;
524 /* See if we reached the end - both lists are equal length. */
525 if (l1 == &cg->cg_links) {
526 BUG_ON(l2 != &old_cg->cg_links);
527 break;
528 } else {
529 BUG_ON(l2 == &old_cg->cg_links);
530 }
531 /* Locate the cgroups associated with these links. */
532 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
533 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
534 cg1 = cgl1->cgrp;
535 cg2 = cgl2->cgrp;
536 /* Hierarchies should be linked in the same order. */
537 BUG_ON(cg1->root != cg2->root);
538
539 /*
540 * If this hierarchy is the hierarchy of the cgroup
541 * that's changing, then we need to check that this
542 * css_set points to the new cgroup; if it's any other
543 * hierarchy, then this css_set should point to the
544 * same cgroup as the old css_set.
545 */
546 if (cg1->root == new_cgrp->root) {
547 if (cg1 != new_cgrp)
548 return false;
549 } else {
550 if (cg1 != cg2)
551 return false;
552 }
553 }
554 return true;
555}
556
557/*
Paul Menage817929e2007-10-18 23:39:36 -0700558 * find_existing_css_set() is a helper for
559 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700560 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700561 *
562 * oldcg: the cgroup group that we're using before the cgroup
563 * transition
564 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700565 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700566 *
567 * template: location in which to build the desired set of subsystem
568 * state objects for the new cgroup group
569 */
Paul Menage817929e2007-10-18 23:39:36 -0700570static struct css_set *find_existing_css_set(
571 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700572 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700573 struct cgroup_subsys_state *template[])
574{
575 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700576 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700577 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800578 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700579
Ben Blumaae8aab2010-03-10 15:22:07 -0800580 /*
581 * Build the set of subsystem state objects that we want to see in the
582 * new css_set. while subsystems can change globally, the entries here
583 * won't change, so no need for locking.
584 */
Paul Menage817929e2007-10-18 23:39:36 -0700585 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400586 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700587 /* Subsystem is in this hierarchy. So we want
588 * the subsystem state from the new
589 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700590 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700591 } else {
592 /* Subsystem is not in this hierarchy, so we
593 * don't want to change the subsystem state */
594 template[i] = oldcg->subsys[i];
595 }
596 }
597
Li Zefan0ac801f2013-01-10 11:49:27 +0800598 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800599 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700600 if (!compare_css_sets(cg, oldcg, cgrp, template))
601 continue;
602
603 /* This css_set matches what we need */
604 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700605 }
Paul Menage817929e2007-10-18 23:39:36 -0700606
607 /* No existing cgroup group matched */
608 return NULL;
609}
610
Paul Menage817929e2007-10-18 23:39:36 -0700611static void free_cg_links(struct list_head *tmp)
612{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700613 struct cg_cgroup_link *link;
614 struct cg_cgroup_link *saved_link;
615
616 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700617 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700618 kfree(link);
619 }
620}
621
622/*
Li Zefan36553432008-07-29 22:33:19 -0700623 * allocate_cg_links() allocates "count" cg_cgroup_link structures
624 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
625 * success or a negative error
626 */
627static int allocate_cg_links(int count, struct list_head *tmp)
628{
629 struct cg_cgroup_link *link;
630 int i;
631 INIT_LIST_HEAD(tmp);
632 for (i = 0; i < count; i++) {
633 link = kmalloc(sizeof(*link), GFP_KERNEL);
634 if (!link) {
635 free_cg_links(tmp);
636 return -ENOMEM;
637 }
638 list_add(&link->cgrp_link_list, tmp);
639 }
640 return 0;
641}
642
Li Zefanc12f65d2009-01-07 18:07:42 -0800643/**
644 * link_css_set - a helper function to link a css_set to a cgroup
645 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
646 * @cg: the css_set to be linked
647 * @cgrp: the destination cgroup
648 */
649static void link_css_set(struct list_head *tmp_cg_links,
650 struct css_set *cg, struct cgroup *cgrp)
651{
652 struct cg_cgroup_link *link;
653
654 BUG_ON(list_empty(tmp_cg_links));
655 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
656 cgrp_link_list);
657 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700658 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700659 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800660 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700661 /*
662 * Always add links to the tail of the list so that the list
663 * is sorted by order of hierarchy creation
664 */
665 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800666}
667
Li Zefan36553432008-07-29 22:33:19 -0700668/*
Paul Menage817929e2007-10-18 23:39:36 -0700669 * find_css_set() takes an existing cgroup group and a
670 * cgroup object, and returns a css_set object that's
671 * equivalent to the old group, but with the given cgroup
672 * substituted into the appropriate hierarchy. Must be called with
673 * cgroup_mutex held
674 */
Paul Menage817929e2007-10-18 23:39:36 -0700675static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700676 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700677{
678 struct css_set *res;
679 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700680
681 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700682
Paul Menage7717f7b2009-09-23 15:56:22 -0700683 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800684 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700685
Paul Menage817929e2007-10-18 23:39:36 -0700686 /* First see if we already have a cgroup group that matches
687 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700688 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700689 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700690 if (res)
691 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700692 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700693
694 if (res)
695 return res;
696
697 res = kmalloc(sizeof(*res), GFP_KERNEL);
698 if (!res)
699 return NULL;
700
701 /* Allocate all the cg_cgroup_link objects that we'll need */
702 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
703 kfree(res);
704 return NULL;
705 }
706
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700707 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700708 INIT_LIST_HEAD(&res->cg_links);
709 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700710 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700711
712 /* Copy the set of subsystem state objects generated in
713 * find_existing_css_set() */
714 memcpy(res->subsys, template, sizeof(res->subsys));
715
716 write_lock(&css_set_lock);
717 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
719 struct cgroup *c = link->cgrp;
720 if (c->root == cgrp->root)
721 c = cgrp;
722 link_css_set(&tmp_cg_links, res, c);
723 }
Paul Menage817929e2007-10-18 23:39:36 -0700724
725 BUG_ON(!list_empty(&tmp_cg_links));
726
Paul Menage817929e2007-10-18 23:39:36 -0700727 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700728
729 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800730 key = css_set_hash(res->subsys);
731 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700732
Paul Menage817929e2007-10-18 23:39:36 -0700733 write_unlock(&css_set_lock);
734
735 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700736}
737
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700739 * Return the cgroup for "task" from the given hierarchy. Must be
740 * called with cgroup_mutex held.
741 */
742static struct cgroup *task_cgroup_from_root(struct task_struct *task,
743 struct cgroupfs_root *root)
744{
745 struct css_set *css;
746 struct cgroup *res = NULL;
747
748 BUG_ON(!mutex_is_locked(&cgroup_mutex));
749 read_lock(&css_set_lock);
750 /*
751 * No need to lock the task - since we hold cgroup_mutex the
752 * task can't change groups, so the only thing that can happen
753 * is that it exits and its css is set back to init_css_set.
754 */
755 css = task->cgroups;
756 if (css == &init_css_set) {
757 res = &root->top_cgroup;
758 } else {
759 struct cg_cgroup_link *link;
760 list_for_each_entry(link, &css->cg_links, cg_link_list) {
761 struct cgroup *c = link->cgrp;
762 if (c->root == root) {
763 res = c;
764 break;
765 }
766 }
767 }
768 read_unlock(&css_set_lock);
769 BUG_ON(!res);
770 return res;
771}
772
773/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 * There is one global cgroup mutex. We also require taking
775 * task_lock() when dereferencing a task's cgroup subsys pointers.
776 * See "The task_lock() exception", at the end of this comment.
777 *
778 * A task must hold cgroup_mutex to modify cgroups.
779 *
780 * Any task can increment and decrement the count field without lock.
781 * So in general, code holding cgroup_mutex can't rely on the count
782 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800783 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784 * means that no tasks are currently attached, therefore there is no
785 * way a task attached to that cgroup can fork (the other way to
786 * increment the count). So code holding cgroup_mutex can safely
787 * assume that if the count is zero, it will stay zero. Similarly, if
788 * a task holds cgroup_mutex on a cgroup with zero count, it
789 * knows that the cgroup won't be removed, as cgroup_rmdir()
790 * needs that mutex.
791 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
793 * (usually) take cgroup_mutex. These are the two most performance
794 * critical pieces of code here. The exception occurs on cgroup_exit(),
795 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
796 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800797 * to the release agent with the name of the cgroup (path relative to
798 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799 *
800 * A cgroup can only be deleted if both its 'count' of using tasks
801 * is zero, and its list of 'children' cgroups is empty. Since all
802 * tasks in the system use _some_ cgroup, and since there is always at
803 * least one task in the system (init, pid == 1), therefore, top_cgroup
804 * always has either children cgroups and/or using tasks. So we don't
805 * need a special hack to ensure that top_cgroup cannot be deleted.
806 *
807 * The task_lock() exception
808 *
809 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800810 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800811 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812 * several performance critical places that need to reference
813 * task->cgroup without the expense of grabbing a system global
814 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800815 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
817 * the task_struct routinely used for such matters.
818 *
819 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800820 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821 */
822
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823/**
824 * cgroup_lock - lock out any changes to cgroup structures
825 *
826 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827void cgroup_lock(void)
828{
829 mutex_lock(&cgroup_mutex);
830}
Ben Blum67523c42010-03-10 15:22:11 -0800831EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700832
833/**
834 * cgroup_unlock - release lock on cgroup changes
835 *
836 * Undo the lock taken in a previous cgroup_lock() call.
837 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700838void cgroup_unlock(void)
839{
840 mutex_unlock(&cgroup_mutex);
841}
Ben Blum67523c42010-03-10 15:22:11 -0800842EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843
844/*
845 * A couple of forward declarations required, due to cyclic reference loop:
846 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
847 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
848 * -> cgroup_mkdir.
849 */
850
Al Viro18bb1db2011-07-26 01:41:39 -0400851static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400852static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400854static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
855 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700856static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700857static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700858
859static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200860 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700861 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700862};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700863
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700864static int alloc_css_id(struct cgroup_subsys *ss,
865 struct cgroup *parent, struct cgroup *child);
866
Al Viroa5e7ed32011-07-26 01:55:55 -0400867static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700868{
869 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700870
871 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400872 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700873 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100874 inode->i_uid = current_fsuid();
875 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700876 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
877 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
878 }
879 return inode;
880}
881
Li Zefan65dff752013-03-01 15:01:56 +0800882static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
883{
884 struct cgroup_name *name;
885
886 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
887 if (!name)
888 return NULL;
889 strcpy(name->name, dentry->d_name.name);
890 return name;
891}
892
Li Zefanbe445622013-01-24 14:31:42 +0800893static void cgroup_free_fn(struct work_struct *work)
894{
895 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
896 struct cgroup_subsys *ss;
897
898 mutex_lock(&cgroup_mutex);
899 /*
900 * Release the subsystem state objects.
901 */
902 for_each_subsys(cgrp->root, ss)
903 ss->css_free(cgrp);
904
905 cgrp->root->number_of_cgroups--;
906 mutex_unlock(&cgroup_mutex);
907
908 /*
909 * Drop the active superblock reference that we took when we
910 * created the cgroup
911 */
912 deactivate_super(cgrp->root->sb);
913
914 /*
915 * if we're getting rid of the cgroup, refcount should ensure
916 * that there are no pidlists left.
917 */
918 BUG_ON(!list_empty(&cgrp->pidlists));
919
920 simple_xattrs_free(&cgrp->xattrs);
921
922 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +0800923 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800924 kfree(cgrp);
925}
926
927static void cgroup_free_rcu(struct rcu_head *head)
928{
929 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
930
931 schedule_work(&cgrp->free_work);
932}
933
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934static void cgroup_diput(struct dentry *dentry, struct inode *inode)
935{
936 /* is dentry a directory ? if so, kfree() associated cgroup */
937 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700938 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800939
Paul Menagebd89aab2007-10-18 23:40:44 -0700940 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800941 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 } else {
943 struct cfent *cfe = __d_cfe(dentry);
944 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400945 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700946
947 WARN_ONCE(!list_empty(&cfe->node) &&
948 cgrp != &cgrp->root->top_cgroup,
949 "cfe still linked for %s\n", cfe->type->name);
950 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400951 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952 }
953 iput(inode);
954}
955
Al Viroc72a04e2011-01-14 05:31:45 +0000956static int cgroup_delete(const struct dentry *d)
957{
958 return 1;
959}
960
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961static void remove_dir(struct dentry *d)
962{
963 struct dentry *parent = dget(d->d_parent);
964
965 d_delete(d);
966 simple_rmdir(parent->d_inode, d);
967 dput(parent);
968}
969
Li Zefan2739d3c2013-01-21 18:18:33 +0800970static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700972 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973
Tejun Heo05ef1d72012-04-01 12:09:56 -0700974 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
975 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100976
Li Zefan2739d3c2013-01-21 18:18:33 +0800977 /*
978 * If we're doing cleanup due to failure of cgroup_create(),
979 * the corresponding @cfe may not exist.
980 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700981 list_for_each_entry(cfe, &cgrp->files, node) {
982 struct dentry *d = cfe->dentry;
983
984 if (cft && cfe->type != cft)
985 continue;
986
987 dget(d);
988 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700989 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700990 list_del_init(&cfe->node);
991 dput(d);
992
Li Zefan2739d3c2013-01-21 18:18:33 +0800993 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700995}
996
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400997/**
998 * cgroup_clear_directory - selective removal of base and subsystem files
999 * @dir: directory containing the files
1000 * @base_files: true if the base files should be removed
1001 * @subsys_mask: mask of the subsystem ids whose files should be removed
1002 */
1003static void cgroup_clear_directory(struct dentry *dir, bool base_files,
1004 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001005{
1006 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001007 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001008
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001009 for_each_subsys(cgrp->root, ss) {
1010 struct cftype_set *set;
1011 if (!test_bit(ss->subsys_id, &subsys_mask))
1012 continue;
1013 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +08001014 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001015 }
1016 if (base_files) {
1017 while (!list_empty(&cgrp->files))
1018 cgroup_rm_file(cgrp, NULL);
1019 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020}
1021
1022/*
1023 * NOTE : the dentry must have been dget()'ed
1024 */
1025static void cgroup_d_remove_dir(struct dentry *dentry)
1026{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001027 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001028 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001029
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001030 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001032 parent = dentry->d_parent;
1033 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001034 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001036 spin_unlock(&dentry->d_lock);
1037 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 remove_dir(dentry);
1039}
1040
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001041/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001042 * Call with cgroup_mutex held. Drops reference counts on modules, including
1043 * any duplicate ones that parse_cgroupfs_options took. If this function
1044 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001045 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001047 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001049 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001050 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 int i;
1052
Ben Blumaae8aab2010-03-10 15:22:07 -08001053 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001054 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001055
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001056 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1057 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 /* Check that any added subsystems are currently free */
1059 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001060 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001062 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001064 /*
1065 * Nobody should tell us to do a subsys that doesn't exist:
1066 * parse_cgroupfs_options should catch that case and refcounts
1067 * ensure that subsystems won't disappear once selected.
1068 */
1069 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 if (ss->root != &rootnode) {
1071 /* Subsystem isn't free */
1072 return -EBUSY;
1073 }
1074 }
1075
1076 /* Currently we don't handle adding/removing subsystems when
1077 * any child cgroups exist. This is theoretically supportable
1078 * but involves complex error handling, so it's being left until
1079 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001080 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 return -EBUSY;
1082
1083 /* Process each subsystem */
1084 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1085 struct cgroup_subsys *ss = subsys[i];
1086 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001087 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001089 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001090 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 BUG_ON(!dummytop->subsys[i]);
1092 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001093 cgrp->subsys[i] = dummytop->subsys[i];
1094 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001095 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001096 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001098 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001099 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001100 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001102 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001103 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1104 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001106 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001108 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001109 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001110 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001111 /* subsystem is now free - drop reference on module */
1112 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001113 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001115 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001116 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001117 /*
1118 * a refcount was taken, but we already had one, so
1119 * drop the extra reference.
1120 */
1121 module_put(ss->module);
1122#ifdef CONFIG_MODULE_UNLOAD
1123 BUG_ON(ss->module && !module_refcount(ss->module));
1124#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 } else {
1126 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001127 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 }
1129 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001130 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131
1132 return 0;
1133}
1134
Al Viro34c80b12011-12-08 21:32:45 -05001135static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136{
Al Viro34c80b12011-12-08 21:32:45 -05001137 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138 struct cgroup_subsys *ss;
1139
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001140 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141 for_each_subsys(root, ss)
1142 seq_printf(seq, ",%s", ss->name);
1143 if (test_bit(ROOT_NOPREFIX, &root->flags))
1144 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001145 if (test_bit(ROOT_XATTR, &root->flags))
1146 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001147 if (strlen(root->release_agent_path))
1148 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001149 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001150 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001151 if (strlen(root->name))
1152 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001153 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154 return 0;
1155}
1156
1157struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001158 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001160 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001161 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001162 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001163 /* User explicitly requested empty subsystem */
1164 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001165
1166 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001167
Paul Menageddbcc7e2007-10-18 23:39:30 -07001168};
1169
Ben Blumaae8aab2010-03-10 15:22:07 -08001170/*
1171 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001172 * with cgroup_mutex held to protect the subsys[] array. This function takes
1173 * refcounts on subsystems to be used, unless it returns error, in which case
1174 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001175 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001176static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001177{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 char *token, *o = data;
1179 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001180 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001181 int i;
1182 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001183
Ben Blumaae8aab2010-03-10 15:22:07 -08001184 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1185
Li Zefanf9ab5b52009-06-17 16:26:33 -07001186#ifdef CONFIG_CPUSETS
1187 mask = ~(1UL << cpuset_subsys_id);
1188#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001189
Paul Menagec6d57f32009-09-23 15:56:19 -07001190 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001191
1192 while ((token = strsep(&o, ",")) != NULL) {
1193 if (!*token)
1194 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001196 /* Explicitly have no subsystems */
1197 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198 continue;
1199 }
1200 if (!strcmp(token, "all")) {
1201 /* Mutually exclusive option 'all' + subsystem name */
1202 if (one_ss)
1203 return -EINVAL;
1204 all_ss = true;
1205 continue;
1206 }
1207 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001208 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001209 continue;
1210 }
1211 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001212 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001213 continue;
1214 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001215 if (!strcmp(token, "xattr")) {
1216 set_bit(ROOT_XATTR, &opts->flags);
1217 continue;
1218 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001219 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001220 /* Specifying two release agents is forbidden */
1221 if (opts->release_agent)
1222 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001223 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001224 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001225 if (!opts->release_agent)
1226 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001227 continue;
1228 }
1229 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001230 const char *name = token + 5;
1231 /* Can't specify an empty name */
1232 if (!strlen(name))
1233 return -EINVAL;
1234 /* Must match [\w.-]+ */
1235 for (i = 0; i < strlen(name); i++) {
1236 char c = name[i];
1237 if (isalnum(c))
1238 continue;
1239 if ((c == '.') || (c == '-') || (c == '_'))
1240 continue;
1241 return -EINVAL;
1242 }
1243 /* Specifying two names is forbidden */
1244 if (opts->name)
1245 return -EINVAL;
1246 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001247 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001248 GFP_KERNEL);
1249 if (!opts->name)
1250 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001251
1252 continue;
1253 }
1254
1255 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1256 struct cgroup_subsys *ss = subsys[i];
1257 if (ss == NULL)
1258 continue;
1259 if (strcmp(token, ss->name))
1260 continue;
1261 if (ss->disabled)
1262 continue;
1263
1264 /* Mutually exclusive option 'all' + subsystem name */
1265 if (all_ss)
1266 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001267 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001268 one_ss = true;
1269
1270 break;
1271 }
1272 if (i == CGROUP_SUBSYS_COUNT)
1273 return -ENOENT;
1274 }
1275
1276 /*
1277 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001278 * otherwise if 'none', 'name=' and a subsystem name options
1279 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001280 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001281 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001282 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1283 struct cgroup_subsys *ss = subsys[i];
1284 if (ss == NULL)
1285 continue;
1286 if (ss->disabled)
1287 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001288 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289 }
1290 }
1291
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001292 /* Consistency checks */
1293
Li Zefanf9ab5b52009-06-17 16:26:33 -07001294 /*
1295 * Option noprefix was introduced just for backward compatibility
1296 * with the old cpuset, so we allow noprefix only if mounting just
1297 * the cpuset subsystem.
1298 */
1299 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001300 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001301 return -EINVAL;
1302
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001303
1304 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001305 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001306 return -EINVAL;
1307
1308 /*
1309 * We either have to specify by name or by subsystems. (So all
1310 * empty hierarchies must have a name).
1311 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001312 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 return -EINVAL;
1314
Ben Blumcf5d5942010-03-10 15:22:09 -08001315 /*
1316 * Grab references on all the modules we'll need, so the subsystems
1317 * don't dance around before rebind_subsystems attaches them. This may
1318 * take duplicate reference counts on a subsystem that's already used,
1319 * but rebind_subsystems handles this case.
1320 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001321 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 unsigned long bit = 1UL << i;
1323
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001324 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 continue;
1326 if (!try_module_get(subsys[i]->module)) {
1327 module_pin_failed = true;
1328 break;
1329 }
1330 }
1331 if (module_pin_failed) {
1332 /*
1333 * oops, one of the modules was going away. this means that we
1334 * raced with a module_delete call, and to the user this is
1335 * essentially a "subsystem doesn't exist" case.
1336 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001337 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001338 /* drop refcounts only on the ones we took */
1339 unsigned long bit = 1UL << i;
1340
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001341 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001342 continue;
1343 module_put(subsys[i]->module);
1344 }
1345 return -ENOENT;
1346 }
1347
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348 return 0;
1349}
1350
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001351static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001352{
1353 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001354 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 unsigned long bit = 1UL << i;
1356
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001357 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001358 continue;
1359 module_put(subsys[i]->module);
1360 }
1361}
1362
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1364{
1365 int ret = 0;
1366 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001369 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370
Paul Menagebd89aab2007-10-18 23:40:44 -07001371 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001373 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374
1375 /* See what subsystems are wanted */
1376 ret = parse_cgroupfs_options(data, &opts);
1377 if (ret)
1378 goto out_unlock;
1379
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001380 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001381 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1382 task_tgid_nr(current), current->comm);
1383
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001384 added_mask = opts.subsys_mask & ~root->subsys_mask;
1385 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001386
Ben Blumcf5d5942010-03-10 15:22:09 -08001387 /* Don't allow flags or name to change at remount */
1388 if (opts.flags != root->flags ||
1389 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001391 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001392 goto out_unlock;
1393 }
1394
Gao feng7083d032012-12-03 09:28:18 +08001395 /*
1396 * Clear out the files of subsystems that should be removed, do
1397 * this before rebind_subsystems, since rebind_subsystems may
1398 * change this hierarchy's subsys_list.
1399 */
1400 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1401
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001402 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001403 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001404 /* rebind_subsystems failed, re-populate the removed files */
1405 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001406 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001407 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001408 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001410 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001411 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412
Paul Menage81a6a5c2007-10-18 23:39:38 -07001413 if (opts.release_agent)
1414 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001416 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001417 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001418 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 return ret;
1422}
1423
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001424static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425 .statfs = simple_statfs,
1426 .drop_inode = generic_delete_inode,
1427 .show_options = cgroup_show_options,
1428 .remount_fs = cgroup_remount,
1429};
1430
Paul Menagecc31edc2008-10-18 20:28:04 -07001431static void init_cgroup_housekeeping(struct cgroup *cgrp)
1432{
1433 INIT_LIST_HEAD(&cgrp->sibling);
1434 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001435 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001436 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001437 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001438 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001439 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001440 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001441 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001442 INIT_LIST_HEAD(&cgrp->event_list);
1443 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001444 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001445}
Paul Menagec6d57f32009-09-23 15:56:19 -07001446
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447static void init_cgroup_root(struct cgroupfs_root *root)
1448{
Paul Menagebd89aab2007-10-18 23:40:44 -07001449 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001450
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 INIT_LIST_HEAD(&root->subsys_list);
1452 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001453 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001455 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001456 cgrp->name = &root_cgroup_name;
Paul Menagebd89aab2007-10-18 23:40:44 -07001457 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001458 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001459 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460}
1461
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001462static bool init_root_id(struct cgroupfs_root *root)
1463{
1464 int ret = 0;
1465
1466 do {
1467 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1468 return false;
1469 spin_lock(&hierarchy_id_lock);
1470 /* Try to allocate the next unused ID */
1471 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1472 &root->hierarchy_id);
1473 if (ret == -ENOSPC)
1474 /* Try again starting from 0 */
1475 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1476 if (!ret) {
1477 next_hierarchy_id = root->hierarchy_id + 1;
1478 } else if (ret != -EAGAIN) {
1479 /* Can only get here if the 31-bit IDR is full ... */
1480 BUG_ON(ret);
1481 }
1482 spin_unlock(&hierarchy_id_lock);
1483 } while (ret);
1484 return true;
1485}
1486
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487static int cgroup_test_super(struct super_block *sb, void *data)
1488{
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490 struct cgroupfs_root *root = sb->s_fs_info;
1491
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 /* If we asked for a name then it must match */
1493 if (opts->name && strcmp(opts->name, root->name))
1494 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001496 /*
1497 * If we asked for subsystems (or explicitly for no
1498 * subsystems) then they must match
1499 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001500 if ((opts->subsys_mask || opts->none)
1501 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502 return 0;
1503
1504 return 1;
1505}
1506
Paul Menagec6d57f32009-09-23 15:56:19 -07001507static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1508{
1509 struct cgroupfs_root *root;
1510
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001511 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001512 return NULL;
1513
1514 root = kzalloc(sizeof(*root), GFP_KERNEL);
1515 if (!root)
1516 return ERR_PTR(-ENOMEM);
1517
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001518 if (!init_root_id(root)) {
1519 kfree(root);
1520 return ERR_PTR(-ENOMEM);
1521 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001523
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001524 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001525 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001526 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001527 if (opts->release_agent)
1528 strcpy(root->release_agent_path, opts->release_agent);
1529 if (opts->name)
1530 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001531 if (opts->cpuset_clone_children)
1532 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001533 return root;
1534}
1535
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001536static void cgroup_drop_root(struct cgroupfs_root *root)
1537{
1538 if (!root)
1539 return;
1540
1541 BUG_ON(!root->hierarchy_id);
1542 spin_lock(&hierarchy_id_lock);
1543 ida_remove(&hierarchy_ida, root->hierarchy_id);
1544 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001545 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001546 kfree(root);
1547}
1548
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549static int cgroup_set_super(struct super_block *sb, void *data)
1550{
1551 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001552 struct cgroup_sb_opts *opts = data;
1553
1554 /* If we don't have a new root, we can't set up a new sb */
1555 if (!opts->new_root)
1556 return -EINVAL;
1557
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001558 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 ret = set_anon_super(sb, NULL);
1561 if (ret)
1562 return ret;
1563
Paul Menagec6d57f32009-09-23 15:56:19 -07001564 sb->s_fs_info = opts->new_root;
1565 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
1567 sb->s_blocksize = PAGE_CACHE_SIZE;
1568 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1569 sb->s_magic = CGROUP_SUPER_MAGIC;
1570 sb->s_op = &cgroup_ops;
1571
1572 return 0;
1573}
1574
1575static int cgroup_get_rootdir(struct super_block *sb)
1576{
Al Viro0df6a632010-12-21 13:29:29 -05001577 static const struct dentry_operations cgroup_dops = {
1578 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001579 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001580 };
1581
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 struct inode *inode =
1583 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
1585 if (!inode)
1586 return -ENOMEM;
1587
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 inode->i_fop = &simple_dir_operations;
1589 inode->i_op = &cgroup_dir_inode_operations;
1590 /* directories start off with i_nlink == 2 (for "." entry) */
1591 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001592 sb->s_root = d_make_root(inode);
1593 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001595 /* for everything else we want ->d_op set */
1596 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597 return 0;
1598}
1599
Al Virof7e83572010-07-26 13:23:11 +04001600static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001602 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603{
1604 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 int ret = 0;
1607 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001609 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
1611 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001612 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001614 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001615 if (ret)
1616 goto out_err;
1617
1618 /*
1619 * Allocate a new cgroup root. We may not need it if we're
1620 * reusing an existing hierarchy.
1621 */
1622 new_root = cgroup_root_from_opts(&opts);
1623 if (IS_ERR(new_root)) {
1624 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001625 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001626 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001627 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628
Paul Menagec6d57f32009-09-23 15:56:19 -07001629 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001630 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001632 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001633 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001634 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635 }
1636
Paul Menagec6d57f32009-09-23 15:56:19 -07001637 root = sb->s_fs_info;
1638 BUG_ON(!root);
1639 if (root == opts.new_root) {
1640 /* We used the new root structure, so this is a new hierarchy */
1641 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001642 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001643 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001644 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001645 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001646 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647
1648 BUG_ON(sb->s_root != NULL);
1649
1650 ret = cgroup_get_rootdir(sb);
1651 if (ret)
1652 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001653 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
Paul Menage817929e2007-10-18 23:39:36 -07001655 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001657 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001659 /* Check for name clashes with existing mounts */
1660 ret = -EBUSY;
1661 if (strlen(root->name))
1662 for_each_active_root(existing_root)
1663 if (!strcmp(existing_root->name, root->name))
1664 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001665
Paul Menage817929e2007-10-18 23:39:36 -07001666 /*
1667 * We're accessing css_set_count without locking
1668 * css_set_lock here, but that's OK - it can only be
1669 * increased by someone holding cgroup_lock, and
1670 * that's us. The worst that can happen is that we
1671 * have some link structures left over
1672 */
1673 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001674 if (ret)
1675 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001676
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001677 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001679 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001680 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001682 /*
1683 * There must be no failure case after here, since rebinding
1684 * takes care of subsystems' refcounts, which are explicitly
1685 * dropped in the failure exit path.
1686 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687
1688 /* EBUSY should be the only error here */
1689 BUG_ON(ret);
1690
1691 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001692 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693
Li Zefanc12f65d2009-01-07 18:07:42 -08001694 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 root->top_cgroup.dentry = sb->s_root;
1696
Paul Menage817929e2007-10-18 23:39:36 -07001697 /* Link the top cgroup in this hierarchy into all
1698 * the css_set objects */
1699 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001700 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001701 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001702 write_unlock(&css_set_lock);
1703
1704 free_cg_links(&tmp_cg_links);
1705
Li Zefanc12f65d2009-01-07 18:07:42 -08001706 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 BUG_ON(root->number_of_cgroups != 1);
1708
eparis@redhat2ce97382011-06-02 21:20:51 +10001709 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001710 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001711 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001712 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001714 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 } else {
1716 /*
1717 * We re-used an existing hierarchy - the new root (if
1718 * any) is not needed
1719 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001720 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001721 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001722 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 }
1724
Paul Menagec6d57f32009-09-23 15:56:19 -07001725 kfree(opts.release_agent);
1726 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001727 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001729 unlock_drop:
1730 mutex_unlock(&cgroup_root_mutex);
1731 mutex_unlock(&cgroup_mutex);
1732 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001734 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001735 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001736 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001737 out_err:
1738 kfree(opts.release_agent);
1739 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001740 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741}
1742
1743static void cgroup_kill_sb(struct super_block *sb) {
1744 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001745 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001747 struct cg_cgroup_link *link;
1748 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
1750 BUG_ON(!root);
1751
1752 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001753 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754
1755 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001756 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757
1758 /* Rebind all subsystems back to the default hierarchy */
1759 ret = rebind_subsystems(root, 0);
1760 /* Shouldn't be able to fail ... */
1761 BUG_ON(ret);
1762
Paul Menage817929e2007-10-18 23:39:36 -07001763 /*
1764 * Release all the links from css_sets to this hierarchy's
1765 * root cgroup
1766 */
1767 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001768
1769 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1770 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001771 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001772 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001773 kfree(link);
1774 }
1775 write_unlock(&css_set_lock);
1776
Paul Menage839ec542009-01-29 14:25:22 -08001777 if (!list_empty(&root->root_list)) {
1778 list_del(&root->root_list);
1779 root_count--;
1780 }
Li Zefane5f6a862009-01-07 18:07:41 -08001781
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001782 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 mutex_unlock(&cgroup_mutex);
1784
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001785 simple_xattrs_free(&cgrp->xattrs);
1786
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001788 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789}
1790
1791static struct file_system_type cgroup_fs_type = {
1792 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001793 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 .kill_sb = cgroup_kill_sb,
1795};
1796
Greg KH676db4a2010-08-05 13:53:35 -07001797static struct kobject *cgroup_kobj;
1798
Li Zefana043e3b2008-02-23 15:24:09 -08001799/**
1800 * cgroup_path - generate the path of a cgroup
1801 * @cgrp: the cgroup in question
1802 * @buf: the buffer to write the path into
1803 * @buflen: the length of the buffer
1804 *
Li Zefan65dff752013-03-01 15:01:56 +08001805 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1806 *
1807 * We can't generate cgroup path using dentry->d_name, as accessing
1808 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1809 * inode's i_mutex, while on the other hand cgroup_path() can be called
1810 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001812int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813{
Li Zefan65dff752013-03-01 15:01:56 +08001814 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001816
Tao Ma316eb662012-11-08 21:36:38 +08001817 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001818 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001819
Li Zefan65dff752013-03-01 15:01:56 +08001820 rcu_read_lock();
1821 while (cgrp) {
1822 const char *name = cgroup_name(cgrp);
1823 int len;
1824
1825 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001827 goto out;
1828 memcpy(start, name, len);
1829
1830 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001832
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001834 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001836
1837 cgrp = cgrp->parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838 }
Li Zefan65dff752013-03-01 15:01:56 +08001839 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001840 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001841out:
1842 rcu_read_unlock();
1843 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001844}
Ben Blum67523c42010-03-10 15:22:11 -08001845EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001846
Ben Blum74a11662011-05-26 16:25:20 -07001847/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001848 * Control Group taskset
1849 */
Tejun Heo134d3372011-12-12 18:12:21 -08001850struct task_and_cgroup {
1851 struct task_struct *task;
1852 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001853 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001854};
1855
Tejun Heo2f7ee562011-12-12 18:12:21 -08001856struct cgroup_taskset {
1857 struct task_and_cgroup single;
1858 struct flex_array *tc_array;
1859 int tc_array_len;
1860 int idx;
1861 struct cgroup *cur_cgrp;
1862};
1863
1864/**
1865 * cgroup_taskset_first - reset taskset and return the first task
1866 * @tset: taskset of interest
1867 *
1868 * @tset iteration is initialized and the first task is returned.
1869 */
1870struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1871{
1872 if (tset->tc_array) {
1873 tset->idx = 0;
1874 return cgroup_taskset_next(tset);
1875 } else {
1876 tset->cur_cgrp = tset->single.cgrp;
1877 return tset->single.task;
1878 }
1879}
1880EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1881
1882/**
1883 * cgroup_taskset_next - iterate to the next task in taskset
1884 * @tset: taskset of interest
1885 *
1886 * Return the next task in @tset. Iteration must have been initialized
1887 * with cgroup_taskset_first().
1888 */
1889struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1890{
1891 struct task_and_cgroup *tc;
1892
1893 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1894 return NULL;
1895
1896 tc = flex_array_get(tset->tc_array, tset->idx++);
1897 tset->cur_cgrp = tc->cgrp;
1898 return tc->task;
1899}
1900EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1901
1902/**
1903 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1904 * @tset: taskset of interest
1905 *
1906 * Return the cgroup for the current (last returned) task of @tset. This
1907 * function must be preceded by either cgroup_taskset_first() or
1908 * cgroup_taskset_next().
1909 */
1910struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1911{
1912 return tset->cur_cgrp;
1913}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1915
1916/**
1917 * cgroup_taskset_size - return the number of tasks in taskset
1918 * @tset: taskset of interest
1919 */
1920int cgroup_taskset_size(struct cgroup_taskset *tset)
1921{
1922 return tset->tc_array ? tset->tc_array_len : 1;
1923}
1924EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1925
1926
Ben Blum74a11662011-05-26 16:25:20 -07001927/*
1928 * cgroup_task_migrate - move a task from one cgroup to another.
1929 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001930 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001932static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001933 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001934{
1935 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001936
1937 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001938 * We are synchronized through threadgroup_lock() against PF_EXITING
1939 * setting such that we can't race against cgroup_exit() changing the
1940 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001941 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001942 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001943 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001944
Ben Blum74a11662011-05-26 16:25:20 -07001945 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001946 rcu_assign_pointer(tsk->cgroups, newcg);
1947 task_unlock(tsk);
1948
1949 /* Update the css_set linked lists if we're using them */
1950 write_lock(&css_set_lock);
1951 if (!list_empty(&tsk->cg_list))
1952 list_move(&tsk->cg_list, &newcg->tasks);
1953 write_unlock(&css_set_lock);
1954
1955 /*
1956 * We just gained a reference on oldcg by taking it from the task. As
1957 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1958 * it here; it will be freed under RCU.
1959 */
Ben Blum74a11662011-05-26 16:25:20 -07001960 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001961 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001962}
1963
Li Zefana043e3b2008-02-23 15:24:09 -08001964/**
Li Zefan081aa452013-03-13 09:17:09 +08001965 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001966 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001967 * @tsk: the task or the leader of the threadgroup to be attached
1968 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001969 *
Tejun Heo257058a2011-12-12 18:12:21 -08001970 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001971 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
Li Zefan081aa452013-03-13 09:17:09 +08001973int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1974 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001975{
1976 int retval, i, group_size;
1977 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001978 struct cgroupfs_root *root = cgrp->root;
1979 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001980 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001981 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001982 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001983 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001984
1985 /*
1986 * step 0: in order to do expensive, possibly blocking operations for
1987 * every thread, we cannot iterate the thread group list, since it needs
1988 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001989 * group - group_rwsem prevents new threads from appearing, and if
1990 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001991 */
Li Zefan081aa452013-03-13 09:17:09 +08001992 if (threadgroup)
1993 group_size = get_nr_threads(tsk);
1994 else
1995 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001996 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001997 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001998 if (!group)
1999 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002000 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002001 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002002 if (retval)
2003 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002004
Ben Blum74a11662011-05-26 16:25:20 -07002005 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002006 /*
2007 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2008 * already PF_EXITING could be freed from underneath us unless we
2009 * take an rcu_read_lock.
2010 */
2011 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002012 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002013 struct task_and_cgroup ent;
2014
Tejun Heocd3d0952011-12-12 18:12:21 -08002015 /* @tsk either already exited or can't exit until the end */
2016 if (tsk->flags & PF_EXITING)
2017 continue;
2018
Ben Blum74a11662011-05-26 16:25:20 -07002019 /* as per above, nr_threads may decrease, but not increase. */
2020 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002021 ent.task = tsk;
2022 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002023 /* nothing to do if this task is already in the cgroup */
2024 if (ent.cgrp == cgrp)
2025 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002026 /*
2027 * saying GFP_ATOMIC has no effect here because we did prealloc
2028 * earlier, but it's good form to communicate our expectations.
2029 */
Tejun Heo134d3372011-12-12 18:12:21 -08002030 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002031 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002032 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002033
2034 if (!threadgroup)
2035 break;
Ben Blum74a11662011-05-26 16:25:20 -07002036 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002037 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002038 /* remember the number of threads in the array for later. */
2039 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002040 tset.tc_array = group;
2041 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002042
Tejun Heo134d3372011-12-12 18:12:21 -08002043 /* methods shouldn't be called if no task is actually migrating */
2044 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002045 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002046 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002047
Ben Blum74a11662011-05-26 16:25:20 -07002048 /*
2049 * step 1: check that we can legitimately attach to the cgroup.
2050 */
2051 for_each_subsys(root, ss) {
2052 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002053 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002054 if (retval) {
2055 failed_ss = ss;
2056 goto out_cancel_attach;
2057 }
2058 }
Ben Blum74a11662011-05-26 16:25:20 -07002059 }
2060
2061 /*
2062 * step 2: make sure css_sets exist for all threads to be migrated.
2063 * we use find_css_set, which allocates a new one if necessary.
2064 */
Ben Blum74a11662011-05-26 16:25:20 -07002065 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002066 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002067 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2068 if (!tc->cg) {
2069 retval = -ENOMEM;
2070 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002071 }
2072 }
2073
2074 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002075 * step 3: now that we're guaranteed success wrt the css_sets,
2076 * proceed to move all tasks to the new cgroup. There are no
2077 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002078 */
Ben Blum74a11662011-05-26 16:25:20 -07002079 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002080 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002081 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002082 }
2083 /* nothing is sensitive to fork() after this point. */
2084
2085 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002086 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002087 */
2088 for_each_subsys(root, ss) {
2089 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002090 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002091 }
2092
2093 /*
2094 * step 5: success! and cleanup
2095 */
Ben Blum74a11662011-05-26 16:25:20 -07002096 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097out_put_css_set_refs:
2098 if (retval) {
2099 for (i = 0; i < group_size; i++) {
2100 tc = flex_array_get(group, i);
2101 if (!tc->cg)
2102 break;
2103 put_css_set(tc->cg);
2104 }
Ben Blum74a11662011-05-26 16:25:20 -07002105 }
2106out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002107 if (retval) {
2108 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002109 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002110 break;
Ben Blum74a11662011-05-26 16:25:20 -07002111 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002112 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002113 }
2114 }
Ben Blum74a11662011-05-26 16:25:20 -07002115out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002116 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002117 return retval;
2118}
2119
2120/*
2121 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002122 * function to attach either it or all tasks in its threadgroup. Will lock
2123 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002124 */
2125static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002126{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002127 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002128 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 int ret;
2130
Ben Blum74a11662011-05-26 16:25:20 -07002131 if (!cgroup_lock_live_group(cgrp))
2132 return -ENODEV;
2133
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002134retry_find_task:
2135 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002137 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002138 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140 ret= -ESRCH;
2141 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 }
Ben Blum74a11662011-05-26 16:25:20 -07002143 /*
2144 * even if we're attaching all tasks in the thread group, we
2145 * only need to check permissions on one of them.
2146 */
David Howellsc69e8d92008-11-14 10:39:19 +11002147 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002148 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2149 !uid_eq(cred->euid, tcred->uid) &&
2150 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002151 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 ret = -EACCES;
2153 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002154 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 } else
2156 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002157
2158 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002159 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002160
2161 /*
2162 * Workqueue threads may acquire PF_THREAD_BOUND and become
2163 * trapped in a cpuset, or RT worker may be born in a cgroup
2164 * with no rt_runtime allocated. Just say no.
2165 */
2166 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2167 ret = -EINVAL;
2168 rcu_read_unlock();
2169 goto out_unlock_cgroup;
2170 }
2171
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 get_task_struct(tsk);
2173 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002174
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002175 threadgroup_lock(tsk);
2176 if (threadgroup) {
2177 if (!thread_group_leader(tsk)) {
2178 /*
2179 * a race with de_thread from another thread's exec()
2180 * may strip us of our leadership, if this happens,
2181 * there is no choice but to throw this task away and
2182 * try again; this is
2183 * "double-double-toil-and-trouble-check locking".
2184 */
2185 threadgroup_unlock(tsk);
2186 put_task_struct(tsk);
2187 goto retry_find_task;
2188 }
Li Zefan081aa452013-03-13 09:17:09 +08002189 }
2190
2191 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2192
Tejun Heocd3d0952011-12-12 18:12:21 -08002193 threadgroup_unlock(tsk);
2194
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002196out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002197 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002198 return ret;
2199}
2200
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002201/**
2202 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2203 * @from: attach to all cgroups of a given task
2204 * @tsk: the task to be attached
2205 */
2206int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2207{
2208 struct cgroupfs_root *root;
2209 int retval = 0;
2210
2211 cgroup_lock();
2212 for_each_active_root(root) {
2213 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2214
2215 retval = cgroup_attach_task(from_cg, tsk, false);
2216 if (retval)
2217 break;
2218 }
2219 cgroup_unlock();
2220
2221 return retval;
2222}
2223EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2224
Paul Menageaf351022008-07-25 01:47:01 -07002225static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2226{
Ben Blum74a11662011-05-26 16:25:20 -07002227 return attach_task_by_pid(cgrp, pid, false);
2228}
2229
2230static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2231{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002232 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002233}
2234
Paul Menagee788e062008-07-25 01:46:59 -07002235static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2236 const char *buffer)
2237{
2238 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002239 if (strlen(buffer) >= PATH_MAX)
2240 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002241 if (!cgroup_lock_live_group(cgrp))
2242 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002243 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002244 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002245 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002246 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002247 return 0;
2248}
2249
2250static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2251 struct seq_file *seq)
2252{
2253 if (!cgroup_lock_live_group(cgrp))
2254 return -ENODEV;
2255 seq_puts(seq, cgrp->root->release_agent_path);
2256 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002257 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002258 return 0;
2259}
2260
Paul Menage84eea842008-07-25 01:47:00 -07002261/* A buffer size big enough for numbers or short strings */
2262#define CGROUP_LOCAL_BUFFER_SIZE 64
2263
Paul Menagee73d2c62008-04-29 01:00:06 -07002264static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002265 struct file *file,
2266 const char __user *userbuf,
2267 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002268{
Paul Menage84eea842008-07-25 01:47:00 -07002269 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002270 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002271 char *end;
2272
2273 if (!nbytes)
2274 return -EINVAL;
2275 if (nbytes >= sizeof(buffer))
2276 return -E2BIG;
2277 if (copy_from_user(buffer, userbuf, nbytes))
2278 return -EFAULT;
2279
2280 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002281 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002282 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002283 if (*end)
2284 return -EINVAL;
2285 retval = cft->write_u64(cgrp, cft, val);
2286 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002287 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002288 if (*end)
2289 return -EINVAL;
2290 retval = cft->write_s64(cgrp, cft, val);
2291 }
Paul Menage355e0c42007-10-18 23:39:33 -07002292 if (!retval)
2293 retval = nbytes;
2294 return retval;
2295}
2296
Paul Menagedb3b1492008-07-25 01:46:58 -07002297static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2298 struct file *file,
2299 const char __user *userbuf,
2300 size_t nbytes, loff_t *unused_ppos)
2301{
Paul Menage84eea842008-07-25 01:47:00 -07002302 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002303 int retval = 0;
2304 size_t max_bytes = cft->max_write_len;
2305 char *buffer = local_buffer;
2306
2307 if (!max_bytes)
2308 max_bytes = sizeof(local_buffer) - 1;
2309 if (nbytes >= max_bytes)
2310 return -E2BIG;
2311 /* Allocate a dynamic buffer if we need one */
2312 if (nbytes >= sizeof(local_buffer)) {
2313 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2314 if (buffer == NULL)
2315 return -ENOMEM;
2316 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002317 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2318 retval = -EFAULT;
2319 goto out;
2320 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002321
2322 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002324 if (!retval)
2325 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002326out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002327 if (buffer != local_buffer)
2328 kfree(buffer);
2329 return retval;
2330}
2331
Paul Menageddbcc7e2007-10-18 23:39:30 -07002332static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2333 size_t nbytes, loff_t *ppos)
2334{
2335 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002336 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002337
Li Zefan75139b82009-01-07 18:07:33 -08002338 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002340 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002341 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002342 if (cft->write_u64 || cft->write_s64)
2343 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 if (cft->write_string)
2345 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002346 if (cft->trigger) {
2347 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2348 return ret ? ret : nbytes;
2349 }
Paul Menage355e0c42007-10-18 23:39:33 -07002350 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351}
2352
Paul Menagef4c753b2008-04-29 00:59:56 -07002353static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2354 struct file *file,
2355 char __user *buf, size_t nbytes,
2356 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357{
Paul Menage84eea842008-07-25 01:47:00 -07002358 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002359 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2361
2362 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2363}
2364
Paul Menagee73d2c62008-04-29 01:00:06 -07002365static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2366 struct file *file,
2367 char __user *buf, size_t nbytes,
2368 loff_t *ppos)
2369{
Paul Menage84eea842008-07-25 01:47:00 -07002370 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002371 s64 val = cft->read_s64(cgrp, cft);
2372 int len = sprintf(tmp, "%lld\n", (long long) val);
2373
2374 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2375}
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2378 size_t nbytes, loff_t *ppos)
2379{
2380 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382
Li Zefan75139b82009-01-07 18:07:33 -08002383 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384 return -ENODEV;
2385
2386 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002387 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002388 if (cft->read_u64)
2389 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002390 if (cft->read_s64)
2391 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return -EINVAL;
2393}
2394
Paul Menage91796562008-04-29 01:00:01 -07002395/*
2396 * seqfile ops/methods for returning structured data. Currently just
2397 * supports string->u64 maps, but can be extended in future.
2398 */
2399
2400struct cgroup_seqfile_state {
2401 struct cftype *cft;
2402 struct cgroup *cgroup;
2403};
2404
2405static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2406{
2407 struct seq_file *sf = cb->state;
2408 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2409}
2410
2411static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2412{
2413 struct cgroup_seqfile_state *state = m->private;
2414 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002415 if (cft->read_map) {
2416 struct cgroup_map_cb cb = {
2417 .fill = cgroup_map_add,
2418 .state = m,
2419 };
2420 return cft->read_map(state->cgroup, cft, &cb);
2421 }
2422 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002423}
2424
Adrian Bunk96930a62008-07-25 19:46:21 -07002425static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002426{
2427 struct seq_file *seq = file->private_data;
2428 kfree(seq->private);
2429 return single_release(inode, file);
2430}
2431
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002432static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002433 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002434 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002435 .llseek = seq_lseek,
2436 .release = cgroup_seqfile_release,
2437};
2438
Paul Menageddbcc7e2007-10-18 23:39:30 -07002439static int cgroup_file_open(struct inode *inode, struct file *file)
2440{
2441 int err;
2442 struct cftype *cft;
2443
2444 err = generic_file_open(inode, file);
2445 if (err)
2446 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002447 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002448
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002449 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002450 struct cgroup_seqfile_state *state =
2451 kzalloc(sizeof(*state), GFP_USER);
2452 if (!state)
2453 return -ENOMEM;
2454 state->cft = cft;
2455 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2456 file->f_op = &cgroup_seqfile_operations;
2457 err = single_open(file, cgroup_seqfile_show, state);
2458 if (err < 0)
2459 kfree(state);
2460 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461 err = cft->open(inode, file);
2462 else
2463 err = 0;
2464
2465 return err;
2466}
2467
2468static int cgroup_file_release(struct inode *inode, struct file *file)
2469{
2470 struct cftype *cft = __d_cft(file->f_dentry);
2471 if (cft->release)
2472 return cft->release(inode, file);
2473 return 0;
2474}
2475
2476/*
2477 * cgroup_rename - Only allow simple rename of directories in place.
2478 */
2479static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2480 struct inode *new_dir, struct dentry *new_dentry)
2481{
Li Zefan65dff752013-03-01 15:01:56 +08002482 int ret;
2483 struct cgroup_name *name, *old_name;
2484 struct cgroup *cgrp;
2485
2486 /*
2487 * It's convinient to use parent dir's i_mutex to protected
2488 * cgrp->name.
2489 */
2490 lockdep_assert_held(&old_dir->i_mutex);
2491
Paul Menageddbcc7e2007-10-18 23:39:30 -07002492 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2493 return -ENOTDIR;
2494 if (new_dentry->d_inode)
2495 return -EEXIST;
2496 if (old_dir != new_dir)
2497 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002498
2499 cgrp = __d_cgrp(old_dentry);
2500
2501 name = cgroup_alloc_name(new_dentry);
2502 if (!name)
2503 return -ENOMEM;
2504
2505 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2506 if (ret) {
2507 kfree(name);
2508 return ret;
2509 }
2510
2511 old_name = cgrp->name;
2512 rcu_assign_pointer(cgrp->name, name);
2513
2514 kfree_rcu(old_name, rcu_head);
2515 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516}
2517
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002518static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2519{
2520 if (S_ISDIR(dentry->d_inode->i_mode))
2521 return &__d_cgrp(dentry)->xattrs;
2522 else
2523 return &__d_cft(dentry)->xattrs;
2524}
2525
2526static inline int xattr_enabled(struct dentry *dentry)
2527{
2528 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2529 return test_bit(ROOT_XATTR, &root->flags);
2530}
2531
2532static bool is_valid_xattr(const char *name)
2533{
2534 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2535 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2536 return true;
2537 return false;
2538}
2539
2540static int cgroup_setxattr(struct dentry *dentry, const char *name,
2541 const void *val, size_t size, int flags)
2542{
2543 if (!xattr_enabled(dentry))
2544 return -EOPNOTSUPP;
2545 if (!is_valid_xattr(name))
2546 return -EINVAL;
2547 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2548}
2549
2550static int cgroup_removexattr(struct dentry *dentry, const char *name)
2551{
2552 if (!xattr_enabled(dentry))
2553 return -EOPNOTSUPP;
2554 if (!is_valid_xattr(name))
2555 return -EINVAL;
2556 return simple_xattr_remove(__d_xattrs(dentry), name);
2557}
2558
2559static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2560 void *buf, size_t size)
2561{
2562 if (!xattr_enabled(dentry))
2563 return -EOPNOTSUPP;
2564 if (!is_valid_xattr(name))
2565 return -EINVAL;
2566 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2567}
2568
2569static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2574}
2575
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002576static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002577 .read = cgroup_file_read,
2578 .write = cgroup_file_write,
2579 .llseek = generic_file_llseek,
2580 .open = cgroup_file_open,
2581 .release = cgroup_file_release,
2582};
2583
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002584static const struct inode_operations cgroup_file_inode_operations = {
2585 .setxattr = cgroup_setxattr,
2586 .getxattr = cgroup_getxattr,
2587 .listxattr = cgroup_listxattr,
2588 .removexattr = cgroup_removexattr,
2589};
2590
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002591static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002592 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002593 .mkdir = cgroup_mkdir,
2594 .rmdir = cgroup_rmdir,
2595 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002596 .setxattr = cgroup_setxattr,
2597 .getxattr = cgroup_getxattr,
2598 .listxattr = cgroup_listxattr,
2599 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600};
2601
Al Viro00cd8dd2012-06-10 17:13:09 -04002602static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002603{
2604 if (dentry->d_name.len > NAME_MAX)
2605 return ERR_PTR(-ENAMETOOLONG);
2606 d_add(dentry, NULL);
2607 return NULL;
2608}
2609
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002610/*
2611 * Check if a file is a control file
2612 */
2613static inline struct cftype *__file_cft(struct file *file)
2614{
Al Viro496ad9a2013-01-23 17:07:38 -05002615 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002616 return ERR_PTR(-EINVAL);
2617 return __d_cft(file->f_dentry);
2618}
2619
Al Viroa5e7ed32011-07-26 01:55:55 -04002620static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002621 struct super_block *sb)
2622{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623 struct inode *inode;
2624
2625 if (!dentry)
2626 return -ENOENT;
2627 if (dentry->d_inode)
2628 return -EEXIST;
2629
2630 inode = cgroup_new_inode(mode, sb);
2631 if (!inode)
2632 return -ENOMEM;
2633
2634 if (S_ISDIR(mode)) {
2635 inode->i_op = &cgroup_dir_inode_operations;
2636 inode->i_fop = &simple_dir_operations;
2637
2638 /* start off with i_nlink == 2 (for "." entry) */
2639 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002640 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641
Tejun Heob8a2df62012-11-19 08:13:37 -08002642 /*
2643 * Control reaches here with cgroup_mutex held.
2644 * @inode->i_mutex should nest outside cgroup_mutex but we
2645 * want to populate it immediately without releasing
2646 * cgroup_mutex. As @inode isn't visible to anyone else
2647 * yet, trylock will always succeed without affecting
2648 * lockdep checks.
2649 */
2650 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651 } else if (S_ISREG(mode)) {
2652 inode->i_size = 0;
2653 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002654 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002655 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002656 d_instantiate(dentry, inode);
2657 dget(dentry); /* Extra count - pin the dentry in core */
2658 return 0;
2659}
2660
Li Zefan099fca32009-04-02 16:57:29 -07002661/**
2662 * cgroup_file_mode - deduce file mode of a control file
2663 * @cft: the control file in question
2664 *
2665 * returns cft->mode if ->mode is not 0
2666 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2667 * returns S_IRUGO if it has only a read handler
2668 * returns S_IWUSR if it has only a write hander
2669 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002670static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002671{
Al Viroa5e7ed32011-07-26 01:55:55 -04002672 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002673
2674 if (cft->mode)
2675 return cft->mode;
2676
2677 if (cft->read || cft->read_u64 || cft->read_s64 ||
2678 cft->read_map || cft->read_seq_string)
2679 mode |= S_IRUGO;
2680
2681 if (cft->write || cft->write_u64 || cft->write_s64 ||
2682 cft->write_string || cft->trigger)
2683 mode |= S_IWUSR;
2684
2685 return mode;
2686}
2687
Tejun Heodb0416b2012-04-01 12:09:55 -07002688static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002689 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690{
Paul Menagebd89aab2007-10-18 23:40:44 -07002691 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002692 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002694 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002696 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002697 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002698
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002699 simple_xattrs_init(&cft->xattrs);
2700
Paul Menagebd89aab2007-10-18 23:40:44 -07002701 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702 strcpy(name, subsys->name);
2703 strcat(name, ".");
2704 }
2705 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002706
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002708
2709 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2710 if (!cfe)
2711 return -ENOMEM;
2712
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002714 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002716 goto out;
2717 }
2718
2719 mode = cgroup_file_mode(cft);
2720 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2721 if (!error) {
2722 cfe->type = (void *)cft;
2723 cfe->dentry = dentry;
2724 dentry->d_fsdata = cfe;
2725 list_add_tail(&cfe->node, &parent->files);
2726 cfe = NULL;
2727 }
2728 dput(dentry);
2729out:
2730 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 return error;
2732}
2733
Tejun Heo79578622012-04-01 12:09:56 -07002734static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002735 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002737 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002738 int err, ret = 0;
2739
2740 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002741 /* does cft->flags tell us to skip this file on @cgrp? */
2742 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2743 continue;
2744 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2745 continue;
2746
Li Zefan2739d3c2013-01-21 18:18:33 +08002747 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002748 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002749 if (err)
2750 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2751 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002752 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002753 } else {
2754 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002755 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002757 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758}
2759
Tejun Heo8e3f6542012-04-01 12:09:55 -07002760static DEFINE_MUTEX(cgroup_cft_mutex);
2761
2762static void cgroup_cfts_prepare(void)
2763 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2764{
2765 /*
2766 * Thanks to the entanglement with vfs inode locking, we can't walk
2767 * the existing cgroups under cgroup_mutex and create files.
2768 * Instead, we increment reference on all cgroups and build list of
2769 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2770 * exclusive access to the field.
2771 */
2772 mutex_lock(&cgroup_cft_mutex);
2773 mutex_lock(&cgroup_mutex);
2774}
2775
2776static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002777 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002778 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2779{
2780 LIST_HEAD(pending);
2781 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002782
2783 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2784 if (cfts && ss->root != &rootnode) {
2785 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2786 dget(cgrp->dentry);
2787 list_add_tail(&cgrp->cft_q_node, &pending);
2788 }
2789 }
2790
2791 mutex_unlock(&cgroup_mutex);
2792
2793 /*
2794 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2795 * files for all cgroups which were created before.
2796 */
2797 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2798 struct inode *inode = cgrp->dentry->d_inode;
2799
2800 mutex_lock(&inode->i_mutex);
2801 mutex_lock(&cgroup_mutex);
2802 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002803 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804 mutex_unlock(&cgroup_mutex);
2805 mutex_unlock(&inode->i_mutex);
2806
2807 list_del_init(&cgrp->cft_q_node);
2808 dput(cgrp->dentry);
2809 }
2810
2811 mutex_unlock(&cgroup_cft_mutex);
2812}
2813
2814/**
2815 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2816 * @ss: target cgroup subsystem
2817 * @cfts: zero-length name terminated array of cftypes
2818 *
2819 * Register @cfts to @ss. Files described by @cfts are created for all
2820 * existing cgroups to which @ss is attached and all future cgroups will
2821 * have them too. This function can be called anytime whether @ss is
2822 * attached or not.
2823 *
2824 * Returns 0 on successful registration, -errno on failure. Note that this
2825 * function currently returns 0 as long as @cfts registration is successful
2826 * even if some file creation attempts on existing cgroups fail.
2827 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002828int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829{
2830 struct cftype_set *set;
2831
2832 set = kzalloc(sizeof(*set), GFP_KERNEL);
2833 if (!set)
2834 return -ENOMEM;
2835
2836 cgroup_cfts_prepare();
2837 set->cfts = cfts;
2838 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002839 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840
2841 return 0;
2842}
2843EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2844
Li Zefana043e3b2008-02-23 15:24:09 -08002845/**
Tejun Heo79578622012-04-01 12:09:56 -07002846 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2847 * @ss: target cgroup subsystem
2848 * @cfts: zero-length name terminated array of cftypes
2849 *
2850 * Unregister @cfts from @ss. Files described by @cfts are removed from
2851 * all existing cgroups to which @ss is attached and all future cgroups
2852 * won't have them either. This function can be called anytime whether @ss
2853 * is attached or not.
2854 *
2855 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2856 * registered with @ss.
2857 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002858int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002859{
2860 struct cftype_set *set;
2861
2862 cgroup_cfts_prepare();
2863
2864 list_for_each_entry(set, &ss->cftsets, node) {
2865 if (set->cfts == cfts) {
2866 list_del_init(&set->node);
2867 cgroup_cfts_commit(ss, cfts, false);
2868 return 0;
2869 }
2870 }
2871
2872 cgroup_cfts_commit(ss, NULL, false);
2873 return -ENOENT;
2874}
2875
2876/**
Li Zefana043e3b2008-02-23 15:24:09 -08002877 * cgroup_task_count - count the number of tasks in a cgroup.
2878 * @cgrp: the cgroup in question
2879 *
2880 * Return the number of tasks in the cgroup.
2881 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002882int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002883{
2884 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002885 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002886
Paul Menage817929e2007-10-18 23:39:36 -07002887 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002888 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002889 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002890 }
2891 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002892 return count;
2893}
2894
2895/*
Paul Menage817929e2007-10-18 23:39:36 -07002896 * Advance a list_head iterator. The iterator should be positioned at
2897 * the start of a css_set
2898 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002899static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002900 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002901{
2902 struct list_head *l = it->cg_link;
2903 struct cg_cgroup_link *link;
2904 struct css_set *cg;
2905
2906 /* Advance to the next non-empty css_set */
2907 do {
2908 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002909 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002910 it->cg_link = NULL;
2911 return;
2912 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002913 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002914 cg = link->cg;
2915 } while (list_empty(&cg->tasks));
2916 it->cg_link = l;
2917 it->task = cg->tasks.next;
2918}
2919
Cliff Wickman31a7df02008-02-07 00:14:42 -08002920/*
2921 * To reduce the fork() overhead for systems that are not actually
2922 * using their cgroups capability, we don't maintain the lists running
2923 * through each css_set to its tasks until we see the list actually
2924 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002925 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002926static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002927{
2928 struct task_struct *p, *g;
2929 write_lock(&css_set_lock);
2930 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002931 /*
2932 * We need tasklist_lock because RCU is not safe against
2933 * while_each_thread(). Besides, a forking task that has passed
2934 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2935 * is not guaranteed to have its child immediately visible in the
2936 * tasklist if we walk through it with RCU.
2937 */
2938 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002939 do_each_thread(g, p) {
2940 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002941 /*
2942 * We should check if the process is exiting, otherwise
2943 * it will race with cgroup_exit() in that the list
2944 * entry won't be deleted though the process has exited.
2945 */
2946 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002947 list_add(&p->cg_list, &p->cgroups->tasks);
2948 task_unlock(p);
2949 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002950 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002951 write_unlock(&css_set_lock);
2952}
2953
Tejun Heo574bd9f2012-11-09 09:12:29 -08002954/**
2955 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2956 * @pos: the current position (%NULL to initiate traversal)
2957 * @cgroup: cgroup whose descendants to walk
2958 *
2959 * To be used by cgroup_for_each_descendant_pre(). Find the next
2960 * descendant to visit for pre-order traversal of @cgroup's descendants.
2961 */
2962struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2963 struct cgroup *cgroup)
2964{
2965 struct cgroup *next;
2966
2967 WARN_ON_ONCE(!rcu_read_lock_held());
2968
2969 /* if first iteration, pretend we just visited @cgroup */
2970 if (!pos) {
2971 if (list_empty(&cgroup->children))
2972 return NULL;
2973 pos = cgroup;
2974 }
2975
2976 /* visit the first child if exists */
2977 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2978 if (next)
2979 return next;
2980
2981 /* no child, visit my or the closest ancestor's next sibling */
2982 do {
2983 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2984 sibling);
2985 if (&next->sibling != &pos->parent->children)
2986 return next;
2987
2988 pos = pos->parent;
2989 } while (pos != cgroup);
2990
2991 return NULL;
2992}
2993EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2994
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002995/**
2996 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2997 * @pos: cgroup of interest
2998 *
2999 * Return the rightmost descendant of @pos. If there's no descendant,
3000 * @pos is returned. This can be used during pre-order traversal to skip
3001 * subtree of @pos.
3002 */
3003struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3004{
3005 struct cgroup *last, *tmp;
3006
3007 WARN_ON_ONCE(!rcu_read_lock_held());
3008
3009 do {
3010 last = pos;
3011 /* ->prev isn't RCU safe, walk ->next till the end */
3012 pos = NULL;
3013 list_for_each_entry_rcu(tmp, &last->children, sibling)
3014 pos = tmp;
3015 } while (pos);
3016
3017 return last;
3018}
3019EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3020
Tejun Heo574bd9f2012-11-09 09:12:29 -08003021static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3022{
3023 struct cgroup *last;
3024
3025 do {
3026 last = pos;
3027 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3028 sibling);
3029 } while (pos);
3030
3031 return last;
3032}
3033
3034/**
3035 * cgroup_next_descendant_post - find the next descendant for post-order walk
3036 * @pos: the current position (%NULL to initiate traversal)
3037 * @cgroup: cgroup whose descendants to walk
3038 *
3039 * To be used by cgroup_for_each_descendant_post(). Find the next
3040 * descendant to visit for post-order traversal of @cgroup's descendants.
3041 */
3042struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3043 struct cgroup *cgroup)
3044{
3045 struct cgroup *next;
3046
3047 WARN_ON_ONCE(!rcu_read_lock_held());
3048
3049 /* if first iteration, visit the leftmost descendant */
3050 if (!pos) {
3051 next = cgroup_leftmost_descendant(cgroup);
3052 return next != cgroup ? next : NULL;
3053 }
3054
3055 /* if there's an unvisited sibling, visit its leftmost descendant */
3056 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3057 if (&next->sibling != &pos->parent->children)
3058 return cgroup_leftmost_descendant(next);
3059
3060 /* no sibling left, visit parent */
3061 next = pos->parent;
3062 return next != cgroup ? next : NULL;
3063}
3064EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3065
Paul Menagebd89aab2007-10-18 23:40:44 -07003066void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003067 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003068{
3069 /*
3070 * The first time anyone tries to iterate across a cgroup,
3071 * we need to enable the list linking each css_set to its
3072 * tasks, and fix up all existing tasks.
3073 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003074 if (!use_task_css_set_links)
3075 cgroup_enable_task_cg_lists();
3076
Paul Menage817929e2007-10-18 23:39:36 -07003077 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003078 it->cg_link = &cgrp->css_sets;
3079 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003080}
3081
Paul Menagebd89aab2007-10-18 23:40:44 -07003082struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003083 struct cgroup_iter *it)
3084{
3085 struct task_struct *res;
3086 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003087 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003088
3089 /* If the iterator cg is NULL, we have no tasks */
3090 if (!it->cg_link)
3091 return NULL;
3092 res = list_entry(l, struct task_struct, cg_list);
3093 /* Advance iterator to find next entry */
3094 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003095 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3096 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003097 /* We reached the end of this task list - move on to
3098 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003099 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003100 } else {
3101 it->task = l;
3102 }
3103 return res;
3104}
3105
Paul Menagebd89aab2007-10-18 23:40:44 -07003106void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003107 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003108{
3109 read_unlock(&css_set_lock);
3110}
3111
Cliff Wickman31a7df02008-02-07 00:14:42 -08003112static inline int started_after_time(struct task_struct *t1,
3113 struct timespec *time,
3114 struct task_struct *t2)
3115{
3116 int start_diff = timespec_compare(&t1->start_time, time);
3117 if (start_diff > 0) {
3118 return 1;
3119 } else if (start_diff < 0) {
3120 return 0;
3121 } else {
3122 /*
3123 * Arbitrarily, if two processes started at the same
3124 * time, we'll say that the lower pointer value
3125 * started first. Note that t2 may have exited by now
3126 * so this may not be a valid pointer any longer, but
3127 * that's fine - it still serves to distinguish
3128 * between two tasks started (effectively) simultaneously.
3129 */
3130 return t1 > t2;
3131 }
3132}
3133
3134/*
3135 * This function is a callback from heap_insert() and is used to order
3136 * the heap.
3137 * In this case we order the heap in descending task start time.
3138 */
3139static inline int started_after(void *p1, void *p2)
3140{
3141 struct task_struct *t1 = p1;
3142 struct task_struct *t2 = p2;
3143 return started_after_time(t1, &t2->start_time, t2);
3144}
3145
3146/**
3147 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3148 * @scan: struct cgroup_scanner containing arguments for the scan
3149 *
3150 * Arguments include pointers to callback functions test_task() and
3151 * process_task().
3152 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3153 * and if it returns true, call process_task() for it also.
3154 * The test_task pointer may be NULL, meaning always true (select all tasks).
3155 * Effectively duplicates cgroup_iter_{start,next,end}()
3156 * but does not lock css_set_lock for the call to process_task().
3157 * The struct cgroup_scanner may be embedded in any structure of the caller's
3158 * creation.
3159 * It is guaranteed that process_task() will act on every task that
3160 * is a member of the cgroup for the duration of this call. This
3161 * function may or may not call process_task() for tasks that exit
3162 * or move to a different cgroup during the call, or are forked or
3163 * move into the cgroup during the call.
3164 *
3165 * Note that test_task() may be called with locks held, and may in some
3166 * situations be called multiple times for the same task, so it should
3167 * be cheap.
3168 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3169 * pre-allocated and will be used for heap operations (and its "gt" member will
3170 * be overwritten), else a temporary heap will be used (allocation of which
3171 * may cause this function to fail).
3172 */
3173int cgroup_scan_tasks(struct cgroup_scanner *scan)
3174{
3175 int retval, i;
3176 struct cgroup_iter it;
3177 struct task_struct *p, *dropped;
3178 /* Never dereference latest_task, since it's not refcounted */
3179 struct task_struct *latest_task = NULL;
3180 struct ptr_heap tmp_heap;
3181 struct ptr_heap *heap;
3182 struct timespec latest_time = { 0, 0 };
3183
3184 if (scan->heap) {
3185 /* The caller supplied our heap and pre-allocated its memory */
3186 heap = scan->heap;
3187 heap->gt = &started_after;
3188 } else {
3189 /* We need to allocate our own heap memory */
3190 heap = &tmp_heap;
3191 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3192 if (retval)
3193 /* cannot allocate the heap */
3194 return retval;
3195 }
3196
3197 again:
3198 /*
3199 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3200 * to determine which are of interest, and using the scanner's
3201 * "process_task" callback to process any of them that need an update.
3202 * Since we don't want to hold any locks during the task updates,
3203 * gather tasks to be processed in a heap structure.
3204 * The heap is sorted by descending task start time.
3205 * If the statically-sized heap fills up, we overflow tasks that
3206 * started later, and in future iterations only consider tasks that
3207 * started after the latest task in the previous pass. This
3208 * guarantees forward progress and that we don't miss any tasks.
3209 */
3210 heap->size = 0;
3211 cgroup_iter_start(scan->cg, &it);
3212 while ((p = cgroup_iter_next(scan->cg, &it))) {
3213 /*
3214 * Only affect tasks that qualify per the caller's callback,
3215 * if he provided one
3216 */
3217 if (scan->test_task && !scan->test_task(p, scan))
3218 continue;
3219 /*
3220 * Only process tasks that started after the last task
3221 * we processed
3222 */
3223 if (!started_after_time(p, &latest_time, latest_task))
3224 continue;
3225 dropped = heap_insert(heap, p);
3226 if (dropped == NULL) {
3227 /*
3228 * The new task was inserted; the heap wasn't
3229 * previously full
3230 */
3231 get_task_struct(p);
3232 } else if (dropped != p) {
3233 /*
3234 * The new task was inserted, and pushed out a
3235 * different task
3236 */
3237 get_task_struct(p);
3238 put_task_struct(dropped);
3239 }
3240 /*
3241 * Else the new task was newer than anything already in
3242 * the heap and wasn't inserted
3243 */
3244 }
3245 cgroup_iter_end(scan->cg, &it);
3246
3247 if (heap->size) {
3248 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003249 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003250 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003251 latest_time = q->start_time;
3252 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003253 }
3254 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003255 scan->process_task(q, scan);
3256 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003257 }
3258 /*
3259 * If we had to process any tasks at all, scan again
3260 * in case some of them were in the middle of forking
3261 * children that didn't get processed.
3262 * Not the most efficient way to do it, but it avoids
3263 * having to take callback_mutex in the fork path
3264 */
3265 goto again;
3266 }
3267 if (heap == &tmp_heap)
3268 heap_free(&tmp_heap);
3269 return 0;
3270}
3271
Tejun Heo8cc99342013-04-07 09:29:50 -07003272static void cgroup_transfer_one_task(struct task_struct *task,
3273 struct cgroup_scanner *scan)
3274{
3275 struct cgroup *new_cgroup = scan->data;
3276
3277 cgroup_lock();
3278 cgroup_attach_task(new_cgroup, task, false);
3279 cgroup_unlock();
3280}
3281
3282/**
3283 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3284 * @to: cgroup to which the tasks will be moved
3285 * @from: cgroup in which the tasks currently reside
3286 */
3287int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3288{
3289 struct cgroup_scanner scan;
3290
3291 scan.cg = from;
3292 scan.test_task = NULL; /* select all tasks in cgroup */
3293 scan.process_task = cgroup_transfer_one_task;
3294 scan.heap = NULL;
3295 scan.data = to;
3296
3297 return cgroup_scan_tasks(&scan);
3298}
3299
Paul Menage817929e2007-10-18 23:39:36 -07003300/*
Ben Blum102a7752009-09-23 15:56:26 -07003301 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003302 *
3303 * Reading this file can return large amounts of data if a cgroup has
3304 * *lots* of attached tasks. So it may need several calls to read(),
3305 * but we cannot guarantee that the information we produce is correct
3306 * unless we produce it entirely atomically.
3307 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003308 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003309
Li Zefan24528252012-01-20 11:58:43 +08003310/* which pidlist file are we talking about? */
3311enum cgroup_filetype {
3312 CGROUP_FILE_PROCS,
3313 CGROUP_FILE_TASKS,
3314};
3315
3316/*
3317 * A pidlist is a list of pids that virtually represents the contents of one
3318 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3319 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3320 * to the cgroup.
3321 */
3322struct cgroup_pidlist {
3323 /*
3324 * used to find which pidlist is wanted. doesn't change as long as
3325 * this particular list stays in the list.
3326 */
3327 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3328 /* array of xids */
3329 pid_t *list;
3330 /* how many elements the above list has */
3331 int length;
3332 /* how many files are using the current array */
3333 int use_count;
3334 /* each of these stored in a list by its cgroup */
3335 struct list_head links;
3336 /* pointer to the cgroup we belong to, for list removal purposes */
3337 struct cgroup *owner;
3338 /* protects the other fields */
3339 struct rw_semaphore mutex;
3340};
3341
Paul Menagebbcb81d2007-10-18 23:39:32 -07003342/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003343 * The following two functions "fix" the issue where there are more pids
3344 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3345 * TODO: replace with a kernel-wide solution to this problem
3346 */
3347#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3348static void *pidlist_allocate(int count)
3349{
3350 if (PIDLIST_TOO_LARGE(count))
3351 return vmalloc(count * sizeof(pid_t));
3352 else
3353 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3354}
3355static void pidlist_free(void *p)
3356{
3357 if (is_vmalloc_addr(p))
3358 vfree(p);
3359 else
3360 kfree(p);
3361}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003362
3363/*
Ben Blum102a7752009-09-23 15:56:26 -07003364 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003365 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003366 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003367static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003368{
Ben Blum102a7752009-09-23 15:56:26 -07003369 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003370
3371 /*
3372 * we presume the 0th element is unique, so i starts at 1. trivial
3373 * edge cases first; no work needs to be done for either
3374 */
3375 if (length == 0 || length == 1)
3376 return length;
3377 /* src and dest walk down the list; dest counts unique elements */
3378 for (src = 1; src < length; src++) {
3379 /* find next unique element */
3380 while (list[src] == list[src-1]) {
3381 src++;
3382 if (src == length)
3383 goto after;
3384 }
3385 /* dest always points to where the next unique element goes */
3386 list[dest] = list[src];
3387 dest++;
3388 }
3389after:
Ben Blum102a7752009-09-23 15:56:26 -07003390 return dest;
3391}
3392
3393static int cmppid(const void *a, const void *b)
3394{
3395 return *(pid_t *)a - *(pid_t *)b;
3396}
3397
3398/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003399 * find the appropriate pidlist for our purpose (given procs vs tasks)
3400 * returns with the lock on that pidlist already held, and takes care
3401 * of the use count, or returns NULL with no locks held if we're out of
3402 * memory.
3403 */
3404static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3405 enum cgroup_filetype type)
3406{
3407 struct cgroup_pidlist *l;
3408 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003409 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003410
Ben Blum72a8cb32009-09-23 15:56:27 -07003411 /*
3412 * We can't drop the pidlist_mutex before taking the l->mutex in case
3413 * the last ref-holder is trying to remove l from the list at the same
3414 * time. Holding the pidlist_mutex precludes somebody taking whichever
3415 * list we find out from under us - compare release_pid_array().
3416 */
3417 mutex_lock(&cgrp->pidlist_mutex);
3418 list_for_each_entry(l, &cgrp->pidlists, links) {
3419 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003420 /* make sure l doesn't vanish out from under us */
3421 down_write(&l->mutex);
3422 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003423 return l;
3424 }
3425 }
3426 /* entry not found; create a new one */
3427 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3428 if (!l) {
3429 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003430 return l;
3431 }
3432 init_rwsem(&l->mutex);
3433 down_write(&l->mutex);
3434 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003435 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003436 l->use_count = 0; /* don't increment here */
3437 l->list = NULL;
3438 l->owner = cgrp;
3439 list_add(&l->links, &cgrp->pidlists);
3440 mutex_unlock(&cgrp->pidlist_mutex);
3441 return l;
3442}
3443
3444/*
Ben Blum102a7752009-09-23 15:56:26 -07003445 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3446 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003447static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3448 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003449{
3450 pid_t *array;
3451 int length;
3452 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003453 struct cgroup_iter it;
3454 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003455 struct cgroup_pidlist *l;
3456
3457 /*
3458 * If cgroup gets more users after we read count, we won't have
3459 * enough space - tough. This race is indistinguishable to the
3460 * caller from the case that the additional cgroup users didn't
3461 * show up until sometime later on.
3462 */
3463 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003464 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003465 if (!array)
3466 return -ENOMEM;
3467 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003468 cgroup_iter_start(cgrp, &it);
3469 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003470 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003471 break;
Ben Blum102a7752009-09-23 15:56:26 -07003472 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003473 if (type == CGROUP_FILE_PROCS)
3474 pid = task_tgid_vnr(tsk);
3475 else
3476 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003477 if (pid > 0) /* make sure to only use valid results */
3478 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003479 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003480 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003481 length = n;
3482 /* now sort & (if procs) strip out duplicates */
3483 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003484 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003485 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003486 l = cgroup_pidlist_find(cgrp, type);
3487 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003488 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003489 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003490 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003491 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003492 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003493 l->list = array;
3494 l->length = length;
3495 l->use_count++;
3496 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003497 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003498 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003499}
3500
Balbir Singh846c7bb2007-10-18 23:39:44 -07003501/**
Li Zefana043e3b2008-02-23 15:24:09 -08003502 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503 * @stats: cgroupstats to fill information into
3504 * @dentry: A dentry entry belonging to the cgroup for which stats have
3505 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003506 *
3507 * Build and fill cgroupstats so that taskstats can export it to user
3508 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003509 */
3510int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3511{
3512 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003513 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003514 struct cgroup_iter it;
3515 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003516
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003518 * Validate dentry by checking the superblock operations,
3519 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520 */
Li Zefan33d283b2008-11-19 15:36:48 -08003521 if (dentry->d_sb->s_op != &cgroup_ops ||
3522 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003523 goto err;
3524
3525 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003526 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003527
Paul Menagebd89aab2007-10-18 23:40:44 -07003528 cgroup_iter_start(cgrp, &it);
3529 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003530 switch (tsk->state) {
3531 case TASK_RUNNING:
3532 stats->nr_running++;
3533 break;
3534 case TASK_INTERRUPTIBLE:
3535 stats->nr_sleeping++;
3536 break;
3537 case TASK_UNINTERRUPTIBLE:
3538 stats->nr_uninterruptible++;
3539 break;
3540 case TASK_STOPPED:
3541 stats->nr_stopped++;
3542 break;
3543 default:
3544 if (delayacct_is_task_waiting_on_io(tsk))
3545 stats->nr_io_wait++;
3546 break;
3547 }
3548 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003549 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003550
Balbir Singh846c7bb2007-10-18 23:39:44 -07003551err:
3552 return ret;
3553}
3554
Paul Menage8f3ff202009-09-23 15:56:25 -07003555
Paul Menagecc31edc2008-10-18 20:28:04 -07003556/*
Ben Blum102a7752009-09-23 15:56:26 -07003557 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003558 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003559 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003560 */
3561
Ben Blum102a7752009-09-23 15:56:26 -07003562static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003563{
3564 /*
3565 * Initially we receive a position value that corresponds to
3566 * one more than the last pid shown (or 0 on the first call or
3567 * after a seek to the start). Use a binary-search to find the
3568 * next pid to display, if any
3569 */
Ben Blum102a7752009-09-23 15:56:26 -07003570 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003571 int index = 0, pid = *pos;
3572 int *iter;
3573
Ben Blum102a7752009-09-23 15:56:26 -07003574 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003575 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003576 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003577
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 while (index < end) {
3579 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003580 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003581 index = mid;
3582 break;
Ben Blum102a7752009-09-23 15:56:26 -07003583 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003584 index = mid + 1;
3585 else
3586 end = mid;
3587 }
3588 }
3589 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003590 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003591 return NULL;
3592 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003593 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 *pos = *iter;
3595 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003596}
3597
Ben Blum102a7752009-09-23 15:56:26 -07003598static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003599{
Ben Blum102a7752009-09-23 15:56:26 -07003600 struct cgroup_pidlist *l = s->private;
3601 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003602}
3603
Ben Blum102a7752009-09-23 15:56:26 -07003604static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003605{
Ben Blum102a7752009-09-23 15:56:26 -07003606 struct cgroup_pidlist *l = s->private;
3607 pid_t *p = v;
3608 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003609 /*
3610 * Advance to the next pid in the array. If this goes off the
3611 * end, we're done
3612 */
3613 p++;
3614 if (p >= end) {
3615 return NULL;
3616 } else {
3617 *pos = *p;
3618 return p;
3619 }
3620}
3621
Ben Blum102a7752009-09-23 15:56:26 -07003622static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003623{
3624 return seq_printf(s, "%d\n", *(int *)v);
3625}
3626
Ben Blum102a7752009-09-23 15:56:26 -07003627/*
3628 * seq_operations functions for iterating on pidlists through seq_file -
3629 * independent of whether it's tasks or procs
3630 */
3631static const struct seq_operations cgroup_pidlist_seq_operations = {
3632 .start = cgroup_pidlist_start,
3633 .stop = cgroup_pidlist_stop,
3634 .next = cgroup_pidlist_next,
3635 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003636};
3637
Ben Blum102a7752009-09-23 15:56:26 -07003638static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003639{
Ben Blum72a8cb32009-09-23 15:56:27 -07003640 /*
3641 * the case where we're the last user of this particular pidlist will
3642 * have us remove it from the cgroup's list, which entails taking the
3643 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3644 * pidlist_mutex, we have to take pidlist_mutex first.
3645 */
3646 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003647 down_write(&l->mutex);
3648 BUG_ON(!l->use_count);
3649 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 /* we're the last user if refcount is 0; remove and free */
3651 list_del(&l->links);
3652 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003653 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 put_pid_ns(l->key.ns);
3655 up_write(&l->mutex);
3656 kfree(l);
3657 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003659 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003660 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003661}
3662
Ben Blum102a7752009-09-23 15:56:26 -07003663static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003664{
Ben Blum102a7752009-09-23 15:56:26 -07003665 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003666 if (!(file->f_mode & FMODE_READ))
3667 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003668 /*
3669 * the seq_file will only be initialized if the file was opened for
3670 * reading; hence we check if it's not null only in that case.
3671 */
3672 l = ((struct seq_file *)file->private_data)->private;
3673 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003674 return seq_release(inode, file);
3675}
3676
Ben Blum102a7752009-09-23 15:56:26 -07003677static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 .read = seq_read,
3679 .llseek = seq_lseek,
3680 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003681 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003682};
3683
3684/*
Ben Blum102a7752009-09-23 15:56:26 -07003685 * The following functions handle opens on a file that displays a pidlist
3686 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3687 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 */
Ben Blum102a7752009-09-23 15:56:26 -07003689/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003690static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003691{
3692 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003694 int retval;
3695
3696 /* Nothing to do for write-only files */
3697 if (!(file->f_mode & FMODE_READ))
3698 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003699
Ben Blum102a7752009-09-23 15:56:26 -07003700 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003702 if (retval)
3703 return retval;
3704 /* configure file information */
3705 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706
Ben Blum102a7752009-09-23 15:56:26 -07003707 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003708 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003709 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003711 }
Ben Blum102a7752009-09-23 15:56:26 -07003712 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003713 return 0;
3714}
Ben Blum102a7752009-09-23 15:56:26 -07003715static int cgroup_tasks_open(struct inode *unused, struct file *file)
3716{
Ben Blum72a8cb32009-09-23 15:56:27 -07003717 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003718}
3719static int cgroup_procs_open(struct inode *unused, struct file *file)
3720{
Ben Blum72a8cb32009-09-23 15:56:27 -07003721 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003722}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003723
Paul Menagebd89aab2007-10-18 23:40:44 -07003724static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003725 struct cftype *cft)
3726{
Paul Menagebd89aab2007-10-18 23:40:44 -07003727 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003728}
3729
Paul Menage6379c102008-07-25 01:47:01 -07003730static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3731 struct cftype *cft,
3732 u64 val)
3733{
3734 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3735 if (val)
3736 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3737 else
3738 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3739 return 0;
3740}
3741
Paul Menagebbcb81d2007-10-18 23:39:32 -07003742/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003743 * Unregister event and free resources.
3744 *
3745 * Gets called from workqueue.
3746 */
3747static void cgroup_event_remove(struct work_struct *work)
3748{
3749 struct cgroup_event *event = container_of(work, struct cgroup_event,
3750 remove);
3751 struct cgroup *cgrp = event->cgrp;
3752
Li Zefan810cbee2013-02-18 18:56:14 +08003753 remove_wait_queue(event->wqh, &event->wait);
3754
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003755 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3756
Li Zefan810cbee2013-02-18 18:56:14 +08003757 /* Notify userspace the event is going away. */
3758 eventfd_signal(event->eventfd, 1);
3759
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003760 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003761 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003762 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003763}
3764
3765/*
3766 * Gets called on POLLHUP on eventfd when user closes it.
3767 *
3768 * Called with wqh->lock held and interrupts disabled.
3769 */
3770static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3771 int sync, void *key)
3772{
3773 struct cgroup_event *event = container_of(wait,
3774 struct cgroup_event, wait);
3775 struct cgroup *cgrp = event->cgrp;
3776 unsigned long flags = (unsigned long)key;
3777
3778 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003779 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003780 * If the event has been detached at cgroup removal, we
3781 * can simply return knowing the other side will cleanup
3782 * for us.
3783 *
3784 * We can't race against event freeing since the other
3785 * side will require wqh->lock via remove_wait_queue(),
3786 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003787 */
Li Zefan810cbee2013-02-18 18:56:14 +08003788 spin_lock(&cgrp->event_list_lock);
3789 if (!list_empty(&event->list)) {
3790 list_del_init(&event->list);
3791 /*
3792 * We are in atomic context, but cgroup_event_remove()
3793 * may sleep, so we have to call it in workqueue.
3794 */
3795 schedule_work(&event->remove);
3796 }
3797 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003798 }
3799
3800 return 0;
3801}
3802
3803static void cgroup_event_ptable_queue_proc(struct file *file,
3804 wait_queue_head_t *wqh, poll_table *pt)
3805{
3806 struct cgroup_event *event = container_of(pt,
3807 struct cgroup_event, pt);
3808
3809 event->wqh = wqh;
3810 add_wait_queue(wqh, &event->wait);
3811}
3812
3813/*
3814 * Parse input and register new cgroup event handler.
3815 *
3816 * Input must be in format '<event_fd> <control_fd> <args>'.
3817 * Interpretation of args is defined by control file implementation.
3818 */
3819static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3820 const char *buffer)
3821{
3822 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003823 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003824 unsigned int efd, cfd;
3825 struct file *efile = NULL;
3826 struct file *cfile = NULL;
3827 char *endp;
3828 int ret;
3829
3830 efd = simple_strtoul(buffer, &endp, 10);
3831 if (*endp != ' ')
3832 return -EINVAL;
3833 buffer = endp + 1;
3834
3835 cfd = simple_strtoul(buffer, &endp, 10);
3836 if ((*endp != ' ') && (*endp != '\0'))
3837 return -EINVAL;
3838 buffer = endp + 1;
3839
3840 event = kzalloc(sizeof(*event), GFP_KERNEL);
3841 if (!event)
3842 return -ENOMEM;
3843 event->cgrp = cgrp;
3844 INIT_LIST_HEAD(&event->list);
3845 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3846 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3847 INIT_WORK(&event->remove, cgroup_event_remove);
3848
3849 efile = eventfd_fget(efd);
3850 if (IS_ERR(efile)) {
3851 ret = PTR_ERR(efile);
3852 goto fail;
3853 }
3854
3855 event->eventfd = eventfd_ctx_fileget(efile);
3856 if (IS_ERR(event->eventfd)) {
3857 ret = PTR_ERR(event->eventfd);
3858 goto fail;
3859 }
3860
3861 cfile = fget(cfd);
3862 if (!cfile) {
3863 ret = -EBADF;
3864 goto fail;
3865 }
3866
3867 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003868 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003869 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003870 if (ret < 0)
3871 goto fail;
3872
3873 event->cft = __file_cft(cfile);
3874 if (IS_ERR(event->cft)) {
3875 ret = PTR_ERR(event->cft);
3876 goto fail;
3877 }
3878
Li Zefanf1690072013-02-18 14:13:35 +08003879 /*
3880 * The file to be monitored must be in the same cgroup as
3881 * cgroup.event_control is.
3882 */
3883 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3884 if (cgrp_cfile != cgrp) {
3885 ret = -EINVAL;
3886 goto fail;
3887 }
3888
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 if (!event->cft->register_event || !event->cft->unregister_event) {
3890 ret = -EINVAL;
3891 goto fail;
3892 }
3893
3894 ret = event->cft->register_event(cgrp, event->cft,
3895 event->eventfd, buffer);
3896 if (ret)
3897 goto fail;
3898
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003899 /*
3900 * Events should be removed after rmdir of cgroup directory, but before
3901 * destroying subsystem state objects. Let's take reference to cgroup
3902 * directory dentry to do that.
3903 */
3904 dget(cgrp->dentry);
3905
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003906 spin_lock(&cgrp->event_list_lock);
3907 list_add(&event->list, &cgrp->event_list);
3908 spin_unlock(&cgrp->event_list_lock);
3909
3910 fput(cfile);
3911 fput(efile);
3912
3913 return 0;
3914
3915fail:
3916 if (cfile)
3917 fput(cfile);
3918
3919 if (event && event->eventfd && !IS_ERR(event->eventfd))
3920 eventfd_ctx_put(event->eventfd);
3921
3922 if (!IS_ERR_OR_NULL(efile))
3923 fput(efile);
3924
3925 kfree(event);
3926
3927 return ret;
3928}
3929
Daniel Lezcano97978e62010-10-27 15:33:35 -07003930static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3931 struct cftype *cft)
3932{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003933 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003934}
3935
3936static int cgroup_clone_children_write(struct cgroup *cgrp,
3937 struct cftype *cft,
3938 u64 val)
3939{
3940 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003941 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003942 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003943 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003944 return 0;
3945}
3946
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003947/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003948 * for the common functions, 'private' gives the type of file
3949 */
Ben Blum102a7752009-09-23 15:56:26 -07003950/* for hysterical raisins, we can't put this on the older files */
3951#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003952static struct cftype files[] = {
3953 {
3954 .name = "tasks",
3955 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003956 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003957 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003958 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003959 },
Ben Blum102a7752009-09-23 15:56:26 -07003960 {
3961 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3962 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003963 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003964 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003965 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003966 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003967 {
3968 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003969 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003970 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003971 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972 {
3973 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3974 .write_string = cgroup_write_event_control,
3975 .mode = S_IWUGO,
3976 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003977 {
3978 .name = "cgroup.clone_children",
3979 .read_u64 = cgroup_clone_children_read,
3980 .write_u64 = cgroup_clone_children_write,
3981 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003982 {
3983 .name = "release_agent",
3984 .flags = CFTYPE_ONLY_ON_ROOT,
3985 .read_seq_string = cgroup_release_agent_show,
3986 .write_string = cgroup_release_agent_write,
3987 .max_write_len = PATH_MAX,
3988 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003989 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003990};
3991
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003992/**
3993 * cgroup_populate_dir - selectively creation of files in a directory
3994 * @cgrp: target cgroup
3995 * @base_files: true if the base files should be added
3996 * @subsys_mask: mask of the subsystem ids whose files should be added
3997 */
3998static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3999 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004000{
4001 int err;
4002 struct cgroup_subsys *ss;
4003
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004004 if (base_files) {
4005 err = cgroup_addrm_files(cgrp, NULL, files, true);
4006 if (err < 0)
4007 return err;
4008 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004009
Tejun Heo8e3f6542012-04-01 12:09:55 -07004010 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004011 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004012 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004013 if (!test_bit(ss->subsys_id, &subsys_mask))
4014 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004015
Tejun Heodb0416b2012-04-01 12:09:55 -07004016 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004017 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004018 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004019
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004020 /* This cgroup is ready now */
4021 for_each_subsys(cgrp->root, ss) {
4022 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4023 /*
4024 * Update id->css pointer and make this css visible from
4025 * CSS ID functions. This pointer will be dereferened
4026 * from RCU-read-side without locks.
4027 */
4028 if (css->id)
4029 rcu_assign_pointer(css->id->css, css);
4030 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031
4032 return 0;
4033}
4034
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035static void css_dput_fn(struct work_struct *work)
4036{
4037 struct cgroup_subsys_state *css =
4038 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004039 struct dentry *dentry = css->cgroup->dentry;
4040 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004041
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004042 atomic_inc(&sb->s_active);
4043 dput(dentry);
4044 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004045}
4046
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047static void init_cgroup_css(struct cgroup_subsys_state *css,
4048 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004049 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050{
Paul Menagebd89aab2007-10-18 23:40:44 -07004051 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004052 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004054 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004055 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004056 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004057 BUG_ON(cgrp->subsys[ss->subsys_id]);
4058 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004059
4060 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004061 * css holds an extra ref to @cgrp->dentry which is put on the last
4062 * css_put(). dput() requires process context, which css_put() may
4063 * be called without. @css->dput_work will be used to invoke
4064 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004065 */
4066 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004067}
4068
Tejun Heob1929db2012-11-19 08:13:38 -08004069/* invoke ->post_create() on a new CSS and mark it online if successful */
4070static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004071{
Tejun Heob1929db2012-11-19 08:13:38 -08004072 int ret = 0;
4073
Tejun Heoa31f2d32012-11-19 08:13:37 -08004074 lockdep_assert_held(&cgroup_mutex);
4075
Tejun Heo92fb9742012-11-19 08:13:38 -08004076 if (ss->css_online)
4077 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004078 if (!ret)
4079 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4080 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004081}
4082
4083/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4084static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4085 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4086{
4087 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4088
4089 lockdep_assert_held(&cgroup_mutex);
4090
4091 if (!(css->flags & CSS_ONLINE))
4092 return;
4093
Li Zefand7eeac12013-03-12 15:35:59 -07004094 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004095 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004096
4097 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4098}
4099
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100/*
Li Zefana043e3b2008-02-23 15:24:09 -08004101 * cgroup_create - create a cgroup
4102 * @parent: cgroup that will be parent of the new cgroup
4103 * @dentry: dentry of the new cgroup
4104 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004105 *
Li Zefana043e3b2008-02-23 15:24:09 -08004106 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004107 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004109 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110{
Paul Menagebd89aab2007-10-18 23:40:44 -07004111 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004112 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004113 struct cgroupfs_root *root = parent->root;
4114 int err = 0;
4115 struct cgroup_subsys *ss;
4116 struct super_block *sb = root->sb;
4117
Tejun Heo0a950f62012-11-19 09:02:12 -08004118 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004119 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4120 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121 return -ENOMEM;
4122
Li Zefan65dff752013-03-01 15:01:56 +08004123 name = cgroup_alloc_name(dentry);
4124 if (!name)
4125 goto err_free_cgrp;
4126 rcu_assign_pointer(cgrp->name, name);
4127
Tejun Heo0a950f62012-11-19 09:02:12 -08004128 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4129 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004130 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004131
Tejun Heo976c06b2012-11-05 09:16:59 -08004132 /*
4133 * Only live parents can have children. Note that the liveliness
4134 * check isn't strictly necessary because cgroup_mkdir() and
4135 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4136 * anyway so that locking is contained inside cgroup proper and we
4137 * don't get nasty surprises if we ever grow another caller.
4138 */
4139 if (!cgroup_lock_live_group(parent)) {
4140 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004141 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004142 }
4143
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 /* Grab a reference on the superblock so the hierarchy doesn't
4145 * get deleted on unmount if there are child cgroups. This
4146 * can be done outside cgroup_mutex, since the sb can't
4147 * disappear while someone has an open control file on the
4148 * fs */
4149 atomic_inc(&sb->s_active);
4150
Paul Menagecc31edc2008-10-18 20:28:04 -07004151 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152
Li Zefanfe1c06c2013-01-24 14:30:22 +08004153 dentry->d_fsdata = cgrp;
4154 cgrp->dentry = dentry;
4155
Paul Menagebd89aab2007-10-18 23:40:44 -07004156 cgrp->parent = parent;
4157 cgrp->root = parent->root;
4158 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159
Li Zefanb6abdb02008-03-04 14:28:19 -08004160 if (notify_on_release(parent))
4161 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4162
Tejun Heo2260e7f2012-11-19 08:13:38 -08004163 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4164 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004165
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004167 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004168
Tejun Heo92fb9742012-11-19 08:13:38 -08004169 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004170 if (IS_ERR(css)) {
4171 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004172 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004174 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004175 if (ss->use_id) {
4176 err = alloc_css_id(ss, parent, cgrp);
4177 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004178 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004179 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180 }
4181
Tejun Heo4e139af2012-11-19 08:13:36 -08004182 /*
4183 * Create directory. cgroup_create_file() returns with the new
4184 * directory locked on success so that it can be populated without
4185 * dropping cgroup_mutex.
4186 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004187 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004189 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004190 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191
Tejun Heo4e139af2012-11-19 08:13:36 -08004192 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004193 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4194 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4195 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004196
Tejun Heob1929db2012-11-19 08:13:38 -08004197 /* each css holds a ref to the cgroup's dentry */
4198 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004199 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004200
Tejun Heob1929db2012-11-19 08:13:38 -08004201 /* creation succeeded, notify subsystems */
4202 for_each_subsys(root, ss) {
4203 err = online_css(ss, cgrp);
4204 if (err)
4205 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004206
4207 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4208 parent->parent) {
4209 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",
4210 current->comm, current->pid, ss->name);
4211 if (!strcmp(ss->name, "memory"))
4212 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4213 ss->warned_broken_hierarchy = true;
4214 }
Tejun Heoa8638032012-11-09 09:12:29 -08004215 }
4216
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004217 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004218 if (err)
4219 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220
4221 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004222 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223
4224 return 0;
4225
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004226err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004228 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004229 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232 /* Release the reference count that we took on the superblock */
4233 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004234err_free_id:
4235 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004236err_free_name:
4237 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004238err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004239 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004241
4242err_destroy:
4243 cgroup_destroy_locked(cgrp);
4244 mutex_unlock(&cgroup_mutex);
4245 mutex_unlock(&dentry->d_inode->i_mutex);
4246 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247}
4248
Al Viro18bb1db2011-07-26 01:41:39 -04004249static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250{
4251 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4252
4253 /* the vfs holds inode->i_mutex already */
4254 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4255}
4256
Tejun Heo42809dd2012-11-19 08:13:37 -08004257static int cgroup_destroy_locked(struct cgroup *cgrp)
4258 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259{
Tejun Heo42809dd2012-11-19 08:13:37 -08004260 struct dentry *d = cgrp->dentry;
4261 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004262 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004263 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264
Tejun Heo42809dd2012-11-19 08:13:37 -08004265 lockdep_assert_held(&d->d_inode->i_mutex);
4266 lockdep_assert_held(&cgroup_mutex);
4267
4268 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004269 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004270
Tejun Heo1a90dd52012-11-05 09:16:59 -08004271 /*
4272 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4273 * removed. This makes future css_tryget() and child creation
4274 * attempts fail thus maintaining the removal conditions verified
4275 * above.
4276 */
Tejun Heoed9577932012-11-05 09:16:58 -08004277 for_each_subsys(cgrp->root, ss) {
4278 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4279
4280 WARN_ON(atomic_read(&css->refcnt) < 0);
4281 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004282 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004283 set_bit(CGRP_REMOVED, &cgrp->flags);
4284
Tejun Heoa31f2d32012-11-19 08:13:37 -08004285 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004286 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004287 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004288
4289 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004290 * Put all the base refs. Each css holds an extra reference to the
4291 * cgroup's dentry and cgroup removal proceeds regardless of css
4292 * refs. On the last put of each css, whenever that may be, the
4293 * extra dentry ref is put so that dentry destruction happens only
4294 * after all css's are released.
4295 */
Tejun Heoe9316082012-11-05 09:16:58 -08004296 for_each_subsys(cgrp->root, ss)
4297 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004298
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004299 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004300 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004301 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004302 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004303
Paul Menage999cd8a2009-01-07 18:08:36 -08004304 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004305 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004306 list_del_init(&cgrp->allcg_node);
4307
Tejun Heo42809dd2012-11-19 08:13:37 -08004308 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004309 cgroup_d_remove_dir(d);
4310 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311
Paul Menagebd89aab2007-10-18 23:40:44 -07004312 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004313 check_for_release(parent);
4314
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004315 /*
4316 * Unregister events and notify userspace.
4317 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004318 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004319 */
4320 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004321 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004322 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004323 schedule_work(&event->remove);
4324 }
Li Zefan810cbee2013-02-18 18:56:14 +08004325 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004326
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327 return 0;
4328}
4329
Tejun Heo42809dd2012-11-19 08:13:37 -08004330static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4331{
4332 int ret;
4333
4334 mutex_lock(&cgroup_mutex);
4335 ret = cgroup_destroy_locked(dentry->d_fsdata);
4336 mutex_unlock(&cgroup_mutex);
4337
4338 return ret;
4339}
4340
Tejun Heo8e3f6542012-04-01 12:09:55 -07004341static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4342{
4343 INIT_LIST_HEAD(&ss->cftsets);
4344
4345 /*
4346 * base_cftset is embedded in subsys itself, no need to worry about
4347 * deregistration.
4348 */
4349 if (ss->base_cftypes) {
4350 ss->base_cftset.cfts = ss->base_cftypes;
4351 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4352 }
4353}
4354
Li Zefan06a11922008-04-29 01:00:07 -07004355static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004358
4359 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360
Tejun Heo648bb562012-11-19 08:13:36 -08004361 mutex_lock(&cgroup_mutex);
4362
Tejun Heo8e3f6542012-04-01 12:09:55 -07004363 /* init base cftset */
4364 cgroup_init_cftsets(ss);
4365
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004367 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004369 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370 /* We don't handle early failures gracefully */
4371 BUG_ON(IS_ERR(css));
4372 init_cgroup_css(css, ss, dummytop);
4373
Li Zefane8d55fd2008-04-29 01:00:13 -07004374 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004375 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004376 * newly registered, all tasks and hence the
4377 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004378 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379
4380 need_forkexit_callback |= ss->fork || ss->exit;
4381
Li Zefane8d55fd2008-04-29 01:00:13 -07004382 /* At system boot, before all subsystems have been
4383 * registered, no tasks have been forked, so we don't
4384 * need to invoke fork callbacks here. */
4385 BUG_ON(!list_empty(&init_task.tasks));
4386
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004388 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004389
Tejun Heo648bb562012-11-19 08:13:36 -08004390 mutex_unlock(&cgroup_mutex);
4391
Ben Blume6a11052010-03-10 15:22:09 -08004392 /* this function shouldn't be used with modular subsystems, since they
4393 * need to register a subsys_id, among other things */
4394 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395}
4396
4397/**
Ben Blume6a11052010-03-10 15:22:09 -08004398 * cgroup_load_subsys: load and register a modular subsystem at runtime
4399 * @ss: the subsystem to load
4400 *
4401 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004402 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004403 * up for use. If the subsystem is built-in anyway, work is delegated to the
4404 * simpler cgroup_init_subsys.
4405 */
4406int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4407{
Ben Blume6a11052010-03-10 15:22:09 -08004408 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004409 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004410 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004411 struct css_set *cg;
4412 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004413
4414 /* check name and function validity */
4415 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004416 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004417 return -EINVAL;
4418
4419 /*
4420 * we don't support callbacks in modular subsystems. this check is
4421 * before the ss->module check for consistency; a subsystem that could
4422 * be a module should still have no callbacks even if the user isn't
4423 * compiling it as one.
4424 */
4425 if (ss->fork || ss->exit)
4426 return -EINVAL;
4427
4428 /*
4429 * an optionally modular subsystem is built-in: we want to do nothing,
4430 * since cgroup_init_subsys will have already taken care of it.
4431 */
4432 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004433 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004434 BUG_ON(subsys[ss->subsys_id] != ss);
4435 return 0;
4436 }
4437
Tejun Heo8e3f6542012-04-01 12:09:55 -07004438 /* init base cftset */
4439 cgroup_init_cftsets(ss);
4440
Ben Blume6a11052010-03-10 15:22:09 -08004441 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004442 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004443
4444 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004445 * no ss->css_alloc seems to need anything important in the ss
4446 * struct, so this can happen first (i.e. before the rootnode
4447 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004448 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004449 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004450 if (IS_ERR(css)) {
4451 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004452 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004453 mutex_unlock(&cgroup_mutex);
4454 return PTR_ERR(css);
4455 }
4456
4457 list_add(&ss->sibling, &rootnode.subsys_list);
4458 ss->root = &rootnode;
4459
4460 /* our new subsystem will be attached to the dummy hierarchy. */
4461 init_cgroup_css(css, ss, dummytop);
4462 /* init_idr must be after init_cgroup_css because it sets css->id. */
4463 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004464 ret = cgroup_init_idr(ss, css);
4465 if (ret)
4466 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004467 }
4468
4469 /*
4470 * Now we need to entangle the css into the existing css_sets. unlike
4471 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4472 * will need a new pointer to it; done by iterating the css_set_table.
4473 * furthermore, modifying the existing css_sets will corrupt the hash
4474 * table state, so each changed css_set will need its hash recomputed.
4475 * this is all done under the css_set_lock.
4476 */
4477 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004478 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004479 /* skip entries that we already rehashed */
4480 if (cg->subsys[ss->subsys_id])
4481 continue;
4482 /* remove existing entry */
4483 hash_del(&cg->hlist);
4484 /* set new value */
4485 cg->subsys[ss->subsys_id] = css;
4486 /* recompute hash and restore entry */
4487 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004488 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004489 }
4490 write_unlock(&css_set_lock);
4491
Ben Blume6a11052010-03-10 15:22:09 -08004492 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004493 ret = online_css(ss, dummytop);
4494 if (ret)
4495 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004496
Ben Blume6a11052010-03-10 15:22:09 -08004497 /* success! */
4498 mutex_unlock(&cgroup_mutex);
4499 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004500
4501err_unload:
4502 mutex_unlock(&cgroup_mutex);
4503 /* @ss can't be mounted here as try_module_get() would fail */
4504 cgroup_unload_subsys(ss);
4505 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004506}
4507EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4508
4509/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004510 * cgroup_unload_subsys: unload a modular subsystem
4511 * @ss: the subsystem to unload
4512 *
4513 * This function should be called in a modular subsystem's exitcall. When this
4514 * function is invoked, the refcount on the subsystem's module will be 0, so
4515 * the subsystem will not be attached to any hierarchy.
4516 */
4517void cgroup_unload_subsys(struct cgroup_subsys *ss)
4518{
4519 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004520
4521 BUG_ON(ss->module == NULL);
4522
4523 /*
4524 * we shouldn't be called if the subsystem is in use, and the use of
4525 * try_module_get in parse_cgroupfs_options should ensure that it
4526 * doesn't start being used while we're killing it off.
4527 */
4528 BUG_ON(ss->root != &rootnode);
4529
4530 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004531
Tejun Heoa31f2d32012-11-19 08:13:37 -08004532 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004533 ss->active = 0;
4534
Tejun Heoc897ff62013-02-27 17:03:49 -08004535 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004536 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004537
Ben Blumcf5d5942010-03-10 15:22:09 -08004538 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004539 subsys[ss->subsys_id] = NULL;
4540
4541 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004542 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004543
4544 /*
4545 * disentangle the css from all css_sets attached to the dummytop. as
4546 * in loading, we need to pay our respects to the hashtable gods.
4547 */
4548 write_lock(&css_set_lock);
4549 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4550 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004551 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004552
Li Zefan0ac801f2013-01-10 11:49:27 +08004553 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004554 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004555 key = css_set_hash(cg->subsys);
4556 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004557 }
4558 write_unlock(&css_set_lock);
4559
4560 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004561 * remove subsystem's css from the dummytop and free it - need to
4562 * free before marking as null because ss->css_free needs the
4563 * cgrp->subsys pointer to find their state. note that this also
4564 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004565 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004566 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004567 dummytop->subsys[ss->subsys_id] = NULL;
4568
4569 mutex_unlock(&cgroup_mutex);
4570}
4571EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4572
4573/**
Li Zefana043e3b2008-02-23 15:24:09 -08004574 * cgroup_init_early - cgroup initialization at system boot
4575 *
4576 * Initialize cgroups at system boot, and initialize any
4577 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578 */
4579int __init cgroup_init_early(void)
4580{
4581 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004582 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004583 INIT_LIST_HEAD(&init_css_set.cg_links);
4584 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004585 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004586 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004587 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004588 root_count = 1;
4589 init_task.cgroups = &init_css_set;
4590
4591 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004592 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004593 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004594 &rootnode.top_cgroup.css_sets);
4595 list_add(&init_css_set_link.cg_link_list,
4596 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004597
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004598 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004599 struct cgroup_subsys *ss = subsys[i];
4600
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004601 /* at bootup time, we don't worry about modular subsystems */
4602 if (!ss || ss->module)
4603 continue;
4604
Paul Menageddbcc7e2007-10-18 23:39:30 -07004605 BUG_ON(!ss->name);
4606 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004607 BUG_ON(!ss->css_alloc);
4608 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004610 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611 ss->name, ss->subsys_id);
4612 BUG();
4613 }
4614
4615 if (ss->early_init)
4616 cgroup_init_subsys(ss);
4617 }
4618 return 0;
4619}
4620
4621/**
Li Zefana043e3b2008-02-23 15:24:09 -08004622 * cgroup_init - cgroup initialization
4623 *
4624 * Register cgroup filesystem and /proc file, and initialize
4625 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626 */
4627int __init cgroup_init(void)
4628{
4629 int err;
4630 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004631 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004632
4633 err = bdi_init(&cgroup_backing_dev_info);
4634 if (err)
4635 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004637 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004639
4640 /* at bootup time, we don't worry about modular subsystems */
4641 if (!ss || ss->module)
4642 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004643 if (!ss->early_init)
4644 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004645 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004646 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647 }
4648
Li Zefan472b1052008-04-29 01:00:11 -07004649 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004650 key = css_set_hash(init_css_set.subsys);
4651 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004652 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004653
4654 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4655 if (!cgroup_kobj) {
4656 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004658 }
4659
4660 err = register_filesystem(&cgroup_fs_type);
4661 if (err < 0) {
4662 kobject_put(cgroup_kobj);
4663 goto out;
4664 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665
Li Zefan46ae2202008-04-29 01:00:08 -07004666 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004667
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668out:
Paul Menagea4243162007-10-18 23:39:35 -07004669 if (err)
4670 bdi_destroy(&cgroup_backing_dev_info);
4671
Paul Menageddbcc7e2007-10-18 23:39:30 -07004672 return err;
4673}
Paul Menageb4f48b62007-10-18 23:39:33 -07004674
Paul Menagea4243162007-10-18 23:39:35 -07004675/*
4676 * proc_cgroup_show()
4677 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4678 * - Used for /proc/<pid>/cgroup.
4679 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4680 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004681 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004682 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4683 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4684 * cgroup to top_cgroup.
4685 */
4686
4687/* TODO: Use a proper seq_file iterator */
4688static int proc_cgroup_show(struct seq_file *m, void *v)
4689{
4690 struct pid *pid;
4691 struct task_struct *tsk;
4692 char *buf;
4693 int retval;
4694 struct cgroupfs_root *root;
4695
4696 retval = -ENOMEM;
4697 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4698 if (!buf)
4699 goto out;
4700
4701 retval = -ESRCH;
4702 pid = m->private;
4703 tsk = get_pid_task(pid, PIDTYPE_PID);
4704 if (!tsk)
4705 goto out_free;
4706
4707 retval = 0;
4708
4709 mutex_lock(&cgroup_mutex);
4710
Li Zefane5f6a862009-01-07 18:07:41 -08004711 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004712 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004713 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004714 int count = 0;
4715
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004716 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004717 for_each_subsys(root, ss)
4718 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004719 if (strlen(root->name))
4720 seq_printf(m, "%sname=%s", count ? "," : "",
4721 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004722 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004723 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004724 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004725 if (retval < 0)
4726 goto out_unlock;
4727 seq_puts(m, buf);
4728 seq_putc(m, '\n');
4729 }
4730
4731out_unlock:
4732 mutex_unlock(&cgroup_mutex);
4733 put_task_struct(tsk);
4734out_free:
4735 kfree(buf);
4736out:
4737 return retval;
4738}
4739
4740static int cgroup_open(struct inode *inode, struct file *file)
4741{
4742 struct pid *pid = PROC_I(inode)->pid;
4743 return single_open(file, proc_cgroup_show, pid);
4744}
4745
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004746const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004747 .open = cgroup_open,
4748 .read = seq_read,
4749 .llseek = seq_lseek,
4750 .release = single_release,
4751};
4752
4753/* Display information about each subsystem and each hierarchy */
4754static int proc_cgroupstats_show(struct seq_file *m, void *v)
4755{
4756 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004757
Paul Menage8bab8dd2008-04-04 14:29:57 -07004758 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004759 /*
4760 * ideally we don't want subsystems moving around while we do this.
4761 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4762 * subsys/hierarchy state.
4763 */
Paul Menagea4243162007-10-18 23:39:35 -07004764 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004765 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4766 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004767 if (ss == NULL)
4768 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004769 seq_printf(m, "%s\t%d\t%d\t%d\n",
4770 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004771 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004772 }
4773 mutex_unlock(&cgroup_mutex);
4774 return 0;
4775}
4776
4777static int cgroupstats_open(struct inode *inode, struct file *file)
4778{
Al Viro9dce07f2008-03-29 03:07:28 +00004779 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004780}
4781
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004782static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004783 .open = cgroupstats_open,
4784 .read = seq_read,
4785 .llseek = seq_lseek,
4786 .release = single_release,
4787};
4788
Paul Menageb4f48b62007-10-18 23:39:33 -07004789/**
4790 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004791 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004792 *
4793 * Description: A task inherits its parent's cgroup at fork().
4794 *
4795 * A pointer to the shared css_set was automatically copied in
4796 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004797 * it was not made under the protection of RCU or cgroup_mutex, so
4798 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4799 * have already changed current->cgroups, allowing the previously
4800 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004801 *
4802 * At the point that cgroup_fork() is called, 'current' is the parent
4803 * task, and the passed argument 'child' points to the child task.
4804 */
4805void cgroup_fork(struct task_struct *child)
4806{
Tejun Heo9bb71302012-10-18 17:52:07 -07004807 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004808 child->cgroups = current->cgroups;
4809 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004810 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004811 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004812}
4813
4814/**
Li Zefana043e3b2008-02-23 15:24:09 -08004815 * cgroup_post_fork - called on a new task after adding it to the task list
4816 * @child: the task in question
4817 *
Tejun Heo5edee612012-10-16 15:03:14 -07004818 * Adds the task to the list running through its css_set if necessary and
4819 * call the subsystem fork() callbacks. Has to be after the task is
4820 * visible on the task list in case we race with the first call to
4821 * cgroup_iter_start() - to guarantee that the new task ends up on its
4822 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004823 */
Paul Menage817929e2007-10-18 23:39:36 -07004824void cgroup_post_fork(struct task_struct *child)
4825{
Tejun Heo5edee612012-10-16 15:03:14 -07004826 int i;
4827
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004828 /*
4829 * use_task_css_set_links is set to 1 before we walk the tasklist
4830 * under the tasklist_lock and we read it here after we added the child
4831 * to the tasklist under the tasklist_lock as well. If the child wasn't
4832 * yet in the tasklist when we walked through it from
4833 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4834 * should be visible now due to the paired locking and barriers implied
4835 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4836 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4837 * lock on fork.
4838 */
Paul Menage817929e2007-10-18 23:39:36 -07004839 if (use_task_css_set_links) {
4840 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004841 task_lock(child);
4842 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004843 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004844 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004845 write_unlock(&css_set_lock);
4846 }
Tejun Heo5edee612012-10-16 15:03:14 -07004847
4848 /*
4849 * Call ss->fork(). This must happen after @child is linked on
4850 * css_set; otherwise, @child might change state between ->fork()
4851 * and addition to css_set.
4852 */
4853 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004854 /*
4855 * fork/exit callbacks are supported only for builtin
4856 * subsystems, and the builtin section of the subsys
4857 * array is immutable, so we don't need to lock the
4858 * subsys array here. On the other hand, modular section
4859 * of the array can be freed at module unload, so we
4860 * can't touch that.
4861 */
4862 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004863 struct cgroup_subsys *ss = subsys[i];
4864
Tejun Heo5edee612012-10-16 15:03:14 -07004865 if (ss->fork)
4866 ss->fork(child);
4867 }
4868 }
Paul Menage817929e2007-10-18 23:39:36 -07004869}
Tejun Heo5edee612012-10-16 15:03:14 -07004870
Paul Menage817929e2007-10-18 23:39:36 -07004871/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004872 * cgroup_exit - detach cgroup from exiting task
4873 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004874 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004875 *
4876 * Description: Detach cgroup from @tsk and release it.
4877 *
4878 * Note that cgroups marked notify_on_release force every task in
4879 * them to take the global cgroup_mutex mutex when exiting.
4880 * This could impact scaling on very large systems. Be reluctant to
4881 * use notify_on_release cgroups where very high task exit scaling
4882 * is required on large systems.
4883 *
4884 * the_top_cgroup_hack:
4885 *
4886 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4887 *
4888 * We call cgroup_exit() while the task is still competent to
4889 * handle notify_on_release(), then leave the task attached to the
4890 * root cgroup in each hierarchy for the remainder of its exit.
4891 *
4892 * To do this properly, we would increment the reference count on
4893 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4894 * code we would add a second cgroup function call, to drop that
4895 * reference. This would just create an unnecessary hot spot on
4896 * the top_cgroup reference count, to no avail.
4897 *
4898 * Normally, holding a reference to a cgroup without bumping its
4899 * count is unsafe. The cgroup could go away, or someone could
4900 * attach us to a different cgroup, decrementing the count on
4901 * the first cgroup that we never incremented. But in this case,
4902 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004903 * which wards off any cgroup_attach_task() attempts, or task is a failed
4904 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004905 */
4906void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4907{
Paul Menage817929e2007-10-18 23:39:36 -07004908 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004909 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004910
4911 /*
4912 * Unlink from the css_set task list if necessary.
4913 * Optimistically check cg_list before taking
4914 * css_set_lock
4915 */
4916 if (!list_empty(&tsk->cg_list)) {
4917 write_lock(&css_set_lock);
4918 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004919 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004920 write_unlock(&css_set_lock);
4921 }
4922
Paul Menageb4f48b62007-10-18 23:39:33 -07004923 /* Reassign the task to the init_css_set. */
4924 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004925 cg = tsk->cgroups;
4926 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004927
4928 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004929 /*
4930 * fork/exit callbacks are supported only for builtin
4931 * subsystems, see cgroup_post_fork() for details.
4932 */
4933 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004934 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004935
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004936 if (ss->exit) {
4937 struct cgroup *old_cgrp =
4938 rcu_dereference_raw(cg->subsys[i])->cgroup;
4939 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004940 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004941 }
4942 }
4943 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004944 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004945
Li Zefanb5d646f2013-01-24 14:43:51 +08004946 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004947}
Paul Menage697f4162007-10-18 23:39:34 -07004948
Paul Menagebd89aab2007-10-18 23:40:44 -07004949static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004950{
4951 /* All of these checks rely on RCU to keep the cgroup
4952 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004953 if (cgroup_is_releasable(cgrp) &&
4954 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4955 /*
4956 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004957 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004958 * it now
4959 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004960 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004961
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004962 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004963 if (!cgroup_is_removed(cgrp) &&
4964 list_empty(&cgrp->release_list)) {
4965 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004966 need_schedule_work = 1;
4967 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004968 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004969 if (need_schedule_work)
4970 schedule_work(&release_agent_work);
4971 }
4972}
4973
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004974/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004975bool __css_tryget(struct cgroup_subsys_state *css)
4976{
Tejun Heoe9316082012-11-05 09:16:58 -08004977 while (true) {
4978 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004979
Tejun Heoe9316082012-11-05 09:16:58 -08004980 v = css_refcnt(css);
4981 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4982 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004983 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004984 else if (t < 0)
4985 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004986 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004987 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004988}
4989EXPORT_SYMBOL_GPL(__css_tryget);
4990
4991/* Caller must verify that the css is not for root cgroup */
4992void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004993{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004994 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004995
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004996 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004997 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004998 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004999}
Ben Blum67523c42010-03-10 15:22:11 -08005000EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005001
5002/*
5003 * Notify userspace when a cgroup is released, by running the
5004 * configured release agent with the name of the cgroup (path
5005 * relative to the root of cgroup file system) as the argument.
5006 *
5007 * Most likely, this user command will try to rmdir this cgroup.
5008 *
5009 * This races with the possibility that some other task will be
5010 * attached to this cgroup before it is removed, or that some other
5011 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5012 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5013 * unused, and this cgroup will be reprieved from its death sentence,
5014 * to continue to serve a useful existence. Next time it's released,
5015 * we will get notified again, if it still has 'notify_on_release' set.
5016 *
5017 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5018 * means only wait until the task is successfully execve()'d. The
5019 * separate release agent task is forked by call_usermodehelper(),
5020 * then control in this thread returns here, without waiting for the
5021 * release agent task. We don't bother to wait because the caller of
5022 * this routine has no use for the exit status of the release agent
5023 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025static void cgroup_release_agent(struct work_struct *work)
5026{
5027 BUG_ON(work != &release_agent_work);
5028 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005029 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005030 while (!list_empty(&release_list)) {
5031 char *argv[3], *envp[3];
5032 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005033 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005034 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005035 struct cgroup,
5036 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005037 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005038 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005039 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005040 if (!pathbuf)
5041 goto continue_free;
5042 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5043 goto continue_free;
5044 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5045 if (!agentbuf)
5046 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047
5048 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005049 argv[i++] = agentbuf;
5050 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005051 argv[i] = NULL;
5052
5053 i = 0;
5054 /* minimal command environment */
5055 envp[i++] = "HOME=/";
5056 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5057 envp[i] = NULL;
5058
5059 /* Drop the lock while we invoke the usermode helper,
5060 * since the exec could involve hitting disk and hence
5061 * be a slow process */
5062 mutex_unlock(&cgroup_mutex);
5063 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005065 continue_free:
5066 kfree(pathbuf);
5067 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005068 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005070 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005071 mutex_unlock(&cgroup_mutex);
5072}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005073
5074static int __init cgroup_disable(char *str)
5075{
5076 int i;
5077 char *token;
5078
5079 while ((token = strsep(&str, ",")) != NULL) {
5080 if (!*token)
5081 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005082 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005083 struct cgroup_subsys *ss = subsys[i];
5084
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005085 /*
5086 * cgroup_disable, being at boot time, can't
5087 * know about module subsystems, so we don't
5088 * worry about them.
5089 */
5090 if (!ss || ss->module)
5091 continue;
5092
Paul Menage8bab8dd2008-04-04 14:29:57 -07005093 if (!strcmp(token, ss->name)) {
5094 ss->disabled = 1;
5095 printk(KERN_INFO "Disabling %s control group"
5096 " subsystem\n", ss->name);
5097 break;
5098 }
5099 }
5100 }
5101 return 1;
5102}
5103__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005104
5105/*
5106 * Functons for CSS ID.
5107 */
5108
5109/*
5110 *To get ID other than 0, this should be called when !cgroup_is_removed().
5111 */
5112unsigned short css_id(struct cgroup_subsys_state *css)
5113{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005114 struct css_id *cssid;
5115
5116 /*
5117 * This css_id() can return correct value when somone has refcnt
5118 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5119 * it's unchanged until freed.
5120 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005121 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005122
5123 if (cssid)
5124 return cssid->id;
5125 return 0;
5126}
Ben Blum67523c42010-03-10 15:22:11 -08005127EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005128
5129unsigned short css_depth(struct cgroup_subsys_state *css)
5130{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005131 struct css_id *cssid;
5132
Tejun Heo28b4c272012-04-01 12:09:56 -07005133 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005134
5135 if (cssid)
5136 return cssid->depth;
5137 return 0;
5138}
Ben Blum67523c42010-03-10 15:22:11 -08005139EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005140
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005141/**
5142 * css_is_ancestor - test "root" css is an ancestor of "child"
5143 * @child: the css to be tested.
5144 * @root: the css supporsed to be an ancestor of the child.
5145 *
5146 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005147 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005148 * But, considering usual usage, the csses should be valid objects after test.
5149 * Assuming that the caller will do some action to the child if this returns
5150 * returns true, the caller must take "child";s reference count.
5151 * If "child" is valid object and this returns true, "root" is valid, too.
5152 */
5153
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005154bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005155 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005156{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005157 struct css_id *child_id;
5158 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005159
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005160 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005161 if (!child_id)
5162 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005163 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005164 if (!root_id)
5165 return false;
5166 if (child_id->depth < root_id->depth)
5167 return false;
5168 if (child_id->stack[root_id->depth] != root_id->id)
5169 return false;
5170 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005171}
5172
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005173void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5174{
5175 struct css_id *id = css->id;
5176 /* When this is called before css_id initialization, id can be NULL */
5177 if (!id)
5178 return;
5179
5180 BUG_ON(!ss->use_id);
5181
5182 rcu_assign_pointer(id->css, NULL);
5183 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005184 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005185 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005186 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005187 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005188}
Ben Blum67523c42010-03-10 15:22:11 -08005189EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005190
5191/*
5192 * This is called by init or create(). Then, calls to this function are
5193 * always serialized (By cgroup_mutex() at create()).
5194 */
5195
5196static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5197{
5198 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005199 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005200
5201 BUG_ON(!ss->use_id);
5202
5203 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5204 newid = kzalloc(size, GFP_KERNEL);
5205 if (!newid)
5206 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005207
5208 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005209 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005211 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005212 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005213 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214
5215 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005216 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005217 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005218
Tejun Heod228d9e2013-02-27 17:04:54 -08005219 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005220 newid->depth = depth;
5221 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005222err_out:
5223 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005224 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225
5226}
5227
Ben Blume6a11052010-03-10 15:22:09 -08005228static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5229 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230{
5231 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005232
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005233 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005234 idr_init(&ss->idr);
5235
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236 newid = get_new_cssid(ss, 0);
5237 if (IS_ERR(newid))
5238 return PTR_ERR(newid);
5239
5240 newid->stack[0] = newid->id;
5241 newid->css = rootcss;
5242 rootcss->id = newid;
5243 return 0;
5244}
5245
5246static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5247 struct cgroup *child)
5248{
5249 int subsys_id, i, depth = 0;
5250 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005251 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252
5253 subsys_id = ss->subsys_id;
5254 parent_css = parent->subsys[subsys_id];
5255 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005256 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005257 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005258
5259 child_id = get_new_cssid(ss, depth);
5260 if (IS_ERR(child_id))
5261 return PTR_ERR(child_id);
5262
5263 for (i = 0; i < depth; i++)
5264 child_id->stack[i] = parent_id->stack[i];
5265 child_id->stack[depth] = child_id->id;
5266 /*
5267 * child_id->css pointer will be set after this cgroup is available
5268 * see cgroup_populate_dir()
5269 */
5270 rcu_assign_pointer(child_css->id, child_id);
5271
5272 return 0;
5273}
5274
5275/**
5276 * css_lookup - lookup css by id
5277 * @ss: cgroup subsys to be looked into.
5278 * @id: the id
5279 *
5280 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5281 * NULL if not. Should be called under rcu_read_lock()
5282 */
5283struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5284{
5285 struct css_id *cssid = NULL;
5286
5287 BUG_ON(!ss->use_id);
5288 cssid = idr_find(&ss->idr, id);
5289
5290 if (unlikely(!cssid))
5291 return NULL;
5292
5293 return rcu_dereference(cssid->css);
5294}
Ben Blum67523c42010-03-10 15:22:11 -08005295EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005296
5297/**
5298 * css_get_next - lookup next cgroup under specified hierarchy.
5299 * @ss: pointer to subsystem
5300 * @id: current position of iteration.
5301 * @root: pointer to css. search tree under this.
5302 * @foundid: position of found object.
5303 *
5304 * Search next css under the specified hierarchy of rootid. Calling under
5305 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5306 */
5307struct cgroup_subsys_state *
5308css_get_next(struct cgroup_subsys *ss, int id,
5309 struct cgroup_subsys_state *root, int *foundid)
5310{
5311 struct cgroup_subsys_state *ret = NULL;
5312 struct css_id *tmp;
5313 int tmpid;
5314 int rootid = css_id(root);
5315 int depth = css_depth(root);
5316
5317 if (!rootid)
5318 return NULL;
5319
5320 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005321 WARN_ON_ONCE(!rcu_read_lock_held());
5322
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005323 /* fill start point for scan */
5324 tmpid = id;
5325 while (1) {
5326 /*
5327 * scan next entry from bitmap(tree), tmpid is updated after
5328 * idr_get_next().
5329 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005331 if (!tmp)
5332 break;
5333 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5334 ret = rcu_dereference(tmp->css);
5335 if (ret) {
5336 *foundid = tmpid;
5337 break;
5338 }
5339 }
5340 /* continue to scan from next id */
5341 tmpid = tmpid + 1;
5342 }
5343 return ret;
5344}
5345
Stephane Eraniane5d13672011-02-14 11:20:01 +02005346/*
5347 * get corresponding css from file open on cgroupfs directory
5348 */
5349struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5350{
5351 struct cgroup *cgrp;
5352 struct inode *inode;
5353 struct cgroup_subsys_state *css;
5354
Al Viro496ad9a2013-01-23 17:07:38 -05005355 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005356 /* check in cgroup filesystem dir */
5357 if (inode->i_op != &cgroup_dir_inode_operations)
5358 return ERR_PTR(-EBADF);
5359
5360 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5361 return ERR_PTR(-EINVAL);
5362
5363 /* get cgroup */
5364 cgrp = __d_cgrp(f->f_dentry);
5365 css = cgrp->subsys[id];
5366 return css ? css : ERR_PTR(-ENOENT);
5367}
5368
Paul Menagefe693432009-09-23 15:56:20 -07005369#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005370static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005371{
5372 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5373
5374 if (!css)
5375 return ERR_PTR(-ENOMEM);
5376
5377 return css;
5378}
5379
Tejun Heo92fb9742012-11-19 08:13:38 -08005380static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005381{
5382 kfree(cont->subsys[debug_subsys_id]);
5383}
5384
5385static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5386{
5387 return atomic_read(&cont->count);
5388}
5389
5390static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5391{
5392 return cgroup_task_count(cont);
5393}
5394
5395static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5396{
5397 return (u64)(unsigned long)current->cgroups;
5398}
5399
5400static u64 current_css_set_refcount_read(struct cgroup *cont,
5401 struct cftype *cft)
5402{
5403 u64 count;
5404
5405 rcu_read_lock();
5406 count = atomic_read(&current->cgroups->refcount);
5407 rcu_read_unlock();
5408 return count;
5409}
5410
Paul Menage7717f7b2009-09-23 15:56:22 -07005411static int current_css_set_cg_links_read(struct cgroup *cont,
5412 struct cftype *cft,
5413 struct seq_file *seq)
5414{
5415 struct cg_cgroup_link *link;
5416 struct css_set *cg;
5417
5418 read_lock(&css_set_lock);
5419 rcu_read_lock();
5420 cg = rcu_dereference(current->cgroups);
5421 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5422 struct cgroup *c = link->cgrp;
5423 const char *name;
5424
5425 if (c->dentry)
5426 name = c->dentry->d_name.name;
5427 else
5428 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005429 seq_printf(seq, "Root %d group %s\n",
5430 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005431 }
5432 rcu_read_unlock();
5433 read_unlock(&css_set_lock);
5434 return 0;
5435}
5436
5437#define MAX_TASKS_SHOWN_PER_CSS 25
5438static int cgroup_css_links_read(struct cgroup *cont,
5439 struct cftype *cft,
5440 struct seq_file *seq)
5441{
5442 struct cg_cgroup_link *link;
5443
5444 read_lock(&css_set_lock);
5445 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5446 struct css_set *cg = link->cg;
5447 struct task_struct *task;
5448 int count = 0;
5449 seq_printf(seq, "css_set %p\n", cg);
5450 list_for_each_entry(task, &cg->tasks, cg_list) {
5451 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5452 seq_puts(seq, " ...\n");
5453 break;
5454 } else {
5455 seq_printf(seq, " task %d\n",
5456 task_pid_vnr(task));
5457 }
5458 }
5459 }
5460 read_unlock(&css_set_lock);
5461 return 0;
5462}
5463
Paul Menagefe693432009-09-23 15:56:20 -07005464static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5465{
5466 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5467}
5468
5469static struct cftype debug_files[] = {
5470 {
5471 .name = "cgroup_refcount",
5472 .read_u64 = cgroup_refcount_read,
5473 },
5474 {
5475 .name = "taskcount",
5476 .read_u64 = debug_taskcount_read,
5477 },
5478
5479 {
5480 .name = "current_css_set",
5481 .read_u64 = current_css_set_read,
5482 },
5483
5484 {
5485 .name = "current_css_set_refcount",
5486 .read_u64 = current_css_set_refcount_read,
5487 },
5488
5489 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005490 .name = "current_css_set_cg_links",
5491 .read_seq_string = current_css_set_cg_links_read,
5492 },
5493
5494 {
5495 .name = "cgroup_css_links",
5496 .read_seq_string = cgroup_css_links_read,
5497 },
5498
5499 {
Paul Menagefe693432009-09-23 15:56:20 -07005500 .name = "releasable",
5501 .read_u64 = releasable_read,
5502 },
Paul Menagefe693432009-09-23 15:56:20 -07005503
Tejun Heo4baf6e32012-04-01 12:09:55 -07005504 { } /* terminate */
5505};
Paul Menagefe693432009-09-23 15:56:20 -07005506
5507struct cgroup_subsys debug_subsys = {
5508 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005509 .css_alloc = debug_css_alloc,
5510 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005511 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005512 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005513};
5514#endif /* CONFIG_CGROUP_DEBUG */