blob: 7bf3ce09c50cdf3201883214525cea8390685227 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
33#include <linux/fs.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100034#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070035#include <linux/kernel.h>
36#include <linux/list.h>
37#include <linux/mm.h>
38#include <linux/mutex.h>
39#include <linux/mount.h>
40#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070041#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070042#include <linux/rcupdate.h>
43#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070044#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070045#include <linux/seq_file.h>
46#include <linux/slab.h>
47#include <linux/magic.h>
48#include <linux/spinlock.h>
49#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070050#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070051#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080052#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070053#include <linux/delayacct.h>
54#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080055#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040056#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070057#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070058#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070059#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080060#include <linux/eventfd.h>
61#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080062#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020063#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heo28b4c272012-04-01 12:09:56 -070067/* css deactivation bias, makes css->refcnt negative to deny new trygets */
68#define CSS_DEACT_BIAS INT_MIN
69
Tejun Heoe25e2cb2011-12-12 18:12:21 -080070/*
71 * cgroup_mutex is the master lock. Any modification to cgroup or its
72 * hierarchy must be performed while holding it.
73 *
74 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
75 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
76 * release_agent_path and so on. Modifying requires both cgroup_mutex and
77 * cgroup_root_mutex. Readers can acquire either of the two. This is to
78 * break the following locking order cycle.
79 *
80 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
81 * B. namespace_sem -> cgroup_mutex
82 *
83 * B happens only through cgroup_show_options() and using cgroup_root_mutex
84 * breaks it.
85 */
Tejun Heo22194492013-04-07 09:29:51 -070086#ifdef CONFIG_PROVE_RCU
87DEFINE_MUTEX(cgroup_mutex);
88EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
89#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070090static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070091#endif
92
Tejun Heoe25e2cb2011-12-12 18:12:21 -080093static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070094
Ben Blumaae8aab2010-03-10 15:22:07 -080095/*
96 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020097 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080098 * registered after that. The mutable section of this array is protected by
99 * cgroup_mutex.
100 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200101#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200102#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800103static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104#include <linux/cgroup_subsys.h>
105};
106
Paul Menagec6d57f32009-09-23 15:56:19 -0700107#define MAX_CGROUP_ROOT_NAMELEN 64
108
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109/*
110 * A cgroupfs_root represents the root of a cgroup hierarchy,
111 * and may be associated with a superblock to form an active
112 * hierarchy
113 */
114struct cgroupfs_root {
115 struct super_block *sb;
116
117 /*
118 * The bitmask of subsystems intended to be attached to this
119 * hierarchy
120 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400121 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700122
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700123 /* Unique id for this hierarchy. */
124 int hierarchy_id;
125
Paul Menageddbcc7e2007-10-18 23:39:30 -0700126 /* The bitmask of subsystems currently attached to this hierarchy */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400127 unsigned long actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700128
129 /* A list running through the attached subsystems */
130 struct list_head subsys_list;
131
132 /* The root cgroup for this hierarchy */
133 struct cgroup top_cgroup;
134
135 /* Tracks how many cgroups are currently defined in hierarchy.*/
136 int number_of_cgroups;
137
Li Zefane5f6a862009-01-07 18:07:41 -0800138 /* A list running through the active hierarchies */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139 struct list_head root_list;
140
Tejun Heob0ca5a82012-04-01 12:09:54 -0700141 /* All cgroups on this root, cgroup_mutex protected */
142 struct list_head allcg_list;
143
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144 /* Hierarchy-specific flags */
145 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700146
Tejun Heo0a950f62012-11-19 09:02:12 -0800147 /* IDs for cgroups in this hierarchy */
148 struct ida cgroup_ida;
149
Paul Menagee788e062008-07-25 01:46:59 -0700150 /* The path to use for release notifications. */
Paul Menage81a6a5c2007-10-18 23:39:38 -0700151 char release_agent_path[PATH_MAX];
Paul Menagec6d57f32009-09-23 15:56:19 -0700152
153 /* The name for this hierarchy - may be empty */
154 char name[MAX_CGROUP_ROOT_NAMELEN];
Paul Menageddbcc7e2007-10-18 23:39:30 -0700155};
156
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157/*
158 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
159 * subsystems that are otherwise unattached - it never has more than a
160 * single cgroup, and all tasks are part of that cgroup.
161 */
162static struct cgroupfs_root rootnode;
163
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700164/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700165 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
166 */
167struct cfent {
168 struct list_head node;
169 struct dentry *dentry;
170 struct cftype *type;
171};
172
173/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700174 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
175 * cgroup_subsys->use_id != 0.
176 */
177#define CSS_ID_MAX (65535)
178struct css_id {
179 /*
180 * The css to which this ID points. This pointer is set to valid value
181 * after cgroup is populated. If cgroup is removed, this will be NULL.
182 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800183 * is called after synchronize_rcu(). But for safe use, css_tryget()
184 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700185 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100186 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700187 /*
188 * ID of this css.
189 */
190 unsigned short id;
191 /*
192 * Depth in hierarchy which this ID belongs to.
193 */
194 unsigned short depth;
195 /*
196 * ID is freed by RCU. (and lookup routine is RCU safe.)
197 */
198 struct rcu_head rcu_head;
199 /*
200 * Hierarchy of CSS ID belongs to.
201 */
202 unsigned short stack[0]; /* Array of Length (depth+1) */
203};
204
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800205/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300206 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800207 */
208struct cgroup_event {
209 /*
210 * Cgroup which the event belongs to.
211 */
212 struct cgroup *cgrp;
213 /*
214 * Control file which the event associated.
215 */
216 struct cftype *cft;
217 /*
218 * eventfd to signal userspace about the event.
219 */
220 struct eventfd_ctx *eventfd;
221 /*
222 * Each of these stored in a list by the cgroup.
223 */
224 struct list_head list;
225 /*
226 * All fields below needed to unregister event when
227 * userspace closes eventfd.
228 */
229 poll_table pt;
230 wait_queue_head_t *wqh;
231 wait_queue_t wait;
232 struct work_struct remove;
233};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700234
Paul Menageddbcc7e2007-10-18 23:39:30 -0700235/* The list of hierarchy roots */
236
237static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700238static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700239
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700240static DEFINE_IDA(hierarchy_ida);
241static int next_hierarchy_id;
242static DEFINE_SPINLOCK(hierarchy_id_lock);
243
Paul Menageddbcc7e2007-10-18 23:39:30 -0700244/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
245#define dummytop (&rootnode.top_cgroup)
246
Li Zefan65dff752013-03-01 15:01:56 +0800247static struct cgroup_name root_cgroup_name = { .name = "/" };
248
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800250 * check for fork/exit handlers to call. This avoids us having to do
251 * extra work in the fork/exit path if none of the subsystems need to
252 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700254static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700255
Tejun Heo42809dd2012-11-19 08:13:37 -0800256static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800257static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
258 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800259
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700260static int css_unbias_refcnt(int refcnt)
261{
262 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
263}
264
Tejun Heo28b4c272012-04-01 12:09:56 -0700265/* the current nr of refs, always >= 0 whether @css is deactivated or not */
266static int css_refcnt(struct cgroup_subsys_state *css)
267{
268 int v = atomic_read(&css->refcnt);
269
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700270 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700271}
272
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700274inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700275{
Paul Menagebd89aab2007-10-18 23:40:44 -0700276 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700277}
278
Li Zefan78574cf2013-04-08 19:00:38 -0700279/**
280 * cgroup_is_descendant - test ancestry
281 * @cgrp: the cgroup to be tested
282 * @ancestor: possible ancestor of @cgrp
283 *
284 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
285 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
286 * and @ancestor are accessible.
287 */
288bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
289{
290 while (cgrp) {
291 if (cgrp == ancestor)
292 return true;
293 cgrp = cgrp->parent;
294 }
295 return false;
296}
297EXPORT_SYMBOL_GPL(cgroup_is_descendant);
298
Paul Menageddbcc7e2007-10-18 23:39:30 -0700299/* bits in struct cgroupfs_root flags field */
300enum {
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400301 ROOT_NOPREFIX, /* mounted subsystems have no named prefix */
302 ROOT_XATTR, /* supports extended attributes */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700303};
304
Adrian Bunke9685a02008-02-07 00:13:46 -0800305static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306{
307 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700308 (1 << CGRP_RELEASABLE) |
309 (1 << CGRP_NOTIFY_ON_RELEASE);
310 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311}
312
Adrian Bunke9685a02008-02-07 00:13:46 -0800313static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314{
Paul Menagebd89aab2007-10-18 23:40:44 -0700315 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700316}
317
Paul Menageddbcc7e2007-10-18 23:39:30 -0700318/*
319 * for_each_subsys() allows you to iterate on each subsystem attached to
320 * an active hierarchy
321 */
322#define for_each_subsys(_root, _ss) \
323list_for_each_entry(_ss, &_root->subsys_list, sibling)
324
Li Zefane5f6a862009-01-07 18:07:41 -0800325/* for_each_active_root() allows you to iterate across the active hierarchies */
326#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700327list_for_each_entry(_root, &roots, root_list)
328
Tejun Heof6ea9372012-04-01 12:09:55 -0700329static inline struct cgroup *__d_cgrp(struct dentry *dentry)
330{
331 return dentry->d_fsdata;
332}
333
Tejun Heo05ef1d72012-04-01 12:09:56 -0700334static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700335{
336 return dentry->d_fsdata;
337}
338
Tejun Heo05ef1d72012-04-01 12:09:56 -0700339static inline struct cftype *__d_cft(struct dentry *dentry)
340{
341 return __d_cfe(dentry)->type;
342}
343
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700344/**
345 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
346 * @cgrp: the cgroup to be checked for liveness
347 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700348 * On success, returns true; the mutex should be later unlocked. On
349 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700350 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700351static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700352{
353 mutex_lock(&cgroup_mutex);
354 if (cgroup_is_removed(cgrp)) {
355 mutex_unlock(&cgroup_mutex);
356 return false;
357 }
358 return true;
359}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700360
Paul Menage81a6a5c2007-10-18 23:39:38 -0700361/* the list of cgroups eligible for automatic release. Protected by
362 * release_list_lock */
363static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200364static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700365static void cgroup_release_agent(struct work_struct *work);
366static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700367static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700368
Paul Menage817929e2007-10-18 23:39:36 -0700369/* Link structure for associating css_set objects with cgroups */
370struct cg_cgroup_link {
371 /*
372 * List running through cg_cgroup_links associated with a
373 * cgroup, anchored on cgroup->css_sets
374 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700375 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700376 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700377 /*
378 * List running through cg_cgroup_links pointing at a
379 * single css_set object, anchored on css_set->cg_links
380 */
381 struct list_head cg_link_list;
382 struct css_set *cg;
383};
384
385/* The default css_set - used by init and its children prior to any
386 * hierarchies being mounted. It contains a pointer to the root state
387 * for each subsystem. Also used to anchor the list of css_sets. Not
388 * reference-counted, to improve performance when child cgroups
389 * haven't been created.
390 */
391
392static struct css_set init_css_set;
393static struct cg_cgroup_link init_css_set_link;
394
Ben Blume6a11052010-03-10 15:22:09 -0800395static int cgroup_init_idr(struct cgroup_subsys *ss,
396 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700397
Paul Menage817929e2007-10-18 23:39:36 -0700398/* css_set_lock protects the list of css_set objects, and the
399 * chain of tasks off each css_set. Nests outside task->alloc_lock
400 * due to cgroup_iter_start() */
401static DEFINE_RWLOCK(css_set_lock);
402static int css_set_count;
403
Paul Menage7717f7b2009-09-23 15:56:22 -0700404/*
405 * hash table for cgroup groups. This improves the performance to find
406 * an existing css_set. This hash doesn't (currently) take into
407 * account cgroups in empty hierarchies.
408 */
Li Zefan472b1052008-04-29 01:00:11 -0700409#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800410static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700411
Li Zefan0ac801f2013-01-10 11:49:27 +0800412static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700413{
414 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800415 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700416
417 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800418 key += (unsigned long)css[i];
419 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700420
Li Zefan0ac801f2013-01-10 11:49:27 +0800421 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700422}
423
Paul Menage817929e2007-10-18 23:39:36 -0700424/* We don't maintain the lists running through each css_set to its
425 * task until after the first call to cgroup_iter_start(). This
426 * reduces the fork()/exit() overhead for people who have cgroups
427 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700428static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700429
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700430static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700431{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700432 struct cg_cgroup_link *link;
433 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700434 /*
435 * Ensure that the refcount doesn't hit zero while any readers
436 * can see it. Similar to atomic_dec_and_lock(), but for an
437 * rwlock
438 */
439 if (atomic_add_unless(&cg->refcount, -1, 1))
440 return;
441 write_lock(&css_set_lock);
442 if (!atomic_dec_and_test(&cg->refcount)) {
443 write_unlock(&css_set_lock);
444 return;
445 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700446
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700447 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800448 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700449 css_set_count--;
450
451 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
452 cg_link_list) {
453 struct cgroup *cgrp = link->cgrp;
454 list_del(&link->cg_link_list);
455 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800456
457 /*
458 * We may not be holding cgroup_mutex, and if cgrp->count is
459 * dropped to 0 the cgroup can be destroyed at any time, hence
460 * rcu_read_lock is used to keep it alive.
461 */
462 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700463 if (atomic_dec_and_test(&cgrp->count) &&
464 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700465 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700466 set_bit(CGRP_RELEASABLE, &cgrp->flags);
467 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700468 }
Li Zefan71b57072013-01-24 14:43:28 +0800469 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700470
471 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700472 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700473
474 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800475 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700476}
477
478/*
479 * refcounted get/put for css_set objects
480 */
481static inline void get_css_set(struct css_set *cg)
482{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700483 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700484}
485
486static inline void put_css_set(struct css_set *cg)
487{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700488 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700489}
490
Paul Menage81a6a5c2007-10-18 23:39:38 -0700491static inline void put_css_set_taskexit(struct css_set *cg)
492{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700493 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700494}
495
Paul Menage817929e2007-10-18 23:39:36 -0700496/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 * compare_css_sets - helper function for find_existing_css_set().
498 * @cg: candidate css_set being tested
499 * @old_cg: existing css_set for a task
500 * @new_cgrp: cgroup that's being entered by the task
501 * @template: desired set of css pointers in css_set (pre-calculated)
502 *
503 * Returns true if "cg" matches "old_cg" except for the hierarchy
504 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
505 */
506static bool compare_css_sets(struct css_set *cg,
507 struct css_set *old_cg,
508 struct cgroup *new_cgrp,
509 struct cgroup_subsys_state *template[])
510{
511 struct list_head *l1, *l2;
512
513 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
514 /* Not all subsystems matched */
515 return false;
516 }
517
518 /*
519 * Compare cgroup pointers in order to distinguish between
520 * different cgroups in heirarchies with no subsystems. We
521 * could get by with just this check alone (and skip the
522 * memcmp above) but on most setups the memcmp check will
523 * avoid the need for this more expensive check on almost all
524 * candidates.
525 */
526
527 l1 = &cg->cg_links;
528 l2 = &old_cg->cg_links;
529 while (1) {
530 struct cg_cgroup_link *cgl1, *cgl2;
531 struct cgroup *cg1, *cg2;
532
533 l1 = l1->next;
534 l2 = l2->next;
535 /* See if we reached the end - both lists are equal length. */
536 if (l1 == &cg->cg_links) {
537 BUG_ON(l2 != &old_cg->cg_links);
538 break;
539 } else {
540 BUG_ON(l2 == &old_cg->cg_links);
541 }
542 /* Locate the cgroups associated with these links. */
543 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
544 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
545 cg1 = cgl1->cgrp;
546 cg2 = cgl2->cgrp;
547 /* Hierarchies should be linked in the same order. */
548 BUG_ON(cg1->root != cg2->root);
549
550 /*
551 * If this hierarchy is the hierarchy of the cgroup
552 * that's changing, then we need to check that this
553 * css_set points to the new cgroup; if it's any other
554 * hierarchy, then this css_set should point to the
555 * same cgroup as the old css_set.
556 */
557 if (cg1->root == new_cgrp->root) {
558 if (cg1 != new_cgrp)
559 return false;
560 } else {
561 if (cg1 != cg2)
562 return false;
563 }
564 }
565 return true;
566}
567
568/*
Paul Menage817929e2007-10-18 23:39:36 -0700569 * find_existing_css_set() is a helper for
570 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700571 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700572 *
573 * oldcg: the cgroup group that we're using before the cgroup
574 * transition
575 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700576 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700577 *
578 * template: location in which to build the desired set of subsystem
579 * state objects for the new cgroup group
580 */
Paul Menage817929e2007-10-18 23:39:36 -0700581static struct css_set *find_existing_css_set(
582 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700583 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700584 struct cgroup_subsys_state *template[])
585{
586 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700587 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700588 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800589 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700590
Ben Blumaae8aab2010-03-10 15:22:07 -0800591 /*
592 * Build the set of subsystem state objects that we want to see in the
593 * new css_set. while subsystems can change globally, the entries here
594 * won't change, so no need for locking.
595 */
Paul Menage817929e2007-10-18 23:39:36 -0700596 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400597 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700598 /* Subsystem is in this hierarchy. So we want
599 * the subsystem state from the new
600 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700601 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700602 } else {
603 /* Subsystem is not in this hierarchy, so we
604 * don't want to change the subsystem state */
605 template[i] = oldcg->subsys[i];
606 }
607 }
608
Li Zefan0ac801f2013-01-10 11:49:27 +0800609 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800610 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700611 if (!compare_css_sets(cg, oldcg, cgrp, template))
612 continue;
613
614 /* This css_set matches what we need */
615 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700616 }
Paul Menage817929e2007-10-18 23:39:36 -0700617
618 /* No existing cgroup group matched */
619 return NULL;
620}
621
Paul Menage817929e2007-10-18 23:39:36 -0700622static void free_cg_links(struct list_head *tmp)
623{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700624 struct cg_cgroup_link *link;
625 struct cg_cgroup_link *saved_link;
626
627 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700628 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700629 kfree(link);
630 }
631}
632
633/*
Li Zefan36553432008-07-29 22:33:19 -0700634 * allocate_cg_links() allocates "count" cg_cgroup_link structures
635 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
636 * success or a negative error
637 */
638static int allocate_cg_links(int count, struct list_head *tmp)
639{
640 struct cg_cgroup_link *link;
641 int i;
642 INIT_LIST_HEAD(tmp);
643 for (i = 0; i < count; i++) {
644 link = kmalloc(sizeof(*link), GFP_KERNEL);
645 if (!link) {
646 free_cg_links(tmp);
647 return -ENOMEM;
648 }
649 list_add(&link->cgrp_link_list, tmp);
650 }
651 return 0;
652}
653
Li Zefanc12f65d2009-01-07 18:07:42 -0800654/**
655 * link_css_set - a helper function to link a css_set to a cgroup
656 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
657 * @cg: the css_set to be linked
658 * @cgrp: the destination cgroup
659 */
660static void link_css_set(struct list_head *tmp_cg_links,
661 struct css_set *cg, struct cgroup *cgrp)
662{
663 struct cg_cgroup_link *link;
664
665 BUG_ON(list_empty(tmp_cg_links));
666 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
667 cgrp_link_list);
668 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700669 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700670 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800671 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700672 /*
673 * Always add links to the tail of the list so that the list
674 * is sorted by order of hierarchy creation
675 */
676 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800677}
678
Li Zefan36553432008-07-29 22:33:19 -0700679/*
Paul Menage817929e2007-10-18 23:39:36 -0700680 * find_css_set() takes an existing cgroup group and a
681 * cgroup object, and returns a css_set object that's
682 * equivalent to the old group, but with the given cgroup
683 * substituted into the appropriate hierarchy. Must be called with
684 * cgroup_mutex held
685 */
Paul Menage817929e2007-10-18 23:39:36 -0700686static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700687 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700688{
689 struct css_set *res;
690 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700691
692 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700693
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800695 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700696
Paul Menage817929e2007-10-18 23:39:36 -0700697 /* First see if we already have a cgroup group that matches
698 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700699 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700700 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700701 if (res)
702 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700703 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700704
705 if (res)
706 return res;
707
708 res = kmalloc(sizeof(*res), GFP_KERNEL);
709 if (!res)
710 return NULL;
711
712 /* Allocate all the cg_cgroup_link objects that we'll need */
713 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
714 kfree(res);
715 return NULL;
716 }
717
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700718 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700719 INIT_LIST_HEAD(&res->cg_links);
720 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700721 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700722
723 /* Copy the set of subsystem state objects generated in
724 * find_existing_css_set() */
725 memcpy(res->subsys, template, sizeof(res->subsys));
726
727 write_lock(&css_set_lock);
728 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
730 struct cgroup *c = link->cgrp;
731 if (c->root == cgrp->root)
732 c = cgrp;
733 link_css_set(&tmp_cg_links, res, c);
734 }
Paul Menage817929e2007-10-18 23:39:36 -0700735
736 BUG_ON(!list_empty(&tmp_cg_links));
737
Paul Menage817929e2007-10-18 23:39:36 -0700738 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700739
740 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800741 key = css_set_hash(res->subsys);
742 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700743
Paul Menage817929e2007-10-18 23:39:36 -0700744 write_unlock(&css_set_lock);
745
746 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700747}
748
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700750 * Return the cgroup for "task" from the given hierarchy. Must be
751 * called with cgroup_mutex held.
752 */
753static struct cgroup *task_cgroup_from_root(struct task_struct *task,
754 struct cgroupfs_root *root)
755{
756 struct css_set *css;
757 struct cgroup *res = NULL;
758
759 BUG_ON(!mutex_is_locked(&cgroup_mutex));
760 read_lock(&css_set_lock);
761 /*
762 * No need to lock the task - since we hold cgroup_mutex the
763 * task can't change groups, so the only thing that can happen
764 * is that it exits and its css is set back to init_css_set.
765 */
766 css = task->cgroups;
767 if (css == &init_css_set) {
768 res = &root->top_cgroup;
769 } else {
770 struct cg_cgroup_link *link;
771 list_for_each_entry(link, &css->cg_links, cg_link_list) {
772 struct cgroup *c = link->cgrp;
773 if (c->root == root) {
774 res = c;
775 break;
776 }
777 }
778 }
779 read_unlock(&css_set_lock);
780 BUG_ON(!res);
781 return res;
782}
783
784/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785 * There is one global cgroup mutex. We also require taking
786 * task_lock() when dereferencing a task's cgroup subsys pointers.
787 * See "The task_lock() exception", at the end of this comment.
788 *
789 * A task must hold cgroup_mutex to modify cgroups.
790 *
791 * Any task can increment and decrement the count field without lock.
792 * So in general, code holding cgroup_mutex can't rely on the count
793 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800794 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795 * means that no tasks are currently attached, therefore there is no
796 * way a task attached to that cgroup can fork (the other way to
797 * increment the count). So code holding cgroup_mutex can safely
798 * assume that if the count is zero, it will stay zero. Similarly, if
799 * a task holds cgroup_mutex on a cgroup with zero count, it
800 * knows that the cgroup won't be removed, as cgroup_rmdir()
801 * needs that mutex.
802 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
804 * (usually) take cgroup_mutex. These are the two most performance
805 * critical pieces of code here. The exception occurs on cgroup_exit(),
806 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
807 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800808 * to the release agent with the name of the cgroup (path relative to
809 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810 *
811 * A cgroup can only be deleted if both its 'count' of using tasks
812 * is zero, and its list of 'children' cgroups is empty. Since all
813 * tasks in the system use _some_ cgroup, and since there is always at
814 * least one task in the system (init, pid == 1), therefore, top_cgroup
815 * always has either children cgroups and/or using tasks. So we don't
816 * need a special hack to ensure that top_cgroup cannot be deleted.
817 *
818 * The task_lock() exception
819 *
820 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800821 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800822 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823 * several performance critical places that need to reference
824 * task->cgroup without the expense of grabbing a system global
825 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800826 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
828 * the task_struct routinely used for such matters.
829 *
830 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800831 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700832 */
833
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834/*
835 * A couple of forward declarations required, due to cyclic reference loop:
836 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
837 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
838 * -> cgroup_mkdir.
839 */
840
Al Viro18bb1db2011-07-26 01:41:39 -0400841static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400842static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400844static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
845 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700846static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700847static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700848
849static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200850 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700851 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700852};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700854static int alloc_css_id(struct cgroup_subsys *ss,
855 struct cgroup *parent, struct cgroup *child);
856
Al Viroa5e7ed32011-07-26 01:55:55 -0400857static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700858{
859 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700860
861 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400862 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700863 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100864 inode->i_uid = current_fsuid();
865 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700866 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
867 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
868 }
869 return inode;
870}
871
Li Zefan65dff752013-03-01 15:01:56 +0800872static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
873{
874 struct cgroup_name *name;
875
876 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
877 if (!name)
878 return NULL;
879 strcpy(name->name, dentry->d_name.name);
880 return name;
881}
882
Li Zefanbe445622013-01-24 14:31:42 +0800883static void cgroup_free_fn(struct work_struct *work)
884{
885 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
886 struct cgroup_subsys *ss;
887
888 mutex_lock(&cgroup_mutex);
889 /*
890 * Release the subsystem state objects.
891 */
892 for_each_subsys(cgrp->root, ss)
893 ss->css_free(cgrp);
894
895 cgrp->root->number_of_cgroups--;
896 mutex_unlock(&cgroup_mutex);
897
898 /*
Li Zefan415cf072013-04-08 14:35:02 +0800899 * We get a ref to the parent's dentry, and put the ref when
900 * this cgroup is being freed, so it's guaranteed that the
901 * parent won't be destroyed before its children.
902 */
903 dput(cgrp->parent->dentry);
904
905 /*
Li Zefanbe445622013-01-24 14:31:42 +0800906 * Drop the active superblock reference that we took when we
907 * created the cgroup
908 */
909 deactivate_super(cgrp->root->sb);
910
911 /*
912 * if we're getting rid of the cgroup, refcount should ensure
913 * that there are no pidlists left.
914 */
915 BUG_ON(!list_empty(&cgrp->pidlists));
916
917 simple_xattrs_free(&cgrp->xattrs);
918
919 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +0800920 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800921 kfree(cgrp);
922}
923
924static void cgroup_free_rcu(struct rcu_head *head)
925{
926 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
927
928 schedule_work(&cgrp->free_work);
929}
930
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931static void cgroup_diput(struct dentry *dentry, struct inode *inode)
932{
933 /* is dentry a directory ? if so, kfree() associated cgroup */
934 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700935 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800936
Paul Menagebd89aab2007-10-18 23:40:44 -0700937 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800938 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700939 } else {
940 struct cfent *cfe = __d_cfe(dentry);
941 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400942 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943
944 WARN_ONCE(!list_empty(&cfe->node) &&
945 cgrp != &cgrp->root->top_cgroup,
946 "cfe still linked for %s\n", cfe->type->name);
947 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400948 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700949 }
950 iput(inode);
951}
952
Al Viroc72a04e2011-01-14 05:31:45 +0000953static int cgroup_delete(const struct dentry *d)
954{
955 return 1;
956}
957
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958static void remove_dir(struct dentry *d)
959{
960 struct dentry *parent = dget(d->d_parent);
961
962 d_delete(d);
963 simple_rmdir(parent->d_inode, d);
964 dput(parent);
965}
966
Li Zefan2739d3c2013-01-21 18:18:33 +0800967static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
972 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100973
Li Zefan2739d3c2013-01-21 18:18:33 +0800974 /*
975 * If we're doing cleanup due to failure of cgroup_create(),
976 * the corresponding @cfe may not exist.
977 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700978 list_for_each_entry(cfe, &cgrp->files, node) {
979 struct dentry *d = cfe->dentry;
980
981 if (cft && cfe->type != cft)
982 continue;
983
984 dget(d);
985 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700986 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700987 list_del_init(&cfe->node);
988 dput(d);
989
Li Zefan2739d3c2013-01-21 18:18:33 +0800990 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700992}
993
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400994/**
995 * cgroup_clear_directory - selective removal of base and subsystem files
996 * @dir: directory containing the files
997 * @base_files: true if the base files should be removed
998 * @subsys_mask: mask of the subsystem ids whose files should be removed
999 */
1000static void cgroup_clear_directory(struct dentry *dir, bool base_files,
1001 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -07001002{
1003 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001004 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -07001005
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001006 for_each_subsys(cgrp->root, ss) {
1007 struct cftype_set *set;
1008 if (!test_bit(ss->subsys_id, &subsys_mask))
1009 continue;
1010 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +08001011 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001012 }
1013 if (base_files) {
1014 while (!list_empty(&cgrp->files))
1015 cgroup_rm_file(cgrp, NULL);
1016 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017}
1018
1019/*
1020 * NOTE : the dentry must have been dget()'ed
1021 */
1022static void cgroup_d_remove_dir(struct dentry *dentry)
1023{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001024 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001025 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001026
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001027 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001029 parent = dentry->d_parent;
1030 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001031 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001033 spin_unlock(&dentry->d_lock);
1034 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 remove_dir(dentry);
1036}
1037
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001038/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001039 * Call with cgroup_mutex held. Drops reference counts on modules, including
1040 * any duplicate ones that parse_cgroupfs_options took. If this function
1041 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001042 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001044 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001046 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001047 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 int i;
1049
Ben Blumaae8aab2010-03-10 15:22:07 -08001050 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001051 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001052
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001053 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1054 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 /* Check that any added subsystems are currently free */
1056 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001057 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001059 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001061 /*
1062 * Nobody should tell us to do a subsys that doesn't exist:
1063 * parse_cgroupfs_options should catch that case and refcounts
1064 * ensure that subsystems won't disappear once selected.
1065 */
1066 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 if (ss->root != &rootnode) {
1068 /* Subsystem isn't free */
1069 return -EBUSY;
1070 }
1071 }
1072
1073 /* Currently we don't handle adding/removing subsystems when
1074 * any child cgroups exist. This is theoretically supportable
1075 * but involves complex error handling, so it's being left until
1076 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001077 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 return -EBUSY;
1079
1080 /* Process each subsystem */
1081 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1082 struct cgroup_subsys *ss = subsys[i];
1083 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001084 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001086 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001087 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 BUG_ON(!dummytop->subsys[i]);
1089 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001090 cgrp->subsys[i] = dummytop->subsys[i];
1091 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001092 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001093 ss->root = root;
Ben Blumcf5d5942010-03-10 15:22:09 -08001094 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001095 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001097 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001098 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1099 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001101 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001102 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001103 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001104 /* subsystem is now free - drop reference on module */
1105 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001106 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001108 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001109 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001110 /*
1111 * a refcount was taken, but we already had one, so
1112 * drop the extra reference.
1113 */
1114 module_put(ss->module);
1115#ifdef CONFIG_MODULE_UNLOAD
1116 BUG_ON(ss->module && !module_refcount(ss->module));
1117#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 } else {
1119 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001120 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121 }
1122 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001123 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124
1125 return 0;
1126}
1127
Al Viro34c80b12011-12-08 21:32:45 -05001128static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129{
Al Viro34c80b12011-12-08 21:32:45 -05001130 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131 struct cgroup_subsys *ss;
1132
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001133 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134 for_each_subsys(root, ss)
1135 seq_printf(seq, ",%s", ss->name);
1136 if (test_bit(ROOT_NOPREFIX, &root->flags))
1137 seq_puts(seq, ",noprefix");
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001138 if (test_bit(ROOT_XATTR, &root->flags))
1139 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001140 if (strlen(root->release_agent_path))
1141 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001142 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001143 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001144 if (strlen(root->name))
1145 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001146 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147 return 0;
1148}
1149
1150struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001151 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001153 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001154 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001156 /* User explicitly requested empty subsystem */
1157 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001158
1159 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001160
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161};
1162
Ben Blumaae8aab2010-03-10 15:22:07 -08001163/*
1164 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001165 * with cgroup_mutex held to protect the subsys[] array. This function takes
1166 * refcounts on subsystems to be used, unless it returns error, in which case
1167 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001168 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001169static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001170{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 char *token, *o = data;
1172 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001173 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001174 int i;
1175 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001176
Ben Blumaae8aab2010-03-10 15:22:07 -08001177 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1178
Li Zefanf9ab5b52009-06-17 16:26:33 -07001179#ifdef CONFIG_CPUSETS
1180 mask = ~(1UL << cpuset_subsys_id);
1181#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001182
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001184
1185 while ((token = strsep(&o, ",")) != NULL) {
1186 if (!*token)
1187 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001189 /* Explicitly have no subsystems */
1190 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191 continue;
1192 }
1193 if (!strcmp(token, "all")) {
1194 /* Mutually exclusive option 'all' + subsystem name */
1195 if (one_ss)
1196 return -EINVAL;
1197 all_ss = true;
1198 continue;
1199 }
1200 if (!strcmp(token, "noprefix")) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001201 set_bit(ROOT_NOPREFIX, &opts->flags);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 continue;
1203 }
1204 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001205 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206 continue;
1207 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001208 if (!strcmp(token, "xattr")) {
1209 set_bit(ROOT_XATTR, &opts->flags);
1210 continue;
1211 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001213 /* Specifying two release agents is forbidden */
1214 if (opts->release_agent)
1215 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001216 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001217 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001218 if (!opts->release_agent)
1219 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220 continue;
1221 }
1222 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001223 const char *name = token + 5;
1224 /* Can't specify an empty name */
1225 if (!strlen(name))
1226 return -EINVAL;
1227 /* Must match [\w.-]+ */
1228 for (i = 0; i < strlen(name); i++) {
1229 char c = name[i];
1230 if (isalnum(c))
1231 continue;
1232 if ((c == '.') || (c == '-') || (c == '_'))
1233 continue;
1234 return -EINVAL;
1235 }
1236 /* Specifying two names is forbidden */
1237 if (opts->name)
1238 return -EINVAL;
1239 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001240 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001241 GFP_KERNEL);
1242 if (!opts->name)
1243 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001244
1245 continue;
1246 }
1247
1248 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1249 struct cgroup_subsys *ss = subsys[i];
1250 if (ss == NULL)
1251 continue;
1252 if (strcmp(token, ss->name))
1253 continue;
1254 if (ss->disabled)
1255 continue;
1256
1257 /* Mutually exclusive option 'all' + subsystem name */
1258 if (all_ss)
1259 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001260 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001261 one_ss = true;
1262
1263 break;
1264 }
1265 if (i == CGROUP_SUBSYS_COUNT)
1266 return -ENOENT;
1267 }
1268
1269 /*
1270 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001271 * otherwise if 'none', 'name=' and a subsystem name options
1272 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001273 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001274 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001275 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1276 struct cgroup_subsys *ss = subsys[i];
1277 if (ss == NULL)
1278 continue;
1279 if (ss->disabled)
1280 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001281 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282 }
1283 }
1284
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001285 /* Consistency checks */
1286
Li Zefanf9ab5b52009-06-17 16:26:33 -07001287 /*
1288 * Option noprefix was introduced just for backward compatibility
1289 * with the old cpuset, so we allow noprefix only if mounting just
1290 * the cpuset subsystem.
1291 */
1292 if (test_bit(ROOT_NOPREFIX, &opts->flags) &&
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001293 (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001294 return -EINVAL;
1295
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001296
1297 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001298 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001299 return -EINVAL;
1300
1301 /*
1302 * We either have to specify by name or by subsystems. (So all
1303 * empty hierarchies must have a name).
1304 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001305 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 return -EINVAL;
1307
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 /*
1309 * Grab references on all the modules we'll need, so the subsystems
1310 * don't dance around before rebind_subsystems attaches them. This may
1311 * take duplicate reference counts on a subsystem that's already used,
1312 * but rebind_subsystems handles this case.
1313 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001314 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001315 unsigned long bit = 1UL << i;
1316
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 continue;
1319 if (!try_module_get(subsys[i]->module)) {
1320 module_pin_failed = true;
1321 break;
1322 }
1323 }
1324 if (module_pin_failed) {
1325 /*
1326 * oops, one of the modules was going away. this means that we
1327 * raced with a module_delete call, and to the user this is
1328 * essentially a "subsystem doesn't exist" case.
1329 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001330 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001331 /* drop refcounts only on the ones we took */
1332 unsigned long bit = 1UL << i;
1333
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001334 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001335 continue;
1336 module_put(subsys[i]->module);
1337 }
1338 return -ENOENT;
1339 }
1340
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 return 0;
1342}
1343
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001344static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001345{
1346 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001347 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001348 unsigned long bit = 1UL << i;
1349
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001350 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001351 continue;
1352 module_put(subsys[i]->module);
1353 }
1354}
1355
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1357{
1358 int ret = 0;
1359 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001360 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363
Paul Menagebd89aab2007-10-18 23:40:44 -07001364 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001366 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367
1368 /* See what subsystems are wanted */
1369 ret = parse_cgroupfs_options(data, &opts);
1370 if (ret)
1371 goto out_unlock;
1372
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001373 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001374 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1375 task_tgid_nr(current), current->comm);
1376
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001377 added_mask = opts.subsys_mask & ~root->subsys_mask;
1378 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001379
Ben Blumcf5d5942010-03-10 15:22:09 -08001380 /* Don't allow flags or name to change at remount */
1381 if (opts.flags != root->flags ||
1382 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001384 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001385 goto out_unlock;
1386 }
1387
Gao feng7083d032012-12-03 09:28:18 +08001388 /*
1389 * Clear out the files of subsystems that should be removed, do
1390 * this before rebind_subsystems, since rebind_subsystems may
1391 * change this hierarchy's subsys_list.
1392 */
1393 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1394
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001395 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001396 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001397 /* rebind_subsystems failed, re-populate the removed files */
1398 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001399 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001400 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001401 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001403 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001404 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001405
Paul Menage81a6a5c2007-10-18 23:39:38 -07001406 if (opts.release_agent)
1407 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001408 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001409 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001410 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001411 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001412 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001413 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 return ret;
1415}
1416
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001417static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418 .statfs = simple_statfs,
1419 .drop_inode = generic_delete_inode,
1420 .show_options = cgroup_show_options,
1421 .remount_fs = cgroup_remount,
1422};
1423
Paul Menagecc31edc2008-10-18 20:28:04 -07001424static void init_cgroup_housekeeping(struct cgroup *cgrp)
1425{
1426 INIT_LIST_HEAD(&cgrp->sibling);
1427 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001428 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001429 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001430 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001431 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001432 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001433 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001434 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001435 INIT_LIST_HEAD(&cgrp->event_list);
1436 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001437 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001438}
Paul Menagec6d57f32009-09-23 15:56:19 -07001439
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440static void init_cgroup_root(struct cgroupfs_root *root)
1441{
Paul Menagebd89aab2007-10-18 23:40:44 -07001442 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001443
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444 INIT_LIST_HEAD(&root->subsys_list);
1445 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001446 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001448 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001449 cgrp->name = &root_cgroup_name;
Paul Menagebd89aab2007-10-18 23:40:44 -07001450 cgrp->top_cgroup = cgrp;
Paul Menagecc31edc2008-10-18 20:28:04 -07001451 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001452 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453}
1454
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001455static bool init_root_id(struct cgroupfs_root *root)
1456{
1457 int ret = 0;
1458
1459 do {
1460 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1461 return false;
1462 spin_lock(&hierarchy_id_lock);
1463 /* Try to allocate the next unused ID */
1464 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1465 &root->hierarchy_id);
1466 if (ret == -ENOSPC)
1467 /* Try again starting from 0 */
1468 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1469 if (!ret) {
1470 next_hierarchy_id = root->hierarchy_id + 1;
1471 } else if (ret != -EAGAIN) {
1472 /* Can only get here if the 31-bit IDR is full ... */
1473 BUG_ON(ret);
1474 }
1475 spin_unlock(&hierarchy_id_lock);
1476 } while (ret);
1477 return true;
1478}
1479
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480static int cgroup_test_super(struct super_block *sb, void *data)
1481{
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483 struct cgroupfs_root *root = sb->s_fs_info;
1484
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 /* If we asked for a name then it must match */
1486 if (opts->name && strcmp(opts->name, root->name))
1487 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001489 /*
1490 * If we asked for subsystems (or explicitly for no
1491 * subsystems) then they must match
1492 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001493 if ((opts->subsys_mask || opts->none)
1494 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495 return 0;
1496
1497 return 1;
1498}
1499
Paul Menagec6d57f32009-09-23 15:56:19 -07001500static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1501{
1502 struct cgroupfs_root *root;
1503
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001504 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 return NULL;
1506
1507 root = kzalloc(sizeof(*root), GFP_KERNEL);
1508 if (!root)
1509 return ERR_PTR(-ENOMEM);
1510
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511 if (!init_root_id(root)) {
1512 kfree(root);
1513 return ERR_PTR(-ENOMEM);
1514 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001516
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001517 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001519 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001520 if (opts->release_agent)
1521 strcpy(root->release_agent_path, opts->release_agent);
1522 if (opts->name)
1523 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001524 if (opts->cpuset_clone_children)
1525 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001526 return root;
1527}
1528
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001529static void cgroup_drop_root(struct cgroupfs_root *root)
1530{
1531 if (!root)
1532 return;
1533
1534 BUG_ON(!root->hierarchy_id);
1535 spin_lock(&hierarchy_id_lock);
1536 ida_remove(&hierarchy_ida, root->hierarchy_id);
1537 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001538 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001539 kfree(root);
1540}
1541
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542static int cgroup_set_super(struct super_block *sb, void *data)
1543{
1544 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001545 struct cgroup_sb_opts *opts = data;
1546
1547 /* If we don't have a new root, we can't set up a new sb */
1548 if (!opts->new_root)
1549 return -EINVAL;
1550
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001551 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
1553 ret = set_anon_super(sb, NULL);
1554 if (ret)
1555 return ret;
1556
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 sb->s_fs_info = opts->new_root;
1558 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 sb->s_blocksize = PAGE_CACHE_SIZE;
1561 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1562 sb->s_magic = CGROUP_SUPER_MAGIC;
1563 sb->s_op = &cgroup_ops;
1564
1565 return 0;
1566}
1567
1568static int cgroup_get_rootdir(struct super_block *sb)
1569{
Al Viro0df6a632010-12-21 13:29:29 -05001570 static const struct dentry_operations cgroup_dops = {
1571 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001572 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001573 };
1574
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 struct inode *inode =
1576 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
1578 if (!inode)
1579 return -ENOMEM;
1580
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581 inode->i_fop = &simple_dir_operations;
1582 inode->i_op = &cgroup_dir_inode_operations;
1583 /* directories start off with i_nlink == 2 (for "." entry) */
1584 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001585 sb->s_root = d_make_root(inode);
1586 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001588 /* for everything else we want ->d_op set */
1589 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 return 0;
1591}
1592
Al Virof7e83572010-07-26 13:23:11 +04001593static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001595 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596{
1597 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 int ret = 0;
1600 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001602 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603
1604 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001605 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001607 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 if (ret)
1609 goto out_err;
1610
1611 /*
1612 * Allocate a new cgroup root. We may not need it if we're
1613 * reusing an existing hierarchy.
1614 */
1615 new_root = cgroup_root_from_opts(&opts);
1616 if (IS_ERR(new_root)) {
1617 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001618 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001619 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001620 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Paul Menagec6d57f32009-09-23 15:56:19 -07001622 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001623 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001625 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001626 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001627 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 }
1629
Paul Menagec6d57f32009-09-23 15:56:19 -07001630 root = sb->s_fs_info;
1631 BUG_ON(!root);
1632 if (root == opts.new_root) {
1633 /* We used the new root structure, so this is a new hierarchy */
1634 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001635 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001636 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001637 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001638 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001639 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
1641 BUG_ON(sb->s_root != NULL);
1642
1643 ret = cgroup_get_rootdir(sb);
1644 if (ret)
1645 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001646 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647
Paul Menage817929e2007-10-18 23:39:36 -07001648 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001650 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001652 /* Check for name clashes with existing mounts */
1653 ret = -EBUSY;
1654 if (strlen(root->name))
1655 for_each_active_root(existing_root)
1656 if (!strcmp(existing_root->name, root->name))
1657 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001658
Paul Menage817929e2007-10-18 23:39:36 -07001659 /*
1660 * We're accessing css_set_count without locking
1661 * css_set_lock here, but that's OK - it can only be
1662 * increased by someone holding cgroup_lock, and
1663 * that's us. The worst that can happen is that we
1664 * have some link structures left over
1665 */
1666 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001667 if (ret)
1668 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001669
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001670 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001672 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001673 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001675 /*
1676 * There must be no failure case after here, since rebinding
1677 * takes care of subsystems' refcounts, which are explicitly
1678 * dropped in the failure exit path.
1679 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680
1681 /* EBUSY should be the only error here */
1682 BUG_ON(ret);
1683
1684 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001685 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686
Li Zefanc12f65d2009-01-07 18:07:42 -08001687 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 root->top_cgroup.dentry = sb->s_root;
1689
Paul Menage817929e2007-10-18 23:39:36 -07001690 /* Link the top cgroup in this hierarchy into all
1691 * the css_set objects */
1692 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001693 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001694 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001695 write_unlock(&css_set_lock);
1696
1697 free_cg_links(&tmp_cg_links);
1698
Li Zefanc12f65d2009-01-07 18:07:42 -08001699 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 BUG_ON(root->number_of_cgroups != 1);
1701
eparis@redhat2ce97382011-06-02 21:20:51 +10001702 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001703 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001704 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001705 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001707 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001708 } else {
1709 /*
1710 * We re-used an existing hierarchy - the new root (if
1711 * any) is not needed
1712 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001713 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001714 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001715 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716 }
1717
Paul Menagec6d57f32009-09-23 15:56:19 -07001718 kfree(opts.release_agent);
1719 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001720 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001722 unlock_drop:
1723 mutex_unlock(&cgroup_root_mutex);
1724 mutex_unlock(&cgroup_mutex);
1725 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001727 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001728 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001729 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001730 out_err:
1731 kfree(opts.release_agent);
1732 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001733 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734}
1735
1736static void cgroup_kill_sb(struct super_block *sb) {
1737 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001738 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001740 struct cg_cgroup_link *link;
1741 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742
1743 BUG_ON(!root);
1744
1745 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001746 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747
1748 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001749 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750
1751 /* Rebind all subsystems back to the default hierarchy */
1752 ret = rebind_subsystems(root, 0);
1753 /* Shouldn't be able to fail ... */
1754 BUG_ON(ret);
1755
Paul Menage817929e2007-10-18 23:39:36 -07001756 /*
1757 * Release all the links from css_sets to this hierarchy's
1758 * root cgroup
1759 */
1760 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001761
1762 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1763 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001764 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001765 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001766 kfree(link);
1767 }
1768 write_unlock(&css_set_lock);
1769
Paul Menage839ec542009-01-29 14:25:22 -08001770 if (!list_empty(&root->root_list)) {
1771 list_del(&root->root_list);
1772 root_count--;
1773 }
Li Zefane5f6a862009-01-07 18:07:41 -08001774
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001775 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 mutex_unlock(&cgroup_mutex);
1777
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001778 simple_xattrs_free(&cgrp->xattrs);
1779
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001781 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782}
1783
1784static struct file_system_type cgroup_fs_type = {
1785 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001786 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 .kill_sb = cgroup_kill_sb,
1788};
1789
Greg KH676db4a2010-08-05 13:53:35 -07001790static struct kobject *cgroup_kobj;
1791
Li Zefana043e3b2008-02-23 15:24:09 -08001792/**
1793 * cgroup_path - generate the path of a cgroup
1794 * @cgrp: the cgroup in question
1795 * @buf: the buffer to write the path into
1796 * @buflen: the length of the buffer
1797 *
Li Zefan65dff752013-03-01 15:01:56 +08001798 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1799 *
1800 * We can't generate cgroup path using dentry->d_name, as accessing
1801 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1802 * inode's i_mutex, while on the other hand cgroup_path() can be called
1803 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001805int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806{
Li Zefan65dff752013-03-01 15:01:56 +08001807 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001809
Tao Ma316eb662012-11-08 21:36:38 +08001810 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001811 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001812
Li Zefan65dff752013-03-01 15:01:56 +08001813 rcu_read_lock();
1814 while (cgrp) {
1815 const char *name = cgroup_name(cgrp);
1816 int len;
1817
1818 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001819 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001820 goto out;
1821 memcpy(start, name, len);
1822
1823 if (!cgrp->parent)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824 break;
Li Zefan9a9686b2010-04-22 17:29:24 +08001825
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001827 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001829
1830 cgrp = cgrp->parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831 }
Li Zefan65dff752013-03-01 15:01:56 +08001832 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001834out:
1835 rcu_read_unlock();
1836 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001837}
Ben Blum67523c42010-03-10 15:22:11 -08001838EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839
Ben Blum74a11662011-05-26 16:25:20 -07001840/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001841 * Control Group taskset
1842 */
Tejun Heo134d3372011-12-12 18:12:21 -08001843struct task_and_cgroup {
1844 struct task_struct *task;
1845 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001846 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001847};
1848
Tejun Heo2f7ee562011-12-12 18:12:21 -08001849struct cgroup_taskset {
1850 struct task_and_cgroup single;
1851 struct flex_array *tc_array;
1852 int tc_array_len;
1853 int idx;
1854 struct cgroup *cur_cgrp;
1855};
1856
1857/**
1858 * cgroup_taskset_first - reset taskset and return the first task
1859 * @tset: taskset of interest
1860 *
1861 * @tset iteration is initialized and the first task is returned.
1862 */
1863struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1864{
1865 if (tset->tc_array) {
1866 tset->idx = 0;
1867 return cgroup_taskset_next(tset);
1868 } else {
1869 tset->cur_cgrp = tset->single.cgrp;
1870 return tset->single.task;
1871 }
1872}
1873EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1874
1875/**
1876 * cgroup_taskset_next - iterate to the next task in taskset
1877 * @tset: taskset of interest
1878 *
1879 * Return the next task in @tset. Iteration must have been initialized
1880 * with cgroup_taskset_first().
1881 */
1882struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1883{
1884 struct task_and_cgroup *tc;
1885
1886 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1887 return NULL;
1888
1889 tc = flex_array_get(tset->tc_array, tset->idx++);
1890 tset->cur_cgrp = tc->cgrp;
1891 return tc->task;
1892}
1893EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1894
1895/**
1896 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1897 * @tset: taskset of interest
1898 *
1899 * Return the cgroup for the current (last returned) task of @tset. This
1900 * function must be preceded by either cgroup_taskset_first() or
1901 * cgroup_taskset_next().
1902 */
1903struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1904{
1905 return tset->cur_cgrp;
1906}
1907EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1908
1909/**
1910 * cgroup_taskset_size - return the number of tasks in taskset
1911 * @tset: taskset of interest
1912 */
1913int cgroup_taskset_size(struct cgroup_taskset *tset)
1914{
1915 return tset->tc_array ? tset->tc_array_len : 1;
1916}
1917EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1918
1919
Ben Blum74a11662011-05-26 16:25:20 -07001920/*
1921 * cgroup_task_migrate - move a task from one cgroup to another.
1922 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001923 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001924 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001925static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001926 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001927{
1928 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001929
1930 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001931 * We are synchronized through threadgroup_lock() against PF_EXITING
1932 * setting such that we can't race against cgroup_exit() changing the
1933 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001934 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001935 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001936 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001937
Ben Blum74a11662011-05-26 16:25:20 -07001938 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001939 rcu_assign_pointer(tsk->cgroups, newcg);
1940 task_unlock(tsk);
1941
1942 /* Update the css_set linked lists if we're using them */
1943 write_lock(&css_set_lock);
1944 if (!list_empty(&tsk->cg_list))
1945 list_move(&tsk->cg_list, &newcg->tasks);
1946 write_unlock(&css_set_lock);
1947
1948 /*
1949 * We just gained a reference on oldcg by taking it from the task. As
1950 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1951 * it here; it will be freed under RCU.
1952 */
Ben Blum74a11662011-05-26 16:25:20 -07001953 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001954 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001955}
1956
Li Zefana043e3b2008-02-23 15:24:09 -08001957/**
Li Zefan081aa452013-03-13 09:17:09 +08001958 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001959 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001960 * @tsk: the task or the leader of the threadgroup to be attached
1961 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001962 *
Tejun Heo257058a2011-12-12 18:12:21 -08001963 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001964 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001965 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001966static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1967 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001968{
1969 int retval, i, group_size;
1970 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001971 struct cgroupfs_root *root = cgrp->root;
1972 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001973 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001974 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001975 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001976 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001977
1978 /*
1979 * step 0: in order to do expensive, possibly blocking operations for
1980 * every thread, we cannot iterate the thread group list, since it needs
1981 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001982 * group - group_rwsem prevents new threads from appearing, and if
1983 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001984 */
Li Zefan081aa452013-03-13 09:17:09 +08001985 if (threadgroup)
1986 group_size = get_nr_threads(tsk);
1987 else
1988 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001989 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001990 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001991 if (!group)
1992 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001993 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001994 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001995 if (retval)
1996 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001997
Ben Blum74a11662011-05-26 16:25:20 -07001998 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001999 /*
2000 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2001 * already PF_EXITING could be freed from underneath us unless we
2002 * take an rcu_read_lock.
2003 */
2004 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002005 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002006 struct task_and_cgroup ent;
2007
Tejun Heocd3d0952011-12-12 18:12:21 -08002008 /* @tsk either already exited or can't exit until the end */
2009 if (tsk->flags & PF_EXITING)
2010 continue;
2011
Ben Blum74a11662011-05-26 16:25:20 -07002012 /* as per above, nr_threads may decrease, but not increase. */
2013 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002014 ent.task = tsk;
2015 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002016 /* nothing to do if this task is already in the cgroup */
2017 if (ent.cgrp == cgrp)
2018 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002019 /*
2020 * saying GFP_ATOMIC has no effect here because we did prealloc
2021 * earlier, but it's good form to communicate our expectations.
2022 */
Tejun Heo134d3372011-12-12 18:12:21 -08002023 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002024 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002025 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002026
2027 if (!threadgroup)
2028 break;
Ben Blum74a11662011-05-26 16:25:20 -07002029 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002030 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002031 /* remember the number of threads in the array for later. */
2032 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002033 tset.tc_array = group;
2034 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002035
Tejun Heo134d3372011-12-12 18:12:21 -08002036 /* methods shouldn't be called if no task is actually migrating */
2037 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002038 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002039 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002040
Ben Blum74a11662011-05-26 16:25:20 -07002041 /*
2042 * step 1: check that we can legitimately attach to the cgroup.
2043 */
2044 for_each_subsys(root, ss) {
2045 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002046 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002047 if (retval) {
2048 failed_ss = ss;
2049 goto out_cancel_attach;
2050 }
2051 }
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053
2054 /*
2055 * step 2: make sure css_sets exist for all threads to be migrated.
2056 * we use find_css_set, which allocates a new one if necessary.
2057 */
Ben Blum74a11662011-05-26 16:25:20 -07002058 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002059 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002060 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2061 if (!tc->cg) {
2062 retval = -ENOMEM;
2063 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002064 }
2065 }
2066
2067 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002068 * step 3: now that we're guaranteed success wrt the css_sets,
2069 * proceed to move all tasks to the new cgroup. There are no
2070 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002071 */
Ben Blum74a11662011-05-26 16:25:20 -07002072 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002073 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002074 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002075 }
2076 /* nothing is sensitive to fork() after this point. */
2077
2078 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002079 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002080 */
2081 for_each_subsys(root, ss) {
2082 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002083 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085
2086 /*
2087 * step 5: success! and cleanup
2088 */
Ben Blum74a11662011-05-26 16:25:20 -07002089 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002090out_put_css_set_refs:
2091 if (retval) {
2092 for (i = 0; i < group_size; i++) {
2093 tc = flex_array_get(group, i);
2094 if (!tc->cg)
2095 break;
2096 put_css_set(tc->cg);
2097 }
Ben Blum74a11662011-05-26 16:25:20 -07002098 }
2099out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (retval) {
2101 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002102 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002103 break;
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002105 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107 }
Ben Blum74a11662011-05-26 16:25:20 -07002108out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002109 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002110 return retval;
2111}
2112
2113/*
2114 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002115 * function to attach either it or all tasks in its threadgroup. Will lock
2116 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002117 */
2118static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002121 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 int ret;
2123
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (!cgroup_lock_live_group(cgrp))
2125 return -ENODEV;
2126
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002127retry_find_task:
2128 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002130 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002131 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 ret= -ESRCH;
2134 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136 /*
2137 * even if we're attaching all tasks in the thread group, we
2138 * only need to check permissions on one of them.
2139 */
David Howellsc69e8d92008-11-14 10:39:19 +11002140 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002141 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2142 !uid_eq(cred->euid, tcred->uid) &&
2143 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002144 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002145 ret = -EACCES;
2146 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002147 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 } else
2149 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002150
2151 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002153
2154 /*
2155 * Workqueue threads may acquire PF_THREAD_BOUND and become
2156 * trapped in a cpuset, or RT worker may be born in a cgroup
2157 * with no rt_runtime allocated. Just say no.
2158 */
2159 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2160 ret = -EINVAL;
2161 rcu_read_unlock();
2162 goto out_unlock_cgroup;
2163 }
2164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 get_task_struct(tsk);
2166 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 threadgroup_lock(tsk);
2169 if (threadgroup) {
2170 if (!thread_group_leader(tsk)) {
2171 /*
2172 * a race with de_thread from another thread's exec()
2173 * may strip us of our leadership, if this happens,
2174 * there is no choice but to throw this task away and
2175 * try again; this is
2176 * "double-double-toil-and-trouble-check locking".
2177 */
2178 threadgroup_unlock(tsk);
2179 put_task_struct(tsk);
2180 goto retry_find_task;
2181 }
Li Zefan081aa452013-03-13 09:17:09 +08002182 }
2183
2184 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2185
Tejun Heocd3d0952011-12-12 18:12:21 -08002186 threadgroup_unlock(tsk);
2187
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002190 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 return ret;
2192}
2193
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002194/**
2195 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2196 * @from: attach to all cgroups of a given task
2197 * @tsk: the task to be attached
2198 */
2199int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2200{
2201 struct cgroupfs_root *root;
2202 int retval = 0;
2203
Tejun Heo47cfcd02013-04-07 09:29:51 -07002204 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002205 for_each_active_root(root) {
2206 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2207
2208 retval = cgroup_attach_task(from_cg, tsk, false);
2209 if (retval)
2210 break;
2211 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213
2214 return retval;
2215}
2216EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2217
Paul Menageaf351022008-07-25 01:47:01 -07002218static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2219{
Ben Blum74a11662011-05-26 16:25:20 -07002220 return attach_task_by_pid(cgrp, pid, false);
2221}
2222
2223static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2224{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002225 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002226}
2227
Paul Menagee788e062008-07-25 01:46:59 -07002228static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2229 const char *buffer)
2230{
2231 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002232 if (strlen(buffer) >= PATH_MAX)
2233 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002234 if (!cgroup_lock_live_group(cgrp))
2235 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002236 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002237 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002238 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002239 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002240 return 0;
2241}
2242
2243static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2244 struct seq_file *seq)
2245{
2246 if (!cgroup_lock_live_group(cgrp))
2247 return -ENODEV;
2248 seq_puts(seq, cgrp->root->release_agent_path);
2249 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002250 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002251 return 0;
2252}
2253
Paul Menage84eea842008-07-25 01:47:00 -07002254/* A buffer size big enough for numbers or short strings */
2255#define CGROUP_LOCAL_BUFFER_SIZE 64
2256
Paul Menagee73d2c62008-04-29 01:00:06 -07002257static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002258 struct file *file,
2259 const char __user *userbuf,
2260 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002261{
Paul Menage84eea842008-07-25 01:47:00 -07002262 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002263 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002264 char *end;
2265
2266 if (!nbytes)
2267 return -EINVAL;
2268 if (nbytes >= sizeof(buffer))
2269 return -E2BIG;
2270 if (copy_from_user(buffer, userbuf, nbytes))
2271 return -EFAULT;
2272
2273 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002274 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002275 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002276 if (*end)
2277 return -EINVAL;
2278 retval = cft->write_u64(cgrp, cft, val);
2279 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002280 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002281 if (*end)
2282 return -EINVAL;
2283 retval = cft->write_s64(cgrp, cft, val);
2284 }
Paul Menage355e0c42007-10-18 23:39:33 -07002285 if (!retval)
2286 retval = nbytes;
2287 return retval;
2288}
2289
Paul Menagedb3b1492008-07-25 01:46:58 -07002290static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2291 struct file *file,
2292 const char __user *userbuf,
2293 size_t nbytes, loff_t *unused_ppos)
2294{
Paul Menage84eea842008-07-25 01:47:00 -07002295 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002296 int retval = 0;
2297 size_t max_bytes = cft->max_write_len;
2298 char *buffer = local_buffer;
2299
2300 if (!max_bytes)
2301 max_bytes = sizeof(local_buffer) - 1;
2302 if (nbytes >= max_bytes)
2303 return -E2BIG;
2304 /* Allocate a dynamic buffer if we need one */
2305 if (nbytes >= sizeof(local_buffer)) {
2306 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2307 if (buffer == NULL)
2308 return -ENOMEM;
2309 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002310 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2311 retval = -EFAULT;
2312 goto out;
2313 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002314
2315 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002316 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002317 if (!retval)
2318 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002319out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002320 if (buffer != local_buffer)
2321 kfree(buffer);
2322 return retval;
2323}
2324
Paul Menageddbcc7e2007-10-18 23:39:30 -07002325static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2326 size_t nbytes, loff_t *ppos)
2327{
2328 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002329 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002330
Li Zefan75139b82009-01-07 18:07:33 -08002331 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002332 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002333 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002334 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002335 if (cft->write_u64 || cft->write_s64)
2336 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002337 if (cft->write_string)
2338 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002339 if (cft->trigger) {
2340 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2341 return ret ? ret : nbytes;
2342 }
Paul Menage355e0c42007-10-18 23:39:33 -07002343 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344}
2345
Paul Menagef4c753b2008-04-29 00:59:56 -07002346static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2347 struct file *file,
2348 char __user *buf, size_t nbytes,
2349 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002350{
Paul Menage84eea842008-07-25 01:47:00 -07002351 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002352 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2354
2355 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2356}
2357
Paul Menagee73d2c62008-04-29 01:00:06 -07002358static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2359 struct file *file,
2360 char __user *buf, size_t nbytes,
2361 loff_t *ppos)
2362{
Paul Menage84eea842008-07-25 01:47:00 -07002363 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002364 s64 val = cft->read_s64(cgrp, cft);
2365 int len = sprintf(tmp, "%lld\n", (long long) val);
2366
2367 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2368}
2369
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2371 size_t nbytes, loff_t *ppos)
2372{
2373 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002374 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375
Li Zefan75139b82009-01-07 18:07:33 -08002376 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 return -ENODEV;
2378
2379 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002380 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002381 if (cft->read_u64)
2382 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002383 if (cft->read_s64)
2384 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385 return -EINVAL;
2386}
2387
Paul Menage91796562008-04-29 01:00:01 -07002388/*
2389 * seqfile ops/methods for returning structured data. Currently just
2390 * supports string->u64 maps, but can be extended in future.
2391 */
2392
2393struct cgroup_seqfile_state {
2394 struct cftype *cft;
2395 struct cgroup *cgroup;
2396};
2397
2398static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2399{
2400 struct seq_file *sf = cb->state;
2401 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2402}
2403
2404static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2405{
2406 struct cgroup_seqfile_state *state = m->private;
2407 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002408 if (cft->read_map) {
2409 struct cgroup_map_cb cb = {
2410 .fill = cgroup_map_add,
2411 .state = m,
2412 };
2413 return cft->read_map(state->cgroup, cft, &cb);
2414 }
2415 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002416}
2417
Adrian Bunk96930a62008-07-25 19:46:21 -07002418static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002419{
2420 struct seq_file *seq = file->private_data;
2421 kfree(seq->private);
2422 return single_release(inode, file);
2423}
2424
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002425static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002426 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002427 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002428 .llseek = seq_lseek,
2429 .release = cgroup_seqfile_release,
2430};
2431
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432static int cgroup_file_open(struct inode *inode, struct file *file)
2433{
2434 int err;
2435 struct cftype *cft;
2436
2437 err = generic_file_open(inode, file);
2438 if (err)
2439 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002441
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002442 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002443 struct cgroup_seqfile_state *state =
2444 kzalloc(sizeof(*state), GFP_USER);
2445 if (!state)
2446 return -ENOMEM;
2447 state->cft = cft;
2448 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2449 file->f_op = &cgroup_seqfile_operations;
2450 err = single_open(file, cgroup_seqfile_show, state);
2451 if (err < 0)
2452 kfree(state);
2453 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002454 err = cft->open(inode, file);
2455 else
2456 err = 0;
2457
2458 return err;
2459}
2460
2461static int cgroup_file_release(struct inode *inode, struct file *file)
2462{
2463 struct cftype *cft = __d_cft(file->f_dentry);
2464 if (cft->release)
2465 return cft->release(inode, file);
2466 return 0;
2467}
2468
2469/*
2470 * cgroup_rename - Only allow simple rename of directories in place.
2471 */
2472static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2473 struct inode *new_dir, struct dentry *new_dentry)
2474{
Li Zefan65dff752013-03-01 15:01:56 +08002475 int ret;
2476 struct cgroup_name *name, *old_name;
2477 struct cgroup *cgrp;
2478
2479 /*
2480 * It's convinient to use parent dir's i_mutex to protected
2481 * cgrp->name.
2482 */
2483 lockdep_assert_held(&old_dir->i_mutex);
2484
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2486 return -ENOTDIR;
2487 if (new_dentry->d_inode)
2488 return -EEXIST;
2489 if (old_dir != new_dir)
2490 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002491
2492 cgrp = __d_cgrp(old_dentry);
2493
2494 name = cgroup_alloc_name(new_dentry);
2495 if (!name)
2496 return -ENOMEM;
2497
2498 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2499 if (ret) {
2500 kfree(name);
2501 return ret;
2502 }
2503
2504 old_name = cgrp->name;
2505 rcu_assign_pointer(cgrp->name, name);
2506
2507 kfree_rcu(old_name, rcu_head);
2508 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509}
2510
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002511static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2512{
2513 if (S_ISDIR(dentry->d_inode->i_mode))
2514 return &__d_cgrp(dentry)->xattrs;
2515 else
2516 return &__d_cft(dentry)->xattrs;
2517}
2518
2519static inline int xattr_enabled(struct dentry *dentry)
2520{
2521 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
2522 return test_bit(ROOT_XATTR, &root->flags);
2523}
2524
2525static bool is_valid_xattr(const char *name)
2526{
2527 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2528 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2529 return true;
2530 return false;
2531}
2532
2533static int cgroup_setxattr(struct dentry *dentry, const char *name,
2534 const void *val, size_t size, int flags)
2535{
2536 if (!xattr_enabled(dentry))
2537 return -EOPNOTSUPP;
2538 if (!is_valid_xattr(name))
2539 return -EINVAL;
2540 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2541}
2542
2543static int cgroup_removexattr(struct dentry *dentry, const char *name)
2544{
2545 if (!xattr_enabled(dentry))
2546 return -EOPNOTSUPP;
2547 if (!is_valid_xattr(name))
2548 return -EINVAL;
2549 return simple_xattr_remove(__d_xattrs(dentry), name);
2550}
2551
2552static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2553 void *buf, size_t size)
2554{
2555 if (!xattr_enabled(dentry))
2556 return -EOPNOTSUPP;
2557 if (!is_valid_xattr(name))
2558 return -EINVAL;
2559 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2560}
2561
2562static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2563{
2564 if (!xattr_enabled(dentry))
2565 return -EOPNOTSUPP;
2566 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2567}
2568
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002569static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570 .read = cgroup_file_read,
2571 .write = cgroup_file_write,
2572 .llseek = generic_file_llseek,
2573 .open = cgroup_file_open,
2574 .release = cgroup_file_release,
2575};
2576
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002577static const struct inode_operations cgroup_file_inode_operations = {
2578 .setxattr = cgroup_setxattr,
2579 .getxattr = cgroup_getxattr,
2580 .listxattr = cgroup_listxattr,
2581 .removexattr = cgroup_removexattr,
2582};
2583
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002584static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002585 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586 .mkdir = cgroup_mkdir,
2587 .rmdir = cgroup_rmdir,
2588 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002589 .setxattr = cgroup_setxattr,
2590 .getxattr = cgroup_getxattr,
2591 .listxattr = cgroup_listxattr,
2592 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002593};
2594
Al Viro00cd8dd2012-06-10 17:13:09 -04002595static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002596{
2597 if (dentry->d_name.len > NAME_MAX)
2598 return ERR_PTR(-ENAMETOOLONG);
2599 d_add(dentry, NULL);
2600 return NULL;
2601}
2602
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002603/*
2604 * Check if a file is a control file
2605 */
2606static inline struct cftype *__file_cft(struct file *file)
2607{
Al Viro496ad9a2013-01-23 17:07:38 -05002608 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002609 return ERR_PTR(-EINVAL);
2610 return __d_cft(file->f_dentry);
2611}
2612
Al Viroa5e7ed32011-07-26 01:55:55 -04002613static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002614 struct super_block *sb)
2615{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 struct inode *inode;
2617
2618 if (!dentry)
2619 return -ENOENT;
2620 if (dentry->d_inode)
2621 return -EEXIST;
2622
2623 inode = cgroup_new_inode(mode, sb);
2624 if (!inode)
2625 return -ENOMEM;
2626
2627 if (S_ISDIR(mode)) {
2628 inode->i_op = &cgroup_dir_inode_operations;
2629 inode->i_fop = &simple_dir_operations;
2630
2631 /* start off with i_nlink == 2 (for "." entry) */
2632 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002633 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634
Tejun Heob8a2df62012-11-19 08:13:37 -08002635 /*
2636 * Control reaches here with cgroup_mutex held.
2637 * @inode->i_mutex should nest outside cgroup_mutex but we
2638 * want to populate it immediately without releasing
2639 * cgroup_mutex. As @inode isn't visible to anyone else
2640 * yet, trylock will always succeed without affecting
2641 * lockdep checks.
2642 */
2643 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002644 } else if (S_ISREG(mode)) {
2645 inode->i_size = 0;
2646 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002647 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 d_instantiate(dentry, inode);
2650 dget(dentry); /* Extra count - pin the dentry in core */
2651 return 0;
2652}
2653
Li Zefan099fca32009-04-02 16:57:29 -07002654/**
2655 * cgroup_file_mode - deduce file mode of a control file
2656 * @cft: the control file in question
2657 *
2658 * returns cft->mode if ->mode is not 0
2659 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2660 * returns S_IRUGO if it has only a read handler
2661 * returns S_IWUSR if it has only a write hander
2662 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002663static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002664{
Al Viroa5e7ed32011-07-26 01:55:55 -04002665 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002666
2667 if (cft->mode)
2668 return cft->mode;
2669
2670 if (cft->read || cft->read_u64 || cft->read_s64 ||
2671 cft->read_map || cft->read_seq_string)
2672 mode |= S_IRUGO;
2673
2674 if (cft->write || cft->write_u64 || cft->write_s64 ||
2675 cft->write_string || cft->trigger)
2676 mode |= S_IWUSR;
2677
2678 return mode;
2679}
2680
Tejun Heodb0416b2012-04-01 12:09:55 -07002681static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002682 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683{
Paul Menagebd89aab2007-10-18 23:40:44 -07002684 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002685 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002687 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002689 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002691
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002692 simple_xattrs_init(&cft->xattrs);
2693
Paul Menagebd89aab2007-10-18 23:40:44 -07002694 if (subsys && !test_bit(ROOT_NOPREFIX, &cgrp->root->flags)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695 strcpy(name, subsys->name);
2696 strcat(name, ".");
2697 }
2698 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002699
Paul Menageddbcc7e2007-10-18 23:39:30 -07002700 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002701
2702 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2703 if (!cfe)
2704 return -ENOMEM;
2705
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002707 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002709 goto out;
2710 }
2711
2712 mode = cgroup_file_mode(cft);
2713 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2714 if (!error) {
2715 cfe->type = (void *)cft;
2716 cfe->dentry = dentry;
2717 dentry->d_fsdata = cfe;
2718 list_add_tail(&cfe->node, &parent->files);
2719 cfe = NULL;
2720 }
2721 dput(dentry);
2722out:
2723 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 return error;
2725}
2726
Tejun Heo79578622012-04-01 12:09:56 -07002727static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002728 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002730 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002731 int err, ret = 0;
2732
2733 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002734 /* does cft->flags tell us to skip this file on @cgrp? */
2735 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2736 continue;
2737 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2738 continue;
2739
Li Zefan2739d3c2013-01-21 18:18:33 +08002740 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002741 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002742 if (err)
2743 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2744 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002745 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002746 } else {
2747 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002748 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002749 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002750 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002751}
2752
Tejun Heo8e3f6542012-04-01 12:09:55 -07002753static DEFINE_MUTEX(cgroup_cft_mutex);
2754
2755static void cgroup_cfts_prepare(void)
2756 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2757{
2758 /*
2759 * Thanks to the entanglement with vfs inode locking, we can't walk
2760 * the existing cgroups under cgroup_mutex and create files.
2761 * Instead, we increment reference on all cgroups and build list of
2762 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2763 * exclusive access to the field.
2764 */
2765 mutex_lock(&cgroup_cft_mutex);
2766 mutex_lock(&cgroup_mutex);
2767}
2768
2769static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002770 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002771 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2772{
2773 LIST_HEAD(pending);
2774 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002775
2776 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2777 if (cfts && ss->root != &rootnode) {
2778 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2779 dget(cgrp->dentry);
2780 list_add_tail(&cgrp->cft_q_node, &pending);
2781 }
2782 }
2783
2784 mutex_unlock(&cgroup_mutex);
2785
2786 /*
2787 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2788 * files for all cgroups which were created before.
2789 */
2790 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2791 struct inode *inode = cgrp->dentry->d_inode;
2792
2793 mutex_lock(&inode->i_mutex);
2794 mutex_lock(&cgroup_mutex);
2795 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002796 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 mutex_unlock(&cgroup_mutex);
2798 mutex_unlock(&inode->i_mutex);
2799
2800 list_del_init(&cgrp->cft_q_node);
2801 dput(cgrp->dentry);
2802 }
2803
2804 mutex_unlock(&cgroup_cft_mutex);
2805}
2806
2807/**
2808 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2809 * @ss: target cgroup subsystem
2810 * @cfts: zero-length name terminated array of cftypes
2811 *
2812 * Register @cfts to @ss. Files described by @cfts are created for all
2813 * existing cgroups to which @ss is attached and all future cgroups will
2814 * have them too. This function can be called anytime whether @ss is
2815 * attached or not.
2816 *
2817 * Returns 0 on successful registration, -errno on failure. Note that this
2818 * function currently returns 0 as long as @cfts registration is successful
2819 * even if some file creation attempts on existing cgroups fail.
2820 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002821int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002822{
2823 struct cftype_set *set;
2824
2825 set = kzalloc(sizeof(*set), GFP_KERNEL);
2826 if (!set)
2827 return -ENOMEM;
2828
2829 cgroup_cfts_prepare();
2830 set->cfts = cfts;
2831 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002832 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002833
2834 return 0;
2835}
2836EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2837
Li Zefana043e3b2008-02-23 15:24:09 -08002838/**
Tejun Heo79578622012-04-01 12:09:56 -07002839 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2840 * @ss: target cgroup subsystem
2841 * @cfts: zero-length name terminated array of cftypes
2842 *
2843 * Unregister @cfts from @ss. Files described by @cfts are removed from
2844 * all existing cgroups to which @ss is attached and all future cgroups
2845 * won't have them either. This function can be called anytime whether @ss
2846 * is attached or not.
2847 *
2848 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2849 * registered with @ss.
2850 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002851int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002852{
2853 struct cftype_set *set;
2854
2855 cgroup_cfts_prepare();
2856
2857 list_for_each_entry(set, &ss->cftsets, node) {
2858 if (set->cfts == cfts) {
2859 list_del_init(&set->node);
2860 cgroup_cfts_commit(ss, cfts, false);
2861 return 0;
2862 }
2863 }
2864
2865 cgroup_cfts_commit(ss, NULL, false);
2866 return -ENOENT;
2867}
2868
2869/**
Li Zefana043e3b2008-02-23 15:24:09 -08002870 * cgroup_task_count - count the number of tasks in a cgroup.
2871 * @cgrp: the cgroup in question
2872 *
2873 * Return the number of tasks in the cgroup.
2874 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002875int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002876{
2877 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002878 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002879
Paul Menage817929e2007-10-18 23:39:36 -07002880 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002881 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002882 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002883 }
2884 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002885 return count;
2886}
2887
2888/*
Paul Menage817929e2007-10-18 23:39:36 -07002889 * Advance a list_head iterator. The iterator should be positioned at
2890 * the start of a css_set
2891 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002892static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002893 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002894{
2895 struct list_head *l = it->cg_link;
2896 struct cg_cgroup_link *link;
2897 struct css_set *cg;
2898
2899 /* Advance to the next non-empty css_set */
2900 do {
2901 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002902 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002903 it->cg_link = NULL;
2904 return;
2905 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002906 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002907 cg = link->cg;
2908 } while (list_empty(&cg->tasks));
2909 it->cg_link = l;
2910 it->task = cg->tasks.next;
2911}
2912
Cliff Wickman31a7df02008-02-07 00:14:42 -08002913/*
2914 * To reduce the fork() overhead for systems that are not actually
2915 * using their cgroups capability, we don't maintain the lists running
2916 * through each css_set to its tasks until we see the list actually
2917 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002918 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002919static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002920{
2921 struct task_struct *p, *g;
2922 write_lock(&css_set_lock);
2923 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002924 /*
2925 * We need tasklist_lock because RCU is not safe against
2926 * while_each_thread(). Besides, a forking task that has passed
2927 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2928 * is not guaranteed to have its child immediately visible in the
2929 * tasklist if we walk through it with RCU.
2930 */
2931 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002932 do_each_thread(g, p) {
2933 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002934 /*
2935 * We should check if the process is exiting, otherwise
2936 * it will race with cgroup_exit() in that the list
2937 * entry won't be deleted though the process has exited.
2938 */
2939 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002940 list_add(&p->cg_list, &p->cgroups->tasks);
2941 task_unlock(p);
2942 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002943 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944 write_unlock(&css_set_lock);
2945}
2946
Tejun Heo574bd9f2012-11-09 09:12:29 -08002947/**
2948 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2949 * @pos: the current position (%NULL to initiate traversal)
2950 * @cgroup: cgroup whose descendants to walk
2951 *
2952 * To be used by cgroup_for_each_descendant_pre(). Find the next
2953 * descendant to visit for pre-order traversal of @cgroup's descendants.
2954 */
2955struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2956 struct cgroup *cgroup)
2957{
2958 struct cgroup *next;
2959
2960 WARN_ON_ONCE(!rcu_read_lock_held());
2961
2962 /* if first iteration, pretend we just visited @cgroup */
2963 if (!pos) {
2964 if (list_empty(&cgroup->children))
2965 return NULL;
2966 pos = cgroup;
2967 }
2968
2969 /* visit the first child if exists */
2970 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2971 if (next)
2972 return next;
2973
2974 /* no child, visit my or the closest ancestor's next sibling */
2975 do {
2976 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2977 sibling);
2978 if (&next->sibling != &pos->parent->children)
2979 return next;
2980
2981 pos = pos->parent;
2982 } while (pos != cgroup);
2983
2984 return NULL;
2985}
2986EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2987
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002988/**
2989 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2990 * @pos: cgroup of interest
2991 *
2992 * Return the rightmost descendant of @pos. If there's no descendant,
2993 * @pos is returned. This can be used during pre-order traversal to skip
2994 * subtree of @pos.
2995 */
2996struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2997{
2998 struct cgroup *last, *tmp;
2999
3000 WARN_ON_ONCE(!rcu_read_lock_held());
3001
3002 do {
3003 last = pos;
3004 /* ->prev isn't RCU safe, walk ->next till the end */
3005 pos = NULL;
3006 list_for_each_entry_rcu(tmp, &last->children, sibling)
3007 pos = tmp;
3008 } while (pos);
3009
3010 return last;
3011}
3012EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3013
Tejun Heo574bd9f2012-11-09 09:12:29 -08003014static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3015{
3016 struct cgroup *last;
3017
3018 do {
3019 last = pos;
3020 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3021 sibling);
3022 } while (pos);
3023
3024 return last;
3025}
3026
3027/**
3028 * cgroup_next_descendant_post - find the next descendant for post-order walk
3029 * @pos: the current position (%NULL to initiate traversal)
3030 * @cgroup: cgroup whose descendants to walk
3031 *
3032 * To be used by cgroup_for_each_descendant_post(). Find the next
3033 * descendant to visit for post-order traversal of @cgroup's descendants.
3034 */
3035struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3036 struct cgroup *cgroup)
3037{
3038 struct cgroup *next;
3039
3040 WARN_ON_ONCE(!rcu_read_lock_held());
3041
3042 /* if first iteration, visit the leftmost descendant */
3043 if (!pos) {
3044 next = cgroup_leftmost_descendant(cgroup);
3045 return next != cgroup ? next : NULL;
3046 }
3047
3048 /* if there's an unvisited sibling, visit its leftmost descendant */
3049 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3050 if (&next->sibling != &pos->parent->children)
3051 return cgroup_leftmost_descendant(next);
3052
3053 /* no sibling left, visit parent */
3054 next = pos->parent;
3055 return next != cgroup ? next : NULL;
3056}
3057EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3058
Paul Menagebd89aab2007-10-18 23:40:44 -07003059void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003060 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003061{
3062 /*
3063 * The first time anyone tries to iterate across a cgroup,
3064 * we need to enable the list linking each css_set to its
3065 * tasks, and fix up all existing tasks.
3066 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003067 if (!use_task_css_set_links)
3068 cgroup_enable_task_cg_lists();
3069
Paul Menage817929e2007-10-18 23:39:36 -07003070 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003071 it->cg_link = &cgrp->css_sets;
3072 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003073}
3074
Paul Menagebd89aab2007-10-18 23:40:44 -07003075struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003076 struct cgroup_iter *it)
3077{
3078 struct task_struct *res;
3079 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003080 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003081
3082 /* If the iterator cg is NULL, we have no tasks */
3083 if (!it->cg_link)
3084 return NULL;
3085 res = list_entry(l, struct task_struct, cg_list);
3086 /* Advance iterator to find next entry */
3087 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003088 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3089 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003090 /* We reached the end of this task list - move on to
3091 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003092 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003093 } else {
3094 it->task = l;
3095 }
3096 return res;
3097}
3098
Paul Menagebd89aab2007-10-18 23:40:44 -07003099void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003100 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003101{
3102 read_unlock(&css_set_lock);
3103}
3104
Cliff Wickman31a7df02008-02-07 00:14:42 -08003105static inline int started_after_time(struct task_struct *t1,
3106 struct timespec *time,
3107 struct task_struct *t2)
3108{
3109 int start_diff = timespec_compare(&t1->start_time, time);
3110 if (start_diff > 0) {
3111 return 1;
3112 } else if (start_diff < 0) {
3113 return 0;
3114 } else {
3115 /*
3116 * Arbitrarily, if two processes started at the same
3117 * time, we'll say that the lower pointer value
3118 * started first. Note that t2 may have exited by now
3119 * so this may not be a valid pointer any longer, but
3120 * that's fine - it still serves to distinguish
3121 * between two tasks started (effectively) simultaneously.
3122 */
3123 return t1 > t2;
3124 }
3125}
3126
3127/*
3128 * This function is a callback from heap_insert() and is used to order
3129 * the heap.
3130 * In this case we order the heap in descending task start time.
3131 */
3132static inline int started_after(void *p1, void *p2)
3133{
3134 struct task_struct *t1 = p1;
3135 struct task_struct *t2 = p2;
3136 return started_after_time(t1, &t2->start_time, t2);
3137}
3138
3139/**
3140 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3141 * @scan: struct cgroup_scanner containing arguments for the scan
3142 *
3143 * Arguments include pointers to callback functions test_task() and
3144 * process_task().
3145 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3146 * and if it returns true, call process_task() for it also.
3147 * The test_task pointer may be NULL, meaning always true (select all tasks).
3148 * Effectively duplicates cgroup_iter_{start,next,end}()
3149 * but does not lock css_set_lock for the call to process_task().
3150 * The struct cgroup_scanner may be embedded in any structure of the caller's
3151 * creation.
3152 * It is guaranteed that process_task() will act on every task that
3153 * is a member of the cgroup for the duration of this call. This
3154 * function may or may not call process_task() for tasks that exit
3155 * or move to a different cgroup during the call, or are forked or
3156 * move into the cgroup during the call.
3157 *
3158 * Note that test_task() may be called with locks held, and may in some
3159 * situations be called multiple times for the same task, so it should
3160 * be cheap.
3161 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3162 * pre-allocated and will be used for heap operations (and its "gt" member will
3163 * be overwritten), else a temporary heap will be used (allocation of which
3164 * may cause this function to fail).
3165 */
3166int cgroup_scan_tasks(struct cgroup_scanner *scan)
3167{
3168 int retval, i;
3169 struct cgroup_iter it;
3170 struct task_struct *p, *dropped;
3171 /* Never dereference latest_task, since it's not refcounted */
3172 struct task_struct *latest_task = NULL;
3173 struct ptr_heap tmp_heap;
3174 struct ptr_heap *heap;
3175 struct timespec latest_time = { 0, 0 };
3176
3177 if (scan->heap) {
3178 /* The caller supplied our heap and pre-allocated its memory */
3179 heap = scan->heap;
3180 heap->gt = &started_after;
3181 } else {
3182 /* We need to allocate our own heap memory */
3183 heap = &tmp_heap;
3184 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3185 if (retval)
3186 /* cannot allocate the heap */
3187 return retval;
3188 }
3189
3190 again:
3191 /*
3192 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3193 * to determine which are of interest, and using the scanner's
3194 * "process_task" callback to process any of them that need an update.
3195 * Since we don't want to hold any locks during the task updates,
3196 * gather tasks to be processed in a heap structure.
3197 * The heap is sorted by descending task start time.
3198 * If the statically-sized heap fills up, we overflow tasks that
3199 * started later, and in future iterations only consider tasks that
3200 * started after the latest task in the previous pass. This
3201 * guarantees forward progress and that we don't miss any tasks.
3202 */
3203 heap->size = 0;
3204 cgroup_iter_start(scan->cg, &it);
3205 while ((p = cgroup_iter_next(scan->cg, &it))) {
3206 /*
3207 * Only affect tasks that qualify per the caller's callback,
3208 * if he provided one
3209 */
3210 if (scan->test_task && !scan->test_task(p, scan))
3211 continue;
3212 /*
3213 * Only process tasks that started after the last task
3214 * we processed
3215 */
3216 if (!started_after_time(p, &latest_time, latest_task))
3217 continue;
3218 dropped = heap_insert(heap, p);
3219 if (dropped == NULL) {
3220 /*
3221 * The new task was inserted; the heap wasn't
3222 * previously full
3223 */
3224 get_task_struct(p);
3225 } else if (dropped != p) {
3226 /*
3227 * The new task was inserted, and pushed out a
3228 * different task
3229 */
3230 get_task_struct(p);
3231 put_task_struct(dropped);
3232 }
3233 /*
3234 * Else the new task was newer than anything already in
3235 * the heap and wasn't inserted
3236 */
3237 }
3238 cgroup_iter_end(scan->cg, &it);
3239
3240 if (heap->size) {
3241 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003242 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003243 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003244 latest_time = q->start_time;
3245 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003246 }
3247 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003248 scan->process_task(q, scan);
3249 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003250 }
3251 /*
3252 * If we had to process any tasks at all, scan again
3253 * in case some of them were in the middle of forking
3254 * children that didn't get processed.
3255 * Not the most efficient way to do it, but it avoids
3256 * having to take callback_mutex in the fork path
3257 */
3258 goto again;
3259 }
3260 if (heap == &tmp_heap)
3261 heap_free(&tmp_heap);
3262 return 0;
3263}
3264
Tejun Heo8cc99342013-04-07 09:29:50 -07003265static void cgroup_transfer_one_task(struct task_struct *task,
3266 struct cgroup_scanner *scan)
3267{
3268 struct cgroup *new_cgroup = scan->data;
3269
Tejun Heo47cfcd02013-04-07 09:29:51 -07003270 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003271 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003272 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003273}
3274
3275/**
3276 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3277 * @to: cgroup to which the tasks will be moved
3278 * @from: cgroup in which the tasks currently reside
3279 */
3280int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3281{
3282 struct cgroup_scanner scan;
3283
3284 scan.cg = from;
3285 scan.test_task = NULL; /* select all tasks in cgroup */
3286 scan.process_task = cgroup_transfer_one_task;
3287 scan.heap = NULL;
3288 scan.data = to;
3289
3290 return cgroup_scan_tasks(&scan);
3291}
3292
Paul Menage817929e2007-10-18 23:39:36 -07003293/*
Ben Blum102a7752009-09-23 15:56:26 -07003294 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003295 *
3296 * Reading this file can return large amounts of data if a cgroup has
3297 * *lots* of attached tasks. So it may need several calls to read(),
3298 * but we cannot guarantee that the information we produce is correct
3299 * unless we produce it entirely atomically.
3300 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003301 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003302
Li Zefan24528252012-01-20 11:58:43 +08003303/* which pidlist file are we talking about? */
3304enum cgroup_filetype {
3305 CGROUP_FILE_PROCS,
3306 CGROUP_FILE_TASKS,
3307};
3308
3309/*
3310 * A pidlist is a list of pids that virtually represents the contents of one
3311 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3312 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3313 * to the cgroup.
3314 */
3315struct cgroup_pidlist {
3316 /*
3317 * used to find which pidlist is wanted. doesn't change as long as
3318 * this particular list stays in the list.
3319 */
3320 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3321 /* array of xids */
3322 pid_t *list;
3323 /* how many elements the above list has */
3324 int length;
3325 /* how many files are using the current array */
3326 int use_count;
3327 /* each of these stored in a list by its cgroup */
3328 struct list_head links;
3329 /* pointer to the cgroup we belong to, for list removal purposes */
3330 struct cgroup *owner;
3331 /* protects the other fields */
3332 struct rw_semaphore mutex;
3333};
3334
Paul Menagebbcb81d2007-10-18 23:39:32 -07003335/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003336 * The following two functions "fix" the issue where there are more pids
3337 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3338 * TODO: replace with a kernel-wide solution to this problem
3339 */
3340#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3341static void *pidlist_allocate(int count)
3342{
3343 if (PIDLIST_TOO_LARGE(count))
3344 return vmalloc(count * sizeof(pid_t));
3345 else
3346 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3347}
3348static void pidlist_free(void *p)
3349{
3350 if (is_vmalloc_addr(p))
3351 vfree(p);
3352 else
3353 kfree(p);
3354}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003355
3356/*
Ben Blum102a7752009-09-23 15:56:26 -07003357 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003358 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003359 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003360static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003361{
Ben Blum102a7752009-09-23 15:56:26 -07003362 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003363
3364 /*
3365 * we presume the 0th element is unique, so i starts at 1. trivial
3366 * edge cases first; no work needs to be done for either
3367 */
3368 if (length == 0 || length == 1)
3369 return length;
3370 /* src and dest walk down the list; dest counts unique elements */
3371 for (src = 1; src < length; src++) {
3372 /* find next unique element */
3373 while (list[src] == list[src-1]) {
3374 src++;
3375 if (src == length)
3376 goto after;
3377 }
3378 /* dest always points to where the next unique element goes */
3379 list[dest] = list[src];
3380 dest++;
3381 }
3382after:
Ben Blum102a7752009-09-23 15:56:26 -07003383 return dest;
3384}
3385
3386static int cmppid(const void *a, const void *b)
3387{
3388 return *(pid_t *)a - *(pid_t *)b;
3389}
3390
3391/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003392 * find the appropriate pidlist for our purpose (given procs vs tasks)
3393 * returns with the lock on that pidlist already held, and takes care
3394 * of the use count, or returns NULL with no locks held if we're out of
3395 * memory.
3396 */
3397static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3398 enum cgroup_filetype type)
3399{
3400 struct cgroup_pidlist *l;
3401 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003402 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003403
Ben Blum72a8cb32009-09-23 15:56:27 -07003404 /*
3405 * We can't drop the pidlist_mutex before taking the l->mutex in case
3406 * the last ref-holder is trying to remove l from the list at the same
3407 * time. Holding the pidlist_mutex precludes somebody taking whichever
3408 * list we find out from under us - compare release_pid_array().
3409 */
3410 mutex_lock(&cgrp->pidlist_mutex);
3411 list_for_each_entry(l, &cgrp->pidlists, links) {
3412 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003413 /* make sure l doesn't vanish out from under us */
3414 down_write(&l->mutex);
3415 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003416 return l;
3417 }
3418 }
3419 /* entry not found; create a new one */
3420 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3421 if (!l) {
3422 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003423 return l;
3424 }
3425 init_rwsem(&l->mutex);
3426 down_write(&l->mutex);
3427 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003428 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003429 l->use_count = 0; /* don't increment here */
3430 l->list = NULL;
3431 l->owner = cgrp;
3432 list_add(&l->links, &cgrp->pidlists);
3433 mutex_unlock(&cgrp->pidlist_mutex);
3434 return l;
3435}
3436
3437/*
Ben Blum102a7752009-09-23 15:56:26 -07003438 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3439 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003440static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3441 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003442{
3443 pid_t *array;
3444 int length;
3445 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003446 struct cgroup_iter it;
3447 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003448 struct cgroup_pidlist *l;
3449
3450 /*
3451 * If cgroup gets more users after we read count, we won't have
3452 * enough space - tough. This race is indistinguishable to the
3453 * caller from the case that the additional cgroup users didn't
3454 * show up until sometime later on.
3455 */
3456 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003457 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003458 if (!array)
3459 return -ENOMEM;
3460 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003461 cgroup_iter_start(cgrp, &it);
3462 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003463 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003464 break;
Ben Blum102a7752009-09-23 15:56:26 -07003465 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003466 if (type == CGROUP_FILE_PROCS)
3467 pid = task_tgid_vnr(tsk);
3468 else
3469 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003470 if (pid > 0) /* make sure to only use valid results */
3471 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003472 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003473 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003474 length = n;
3475 /* now sort & (if procs) strip out duplicates */
3476 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003477 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003478 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003479 l = cgroup_pidlist_find(cgrp, type);
3480 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003481 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003482 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003483 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003484 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003485 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003486 l->list = array;
3487 l->length = length;
3488 l->use_count++;
3489 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003490 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003491 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003492}
3493
Balbir Singh846c7bb2007-10-18 23:39:44 -07003494/**
Li Zefana043e3b2008-02-23 15:24:09 -08003495 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003496 * @stats: cgroupstats to fill information into
3497 * @dentry: A dentry entry belonging to the cgroup for which stats have
3498 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003499 *
3500 * Build and fill cgroupstats so that taskstats can export it to user
3501 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003502 */
3503int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3504{
3505 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003506 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003507 struct cgroup_iter it;
3508 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003509
Balbir Singh846c7bb2007-10-18 23:39:44 -07003510 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003511 * Validate dentry by checking the superblock operations,
3512 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003513 */
Li Zefan33d283b2008-11-19 15:36:48 -08003514 if (dentry->d_sb->s_op != &cgroup_ops ||
3515 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003516 goto err;
3517
3518 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003519 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520
Paul Menagebd89aab2007-10-18 23:40:44 -07003521 cgroup_iter_start(cgrp, &it);
3522 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003523 switch (tsk->state) {
3524 case TASK_RUNNING:
3525 stats->nr_running++;
3526 break;
3527 case TASK_INTERRUPTIBLE:
3528 stats->nr_sleeping++;
3529 break;
3530 case TASK_UNINTERRUPTIBLE:
3531 stats->nr_uninterruptible++;
3532 break;
3533 case TASK_STOPPED:
3534 stats->nr_stopped++;
3535 break;
3536 default:
3537 if (delayacct_is_task_waiting_on_io(tsk))
3538 stats->nr_io_wait++;
3539 break;
3540 }
3541 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003542 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003543
Balbir Singh846c7bb2007-10-18 23:39:44 -07003544err:
3545 return ret;
3546}
3547
Paul Menage8f3ff202009-09-23 15:56:25 -07003548
Paul Menagecc31edc2008-10-18 20:28:04 -07003549/*
Ben Blum102a7752009-09-23 15:56:26 -07003550 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003551 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003552 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003553 */
3554
Ben Blum102a7752009-09-23 15:56:26 -07003555static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003556{
3557 /*
3558 * Initially we receive a position value that corresponds to
3559 * one more than the last pid shown (or 0 on the first call or
3560 * after a seek to the start). Use a binary-search to find the
3561 * next pid to display, if any
3562 */
Ben Blum102a7752009-09-23 15:56:26 -07003563 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003564 int index = 0, pid = *pos;
3565 int *iter;
3566
Ben Blum102a7752009-09-23 15:56:26 -07003567 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003568 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003569 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003570
Paul Menagecc31edc2008-10-18 20:28:04 -07003571 while (index < end) {
3572 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003573 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 index = mid;
3575 break;
Ben Blum102a7752009-09-23 15:56:26 -07003576 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 index = mid + 1;
3578 else
3579 end = mid;
3580 }
3581 }
3582 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003583 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003584 return NULL;
3585 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003586 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003587 *pos = *iter;
3588 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003589}
3590
Ben Blum102a7752009-09-23 15:56:26 -07003591static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003592{
Ben Blum102a7752009-09-23 15:56:26 -07003593 struct cgroup_pidlist *l = s->private;
3594 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003595}
3596
Ben Blum102a7752009-09-23 15:56:26 -07003597static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003598{
Ben Blum102a7752009-09-23 15:56:26 -07003599 struct cgroup_pidlist *l = s->private;
3600 pid_t *p = v;
3601 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003602 /*
3603 * Advance to the next pid in the array. If this goes off the
3604 * end, we're done
3605 */
3606 p++;
3607 if (p >= end) {
3608 return NULL;
3609 } else {
3610 *pos = *p;
3611 return p;
3612 }
3613}
3614
Ben Blum102a7752009-09-23 15:56:26 -07003615static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003616{
3617 return seq_printf(s, "%d\n", *(int *)v);
3618}
3619
Ben Blum102a7752009-09-23 15:56:26 -07003620/*
3621 * seq_operations functions for iterating on pidlists through seq_file -
3622 * independent of whether it's tasks or procs
3623 */
3624static const struct seq_operations cgroup_pidlist_seq_operations = {
3625 .start = cgroup_pidlist_start,
3626 .stop = cgroup_pidlist_stop,
3627 .next = cgroup_pidlist_next,
3628 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003629};
3630
Ben Blum102a7752009-09-23 15:56:26 -07003631static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003632{
Ben Blum72a8cb32009-09-23 15:56:27 -07003633 /*
3634 * the case where we're the last user of this particular pidlist will
3635 * have us remove it from the cgroup's list, which entails taking the
3636 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3637 * pidlist_mutex, we have to take pidlist_mutex first.
3638 */
3639 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003640 down_write(&l->mutex);
3641 BUG_ON(!l->use_count);
3642 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /* we're the last user if refcount is 0; remove and free */
3644 list_del(&l->links);
3645 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003646 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 put_pid_ns(l->key.ns);
3648 up_write(&l->mutex);
3649 kfree(l);
3650 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003651 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003653 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003654}
3655
Ben Blum102a7752009-09-23 15:56:26 -07003656static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003657{
Ben Blum102a7752009-09-23 15:56:26 -07003658 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003659 if (!(file->f_mode & FMODE_READ))
3660 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003661 /*
3662 * the seq_file will only be initialized if the file was opened for
3663 * reading; hence we check if it's not null only in that case.
3664 */
3665 l = ((struct seq_file *)file->private_data)->private;
3666 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003667 return seq_release(inode, file);
3668}
3669
Ben Blum102a7752009-09-23 15:56:26 -07003670static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003671 .read = seq_read,
3672 .llseek = seq_lseek,
3673 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003674 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003675};
3676
3677/*
Ben Blum102a7752009-09-23 15:56:26 -07003678 * The following functions handle opens on a file that displays a pidlist
3679 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3680 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 */
Ben Blum102a7752009-09-23 15:56:26 -07003682/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003683static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003684{
3685 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003686 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003687 int retval;
3688
3689 /* Nothing to do for write-only files */
3690 if (!(file->f_mode & FMODE_READ))
3691 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003692
Ben Blum102a7752009-09-23 15:56:26 -07003693 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003694 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003695 if (retval)
3696 return retval;
3697 /* configure file information */
3698 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003699
Ben Blum102a7752009-09-23 15:56:26 -07003700 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003701 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003702 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003703 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003704 }
Ben Blum102a7752009-09-23 15:56:26 -07003705 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706 return 0;
3707}
Ben Blum102a7752009-09-23 15:56:26 -07003708static int cgroup_tasks_open(struct inode *unused, struct file *file)
3709{
Ben Blum72a8cb32009-09-23 15:56:27 -07003710 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003711}
3712static int cgroup_procs_open(struct inode *unused, struct file *file)
3713{
Ben Blum72a8cb32009-09-23 15:56:27 -07003714 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003715}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003716
Paul Menagebd89aab2007-10-18 23:40:44 -07003717static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003718 struct cftype *cft)
3719{
Paul Menagebd89aab2007-10-18 23:40:44 -07003720 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003721}
3722
Paul Menage6379c102008-07-25 01:47:01 -07003723static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3724 struct cftype *cft,
3725 u64 val)
3726{
3727 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3728 if (val)
3729 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3730 else
3731 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3732 return 0;
3733}
3734
Paul Menagebbcb81d2007-10-18 23:39:32 -07003735/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003736 * Unregister event and free resources.
3737 *
3738 * Gets called from workqueue.
3739 */
3740static void cgroup_event_remove(struct work_struct *work)
3741{
3742 struct cgroup_event *event = container_of(work, struct cgroup_event,
3743 remove);
3744 struct cgroup *cgrp = event->cgrp;
3745
Li Zefan810cbee2013-02-18 18:56:14 +08003746 remove_wait_queue(event->wqh, &event->wait);
3747
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003748 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3749
Li Zefan810cbee2013-02-18 18:56:14 +08003750 /* Notify userspace the event is going away. */
3751 eventfd_signal(event->eventfd, 1);
3752
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003753 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003754 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003755 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003756}
3757
3758/*
3759 * Gets called on POLLHUP on eventfd when user closes it.
3760 *
3761 * Called with wqh->lock held and interrupts disabled.
3762 */
3763static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3764 int sync, void *key)
3765{
3766 struct cgroup_event *event = container_of(wait,
3767 struct cgroup_event, wait);
3768 struct cgroup *cgrp = event->cgrp;
3769 unsigned long flags = (unsigned long)key;
3770
3771 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003772 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003773 * If the event has been detached at cgroup removal, we
3774 * can simply return knowing the other side will cleanup
3775 * for us.
3776 *
3777 * We can't race against event freeing since the other
3778 * side will require wqh->lock via remove_wait_queue(),
3779 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003780 */
Li Zefan810cbee2013-02-18 18:56:14 +08003781 spin_lock(&cgrp->event_list_lock);
3782 if (!list_empty(&event->list)) {
3783 list_del_init(&event->list);
3784 /*
3785 * We are in atomic context, but cgroup_event_remove()
3786 * may sleep, so we have to call it in workqueue.
3787 */
3788 schedule_work(&event->remove);
3789 }
3790 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003791 }
3792
3793 return 0;
3794}
3795
3796static void cgroup_event_ptable_queue_proc(struct file *file,
3797 wait_queue_head_t *wqh, poll_table *pt)
3798{
3799 struct cgroup_event *event = container_of(pt,
3800 struct cgroup_event, pt);
3801
3802 event->wqh = wqh;
3803 add_wait_queue(wqh, &event->wait);
3804}
3805
3806/*
3807 * Parse input and register new cgroup event handler.
3808 *
3809 * Input must be in format '<event_fd> <control_fd> <args>'.
3810 * Interpretation of args is defined by control file implementation.
3811 */
3812static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3813 const char *buffer)
3814{
3815 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003816 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003817 unsigned int efd, cfd;
3818 struct file *efile = NULL;
3819 struct file *cfile = NULL;
3820 char *endp;
3821 int ret;
3822
3823 efd = simple_strtoul(buffer, &endp, 10);
3824 if (*endp != ' ')
3825 return -EINVAL;
3826 buffer = endp + 1;
3827
3828 cfd = simple_strtoul(buffer, &endp, 10);
3829 if ((*endp != ' ') && (*endp != '\0'))
3830 return -EINVAL;
3831 buffer = endp + 1;
3832
3833 event = kzalloc(sizeof(*event), GFP_KERNEL);
3834 if (!event)
3835 return -ENOMEM;
3836 event->cgrp = cgrp;
3837 INIT_LIST_HEAD(&event->list);
3838 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3839 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3840 INIT_WORK(&event->remove, cgroup_event_remove);
3841
3842 efile = eventfd_fget(efd);
3843 if (IS_ERR(efile)) {
3844 ret = PTR_ERR(efile);
3845 goto fail;
3846 }
3847
3848 event->eventfd = eventfd_ctx_fileget(efile);
3849 if (IS_ERR(event->eventfd)) {
3850 ret = PTR_ERR(event->eventfd);
3851 goto fail;
3852 }
3853
3854 cfile = fget(cfd);
3855 if (!cfile) {
3856 ret = -EBADF;
3857 goto fail;
3858 }
3859
3860 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003861 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003862 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003863 if (ret < 0)
3864 goto fail;
3865
3866 event->cft = __file_cft(cfile);
3867 if (IS_ERR(event->cft)) {
3868 ret = PTR_ERR(event->cft);
3869 goto fail;
3870 }
3871
Li Zefanf1690072013-02-18 14:13:35 +08003872 /*
3873 * The file to be monitored must be in the same cgroup as
3874 * cgroup.event_control is.
3875 */
3876 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3877 if (cgrp_cfile != cgrp) {
3878 ret = -EINVAL;
3879 goto fail;
3880 }
3881
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882 if (!event->cft->register_event || !event->cft->unregister_event) {
3883 ret = -EINVAL;
3884 goto fail;
3885 }
3886
3887 ret = event->cft->register_event(cgrp, event->cft,
3888 event->eventfd, buffer);
3889 if (ret)
3890 goto fail;
3891
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003892 /*
3893 * Events should be removed after rmdir of cgroup directory, but before
3894 * destroying subsystem state objects. Let's take reference to cgroup
3895 * directory dentry to do that.
3896 */
3897 dget(cgrp->dentry);
3898
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003899 spin_lock(&cgrp->event_list_lock);
3900 list_add(&event->list, &cgrp->event_list);
3901 spin_unlock(&cgrp->event_list_lock);
3902
3903 fput(cfile);
3904 fput(efile);
3905
3906 return 0;
3907
3908fail:
3909 if (cfile)
3910 fput(cfile);
3911
3912 if (event && event->eventfd && !IS_ERR(event->eventfd))
3913 eventfd_ctx_put(event->eventfd);
3914
3915 if (!IS_ERR_OR_NULL(efile))
3916 fput(efile);
3917
3918 kfree(event);
3919
3920 return ret;
3921}
3922
Daniel Lezcano97978e62010-10-27 15:33:35 -07003923static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3924 struct cftype *cft)
3925{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003926 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003927}
3928
3929static int cgroup_clone_children_write(struct cgroup *cgrp,
3930 struct cftype *cft,
3931 u64 val)
3932{
3933 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003934 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003935 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003936 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003937 return 0;
3938}
3939
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003940/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003941 * for the common functions, 'private' gives the type of file
3942 */
Ben Blum102a7752009-09-23 15:56:26 -07003943/* for hysterical raisins, we can't put this on the older files */
3944#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003945static struct cftype files[] = {
3946 {
3947 .name = "tasks",
3948 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003949 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003950 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003951 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003952 },
Ben Blum102a7752009-09-23 15:56:26 -07003953 {
3954 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3955 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003956 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003957 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003958 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003959 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003960 {
3961 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003962 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003963 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003964 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003965 {
3966 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3967 .write_string = cgroup_write_event_control,
3968 .mode = S_IWUGO,
3969 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003970 {
3971 .name = "cgroup.clone_children",
3972 .read_u64 = cgroup_clone_children_read,
3973 .write_u64 = cgroup_clone_children_write,
3974 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003975 {
3976 .name = "release_agent",
3977 .flags = CFTYPE_ONLY_ON_ROOT,
3978 .read_seq_string = cgroup_release_agent_show,
3979 .write_string = cgroup_release_agent_write,
3980 .max_write_len = PATH_MAX,
3981 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003982 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003983};
3984
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003985/**
3986 * cgroup_populate_dir - selectively creation of files in a directory
3987 * @cgrp: target cgroup
3988 * @base_files: true if the base files should be added
3989 * @subsys_mask: mask of the subsystem ids whose files should be added
3990 */
3991static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3992 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003993{
3994 int err;
3995 struct cgroup_subsys *ss;
3996
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003997 if (base_files) {
3998 err = cgroup_addrm_files(cgrp, NULL, files, true);
3999 if (err < 0)
4000 return err;
4001 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004002
Tejun Heo8e3f6542012-04-01 12:09:55 -07004003 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004004 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004005 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004006 if (!test_bit(ss->subsys_id, &subsys_mask))
4007 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004008
Tejun Heodb0416b2012-04-01 12:09:55 -07004009 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004010 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004011 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004012
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004013 /* This cgroup is ready now */
4014 for_each_subsys(cgrp->root, ss) {
4015 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4016 /*
4017 * Update id->css pointer and make this css visible from
4018 * CSS ID functions. This pointer will be dereferened
4019 * from RCU-read-side without locks.
4020 */
4021 if (css->id)
4022 rcu_assign_pointer(css->id->css, css);
4023 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004024
4025 return 0;
4026}
4027
Tejun Heo48ddbe12012-04-01 12:09:56 -07004028static void css_dput_fn(struct work_struct *work)
4029{
4030 struct cgroup_subsys_state *css =
4031 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004032 struct dentry *dentry = css->cgroup->dentry;
4033 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004034
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004035 atomic_inc(&sb->s_active);
4036 dput(dentry);
4037 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004038}
4039
Paul Menageddbcc7e2007-10-18 23:39:30 -07004040static void init_cgroup_css(struct cgroup_subsys_state *css,
4041 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004042 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004043{
Paul Menagebd89aab2007-10-18 23:40:44 -07004044 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004045 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004046 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004047 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004048 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004049 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004050 BUG_ON(cgrp->subsys[ss->subsys_id]);
4051 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004052
4053 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004054 * css holds an extra ref to @cgrp->dentry which is put on the last
4055 * css_put(). dput() requires process context, which css_put() may
4056 * be called without. @css->dput_work will be used to invoke
4057 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004058 */
4059 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004060}
4061
Tejun Heob1929db2012-11-19 08:13:38 -08004062/* invoke ->post_create() on a new CSS and mark it online if successful */
4063static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004064{
Tejun Heob1929db2012-11-19 08:13:38 -08004065 int ret = 0;
4066
Tejun Heoa31f2d32012-11-19 08:13:37 -08004067 lockdep_assert_held(&cgroup_mutex);
4068
Tejun Heo92fb9742012-11-19 08:13:38 -08004069 if (ss->css_online)
4070 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004071 if (!ret)
4072 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4073 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004074}
4075
4076/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4077static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4078 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4079{
4080 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4081
4082 lockdep_assert_held(&cgroup_mutex);
4083
4084 if (!(css->flags & CSS_ONLINE))
4085 return;
4086
Li Zefand7eeac12013-03-12 15:35:59 -07004087 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004088 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004089
4090 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4091}
4092
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093/*
Li Zefana043e3b2008-02-23 15:24:09 -08004094 * cgroup_create - create a cgroup
4095 * @parent: cgroup that will be parent of the new cgroup
4096 * @dentry: dentry of the new cgroup
4097 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098 *
Li Zefana043e3b2008-02-23 15:24:09 -08004099 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004100 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004102 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004103{
Paul Menagebd89aab2007-10-18 23:40:44 -07004104 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004105 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106 struct cgroupfs_root *root = parent->root;
4107 int err = 0;
4108 struct cgroup_subsys *ss;
4109 struct super_block *sb = root->sb;
4110
Tejun Heo0a950f62012-11-19 09:02:12 -08004111 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004112 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4113 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004114 return -ENOMEM;
4115
Li Zefan65dff752013-03-01 15:01:56 +08004116 name = cgroup_alloc_name(dentry);
4117 if (!name)
4118 goto err_free_cgrp;
4119 rcu_assign_pointer(cgrp->name, name);
4120
Tejun Heo0a950f62012-11-19 09:02:12 -08004121 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4122 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004123 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004124
Tejun Heo976c06b2012-11-05 09:16:59 -08004125 /*
4126 * Only live parents can have children. Note that the liveliness
4127 * check isn't strictly necessary because cgroup_mkdir() and
4128 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4129 * anyway so that locking is contained inside cgroup proper and we
4130 * don't get nasty surprises if we ever grow another caller.
4131 */
4132 if (!cgroup_lock_live_group(parent)) {
4133 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004134 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004135 }
4136
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137 /* Grab a reference on the superblock so the hierarchy doesn't
4138 * get deleted on unmount if there are child cgroups. This
4139 * can be done outside cgroup_mutex, since the sb can't
4140 * disappear while someone has an open control file on the
4141 * fs */
4142 atomic_inc(&sb->s_active);
4143
Paul Menagecc31edc2008-10-18 20:28:04 -07004144 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145
Li Zefanfe1c06c2013-01-24 14:30:22 +08004146 dentry->d_fsdata = cgrp;
4147 cgrp->dentry = dentry;
4148
Paul Menagebd89aab2007-10-18 23:40:44 -07004149 cgrp->parent = parent;
4150 cgrp->root = parent->root;
4151 cgrp->top_cgroup = parent->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152
Li Zefanb6abdb02008-03-04 14:28:19 -08004153 if (notify_on_release(parent))
4154 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4155
Tejun Heo2260e7f2012-11-19 08:13:38 -08004156 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4157 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004158
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004160 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004161
Tejun Heo92fb9742012-11-19 08:13:38 -08004162 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163 if (IS_ERR(css)) {
4164 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004165 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004167 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004168 if (ss->use_id) {
4169 err = alloc_css_id(ss, parent, cgrp);
4170 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004171 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004172 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 }
4174
Tejun Heo4e139af2012-11-19 08:13:36 -08004175 /*
4176 * Create directory. cgroup_create_file() returns with the new
4177 * directory locked on success so that it can be populated without
4178 * dropping cgroup_mutex.
4179 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004180 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004182 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004183 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184
Tejun Heo4e139af2012-11-19 08:13:36 -08004185 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004186 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4187 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4188 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004189
Tejun Heob1929db2012-11-19 08:13:38 -08004190 /* each css holds a ref to the cgroup's dentry */
4191 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004192 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004193
Li Zefan415cf072013-04-08 14:35:02 +08004194 /* hold a ref to the parent's dentry */
4195 dget(parent->dentry);
4196
Tejun Heob1929db2012-11-19 08:13:38 -08004197 /* creation succeeded, notify subsystems */
4198 for_each_subsys(root, ss) {
4199 err = online_css(ss, cgrp);
4200 if (err)
4201 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004202
4203 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4204 parent->parent) {
4205 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",
4206 current->comm, current->pid, ss->name);
4207 if (!strcmp(ss->name, "memory"))
4208 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4209 ss->warned_broken_hierarchy = true;
4210 }
Tejun Heoa8638032012-11-09 09:12:29 -08004211 }
4212
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004213 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004214 if (err)
4215 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216
4217 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004218 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004219
4220 return 0;
4221
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004222err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004225 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228 /* Release the reference count that we took on the superblock */
4229 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004230err_free_id:
4231 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004232err_free_name:
4233 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004234err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004235 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004237
4238err_destroy:
4239 cgroup_destroy_locked(cgrp);
4240 mutex_unlock(&cgroup_mutex);
4241 mutex_unlock(&dentry->d_inode->i_mutex);
4242 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243}
4244
Al Viro18bb1db2011-07-26 01:41:39 -04004245static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246{
4247 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4248
4249 /* the vfs holds inode->i_mutex already */
4250 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4251}
4252
Tejun Heo42809dd2012-11-19 08:13:37 -08004253static int cgroup_destroy_locked(struct cgroup *cgrp)
4254 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255{
Tejun Heo42809dd2012-11-19 08:13:37 -08004256 struct dentry *d = cgrp->dentry;
4257 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004258 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004259 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260
Tejun Heo42809dd2012-11-19 08:13:37 -08004261 lockdep_assert_held(&d->d_inode->i_mutex);
4262 lockdep_assert_held(&cgroup_mutex);
4263
4264 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004266
Tejun Heo1a90dd52012-11-05 09:16:59 -08004267 /*
4268 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4269 * removed. This makes future css_tryget() and child creation
4270 * attempts fail thus maintaining the removal conditions verified
4271 * above.
4272 */
Tejun Heoed9577932012-11-05 09:16:58 -08004273 for_each_subsys(cgrp->root, ss) {
4274 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4275
4276 WARN_ON(atomic_read(&css->refcnt) < 0);
4277 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004278 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004279 set_bit(CGRP_REMOVED, &cgrp->flags);
4280
Tejun Heoa31f2d32012-11-19 08:13:37 -08004281 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004282 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004283 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004284
4285 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004286 * Put all the base refs. Each css holds an extra reference to the
4287 * cgroup's dentry and cgroup removal proceeds regardless of css
4288 * refs. On the last put of each css, whenever that may be, the
4289 * extra dentry ref is put so that dentry destruction happens only
4290 * after all css's are released.
4291 */
Tejun Heoe9316082012-11-05 09:16:58 -08004292 for_each_subsys(cgrp->root, ss)
4293 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004295 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004296 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004297 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004298 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004299
Paul Menage999cd8a2009-01-07 18:08:36 -08004300 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004301 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004302 list_del_init(&cgrp->allcg_node);
4303
Tejun Heo42809dd2012-11-19 08:13:37 -08004304 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305 cgroup_d_remove_dir(d);
4306 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307
Paul Menagebd89aab2007-10-18 23:40:44 -07004308 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004309 check_for_release(parent);
4310
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004311 /*
4312 * Unregister events and notify userspace.
4313 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004314 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004315 */
4316 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004317 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004318 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004319 schedule_work(&event->remove);
4320 }
Li Zefan810cbee2013-02-18 18:56:14 +08004321 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004322
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323 return 0;
4324}
4325
Tejun Heo42809dd2012-11-19 08:13:37 -08004326static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4327{
4328 int ret;
4329
4330 mutex_lock(&cgroup_mutex);
4331 ret = cgroup_destroy_locked(dentry->d_fsdata);
4332 mutex_unlock(&cgroup_mutex);
4333
4334 return ret;
4335}
4336
Tejun Heo8e3f6542012-04-01 12:09:55 -07004337static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4338{
4339 INIT_LIST_HEAD(&ss->cftsets);
4340
4341 /*
4342 * base_cftset is embedded in subsys itself, no need to worry about
4343 * deregistration.
4344 */
4345 if (ss->base_cftypes) {
4346 ss->base_cftset.cfts = ss->base_cftypes;
4347 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4348 }
4349}
4350
Li Zefan06a11922008-04-29 01:00:07 -07004351static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004354
4355 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356
Tejun Heo648bb562012-11-19 08:13:36 -08004357 mutex_lock(&cgroup_mutex);
4358
Tejun Heo8e3f6542012-04-01 12:09:55 -07004359 /* init base cftset */
4360 cgroup_init_cftsets(ss);
4361
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004363 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004364 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004365 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 /* We don't handle early failures gracefully */
4367 BUG_ON(IS_ERR(css));
4368 init_cgroup_css(css, ss, dummytop);
4369
Li Zefane8d55fd2008-04-29 01:00:13 -07004370 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004371 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004372 * newly registered, all tasks and hence the
4373 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004374 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375
4376 need_forkexit_callback |= ss->fork || ss->exit;
4377
Li Zefane8d55fd2008-04-29 01:00:13 -07004378 /* At system boot, before all subsystems have been
4379 * registered, no tasks have been forked, so we don't
4380 * need to invoke fork callbacks here. */
4381 BUG_ON(!list_empty(&init_task.tasks));
4382
Paul Menageddbcc7e2007-10-18 23:39:30 -07004383 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004384 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004385
Tejun Heo648bb562012-11-19 08:13:36 -08004386 mutex_unlock(&cgroup_mutex);
4387
Ben Blume6a11052010-03-10 15:22:09 -08004388 /* this function shouldn't be used with modular subsystems, since they
4389 * need to register a subsys_id, among other things */
4390 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004391}
4392
4393/**
Ben Blume6a11052010-03-10 15:22:09 -08004394 * cgroup_load_subsys: load and register a modular subsystem at runtime
4395 * @ss: the subsystem to load
4396 *
4397 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004398 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004399 * up for use. If the subsystem is built-in anyway, work is delegated to the
4400 * simpler cgroup_init_subsys.
4401 */
4402int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4403{
Ben Blume6a11052010-03-10 15:22:09 -08004404 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004405 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004406 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004407 struct css_set *cg;
4408 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004409
4410 /* check name and function validity */
4411 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004412 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004413 return -EINVAL;
4414
4415 /*
4416 * we don't support callbacks in modular subsystems. this check is
4417 * before the ss->module check for consistency; a subsystem that could
4418 * be a module should still have no callbacks even if the user isn't
4419 * compiling it as one.
4420 */
4421 if (ss->fork || ss->exit)
4422 return -EINVAL;
4423
4424 /*
4425 * an optionally modular subsystem is built-in: we want to do nothing,
4426 * since cgroup_init_subsys will have already taken care of it.
4427 */
4428 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004429 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004430 BUG_ON(subsys[ss->subsys_id] != ss);
4431 return 0;
4432 }
4433
Tejun Heo8e3f6542012-04-01 12:09:55 -07004434 /* init base cftset */
4435 cgroup_init_cftsets(ss);
4436
Ben Blume6a11052010-03-10 15:22:09 -08004437 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004438 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004439
4440 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004441 * no ss->css_alloc seems to need anything important in the ss
4442 * struct, so this can happen first (i.e. before the rootnode
4443 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004444 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004445 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004446 if (IS_ERR(css)) {
4447 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004448 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004449 mutex_unlock(&cgroup_mutex);
4450 return PTR_ERR(css);
4451 }
4452
4453 list_add(&ss->sibling, &rootnode.subsys_list);
4454 ss->root = &rootnode;
4455
4456 /* our new subsystem will be attached to the dummy hierarchy. */
4457 init_cgroup_css(css, ss, dummytop);
4458 /* init_idr must be after init_cgroup_css because it sets css->id. */
4459 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004460 ret = cgroup_init_idr(ss, css);
4461 if (ret)
4462 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004463 }
4464
4465 /*
4466 * Now we need to entangle the css into the existing css_sets. unlike
4467 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4468 * will need a new pointer to it; done by iterating the css_set_table.
4469 * furthermore, modifying the existing css_sets will corrupt the hash
4470 * table state, so each changed css_set will need its hash recomputed.
4471 * this is all done under the css_set_lock.
4472 */
4473 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004474 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004475 /* skip entries that we already rehashed */
4476 if (cg->subsys[ss->subsys_id])
4477 continue;
4478 /* remove existing entry */
4479 hash_del(&cg->hlist);
4480 /* set new value */
4481 cg->subsys[ss->subsys_id] = css;
4482 /* recompute hash and restore entry */
4483 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004484 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004485 }
4486 write_unlock(&css_set_lock);
4487
Ben Blume6a11052010-03-10 15:22:09 -08004488 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004489 ret = online_css(ss, dummytop);
4490 if (ret)
4491 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004492
Ben Blume6a11052010-03-10 15:22:09 -08004493 /* success! */
4494 mutex_unlock(&cgroup_mutex);
4495 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004496
4497err_unload:
4498 mutex_unlock(&cgroup_mutex);
4499 /* @ss can't be mounted here as try_module_get() would fail */
4500 cgroup_unload_subsys(ss);
4501 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004502}
4503EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4504
4505/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004506 * cgroup_unload_subsys: unload a modular subsystem
4507 * @ss: the subsystem to unload
4508 *
4509 * This function should be called in a modular subsystem's exitcall. When this
4510 * function is invoked, the refcount on the subsystem's module will be 0, so
4511 * the subsystem will not be attached to any hierarchy.
4512 */
4513void cgroup_unload_subsys(struct cgroup_subsys *ss)
4514{
4515 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004516
4517 BUG_ON(ss->module == NULL);
4518
4519 /*
4520 * we shouldn't be called if the subsystem is in use, and the use of
4521 * try_module_get in parse_cgroupfs_options should ensure that it
4522 * doesn't start being used while we're killing it off.
4523 */
4524 BUG_ON(ss->root != &rootnode);
4525
4526 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004527
Tejun Heoa31f2d32012-11-19 08:13:37 -08004528 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004529 ss->active = 0;
4530
Tejun Heoc897ff62013-02-27 17:03:49 -08004531 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004532 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004533
Ben Blumcf5d5942010-03-10 15:22:09 -08004534 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004535 subsys[ss->subsys_id] = NULL;
4536
4537 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004538 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004539
4540 /*
4541 * disentangle the css from all css_sets attached to the dummytop. as
4542 * in loading, we need to pay our respects to the hashtable gods.
4543 */
4544 write_lock(&css_set_lock);
4545 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4546 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004547 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004548
Li Zefan0ac801f2013-01-10 11:49:27 +08004549 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004550 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004551 key = css_set_hash(cg->subsys);
4552 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004553 }
4554 write_unlock(&css_set_lock);
4555
4556 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004557 * remove subsystem's css from the dummytop and free it - need to
4558 * free before marking as null because ss->css_free needs the
4559 * cgrp->subsys pointer to find their state. note that this also
4560 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004561 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004562 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004563 dummytop->subsys[ss->subsys_id] = NULL;
4564
4565 mutex_unlock(&cgroup_mutex);
4566}
4567EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4568
4569/**
Li Zefana043e3b2008-02-23 15:24:09 -08004570 * cgroup_init_early - cgroup initialization at system boot
4571 *
4572 * Initialize cgroups at system boot, and initialize any
4573 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574 */
4575int __init cgroup_init_early(void)
4576{
4577 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004578 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004579 INIT_LIST_HEAD(&init_css_set.cg_links);
4580 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004581 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004582 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004583 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004584 root_count = 1;
4585 init_task.cgroups = &init_css_set;
4586
4587 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004588 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004589 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004590 &rootnode.top_cgroup.css_sets);
4591 list_add(&init_css_set_link.cg_link_list,
4592 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004593
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004594 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004595 struct cgroup_subsys *ss = subsys[i];
4596
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004597 /* at bootup time, we don't worry about modular subsystems */
4598 if (!ss || ss->module)
4599 continue;
4600
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 BUG_ON(!ss->name);
4602 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004603 BUG_ON(!ss->css_alloc);
4604 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004605 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004606 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607 ss->name, ss->subsys_id);
4608 BUG();
4609 }
4610
4611 if (ss->early_init)
4612 cgroup_init_subsys(ss);
4613 }
4614 return 0;
4615}
4616
4617/**
Li Zefana043e3b2008-02-23 15:24:09 -08004618 * cgroup_init - cgroup initialization
4619 *
4620 * Register cgroup filesystem and /proc file, and initialize
4621 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622 */
4623int __init cgroup_init(void)
4624{
4625 int err;
4626 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004627 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004628
4629 err = bdi_init(&cgroup_backing_dev_info);
4630 if (err)
4631 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004633 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004635
4636 /* at bootup time, we don't worry about modular subsystems */
4637 if (!ss || ss->module)
4638 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 if (!ss->early_init)
4640 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004641 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004642 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004643 }
4644
Li Zefan472b1052008-04-29 01:00:11 -07004645 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004646 key = css_set_hash(init_css_set.subsys);
4647 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004648 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004649
4650 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4651 if (!cgroup_kobj) {
4652 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004654 }
4655
4656 err = register_filesystem(&cgroup_fs_type);
4657 if (err < 0) {
4658 kobject_put(cgroup_kobj);
4659 goto out;
4660 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004661
Li Zefan46ae2202008-04-29 01:00:08 -07004662 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004663
Paul Menageddbcc7e2007-10-18 23:39:30 -07004664out:
Paul Menagea4243162007-10-18 23:39:35 -07004665 if (err)
4666 bdi_destroy(&cgroup_backing_dev_info);
4667
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668 return err;
4669}
Paul Menageb4f48b62007-10-18 23:39:33 -07004670
Paul Menagea4243162007-10-18 23:39:35 -07004671/*
4672 * proc_cgroup_show()
4673 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4674 * - Used for /proc/<pid>/cgroup.
4675 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4676 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004677 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004678 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4679 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4680 * cgroup to top_cgroup.
4681 */
4682
4683/* TODO: Use a proper seq_file iterator */
4684static int proc_cgroup_show(struct seq_file *m, void *v)
4685{
4686 struct pid *pid;
4687 struct task_struct *tsk;
4688 char *buf;
4689 int retval;
4690 struct cgroupfs_root *root;
4691
4692 retval = -ENOMEM;
4693 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4694 if (!buf)
4695 goto out;
4696
4697 retval = -ESRCH;
4698 pid = m->private;
4699 tsk = get_pid_task(pid, PIDTYPE_PID);
4700 if (!tsk)
4701 goto out_free;
4702
4703 retval = 0;
4704
4705 mutex_lock(&cgroup_mutex);
4706
Li Zefane5f6a862009-01-07 18:07:41 -08004707 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004708 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004709 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004710 int count = 0;
4711
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004712 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004713 for_each_subsys(root, ss)
4714 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004715 if (strlen(root->name))
4716 seq_printf(m, "%sname=%s", count ? "," : "",
4717 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004718 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004719 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004720 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004721 if (retval < 0)
4722 goto out_unlock;
4723 seq_puts(m, buf);
4724 seq_putc(m, '\n');
4725 }
4726
4727out_unlock:
4728 mutex_unlock(&cgroup_mutex);
4729 put_task_struct(tsk);
4730out_free:
4731 kfree(buf);
4732out:
4733 return retval;
4734}
4735
4736static int cgroup_open(struct inode *inode, struct file *file)
4737{
4738 struct pid *pid = PROC_I(inode)->pid;
4739 return single_open(file, proc_cgroup_show, pid);
4740}
4741
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004742const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004743 .open = cgroup_open,
4744 .read = seq_read,
4745 .llseek = seq_lseek,
4746 .release = single_release,
4747};
4748
4749/* Display information about each subsystem and each hierarchy */
4750static int proc_cgroupstats_show(struct seq_file *m, void *v)
4751{
4752 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004753
Paul Menage8bab8dd2008-04-04 14:29:57 -07004754 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004755 /*
4756 * ideally we don't want subsystems moving around while we do this.
4757 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4758 * subsys/hierarchy state.
4759 */
Paul Menagea4243162007-10-18 23:39:35 -07004760 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004761 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4762 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004763 if (ss == NULL)
4764 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004765 seq_printf(m, "%s\t%d\t%d\t%d\n",
4766 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004767 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004768 }
4769 mutex_unlock(&cgroup_mutex);
4770 return 0;
4771}
4772
4773static int cgroupstats_open(struct inode *inode, struct file *file)
4774{
Al Viro9dce07f2008-03-29 03:07:28 +00004775 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004776}
4777
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004778static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004779 .open = cgroupstats_open,
4780 .read = seq_read,
4781 .llseek = seq_lseek,
4782 .release = single_release,
4783};
4784
Paul Menageb4f48b62007-10-18 23:39:33 -07004785/**
4786 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004787 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004788 *
4789 * Description: A task inherits its parent's cgroup at fork().
4790 *
4791 * A pointer to the shared css_set was automatically copied in
4792 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004793 * it was not made under the protection of RCU or cgroup_mutex, so
4794 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4795 * have already changed current->cgroups, allowing the previously
4796 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004797 *
4798 * At the point that cgroup_fork() is called, 'current' is the parent
4799 * task, and the passed argument 'child' points to the child task.
4800 */
4801void cgroup_fork(struct task_struct *child)
4802{
Tejun Heo9bb71302012-10-18 17:52:07 -07004803 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004804 child->cgroups = current->cgroups;
4805 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004806 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004807 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004808}
4809
4810/**
Li Zefana043e3b2008-02-23 15:24:09 -08004811 * cgroup_post_fork - called on a new task after adding it to the task list
4812 * @child: the task in question
4813 *
Tejun Heo5edee612012-10-16 15:03:14 -07004814 * Adds the task to the list running through its css_set if necessary and
4815 * call the subsystem fork() callbacks. Has to be after the task is
4816 * visible on the task list in case we race with the first call to
4817 * cgroup_iter_start() - to guarantee that the new task ends up on its
4818 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004819 */
Paul Menage817929e2007-10-18 23:39:36 -07004820void cgroup_post_fork(struct task_struct *child)
4821{
Tejun Heo5edee612012-10-16 15:03:14 -07004822 int i;
4823
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004824 /*
4825 * use_task_css_set_links is set to 1 before we walk the tasklist
4826 * under the tasklist_lock and we read it here after we added the child
4827 * to the tasklist under the tasklist_lock as well. If the child wasn't
4828 * yet in the tasklist when we walked through it from
4829 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4830 * should be visible now due to the paired locking and barriers implied
4831 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4832 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4833 * lock on fork.
4834 */
Paul Menage817929e2007-10-18 23:39:36 -07004835 if (use_task_css_set_links) {
4836 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004837 task_lock(child);
4838 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004839 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004840 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004841 write_unlock(&css_set_lock);
4842 }
Tejun Heo5edee612012-10-16 15:03:14 -07004843
4844 /*
4845 * Call ss->fork(). This must happen after @child is linked on
4846 * css_set; otherwise, @child might change state between ->fork()
4847 * and addition to css_set.
4848 */
4849 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004850 /*
4851 * fork/exit callbacks are supported only for builtin
4852 * subsystems, and the builtin section of the subsys
4853 * array is immutable, so we don't need to lock the
4854 * subsys array here. On the other hand, modular section
4855 * of the array can be freed at module unload, so we
4856 * can't touch that.
4857 */
4858 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004859 struct cgroup_subsys *ss = subsys[i];
4860
Tejun Heo5edee612012-10-16 15:03:14 -07004861 if (ss->fork)
4862 ss->fork(child);
4863 }
4864 }
Paul Menage817929e2007-10-18 23:39:36 -07004865}
Tejun Heo5edee612012-10-16 15:03:14 -07004866
Paul Menage817929e2007-10-18 23:39:36 -07004867/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004868 * cgroup_exit - detach cgroup from exiting task
4869 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004870 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004871 *
4872 * Description: Detach cgroup from @tsk and release it.
4873 *
4874 * Note that cgroups marked notify_on_release force every task in
4875 * them to take the global cgroup_mutex mutex when exiting.
4876 * This could impact scaling on very large systems. Be reluctant to
4877 * use notify_on_release cgroups where very high task exit scaling
4878 * is required on large systems.
4879 *
4880 * the_top_cgroup_hack:
4881 *
4882 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4883 *
4884 * We call cgroup_exit() while the task is still competent to
4885 * handle notify_on_release(), then leave the task attached to the
4886 * root cgroup in each hierarchy for the remainder of its exit.
4887 *
4888 * To do this properly, we would increment the reference count on
4889 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4890 * code we would add a second cgroup function call, to drop that
4891 * reference. This would just create an unnecessary hot spot on
4892 * the top_cgroup reference count, to no avail.
4893 *
4894 * Normally, holding a reference to a cgroup without bumping its
4895 * count is unsafe. The cgroup could go away, or someone could
4896 * attach us to a different cgroup, decrementing the count on
4897 * the first cgroup that we never incremented. But in this case,
4898 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004899 * which wards off any cgroup_attach_task() attempts, or task is a failed
4900 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004901 */
4902void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4903{
Paul Menage817929e2007-10-18 23:39:36 -07004904 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004905 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004906
4907 /*
4908 * Unlink from the css_set task list if necessary.
4909 * Optimistically check cg_list before taking
4910 * css_set_lock
4911 */
4912 if (!list_empty(&tsk->cg_list)) {
4913 write_lock(&css_set_lock);
4914 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004915 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004916 write_unlock(&css_set_lock);
4917 }
4918
Paul Menageb4f48b62007-10-18 23:39:33 -07004919 /* Reassign the task to the init_css_set. */
4920 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004921 cg = tsk->cgroups;
4922 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004923
4924 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004925 /*
4926 * fork/exit callbacks are supported only for builtin
4927 * subsystems, see cgroup_post_fork() for details.
4928 */
4929 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004930 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004931
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004932 if (ss->exit) {
4933 struct cgroup *old_cgrp =
4934 rcu_dereference_raw(cg->subsys[i])->cgroup;
4935 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004936 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004937 }
4938 }
4939 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004940 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004941
Li Zefanb5d646f2013-01-24 14:43:51 +08004942 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004943}
Paul Menage697f4162007-10-18 23:39:34 -07004944
Paul Menagebd89aab2007-10-18 23:40:44 -07004945static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004946{
4947 /* All of these checks rely on RCU to keep the cgroup
4948 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004949 if (cgroup_is_releasable(cgrp) &&
4950 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4951 /*
4952 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004953 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004954 * it now
4955 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004956 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004957
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004958 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004959 if (!cgroup_is_removed(cgrp) &&
4960 list_empty(&cgrp->release_list)) {
4961 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004962 need_schedule_work = 1;
4963 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004964 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004965 if (need_schedule_work)
4966 schedule_work(&release_agent_work);
4967 }
4968}
4969
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004970/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004971bool __css_tryget(struct cgroup_subsys_state *css)
4972{
Tejun Heoe9316082012-11-05 09:16:58 -08004973 while (true) {
4974 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004975
Tejun Heoe9316082012-11-05 09:16:58 -08004976 v = css_refcnt(css);
4977 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4978 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004979 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004980 else if (t < 0)
4981 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004982 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004983 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004984}
4985EXPORT_SYMBOL_GPL(__css_tryget);
4986
4987/* Caller must verify that the css is not for root cgroup */
4988void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004989{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004990 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004991
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004992 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004993 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004994 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004995}
Ben Blum67523c42010-03-10 15:22:11 -08004996EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004997
4998/*
4999 * Notify userspace when a cgroup is released, by running the
5000 * configured release agent with the name of the cgroup (path
5001 * relative to the root of cgroup file system) as the argument.
5002 *
5003 * Most likely, this user command will try to rmdir this cgroup.
5004 *
5005 * This races with the possibility that some other task will be
5006 * attached to this cgroup before it is removed, or that some other
5007 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5008 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5009 * unused, and this cgroup will be reprieved from its death sentence,
5010 * to continue to serve a useful existence. Next time it's released,
5011 * we will get notified again, if it still has 'notify_on_release' set.
5012 *
5013 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5014 * means only wait until the task is successfully execve()'d. The
5015 * separate release agent task is forked by call_usermodehelper(),
5016 * then control in this thread returns here, without waiting for the
5017 * release agent task. We don't bother to wait because the caller of
5018 * this routine has no use for the exit status of the release agent
5019 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005020 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021static void cgroup_release_agent(struct work_struct *work)
5022{
5023 BUG_ON(work != &release_agent_work);
5024 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005025 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026 while (!list_empty(&release_list)) {
5027 char *argv[3], *envp[3];
5028 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005029 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005030 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005031 struct cgroup,
5032 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005033 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005034 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005035 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005036 if (!pathbuf)
5037 goto continue_free;
5038 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5039 goto continue_free;
5040 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5041 if (!agentbuf)
5042 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005043
5044 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005045 argv[i++] = agentbuf;
5046 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005047 argv[i] = NULL;
5048
5049 i = 0;
5050 /* minimal command environment */
5051 envp[i++] = "HOME=/";
5052 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5053 envp[i] = NULL;
5054
5055 /* Drop the lock while we invoke the usermode helper,
5056 * since the exec could involve hitting disk and hence
5057 * be a slow process */
5058 mutex_unlock(&cgroup_mutex);
5059 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005061 continue_free:
5062 kfree(pathbuf);
5063 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005064 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005065 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005066 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005067 mutex_unlock(&cgroup_mutex);
5068}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005069
5070static int __init cgroup_disable(char *str)
5071{
5072 int i;
5073 char *token;
5074
5075 while ((token = strsep(&str, ",")) != NULL) {
5076 if (!*token)
5077 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005078 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005079 struct cgroup_subsys *ss = subsys[i];
5080
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005081 /*
5082 * cgroup_disable, being at boot time, can't
5083 * know about module subsystems, so we don't
5084 * worry about them.
5085 */
5086 if (!ss || ss->module)
5087 continue;
5088
Paul Menage8bab8dd2008-04-04 14:29:57 -07005089 if (!strcmp(token, ss->name)) {
5090 ss->disabled = 1;
5091 printk(KERN_INFO "Disabling %s control group"
5092 " subsystem\n", ss->name);
5093 break;
5094 }
5095 }
5096 }
5097 return 1;
5098}
5099__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005100
5101/*
5102 * Functons for CSS ID.
5103 */
5104
5105/*
5106 *To get ID other than 0, this should be called when !cgroup_is_removed().
5107 */
5108unsigned short css_id(struct cgroup_subsys_state *css)
5109{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005110 struct css_id *cssid;
5111
5112 /*
5113 * This css_id() can return correct value when somone has refcnt
5114 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5115 * it's unchanged until freed.
5116 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005117 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005118
5119 if (cssid)
5120 return cssid->id;
5121 return 0;
5122}
Ben Blum67523c42010-03-10 15:22:11 -08005123EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005124
5125unsigned short css_depth(struct cgroup_subsys_state *css)
5126{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005127 struct css_id *cssid;
5128
Tejun Heo28b4c272012-04-01 12:09:56 -07005129 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005130
5131 if (cssid)
5132 return cssid->depth;
5133 return 0;
5134}
Ben Blum67523c42010-03-10 15:22:11 -08005135EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005136
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005137/**
5138 * css_is_ancestor - test "root" css is an ancestor of "child"
5139 * @child: the css to be tested.
5140 * @root: the css supporsed to be an ancestor of the child.
5141 *
5142 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005143 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005144 * But, considering usual usage, the csses should be valid objects after test.
5145 * Assuming that the caller will do some action to the child if this returns
5146 * returns true, the caller must take "child";s reference count.
5147 * If "child" is valid object and this returns true, "root" is valid, too.
5148 */
5149
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005150bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005151 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005152{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005153 struct css_id *child_id;
5154 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005155
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005156 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005157 if (!child_id)
5158 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005159 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005160 if (!root_id)
5161 return false;
5162 if (child_id->depth < root_id->depth)
5163 return false;
5164 if (child_id->stack[root_id->depth] != root_id->id)
5165 return false;
5166 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005167}
5168
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005169void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5170{
5171 struct css_id *id = css->id;
5172 /* When this is called before css_id initialization, id can be NULL */
5173 if (!id)
5174 return;
5175
5176 BUG_ON(!ss->use_id);
5177
5178 rcu_assign_pointer(id->css, NULL);
5179 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005180 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005181 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005182 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005183 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005184}
Ben Blum67523c42010-03-10 15:22:11 -08005185EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005186
5187/*
5188 * This is called by init or create(). Then, calls to this function are
5189 * always serialized (By cgroup_mutex() at create()).
5190 */
5191
5192static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5193{
5194 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005195 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005196
5197 BUG_ON(!ss->use_id);
5198
5199 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5200 newid = kzalloc(size, GFP_KERNEL);
5201 if (!newid)
5202 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005203
5204 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005205 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005206 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005207 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005208 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005209 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210
5211 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005212 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005213 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214
Tejun Heod228d9e2013-02-27 17:04:54 -08005215 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005216 newid->depth = depth;
5217 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005218err_out:
5219 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005220 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221
5222}
5223
Ben Blume6a11052010-03-10 15:22:09 -08005224static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5225 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005226{
5227 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005228
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005229 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230 idr_init(&ss->idr);
5231
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005232 newid = get_new_cssid(ss, 0);
5233 if (IS_ERR(newid))
5234 return PTR_ERR(newid);
5235
5236 newid->stack[0] = newid->id;
5237 newid->css = rootcss;
5238 rootcss->id = newid;
5239 return 0;
5240}
5241
5242static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5243 struct cgroup *child)
5244{
5245 int subsys_id, i, depth = 0;
5246 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005247 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005248
5249 subsys_id = ss->subsys_id;
5250 parent_css = parent->subsys[subsys_id];
5251 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005253 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005254
5255 child_id = get_new_cssid(ss, depth);
5256 if (IS_ERR(child_id))
5257 return PTR_ERR(child_id);
5258
5259 for (i = 0; i < depth; i++)
5260 child_id->stack[i] = parent_id->stack[i];
5261 child_id->stack[depth] = child_id->id;
5262 /*
5263 * child_id->css pointer will be set after this cgroup is available
5264 * see cgroup_populate_dir()
5265 */
5266 rcu_assign_pointer(child_css->id, child_id);
5267
5268 return 0;
5269}
5270
5271/**
5272 * css_lookup - lookup css by id
5273 * @ss: cgroup subsys to be looked into.
5274 * @id: the id
5275 *
5276 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5277 * NULL if not. Should be called under rcu_read_lock()
5278 */
5279struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5280{
5281 struct css_id *cssid = NULL;
5282
5283 BUG_ON(!ss->use_id);
5284 cssid = idr_find(&ss->idr, id);
5285
5286 if (unlikely(!cssid))
5287 return NULL;
5288
5289 return rcu_dereference(cssid->css);
5290}
Ben Blum67523c42010-03-10 15:22:11 -08005291EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005292
5293/**
5294 * css_get_next - lookup next cgroup under specified hierarchy.
5295 * @ss: pointer to subsystem
5296 * @id: current position of iteration.
5297 * @root: pointer to css. search tree under this.
5298 * @foundid: position of found object.
5299 *
5300 * Search next css under the specified hierarchy of rootid. Calling under
5301 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5302 */
5303struct cgroup_subsys_state *
5304css_get_next(struct cgroup_subsys *ss, int id,
5305 struct cgroup_subsys_state *root, int *foundid)
5306{
5307 struct cgroup_subsys_state *ret = NULL;
5308 struct css_id *tmp;
5309 int tmpid;
5310 int rootid = css_id(root);
5311 int depth = css_depth(root);
5312
5313 if (!rootid)
5314 return NULL;
5315
5316 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005317 WARN_ON_ONCE(!rcu_read_lock_held());
5318
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005319 /* fill start point for scan */
5320 tmpid = id;
5321 while (1) {
5322 /*
5323 * scan next entry from bitmap(tree), tmpid is updated after
5324 * idr_get_next().
5325 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005326 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005327 if (!tmp)
5328 break;
5329 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5330 ret = rcu_dereference(tmp->css);
5331 if (ret) {
5332 *foundid = tmpid;
5333 break;
5334 }
5335 }
5336 /* continue to scan from next id */
5337 tmpid = tmpid + 1;
5338 }
5339 return ret;
5340}
5341
Stephane Eraniane5d13672011-02-14 11:20:01 +02005342/*
5343 * get corresponding css from file open on cgroupfs directory
5344 */
5345struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5346{
5347 struct cgroup *cgrp;
5348 struct inode *inode;
5349 struct cgroup_subsys_state *css;
5350
Al Viro496ad9a2013-01-23 17:07:38 -05005351 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005352 /* check in cgroup filesystem dir */
5353 if (inode->i_op != &cgroup_dir_inode_operations)
5354 return ERR_PTR(-EBADF);
5355
5356 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5357 return ERR_PTR(-EINVAL);
5358
5359 /* get cgroup */
5360 cgrp = __d_cgrp(f->f_dentry);
5361 css = cgrp->subsys[id];
5362 return css ? css : ERR_PTR(-ENOENT);
5363}
5364
Paul Menagefe693432009-09-23 15:56:20 -07005365#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005366static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005367{
5368 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5369
5370 if (!css)
5371 return ERR_PTR(-ENOMEM);
5372
5373 return css;
5374}
5375
Tejun Heo92fb9742012-11-19 08:13:38 -08005376static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005377{
5378 kfree(cont->subsys[debug_subsys_id]);
5379}
5380
5381static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5382{
5383 return atomic_read(&cont->count);
5384}
5385
5386static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5387{
5388 return cgroup_task_count(cont);
5389}
5390
5391static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5392{
5393 return (u64)(unsigned long)current->cgroups;
5394}
5395
5396static u64 current_css_set_refcount_read(struct cgroup *cont,
5397 struct cftype *cft)
5398{
5399 u64 count;
5400
5401 rcu_read_lock();
5402 count = atomic_read(&current->cgroups->refcount);
5403 rcu_read_unlock();
5404 return count;
5405}
5406
Paul Menage7717f7b2009-09-23 15:56:22 -07005407static int current_css_set_cg_links_read(struct cgroup *cont,
5408 struct cftype *cft,
5409 struct seq_file *seq)
5410{
5411 struct cg_cgroup_link *link;
5412 struct css_set *cg;
5413
5414 read_lock(&css_set_lock);
5415 rcu_read_lock();
5416 cg = rcu_dereference(current->cgroups);
5417 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5418 struct cgroup *c = link->cgrp;
5419 const char *name;
5420
5421 if (c->dentry)
5422 name = c->dentry->d_name.name;
5423 else
5424 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005425 seq_printf(seq, "Root %d group %s\n",
5426 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005427 }
5428 rcu_read_unlock();
5429 read_unlock(&css_set_lock);
5430 return 0;
5431}
5432
5433#define MAX_TASKS_SHOWN_PER_CSS 25
5434static int cgroup_css_links_read(struct cgroup *cont,
5435 struct cftype *cft,
5436 struct seq_file *seq)
5437{
5438 struct cg_cgroup_link *link;
5439
5440 read_lock(&css_set_lock);
5441 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5442 struct css_set *cg = link->cg;
5443 struct task_struct *task;
5444 int count = 0;
5445 seq_printf(seq, "css_set %p\n", cg);
5446 list_for_each_entry(task, &cg->tasks, cg_list) {
5447 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5448 seq_puts(seq, " ...\n");
5449 break;
5450 } else {
5451 seq_printf(seq, " task %d\n",
5452 task_pid_vnr(task));
5453 }
5454 }
5455 }
5456 read_unlock(&css_set_lock);
5457 return 0;
5458}
5459
Paul Menagefe693432009-09-23 15:56:20 -07005460static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5461{
5462 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5463}
5464
5465static struct cftype debug_files[] = {
5466 {
5467 .name = "cgroup_refcount",
5468 .read_u64 = cgroup_refcount_read,
5469 },
5470 {
5471 .name = "taskcount",
5472 .read_u64 = debug_taskcount_read,
5473 },
5474
5475 {
5476 .name = "current_css_set",
5477 .read_u64 = current_css_set_read,
5478 },
5479
5480 {
5481 .name = "current_css_set_refcount",
5482 .read_u64 = current_css_set_refcount_read,
5483 },
5484
5485 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005486 .name = "current_css_set_cg_links",
5487 .read_seq_string = current_css_set_cg_links_read,
5488 },
5489
5490 {
5491 .name = "cgroup_css_links",
5492 .read_seq_string = cgroup_css_links_read,
5493 },
5494
5495 {
Paul Menagefe693432009-09-23 15:56:20 -07005496 .name = "releasable",
5497 .read_u64 = releasable_read,
5498 },
Paul Menagefe693432009-09-23 15:56:20 -07005499
Tejun Heo4baf6e32012-04-01 12:09:55 -07005500 { } /* terminate */
5501};
Paul Menagefe693432009-09-23 15:56:20 -07005502
5503struct cgroup_subsys debug_subsys = {
5504 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005505 .css_alloc = debug_css_alloc,
5506 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005507 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005508 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005509};
5510#endif /* CONFIG_CGROUP_DEBUG */