blob: 6b2b1d945df2c4fd6a3b7334aabfcc72faf7d16e [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>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heo28b4c272012-04-01 12:09:56 -070066/* css deactivation bias, makes css->refcnt negative to deny new trygets */
67#define CSS_DEACT_BIAS INT_MIN
68
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
73 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
74 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
75 * release_agent_path and so on. Modifying requires both cgroup_mutex and
76 * cgroup_root_mutex. Readers can acquire either of the two. This is to
77 * break the following locking order cycle.
78 *
79 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
80 * B. namespace_sem -> cgroup_mutex
81 *
82 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 * breaks it.
84 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
88#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heoe25e2cb2011-12-12 18:12:21 -080092static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Ben Blumaae8aab2010-03-10 15:22:07 -080094/*
95 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020096 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080097 * registered after that. The mutable section of this array is protected by
98 * cgroup_mutex.
99 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200100#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200101#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800102static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103#include <linux/cgroup_subsys.h>
104};
105
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106/*
107 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
108 * subsystems that are otherwise unattached - it never has more than a
109 * single cgroup, and all tasks are part of that cgroup.
110 */
111static struct cgroupfs_root rootnode;
112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
189static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700190static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
200#define dummytop (&rootnode.top_cgroup)
201
Li Zefan65dff752013-03-01 15:01:56 +0800202static struct cgroup_name root_cgroup_name = { .name = "/" };
203
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800205 * check for fork/exit handlers to call. This avoids us having to do
206 * extra work in the fork/exit path if none of the subsystems need to
207 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700209static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Tejun Heo42809dd2012-11-19 08:13:37 -0800211static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800212static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
213 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800214
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700215static int css_unbias_refcnt(int refcnt)
216{
217 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
218}
219
Tejun Heo28b4c272012-04-01 12:09:56 -0700220/* the current nr of refs, always >= 0 whether @css is deactivated or not */
221static int css_refcnt(struct cgroup_subsys_state *css)
222{
223 int v = atomic_read(&css->refcnt);
224
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700225 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700226}
227
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700229inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230{
Paul Menagebd89aab2007-10-18 23:40:44 -0700231 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700232}
233
Li Zefan78574cf2013-04-08 19:00:38 -0700234/**
235 * cgroup_is_descendant - test ancestry
236 * @cgrp: the cgroup to be tested
237 * @ancestor: possible ancestor of @cgrp
238 *
239 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
240 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
241 * and @ancestor are accessible.
242 */
243bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
244{
245 while (cgrp) {
246 if (cgrp == ancestor)
247 return true;
248 cgrp = cgrp->parent;
249 }
250 return false;
251}
252EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253
Adrian Bunke9685a02008-02-07 00:13:46 -0800254static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255{
256 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700257 (1 << CGRP_RELEASABLE) |
258 (1 << CGRP_NOTIFY_ON_RELEASE);
259 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260}
261
Adrian Bunke9685a02008-02-07 00:13:46 -0800262static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700263{
Paul Menagebd89aab2007-10-18 23:40:44 -0700264 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700265}
266
Paul Menageddbcc7e2007-10-18 23:39:30 -0700267/*
268 * for_each_subsys() allows you to iterate on each subsystem attached to
269 * an active hierarchy
270 */
271#define for_each_subsys(_root, _ss) \
272list_for_each_entry(_ss, &_root->subsys_list, sibling)
273
Li Zefane5f6a862009-01-07 18:07:41 -0800274/* for_each_active_root() allows you to iterate across the active hierarchies */
275#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276list_for_each_entry(_root, &roots, root_list)
277
Tejun Heof6ea9372012-04-01 12:09:55 -0700278static inline struct cgroup *__d_cgrp(struct dentry *dentry)
279{
280 return dentry->d_fsdata;
281}
282
Tejun Heo05ef1d72012-04-01 12:09:56 -0700283static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700284{
285 return dentry->d_fsdata;
286}
287
Tejun Heo05ef1d72012-04-01 12:09:56 -0700288static inline struct cftype *__d_cft(struct dentry *dentry)
289{
290 return __d_cfe(dentry)->type;
291}
292
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293/**
294 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
295 * @cgrp: the cgroup to be checked for liveness
296 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700297 * On success, returns true; the mutex should be later unlocked. On
298 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700299 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700300static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700301{
302 mutex_lock(&cgroup_mutex);
303 if (cgroup_is_removed(cgrp)) {
304 mutex_unlock(&cgroup_mutex);
305 return false;
306 }
307 return true;
308}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700309
Paul Menage81a6a5c2007-10-18 23:39:38 -0700310/* the list of cgroups eligible for automatic release. Protected by
311 * release_list_lock */
312static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200313static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314static void cgroup_release_agent(struct work_struct *work);
315static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700316static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700317
Paul Menage817929e2007-10-18 23:39:36 -0700318/* Link structure for associating css_set objects with cgroups */
319struct cg_cgroup_link {
320 /*
321 * List running through cg_cgroup_links associated with a
322 * cgroup, anchored on cgroup->css_sets
323 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700324 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700325 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700326 /*
327 * List running through cg_cgroup_links pointing at a
328 * single css_set object, anchored on css_set->cg_links
329 */
330 struct list_head cg_link_list;
331 struct css_set *cg;
332};
333
334/* The default css_set - used by init and its children prior to any
335 * hierarchies being mounted. It contains a pointer to the root state
336 * for each subsystem. Also used to anchor the list of css_sets. Not
337 * reference-counted, to improve performance when child cgroups
338 * haven't been created.
339 */
340
341static struct css_set init_css_set;
342static struct cg_cgroup_link init_css_set_link;
343
Ben Blume6a11052010-03-10 15:22:09 -0800344static int cgroup_init_idr(struct cgroup_subsys *ss,
345 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700346
Paul Menage817929e2007-10-18 23:39:36 -0700347/* css_set_lock protects the list of css_set objects, and the
348 * chain of tasks off each css_set. Nests outside task->alloc_lock
349 * due to cgroup_iter_start() */
350static DEFINE_RWLOCK(css_set_lock);
351static int css_set_count;
352
Paul Menage7717f7b2009-09-23 15:56:22 -0700353/*
354 * hash table for cgroup groups. This improves the performance to find
355 * an existing css_set. This hash doesn't (currently) take into
356 * account cgroups in empty hierarchies.
357 */
Li Zefan472b1052008-04-29 01:00:11 -0700358#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800359static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700360
Li Zefan0ac801f2013-01-10 11:49:27 +0800361static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700362{
363 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800364 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700365
366 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800367 key += (unsigned long)css[i];
368 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700369
Li Zefan0ac801f2013-01-10 11:49:27 +0800370 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700371}
372
Paul Menage817929e2007-10-18 23:39:36 -0700373/* We don't maintain the lists running through each css_set to its
374 * task until after the first call to cgroup_iter_start(). This
375 * reduces the fork()/exit() overhead for people who have cgroups
376 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700377static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700378
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700379static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700380{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700381 struct cg_cgroup_link *link;
382 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700383 /*
384 * Ensure that the refcount doesn't hit zero while any readers
385 * can see it. Similar to atomic_dec_and_lock(), but for an
386 * rwlock
387 */
388 if (atomic_add_unless(&cg->refcount, -1, 1))
389 return;
390 write_lock(&css_set_lock);
391 if (!atomic_dec_and_test(&cg->refcount)) {
392 write_unlock(&css_set_lock);
393 return;
394 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700395
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700396 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800397 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700398 css_set_count--;
399
400 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
401 cg_link_list) {
402 struct cgroup *cgrp = link->cgrp;
403 list_del(&link->cg_link_list);
404 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800405
406 /*
407 * We may not be holding cgroup_mutex, and if cgrp->count is
408 * dropped to 0 the cgroup can be destroyed at any time, hence
409 * rcu_read_lock is used to keep it alive.
410 */
411 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700412 if (atomic_dec_and_test(&cgrp->count) &&
413 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700414 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700415 set_bit(CGRP_RELEASABLE, &cgrp->flags);
416 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417 }
Li Zefan71b57072013-01-24 14:43:28 +0800418 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419
420 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700421 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422
423 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800424 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700425}
426
427/*
428 * refcounted get/put for css_set objects
429 */
430static inline void get_css_set(struct css_set *cg)
431{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700432 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700433}
434
435static inline void put_css_set(struct css_set *cg)
436{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700437 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700438}
439
Paul Menage81a6a5c2007-10-18 23:39:38 -0700440static inline void put_css_set_taskexit(struct css_set *cg)
441{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700442 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700443}
444
Paul Menage817929e2007-10-18 23:39:36 -0700445/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 * compare_css_sets - helper function for find_existing_css_set().
447 * @cg: candidate css_set being tested
448 * @old_cg: existing css_set for a task
449 * @new_cgrp: cgroup that's being entered by the task
450 * @template: desired set of css pointers in css_set (pre-calculated)
451 *
452 * Returns true if "cg" matches "old_cg" except for the hierarchy
453 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
454 */
455static bool compare_css_sets(struct css_set *cg,
456 struct css_set *old_cg,
457 struct cgroup *new_cgrp,
458 struct cgroup_subsys_state *template[])
459{
460 struct list_head *l1, *l2;
461
462 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
463 /* Not all subsystems matched */
464 return false;
465 }
466
467 /*
468 * Compare cgroup pointers in order to distinguish between
469 * different cgroups in heirarchies with no subsystems. We
470 * could get by with just this check alone (and skip the
471 * memcmp above) but on most setups the memcmp check will
472 * avoid the need for this more expensive check on almost all
473 * candidates.
474 */
475
476 l1 = &cg->cg_links;
477 l2 = &old_cg->cg_links;
478 while (1) {
479 struct cg_cgroup_link *cgl1, *cgl2;
480 struct cgroup *cg1, *cg2;
481
482 l1 = l1->next;
483 l2 = l2->next;
484 /* See if we reached the end - both lists are equal length. */
485 if (l1 == &cg->cg_links) {
486 BUG_ON(l2 != &old_cg->cg_links);
487 break;
488 } else {
489 BUG_ON(l2 == &old_cg->cg_links);
490 }
491 /* Locate the cgroups associated with these links. */
492 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
493 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
494 cg1 = cgl1->cgrp;
495 cg2 = cgl2->cgrp;
496 /* Hierarchies should be linked in the same order. */
497 BUG_ON(cg1->root != cg2->root);
498
499 /*
500 * If this hierarchy is the hierarchy of the cgroup
501 * that's changing, then we need to check that this
502 * css_set points to the new cgroup; if it's any other
503 * hierarchy, then this css_set should point to the
504 * same cgroup as the old css_set.
505 */
506 if (cg1->root == new_cgrp->root) {
507 if (cg1 != new_cgrp)
508 return false;
509 } else {
510 if (cg1 != cg2)
511 return false;
512 }
513 }
514 return true;
515}
516
517/*
Paul Menage817929e2007-10-18 23:39:36 -0700518 * find_existing_css_set() is a helper for
519 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700520 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700521 *
522 * oldcg: the cgroup group that we're using before the cgroup
523 * transition
524 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700525 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700526 *
527 * template: location in which to build the desired set of subsystem
528 * state objects for the new cgroup group
529 */
Paul Menage817929e2007-10-18 23:39:36 -0700530static struct css_set *find_existing_css_set(
531 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700532 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700533 struct cgroup_subsys_state *template[])
534{
535 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700536 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700537 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800538 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700539
Ben Blumaae8aab2010-03-10 15:22:07 -0800540 /*
541 * Build the set of subsystem state objects that we want to see in the
542 * new css_set. while subsystems can change globally, the entries here
543 * won't change, so no need for locking.
544 */
Paul Menage817929e2007-10-18 23:39:36 -0700545 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400546 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700547 /* Subsystem is in this hierarchy. So we want
548 * the subsystem state from the new
549 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700550 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700551 } else {
552 /* Subsystem is not in this hierarchy, so we
553 * don't want to change the subsystem state */
554 template[i] = oldcg->subsys[i];
555 }
556 }
557
Li Zefan0ac801f2013-01-10 11:49:27 +0800558 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800559 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700560 if (!compare_css_sets(cg, oldcg, cgrp, template))
561 continue;
562
563 /* This css_set matches what we need */
564 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700565 }
Paul Menage817929e2007-10-18 23:39:36 -0700566
567 /* No existing cgroup group matched */
568 return NULL;
569}
570
Paul Menage817929e2007-10-18 23:39:36 -0700571static void free_cg_links(struct list_head *tmp)
572{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700573 struct cg_cgroup_link *link;
574 struct cg_cgroup_link *saved_link;
575
576 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700577 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700578 kfree(link);
579 }
580}
581
582/*
Li Zefan36553432008-07-29 22:33:19 -0700583 * allocate_cg_links() allocates "count" cg_cgroup_link structures
584 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
585 * success or a negative error
586 */
587static int allocate_cg_links(int count, struct list_head *tmp)
588{
589 struct cg_cgroup_link *link;
590 int i;
591 INIT_LIST_HEAD(tmp);
592 for (i = 0; i < count; i++) {
593 link = kmalloc(sizeof(*link), GFP_KERNEL);
594 if (!link) {
595 free_cg_links(tmp);
596 return -ENOMEM;
597 }
598 list_add(&link->cgrp_link_list, tmp);
599 }
600 return 0;
601}
602
Li Zefanc12f65d2009-01-07 18:07:42 -0800603/**
604 * link_css_set - a helper function to link a css_set to a cgroup
605 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
606 * @cg: the css_set to be linked
607 * @cgrp: the destination cgroup
608 */
609static void link_css_set(struct list_head *tmp_cg_links,
610 struct css_set *cg, struct cgroup *cgrp)
611{
612 struct cg_cgroup_link *link;
613
614 BUG_ON(list_empty(tmp_cg_links));
615 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
616 cgrp_link_list);
617 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700618 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700619 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800620 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700621 /*
622 * Always add links to the tail of the list so that the list
623 * is sorted by order of hierarchy creation
624 */
625 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800626}
627
Li Zefan36553432008-07-29 22:33:19 -0700628/*
Paul Menage817929e2007-10-18 23:39:36 -0700629 * find_css_set() takes an existing cgroup group and a
630 * cgroup object, and returns a css_set object that's
631 * equivalent to the old group, but with the given cgroup
632 * substituted into the appropriate hierarchy. Must be called with
633 * cgroup_mutex held
634 */
Paul Menage817929e2007-10-18 23:39:36 -0700635static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700636 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700637{
638 struct css_set *res;
639 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700640
641 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700642
Paul Menage7717f7b2009-09-23 15:56:22 -0700643 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800644 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700645
Paul Menage817929e2007-10-18 23:39:36 -0700646 /* First see if we already have a cgroup group that matches
647 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700648 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700649 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700650 if (res)
651 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700652 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700653
654 if (res)
655 return res;
656
657 res = kmalloc(sizeof(*res), GFP_KERNEL);
658 if (!res)
659 return NULL;
660
661 /* Allocate all the cg_cgroup_link objects that we'll need */
662 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
663 kfree(res);
664 return NULL;
665 }
666
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700667 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700668 INIT_LIST_HEAD(&res->cg_links);
669 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700670 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700671
672 /* Copy the set of subsystem state objects generated in
673 * find_existing_css_set() */
674 memcpy(res->subsys, template, sizeof(res->subsys));
675
676 write_lock(&css_set_lock);
677 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700678 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
679 struct cgroup *c = link->cgrp;
680 if (c->root == cgrp->root)
681 c = cgrp;
682 link_css_set(&tmp_cg_links, res, c);
683 }
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 BUG_ON(!list_empty(&tmp_cg_links));
686
Paul Menage817929e2007-10-18 23:39:36 -0700687 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700688
689 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800690 key = css_set_hash(res->subsys);
691 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700692
Paul Menage817929e2007-10-18 23:39:36 -0700693 write_unlock(&css_set_lock);
694
695 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700696}
697
Paul Menageddbcc7e2007-10-18 23:39:30 -0700698/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700699 * Return the cgroup for "task" from the given hierarchy. Must be
700 * called with cgroup_mutex held.
701 */
702static struct cgroup *task_cgroup_from_root(struct task_struct *task,
703 struct cgroupfs_root *root)
704{
705 struct css_set *css;
706 struct cgroup *res = NULL;
707
708 BUG_ON(!mutex_is_locked(&cgroup_mutex));
709 read_lock(&css_set_lock);
710 /*
711 * No need to lock the task - since we hold cgroup_mutex the
712 * task can't change groups, so the only thing that can happen
713 * is that it exits and its css is set back to init_css_set.
714 */
715 css = task->cgroups;
716 if (css == &init_css_set) {
717 res = &root->top_cgroup;
718 } else {
719 struct cg_cgroup_link *link;
720 list_for_each_entry(link, &css->cg_links, cg_link_list) {
721 struct cgroup *c = link->cgrp;
722 if (c->root == root) {
723 res = c;
724 break;
725 }
726 }
727 }
728 read_unlock(&css_set_lock);
729 BUG_ON(!res);
730 return res;
731}
732
733/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734 * There is one global cgroup mutex. We also require taking
735 * task_lock() when dereferencing a task's cgroup subsys pointers.
736 * See "The task_lock() exception", at the end of this comment.
737 *
738 * A task must hold cgroup_mutex to modify cgroups.
739 *
740 * Any task can increment and decrement the count field without lock.
741 * So in general, code holding cgroup_mutex can't rely on the count
742 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800743 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700744 * means that no tasks are currently attached, therefore there is no
745 * way a task attached to that cgroup can fork (the other way to
746 * increment the count). So code holding cgroup_mutex can safely
747 * assume that if the count is zero, it will stay zero. Similarly, if
748 * a task holds cgroup_mutex on a cgroup with zero count, it
749 * knows that the cgroup won't be removed, as cgroup_rmdir()
750 * needs that mutex.
751 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
753 * (usually) take cgroup_mutex. These are the two most performance
754 * critical pieces of code here. The exception occurs on cgroup_exit(),
755 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
756 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800757 * to the release agent with the name of the cgroup (path relative to
758 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 *
760 * A cgroup can only be deleted if both its 'count' of using tasks
761 * is zero, and its list of 'children' cgroups is empty. Since all
762 * tasks in the system use _some_ cgroup, and since there is always at
763 * least one task in the system (init, pid == 1), therefore, top_cgroup
764 * always has either children cgroups and/or using tasks. So we don't
765 * need a special hack to ensure that top_cgroup cannot be deleted.
766 *
767 * The task_lock() exception
768 *
769 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800770 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800771 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 * several performance critical places that need to reference
773 * task->cgroup without the expense of grabbing a system global
774 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800775 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
777 * the task_struct routinely used for such matters.
778 *
779 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800780 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781 */
782
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783/*
784 * A couple of forward declarations required, due to cyclic reference loop:
785 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
786 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
787 * -> cgroup_mkdir.
788 */
789
Al Viro18bb1db2011-07-26 01:41:39 -0400790static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400791static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400793static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
794 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700795static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700796static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700797
798static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200799 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700800 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700801};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700803static int alloc_css_id(struct cgroup_subsys *ss,
804 struct cgroup *parent, struct cgroup *child);
805
Al Viroa5e7ed32011-07-26 01:55:55 -0400806static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807{
808 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
810 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400811 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100813 inode->i_uid = current_fsuid();
814 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
816 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
817 }
818 return inode;
819}
820
Li Zefan65dff752013-03-01 15:01:56 +0800821static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
822{
823 struct cgroup_name *name;
824
825 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
826 if (!name)
827 return NULL;
828 strcpy(name->name, dentry->d_name.name);
829 return name;
830}
831
Li Zefanbe445622013-01-24 14:31:42 +0800832static void cgroup_free_fn(struct work_struct *work)
833{
834 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
835 struct cgroup_subsys *ss;
836
837 mutex_lock(&cgroup_mutex);
838 /*
839 * Release the subsystem state objects.
840 */
841 for_each_subsys(cgrp->root, ss)
842 ss->css_free(cgrp);
843
844 cgrp->root->number_of_cgroups--;
845 mutex_unlock(&cgroup_mutex);
846
847 /*
Li Zefan415cf072013-04-08 14:35:02 +0800848 * We get a ref to the parent's dentry, and put the ref when
849 * this cgroup is being freed, so it's guaranteed that the
850 * parent won't be destroyed before its children.
851 */
852 dput(cgrp->parent->dentry);
853
Li Zefancc20e012013-04-26 11:58:02 -0700854 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
855
Li Zefan415cf072013-04-08 14:35:02 +0800856 /*
Li Zefanbe445622013-01-24 14:31:42 +0800857 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700858 * created the cgroup. This will free cgrp->root, if we are
859 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800860 */
861 deactivate_super(cgrp->root->sb);
862
863 /*
864 * if we're getting rid of the cgroup, refcount should ensure
865 * that there are no pidlists left.
866 */
867 BUG_ON(!list_empty(&cgrp->pidlists));
868
869 simple_xattrs_free(&cgrp->xattrs);
870
Li Zefan65dff752013-03-01 15:01:56 +0800871 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800872 kfree(cgrp);
873}
874
875static void cgroup_free_rcu(struct rcu_head *head)
876{
877 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
878
879 schedule_work(&cgrp->free_work);
880}
881
Paul Menageddbcc7e2007-10-18 23:39:30 -0700882static void cgroup_diput(struct dentry *dentry, struct inode *inode)
883{
884 /* is dentry a directory ? if so, kfree() associated cgroup */
885 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700886 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800887
Paul Menagebd89aab2007-10-18 23:40:44 -0700888 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800889 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700890 } else {
891 struct cfent *cfe = __d_cfe(dentry);
892 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
893
894 WARN_ONCE(!list_empty(&cfe->node) &&
895 cgrp != &cgrp->root->top_cgroup,
896 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700897 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700898 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899 }
900 iput(inode);
901}
902
Al Viroc72a04e2011-01-14 05:31:45 +0000903static int cgroup_delete(const struct dentry *d)
904{
905 return 1;
906}
907
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908static void remove_dir(struct dentry *d)
909{
910 struct dentry *parent = dget(d->d_parent);
911
912 d_delete(d);
913 simple_rmdir(parent->d_inode, d);
914 dput(parent);
915}
916
Li Zefan2739d3c2013-01-21 18:18:33 +0800917static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920
Tejun Heo05ef1d72012-04-01 12:09:56 -0700921 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
922 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100923
Li Zefan2739d3c2013-01-21 18:18:33 +0800924 /*
925 * If we're doing cleanup due to failure of cgroup_create(),
926 * the corresponding @cfe may not exist.
927 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 list_for_each_entry(cfe, &cgrp->files, node) {
929 struct dentry *d = cfe->dentry;
930
931 if (cft && cfe->type != cft)
932 continue;
933
934 dget(d);
935 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700936 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 list_del_init(&cfe->node);
938 dput(d);
939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942}
943
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400944/**
945 * cgroup_clear_directory - selective removal of base and subsystem files
946 * @dir: directory containing the files
947 * @base_files: true if the base files should be removed
948 * @subsys_mask: mask of the subsystem ids whose files should be removed
949 */
950static void cgroup_clear_directory(struct dentry *dir, bool base_files,
951 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952{
953 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400954 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700955
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400956 for_each_subsys(cgrp->root, ss) {
957 struct cftype_set *set;
958 if (!test_bit(ss->subsys_id, &subsys_mask))
959 continue;
960 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800961 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400962 }
963 if (base_files) {
964 while (!list_empty(&cgrp->files))
965 cgroup_rm_file(cgrp, NULL);
966 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967}
968
969/*
970 * NOTE : the dentry must have been dget()'ed
971 */
972static void cgroup_d_remove_dir(struct dentry *dentry)
973{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100974 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100976
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400977 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100979 parent = dentry->d_parent;
980 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800981 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983 spin_unlock(&dentry->d_lock);
984 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985 remove_dir(dentry);
986}
987
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700988/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800989 * Call with cgroup_mutex held. Drops reference counts on modules, including
990 * any duplicate ones that parse_cgroupfs_options took. If this function
991 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800992 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400994 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400996 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700997 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 int i;
999
Ben Blumaae8aab2010-03-10 15:22:07 -08001000 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001001 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001002
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001003 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1004 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 /* Check that any added subsystems are currently free */
1006 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001007 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001009 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001011 /*
1012 * Nobody should tell us to do a subsys that doesn't exist:
1013 * parse_cgroupfs_options should catch that case and refcounts
1014 * ensure that subsystems won't disappear once selected.
1015 */
1016 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 if (ss->root != &rootnode) {
1018 /* Subsystem isn't free */
1019 return -EBUSY;
1020 }
1021 }
1022
1023 /* Currently we don't handle adding/removing subsystems when
1024 * any child cgroups exist. This is theoretically supportable
1025 * but involves complex error handling, so it's being left until
1026 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001027 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 return -EBUSY;
1029
1030 /* Process each subsystem */
1031 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1032 struct cgroup_subsys *ss = subsys[i];
1033 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001034 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001036 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001037 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 BUG_ON(!dummytop->subsys[i]);
1039 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001040 cgrp->subsys[i] = dummytop->subsys[i];
1041 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001042 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001043 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001045 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001046 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001047 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001049 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001050 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1051 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001053 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001055 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001056 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001057 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001058 /* subsystem is now free - drop reference on module */
1059 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001060 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001062 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001063 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /*
1065 * a refcount was taken, but we already had one, so
1066 * drop the extra reference.
1067 */
1068 module_put(ss->module);
1069#ifdef CONFIG_MODULE_UNLOAD
1070 BUG_ON(ss->module && !module_refcount(ss->module));
1071#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 } else {
1073 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 }
1076 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001077 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078
1079 return 0;
1080}
1081
Al Viro34c80b12011-12-08 21:32:45 -05001082static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083{
Al Viro34c80b12011-12-08 21:32:45 -05001084 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 struct cgroup_subsys *ss;
1086
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001087 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 for_each_subsys(root, ss)
1089 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001090 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1091 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001092 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001094 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001095 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001096 if (strlen(root->release_agent_path))
1097 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001098 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001099 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001100 if (strlen(root->name))
1101 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001102 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 return 0;
1104}
1105
1106struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001107 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001109 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001110 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001111 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001112 /* User explicitly requested empty subsystem */
1113 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001114
1115 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001116
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117};
1118
Ben Blumaae8aab2010-03-10 15:22:07 -08001119/*
1120 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001121 * with cgroup_mutex held to protect the subsys[] array. This function takes
1122 * refcounts on subsystems to be used, unless it returns error, in which case
1123 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001124 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001125static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001127 char *token, *o = data;
1128 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001129 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001130 int i;
1131 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001132
Ben Blumaae8aab2010-03-10 15:22:07 -08001133 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1134
Li Zefanf9ab5b52009-06-17 16:26:33 -07001135#ifdef CONFIG_CPUSETS
1136 mask = ~(1UL << cpuset_subsys_id);
1137#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138
Paul Menagec6d57f32009-09-23 15:56:19 -07001139 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140
1141 while ((token = strsep(&o, ",")) != NULL) {
1142 if (!*token)
1143 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001145 /* Explicitly have no subsystems */
1146 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 continue;
1148 }
1149 if (!strcmp(token, "all")) {
1150 /* Mutually exclusive option 'all' + subsystem name */
1151 if (one_ss)
1152 return -EINVAL;
1153 all_ss = true;
1154 continue;
1155 }
Tejun Heo873fe092013-04-14 20:15:26 -07001156 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1157 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1158 continue;
1159 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001161 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001162 continue;
1163 }
1164 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001165 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 continue;
1167 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001168 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001169 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001170 continue;
1171 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001173 /* Specifying two release agents is forbidden */
1174 if (opts->release_agent)
1175 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001176 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001177 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001178 if (!opts->release_agent)
1179 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 continue;
1181 }
1182 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 const char *name = token + 5;
1184 /* Can't specify an empty name */
1185 if (!strlen(name))
1186 return -EINVAL;
1187 /* Must match [\w.-]+ */
1188 for (i = 0; i < strlen(name); i++) {
1189 char c = name[i];
1190 if (isalnum(c))
1191 continue;
1192 if ((c == '.') || (c == '-') || (c == '_'))
1193 continue;
1194 return -EINVAL;
1195 }
1196 /* Specifying two names is forbidden */
1197 if (opts->name)
1198 return -EINVAL;
1199 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001200 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001201 GFP_KERNEL);
1202 if (!opts->name)
1203 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001204
1205 continue;
1206 }
1207
1208 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1209 struct cgroup_subsys *ss = subsys[i];
1210 if (ss == NULL)
1211 continue;
1212 if (strcmp(token, ss->name))
1213 continue;
1214 if (ss->disabled)
1215 continue;
1216
1217 /* Mutually exclusive option 'all' + subsystem name */
1218 if (all_ss)
1219 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001220 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221 one_ss = true;
1222
1223 break;
1224 }
1225 if (i == CGROUP_SUBSYS_COUNT)
1226 return -ENOENT;
1227 }
1228
1229 /*
1230 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 * otherwise if 'none', 'name=' and a subsystem name options
1232 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001233 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001234 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1236 struct cgroup_subsys *ss = subsys[i];
1237 if (ss == NULL)
1238 continue;
1239 if (ss->disabled)
1240 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001241 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242 }
1243 }
1244
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001245 /* Consistency checks */
1246
Tejun Heo873fe092013-04-14 20:15:26 -07001247 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1248 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1249
1250 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1251 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1252 return -EINVAL;
1253 }
1254
1255 if (opts->cpuset_clone_children) {
1256 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1257 return -EINVAL;
1258 }
1259 }
1260
Li Zefanf9ab5b52009-06-17 16:26:33 -07001261 /*
1262 * Option noprefix was introduced just for backward compatibility
1263 * with the old cpuset, so we allow noprefix only if mounting just
1264 * the cpuset subsystem.
1265 */
Tejun Heo93438622013-04-14 20:15:25 -07001266 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001267 return -EINVAL;
1268
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001269
1270 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001271 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001272 return -EINVAL;
1273
1274 /*
1275 * We either have to specify by name or by subsystems. (So all
1276 * empty hierarchies must have a name).
1277 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001278 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279 return -EINVAL;
1280
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 /*
1282 * Grab references on all the modules we'll need, so the subsystems
1283 * don't dance around before rebind_subsystems attaches them. This may
1284 * take duplicate reference counts on a subsystem that's already used,
1285 * but rebind_subsystems handles this case.
1286 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001287 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 unsigned long bit = 1UL << i;
1289
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001290 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001291 continue;
1292 if (!try_module_get(subsys[i]->module)) {
1293 module_pin_failed = true;
1294 break;
1295 }
1296 }
1297 if (module_pin_failed) {
1298 /*
1299 * oops, one of the modules was going away. this means that we
1300 * raced with a module_delete call, and to the user this is
1301 * essentially a "subsystem doesn't exist" case.
1302 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001303 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001304 /* drop refcounts only on the ones we took */
1305 unsigned long bit = 1UL << i;
1306
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001307 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 continue;
1309 module_put(subsys[i]->module);
1310 }
1311 return -ENOENT;
1312 }
1313
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 return 0;
1315}
1316
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001318{
1319 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001320 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 unsigned long bit = 1UL << i;
1322
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001323 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001324 continue;
1325 module_put(subsys[i]->module);
1326 }
1327}
1328
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1330{
1331 int ret = 0;
1332 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001333 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336
Tejun Heo873fe092013-04-14 20:15:26 -07001337 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1338 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1339 return -EINVAL;
1340 }
1341
Paul Menagebd89aab2007-10-18 23:40:44 -07001342 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001344 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345
1346 /* See what subsystems are wanted */
1347 ret = parse_cgroupfs_options(data, &opts);
1348 if (ret)
1349 goto out_unlock;
1350
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001351 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001352 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1353 task_tgid_nr(current), current->comm);
1354
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001355 added_mask = opts.subsys_mask & ~root->subsys_mask;
1356 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001357
Ben Blumcf5d5942010-03-10 15:22:09 -08001358 /* Don't allow flags or name to change at remount */
1359 if (opts.flags != root->flags ||
1360 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001363 goto out_unlock;
1364 }
1365
Gao feng7083d032012-12-03 09:28:18 +08001366 /*
1367 * Clear out the files of subsystems that should be removed, do
1368 * this before rebind_subsystems, since rebind_subsystems may
1369 * change this hierarchy's subsys_list.
1370 */
1371 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1372
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001373 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001374 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001375 /* rebind_subsystems failed, re-populate the removed files */
1376 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001377 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001378 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001381 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001382 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383
Paul Menage81a6a5c2007-10-18 23:39:38 -07001384 if (opts.release_agent)
1385 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001387 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001388 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001389 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001391 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 return ret;
1393}
1394
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001395static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 .statfs = simple_statfs,
1397 .drop_inode = generic_delete_inode,
1398 .show_options = cgroup_show_options,
1399 .remount_fs = cgroup_remount,
1400};
1401
Paul Menagecc31edc2008-10-18 20:28:04 -07001402static void init_cgroup_housekeeping(struct cgroup *cgrp)
1403{
1404 INIT_LIST_HEAD(&cgrp->sibling);
1405 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001406 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001407 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001408 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001409 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001410 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001411 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001412 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001413 INIT_LIST_HEAD(&cgrp->event_list);
1414 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001415 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416}
Paul Menagec6d57f32009-09-23 15:56:19 -07001417
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418static void init_cgroup_root(struct cgroupfs_root *root)
1419{
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001421
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 INIT_LIST_HEAD(&root->subsys_list);
1423 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001424 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001426 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001427 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001428 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001429 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430}
1431
Tejun Heofa3ca072013-04-14 11:36:56 -07001432static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001433{
Tejun Heo1a574232013-04-14 11:36:58 -07001434 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001435
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001436 lockdep_assert_held(&cgroup_mutex);
1437 lockdep_assert_held(&cgroup_root_mutex);
1438
Tejun Heo1a574232013-04-14 11:36:58 -07001439 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1440 if (id < 0)
1441 return id;
1442
1443 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001444 return 0;
1445}
1446
1447static void cgroup_exit_root_id(struct cgroupfs_root *root)
1448{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001449 lockdep_assert_held(&cgroup_mutex);
1450 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001451
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001452 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001453 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001454 root->hierarchy_id = 0;
1455 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001456}
1457
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458static int cgroup_test_super(struct super_block *sb, void *data)
1459{
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 struct cgroupfs_root *root = sb->s_fs_info;
1462
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 /* If we asked for a name then it must match */
1464 if (opts->name && strcmp(opts->name, root->name))
1465 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467 /*
1468 * If we asked for subsystems (or explicitly for no
1469 * subsystems) then they must match
1470 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001471 if ((opts->subsys_mask || opts->none)
1472 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 return 0;
1474
1475 return 1;
1476}
1477
Paul Menagec6d57f32009-09-23 15:56:19 -07001478static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1479{
1480 struct cgroupfs_root *root;
1481
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001482 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001483 return NULL;
1484
1485 root = kzalloc(sizeof(*root), GFP_KERNEL);
1486 if (!root)
1487 return ERR_PTR(-ENOMEM);
1488
1489 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001491 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001493 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 if (opts->release_agent)
1495 strcpy(root->release_agent_path, opts->release_agent);
1496 if (opts->name)
1497 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001498 if (opts->cpuset_clone_children)
1499 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001500 return root;
1501}
1502
Tejun Heofa3ca072013-04-14 11:36:56 -07001503static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001504{
Tejun Heofa3ca072013-04-14 11:36:56 -07001505 if (root) {
1506 /* hierarhcy ID shoulid already have been released */
1507 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001508
Tejun Heofa3ca072013-04-14 11:36:56 -07001509 ida_destroy(&root->cgroup_ida);
1510 kfree(root);
1511 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001512}
1513
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514static int cgroup_set_super(struct super_block *sb, void *data)
1515{
1516 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001517 struct cgroup_sb_opts *opts = data;
1518
1519 /* If we don't have a new root, we can't set up a new sb */
1520 if (!opts->new_root)
1521 return -EINVAL;
1522
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001523 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
1525 ret = set_anon_super(sb, NULL);
1526 if (ret)
1527 return ret;
1528
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 sb->s_fs_info = opts->new_root;
1530 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 sb->s_blocksize = PAGE_CACHE_SIZE;
1533 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1534 sb->s_magic = CGROUP_SUPER_MAGIC;
1535 sb->s_op = &cgroup_ops;
1536
1537 return 0;
1538}
1539
1540static int cgroup_get_rootdir(struct super_block *sb)
1541{
Al Viro0df6a632010-12-21 13:29:29 -05001542 static const struct dentry_operations cgroup_dops = {
1543 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001544 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001545 };
1546
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 struct inode *inode =
1548 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549
1550 if (!inode)
1551 return -ENOMEM;
1552
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 inode->i_fop = &simple_dir_operations;
1554 inode->i_op = &cgroup_dir_inode_operations;
1555 /* directories start off with i_nlink == 2 (for "." entry) */
1556 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001557 sb->s_root = d_make_root(inode);
1558 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001560 /* for everything else we want ->d_op set */
1561 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 return 0;
1563}
1564
Al Virof7e83572010-07-26 13:23:11 +04001565static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001567 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568{
1569 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 int ret = 0;
1572 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001574 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
1576 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001577 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001579 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 if (ret)
1581 goto out_err;
1582
1583 /*
1584 * Allocate a new cgroup root. We may not need it if we're
1585 * reusing an existing hierarchy.
1586 */
1587 new_root = cgroup_root_from_opts(&opts);
1588 if (IS_ERR(new_root)) {
1589 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001590 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001591 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001595 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001598 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001599 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 }
1601
Paul Menagec6d57f32009-09-23 15:56:19 -07001602 root = sb->s_fs_info;
1603 BUG_ON(!root);
1604 if (root == opts.new_root) {
1605 /* We used the new root structure, so this is a new hierarchy */
1606 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001607 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001609 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001610 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001611 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
1613 BUG_ON(sb->s_root != NULL);
1614
1615 ret = cgroup_get_rootdir(sb);
1616 if (ret)
1617 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001618 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
Paul Menage817929e2007-10-18 23:39:36 -07001620 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001622 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 /* Check for name clashes with existing mounts */
1625 ret = -EBUSY;
1626 if (strlen(root->name))
1627 for_each_active_root(existing_root)
1628 if (!strcmp(existing_root->name, root->name))
1629 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001630
Paul Menage817929e2007-10-18 23:39:36 -07001631 /*
1632 * We're accessing css_set_count without locking
1633 * css_set_lock here, but that's OK - it can only be
1634 * increased by someone holding cgroup_lock, and
1635 * that's us. The worst that can happen is that we
1636 * have some link structures left over
1637 */
1638 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001639 if (ret)
1640 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001641
Tejun Heofa3ca072013-04-14 11:36:56 -07001642 ret = cgroup_init_root_id(root);
1643 if (ret)
1644 goto unlock_drop;
1645
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001646 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001647 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001648 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001649 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001651 /*
1652 * There must be no failure case after here, since rebinding
1653 * takes care of subsystems' refcounts, which are explicitly
1654 * dropped in the failure exit path.
1655 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656
1657 /* EBUSY should be the only error here */
1658 BUG_ON(ret);
1659
1660 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001661 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
Li Zefanc12f65d2009-01-07 18:07:42 -08001663 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 root->top_cgroup.dentry = sb->s_root;
1665
Paul Menage817929e2007-10-18 23:39:36 -07001666 /* Link the top cgroup in this hierarchy into all
1667 * the css_set objects */
1668 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001669 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001670 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001671 write_unlock(&css_set_lock);
1672
1673 free_cg_links(&tmp_cg_links);
1674
Li Zefanc12f65d2009-01-07 18:07:42 -08001675 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 BUG_ON(root->number_of_cgroups != 1);
1677
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001679 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001680 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001681 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001683 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001684 } else {
1685 /*
1686 * We re-used an existing hierarchy - the new root (if
1687 * any) is not needed
1688 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001689 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001690
1691 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1692 root->flags != opts.flags) {
1693 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1694 ret = -EINVAL;
1695 goto drop_new_super;
1696 }
1697
Ben Blumcf5d5942010-03-10 15:22:09 -08001698 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001699 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 }
1701
Paul Menagec6d57f32009-09-23 15:56:19 -07001702 kfree(opts.release_agent);
1703 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001704 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001707 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001708 mutex_unlock(&cgroup_root_mutex);
1709 mutex_unlock(&cgroup_mutex);
1710 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001712 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001713 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001714 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001715 out_err:
1716 kfree(opts.release_agent);
1717 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001718 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719}
1720
1721static void cgroup_kill_sb(struct super_block *sb) {
1722 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001725 struct cg_cgroup_link *link;
1726 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 BUG_ON(!root);
1729
1730 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001734 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
1736 /* Rebind all subsystems back to the default hierarchy */
1737 ret = rebind_subsystems(root, 0);
1738 /* Shouldn't be able to fail ... */
1739 BUG_ON(ret);
1740
Paul Menage817929e2007-10-18 23:39:36 -07001741 /*
1742 * Release all the links from css_sets to this hierarchy's
1743 * root cgroup
1744 */
1745 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001746
1747 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1748 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001749 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001750 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001751 kfree(link);
1752 }
1753 write_unlock(&css_set_lock);
1754
Paul Menage839ec542009-01-29 14:25:22 -08001755 if (!list_empty(&root->root_list)) {
1756 list_del(&root->root_list);
1757 root_count--;
1758 }
Li Zefane5f6a862009-01-07 18:07:41 -08001759
Tejun Heofa3ca072013-04-14 11:36:56 -07001760 cgroup_exit_root_id(root);
1761
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001762 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 mutex_unlock(&cgroup_mutex);
1764
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001765 simple_xattrs_free(&cgrp->xattrs);
1766
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001768 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769}
1770
1771static struct file_system_type cgroup_fs_type = {
1772 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001773 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 .kill_sb = cgroup_kill_sb,
1775};
1776
Greg KH676db4a2010-08-05 13:53:35 -07001777static struct kobject *cgroup_kobj;
1778
Li Zefana043e3b2008-02-23 15:24:09 -08001779/**
1780 * cgroup_path - generate the path of a cgroup
1781 * @cgrp: the cgroup in question
1782 * @buf: the buffer to write the path into
1783 * @buflen: the length of the buffer
1784 *
Li Zefan65dff752013-03-01 15:01:56 +08001785 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1786 *
1787 * We can't generate cgroup path using dentry->d_name, as accessing
1788 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1789 * inode's i_mutex, while on the other hand cgroup_path() can be called
1790 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001792int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793{
Li Zefan65dff752013-03-01 15:01:56 +08001794 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001796
Tejun Heoda1f2962013-04-14 10:32:19 -07001797 if (!cgrp->parent) {
1798 if (strlcpy(buf, "/", buflen) >= buflen)
1799 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 return 0;
1801 }
1802
Tao Ma316eb662012-11-08 21:36:38 +08001803 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001804 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001805
Li Zefan65dff752013-03-01 15:01:56 +08001806 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001807 do {
Li Zefan65dff752013-03-01 15:01:56 +08001808 const char *name = cgroup_name(cgrp);
1809 int len;
1810
1811 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001813 goto out;
1814 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001815
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001817 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001819
1820 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001821 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001822 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001824out:
1825 rcu_read_unlock();
1826 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827}
Ben Blum67523c42010-03-10 15:22:11 -08001828EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829
Tejun Heo857a2be2013-04-14 20:50:08 -07001830/**
1831 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1832 * @task: target task
1833 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1834 * @buf: the buffer to write the path into
1835 * @buflen: the length of the buffer
1836 *
1837 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1838 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1839 * be used inside locks used by cgroup controller callbacks.
1840 */
1841int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1842 char *buf, size_t buflen)
1843{
1844 struct cgroupfs_root *root;
1845 struct cgroup *cgrp = NULL;
1846 int ret = -ENOENT;
1847
1848 mutex_lock(&cgroup_mutex);
1849
1850 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1851 if (root) {
1852 cgrp = task_cgroup_from_root(task, root);
1853 ret = cgroup_path(cgrp, buf, buflen);
1854 }
1855
1856 mutex_unlock(&cgroup_mutex);
1857
1858 return ret;
1859}
1860EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1861
Ben Blum74a11662011-05-26 16:25:20 -07001862/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001863 * Control Group taskset
1864 */
Tejun Heo134d3372011-12-12 18:12:21 -08001865struct task_and_cgroup {
1866 struct task_struct *task;
1867 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001868 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001869};
1870
Tejun Heo2f7ee562011-12-12 18:12:21 -08001871struct cgroup_taskset {
1872 struct task_and_cgroup single;
1873 struct flex_array *tc_array;
1874 int tc_array_len;
1875 int idx;
1876 struct cgroup *cur_cgrp;
1877};
1878
1879/**
1880 * cgroup_taskset_first - reset taskset and return the first task
1881 * @tset: taskset of interest
1882 *
1883 * @tset iteration is initialized and the first task is returned.
1884 */
1885struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1886{
1887 if (tset->tc_array) {
1888 tset->idx = 0;
1889 return cgroup_taskset_next(tset);
1890 } else {
1891 tset->cur_cgrp = tset->single.cgrp;
1892 return tset->single.task;
1893 }
1894}
1895EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1896
1897/**
1898 * cgroup_taskset_next - iterate to the next task in taskset
1899 * @tset: taskset of interest
1900 *
1901 * Return the next task in @tset. Iteration must have been initialized
1902 * with cgroup_taskset_first().
1903 */
1904struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1905{
1906 struct task_and_cgroup *tc;
1907
1908 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1909 return NULL;
1910
1911 tc = flex_array_get(tset->tc_array, tset->idx++);
1912 tset->cur_cgrp = tc->cgrp;
1913 return tc->task;
1914}
1915EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1916
1917/**
1918 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1919 * @tset: taskset of interest
1920 *
1921 * Return the cgroup for the current (last returned) task of @tset. This
1922 * function must be preceded by either cgroup_taskset_first() or
1923 * cgroup_taskset_next().
1924 */
1925struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1926{
1927 return tset->cur_cgrp;
1928}
1929EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1930
1931/**
1932 * cgroup_taskset_size - return the number of tasks in taskset
1933 * @tset: taskset of interest
1934 */
1935int cgroup_taskset_size(struct cgroup_taskset *tset)
1936{
1937 return tset->tc_array ? tset->tc_array_len : 1;
1938}
1939EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1940
1941
Ben Blum74a11662011-05-26 16:25:20 -07001942/*
1943 * cgroup_task_migrate - move a task from one cgroup to another.
1944 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001945 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001946 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001947static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001948 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
1950 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001951
1952 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001953 * We are synchronized through threadgroup_lock() against PF_EXITING
1954 * setting such that we can't race against cgroup_exit() changing the
1955 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001956 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001957 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001958 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001959
Ben Blum74a11662011-05-26 16:25:20 -07001960 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001961 rcu_assign_pointer(tsk->cgroups, newcg);
1962 task_unlock(tsk);
1963
1964 /* Update the css_set linked lists if we're using them */
1965 write_lock(&css_set_lock);
1966 if (!list_empty(&tsk->cg_list))
1967 list_move(&tsk->cg_list, &newcg->tasks);
1968 write_unlock(&css_set_lock);
1969
1970 /*
1971 * We just gained a reference on oldcg by taking it from the task. As
1972 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1973 * it here; it will be freed under RCU.
1974 */
Ben Blum74a11662011-05-26 16:25:20 -07001975 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001976 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001977}
1978
Li Zefana043e3b2008-02-23 15:24:09 -08001979/**
Li Zefan081aa452013-03-13 09:17:09 +08001980 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001981 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001982 * @tsk: the task or the leader of the threadgroup to be attached
1983 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001984 *
Tejun Heo257058a2011-12-12 18:12:21 -08001985 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001986 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001987 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001988static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1989 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001990{
1991 int retval, i, group_size;
1992 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001993 struct cgroupfs_root *root = cgrp->root;
1994 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001995 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001996 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001997 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001998 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001999
2000 /*
2001 * step 0: in order to do expensive, possibly blocking operations for
2002 * every thread, we cannot iterate the thread group list, since it needs
2003 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002004 * group - group_rwsem prevents new threads from appearing, and if
2005 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002006 */
Li Zefan081aa452013-03-13 09:17:09 +08002007 if (threadgroup)
2008 group_size = get_nr_threads(tsk);
2009 else
2010 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002011 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002012 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002013 if (!group)
2014 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002015 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002016 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002017 if (retval)
2018 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002019
Ben Blum74a11662011-05-26 16:25:20 -07002020 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002021 /*
2022 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2023 * already PF_EXITING could be freed from underneath us unless we
2024 * take an rcu_read_lock.
2025 */
2026 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002027 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002028 struct task_and_cgroup ent;
2029
Tejun Heocd3d0952011-12-12 18:12:21 -08002030 /* @tsk either already exited or can't exit until the end */
2031 if (tsk->flags & PF_EXITING)
2032 continue;
2033
Ben Blum74a11662011-05-26 16:25:20 -07002034 /* as per above, nr_threads may decrease, but not increase. */
2035 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002036 ent.task = tsk;
2037 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002038 /* nothing to do if this task is already in the cgroup */
2039 if (ent.cgrp == cgrp)
2040 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002041 /*
2042 * saying GFP_ATOMIC has no effect here because we did prealloc
2043 * earlier, but it's good form to communicate our expectations.
2044 */
Tejun Heo134d3372011-12-12 18:12:21 -08002045 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002046 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002047 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002048
2049 if (!threadgroup)
2050 break;
Ben Blum74a11662011-05-26 16:25:20 -07002051 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002052 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002053 /* remember the number of threads in the array for later. */
2054 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002055 tset.tc_array = group;
2056 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002057
Tejun Heo134d3372011-12-12 18:12:21 -08002058 /* methods shouldn't be called if no task is actually migrating */
2059 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002060 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002061 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002062
Ben Blum74a11662011-05-26 16:25:20 -07002063 /*
2064 * step 1: check that we can legitimately attach to the cgroup.
2065 */
2066 for_each_subsys(root, ss) {
2067 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002068 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002069 if (retval) {
2070 failed_ss = ss;
2071 goto out_cancel_attach;
2072 }
2073 }
Ben Blum74a11662011-05-26 16:25:20 -07002074 }
2075
2076 /*
2077 * step 2: make sure css_sets exist for all threads to be migrated.
2078 * we use find_css_set, which allocates a new one if necessary.
2079 */
Ben Blum74a11662011-05-26 16:25:20 -07002080 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002081 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002082 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2083 if (!tc->cg) {
2084 retval = -ENOMEM;
2085 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087 }
2088
2089 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002090 * step 3: now that we're guaranteed success wrt the css_sets,
2091 * proceed to move all tasks to the new cgroup. There are no
2092 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002093 */
Ben Blum74a11662011-05-26 16:25:20 -07002094 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002095 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002096 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002097 }
2098 /* nothing is sensitive to fork() after this point. */
2099
2100 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002101 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
2103 for_each_subsys(root, ss) {
2104 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002105 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107
2108 /*
2109 * step 5: success! and cleanup
2110 */
Ben Blum74a11662011-05-26 16:25:20 -07002111 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002112out_put_css_set_refs:
2113 if (retval) {
2114 for (i = 0; i < group_size; i++) {
2115 tc = flex_array_get(group, i);
2116 if (!tc->cg)
2117 break;
2118 put_css_set(tc->cg);
2119 }
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002122 if (retval) {
2123 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002124 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002125 break;
Ben Blum74a11662011-05-26 16:25:20 -07002126 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002127 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002128 }
2129 }
Ben Blum74a11662011-05-26 16:25:20 -07002130out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002131 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002132 return retval;
2133}
2134
2135/*
2136 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002137 * function to attach either it or all tasks in its threadgroup. Will lock
2138 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002139 */
2140static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002143 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002144 int ret;
2145
Ben Blum74a11662011-05-26 16:25:20 -07002146 if (!cgroup_lock_live_group(cgrp))
2147 return -ENODEV;
2148
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002149retry_find_task:
2150 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002151 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002152 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002153 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002154 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 ret= -ESRCH;
2156 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157 }
Ben Blum74a11662011-05-26 16:25:20 -07002158 /*
2159 * even if we're attaching all tasks in the thread group, we
2160 * only need to check permissions on one of them.
2161 */
David Howellsc69e8d92008-11-14 10:39:19 +11002162 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002163 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2164 !uid_eq(cred->euid, tcred->uid) &&
2165 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002166 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167 ret = -EACCES;
2168 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002170 } else
2171 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002172
2173 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002175
2176 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002177 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002178 * trapped in a cpuset, or RT worker may be born in a cgroup
2179 * with no rt_runtime allocated. Just say no.
2180 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002181 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002182 ret = -EINVAL;
2183 rcu_read_unlock();
2184 goto out_unlock_cgroup;
2185 }
2186
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187 get_task_struct(tsk);
2188 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002189
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002190 threadgroup_lock(tsk);
2191 if (threadgroup) {
2192 if (!thread_group_leader(tsk)) {
2193 /*
2194 * a race with de_thread from another thread's exec()
2195 * may strip us of our leadership, if this happens,
2196 * there is no choice but to throw this task away and
2197 * try again; this is
2198 * "double-double-toil-and-trouble-check locking".
2199 */
2200 threadgroup_unlock(tsk);
2201 put_task_struct(tsk);
2202 goto retry_find_task;
2203 }
Li Zefan081aa452013-03-13 09:17:09 +08002204 }
2205
2206 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2207
Tejun Heocd3d0952011-12-12 18:12:21 -08002208 threadgroup_unlock(tsk);
2209
Paul Menagebbcb81d2007-10-18 23:39:32 -07002210 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002213 return ret;
2214}
2215
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002216/**
2217 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2218 * @from: attach to all cgroups of a given task
2219 * @tsk: the task to be attached
2220 */
2221int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2222{
2223 struct cgroupfs_root *root;
2224 int retval = 0;
2225
Tejun Heo47cfcd02013-04-07 09:29:51 -07002226 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002227 for_each_active_root(root) {
2228 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2229
2230 retval = cgroup_attach_task(from_cg, tsk, false);
2231 if (retval)
2232 break;
2233 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002234 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002235
2236 return retval;
2237}
2238EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2239
Paul Menageaf351022008-07-25 01:47:01 -07002240static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2241{
Ben Blum74a11662011-05-26 16:25:20 -07002242 return attach_task_by_pid(cgrp, pid, false);
2243}
2244
2245static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2246{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002247 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002248}
2249
Paul Menagee788e062008-07-25 01:46:59 -07002250static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2251 const char *buffer)
2252{
2253 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002254 if (strlen(buffer) >= PATH_MAX)
2255 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002256 if (!cgroup_lock_live_group(cgrp))
2257 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002258 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002259 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002260 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002261 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002262 return 0;
2263}
2264
2265static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2266 struct seq_file *seq)
2267{
2268 if (!cgroup_lock_live_group(cgrp))
2269 return -ENODEV;
2270 seq_puts(seq, cgrp->root->release_agent_path);
2271 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002272 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002273 return 0;
2274}
2275
Tejun Heo873fe092013-04-14 20:15:26 -07002276static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2277 struct seq_file *seq)
2278{
2279 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002280 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002281}
2282
Paul Menage84eea842008-07-25 01:47:00 -07002283/* A buffer size big enough for numbers or short strings */
2284#define CGROUP_LOCAL_BUFFER_SIZE 64
2285
Paul Menagee73d2c62008-04-29 01:00:06 -07002286static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002287 struct file *file,
2288 const char __user *userbuf,
2289 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002290{
Paul Menage84eea842008-07-25 01:47:00 -07002291 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002292 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002293 char *end;
2294
2295 if (!nbytes)
2296 return -EINVAL;
2297 if (nbytes >= sizeof(buffer))
2298 return -E2BIG;
2299 if (copy_from_user(buffer, userbuf, nbytes))
2300 return -EFAULT;
2301
2302 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002303 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002304 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002305 if (*end)
2306 return -EINVAL;
2307 retval = cft->write_u64(cgrp, cft, val);
2308 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002309 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002310 if (*end)
2311 return -EINVAL;
2312 retval = cft->write_s64(cgrp, cft, val);
2313 }
Paul Menage355e0c42007-10-18 23:39:33 -07002314 if (!retval)
2315 retval = nbytes;
2316 return retval;
2317}
2318
Paul Menagedb3b1492008-07-25 01:46:58 -07002319static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2320 struct file *file,
2321 const char __user *userbuf,
2322 size_t nbytes, loff_t *unused_ppos)
2323{
Paul Menage84eea842008-07-25 01:47:00 -07002324 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002325 int retval = 0;
2326 size_t max_bytes = cft->max_write_len;
2327 char *buffer = local_buffer;
2328
2329 if (!max_bytes)
2330 max_bytes = sizeof(local_buffer) - 1;
2331 if (nbytes >= max_bytes)
2332 return -E2BIG;
2333 /* Allocate a dynamic buffer if we need one */
2334 if (nbytes >= sizeof(local_buffer)) {
2335 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2336 if (buffer == NULL)
2337 return -ENOMEM;
2338 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002339 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2340 retval = -EFAULT;
2341 goto out;
2342 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002343
2344 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002345 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002346 if (!retval)
2347 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002348out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002349 if (buffer != local_buffer)
2350 kfree(buffer);
2351 return retval;
2352}
2353
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2355 size_t nbytes, loff_t *ppos)
2356{
2357 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002358 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359
Li Zefan75139b82009-01-07 18:07:33 -08002360 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002362 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002363 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002364 if (cft->write_u64 || cft->write_s64)
2365 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002366 if (cft->write_string)
2367 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002368 if (cft->trigger) {
2369 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2370 return ret ? ret : nbytes;
2371 }
Paul Menage355e0c42007-10-18 23:39:33 -07002372 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373}
2374
Paul Menagef4c753b2008-04-29 00:59:56 -07002375static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2376 struct file *file,
2377 char __user *buf, size_t nbytes,
2378 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379{
Paul Menage84eea842008-07-25 01:47:00 -07002380 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002381 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2383
2384 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2385}
2386
Paul Menagee73d2c62008-04-29 01:00:06 -07002387static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2388 struct file *file,
2389 char __user *buf, size_t nbytes,
2390 loff_t *ppos)
2391{
Paul Menage84eea842008-07-25 01:47:00 -07002392 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002393 s64 val = cft->read_s64(cgrp, cft);
2394 int len = sprintf(tmp, "%lld\n", (long long) val);
2395
2396 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2397}
2398
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2400 size_t nbytes, loff_t *ppos)
2401{
2402 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002403 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404
Li Zefan75139b82009-01-07 18:07:33 -08002405 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406 return -ENODEV;
2407
2408 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002410 if (cft->read_u64)
2411 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002412 if (cft->read_s64)
2413 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002414 return -EINVAL;
2415}
2416
Paul Menage91796562008-04-29 01:00:01 -07002417/*
2418 * seqfile ops/methods for returning structured data. Currently just
2419 * supports string->u64 maps, but can be extended in future.
2420 */
2421
2422struct cgroup_seqfile_state {
2423 struct cftype *cft;
2424 struct cgroup *cgroup;
2425};
2426
2427static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2428{
2429 struct seq_file *sf = cb->state;
2430 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2431}
2432
2433static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2434{
2435 struct cgroup_seqfile_state *state = m->private;
2436 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002437 if (cft->read_map) {
2438 struct cgroup_map_cb cb = {
2439 .fill = cgroup_map_add,
2440 .state = m,
2441 };
2442 return cft->read_map(state->cgroup, cft, &cb);
2443 }
2444 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002445}
2446
Adrian Bunk96930a62008-07-25 19:46:21 -07002447static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002448{
2449 struct seq_file *seq = file->private_data;
2450 kfree(seq->private);
2451 return single_release(inode, file);
2452}
2453
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002454static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002455 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002456 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002457 .llseek = seq_lseek,
2458 .release = cgroup_seqfile_release,
2459};
2460
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461static int cgroup_file_open(struct inode *inode, struct file *file)
2462{
2463 int err;
2464 struct cftype *cft;
2465
2466 err = generic_file_open(inode, file);
2467 if (err)
2468 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002470
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002471 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002472 struct cgroup_seqfile_state *state =
2473 kzalloc(sizeof(*state), GFP_USER);
2474 if (!state)
2475 return -ENOMEM;
2476 state->cft = cft;
2477 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2478 file->f_op = &cgroup_seqfile_operations;
2479 err = single_open(file, cgroup_seqfile_show, state);
2480 if (err < 0)
2481 kfree(state);
2482 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 err = cft->open(inode, file);
2484 else
2485 err = 0;
2486
2487 return err;
2488}
2489
2490static int cgroup_file_release(struct inode *inode, struct file *file)
2491{
2492 struct cftype *cft = __d_cft(file->f_dentry);
2493 if (cft->release)
2494 return cft->release(inode, file);
2495 return 0;
2496}
2497
2498/*
2499 * cgroup_rename - Only allow simple rename of directories in place.
2500 */
2501static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2502 struct inode *new_dir, struct dentry *new_dentry)
2503{
Li Zefan65dff752013-03-01 15:01:56 +08002504 int ret;
2505 struct cgroup_name *name, *old_name;
2506 struct cgroup *cgrp;
2507
2508 /*
2509 * It's convinient to use parent dir's i_mutex to protected
2510 * cgrp->name.
2511 */
2512 lockdep_assert_held(&old_dir->i_mutex);
2513
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2515 return -ENOTDIR;
2516 if (new_dentry->d_inode)
2517 return -EEXIST;
2518 if (old_dir != new_dir)
2519 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002520
2521 cgrp = __d_cgrp(old_dentry);
2522
2523 name = cgroup_alloc_name(new_dentry);
2524 if (!name)
2525 return -ENOMEM;
2526
2527 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2528 if (ret) {
2529 kfree(name);
2530 return ret;
2531 }
2532
2533 old_name = cgrp->name;
2534 rcu_assign_pointer(cgrp->name, name);
2535
2536 kfree_rcu(old_name, rcu_head);
2537 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538}
2539
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002540static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2541{
2542 if (S_ISDIR(dentry->d_inode->i_mode))
2543 return &__d_cgrp(dentry)->xattrs;
2544 else
Li Zefan712317a2013-04-18 23:09:52 -07002545 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002546}
2547
2548static inline int xattr_enabled(struct dentry *dentry)
2549{
2550 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002551 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002552}
2553
2554static bool is_valid_xattr(const char *name)
2555{
2556 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2557 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2558 return true;
2559 return false;
2560}
2561
2562static int cgroup_setxattr(struct dentry *dentry, const char *name,
2563 const void *val, size_t size, int flags)
2564{
2565 if (!xattr_enabled(dentry))
2566 return -EOPNOTSUPP;
2567 if (!is_valid_xattr(name))
2568 return -EINVAL;
2569 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2570}
2571
2572static int cgroup_removexattr(struct dentry *dentry, const char *name)
2573{
2574 if (!xattr_enabled(dentry))
2575 return -EOPNOTSUPP;
2576 if (!is_valid_xattr(name))
2577 return -EINVAL;
2578 return simple_xattr_remove(__d_xattrs(dentry), name);
2579}
2580
2581static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2582 void *buf, size_t size)
2583{
2584 if (!xattr_enabled(dentry))
2585 return -EOPNOTSUPP;
2586 if (!is_valid_xattr(name))
2587 return -EINVAL;
2588 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2589}
2590
2591static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2592{
2593 if (!xattr_enabled(dentry))
2594 return -EOPNOTSUPP;
2595 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2596}
2597
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002598static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002599 .read = cgroup_file_read,
2600 .write = cgroup_file_write,
2601 .llseek = generic_file_llseek,
2602 .open = cgroup_file_open,
2603 .release = cgroup_file_release,
2604};
2605
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002606static const struct inode_operations cgroup_file_inode_operations = {
2607 .setxattr = cgroup_setxattr,
2608 .getxattr = cgroup_getxattr,
2609 .listxattr = cgroup_listxattr,
2610 .removexattr = cgroup_removexattr,
2611};
2612
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002613static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002614 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002615 .mkdir = cgroup_mkdir,
2616 .rmdir = cgroup_rmdir,
2617 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002618 .setxattr = cgroup_setxattr,
2619 .getxattr = cgroup_getxattr,
2620 .listxattr = cgroup_listxattr,
2621 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622};
2623
Al Viro00cd8dd2012-06-10 17:13:09 -04002624static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002625{
2626 if (dentry->d_name.len > NAME_MAX)
2627 return ERR_PTR(-ENAMETOOLONG);
2628 d_add(dentry, NULL);
2629 return NULL;
2630}
2631
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002632/*
2633 * Check if a file is a control file
2634 */
2635static inline struct cftype *__file_cft(struct file *file)
2636{
Al Viro496ad9a2013-01-23 17:07:38 -05002637 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002638 return ERR_PTR(-EINVAL);
2639 return __d_cft(file->f_dentry);
2640}
2641
Al Viroa5e7ed32011-07-26 01:55:55 -04002642static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002643 struct super_block *sb)
2644{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 struct inode *inode;
2646
2647 if (!dentry)
2648 return -ENOENT;
2649 if (dentry->d_inode)
2650 return -EEXIST;
2651
2652 inode = cgroup_new_inode(mode, sb);
2653 if (!inode)
2654 return -ENOMEM;
2655
2656 if (S_ISDIR(mode)) {
2657 inode->i_op = &cgroup_dir_inode_operations;
2658 inode->i_fop = &simple_dir_operations;
2659
2660 /* start off with i_nlink == 2 (for "." entry) */
2661 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002662 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663
Tejun Heob8a2df62012-11-19 08:13:37 -08002664 /*
2665 * Control reaches here with cgroup_mutex held.
2666 * @inode->i_mutex should nest outside cgroup_mutex but we
2667 * want to populate it immediately without releasing
2668 * cgroup_mutex. As @inode isn't visible to anyone else
2669 * yet, trylock will always succeed without affecting
2670 * lockdep checks.
2671 */
2672 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 } else if (S_ISREG(mode)) {
2674 inode->i_size = 0;
2675 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002676 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 d_instantiate(dentry, inode);
2679 dget(dentry); /* Extra count - pin the dentry in core */
2680 return 0;
2681}
2682
Li Zefan099fca32009-04-02 16:57:29 -07002683/**
2684 * cgroup_file_mode - deduce file mode of a control file
2685 * @cft: the control file in question
2686 *
2687 * returns cft->mode if ->mode is not 0
2688 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2689 * returns S_IRUGO if it has only a read handler
2690 * returns S_IWUSR if it has only a write hander
2691 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002692static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002693{
Al Viroa5e7ed32011-07-26 01:55:55 -04002694 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002695
2696 if (cft->mode)
2697 return cft->mode;
2698
2699 if (cft->read || cft->read_u64 || cft->read_s64 ||
2700 cft->read_map || cft->read_seq_string)
2701 mode |= S_IRUGO;
2702
2703 if (cft->write || cft->write_u64 || cft->write_s64 ||
2704 cft->write_string || cft->trigger)
2705 mode |= S_IWUSR;
2706
2707 return mode;
2708}
2709
Tejun Heodb0416b2012-04-01 12:09:55 -07002710static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002711 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712{
Paul Menagebd89aab2007-10-18 23:40:44 -07002713 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002714 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002716 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002718 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002720
Tejun Heo93438622013-04-14 20:15:25 -07002721 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 strcpy(name, subsys->name);
2723 strcat(name, ".");
2724 }
2725 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002726
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728
2729 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2730 if (!cfe)
2731 return -ENOMEM;
2732
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002734 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002736 goto out;
2737 }
2738
2739 mode = cgroup_file_mode(cft);
2740 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2741 if (!error) {
2742 cfe->type = (void *)cft;
2743 cfe->dentry = dentry;
2744 dentry->d_fsdata = cfe;
Li Zefan712317a2013-04-18 23:09:52 -07002745 simple_xattrs_init(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002746 list_add_tail(&cfe->node, &parent->files);
2747 cfe = NULL;
2748 }
2749 dput(dentry);
2750out:
2751 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752 return error;
2753}
2754
Tejun Heo79578622012-04-01 12:09:56 -07002755static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002756 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002759 int err, ret = 0;
2760
2761 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002762 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002763 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2764 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002765 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2766 continue;
2767 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2768 continue;
2769
Li Zefan2739d3c2013-01-21 18:18:33 +08002770 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002771 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002772 if (err)
2773 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2774 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002775 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002776 } else {
2777 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002778 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002779 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781}
2782
Tejun Heo8e3f6542012-04-01 12:09:55 -07002783static DEFINE_MUTEX(cgroup_cft_mutex);
2784
2785static void cgroup_cfts_prepare(void)
2786 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2787{
2788 /*
2789 * Thanks to the entanglement with vfs inode locking, we can't walk
2790 * the existing cgroups under cgroup_mutex and create files.
2791 * Instead, we increment reference on all cgroups and build list of
2792 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2793 * exclusive access to the field.
2794 */
2795 mutex_lock(&cgroup_cft_mutex);
2796 mutex_lock(&cgroup_mutex);
2797}
2798
2799static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002800 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002801 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2802{
2803 LIST_HEAD(pending);
2804 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002805
2806 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2807 if (cfts && ss->root != &rootnode) {
2808 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2809 dget(cgrp->dentry);
2810 list_add_tail(&cgrp->cft_q_node, &pending);
2811 }
2812 }
2813
2814 mutex_unlock(&cgroup_mutex);
2815
2816 /*
2817 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2818 * files for all cgroups which were created before.
2819 */
2820 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2821 struct inode *inode = cgrp->dentry->d_inode;
2822
2823 mutex_lock(&inode->i_mutex);
2824 mutex_lock(&cgroup_mutex);
2825 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002826 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002827 mutex_unlock(&cgroup_mutex);
2828 mutex_unlock(&inode->i_mutex);
2829
2830 list_del_init(&cgrp->cft_q_node);
2831 dput(cgrp->dentry);
2832 }
2833
2834 mutex_unlock(&cgroup_cft_mutex);
2835}
2836
2837/**
2838 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2839 * @ss: target cgroup subsystem
2840 * @cfts: zero-length name terminated array of cftypes
2841 *
2842 * Register @cfts to @ss. Files described by @cfts are created for all
2843 * existing cgroups to which @ss is attached and all future cgroups will
2844 * have them too. This function can be called anytime whether @ss is
2845 * attached or not.
2846 *
2847 * Returns 0 on successful registration, -errno on failure. Note that this
2848 * function currently returns 0 as long as @cfts registration is successful
2849 * even if some file creation attempts on existing cgroups fail.
2850 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002851int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852{
2853 struct cftype_set *set;
2854
2855 set = kzalloc(sizeof(*set), GFP_KERNEL);
2856 if (!set)
2857 return -ENOMEM;
2858
2859 cgroup_cfts_prepare();
2860 set->cfts = cfts;
2861 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002862 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002863
2864 return 0;
2865}
2866EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2867
Li Zefana043e3b2008-02-23 15:24:09 -08002868/**
Tejun Heo79578622012-04-01 12:09:56 -07002869 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2870 * @ss: target cgroup subsystem
2871 * @cfts: zero-length name terminated array of cftypes
2872 *
2873 * Unregister @cfts from @ss. Files described by @cfts are removed from
2874 * all existing cgroups to which @ss is attached and all future cgroups
2875 * won't have them either. This function can be called anytime whether @ss
2876 * is attached or not.
2877 *
2878 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2879 * registered with @ss.
2880 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002881int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002882{
2883 struct cftype_set *set;
2884
2885 cgroup_cfts_prepare();
2886
2887 list_for_each_entry(set, &ss->cftsets, node) {
2888 if (set->cfts == cfts) {
2889 list_del_init(&set->node);
2890 cgroup_cfts_commit(ss, cfts, false);
2891 return 0;
2892 }
2893 }
2894
2895 cgroup_cfts_commit(ss, NULL, false);
2896 return -ENOENT;
2897}
2898
2899/**
Li Zefana043e3b2008-02-23 15:24:09 -08002900 * cgroup_task_count - count the number of tasks in a cgroup.
2901 * @cgrp: the cgroup in question
2902 *
2903 * Return the number of tasks in the cgroup.
2904 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002905int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002906{
2907 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002908 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002909
Paul Menage817929e2007-10-18 23:39:36 -07002910 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002911 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002912 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002913 }
2914 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002915 return count;
2916}
2917
2918/*
Paul Menage817929e2007-10-18 23:39:36 -07002919 * Advance a list_head iterator. The iterator should be positioned at
2920 * the start of a css_set
2921 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002922static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002923 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002924{
2925 struct list_head *l = it->cg_link;
2926 struct cg_cgroup_link *link;
2927 struct css_set *cg;
2928
2929 /* Advance to the next non-empty css_set */
2930 do {
2931 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002932 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002933 it->cg_link = NULL;
2934 return;
2935 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002936 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002937 cg = link->cg;
2938 } while (list_empty(&cg->tasks));
2939 it->cg_link = l;
2940 it->task = cg->tasks.next;
2941}
2942
Cliff Wickman31a7df02008-02-07 00:14:42 -08002943/*
2944 * To reduce the fork() overhead for systems that are not actually
2945 * using their cgroups capability, we don't maintain the lists running
2946 * through each css_set to its tasks until we see the list actually
2947 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002948 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002949static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002950{
2951 struct task_struct *p, *g;
2952 write_lock(&css_set_lock);
2953 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002954 /*
2955 * We need tasklist_lock because RCU is not safe against
2956 * while_each_thread(). Besides, a forking task that has passed
2957 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2958 * is not guaranteed to have its child immediately visible in the
2959 * tasklist if we walk through it with RCU.
2960 */
2961 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002962 do_each_thread(g, p) {
2963 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002964 /*
2965 * We should check if the process is exiting, otherwise
2966 * it will race with cgroup_exit() in that the list
2967 * entry won't be deleted though the process has exited.
2968 */
2969 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970 list_add(&p->cg_list, &p->cgroups->tasks);
2971 task_unlock(p);
2972 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002973 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002974 write_unlock(&css_set_lock);
2975}
2976
Tejun Heo574bd9f2012-11-09 09:12:29 -08002977/**
2978 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2979 * @pos: the current position (%NULL to initiate traversal)
2980 * @cgroup: cgroup whose descendants to walk
2981 *
2982 * To be used by cgroup_for_each_descendant_pre(). Find the next
2983 * descendant to visit for pre-order traversal of @cgroup's descendants.
2984 */
2985struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2986 struct cgroup *cgroup)
2987{
2988 struct cgroup *next;
2989
2990 WARN_ON_ONCE(!rcu_read_lock_held());
2991
2992 /* if first iteration, pretend we just visited @cgroup */
2993 if (!pos) {
2994 if (list_empty(&cgroup->children))
2995 return NULL;
2996 pos = cgroup;
2997 }
2998
2999 /* visit the first child if exists */
3000 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3001 if (next)
3002 return next;
3003
3004 /* no child, visit my or the closest ancestor's next sibling */
3005 do {
3006 next = list_entry_rcu(pos->sibling.next, struct cgroup,
3007 sibling);
3008 if (&next->sibling != &pos->parent->children)
3009 return next;
3010
3011 pos = pos->parent;
3012 } while (pos != cgroup);
3013
3014 return NULL;
3015}
3016EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3017
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003018/**
3019 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3020 * @pos: cgroup of interest
3021 *
3022 * Return the rightmost descendant of @pos. If there's no descendant,
3023 * @pos is returned. This can be used during pre-order traversal to skip
3024 * subtree of @pos.
3025 */
3026struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3027{
3028 struct cgroup *last, *tmp;
3029
3030 WARN_ON_ONCE(!rcu_read_lock_held());
3031
3032 do {
3033 last = pos;
3034 /* ->prev isn't RCU safe, walk ->next till the end */
3035 pos = NULL;
3036 list_for_each_entry_rcu(tmp, &last->children, sibling)
3037 pos = tmp;
3038 } while (pos);
3039
3040 return last;
3041}
3042EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3043
Tejun Heo574bd9f2012-11-09 09:12:29 -08003044static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3045{
3046 struct cgroup *last;
3047
3048 do {
3049 last = pos;
3050 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3051 sibling);
3052 } while (pos);
3053
3054 return last;
3055}
3056
3057/**
3058 * cgroup_next_descendant_post - find the next descendant for post-order walk
3059 * @pos: the current position (%NULL to initiate traversal)
3060 * @cgroup: cgroup whose descendants to walk
3061 *
3062 * To be used by cgroup_for_each_descendant_post(). Find the next
3063 * descendant to visit for post-order traversal of @cgroup's descendants.
3064 */
3065struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3066 struct cgroup *cgroup)
3067{
3068 struct cgroup *next;
3069
3070 WARN_ON_ONCE(!rcu_read_lock_held());
3071
3072 /* if first iteration, visit the leftmost descendant */
3073 if (!pos) {
3074 next = cgroup_leftmost_descendant(cgroup);
3075 return next != cgroup ? next : NULL;
3076 }
3077
3078 /* if there's an unvisited sibling, visit its leftmost descendant */
3079 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3080 if (&next->sibling != &pos->parent->children)
3081 return cgroup_leftmost_descendant(next);
3082
3083 /* no sibling left, visit parent */
3084 next = pos->parent;
3085 return next != cgroup ? next : NULL;
3086}
3087EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3088
Paul Menagebd89aab2007-10-18 23:40:44 -07003089void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003090 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003091{
3092 /*
3093 * The first time anyone tries to iterate across a cgroup,
3094 * we need to enable the list linking each css_set to its
3095 * tasks, and fix up all existing tasks.
3096 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003097 if (!use_task_css_set_links)
3098 cgroup_enable_task_cg_lists();
3099
Paul Menage817929e2007-10-18 23:39:36 -07003100 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003101 it->cg_link = &cgrp->css_sets;
3102 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003103}
3104
Paul Menagebd89aab2007-10-18 23:40:44 -07003105struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003106 struct cgroup_iter *it)
3107{
3108 struct task_struct *res;
3109 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003110 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003111
3112 /* If the iterator cg is NULL, we have no tasks */
3113 if (!it->cg_link)
3114 return NULL;
3115 res = list_entry(l, struct task_struct, cg_list);
3116 /* Advance iterator to find next entry */
3117 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003118 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3119 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003120 /* We reached the end of this task list - move on to
3121 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003122 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003123 } else {
3124 it->task = l;
3125 }
3126 return res;
3127}
3128
Paul Menagebd89aab2007-10-18 23:40:44 -07003129void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003130 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003131{
3132 read_unlock(&css_set_lock);
3133}
3134
Cliff Wickman31a7df02008-02-07 00:14:42 -08003135static inline int started_after_time(struct task_struct *t1,
3136 struct timespec *time,
3137 struct task_struct *t2)
3138{
3139 int start_diff = timespec_compare(&t1->start_time, time);
3140 if (start_diff > 0) {
3141 return 1;
3142 } else if (start_diff < 0) {
3143 return 0;
3144 } else {
3145 /*
3146 * Arbitrarily, if two processes started at the same
3147 * time, we'll say that the lower pointer value
3148 * started first. Note that t2 may have exited by now
3149 * so this may not be a valid pointer any longer, but
3150 * that's fine - it still serves to distinguish
3151 * between two tasks started (effectively) simultaneously.
3152 */
3153 return t1 > t2;
3154 }
3155}
3156
3157/*
3158 * This function is a callback from heap_insert() and is used to order
3159 * the heap.
3160 * In this case we order the heap in descending task start time.
3161 */
3162static inline int started_after(void *p1, void *p2)
3163{
3164 struct task_struct *t1 = p1;
3165 struct task_struct *t2 = p2;
3166 return started_after_time(t1, &t2->start_time, t2);
3167}
3168
3169/**
3170 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3171 * @scan: struct cgroup_scanner containing arguments for the scan
3172 *
3173 * Arguments include pointers to callback functions test_task() and
3174 * process_task().
3175 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3176 * and if it returns true, call process_task() for it also.
3177 * The test_task pointer may be NULL, meaning always true (select all tasks).
3178 * Effectively duplicates cgroup_iter_{start,next,end}()
3179 * but does not lock css_set_lock for the call to process_task().
3180 * The struct cgroup_scanner may be embedded in any structure of the caller's
3181 * creation.
3182 * It is guaranteed that process_task() will act on every task that
3183 * is a member of the cgroup for the duration of this call. This
3184 * function may or may not call process_task() for tasks that exit
3185 * or move to a different cgroup during the call, or are forked or
3186 * move into the cgroup during the call.
3187 *
3188 * Note that test_task() may be called with locks held, and may in some
3189 * situations be called multiple times for the same task, so it should
3190 * be cheap.
3191 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3192 * pre-allocated and will be used for heap operations (and its "gt" member will
3193 * be overwritten), else a temporary heap will be used (allocation of which
3194 * may cause this function to fail).
3195 */
3196int cgroup_scan_tasks(struct cgroup_scanner *scan)
3197{
3198 int retval, i;
3199 struct cgroup_iter it;
3200 struct task_struct *p, *dropped;
3201 /* Never dereference latest_task, since it's not refcounted */
3202 struct task_struct *latest_task = NULL;
3203 struct ptr_heap tmp_heap;
3204 struct ptr_heap *heap;
3205 struct timespec latest_time = { 0, 0 };
3206
3207 if (scan->heap) {
3208 /* The caller supplied our heap and pre-allocated its memory */
3209 heap = scan->heap;
3210 heap->gt = &started_after;
3211 } else {
3212 /* We need to allocate our own heap memory */
3213 heap = &tmp_heap;
3214 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3215 if (retval)
3216 /* cannot allocate the heap */
3217 return retval;
3218 }
3219
3220 again:
3221 /*
3222 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3223 * to determine which are of interest, and using the scanner's
3224 * "process_task" callback to process any of them that need an update.
3225 * Since we don't want to hold any locks during the task updates,
3226 * gather tasks to be processed in a heap structure.
3227 * The heap is sorted by descending task start time.
3228 * If the statically-sized heap fills up, we overflow tasks that
3229 * started later, and in future iterations only consider tasks that
3230 * started after the latest task in the previous pass. This
3231 * guarantees forward progress and that we don't miss any tasks.
3232 */
3233 heap->size = 0;
3234 cgroup_iter_start(scan->cg, &it);
3235 while ((p = cgroup_iter_next(scan->cg, &it))) {
3236 /*
3237 * Only affect tasks that qualify per the caller's callback,
3238 * if he provided one
3239 */
3240 if (scan->test_task && !scan->test_task(p, scan))
3241 continue;
3242 /*
3243 * Only process tasks that started after the last task
3244 * we processed
3245 */
3246 if (!started_after_time(p, &latest_time, latest_task))
3247 continue;
3248 dropped = heap_insert(heap, p);
3249 if (dropped == NULL) {
3250 /*
3251 * The new task was inserted; the heap wasn't
3252 * previously full
3253 */
3254 get_task_struct(p);
3255 } else if (dropped != p) {
3256 /*
3257 * The new task was inserted, and pushed out a
3258 * different task
3259 */
3260 get_task_struct(p);
3261 put_task_struct(dropped);
3262 }
3263 /*
3264 * Else the new task was newer than anything already in
3265 * the heap and wasn't inserted
3266 */
3267 }
3268 cgroup_iter_end(scan->cg, &it);
3269
3270 if (heap->size) {
3271 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003272 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003273 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003274 latest_time = q->start_time;
3275 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003276 }
3277 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003278 scan->process_task(q, scan);
3279 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003280 }
3281 /*
3282 * If we had to process any tasks at all, scan again
3283 * in case some of them were in the middle of forking
3284 * children that didn't get processed.
3285 * Not the most efficient way to do it, but it avoids
3286 * having to take callback_mutex in the fork path
3287 */
3288 goto again;
3289 }
3290 if (heap == &tmp_heap)
3291 heap_free(&tmp_heap);
3292 return 0;
3293}
3294
Tejun Heo8cc99342013-04-07 09:29:50 -07003295static void cgroup_transfer_one_task(struct task_struct *task,
3296 struct cgroup_scanner *scan)
3297{
3298 struct cgroup *new_cgroup = scan->data;
3299
Tejun Heo47cfcd02013-04-07 09:29:51 -07003300 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003301 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003302 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003303}
3304
3305/**
3306 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3307 * @to: cgroup to which the tasks will be moved
3308 * @from: cgroup in which the tasks currently reside
3309 */
3310int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3311{
3312 struct cgroup_scanner scan;
3313
3314 scan.cg = from;
3315 scan.test_task = NULL; /* select all tasks in cgroup */
3316 scan.process_task = cgroup_transfer_one_task;
3317 scan.heap = NULL;
3318 scan.data = to;
3319
3320 return cgroup_scan_tasks(&scan);
3321}
3322
Paul Menage817929e2007-10-18 23:39:36 -07003323/*
Ben Blum102a7752009-09-23 15:56:26 -07003324 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003325 *
3326 * Reading this file can return large amounts of data if a cgroup has
3327 * *lots* of attached tasks. So it may need several calls to read(),
3328 * but we cannot guarantee that the information we produce is correct
3329 * unless we produce it entirely atomically.
3330 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003331 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003332
Li Zefan24528252012-01-20 11:58:43 +08003333/* which pidlist file are we talking about? */
3334enum cgroup_filetype {
3335 CGROUP_FILE_PROCS,
3336 CGROUP_FILE_TASKS,
3337};
3338
3339/*
3340 * A pidlist is a list of pids that virtually represents the contents of one
3341 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3342 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3343 * to the cgroup.
3344 */
3345struct cgroup_pidlist {
3346 /*
3347 * used to find which pidlist is wanted. doesn't change as long as
3348 * this particular list stays in the list.
3349 */
3350 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3351 /* array of xids */
3352 pid_t *list;
3353 /* how many elements the above list has */
3354 int length;
3355 /* how many files are using the current array */
3356 int use_count;
3357 /* each of these stored in a list by its cgroup */
3358 struct list_head links;
3359 /* pointer to the cgroup we belong to, for list removal purposes */
3360 struct cgroup *owner;
3361 /* protects the other fields */
3362 struct rw_semaphore mutex;
3363};
3364
Paul Menagebbcb81d2007-10-18 23:39:32 -07003365/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003366 * The following two functions "fix" the issue where there are more pids
3367 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3368 * TODO: replace with a kernel-wide solution to this problem
3369 */
3370#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3371static void *pidlist_allocate(int count)
3372{
3373 if (PIDLIST_TOO_LARGE(count))
3374 return vmalloc(count * sizeof(pid_t));
3375 else
3376 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3377}
3378static void pidlist_free(void *p)
3379{
3380 if (is_vmalloc_addr(p))
3381 vfree(p);
3382 else
3383 kfree(p);
3384}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003385
3386/*
Ben Blum102a7752009-09-23 15:56:26 -07003387 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003388 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003389 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003390static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003391{
Ben Blum102a7752009-09-23 15:56:26 -07003392 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003393
3394 /*
3395 * we presume the 0th element is unique, so i starts at 1. trivial
3396 * edge cases first; no work needs to be done for either
3397 */
3398 if (length == 0 || length == 1)
3399 return length;
3400 /* src and dest walk down the list; dest counts unique elements */
3401 for (src = 1; src < length; src++) {
3402 /* find next unique element */
3403 while (list[src] == list[src-1]) {
3404 src++;
3405 if (src == length)
3406 goto after;
3407 }
3408 /* dest always points to where the next unique element goes */
3409 list[dest] = list[src];
3410 dest++;
3411 }
3412after:
Ben Blum102a7752009-09-23 15:56:26 -07003413 return dest;
3414}
3415
3416static int cmppid(const void *a, const void *b)
3417{
3418 return *(pid_t *)a - *(pid_t *)b;
3419}
3420
3421/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003422 * find the appropriate pidlist for our purpose (given procs vs tasks)
3423 * returns with the lock on that pidlist already held, and takes care
3424 * of the use count, or returns NULL with no locks held if we're out of
3425 * memory.
3426 */
3427static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3428 enum cgroup_filetype type)
3429{
3430 struct cgroup_pidlist *l;
3431 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003432 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003433
Ben Blum72a8cb32009-09-23 15:56:27 -07003434 /*
3435 * We can't drop the pidlist_mutex before taking the l->mutex in case
3436 * the last ref-holder is trying to remove l from the list at the same
3437 * time. Holding the pidlist_mutex precludes somebody taking whichever
3438 * list we find out from under us - compare release_pid_array().
3439 */
3440 mutex_lock(&cgrp->pidlist_mutex);
3441 list_for_each_entry(l, &cgrp->pidlists, links) {
3442 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003443 /* make sure l doesn't vanish out from under us */
3444 down_write(&l->mutex);
3445 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003446 return l;
3447 }
3448 }
3449 /* entry not found; create a new one */
3450 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3451 if (!l) {
3452 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003453 return l;
3454 }
3455 init_rwsem(&l->mutex);
3456 down_write(&l->mutex);
3457 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003458 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003459 l->use_count = 0; /* don't increment here */
3460 l->list = NULL;
3461 l->owner = cgrp;
3462 list_add(&l->links, &cgrp->pidlists);
3463 mutex_unlock(&cgrp->pidlist_mutex);
3464 return l;
3465}
3466
3467/*
Ben Blum102a7752009-09-23 15:56:26 -07003468 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3469 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003470static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3471 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003472{
3473 pid_t *array;
3474 int length;
3475 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003476 struct cgroup_iter it;
3477 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003478 struct cgroup_pidlist *l;
3479
3480 /*
3481 * If cgroup gets more users after we read count, we won't have
3482 * enough space - tough. This race is indistinguishable to the
3483 * caller from the case that the additional cgroup users didn't
3484 * show up until sometime later on.
3485 */
3486 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003487 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003488 if (!array)
3489 return -ENOMEM;
3490 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003491 cgroup_iter_start(cgrp, &it);
3492 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003493 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003494 break;
Ben Blum102a7752009-09-23 15:56:26 -07003495 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003496 if (type == CGROUP_FILE_PROCS)
3497 pid = task_tgid_vnr(tsk);
3498 else
3499 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003500 if (pid > 0) /* make sure to only use valid results */
3501 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003502 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003503 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003504 length = n;
3505 /* now sort & (if procs) strip out duplicates */
3506 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003507 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003508 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003509 l = cgroup_pidlist_find(cgrp, type);
3510 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003511 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003512 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003513 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003514 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003515 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003516 l->list = array;
3517 l->length = length;
3518 l->use_count++;
3519 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003521 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003522}
3523
Balbir Singh846c7bb2007-10-18 23:39:44 -07003524/**
Li Zefana043e3b2008-02-23 15:24:09 -08003525 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003526 * @stats: cgroupstats to fill information into
3527 * @dentry: A dentry entry belonging to the cgroup for which stats have
3528 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003529 *
3530 * Build and fill cgroupstats so that taskstats can export it to user
3531 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003532 */
3533int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3534{
3535 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003536 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003537 struct cgroup_iter it;
3538 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003539
Balbir Singh846c7bb2007-10-18 23:39:44 -07003540 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003541 * Validate dentry by checking the superblock operations,
3542 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003543 */
Li Zefan33d283b2008-11-19 15:36:48 -08003544 if (dentry->d_sb->s_op != &cgroup_ops ||
3545 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003546 goto err;
3547
3548 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003549 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003550
Paul Menagebd89aab2007-10-18 23:40:44 -07003551 cgroup_iter_start(cgrp, &it);
3552 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003553 switch (tsk->state) {
3554 case TASK_RUNNING:
3555 stats->nr_running++;
3556 break;
3557 case TASK_INTERRUPTIBLE:
3558 stats->nr_sleeping++;
3559 break;
3560 case TASK_UNINTERRUPTIBLE:
3561 stats->nr_uninterruptible++;
3562 break;
3563 case TASK_STOPPED:
3564 stats->nr_stopped++;
3565 break;
3566 default:
3567 if (delayacct_is_task_waiting_on_io(tsk))
3568 stats->nr_io_wait++;
3569 break;
3570 }
3571 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003572 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003573
Balbir Singh846c7bb2007-10-18 23:39:44 -07003574err:
3575 return ret;
3576}
3577
Paul Menage8f3ff202009-09-23 15:56:25 -07003578
Paul Menagecc31edc2008-10-18 20:28:04 -07003579/*
Ben Blum102a7752009-09-23 15:56:26 -07003580 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003581 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003582 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003583 */
3584
Ben Blum102a7752009-09-23 15:56:26 -07003585static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003586{
3587 /*
3588 * Initially we receive a position value that corresponds to
3589 * one more than the last pid shown (or 0 on the first call or
3590 * after a seek to the start). Use a binary-search to find the
3591 * next pid to display, if any
3592 */
Ben Blum102a7752009-09-23 15:56:26 -07003593 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 int index = 0, pid = *pos;
3595 int *iter;
3596
Ben Blum102a7752009-09-23 15:56:26 -07003597 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003598 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003599 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003600
Paul Menagecc31edc2008-10-18 20:28:04 -07003601 while (index < end) {
3602 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003603 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003604 index = mid;
3605 break;
Ben Blum102a7752009-09-23 15:56:26 -07003606 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003607 index = mid + 1;
3608 else
3609 end = mid;
3610 }
3611 }
3612 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003613 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003614 return NULL;
3615 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003616 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003617 *pos = *iter;
3618 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003619}
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003622{
Ben Blum102a7752009-09-23 15:56:26 -07003623 struct cgroup_pidlist *l = s->private;
3624 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003625}
3626
Ben Blum102a7752009-09-23 15:56:26 -07003627static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003628{
Ben Blum102a7752009-09-23 15:56:26 -07003629 struct cgroup_pidlist *l = s->private;
3630 pid_t *p = v;
3631 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003632 /*
3633 * Advance to the next pid in the array. If this goes off the
3634 * end, we're done
3635 */
3636 p++;
3637 if (p >= end) {
3638 return NULL;
3639 } else {
3640 *pos = *p;
3641 return p;
3642 }
3643}
3644
Ben Blum102a7752009-09-23 15:56:26 -07003645static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003646{
3647 return seq_printf(s, "%d\n", *(int *)v);
3648}
3649
Ben Blum102a7752009-09-23 15:56:26 -07003650/*
3651 * seq_operations functions for iterating on pidlists through seq_file -
3652 * independent of whether it's tasks or procs
3653 */
3654static const struct seq_operations cgroup_pidlist_seq_operations = {
3655 .start = cgroup_pidlist_start,
3656 .stop = cgroup_pidlist_stop,
3657 .next = cgroup_pidlist_next,
3658 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003659};
3660
Ben Blum102a7752009-09-23 15:56:26 -07003661static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003662{
Ben Blum72a8cb32009-09-23 15:56:27 -07003663 /*
3664 * the case where we're the last user of this particular pidlist will
3665 * have us remove it from the cgroup's list, which entails taking the
3666 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3667 * pidlist_mutex, we have to take pidlist_mutex first.
3668 */
3669 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003670 down_write(&l->mutex);
3671 BUG_ON(!l->use_count);
3672 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003673 /* we're the last user if refcount is 0; remove and free */
3674 list_del(&l->links);
3675 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003676 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003677 put_pid_ns(l->key.ns);
3678 up_write(&l->mutex);
3679 kfree(l);
3680 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003683 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003684}
3685
Ben Blum102a7752009-09-23 15:56:26 -07003686static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003687{
Ben Blum102a7752009-09-23 15:56:26 -07003688 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003689 if (!(file->f_mode & FMODE_READ))
3690 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003691 /*
3692 * the seq_file will only be initialized if the file was opened for
3693 * reading; hence we check if it's not null only in that case.
3694 */
3695 l = ((struct seq_file *)file->private_data)->private;
3696 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003697 return seq_release(inode, file);
3698}
3699
Ben Blum102a7752009-09-23 15:56:26 -07003700static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003701 .read = seq_read,
3702 .llseek = seq_lseek,
3703 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003704 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003705};
3706
3707/*
Ben Blum102a7752009-09-23 15:56:26 -07003708 * The following functions handle opens on a file that displays a pidlist
3709 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3710 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 */
Ben Blum102a7752009-09-23 15:56:26 -07003712/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003713static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003714{
3715 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003716 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003717 int retval;
3718
3719 /* Nothing to do for write-only files */
3720 if (!(file->f_mode & FMODE_READ))
3721 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003722
Ben Blum102a7752009-09-23 15:56:26 -07003723 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003724 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003725 if (retval)
3726 return retval;
3727 /* configure file information */
3728 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003729
Ben Blum102a7752009-09-23 15:56:26 -07003730 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003731 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003732 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003733 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003734 }
Ben Blum102a7752009-09-23 15:56:26 -07003735 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003736 return 0;
3737}
Ben Blum102a7752009-09-23 15:56:26 -07003738static int cgroup_tasks_open(struct inode *unused, struct file *file)
3739{
Ben Blum72a8cb32009-09-23 15:56:27 -07003740 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003741}
3742static int cgroup_procs_open(struct inode *unused, struct file *file)
3743{
Ben Blum72a8cb32009-09-23 15:56:27 -07003744 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003745}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003746
Paul Menagebd89aab2007-10-18 23:40:44 -07003747static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003748 struct cftype *cft)
3749{
Paul Menagebd89aab2007-10-18 23:40:44 -07003750 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003751}
3752
Paul Menage6379c102008-07-25 01:47:01 -07003753static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3754 struct cftype *cft,
3755 u64 val)
3756{
3757 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3758 if (val)
3759 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3760 else
3761 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3762 return 0;
3763}
3764
Paul Menagebbcb81d2007-10-18 23:39:32 -07003765/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003766 * Unregister event and free resources.
3767 *
3768 * Gets called from workqueue.
3769 */
3770static void cgroup_event_remove(struct work_struct *work)
3771{
3772 struct cgroup_event *event = container_of(work, struct cgroup_event,
3773 remove);
3774 struct cgroup *cgrp = event->cgrp;
3775
Li Zefan810cbee2013-02-18 18:56:14 +08003776 remove_wait_queue(event->wqh, &event->wait);
3777
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003778 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3779
Li Zefan810cbee2013-02-18 18:56:14 +08003780 /* Notify userspace the event is going away. */
3781 eventfd_signal(event->eventfd, 1);
3782
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003783 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003784 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003785 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003786}
3787
3788/*
3789 * Gets called on POLLHUP on eventfd when user closes it.
3790 *
3791 * Called with wqh->lock held and interrupts disabled.
3792 */
3793static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3794 int sync, void *key)
3795{
3796 struct cgroup_event *event = container_of(wait,
3797 struct cgroup_event, wait);
3798 struct cgroup *cgrp = event->cgrp;
3799 unsigned long flags = (unsigned long)key;
3800
3801 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003802 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003803 * If the event has been detached at cgroup removal, we
3804 * can simply return knowing the other side will cleanup
3805 * for us.
3806 *
3807 * We can't race against event freeing since the other
3808 * side will require wqh->lock via remove_wait_queue(),
3809 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003810 */
Li Zefan810cbee2013-02-18 18:56:14 +08003811 spin_lock(&cgrp->event_list_lock);
3812 if (!list_empty(&event->list)) {
3813 list_del_init(&event->list);
3814 /*
3815 * We are in atomic context, but cgroup_event_remove()
3816 * may sleep, so we have to call it in workqueue.
3817 */
3818 schedule_work(&event->remove);
3819 }
3820 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003821 }
3822
3823 return 0;
3824}
3825
3826static void cgroup_event_ptable_queue_proc(struct file *file,
3827 wait_queue_head_t *wqh, poll_table *pt)
3828{
3829 struct cgroup_event *event = container_of(pt,
3830 struct cgroup_event, pt);
3831
3832 event->wqh = wqh;
3833 add_wait_queue(wqh, &event->wait);
3834}
3835
3836/*
3837 * Parse input and register new cgroup event handler.
3838 *
3839 * Input must be in format '<event_fd> <control_fd> <args>'.
3840 * Interpretation of args is defined by control file implementation.
3841 */
3842static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3843 const char *buffer)
3844{
3845 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003846 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003847 unsigned int efd, cfd;
3848 struct file *efile = NULL;
3849 struct file *cfile = NULL;
3850 char *endp;
3851 int ret;
3852
3853 efd = simple_strtoul(buffer, &endp, 10);
3854 if (*endp != ' ')
3855 return -EINVAL;
3856 buffer = endp + 1;
3857
3858 cfd = simple_strtoul(buffer, &endp, 10);
3859 if ((*endp != ' ') && (*endp != '\0'))
3860 return -EINVAL;
3861 buffer = endp + 1;
3862
3863 event = kzalloc(sizeof(*event), GFP_KERNEL);
3864 if (!event)
3865 return -ENOMEM;
3866 event->cgrp = cgrp;
3867 INIT_LIST_HEAD(&event->list);
3868 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3869 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3870 INIT_WORK(&event->remove, cgroup_event_remove);
3871
3872 efile = eventfd_fget(efd);
3873 if (IS_ERR(efile)) {
3874 ret = PTR_ERR(efile);
3875 goto fail;
3876 }
3877
3878 event->eventfd = eventfd_ctx_fileget(efile);
3879 if (IS_ERR(event->eventfd)) {
3880 ret = PTR_ERR(event->eventfd);
3881 goto fail;
3882 }
3883
3884 cfile = fget(cfd);
3885 if (!cfile) {
3886 ret = -EBADF;
3887 goto fail;
3888 }
3889
3890 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003891 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003892 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003893 if (ret < 0)
3894 goto fail;
3895
3896 event->cft = __file_cft(cfile);
3897 if (IS_ERR(event->cft)) {
3898 ret = PTR_ERR(event->cft);
3899 goto fail;
3900 }
3901
Li Zefanf1690072013-02-18 14:13:35 +08003902 /*
3903 * The file to be monitored must be in the same cgroup as
3904 * cgroup.event_control is.
3905 */
3906 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3907 if (cgrp_cfile != cgrp) {
3908 ret = -EINVAL;
3909 goto fail;
3910 }
3911
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003912 if (!event->cft->register_event || !event->cft->unregister_event) {
3913 ret = -EINVAL;
3914 goto fail;
3915 }
3916
3917 ret = event->cft->register_event(cgrp, event->cft,
3918 event->eventfd, buffer);
3919 if (ret)
3920 goto fail;
3921
Li Zefan7ef70e42013-04-26 11:58:03 -07003922 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003924 /*
3925 * Events should be removed after rmdir of cgroup directory, but before
3926 * destroying subsystem state objects. Let's take reference to cgroup
3927 * directory dentry to do that.
3928 */
3929 dget(cgrp->dentry);
3930
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003931 spin_lock(&cgrp->event_list_lock);
3932 list_add(&event->list, &cgrp->event_list);
3933 spin_unlock(&cgrp->event_list_lock);
3934
3935 fput(cfile);
3936 fput(efile);
3937
3938 return 0;
3939
3940fail:
3941 if (cfile)
3942 fput(cfile);
3943
3944 if (event && event->eventfd && !IS_ERR(event->eventfd))
3945 eventfd_ctx_put(event->eventfd);
3946
3947 if (!IS_ERR_OR_NULL(efile))
3948 fput(efile);
3949
3950 kfree(event);
3951
3952 return ret;
3953}
3954
Daniel Lezcano97978e62010-10-27 15:33:35 -07003955static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3956 struct cftype *cft)
3957{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003958 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003959}
3960
3961static int cgroup_clone_children_write(struct cgroup *cgrp,
3962 struct cftype *cft,
3963 u64 val)
3964{
3965 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003966 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003967 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003968 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003969 return 0;
3970}
3971
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003973 * for the common functions, 'private' gives the type of file
3974 */
Ben Blum102a7752009-09-23 15:56:26 -07003975/* for hysterical raisins, we can't put this on the older files */
3976#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003977static struct cftype files[] = {
3978 {
3979 .name = "tasks",
3980 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003981 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003982 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003983 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003984 },
Ben Blum102a7752009-09-23 15:56:26 -07003985 {
3986 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3987 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003988 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003989 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003990 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003991 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003992 {
3993 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003994 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003995 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003996 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003997 {
3998 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3999 .write_string = cgroup_write_event_control,
4000 .mode = S_IWUGO,
4001 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004002 {
4003 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004004 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004005 .read_u64 = cgroup_clone_children_read,
4006 .write_u64 = cgroup_clone_children_write,
4007 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004008 {
Tejun Heo873fe092013-04-14 20:15:26 -07004009 .name = "cgroup.sane_behavior",
4010 .flags = CFTYPE_ONLY_ON_ROOT,
4011 .read_seq_string = cgroup_sane_behavior_show,
4012 },
4013 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004014 .name = "release_agent",
4015 .flags = CFTYPE_ONLY_ON_ROOT,
4016 .read_seq_string = cgroup_release_agent_show,
4017 .write_string = cgroup_release_agent_write,
4018 .max_write_len = PATH_MAX,
4019 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004020 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004021};
4022
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004023/**
4024 * cgroup_populate_dir - selectively creation of files in a directory
4025 * @cgrp: target cgroup
4026 * @base_files: true if the base files should be added
4027 * @subsys_mask: mask of the subsystem ids whose files should be added
4028 */
4029static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4030 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031{
4032 int err;
4033 struct cgroup_subsys *ss;
4034
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004035 if (base_files) {
4036 err = cgroup_addrm_files(cgrp, NULL, files, true);
4037 if (err < 0)
4038 return err;
4039 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004040
Tejun Heo8e3f6542012-04-01 12:09:55 -07004041 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004042 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004043 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004044 if (!test_bit(ss->subsys_id, &subsys_mask))
4045 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004046
Tejun Heodb0416b2012-04-01 12:09:55 -07004047 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004048 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004049 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004050
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004051 /* This cgroup is ready now */
4052 for_each_subsys(cgrp->root, ss) {
4053 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4054 /*
4055 * Update id->css pointer and make this css visible from
4056 * CSS ID functions. This pointer will be dereferened
4057 * from RCU-read-side without locks.
4058 */
4059 if (css->id)
4060 rcu_assign_pointer(css->id->css, css);
4061 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004062
4063 return 0;
4064}
4065
Tejun Heo48ddbe12012-04-01 12:09:56 -07004066static void css_dput_fn(struct work_struct *work)
4067{
4068 struct cgroup_subsys_state *css =
4069 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004070 struct dentry *dentry = css->cgroup->dentry;
4071 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004072
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004073 atomic_inc(&sb->s_active);
4074 dput(dentry);
4075 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004076}
4077
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078static void init_cgroup_css(struct cgroup_subsys_state *css,
4079 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004080 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004081{
Paul Menagebd89aab2007-10-18 23:40:44 -07004082 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004083 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004084 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004085 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004086 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004087 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004088 BUG_ON(cgrp->subsys[ss->subsys_id]);
4089 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004090
4091 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004092 * css holds an extra ref to @cgrp->dentry which is put on the last
4093 * css_put(). dput() requires process context, which css_put() may
4094 * be called without. @css->dput_work will be used to invoke
4095 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004096 */
4097 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098}
4099
Tejun Heob1929db2012-11-19 08:13:38 -08004100/* invoke ->post_create() on a new CSS and mark it online if successful */
4101static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004102{
Tejun Heob1929db2012-11-19 08:13:38 -08004103 int ret = 0;
4104
Tejun Heoa31f2d32012-11-19 08:13:37 -08004105 lockdep_assert_held(&cgroup_mutex);
4106
Tejun Heo92fb9742012-11-19 08:13:38 -08004107 if (ss->css_online)
4108 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004109 if (!ret)
4110 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4111 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004112}
4113
4114/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4115static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4116 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4117{
4118 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4119
4120 lockdep_assert_held(&cgroup_mutex);
4121
4122 if (!(css->flags & CSS_ONLINE))
4123 return;
4124
Li Zefand7eeac12013-03-12 15:35:59 -07004125 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004126 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004127
4128 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4129}
4130
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131/*
Li Zefana043e3b2008-02-23 15:24:09 -08004132 * cgroup_create - create a cgroup
4133 * @parent: cgroup that will be parent of the new cgroup
4134 * @dentry: dentry of the new cgroup
4135 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136 *
Li Zefana043e3b2008-02-23 15:24:09 -08004137 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004139static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004140 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141{
Paul Menagebd89aab2007-10-18 23:40:44 -07004142 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004143 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 struct cgroupfs_root *root = parent->root;
4145 int err = 0;
4146 struct cgroup_subsys *ss;
4147 struct super_block *sb = root->sb;
4148
Tejun Heo0a950f62012-11-19 09:02:12 -08004149 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004150 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4151 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152 return -ENOMEM;
4153
Li Zefan65dff752013-03-01 15:01:56 +08004154 name = cgroup_alloc_name(dentry);
4155 if (!name)
4156 goto err_free_cgrp;
4157 rcu_assign_pointer(cgrp->name, name);
4158
Tejun Heo0a950f62012-11-19 09:02:12 -08004159 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4160 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004161 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004162
Tejun Heo976c06b2012-11-05 09:16:59 -08004163 /*
4164 * Only live parents can have children. Note that the liveliness
4165 * check isn't strictly necessary because cgroup_mkdir() and
4166 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4167 * anyway so that locking is contained inside cgroup proper and we
4168 * don't get nasty surprises if we ever grow another caller.
4169 */
4170 if (!cgroup_lock_live_group(parent)) {
4171 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004172 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004173 }
4174
Paul Menageddbcc7e2007-10-18 23:39:30 -07004175 /* Grab a reference on the superblock so the hierarchy doesn't
4176 * get deleted on unmount if there are child cgroups. This
4177 * can be done outside cgroup_mutex, since the sb can't
4178 * disappear while someone has an open control file on the
4179 * fs */
4180 atomic_inc(&sb->s_active);
4181
Paul Menagecc31edc2008-10-18 20:28:04 -07004182 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183
Li Zefanfe1c06c2013-01-24 14:30:22 +08004184 dentry->d_fsdata = cgrp;
4185 cgrp->dentry = dentry;
4186
Paul Menagebd89aab2007-10-18 23:40:44 -07004187 cgrp->parent = parent;
4188 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004189
Li Zefanb6abdb02008-03-04 14:28:19 -08004190 if (notify_on_release(parent))
4191 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4192
Tejun Heo2260e7f2012-11-19 08:13:38 -08004193 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4194 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004195
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004197 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004198
Tejun Heo92fb9742012-11-19 08:13:38 -08004199 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200 if (IS_ERR(css)) {
4201 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004202 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004204 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004205 if (ss->use_id) {
4206 err = alloc_css_id(ss, parent, cgrp);
4207 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004208 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004209 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210 }
4211
Tejun Heo4e139af2012-11-19 08:13:36 -08004212 /*
4213 * Create directory. cgroup_create_file() returns with the new
4214 * directory locked on success so that it can be populated without
4215 * dropping cgroup_mutex.
4216 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004217 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004219 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004220 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221
Tejun Heo4e139af2012-11-19 08:13:36 -08004222 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004223 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4224 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4225 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004226
Tejun Heob1929db2012-11-19 08:13:38 -08004227 /* each css holds a ref to the cgroup's dentry */
4228 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004229 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004230
Li Zefan415cf072013-04-08 14:35:02 +08004231 /* hold a ref to the parent's dentry */
4232 dget(parent->dentry);
4233
Tejun Heob1929db2012-11-19 08:13:38 -08004234 /* creation succeeded, notify subsystems */
4235 for_each_subsys(root, ss) {
4236 err = online_css(ss, cgrp);
4237 if (err)
4238 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004239
4240 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4241 parent->parent) {
4242 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",
4243 current->comm, current->pid, ss->name);
4244 if (!strcmp(ss->name, "memory"))
4245 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4246 ss->warned_broken_hierarchy = true;
4247 }
Tejun Heoa8638032012-11-09 09:12:29 -08004248 }
4249
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004250 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004251 if (err)
4252 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004253
4254 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004255 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256
4257 return 0;
4258
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004259err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004261 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004262 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265 /* Release the reference count that we took on the superblock */
4266 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004267err_free_id:
4268 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004269err_free_name:
4270 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004271err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004272 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004274
4275err_destroy:
4276 cgroup_destroy_locked(cgrp);
4277 mutex_unlock(&cgroup_mutex);
4278 mutex_unlock(&dentry->d_inode->i_mutex);
4279 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280}
4281
Al Viro18bb1db2011-07-26 01:41:39 -04004282static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283{
4284 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4285
4286 /* the vfs holds inode->i_mutex already */
4287 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4288}
4289
Tejun Heo42809dd2012-11-19 08:13:37 -08004290static int cgroup_destroy_locked(struct cgroup *cgrp)
4291 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004292{
Tejun Heo42809dd2012-11-19 08:13:37 -08004293 struct dentry *d = cgrp->dentry;
4294 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004295 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004296 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297
Tejun Heo42809dd2012-11-19 08:13:37 -08004298 lockdep_assert_held(&d->d_inode->i_mutex);
4299 lockdep_assert_held(&cgroup_mutex);
4300
4301 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004303
Tejun Heo1a90dd52012-11-05 09:16:59 -08004304 /*
4305 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4306 * removed. This makes future css_tryget() and child creation
4307 * attempts fail thus maintaining the removal conditions verified
4308 * above.
4309 */
Tejun Heoed9577932012-11-05 09:16:58 -08004310 for_each_subsys(cgrp->root, ss) {
4311 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4312
4313 WARN_ON(atomic_read(&css->refcnt) < 0);
4314 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004315 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004316 set_bit(CGRP_REMOVED, &cgrp->flags);
4317
Tejun Heoa31f2d32012-11-19 08:13:37 -08004318 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004319 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004320 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004321
4322 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004323 * Put all the base refs. Each css holds an extra reference to the
4324 * cgroup's dentry and cgroup removal proceeds regardless of css
4325 * refs. On the last put of each css, whenever that may be, the
4326 * extra dentry ref is put so that dentry destruction happens only
4327 * after all css's are released.
4328 */
Tejun Heoe9316082012-11-05 09:16:58 -08004329 for_each_subsys(cgrp->root, ss)
4330 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004332 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004333 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004334 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004335 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004336
Paul Menage999cd8a2009-01-07 18:08:36 -08004337 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004338 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004339 list_del_init(&cgrp->allcg_node);
4340
Tejun Heo42809dd2012-11-19 08:13:37 -08004341 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004342 cgroup_d_remove_dir(d);
4343 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Paul Menagebd89aab2007-10-18 23:40:44 -07004345 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004346 check_for_release(parent);
4347
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004348 /*
4349 * Unregister events and notify userspace.
4350 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004351 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004352 */
4353 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004354 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004355 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004356 schedule_work(&event->remove);
4357 }
Li Zefan810cbee2013-02-18 18:56:14 +08004358 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004359
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360 return 0;
4361}
4362
Tejun Heo42809dd2012-11-19 08:13:37 -08004363static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4364{
4365 int ret;
4366
4367 mutex_lock(&cgroup_mutex);
4368 ret = cgroup_destroy_locked(dentry->d_fsdata);
4369 mutex_unlock(&cgroup_mutex);
4370
4371 return ret;
4372}
4373
Tejun Heo8e3f6542012-04-01 12:09:55 -07004374static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4375{
4376 INIT_LIST_HEAD(&ss->cftsets);
4377
4378 /*
4379 * base_cftset is embedded in subsys itself, no need to worry about
4380 * deregistration.
4381 */
4382 if (ss->base_cftypes) {
4383 ss->base_cftset.cfts = ss->base_cftypes;
4384 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4385 }
4386}
4387
Li Zefan06a11922008-04-29 01:00:07 -07004388static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004391
4392 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393
Tejun Heo648bb562012-11-19 08:13:36 -08004394 mutex_lock(&cgroup_mutex);
4395
Tejun Heo8e3f6542012-04-01 12:09:55 -07004396 /* init base cftset */
4397 cgroup_init_cftsets(ss);
4398
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004400 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004401 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004402 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403 /* We don't handle early failures gracefully */
4404 BUG_ON(IS_ERR(css));
4405 init_cgroup_css(css, ss, dummytop);
4406
Li Zefane8d55fd2008-04-29 01:00:13 -07004407 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004408 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004409 * newly registered, all tasks and hence the
4410 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004411 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004412
4413 need_forkexit_callback |= ss->fork || ss->exit;
4414
Li Zefane8d55fd2008-04-29 01:00:13 -07004415 /* At system boot, before all subsystems have been
4416 * registered, no tasks have been forked, so we don't
4417 * need to invoke fork callbacks here. */
4418 BUG_ON(!list_empty(&init_task.tasks));
4419
Tejun Heob1929db2012-11-19 08:13:38 -08004420 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004421
Tejun Heo648bb562012-11-19 08:13:36 -08004422 mutex_unlock(&cgroup_mutex);
4423
Ben Blume6a11052010-03-10 15:22:09 -08004424 /* this function shouldn't be used with modular subsystems, since they
4425 * need to register a subsys_id, among other things */
4426 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004427}
4428
4429/**
Ben Blume6a11052010-03-10 15:22:09 -08004430 * cgroup_load_subsys: load and register a modular subsystem at runtime
4431 * @ss: the subsystem to load
4432 *
4433 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004434 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004435 * up for use. If the subsystem is built-in anyway, work is delegated to the
4436 * simpler cgroup_init_subsys.
4437 */
4438int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4439{
Ben Blume6a11052010-03-10 15:22:09 -08004440 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004441 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004442 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004443 struct css_set *cg;
4444 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004445
4446 /* check name and function validity */
4447 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004448 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004449 return -EINVAL;
4450
4451 /*
4452 * we don't support callbacks in modular subsystems. this check is
4453 * before the ss->module check for consistency; a subsystem that could
4454 * be a module should still have no callbacks even if the user isn't
4455 * compiling it as one.
4456 */
4457 if (ss->fork || ss->exit)
4458 return -EINVAL;
4459
4460 /*
4461 * an optionally modular subsystem is built-in: we want to do nothing,
4462 * since cgroup_init_subsys will have already taken care of it.
4463 */
4464 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004465 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004466 BUG_ON(subsys[ss->subsys_id] != ss);
4467 return 0;
4468 }
4469
Tejun Heo8e3f6542012-04-01 12:09:55 -07004470 /* init base cftset */
4471 cgroup_init_cftsets(ss);
4472
Ben Blume6a11052010-03-10 15:22:09 -08004473 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004474 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004475
4476 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004477 * no ss->css_alloc seems to need anything important in the ss
4478 * struct, so this can happen first (i.e. before the rootnode
4479 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004480 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004481 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004482 if (IS_ERR(css)) {
4483 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004484 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004485 mutex_unlock(&cgroup_mutex);
4486 return PTR_ERR(css);
4487 }
4488
4489 list_add(&ss->sibling, &rootnode.subsys_list);
4490 ss->root = &rootnode;
4491
4492 /* our new subsystem will be attached to the dummy hierarchy. */
4493 init_cgroup_css(css, ss, dummytop);
4494 /* init_idr must be after init_cgroup_css because it sets css->id. */
4495 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004496 ret = cgroup_init_idr(ss, css);
4497 if (ret)
4498 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004499 }
4500
4501 /*
4502 * Now we need to entangle the css into the existing css_sets. unlike
4503 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4504 * will need a new pointer to it; done by iterating the css_set_table.
4505 * furthermore, modifying the existing css_sets will corrupt the hash
4506 * table state, so each changed css_set will need its hash recomputed.
4507 * this is all done under the css_set_lock.
4508 */
4509 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004510 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004511 /* skip entries that we already rehashed */
4512 if (cg->subsys[ss->subsys_id])
4513 continue;
4514 /* remove existing entry */
4515 hash_del(&cg->hlist);
4516 /* set new value */
4517 cg->subsys[ss->subsys_id] = css;
4518 /* recompute hash and restore entry */
4519 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004520 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004521 }
4522 write_unlock(&css_set_lock);
4523
Tejun Heob1929db2012-11-19 08:13:38 -08004524 ret = online_css(ss, dummytop);
4525 if (ret)
4526 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004527
Ben Blume6a11052010-03-10 15:22:09 -08004528 /* success! */
4529 mutex_unlock(&cgroup_mutex);
4530 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004531
4532err_unload:
4533 mutex_unlock(&cgroup_mutex);
4534 /* @ss can't be mounted here as try_module_get() would fail */
4535 cgroup_unload_subsys(ss);
4536 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004537}
4538EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4539
4540/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004541 * cgroup_unload_subsys: unload a modular subsystem
4542 * @ss: the subsystem to unload
4543 *
4544 * This function should be called in a modular subsystem's exitcall. When this
4545 * function is invoked, the refcount on the subsystem's module will be 0, so
4546 * the subsystem will not be attached to any hierarchy.
4547 */
4548void cgroup_unload_subsys(struct cgroup_subsys *ss)
4549{
4550 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004551
4552 BUG_ON(ss->module == NULL);
4553
4554 /*
4555 * we shouldn't be called if the subsystem is in use, and the use of
4556 * try_module_get in parse_cgroupfs_options should ensure that it
4557 * doesn't start being used while we're killing it off.
4558 */
4559 BUG_ON(ss->root != &rootnode);
4560
4561 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004562
Tejun Heoa31f2d32012-11-19 08:13:37 -08004563 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004564
Tejun Heoc897ff62013-02-27 17:03:49 -08004565 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004566 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004567
Ben Blumcf5d5942010-03-10 15:22:09 -08004568 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004569 subsys[ss->subsys_id] = NULL;
4570
4571 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004572 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004573
4574 /*
4575 * disentangle the css from all css_sets attached to the dummytop. as
4576 * in loading, we need to pay our respects to the hashtable gods.
4577 */
4578 write_lock(&css_set_lock);
4579 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4580 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004581 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004582
Li Zefan0ac801f2013-01-10 11:49:27 +08004583 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004584 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004585 key = css_set_hash(cg->subsys);
4586 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004587 }
4588 write_unlock(&css_set_lock);
4589
4590 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004591 * remove subsystem's css from the dummytop and free it - need to
4592 * free before marking as null because ss->css_free needs the
4593 * cgrp->subsys pointer to find their state. note that this also
4594 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004595 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004596 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004597 dummytop->subsys[ss->subsys_id] = NULL;
4598
4599 mutex_unlock(&cgroup_mutex);
4600}
4601EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4602
4603/**
Li Zefana043e3b2008-02-23 15:24:09 -08004604 * cgroup_init_early - cgroup initialization at system boot
4605 *
4606 * Initialize cgroups at system boot, and initialize any
4607 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608 */
4609int __init cgroup_init_early(void)
4610{
4611 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004612 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004613 INIT_LIST_HEAD(&init_css_set.cg_links);
4614 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004615 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004616 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004618 root_count = 1;
4619 init_task.cgroups = &init_css_set;
4620
4621 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004622 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004623 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004624 &rootnode.top_cgroup.css_sets);
4625 list_add(&init_css_set_link.cg_link_list,
4626 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004628 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629 struct cgroup_subsys *ss = subsys[i];
4630
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004631 /* at bootup time, we don't worry about modular subsystems */
4632 if (!ss || ss->module)
4633 continue;
4634
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635 BUG_ON(!ss->name);
4636 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004637 BUG_ON(!ss->css_alloc);
4638 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004640 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004641 ss->name, ss->subsys_id);
4642 BUG();
4643 }
4644
4645 if (ss->early_init)
4646 cgroup_init_subsys(ss);
4647 }
4648 return 0;
4649}
4650
4651/**
Li Zefana043e3b2008-02-23 15:24:09 -08004652 * cgroup_init - cgroup initialization
4653 *
4654 * Register cgroup filesystem and /proc file, and initialize
4655 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004656 */
4657int __init cgroup_init(void)
4658{
4659 int err;
4660 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004661 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004662
4663 err = bdi_init(&cgroup_backing_dev_info);
4664 if (err)
4665 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004667 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004668 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004669
4670 /* at bootup time, we don't worry about modular subsystems */
4671 if (!ss || ss->module)
4672 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673 if (!ss->early_init)
4674 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004675 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004676 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677 }
4678
Li Zefan472b1052008-04-29 01:00:11 -07004679 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004680 key = css_set_hash(init_css_set.subsys);
4681 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004682
4683 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004684 mutex_lock(&cgroup_mutex);
4685 mutex_lock(&cgroup_root_mutex);
4686
Tejun Heofa3ca072013-04-14 11:36:56 -07004687 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004688
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004689 mutex_unlock(&cgroup_root_mutex);
4690 mutex_unlock(&cgroup_mutex);
4691
Greg KH676db4a2010-08-05 13:53:35 -07004692 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4693 if (!cgroup_kobj) {
4694 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004695 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004696 }
4697
4698 err = register_filesystem(&cgroup_fs_type);
4699 if (err < 0) {
4700 kobject_put(cgroup_kobj);
4701 goto out;
4702 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004703
Li Zefan46ae2202008-04-29 01:00:08 -07004704 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004705
Paul Menageddbcc7e2007-10-18 23:39:30 -07004706out:
Paul Menagea4243162007-10-18 23:39:35 -07004707 if (err)
4708 bdi_destroy(&cgroup_backing_dev_info);
4709
Paul Menageddbcc7e2007-10-18 23:39:30 -07004710 return err;
4711}
Paul Menageb4f48b62007-10-18 23:39:33 -07004712
Paul Menagea4243162007-10-18 23:39:35 -07004713/*
4714 * proc_cgroup_show()
4715 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4716 * - Used for /proc/<pid>/cgroup.
4717 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4718 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004719 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004720 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4721 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4722 * cgroup to top_cgroup.
4723 */
4724
4725/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004726int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004727{
4728 struct pid *pid;
4729 struct task_struct *tsk;
4730 char *buf;
4731 int retval;
4732 struct cgroupfs_root *root;
4733
4734 retval = -ENOMEM;
4735 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4736 if (!buf)
4737 goto out;
4738
4739 retval = -ESRCH;
4740 pid = m->private;
4741 tsk = get_pid_task(pid, PIDTYPE_PID);
4742 if (!tsk)
4743 goto out_free;
4744
4745 retval = 0;
4746
4747 mutex_lock(&cgroup_mutex);
4748
Li Zefane5f6a862009-01-07 18:07:41 -08004749 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004750 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004751 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004752 int count = 0;
4753
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004754 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004755 for_each_subsys(root, ss)
4756 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004757 if (strlen(root->name))
4758 seq_printf(m, "%sname=%s", count ? "," : "",
4759 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004760 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004761 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004762 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004763 if (retval < 0)
4764 goto out_unlock;
4765 seq_puts(m, buf);
4766 seq_putc(m, '\n');
4767 }
4768
4769out_unlock:
4770 mutex_unlock(&cgroup_mutex);
4771 put_task_struct(tsk);
4772out_free:
4773 kfree(buf);
4774out:
4775 return retval;
4776}
4777
Paul Menagea4243162007-10-18 23:39:35 -07004778/* Display information about each subsystem and each hierarchy */
4779static int proc_cgroupstats_show(struct seq_file *m, void *v)
4780{
4781 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004782
Paul Menage8bab8dd2008-04-04 14:29:57 -07004783 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004784 /*
4785 * ideally we don't want subsystems moving around while we do this.
4786 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4787 * subsys/hierarchy state.
4788 */
Paul Menagea4243162007-10-18 23:39:35 -07004789 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004790 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4791 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004792 if (ss == NULL)
4793 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004794 seq_printf(m, "%s\t%d\t%d\t%d\n",
4795 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004796 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004797 }
4798 mutex_unlock(&cgroup_mutex);
4799 return 0;
4800}
4801
4802static int cgroupstats_open(struct inode *inode, struct file *file)
4803{
Al Viro9dce07f2008-03-29 03:07:28 +00004804 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004805}
4806
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004807static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004808 .open = cgroupstats_open,
4809 .read = seq_read,
4810 .llseek = seq_lseek,
4811 .release = single_release,
4812};
4813
Paul Menageb4f48b62007-10-18 23:39:33 -07004814/**
4815 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004816 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004817 *
4818 * Description: A task inherits its parent's cgroup at fork().
4819 *
4820 * A pointer to the shared css_set was automatically copied in
4821 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004822 * it was not made under the protection of RCU or cgroup_mutex, so
4823 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4824 * have already changed current->cgroups, allowing the previously
4825 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004826 *
4827 * At the point that cgroup_fork() is called, 'current' is the parent
4828 * task, and the passed argument 'child' points to the child task.
4829 */
4830void cgroup_fork(struct task_struct *child)
4831{
Tejun Heo9bb71302012-10-18 17:52:07 -07004832 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004833 child->cgroups = current->cgroups;
4834 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004835 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004836 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004837}
4838
4839/**
Li Zefana043e3b2008-02-23 15:24:09 -08004840 * cgroup_post_fork - called on a new task after adding it to the task list
4841 * @child: the task in question
4842 *
Tejun Heo5edee612012-10-16 15:03:14 -07004843 * Adds the task to the list running through its css_set if necessary and
4844 * call the subsystem fork() callbacks. Has to be after the task is
4845 * visible on the task list in case we race with the first call to
4846 * cgroup_iter_start() - to guarantee that the new task ends up on its
4847 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004848 */
Paul Menage817929e2007-10-18 23:39:36 -07004849void cgroup_post_fork(struct task_struct *child)
4850{
Tejun Heo5edee612012-10-16 15:03:14 -07004851 int i;
4852
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004853 /*
4854 * use_task_css_set_links is set to 1 before we walk the tasklist
4855 * under the tasklist_lock and we read it here after we added the child
4856 * to the tasklist under the tasklist_lock as well. If the child wasn't
4857 * yet in the tasklist when we walked through it from
4858 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4859 * should be visible now due to the paired locking and barriers implied
4860 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4861 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4862 * lock on fork.
4863 */
Paul Menage817929e2007-10-18 23:39:36 -07004864 if (use_task_css_set_links) {
4865 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004866 task_lock(child);
4867 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004868 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004869 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004870 write_unlock(&css_set_lock);
4871 }
Tejun Heo5edee612012-10-16 15:03:14 -07004872
4873 /*
4874 * Call ss->fork(). This must happen after @child is linked on
4875 * css_set; otherwise, @child might change state between ->fork()
4876 * and addition to css_set.
4877 */
4878 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004879 /*
4880 * fork/exit callbacks are supported only for builtin
4881 * subsystems, and the builtin section of the subsys
4882 * array is immutable, so we don't need to lock the
4883 * subsys array here. On the other hand, modular section
4884 * of the array can be freed at module unload, so we
4885 * can't touch that.
4886 */
4887 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004888 struct cgroup_subsys *ss = subsys[i];
4889
Tejun Heo5edee612012-10-16 15:03:14 -07004890 if (ss->fork)
4891 ss->fork(child);
4892 }
4893 }
Paul Menage817929e2007-10-18 23:39:36 -07004894}
Tejun Heo5edee612012-10-16 15:03:14 -07004895
Paul Menage817929e2007-10-18 23:39:36 -07004896/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004897 * cgroup_exit - detach cgroup from exiting task
4898 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004899 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004900 *
4901 * Description: Detach cgroup from @tsk and release it.
4902 *
4903 * Note that cgroups marked notify_on_release force every task in
4904 * them to take the global cgroup_mutex mutex when exiting.
4905 * This could impact scaling on very large systems. Be reluctant to
4906 * use notify_on_release cgroups where very high task exit scaling
4907 * is required on large systems.
4908 *
4909 * the_top_cgroup_hack:
4910 *
4911 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4912 *
4913 * We call cgroup_exit() while the task is still competent to
4914 * handle notify_on_release(), then leave the task attached to the
4915 * root cgroup in each hierarchy for the remainder of its exit.
4916 *
4917 * To do this properly, we would increment the reference count on
4918 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4919 * code we would add a second cgroup function call, to drop that
4920 * reference. This would just create an unnecessary hot spot on
4921 * the top_cgroup reference count, to no avail.
4922 *
4923 * Normally, holding a reference to a cgroup without bumping its
4924 * count is unsafe. The cgroup could go away, or someone could
4925 * attach us to a different cgroup, decrementing the count on
4926 * the first cgroup that we never incremented. But in this case,
4927 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004928 * which wards off any cgroup_attach_task() attempts, or task is a failed
4929 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004930 */
4931void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4932{
Paul Menage817929e2007-10-18 23:39:36 -07004933 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004934 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004935
4936 /*
4937 * Unlink from the css_set task list if necessary.
4938 * Optimistically check cg_list before taking
4939 * css_set_lock
4940 */
4941 if (!list_empty(&tsk->cg_list)) {
4942 write_lock(&css_set_lock);
4943 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004944 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004945 write_unlock(&css_set_lock);
4946 }
4947
Paul Menageb4f48b62007-10-18 23:39:33 -07004948 /* Reassign the task to the init_css_set. */
4949 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004950 cg = tsk->cgroups;
4951 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004952
4953 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004954 /*
4955 * fork/exit callbacks are supported only for builtin
4956 * subsystems, see cgroup_post_fork() for details.
4957 */
4958 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004959 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004960
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004961 if (ss->exit) {
4962 struct cgroup *old_cgrp =
4963 rcu_dereference_raw(cg->subsys[i])->cgroup;
4964 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004965 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004966 }
4967 }
4968 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004969 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004970
Li Zefanb5d646f2013-01-24 14:43:51 +08004971 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004972}
Paul Menage697f4162007-10-18 23:39:34 -07004973
Paul Menagebd89aab2007-10-18 23:40:44 -07004974static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004975{
4976 /* All of these checks rely on RCU to keep the cgroup
4977 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004978 if (cgroup_is_releasable(cgrp) &&
4979 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4980 /*
4981 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004982 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004983 * it now
4984 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004985 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004986
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004987 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004988 if (!cgroup_is_removed(cgrp) &&
4989 list_empty(&cgrp->release_list)) {
4990 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004991 need_schedule_work = 1;
4992 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004993 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004994 if (need_schedule_work)
4995 schedule_work(&release_agent_work);
4996 }
4997}
4998
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004999/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005000bool __css_tryget(struct cgroup_subsys_state *css)
5001{
Tejun Heoe9316082012-11-05 09:16:58 -08005002 while (true) {
5003 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005004
Tejun Heoe9316082012-11-05 09:16:58 -08005005 v = css_refcnt(css);
5006 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5007 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005008 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005009 else if (t < 0)
5010 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005011 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005012 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005013}
5014EXPORT_SYMBOL_GPL(__css_tryget);
5015
5016/* Caller must verify that the css is not for root cgroup */
5017void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005018{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005019 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005020
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005021 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005022 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005023 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005024}
Ben Blum67523c42010-03-10 15:22:11 -08005025EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026
5027/*
5028 * Notify userspace when a cgroup is released, by running the
5029 * configured release agent with the name of the cgroup (path
5030 * relative to the root of cgroup file system) as the argument.
5031 *
5032 * Most likely, this user command will try to rmdir this cgroup.
5033 *
5034 * This races with the possibility that some other task will be
5035 * attached to this cgroup before it is removed, or that some other
5036 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5037 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5038 * unused, and this cgroup will be reprieved from its death sentence,
5039 * to continue to serve a useful existence. Next time it's released,
5040 * we will get notified again, if it still has 'notify_on_release' set.
5041 *
5042 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5043 * means only wait until the task is successfully execve()'d. The
5044 * separate release agent task is forked by call_usermodehelper(),
5045 * then control in this thread returns here, without waiting for the
5046 * release agent task. We don't bother to wait because the caller of
5047 * this routine has no use for the exit status of the release agent
5048 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005049 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005050static void cgroup_release_agent(struct work_struct *work)
5051{
5052 BUG_ON(work != &release_agent_work);
5053 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005054 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055 while (!list_empty(&release_list)) {
5056 char *argv[3], *envp[3];
5057 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005058 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005059 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 struct cgroup,
5061 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005062 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005063 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005064 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005065 if (!pathbuf)
5066 goto continue_free;
5067 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5068 goto continue_free;
5069 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5070 if (!agentbuf)
5071 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072
5073 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005074 argv[i++] = agentbuf;
5075 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 argv[i] = NULL;
5077
5078 i = 0;
5079 /* minimal command environment */
5080 envp[i++] = "HOME=/";
5081 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5082 envp[i] = NULL;
5083
5084 /* Drop the lock while we invoke the usermode helper,
5085 * since the exec could involve hitting disk and hence
5086 * be a slow process */
5087 mutex_unlock(&cgroup_mutex);
5088 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005089 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005090 continue_free:
5091 kfree(pathbuf);
5092 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005093 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005094 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005095 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096 mutex_unlock(&cgroup_mutex);
5097}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005098
5099static int __init cgroup_disable(char *str)
5100{
5101 int i;
5102 char *token;
5103
5104 while ((token = strsep(&str, ",")) != NULL) {
5105 if (!*token)
5106 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005107 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005108 struct cgroup_subsys *ss = subsys[i];
5109
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005110 /*
5111 * cgroup_disable, being at boot time, can't
5112 * know about module subsystems, so we don't
5113 * worry about them.
5114 */
5115 if (!ss || ss->module)
5116 continue;
5117
Paul Menage8bab8dd2008-04-04 14:29:57 -07005118 if (!strcmp(token, ss->name)) {
5119 ss->disabled = 1;
5120 printk(KERN_INFO "Disabling %s control group"
5121 " subsystem\n", ss->name);
5122 break;
5123 }
5124 }
5125 }
5126 return 1;
5127}
5128__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005129
5130/*
5131 * Functons for CSS ID.
5132 */
5133
5134/*
5135 *To get ID other than 0, this should be called when !cgroup_is_removed().
5136 */
5137unsigned short css_id(struct cgroup_subsys_state *css)
5138{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005139 struct css_id *cssid;
5140
5141 /*
5142 * This css_id() can return correct value when somone has refcnt
5143 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5144 * it's unchanged until freed.
5145 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005146 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005147
5148 if (cssid)
5149 return cssid->id;
5150 return 0;
5151}
Ben Blum67523c42010-03-10 15:22:11 -08005152EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005153
5154unsigned short css_depth(struct cgroup_subsys_state *css)
5155{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005156 struct css_id *cssid;
5157
Tejun Heo28b4c272012-04-01 12:09:56 -07005158 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005159
5160 if (cssid)
5161 return cssid->depth;
5162 return 0;
5163}
Ben Blum67523c42010-03-10 15:22:11 -08005164EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005165
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005166/**
5167 * css_is_ancestor - test "root" css is an ancestor of "child"
5168 * @child: the css to be tested.
5169 * @root: the css supporsed to be an ancestor of the child.
5170 *
5171 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005172 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005173 * But, considering usual usage, the csses should be valid objects after test.
5174 * Assuming that the caller will do some action to the child if this returns
5175 * returns true, the caller must take "child";s reference count.
5176 * If "child" is valid object and this returns true, "root" is valid, too.
5177 */
5178
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005179bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005180 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005181{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005182 struct css_id *child_id;
5183 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005184
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005185 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005186 if (!child_id)
5187 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005188 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005189 if (!root_id)
5190 return false;
5191 if (child_id->depth < root_id->depth)
5192 return false;
5193 if (child_id->stack[root_id->depth] != root_id->id)
5194 return false;
5195 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005196}
5197
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005198void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5199{
5200 struct css_id *id = css->id;
5201 /* When this is called before css_id initialization, id can be NULL */
5202 if (!id)
5203 return;
5204
5205 BUG_ON(!ss->use_id);
5206
5207 rcu_assign_pointer(id->css, NULL);
5208 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005209 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005210 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005211 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005212 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005213}
Ben Blum67523c42010-03-10 15:22:11 -08005214EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215
5216/*
5217 * This is called by init or create(). Then, calls to this function are
5218 * always serialized (By cgroup_mutex() at create()).
5219 */
5220
5221static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5222{
5223 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005224 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225
5226 BUG_ON(!ss->use_id);
5227
5228 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5229 newid = kzalloc(size, GFP_KERNEL);
5230 if (!newid)
5231 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005232
5233 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005234 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005236 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005237 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005238 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005239
5240 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005241 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005242 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243
Tejun Heod228d9e2013-02-27 17:04:54 -08005244 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005245 newid->depth = depth;
5246 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005247err_out:
5248 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005249 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250
5251}
5252
Ben Blume6a11052010-03-10 15:22:09 -08005253static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5254 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005255{
5256 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005258 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005259 idr_init(&ss->idr);
5260
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261 newid = get_new_cssid(ss, 0);
5262 if (IS_ERR(newid))
5263 return PTR_ERR(newid);
5264
5265 newid->stack[0] = newid->id;
5266 newid->css = rootcss;
5267 rootcss->id = newid;
5268 return 0;
5269}
5270
5271static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5272 struct cgroup *child)
5273{
5274 int subsys_id, i, depth = 0;
5275 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005276 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005277
5278 subsys_id = ss->subsys_id;
5279 parent_css = parent->subsys[subsys_id];
5280 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005282 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005283
5284 child_id = get_new_cssid(ss, depth);
5285 if (IS_ERR(child_id))
5286 return PTR_ERR(child_id);
5287
5288 for (i = 0; i < depth; i++)
5289 child_id->stack[i] = parent_id->stack[i];
5290 child_id->stack[depth] = child_id->id;
5291 /*
5292 * child_id->css pointer will be set after this cgroup is available
5293 * see cgroup_populate_dir()
5294 */
5295 rcu_assign_pointer(child_css->id, child_id);
5296
5297 return 0;
5298}
5299
5300/**
5301 * css_lookup - lookup css by id
5302 * @ss: cgroup subsys to be looked into.
5303 * @id: the id
5304 *
5305 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5306 * NULL if not. Should be called under rcu_read_lock()
5307 */
5308struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5309{
5310 struct css_id *cssid = NULL;
5311
5312 BUG_ON(!ss->use_id);
5313 cssid = idr_find(&ss->idr, id);
5314
5315 if (unlikely(!cssid))
5316 return NULL;
5317
5318 return rcu_dereference(cssid->css);
5319}
Ben Blum67523c42010-03-10 15:22:11 -08005320EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321
Stephane Eraniane5d13672011-02-14 11:20:01 +02005322/*
5323 * get corresponding css from file open on cgroupfs directory
5324 */
5325struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5326{
5327 struct cgroup *cgrp;
5328 struct inode *inode;
5329 struct cgroup_subsys_state *css;
5330
Al Viro496ad9a2013-01-23 17:07:38 -05005331 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005332 /* check in cgroup filesystem dir */
5333 if (inode->i_op != &cgroup_dir_inode_operations)
5334 return ERR_PTR(-EBADF);
5335
5336 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5337 return ERR_PTR(-EINVAL);
5338
5339 /* get cgroup */
5340 cgrp = __d_cgrp(f->f_dentry);
5341 css = cgrp->subsys[id];
5342 return css ? css : ERR_PTR(-ENOENT);
5343}
5344
Paul Menagefe693432009-09-23 15:56:20 -07005345#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005346static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005347{
5348 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5349
5350 if (!css)
5351 return ERR_PTR(-ENOMEM);
5352
5353 return css;
5354}
5355
Tejun Heo92fb9742012-11-19 08:13:38 -08005356static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005357{
5358 kfree(cont->subsys[debug_subsys_id]);
5359}
5360
5361static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5362{
5363 return atomic_read(&cont->count);
5364}
5365
5366static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5367{
5368 return cgroup_task_count(cont);
5369}
5370
5371static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5372{
5373 return (u64)(unsigned long)current->cgroups;
5374}
5375
5376static u64 current_css_set_refcount_read(struct cgroup *cont,
5377 struct cftype *cft)
5378{
5379 u64 count;
5380
5381 rcu_read_lock();
5382 count = atomic_read(&current->cgroups->refcount);
5383 rcu_read_unlock();
5384 return count;
5385}
5386
Paul Menage7717f7b2009-09-23 15:56:22 -07005387static int current_css_set_cg_links_read(struct cgroup *cont,
5388 struct cftype *cft,
5389 struct seq_file *seq)
5390{
5391 struct cg_cgroup_link *link;
5392 struct css_set *cg;
5393
5394 read_lock(&css_set_lock);
5395 rcu_read_lock();
5396 cg = rcu_dereference(current->cgroups);
5397 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5398 struct cgroup *c = link->cgrp;
5399 const char *name;
5400
5401 if (c->dentry)
5402 name = c->dentry->d_name.name;
5403 else
5404 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005405 seq_printf(seq, "Root %d group %s\n",
5406 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005407 }
5408 rcu_read_unlock();
5409 read_unlock(&css_set_lock);
5410 return 0;
5411}
5412
5413#define MAX_TASKS_SHOWN_PER_CSS 25
5414static int cgroup_css_links_read(struct cgroup *cont,
5415 struct cftype *cft,
5416 struct seq_file *seq)
5417{
5418 struct cg_cgroup_link *link;
5419
5420 read_lock(&css_set_lock);
5421 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5422 struct css_set *cg = link->cg;
5423 struct task_struct *task;
5424 int count = 0;
5425 seq_printf(seq, "css_set %p\n", cg);
5426 list_for_each_entry(task, &cg->tasks, cg_list) {
5427 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5428 seq_puts(seq, " ...\n");
5429 break;
5430 } else {
5431 seq_printf(seq, " task %d\n",
5432 task_pid_vnr(task));
5433 }
5434 }
5435 }
5436 read_unlock(&css_set_lock);
5437 return 0;
5438}
5439
Paul Menagefe693432009-09-23 15:56:20 -07005440static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5441{
5442 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5443}
5444
5445static struct cftype debug_files[] = {
5446 {
5447 .name = "cgroup_refcount",
5448 .read_u64 = cgroup_refcount_read,
5449 },
5450 {
5451 .name = "taskcount",
5452 .read_u64 = debug_taskcount_read,
5453 },
5454
5455 {
5456 .name = "current_css_set",
5457 .read_u64 = current_css_set_read,
5458 },
5459
5460 {
5461 .name = "current_css_set_refcount",
5462 .read_u64 = current_css_set_refcount_read,
5463 },
5464
5465 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005466 .name = "current_css_set_cg_links",
5467 .read_seq_string = current_css_set_cg_links_read,
5468 },
5469
5470 {
5471 .name = "cgroup_css_links",
5472 .read_seq_string = cgroup_css_links_read,
5473 },
5474
5475 {
Paul Menagefe693432009-09-23 15:56:20 -07005476 .name = "releasable",
5477 .read_u64 = releasable_read,
5478 },
Paul Menagefe693432009-09-23 15:56:20 -07005479
Tejun Heo4baf6e32012-04-01 12:09:55 -07005480 { } /* terminate */
5481};
Paul Menagefe693432009-09-23 15:56:20 -07005482
5483struct cgroup_subsys debug_subsys = {
5484 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005485 .css_alloc = debug_css_alloc,
5486 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005487 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005488 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005489};
5490#endif /* CONFIG_CGROUP_DEBUG */