blob: 29c77a714731ae74f7ab18cec3716f354223647a [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Ben Blumd8466872011-05-26 16:25:21 -070062#include <linux/flex_array.h> /* used in cgroup_attach_proc */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400115 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700117 /* Unique id for this hierarchy. */
118 int hierarchy_id;
119
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400121 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
123 /* A list running through the attached subsystems */
124 struct list_head subsys_list;
125
126 /* The root cgroup for this hierarchy */
127 struct cgroup top_cgroup;
128
129 /* Tracks how many cgroups are currently defined in hierarchy.*/
130 int number_of_cgroups;
131
Li Zefane5f6a862009-01-07 18:07:41 -0800132 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700133 struct list_head root_list;
134
Tejun Heob0ca5a82012-04-01 12:09:54 -0700135 /* All cgroups on this root, cgroup_mutex protected */
136 struct list_head allcg_list;
137
Paul Menageddbcc7e2007-10-18 23:39:30 -0700138 /* Hierarchy-specific flags */
139 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700140
Tejun Heo0a950f62012-11-19 09:02:12 -0800141 /* IDs for cgroups in this hierarchy */
142 struct ida cgroup_ida;
143
Paul Menagee788e062008-07-25 01:46:59 -0700144 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700145 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700146
147 /* The name for this hierarchy - may be empty */
148 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149};
150
Paul Menageddbcc7e2007-10-18 23:39:30 -0700151/*
152 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
153 * subsystems that are otherwise unattached - it never has more than a
154 * single cgroup, and all tasks are part of that cgroup.
155 */
156static struct cgroupfs_root rootnode;
157
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700158/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700159 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
160 */
161struct cfent {
162 struct list_head node;
163 struct dentry *dentry;
164 struct cftype *type;
165};
166
167/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700168 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
169 * cgroup_subsys->use_id != 0.
170 */
171#define CSS_ID_MAX (65535)
172struct css_id {
173 /*
174 * The css to which this ID points. This pointer is set to valid value
175 * after cgroup is populated. If cgroup is removed, this will be NULL.
176 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800177 * is called after synchronize_rcu(). But for safe use, css_tryget()
178 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700179 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100180 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700181 /*
182 * ID of this css.
183 */
184 unsigned short id;
185 /*
186 * Depth in hierarchy which this ID belongs to.
187 */
188 unsigned short depth;
189 /*
190 * ID is freed by RCU. (and lookup routine is RCU safe.)
191 */
192 struct rcu_head rcu_head;
193 /*
194 * Hierarchy of CSS ID belongs to.
195 */
196 unsigned short stack[0]; /* Array of Length (depth+1) */
197};
198
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800199/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300200 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800201 */
202struct cgroup_event {
203 /*
204 * Cgroup which the event belongs to.
205 */
206 struct cgroup *cgrp;
207 /*
208 * Control file which the event associated.
209 */
210 struct cftype *cft;
211 /*
212 * eventfd to signal userspace about the event.
213 */
214 struct eventfd_ctx *eventfd;
215 /*
216 * Each of these stored in a list by the cgroup.
217 */
218 struct list_head list;
219 /*
220 * All fields below needed to unregister event when
221 * userspace closes eventfd.
222 */
223 poll_table pt;
224 wait_queue_head_t *wqh;
225 wait_queue_t wait;
226 struct work_struct remove;
227};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* The list of hierarchy roots */
230
231static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700232static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700234static DEFINE_IDA(hierarchy_ida);
235static int next_hierarchy_id;
236static DEFINE_SPINLOCK(hierarchy_id_lock);
237
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
239#define dummytop (&rootnode.top_cgroup)
240
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
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332/* the list of cgroups eligible for automatic release. Protected by
333 * release_list_lock */
334static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200335static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336static void cgroup_release_agent(struct work_struct *work);
337static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700338static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700339
Paul Menage817929e2007-10-18 23:39:36 -0700340/* Link structure for associating css_set objects with cgroups */
341struct cg_cgroup_link {
342 /*
343 * List running through cg_cgroup_links associated with a
344 * cgroup, anchored on cgroup->css_sets
345 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700346 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700347 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700348 /*
349 * List running through cg_cgroup_links pointing at a
350 * single css_set object, anchored on css_set->cg_links
351 */
352 struct list_head cg_link_list;
353 struct css_set *cg;
354};
355
356/* The default css_set - used by init and its children prior to any
357 * hierarchies being mounted. It contains a pointer to the root state
358 * for each subsystem. Also used to anchor the list of css_sets. Not
359 * reference-counted, to improve performance when child cgroups
360 * haven't been created.
361 */
362
363static struct css_set init_css_set;
364static struct cg_cgroup_link init_css_set_link;
365
Ben Blume6a11052010-03-10 15:22:09 -0800366static int cgroup_init_idr(struct cgroup_subsys *ss,
367 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700368
Paul Menage817929e2007-10-18 23:39:36 -0700369/* css_set_lock protects the list of css_set objects, and the
370 * chain of tasks off each css_set. Nests outside task->alloc_lock
371 * due to cgroup_iter_start() */
372static DEFINE_RWLOCK(css_set_lock);
373static int css_set_count;
374
Paul Menage7717f7b2009-09-23 15:56:22 -0700375/*
376 * hash table for cgroup groups. This improves the performance to find
377 * an existing css_set. This hash doesn't (currently) take into
378 * account cgroups in empty hierarchies.
379 */
Li Zefan472b1052008-04-29 01:00:11 -0700380#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800381static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700382
Li Zefan0ac801f2013-01-10 11:49:27 +0800383static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700384{
385 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700387
388 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800389 key += (unsigned long)css[i];
390 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700391
Li Zefan0ac801f2013-01-10 11:49:27 +0800392 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700399static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700400
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700402{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700403 struct cg_cgroup_link *link;
404 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 /*
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
408 * rwlock
409 */
410 if (atomic_add_unless(&cg->refcount, -1, 1))
411 return;
412 write_lock(&css_set_lock);
413 if (!atomic_dec_and_test(&cg->refcount)) {
414 write_unlock(&css_set_lock);
415 return;
416 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800419 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 css_set_count--;
421
422 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
423 cg_link_list) {
424 struct cgroup *cgrp = link->cgrp;
425 list_del(&link->cg_link_list);
426 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800427
428 /*
429 * We may not be holding cgroup_mutex, and if cgrp->count is
430 * dropped to 0 the cgroup can be destroyed at any time, hence
431 * rcu_read_lock is used to keep it alive.
432 */
433 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700434 if (atomic_dec_and_test(&cgrp->count) &&
435 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700437 set_bit(CGRP_RELEASABLE, &cgrp->flags);
438 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700439 }
Li Zefan71b57072013-01-24 14:43:28 +0800440 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700441
442 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700443 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700444
445 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800446 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700447}
448
449/*
450 * refcounted get/put for css_set objects
451 */
452static inline void get_css_set(struct css_set *cg)
453{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700454 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
457static inline void put_css_set(struct css_set *cg)
458{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700459 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700460}
461
Paul Menage81a6a5c2007-10-18 23:39:38 -0700462static inline void put_css_set_taskexit(struct css_set *cg)
463{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700464 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700465}
466
Paul Menage817929e2007-10-18 23:39:36 -0700467/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700468 * compare_css_sets - helper function for find_existing_css_set().
469 * @cg: candidate css_set being tested
470 * @old_cg: existing css_set for a task
471 * @new_cgrp: cgroup that's being entered by the task
472 * @template: desired set of css pointers in css_set (pre-calculated)
473 *
474 * Returns true if "cg" matches "old_cg" except for the hierarchy
475 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
476 */
477static bool compare_css_sets(struct css_set *cg,
478 struct css_set *old_cg,
479 struct cgroup *new_cgrp,
480 struct cgroup_subsys_state *template[])
481{
482 struct list_head *l1, *l2;
483
484 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
485 /* Not all subsystems matched */
486 return false;
487 }
488
489 /*
490 * Compare cgroup pointers in order to distinguish between
491 * different cgroups in heirarchies with no subsystems. We
492 * could get by with just this check alone (and skip the
493 * memcmp above) but on most setups the memcmp check will
494 * avoid the need for this more expensive check on almost all
495 * candidates.
496 */
497
498 l1 = &cg->cg_links;
499 l2 = &old_cg->cg_links;
500 while (1) {
501 struct cg_cgroup_link *cgl1, *cgl2;
502 struct cgroup *cg1, *cg2;
503
504 l1 = l1->next;
505 l2 = l2->next;
506 /* See if we reached the end - both lists are equal length. */
507 if (l1 == &cg->cg_links) {
508 BUG_ON(l2 != &old_cg->cg_links);
509 break;
510 } else {
511 BUG_ON(l2 == &old_cg->cg_links);
512 }
513 /* Locate the cgroups associated with these links. */
514 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
515 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
516 cg1 = cgl1->cgrp;
517 cg2 = cgl2->cgrp;
518 /* Hierarchies should be linked in the same order. */
519 BUG_ON(cg1->root != cg2->root);
520
521 /*
522 * If this hierarchy is the hierarchy of the cgroup
523 * that's changing, then we need to check that this
524 * css_set points to the new cgroup; if it's any other
525 * hierarchy, then this css_set should point to the
526 * same cgroup as the old css_set.
527 */
528 if (cg1->root == new_cgrp->root) {
529 if (cg1 != new_cgrp)
530 return false;
531 } else {
532 if (cg1 != cg2)
533 return false;
534 }
535 }
536 return true;
537}
538
539/*
Paul Menage817929e2007-10-18 23:39:36 -0700540 * find_existing_css_set() is a helper for
541 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700542 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700543 *
544 * oldcg: the cgroup group that we're using before the cgroup
545 * transition
546 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700547 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700548 *
549 * template: location in which to build the desired set of subsystem
550 * state objects for the new cgroup group
551 */
Paul Menage817929e2007-10-18 23:39:36 -0700552static struct css_set *find_existing_css_set(
553 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700554 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700555 struct cgroup_subsys_state *template[])
556{
557 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700558 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700559 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800560 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700561
Ben Blumaae8aab2010-03-10 15:22:07 -0800562 /*
563 * Build the set of subsystem state objects that we want to see in the
564 * new css_set. while subsystems can change globally, the entries here
565 * won't change, so no need for locking.
566 */
Paul Menage817929e2007-10-18 23:39:36 -0700567 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400568 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700569 /* Subsystem is in this hierarchy. So we want
570 * the subsystem state from the new
571 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700572 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700573 } else {
574 /* Subsystem is not in this hierarchy, so we
575 * don't want to change the subsystem state */
576 template[i] = oldcg->subsys[i];
577 }
578 }
579
Li Zefan0ac801f2013-01-10 11:49:27 +0800580 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800581 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700582 if (!compare_css_sets(cg, oldcg, cgrp, template))
583 continue;
584
585 /* This css_set matches what we need */
586 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700587 }
Paul Menage817929e2007-10-18 23:39:36 -0700588
589 /* No existing cgroup group matched */
590 return NULL;
591}
592
Paul Menage817929e2007-10-18 23:39:36 -0700593static void free_cg_links(struct list_head *tmp)
594{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700595 struct cg_cgroup_link *link;
596 struct cg_cgroup_link *saved_link;
597
598 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700599 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700600 kfree(link);
601 }
602}
603
604/*
Li Zefan36553432008-07-29 22:33:19 -0700605 * allocate_cg_links() allocates "count" cg_cgroup_link structures
606 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
607 * success or a negative error
608 */
609static int allocate_cg_links(int count, struct list_head *tmp)
610{
611 struct cg_cgroup_link *link;
612 int i;
613 INIT_LIST_HEAD(tmp);
614 for (i = 0; i < count; i++) {
615 link = kmalloc(sizeof(*link), GFP_KERNEL);
616 if (!link) {
617 free_cg_links(tmp);
618 return -ENOMEM;
619 }
620 list_add(&link->cgrp_link_list, tmp);
621 }
622 return 0;
623}
624
Li Zefanc12f65d2009-01-07 18:07:42 -0800625/**
626 * link_css_set - a helper function to link a css_set to a cgroup
627 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
628 * @cg: the css_set to be linked
629 * @cgrp: the destination cgroup
630 */
631static void link_css_set(struct list_head *tmp_cg_links,
632 struct css_set *cg, struct cgroup *cgrp)
633{
634 struct cg_cgroup_link *link;
635
636 BUG_ON(list_empty(tmp_cg_links));
637 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
638 cgrp_link_list);
639 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700640 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700641 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800642 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700643 /*
644 * Always add links to the tail of the list so that the list
645 * is sorted by order of hierarchy creation
646 */
647 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800648}
649
Li Zefan36553432008-07-29 22:33:19 -0700650/*
Paul Menage817929e2007-10-18 23:39:36 -0700651 * find_css_set() takes an existing cgroup group and a
652 * cgroup object, and returns a css_set object that's
653 * equivalent to the old group, but with the given cgroup
654 * substituted into the appropriate hierarchy. Must be called with
655 * cgroup_mutex held
656 */
Paul Menage817929e2007-10-18 23:39:36 -0700657static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700658 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700659{
660 struct css_set *res;
661 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700662
663 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700664
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800666 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700667
Paul Menage817929e2007-10-18 23:39:36 -0700668 /* First see if we already have a cgroup group that matches
669 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700670 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700671 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700672 if (res)
673 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700674 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700675
676 if (res)
677 return res;
678
679 res = kmalloc(sizeof(*res), GFP_KERNEL);
680 if (!res)
681 return NULL;
682
683 /* Allocate all the cg_cgroup_link objects that we'll need */
684 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
685 kfree(res);
686 return NULL;
687 }
688
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700689 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700690 INIT_LIST_HEAD(&res->cg_links);
691 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700692 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700693
694 /* Copy the set of subsystem state objects generated in
695 * find_existing_css_set() */
696 memcpy(res->subsys, template, sizeof(res->subsys));
697
698 write_lock(&css_set_lock);
699 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700700 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
701 struct cgroup *c = link->cgrp;
702 if (c->root == cgrp->root)
703 c = cgrp;
704 link_css_set(&tmp_cg_links, res, c);
705 }
Paul Menage817929e2007-10-18 23:39:36 -0700706
707 BUG_ON(!list_empty(&tmp_cg_links));
708
Paul Menage817929e2007-10-18 23:39:36 -0700709 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700710
711 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800712 key = css_set_hash(res->subsys);
713 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700714
Paul Menage817929e2007-10-18 23:39:36 -0700715 write_unlock(&css_set_lock);
716
717 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700718}
719
Paul Menageddbcc7e2007-10-18 23:39:30 -0700720/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 * Return the cgroup for "task" from the given hierarchy. Must be
722 * called with cgroup_mutex held.
723 */
724static struct cgroup *task_cgroup_from_root(struct task_struct *task,
725 struct cgroupfs_root *root)
726{
727 struct css_set *css;
728 struct cgroup *res = NULL;
729
730 BUG_ON(!mutex_is_locked(&cgroup_mutex));
731 read_lock(&css_set_lock);
732 /*
733 * No need to lock the task - since we hold cgroup_mutex the
734 * task can't change groups, so the only thing that can happen
735 * is that it exits and its css is set back to init_css_set.
736 */
737 css = task->cgroups;
738 if (css == &init_css_set) {
739 res = &root->top_cgroup;
740 } else {
741 struct cg_cgroup_link *link;
742 list_for_each_entry(link, &css->cg_links, cg_link_list) {
743 struct cgroup *c = link->cgrp;
744 if (c->root == root) {
745 res = c;
746 break;
747 }
748 }
749 }
750 read_unlock(&css_set_lock);
751 BUG_ON(!res);
752 return res;
753}
754
755/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 * There is one global cgroup mutex. We also require taking
757 * task_lock() when dereferencing a task's cgroup subsys pointers.
758 * See "The task_lock() exception", at the end of this comment.
759 *
760 * A task must hold cgroup_mutex to modify cgroups.
761 *
762 * Any task can increment and decrement the count field without lock.
763 * So in general, code holding cgroup_mutex can't rely on the count
764 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800765 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * means that no tasks are currently attached, therefore there is no
767 * way a task attached to that cgroup can fork (the other way to
768 * increment the count). So code holding cgroup_mutex can safely
769 * assume that if the count is zero, it will stay zero. Similarly, if
770 * a task holds cgroup_mutex on a cgroup with zero count, it
771 * knows that the cgroup won't be removed, as cgroup_rmdir()
772 * needs that mutex.
773 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
775 * (usually) take cgroup_mutex. These are the two most performance
776 * critical pieces of code here. The exception occurs on cgroup_exit(),
777 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
778 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800779 * to the release agent with the name of the cgroup (path relative to
780 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 *
782 * A cgroup can only be deleted if both its 'count' of using tasks
783 * is zero, and its list of 'children' cgroups is empty. Since all
784 * tasks in the system use _some_ cgroup, and since there is always at
785 * least one task in the system (init, pid == 1), therefore, top_cgroup
786 * always has either children cgroups and/or using tasks. So we don't
787 * need a special hack to ensure that top_cgroup cannot be deleted.
788 *
789 * The task_lock() exception
790 *
791 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800792 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800793 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794 * several performance critical places that need to reference
795 * task->cgroup without the expense of grabbing a system global
796 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800797 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
799 * the task_struct routinely used for such matters.
800 *
801 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800802 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 */
804
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805/**
806 * cgroup_lock - lock out any changes to cgroup structures
807 *
808 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809void cgroup_lock(void)
810{
811 mutex_lock(&cgroup_mutex);
812}
Ben Blum67523c42010-03-10 15:22:11 -0800813EXPORT_SYMBOL_GPL(cgroup_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814
815/**
816 * cgroup_unlock - release lock on cgroup changes
817 *
818 * Undo the lock taken in a previous cgroup_lock() call.
819 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820void cgroup_unlock(void)
821{
822 mutex_unlock(&cgroup_mutex);
823}
Ben Blum67523c42010-03-10 15:22:11 -0800824EXPORT_SYMBOL_GPL(cgroup_unlock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825
826/*
827 * A couple of forward declarations required, due to cyclic reference loop:
828 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
829 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
830 * -> cgroup_mkdir.
831 */
832
Al Viro18bb1db2011-07-26 01:41:39 -0400833static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400834static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700835static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400836static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
837 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700838static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700839static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700840
841static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200842 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700843 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700844};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700845
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700846static int alloc_css_id(struct cgroup_subsys *ss,
847 struct cgroup *parent, struct cgroup *child);
848
Al Viroa5e7ed32011-07-26 01:55:55 -0400849static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700850{
851 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852
853 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400854 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700855 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100856 inode->i_uid = current_fsuid();
857 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700858 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
859 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
860 }
861 return inode;
862}
863
Li Zefan65dff752013-03-01 15:01:56 +0800864static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
865{
866 struct cgroup_name *name;
867
868 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
869 if (!name)
870 return NULL;
871 strcpy(name->name, dentry->d_name.name);
872 return name;
873}
874
Li Zefanbe445622013-01-24 14:31:42 +0800875static void cgroup_free_fn(struct work_struct *work)
876{
877 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
878 struct cgroup_subsys *ss;
879
880 mutex_lock(&cgroup_mutex);
881 /*
882 * Release the subsystem state objects.
883 */
884 for_each_subsys(cgrp->root, ss)
885 ss->css_free(cgrp);
886
887 cgrp->root->number_of_cgroups--;
888 mutex_unlock(&cgroup_mutex);
889
890 /*
891 * Drop the active superblock reference that we took when we
892 * created the cgroup
893 */
894 deactivate_super(cgrp->root->sb);
895
896 /*
897 * if we're getting rid of the cgroup, refcount should ensure
898 * that there are no pidlists left.
899 */
900 BUG_ON(!list_empty(&cgrp->pidlists));
901
902 simple_xattrs_free(&cgrp->xattrs);
903
904 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +0800905 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800906 kfree(cgrp);
907}
908
909static void cgroup_free_rcu(struct rcu_head *head)
910{
911 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
912
913 schedule_work(&cgrp->free_work);
914}
915
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916static void cgroup_diput(struct dentry *dentry, struct inode *inode)
917{
918 /* is dentry a directory ? if so, kfree() associated cgroup */
919 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700920 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800921
Paul Menagebd89aab2007-10-18 23:40:44 -0700922 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800923 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924 } else {
925 struct cfent *cfe = __d_cfe(dentry);
926 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400927 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928
929 WARN_ONCE(!list_empty(&cfe->node) &&
930 cgrp != &cgrp->root->top_cgroup,
931 "cfe still linked for %s\n", cfe->type->name);
932 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400933 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934 }
935 iput(inode);
936}
937
Al Viroc72a04e2011-01-14 05:31:45 +0000938static int cgroup_delete(const struct dentry *d)
939{
940 return 1;
941}
942
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943static void remove_dir(struct dentry *d)
944{
945 struct dentry *parent = dget(d->d_parent);
946
947 d_delete(d);
948 simple_rmdir(parent->d_inode, d);
949 dput(parent);
950}
951
Li Zefan2739d3c2013-01-21 18:18:33 +0800952static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700954 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
957 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100958
Li Zefan2739d3c2013-01-21 18:18:33 +0800959 /*
960 * If we're doing cleanup due to failure of cgroup_create(),
961 * the corresponding @cfe may not exist.
962 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700963 list_for_each_entry(cfe, &cgrp->files, node) {
964 struct dentry *d = cfe->dentry;
965
966 if (cft && cfe->type != cft)
967 continue;
968
969 dget(d);
970 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700971 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700972 list_del_init(&cfe->node);
973 dput(d);
974
Li Zefan2739d3c2013-01-21 18:18:33 +0800975 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700977}
978
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979/**
980 * cgroup_clear_directory - selective removal of base and subsystem files
981 * @dir: directory containing the files
982 * @base_files: true if the base files should be removed
983 * @subsys_mask: mask of the subsystem ids whose files should be removed
984 */
985static void cgroup_clear_directory(struct dentry *dir, bool base_files,
986 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700987{
988 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400989 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700990
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400991 for_each_subsys(cgrp->root, ss) {
992 struct cftype_set *set;
993 if (!test_bit(ss->subsys_id, &subsys_mask))
994 continue;
995 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800996 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400997 }
998 if (base_files) {
999 while (!list_empty(&cgrp->files))
1000 cgroup_rm_file(cgrp, NULL);
1001 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002}
1003
1004/*
1005 * NOTE : the dentry must have been dget()'ed
1006 */
1007static void cgroup_d_remove_dir(struct dentry *dentry)
1008{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001009 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001010 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001011
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001012 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001014 parent = dentry->d_parent;
1015 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001016 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001018 spin_unlock(&dentry->d_lock);
1019 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 remove_dir(dentry);
1021}
1022
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001023/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001024 * Call with cgroup_mutex held. Drops reference counts on modules, including
1025 * any duplicate ones that parse_cgroupfs_options took. If this function
1026 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001027 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001029 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001031 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001032 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 int i;
1034
Ben Blumaae8aab2010-03-10 15:22:07 -08001035 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001036 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001037
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001038 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1039 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 /* Check that any added subsystems are currently free */
1041 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001042 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001044 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001046 /*
1047 * Nobody should tell us to do a subsys that doesn't exist:
1048 * parse_cgroupfs_options should catch that case and refcounts
1049 * ensure that subsystems won't disappear once selected.
1050 */
1051 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->root != &rootnode) {
1053 /* Subsystem isn't free */
1054 return -EBUSY;
1055 }
1056 }
1057
1058 /* Currently we don't handle adding/removing subsystems when
1059 * any child cgroups exist. This is theoretically supportable
1060 * but involves complex error handling, so it's being left until
1061 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001062 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 return -EBUSY;
1064
1065 /* Process each subsystem */
1066 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1067 struct cgroup_subsys *ss = subsys[i];
1068 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001069 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001071 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001072 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 BUG_ON(!dummytop->subsys[i]);
1074 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001075 cgrp->subsys[i] = dummytop->subsys[i];
1076 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001077 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001078 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001080 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001081 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001082 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001084 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001085 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1086 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001088 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001090 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001091 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001092 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001093 /* subsystem is now free - drop reference on module */
1094 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001095 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001097 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001098 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001099 /*
1100 * a refcount was taken, but we already had one, so
1101 * drop the extra reference.
1102 */
1103 module_put(ss->module);
1104#ifdef CONFIG_MODULE_UNLOAD
1105 BUG_ON(ss->module && !module_refcount(ss->module));
1106#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 } else {
1108 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001109 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 }
1111 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001112 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113
1114 return 0;
1115}
1116
Al Viro34c80b12011-12-08 21:32:45 -05001117static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118{
Al Viro34c80b12011-12-08 21:32:45 -05001119 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120 struct cgroup_subsys *ss;
1121
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001122 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 for_each_subsys(root, ss)
1124 seq_printf(seq, ",%s", ss->name);
1125 if (test_bit(ROOT_NOPREFIX, &root->flags))
1126 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001127 if (test_bit(ROOT_XATTR, &root->flags))
1128 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001129 if (strlen(root->release_agent_path))
1130 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001131 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001132 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001133 if (strlen(root->name))
1134 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001135 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136 return 0;
1137}
1138
1139struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001140 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001142 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001143 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001145 /* User explicitly requested empty subsystem */
1146 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001147
1148 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001149
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150};
1151
Ben Blumaae8aab2010-03-10 15:22:07 -08001152/*
1153 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001154 * with cgroup_mutex held to protect the subsys[] array. This function takes
1155 * refcounts on subsystems to be used, unless it returns error, in which case
1156 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001157 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001158static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 char *token, *o = data;
1161 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001162 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001163 int i;
1164 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001165
Ben Blumaae8aab2010-03-10 15:22:07 -08001166 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1167
Li Zefanf9ab5b52009-06-17 16:26:33 -07001168#ifdef CONFIG_CPUSETS
1169 mask = ~(1UL << cpuset_subsys_id);
1170#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001171
Paul Menagec6d57f32009-09-23 15:56:19 -07001172 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001173
1174 while ((token = strsep(&o, ",")) != NULL) {
1175 if (!*token)
1176 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001178 /* Explicitly have no subsystems */
1179 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 continue;
1181 }
1182 if (!strcmp(token, "all")) {
1183 /* Mutually exclusive option 'all' + subsystem name */
1184 if (one_ss)
1185 return -EINVAL;
1186 all_ss = true;
1187 continue;
1188 }
1189 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001190 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191 continue;
1192 }
1193 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001194 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 continue;
1196 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001197 if (!strcmp(token, "xattr")) {
1198 set_bit(ROOT_XATTR, &opts->flags);
1199 continue;
1200 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001202 /* Specifying two release agents is forbidden */
1203 if (opts->release_agent)
1204 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001205 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001206 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001207 if (!opts->release_agent)
1208 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001209 continue;
1210 }
1211 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001212 const char *name = token + 5;
1213 /* Can't specify an empty name */
1214 if (!strlen(name))
1215 return -EINVAL;
1216 /* Must match [\w.-]+ */
1217 for (i = 0; i < strlen(name); i++) {
1218 char c = name[i];
1219 if (isalnum(c))
1220 continue;
1221 if ((c == '.') || (c == '-') || (c == '_'))
1222 continue;
1223 return -EINVAL;
1224 }
1225 /* Specifying two names is forbidden */
1226 if (opts->name)
1227 return -EINVAL;
1228 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001229 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001230 GFP_KERNEL);
1231 if (!opts->name)
1232 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001233
1234 continue;
1235 }
1236
1237 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1238 struct cgroup_subsys *ss = subsys[i];
1239 if (ss == NULL)
1240 continue;
1241 if (strcmp(token, ss->name))
1242 continue;
1243 if (ss->disabled)
1244 continue;
1245
1246 /* Mutually exclusive option 'all' + subsystem name */
1247 if (all_ss)
1248 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001249 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001250 one_ss = true;
1251
1252 break;
1253 }
1254 if (i == CGROUP_SUBSYS_COUNT)
1255 return -ENOENT;
1256 }
1257
1258 /*
1259 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001260 * otherwise if 'none', 'name=' and a subsystem name options
1261 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001262 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001263 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001264 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1265 struct cgroup_subsys *ss = subsys[i];
1266 if (ss == NULL)
1267 continue;
1268 if (ss->disabled)
1269 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001270 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271 }
1272 }
1273
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001274 /* Consistency checks */
1275
Li Zefanf9ab5b52009-06-17 16:26:33 -07001276 /*
1277 * Option noprefix was introduced just for backward compatibility
1278 * with the old cpuset, so we allow noprefix only if mounting just
1279 * the cpuset subsystem.
1280 */
1281 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001282 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001283 return -EINVAL;
1284
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001285
1286 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001287 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001288 return -EINVAL;
1289
1290 /*
1291 * We either have to specify by name or by subsystems. (So all
1292 * empty hierarchies must have a name).
1293 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001294 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295 return -EINVAL;
1296
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 /*
1298 * Grab references on all the modules we'll need, so the subsystems
1299 * don't dance around before rebind_subsystems attaches them. This may
1300 * take duplicate reference counts on a subsystem that's already used,
1301 * but rebind_subsystems handles this case.
1302 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001303 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001304 unsigned long bit = 1UL << i;
1305
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001306 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 continue;
1308 if (!try_module_get(subsys[i]->module)) {
1309 module_pin_failed = true;
1310 break;
1311 }
1312 }
1313 if (module_pin_failed) {
1314 /*
1315 * oops, one of the modules was going away. this means that we
1316 * raced with a module_delete call, and to the user this is
1317 * essentially a "subsystem doesn't exist" case.
1318 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001319 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 /* drop refcounts only on the ones we took */
1321 unsigned long bit = 1UL << i;
1322
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001323 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001324 continue;
1325 module_put(subsys[i]->module);
1326 }
1327 return -ENOENT;
1328 }
1329
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 return 0;
1331}
1332
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001333static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001334{
1335 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001336 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001337 unsigned long bit = 1UL << i;
1338
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001339 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001340 continue;
1341 module_put(subsys[i]->module);
1342 }
1343}
1344
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1346{
1347 int ret = 0;
1348 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001349 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001351 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352
Paul Menagebd89aab2007-10-18 23:40:44 -07001353 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001355 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356
1357 /* See what subsystems are wanted */
1358 ret = parse_cgroupfs_options(data, &opts);
1359 if (ret)
1360 goto out_unlock;
1361
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001363 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1364 task_tgid_nr(current), current->comm);
1365
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001366 added_mask = opts.subsys_mask & ~root->subsys_mask;
1367 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001368
Ben Blumcf5d5942010-03-10 15:22:09 -08001369 /* Don't allow flags or name to change at remount */
1370 if (opts.flags != root->flags ||
1371 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001373 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001374 goto out_unlock;
1375 }
1376
Gao feng7083d032012-12-03 09:28:18 +08001377 /*
1378 * Clear out the files of subsystems that should be removed, do
1379 * this before rebind_subsystems, since rebind_subsystems may
1380 * change this hierarchy's subsys_list.
1381 */
1382 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1383
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001384 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001385 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001386 /* rebind_subsystems failed, re-populate the removed files */
1387 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001388 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001389 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001390 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001392 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001393 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394
Paul Menage81a6a5c2007-10-18 23:39:38 -07001395 if (opts.release_agent)
1396 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001398 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001399 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001400 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001402 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 return ret;
1404}
1405
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001406static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407 .statfs = simple_statfs,
1408 .drop_inode = generic_delete_inode,
1409 .show_options = cgroup_show_options,
1410 .remount_fs = cgroup_remount,
1411};
1412
Paul Menagecc31edc2008-10-18 20:28:04 -07001413static void init_cgroup_housekeeping(struct cgroup *cgrp)
1414{
1415 INIT_LIST_HEAD(&cgrp->sibling);
1416 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001417 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001418 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001419 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001420 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001421 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001422 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001423 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001424 INIT_LIST_HEAD(&cgrp->event_list);
1425 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001426 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001427}
Paul Menagec6d57f32009-09-23 15:56:19 -07001428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429static void init_cgroup_root(struct cgroupfs_root *root)
1430{
Paul Menagebd89aab2007-10-18 23:40:44 -07001431 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001432
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433 INIT_LIST_HEAD(&root->subsys_list);
1434 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001435 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001437 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001438 cgrp->name = &root_cgroup_name;
Paul Menagebd89aab2007-10-18 23:40:44 -07001439 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001440 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001441 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442}
1443
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001444static bool init_root_id(struct cgroupfs_root *root)
1445{
1446 int ret = 0;
1447
1448 do {
1449 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1450 return false;
1451 spin_lock(&hierarchy_id_lock);
1452 /* Try to allocate the next unused ID */
1453 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1454 &root->hierarchy_id);
1455 if (ret == -ENOSPC)
1456 /* Try again starting from 0 */
1457 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1458 if (!ret) {
1459 next_hierarchy_id = root->hierarchy_id + 1;
1460 } else if (ret != -EAGAIN) {
1461 /* Can only get here if the 31-bit IDR is full ... */
1462 BUG_ON(ret);
1463 }
1464 spin_unlock(&hierarchy_id_lock);
1465 } while (ret);
1466 return true;
1467}
1468
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469static int cgroup_test_super(struct super_block *sb, void *data)
1470{
Paul Menagec6d57f32009-09-23 15:56:19 -07001471 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 struct cgroupfs_root *root = sb->s_fs_info;
1473
Paul Menagec6d57f32009-09-23 15:56:19 -07001474 /* If we asked for a name then it must match */
1475 if (opts->name && strcmp(opts->name, root->name))
1476 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001478 /*
1479 * If we asked for subsystems (or explicitly for no
1480 * subsystems) then they must match
1481 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001482 if ((opts->subsys_mask || opts->none)
1483 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 return 0;
1485
1486 return 1;
1487}
1488
Paul Menagec6d57f32009-09-23 15:56:19 -07001489static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1490{
1491 struct cgroupfs_root *root;
1492
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001493 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 return NULL;
1495
1496 root = kzalloc(sizeof(*root), GFP_KERNEL);
1497 if (!root)
1498 return ERR_PTR(-ENOMEM);
1499
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001500 if (!init_root_id(root)) {
1501 kfree(root);
1502 return ERR_PTR(-ENOMEM);
1503 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001505
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001506 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001508 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 if (opts->release_agent)
1510 strcpy(root->release_agent_path, opts->release_agent);
1511 if (opts->name)
1512 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001513 if (opts->cpuset_clone_children)
1514 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 return root;
1516}
1517
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001518static void cgroup_drop_root(struct cgroupfs_root *root)
1519{
1520 if (!root)
1521 return;
1522
1523 BUG_ON(!root->hierarchy_id);
1524 spin_lock(&hierarchy_id_lock);
1525 ida_remove(&hierarchy_ida, root->hierarchy_id);
1526 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001527 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001528 kfree(root);
1529}
1530
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531static int cgroup_set_super(struct super_block *sb, void *data)
1532{
1533 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 struct cgroup_sb_opts *opts = data;
1535
1536 /* If we don't have a new root, we can't set up a new sb */
1537 if (!opts->new_root)
1538 return -EINVAL;
1539
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001540 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541
1542 ret = set_anon_super(sb, NULL);
1543 if (ret)
1544 return ret;
1545
Paul Menagec6d57f32009-09-23 15:56:19 -07001546 sb->s_fs_info = opts->new_root;
1547 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548
1549 sb->s_blocksize = PAGE_CACHE_SIZE;
1550 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1551 sb->s_magic = CGROUP_SUPER_MAGIC;
1552 sb->s_op = &cgroup_ops;
1553
1554 return 0;
1555}
1556
1557static int cgroup_get_rootdir(struct super_block *sb)
1558{
Al Viro0df6a632010-12-21 13:29:29 -05001559 static const struct dentry_operations cgroup_dops = {
1560 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001561 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001562 };
1563
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 struct inode *inode =
1565 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
1567 if (!inode)
1568 return -ENOMEM;
1569
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 inode->i_fop = &simple_dir_operations;
1571 inode->i_op = &cgroup_dir_inode_operations;
1572 /* directories start off with i_nlink == 2 (for "." entry) */
1573 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001574 sb->s_root = d_make_root(inode);
1575 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001577 /* for everything else we want ->d_op set */
1578 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 return 0;
1580}
1581
Al Virof7e83572010-07-26 13:23:11 +04001582static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001584 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585{
1586 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 int ret = 0;
1589 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001591 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
1593 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001594 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001596 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 if (ret)
1598 goto out_err;
1599
1600 /*
1601 * Allocate a new cgroup root. We may not need it if we're
1602 * reusing an existing hierarchy.
1603 */
1604 new_root = cgroup_root_from_opts(&opts);
1605 if (IS_ERR(new_root)) {
1606 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001607 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001608 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
Paul Menagec6d57f32009-09-23 15:56:19 -07001611 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001612 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001614 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001615 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001616 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617 }
1618
Paul Menagec6d57f32009-09-23 15:56:19 -07001619 root = sb->s_fs_info;
1620 BUG_ON(!root);
1621 if (root == opts.new_root) {
1622 /* We used the new root structure, so this is a new hierarchy */
1623 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001624 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001625 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001626 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001627 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001628 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629
1630 BUG_ON(sb->s_root != NULL);
1631
1632 ret = cgroup_get_rootdir(sb);
1633 if (ret)
1634 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001635 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636
Paul Menage817929e2007-10-18 23:39:36 -07001637 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001639 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001641 /* Check for name clashes with existing mounts */
1642 ret = -EBUSY;
1643 if (strlen(root->name))
1644 for_each_active_root(existing_root)
1645 if (!strcmp(existing_root->name, root->name))
1646 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001647
Paul Menage817929e2007-10-18 23:39:36 -07001648 /*
1649 * We're accessing css_set_count without locking
1650 * css_set_lock here, but that's OK - it can only be
1651 * increased by someone holding cgroup_lock, and
1652 * that's us. The worst that can happen is that we
1653 * have some link structures left over
1654 */
1655 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001656 if (ret)
1657 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001658
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001659 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001661 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001662 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001664 /*
1665 * There must be no failure case after here, since rebinding
1666 * takes care of subsystems' refcounts, which are explicitly
1667 * dropped in the failure exit path.
1668 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
1670 /* EBUSY should be the only error here */
1671 BUG_ON(ret);
1672
1673 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001674 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675
Li Zefanc12f65d2009-01-07 18:07:42 -08001676 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 root->top_cgroup.dentry = sb->s_root;
1678
Paul Menage817929e2007-10-18 23:39:36 -07001679 /* Link the top cgroup in this hierarchy into all
1680 * the css_set objects */
1681 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001682 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001683 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001684 write_unlock(&css_set_lock);
1685
1686 free_cg_links(&tmp_cg_links);
1687
Li Zefanc12f65d2009-01-07 18:07:42 -08001688 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 BUG_ON(root->number_of_cgroups != 1);
1690
eparis@redhat2ce97382011-06-02 21:20:51 +10001691 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001692 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001693 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001694 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001696 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001697 } else {
1698 /*
1699 * We re-used an existing hierarchy - the new root (if
1700 * any) is not needed
1701 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001702 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001703 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001704 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 }
1706
Paul Menagec6d57f32009-09-23 15:56:19 -07001707 kfree(opts.release_agent);
1708 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001709 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001711 unlock_drop:
1712 mutex_unlock(&cgroup_root_mutex);
1713 mutex_unlock(&cgroup_mutex);
1714 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001716 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001717 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001718 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001719 out_err:
1720 kfree(opts.release_agent);
1721 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001722 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723}
1724
1725static void cgroup_kill_sb(struct super_block *sb) {
1726 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001727 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001729 struct cg_cgroup_link *link;
1730 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731
1732 BUG_ON(!root);
1733
1734 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001735 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736
1737 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001738 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
1740 /* Rebind all subsystems back to the default hierarchy */
1741 ret = rebind_subsystems(root, 0);
1742 /* Shouldn't be able to fail ... */
1743 BUG_ON(ret);
1744
Paul Menage817929e2007-10-18 23:39:36 -07001745 /*
1746 * Release all the links from css_sets to this hierarchy's
1747 * root cgroup
1748 */
1749 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001750
1751 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1752 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001753 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001754 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001755 kfree(link);
1756 }
1757 write_unlock(&css_set_lock);
1758
Paul Menage839ec542009-01-29 14:25:22 -08001759 if (!list_empty(&root->root_list)) {
1760 list_del(&root->root_list);
1761 root_count--;
1762 }
Li Zefane5f6a862009-01-07 18:07:41 -08001763
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001764 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 mutex_unlock(&cgroup_mutex);
1766
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001767 simple_xattrs_free(&cgrp->xattrs);
1768
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001770 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771}
1772
1773static struct file_system_type cgroup_fs_type = {
1774 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001775 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 .kill_sb = cgroup_kill_sb,
1777};
1778
Greg KH676db4a2010-08-05 13:53:35 -07001779static struct kobject *cgroup_kobj;
1780
Li Zefana043e3b2008-02-23 15:24:09 -08001781/**
1782 * cgroup_path - generate the path of a cgroup
1783 * @cgrp: the cgroup in question
1784 * @buf: the buffer to write the path into
1785 * @buflen: the length of the buffer
1786 *
Li Zefan65dff752013-03-01 15:01:56 +08001787 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1788 *
1789 * We can't generate cgroup path using dentry->d_name, as accessing
1790 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1791 * inode's i_mutex, while on the other hand cgroup_path() can be called
1792 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001794int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795{
Li Zefan65dff752013-03-01 15:01:56 +08001796 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001798
Tao Ma316eb662012-11-08 21:36:38 +08001799 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001800 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001801
Li Zefan65dff752013-03-01 15:01:56 +08001802 rcu_read_lock();
1803 while (cgrp) {
1804 const char *name = cgroup_name(cgrp);
1805 int len;
1806
1807 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001809 goto out;
1810 memcpy(start, name, len);
1811
1812 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001814
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001816 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001818
1819 cgrp = cgrp->parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 }
Li Zefan65dff752013-03-01 15:01:56 +08001821 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001823out:
1824 rcu_read_unlock();
1825 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826}
Ben Blum67523c42010-03-10 15:22:11 -08001827EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828
Ben Blum74a11662011-05-26 16:25:20 -07001829/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001830 * Control Group taskset
1831 */
Tejun Heo134d3372011-12-12 18:12:21 -08001832struct task_and_cgroup {
1833 struct task_struct *task;
1834 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001835 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001836};
1837
Tejun Heo2f7ee562011-12-12 18:12:21 -08001838struct cgroup_taskset {
1839 struct task_and_cgroup single;
1840 struct flex_array *tc_array;
1841 int tc_array_len;
1842 int idx;
1843 struct cgroup *cur_cgrp;
1844};
1845
1846/**
1847 * cgroup_taskset_first - reset taskset and return the first task
1848 * @tset: taskset of interest
1849 *
1850 * @tset iteration is initialized and the first task is returned.
1851 */
1852struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1853{
1854 if (tset->tc_array) {
1855 tset->idx = 0;
1856 return cgroup_taskset_next(tset);
1857 } else {
1858 tset->cur_cgrp = tset->single.cgrp;
1859 return tset->single.task;
1860 }
1861}
1862EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1863
1864/**
1865 * cgroup_taskset_next - iterate to the next task in taskset
1866 * @tset: taskset of interest
1867 *
1868 * Return the next task in @tset. Iteration must have been initialized
1869 * with cgroup_taskset_first().
1870 */
1871struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1872{
1873 struct task_and_cgroup *tc;
1874
1875 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1876 return NULL;
1877
1878 tc = flex_array_get(tset->tc_array, tset->idx++);
1879 tset->cur_cgrp = tc->cgrp;
1880 return tc->task;
1881}
1882EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1883
1884/**
1885 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1886 * @tset: taskset of interest
1887 *
1888 * Return the cgroup for the current (last returned) task of @tset. This
1889 * function must be preceded by either cgroup_taskset_first() or
1890 * cgroup_taskset_next().
1891 */
1892struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1893{
1894 return tset->cur_cgrp;
1895}
1896EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1897
1898/**
1899 * cgroup_taskset_size - return the number of tasks in taskset
1900 * @tset: taskset of interest
1901 */
1902int cgroup_taskset_size(struct cgroup_taskset *tset)
1903{
1904 return tset->tc_array ? tset->tc_array_len : 1;
1905}
1906EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1907
1908
Ben Blum74a11662011-05-26 16:25:20 -07001909/*
1910 * cgroup_task_migrate - move a task from one cgroup to another.
1911 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001912 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001913 */
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001914static void cgroup_task_migrate(struct cgroup *cgrp, struct cgroup *oldcgrp,
1915 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001916{
1917 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001918
1919 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001920 * We are synchronized through threadgroup_lock() against PF_EXITING
1921 * setting such that we can't race against cgroup_exit() changing the
1922 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001923 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001924 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001925 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001926
Ben Blum74a11662011-05-26 16:25:20 -07001927 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001928 rcu_assign_pointer(tsk->cgroups, newcg);
1929 task_unlock(tsk);
1930
1931 /* Update the css_set linked lists if we're using them */
1932 write_lock(&css_set_lock);
1933 if (!list_empty(&tsk->cg_list))
1934 list_move(&tsk->cg_list, &newcg->tasks);
1935 write_unlock(&css_set_lock);
1936
1937 /*
1938 * We just gained a reference on oldcg by taking it from the task. As
1939 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1940 * it here; it will be freed under RCU.
1941 */
Ben Blum74a11662011-05-26 16:25:20 -07001942 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001943 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001944}
1945
Li Zefana043e3b2008-02-23 15:24:09 -08001946/**
1947 * cgroup_attach_task - attach task 'tsk' to cgroup 'cgrp'
1948 * @cgrp: the cgroup the task is attaching to
1949 * @tsk: the task to be attached
Paul Menagebbcb81d2007-10-18 23:39:32 -07001950 *
Tejun Heocd3d0952011-12-12 18:12:21 -08001951 * Call with cgroup_mutex and threadgroup locked. May take task_lock of
1952 * @tsk during call.
Paul Menagebbcb81d2007-10-18 23:39:32 -07001953 */
Cliff Wickman956db3c2008-02-07 00:14:43 -08001954int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001955{
Tejun Heo8f121912012-03-29 22:03:33 -07001956 int retval = 0;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001957 struct cgroup_subsys *ss, *failed_ss = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07001958 struct cgroup *oldcgrp;
Paul Menagebd89aab2007-10-18 23:40:44 -07001959 struct cgroupfs_root *root = cgrp->root;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001960 struct cgroup_taskset tset = { };
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001961 struct css_set *newcg;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001962
Tejun Heocd3d0952011-12-12 18:12:21 -08001963 /* @tsk either already exited or can't exit until the end */
1964 if (tsk->flags & PF_EXITING)
1965 return -ESRCH;
Paul Menagebbcb81d2007-10-18 23:39:32 -07001966
1967 /* Nothing to do if the task is already in that cgroup */
Paul Menage7717f7b2009-09-23 15:56:22 -07001968 oldcgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07001969 if (cgrp == oldcgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07001970 return 0;
1971
Tejun Heo2f7ee562011-12-12 18:12:21 -08001972 tset.single.task = tsk;
1973 tset.single.cgrp = oldcgrp;
1974
Paul Menagebbcb81d2007-10-18 23:39:32 -07001975 for_each_subsys(root, ss) {
1976 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08001977 retval = ss->can_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001978 if (retval) {
1979 /*
1980 * Remember on which subsystem the can_attach()
1981 * failed, so that we only call cancel_attach()
1982 * against the subsystems whose can_attach()
1983 * succeeded. (See below)
1984 */
1985 failed_ss = ss;
1986 goto out;
1987 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07001988 }
1989 }
1990
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001991 newcg = find_css_set(tsk->cgroups, cgrp);
1992 if (!newcg) {
1993 retval = -ENOMEM;
Daisuke Nishimura2468c722010-03-10 15:22:03 -08001994 goto out;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001995 }
1996
1997 cgroup_task_migrate(cgrp, oldcgrp, tsk, newcg);
Paul Menage817929e2007-10-18 23:39:36 -07001998
Paul Menagebbcb81d2007-10-18 23:39:32 -07001999 for_each_subsys(root, ss) {
Paul Jacksone18f6312008-02-07 00:13:44 -08002000 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002001 ss->attach(cgrp, &tset);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002002 }
Ben Blum74a11662011-05-26 16:25:20 -07002003
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002004out:
2005 if (retval) {
2006 for_each_subsys(root, ss) {
2007 if (ss == failed_ss)
2008 /*
2009 * This subsystem was the one that failed the
2010 * can_attach() check earlier, so we don't need
2011 * to call cancel_attach() against it or any
2012 * remaining subsystems.
2013 */
2014 break;
2015 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002016 ss->cancel_attach(cgrp, &tset);
Daisuke Nishimura2468c722010-03-10 15:22:03 -08002017 }
2018 }
2019 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002020}
2021
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002022/**
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002023 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2024 * @from: attach to all cgroups of a given task
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002025 * @tsk: the task to be attached
2026 */
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002027int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002028{
2029 struct cgroupfs_root *root;
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002030 int retval = 0;
2031
2032 cgroup_lock();
2033 for_each_active_root(root) {
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002034 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2035
2036 retval = cgroup_attach_task(from_cg, tsk);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002037 if (retval)
2038 break;
2039 }
2040 cgroup_unlock();
2041
2042 return retval;
2043}
Michael S. Tsirkin31583bb2010-09-09 16:37:37 -07002044EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
Sridhar Samudralad7926ee2010-05-30 22:24:39 +02002045
Ben Blum74a11662011-05-26 16:25:20 -07002046/**
2047 * cgroup_attach_proc - attach all threads in a threadgroup to a cgroup
2048 * @cgrp: the cgroup to attach to
2049 * @leader: the threadgroup leader task_struct of the group to be attached
2050 *
Tejun Heo257058a2011-12-12 18:12:21 -08002051 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
2052 * task_lock of each thread in leader's threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002053 */
Kirill A. Shutemov1c6c3fa2011-12-27 07:46:25 +02002054static int cgroup_attach_proc(struct cgroup *cgrp, struct task_struct *leader)
Ben Blum74a11662011-05-26 16:25:20 -07002055{
2056 int retval, i, group_size;
2057 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002058 /* guaranteed to be initialized later, but the compiler needs this */
Ben Blum74a11662011-05-26 16:25:20 -07002059 struct cgroupfs_root *root = cgrp->root;
2060 /* threadgroup list cursor and array */
2061 struct task_struct *tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002062 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002063 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002064 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002065
2066 /*
2067 * step 0: in order to do expensive, possibly blocking operations for
2068 * every thread, we cannot iterate the thread group list, since it needs
2069 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002070 * group - group_rwsem prevents new threads from appearing, and if
2071 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002072 */
2073 group_size = get_nr_threads(leader);
Ben Blumd8466872011-05-26 16:25:21 -07002074 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002075 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002076 if (!group)
2077 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002078 /* pre-allocate to guarantee space while iterating in rcu read-side. */
2079 retval = flex_array_prealloc(group, 0, group_size - 1, GFP_KERNEL);
2080 if (retval)
2081 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002082
Ben Blum74a11662011-05-26 16:25:20 -07002083 tsk = leader;
2084 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002085 /*
2086 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2087 * already PF_EXITING could be freed from underneath us unless we
2088 * take an rcu_read_lock.
2089 */
2090 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002091 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002092 struct task_and_cgroup ent;
2093
Tejun Heocd3d0952011-12-12 18:12:21 -08002094 /* @tsk either already exited or can't exit until the end */
2095 if (tsk->flags & PF_EXITING)
2096 continue;
2097
Ben Blum74a11662011-05-26 16:25:20 -07002098 /* as per above, nr_threads may decrease, but not increase. */
2099 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002100 ent.task = tsk;
2101 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002102 /* nothing to do if this task is already in the cgroup */
2103 if (ent.cgrp == cgrp)
2104 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002105 /*
2106 * saying GFP_ATOMIC has no effect here because we did prealloc
2107 * earlier, but it's good form to communicate our expectations.
2108 */
Tejun Heo134d3372011-12-12 18:12:21 -08002109 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002110 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002111 i++;
2112 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002113 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002114 /* remember the number of threads in the array for later. */
2115 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002116 tset.tc_array = group;
2117 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002118
Tejun Heo134d3372011-12-12 18:12:21 -08002119 /* methods shouldn't be called if no task is actually migrating */
2120 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002121 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002122 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002123
Ben Blum74a11662011-05-26 16:25:20 -07002124 /*
2125 * step 1: check that we can legitimately attach to the cgroup.
2126 */
2127 for_each_subsys(root, ss) {
2128 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002129 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002130 if (retval) {
2131 failed_ss = ss;
2132 goto out_cancel_attach;
2133 }
2134 }
Ben Blum74a11662011-05-26 16:25:20 -07002135 }
2136
2137 /*
2138 * step 2: make sure css_sets exist for all threads to be migrated.
2139 * we use find_css_set, which allocates a new one if necessary.
2140 */
Ben Blum74a11662011-05-26 16:25:20 -07002141 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002142 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002143 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2144 if (!tc->cg) {
2145 retval = -ENOMEM;
2146 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002147 }
2148 }
2149
2150 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002151 * step 3: now that we're guaranteed success wrt the css_sets,
2152 * proceed to move all tasks to the new cgroup. There are no
2153 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002154 */
Ben Blum74a11662011-05-26 16:25:20 -07002155 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002156 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002157 cgroup_task_migrate(cgrp, tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002158 }
2159 /* nothing is sensitive to fork() after this point. */
2160
2161 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002162 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002163 */
2164 for_each_subsys(root, ss) {
2165 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002166 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002167 }
2168
2169 /*
2170 * step 5: success! and cleanup
2171 */
Ben Blum74a11662011-05-26 16:25:20 -07002172 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002173out_put_css_set_refs:
2174 if (retval) {
2175 for (i = 0; i < group_size; i++) {
2176 tc = flex_array_get(group, i);
2177 if (!tc->cg)
2178 break;
2179 put_css_set(tc->cg);
2180 }
Ben Blum74a11662011-05-26 16:25:20 -07002181 }
2182out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002183 if (retval) {
2184 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002185 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002186 break;
Ben Blum74a11662011-05-26 16:25:20 -07002187 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002188 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002189 }
2190 }
Ben Blum74a11662011-05-26 16:25:20 -07002191out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002192 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002193 return retval;
2194}
2195
2196/*
2197 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002198 * function to attach either it or all tasks in its threadgroup. Will lock
2199 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002200 */
2201static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002203 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002204 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002205 int ret;
2206
Ben Blum74a11662011-05-26 16:25:20 -07002207 if (!cgroup_lock_live_group(cgrp))
2208 return -ENODEV;
2209
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210retry_find_task:
2211 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002212 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002213 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002214 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002215 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002216 ret= -ESRCH;
2217 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002218 }
Ben Blum74a11662011-05-26 16:25:20 -07002219 /*
2220 * even if we're attaching all tasks in the thread group, we
2221 * only need to check permissions on one of them.
2222 */
David Howellsc69e8d92008-11-14 10:39:19 +11002223 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002224 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2225 !uid_eq(cred->euid, tcred->uid) &&
2226 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002227 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002228 ret = -EACCES;
2229 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002230 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002231 } else
2232 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002233
2234 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002236
2237 /*
2238 * Workqueue threads may acquire PF_THREAD_BOUND and become
2239 * trapped in a cpuset, or RT worker may be born in a cgroup
2240 * with no rt_runtime allocated. Just say no.
2241 */
2242 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2243 ret = -EINVAL;
2244 rcu_read_unlock();
2245 goto out_unlock_cgroup;
2246 }
2247
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002248 get_task_struct(tsk);
2249 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002250
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002251 threadgroup_lock(tsk);
2252 if (threadgroup) {
2253 if (!thread_group_leader(tsk)) {
2254 /*
2255 * a race with de_thread from another thread's exec()
2256 * may strip us of our leadership, if this happens,
2257 * there is no choice but to throw this task away and
2258 * try again; this is
2259 * "double-double-toil-and-trouble-check locking".
2260 */
2261 threadgroup_unlock(tsk);
2262 put_task_struct(tsk);
2263 goto retry_find_task;
2264 }
2265 ret = cgroup_attach_proc(cgrp, tsk);
2266 } else
2267 ret = cgroup_attach_task(cgrp, tsk);
Tejun Heocd3d0952011-12-12 18:12:21 -08002268 threadgroup_unlock(tsk);
2269
Paul Menagebbcb81d2007-10-18 23:39:32 -07002270 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002271out_unlock_cgroup:
Ben Blum74a11662011-05-26 16:25:20 -07002272 cgroup_unlock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002273 return ret;
2274}
2275
Paul Menageaf351022008-07-25 01:47:01 -07002276static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2277{
Ben Blum74a11662011-05-26 16:25:20 -07002278 return attach_task_by_pid(cgrp, pid, false);
2279}
2280
2281static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2282{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002283 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002284}
2285
Paul Menagee788e062008-07-25 01:46:59 -07002286/**
2287 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
2288 * @cgrp: the cgroup to be checked for liveness
2289 *
Paul Menage84eea842008-07-25 01:47:00 -07002290 * On success, returns true; the lock should be later released with
2291 * cgroup_unlock(). On failure returns false with no lock held.
Paul Menagee788e062008-07-25 01:46:59 -07002292 */
Paul Menage84eea842008-07-25 01:47:00 -07002293bool cgroup_lock_live_group(struct cgroup *cgrp)
Paul Menagee788e062008-07-25 01:46:59 -07002294{
2295 mutex_lock(&cgroup_mutex);
2296 if (cgroup_is_removed(cgrp)) {
2297 mutex_unlock(&cgroup_mutex);
2298 return false;
2299 }
2300 return true;
2301}
Ben Blum67523c42010-03-10 15:22:11 -08002302EXPORT_SYMBOL_GPL(cgroup_lock_live_group);
Paul Menagee788e062008-07-25 01:46:59 -07002303
2304static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2305 const char *buffer)
2306{
2307 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002308 if (strlen(buffer) >= PATH_MAX)
2309 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002310 if (!cgroup_lock_live_group(cgrp))
2311 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002312 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002313 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002314 mutex_unlock(&cgroup_root_mutex);
Paul Menage84eea842008-07-25 01:47:00 -07002315 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002316 return 0;
2317}
2318
2319static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2320 struct seq_file *seq)
2321{
2322 if (!cgroup_lock_live_group(cgrp))
2323 return -ENODEV;
2324 seq_puts(seq, cgrp->root->release_agent_path);
2325 seq_putc(seq, '\n');
Paul Menage84eea842008-07-25 01:47:00 -07002326 cgroup_unlock();
Paul Menagee788e062008-07-25 01:46:59 -07002327 return 0;
2328}
2329
Paul Menage84eea842008-07-25 01:47:00 -07002330/* A buffer size big enough for numbers or short strings */
2331#define CGROUP_LOCAL_BUFFER_SIZE 64
2332
Paul Menagee73d2c62008-04-29 01:00:06 -07002333static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002334 struct file *file,
2335 const char __user *userbuf,
2336 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002337{
Paul Menage84eea842008-07-25 01:47:00 -07002338 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002339 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002340 char *end;
2341
2342 if (!nbytes)
2343 return -EINVAL;
2344 if (nbytes >= sizeof(buffer))
2345 return -E2BIG;
2346 if (copy_from_user(buffer, userbuf, nbytes))
2347 return -EFAULT;
2348
2349 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002350 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002351 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002352 if (*end)
2353 return -EINVAL;
2354 retval = cft->write_u64(cgrp, cft, val);
2355 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002356 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002357 if (*end)
2358 return -EINVAL;
2359 retval = cft->write_s64(cgrp, cft, val);
2360 }
Paul Menage355e0c42007-10-18 23:39:33 -07002361 if (!retval)
2362 retval = nbytes;
2363 return retval;
2364}
2365
Paul Menagedb3b1492008-07-25 01:46:58 -07002366static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2367 struct file *file,
2368 const char __user *userbuf,
2369 size_t nbytes, loff_t *unused_ppos)
2370{
Paul Menage84eea842008-07-25 01:47:00 -07002371 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002372 int retval = 0;
2373 size_t max_bytes = cft->max_write_len;
2374 char *buffer = local_buffer;
2375
2376 if (!max_bytes)
2377 max_bytes = sizeof(local_buffer) - 1;
2378 if (nbytes >= max_bytes)
2379 return -E2BIG;
2380 /* Allocate a dynamic buffer if we need one */
2381 if (nbytes >= sizeof(local_buffer)) {
2382 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2383 if (buffer == NULL)
2384 return -ENOMEM;
2385 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002386 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2387 retval = -EFAULT;
2388 goto out;
2389 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002390
2391 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002392 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002393 if (!retval)
2394 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002395out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002396 if (buffer != local_buffer)
2397 kfree(buffer);
2398 return retval;
2399}
2400
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2402 size_t nbytes, loff_t *ppos)
2403{
2404 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002405 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406
Li Zefan75139b82009-01-07 18:07:33 -08002407 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002408 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002409 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002410 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002411 if (cft->write_u64 || cft->write_s64)
2412 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002413 if (cft->write_string)
2414 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002415 if (cft->trigger) {
2416 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2417 return ret ? ret : nbytes;
2418 }
Paul Menage355e0c42007-10-18 23:39:33 -07002419 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420}
2421
Paul Menagef4c753b2008-04-29 00:59:56 -07002422static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2423 struct file *file,
2424 char __user *buf, size_t nbytes,
2425 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426{
Paul Menage84eea842008-07-25 01:47:00 -07002427 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002428 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2430
2431 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2432}
2433
Paul Menagee73d2c62008-04-29 01:00:06 -07002434static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2435 struct file *file,
2436 char __user *buf, size_t nbytes,
2437 loff_t *ppos)
2438{
Paul Menage84eea842008-07-25 01:47:00 -07002439 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002440 s64 val = cft->read_s64(cgrp, cft);
2441 int len = sprintf(tmp, "%lld\n", (long long) val);
2442
2443 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2444}
2445
Paul Menageddbcc7e2007-10-18 23:39:30 -07002446static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2447 size_t nbytes, loff_t *ppos)
2448{
2449 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002450 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002451
Li Zefan75139b82009-01-07 18:07:33 -08002452 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002453 return -ENODEV;
2454
2455 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002456 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002457 if (cft->read_u64)
2458 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002459 if (cft->read_s64)
2460 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461 return -EINVAL;
2462}
2463
Paul Menage91796562008-04-29 01:00:01 -07002464/*
2465 * seqfile ops/methods for returning structured data. Currently just
2466 * supports string->u64 maps, but can be extended in future.
2467 */
2468
2469struct cgroup_seqfile_state {
2470 struct cftype *cft;
2471 struct cgroup *cgroup;
2472};
2473
2474static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2475{
2476 struct seq_file *sf = cb->state;
2477 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2478}
2479
2480static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2481{
2482 struct cgroup_seqfile_state *state = m->private;
2483 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002484 if (cft->read_map) {
2485 struct cgroup_map_cb cb = {
2486 .fill = cgroup_map_add,
2487 .state = m,
2488 };
2489 return cft->read_map(state->cgroup, cft, &cb);
2490 }
2491 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002492}
2493
Adrian Bunk96930a62008-07-25 19:46:21 -07002494static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002495{
2496 struct seq_file *seq = file->private_data;
2497 kfree(seq->private);
2498 return single_release(inode, file);
2499}
2500
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002501static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002502 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002503 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002504 .llseek = seq_lseek,
2505 .release = cgroup_seqfile_release,
2506};
2507
Paul Menageddbcc7e2007-10-18 23:39:30 -07002508static int cgroup_file_open(struct inode *inode, struct file *file)
2509{
2510 int err;
2511 struct cftype *cft;
2512
2513 err = generic_file_open(inode, file);
2514 if (err)
2515 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002517
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002518 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002519 struct cgroup_seqfile_state *state =
2520 kzalloc(sizeof(*state), GFP_USER);
2521 if (!state)
2522 return -ENOMEM;
2523 state->cft = cft;
2524 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2525 file->f_op = &cgroup_seqfile_operations;
2526 err = single_open(file, cgroup_seqfile_show, state);
2527 if (err < 0)
2528 kfree(state);
2529 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002530 err = cft->open(inode, file);
2531 else
2532 err = 0;
2533
2534 return err;
2535}
2536
2537static int cgroup_file_release(struct inode *inode, struct file *file)
2538{
2539 struct cftype *cft = __d_cft(file->f_dentry);
2540 if (cft->release)
2541 return cft->release(inode, file);
2542 return 0;
2543}
2544
2545/*
2546 * cgroup_rename - Only allow simple rename of directories in place.
2547 */
2548static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2549 struct inode *new_dir, struct dentry *new_dentry)
2550{
Li Zefan65dff752013-03-01 15:01:56 +08002551 int ret;
2552 struct cgroup_name *name, *old_name;
2553 struct cgroup *cgrp;
2554
2555 /*
2556 * It's convinient to use parent dir's i_mutex to protected
2557 * cgrp->name.
2558 */
2559 lockdep_assert_held(&old_dir->i_mutex);
2560
Paul Menageddbcc7e2007-10-18 23:39:30 -07002561 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2562 return -ENOTDIR;
2563 if (new_dentry->d_inode)
2564 return -EEXIST;
2565 if (old_dir != new_dir)
2566 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002567
2568 cgrp = __d_cgrp(old_dentry);
2569
2570 name = cgroup_alloc_name(new_dentry);
2571 if (!name)
2572 return -ENOMEM;
2573
2574 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2575 if (ret) {
2576 kfree(name);
2577 return ret;
2578 }
2579
2580 old_name = cgrp->name;
2581 rcu_assign_pointer(cgrp->name, name);
2582
2583 kfree_rcu(old_name, rcu_head);
2584 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002585}
2586
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002587static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2588{
2589 if (S_ISDIR(dentry->d_inode->i_mode))
2590 return &__d_cgrp(dentry)->xattrs;
2591 else
2592 return &__d_cft(dentry)->xattrs;
2593}
2594
2595static inline int xattr_enabled(struct dentry *dentry)
2596{
2597 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2598 return test_bit(ROOT_XATTR, &root->flags);
2599}
2600
2601static bool is_valid_xattr(const char *name)
2602{
2603 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2604 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2605 return true;
2606 return false;
2607}
2608
2609static int cgroup_setxattr(struct dentry *dentry, const char *name,
2610 const void *val, size_t size, int flags)
2611{
2612 if (!xattr_enabled(dentry))
2613 return -EOPNOTSUPP;
2614 if (!is_valid_xattr(name))
2615 return -EINVAL;
2616 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2617}
2618
2619static int cgroup_removexattr(struct dentry *dentry, const char *name)
2620{
2621 if (!xattr_enabled(dentry))
2622 return -EOPNOTSUPP;
2623 if (!is_valid_xattr(name))
2624 return -EINVAL;
2625 return simple_xattr_remove(__d_xattrs(dentry), name);
2626}
2627
2628static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2629 void *buf, size_t size)
2630{
2631 if (!xattr_enabled(dentry))
2632 return -EOPNOTSUPP;
2633 if (!is_valid_xattr(name))
2634 return -EINVAL;
2635 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2636}
2637
2638static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2639{
2640 if (!xattr_enabled(dentry))
2641 return -EOPNOTSUPP;
2642 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2643}
2644
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002645static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002646 .read = cgroup_file_read,
2647 .write = cgroup_file_write,
2648 .llseek = generic_file_llseek,
2649 .open = cgroup_file_open,
2650 .release = cgroup_file_release,
2651};
2652
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002653static const struct inode_operations cgroup_file_inode_operations = {
2654 .setxattr = cgroup_setxattr,
2655 .getxattr = cgroup_getxattr,
2656 .listxattr = cgroup_listxattr,
2657 .removexattr = cgroup_removexattr,
2658};
2659
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002660static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002661 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002662 .mkdir = cgroup_mkdir,
2663 .rmdir = cgroup_rmdir,
2664 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002665 .setxattr = cgroup_setxattr,
2666 .getxattr = cgroup_getxattr,
2667 .listxattr = cgroup_listxattr,
2668 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669};
2670
Al Viro00cd8dd2012-06-10 17:13:09 -04002671static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002672{
2673 if (dentry->d_name.len > NAME_MAX)
2674 return ERR_PTR(-ENAMETOOLONG);
2675 d_add(dentry, NULL);
2676 return NULL;
2677}
2678
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002679/*
2680 * Check if a file is a control file
2681 */
2682static inline struct cftype *__file_cft(struct file *file)
2683{
Al Viro496ad9a2013-01-23 17:07:38 -05002684 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002685 return ERR_PTR(-EINVAL);
2686 return __d_cft(file->f_dentry);
2687}
2688
Al Viroa5e7ed32011-07-26 01:55:55 -04002689static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002690 struct super_block *sb)
2691{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002692 struct inode *inode;
2693
2694 if (!dentry)
2695 return -ENOENT;
2696 if (dentry->d_inode)
2697 return -EEXIST;
2698
2699 inode = cgroup_new_inode(mode, sb);
2700 if (!inode)
2701 return -ENOMEM;
2702
2703 if (S_ISDIR(mode)) {
2704 inode->i_op = &cgroup_dir_inode_operations;
2705 inode->i_fop = &simple_dir_operations;
2706
2707 /* start off with i_nlink == 2 (for "." entry) */
2708 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002709 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710
Tejun Heob8a2df62012-11-19 08:13:37 -08002711 /*
2712 * Control reaches here with cgroup_mutex held.
2713 * @inode->i_mutex should nest outside cgroup_mutex but we
2714 * want to populate it immediately without releasing
2715 * cgroup_mutex. As @inode isn't visible to anyone else
2716 * yet, trylock will always succeed without affecting
2717 * lockdep checks.
2718 */
2719 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 } else if (S_ISREG(mode)) {
2721 inode->i_size = 0;
2722 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002723 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 d_instantiate(dentry, inode);
2726 dget(dentry); /* Extra count - pin the dentry in core */
2727 return 0;
2728}
2729
Li Zefan099fca32009-04-02 16:57:29 -07002730/**
2731 * cgroup_file_mode - deduce file mode of a control file
2732 * @cft: the control file in question
2733 *
2734 * returns cft->mode if ->mode is not 0
2735 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2736 * returns S_IRUGO if it has only a read handler
2737 * returns S_IWUSR if it has only a write hander
2738 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002739static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002740{
Al Viroa5e7ed32011-07-26 01:55:55 -04002741 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002742
2743 if (cft->mode)
2744 return cft->mode;
2745
2746 if (cft->read || cft->read_u64 || cft->read_s64 ||
2747 cft->read_map || cft->read_seq_string)
2748 mode |= S_IRUGO;
2749
2750 if (cft->write || cft->write_u64 || cft->write_s64 ||
2751 cft->write_string || cft->trigger)
2752 mode |= S_IWUSR;
2753
2754 return mode;
2755}
2756
Tejun Heodb0416b2012-04-01 12:09:55 -07002757static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759{
Paul Menagebd89aab2007-10-18 23:40:44 -07002760 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002761 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002763 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002765 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002767
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002768 simple_xattrs_init(&cft->xattrs);
2769
Paul Menagebd89aab2007-10-18 23:40:44 -07002770 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002771 strcpy(name, subsys->name);
2772 strcat(name, ".");
2773 }
2774 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002777
2778 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2779 if (!cfe)
2780 return -ENOMEM;
2781
Paul Menageddbcc7e2007-10-18 23:39:30 -07002782 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002783 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002785 goto out;
2786 }
2787
2788 mode = cgroup_file_mode(cft);
2789 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2790 if (!error) {
2791 cfe->type = (void *)cft;
2792 cfe->dentry = dentry;
2793 dentry->d_fsdata = cfe;
2794 list_add_tail(&cfe->node, &parent->files);
2795 cfe = NULL;
2796 }
2797 dput(dentry);
2798out:
2799 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002800 return error;
2801}
2802
Tejun Heo79578622012-04-01 12:09:56 -07002803static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002804 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002805{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002806 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002807 int err, ret = 0;
2808
2809 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002810 /* does cft->flags tell us to skip this file on @cgrp? */
2811 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2812 continue;
2813 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2814 continue;
2815
Li Zefan2739d3c2013-01-21 18:18:33 +08002816 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002817 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002818 if (err)
2819 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2820 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002821 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002822 } else {
2823 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002824 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002825 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002826 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002827}
2828
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829static DEFINE_MUTEX(cgroup_cft_mutex);
2830
2831static void cgroup_cfts_prepare(void)
2832 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2833{
2834 /*
2835 * Thanks to the entanglement with vfs inode locking, we can't walk
2836 * the existing cgroups under cgroup_mutex and create files.
2837 * Instead, we increment reference on all cgroups and build list of
2838 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2839 * exclusive access to the field.
2840 */
2841 mutex_lock(&cgroup_cft_mutex);
2842 mutex_lock(&cgroup_mutex);
2843}
2844
2845static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002846 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002847 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2848{
2849 LIST_HEAD(pending);
2850 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002851
2852 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2853 if (cfts && ss->root != &rootnode) {
2854 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2855 dget(cgrp->dentry);
2856 list_add_tail(&cgrp->cft_q_node, &pending);
2857 }
2858 }
2859
2860 mutex_unlock(&cgroup_mutex);
2861
2862 /*
2863 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2864 * files for all cgroups which were created before.
2865 */
2866 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2867 struct inode *inode = cgrp->dentry->d_inode;
2868
2869 mutex_lock(&inode->i_mutex);
2870 mutex_lock(&cgroup_mutex);
2871 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002872 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002873 mutex_unlock(&cgroup_mutex);
2874 mutex_unlock(&inode->i_mutex);
2875
2876 list_del_init(&cgrp->cft_q_node);
2877 dput(cgrp->dentry);
2878 }
2879
2880 mutex_unlock(&cgroup_cft_mutex);
2881}
2882
2883/**
2884 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2885 * @ss: target cgroup subsystem
2886 * @cfts: zero-length name terminated array of cftypes
2887 *
2888 * Register @cfts to @ss. Files described by @cfts are created for all
2889 * existing cgroups to which @ss is attached and all future cgroups will
2890 * have them too. This function can be called anytime whether @ss is
2891 * attached or not.
2892 *
2893 * Returns 0 on successful registration, -errno on failure. Note that this
2894 * function currently returns 0 as long as @cfts registration is successful
2895 * even if some file creation attempts on existing cgroups fail.
2896 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002897int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002898{
2899 struct cftype_set *set;
2900
2901 set = kzalloc(sizeof(*set), GFP_KERNEL);
2902 if (!set)
2903 return -ENOMEM;
2904
2905 cgroup_cfts_prepare();
2906 set->cfts = cfts;
2907 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002908 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002909
2910 return 0;
2911}
2912EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2913
Li Zefana043e3b2008-02-23 15:24:09 -08002914/**
Tejun Heo79578622012-04-01 12:09:56 -07002915 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2916 * @ss: target cgroup subsystem
2917 * @cfts: zero-length name terminated array of cftypes
2918 *
2919 * Unregister @cfts from @ss. Files described by @cfts are removed from
2920 * all existing cgroups to which @ss is attached and all future cgroups
2921 * won't have them either. This function can be called anytime whether @ss
2922 * is attached or not.
2923 *
2924 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2925 * registered with @ss.
2926 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002927int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002928{
2929 struct cftype_set *set;
2930
2931 cgroup_cfts_prepare();
2932
2933 list_for_each_entry(set, &ss->cftsets, node) {
2934 if (set->cfts == cfts) {
2935 list_del_init(&set->node);
2936 cgroup_cfts_commit(ss, cfts, false);
2937 return 0;
2938 }
2939 }
2940
2941 cgroup_cfts_commit(ss, NULL, false);
2942 return -ENOENT;
2943}
2944
2945/**
Li Zefana043e3b2008-02-23 15:24:09 -08002946 * cgroup_task_count - count the number of tasks in a cgroup.
2947 * @cgrp: the cgroup in question
2948 *
2949 * Return the number of tasks in the cgroup.
2950 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002951int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002952{
2953 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002954 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002955
Paul Menage817929e2007-10-18 23:39:36 -07002956 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002957 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002958 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002959 }
2960 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002961 return count;
2962}
2963
2964/*
Paul Menage817929e2007-10-18 23:39:36 -07002965 * Advance a list_head iterator. The iterator should be positioned at
2966 * the start of a css_set
2967 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002968static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002969 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002970{
2971 struct list_head *l = it->cg_link;
2972 struct cg_cgroup_link *link;
2973 struct css_set *cg;
2974
2975 /* Advance to the next non-empty css_set */
2976 do {
2977 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002978 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002979 it->cg_link = NULL;
2980 return;
2981 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002982 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002983 cg = link->cg;
2984 } while (list_empty(&cg->tasks));
2985 it->cg_link = l;
2986 it->task = cg->tasks.next;
2987}
2988
Cliff Wickman31a7df02008-02-07 00:14:42 -08002989/*
2990 * To reduce the fork() overhead for systems that are not actually
2991 * using their cgroups capability, we don't maintain the lists running
2992 * through each css_set to its tasks until we see the list actually
2993 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002994 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002995static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002996{
2997 struct task_struct *p, *g;
2998 write_lock(&css_set_lock);
2999 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003000 /*
3001 * We need tasklist_lock because RCU is not safe against
3002 * while_each_thread(). Besides, a forking task that has passed
3003 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3004 * is not guaranteed to have its child immediately visible in the
3005 * tasklist if we walk through it with RCU.
3006 */
3007 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003008 do_each_thread(g, p) {
3009 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003010 /*
3011 * We should check if the process is exiting, otherwise
3012 * it will race with cgroup_exit() in that the list
3013 * entry won't be deleted though the process has exited.
3014 */
3015 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003016 list_add(&p->cg_list, &p->cgroups->tasks);
3017 task_unlock(p);
3018 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003019 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003020 write_unlock(&css_set_lock);
3021}
3022
Tejun Heo574bd9f2012-11-09 09:12:29 -08003023/**
3024 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3025 * @pos: the current position (%NULL to initiate traversal)
3026 * @cgroup: cgroup whose descendants to walk
3027 *
3028 * To be used by cgroup_for_each_descendant_pre(). Find the next
3029 * descendant to visit for pre-order traversal of @cgroup's descendants.
3030 */
3031struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3032 struct cgroup *cgroup)
3033{
3034 struct cgroup *next;
3035
3036 WARN_ON_ONCE(!rcu_read_lock_held());
3037
3038 /* if first iteration, pretend we just visited @cgroup */
3039 if (!pos) {
3040 if (list_empty(&cgroup->children))
3041 return NULL;
3042 pos = cgroup;
3043 }
3044
3045 /* visit the first child if exists */
3046 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3047 if (next)
3048 return next;
3049
3050 /* no child, visit my or the closest ancestor's next sibling */
3051 do {
3052 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3053 sibling);
3054 if (&next->sibling != &pos->parent->children)
3055 return next;
3056
3057 pos = pos->parent;
3058 } while (pos != cgroup);
3059
3060 return NULL;
3061}
3062EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3063
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003064/**
3065 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3066 * @pos: cgroup of interest
3067 *
3068 * Return the rightmost descendant of @pos. If there's no descendant,
3069 * @pos is returned. This can be used during pre-order traversal to skip
3070 * subtree of @pos.
3071 */
3072struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3073{
3074 struct cgroup *last, *tmp;
3075
3076 WARN_ON_ONCE(!rcu_read_lock_held());
3077
3078 do {
3079 last = pos;
3080 /* ->prev isn't RCU safe, walk ->next till the end */
3081 pos = NULL;
3082 list_for_each_entry_rcu(tmp, &last->children, sibling)
3083 pos = tmp;
3084 } while (pos);
3085
3086 return last;
3087}
3088EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3089
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3091{
3092 struct cgroup *last;
3093
3094 do {
3095 last = pos;
3096 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3097 sibling);
3098 } while (pos);
3099
3100 return last;
3101}
3102
3103/**
3104 * cgroup_next_descendant_post - find the next descendant for post-order walk
3105 * @pos: the current position (%NULL to initiate traversal)
3106 * @cgroup: cgroup whose descendants to walk
3107 *
3108 * To be used by cgroup_for_each_descendant_post(). Find the next
3109 * descendant to visit for post-order traversal of @cgroup's descendants.
3110 */
3111struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3112 struct cgroup *cgroup)
3113{
3114 struct cgroup *next;
3115
3116 WARN_ON_ONCE(!rcu_read_lock_held());
3117
3118 /* if first iteration, visit the leftmost descendant */
3119 if (!pos) {
3120 next = cgroup_leftmost_descendant(cgroup);
3121 return next != cgroup ? next : NULL;
3122 }
3123
3124 /* if there's an unvisited sibling, visit its leftmost descendant */
3125 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3126 if (&next->sibling != &pos->parent->children)
3127 return cgroup_leftmost_descendant(next);
3128
3129 /* no sibling left, visit parent */
3130 next = pos->parent;
3131 return next != cgroup ? next : NULL;
3132}
3133EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3134
Paul Menagebd89aab2007-10-18 23:40:44 -07003135void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003136 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003137{
3138 /*
3139 * The first time anyone tries to iterate across a cgroup,
3140 * we need to enable the list linking each css_set to its
3141 * tasks, and fix up all existing tasks.
3142 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003143 if (!use_task_css_set_links)
3144 cgroup_enable_task_cg_lists();
3145
Paul Menage817929e2007-10-18 23:39:36 -07003146 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003147 it->cg_link = &cgrp->css_sets;
3148 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003149}
3150
Paul Menagebd89aab2007-10-18 23:40:44 -07003151struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003152 struct cgroup_iter *it)
3153{
3154 struct task_struct *res;
3155 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003156 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003157
3158 /* If the iterator cg is NULL, we have no tasks */
3159 if (!it->cg_link)
3160 return NULL;
3161 res = list_entry(l, struct task_struct, cg_list);
3162 /* Advance iterator to find next entry */
3163 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003164 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3165 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003166 /* We reached the end of this task list - move on to
3167 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003168 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003169 } else {
3170 it->task = l;
3171 }
3172 return res;
3173}
3174
Paul Menagebd89aab2007-10-18 23:40:44 -07003175void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003176 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003177{
3178 read_unlock(&css_set_lock);
3179}
3180
Cliff Wickman31a7df02008-02-07 00:14:42 -08003181static inline int started_after_time(struct task_struct *t1,
3182 struct timespec *time,
3183 struct task_struct *t2)
3184{
3185 int start_diff = timespec_compare(&t1->start_time, time);
3186 if (start_diff > 0) {
3187 return 1;
3188 } else if (start_diff < 0) {
3189 return 0;
3190 } else {
3191 /*
3192 * Arbitrarily, if two processes started at the same
3193 * time, we'll say that the lower pointer value
3194 * started first. Note that t2 may have exited by now
3195 * so this may not be a valid pointer any longer, but
3196 * that's fine - it still serves to distinguish
3197 * between two tasks started (effectively) simultaneously.
3198 */
3199 return t1 > t2;
3200 }
3201}
3202
3203/*
3204 * This function is a callback from heap_insert() and is used to order
3205 * the heap.
3206 * In this case we order the heap in descending task start time.
3207 */
3208static inline int started_after(void *p1, void *p2)
3209{
3210 struct task_struct *t1 = p1;
3211 struct task_struct *t2 = p2;
3212 return started_after_time(t1, &t2->start_time, t2);
3213}
3214
3215/**
3216 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3217 * @scan: struct cgroup_scanner containing arguments for the scan
3218 *
3219 * Arguments include pointers to callback functions test_task() and
3220 * process_task().
3221 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3222 * and if it returns true, call process_task() for it also.
3223 * The test_task pointer may be NULL, meaning always true (select all tasks).
3224 * Effectively duplicates cgroup_iter_{start,next,end}()
3225 * but does not lock css_set_lock for the call to process_task().
3226 * The struct cgroup_scanner may be embedded in any structure of the caller's
3227 * creation.
3228 * It is guaranteed that process_task() will act on every task that
3229 * is a member of the cgroup for the duration of this call. This
3230 * function may or may not call process_task() for tasks that exit
3231 * or move to a different cgroup during the call, or are forked or
3232 * move into the cgroup during the call.
3233 *
3234 * Note that test_task() may be called with locks held, and may in some
3235 * situations be called multiple times for the same task, so it should
3236 * be cheap.
3237 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3238 * pre-allocated and will be used for heap operations (and its "gt" member will
3239 * be overwritten), else a temporary heap will be used (allocation of which
3240 * may cause this function to fail).
3241 */
3242int cgroup_scan_tasks(struct cgroup_scanner *scan)
3243{
3244 int retval, i;
3245 struct cgroup_iter it;
3246 struct task_struct *p, *dropped;
3247 /* Never dereference latest_task, since it's not refcounted */
3248 struct task_struct *latest_task = NULL;
3249 struct ptr_heap tmp_heap;
3250 struct ptr_heap *heap;
3251 struct timespec latest_time = { 0, 0 };
3252
3253 if (scan->heap) {
3254 /* The caller supplied our heap and pre-allocated its memory */
3255 heap = scan->heap;
3256 heap->gt = &started_after;
3257 } else {
3258 /* We need to allocate our own heap memory */
3259 heap = &tmp_heap;
3260 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3261 if (retval)
3262 /* cannot allocate the heap */
3263 return retval;
3264 }
3265
3266 again:
3267 /*
3268 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3269 * to determine which are of interest, and using the scanner's
3270 * "process_task" callback to process any of them that need an update.
3271 * Since we don't want to hold any locks during the task updates,
3272 * gather tasks to be processed in a heap structure.
3273 * The heap is sorted by descending task start time.
3274 * If the statically-sized heap fills up, we overflow tasks that
3275 * started later, and in future iterations only consider tasks that
3276 * started after the latest task in the previous pass. This
3277 * guarantees forward progress and that we don't miss any tasks.
3278 */
3279 heap->size = 0;
3280 cgroup_iter_start(scan->cg, &it);
3281 while ((p = cgroup_iter_next(scan->cg, &it))) {
3282 /*
3283 * Only affect tasks that qualify per the caller's callback,
3284 * if he provided one
3285 */
3286 if (scan->test_task && !scan->test_task(p, scan))
3287 continue;
3288 /*
3289 * Only process tasks that started after the last task
3290 * we processed
3291 */
3292 if (!started_after_time(p, &latest_time, latest_task))
3293 continue;
3294 dropped = heap_insert(heap, p);
3295 if (dropped == NULL) {
3296 /*
3297 * The new task was inserted; the heap wasn't
3298 * previously full
3299 */
3300 get_task_struct(p);
3301 } else if (dropped != p) {
3302 /*
3303 * The new task was inserted, and pushed out a
3304 * different task
3305 */
3306 get_task_struct(p);
3307 put_task_struct(dropped);
3308 }
3309 /*
3310 * Else the new task was newer than anything already in
3311 * the heap and wasn't inserted
3312 */
3313 }
3314 cgroup_iter_end(scan->cg, &it);
3315
3316 if (heap->size) {
3317 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003318 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003319 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003320 latest_time = q->start_time;
3321 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003322 }
3323 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003324 scan->process_task(q, scan);
3325 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003326 }
3327 /*
3328 * If we had to process any tasks at all, scan again
3329 * in case some of them were in the middle of forking
3330 * children that didn't get processed.
3331 * Not the most efficient way to do it, but it avoids
3332 * having to take callback_mutex in the fork path
3333 */
3334 goto again;
3335 }
3336 if (heap == &tmp_heap)
3337 heap_free(&tmp_heap);
3338 return 0;
3339}
3340
Paul Menage817929e2007-10-18 23:39:36 -07003341/*
Ben Blum102a7752009-09-23 15:56:26 -07003342 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003343 *
3344 * Reading this file can return large amounts of data if a cgroup has
3345 * *lots* of attached tasks. So it may need several calls to read(),
3346 * but we cannot guarantee that the information we produce is correct
3347 * unless we produce it entirely atomically.
3348 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003349 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003350
Li Zefan24528252012-01-20 11:58:43 +08003351/* which pidlist file are we talking about? */
3352enum cgroup_filetype {
3353 CGROUP_FILE_PROCS,
3354 CGROUP_FILE_TASKS,
3355};
3356
3357/*
3358 * A pidlist is a list of pids that virtually represents the contents of one
3359 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3360 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3361 * to the cgroup.
3362 */
3363struct cgroup_pidlist {
3364 /*
3365 * used to find which pidlist is wanted. doesn't change as long as
3366 * this particular list stays in the list.
3367 */
3368 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3369 /* array of xids */
3370 pid_t *list;
3371 /* how many elements the above list has */
3372 int length;
3373 /* how many files are using the current array */
3374 int use_count;
3375 /* each of these stored in a list by its cgroup */
3376 struct list_head links;
3377 /* pointer to the cgroup we belong to, for list removal purposes */
3378 struct cgroup *owner;
3379 /* protects the other fields */
3380 struct rw_semaphore mutex;
3381};
3382
Paul Menagebbcb81d2007-10-18 23:39:32 -07003383/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003384 * The following two functions "fix" the issue where there are more pids
3385 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3386 * TODO: replace with a kernel-wide solution to this problem
3387 */
3388#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3389static void *pidlist_allocate(int count)
3390{
3391 if (PIDLIST_TOO_LARGE(count))
3392 return vmalloc(count * sizeof(pid_t));
3393 else
3394 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3395}
3396static void pidlist_free(void *p)
3397{
3398 if (is_vmalloc_addr(p))
3399 vfree(p);
3400 else
3401 kfree(p);
3402}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003403
3404/*
Ben Blum102a7752009-09-23 15:56:26 -07003405 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003406 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003407 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003408static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003409{
Ben Blum102a7752009-09-23 15:56:26 -07003410 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003411
3412 /*
3413 * we presume the 0th element is unique, so i starts at 1. trivial
3414 * edge cases first; no work needs to be done for either
3415 */
3416 if (length == 0 || length == 1)
3417 return length;
3418 /* src and dest walk down the list; dest counts unique elements */
3419 for (src = 1; src < length; src++) {
3420 /* find next unique element */
3421 while (list[src] == list[src-1]) {
3422 src++;
3423 if (src == length)
3424 goto after;
3425 }
3426 /* dest always points to where the next unique element goes */
3427 list[dest] = list[src];
3428 dest++;
3429 }
3430after:
Ben Blum102a7752009-09-23 15:56:26 -07003431 return dest;
3432}
3433
3434static int cmppid(const void *a, const void *b)
3435{
3436 return *(pid_t *)a - *(pid_t *)b;
3437}
3438
3439/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003440 * find the appropriate pidlist for our purpose (given procs vs tasks)
3441 * returns with the lock on that pidlist already held, and takes care
3442 * of the use count, or returns NULL with no locks held if we're out of
3443 * memory.
3444 */
3445static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3446 enum cgroup_filetype type)
3447{
3448 struct cgroup_pidlist *l;
3449 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003450 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003451
Ben Blum72a8cb32009-09-23 15:56:27 -07003452 /*
3453 * We can't drop the pidlist_mutex before taking the l->mutex in case
3454 * the last ref-holder is trying to remove l from the list at the same
3455 * time. Holding the pidlist_mutex precludes somebody taking whichever
3456 * list we find out from under us - compare release_pid_array().
3457 */
3458 mutex_lock(&cgrp->pidlist_mutex);
3459 list_for_each_entry(l, &cgrp->pidlists, links) {
3460 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003461 /* make sure l doesn't vanish out from under us */
3462 down_write(&l->mutex);
3463 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003464 return l;
3465 }
3466 }
3467 /* entry not found; create a new one */
3468 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3469 if (!l) {
3470 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003471 return l;
3472 }
3473 init_rwsem(&l->mutex);
3474 down_write(&l->mutex);
3475 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003476 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003477 l->use_count = 0; /* don't increment here */
3478 l->list = NULL;
3479 l->owner = cgrp;
3480 list_add(&l->links, &cgrp->pidlists);
3481 mutex_unlock(&cgrp->pidlist_mutex);
3482 return l;
3483}
3484
3485/*
Ben Blum102a7752009-09-23 15:56:26 -07003486 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3487 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003488static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3489 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003490{
3491 pid_t *array;
3492 int length;
3493 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003494 struct cgroup_iter it;
3495 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003496 struct cgroup_pidlist *l;
3497
3498 /*
3499 * If cgroup gets more users after we read count, we won't have
3500 * enough space - tough. This race is indistinguishable to the
3501 * caller from the case that the additional cgroup users didn't
3502 * show up until sometime later on.
3503 */
3504 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003505 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003506 if (!array)
3507 return -ENOMEM;
3508 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003509 cgroup_iter_start(cgrp, &it);
3510 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003511 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003512 break;
Ben Blum102a7752009-09-23 15:56:26 -07003513 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003514 if (type == CGROUP_FILE_PROCS)
3515 pid = task_tgid_vnr(tsk);
3516 else
3517 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003518 if (pid > 0) /* make sure to only use valid results */
3519 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003520 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003521 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003522 length = n;
3523 /* now sort & (if procs) strip out duplicates */
3524 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003526 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 l = cgroup_pidlist_find(cgrp, type);
3528 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003529 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003531 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003532 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003533 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003534 l->list = array;
3535 l->length = length;
3536 l->use_count++;
3537 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003538 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003539 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003540}
3541
Balbir Singh846c7bb2007-10-18 23:39:44 -07003542/**
Li Zefana043e3b2008-02-23 15:24:09 -08003543 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003544 * @stats: cgroupstats to fill information into
3545 * @dentry: A dentry entry belonging to the cgroup for which stats have
3546 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003547 *
3548 * Build and fill cgroupstats so that taskstats can export it to user
3549 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003550 */
3551int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3552{
3553 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003554 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003555 struct cgroup_iter it;
3556 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003557
Balbir Singh846c7bb2007-10-18 23:39:44 -07003558 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003559 * Validate dentry by checking the superblock operations,
3560 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003561 */
Li Zefan33d283b2008-11-19 15:36:48 -08003562 if (dentry->d_sb->s_op != &cgroup_ops ||
3563 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003564 goto err;
3565
3566 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003567 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003568
Paul Menagebd89aab2007-10-18 23:40:44 -07003569 cgroup_iter_start(cgrp, &it);
3570 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003571 switch (tsk->state) {
3572 case TASK_RUNNING:
3573 stats->nr_running++;
3574 break;
3575 case TASK_INTERRUPTIBLE:
3576 stats->nr_sleeping++;
3577 break;
3578 case TASK_UNINTERRUPTIBLE:
3579 stats->nr_uninterruptible++;
3580 break;
3581 case TASK_STOPPED:
3582 stats->nr_stopped++;
3583 break;
3584 default:
3585 if (delayacct_is_task_waiting_on_io(tsk))
3586 stats->nr_io_wait++;
3587 break;
3588 }
3589 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003590 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003591
Balbir Singh846c7bb2007-10-18 23:39:44 -07003592err:
3593 return ret;
3594}
3595
Paul Menage8f3ff202009-09-23 15:56:25 -07003596
Paul Menagecc31edc2008-10-18 20:28:04 -07003597/*
Ben Blum102a7752009-09-23 15:56:26 -07003598 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003599 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003600 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003601 */
3602
Ben Blum102a7752009-09-23 15:56:26 -07003603static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003604{
3605 /*
3606 * Initially we receive a position value that corresponds to
3607 * one more than the last pid shown (or 0 on the first call or
3608 * after a seek to the start). Use a binary-search to find the
3609 * next pid to display, if any
3610 */
Ben Blum102a7752009-09-23 15:56:26 -07003611 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003612 int index = 0, pid = *pos;
3613 int *iter;
3614
Ben Blum102a7752009-09-23 15:56:26 -07003615 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003616 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003617 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003618
Paul Menagecc31edc2008-10-18 20:28:04 -07003619 while (index < end) {
3620 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003621 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003622 index = mid;
3623 break;
Ben Blum102a7752009-09-23 15:56:26 -07003624 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003625 index = mid + 1;
3626 else
3627 end = mid;
3628 }
3629 }
3630 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003631 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003632 return NULL;
3633 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003634 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003635 *pos = *iter;
3636 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003637}
3638
Ben Blum102a7752009-09-23 15:56:26 -07003639static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003640{
Ben Blum102a7752009-09-23 15:56:26 -07003641 struct cgroup_pidlist *l = s->private;
3642 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003643}
3644
Ben Blum102a7752009-09-23 15:56:26 -07003645static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003646{
Ben Blum102a7752009-09-23 15:56:26 -07003647 struct cgroup_pidlist *l = s->private;
3648 pid_t *p = v;
3649 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003650 /*
3651 * Advance to the next pid in the array. If this goes off the
3652 * end, we're done
3653 */
3654 p++;
3655 if (p >= end) {
3656 return NULL;
3657 } else {
3658 *pos = *p;
3659 return p;
3660 }
3661}
3662
Ben Blum102a7752009-09-23 15:56:26 -07003663static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003664{
3665 return seq_printf(s, "%d\n", *(int *)v);
3666}
3667
Ben Blum102a7752009-09-23 15:56:26 -07003668/*
3669 * seq_operations functions for iterating on pidlists through seq_file -
3670 * independent of whether it's tasks or procs
3671 */
3672static const struct seq_operations cgroup_pidlist_seq_operations = {
3673 .start = cgroup_pidlist_start,
3674 .stop = cgroup_pidlist_stop,
3675 .next = cgroup_pidlist_next,
3676 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003677};
3678
Ben Blum102a7752009-09-23 15:56:26 -07003679static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003680{
Ben Blum72a8cb32009-09-23 15:56:27 -07003681 /*
3682 * the case where we're the last user of this particular pidlist will
3683 * have us remove it from the cgroup's list, which entails taking the
3684 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3685 * pidlist_mutex, we have to take pidlist_mutex first.
3686 */
3687 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003688 down_write(&l->mutex);
3689 BUG_ON(!l->use_count);
3690 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003691 /* we're the last user if refcount is 0; remove and free */
3692 list_del(&l->links);
3693 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003694 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 put_pid_ns(l->key.ns);
3696 up_write(&l->mutex);
3697 kfree(l);
3698 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003699 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003700 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003701 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003702}
3703
Ben Blum102a7752009-09-23 15:56:26 -07003704static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003705{
Ben Blum102a7752009-09-23 15:56:26 -07003706 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003707 if (!(file->f_mode & FMODE_READ))
3708 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003709 /*
3710 * the seq_file will only be initialized if the file was opened for
3711 * reading; hence we check if it's not null only in that case.
3712 */
3713 l = ((struct seq_file *)file->private_data)->private;
3714 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003715 return seq_release(inode, file);
3716}
3717
Ben Blum102a7752009-09-23 15:56:26 -07003718static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003719 .read = seq_read,
3720 .llseek = seq_lseek,
3721 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003722 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003723};
3724
3725/*
Ben Blum102a7752009-09-23 15:56:26 -07003726 * The following functions handle opens on a file that displays a pidlist
3727 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3728 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003729 */
Ben Blum102a7752009-09-23 15:56:26 -07003730/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003731static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003732{
3733 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003734 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 int retval;
3736
3737 /* Nothing to do for write-only files */
3738 if (!(file->f_mode & FMODE_READ))
3739 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003740
Ben Blum102a7752009-09-23 15:56:26 -07003741 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003742 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003743 if (retval)
3744 return retval;
3745 /* configure file information */
3746 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003747
Ben Blum102a7752009-09-23 15:56:26 -07003748 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003749 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003750 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003751 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003752 }
Ben Blum102a7752009-09-23 15:56:26 -07003753 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003754 return 0;
3755}
Ben Blum102a7752009-09-23 15:56:26 -07003756static int cgroup_tasks_open(struct inode *unused, struct file *file)
3757{
Ben Blum72a8cb32009-09-23 15:56:27 -07003758 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003759}
3760static int cgroup_procs_open(struct inode *unused, struct file *file)
3761{
Ben Blum72a8cb32009-09-23 15:56:27 -07003762 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003763}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003764
Paul Menagebd89aab2007-10-18 23:40:44 -07003765static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003766 struct cftype *cft)
3767{
Paul Menagebd89aab2007-10-18 23:40:44 -07003768 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003769}
3770
Paul Menage6379c102008-07-25 01:47:01 -07003771static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3772 struct cftype *cft,
3773 u64 val)
3774{
3775 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3776 if (val)
3777 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3778 else
3779 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3780 return 0;
3781}
3782
Paul Menagebbcb81d2007-10-18 23:39:32 -07003783/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003784 * Unregister event and free resources.
3785 *
3786 * Gets called from workqueue.
3787 */
3788static void cgroup_event_remove(struct work_struct *work)
3789{
3790 struct cgroup_event *event = container_of(work, struct cgroup_event,
3791 remove);
3792 struct cgroup *cgrp = event->cgrp;
3793
Li Zefan810cbee2013-02-18 18:56:14 +08003794 remove_wait_queue(event->wqh, &event->wait);
3795
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003796 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3797
Li Zefan810cbee2013-02-18 18:56:14 +08003798 /* Notify userspace the event is going away. */
3799 eventfd_signal(event->eventfd, 1);
3800
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003801 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003802 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003803 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003804}
3805
3806/*
3807 * Gets called on POLLHUP on eventfd when user closes it.
3808 *
3809 * Called with wqh->lock held and interrupts disabled.
3810 */
3811static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3812 int sync, void *key)
3813{
3814 struct cgroup_event *event = container_of(wait,
3815 struct cgroup_event, wait);
3816 struct cgroup *cgrp = event->cgrp;
3817 unsigned long flags = (unsigned long)key;
3818
3819 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003820 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003821 * If the event has been detached at cgroup removal, we
3822 * can simply return knowing the other side will cleanup
3823 * for us.
3824 *
3825 * We can't race against event freeing since the other
3826 * side will require wqh->lock via remove_wait_queue(),
3827 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003828 */
Li Zefan810cbee2013-02-18 18:56:14 +08003829 spin_lock(&cgrp->event_list_lock);
3830 if (!list_empty(&event->list)) {
3831 list_del_init(&event->list);
3832 /*
3833 * We are in atomic context, but cgroup_event_remove()
3834 * may sleep, so we have to call it in workqueue.
3835 */
3836 schedule_work(&event->remove);
3837 }
3838 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003839 }
3840
3841 return 0;
3842}
3843
3844static void cgroup_event_ptable_queue_proc(struct file *file,
3845 wait_queue_head_t *wqh, poll_table *pt)
3846{
3847 struct cgroup_event *event = container_of(pt,
3848 struct cgroup_event, pt);
3849
3850 event->wqh = wqh;
3851 add_wait_queue(wqh, &event->wait);
3852}
3853
3854/*
3855 * Parse input and register new cgroup event handler.
3856 *
3857 * Input must be in format '<event_fd> <control_fd> <args>'.
3858 * Interpretation of args is defined by control file implementation.
3859 */
3860static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3861 const char *buffer)
3862{
3863 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003864 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003865 unsigned int efd, cfd;
3866 struct file *efile = NULL;
3867 struct file *cfile = NULL;
3868 char *endp;
3869 int ret;
3870
3871 efd = simple_strtoul(buffer, &endp, 10);
3872 if (*endp != ' ')
3873 return -EINVAL;
3874 buffer = endp + 1;
3875
3876 cfd = simple_strtoul(buffer, &endp, 10);
3877 if ((*endp != ' ') && (*endp != '\0'))
3878 return -EINVAL;
3879 buffer = endp + 1;
3880
3881 event = kzalloc(sizeof(*event), GFP_KERNEL);
3882 if (!event)
3883 return -ENOMEM;
3884 event->cgrp = cgrp;
3885 INIT_LIST_HEAD(&event->list);
3886 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3887 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3888 INIT_WORK(&event->remove, cgroup_event_remove);
3889
3890 efile = eventfd_fget(efd);
3891 if (IS_ERR(efile)) {
3892 ret = PTR_ERR(efile);
3893 goto fail;
3894 }
3895
3896 event->eventfd = eventfd_ctx_fileget(efile);
3897 if (IS_ERR(event->eventfd)) {
3898 ret = PTR_ERR(event->eventfd);
3899 goto fail;
3900 }
3901
3902 cfile = fget(cfd);
3903 if (!cfile) {
3904 ret = -EBADF;
3905 goto fail;
3906 }
3907
3908 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003909 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003910 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911 if (ret < 0)
3912 goto fail;
3913
3914 event->cft = __file_cft(cfile);
3915 if (IS_ERR(event->cft)) {
3916 ret = PTR_ERR(event->cft);
3917 goto fail;
3918 }
3919
Li Zefanf1690072013-02-18 14:13:35 +08003920 /*
3921 * The file to be monitored must be in the same cgroup as
3922 * cgroup.event_control is.
3923 */
3924 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3925 if (cgrp_cfile != cgrp) {
3926 ret = -EINVAL;
3927 goto fail;
3928 }
3929
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003930 if (!event->cft->register_event || !event->cft->unregister_event) {
3931 ret = -EINVAL;
3932 goto fail;
3933 }
3934
3935 ret = event->cft->register_event(cgrp, event->cft,
3936 event->eventfd, buffer);
3937 if (ret)
3938 goto fail;
3939
3940 if (efile->f_op->poll(efile, &event->pt) & POLLHUP) {
3941 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3942 ret = 0;
3943 goto fail;
3944 }
3945
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003946 /*
3947 * Events should be removed after rmdir of cgroup directory, but before
3948 * destroying subsystem state objects. Let's take reference to cgroup
3949 * directory dentry to do that.
3950 */
3951 dget(cgrp->dentry);
3952
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003953 spin_lock(&cgrp->event_list_lock);
3954 list_add(&event->list, &cgrp->event_list);
3955 spin_unlock(&cgrp->event_list_lock);
3956
3957 fput(cfile);
3958 fput(efile);
3959
3960 return 0;
3961
3962fail:
3963 if (cfile)
3964 fput(cfile);
3965
3966 if (event && event->eventfd && !IS_ERR(event->eventfd))
3967 eventfd_ctx_put(event->eventfd);
3968
3969 if (!IS_ERR_OR_NULL(efile))
3970 fput(efile);
3971
3972 kfree(event);
3973
3974 return ret;
3975}
3976
Daniel Lezcano97978e62010-10-27 15:33:35 -07003977static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3978 struct cftype *cft)
3979{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003980 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003981}
3982
3983static int cgroup_clone_children_write(struct cgroup *cgrp,
3984 struct cftype *cft,
3985 u64 val)
3986{
3987 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003988 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003989 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003990 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003991 return 0;
3992}
3993
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003994/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003995 * for the common functions, 'private' gives the type of file
3996 */
Ben Blum102a7752009-09-23 15:56:26 -07003997/* for hysterical raisins, we can't put this on the older files */
3998#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003999static struct cftype files[] = {
4000 {
4001 .name = "tasks",
4002 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07004003 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07004004 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07004005 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004006 },
Ben Blum102a7752009-09-23 15:56:26 -07004007 {
4008 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
4009 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004010 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004011 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004012 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004013 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004014 {
4015 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07004016 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07004017 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07004018 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004019 {
4020 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
4021 .write_string = cgroup_write_event_control,
4022 .mode = S_IWUGO,
4023 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004024 {
4025 .name = "cgroup.clone_children",
4026 .read_u64 = cgroup_clone_children_read,
4027 .write_u64 = cgroup_clone_children_write,
4028 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004029 {
4030 .name = "release_agent",
4031 .flags = CFTYPE_ONLY_ON_ROOT,
4032 .read_seq_string = cgroup_release_agent_show,
4033 .write_string = cgroup_release_agent_write,
4034 .max_write_len = PATH_MAX,
4035 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004036 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004037};
4038
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004039/**
4040 * cgroup_populate_dir - selectively creation of files in a directory
4041 * @cgrp: target cgroup
4042 * @base_files: true if the base files should be added
4043 * @subsys_mask: mask of the subsystem ids whose files should be added
4044 */
4045static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4046 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004047{
4048 int err;
4049 struct cgroup_subsys *ss;
4050
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004051 if (base_files) {
4052 err = cgroup_addrm_files(cgrp, NULL, files, true);
4053 if (err < 0)
4054 return err;
4055 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004056
Tejun Heo8e3f6542012-04-01 12:09:55 -07004057 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004058 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004059 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004060 if (!test_bit(ss->subsys_id, &subsys_mask))
4061 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004062
Tejun Heodb0416b2012-04-01 12:09:55 -07004063 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004064 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004065 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004066
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004067 /* This cgroup is ready now */
4068 for_each_subsys(cgrp->root, ss) {
4069 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4070 /*
4071 * Update id->css pointer and make this css visible from
4072 * CSS ID functions. This pointer will be dereferened
4073 * from RCU-read-side without locks.
4074 */
4075 if (css->id)
4076 rcu_assign_pointer(css->id->css, css);
4077 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078
4079 return 0;
4080}
4081
Tejun Heo48ddbe12012-04-01 12:09:56 -07004082static void css_dput_fn(struct work_struct *work)
4083{
4084 struct cgroup_subsys_state *css =
4085 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004086 struct dentry *dentry = css->cgroup->dentry;
4087 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004088
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004089 atomic_inc(&sb->s_active);
4090 dput(dentry);
4091 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004092}
4093
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094static void init_cgroup_css(struct cgroup_subsys_state *css,
4095 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004096 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097{
Paul Menagebd89aab2007-10-18 23:40:44 -07004098 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004099 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004101 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004102 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004103 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004104 BUG_ON(cgrp->subsys[ss->subsys_id]);
4105 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004106
4107 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004108 * css holds an extra ref to @cgrp->dentry which is put on the last
4109 * css_put(). dput() requires process context, which css_put() may
4110 * be called without. @css->dput_work will be used to invoke
4111 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004112 */
4113 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114}
4115
Tejun Heob1929db2012-11-19 08:13:38 -08004116/* invoke ->post_create() on a new CSS and mark it online if successful */
4117static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004118{
Tejun Heob1929db2012-11-19 08:13:38 -08004119 int ret = 0;
4120
Tejun Heoa31f2d32012-11-19 08:13:37 -08004121 lockdep_assert_held(&cgroup_mutex);
4122
Tejun Heo92fb9742012-11-19 08:13:38 -08004123 if (ss->css_online)
4124 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004125 if (!ret)
4126 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4127 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004128}
4129
4130/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4131static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4132 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4133{
4134 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4135
4136 lockdep_assert_held(&cgroup_mutex);
4137
4138 if (!(css->flags & CSS_ONLINE))
4139 return;
4140
Li Zefand7eeac12013-03-12 15:35:59 -07004141 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004142 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004143
4144 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4145}
4146
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147/*
Li Zefana043e3b2008-02-23 15:24:09 -08004148 * cgroup_create - create a cgroup
4149 * @parent: cgroup that will be parent of the new cgroup
4150 * @dentry: dentry of the new cgroup
4151 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152 *
Li Zefana043e3b2008-02-23 15:24:09 -08004153 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004156 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157{
Paul Menagebd89aab2007-10-18 23:40:44 -07004158 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004159 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004160 struct cgroupfs_root *root = parent->root;
4161 int err = 0;
4162 struct cgroup_subsys *ss;
4163 struct super_block *sb = root->sb;
4164
Tejun Heo0a950f62012-11-19 09:02:12 -08004165 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004166 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4167 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004168 return -ENOMEM;
4169
Li Zefan65dff752013-03-01 15:01:56 +08004170 name = cgroup_alloc_name(dentry);
4171 if (!name)
4172 goto err_free_cgrp;
4173 rcu_assign_pointer(cgrp->name, name);
4174
Tejun Heo0a950f62012-11-19 09:02:12 -08004175 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4176 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004177 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004178
Tejun Heo976c06b2012-11-05 09:16:59 -08004179 /*
4180 * Only live parents can have children. Note that the liveliness
4181 * check isn't strictly necessary because cgroup_mkdir() and
4182 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4183 * anyway so that locking is contained inside cgroup proper and we
4184 * don't get nasty surprises if we ever grow another caller.
4185 */
4186 if (!cgroup_lock_live_group(parent)) {
4187 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004188 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004189 }
4190
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191 /* Grab a reference on the superblock so the hierarchy doesn't
4192 * get deleted on unmount if there are child cgroups. This
4193 * can be done outside cgroup_mutex, since the sb can't
4194 * disappear while someone has an open control file on the
4195 * fs */
4196 atomic_inc(&sb->s_active);
4197
Paul Menagecc31edc2008-10-18 20:28:04 -07004198 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
Li Zefanfe1c06c2013-01-24 14:30:22 +08004200 dentry->d_fsdata = cgrp;
4201 cgrp->dentry = dentry;
4202
Paul Menagebd89aab2007-10-18 23:40:44 -07004203 cgrp->parent = parent;
4204 cgrp->root = parent->root;
4205 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206
Li Zefanb6abdb02008-03-04 14:28:19 -08004207 if (notify_on_release(parent))
4208 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4209
Tejun Heo2260e7f2012-11-19 08:13:38 -08004210 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4211 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004212
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004214 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004215
Tejun Heo92fb9742012-11-19 08:13:38 -08004216 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004217 if (IS_ERR(css)) {
4218 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004219 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004221 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004222 if (ss->use_id) {
4223 err = alloc_css_id(ss, parent, cgrp);
4224 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004225 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004226 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 }
4228
Tejun Heo4e139af2012-11-19 08:13:36 -08004229 /*
4230 * Create directory. cgroup_create_file() returns with the new
4231 * directory locked on success so that it can be populated without
4232 * dropping cgroup_mutex.
4233 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004234 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004236 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004237 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238
Tejun Heo4e139af2012-11-19 08:13:36 -08004239 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004240 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4241 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4242 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004243
Tejun Heob1929db2012-11-19 08:13:38 -08004244 /* each css holds a ref to the cgroup's dentry */
4245 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004246 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004247
Tejun Heob1929db2012-11-19 08:13:38 -08004248 /* creation succeeded, notify subsystems */
4249 for_each_subsys(root, ss) {
4250 err = online_css(ss, cgrp);
4251 if (err)
4252 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004253
4254 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4255 parent->parent) {
4256 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",
4257 current->comm, current->pid, ss->name);
4258 if (!strcmp(ss->name, "memory"))
4259 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4260 ss->warned_broken_hierarchy = true;
4261 }
Tejun Heoa8638032012-11-09 09:12:29 -08004262 }
4263
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004264 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004265 if (err)
4266 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267
4268 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004269 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270
4271 return 0;
4272
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004273err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004275 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004276 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 /* Release the reference count that we took on the superblock */
4280 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004281err_free_id:
4282 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004283err_free_name:
4284 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004285err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004286 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004288
4289err_destroy:
4290 cgroup_destroy_locked(cgrp);
4291 mutex_unlock(&cgroup_mutex);
4292 mutex_unlock(&dentry->d_inode->i_mutex);
4293 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294}
4295
Al Viro18bb1db2011-07-26 01:41:39 -04004296static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297{
4298 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4299
4300 /* the vfs holds inode->i_mutex already */
4301 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4302}
4303
Tejun Heo42809dd2012-11-19 08:13:37 -08004304static int cgroup_destroy_locked(struct cgroup *cgrp)
4305 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004306{
Tejun Heo42809dd2012-11-19 08:13:37 -08004307 struct dentry *d = cgrp->dentry;
4308 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004309 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004310 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311
Tejun Heo42809dd2012-11-19 08:13:37 -08004312 lockdep_assert_held(&d->d_inode->i_mutex);
4313 lockdep_assert_held(&cgroup_mutex);
4314
4315 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004316 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004317
Tejun Heo1a90dd52012-11-05 09:16:59 -08004318 /*
4319 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4320 * removed. This makes future css_tryget() and child creation
4321 * attempts fail thus maintaining the removal conditions verified
4322 * above.
4323 */
Tejun Heoed9577932012-11-05 09:16:58 -08004324 for_each_subsys(cgrp->root, ss) {
4325 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4326
4327 WARN_ON(atomic_read(&css->refcnt) < 0);
4328 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004329 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004330 set_bit(CGRP_REMOVED, &cgrp->flags);
4331
Tejun Heoa31f2d32012-11-19 08:13:37 -08004332 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004333 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004334 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004335
4336 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004337 * Put all the base refs. Each css holds an extra reference to the
4338 * cgroup's dentry and cgroup removal proceeds regardless of css
4339 * refs. On the last put of each css, whenever that may be, the
4340 * extra dentry ref is put so that dentry destruction happens only
4341 * after all css's are released.
4342 */
Tejun Heoe9316082012-11-05 09:16:58 -08004343 for_each_subsys(cgrp->root, ss)
4344 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004346 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004347 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004348 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004349 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004350
Paul Menage999cd8a2009-01-07 18:08:36 -08004351 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004352 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004353 list_del_init(&cgrp->allcg_node);
4354
Tejun Heo42809dd2012-11-19 08:13:37 -08004355 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356 cgroup_d_remove_dir(d);
4357 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358
Paul Menagebd89aab2007-10-18 23:40:44 -07004359 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004360 check_for_release(parent);
4361
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004362 /*
4363 * Unregister events and notify userspace.
4364 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004365 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004366 */
4367 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004368 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004369 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004370 schedule_work(&event->remove);
4371 }
Li Zefan810cbee2013-02-18 18:56:14 +08004372 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004373
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 return 0;
4375}
4376
Tejun Heo42809dd2012-11-19 08:13:37 -08004377static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4378{
4379 int ret;
4380
4381 mutex_lock(&cgroup_mutex);
4382 ret = cgroup_destroy_locked(dentry->d_fsdata);
4383 mutex_unlock(&cgroup_mutex);
4384
4385 return ret;
4386}
4387
Tejun Heo8e3f6542012-04-01 12:09:55 -07004388static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4389{
4390 INIT_LIST_HEAD(&ss->cftsets);
4391
4392 /*
4393 * base_cftset is embedded in subsys itself, no need to worry about
4394 * deregistration.
4395 */
4396 if (ss->base_cftypes) {
4397 ss->base_cftset.cfts = ss->base_cftypes;
4398 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4399 }
4400}
4401
Li Zefan06a11922008-04-29 01:00:07 -07004402static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004405
4406 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407
Tejun Heo648bb562012-11-19 08:13:36 -08004408 mutex_lock(&cgroup_mutex);
4409
Tejun Heo8e3f6542012-04-01 12:09:55 -07004410 /* init base cftset */
4411 cgroup_init_cftsets(ss);
4412
Paul Menageddbcc7e2007-10-18 23:39:30 -07004413 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004414 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004416 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004417 /* We don't handle early failures gracefully */
4418 BUG_ON(IS_ERR(css));
4419 init_cgroup_css(css, ss, dummytop);
4420
Li Zefane8d55fd2008-04-29 01:00:13 -07004421 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004422 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004423 * newly registered, all tasks and hence the
4424 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004425 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004426
4427 need_forkexit_callback |= ss->fork || ss->exit;
4428
Li Zefane8d55fd2008-04-29 01:00:13 -07004429 /* At system boot, before all subsystems have been
4430 * registered, no tasks have been forked, so we don't
4431 * need to invoke fork callbacks here. */
4432 BUG_ON(!list_empty(&init_task.tasks));
4433
Paul Menageddbcc7e2007-10-18 23:39:30 -07004434 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004435 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004436
Tejun Heo648bb562012-11-19 08:13:36 -08004437 mutex_unlock(&cgroup_mutex);
4438
Ben Blume6a11052010-03-10 15:22:09 -08004439 /* this function shouldn't be used with modular subsystems, since they
4440 * need to register a subsys_id, among other things */
4441 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004442}
4443
4444/**
Ben Blume6a11052010-03-10 15:22:09 -08004445 * cgroup_load_subsys: load and register a modular subsystem at runtime
4446 * @ss: the subsystem to load
4447 *
4448 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004449 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004450 * up for use. If the subsystem is built-in anyway, work is delegated to the
4451 * simpler cgroup_init_subsys.
4452 */
4453int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4454{
Ben Blume6a11052010-03-10 15:22:09 -08004455 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004456 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004457 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004458 struct css_set *cg;
4459 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004460
4461 /* check name and function validity */
4462 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004463 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004464 return -EINVAL;
4465
4466 /*
4467 * we don't support callbacks in modular subsystems. this check is
4468 * before the ss->module check for consistency; a subsystem that could
4469 * be a module should still have no callbacks even if the user isn't
4470 * compiling it as one.
4471 */
4472 if (ss->fork || ss->exit)
4473 return -EINVAL;
4474
4475 /*
4476 * an optionally modular subsystem is built-in: we want to do nothing,
4477 * since cgroup_init_subsys will have already taken care of it.
4478 */
4479 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004480 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004481 BUG_ON(subsys[ss->subsys_id] != ss);
4482 return 0;
4483 }
4484
Tejun Heo8e3f6542012-04-01 12:09:55 -07004485 /* init base cftset */
4486 cgroup_init_cftsets(ss);
4487
Ben Blume6a11052010-03-10 15:22:09 -08004488 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004489 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004490
4491 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004492 * no ss->css_alloc seems to need anything important in the ss
4493 * struct, so this can happen first (i.e. before the rootnode
4494 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004495 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004496 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004497 if (IS_ERR(css)) {
4498 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004499 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004500 mutex_unlock(&cgroup_mutex);
4501 return PTR_ERR(css);
4502 }
4503
4504 list_add(&ss->sibling, &rootnode.subsys_list);
4505 ss->root = &rootnode;
4506
4507 /* our new subsystem will be attached to the dummy hierarchy. */
4508 init_cgroup_css(css, ss, dummytop);
4509 /* init_idr must be after init_cgroup_css because it sets css->id. */
4510 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004511 ret = cgroup_init_idr(ss, css);
4512 if (ret)
4513 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004514 }
4515
4516 /*
4517 * Now we need to entangle the css into the existing css_sets. unlike
4518 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4519 * will need a new pointer to it; done by iterating the css_set_table.
4520 * furthermore, modifying the existing css_sets will corrupt the hash
4521 * table state, so each changed css_set will need its hash recomputed.
4522 * this is all done under the css_set_lock.
4523 */
4524 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004525 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004526 /* skip entries that we already rehashed */
4527 if (cg->subsys[ss->subsys_id])
4528 continue;
4529 /* remove existing entry */
4530 hash_del(&cg->hlist);
4531 /* set new value */
4532 cg->subsys[ss->subsys_id] = css;
4533 /* recompute hash and restore entry */
4534 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004535 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004536 }
4537 write_unlock(&css_set_lock);
4538
Ben Blume6a11052010-03-10 15:22:09 -08004539 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004540 ret = online_css(ss, dummytop);
4541 if (ret)
4542 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004543
Ben Blume6a11052010-03-10 15:22:09 -08004544 /* success! */
4545 mutex_unlock(&cgroup_mutex);
4546 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004547
4548err_unload:
4549 mutex_unlock(&cgroup_mutex);
4550 /* @ss can't be mounted here as try_module_get() would fail */
4551 cgroup_unload_subsys(ss);
4552 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004553}
4554EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4555
4556/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004557 * cgroup_unload_subsys: unload a modular subsystem
4558 * @ss: the subsystem to unload
4559 *
4560 * This function should be called in a modular subsystem's exitcall. When this
4561 * function is invoked, the refcount on the subsystem's module will be 0, so
4562 * the subsystem will not be attached to any hierarchy.
4563 */
4564void cgroup_unload_subsys(struct cgroup_subsys *ss)
4565{
4566 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004567
4568 BUG_ON(ss->module == NULL);
4569
4570 /*
4571 * we shouldn't be called if the subsystem is in use, and the use of
4572 * try_module_get in parse_cgroupfs_options should ensure that it
4573 * doesn't start being used while we're killing it off.
4574 */
4575 BUG_ON(ss->root != &rootnode);
4576
4577 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004578
Tejun Heoa31f2d32012-11-19 08:13:37 -08004579 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004580 ss->active = 0;
4581
Tejun Heoc897ff62013-02-27 17:03:49 -08004582 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004583 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004584
Ben Blumcf5d5942010-03-10 15:22:09 -08004585 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004586 subsys[ss->subsys_id] = NULL;
4587
4588 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004589 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004590
4591 /*
4592 * disentangle the css from all css_sets attached to the dummytop. as
4593 * in loading, we need to pay our respects to the hashtable gods.
4594 */
4595 write_lock(&css_set_lock);
4596 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4597 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004598 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004599
Li Zefan0ac801f2013-01-10 11:49:27 +08004600 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004601 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004602 key = css_set_hash(cg->subsys);
4603 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004604 }
4605 write_unlock(&css_set_lock);
4606
4607 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004608 * remove subsystem's css from the dummytop and free it - need to
4609 * free before marking as null because ss->css_free needs the
4610 * cgrp->subsys pointer to find their state. note that this also
4611 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004612 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004613 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004614 dummytop->subsys[ss->subsys_id] = NULL;
4615
4616 mutex_unlock(&cgroup_mutex);
4617}
4618EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4619
4620/**
Li Zefana043e3b2008-02-23 15:24:09 -08004621 * cgroup_init_early - cgroup initialization at system boot
4622 *
4623 * Initialize cgroups at system boot, and initialize any
4624 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 */
4626int __init cgroup_init_early(void)
4627{
4628 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004629 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004630 INIT_LIST_HEAD(&init_css_set.cg_links);
4631 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004632 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004633 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004635 root_count = 1;
4636 init_task.cgroups = &init_css_set;
4637
4638 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004639 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004640 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004641 &rootnode.top_cgroup.css_sets);
4642 list_add(&init_css_set_link.cg_link_list,
4643 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004645 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646 struct cgroup_subsys *ss = subsys[i];
4647
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004648 /* at bootup time, we don't worry about modular subsystems */
4649 if (!ss || ss->module)
4650 continue;
4651
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652 BUG_ON(!ss->name);
4653 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004654 BUG_ON(!ss->css_alloc);
4655 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004656 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004657 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658 ss->name, ss->subsys_id);
4659 BUG();
4660 }
4661
4662 if (ss->early_init)
4663 cgroup_init_subsys(ss);
4664 }
4665 return 0;
4666}
4667
4668/**
Li Zefana043e3b2008-02-23 15:24:09 -08004669 * cgroup_init - cgroup initialization
4670 *
4671 * Register cgroup filesystem and /proc file, and initialize
4672 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673 */
4674int __init cgroup_init(void)
4675{
4676 int err;
4677 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004678 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004679
4680 err = bdi_init(&cgroup_backing_dev_info);
4681 if (err)
4682 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004684 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004686
4687 /* at bootup time, we don't worry about modular subsystems */
4688 if (!ss || ss->module)
4689 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690 if (!ss->early_init)
4691 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004692 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004693 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694 }
4695
Li Zefan472b1052008-04-29 01:00:11 -07004696 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004697 key = css_set_hash(init_css_set.subsys);
4698 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004699 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004700
4701 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4702 if (!cgroup_kobj) {
4703 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004704 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004705 }
4706
4707 err = register_filesystem(&cgroup_fs_type);
4708 if (err < 0) {
4709 kobject_put(cgroup_kobj);
4710 goto out;
4711 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004712
Li Zefan46ae2202008-04-29 01:00:08 -07004713 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004714
Paul Menageddbcc7e2007-10-18 23:39:30 -07004715out:
Paul Menagea4243162007-10-18 23:39:35 -07004716 if (err)
4717 bdi_destroy(&cgroup_backing_dev_info);
4718
Paul Menageddbcc7e2007-10-18 23:39:30 -07004719 return err;
4720}
Paul Menageb4f48b62007-10-18 23:39:33 -07004721
Paul Menagea4243162007-10-18 23:39:35 -07004722/*
4723 * proc_cgroup_show()
4724 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4725 * - Used for /proc/<pid>/cgroup.
4726 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4727 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004728 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004729 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4730 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4731 * cgroup to top_cgroup.
4732 */
4733
4734/* TODO: Use a proper seq_file iterator */
4735static int proc_cgroup_show(struct seq_file *m, void *v)
4736{
4737 struct pid *pid;
4738 struct task_struct *tsk;
4739 char *buf;
4740 int retval;
4741 struct cgroupfs_root *root;
4742
4743 retval = -ENOMEM;
4744 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4745 if (!buf)
4746 goto out;
4747
4748 retval = -ESRCH;
4749 pid = m->private;
4750 tsk = get_pid_task(pid, PIDTYPE_PID);
4751 if (!tsk)
4752 goto out_free;
4753
4754 retval = 0;
4755
4756 mutex_lock(&cgroup_mutex);
4757
Li Zefane5f6a862009-01-07 18:07:41 -08004758 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004759 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004760 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004761 int count = 0;
4762
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004763 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004764 for_each_subsys(root, ss)
4765 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004766 if (strlen(root->name))
4767 seq_printf(m, "%sname=%s", count ? "," : "",
4768 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004769 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004770 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004771 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004772 if (retval < 0)
4773 goto out_unlock;
4774 seq_puts(m, buf);
4775 seq_putc(m, '\n');
4776 }
4777
4778out_unlock:
4779 mutex_unlock(&cgroup_mutex);
4780 put_task_struct(tsk);
4781out_free:
4782 kfree(buf);
4783out:
4784 return retval;
4785}
4786
4787static int cgroup_open(struct inode *inode, struct file *file)
4788{
4789 struct pid *pid = PROC_I(inode)->pid;
4790 return single_open(file, proc_cgroup_show, pid);
4791}
4792
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004793const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004794 .open = cgroup_open,
4795 .read = seq_read,
4796 .llseek = seq_lseek,
4797 .release = single_release,
4798};
4799
4800/* Display information about each subsystem and each hierarchy */
4801static int proc_cgroupstats_show(struct seq_file *m, void *v)
4802{
4803 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004804
Paul Menage8bab8dd2008-04-04 14:29:57 -07004805 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004806 /*
4807 * ideally we don't want subsystems moving around while we do this.
4808 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4809 * subsys/hierarchy state.
4810 */
Paul Menagea4243162007-10-18 23:39:35 -07004811 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004812 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4813 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004814 if (ss == NULL)
4815 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004816 seq_printf(m, "%s\t%d\t%d\t%d\n",
4817 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004818 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004819 }
4820 mutex_unlock(&cgroup_mutex);
4821 return 0;
4822}
4823
4824static int cgroupstats_open(struct inode *inode, struct file *file)
4825{
Al Viro9dce07f2008-03-29 03:07:28 +00004826 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004827}
4828
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004829static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004830 .open = cgroupstats_open,
4831 .read = seq_read,
4832 .llseek = seq_lseek,
4833 .release = single_release,
4834};
4835
Paul Menageb4f48b62007-10-18 23:39:33 -07004836/**
4837 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004838 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004839 *
4840 * Description: A task inherits its parent's cgroup at fork().
4841 *
4842 * A pointer to the shared css_set was automatically copied in
4843 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004844 * it was not made under the protection of RCU or cgroup_mutex, so
4845 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4846 * have already changed current->cgroups, allowing the previously
4847 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004848 *
4849 * At the point that cgroup_fork() is called, 'current' is the parent
4850 * task, and the passed argument 'child' points to the child task.
4851 */
4852void cgroup_fork(struct task_struct *child)
4853{
Tejun Heo9bb71302012-10-18 17:52:07 -07004854 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004855 child->cgroups = current->cgroups;
4856 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004857 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004858 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004859}
4860
4861/**
Li Zefana043e3b2008-02-23 15:24:09 -08004862 * cgroup_post_fork - called on a new task after adding it to the task list
4863 * @child: the task in question
4864 *
Tejun Heo5edee612012-10-16 15:03:14 -07004865 * Adds the task to the list running through its css_set if necessary and
4866 * call the subsystem fork() callbacks. Has to be after the task is
4867 * visible on the task list in case we race with the first call to
4868 * cgroup_iter_start() - to guarantee that the new task ends up on its
4869 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004870 */
Paul Menage817929e2007-10-18 23:39:36 -07004871void cgroup_post_fork(struct task_struct *child)
4872{
Tejun Heo5edee612012-10-16 15:03:14 -07004873 int i;
4874
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004875 /*
4876 * use_task_css_set_links is set to 1 before we walk the tasklist
4877 * under the tasklist_lock and we read it here after we added the child
4878 * to the tasklist under the tasklist_lock as well. If the child wasn't
4879 * yet in the tasklist when we walked through it from
4880 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4881 * should be visible now due to the paired locking and barriers implied
4882 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4883 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4884 * lock on fork.
4885 */
Paul Menage817929e2007-10-18 23:39:36 -07004886 if (use_task_css_set_links) {
4887 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004888 task_lock(child);
4889 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004890 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004891 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004892 write_unlock(&css_set_lock);
4893 }
Tejun Heo5edee612012-10-16 15:03:14 -07004894
4895 /*
4896 * Call ss->fork(). This must happen after @child is linked on
4897 * css_set; otherwise, @child might change state between ->fork()
4898 * and addition to css_set.
4899 */
4900 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004901 /*
4902 * fork/exit callbacks are supported only for builtin
4903 * subsystems, and the builtin section of the subsys
4904 * array is immutable, so we don't need to lock the
4905 * subsys array here. On the other hand, modular section
4906 * of the array can be freed at module unload, so we
4907 * can't touch that.
4908 */
4909 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004910 struct cgroup_subsys *ss = subsys[i];
4911
Tejun Heo5edee612012-10-16 15:03:14 -07004912 if (ss->fork)
4913 ss->fork(child);
4914 }
4915 }
Paul Menage817929e2007-10-18 23:39:36 -07004916}
Tejun Heo5edee612012-10-16 15:03:14 -07004917
Paul Menage817929e2007-10-18 23:39:36 -07004918/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004919 * cgroup_exit - detach cgroup from exiting task
4920 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004921 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004922 *
4923 * Description: Detach cgroup from @tsk and release it.
4924 *
4925 * Note that cgroups marked notify_on_release force every task in
4926 * them to take the global cgroup_mutex mutex when exiting.
4927 * This could impact scaling on very large systems. Be reluctant to
4928 * use notify_on_release cgroups where very high task exit scaling
4929 * is required on large systems.
4930 *
4931 * the_top_cgroup_hack:
4932 *
4933 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4934 *
4935 * We call cgroup_exit() while the task is still competent to
4936 * handle notify_on_release(), then leave the task attached to the
4937 * root cgroup in each hierarchy for the remainder of its exit.
4938 *
4939 * To do this properly, we would increment the reference count on
4940 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4941 * code we would add a second cgroup function call, to drop that
4942 * reference. This would just create an unnecessary hot spot on
4943 * the top_cgroup reference count, to no avail.
4944 *
4945 * Normally, holding a reference to a cgroup without bumping its
4946 * count is unsafe. The cgroup could go away, or someone could
4947 * attach us to a different cgroup, decrementing the count on
4948 * the first cgroup that we never incremented. But in this case,
4949 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004950 * which wards off any cgroup_attach_task() attempts, or task is a failed
4951 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004952 */
4953void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4954{
Paul Menage817929e2007-10-18 23:39:36 -07004955 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004956 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004957
4958 /*
4959 * Unlink from the css_set task list if necessary.
4960 * Optimistically check cg_list before taking
4961 * css_set_lock
4962 */
4963 if (!list_empty(&tsk->cg_list)) {
4964 write_lock(&css_set_lock);
4965 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004966 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004967 write_unlock(&css_set_lock);
4968 }
4969
Paul Menageb4f48b62007-10-18 23:39:33 -07004970 /* Reassign the task to the init_css_set. */
4971 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004972 cg = tsk->cgroups;
4973 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004974
4975 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004976 /*
4977 * fork/exit callbacks are supported only for builtin
4978 * subsystems, see cgroup_post_fork() for details.
4979 */
4980 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004981 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004982
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004983 if (ss->exit) {
4984 struct cgroup *old_cgrp =
4985 rcu_dereference_raw(cg->subsys[i])->cgroup;
4986 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004987 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004988 }
4989 }
4990 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004991 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004992
Li Zefanb5d646f2013-01-24 14:43:51 +08004993 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004994}
Paul Menage697f4162007-10-18 23:39:34 -07004995
Paul Menagebd89aab2007-10-18 23:40:44 -07004996static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004997{
4998 /* All of these checks rely on RCU to keep the cgroup
4999 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08005000 if (cgroup_is_releasable(cgrp) &&
5001 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
5002 /*
5003 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005004 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005005 * it now
5006 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005007 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005008
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005009 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005010 if (!cgroup_is_removed(cgrp) &&
5011 list_empty(&cgrp->release_list)) {
5012 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005013 need_schedule_work = 1;
5014 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005015 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005016 if (need_schedule_work)
5017 schedule_work(&release_agent_work);
5018 }
5019}
5020
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005021/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005022bool __css_tryget(struct cgroup_subsys_state *css)
5023{
Tejun Heoe9316082012-11-05 09:16:58 -08005024 while (true) {
5025 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005026
Tejun Heoe9316082012-11-05 09:16:58 -08005027 v = css_refcnt(css);
5028 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5029 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005030 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005031 else if (t < 0)
5032 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005033 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005034 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005035}
5036EXPORT_SYMBOL_GPL(__css_tryget);
5037
5038/* Caller must verify that the css is not for root cgroup */
5039void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005041 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005042
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005043 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005044 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005045 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005046}
Ben Blum67523c42010-03-10 15:22:11 -08005047EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005048
5049/*
5050 * Notify userspace when a cgroup is released, by running the
5051 * configured release agent with the name of the cgroup (path
5052 * relative to the root of cgroup file system) as the argument.
5053 *
5054 * Most likely, this user command will try to rmdir this cgroup.
5055 *
5056 * This races with the possibility that some other task will be
5057 * attached to this cgroup before it is removed, or that some other
5058 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5059 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5060 * unused, and this cgroup will be reprieved from its death sentence,
5061 * to continue to serve a useful existence. Next time it's released,
5062 * we will get notified again, if it still has 'notify_on_release' set.
5063 *
5064 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5065 * means only wait until the task is successfully execve()'d. The
5066 * separate release agent task is forked by call_usermodehelper(),
5067 * then control in this thread returns here, without waiting for the
5068 * release agent task. We don't bother to wait because the caller of
5069 * this routine has no use for the exit status of the release agent
5070 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005071 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072static void cgroup_release_agent(struct work_struct *work)
5073{
5074 BUG_ON(work != &release_agent_work);
5075 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005076 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005077 while (!list_empty(&release_list)) {
5078 char *argv[3], *envp[3];
5079 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005080 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005081 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005082 struct cgroup,
5083 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005084 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005085 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005086 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005087 if (!pathbuf)
5088 goto continue_free;
5089 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5090 goto continue_free;
5091 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5092 if (!agentbuf)
5093 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094
5095 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005096 argv[i++] = agentbuf;
5097 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005098 argv[i] = NULL;
5099
5100 i = 0;
5101 /* minimal command environment */
5102 envp[i++] = "HOME=/";
5103 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5104 envp[i] = NULL;
5105
5106 /* Drop the lock while we invoke the usermode helper,
5107 * since the exec could involve hitting disk and hence
5108 * be a slow process */
5109 mutex_unlock(&cgroup_mutex);
5110 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005111 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005112 continue_free:
5113 kfree(pathbuf);
5114 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005115 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005116 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005117 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005118 mutex_unlock(&cgroup_mutex);
5119}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005120
5121static int __init cgroup_disable(char *str)
5122{
5123 int i;
5124 char *token;
5125
5126 while ((token = strsep(&str, ",")) != NULL) {
5127 if (!*token)
5128 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005129 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005130 struct cgroup_subsys *ss = subsys[i];
5131
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005132 /*
5133 * cgroup_disable, being at boot time, can't
5134 * know about module subsystems, so we don't
5135 * worry about them.
5136 */
5137 if (!ss || ss->module)
5138 continue;
5139
Paul Menage8bab8dd2008-04-04 14:29:57 -07005140 if (!strcmp(token, ss->name)) {
5141 ss->disabled = 1;
5142 printk(KERN_INFO "Disabling %s control group"
5143 " subsystem\n", ss->name);
5144 break;
5145 }
5146 }
5147 }
5148 return 1;
5149}
5150__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005151
5152/*
5153 * Functons for CSS ID.
5154 */
5155
5156/*
5157 *To get ID other than 0, this should be called when !cgroup_is_removed().
5158 */
5159unsigned short css_id(struct cgroup_subsys_state *css)
5160{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005161 struct css_id *cssid;
5162
5163 /*
5164 * This css_id() can return correct value when somone has refcnt
5165 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5166 * it's unchanged until freed.
5167 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005168 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005169
5170 if (cssid)
5171 return cssid->id;
5172 return 0;
5173}
Ben Blum67523c42010-03-10 15:22:11 -08005174EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005175
5176unsigned short css_depth(struct cgroup_subsys_state *css)
5177{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005178 struct css_id *cssid;
5179
Tejun Heo28b4c272012-04-01 12:09:56 -07005180 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005181
5182 if (cssid)
5183 return cssid->depth;
5184 return 0;
5185}
Ben Blum67523c42010-03-10 15:22:11 -08005186EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005187
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005188/**
5189 * css_is_ancestor - test "root" css is an ancestor of "child"
5190 * @child: the css to be tested.
5191 * @root: the css supporsed to be an ancestor of the child.
5192 *
5193 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005194 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005195 * But, considering usual usage, the csses should be valid objects after test.
5196 * Assuming that the caller will do some action to the child if this returns
5197 * returns true, the caller must take "child";s reference count.
5198 * If "child" is valid object and this returns true, "root" is valid, too.
5199 */
5200
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005202 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005203{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005204 struct css_id *child_id;
5205 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005206
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005207 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005208 if (!child_id)
5209 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005210 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005211 if (!root_id)
5212 return false;
5213 if (child_id->depth < root_id->depth)
5214 return false;
5215 if (child_id->stack[root_id->depth] != root_id->id)
5216 return false;
5217 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005218}
5219
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005220void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5221{
5222 struct css_id *id = css->id;
5223 /* When this is called before css_id initialization, id can be NULL */
5224 if (!id)
5225 return;
5226
5227 BUG_ON(!ss->use_id);
5228
5229 rcu_assign_pointer(id->css, NULL);
5230 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005231 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005232 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005233 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005234 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235}
Ben Blum67523c42010-03-10 15:22:11 -08005236EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005237
5238/*
5239 * This is called by init or create(). Then, calls to this function are
5240 * always serialized (By cgroup_mutex() at create()).
5241 */
5242
5243static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5244{
5245 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005246 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005247
5248 BUG_ON(!ss->use_id);
5249
5250 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5251 newid = kzalloc(size, GFP_KERNEL);
5252 if (!newid)
5253 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005254
5255 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005256 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005258 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005259 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005260 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261
5262 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005263 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005264 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005265
Tejun Heod228d9e2013-02-27 17:04:54 -08005266 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005267 newid->depth = depth;
5268 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005269err_out:
5270 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005271 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005272
5273}
5274
Ben Blume6a11052010-03-10 15:22:09 -08005275static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5276 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277{
5278 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005279
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005280 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281 idr_init(&ss->idr);
5282
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005283 newid = get_new_cssid(ss, 0);
5284 if (IS_ERR(newid))
5285 return PTR_ERR(newid);
5286
5287 newid->stack[0] = newid->id;
5288 newid->css = rootcss;
5289 rootcss->id = newid;
5290 return 0;
5291}
5292
5293static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5294 struct cgroup *child)
5295{
5296 int subsys_id, i, depth = 0;
5297 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005298 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005299
5300 subsys_id = ss->subsys_id;
5301 parent_css = parent->subsys[subsys_id];
5302 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005303 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005304 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005305
5306 child_id = get_new_cssid(ss, depth);
5307 if (IS_ERR(child_id))
5308 return PTR_ERR(child_id);
5309
5310 for (i = 0; i < depth; i++)
5311 child_id->stack[i] = parent_id->stack[i];
5312 child_id->stack[depth] = child_id->id;
5313 /*
5314 * child_id->css pointer will be set after this cgroup is available
5315 * see cgroup_populate_dir()
5316 */
5317 rcu_assign_pointer(child_css->id, child_id);
5318
5319 return 0;
5320}
5321
5322/**
5323 * css_lookup - lookup css by id
5324 * @ss: cgroup subsys to be looked into.
5325 * @id: the id
5326 *
5327 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5328 * NULL if not. Should be called under rcu_read_lock()
5329 */
5330struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5331{
5332 struct css_id *cssid = NULL;
5333
5334 BUG_ON(!ss->use_id);
5335 cssid = idr_find(&ss->idr, id);
5336
5337 if (unlikely(!cssid))
5338 return NULL;
5339
5340 return rcu_dereference(cssid->css);
5341}
Ben Blum67523c42010-03-10 15:22:11 -08005342EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005343
5344/**
5345 * css_get_next - lookup next cgroup under specified hierarchy.
5346 * @ss: pointer to subsystem
5347 * @id: current position of iteration.
5348 * @root: pointer to css. search tree under this.
5349 * @foundid: position of found object.
5350 *
5351 * Search next css under the specified hierarchy of rootid. Calling under
5352 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5353 */
5354struct cgroup_subsys_state *
5355css_get_next(struct cgroup_subsys *ss, int id,
5356 struct cgroup_subsys_state *root, int *foundid)
5357{
5358 struct cgroup_subsys_state *ret = NULL;
5359 struct css_id *tmp;
5360 int tmpid;
5361 int rootid = css_id(root);
5362 int depth = css_depth(root);
5363
5364 if (!rootid)
5365 return NULL;
5366
5367 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005368 WARN_ON_ONCE(!rcu_read_lock_held());
5369
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005370 /* fill start point for scan */
5371 tmpid = id;
5372 while (1) {
5373 /*
5374 * scan next entry from bitmap(tree), tmpid is updated after
5375 * idr_get_next().
5376 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005377 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378 if (!tmp)
5379 break;
5380 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5381 ret = rcu_dereference(tmp->css);
5382 if (ret) {
5383 *foundid = tmpid;
5384 break;
5385 }
5386 }
5387 /* continue to scan from next id */
5388 tmpid = tmpid + 1;
5389 }
5390 return ret;
5391}
5392
Stephane Eraniane5d13672011-02-14 11:20:01 +02005393/*
5394 * get corresponding css from file open on cgroupfs directory
5395 */
5396struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5397{
5398 struct cgroup *cgrp;
5399 struct inode *inode;
5400 struct cgroup_subsys_state *css;
5401
Al Viro496ad9a2013-01-23 17:07:38 -05005402 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005403 /* check in cgroup filesystem dir */
5404 if (inode->i_op != &cgroup_dir_inode_operations)
5405 return ERR_PTR(-EBADF);
5406
5407 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5408 return ERR_PTR(-EINVAL);
5409
5410 /* get cgroup */
5411 cgrp = __d_cgrp(f->f_dentry);
5412 css = cgrp->subsys[id];
5413 return css ? css : ERR_PTR(-ENOENT);
5414}
5415
Paul Menagefe693432009-09-23 15:56:20 -07005416#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005417static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005418{
5419 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5420
5421 if (!css)
5422 return ERR_PTR(-ENOMEM);
5423
5424 return css;
5425}
5426
Tejun Heo92fb9742012-11-19 08:13:38 -08005427static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005428{
5429 kfree(cont->subsys[debug_subsys_id]);
5430}
5431
5432static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5433{
5434 return atomic_read(&cont->count);
5435}
5436
5437static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5438{
5439 return cgroup_task_count(cont);
5440}
5441
5442static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5443{
5444 return (u64)(unsigned long)current->cgroups;
5445}
5446
5447static u64 current_css_set_refcount_read(struct cgroup *cont,
5448 struct cftype *cft)
5449{
5450 u64 count;
5451
5452 rcu_read_lock();
5453 count = atomic_read(&current->cgroups->refcount);
5454 rcu_read_unlock();
5455 return count;
5456}
5457
Paul Menage7717f7b2009-09-23 15:56:22 -07005458static int current_css_set_cg_links_read(struct cgroup *cont,
5459 struct cftype *cft,
5460 struct seq_file *seq)
5461{
5462 struct cg_cgroup_link *link;
5463 struct css_set *cg;
5464
5465 read_lock(&css_set_lock);
5466 rcu_read_lock();
5467 cg = rcu_dereference(current->cgroups);
5468 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5469 struct cgroup *c = link->cgrp;
5470 const char *name;
5471
5472 if (c->dentry)
5473 name = c->dentry->d_name.name;
5474 else
5475 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005476 seq_printf(seq, "Root %d group %s\n",
5477 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005478 }
5479 rcu_read_unlock();
5480 read_unlock(&css_set_lock);
5481 return 0;
5482}
5483
5484#define MAX_TASKS_SHOWN_PER_CSS 25
5485static int cgroup_css_links_read(struct cgroup *cont,
5486 struct cftype *cft,
5487 struct seq_file *seq)
5488{
5489 struct cg_cgroup_link *link;
5490
5491 read_lock(&css_set_lock);
5492 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5493 struct css_set *cg = link->cg;
5494 struct task_struct *task;
5495 int count = 0;
5496 seq_printf(seq, "css_set %p\n", cg);
5497 list_for_each_entry(task, &cg->tasks, cg_list) {
5498 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5499 seq_puts(seq, " ...\n");
5500 break;
5501 } else {
5502 seq_printf(seq, " task %d\n",
5503 task_pid_vnr(task));
5504 }
5505 }
5506 }
5507 read_unlock(&css_set_lock);
5508 return 0;
5509}
5510
Paul Menagefe693432009-09-23 15:56:20 -07005511static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5512{
5513 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5514}
5515
5516static struct cftype debug_files[] = {
5517 {
5518 .name = "cgroup_refcount",
5519 .read_u64 = cgroup_refcount_read,
5520 },
5521 {
5522 .name = "taskcount",
5523 .read_u64 = debug_taskcount_read,
5524 },
5525
5526 {
5527 .name = "current_css_set",
5528 .read_u64 = current_css_set_read,
5529 },
5530
5531 {
5532 .name = "current_css_set_refcount",
5533 .read_u64 = current_css_set_refcount_read,
5534 },
5535
5536 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005537 .name = "current_css_set_cg_links",
5538 .read_seq_string = current_css_set_cg_links_read,
5539 },
5540
5541 {
5542 .name = "cgroup_css_links",
5543 .read_seq_string = cgroup_css_links_read,
5544 },
5545
5546 {
Paul Menagefe693432009-09-23 15:56:20 -07005547 .name = "releasable",
5548 .read_u64 = releasable_read,
5549 },
Paul Menagefe693432009-09-23 15:56:20 -07005550
Tejun Heo4baf6e32012-04-01 12:09:55 -07005551 { } /* terminate */
5552};
Paul Menagefe693432009-09-23 15:56:20 -07005553
5554struct cgroup_subsys debug_subsys = {
5555 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005556 .css_alloc = debug_css_alloc,
5557 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005558 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005559 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005560};
5561#endif /* CONFIG_CGROUP_DEBUG */