blob: 1a65958c1a0be2f5a05295a96821fbc106bf19d0 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080062#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
90 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020091 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080092 * registered after that. The mutable section of this array is protected by
93 * cgroup_mutex.
94 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020095#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020096#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080097static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -070098#include <linux/cgroup_subsys.h>
99};
100
Paul Menagec6d57f32009-09-23 15:56:19 -0700101#define MAX_CGROUP_ROOT_NAMELEN 64
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * A cgroupfs_root represents the root of a cgroup hierarchy,
105 * and may be associated with a superblock to form an active
106 * hierarchy
107 */
108struct cgroupfs_root {
109 struct super_block *sb;
110
111 /*
112 * The bitmask of subsystems intended to be attached to this
113 * hierarchy
114 */
Aristeu 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
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700332/**
333 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
334 * @cgrp: the cgroup to be checked for liveness
335 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700336 * On success, returns true; the mutex should be later unlocked. On
337 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700338 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700339static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700340{
341 mutex_lock(&cgroup_mutex);
342 if (cgroup_is_removed(cgrp)) {
343 mutex_unlock(&cgroup_mutex);
344 return false;
345 }
346 return true;
347}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700348
Paul Menage81a6a5c2007-10-18 23:39:38 -0700349/* the list of cgroups eligible for automatic release. Protected by
350 * release_list_lock */
351static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200352static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700353static void cgroup_release_agent(struct work_struct *work);
354static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700355static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700356
Paul Menage817929e2007-10-18 23:39:36 -0700357/* Link structure for associating css_set objects with cgroups */
358struct cg_cgroup_link {
359 /*
360 * List running through cg_cgroup_links associated with a
361 * cgroup, anchored on cgroup->css_sets
362 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700363 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700364 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700365 /*
366 * List running through cg_cgroup_links pointing at a
367 * single css_set object, anchored on css_set->cg_links
368 */
369 struct list_head cg_link_list;
370 struct css_set *cg;
371};
372
373/* The default css_set - used by init and its children prior to any
374 * hierarchies being mounted. It contains a pointer to the root state
375 * for each subsystem. Also used to anchor the list of css_sets. Not
376 * reference-counted, to improve performance when child cgroups
377 * haven't been created.
378 */
379
380static struct css_set init_css_set;
381static struct cg_cgroup_link init_css_set_link;
382
Ben Blume6a11052010-03-10 15:22:09 -0800383static int cgroup_init_idr(struct cgroup_subsys *ss,
384 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700385
Paul Menage817929e2007-10-18 23:39:36 -0700386/* css_set_lock protects the list of css_set objects, and the
387 * chain of tasks off each css_set. Nests outside task->alloc_lock
388 * due to cgroup_iter_start() */
389static DEFINE_RWLOCK(css_set_lock);
390static int css_set_count;
391
Paul Menage7717f7b2009-09-23 15:56:22 -0700392/*
393 * hash table for cgroup groups. This improves the performance to find
394 * an existing css_set. This hash doesn't (currently) take into
395 * account cgroups in empty hierarchies.
396 */
Li Zefan472b1052008-04-29 01:00:11 -0700397#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800398static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700399
Li Zefan0ac801f2013-01-10 11:49:27 +0800400static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700401{
402 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800403 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700404
405 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800406 key += (unsigned long)css[i];
407 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700408
Li Zefan0ac801f2013-01-10 11:49:27 +0800409 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700410}
411
Paul Menage817929e2007-10-18 23:39:36 -0700412/* We don't maintain the lists running through each css_set to its
413 * task until after the first call to cgroup_iter_start(). This
414 * reduces the fork()/exit() overhead for people who have cgroups
415 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700416static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700419{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700420 struct cg_cgroup_link *link;
421 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700422 /*
423 * Ensure that the refcount doesn't hit zero while any readers
424 * can see it. Similar to atomic_dec_and_lock(), but for an
425 * rwlock
426 */
427 if (atomic_add_unless(&cg->refcount, -1, 1))
428 return;
429 write_lock(&css_set_lock);
430 if (!atomic_dec_and_test(&cg->refcount)) {
431 write_unlock(&css_set_lock);
432 return;
433 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700435 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800436 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700437 css_set_count--;
438
439 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
440 cg_link_list) {
441 struct cgroup *cgrp = link->cgrp;
442 list_del(&link->cg_link_list);
443 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800444
445 /*
446 * We may not be holding cgroup_mutex, and if cgrp->count is
447 * dropped to 0 the cgroup can be destroyed at any time, hence
448 * rcu_read_lock is used to keep it alive.
449 */
450 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700451 if (atomic_dec_and_test(&cgrp->count) &&
452 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700453 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700454 set_bit(CGRP_RELEASABLE, &cgrp->flags);
455 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456 }
Li Zefan71b57072013-01-24 14:43:28 +0800457 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700458
459 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700461
462 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800463 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700464}
465
466/*
467 * refcounted get/put for css_set objects
468 */
469static inline void get_css_set(struct css_set *cg)
470{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700471 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700472}
473
474static inline void put_css_set(struct css_set *cg)
475{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700476 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700477}
478
Paul Menage81a6a5c2007-10-18 23:39:38 -0700479static inline void put_css_set_taskexit(struct css_set *cg)
480{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700481 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700482}
483
Paul Menage817929e2007-10-18 23:39:36 -0700484/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 * compare_css_sets - helper function for find_existing_css_set().
486 * @cg: candidate css_set being tested
487 * @old_cg: existing css_set for a task
488 * @new_cgrp: cgroup that's being entered by the task
489 * @template: desired set of css pointers in css_set (pre-calculated)
490 *
491 * Returns true if "cg" matches "old_cg" except for the hierarchy
492 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
493 */
494static bool compare_css_sets(struct css_set *cg,
495 struct css_set *old_cg,
496 struct cgroup *new_cgrp,
497 struct cgroup_subsys_state *template[])
498{
499 struct list_head *l1, *l2;
500
501 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
502 /* Not all subsystems matched */
503 return false;
504 }
505
506 /*
507 * Compare cgroup pointers in order to distinguish between
508 * different cgroups in heirarchies with no subsystems. We
509 * could get by with just this check alone (and skip the
510 * memcmp above) but on most setups the memcmp check will
511 * avoid the need for this more expensive check on almost all
512 * candidates.
513 */
514
515 l1 = &cg->cg_links;
516 l2 = &old_cg->cg_links;
517 while (1) {
518 struct cg_cgroup_link *cgl1, *cgl2;
519 struct cgroup *cg1, *cg2;
520
521 l1 = l1->next;
522 l2 = l2->next;
523 /* See if we reached the end - both lists are equal length. */
524 if (l1 == &cg->cg_links) {
525 BUG_ON(l2 != &old_cg->cg_links);
526 break;
527 } else {
528 BUG_ON(l2 == &old_cg->cg_links);
529 }
530 /* Locate the cgroups associated with these links. */
531 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
532 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
533 cg1 = cgl1->cgrp;
534 cg2 = cgl2->cgrp;
535 /* Hierarchies should be linked in the same order. */
536 BUG_ON(cg1->root != cg2->root);
537
538 /*
539 * If this hierarchy is the hierarchy of the cgroup
540 * that's changing, then we need to check that this
541 * css_set points to the new cgroup; if it's any other
542 * hierarchy, then this css_set should point to the
543 * same cgroup as the old css_set.
544 */
545 if (cg1->root == new_cgrp->root) {
546 if (cg1 != new_cgrp)
547 return false;
548 } else {
549 if (cg1 != cg2)
550 return false;
551 }
552 }
553 return true;
554}
555
556/*
Paul Menage817929e2007-10-18 23:39:36 -0700557 * find_existing_css_set() is a helper for
558 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700559 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700560 *
561 * oldcg: the cgroup group that we're using before the cgroup
562 * transition
563 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700564 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700565 *
566 * template: location in which to build the desired set of subsystem
567 * state objects for the new cgroup group
568 */
Paul Menage817929e2007-10-18 23:39:36 -0700569static struct css_set *find_existing_css_set(
570 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700571 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700572 struct cgroup_subsys_state *template[])
573{
574 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700575 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700576 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800577 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700578
Ben Blumaae8aab2010-03-10 15:22:07 -0800579 /*
580 * Build the set of subsystem state objects that we want to see in the
581 * new css_set. while subsystems can change globally, the entries here
582 * won't change, so no need for locking.
583 */
Paul Menage817929e2007-10-18 23:39:36 -0700584 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400585 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700586 /* Subsystem is in this hierarchy. So we want
587 * the subsystem state from the new
588 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700589 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700590 } else {
591 /* Subsystem is not in this hierarchy, so we
592 * don't want to change the subsystem state */
593 template[i] = oldcg->subsys[i];
594 }
595 }
596
Li Zefan0ac801f2013-01-10 11:49:27 +0800597 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800598 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700599 if (!compare_css_sets(cg, oldcg, cgrp, template))
600 continue;
601
602 /* This css_set matches what we need */
603 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700604 }
Paul Menage817929e2007-10-18 23:39:36 -0700605
606 /* No existing cgroup group matched */
607 return NULL;
608}
609
Paul Menage817929e2007-10-18 23:39:36 -0700610static void free_cg_links(struct list_head *tmp)
611{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700612 struct cg_cgroup_link *link;
613 struct cg_cgroup_link *saved_link;
614
615 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700616 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700617 kfree(link);
618 }
619}
620
621/*
Li Zefan36553432008-07-29 22:33:19 -0700622 * allocate_cg_links() allocates "count" cg_cgroup_link structures
623 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
624 * success or a negative error
625 */
626static int allocate_cg_links(int count, struct list_head *tmp)
627{
628 struct cg_cgroup_link *link;
629 int i;
630 INIT_LIST_HEAD(tmp);
631 for (i = 0; i < count; i++) {
632 link = kmalloc(sizeof(*link), GFP_KERNEL);
633 if (!link) {
634 free_cg_links(tmp);
635 return -ENOMEM;
636 }
637 list_add(&link->cgrp_link_list, tmp);
638 }
639 return 0;
640}
641
Li Zefanc12f65d2009-01-07 18:07:42 -0800642/**
643 * link_css_set - a helper function to link a css_set to a cgroup
644 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
645 * @cg: the css_set to be linked
646 * @cgrp: the destination cgroup
647 */
648static void link_css_set(struct list_head *tmp_cg_links,
649 struct css_set *cg, struct cgroup *cgrp)
650{
651 struct cg_cgroup_link *link;
652
653 BUG_ON(list_empty(tmp_cg_links));
654 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
655 cgrp_link_list);
656 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700657 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700658 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800659 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700660 /*
661 * Always add links to the tail of the list so that the list
662 * is sorted by order of hierarchy creation
663 */
664 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800665}
666
Li Zefan36553432008-07-29 22:33:19 -0700667/*
Paul Menage817929e2007-10-18 23:39:36 -0700668 * find_css_set() takes an existing cgroup group and a
669 * cgroup object, and returns a css_set object that's
670 * equivalent to the old group, but with the given cgroup
671 * substituted into the appropriate hierarchy. Must be called with
672 * cgroup_mutex held
673 */
Paul Menage817929e2007-10-18 23:39:36 -0700674static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700675 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700676{
677 struct css_set *res;
678 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700679
680 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700681
Paul Menage7717f7b2009-09-23 15:56:22 -0700682 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800683 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700684
Paul Menage817929e2007-10-18 23:39:36 -0700685 /* First see if we already have a cgroup group that matches
686 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700687 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700688 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700689 if (res)
690 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700691 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700692
693 if (res)
694 return res;
695
696 res = kmalloc(sizeof(*res), GFP_KERNEL);
697 if (!res)
698 return NULL;
699
700 /* Allocate all the cg_cgroup_link objects that we'll need */
701 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
702 kfree(res);
703 return NULL;
704 }
705
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700706 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700707 INIT_LIST_HEAD(&res->cg_links);
708 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700709 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700710
711 /* Copy the set of subsystem state objects generated in
712 * find_existing_css_set() */
713 memcpy(res->subsys, template, sizeof(res->subsys));
714
715 write_lock(&css_set_lock);
716 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700717 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
718 struct cgroup *c = link->cgrp;
719 if (c->root == cgrp->root)
720 c = cgrp;
721 link_css_set(&tmp_cg_links, res, c);
722 }
Paul Menage817929e2007-10-18 23:39:36 -0700723
724 BUG_ON(!list_empty(&tmp_cg_links));
725
Paul Menage817929e2007-10-18 23:39:36 -0700726 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700727
728 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800729 key = css_set_hash(res->subsys);
730 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700731
Paul Menage817929e2007-10-18 23:39:36 -0700732 write_unlock(&css_set_lock);
733
734 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700735}
736
Paul Menageddbcc7e2007-10-18 23:39:30 -0700737/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700738 * Return the cgroup for "task" from the given hierarchy. Must be
739 * called with cgroup_mutex held.
740 */
741static struct cgroup *task_cgroup_from_root(struct task_struct *task,
742 struct cgroupfs_root *root)
743{
744 struct css_set *css;
745 struct cgroup *res = NULL;
746
747 BUG_ON(!mutex_is_locked(&cgroup_mutex));
748 read_lock(&css_set_lock);
749 /*
750 * No need to lock the task - since we hold cgroup_mutex the
751 * task can't change groups, so the only thing that can happen
752 * is that it exits and its css is set back to init_css_set.
753 */
754 css = task->cgroups;
755 if (css == &init_css_set) {
756 res = &root->top_cgroup;
757 } else {
758 struct cg_cgroup_link *link;
759 list_for_each_entry(link, &css->cg_links, cg_link_list) {
760 struct cgroup *c = link->cgrp;
761 if (c->root == root) {
762 res = c;
763 break;
764 }
765 }
766 }
767 read_unlock(&css_set_lock);
768 BUG_ON(!res);
769 return res;
770}
771
772/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * There is one global cgroup mutex. We also require taking
774 * task_lock() when dereferencing a task's cgroup subsys pointers.
775 * See "The task_lock() exception", at the end of this comment.
776 *
777 * A task must hold cgroup_mutex to modify cgroups.
778 *
779 * Any task can increment and decrement the count field without lock.
780 * So in general, code holding cgroup_mutex can't rely on the count
781 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800782 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * means that no tasks are currently attached, therefore there is no
784 * way a task attached to that cgroup can fork (the other way to
785 * increment the count). So code holding cgroup_mutex can safely
786 * assume that if the count is zero, it will stay zero. Similarly, if
787 * a task holds cgroup_mutex on a cgroup with zero count, it
788 * knows that the cgroup won't be removed, as cgroup_rmdir()
789 * needs that mutex.
790 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
792 * (usually) take cgroup_mutex. These are the two most performance
793 * critical pieces of code here. The exception occurs on cgroup_exit(),
794 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
795 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800796 * to the release agent with the name of the cgroup (path relative to
797 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 *
799 * A cgroup can only be deleted if both its 'count' of using tasks
800 * is zero, and its list of 'children' cgroups is empty. Since all
801 * tasks in the system use _some_ cgroup, and since there is always at
802 * least one task in the system (init, pid == 1), therefore, top_cgroup
803 * always has either children cgroups and/or using tasks. So we don't
804 * need a special hack to ensure that top_cgroup cannot be deleted.
805 *
806 * The task_lock() exception
807 *
808 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800809 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800810 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 * several performance critical places that need to reference
812 * task->cgroup without the expense of grabbing a system global
813 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800814 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
816 * the task_struct routinely used for such matters.
817 *
818 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800819 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 */
821
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822/*
823 * A couple of forward declarations required, due to cyclic reference loop:
824 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
825 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
826 * -> cgroup_mkdir.
827 */
828
Al Viro18bb1db2011-07-26 01:41:39 -0400829static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400830static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400832static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
833 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700834static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700835static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700836
837static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200838 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700839 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700840};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700841
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700842static int alloc_css_id(struct cgroup_subsys *ss,
843 struct cgroup *parent, struct cgroup *child);
844
Al Viroa5e7ed32011-07-26 01:55:55 -0400845static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846{
847 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848
849 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400850 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100852 inode->i_uid = current_fsuid();
853 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700854 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
855 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
856 }
857 return inode;
858}
859
Li Zefan65dff752013-03-01 15:01:56 +0800860static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
861{
862 struct cgroup_name *name;
863
864 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
865 if (!name)
866 return NULL;
867 strcpy(name->name, dentry->d_name.name);
868 return name;
869}
870
Li Zefanbe445622013-01-24 14:31:42 +0800871static void cgroup_free_fn(struct work_struct *work)
872{
873 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
874 struct cgroup_subsys *ss;
875
876 mutex_lock(&cgroup_mutex);
877 /*
878 * Release the subsystem state objects.
879 */
880 for_each_subsys(cgrp->root, ss)
881 ss->css_free(cgrp);
882
883 cgrp->root->number_of_cgroups--;
884 mutex_unlock(&cgroup_mutex);
885
886 /*
887 * Drop the active superblock reference that we took when we
888 * created the cgroup
889 */
890 deactivate_super(cgrp->root->sb);
891
892 /*
893 * if we're getting rid of the cgroup, refcount should ensure
894 * that there are no pidlists left.
895 */
896 BUG_ON(!list_empty(&cgrp->pidlists));
897
898 simple_xattrs_free(&cgrp->xattrs);
899
900 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +0800901 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800902 kfree(cgrp);
903}
904
905static void cgroup_free_rcu(struct rcu_head *head)
906{
907 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
908
909 schedule_work(&cgrp->free_work);
910}
911
Paul Menageddbcc7e2007-10-18 23:39:30 -0700912static void cgroup_diput(struct dentry *dentry, struct inode *inode)
913{
914 /* is dentry a directory ? if so, kfree() associated cgroup */
915 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700916 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800917
Paul Menagebd89aab2007-10-18 23:40:44 -0700918 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800919 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 } else {
921 struct cfent *cfe = __d_cfe(dentry);
922 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400923 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924
925 WARN_ONCE(!list_empty(&cfe->node) &&
926 cgrp != &cgrp->root->top_cgroup,
927 "cfe still linked for %s\n", cfe->type->name);
928 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400929 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 }
931 iput(inode);
932}
933
Al Viroc72a04e2011-01-14 05:31:45 +0000934static int cgroup_delete(const struct dentry *d)
935{
936 return 1;
937}
938
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939static void remove_dir(struct dentry *d)
940{
941 struct dentry *parent = dget(d->d_parent);
942
943 d_delete(d);
944 simple_rmdir(parent->d_inode, d);
945 dput(parent);
946}
947
Li Zefan2739d3c2013-01-21 18:18:33 +0800948static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700949{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
953 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100954
Li Zefan2739d3c2013-01-21 18:18:33 +0800955 /*
956 * If we're doing cleanup due to failure of cgroup_create(),
957 * the corresponding @cfe may not exist.
958 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959 list_for_each_entry(cfe, &cgrp->files, node) {
960 struct dentry *d = cfe->dentry;
961
962 if (cft && cfe->type != cft)
963 continue;
964
965 dget(d);
966 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700967 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968 list_del_init(&cfe->node);
969 dput(d);
970
Li Zefan2739d3c2013-01-21 18:18:33 +0800971 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700973}
974
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975/**
976 * cgroup_clear_directory - selective removal of base and subsystem files
977 * @dir: directory containing the files
978 * @base_files: true if the base files should be removed
979 * @subsys_mask: mask of the subsystem ids whose files should be removed
980 */
981static void cgroup_clear_directory(struct dentry *dir, bool base_files,
982 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700983{
984 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400985 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700986
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400987 for_each_subsys(cgrp->root, ss) {
988 struct cftype_set *set;
989 if (!test_bit(ss->subsys_id, &subsys_mask))
990 continue;
991 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800992 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400993 }
994 if (base_files) {
995 while (!list_empty(&cgrp->files))
996 cgroup_rm_file(cgrp, NULL);
997 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998}
999
1000/*
1001 * NOTE : the dentry must have been dget()'ed
1002 */
1003static void cgroup_d_remove_dir(struct dentry *dentry)
1004{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001005 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001006 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001007
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001008 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001010 parent = dentry->d_parent;
1011 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001012 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001014 spin_unlock(&dentry->d_lock);
1015 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 remove_dir(dentry);
1017}
1018
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001019/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001020 * Call with cgroup_mutex held. Drops reference counts on modules, including
1021 * any duplicate ones that parse_cgroupfs_options took. If this function
1022 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001023 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001025 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001027 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001028 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 int i;
1030
Ben Blumaae8aab2010-03-10 15:22:07 -08001031 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001032 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001033
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001034 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1035 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* Check that any added subsystems are currently free */
1037 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001038 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001040 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001042 /*
1043 * Nobody should tell us to do a subsys that doesn't exist:
1044 * parse_cgroupfs_options should catch that case and refcounts
1045 * ensure that subsystems won't disappear once selected.
1046 */
1047 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 if (ss->root != &rootnode) {
1049 /* Subsystem isn't free */
1050 return -EBUSY;
1051 }
1052 }
1053
1054 /* Currently we don't handle adding/removing subsystems when
1055 * any child cgroups exist. This is theoretically supportable
1056 * but involves complex error handling, so it's being left until
1057 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001058 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 return -EBUSY;
1060
1061 /* Process each subsystem */
1062 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1063 struct cgroup_subsys *ss = subsys[i];
1064 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001065 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001067 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001068 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 BUG_ON(!dummytop->subsys[i]);
1070 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 cgrp->subsys[i] = dummytop->subsys[i];
1072 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001073 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001074 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001076 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001077 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001078 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001080 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001081 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1082 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001084 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001086 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001087 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001088 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001089 /* subsystem is now free - drop reference on module */
1090 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001091 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001093 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001094 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001095 /*
1096 * a refcount was taken, but we already had one, so
1097 * drop the extra reference.
1098 */
1099 module_put(ss->module);
1100#ifdef CONFIG_MODULE_UNLOAD
1101 BUG_ON(ss->module && !module_refcount(ss->module));
1102#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 } else {
1104 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001105 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 }
1107 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001108 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109
1110 return 0;
1111}
1112
Al Viro34c80b12011-12-08 21:32:45 -05001113static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114{
Al Viro34c80b12011-12-08 21:32:45 -05001115 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116 struct cgroup_subsys *ss;
1117
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001118 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119 for_each_subsys(root, ss)
1120 seq_printf(seq, ",%s", ss->name);
1121 if (test_bit(ROOT_NOPREFIX, &root->flags))
1122 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001123 if (test_bit(ROOT_XATTR, &root->flags))
1124 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001125 if (strlen(root->release_agent_path))
1126 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001127 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001128 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001129 if (strlen(root->name))
1130 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001131 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132 return 0;
1133}
1134
1135struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001136 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001138 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001139 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001140 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001141 /* User explicitly requested empty subsystem */
1142 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001143
1144 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001145
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146};
1147
Ben Blumaae8aab2010-03-10 15:22:07 -08001148/*
1149 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001150 * with cgroup_mutex held to protect the subsys[] array. This function takes
1151 * refcounts on subsystems to be used, unless it returns error, in which case
1152 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001153 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001154static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001156 char *token, *o = data;
1157 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001158 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001159 int i;
1160 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001161
Ben Blumaae8aab2010-03-10 15:22:07 -08001162 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1163
Li Zefanf9ab5b52009-06-17 16:26:33 -07001164#ifdef CONFIG_CPUSETS
1165 mask = ~(1UL << cpuset_subsys_id);
1166#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001167
Paul Menagec6d57f32009-09-23 15:56:19 -07001168 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001169
1170 while ((token = strsep(&o, ",")) != NULL) {
1171 if (!*token)
1172 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001174 /* Explicitly have no subsystems */
1175 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 continue;
1177 }
1178 if (!strcmp(token, "all")) {
1179 /* Mutually exclusive option 'all' + subsystem name */
1180 if (one_ss)
1181 return -EINVAL;
1182 all_ss = true;
1183 continue;
1184 }
1185 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001186 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001187 continue;
1188 }
1189 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001190 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191 continue;
1192 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001193 if (!strcmp(token, "xattr")) {
1194 set_bit(ROOT_XATTR, &opts->flags);
1195 continue;
1196 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001198 /* Specifying two release agents is forbidden */
1199 if (opts->release_agent)
1200 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001201 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001202 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001203 if (!opts->release_agent)
1204 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205 continue;
1206 }
1207 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001208 const char *name = token + 5;
1209 /* Can't specify an empty name */
1210 if (!strlen(name))
1211 return -EINVAL;
1212 /* Must match [\w.-]+ */
1213 for (i = 0; i < strlen(name); i++) {
1214 char c = name[i];
1215 if (isalnum(c))
1216 continue;
1217 if ((c == '.') || (c == '-') || (c == '_'))
1218 continue;
1219 return -EINVAL;
1220 }
1221 /* Specifying two names is forbidden */
1222 if (opts->name)
1223 return -EINVAL;
1224 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001225 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001226 GFP_KERNEL);
1227 if (!opts->name)
1228 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001229
1230 continue;
1231 }
1232
1233 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1234 struct cgroup_subsys *ss = subsys[i];
1235 if (ss == NULL)
1236 continue;
1237 if (strcmp(token, ss->name))
1238 continue;
1239 if (ss->disabled)
1240 continue;
1241
1242 /* Mutually exclusive option 'all' + subsystem name */
1243 if (all_ss)
1244 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001245 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001246 one_ss = true;
1247
1248 break;
1249 }
1250 if (i == CGROUP_SUBSYS_COUNT)
1251 return -ENOENT;
1252 }
1253
1254 /*
1255 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001256 * otherwise if 'none', 'name=' and a subsystem name options
1257 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001258 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001259 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001260 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1261 struct cgroup_subsys *ss = subsys[i];
1262 if (ss == NULL)
1263 continue;
1264 if (ss->disabled)
1265 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001266 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001267 }
1268 }
1269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270 /* Consistency checks */
1271
Li Zefanf9ab5b52009-06-17 16:26:33 -07001272 /*
1273 * Option noprefix was introduced just for backward compatibility
1274 * with the old cpuset, so we allow noprefix only if mounting just
1275 * the cpuset subsystem.
1276 */
1277 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001278 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001279 return -EINVAL;
1280
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001281
1282 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001283 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001284 return -EINVAL;
1285
1286 /*
1287 * We either have to specify by name or by subsystems. (So all
1288 * empty hierarchies must have a name).
1289 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001290 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291 return -EINVAL;
1292
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 /*
1294 * Grab references on all the modules we'll need, so the subsystems
1295 * don't dance around before rebind_subsystems attaches them. This may
1296 * take duplicate reference counts on a subsystem that's already used,
1297 * but rebind_subsystems handles this case.
1298 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001299 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001300 unsigned long bit = 1UL << i;
1301
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001302 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001303 continue;
1304 if (!try_module_get(subsys[i]->module)) {
1305 module_pin_failed = true;
1306 break;
1307 }
1308 }
1309 if (module_pin_failed) {
1310 /*
1311 * oops, one of the modules was going away. this means that we
1312 * raced with a module_delete call, and to the user this is
1313 * essentially a "subsystem doesn't exist" case.
1314 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001315 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001316 /* drop refcounts only on the ones we took */
1317 unsigned long bit = 1UL << i;
1318
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001319 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 continue;
1321 module_put(subsys[i]->module);
1322 }
1323 return -ENOENT;
1324 }
1325
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 return 0;
1327}
1328
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001329static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001330{
1331 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001332 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001333 unsigned long bit = 1UL << i;
1334
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001336 continue;
1337 module_put(subsys[i]->module);
1338 }
1339}
1340
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1342{
1343 int ret = 0;
1344 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001345 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001347 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348
Paul Menagebd89aab2007-10-18 23:40:44 -07001349 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001351 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352
1353 /* See what subsystems are wanted */
1354 ret = parse_cgroupfs_options(data, &opts);
1355 if (ret)
1356 goto out_unlock;
1357
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001358 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001359 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1360 task_tgid_nr(current), current->comm);
1361
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 added_mask = opts.subsys_mask & ~root->subsys_mask;
1363 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001364
Ben Blumcf5d5942010-03-10 15:22:09 -08001365 /* Don't allow flags or name to change at remount */
1366 if (opts.flags != root->flags ||
1367 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001369 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001370 goto out_unlock;
1371 }
1372
Gao feng7083d032012-12-03 09:28:18 +08001373 /*
1374 * Clear out the files of subsystems that should be removed, do
1375 * this before rebind_subsystems, since rebind_subsystems may
1376 * change this hierarchy's subsys_list.
1377 */
1378 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1379
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001380 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001381 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001382 /* rebind_subsystems failed, re-populate the removed files */
1383 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001384 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001385 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001386 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001388 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001389 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390
Paul Menage81a6a5c2007-10-18 23:39:38 -07001391 if (opts.release_agent)
1392 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001394 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001395 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001396 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001398 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399 return ret;
1400}
1401
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001402static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 .statfs = simple_statfs,
1404 .drop_inode = generic_delete_inode,
1405 .show_options = cgroup_show_options,
1406 .remount_fs = cgroup_remount,
1407};
1408
Paul Menagecc31edc2008-10-18 20:28:04 -07001409static void init_cgroup_housekeeping(struct cgroup *cgrp)
1410{
1411 INIT_LIST_HEAD(&cgrp->sibling);
1412 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001413 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001415 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001417 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001418 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001419 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001420 INIT_LIST_HEAD(&cgrp->event_list);
1421 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001422 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001423}
Paul Menagec6d57f32009-09-23 15:56:19 -07001424
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425static void init_cgroup_root(struct cgroupfs_root *root)
1426{
Paul Menagebd89aab2007-10-18 23:40:44 -07001427 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 INIT_LIST_HEAD(&root->subsys_list);
1430 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001431 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001433 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001434 cgrp->name = &root_cgroup_name;
Paul Menagebd89aab2007-10-18 23:40:44 -07001435 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001436 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001437 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438}
1439
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440static bool init_root_id(struct cgroupfs_root *root)
1441{
1442 int ret = 0;
1443
1444 do {
1445 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1446 return false;
1447 spin_lock(&hierarchy_id_lock);
1448 /* Try to allocate the next unused ID */
1449 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1450 &root->hierarchy_id);
1451 if (ret == -ENOSPC)
1452 /* Try again starting from 0 */
1453 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1454 if (!ret) {
1455 next_hierarchy_id = root->hierarchy_id + 1;
1456 } else if (ret != -EAGAIN) {
1457 /* Can only get here if the 31-bit IDR is full ... */
1458 BUG_ON(ret);
1459 }
1460 spin_unlock(&hierarchy_id_lock);
1461 } while (ret);
1462 return true;
1463}
1464
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465static int cgroup_test_super(struct super_block *sb, void *data)
1466{
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001468 struct cgroupfs_root *root = sb->s_fs_info;
1469
Paul Menagec6d57f32009-09-23 15:56:19 -07001470 /* If we asked for a name then it must match */
1471 if (opts->name && strcmp(opts->name, root->name))
1472 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001474 /*
1475 * If we asked for subsystems (or explicitly for no
1476 * subsystems) then they must match
1477 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001478 if ((opts->subsys_mask || opts->none)
1479 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480 return 0;
1481
1482 return 1;
1483}
1484
Paul Menagec6d57f32009-09-23 15:56:19 -07001485static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1486{
1487 struct cgroupfs_root *root;
1488
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001489 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 return NULL;
1491
1492 root = kzalloc(sizeof(*root), GFP_KERNEL);
1493 if (!root)
1494 return ERR_PTR(-ENOMEM);
1495
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001496 if (!init_root_id(root)) {
1497 kfree(root);
1498 return ERR_PTR(-ENOMEM);
1499 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001501
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001502 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001503 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001504 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 if (opts->release_agent)
1506 strcpy(root->release_agent_path, opts->release_agent);
1507 if (opts->name)
1508 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001509 if (opts->cpuset_clone_children)
1510 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001511 return root;
1512}
1513
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001514static void cgroup_drop_root(struct cgroupfs_root *root)
1515{
1516 if (!root)
1517 return;
1518
1519 BUG_ON(!root->hierarchy_id);
1520 spin_lock(&hierarchy_id_lock);
1521 ida_remove(&hierarchy_ida, root->hierarchy_id);
1522 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001523 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001524 kfree(root);
1525}
1526
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527static int cgroup_set_super(struct super_block *sb, void *data)
1528{
1529 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001530 struct cgroup_sb_opts *opts = data;
1531
1532 /* If we don't have a new root, we can't set up a new sb */
1533 if (!opts->new_root)
1534 return -EINVAL;
1535
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001536 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
1538 ret = set_anon_super(sb, NULL);
1539 if (ret)
1540 return ret;
1541
Paul Menagec6d57f32009-09-23 15:56:19 -07001542 sb->s_fs_info = opts->new_root;
1543 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
1545 sb->s_blocksize = PAGE_CACHE_SIZE;
1546 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1547 sb->s_magic = CGROUP_SUPER_MAGIC;
1548 sb->s_op = &cgroup_ops;
1549
1550 return 0;
1551}
1552
1553static int cgroup_get_rootdir(struct super_block *sb)
1554{
Al Viro0df6a632010-12-21 13:29:29 -05001555 static const struct dentry_operations cgroup_dops = {
1556 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001557 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001558 };
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 struct inode *inode =
1561 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562
1563 if (!inode)
1564 return -ENOMEM;
1565
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 inode->i_fop = &simple_dir_operations;
1567 inode->i_op = &cgroup_dir_inode_operations;
1568 /* directories start off with i_nlink == 2 (for "." entry) */
1569 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001570 sb->s_root = d_make_root(inode);
1571 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001573 /* for everything else we want ->d_op set */
1574 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 return 0;
1576}
1577
Al Virof7e83572010-07-26 13:23:11 +04001578static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001580 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581{
1582 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 int ret = 0;
1585 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001587 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
1589 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001590 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001592 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 if (ret)
1594 goto out_err;
1595
1596 /*
1597 * Allocate a new cgroup root. We may not need it if we're
1598 * reusing an existing hierarchy.
1599 */
1600 new_root = cgroup_root_from_opts(&opts);
1601 if (IS_ERR(new_root)) {
1602 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001603 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001604 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001608 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001611 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001612 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613 }
1614
Paul Menagec6d57f32009-09-23 15:56:19 -07001615 root = sb->s_fs_info;
1616 BUG_ON(!root);
1617 if (root == opts.new_root) {
1618 /* We used the new root structure, so this is a new hierarchy */
1619 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001620 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001621 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001622 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001623 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001624 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
1626 BUG_ON(sb->s_root != NULL);
1627
1628 ret = cgroup_get_rootdir(sb);
1629 if (ret)
1630 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001631 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
Paul Menage817929e2007-10-18 23:39:36 -07001633 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001637 /* Check for name clashes with existing mounts */
1638 ret = -EBUSY;
1639 if (strlen(root->name))
1640 for_each_active_root(existing_root)
1641 if (!strcmp(existing_root->name, root->name))
1642 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001643
Paul Menage817929e2007-10-18 23:39:36 -07001644 /*
1645 * We're accessing css_set_count without locking
1646 * css_set_lock here, but that's OK - it can only be
1647 * increased by someone holding cgroup_lock, and
1648 * that's us. The worst that can happen is that we
1649 * have some link structures left over
1650 */
1651 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001652 if (ret)
1653 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001654
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001655 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001657 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001658 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001660 /*
1661 * There must be no failure case after here, since rebinding
1662 * takes care of subsystems' refcounts, which are explicitly
1663 * dropped in the failure exit path.
1664 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665
1666 /* EBUSY should be the only error here */
1667 BUG_ON(ret);
1668
1669 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001670 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671
Li Zefanc12f65d2009-01-07 18:07:42 -08001672 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 root->top_cgroup.dentry = sb->s_root;
1674
Paul Menage817929e2007-10-18 23:39:36 -07001675 /* Link the top cgroup in this hierarchy into all
1676 * the css_set objects */
1677 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001678 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001679 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001680 write_unlock(&css_set_lock);
1681
1682 free_cg_links(&tmp_cg_links);
1683
Li Zefanc12f65d2009-01-07 18:07:42 -08001684 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 BUG_ON(root->number_of_cgroups != 1);
1686
eparis@redhat2ce97382011-06-02 21:20:51 +10001687 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001688 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001689 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001690 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001692 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001693 } else {
1694 /*
1695 * We re-used an existing hierarchy - the new root (if
1696 * any) is not needed
1697 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001698 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001699 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001700 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 }
1702
Paul Menagec6d57f32009-09-23 15:56:19 -07001703 kfree(opts.release_agent);
1704 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001705 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 unlock_drop:
1708 mutex_unlock(&cgroup_root_mutex);
1709 mutex_unlock(&cgroup_mutex);
1710 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001712 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001713 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001714 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 out_err:
1716 kfree(opts.release_agent);
1717 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001718 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719}
1720
1721static void cgroup_kill_sb(struct super_block *sb) {
1722 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001725 struct cg_cgroup_link *link;
1726 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 BUG_ON(!root);
1729
1730 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001734 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
1736 /* Rebind all subsystems back to the default hierarchy */
1737 ret = rebind_subsystems(root, 0);
1738 /* Shouldn't be able to fail ... */
1739 BUG_ON(ret);
1740
Paul Menage817929e2007-10-18 23:39:36 -07001741 /*
1742 * Release all the links from css_sets to this hierarchy's
1743 * root cgroup
1744 */
1745 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001746
1747 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1748 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001749 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001750 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001751 kfree(link);
1752 }
1753 write_unlock(&css_set_lock);
1754
Paul Menage839ec542009-01-29 14:25:22 -08001755 if (!list_empty(&root->root_list)) {
1756 list_del(&root->root_list);
1757 root_count--;
1758 }
Li Zefane5f6a862009-01-07 18:07:41 -08001759
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001760 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 mutex_unlock(&cgroup_mutex);
1762
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001763 simple_xattrs_free(&cgrp->xattrs);
1764
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001766 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767}
1768
1769static struct file_system_type cgroup_fs_type = {
1770 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001771 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 .kill_sb = cgroup_kill_sb,
1773};
1774
Greg KH676db4a2010-08-05 13:53:35 -07001775static struct kobject *cgroup_kobj;
1776
Li Zefana043e3b2008-02-23 15:24:09 -08001777/**
1778 * cgroup_path - generate the path of a cgroup
1779 * @cgrp: the cgroup in question
1780 * @buf: the buffer to write the path into
1781 * @buflen: the length of the buffer
1782 *
Li Zefan65dff752013-03-01 15:01:56 +08001783 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1784 *
1785 * We can't generate cgroup path using dentry->d_name, as accessing
1786 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1787 * inode's i_mutex, while on the other hand cgroup_path() can be called
1788 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001790int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791{
Li Zefan65dff752013-03-01 15:01:56 +08001792 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001794
Tao Ma316eb662012-11-08 21:36:38 +08001795 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001796 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001797
Li Zefan65dff752013-03-01 15:01:56 +08001798 rcu_read_lock();
1799 while (cgrp) {
1800 const char *name = cgroup_name(cgrp);
1801 int len;
1802
1803 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001805 goto out;
1806 memcpy(start, name, len);
1807
1808 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001810
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001812 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001814
1815 cgrp = cgrp->parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 }
Li Zefan65dff752013-03-01 15:01:56 +08001817 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001819out:
1820 rcu_read_unlock();
1821 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822}
Ben Blum67523c42010-03-10 15:22:11 -08001823EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824
Ben Blum74a11662011-05-26 16:25:20 -07001825/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001826 * Control Group taskset
1827 */
Tejun Heo134d3372011-12-12 18:12:21 -08001828struct task_and_cgroup {
1829 struct task_struct *task;
1830 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001831 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001832};
1833
Tejun Heo2f7ee562011-12-12 18:12:21 -08001834struct cgroup_taskset {
1835 struct task_and_cgroup single;
1836 struct flex_array *tc_array;
1837 int tc_array_len;
1838 int idx;
1839 struct cgroup *cur_cgrp;
1840};
1841
1842/**
1843 * cgroup_taskset_first - reset taskset and return the first task
1844 * @tset: taskset of interest
1845 *
1846 * @tset iteration is initialized and the first task is returned.
1847 */
1848struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1849{
1850 if (tset->tc_array) {
1851 tset->idx = 0;
1852 return cgroup_taskset_next(tset);
1853 } else {
1854 tset->cur_cgrp = tset->single.cgrp;
1855 return tset->single.task;
1856 }
1857}
1858EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1859
1860/**
1861 * cgroup_taskset_next - iterate to the next task in taskset
1862 * @tset: taskset of interest
1863 *
1864 * Return the next task in @tset. Iteration must have been initialized
1865 * with cgroup_taskset_first().
1866 */
1867struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1868{
1869 struct task_and_cgroup *tc;
1870
1871 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1872 return NULL;
1873
1874 tc = flex_array_get(tset->tc_array, tset->idx++);
1875 tset->cur_cgrp = tc->cgrp;
1876 return tc->task;
1877}
1878EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1879
1880/**
1881 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1882 * @tset: taskset of interest
1883 *
1884 * Return the cgroup for the current (last returned) task of @tset. This
1885 * function must be preceded by either cgroup_taskset_first() or
1886 * cgroup_taskset_next().
1887 */
1888struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1889{
1890 return tset->cur_cgrp;
1891}
1892EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1893
1894/**
1895 * cgroup_taskset_size - return the number of tasks in taskset
1896 * @tset: taskset of interest
1897 */
1898int cgroup_taskset_size(struct cgroup_taskset *tset)
1899{
1900 return tset->tc_array ? tset->tc_array_len : 1;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1903
1904
Ben Blum74a11662011-05-26 16:25:20 -07001905/*
1906 * cgroup_task_migrate - move a task from one cgroup to another.
1907 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001908 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001909 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001910static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001911 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001912{
1913 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001914
1915 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001916 * We are synchronized through threadgroup_lock() against PF_EXITING
1917 * setting such that we can't race against cgroup_exit() changing the
1918 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001919 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001920 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001921 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001922
Ben Blum74a11662011-05-26 16:25:20 -07001923 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001924 rcu_assign_pointer(tsk->cgroups, newcg);
1925 task_unlock(tsk);
1926
1927 /* Update the css_set linked lists if we're using them */
1928 write_lock(&css_set_lock);
1929 if (!list_empty(&tsk->cg_list))
1930 list_move(&tsk->cg_list, &newcg->tasks);
1931 write_unlock(&css_set_lock);
1932
1933 /*
1934 * We just gained a reference on oldcg by taking it from the task. As
1935 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1936 * it here; it will be freed under RCU.
1937 */
Ben Blum74a11662011-05-26 16:25:20 -07001938 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001939 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001940}
1941
Li Zefana043e3b2008-02-23 15:24:09 -08001942/**
Li Zefan081aa452013-03-13 09:17:09 +08001943 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001944 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001945 * @tsk: the task or the leader of the threadgroup to be attached
1946 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001947 *
Tejun Heo257058a2011-12-12 18:12:21 -08001948 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001949 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001951static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1952 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001953{
1954 int retval, i, group_size;
1955 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001956 struct cgroupfs_root *root = cgrp->root;
1957 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001958 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001959 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001960 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001961 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001962
1963 /*
1964 * step 0: in order to do expensive, possibly blocking operations for
1965 * every thread, we cannot iterate the thread group list, since it needs
1966 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001967 * group - group_rwsem prevents new threads from appearing, and if
1968 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001969 */
Li Zefan081aa452013-03-13 09:17:09 +08001970 if (threadgroup)
1971 group_size = get_nr_threads(tsk);
1972 else
1973 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001974 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001975 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001976 if (!group)
1977 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001978 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001979 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001980 if (retval)
1981 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001982
Ben Blum74a11662011-05-26 16:25:20 -07001983 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001984 /*
1985 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1986 * already PF_EXITING could be freed from underneath us unless we
1987 * take an rcu_read_lock.
1988 */
1989 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001990 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001991 struct task_and_cgroup ent;
1992
Tejun Heocd3d0952011-12-12 18:12:21 -08001993 /* @tsk either already exited or can't exit until the end */
1994 if (tsk->flags & PF_EXITING)
1995 continue;
1996
Ben Blum74a11662011-05-26 16:25:20 -07001997 /* as per above, nr_threads may decrease, but not increase. */
1998 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001999 ent.task = tsk;
2000 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002001 /* nothing to do if this task is already in the cgroup */
2002 if (ent.cgrp == cgrp)
2003 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002004 /*
2005 * saying GFP_ATOMIC has no effect here because we did prealloc
2006 * earlier, but it's good form to communicate our expectations.
2007 */
Tejun Heo134d3372011-12-12 18:12:21 -08002008 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002009 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002010 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002011
2012 if (!threadgroup)
2013 break;
Ben Blum74a11662011-05-26 16:25:20 -07002014 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002015 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002016 /* remember the number of threads in the array for later. */
2017 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002018 tset.tc_array = group;
2019 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002020
Tejun Heo134d3372011-12-12 18:12:21 -08002021 /* methods shouldn't be called if no task is actually migrating */
2022 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002023 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002024 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002025
Ben Blum74a11662011-05-26 16:25:20 -07002026 /*
2027 * step 1: check that we can legitimately attach to the cgroup.
2028 */
2029 for_each_subsys(root, ss) {
2030 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002031 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002032 if (retval) {
2033 failed_ss = ss;
2034 goto out_cancel_attach;
2035 }
2036 }
Ben Blum74a11662011-05-26 16:25:20 -07002037 }
2038
2039 /*
2040 * step 2: make sure css_sets exist for all threads to be migrated.
2041 * we use find_css_set, which allocates a new one if necessary.
2042 */
Ben Blum74a11662011-05-26 16:25:20 -07002043 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002044 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002045 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2046 if (!tc->cg) {
2047 retval = -ENOMEM;
2048 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002049 }
2050 }
2051
2052 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002053 * step 3: now that we're guaranteed success wrt the css_sets,
2054 * proceed to move all tasks to the new cgroup. There are no
2055 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002056 */
Ben Blum74a11662011-05-26 16:25:20 -07002057 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002058 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002059 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
2061 /* nothing is sensitive to fork() after this point. */
2062
2063 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002064 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002065 */
2066 for_each_subsys(root, ss) {
2067 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002068 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002069 }
2070
2071 /*
2072 * step 5: success! and cleanup
2073 */
Ben Blum74a11662011-05-26 16:25:20 -07002074 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002075out_put_css_set_refs:
2076 if (retval) {
2077 for (i = 0; i < group_size; i++) {
2078 tc = flex_array_get(group, i);
2079 if (!tc->cg)
2080 break;
2081 put_css_set(tc->cg);
2082 }
Ben Blum74a11662011-05-26 16:25:20 -07002083 }
2084out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002085 if (retval) {
2086 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002087 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002088 break;
Ben Blum74a11662011-05-26 16:25:20 -07002089 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002090 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002091 }
2092 }
Ben Blum74a11662011-05-26 16:25:20 -07002093out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002094 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002095 return retval;
2096}
2097
2098/*
2099 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002100 * function to attach either it or all tasks in its threadgroup. Will lock
2101 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
2103static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002105 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002106 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107 int ret;
2108
Ben Blum74a11662011-05-26 16:25:20 -07002109 if (!cgroup_lock_live_group(cgrp))
2110 return -ENODEV;
2111
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002112retry_find_task:
2113 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002114 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002115 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002116 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002118 ret= -ESRCH;
2119 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 }
Ben Blum74a11662011-05-26 16:25:20 -07002121 /*
2122 * even if we're attaching all tasks in the thread group, we
2123 * only need to check permissions on one of them.
2124 */
David Howellsc69e8d92008-11-14 10:39:19 +11002125 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002126 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2127 !uid_eq(cred->euid, tcred->uid) &&
2128 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002129 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002130 ret = -EACCES;
2131 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 } else
2134 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002135
2136 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002137 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002138
2139 /*
2140 * Workqueue threads may acquire PF_THREAD_BOUND and become
2141 * trapped in a cpuset, or RT worker may be born in a cgroup
2142 * with no rt_runtime allocated. Just say no.
2143 */
2144 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2145 ret = -EINVAL;
2146 rcu_read_unlock();
2147 goto out_unlock_cgroup;
2148 }
2149
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002150 get_task_struct(tsk);
2151 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002152
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 threadgroup_lock(tsk);
2154 if (threadgroup) {
2155 if (!thread_group_leader(tsk)) {
2156 /*
2157 * a race with de_thread from another thread's exec()
2158 * may strip us of our leadership, if this happens,
2159 * there is no choice but to throw this task away and
2160 * try again; this is
2161 * "double-double-toil-and-trouble-check locking".
2162 */
2163 threadgroup_unlock(tsk);
2164 put_task_struct(tsk);
2165 goto retry_find_task;
2166 }
Li Zefan081aa452013-03-13 09:17:09 +08002167 }
2168
2169 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2170
Tejun Heocd3d0952011-12-12 18:12:21 -08002171 threadgroup_unlock(tsk);
2172
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002175 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 return ret;
2177}
2178
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002179/**
2180 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2181 * @from: attach to all cgroups of a given task
2182 * @tsk: the task to be attached
2183 */
2184int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2185{
2186 struct cgroupfs_root *root;
2187 int retval = 0;
2188
Tejun Heo47cfcd02013-04-07 09:29:51 -07002189 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002190 for_each_active_root(root) {
2191 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2192
2193 retval = cgroup_attach_task(from_cg, tsk, false);
2194 if (retval)
2195 break;
2196 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002197 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002198
2199 return retval;
2200}
2201EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2202
Paul Menageaf351022008-07-25 01:47:01 -07002203static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2204{
Ben Blum74a11662011-05-26 16:25:20 -07002205 return attach_task_by_pid(cgrp, pid, false);
2206}
2207
2208static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2209{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002210 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002211}
2212
Paul Menagee788e062008-07-25 01:46:59 -07002213static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2214 const char *buffer)
2215{
2216 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002217 if (strlen(buffer) >= PATH_MAX)
2218 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002219 if (!cgroup_lock_live_group(cgrp))
2220 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002221 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002222 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002223 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002224 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002225 return 0;
2226}
2227
2228static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2229 struct seq_file *seq)
2230{
2231 if (!cgroup_lock_live_group(cgrp))
2232 return -ENODEV;
2233 seq_puts(seq, cgrp->root->release_agent_path);
2234 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002235 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002236 return 0;
2237}
2238
Paul Menage84eea842008-07-25 01:47:00 -07002239/* A buffer size big enough for numbers or short strings */
2240#define CGROUP_LOCAL_BUFFER_SIZE 64
2241
Paul Menagee73d2c62008-04-29 01:00:06 -07002242static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002243 struct file *file,
2244 const char __user *userbuf,
2245 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002246{
Paul Menage84eea842008-07-25 01:47:00 -07002247 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002248 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002249 char *end;
2250
2251 if (!nbytes)
2252 return -EINVAL;
2253 if (nbytes >= sizeof(buffer))
2254 return -E2BIG;
2255 if (copy_from_user(buffer, userbuf, nbytes))
2256 return -EFAULT;
2257
2258 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002259 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002260 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002261 if (*end)
2262 return -EINVAL;
2263 retval = cft->write_u64(cgrp, cft, val);
2264 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002265 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002266 if (*end)
2267 return -EINVAL;
2268 retval = cft->write_s64(cgrp, cft, val);
2269 }
Paul Menage355e0c42007-10-18 23:39:33 -07002270 if (!retval)
2271 retval = nbytes;
2272 return retval;
2273}
2274
Paul Menagedb3b1492008-07-25 01:46:58 -07002275static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2276 struct file *file,
2277 const char __user *userbuf,
2278 size_t nbytes, loff_t *unused_ppos)
2279{
Paul Menage84eea842008-07-25 01:47:00 -07002280 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002281 int retval = 0;
2282 size_t max_bytes = cft->max_write_len;
2283 char *buffer = local_buffer;
2284
2285 if (!max_bytes)
2286 max_bytes = sizeof(local_buffer) - 1;
2287 if (nbytes >= max_bytes)
2288 return -E2BIG;
2289 /* Allocate a dynamic buffer if we need one */
2290 if (nbytes >= sizeof(local_buffer)) {
2291 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2292 if (buffer == NULL)
2293 return -ENOMEM;
2294 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002295 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2296 retval = -EFAULT;
2297 goto out;
2298 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002299
2300 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002301 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002302 if (!retval)
2303 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002304out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002305 if (buffer != local_buffer)
2306 kfree(buffer);
2307 return retval;
2308}
2309
Paul Menageddbcc7e2007-10-18 23:39:30 -07002310static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2311 size_t nbytes, loff_t *ppos)
2312{
2313 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002314 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315
Li Zefan75139b82009-01-07 18:07:33 -08002316 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002317 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002318 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002319 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002320 if (cft->write_u64 || cft->write_s64)
2321 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002322 if (cft->write_string)
2323 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002324 if (cft->trigger) {
2325 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2326 return ret ? ret : nbytes;
2327 }
Paul Menage355e0c42007-10-18 23:39:33 -07002328 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002329}
2330
Paul Menagef4c753b2008-04-29 00:59:56 -07002331static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2332 struct file *file,
2333 char __user *buf, size_t nbytes,
2334 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335{
Paul Menage84eea842008-07-25 01:47:00 -07002336 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002337 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002338 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2339
2340 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2341}
2342
Paul Menagee73d2c62008-04-29 01:00:06 -07002343static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2344 struct file *file,
2345 char __user *buf, size_t nbytes,
2346 loff_t *ppos)
2347{
Paul Menage84eea842008-07-25 01:47:00 -07002348 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002349 s64 val = cft->read_s64(cgrp, cft);
2350 int len = sprintf(tmp, "%lld\n", (long long) val);
2351
2352 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2353}
2354
Paul Menageddbcc7e2007-10-18 23:39:30 -07002355static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2356 size_t nbytes, loff_t *ppos)
2357{
2358 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002359 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360
Li Zefan75139b82009-01-07 18:07:33 -08002361 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362 return -ENODEV;
2363
2364 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002365 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002366 if (cft->read_u64)
2367 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002368 if (cft->read_s64)
2369 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370 return -EINVAL;
2371}
2372
Paul Menage91796562008-04-29 01:00:01 -07002373/*
2374 * seqfile ops/methods for returning structured data. Currently just
2375 * supports string->u64 maps, but can be extended in future.
2376 */
2377
2378struct cgroup_seqfile_state {
2379 struct cftype *cft;
2380 struct cgroup *cgroup;
2381};
2382
2383static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2384{
2385 struct seq_file *sf = cb->state;
2386 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2387}
2388
2389static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2390{
2391 struct cgroup_seqfile_state *state = m->private;
2392 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002393 if (cft->read_map) {
2394 struct cgroup_map_cb cb = {
2395 .fill = cgroup_map_add,
2396 .state = m,
2397 };
2398 return cft->read_map(state->cgroup, cft, &cb);
2399 }
2400 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002401}
2402
Adrian Bunk96930a62008-07-25 19:46:21 -07002403static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002404{
2405 struct seq_file *seq = file->private_data;
2406 kfree(seq->private);
2407 return single_release(inode, file);
2408}
2409
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002410static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002411 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002412 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002413 .llseek = seq_lseek,
2414 .release = cgroup_seqfile_release,
2415};
2416
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417static int cgroup_file_open(struct inode *inode, struct file *file)
2418{
2419 int err;
2420 struct cftype *cft;
2421
2422 err = generic_file_open(inode, file);
2423 if (err)
2424 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002426
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002427 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002428 struct cgroup_seqfile_state *state =
2429 kzalloc(sizeof(*state), GFP_USER);
2430 if (!state)
2431 return -ENOMEM;
2432 state->cft = cft;
2433 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2434 file->f_op = &cgroup_seqfile_operations;
2435 err = single_open(file, cgroup_seqfile_show, state);
2436 if (err < 0)
2437 kfree(state);
2438 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002439 err = cft->open(inode, file);
2440 else
2441 err = 0;
2442
2443 return err;
2444}
2445
2446static int cgroup_file_release(struct inode *inode, struct file *file)
2447{
2448 struct cftype *cft = __d_cft(file->f_dentry);
2449 if (cft->release)
2450 return cft->release(inode, file);
2451 return 0;
2452}
2453
2454/*
2455 * cgroup_rename - Only allow simple rename of directories in place.
2456 */
2457static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2458 struct inode *new_dir, struct dentry *new_dentry)
2459{
Li Zefan65dff752013-03-01 15:01:56 +08002460 int ret;
2461 struct cgroup_name *name, *old_name;
2462 struct cgroup *cgrp;
2463
2464 /*
2465 * It's convinient to use parent dir's i_mutex to protected
2466 * cgrp->name.
2467 */
2468 lockdep_assert_held(&old_dir->i_mutex);
2469
Paul Menageddbcc7e2007-10-18 23:39:30 -07002470 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2471 return -ENOTDIR;
2472 if (new_dentry->d_inode)
2473 return -EEXIST;
2474 if (old_dir != new_dir)
2475 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002476
2477 cgrp = __d_cgrp(old_dentry);
2478
2479 name = cgroup_alloc_name(new_dentry);
2480 if (!name)
2481 return -ENOMEM;
2482
2483 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2484 if (ret) {
2485 kfree(name);
2486 return ret;
2487 }
2488
2489 old_name = cgrp->name;
2490 rcu_assign_pointer(cgrp->name, name);
2491
2492 kfree_rcu(old_name, rcu_head);
2493 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002494}
2495
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002496static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2497{
2498 if (S_ISDIR(dentry->d_inode->i_mode))
2499 return &__d_cgrp(dentry)->xattrs;
2500 else
2501 return &__d_cft(dentry)->xattrs;
2502}
2503
2504static inline int xattr_enabled(struct dentry *dentry)
2505{
2506 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2507 return test_bit(ROOT_XATTR, &root->flags);
2508}
2509
2510static bool is_valid_xattr(const char *name)
2511{
2512 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2513 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2514 return true;
2515 return false;
2516}
2517
2518static int cgroup_setxattr(struct dentry *dentry, const char *name,
2519 const void *val, size_t size, int flags)
2520{
2521 if (!xattr_enabled(dentry))
2522 return -EOPNOTSUPP;
2523 if (!is_valid_xattr(name))
2524 return -EINVAL;
2525 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2526}
2527
2528static int cgroup_removexattr(struct dentry *dentry, const char *name)
2529{
2530 if (!xattr_enabled(dentry))
2531 return -EOPNOTSUPP;
2532 if (!is_valid_xattr(name))
2533 return -EINVAL;
2534 return simple_xattr_remove(__d_xattrs(dentry), name);
2535}
2536
2537static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2538 void *buf, size_t size)
2539{
2540 if (!xattr_enabled(dentry))
2541 return -EOPNOTSUPP;
2542 if (!is_valid_xattr(name))
2543 return -EINVAL;
2544 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2545}
2546
2547static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2548{
2549 if (!xattr_enabled(dentry))
2550 return -EOPNOTSUPP;
2551 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2552}
2553
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002554static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555 .read = cgroup_file_read,
2556 .write = cgroup_file_write,
2557 .llseek = generic_file_llseek,
2558 .open = cgroup_file_open,
2559 .release = cgroup_file_release,
2560};
2561
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002562static const struct inode_operations cgroup_file_inode_operations = {
2563 .setxattr = cgroup_setxattr,
2564 .getxattr = cgroup_getxattr,
2565 .listxattr = cgroup_listxattr,
2566 .removexattr = cgroup_removexattr,
2567};
2568
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002569static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002570 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002571 .mkdir = cgroup_mkdir,
2572 .rmdir = cgroup_rmdir,
2573 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002574 .setxattr = cgroup_setxattr,
2575 .getxattr = cgroup_getxattr,
2576 .listxattr = cgroup_listxattr,
2577 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002578};
2579
Al Viro00cd8dd2012-06-10 17:13:09 -04002580static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002581{
2582 if (dentry->d_name.len > NAME_MAX)
2583 return ERR_PTR(-ENAMETOOLONG);
2584 d_add(dentry, NULL);
2585 return NULL;
2586}
2587
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002588/*
2589 * Check if a file is a control file
2590 */
2591static inline struct cftype *__file_cft(struct file *file)
2592{
Al Viro496ad9a2013-01-23 17:07:38 -05002593 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002594 return ERR_PTR(-EINVAL);
2595 return __d_cft(file->f_dentry);
2596}
2597
Al Viroa5e7ed32011-07-26 01:55:55 -04002598static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002599 struct super_block *sb)
2600{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601 struct inode *inode;
2602
2603 if (!dentry)
2604 return -ENOENT;
2605 if (dentry->d_inode)
2606 return -EEXIST;
2607
2608 inode = cgroup_new_inode(mode, sb);
2609 if (!inode)
2610 return -ENOMEM;
2611
2612 if (S_ISDIR(mode)) {
2613 inode->i_op = &cgroup_dir_inode_operations;
2614 inode->i_fop = &simple_dir_operations;
2615
2616 /* start off with i_nlink == 2 (for "." entry) */
2617 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002618 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619
Tejun Heob8a2df62012-11-19 08:13:37 -08002620 /*
2621 * Control reaches here with cgroup_mutex held.
2622 * @inode->i_mutex should nest outside cgroup_mutex but we
2623 * want to populate it immediately without releasing
2624 * cgroup_mutex. As @inode isn't visible to anyone else
2625 * yet, trylock will always succeed without affecting
2626 * lockdep checks.
2627 */
2628 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002629 } else if (S_ISREG(mode)) {
2630 inode->i_size = 0;
2631 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002632 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002633 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634 d_instantiate(dentry, inode);
2635 dget(dentry); /* Extra count - pin the dentry in core */
2636 return 0;
2637}
2638
Li Zefan099fca32009-04-02 16:57:29 -07002639/**
2640 * cgroup_file_mode - deduce file mode of a control file
2641 * @cft: the control file in question
2642 *
2643 * returns cft->mode if ->mode is not 0
2644 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2645 * returns S_IRUGO if it has only a read handler
2646 * returns S_IWUSR if it has only a write hander
2647 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002648static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002649{
Al Viroa5e7ed32011-07-26 01:55:55 -04002650 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002651
2652 if (cft->mode)
2653 return cft->mode;
2654
2655 if (cft->read || cft->read_u64 || cft->read_s64 ||
2656 cft->read_map || cft->read_seq_string)
2657 mode |= S_IRUGO;
2658
2659 if (cft->write || cft->write_u64 || cft->write_s64 ||
2660 cft->write_string || cft->trigger)
2661 mode |= S_IWUSR;
2662
2663 return mode;
2664}
2665
Tejun Heodb0416b2012-04-01 12:09:55 -07002666static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002667 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002668{
Paul Menagebd89aab2007-10-18 23:40:44 -07002669 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002670 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002672 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002674 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002676
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002677 simple_xattrs_init(&cft->xattrs);
2678
Paul Menagebd89aab2007-10-18 23:40:44 -07002679 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680 strcpy(name, subsys->name);
2681 strcat(name, ".");
2682 }
2683 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002684
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002686
2687 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2688 if (!cfe)
2689 return -ENOMEM;
2690
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002692 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002694 goto out;
2695 }
2696
2697 mode = cgroup_file_mode(cft);
2698 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2699 if (!error) {
2700 cfe->type = (void *)cft;
2701 cfe->dentry = dentry;
2702 dentry->d_fsdata = cfe;
2703 list_add_tail(&cfe->node, &parent->files);
2704 cfe = NULL;
2705 }
2706 dput(dentry);
2707out:
2708 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 return error;
2710}
2711
Tejun Heo79578622012-04-01 12:09:56 -07002712static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002713 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002715 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002716 int err, ret = 0;
2717
2718 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002719 /* does cft->flags tell us to skip this file on @cgrp? */
2720 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2721 continue;
2722 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2723 continue;
2724
Li Zefan2739d3c2013-01-21 18:18:33 +08002725 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002726 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002727 if (err)
2728 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2729 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002730 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002731 } else {
2732 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002733 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002735 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736}
2737
Tejun Heo8e3f6542012-04-01 12:09:55 -07002738static DEFINE_MUTEX(cgroup_cft_mutex);
2739
2740static void cgroup_cfts_prepare(void)
2741 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2742{
2743 /*
2744 * Thanks to the entanglement with vfs inode locking, we can't walk
2745 * the existing cgroups under cgroup_mutex and create files.
2746 * Instead, we increment reference on all cgroups and build list of
2747 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2748 * exclusive access to the field.
2749 */
2750 mutex_lock(&cgroup_cft_mutex);
2751 mutex_lock(&cgroup_mutex);
2752}
2753
2754static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002755 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002756 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2757{
2758 LIST_HEAD(pending);
2759 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002760
2761 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2762 if (cfts && ss->root != &rootnode) {
2763 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2764 dget(cgrp->dentry);
2765 list_add_tail(&cgrp->cft_q_node, &pending);
2766 }
2767 }
2768
2769 mutex_unlock(&cgroup_mutex);
2770
2771 /*
2772 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2773 * files for all cgroups which were created before.
2774 */
2775 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2776 struct inode *inode = cgrp->dentry->d_inode;
2777
2778 mutex_lock(&inode->i_mutex);
2779 mutex_lock(&cgroup_mutex);
2780 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002781 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002782 mutex_unlock(&cgroup_mutex);
2783 mutex_unlock(&inode->i_mutex);
2784
2785 list_del_init(&cgrp->cft_q_node);
2786 dput(cgrp->dentry);
2787 }
2788
2789 mutex_unlock(&cgroup_cft_mutex);
2790}
2791
2792/**
2793 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2794 * @ss: target cgroup subsystem
2795 * @cfts: zero-length name terminated array of cftypes
2796 *
2797 * Register @cfts to @ss. Files described by @cfts are created for all
2798 * existing cgroups to which @ss is attached and all future cgroups will
2799 * have them too. This function can be called anytime whether @ss is
2800 * attached or not.
2801 *
2802 * Returns 0 on successful registration, -errno on failure. Note that this
2803 * function currently returns 0 as long as @cfts registration is successful
2804 * even if some file creation attempts on existing cgroups fail.
2805 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002806int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807{
2808 struct cftype_set *set;
2809
2810 set = kzalloc(sizeof(*set), GFP_KERNEL);
2811 if (!set)
2812 return -ENOMEM;
2813
2814 cgroup_cfts_prepare();
2815 set->cfts = cfts;
2816 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002817 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002818
2819 return 0;
2820}
2821EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2822
Li Zefana043e3b2008-02-23 15:24:09 -08002823/**
Tejun Heo79578622012-04-01 12:09:56 -07002824 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2825 * @ss: target cgroup subsystem
2826 * @cfts: zero-length name terminated array of cftypes
2827 *
2828 * Unregister @cfts from @ss. Files described by @cfts are removed from
2829 * all existing cgroups to which @ss is attached and all future cgroups
2830 * won't have them either. This function can be called anytime whether @ss
2831 * is attached or not.
2832 *
2833 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2834 * registered with @ss.
2835 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002836int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002837{
2838 struct cftype_set *set;
2839
2840 cgroup_cfts_prepare();
2841
2842 list_for_each_entry(set, &ss->cftsets, node) {
2843 if (set->cfts == cfts) {
2844 list_del_init(&set->node);
2845 cgroup_cfts_commit(ss, cfts, false);
2846 return 0;
2847 }
2848 }
2849
2850 cgroup_cfts_commit(ss, NULL, false);
2851 return -ENOENT;
2852}
2853
2854/**
Li Zefana043e3b2008-02-23 15:24:09 -08002855 * cgroup_task_count - count the number of tasks in a cgroup.
2856 * @cgrp: the cgroup in question
2857 *
2858 * Return the number of tasks in the cgroup.
2859 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002860int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002861{
2862 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002863 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002864
Paul Menage817929e2007-10-18 23:39:36 -07002865 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002866 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002867 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002868 }
2869 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002870 return count;
2871}
2872
2873/*
Paul Menage817929e2007-10-18 23:39:36 -07002874 * Advance a list_head iterator. The iterator should be positioned at
2875 * the start of a css_set
2876 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002877static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002878 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002879{
2880 struct list_head *l = it->cg_link;
2881 struct cg_cgroup_link *link;
2882 struct css_set *cg;
2883
2884 /* Advance to the next non-empty css_set */
2885 do {
2886 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002887 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002888 it->cg_link = NULL;
2889 return;
2890 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002891 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002892 cg = link->cg;
2893 } while (list_empty(&cg->tasks));
2894 it->cg_link = l;
2895 it->task = cg->tasks.next;
2896}
2897
Cliff Wickman31a7df02008-02-07 00:14:42 -08002898/*
2899 * To reduce the fork() overhead for systems that are not actually
2900 * using their cgroups capability, we don't maintain the lists running
2901 * through each css_set to its tasks until we see the list actually
2902 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002903 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002904static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002905{
2906 struct task_struct *p, *g;
2907 write_lock(&css_set_lock);
2908 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002909 /*
2910 * We need tasklist_lock because RCU is not safe against
2911 * while_each_thread(). Besides, a forking task that has passed
2912 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2913 * is not guaranteed to have its child immediately visible in the
2914 * tasklist if we walk through it with RCU.
2915 */
2916 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002917 do_each_thread(g, p) {
2918 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002919 /*
2920 * We should check if the process is exiting, otherwise
2921 * it will race with cgroup_exit() in that the list
2922 * entry won't be deleted though the process has exited.
2923 */
2924 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002925 list_add(&p->cg_list, &p->cgroups->tasks);
2926 task_unlock(p);
2927 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002928 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002929 write_unlock(&css_set_lock);
2930}
2931
Tejun Heo574bd9f2012-11-09 09:12:29 -08002932/**
2933 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2934 * @pos: the current position (%NULL to initiate traversal)
2935 * @cgroup: cgroup whose descendants to walk
2936 *
2937 * To be used by cgroup_for_each_descendant_pre(). Find the next
2938 * descendant to visit for pre-order traversal of @cgroup's descendants.
2939 */
2940struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2941 struct cgroup *cgroup)
2942{
2943 struct cgroup *next;
2944
2945 WARN_ON_ONCE(!rcu_read_lock_held());
2946
2947 /* if first iteration, pretend we just visited @cgroup */
2948 if (!pos) {
2949 if (list_empty(&cgroup->children))
2950 return NULL;
2951 pos = cgroup;
2952 }
2953
2954 /* visit the first child if exists */
2955 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2956 if (next)
2957 return next;
2958
2959 /* no child, visit my or the closest ancestor's next sibling */
2960 do {
2961 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2962 sibling);
2963 if (&next->sibling != &pos->parent->children)
2964 return next;
2965
2966 pos = pos->parent;
2967 } while (pos != cgroup);
2968
2969 return NULL;
2970}
2971EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2972
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002973/**
2974 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2975 * @pos: cgroup of interest
2976 *
2977 * Return the rightmost descendant of @pos. If there's no descendant,
2978 * @pos is returned. This can be used during pre-order traversal to skip
2979 * subtree of @pos.
2980 */
2981struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2982{
2983 struct cgroup *last, *tmp;
2984
2985 WARN_ON_ONCE(!rcu_read_lock_held());
2986
2987 do {
2988 last = pos;
2989 /* ->prev isn't RCU safe, walk ->next till the end */
2990 pos = NULL;
2991 list_for_each_entry_rcu(tmp, &last->children, sibling)
2992 pos = tmp;
2993 } while (pos);
2994
2995 return last;
2996}
2997EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
2998
Tejun Heo574bd9f2012-11-09 09:12:29 -08002999static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3000{
3001 struct cgroup *last;
3002
3003 do {
3004 last = pos;
3005 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3006 sibling);
3007 } while (pos);
3008
3009 return last;
3010}
3011
3012/**
3013 * cgroup_next_descendant_post - find the next descendant for post-order walk
3014 * @pos: the current position (%NULL to initiate traversal)
3015 * @cgroup: cgroup whose descendants to walk
3016 *
3017 * To be used by cgroup_for_each_descendant_post(). Find the next
3018 * descendant to visit for post-order traversal of @cgroup's descendants.
3019 */
3020struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3021 struct cgroup *cgroup)
3022{
3023 struct cgroup *next;
3024
3025 WARN_ON_ONCE(!rcu_read_lock_held());
3026
3027 /* if first iteration, visit the leftmost descendant */
3028 if (!pos) {
3029 next = cgroup_leftmost_descendant(cgroup);
3030 return next != cgroup ? next : NULL;
3031 }
3032
3033 /* if there's an unvisited sibling, visit its leftmost descendant */
3034 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3035 if (&next->sibling != &pos->parent->children)
3036 return cgroup_leftmost_descendant(next);
3037
3038 /* no sibling left, visit parent */
3039 next = pos->parent;
3040 return next != cgroup ? next : NULL;
3041}
3042EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3043
Paul Menagebd89aab2007-10-18 23:40:44 -07003044void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003045 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003046{
3047 /*
3048 * The first time anyone tries to iterate across a cgroup,
3049 * we need to enable the list linking each css_set to its
3050 * tasks, and fix up all existing tasks.
3051 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003052 if (!use_task_css_set_links)
3053 cgroup_enable_task_cg_lists();
3054
Paul Menage817929e2007-10-18 23:39:36 -07003055 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003056 it->cg_link = &cgrp->css_sets;
3057 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003058}
3059
Paul Menagebd89aab2007-10-18 23:40:44 -07003060struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003061 struct cgroup_iter *it)
3062{
3063 struct task_struct *res;
3064 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003065 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003066
3067 /* If the iterator cg is NULL, we have no tasks */
3068 if (!it->cg_link)
3069 return NULL;
3070 res = list_entry(l, struct task_struct, cg_list);
3071 /* Advance iterator to find next entry */
3072 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003073 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3074 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003075 /* We reached the end of this task list - move on to
3076 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003077 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003078 } else {
3079 it->task = l;
3080 }
3081 return res;
3082}
3083
Paul Menagebd89aab2007-10-18 23:40:44 -07003084void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003085 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003086{
3087 read_unlock(&css_set_lock);
3088}
3089
Cliff Wickman31a7df02008-02-07 00:14:42 -08003090static inline int started_after_time(struct task_struct *t1,
3091 struct timespec *time,
3092 struct task_struct *t2)
3093{
3094 int start_diff = timespec_compare(&t1->start_time, time);
3095 if (start_diff > 0) {
3096 return 1;
3097 } else if (start_diff < 0) {
3098 return 0;
3099 } else {
3100 /*
3101 * Arbitrarily, if two processes started at the same
3102 * time, we'll say that the lower pointer value
3103 * started first. Note that t2 may have exited by now
3104 * so this may not be a valid pointer any longer, but
3105 * that's fine - it still serves to distinguish
3106 * between two tasks started (effectively) simultaneously.
3107 */
3108 return t1 > t2;
3109 }
3110}
3111
3112/*
3113 * This function is a callback from heap_insert() and is used to order
3114 * the heap.
3115 * In this case we order the heap in descending task start time.
3116 */
3117static inline int started_after(void *p1, void *p2)
3118{
3119 struct task_struct *t1 = p1;
3120 struct task_struct *t2 = p2;
3121 return started_after_time(t1, &t2->start_time, t2);
3122}
3123
3124/**
3125 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3126 * @scan: struct cgroup_scanner containing arguments for the scan
3127 *
3128 * Arguments include pointers to callback functions test_task() and
3129 * process_task().
3130 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3131 * and if it returns true, call process_task() for it also.
3132 * The test_task pointer may be NULL, meaning always true (select all tasks).
3133 * Effectively duplicates cgroup_iter_{start,next,end}()
3134 * but does not lock css_set_lock for the call to process_task().
3135 * The struct cgroup_scanner may be embedded in any structure of the caller's
3136 * creation.
3137 * It is guaranteed that process_task() will act on every task that
3138 * is a member of the cgroup for the duration of this call. This
3139 * function may or may not call process_task() for tasks that exit
3140 * or move to a different cgroup during the call, or are forked or
3141 * move into the cgroup during the call.
3142 *
3143 * Note that test_task() may be called with locks held, and may in some
3144 * situations be called multiple times for the same task, so it should
3145 * be cheap.
3146 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3147 * pre-allocated and will be used for heap operations (and its "gt" member will
3148 * be overwritten), else a temporary heap will be used (allocation of which
3149 * may cause this function to fail).
3150 */
3151int cgroup_scan_tasks(struct cgroup_scanner *scan)
3152{
3153 int retval, i;
3154 struct cgroup_iter it;
3155 struct task_struct *p, *dropped;
3156 /* Never dereference latest_task, since it's not refcounted */
3157 struct task_struct *latest_task = NULL;
3158 struct ptr_heap tmp_heap;
3159 struct ptr_heap *heap;
3160 struct timespec latest_time = { 0, 0 };
3161
3162 if (scan->heap) {
3163 /* The caller supplied our heap and pre-allocated its memory */
3164 heap = scan->heap;
3165 heap->gt = &started_after;
3166 } else {
3167 /* We need to allocate our own heap memory */
3168 heap = &tmp_heap;
3169 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3170 if (retval)
3171 /* cannot allocate the heap */
3172 return retval;
3173 }
3174
3175 again:
3176 /*
3177 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3178 * to determine which are of interest, and using the scanner's
3179 * "process_task" callback to process any of them that need an update.
3180 * Since we don't want to hold any locks during the task updates,
3181 * gather tasks to be processed in a heap structure.
3182 * The heap is sorted by descending task start time.
3183 * If the statically-sized heap fills up, we overflow tasks that
3184 * started later, and in future iterations only consider tasks that
3185 * started after the latest task in the previous pass. This
3186 * guarantees forward progress and that we don't miss any tasks.
3187 */
3188 heap->size = 0;
3189 cgroup_iter_start(scan->cg, &it);
3190 while ((p = cgroup_iter_next(scan->cg, &it))) {
3191 /*
3192 * Only affect tasks that qualify per the caller's callback,
3193 * if he provided one
3194 */
3195 if (scan->test_task && !scan->test_task(p, scan))
3196 continue;
3197 /*
3198 * Only process tasks that started after the last task
3199 * we processed
3200 */
3201 if (!started_after_time(p, &latest_time, latest_task))
3202 continue;
3203 dropped = heap_insert(heap, p);
3204 if (dropped == NULL) {
3205 /*
3206 * The new task was inserted; the heap wasn't
3207 * previously full
3208 */
3209 get_task_struct(p);
3210 } else if (dropped != p) {
3211 /*
3212 * The new task was inserted, and pushed out a
3213 * different task
3214 */
3215 get_task_struct(p);
3216 put_task_struct(dropped);
3217 }
3218 /*
3219 * Else the new task was newer than anything already in
3220 * the heap and wasn't inserted
3221 */
3222 }
3223 cgroup_iter_end(scan->cg, &it);
3224
3225 if (heap->size) {
3226 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003227 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003228 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003229 latest_time = q->start_time;
3230 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003231 }
3232 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003233 scan->process_task(q, scan);
3234 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003235 }
3236 /*
3237 * If we had to process any tasks at all, scan again
3238 * in case some of them were in the middle of forking
3239 * children that didn't get processed.
3240 * Not the most efficient way to do it, but it avoids
3241 * having to take callback_mutex in the fork path
3242 */
3243 goto again;
3244 }
3245 if (heap == &tmp_heap)
3246 heap_free(&tmp_heap);
3247 return 0;
3248}
3249
Tejun Heo8cc99342013-04-07 09:29:50 -07003250static void cgroup_transfer_one_task(struct task_struct *task,
3251 struct cgroup_scanner *scan)
3252{
3253 struct cgroup *new_cgroup = scan->data;
3254
Tejun Heo47cfcd02013-04-07 09:29:51 -07003255 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003256 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003257 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003258}
3259
3260/**
3261 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3262 * @to: cgroup to which the tasks will be moved
3263 * @from: cgroup in which the tasks currently reside
3264 */
3265int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3266{
3267 struct cgroup_scanner scan;
3268
3269 scan.cg = from;
3270 scan.test_task = NULL; /* select all tasks in cgroup */
3271 scan.process_task = cgroup_transfer_one_task;
3272 scan.heap = NULL;
3273 scan.data = to;
3274
3275 return cgroup_scan_tasks(&scan);
3276}
3277
Paul Menage817929e2007-10-18 23:39:36 -07003278/*
Ben Blum102a7752009-09-23 15:56:26 -07003279 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003280 *
3281 * Reading this file can return large amounts of data if a cgroup has
3282 * *lots* of attached tasks. So it may need several calls to read(),
3283 * but we cannot guarantee that the information we produce is correct
3284 * unless we produce it entirely atomically.
3285 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003286 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003287
Li Zefan24528252012-01-20 11:58:43 +08003288/* which pidlist file are we talking about? */
3289enum cgroup_filetype {
3290 CGROUP_FILE_PROCS,
3291 CGROUP_FILE_TASKS,
3292};
3293
3294/*
3295 * A pidlist is a list of pids that virtually represents the contents of one
3296 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3297 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3298 * to the cgroup.
3299 */
3300struct cgroup_pidlist {
3301 /*
3302 * used to find which pidlist is wanted. doesn't change as long as
3303 * this particular list stays in the list.
3304 */
3305 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3306 /* array of xids */
3307 pid_t *list;
3308 /* how many elements the above list has */
3309 int length;
3310 /* how many files are using the current array */
3311 int use_count;
3312 /* each of these stored in a list by its cgroup */
3313 struct list_head links;
3314 /* pointer to the cgroup we belong to, for list removal purposes */
3315 struct cgroup *owner;
3316 /* protects the other fields */
3317 struct rw_semaphore mutex;
3318};
3319
Paul Menagebbcb81d2007-10-18 23:39:32 -07003320/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003321 * The following two functions "fix" the issue where there are more pids
3322 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3323 * TODO: replace with a kernel-wide solution to this problem
3324 */
3325#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3326static void *pidlist_allocate(int count)
3327{
3328 if (PIDLIST_TOO_LARGE(count))
3329 return vmalloc(count * sizeof(pid_t));
3330 else
3331 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3332}
3333static void pidlist_free(void *p)
3334{
3335 if (is_vmalloc_addr(p))
3336 vfree(p);
3337 else
3338 kfree(p);
3339}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003340
3341/*
Ben Blum102a7752009-09-23 15:56:26 -07003342 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003343 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003344 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003345static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003346{
Ben Blum102a7752009-09-23 15:56:26 -07003347 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003348
3349 /*
3350 * we presume the 0th element is unique, so i starts at 1. trivial
3351 * edge cases first; no work needs to be done for either
3352 */
3353 if (length == 0 || length == 1)
3354 return length;
3355 /* src and dest walk down the list; dest counts unique elements */
3356 for (src = 1; src < length; src++) {
3357 /* find next unique element */
3358 while (list[src] == list[src-1]) {
3359 src++;
3360 if (src == length)
3361 goto after;
3362 }
3363 /* dest always points to where the next unique element goes */
3364 list[dest] = list[src];
3365 dest++;
3366 }
3367after:
Ben Blum102a7752009-09-23 15:56:26 -07003368 return dest;
3369}
3370
3371static int cmppid(const void *a, const void *b)
3372{
3373 return *(pid_t *)a - *(pid_t *)b;
3374}
3375
3376/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003377 * find the appropriate pidlist for our purpose (given procs vs tasks)
3378 * returns with the lock on that pidlist already held, and takes care
3379 * of the use count, or returns NULL with no locks held if we're out of
3380 * memory.
3381 */
3382static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3383 enum cgroup_filetype type)
3384{
3385 struct cgroup_pidlist *l;
3386 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003387 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003388
Ben Blum72a8cb32009-09-23 15:56:27 -07003389 /*
3390 * We can't drop the pidlist_mutex before taking the l->mutex in case
3391 * the last ref-holder is trying to remove l from the list at the same
3392 * time. Holding the pidlist_mutex precludes somebody taking whichever
3393 * list we find out from under us - compare release_pid_array().
3394 */
3395 mutex_lock(&cgrp->pidlist_mutex);
3396 list_for_each_entry(l, &cgrp->pidlists, links) {
3397 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003398 /* make sure l doesn't vanish out from under us */
3399 down_write(&l->mutex);
3400 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003401 return l;
3402 }
3403 }
3404 /* entry not found; create a new one */
3405 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3406 if (!l) {
3407 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003408 return l;
3409 }
3410 init_rwsem(&l->mutex);
3411 down_write(&l->mutex);
3412 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003413 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 l->use_count = 0; /* don't increment here */
3415 l->list = NULL;
3416 l->owner = cgrp;
3417 list_add(&l->links, &cgrp->pidlists);
3418 mutex_unlock(&cgrp->pidlist_mutex);
3419 return l;
3420}
3421
3422/*
Ben Blum102a7752009-09-23 15:56:26 -07003423 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3424 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003425static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3426 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003427{
3428 pid_t *array;
3429 int length;
3430 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003431 struct cgroup_iter it;
3432 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003433 struct cgroup_pidlist *l;
3434
3435 /*
3436 * If cgroup gets more users after we read count, we won't have
3437 * enough space - tough. This race is indistinguishable to the
3438 * caller from the case that the additional cgroup users didn't
3439 * show up until sometime later on.
3440 */
3441 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003442 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003443 if (!array)
3444 return -ENOMEM;
3445 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003446 cgroup_iter_start(cgrp, &it);
3447 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003448 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003449 break;
Ben Blum102a7752009-09-23 15:56:26 -07003450 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003451 if (type == CGROUP_FILE_PROCS)
3452 pid = task_tgid_vnr(tsk);
3453 else
3454 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003455 if (pid > 0) /* make sure to only use valid results */
3456 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003457 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003458 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003459 length = n;
3460 /* now sort & (if procs) strip out duplicates */
3461 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003462 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003463 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003464 l = cgroup_pidlist_find(cgrp, type);
3465 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003466 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003467 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003468 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003469 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003470 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003471 l->list = array;
3472 l->length = length;
3473 l->use_count++;
3474 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003475 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003476 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003477}
3478
Balbir Singh846c7bb2007-10-18 23:39:44 -07003479/**
Li Zefana043e3b2008-02-23 15:24:09 -08003480 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003481 * @stats: cgroupstats to fill information into
3482 * @dentry: A dentry entry belonging to the cgroup for which stats have
3483 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003484 *
3485 * Build and fill cgroupstats so that taskstats can export it to user
3486 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003487 */
3488int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3489{
3490 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003491 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003492 struct cgroup_iter it;
3493 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003494
Balbir Singh846c7bb2007-10-18 23:39:44 -07003495 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003496 * Validate dentry by checking the superblock operations,
3497 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003498 */
Li Zefan33d283b2008-11-19 15:36:48 -08003499 if (dentry->d_sb->s_op != &cgroup_ops ||
3500 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003501 goto err;
3502
3503 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003504 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003505
Paul Menagebd89aab2007-10-18 23:40:44 -07003506 cgroup_iter_start(cgrp, &it);
3507 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003508 switch (tsk->state) {
3509 case TASK_RUNNING:
3510 stats->nr_running++;
3511 break;
3512 case TASK_INTERRUPTIBLE:
3513 stats->nr_sleeping++;
3514 break;
3515 case TASK_UNINTERRUPTIBLE:
3516 stats->nr_uninterruptible++;
3517 break;
3518 case TASK_STOPPED:
3519 stats->nr_stopped++;
3520 break;
3521 default:
3522 if (delayacct_is_task_waiting_on_io(tsk))
3523 stats->nr_io_wait++;
3524 break;
3525 }
3526 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003527 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003528
Balbir Singh846c7bb2007-10-18 23:39:44 -07003529err:
3530 return ret;
3531}
3532
Paul Menage8f3ff202009-09-23 15:56:25 -07003533
Paul Menagecc31edc2008-10-18 20:28:04 -07003534/*
Ben Blum102a7752009-09-23 15:56:26 -07003535 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003536 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003537 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003538 */
3539
Ben Blum102a7752009-09-23 15:56:26 -07003540static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003541{
3542 /*
3543 * Initially we receive a position value that corresponds to
3544 * one more than the last pid shown (or 0 on the first call or
3545 * after a seek to the start). Use a binary-search to find the
3546 * next pid to display, if any
3547 */
Ben Blum102a7752009-09-23 15:56:26 -07003548 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003549 int index = 0, pid = *pos;
3550 int *iter;
3551
Ben Blum102a7752009-09-23 15:56:26 -07003552 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003553 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003554 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003555
Paul Menagecc31edc2008-10-18 20:28:04 -07003556 while (index < end) {
3557 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003558 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003559 index = mid;
3560 break;
Ben Blum102a7752009-09-23 15:56:26 -07003561 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003562 index = mid + 1;
3563 else
3564 end = mid;
3565 }
3566 }
3567 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003568 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003569 return NULL;
3570 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003571 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003572 *pos = *iter;
3573 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003574}
3575
Ben Blum102a7752009-09-23 15:56:26 -07003576static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003577{
Ben Blum102a7752009-09-23 15:56:26 -07003578 struct cgroup_pidlist *l = s->private;
3579 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003580}
3581
Ben Blum102a7752009-09-23 15:56:26 -07003582static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003583{
Ben Blum102a7752009-09-23 15:56:26 -07003584 struct cgroup_pidlist *l = s->private;
3585 pid_t *p = v;
3586 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003587 /*
3588 * Advance to the next pid in the array. If this goes off the
3589 * end, we're done
3590 */
3591 p++;
3592 if (p >= end) {
3593 return NULL;
3594 } else {
3595 *pos = *p;
3596 return p;
3597 }
3598}
3599
Ben Blum102a7752009-09-23 15:56:26 -07003600static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003601{
3602 return seq_printf(s, "%d\n", *(int *)v);
3603}
3604
Ben Blum102a7752009-09-23 15:56:26 -07003605/*
3606 * seq_operations functions for iterating on pidlists through seq_file -
3607 * independent of whether it's tasks or procs
3608 */
3609static const struct seq_operations cgroup_pidlist_seq_operations = {
3610 .start = cgroup_pidlist_start,
3611 .stop = cgroup_pidlist_stop,
3612 .next = cgroup_pidlist_next,
3613 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003614};
3615
Ben Blum102a7752009-09-23 15:56:26 -07003616static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003617{
Ben Blum72a8cb32009-09-23 15:56:27 -07003618 /*
3619 * the case where we're the last user of this particular pidlist will
3620 * have us remove it from the cgroup's list, which entails taking the
3621 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3622 * pidlist_mutex, we have to take pidlist_mutex first.
3623 */
3624 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003625 down_write(&l->mutex);
3626 BUG_ON(!l->use_count);
3627 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 /* we're the last user if refcount is 0; remove and free */
3629 list_del(&l->links);
3630 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003631 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003632 put_pid_ns(l->key.ns);
3633 up_write(&l->mutex);
3634 kfree(l);
3635 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003636 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003638 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003639}
3640
Ben Blum102a7752009-09-23 15:56:26 -07003641static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003642{
Ben Blum102a7752009-09-23 15:56:26 -07003643 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003644 if (!(file->f_mode & FMODE_READ))
3645 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003646 /*
3647 * the seq_file will only be initialized if the file was opened for
3648 * reading; hence we check if it's not null only in that case.
3649 */
3650 l = ((struct seq_file *)file->private_data)->private;
3651 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003652 return seq_release(inode, file);
3653}
3654
Ben Blum102a7752009-09-23 15:56:26 -07003655static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003656 .read = seq_read,
3657 .llseek = seq_lseek,
3658 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003659 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003660};
3661
3662/*
Ben Blum102a7752009-09-23 15:56:26 -07003663 * The following functions handle opens on a file that displays a pidlist
3664 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3665 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003666 */
Ben Blum102a7752009-09-23 15:56:26 -07003667/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003668static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003669{
3670 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003671 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003672 int retval;
3673
3674 /* Nothing to do for write-only files */
3675 if (!(file->f_mode & FMODE_READ))
3676 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003677
Ben Blum102a7752009-09-23 15:56:26 -07003678 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003680 if (retval)
3681 return retval;
3682 /* configure file information */
3683 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003684
Ben Blum102a7752009-09-23 15:56:26 -07003685 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003687 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003689 }
Ben Blum102a7752009-09-23 15:56:26 -07003690 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003691 return 0;
3692}
Ben Blum102a7752009-09-23 15:56:26 -07003693static int cgroup_tasks_open(struct inode *unused, struct file *file)
3694{
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003696}
3697static int cgroup_procs_open(struct inode *unused, struct file *file)
3698{
Ben Blum72a8cb32009-09-23 15:56:27 -07003699 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003700}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003701
Paul Menagebd89aab2007-10-18 23:40:44 -07003702static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003703 struct cftype *cft)
3704{
Paul Menagebd89aab2007-10-18 23:40:44 -07003705 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003706}
3707
Paul Menage6379c102008-07-25 01:47:01 -07003708static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3709 struct cftype *cft,
3710 u64 val)
3711{
3712 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3713 if (val)
3714 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3715 else
3716 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3717 return 0;
3718}
3719
Paul Menagebbcb81d2007-10-18 23:39:32 -07003720/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003721 * Unregister event and free resources.
3722 *
3723 * Gets called from workqueue.
3724 */
3725static void cgroup_event_remove(struct work_struct *work)
3726{
3727 struct cgroup_event *event = container_of(work, struct cgroup_event,
3728 remove);
3729 struct cgroup *cgrp = event->cgrp;
3730
Li Zefan810cbee2013-02-18 18:56:14 +08003731 remove_wait_queue(event->wqh, &event->wait);
3732
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003733 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3734
Li Zefan810cbee2013-02-18 18:56:14 +08003735 /* Notify userspace the event is going away. */
3736 eventfd_signal(event->eventfd, 1);
3737
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003738 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003739 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003740 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003741}
3742
3743/*
3744 * Gets called on POLLHUP on eventfd when user closes it.
3745 *
3746 * Called with wqh->lock held and interrupts disabled.
3747 */
3748static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3749 int sync, void *key)
3750{
3751 struct cgroup_event *event = container_of(wait,
3752 struct cgroup_event, wait);
3753 struct cgroup *cgrp = event->cgrp;
3754 unsigned long flags = (unsigned long)key;
3755
3756 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003757 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003758 * If the event has been detached at cgroup removal, we
3759 * can simply return knowing the other side will cleanup
3760 * for us.
3761 *
3762 * We can't race against event freeing since the other
3763 * side will require wqh->lock via remove_wait_queue(),
3764 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003765 */
Li Zefan810cbee2013-02-18 18:56:14 +08003766 spin_lock(&cgrp->event_list_lock);
3767 if (!list_empty(&event->list)) {
3768 list_del_init(&event->list);
3769 /*
3770 * We are in atomic context, but cgroup_event_remove()
3771 * may sleep, so we have to call it in workqueue.
3772 */
3773 schedule_work(&event->remove);
3774 }
3775 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003776 }
3777
3778 return 0;
3779}
3780
3781static void cgroup_event_ptable_queue_proc(struct file *file,
3782 wait_queue_head_t *wqh, poll_table *pt)
3783{
3784 struct cgroup_event *event = container_of(pt,
3785 struct cgroup_event, pt);
3786
3787 event->wqh = wqh;
3788 add_wait_queue(wqh, &event->wait);
3789}
3790
3791/*
3792 * Parse input and register new cgroup event handler.
3793 *
3794 * Input must be in format '<event_fd> <control_fd> <args>'.
3795 * Interpretation of args is defined by control file implementation.
3796 */
3797static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3798 const char *buffer)
3799{
3800 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003801 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003802 unsigned int efd, cfd;
3803 struct file *efile = NULL;
3804 struct file *cfile = NULL;
3805 char *endp;
3806 int ret;
3807
3808 efd = simple_strtoul(buffer, &endp, 10);
3809 if (*endp != ' ')
3810 return -EINVAL;
3811 buffer = endp + 1;
3812
3813 cfd = simple_strtoul(buffer, &endp, 10);
3814 if ((*endp != ' ') && (*endp != '\0'))
3815 return -EINVAL;
3816 buffer = endp + 1;
3817
3818 event = kzalloc(sizeof(*event), GFP_KERNEL);
3819 if (!event)
3820 return -ENOMEM;
3821 event->cgrp = cgrp;
3822 INIT_LIST_HEAD(&event->list);
3823 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3824 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3825 INIT_WORK(&event->remove, cgroup_event_remove);
3826
3827 efile = eventfd_fget(efd);
3828 if (IS_ERR(efile)) {
3829 ret = PTR_ERR(efile);
3830 goto fail;
3831 }
3832
3833 event->eventfd = eventfd_ctx_fileget(efile);
3834 if (IS_ERR(event->eventfd)) {
3835 ret = PTR_ERR(event->eventfd);
3836 goto fail;
3837 }
3838
3839 cfile = fget(cfd);
3840 if (!cfile) {
3841 ret = -EBADF;
3842 goto fail;
3843 }
3844
3845 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003846 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003847 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003848 if (ret < 0)
3849 goto fail;
3850
3851 event->cft = __file_cft(cfile);
3852 if (IS_ERR(event->cft)) {
3853 ret = PTR_ERR(event->cft);
3854 goto fail;
3855 }
3856
Li Zefanf1690072013-02-18 14:13:35 +08003857 /*
3858 * The file to be monitored must be in the same cgroup as
3859 * cgroup.event_control is.
3860 */
3861 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3862 if (cgrp_cfile != cgrp) {
3863 ret = -EINVAL;
3864 goto fail;
3865 }
3866
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003867 if (!event->cft->register_event || !event->cft->unregister_event) {
3868 ret = -EINVAL;
3869 goto fail;
3870 }
3871
3872 ret = event->cft->register_event(cgrp, event->cft,
3873 event->eventfd, buffer);
3874 if (ret)
3875 goto fail;
3876
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003877 /*
3878 * Events should be removed after rmdir of cgroup directory, but before
3879 * destroying subsystem state objects. Let's take reference to cgroup
3880 * directory dentry to do that.
3881 */
3882 dget(cgrp->dentry);
3883
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003884 spin_lock(&cgrp->event_list_lock);
3885 list_add(&event->list, &cgrp->event_list);
3886 spin_unlock(&cgrp->event_list_lock);
3887
3888 fput(cfile);
3889 fput(efile);
3890
3891 return 0;
3892
3893fail:
3894 if (cfile)
3895 fput(cfile);
3896
3897 if (event && event->eventfd && !IS_ERR(event->eventfd))
3898 eventfd_ctx_put(event->eventfd);
3899
3900 if (!IS_ERR_OR_NULL(efile))
3901 fput(efile);
3902
3903 kfree(event);
3904
3905 return ret;
3906}
3907
Daniel Lezcano97978e62010-10-27 15:33:35 -07003908static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3909 struct cftype *cft)
3910{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003911 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003912}
3913
3914static int cgroup_clone_children_write(struct cgroup *cgrp,
3915 struct cftype *cft,
3916 u64 val)
3917{
3918 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003919 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003920 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003921 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003922 return 0;
3923}
3924
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003925/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003926 * for the common functions, 'private' gives the type of file
3927 */
Ben Blum102a7752009-09-23 15:56:26 -07003928/* for hysterical raisins, we can't put this on the older files */
3929#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003930static struct cftype files[] = {
3931 {
3932 .name = "tasks",
3933 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003934 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003935 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003936 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003937 },
Ben Blum102a7752009-09-23 15:56:26 -07003938 {
3939 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3940 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003941 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003942 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003943 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003944 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003945 {
3946 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003947 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003948 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003949 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003950 {
3951 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3952 .write_string = cgroup_write_event_control,
3953 .mode = S_IWUGO,
3954 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003955 {
3956 .name = "cgroup.clone_children",
3957 .read_u64 = cgroup_clone_children_read,
3958 .write_u64 = cgroup_clone_children_write,
3959 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003960 {
3961 .name = "release_agent",
3962 .flags = CFTYPE_ONLY_ON_ROOT,
3963 .read_seq_string = cgroup_release_agent_show,
3964 .write_string = cgroup_release_agent_write,
3965 .max_write_len = PATH_MAX,
3966 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003967 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003968};
3969
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003970/**
3971 * cgroup_populate_dir - selectively creation of files in a directory
3972 * @cgrp: target cgroup
3973 * @base_files: true if the base files should be added
3974 * @subsys_mask: mask of the subsystem ids whose files should be added
3975 */
3976static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3977 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003978{
3979 int err;
3980 struct cgroup_subsys *ss;
3981
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003982 if (base_files) {
3983 err = cgroup_addrm_files(cgrp, NULL, files, true);
3984 if (err < 0)
3985 return err;
3986 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003987
Tejun Heo8e3f6542012-04-01 12:09:55 -07003988 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07003989 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003990 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003991 if (!test_bit(ss->subsys_id, &subsys_mask))
3992 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003993
Tejun Heodb0416b2012-04-01 12:09:55 -07003994 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07003995 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07003996 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07003997
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07003998 /* This cgroup is ready now */
3999 for_each_subsys(cgrp->root, ss) {
4000 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4001 /*
4002 * Update id->css pointer and make this css visible from
4003 * CSS ID functions. This pointer will be dereferened
4004 * from RCU-read-side without locks.
4005 */
4006 if (css->id)
4007 rcu_assign_pointer(css->id->css, css);
4008 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004009
4010 return 0;
4011}
4012
Tejun Heo48ddbe12012-04-01 12:09:56 -07004013static void css_dput_fn(struct work_struct *work)
4014{
4015 struct cgroup_subsys_state *css =
4016 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004017 struct dentry *dentry = css->cgroup->dentry;
4018 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004019
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004020 atomic_inc(&sb->s_active);
4021 dput(dentry);
4022 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004023}
4024
Paul Menageddbcc7e2007-10-18 23:39:30 -07004025static void init_cgroup_css(struct cgroup_subsys_state *css,
4026 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004027 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004028{
Paul Menagebd89aab2007-10-18 23:40:44 -07004029 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004030 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004032 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004033 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004034 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004035 BUG_ON(cgrp->subsys[ss->subsys_id]);
4036 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004037
4038 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004039 * css holds an extra ref to @cgrp->dentry which is put on the last
4040 * css_put(). dput() requires process context, which css_put() may
4041 * be called without. @css->dput_work will be used to invoke
4042 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004043 */
4044 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004045}
4046
Tejun Heob1929db2012-11-19 08:13:38 -08004047/* invoke ->post_create() on a new CSS and mark it online if successful */
4048static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004049{
Tejun Heob1929db2012-11-19 08:13:38 -08004050 int ret = 0;
4051
Tejun Heoa31f2d32012-11-19 08:13:37 -08004052 lockdep_assert_held(&cgroup_mutex);
4053
Tejun Heo92fb9742012-11-19 08:13:38 -08004054 if (ss->css_online)
4055 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004056 if (!ret)
4057 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4058 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004059}
4060
4061/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4062static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4063 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4064{
4065 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4066
4067 lockdep_assert_held(&cgroup_mutex);
4068
4069 if (!(css->flags & CSS_ONLINE))
4070 return;
4071
Li Zefand7eeac12013-03-12 15:35:59 -07004072 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004073 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004074
4075 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4076}
4077
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078/*
Li Zefana043e3b2008-02-23 15:24:09 -08004079 * cgroup_create - create a cgroup
4080 * @parent: cgroup that will be parent of the new cgroup
4081 * @dentry: dentry of the new cgroup
4082 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004083 *
Li Zefana043e3b2008-02-23 15:24:09 -08004084 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004085 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004086static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004087 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004088{
Paul Menagebd89aab2007-10-18 23:40:44 -07004089 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004090 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004091 struct cgroupfs_root *root = parent->root;
4092 int err = 0;
4093 struct cgroup_subsys *ss;
4094 struct super_block *sb = root->sb;
4095
Tejun Heo0a950f62012-11-19 09:02:12 -08004096 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004097 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4098 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099 return -ENOMEM;
4100
Li Zefan65dff752013-03-01 15:01:56 +08004101 name = cgroup_alloc_name(dentry);
4102 if (!name)
4103 goto err_free_cgrp;
4104 rcu_assign_pointer(cgrp->name, name);
4105
Tejun Heo0a950f62012-11-19 09:02:12 -08004106 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4107 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004108 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004109
Tejun Heo976c06b2012-11-05 09:16:59 -08004110 /*
4111 * Only live parents can have children. Note that the liveliness
4112 * check isn't strictly necessary because cgroup_mkdir() and
4113 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4114 * anyway so that locking is contained inside cgroup proper and we
4115 * don't get nasty surprises if we ever grow another caller.
4116 */
4117 if (!cgroup_lock_live_group(parent)) {
4118 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004119 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004120 }
4121
Paul Menageddbcc7e2007-10-18 23:39:30 -07004122 /* Grab a reference on the superblock so the hierarchy doesn't
4123 * get deleted on unmount if there are child cgroups. This
4124 * can be done outside cgroup_mutex, since the sb can't
4125 * disappear while someone has an open control file on the
4126 * fs */
4127 atomic_inc(&sb->s_active);
4128
Paul Menagecc31edc2008-10-18 20:28:04 -07004129 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130
Li Zefanfe1c06c2013-01-24 14:30:22 +08004131 dentry->d_fsdata = cgrp;
4132 cgrp->dentry = dentry;
4133
Paul Menagebd89aab2007-10-18 23:40:44 -07004134 cgrp->parent = parent;
4135 cgrp->root = parent->root;
4136 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137
Li Zefanb6abdb02008-03-04 14:28:19 -08004138 if (notify_on_release(parent))
4139 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4140
Tejun Heo2260e7f2012-11-19 08:13:38 -08004141 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4142 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004143
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004145 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004146
Tejun Heo92fb9742012-11-19 08:13:38 -08004147 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004148 if (IS_ERR(css)) {
4149 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004150 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004152 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004153 if (ss->use_id) {
4154 err = alloc_css_id(ss, parent, cgrp);
4155 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004156 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 }
4159
Tejun Heo4e139af2012-11-19 08:13:36 -08004160 /*
4161 * Create directory. cgroup_create_file() returns with the new
4162 * directory locked on success so that it can be populated without
4163 * dropping cgroup_mutex.
4164 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004165 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004167 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004168 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169
Tejun Heo4e139af2012-11-19 08:13:36 -08004170 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004171 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4172 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4173 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004174
Tejun Heob1929db2012-11-19 08:13:38 -08004175 /* each css holds a ref to the cgroup's dentry */
4176 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004177 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004178
Tejun Heob1929db2012-11-19 08:13:38 -08004179 /* creation succeeded, notify subsystems */
4180 for_each_subsys(root, ss) {
4181 err = online_css(ss, cgrp);
4182 if (err)
4183 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004184
4185 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4186 parent->parent) {
4187 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",
4188 current->comm, current->pid, ss->name);
4189 if (!strcmp(ss->name, "memory"))
4190 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4191 ss->warned_broken_hierarchy = true;
4192 }
Tejun Heoa8638032012-11-09 09:12:29 -08004193 }
4194
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004195 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004196 if (err)
4197 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198
4199 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004200 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201
4202 return 0;
4203
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004204err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004207 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004209 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210 /* Release the reference count that we took on the superblock */
4211 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004212err_free_id:
4213 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004214err_free_name:
4215 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004216err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004217 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004219
4220err_destroy:
4221 cgroup_destroy_locked(cgrp);
4222 mutex_unlock(&cgroup_mutex);
4223 mutex_unlock(&dentry->d_inode->i_mutex);
4224 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225}
4226
Al Viro18bb1db2011-07-26 01:41:39 -04004227static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228{
4229 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4230
4231 /* the vfs holds inode->i_mutex already */
4232 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4233}
4234
Tejun Heo42809dd2012-11-19 08:13:37 -08004235static int cgroup_destroy_locked(struct cgroup *cgrp)
4236 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237{
Tejun Heo42809dd2012-11-19 08:13:37 -08004238 struct dentry *d = cgrp->dentry;
4239 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004240 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004241 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242
Tejun Heo42809dd2012-11-19 08:13:37 -08004243 lockdep_assert_held(&d->d_inode->i_mutex);
4244 lockdep_assert_held(&cgroup_mutex);
4245
4246 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004248
Tejun Heo1a90dd52012-11-05 09:16:59 -08004249 /*
4250 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4251 * removed. This makes future css_tryget() and child creation
4252 * attempts fail thus maintaining the removal conditions verified
4253 * above.
4254 */
Tejun Heoed9577932012-11-05 09:16:58 -08004255 for_each_subsys(cgrp->root, ss) {
4256 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4257
4258 WARN_ON(atomic_read(&css->refcnt) < 0);
4259 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004260 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004261 set_bit(CGRP_REMOVED, &cgrp->flags);
4262
Tejun Heoa31f2d32012-11-19 08:13:37 -08004263 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004264 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004265 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004266
4267 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004268 * Put all the base refs. Each css holds an extra reference to the
4269 * cgroup's dentry and cgroup removal proceeds regardless of css
4270 * refs. On the last put of each css, whenever that may be, the
4271 * extra dentry ref is put so that dentry destruction happens only
4272 * after all css's are released.
4273 */
Tejun Heoe9316082012-11-05 09:16:58 -08004274 for_each_subsys(cgrp->root, ss)
4275 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004277 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004278 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004279 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004280 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004281
Paul Menage999cd8a2009-01-07 18:08:36 -08004282 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004283 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004284 list_del_init(&cgrp->allcg_node);
4285
Tejun Heo42809dd2012-11-19 08:13:37 -08004286 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287 cgroup_d_remove_dir(d);
4288 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Paul Menagebd89aab2007-10-18 23:40:44 -07004290 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004291 check_for_release(parent);
4292
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004293 /*
4294 * Unregister events and notify userspace.
4295 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004296 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004297 */
4298 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004299 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004300 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004301 schedule_work(&event->remove);
4302 }
Li Zefan810cbee2013-02-18 18:56:14 +08004303 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004304
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305 return 0;
4306}
4307
Tejun Heo42809dd2012-11-19 08:13:37 -08004308static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4309{
4310 int ret;
4311
4312 mutex_lock(&cgroup_mutex);
4313 ret = cgroup_destroy_locked(dentry->d_fsdata);
4314 mutex_unlock(&cgroup_mutex);
4315
4316 return ret;
4317}
4318
Tejun Heo8e3f6542012-04-01 12:09:55 -07004319static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4320{
4321 INIT_LIST_HEAD(&ss->cftsets);
4322
4323 /*
4324 * base_cftset is embedded in subsys itself, no need to worry about
4325 * deregistration.
4326 */
4327 if (ss->base_cftypes) {
4328 ss->base_cftset.cfts = ss->base_cftypes;
4329 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4330 }
4331}
4332
Li Zefan06a11922008-04-29 01:00:07 -07004333static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004336
4337 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338
Tejun Heo648bb562012-11-19 08:13:36 -08004339 mutex_lock(&cgroup_mutex);
4340
Tejun Heo8e3f6542012-04-01 12:09:55 -07004341 /* init base cftset */
4342 cgroup_init_cftsets(ss);
4343
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004345 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004346 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004347 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 /* We don't handle early failures gracefully */
4349 BUG_ON(IS_ERR(css));
4350 init_cgroup_css(css, ss, dummytop);
4351
Li Zefane8d55fd2008-04-29 01:00:13 -07004352 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004353 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004354 * newly registered, all tasks and hence the
4355 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004356 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357
4358 need_forkexit_callback |= ss->fork || ss->exit;
4359
Li Zefane8d55fd2008-04-29 01:00:13 -07004360 /* At system boot, before all subsystems have been
4361 * registered, no tasks have been forked, so we don't
4362 * need to invoke fork callbacks here. */
4363 BUG_ON(!list_empty(&init_task.tasks));
4364
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004366 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004367
Tejun Heo648bb562012-11-19 08:13:36 -08004368 mutex_unlock(&cgroup_mutex);
4369
Ben Blume6a11052010-03-10 15:22:09 -08004370 /* this function shouldn't be used with modular subsystems, since they
4371 * need to register a subsys_id, among other things */
4372 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373}
4374
4375/**
Ben Blume6a11052010-03-10 15:22:09 -08004376 * cgroup_load_subsys: load and register a modular subsystem at runtime
4377 * @ss: the subsystem to load
4378 *
4379 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004380 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004381 * up for use. If the subsystem is built-in anyway, work is delegated to the
4382 * simpler cgroup_init_subsys.
4383 */
4384int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4385{
Ben Blume6a11052010-03-10 15:22:09 -08004386 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004387 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004388 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004389 struct css_set *cg;
4390 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004391
4392 /* check name and function validity */
4393 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004394 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004395 return -EINVAL;
4396
4397 /*
4398 * we don't support callbacks in modular subsystems. this check is
4399 * before the ss->module check for consistency; a subsystem that could
4400 * be a module should still have no callbacks even if the user isn't
4401 * compiling it as one.
4402 */
4403 if (ss->fork || ss->exit)
4404 return -EINVAL;
4405
4406 /*
4407 * an optionally modular subsystem is built-in: we want to do nothing,
4408 * since cgroup_init_subsys will have already taken care of it.
4409 */
4410 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004411 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004412 BUG_ON(subsys[ss->subsys_id] != ss);
4413 return 0;
4414 }
4415
Tejun Heo8e3f6542012-04-01 12:09:55 -07004416 /* init base cftset */
4417 cgroup_init_cftsets(ss);
4418
Ben Blume6a11052010-03-10 15:22:09 -08004419 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004420 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004421
4422 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004423 * no ss->css_alloc seems to need anything important in the ss
4424 * struct, so this can happen first (i.e. before the rootnode
4425 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004426 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004427 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004428 if (IS_ERR(css)) {
4429 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004430 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004431 mutex_unlock(&cgroup_mutex);
4432 return PTR_ERR(css);
4433 }
4434
4435 list_add(&ss->sibling, &rootnode.subsys_list);
4436 ss->root = &rootnode;
4437
4438 /* our new subsystem will be attached to the dummy hierarchy. */
4439 init_cgroup_css(css, ss, dummytop);
4440 /* init_idr must be after init_cgroup_css because it sets css->id. */
4441 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004442 ret = cgroup_init_idr(ss, css);
4443 if (ret)
4444 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004445 }
4446
4447 /*
4448 * Now we need to entangle the css into the existing css_sets. unlike
4449 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4450 * will need a new pointer to it; done by iterating the css_set_table.
4451 * furthermore, modifying the existing css_sets will corrupt the hash
4452 * table state, so each changed css_set will need its hash recomputed.
4453 * this is all done under the css_set_lock.
4454 */
4455 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004456 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004457 /* skip entries that we already rehashed */
4458 if (cg->subsys[ss->subsys_id])
4459 continue;
4460 /* remove existing entry */
4461 hash_del(&cg->hlist);
4462 /* set new value */
4463 cg->subsys[ss->subsys_id] = css;
4464 /* recompute hash and restore entry */
4465 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004466 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004467 }
4468 write_unlock(&css_set_lock);
4469
Ben Blume6a11052010-03-10 15:22:09 -08004470 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004471 ret = online_css(ss, dummytop);
4472 if (ret)
4473 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004474
Ben Blume6a11052010-03-10 15:22:09 -08004475 /* success! */
4476 mutex_unlock(&cgroup_mutex);
4477 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004478
4479err_unload:
4480 mutex_unlock(&cgroup_mutex);
4481 /* @ss can't be mounted here as try_module_get() would fail */
4482 cgroup_unload_subsys(ss);
4483 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004484}
4485EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4486
4487/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004488 * cgroup_unload_subsys: unload a modular subsystem
4489 * @ss: the subsystem to unload
4490 *
4491 * This function should be called in a modular subsystem's exitcall. When this
4492 * function is invoked, the refcount on the subsystem's module will be 0, so
4493 * the subsystem will not be attached to any hierarchy.
4494 */
4495void cgroup_unload_subsys(struct cgroup_subsys *ss)
4496{
4497 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004498
4499 BUG_ON(ss->module == NULL);
4500
4501 /*
4502 * we shouldn't be called if the subsystem is in use, and the use of
4503 * try_module_get in parse_cgroupfs_options should ensure that it
4504 * doesn't start being used while we're killing it off.
4505 */
4506 BUG_ON(ss->root != &rootnode);
4507
4508 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004509
Tejun Heoa31f2d32012-11-19 08:13:37 -08004510 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004511 ss->active = 0;
4512
Tejun Heoc897ff62013-02-27 17:03:49 -08004513 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004514 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004515
Ben Blumcf5d5942010-03-10 15:22:09 -08004516 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004517 subsys[ss->subsys_id] = NULL;
4518
4519 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004520 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004521
4522 /*
4523 * disentangle the css from all css_sets attached to the dummytop. as
4524 * in loading, we need to pay our respects to the hashtable gods.
4525 */
4526 write_lock(&css_set_lock);
4527 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4528 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004529 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004530
Li Zefan0ac801f2013-01-10 11:49:27 +08004531 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004532 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004533 key = css_set_hash(cg->subsys);
4534 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004535 }
4536 write_unlock(&css_set_lock);
4537
4538 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004539 * remove subsystem's css from the dummytop and free it - need to
4540 * free before marking as null because ss->css_free needs the
4541 * cgrp->subsys pointer to find their state. note that this also
4542 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004543 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004544 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004545 dummytop->subsys[ss->subsys_id] = NULL;
4546
4547 mutex_unlock(&cgroup_mutex);
4548}
4549EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4550
4551/**
Li Zefana043e3b2008-02-23 15:24:09 -08004552 * cgroup_init_early - cgroup initialization at system boot
4553 *
4554 * Initialize cgroups at system boot, and initialize any
4555 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556 */
4557int __init cgroup_init_early(void)
4558{
4559 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004560 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004561 INIT_LIST_HEAD(&init_css_set.cg_links);
4562 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004563 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004564 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004565 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004566 root_count = 1;
4567 init_task.cgroups = &init_css_set;
4568
4569 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004570 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004571 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004572 &rootnode.top_cgroup.css_sets);
4573 list_add(&init_css_set_link.cg_link_list,
4574 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004575
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004576 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004577 struct cgroup_subsys *ss = subsys[i];
4578
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004579 /* at bootup time, we don't worry about modular subsystems */
4580 if (!ss || ss->module)
4581 continue;
4582
Paul Menageddbcc7e2007-10-18 23:39:30 -07004583 BUG_ON(!ss->name);
4584 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004585 BUG_ON(!ss->css_alloc);
4586 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004587 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004588 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004589 ss->name, ss->subsys_id);
4590 BUG();
4591 }
4592
4593 if (ss->early_init)
4594 cgroup_init_subsys(ss);
4595 }
4596 return 0;
4597}
4598
4599/**
Li Zefana043e3b2008-02-23 15:24:09 -08004600 * cgroup_init - cgroup initialization
4601 *
4602 * Register cgroup filesystem and /proc file, and initialize
4603 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004604 */
4605int __init cgroup_init(void)
4606{
4607 int err;
4608 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004609 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004610
4611 err = bdi_init(&cgroup_backing_dev_info);
4612 if (err)
4613 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004615 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004617
4618 /* at bootup time, we don't worry about modular subsystems */
4619 if (!ss || ss->module)
4620 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621 if (!ss->early_init)
4622 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004623 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004624 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 }
4626
Li Zefan472b1052008-04-29 01:00:11 -07004627 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004628 key = css_set_hash(init_css_set.subsys);
4629 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004630 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004631
4632 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4633 if (!cgroup_kobj) {
4634 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004636 }
4637
4638 err = register_filesystem(&cgroup_fs_type);
4639 if (err < 0) {
4640 kobject_put(cgroup_kobj);
4641 goto out;
4642 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004643
Li Zefan46ae2202008-04-29 01:00:08 -07004644 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004645
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646out:
Paul Menagea4243162007-10-18 23:39:35 -07004647 if (err)
4648 bdi_destroy(&cgroup_backing_dev_info);
4649
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650 return err;
4651}
Paul Menageb4f48b62007-10-18 23:39:33 -07004652
Paul Menagea4243162007-10-18 23:39:35 -07004653/*
4654 * proc_cgroup_show()
4655 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4656 * - Used for /proc/<pid>/cgroup.
4657 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4658 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004659 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004660 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4661 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4662 * cgroup to top_cgroup.
4663 */
4664
4665/* TODO: Use a proper seq_file iterator */
4666static int proc_cgroup_show(struct seq_file *m, void *v)
4667{
4668 struct pid *pid;
4669 struct task_struct *tsk;
4670 char *buf;
4671 int retval;
4672 struct cgroupfs_root *root;
4673
4674 retval = -ENOMEM;
4675 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4676 if (!buf)
4677 goto out;
4678
4679 retval = -ESRCH;
4680 pid = m->private;
4681 tsk = get_pid_task(pid, PIDTYPE_PID);
4682 if (!tsk)
4683 goto out_free;
4684
4685 retval = 0;
4686
4687 mutex_lock(&cgroup_mutex);
4688
Li Zefane5f6a862009-01-07 18:07:41 -08004689 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004690 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004691 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004692 int count = 0;
4693
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004694 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004695 for_each_subsys(root, ss)
4696 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004697 if (strlen(root->name))
4698 seq_printf(m, "%sname=%s", count ? "," : "",
4699 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004700 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004701 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004702 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004703 if (retval < 0)
4704 goto out_unlock;
4705 seq_puts(m, buf);
4706 seq_putc(m, '\n');
4707 }
4708
4709out_unlock:
4710 mutex_unlock(&cgroup_mutex);
4711 put_task_struct(tsk);
4712out_free:
4713 kfree(buf);
4714out:
4715 return retval;
4716}
4717
4718static int cgroup_open(struct inode *inode, struct file *file)
4719{
4720 struct pid *pid = PROC_I(inode)->pid;
4721 return single_open(file, proc_cgroup_show, pid);
4722}
4723
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004724const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004725 .open = cgroup_open,
4726 .read = seq_read,
4727 .llseek = seq_lseek,
4728 .release = single_release,
4729};
4730
4731/* Display information about each subsystem and each hierarchy */
4732static int proc_cgroupstats_show(struct seq_file *m, void *v)
4733{
4734 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004735
Paul Menage8bab8dd2008-04-04 14:29:57 -07004736 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004737 /*
4738 * ideally we don't want subsystems moving around while we do this.
4739 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4740 * subsys/hierarchy state.
4741 */
Paul Menagea4243162007-10-18 23:39:35 -07004742 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004743 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4744 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004745 if (ss == NULL)
4746 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004747 seq_printf(m, "%s\t%d\t%d\t%d\n",
4748 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004749 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004750 }
4751 mutex_unlock(&cgroup_mutex);
4752 return 0;
4753}
4754
4755static int cgroupstats_open(struct inode *inode, struct file *file)
4756{
Al Viro9dce07f2008-03-29 03:07:28 +00004757 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004758}
4759
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004760static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004761 .open = cgroupstats_open,
4762 .read = seq_read,
4763 .llseek = seq_lseek,
4764 .release = single_release,
4765};
4766
Paul Menageb4f48b62007-10-18 23:39:33 -07004767/**
4768 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004769 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004770 *
4771 * Description: A task inherits its parent's cgroup at fork().
4772 *
4773 * A pointer to the shared css_set was automatically copied in
4774 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004775 * it was not made under the protection of RCU or cgroup_mutex, so
4776 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4777 * have already changed current->cgroups, allowing the previously
4778 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004779 *
4780 * At the point that cgroup_fork() is called, 'current' is the parent
4781 * task, and the passed argument 'child' points to the child task.
4782 */
4783void cgroup_fork(struct task_struct *child)
4784{
Tejun Heo9bb71302012-10-18 17:52:07 -07004785 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004786 child->cgroups = current->cgroups;
4787 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004788 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004789 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004790}
4791
4792/**
Li Zefana043e3b2008-02-23 15:24:09 -08004793 * cgroup_post_fork - called on a new task after adding it to the task list
4794 * @child: the task in question
4795 *
Tejun Heo5edee612012-10-16 15:03:14 -07004796 * Adds the task to the list running through its css_set if necessary and
4797 * call the subsystem fork() callbacks. Has to be after the task is
4798 * visible on the task list in case we race with the first call to
4799 * cgroup_iter_start() - to guarantee that the new task ends up on its
4800 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004801 */
Paul Menage817929e2007-10-18 23:39:36 -07004802void cgroup_post_fork(struct task_struct *child)
4803{
Tejun Heo5edee612012-10-16 15:03:14 -07004804 int i;
4805
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004806 /*
4807 * use_task_css_set_links is set to 1 before we walk the tasklist
4808 * under the tasklist_lock and we read it here after we added the child
4809 * to the tasklist under the tasklist_lock as well. If the child wasn't
4810 * yet in the tasklist when we walked through it from
4811 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4812 * should be visible now due to the paired locking and barriers implied
4813 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4814 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4815 * lock on fork.
4816 */
Paul Menage817929e2007-10-18 23:39:36 -07004817 if (use_task_css_set_links) {
4818 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004819 task_lock(child);
4820 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004821 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004822 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004823 write_unlock(&css_set_lock);
4824 }
Tejun Heo5edee612012-10-16 15:03:14 -07004825
4826 /*
4827 * Call ss->fork(). This must happen after @child is linked on
4828 * css_set; otherwise, @child might change state between ->fork()
4829 * and addition to css_set.
4830 */
4831 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004832 /*
4833 * fork/exit callbacks are supported only for builtin
4834 * subsystems, and the builtin section of the subsys
4835 * array is immutable, so we don't need to lock the
4836 * subsys array here. On the other hand, modular section
4837 * of the array can be freed at module unload, so we
4838 * can't touch that.
4839 */
4840 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004841 struct cgroup_subsys *ss = subsys[i];
4842
Tejun Heo5edee612012-10-16 15:03:14 -07004843 if (ss->fork)
4844 ss->fork(child);
4845 }
4846 }
Paul Menage817929e2007-10-18 23:39:36 -07004847}
Tejun Heo5edee612012-10-16 15:03:14 -07004848
Paul Menage817929e2007-10-18 23:39:36 -07004849/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004850 * cgroup_exit - detach cgroup from exiting task
4851 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004852 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004853 *
4854 * Description: Detach cgroup from @tsk and release it.
4855 *
4856 * Note that cgroups marked notify_on_release force every task in
4857 * them to take the global cgroup_mutex mutex when exiting.
4858 * This could impact scaling on very large systems. Be reluctant to
4859 * use notify_on_release cgroups where very high task exit scaling
4860 * is required on large systems.
4861 *
4862 * the_top_cgroup_hack:
4863 *
4864 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4865 *
4866 * We call cgroup_exit() while the task is still competent to
4867 * handle notify_on_release(), then leave the task attached to the
4868 * root cgroup in each hierarchy for the remainder of its exit.
4869 *
4870 * To do this properly, we would increment the reference count on
4871 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4872 * code we would add a second cgroup function call, to drop that
4873 * reference. This would just create an unnecessary hot spot on
4874 * the top_cgroup reference count, to no avail.
4875 *
4876 * Normally, holding a reference to a cgroup without bumping its
4877 * count is unsafe. The cgroup could go away, or someone could
4878 * attach us to a different cgroup, decrementing the count on
4879 * the first cgroup that we never incremented. But in this case,
4880 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004881 * which wards off any cgroup_attach_task() attempts, or task is a failed
4882 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004883 */
4884void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4885{
Paul Menage817929e2007-10-18 23:39:36 -07004886 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004887 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004888
4889 /*
4890 * Unlink from the css_set task list if necessary.
4891 * Optimistically check cg_list before taking
4892 * css_set_lock
4893 */
4894 if (!list_empty(&tsk->cg_list)) {
4895 write_lock(&css_set_lock);
4896 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004897 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004898 write_unlock(&css_set_lock);
4899 }
4900
Paul Menageb4f48b62007-10-18 23:39:33 -07004901 /* Reassign the task to the init_css_set. */
4902 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004903 cg = tsk->cgroups;
4904 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004905
4906 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004907 /*
4908 * fork/exit callbacks are supported only for builtin
4909 * subsystems, see cgroup_post_fork() for details.
4910 */
4911 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004912 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004913
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004914 if (ss->exit) {
4915 struct cgroup *old_cgrp =
4916 rcu_dereference_raw(cg->subsys[i])->cgroup;
4917 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004918 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004919 }
4920 }
4921 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004922 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004923
Li Zefanb5d646f2013-01-24 14:43:51 +08004924 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004925}
Paul Menage697f4162007-10-18 23:39:34 -07004926
Paul Menagebd89aab2007-10-18 23:40:44 -07004927static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004928{
4929 /* All of these checks rely on RCU to keep the cgroup
4930 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004931 if (cgroup_is_releasable(cgrp) &&
4932 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4933 /*
4934 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004935 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004936 * it now
4937 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004938 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004939
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004940 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004941 if (!cgroup_is_removed(cgrp) &&
4942 list_empty(&cgrp->release_list)) {
4943 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004944 need_schedule_work = 1;
4945 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004946 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004947 if (need_schedule_work)
4948 schedule_work(&release_agent_work);
4949 }
4950}
4951
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004952/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004953bool __css_tryget(struct cgroup_subsys_state *css)
4954{
Tejun Heoe9316082012-11-05 09:16:58 -08004955 while (true) {
4956 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004957
Tejun Heoe9316082012-11-05 09:16:58 -08004958 v = css_refcnt(css);
4959 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4960 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004961 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004962 else if (t < 0)
4963 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004964 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004965 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004966}
4967EXPORT_SYMBOL_GPL(__css_tryget);
4968
4969/* Caller must verify that the css is not for root cgroup */
4970void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004971{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004972 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004973
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004974 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004975 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004976 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004977}
Ben Blum67523c42010-03-10 15:22:11 -08004978EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004979
4980/*
4981 * Notify userspace when a cgroup is released, by running the
4982 * configured release agent with the name of the cgroup (path
4983 * relative to the root of cgroup file system) as the argument.
4984 *
4985 * Most likely, this user command will try to rmdir this cgroup.
4986 *
4987 * This races with the possibility that some other task will be
4988 * attached to this cgroup before it is removed, or that some other
4989 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
4990 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
4991 * unused, and this cgroup will be reprieved from its death sentence,
4992 * to continue to serve a useful existence. Next time it's released,
4993 * we will get notified again, if it still has 'notify_on_release' set.
4994 *
4995 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
4996 * means only wait until the task is successfully execve()'d. The
4997 * separate release agent task is forked by call_usermodehelper(),
4998 * then control in this thread returns here, without waiting for the
4999 * release agent task. We don't bother to wait because the caller of
5000 * this routine has no use for the exit status of the release agent
5001 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005002 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005003static void cgroup_release_agent(struct work_struct *work)
5004{
5005 BUG_ON(work != &release_agent_work);
5006 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005007 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005008 while (!list_empty(&release_list)) {
5009 char *argv[3], *envp[3];
5010 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005011 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005012 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005013 struct cgroup,
5014 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005015 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005016 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005017 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005018 if (!pathbuf)
5019 goto continue_free;
5020 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5021 goto continue_free;
5022 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5023 if (!agentbuf)
5024 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005025
5026 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005027 argv[i++] = agentbuf;
5028 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005029 argv[i] = NULL;
5030
5031 i = 0;
5032 /* minimal command environment */
5033 envp[i++] = "HOME=/";
5034 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5035 envp[i] = NULL;
5036
5037 /* Drop the lock while we invoke the usermode helper,
5038 * since the exec could involve hitting disk and hence
5039 * be a slow process */
5040 mutex_unlock(&cgroup_mutex);
5041 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005043 continue_free:
5044 kfree(pathbuf);
5045 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005046 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005048 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 mutex_unlock(&cgroup_mutex);
5050}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005051
5052static int __init cgroup_disable(char *str)
5053{
5054 int i;
5055 char *token;
5056
5057 while ((token = strsep(&str, ",")) != NULL) {
5058 if (!*token)
5059 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005060 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005061 struct cgroup_subsys *ss = subsys[i];
5062
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005063 /*
5064 * cgroup_disable, being at boot time, can't
5065 * know about module subsystems, so we don't
5066 * worry about them.
5067 */
5068 if (!ss || ss->module)
5069 continue;
5070
Paul Menage8bab8dd2008-04-04 14:29:57 -07005071 if (!strcmp(token, ss->name)) {
5072 ss->disabled = 1;
5073 printk(KERN_INFO "Disabling %s control group"
5074 " subsystem\n", ss->name);
5075 break;
5076 }
5077 }
5078 }
5079 return 1;
5080}
5081__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005082
5083/*
5084 * Functons for CSS ID.
5085 */
5086
5087/*
5088 *To get ID other than 0, this should be called when !cgroup_is_removed().
5089 */
5090unsigned short css_id(struct cgroup_subsys_state *css)
5091{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005092 struct css_id *cssid;
5093
5094 /*
5095 * This css_id() can return correct value when somone has refcnt
5096 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5097 * it's unchanged until freed.
5098 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005099 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005100
5101 if (cssid)
5102 return cssid->id;
5103 return 0;
5104}
Ben Blum67523c42010-03-10 15:22:11 -08005105EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005106
5107unsigned short css_depth(struct cgroup_subsys_state *css)
5108{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005109 struct css_id *cssid;
5110
Tejun Heo28b4c272012-04-01 12:09:56 -07005111 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005112
5113 if (cssid)
5114 return cssid->depth;
5115 return 0;
5116}
Ben Blum67523c42010-03-10 15:22:11 -08005117EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005118
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005119/**
5120 * css_is_ancestor - test "root" css is an ancestor of "child"
5121 * @child: the css to be tested.
5122 * @root: the css supporsed to be an ancestor of the child.
5123 *
5124 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005125 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005126 * But, considering usual usage, the csses should be valid objects after test.
5127 * Assuming that the caller will do some action to the child if this returns
5128 * returns true, the caller must take "child";s reference count.
5129 * If "child" is valid object and this returns true, "root" is valid, too.
5130 */
5131
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005132bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005133 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005134{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005135 struct css_id *child_id;
5136 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005137
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005138 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005139 if (!child_id)
5140 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005141 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005142 if (!root_id)
5143 return false;
5144 if (child_id->depth < root_id->depth)
5145 return false;
5146 if (child_id->stack[root_id->depth] != root_id->id)
5147 return false;
5148 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005149}
5150
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005151void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5152{
5153 struct css_id *id = css->id;
5154 /* When this is called before css_id initialization, id can be NULL */
5155 if (!id)
5156 return;
5157
5158 BUG_ON(!ss->use_id);
5159
5160 rcu_assign_pointer(id->css, NULL);
5161 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005162 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005163 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005164 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005165 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005166}
Ben Blum67523c42010-03-10 15:22:11 -08005167EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005168
5169/*
5170 * This is called by init or create(). Then, calls to this function are
5171 * always serialized (By cgroup_mutex() at create()).
5172 */
5173
5174static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5175{
5176 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005177 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005178
5179 BUG_ON(!ss->use_id);
5180
5181 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5182 newid = kzalloc(size, GFP_KERNEL);
5183 if (!newid)
5184 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005185
5186 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005187 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005188 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005189 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005190 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005191 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005192
5193 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005194 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005195 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005196
Tejun Heod228d9e2013-02-27 17:04:54 -08005197 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005198 newid->depth = depth;
5199 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005200err_out:
5201 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005202 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005203
5204}
5205
Ben Blume6a11052010-03-10 15:22:09 -08005206static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5207 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005208{
5209 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005211 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005212 idr_init(&ss->idr);
5213
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214 newid = get_new_cssid(ss, 0);
5215 if (IS_ERR(newid))
5216 return PTR_ERR(newid);
5217
5218 newid->stack[0] = newid->id;
5219 newid->css = rootcss;
5220 rootcss->id = newid;
5221 return 0;
5222}
5223
5224static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5225 struct cgroup *child)
5226{
5227 int subsys_id, i, depth = 0;
5228 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005229 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230
5231 subsys_id = ss->subsys_id;
5232 parent_css = parent->subsys[subsys_id];
5233 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005234 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005235 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236
5237 child_id = get_new_cssid(ss, depth);
5238 if (IS_ERR(child_id))
5239 return PTR_ERR(child_id);
5240
5241 for (i = 0; i < depth; i++)
5242 child_id->stack[i] = parent_id->stack[i];
5243 child_id->stack[depth] = child_id->id;
5244 /*
5245 * child_id->css pointer will be set after this cgroup is available
5246 * see cgroup_populate_dir()
5247 */
5248 rcu_assign_pointer(child_css->id, child_id);
5249
5250 return 0;
5251}
5252
5253/**
5254 * css_lookup - lookup css by id
5255 * @ss: cgroup subsys to be looked into.
5256 * @id: the id
5257 *
5258 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5259 * NULL if not. Should be called under rcu_read_lock()
5260 */
5261struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5262{
5263 struct css_id *cssid = NULL;
5264
5265 BUG_ON(!ss->use_id);
5266 cssid = idr_find(&ss->idr, id);
5267
5268 if (unlikely(!cssid))
5269 return NULL;
5270
5271 return rcu_dereference(cssid->css);
5272}
Ben Blum67523c42010-03-10 15:22:11 -08005273EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005274
5275/**
5276 * css_get_next - lookup next cgroup under specified hierarchy.
5277 * @ss: pointer to subsystem
5278 * @id: current position of iteration.
5279 * @root: pointer to css. search tree under this.
5280 * @foundid: position of found object.
5281 *
5282 * Search next css under the specified hierarchy of rootid. Calling under
5283 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5284 */
5285struct cgroup_subsys_state *
5286css_get_next(struct cgroup_subsys *ss, int id,
5287 struct cgroup_subsys_state *root, int *foundid)
5288{
5289 struct cgroup_subsys_state *ret = NULL;
5290 struct css_id *tmp;
5291 int tmpid;
5292 int rootid = css_id(root);
5293 int depth = css_depth(root);
5294
5295 if (!rootid)
5296 return NULL;
5297
5298 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005299 WARN_ON_ONCE(!rcu_read_lock_held());
5300
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005301 /* fill start point for scan */
5302 tmpid = id;
5303 while (1) {
5304 /*
5305 * scan next entry from bitmap(tree), tmpid is updated after
5306 * idr_get_next().
5307 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005309 if (!tmp)
5310 break;
5311 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5312 ret = rcu_dereference(tmp->css);
5313 if (ret) {
5314 *foundid = tmpid;
5315 break;
5316 }
5317 }
5318 /* continue to scan from next id */
5319 tmpid = tmpid + 1;
5320 }
5321 return ret;
5322}
5323
Stephane Eraniane5d13672011-02-14 11:20:01 +02005324/*
5325 * get corresponding css from file open on cgroupfs directory
5326 */
5327struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5328{
5329 struct cgroup *cgrp;
5330 struct inode *inode;
5331 struct cgroup_subsys_state *css;
5332
Al Viro496ad9a2013-01-23 17:07:38 -05005333 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005334 /* check in cgroup filesystem dir */
5335 if (inode->i_op != &cgroup_dir_inode_operations)
5336 return ERR_PTR(-EBADF);
5337
5338 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5339 return ERR_PTR(-EINVAL);
5340
5341 /* get cgroup */
5342 cgrp = __d_cgrp(f->f_dentry);
5343 css = cgrp->subsys[id];
5344 return css ? css : ERR_PTR(-ENOENT);
5345}
5346
Paul Menagefe693432009-09-23 15:56:20 -07005347#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005348static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005349{
5350 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5351
5352 if (!css)
5353 return ERR_PTR(-ENOMEM);
5354
5355 return css;
5356}
5357
Tejun Heo92fb9742012-11-19 08:13:38 -08005358static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005359{
5360 kfree(cont->subsys[debug_subsys_id]);
5361}
5362
5363static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5364{
5365 return atomic_read(&cont->count);
5366}
5367
5368static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5369{
5370 return cgroup_task_count(cont);
5371}
5372
5373static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5374{
5375 return (u64)(unsigned long)current->cgroups;
5376}
5377
5378static u64 current_css_set_refcount_read(struct cgroup *cont,
5379 struct cftype *cft)
5380{
5381 u64 count;
5382
5383 rcu_read_lock();
5384 count = atomic_read(&current->cgroups->refcount);
5385 rcu_read_unlock();
5386 return count;
5387}
5388
Paul Menage7717f7b2009-09-23 15:56:22 -07005389static int current_css_set_cg_links_read(struct cgroup *cont,
5390 struct cftype *cft,
5391 struct seq_file *seq)
5392{
5393 struct cg_cgroup_link *link;
5394 struct css_set *cg;
5395
5396 read_lock(&css_set_lock);
5397 rcu_read_lock();
5398 cg = rcu_dereference(current->cgroups);
5399 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5400 struct cgroup *c = link->cgrp;
5401 const char *name;
5402
5403 if (c->dentry)
5404 name = c->dentry->d_name.name;
5405 else
5406 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005407 seq_printf(seq, "Root %d group %s\n",
5408 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005409 }
5410 rcu_read_unlock();
5411 read_unlock(&css_set_lock);
5412 return 0;
5413}
5414
5415#define MAX_TASKS_SHOWN_PER_CSS 25
5416static int cgroup_css_links_read(struct cgroup *cont,
5417 struct cftype *cft,
5418 struct seq_file *seq)
5419{
5420 struct cg_cgroup_link *link;
5421
5422 read_lock(&css_set_lock);
5423 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5424 struct css_set *cg = link->cg;
5425 struct task_struct *task;
5426 int count = 0;
5427 seq_printf(seq, "css_set %p\n", cg);
5428 list_for_each_entry(task, &cg->tasks, cg_list) {
5429 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5430 seq_puts(seq, " ...\n");
5431 break;
5432 } else {
5433 seq_printf(seq, " task %d\n",
5434 task_pid_vnr(task));
5435 }
5436 }
5437 }
5438 read_unlock(&css_set_lock);
5439 return 0;
5440}
5441
Paul Menagefe693432009-09-23 15:56:20 -07005442static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5443{
5444 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5445}
5446
5447static struct cftype debug_files[] = {
5448 {
5449 .name = "cgroup_refcount",
5450 .read_u64 = cgroup_refcount_read,
5451 },
5452 {
5453 .name = "taskcount",
5454 .read_u64 = debug_taskcount_read,
5455 },
5456
5457 {
5458 .name = "current_css_set",
5459 .read_u64 = current_css_set_read,
5460 },
5461
5462 {
5463 .name = "current_css_set_refcount",
5464 .read_u64 = current_css_set_refcount_read,
5465 },
5466
5467 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005468 .name = "current_css_set_cg_links",
5469 .read_seq_string = current_css_set_cg_links_read,
5470 },
5471
5472 {
5473 .name = "cgroup_css_links",
5474 .read_seq_string = cgroup_css_links_read,
5475 },
5476
5477 {
Paul Menagefe693432009-09-23 15:56:20 -07005478 .name = "releasable",
5479 .read_u64 = releasable_read,
5480 },
Paul Menagefe693432009-09-23 15:56:20 -07005481
Tejun Heo4baf6e32012-04-01 12:09:55 -07005482 { } /* terminate */
5483};
Paul Menagefe693432009-09-23 15:56:20 -07005484
5485struct cgroup_subsys debug_subsys = {
5486 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005487 .css_alloc = debug_css_alloc,
5488 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005489 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005490 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005491};
5492#endif /* CONFIG_CGROUP_DEBUG */