blob: c16719e056aba60b2a915b7ce6072fa733cf39b5 [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;
120};
121
122/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700123 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
124 * cgroup_subsys->use_id != 0.
125 */
126#define CSS_ID_MAX (65535)
127struct css_id {
128 /*
129 * The css to which this ID points. This pointer is set to valid value
130 * after cgroup is populated. If cgroup is removed, this will be NULL.
131 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800132 * is called after synchronize_rcu(). But for safe use, css_tryget()
133 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700134 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100135 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700136 /*
137 * ID of this css.
138 */
139 unsigned short id;
140 /*
141 * Depth in hierarchy which this ID belongs to.
142 */
143 unsigned short depth;
144 /*
145 * ID is freed by RCU. (and lookup routine is RCU safe.)
146 */
147 struct rcu_head rcu_head;
148 /*
149 * Hierarchy of CSS ID belongs to.
150 */
151 unsigned short stack[0]; /* Array of Length (depth+1) */
152};
153
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800154/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300155 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800156 */
157struct cgroup_event {
158 /*
159 * Cgroup which the event belongs to.
160 */
161 struct cgroup *cgrp;
162 /*
163 * Control file which the event associated.
164 */
165 struct cftype *cft;
166 /*
167 * eventfd to signal userspace about the event.
168 */
169 struct eventfd_ctx *eventfd;
170 /*
171 * Each of these stored in a list by the cgroup.
172 */
173 struct list_head list;
174 /*
175 * All fields below needed to unregister event when
176 * userspace closes eventfd.
177 */
178 poll_table pt;
179 wait_queue_head_t *wqh;
180 wait_queue_t wait;
181 struct work_struct remove;
182};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700183
Paul Menageddbcc7e2007-10-18 23:39:30 -0700184/* The list of hierarchy roots */
185
186static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700187static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700189static DEFINE_IDA(hierarchy_ida);
190static int next_hierarchy_id;
191static DEFINE_SPINLOCK(hierarchy_id_lock);
192
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
194#define dummytop (&rootnode.top_cgroup)
195
Li Zefan65dff752013-03-01 15:01:56 +0800196static struct cgroup_name root_cgroup_name = { .name = "/" };
197
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800199 * check for fork/exit handlers to call. This avoids us having to do
200 * extra work in the fork/exit path if none of the subsystems need to
201 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700203static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204
Tejun Heo42809dd2012-11-19 08:13:37 -0800205static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800206static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
207 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800208
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700209static int css_unbias_refcnt(int refcnt)
210{
211 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
212}
213
Tejun Heo28b4c272012-04-01 12:09:56 -0700214/* the current nr of refs, always >= 0 whether @css is deactivated or not */
215static int css_refcnt(struct cgroup_subsys_state *css)
216{
217 int v = atomic_read(&css->refcnt);
218
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700219 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700220}
221
Paul Menageddbcc7e2007-10-18 23:39:30 -0700222/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700223inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224{
Paul Menagebd89aab2007-10-18 23:40:44 -0700225 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226}
227
Li Zefan78574cf2013-04-08 19:00:38 -0700228/**
229 * cgroup_is_descendant - test ancestry
230 * @cgrp: the cgroup to be tested
231 * @ancestor: possible ancestor of @cgrp
232 *
233 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
234 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
235 * and @ancestor are accessible.
236 */
237bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
238{
239 while (cgrp) {
240 if (cgrp == ancestor)
241 return true;
242 cgrp = cgrp->parent;
243 }
244 return false;
245}
246EXPORT_SYMBOL_GPL(cgroup_is_descendant);
247
Adrian Bunke9685a02008-02-07 00:13:46 -0800248static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700249{
250 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700251 (1 << CGRP_RELEASABLE) |
252 (1 << CGRP_NOTIFY_ON_RELEASE);
253 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700254}
255
Adrian Bunke9685a02008-02-07 00:13:46 -0800256static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257{
Paul Menagebd89aab2007-10-18 23:40:44 -0700258 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700259}
260
Paul Menageddbcc7e2007-10-18 23:39:30 -0700261/*
262 * for_each_subsys() allows you to iterate on each subsystem attached to
263 * an active hierarchy
264 */
265#define for_each_subsys(_root, _ss) \
266list_for_each_entry(_ss, &_root->subsys_list, sibling)
267
Li Zefane5f6a862009-01-07 18:07:41 -0800268/* for_each_active_root() allows you to iterate across the active hierarchies */
269#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700270list_for_each_entry(_root, &roots, root_list)
271
Tejun Heof6ea9372012-04-01 12:09:55 -0700272static inline struct cgroup *__d_cgrp(struct dentry *dentry)
273{
274 return dentry->d_fsdata;
275}
276
Tejun Heo05ef1d72012-04-01 12:09:56 -0700277static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700278{
279 return dentry->d_fsdata;
280}
281
Tejun Heo05ef1d72012-04-01 12:09:56 -0700282static inline struct cftype *__d_cft(struct dentry *dentry)
283{
284 return __d_cfe(dentry)->type;
285}
286
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700287/**
288 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
289 * @cgrp: the cgroup to be checked for liveness
290 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700291 * On success, returns true; the mutex should be later unlocked. On
292 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700294static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295{
296 mutex_lock(&cgroup_mutex);
297 if (cgroup_is_removed(cgrp)) {
298 mutex_unlock(&cgroup_mutex);
299 return false;
300 }
301 return true;
302}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700303
Paul Menage81a6a5c2007-10-18 23:39:38 -0700304/* the list of cgroups eligible for automatic release. Protected by
305 * release_list_lock */
306static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200307static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700308static void cgroup_release_agent(struct work_struct *work);
309static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700310static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311
Paul Menage817929e2007-10-18 23:39:36 -0700312/* Link structure for associating css_set objects with cgroups */
313struct cg_cgroup_link {
314 /*
315 * List running through cg_cgroup_links associated with a
316 * cgroup, anchored on cgroup->css_sets
317 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700318 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700319 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700320 /*
321 * List running through cg_cgroup_links pointing at a
322 * single css_set object, anchored on css_set->cg_links
323 */
324 struct list_head cg_link_list;
325 struct css_set *cg;
326};
327
328/* The default css_set - used by init and its children prior to any
329 * hierarchies being mounted. It contains a pointer to the root state
330 * for each subsystem. Also used to anchor the list of css_sets. Not
331 * reference-counted, to improve performance when child cgroups
332 * haven't been created.
333 */
334
335static struct css_set init_css_set;
336static struct cg_cgroup_link init_css_set_link;
337
Ben Blume6a11052010-03-10 15:22:09 -0800338static int cgroup_init_idr(struct cgroup_subsys *ss,
339 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700340
Paul Menage817929e2007-10-18 23:39:36 -0700341/* css_set_lock protects the list of css_set objects, and the
342 * chain of tasks off each css_set. Nests outside task->alloc_lock
343 * due to cgroup_iter_start() */
344static DEFINE_RWLOCK(css_set_lock);
345static int css_set_count;
346
Paul Menage7717f7b2009-09-23 15:56:22 -0700347/*
348 * hash table for cgroup groups. This improves the performance to find
349 * an existing css_set. This hash doesn't (currently) take into
350 * account cgroups in empty hierarchies.
351 */
Li Zefan472b1052008-04-29 01:00:11 -0700352#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800353static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700354
Li Zefan0ac801f2013-01-10 11:49:27 +0800355static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700356{
357 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800358 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700359
360 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800361 key += (unsigned long)css[i];
362 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700363
Li Zefan0ac801f2013-01-10 11:49:27 +0800364 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700365}
366
Paul Menage817929e2007-10-18 23:39:36 -0700367/* We don't maintain the lists running through each css_set to its
368 * task until after the first call to cgroup_iter_start(). This
369 * reduces the fork()/exit() overhead for people who have cgroups
370 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700371static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700372
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700373static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700374{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700375 struct cg_cgroup_link *link;
376 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700377 /*
378 * Ensure that the refcount doesn't hit zero while any readers
379 * can see it. Similar to atomic_dec_and_lock(), but for an
380 * rwlock
381 */
382 if (atomic_add_unless(&cg->refcount, -1, 1))
383 return;
384 write_lock(&css_set_lock);
385 if (!atomic_dec_and_test(&cg->refcount)) {
386 write_unlock(&css_set_lock);
387 return;
388 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700389
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700390 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700392 css_set_count--;
393
394 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
395 cg_link_list) {
396 struct cgroup *cgrp = link->cgrp;
397 list_del(&link->cg_link_list);
398 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800399
400 /*
401 * We may not be holding cgroup_mutex, and if cgrp->count is
402 * dropped to 0 the cgroup can be destroyed at any time, hence
403 * rcu_read_lock is used to keep it alive.
404 */
405 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700406 if (atomic_dec_and_test(&cgrp->count) &&
407 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700408 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700409 set_bit(CGRP_RELEASABLE, &cgrp->flags);
410 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411 }
Li Zefan71b57072013-01-24 14:43:28 +0800412 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700413
414 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416
417 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800418 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700419}
420
421/*
422 * refcounted get/put for css_set objects
423 */
424static inline void get_css_set(struct css_set *cg)
425{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700426 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700427}
428
429static inline void put_css_set(struct css_set *cg)
430{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700431 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700432}
433
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434static inline void put_css_set_taskexit(struct css_set *cg)
435{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700436 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700437}
438
Paul Menage817929e2007-10-18 23:39:36 -0700439/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700440 * compare_css_sets - helper function for find_existing_css_set().
441 * @cg: candidate css_set being tested
442 * @old_cg: existing css_set for a task
443 * @new_cgrp: cgroup that's being entered by the task
444 * @template: desired set of css pointers in css_set (pre-calculated)
445 *
446 * Returns true if "cg" matches "old_cg" except for the hierarchy
447 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
448 */
449static bool compare_css_sets(struct css_set *cg,
450 struct css_set *old_cg,
451 struct cgroup *new_cgrp,
452 struct cgroup_subsys_state *template[])
453{
454 struct list_head *l1, *l2;
455
456 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
457 /* Not all subsystems matched */
458 return false;
459 }
460
461 /*
462 * Compare cgroup pointers in order to distinguish between
463 * different cgroups in heirarchies with no subsystems. We
464 * could get by with just this check alone (and skip the
465 * memcmp above) but on most setups the memcmp check will
466 * avoid the need for this more expensive check on almost all
467 * candidates.
468 */
469
470 l1 = &cg->cg_links;
471 l2 = &old_cg->cg_links;
472 while (1) {
473 struct cg_cgroup_link *cgl1, *cgl2;
474 struct cgroup *cg1, *cg2;
475
476 l1 = l1->next;
477 l2 = l2->next;
478 /* See if we reached the end - both lists are equal length. */
479 if (l1 == &cg->cg_links) {
480 BUG_ON(l2 != &old_cg->cg_links);
481 break;
482 } else {
483 BUG_ON(l2 == &old_cg->cg_links);
484 }
485 /* Locate the cgroups associated with these links. */
486 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
487 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
488 cg1 = cgl1->cgrp;
489 cg2 = cgl2->cgrp;
490 /* Hierarchies should be linked in the same order. */
491 BUG_ON(cg1->root != cg2->root);
492
493 /*
494 * If this hierarchy is the hierarchy of the cgroup
495 * that's changing, then we need to check that this
496 * css_set points to the new cgroup; if it's any other
497 * hierarchy, then this css_set should point to the
498 * same cgroup as the old css_set.
499 */
500 if (cg1->root == new_cgrp->root) {
501 if (cg1 != new_cgrp)
502 return false;
503 } else {
504 if (cg1 != cg2)
505 return false;
506 }
507 }
508 return true;
509}
510
511/*
Paul Menage817929e2007-10-18 23:39:36 -0700512 * find_existing_css_set() is a helper for
513 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700514 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700515 *
516 * oldcg: the cgroup group that we're using before the cgroup
517 * transition
518 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700519 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700520 *
521 * template: location in which to build the desired set of subsystem
522 * state objects for the new cgroup group
523 */
Paul Menage817929e2007-10-18 23:39:36 -0700524static struct css_set *find_existing_css_set(
525 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700526 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700527 struct cgroup_subsys_state *template[])
528{
529 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700530 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700531 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800532 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700533
Ben Blumaae8aab2010-03-10 15:22:07 -0800534 /*
535 * Build the set of subsystem state objects that we want to see in the
536 * new css_set. while subsystems can change globally, the entries here
537 * won't change, so no need for locking.
538 */
Paul Menage817929e2007-10-18 23:39:36 -0700539 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400540 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700541 /* Subsystem is in this hierarchy. So we want
542 * the subsystem state from the new
543 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700545 } else {
546 /* Subsystem is not in this hierarchy, so we
547 * don't want to change the subsystem state */
548 template[i] = oldcg->subsys[i];
549 }
550 }
551
Li Zefan0ac801f2013-01-10 11:49:27 +0800552 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800553 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700554 if (!compare_css_sets(cg, oldcg, cgrp, template))
555 continue;
556
557 /* This css_set matches what we need */
558 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700559 }
Paul Menage817929e2007-10-18 23:39:36 -0700560
561 /* No existing cgroup group matched */
562 return NULL;
563}
564
Paul Menage817929e2007-10-18 23:39:36 -0700565static void free_cg_links(struct list_head *tmp)
566{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700567 struct cg_cgroup_link *link;
568 struct cg_cgroup_link *saved_link;
569
570 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700571 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700572 kfree(link);
573 }
574}
575
576/*
Li Zefan36553432008-07-29 22:33:19 -0700577 * allocate_cg_links() allocates "count" cg_cgroup_link structures
578 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
579 * success or a negative error
580 */
581static int allocate_cg_links(int count, struct list_head *tmp)
582{
583 struct cg_cgroup_link *link;
584 int i;
585 INIT_LIST_HEAD(tmp);
586 for (i = 0; i < count; i++) {
587 link = kmalloc(sizeof(*link), GFP_KERNEL);
588 if (!link) {
589 free_cg_links(tmp);
590 return -ENOMEM;
591 }
592 list_add(&link->cgrp_link_list, tmp);
593 }
594 return 0;
595}
596
Li Zefanc12f65d2009-01-07 18:07:42 -0800597/**
598 * link_css_set - a helper function to link a css_set to a cgroup
599 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
600 * @cg: the css_set to be linked
601 * @cgrp: the destination cgroup
602 */
603static void link_css_set(struct list_head *tmp_cg_links,
604 struct css_set *cg, struct cgroup *cgrp)
605{
606 struct cg_cgroup_link *link;
607
608 BUG_ON(list_empty(tmp_cg_links));
609 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
610 cgrp_link_list);
611 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700612 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700613 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800614 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700615 /*
616 * Always add links to the tail of the list so that the list
617 * is sorted by order of hierarchy creation
618 */
619 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800620}
621
Li Zefan36553432008-07-29 22:33:19 -0700622/*
Paul Menage817929e2007-10-18 23:39:36 -0700623 * find_css_set() takes an existing cgroup group and a
624 * cgroup object, and returns a css_set object that's
625 * equivalent to the old group, but with the given cgroup
626 * substituted into the appropriate hierarchy. Must be called with
627 * cgroup_mutex held
628 */
Paul Menage817929e2007-10-18 23:39:36 -0700629static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700630 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700631{
632 struct css_set *res;
633 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700634
635 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700636
Paul Menage7717f7b2009-09-23 15:56:22 -0700637 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800638 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700639
Paul Menage817929e2007-10-18 23:39:36 -0700640 /* First see if we already have a cgroup group that matches
641 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700642 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700643 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700644 if (res)
645 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700646 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700647
648 if (res)
649 return res;
650
651 res = kmalloc(sizeof(*res), GFP_KERNEL);
652 if (!res)
653 return NULL;
654
655 /* Allocate all the cg_cgroup_link objects that we'll need */
656 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
657 kfree(res);
658 return NULL;
659 }
660
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700661 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700662 INIT_LIST_HEAD(&res->cg_links);
663 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700664 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700665
666 /* Copy the set of subsystem state objects generated in
667 * find_existing_css_set() */
668 memcpy(res->subsys, template, sizeof(res->subsys));
669
670 write_lock(&css_set_lock);
671 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700672 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
673 struct cgroup *c = link->cgrp;
674 if (c->root == cgrp->root)
675 c = cgrp;
676 link_css_set(&tmp_cg_links, res, c);
677 }
Paul Menage817929e2007-10-18 23:39:36 -0700678
679 BUG_ON(!list_empty(&tmp_cg_links));
680
Paul Menage817929e2007-10-18 23:39:36 -0700681 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700682
683 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800684 key = css_set_hash(res->subsys);
685 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700686
Paul Menage817929e2007-10-18 23:39:36 -0700687 write_unlock(&css_set_lock);
688
689 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700690}
691
Paul Menageddbcc7e2007-10-18 23:39:30 -0700692/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700693 * Return the cgroup for "task" from the given hierarchy. Must be
694 * called with cgroup_mutex held.
695 */
696static struct cgroup *task_cgroup_from_root(struct task_struct *task,
697 struct cgroupfs_root *root)
698{
699 struct css_set *css;
700 struct cgroup *res = NULL;
701
702 BUG_ON(!mutex_is_locked(&cgroup_mutex));
703 read_lock(&css_set_lock);
704 /*
705 * No need to lock the task - since we hold cgroup_mutex the
706 * task can't change groups, so the only thing that can happen
707 * is that it exits and its css is set back to init_css_set.
708 */
709 css = task->cgroups;
710 if (css == &init_css_set) {
711 res = &root->top_cgroup;
712 } else {
713 struct cg_cgroup_link *link;
714 list_for_each_entry(link, &css->cg_links, cg_link_list) {
715 struct cgroup *c = link->cgrp;
716 if (c->root == root) {
717 res = c;
718 break;
719 }
720 }
721 }
722 read_unlock(&css_set_lock);
723 BUG_ON(!res);
724 return res;
725}
726
727/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 * There is one global cgroup mutex. We also require taking
729 * task_lock() when dereferencing a task's cgroup subsys pointers.
730 * See "The task_lock() exception", at the end of this comment.
731 *
732 * A task must hold cgroup_mutex to modify cgroups.
733 *
734 * Any task can increment and decrement the count field without lock.
735 * So in general, code holding cgroup_mutex can't rely on the count
736 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800737 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738 * means that no tasks are currently attached, therefore there is no
739 * way a task attached to that cgroup can fork (the other way to
740 * increment the count). So code holding cgroup_mutex can safely
741 * assume that if the count is zero, it will stay zero. Similarly, if
742 * a task holds cgroup_mutex on a cgroup with zero count, it
743 * knows that the cgroup won't be removed, as cgroup_rmdir()
744 * needs that mutex.
745 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
747 * (usually) take cgroup_mutex. These are the two most performance
748 * critical pieces of code here. The exception occurs on cgroup_exit(),
749 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
750 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800751 * to the release agent with the name of the cgroup (path relative to
752 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753 *
754 * A cgroup can only be deleted if both its 'count' of using tasks
755 * is zero, and its list of 'children' cgroups is empty. Since all
756 * tasks in the system use _some_ cgroup, and since there is always at
757 * least one task in the system (init, pid == 1), therefore, top_cgroup
758 * always has either children cgroups and/or using tasks. So we don't
759 * need a special hack to ensure that top_cgroup cannot be deleted.
760 *
761 * The task_lock() exception
762 *
763 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800764 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800765 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * several performance critical places that need to reference
767 * task->cgroup without the expense of grabbing a system global
768 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800769 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700770 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
771 * the task_struct routinely used for such matters.
772 *
773 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800774 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 */
776
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777/*
778 * A couple of forward declarations required, due to cyclic reference loop:
779 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
780 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
781 * -> cgroup_mkdir.
782 */
783
Al Viro18bb1db2011-07-26 01:41:39 -0400784static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400785static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400787static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
788 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700789static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700790static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700791
792static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200793 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700794 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700795};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700797static int alloc_css_id(struct cgroup_subsys *ss,
798 struct cgroup *parent, struct cgroup *child);
799
Al Viroa5e7ed32011-07-26 01:55:55 -0400800static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801{
802 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803
804 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400805 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100807 inode->i_uid = current_fsuid();
808 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
810 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
811 }
812 return inode;
813}
814
Li Zefan65dff752013-03-01 15:01:56 +0800815static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
816{
817 struct cgroup_name *name;
818
819 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
820 if (!name)
821 return NULL;
822 strcpy(name->name, dentry->d_name.name);
823 return name;
824}
825
Li Zefanbe445622013-01-24 14:31:42 +0800826static void cgroup_free_fn(struct work_struct *work)
827{
828 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
829 struct cgroup_subsys *ss;
830
831 mutex_lock(&cgroup_mutex);
832 /*
833 * Release the subsystem state objects.
834 */
835 for_each_subsys(cgrp->root, ss)
836 ss->css_free(cgrp);
837
838 cgrp->root->number_of_cgroups--;
839 mutex_unlock(&cgroup_mutex);
840
841 /*
Li Zefan415cf072013-04-08 14:35:02 +0800842 * We get a ref to the parent's dentry, and put the ref when
843 * this cgroup is being freed, so it's guaranteed that the
844 * parent won't be destroyed before its children.
845 */
846 dput(cgrp->parent->dentry);
847
848 /*
Li Zefanbe445622013-01-24 14:31:42 +0800849 * Drop the active superblock reference that we took when we
850 * created the cgroup
851 */
852 deactivate_super(cgrp->root->sb);
853
854 /*
855 * if we're getting rid of the cgroup, refcount should ensure
856 * that there are no pidlists left.
857 */
858 BUG_ON(!list_empty(&cgrp->pidlists));
859
860 simple_xattrs_free(&cgrp->xattrs);
861
862 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +0800863 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800864 kfree(cgrp);
865}
866
867static void cgroup_free_rcu(struct rcu_head *head)
868{
869 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
870
871 schedule_work(&cgrp->free_work);
872}
873
Paul Menageddbcc7e2007-10-18 23:39:30 -0700874static void cgroup_diput(struct dentry *dentry, struct inode *inode)
875{
876 /* is dentry a directory ? if so, kfree() associated cgroup */
877 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700878 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800879
Paul Menagebd89aab2007-10-18 23:40:44 -0700880 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800881 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700882 } else {
883 struct cfent *cfe = __d_cfe(dentry);
884 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400885 struct cftype *cft = cfe->type;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700886
887 WARN_ONCE(!list_empty(&cfe->node) &&
888 cgrp != &cgrp->root->top_cgroup,
889 "cfe still linked for %s\n", cfe->type->name);
890 kfree(cfe);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -0400891 simple_xattrs_free(&cft->xattrs);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700892 }
893 iput(inode);
894}
895
Al Viroc72a04e2011-01-14 05:31:45 +0000896static int cgroup_delete(const struct dentry *d)
897{
898 return 1;
899}
900
Paul Menageddbcc7e2007-10-18 23:39:30 -0700901static void remove_dir(struct dentry *d)
902{
903 struct dentry *parent = dget(d->d_parent);
904
905 d_delete(d);
906 simple_rmdir(parent->d_inode, d);
907 dput(parent);
908}
909
Li Zefan2739d3c2013-01-21 18:18:33 +0800910static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700911{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700912 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
915 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100916
Li Zefan2739d3c2013-01-21 18:18:33 +0800917 /*
918 * If we're doing cleanup due to failure of cgroup_create(),
919 * the corresponding @cfe may not exist.
920 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700921 list_for_each_entry(cfe, &cgrp->files, node) {
922 struct dentry *d = cfe->dentry;
923
924 if (cft && cfe->type != cft)
925 continue;
926
927 dget(d);
928 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700929 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 list_del_init(&cfe->node);
931 dput(d);
932
Li Zefan2739d3c2013-01-21 18:18:33 +0800933 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935}
936
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400937/**
938 * cgroup_clear_directory - selective removal of base and subsystem files
939 * @dir: directory containing the files
940 * @base_files: true if the base files should be removed
941 * @subsys_mask: mask of the subsystem ids whose files should be removed
942 */
943static void cgroup_clear_directory(struct dentry *dir, bool base_files,
944 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700945{
946 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400947 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949 for_each_subsys(cgrp->root, ss) {
950 struct cftype_set *set;
951 if (!test_bit(ss->subsys_id, &subsys_mask))
952 continue;
953 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800954 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400955 }
956 if (base_files) {
957 while (!list_empty(&cgrp->files))
958 cgroup_rm_file(cgrp, NULL);
959 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960}
961
962/*
963 * NOTE : the dentry must have been dget()'ed
964 */
965static void cgroup_d_remove_dir(struct dentry *dentry)
966{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100967 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400968 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100969
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400970 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100972 parent = dentry->d_parent;
973 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800974 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100976 spin_unlock(&dentry->d_lock);
977 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978 remove_dir(dentry);
979}
980
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700981/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800982 * Call with cgroup_mutex held. Drops reference counts on modules, including
983 * any duplicate ones that parse_cgroupfs_options took. If this function
984 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800985 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400987 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400989 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700990 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 int i;
992
Ben Blumaae8aab2010-03-10 15:22:07 -0800993 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800994 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800995
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400996 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
997 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 /* Check that any added subsystems are currently free */
999 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001000 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001002 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001004 /*
1005 * Nobody should tell us to do a subsys that doesn't exist:
1006 * parse_cgroupfs_options should catch that case and refcounts
1007 * ensure that subsystems won't disappear once selected.
1008 */
1009 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 if (ss->root != &rootnode) {
1011 /* Subsystem isn't free */
1012 return -EBUSY;
1013 }
1014 }
1015
1016 /* Currently we don't handle adding/removing subsystems when
1017 * any child cgroups exist. This is theoretically supportable
1018 * but involves complex error handling, so it's being left until
1019 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001020 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 return -EBUSY;
1022
1023 /* Process each subsystem */
1024 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1025 struct cgroup_subsys *ss = subsys[i];
1026 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001027 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001029 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001030 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 BUG_ON(!dummytop->subsys[i]);
1032 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001033 cgrp->subsys[i] = dummytop->subsys[i];
1034 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001035 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001036 ss->root = root;
Tejun Heo26d5bbe2013-04-12 10:29:04 -07001037 if (ss->bind)
1038 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001039 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001040 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001042 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001043 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1044 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heo26d5bbe2013-04-12 10:29:04 -07001045 if (ss->bind)
1046 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001048 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001049 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001050 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001051 /* subsystem is now free - drop reference on module */
1052 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001053 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001055 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001056 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001057 /*
1058 * a refcount was taken, but we already had one, so
1059 * drop the extra reference.
1060 */
1061 module_put(ss->module);
1062#ifdef CONFIG_MODULE_UNLOAD
1063 BUG_ON(ss->module && !module_refcount(ss->module));
1064#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 } else {
1066 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001067 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 }
1069 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001070 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071
1072 return 0;
1073}
1074
Al Viro34c80b12011-12-08 21:32:45 -05001075static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076{
Al Viro34c80b12011-12-08 21:32:45 -05001077 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 struct cgroup_subsys *ss;
1079
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001080 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 for_each_subsys(root, ss)
1082 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001083 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1084 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001085 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001087 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001088 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001089 if (strlen(root->release_agent_path))
1090 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001091 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001092 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001093 if (strlen(root->name))
1094 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001095 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 return 0;
1097}
1098
1099struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001100 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001102 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001103 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001104 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001105 /* User explicitly requested empty subsystem */
1106 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001107
1108 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001109
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110};
1111
Ben Blumaae8aab2010-03-10 15:22:07 -08001112/*
1113 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001114 * with cgroup_mutex held to protect the subsys[] array. This function takes
1115 * refcounts on subsystems to be used, unless it returns error, in which case
1116 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001117 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001118static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001120 char *token, *o = data;
1121 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001122 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001123 int i;
1124 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1127
Li Zefanf9ab5b52009-06-17 16:26:33 -07001128#ifdef CONFIG_CPUSETS
1129 mask = ~(1UL << cpuset_subsys_id);
1130#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133
1134 while ((token = strsep(&o, ",")) != NULL) {
1135 if (!*token)
1136 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001137 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001138 /* Explicitly have no subsystems */
1139 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001140 continue;
1141 }
1142 if (!strcmp(token, "all")) {
1143 /* Mutually exclusive option 'all' + subsystem name */
1144 if (one_ss)
1145 return -EINVAL;
1146 all_ss = true;
1147 continue;
1148 }
Tejun Heo873fe092013-04-14 20:15:26 -07001149 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1150 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1151 continue;
1152 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001153 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001154 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001155 continue;
1156 }
1157 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001158 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 continue;
1160 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001161 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001162 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001163 continue;
1164 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001166 /* Specifying two release agents is forbidden */
1167 if (opts->release_agent)
1168 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001169 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001170 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001171 if (!opts->release_agent)
1172 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 continue;
1174 }
1175 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001176 const char *name = token + 5;
1177 /* Can't specify an empty name */
1178 if (!strlen(name))
1179 return -EINVAL;
1180 /* Must match [\w.-]+ */
1181 for (i = 0; i < strlen(name); i++) {
1182 char c = name[i];
1183 if (isalnum(c))
1184 continue;
1185 if ((c == '.') || (c == '-') || (c == '_'))
1186 continue;
1187 return -EINVAL;
1188 }
1189 /* Specifying two names is forbidden */
1190 if (opts->name)
1191 return -EINVAL;
1192 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001193 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001194 GFP_KERNEL);
1195 if (!opts->name)
1196 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197
1198 continue;
1199 }
1200
1201 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1202 struct cgroup_subsys *ss = subsys[i];
1203 if (ss == NULL)
1204 continue;
1205 if (strcmp(token, ss->name))
1206 continue;
1207 if (ss->disabled)
1208 continue;
1209
1210 /* Mutually exclusive option 'all' + subsystem name */
1211 if (all_ss)
1212 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001213 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001214 one_ss = true;
1215
1216 break;
1217 }
1218 if (i == CGROUP_SUBSYS_COUNT)
1219 return -ENOENT;
1220 }
1221
1222 /*
1223 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001224 * otherwise if 'none', 'name=' and a subsystem name options
1225 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001227 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001228 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1229 struct cgroup_subsys *ss = subsys[i];
1230 if (ss == NULL)
1231 continue;
1232 if (ss->disabled)
1233 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001234 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001235 }
1236 }
1237
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001238 /* Consistency checks */
1239
Tejun Heo873fe092013-04-14 20:15:26 -07001240 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1241 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1242
1243 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1244 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1245 return -EINVAL;
1246 }
1247
1248 if (opts->cpuset_clone_children) {
1249 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1250 return -EINVAL;
1251 }
1252 }
1253
Li Zefanf9ab5b52009-06-17 16:26:33 -07001254 /*
1255 * Option noprefix was introduced just for backward compatibility
1256 * with the old cpuset, so we allow noprefix only if mounting just
1257 * the cpuset subsystem.
1258 */
Tejun Heo93438622013-04-14 20:15:25 -07001259 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001260 return -EINVAL;
1261
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262
1263 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001264 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001265 return -EINVAL;
1266
1267 /*
1268 * We either have to specify by name or by subsystems. (So all
1269 * empty hierarchies must have a name).
1270 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001271 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 return -EINVAL;
1273
Ben Blumcf5d5942010-03-10 15:22:09 -08001274 /*
1275 * Grab references on all the modules we'll need, so the subsystems
1276 * don't dance around before rebind_subsystems attaches them. This may
1277 * take duplicate reference counts on a subsystem that's already used,
1278 * but rebind_subsystems handles this case.
1279 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001280 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 unsigned long bit = 1UL << i;
1282
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001283 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001284 continue;
1285 if (!try_module_get(subsys[i]->module)) {
1286 module_pin_failed = true;
1287 break;
1288 }
1289 }
1290 if (module_pin_failed) {
1291 /*
1292 * oops, one of the modules was going away. this means that we
1293 * raced with a module_delete call, and to the user this is
1294 * essentially a "subsystem doesn't exist" case.
1295 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001296 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 /* drop refcounts only on the ones we took */
1298 unsigned long bit = 1UL << i;
1299
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001300 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001301 continue;
1302 module_put(subsys[i]->module);
1303 }
1304 return -ENOENT;
1305 }
1306
Paul Menageddbcc7e2007-10-18 23:39:30 -07001307 return 0;
1308}
1309
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001310static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001311{
1312 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001313 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 unsigned long bit = 1UL << i;
1315
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001316 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001317 continue;
1318 module_put(subsys[i]->module);
1319 }
1320}
1321
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1323{
1324 int ret = 0;
1325 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001326 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001328 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329
Tejun Heo873fe092013-04-14 20:15:26 -07001330 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1331 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1332 return -EINVAL;
1333 }
1334
Paul Menagebd89aab2007-10-18 23:40:44 -07001335 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001337 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338
1339 /* See what subsystems are wanted */
1340 ret = parse_cgroupfs_options(data, &opts);
1341 if (ret)
1342 goto out_unlock;
1343
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001344 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001345 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1346 task_tgid_nr(current), current->comm);
1347
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001348 added_mask = opts.subsys_mask & ~root->subsys_mask;
1349 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001350
Ben Blumcf5d5942010-03-10 15:22:09 -08001351 /* Don't allow flags or name to change at remount */
1352 if (opts.flags != root->flags ||
1353 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001355 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001356 goto out_unlock;
1357 }
1358
Gao feng7083d032012-12-03 09:28:18 +08001359 /*
1360 * Clear out the files of subsystems that should be removed, do
1361 * this before rebind_subsystems, since rebind_subsystems may
1362 * change this hierarchy's subsys_list.
1363 */
1364 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1365
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001366 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001367 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001368 /* rebind_subsystems failed, re-populate the removed files */
1369 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001370 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001371 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001372 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001374 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001375 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376
Paul Menage81a6a5c2007-10-18 23:39:38 -07001377 if (opts.release_agent)
1378 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001380 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001381 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001382 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001384 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 return ret;
1386}
1387
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001388static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 .statfs = simple_statfs,
1390 .drop_inode = generic_delete_inode,
1391 .show_options = cgroup_show_options,
1392 .remount_fs = cgroup_remount,
1393};
1394
Paul Menagecc31edc2008-10-18 20:28:04 -07001395static void init_cgroup_housekeeping(struct cgroup *cgrp)
1396{
1397 INIT_LIST_HEAD(&cgrp->sibling);
1398 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001399 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001400 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001401 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001402 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001403 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001404 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001405 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001406 INIT_LIST_HEAD(&cgrp->event_list);
1407 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001408 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001409}
Paul Menagec6d57f32009-09-23 15:56:19 -07001410
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411static void init_cgroup_root(struct cgroupfs_root *root)
1412{
Paul Menagebd89aab2007-10-18 23:40:44 -07001413 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001414
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 INIT_LIST_HEAD(&root->subsys_list);
1416 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001417 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001419 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001420 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001421 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001422 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423}
1424
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001425static bool init_root_id(struct cgroupfs_root *root)
1426{
1427 int ret = 0;
1428
1429 do {
1430 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
1431 return false;
1432 spin_lock(&hierarchy_id_lock);
1433 /* Try to allocate the next unused ID */
1434 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1435 &root->hierarchy_id);
1436 if (ret == -ENOSPC)
1437 /* Try again starting from 0 */
1438 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1439 if (!ret) {
1440 next_hierarchy_id = root->hierarchy_id + 1;
1441 } else if (ret != -EAGAIN) {
1442 /* Can only get here if the 31-bit IDR is full ... */
1443 BUG_ON(ret);
1444 }
1445 spin_unlock(&hierarchy_id_lock);
1446 } while (ret);
1447 return true;
1448}
1449
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450static int cgroup_test_super(struct super_block *sb, void *data)
1451{
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001453 struct cgroupfs_root *root = sb->s_fs_info;
1454
Paul Menagec6d57f32009-09-23 15:56:19 -07001455 /* If we asked for a name then it must match */
1456 if (opts->name && strcmp(opts->name, root->name))
1457 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001459 /*
1460 * If we asked for subsystems (or explicitly for no
1461 * subsystems) then they must match
1462 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001463 if ((opts->subsys_mask || opts->none)
1464 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465 return 0;
1466
1467 return 1;
1468}
1469
Paul Menagec6d57f32009-09-23 15:56:19 -07001470static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1471{
1472 struct cgroupfs_root *root;
1473
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001474 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001475 return NULL;
1476
1477 root = kzalloc(sizeof(*root), GFP_KERNEL);
1478 if (!root)
1479 return ERR_PTR(-ENOMEM);
1480
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001481 if (!init_root_id(root)) {
1482 kfree(root);
1483 return ERR_PTR(-ENOMEM);
1484 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001486
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001487 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001489 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 if (opts->release_agent)
1491 strcpy(root->release_agent_path, opts->release_agent);
1492 if (opts->name)
1493 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001494 if (opts->cpuset_clone_children)
1495 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 return root;
1497}
1498
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001499static void cgroup_drop_root(struct cgroupfs_root *root)
1500{
1501 if (!root)
1502 return;
1503
1504 BUG_ON(!root->hierarchy_id);
1505 spin_lock(&hierarchy_id_lock);
1506 ida_remove(&hierarchy_ida, root->hierarchy_id);
1507 spin_unlock(&hierarchy_id_lock);
Tejun Heo0a950f62012-11-19 09:02:12 -08001508 ida_destroy(&root->cgroup_ida);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001509 kfree(root);
1510}
1511
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512static int cgroup_set_super(struct super_block *sb, void *data)
1513{
1514 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 struct cgroup_sb_opts *opts = data;
1516
1517 /* If we don't have a new root, we can't set up a new sb */
1518 if (!opts->new_root)
1519 return -EINVAL;
1520
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001521 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522
1523 ret = set_anon_super(sb, NULL);
1524 if (ret)
1525 return ret;
1526
Paul Menagec6d57f32009-09-23 15:56:19 -07001527 sb->s_fs_info = opts->new_root;
1528 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529
1530 sb->s_blocksize = PAGE_CACHE_SIZE;
1531 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1532 sb->s_magic = CGROUP_SUPER_MAGIC;
1533 sb->s_op = &cgroup_ops;
1534
1535 return 0;
1536}
1537
1538static int cgroup_get_rootdir(struct super_block *sb)
1539{
Al Viro0df6a632010-12-21 13:29:29 -05001540 static const struct dentry_operations cgroup_dops = {
1541 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001542 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001543 };
1544
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545 struct inode *inode =
1546 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547
1548 if (!inode)
1549 return -ENOMEM;
1550
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 inode->i_fop = &simple_dir_operations;
1552 inode->i_op = &cgroup_dir_inode_operations;
1553 /* directories start off with i_nlink == 2 (for "." entry) */
1554 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001555 sb->s_root = d_make_root(inode);
1556 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001558 /* for everything else we want ->d_op set */
1559 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 return 0;
1561}
1562
Al Virof7e83572010-07-26 13:23:11 +04001563static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001565 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566{
1567 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001568 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 int ret = 0;
1570 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001572 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573
1574 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001575 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001577 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 if (ret)
1579 goto out_err;
1580
1581 /*
1582 * Allocate a new cgroup root. We may not need it if we're
1583 * reusing an existing hierarchy.
1584 */
1585 new_root = cgroup_root_from_opts(&opts);
1586 if (IS_ERR(new_root)) {
1587 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001588 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001589 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001593 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001595 ret = PTR_ERR(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001596 cgroup_drop_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001597 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598 }
1599
Paul Menagec6d57f32009-09-23 15:56:19 -07001600 root = sb->s_fs_info;
1601 BUG_ON(!root);
1602 if (root == opts.new_root) {
1603 /* We used the new root structure, so this is a new hierarchy */
1604 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001605 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001606 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001607 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001608 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001609 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610
1611 BUG_ON(sb->s_root != NULL);
1612
1613 ret = cgroup_get_rootdir(sb);
1614 if (ret)
1615 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001616 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617
Paul Menage817929e2007-10-18 23:39:36 -07001618 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001620 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001622 /* Check for name clashes with existing mounts */
1623 ret = -EBUSY;
1624 if (strlen(root->name))
1625 for_each_active_root(existing_root)
1626 if (!strcmp(existing_root->name, root->name))
1627 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001628
Paul Menage817929e2007-10-18 23:39:36 -07001629 /*
1630 * We're accessing css_set_count without locking
1631 * css_set_lock here, but that's OK - it can only be
1632 * increased by someone holding cgroup_lock, and
1633 * that's us. The worst that can happen is that we
1634 * have some link structures left over
1635 */
1636 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001637 if (ret)
1638 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001639
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001640 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001642 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001643 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001645 /*
1646 * There must be no failure case after here, since rebinding
1647 * takes care of subsystems' refcounts, which are explicitly
1648 * dropped in the failure exit path.
1649 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650
1651 /* EBUSY should be the only error here */
1652 BUG_ON(ret);
1653
1654 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001655 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656
Li Zefanc12f65d2009-01-07 18:07:42 -08001657 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658 root->top_cgroup.dentry = sb->s_root;
1659
Paul Menage817929e2007-10-18 23:39:36 -07001660 /* Link the top cgroup in this hierarchy into all
1661 * the css_set objects */
1662 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001663 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001664 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001665 write_unlock(&css_set_lock);
1666
1667 free_cg_links(&tmp_cg_links);
1668
Li Zefanc12f65d2009-01-07 18:07:42 -08001669 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 BUG_ON(root->number_of_cgroups != 1);
1671
eparis@redhat2ce97382011-06-02 21:20:51 +10001672 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001673 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001674 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001675 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001677 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001678 } else {
1679 /*
1680 * We re-used an existing hierarchy - the new root (if
1681 * any) is not needed
1682 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001683 cgroup_drop_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001684
1685 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1686 root->flags != opts.flags) {
1687 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1688 ret = -EINVAL;
1689 goto drop_new_super;
1690 }
1691
Ben Blumcf5d5942010-03-10 15:22:09 -08001692 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001693 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 }
1695
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 kfree(opts.release_agent);
1697 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001698 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001700 unlock_drop:
1701 mutex_unlock(&cgroup_root_mutex);
1702 mutex_unlock(&cgroup_mutex);
1703 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001705 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001706 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001707 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001708 out_err:
1709 kfree(opts.release_agent);
1710 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001711 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712}
1713
1714static void cgroup_kill_sb(struct super_block *sb) {
1715 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001716 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001718 struct cg_cgroup_link *link;
1719 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720
1721 BUG_ON(!root);
1722
1723 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001724 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725
1726 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728
1729 /* Rebind all subsystems back to the default hierarchy */
1730 ret = rebind_subsystems(root, 0);
1731 /* Shouldn't be able to fail ... */
1732 BUG_ON(ret);
1733
Paul Menage817929e2007-10-18 23:39:36 -07001734 /*
1735 * Release all the links from css_sets to this hierarchy's
1736 * root cgroup
1737 */
1738 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001739
1740 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1741 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001742 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001744 kfree(link);
1745 }
1746 write_unlock(&css_set_lock);
1747
Paul Menage839ec542009-01-29 14:25:22 -08001748 if (!list_empty(&root->root_list)) {
1749 list_del(&root->root_list);
1750 root_count--;
1751 }
Li Zefane5f6a862009-01-07 18:07:41 -08001752
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001753 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 mutex_unlock(&cgroup_mutex);
1755
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001756 simple_xattrs_free(&cgrp->xattrs);
1757
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 kill_litter_super(sb);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001759 cgroup_drop_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760}
1761
1762static struct file_system_type cgroup_fs_type = {
1763 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001764 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 .kill_sb = cgroup_kill_sb,
1766};
1767
Greg KH676db4a2010-08-05 13:53:35 -07001768static struct kobject *cgroup_kobj;
1769
Li Zefana043e3b2008-02-23 15:24:09 -08001770/**
1771 * cgroup_path - generate the path of a cgroup
1772 * @cgrp: the cgroup in question
1773 * @buf: the buffer to write the path into
1774 * @buflen: the length of the buffer
1775 *
Li Zefan65dff752013-03-01 15:01:56 +08001776 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1777 *
1778 * We can't generate cgroup path using dentry->d_name, as accessing
1779 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1780 * inode's i_mutex, while on the other hand cgroup_path() can be called
1781 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001783int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784{
Li Zefan65dff752013-03-01 15:01:56 +08001785 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001787
Tejun Heoda1f2962013-04-14 10:32:19 -07001788 if (!cgrp->parent) {
1789 if (strlcpy(buf, "/", buflen) >= buflen)
1790 return -ENAMETOOLONG;
1791 return 0;
1792 }
1793
Tao Ma316eb662012-11-08 21:36:38 +08001794 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001795 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001796
Li Zefan65dff752013-03-01 15:01:56 +08001797 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001798 do {
Li Zefan65dff752013-03-01 15:01:56 +08001799 const char *name = cgroup_name(cgrp);
1800 int len;
1801
1802 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001804 goto out;
1805 memcpy(start, name, len);
1806
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001808 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001810
1811 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001812 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001813 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001815out:
1816 rcu_read_unlock();
1817 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818}
Ben Blum67523c42010-03-10 15:22:11 -08001819EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820
Ben Blum74a11662011-05-26 16:25:20 -07001821/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001822 * Control Group taskset
1823 */
Tejun Heo134d3372011-12-12 18:12:21 -08001824struct task_and_cgroup {
1825 struct task_struct *task;
1826 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001827 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001828};
1829
Tejun Heo2f7ee562011-12-12 18:12:21 -08001830struct cgroup_taskset {
1831 struct task_and_cgroup single;
1832 struct flex_array *tc_array;
1833 int tc_array_len;
1834 int idx;
1835 struct cgroup *cur_cgrp;
1836};
1837
1838/**
1839 * cgroup_taskset_first - reset taskset and return the first task
1840 * @tset: taskset of interest
1841 *
1842 * @tset iteration is initialized and the first task is returned.
1843 */
1844struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1845{
1846 if (tset->tc_array) {
1847 tset->idx = 0;
1848 return cgroup_taskset_next(tset);
1849 } else {
1850 tset->cur_cgrp = tset->single.cgrp;
1851 return tset->single.task;
1852 }
1853}
1854EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1855
1856/**
1857 * cgroup_taskset_next - iterate to the next task in taskset
1858 * @tset: taskset of interest
1859 *
1860 * Return the next task in @tset. Iteration must have been initialized
1861 * with cgroup_taskset_first().
1862 */
1863struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1864{
1865 struct task_and_cgroup *tc;
1866
1867 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1868 return NULL;
1869
1870 tc = flex_array_get(tset->tc_array, tset->idx++);
1871 tset->cur_cgrp = tc->cgrp;
1872 return tc->task;
1873}
1874EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1875
1876/**
1877 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1878 * @tset: taskset of interest
1879 *
1880 * Return the cgroup for the current (last returned) task of @tset. This
1881 * function must be preceded by either cgroup_taskset_first() or
1882 * cgroup_taskset_next().
1883 */
1884struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1885{
1886 return tset->cur_cgrp;
1887}
1888EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1889
1890/**
1891 * cgroup_taskset_size - return the number of tasks in taskset
1892 * @tset: taskset of interest
1893 */
1894int cgroup_taskset_size(struct cgroup_taskset *tset)
1895{
1896 return tset->tc_array ? tset->tc_array_len : 1;
1897}
1898EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1899
1900
Ben Blum74a11662011-05-26 16:25:20 -07001901/*
1902 * cgroup_task_migrate - move a task from one cgroup to another.
1903 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001904 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001905 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001906static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001907 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001908{
1909 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001910
1911 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001912 * We are synchronized through threadgroup_lock() against PF_EXITING
1913 * setting such that we can't race against cgroup_exit() changing the
1914 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001915 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001916 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001917 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001918
Ben Blum74a11662011-05-26 16:25:20 -07001919 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001920 rcu_assign_pointer(tsk->cgroups, newcg);
1921 task_unlock(tsk);
1922
1923 /* Update the css_set linked lists if we're using them */
1924 write_lock(&css_set_lock);
1925 if (!list_empty(&tsk->cg_list))
1926 list_move(&tsk->cg_list, &newcg->tasks);
1927 write_unlock(&css_set_lock);
1928
1929 /*
1930 * We just gained a reference on oldcg by taking it from the task. As
1931 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1932 * it here; it will be freed under RCU.
1933 */
Ben Blum74a11662011-05-26 16:25:20 -07001934 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001935 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001936}
1937
Li Zefana043e3b2008-02-23 15:24:09 -08001938/**
Li Zefan081aa452013-03-13 09:17:09 +08001939 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001940 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001941 * @tsk: the task or the leader of the threadgroup to be attached
1942 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001943 *
Tejun Heo257058a2011-12-12 18:12:21 -08001944 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001945 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001946 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001947static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1948 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
1950 int retval, i, group_size;
1951 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001952 struct cgroupfs_root *root = cgrp->root;
1953 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001954 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001955 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001956 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001957 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001958
1959 /*
1960 * step 0: in order to do expensive, possibly blocking operations for
1961 * every thread, we cannot iterate the thread group list, since it needs
1962 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001963 * group - group_rwsem prevents new threads from appearing, and if
1964 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001965 */
Li Zefan081aa452013-03-13 09:17:09 +08001966 if (threadgroup)
1967 group_size = get_nr_threads(tsk);
1968 else
1969 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001970 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001971 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001972 if (!group)
1973 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001974 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001975 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001976 if (retval)
1977 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001978
Ben Blum74a11662011-05-26 16:25:20 -07001979 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001980 /*
1981 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1982 * already PF_EXITING could be freed from underneath us unless we
1983 * take an rcu_read_lock.
1984 */
1985 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001986 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001987 struct task_and_cgroup ent;
1988
Tejun Heocd3d0952011-12-12 18:12:21 -08001989 /* @tsk either already exited or can't exit until the end */
1990 if (tsk->flags & PF_EXITING)
1991 continue;
1992
Ben Blum74a11662011-05-26 16:25:20 -07001993 /* as per above, nr_threads may decrease, but not increase. */
1994 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001995 ent.task = tsk;
1996 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001997 /* nothing to do if this task is already in the cgroup */
1998 if (ent.cgrp == cgrp)
1999 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002000 /*
2001 * saying GFP_ATOMIC has no effect here because we did prealloc
2002 * earlier, but it's good form to communicate our expectations.
2003 */
Tejun Heo134d3372011-12-12 18:12:21 -08002004 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002005 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002006 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002007
2008 if (!threadgroup)
2009 break;
Ben Blum74a11662011-05-26 16:25:20 -07002010 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002011 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002012 /* remember the number of threads in the array for later. */
2013 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002014 tset.tc_array = group;
2015 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002016
Tejun Heo134d3372011-12-12 18:12:21 -08002017 /* methods shouldn't be called if no task is actually migrating */
2018 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002019 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002020 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002021
Ben Blum74a11662011-05-26 16:25:20 -07002022 /*
2023 * step 1: check that we can legitimately attach to the cgroup.
2024 */
2025 for_each_subsys(root, ss) {
2026 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002027 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002028 if (retval) {
2029 failed_ss = ss;
2030 goto out_cancel_attach;
2031 }
2032 }
Ben Blum74a11662011-05-26 16:25:20 -07002033 }
2034
2035 /*
2036 * step 2: make sure css_sets exist for all threads to be migrated.
2037 * we use find_css_set, which allocates a new one if necessary.
2038 */
Ben Blum74a11662011-05-26 16:25:20 -07002039 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002040 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002041 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2042 if (!tc->cg) {
2043 retval = -ENOMEM;
2044 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002045 }
2046 }
2047
2048 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002049 * step 3: now that we're guaranteed success wrt the css_sets,
2050 * proceed to move all tasks to the new cgroup. There are no
2051 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002052 */
Ben Blum74a11662011-05-26 16:25:20 -07002053 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002054 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002055 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002056 }
2057 /* nothing is sensitive to fork() after this point. */
2058
2059 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002060 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002061 */
2062 for_each_subsys(root, ss) {
2063 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002064 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002065 }
2066
2067 /*
2068 * step 5: success! and cleanup
2069 */
Ben Blum74a11662011-05-26 16:25:20 -07002070 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002071out_put_css_set_refs:
2072 if (retval) {
2073 for (i = 0; i < group_size; i++) {
2074 tc = flex_array_get(group, i);
2075 if (!tc->cg)
2076 break;
2077 put_css_set(tc->cg);
2078 }
Ben Blum74a11662011-05-26 16:25:20 -07002079 }
2080out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002081 if (retval) {
2082 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002083 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002084 break;
Ben Blum74a11662011-05-26 16:25:20 -07002085 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002086 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002087 }
2088 }
Ben Blum74a11662011-05-26 16:25:20 -07002089out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002090 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002091 return retval;
2092}
2093
2094/*
2095 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002096 * function to attach either it or all tasks in its threadgroup. Will lock
2097 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002098 */
2099static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002100{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002101 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002102 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002103 int ret;
2104
Ben Blum74a11662011-05-26 16:25:20 -07002105 if (!cgroup_lock_live_group(cgrp))
2106 return -ENODEV;
2107
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002108retry_find_task:
2109 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002110 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002111 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002113 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002114 ret= -ESRCH;
2115 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002116 }
Ben Blum74a11662011-05-26 16:25:20 -07002117 /*
2118 * even if we're attaching all tasks in the thread group, we
2119 * only need to check permissions on one of them.
2120 */
David Howellsc69e8d92008-11-14 10:39:19 +11002121 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002122 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2123 !uid_eq(cred->euid, tcred->uid) &&
2124 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002125 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002126 ret = -EACCES;
2127 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002129 } else
2130 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002131
2132 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002134
2135 /*
2136 * Workqueue threads may acquire PF_THREAD_BOUND and become
2137 * trapped in a cpuset, or RT worker may be born in a cgroup
2138 * with no rt_runtime allocated. Just say no.
2139 */
2140 if (tsk == kthreadd_task || (tsk->flags & PF_THREAD_BOUND)) {
2141 ret = -EINVAL;
2142 rcu_read_unlock();
2143 goto out_unlock_cgroup;
2144 }
2145
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002146 get_task_struct(tsk);
2147 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002148
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002149 threadgroup_lock(tsk);
2150 if (threadgroup) {
2151 if (!thread_group_leader(tsk)) {
2152 /*
2153 * a race with de_thread from another thread's exec()
2154 * may strip us of our leadership, if this happens,
2155 * there is no choice but to throw this task away and
2156 * try again; this is
2157 * "double-double-toil-and-trouble-check locking".
2158 */
2159 threadgroup_unlock(tsk);
2160 put_task_struct(tsk);
2161 goto retry_find_task;
2162 }
Li Zefan081aa452013-03-13 09:17:09 +08002163 }
2164
2165 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2166
Tejun Heocd3d0952011-12-12 18:12:21 -08002167 threadgroup_unlock(tsk);
2168
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002170out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002171 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002172 return ret;
2173}
2174
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002175/**
2176 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2177 * @from: attach to all cgroups of a given task
2178 * @tsk: the task to be attached
2179 */
2180int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2181{
2182 struct cgroupfs_root *root;
2183 int retval = 0;
2184
Tejun Heo47cfcd02013-04-07 09:29:51 -07002185 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002186 for_each_active_root(root) {
2187 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2188
2189 retval = cgroup_attach_task(from_cg, tsk, false);
2190 if (retval)
2191 break;
2192 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002193 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002194
2195 return retval;
2196}
2197EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2198
Paul Menageaf351022008-07-25 01:47:01 -07002199static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2200{
Ben Blum74a11662011-05-26 16:25:20 -07002201 return attach_task_by_pid(cgrp, pid, false);
2202}
2203
2204static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2205{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002207}
2208
Paul Menagee788e062008-07-25 01:46:59 -07002209static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2210 const char *buffer)
2211{
2212 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002213 if (strlen(buffer) >= PATH_MAX)
2214 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002215 if (!cgroup_lock_live_group(cgrp))
2216 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002217 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002218 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002219 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002220 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002221 return 0;
2222}
2223
2224static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2225 struct seq_file *seq)
2226{
2227 if (!cgroup_lock_live_group(cgrp))
2228 return -ENODEV;
2229 seq_puts(seq, cgrp->root->release_agent_path);
2230 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002231 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002232 return 0;
2233}
2234
Tejun Heo873fe092013-04-14 20:15:26 -07002235static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2236 struct seq_file *seq)
2237{
2238 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
2239 return 0;
2240}
2241
Paul Menage84eea842008-07-25 01:47:00 -07002242/* A buffer size big enough for numbers or short strings */
2243#define CGROUP_LOCAL_BUFFER_SIZE 64
2244
Paul Menagee73d2c62008-04-29 01:00:06 -07002245static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002246 struct file *file,
2247 const char __user *userbuf,
2248 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002249{
Paul Menage84eea842008-07-25 01:47:00 -07002250 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002251 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002252 char *end;
2253
2254 if (!nbytes)
2255 return -EINVAL;
2256 if (nbytes >= sizeof(buffer))
2257 return -E2BIG;
2258 if (copy_from_user(buffer, userbuf, nbytes))
2259 return -EFAULT;
2260
2261 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002262 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002263 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002264 if (*end)
2265 return -EINVAL;
2266 retval = cft->write_u64(cgrp, cft, val);
2267 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002268 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002269 if (*end)
2270 return -EINVAL;
2271 retval = cft->write_s64(cgrp, cft, val);
2272 }
Paul Menage355e0c42007-10-18 23:39:33 -07002273 if (!retval)
2274 retval = nbytes;
2275 return retval;
2276}
2277
Paul Menagedb3b1492008-07-25 01:46:58 -07002278static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2279 struct file *file,
2280 const char __user *userbuf,
2281 size_t nbytes, loff_t *unused_ppos)
2282{
Paul Menage84eea842008-07-25 01:47:00 -07002283 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002284 int retval = 0;
2285 size_t max_bytes = cft->max_write_len;
2286 char *buffer = local_buffer;
2287
2288 if (!max_bytes)
2289 max_bytes = sizeof(local_buffer) - 1;
2290 if (nbytes >= max_bytes)
2291 return -E2BIG;
2292 /* Allocate a dynamic buffer if we need one */
2293 if (nbytes >= sizeof(local_buffer)) {
2294 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2295 if (buffer == NULL)
2296 return -ENOMEM;
2297 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002298 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2299 retval = -EFAULT;
2300 goto out;
2301 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002302
2303 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002304 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002305 if (!retval)
2306 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002307out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002308 if (buffer != local_buffer)
2309 kfree(buffer);
2310 return retval;
2311}
2312
Paul Menageddbcc7e2007-10-18 23:39:30 -07002313static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2314 size_t nbytes, loff_t *ppos)
2315{
2316 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002317 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002318
Li Zefan75139b82009-01-07 18:07:33 -08002319 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002321 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002322 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002323 if (cft->write_u64 || cft->write_s64)
2324 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002325 if (cft->write_string)
2326 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002327 if (cft->trigger) {
2328 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2329 return ret ? ret : nbytes;
2330 }
Paul Menage355e0c42007-10-18 23:39:33 -07002331 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002332}
2333
Paul Menagef4c753b2008-04-29 00:59:56 -07002334static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2335 struct file *file,
2336 char __user *buf, size_t nbytes,
2337 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002338{
Paul Menage84eea842008-07-25 01:47:00 -07002339 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002340 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002341 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2342
2343 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2344}
2345
Paul Menagee73d2c62008-04-29 01:00:06 -07002346static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2347 struct file *file,
2348 char __user *buf, size_t nbytes,
2349 loff_t *ppos)
2350{
Paul Menage84eea842008-07-25 01:47:00 -07002351 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002352 s64 val = cft->read_s64(cgrp, cft);
2353 int len = sprintf(tmp, "%lld\n", (long long) val);
2354
2355 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2356}
2357
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2359 size_t nbytes, loff_t *ppos)
2360{
2361 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002362 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363
Li Zefan75139b82009-01-07 18:07:33 -08002364 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365 return -ENODEV;
2366
2367 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002368 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002369 if (cft->read_u64)
2370 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002371 if (cft->read_s64)
2372 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373 return -EINVAL;
2374}
2375
Paul Menage91796562008-04-29 01:00:01 -07002376/*
2377 * seqfile ops/methods for returning structured data. Currently just
2378 * supports string->u64 maps, but can be extended in future.
2379 */
2380
2381struct cgroup_seqfile_state {
2382 struct cftype *cft;
2383 struct cgroup *cgroup;
2384};
2385
2386static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2387{
2388 struct seq_file *sf = cb->state;
2389 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2390}
2391
2392static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2393{
2394 struct cgroup_seqfile_state *state = m->private;
2395 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002396 if (cft->read_map) {
2397 struct cgroup_map_cb cb = {
2398 .fill = cgroup_map_add,
2399 .state = m,
2400 };
2401 return cft->read_map(state->cgroup, cft, &cb);
2402 }
2403 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002404}
2405
Adrian Bunk96930a62008-07-25 19:46:21 -07002406static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002407{
2408 struct seq_file *seq = file->private_data;
2409 kfree(seq->private);
2410 return single_release(inode, file);
2411}
2412
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002413static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002414 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002415 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002416 .llseek = seq_lseek,
2417 .release = cgroup_seqfile_release,
2418};
2419
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420static int cgroup_file_open(struct inode *inode, struct file *file)
2421{
2422 int err;
2423 struct cftype *cft;
2424
2425 err = generic_file_open(inode, file);
2426 if (err)
2427 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002429
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002430 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002431 struct cgroup_seqfile_state *state =
2432 kzalloc(sizeof(*state), GFP_USER);
2433 if (!state)
2434 return -ENOMEM;
2435 state->cft = cft;
2436 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2437 file->f_op = &cgroup_seqfile_operations;
2438 err = single_open(file, cgroup_seqfile_show, state);
2439 if (err < 0)
2440 kfree(state);
2441 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 err = cft->open(inode, file);
2443 else
2444 err = 0;
2445
2446 return err;
2447}
2448
2449static int cgroup_file_release(struct inode *inode, struct file *file)
2450{
2451 struct cftype *cft = __d_cft(file->f_dentry);
2452 if (cft->release)
2453 return cft->release(inode, file);
2454 return 0;
2455}
2456
2457/*
2458 * cgroup_rename - Only allow simple rename of directories in place.
2459 */
2460static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2461 struct inode *new_dir, struct dentry *new_dentry)
2462{
Li Zefan65dff752013-03-01 15:01:56 +08002463 int ret;
2464 struct cgroup_name *name, *old_name;
2465 struct cgroup *cgrp;
2466
2467 /*
2468 * It's convinient to use parent dir's i_mutex to protected
2469 * cgrp->name.
2470 */
2471 lockdep_assert_held(&old_dir->i_mutex);
2472
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2474 return -ENOTDIR;
2475 if (new_dentry->d_inode)
2476 return -EEXIST;
2477 if (old_dir != new_dir)
2478 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002479
2480 cgrp = __d_cgrp(old_dentry);
2481
2482 name = cgroup_alloc_name(new_dentry);
2483 if (!name)
2484 return -ENOMEM;
2485
2486 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2487 if (ret) {
2488 kfree(name);
2489 return ret;
2490 }
2491
2492 old_name = cgrp->name;
2493 rcu_assign_pointer(cgrp->name, name);
2494
2495 kfree_rcu(old_name, rcu_head);
2496 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497}
2498
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002499static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2500{
2501 if (S_ISDIR(dentry->d_inode->i_mode))
2502 return &__d_cgrp(dentry)->xattrs;
2503 else
2504 return &__d_cft(dentry)->xattrs;
2505}
2506
2507static inline int xattr_enabled(struct dentry *dentry)
2508{
2509 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002510 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002511}
2512
2513static bool is_valid_xattr(const char *name)
2514{
2515 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2516 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2517 return true;
2518 return false;
2519}
2520
2521static int cgroup_setxattr(struct dentry *dentry, const char *name,
2522 const void *val, size_t size, int flags)
2523{
2524 if (!xattr_enabled(dentry))
2525 return -EOPNOTSUPP;
2526 if (!is_valid_xattr(name))
2527 return -EINVAL;
2528 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2529}
2530
2531static int cgroup_removexattr(struct dentry *dentry, const char *name)
2532{
2533 if (!xattr_enabled(dentry))
2534 return -EOPNOTSUPP;
2535 if (!is_valid_xattr(name))
2536 return -EINVAL;
2537 return simple_xattr_remove(__d_xattrs(dentry), name);
2538}
2539
2540static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2541 void *buf, size_t size)
2542{
2543 if (!xattr_enabled(dentry))
2544 return -EOPNOTSUPP;
2545 if (!is_valid_xattr(name))
2546 return -EINVAL;
2547 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2548}
2549
2550static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2551{
2552 if (!xattr_enabled(dentry))
2553 return -EOPNOTSUPP;
2554 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2555}
2556
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002557static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002558 .read = cgroup_file_read,
2559 .write = cgroup_file_write,
2560 .llseek = generic_file_llseek,
2561 .open = cgroup_file_open,
2562 .release = cgroup_file_release,
2563};
2564
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002565static const struct inode_operations cgroup_file_inode_operations = {
2566 .setxattr = cgroup_setxattr,
2567 .getxattr = cgroup_getxattr,
2568 .listxattr = cgroup_listxattr,
2569 .removexattr = cgroup_removexattr,
2570};
2571
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002572static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002573 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002574 .mkdir = cgroup_mkdir,
2575 .rmdir = cgroup_rmdir,
2576 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002577 .setxattr = cgroup_setxattr,
2578 .getxattr = cgroup_getxattr,
2579 .listxattr = cgroup_listxattr,
2580 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581};
2582
Al Viro00cd8dd2012-06-10 17:13:09 -04002583static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002584{
2585 if (dentry->d_name.len > NAME_MAX)
2586 return ERR_PTR(-ENAMETOOLONG);
2587 d_add(dentry, NULL);
2588 return NULL;
2589}
2590
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002591/*
2592 * Check if a file is a control file
2593 */
2594static inline struct cftype *__file_cft(struct file *file)
2595{
Al Viro496ad9a2013-01-23 17:07:38 -05002596 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002597 return ERR_PTR(-EINVAL);
2598 return __d_cft(file->f_dentry);
2599}
2600
Al Viroa5e7ed32011-07-26 01:55:55 -04002601static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002602 struct super_block *sb)
2603{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002604 struct inode *inode;
2605
2606 if (!dentry)
2607 return -ENOENT;
2608 if (dentry->d_inode)
2609 return -EEXIST;
2610
2611 inode = cgroup_new_inode(mode, sb);
2612 if (!inode)
2613 return -ENOMEM;
2614
2615 if (S_ISDIR(mode)) {
2616 inode->i_op = &cgroup_dir_inode_operations;
2617 inode->i_fop = &simple_dir_operations;
2618
2619 /* start off with i_nlink == 2 (for "." entry) */
2620 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002621 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622
Tejun Heob8a2df62012-11-19 08:13:37 -08002623 /*
2624 * Control reaches here with cgroup_mutex held.
2625 * @inode->i_mutex should nest outside cgroup_mutex but we
2626 * want to populate it immediately without releasing
2627 * cgroup_mutex. As @inode isn't visible to anyone else
2628 * yet, trylock will always succeed without affecting
2629 * lockdep checks.
2630 */
2631 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 } else if (S_ISREG(mode)) {
2633 inode->i_size = 0;
2634 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002635 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637 d_instantiate(dentry, inode);
2638 dget(dentry); /* Extra count - pin the dentry in core */
2639 return 0;
2640}
2641
Li Zefan099fca32009-04-02 16:57:29 -07002642/**
2643 * cgroup_file_mode - deduce file mode of a control file
2644 * @cft: the control file in question
2645 *
2646 * returns cft->mode if ->mode is not 0
2647 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2648 * returns S_IRUGO if it has only a read handler
2649 * returns S_IWUSR if it has only a write hander
2650 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002651static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002652{
Al Viroa5e7ed32011-07-26 01:55:55 -04002653 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002654
2655 if (cft->mode)
2656 return cft->mode;
2657
2658 if (cft->read || cft->read_u64 || cft->read_s64 ||
2659 cft->read_map || cft->read_seq_string)
2660 mode |= S_IRUGO;
2661
2662 if (cft->write || cft->write_u64 || cft->write_s64 ||
2663 cft->write_string || cft->trigger)
2664 mode |= S_IWUSR;
2665
2666 return mode;
2667}
2668
Tejun Heodb0416b2012-04-01 12:09:55 -07002669static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002670 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671{
Paul Menagebd89aab2007-10-18 23:40:44 -07002672 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002673 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002675 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002677 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002679
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002680 simple_xattrs_init(&cft->xattrs);
2681
Tejun Heo93438622013-04-14 20:15:25 -07002682 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 strcpy(name, subsys->name);
2684 strcat(name, ".");
2685 }
2686 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002687
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002689
2690 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2691 if (!cfe)
2692 return -ENOMEM;
2693
Paul Menageddbcc7e2007-10-18 23:39:30 -07002694 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002695 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002697 goto out;
2698 }
2699
2700 mode = cgroup_file_mode(cft);
2701 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2702 if (!error) {
2703 cfe->type = (void *)cft;
2704 cfe->dentry = dentry;
2705 dentry->d_fsdata = cfe;
2706 list_add_tail(&cfe->node, &parent->files);
2707 cfe = NULL;
2708 }
2709 dput(dentry);
2710out:
2711 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 return error;
2713}
2714
Tejun Heo79578622012-04-01 12:09:56 -07002715static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002716 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002718 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002719 int err, ret = 0;
2720
2721 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002722 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002723 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2724 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002725 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2726 continue;
2727 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2728 continue;
2729
Li Zefan2739d3c2013-01-21 18:18:33 +08002730 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002731 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002732 if (err)
2733 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2734 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002735 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002736 } else {
2737 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002738 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002740 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741}
2742
Tejun Heo8e3f6542012-04-01 12:09:55 -07002743static DEFINE_MUTEX(cgroup_cft_mutex);
2744
2745static void cgroup_cfts_prepare(void)
2746 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2747{
2748 /*
2749 * Thanks to the entanglement with vfs inode locking, we can't walk
2750 * the existing cgroups under cgroup_mutex and create files.
2751 * Instead, we increment reference on all cgroups and build list of
2752 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2753 * exclusive access to the field.
2754 */
2755 mutex_lock(&cgroup_cft_mutex);
2756 mutex_lock(&cgroup_mutex);
2757}
2758
2759static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002761 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2762{
2763 LIST_HEAD(pending);
2764 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002765
2766 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2767 if (cfts && ss->root != &rootnode) {
2768 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2769 dget(cgrp->dentry);
2770 list_add_tail(&cgrp->cft_q_node, &pending);
2771 }
2772 }
2773
2774 mutex_unlock(&cgroup_mutex);
2775
2776 /*
2777 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2778 * files for all cgroups which were created before.
2779 */
2780 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2781 struct inode *inode = cgrp->dentry->d_inode;
2782
2783 mutex_lock(&inode->i_mutex);
2784 mutex_lock(&cgroup_mutex);
2785 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002786 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002787 mutex_unlock(&cgroup_mutex);
2788 mutex_unlock(&inode->i_mutex);
2789
2790 list_del_init(&cgrp->cft_q_node);
2791 dput(cgrp->dentry);
2792 }
2793
2794 mutex_unlock(&cgroup_cft_mutex);
2795}
2796
2797/**
2798 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2799 * @ss: target cgroup subsystem
2800 * @cfts: zero-length name terminated array of cftypes
2801 *
2802 * Register @cfts to @ss. Files described by @cfts are created for all
2803 * existing cgroups to which @ss is attached and all future cgroups will
2804 * have them too. This function can be called anytime whether @ss is
2805 * attached or not.
2806 *
2807 * Returns 0 on successful registration, -errno on failure. Note that this
2808 * function currently returns 0 as long as @cfts registration is successful
2809 * even if some file creation attempts on existing cgroups fail.
2810 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002811int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002812{
2813 struct cftype_set *set;
2814
2815 set = kzalloc(sizeof(*set), GFP_KERNEL);
2816 if (!set)
2817 return -ENOMEM;
2818
2819 cgroup_cfts_prepare();
2820 set->cfts = cfts;
2821 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002822 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823
2824 return 0;
2825}
2826EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2827
Li Zefana043e3b2008-02-23 15:24:09 -08002828/**
Tejun Heo79578622012-04-01 12:09:56 -07002829 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2830 * @ss: target cgroup subsystem
2831 * @cfts: zero-length name terminated array of cftypes
2832 *
2833 * Unregister @cfts from @ss. Files described by @cfts are removed from
2834 * all existing cgroups to which @ss is attached and all future cgroups
2835 * won't have them either. This function can be called anytime whether @ss
2836 * is attached or not.
2837 *
2838 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2839 * registered with @ss.
2840 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002841int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002842{
2843 struct cftype_set *set;
2844
2845 cgroup_cfts_prepare();
2846
2847 list_for_each_entry(set, &ss->cftsets, node) {
2848 if (set->cfts == cfts) {
2849 list_del_init(&set->node);
2850 cgroup_cfts_commit(ss, cfts, false);
2851 return 0;
2852 }
2853 }
2854
2855 cgroup_cfts_commit(ss, NULL, false);
2856 return -ENOENT;
2857}
2858
2859/**
Li Zefana043e3b2008-02-23 15:24:09 -08002860 * cgroup_task_count - count the number of tasks in a cgroup.
2861 * @cgrp: the cgroup in question
2862 *
2863 * Return the number of tasks in the cgroup.
2864 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002865int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002866{
2867 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002868 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002869
Paul Menage817929e2007-10-18 23:39:36 -07002870 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002871 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002872 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002873 }
2874 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002875 return count;
2876}
2877
2878/*
Paul Menage817929e2007-10-18 23:39:36 -07002879 * Advance a list_head iterator. The iterator should be positioned at
2880 * the start of a css_set
2881 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002882static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002883 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002884{
2885 struct list_head *l = it->cg_link;
2886 struct cg_cgroup_link *link;
2887 struct css_set *cg;
2888
2889 /* Advance to the next non-empty css_set */
2890 do {
2891 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002892 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002893 it->cg_link = NULL;
2894 return;
2895 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002896 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002897 cg = link->cg;
2898 } while (list_empty(&cg->tasks));
2899 it->cg_link = l;
2900 it->task = cg->tasks.next;
2901}
2902
Cliff Wickman31a7df02008-02-07 00:14:42 -08002903/*
2904 * To reduce the fork() overhead for systems that are not actually
2905 * using their cgroups capability, we don't maintain the lists running
2906 * through each css_set to its tasks until we see the list actually
2907 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002908 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002909static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002910{
2911 struct task_struct *p, *g;
2912 write_lock(&css_set_lock);
2913 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002914 /*
2915 * We need tasklist_lock because RCU is not safe against
2916 * while_each_thread(). Besides, a forking task that has passed
2917 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2918 * is not guaranteed to have its child immediately visible in the
2919 * tasklist if we walk through it with RCU.
2920 */
2921 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002922 do_each_thread(g, p) {
2923 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002924 /*
2925 * We should check if the process is exiting, otherwise
2926 * it will race with cgroup_exit() in that the list
2927 * entry won't be deleted though the process has exited.
2928 */
2929 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002930 list_add(&p->cg_list, &p->cgroups->tasks);
2931 task_unlock(p);
2932 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002933 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002934 write_unlock(&css_set_lock);
2935}
2936
Tejun Heo574bd9f2012-11-09 09:12:29 -08002937/**
2938 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2939 * @pos: the current position (%NULL to initiate traversal)
2940 * @cgroup: cgroup whose descendants to walk
2941 *
2942 * To be used by cgroup_for_each_descendant_pre(). Find the next
2943 * descendant to visit for pre-order traversal of @cgroup's descendants.
2944 */
2945struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2946 struct cgroup *cgroup)
2947{
2948 struct cgroup *next;
2949
2950 WARN_ON_ONCE(!rcu_read_lock_held());
2951
2952 /* if first iteration, pretend we just visited @cgroup */
2953 if (!pos) {
2954 if (list_empty(&cgroup->children))
2955 return NULL;
2956 pos = cgroup;
2957 }
2958
2959 /* visit the first child if exists */
2960 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2961 if (next)
2962 return next;
2963
2964 /* no child, visit my or the closest ancestor's next sibling */
2965 do {
2966 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2967 sibling);
2968 if (&next->sibling != &pos->parent->children)
2969 return next;
2970
2971 pos = pos->parent;
2972 } while (pos != cgroup);
2973
2974 return NULL;
2975}
2976EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2977
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002978/**
2979 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
2980 * @pos: cgroup of interest
2981 *
2982 * Return the rightmost descendant of @pos. If there's no descendant,
2983 * @pos is returned. This can be used during pre-order traversal to skip
2984 * subtree of @pos.
2985 */
2986struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
2987{
2988 struct cgroup *last, *tmp;
2989
2990 WARN_ON_ONCE(!rcu_read_lock_held());
2991
2992 do {
2993 last = pos;
2994 /* ->prev isn't RCU safe, walk ->next till the end */
2995 pos = NULL;
2996 list_for_each_entry_rcu(tmp, &last->children, sibling)
2997 pos = tmp;
2998 } while (pos);
2999
3000 return last;
3001}
3002EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3003
Tejun Heo574bd9f2012-11-09 09:12:29 -08003004static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3005{
3006 struct cgroup *last;
3007
3008 do {
3009 last = pos;
3010 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3011 sibling);
3012 } while (pos);
3013
3014 return last;
3015}
3016
3017/**
3018 * cgroup_next_descendant_post - find the next descendant for post-order walk
3019 * @pos: the current position (%NULL to initiate traversal)
3020 * @cgroup: cgroup whose descendants to walk
3021 *
3022 * To be used by cgroup_for_each_descendant_post(). Find the next
3023 * descendant to visit for post-order traversal of @cgroup's descendants.
3024 */
3025struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3026 struct cgroup *cgroup)
3027{
3028 struct cgroup *next;
3029
3030 WARN_ON_ONCE(!rcu_read_lock_held());
3031
3032 /* if first iteration, visit the leftmost descendant */
3033 if (!pos) {
3034 next = cgroup_leftmost_descendant(cgroup);
3035 return next != cgroup ? next : NULL;
3036 }
3037
3038 /* if there's an unvisited sibling, visit its leftmost descendant */
3039 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3040 if (&next->sibling != &pos->parent->children)
3041 return cgroup_leftmost_descendant(next);
3042
3043 /* no sibling left, visit parent */
3044 next = pos->parent;
3045 return next != cgroup ? next : NULL;
3046}
3047EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3048
Paul Menagebd89aab2007-10-18 23:40:44 -07003049void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003050 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003051{
3052 /*
3053 * The first time anyone tries to iterate across a cgroup,
3054 * we need to enable the list linking each css_set to its
3055 * tasks, and fix up all existing tasks.
3056 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003057 if (!use_task_css_set_links)
3058 cgroup_enable_task_cg_lists();
3059
Paul Menage817929e2007-10-18 23:39:36 -07003060 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003061 it->cg_link = &cgrp->css_sets;
3062 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003063}
3064
Paul Menagebd89aab2007-10-18 23:40:44 -07003065struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003066 struct cgroup_iter *it)
3067{
3068 struct task_struct *res;
3069 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003070 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003071
3072 /* If the iterator cg is NULL, we have no tasks */
3073 if (!it->cg_link)
3074 return NULL;
3075 res = list_entry(l, struct task_struct, cg_list);
3076 /* Advance iterator to find next entry */
3077 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003078 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3079 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003080 /* We reached the end of this task list - move on to
3081 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003082 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003083 } else {
3084 it->task = l;
3085 }
3086 return res;
3087}
3088
Paul Menagebd89aab2007-10-18 23:40:44 -07003089void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003090 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003091{
3092 read_unlock(&css_set_lock);
3093}
3094
Cliff Wickman31a7df02008-02-07 00:14:42 -08003095static inline int started_after_time(struct task_struct *t1,
3096 struct timespec *time,
3097 struct task_struct *t2)
3098{
3099 int start_diff = timespec_compare(&t1->start_time, time);
3100 if (start_diff > 0) {
3101 return 1;
3102 } else if (start_diff < 0) {
3103 return 0;
3104 } else {
3105 /*
3106 * Arbitrarily, if two processes started at the same
3107 * time, we'll say that the lower pointer value
3108 * started first. Note that t2 may have exited by now
3109 * so this may not be a valid pointer any longer, but
3110 * that's fine - it still serves to distinguish
3111 * between two tasks started (effectively) simultaneously.
3112 */
3113 return t1 > t2;
3114 }
3115}
3116
3117/*
3118 * This function is a callback from heap_insert() and is used to order
3119 * the heap.
3120 * In this case we order the heap in descending task start time.
3121 */
3122static inline int started_after(void *p1, void *p2)
3123{
3124 struct task_struct *t1 = p1;
3125 struct task_struct *t2 = p2;
3126 return started_after_time(t1, &t2->start_time, t2);
3127}
3128
3129/**
3130 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3131 * @scan: struct cgroup_scanner containing arguments for the scan
3132 *
3133 * Arguments include pointers to callback functions test_task() and
3134 * process_task().
3135 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3136 * and if it returns true, call process_task() for it also.
3137 * The test_task pointer may be NULL, meaning always true (select all tasks).
3138 * Effectively duplicates cgroup_iter_{start,next,end}()
3139 * but does not lock css_set_lock for the call to process_task().
3140 * The struct cgroup_scanner may be embedded in any structure of the caller's
3141 * creation.
3142 * It is guaranteed that process_task() will act on every task that
3143 * is a member of the cgroup for the duration of this call. This
3144 * function may or may not call process_task() for tasks that exit
3145 * or move to a different cgroup during the call, or are forked or
3146 * move into the cgroup during the call.
3147 *
3148 * Note that test_task() may be called with locks held, and may in some
3149 * situations be called multiple times for the same task, so it should
3150 * be cheap.
3151 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3152 * pre-allocated and will be used for heap operations (and its "gt" member will
3153 * be overwritten), else a temporary heap will be used (allocation of which
3154 * may cause this function to fail).
3155 */
3156int cgroup_scan_tasks(struct cgroup_scanner *scan)
3157{
3158 int retval, i;
3159 struct cgroup_iter it;
3160 struct task_struct *p, *dropped;
3161 /* Never dereference latest_task, since it's not refcounted */
3162 struct task_struct *latest_task = NULL;
3163 struct ptr_heap tmp_heap;
3164 struct ptr_heap *heap;
3165 struct timespec latest_time = { 0, 0 };
3166
3167 if (scan->heap) {
3168 /* The caller supplied our heap and pre-allocated its memory */
3169 heap = scan->heap;
3170 heap->gt = &started_after;
3171 } else {
3172 /* We need to allocate our own heap memory */
3173 heap = &tmp_heap;
3174 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3175 if (retval)
3176 /* cannot allocate the heap */
3177 return retval;
3178 }
3179
3180 again:
3181 /*
3182 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3183 * to determine which are of interest, and using the scanner's
3184 * "process_task" callback to process any of them that need an update.
3185 * Since we don't want to hold any locks during the task updates,
3186 * gather tasks to be processed in a heap structure.
3187 * The heap is sorted by descending task start time.
3188 * If the statically-sized heap fills up, we overflow tasks that
3189 * started later, and in future iterations only consider tasks that
3190 * started after the latest task in the previous pass. This
3191 * guarantees forward progress and that we don't miss any tasks.
3192 */
3193 heap->size = 0;
3194 cgroup_iter_start(scan->cg, &it);
3195 while ((p = cgroup_iter_next(scan->cg, &it))) {
3196 /*
3197 * Only affect tasks that qualify per the caller's callback,
3198 * if he provided one
3199 */
3200 if (scan->test_task && !scan->test_task(p, scan))
3201 continue;
3202 /*
3203 * Only process tasks that started after the last task
3204 * we processed
3205 */
3206 if (!started_after_time(p, &latest_time, latest_task))
3207 continue;
3208 dropped = heap_insert(heap, p);
3209 if (dropped == NULL) {
3210 /*
3211 * The new task was inserted; the heap wasn't
3212 * previously full
3213 */
3214 get_task_struct(p);
3215 } else if (dropped != p) {
3216 /*
3217 * The new task was inserted, and pushed out a
3218 * different task
3219 */
3220 get_task_struct(p);
3221 put_task_struct(dropped);
3222 }
3223 /*
3224 * Else the new task was newer than anything already in
3225 * the heap and wasn't inserted
3226 */
3227 }
3228 cgroup_iter_end(scan->cg, &it);
3229
3230 if (heap->size) {
3231 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003232 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003233 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003234 latest_time = q->start_time;
3235 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003236 }
3237 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003238 scan->process_task(q, scan);
3239 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003240 }
3241 /*
3242 * If we had to process any tasks at all, scan again
3243 * in case some of them were in the middle of forking
3244 * children that didn't get processed.
3245 * Not the most efficient way to do it, but it avoids
3246 * having to take callback_mutex in the fork path
3247 */
3248 goto again;
3249 }
3250 if (heap == &tmp_heap)
3251 heap_free(&tmp_heap);
3252 return 0;
3253}
3254
Tejun Heo8cc99342013-04-07 09:29:50 -07003255static void cgroup_transfer_one_task(struct task_struct *task,
3256 struct cgroup_scanner *scan)
3257{
3258 struct cgroup *new_cgroup = scan->data;
3259
Tejun Heo47cfcd02013-04-07 09:29:51 -07003260 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003261 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003262 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003263}
3264
3265/**
3266 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3267 * @to: cgroup to which the tasks will be moved
3268 * @from: cgroup in which the tasks currently reside
3269 */
3270int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3271{
3272 struct cgroup_scanner scan;
3273
3274 scan.cg = from;
3275 scan.test_task = NULL; /* select all tasks in cgroup */
3276 scan.process_task = cgroup_transfer_one_task;
3277 scan.heap = NULL;
3278 scan.data = to;
3279
3280 return cgroup_scan_tasks(&scan);
3281}
3282
Paul Menage817929e2007-10-18 23:39:36 -07003283/*
Ben Blum102a7752009-09-23 15:56:26 -07003284 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003285 *
3286 * Reading this file can return large amounts of data if a cgroup has
3287 * *lots* of attached tasks. So it may need several calls to read(),
3288 * but we cannot guarantee that the information we produce is correct
3289 * unless we produce it entirely atomically.
3290 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003291 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003292
Li Zefan24528252012-01-20 11:58:43 +08003293/* which pidlist file are we talking about? */
3294enum cgroup_filetype {
3295 CGROUP_FILE_PROCS,
3296 CGROUP_FILE_TASKS,
3297};
3298
3299/*
3300 * A pidlist is a list of pids that virtually represents the contents of one
3301 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3302 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3303 * to the cgroup.
3304 */
3305struct cgroup_pidlist {
3306 /*
3307 * used to find which pidlist is wanted. doesn't change as long as
3308 * this particular list stays in the list.
3309 */
3310 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3311 /* array of xids */
3312 pid_t *list;
3313 /* how many elements the above list has */
3314 int length;
3315 /* how many files are using the current array */
3316 int use_count;
3317 /* each of these stored in a list by its cgroup */
3318 struct list_head links;
3319 /* pointer to the cgroup we belong to, for list removal purposes */
3320 struct cgroup *owner;
3321 /* protects the other fields */
3322 struct rw_semaphore mutex;
3323};
3324
Paul Menagebbcb81d2007-10-18 23:39:32 -07003325/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003326 * The following two functions "fix" the issue where there are more pids
3327 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3328 * TODO: replace with a kernel-wide solution to this problem
3329 */
3330#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3331static void *pidlist_allocate(int count)
3332{
3333 if (PIDLIST_TOO_LARGE(count))
3334 return vmalloc(count * sizeof(pid_t));
3335 else
3336 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3337}
3338static void pidlist_free(void *p)
3339{
3340 if (is_vmalloc_addr(p))
3341 vfree(p);
3342 else
3343 kfree(p);
3344}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003345
3346/*
Ben Blum102a7752009-09-23 15:56:26 -07003347 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003348 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003349 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003350static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003351{
Ben Blum102a7752009-09-23 15:56:26 -07003352 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003353
3354 /*
3355 * we presume the 0th element is unique, so i starts at 1. trivial
3356 * edge cases first; no work needs to be done for either
3357 */
3358 if (length == 0 || length == 1)
3359 return length;
3360 /* src and dest walk down the list; dest counts unique elements */
3361 for (src = 1; src < length; src++) {
3362 /* find next unique element */
3363 while (list[src] == list[src-1]) {
3364 src++;
3365 if (src == length)
3366 goto after;
3367 }
3368 /* dest always points to where the next unique element goes */
3369 list[dest] = list[src];
3370 dest++;
3371 }
3372after:
Ben Blum102a7752009-09-23 15:56:26 -07003373 return dest;
3374}
3375
3376static int cmppid(const void *a, const void *b)
3377{
3378 return *(pid_t *)a - *(pid_t *)b;
3379}
3380
3381/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003382 * find the appropriate pidlist for our purpose (given procs vs tasks)
3383 * returns with the lock on that pidlist already held, and takes care
3384 * of the use count, or returns NULL with no locks held if we're out of
3385 * memory.
3386 */
3387static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3388 enum cgroup_filetype type)
3389{
3390 struct cgroup_pidlist *l;
3391 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003392 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003393
Ben Blum72a8cb32009-09-23 15:56:27 -07003394 /*
3395 * We can't drop the pidlist_mutex before taking the l->mutex in case
3396 * the last ref-holder is trying to remove l from the list at the same
3397 * time. Holding the pidlist_mutex precludes somebody taking whichever
3398 * list we find out from under us - compare release_pid_array().
3399 */
3400 mutex_lock(&cgrp->pidlist_mutex);
3401 list_for_each_entry(l, &cgrp->pidlists, links) {
3402 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003403 /* make sure l doesn't vanish out from under us */
3404 down_write(&l->mutex);
3405 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003406 return l;
3407 }
3408 }
3409 /* entry not found; create a new one */
3410 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3411 if (!l) {
3412 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003413 return l;
3414 }
3415 init_rwsem(&l->mutex);
3416 down_write(&l->mutex);
3417 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003418 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003419 l->use_count = 0; /* don't increment here */
3420 l->list = NULL;
3421 l->owner = cgrp;
3422 list_add(&l->links, &cgrp->pidlists);
3423 mutex_unlock(&cgrp->pidlist_mutex);
3424 return l;
3425}
3426
3427/*
Ben Blum102a7752009-09-23 15:56:26 -07003428 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3429 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003430static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3431 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003432{
3433 pid_t *array;
3434 int length;
3435 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003436 struct cgroup_iter it;
3437 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003438 struct cgroup_pidlist *l;
3439
3440 /*
3441 * If cgroup gets more users after we read count, we won't have
3442 * enough space - tough. This race is indistinguishable to the
3443 * caller from the case that the additional cgroup users didn't
3444 * show up until sometime later on.
3445 */
3446 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003447 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003448 if (!array)
3449 return -ENOMEM;
3450 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003451 cgroup_iter_start(cgrp, &it);
3452 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003453 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003454 break;
Ben Blum102a7752009-09-23 15:56:26 -07003455 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003456 if (type == CGROUP_FILE_PROCS)
3457 pid = task_tgid_vnr(tsk);
3458 else
3459 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003460 if (pid > 0) /* make sure to only use valid results */
3461 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003462 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003463 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003464 length = n;
3465 /* now sort & (if procs) strip out duplicates */
3466 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003467 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003468 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003469 l = cgroup_pidlist_find(cgrp, type);
3470 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003471 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003472 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003473 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003474 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003475 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003476 l->list = array;
3477 l->length = length;
3478 l->use_count++;
3479 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003480 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003481 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003482}
3483
Balbir Singh846c7bb2007-10-18 23:39:44 -07003484/**
Li Zefana043e3b2008-02-23 15:24:09 -08003485 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003486 * @stats: cgroupstats to fill information into
3487 * @dentry: A dentry entry belonging to the cgroup for which stats have
3488 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003489 *
3490 * Build and fill cgroupstats so that taskstats can export it to user
3491 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003492 */
3493int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3494{
3495 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003496 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003497 struct cgroup_iter it;
3498 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003499
Balbir Singh846c7bb2007-10-18 23:39:44 -07003500 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003501 * Validate dentry by checking the superblock operations,
3502 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003503 */
Li Zefan33d283b2008-11-19 15:36:48 -08003504 if (dentry->d_sb->s_op != &cgroup_ops ||
3505 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003506 goto err;
3507
3508 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003509 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003510
Paul Menagebd89aab2007-10-18 23:40:44 -07003511 cgroup_iter_start(cgrp, &it);
3512 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003513 switch (tsk->state) {
3514 case TASK_RUNNING:
3515 stats->nr_running++;
3516 break;
3517 case TASK_INTERRUPTIBLE:
3518 stats->nr_sleeping++;
3519 break;
3520 case TASK_UNINTERRUPTIBLE:
3521 stats->nr_uninterruptible++;
3522 break;
3523 case TASK_STOPPED:
3524 stats->nr_stopped++;
3525 break;
3526 default:
3527 if (delayacct_is_task_waiting_on_io(tsk))
3528 stats->nr_io_wait++;
3529 break;
3530 }
3531 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003532 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003533
Balbir Singh846c7bb2007-10-18 23:39:44 -07003534err:
3535 return ret;
3536}
3537
Paul Menage8f3ff202009-09-23 15:56:25 -07003538
Paul Menagecc31edc2008-10-18 20:28:04 -07003539/*
Ben Blum102a7752009-09-23 15:56:26 -07003540 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003541 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003542 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003543 */
3544
Ben Blum102a7752009-09-23 15:56:26 -07003545static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003546{
3547 /*
3548 * Initially we receive a position value that corresponds to
3549 * one more than the last pid shown (or 0 on the first call or
3550 * after a seek to the start). Use a binary-search to find the
3551 * next pid to display, if any
3552 */
Ben Blum102a7752009-09-23 15:56:26 -07003553 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003554 int index = 0, pid = *pos;
3555 int *iter;
3556
Ben Blum102a7752009-09-23 15:56:26 -07003557 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003558 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003559 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003560
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 while (index < end) {
3562 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003563 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003564 index = mid;
3565 break;
Ben Blum102a7752009-09-23 15:56:26 -07003566 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003567 index = mid + 1;
3568 else
3569 end = mid;
3570 }
3571 }
3572 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003573 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 return NULL;
3575 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003576 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003577 *pos = *iter;
3578 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003579}
3580
Ben Blum102a7752009-09-23 15:56:26 -07003581static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003582{
Ben Blum102a7752009-09-23 15:56:26 -07003583 struct cgroup_pidlist *l = s->private;
3584 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003585}
3586
Ben Blum102a7752009-09-23 15:56:26 -07003587static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003588{
Ben Blum102a7752009-09-23 15:56:26 -07003589 struct cgroup_pidlist *l = s->private;
3590 pid_t *p = v;
3591 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003592 /*
3593 * Advance to the next pid in the array. If this goes off the
3594 * end, we're done
3595 */
3596 p++;
3597 if (p >= end) {
3598 return NULL;
3599 } else {
3600 *pos = *p;
3601 return p;
3602 }
3603}
3604
Ben Blum102a7752009-09-23 15:56:26 -07003605static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003606{
3607 return seq_printf(s, "%d\n", *(int *)v);
3608}
3609
Ben Blum102a7752009-09-23 15:56:26 -07003610/*
3611 * seq_operations functions for iterating on pidlists through seq_file -
3612 * independent of whether it's tasks or procs
3613 */
3614static const struct seq_operations cgroup_pidlist_seq_operations = {
3615 .start = cgroup_pidlist_start,
3616 .stop = cgroup_pidlist_stop,
3617 .next = cgroup_pidlist_next,
3618 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003619};
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003622{
Ben Blum72a8cb32009-09-23 15:56:27 -07003623 /*
3624 * the case where we're the last user of this particular pidlist will
3625 * have us remove it from the cgroup's list, which entails taking the
3626 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3627 * pidlist_mutex, we have to take pidlist_mutex first.
3628 */
3629 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003630 down_write(&l->mutex);
3631 BUG_ON(!l->use_count);
3632 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003633 /* we're the last user if refcount is 0; remove and free */
3634 list_del(&l->links);
3635 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003636 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 put_pid_ns(l->key.ns);
3638 up_write(&l->mutex);
3639 kfree(l);
3640 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003641 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003642 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003643 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003644}
3645
Ben Blum102a7752009-09-23 15:56:26 -07003646static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003647{
Ben Blum102a7752009-09-23 15:56:26 -07003648 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003649 if (!(file->f_mode & FMODE_READ))
3650 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003651 /*
3652 * the seq_file will only be initialized if the file was opened for
3653 * reading; hence we check if it's not null only in that case.
3654 */
3655 l = ((struct seq_file *)file->private_data)->private;
3656 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003657 return seq_release(inode, file);
3658}
3659
Ben Blum102a7752009-09-23 15:56:26 -07003660static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003661 .read = seq_read,
3662 .llseek = seq_lseek,
3663 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003664 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003665};
3666
3667/*
Ben Blum102a7752009-09-23 15:56:26 -07003668 * The following functions handle opens on a file that displays a pidlist
3669 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3670 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003671 */
Ben Blum102a7752009-09-23 15:56:26 -07003672/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003673static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003674{
3675 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003676 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003677 int retval;
3678
3679 /* Nothing to do for write-only files */
3680 if (!(file->f_mode & FMODE_READ))
3681 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003682
Ben Blum102a7752009-09-23 15:56:26 -07003683 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003685 if (retval)
3686 return retval;
3687 /* configure file information */
3688 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003689
Ben Blum102a7752009-09-23 15:56:26 -07003690 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003692 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003693 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694 }
Ben Blum102a7752009-09-23 15:56:26 -07003695 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003696 return 0;
3697}
Ben Blum102a7752009-09-23 15:56:26 -07003698static int cgroup_tasks_open(struct inode *unused, struct file *file)
3699{
Ben Blum72a8cb32009-09-23 15:56:27 -07003700 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003701}
3702static int cgroup_procs_open(struct inode *unused, struct file *file)
3703{
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003705}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706
Paul Menagebd89aab2007-10-18 23:40:44 -07003707static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003708 struct cftype *cft)
3709{
Paul Menagebd89aab2007-10-18 23:40:44 -07003710 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003711}
3712
Paul Menage6379c102008-07-25 01:47:01 -07003713static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3714 struct cftype *cft,
3715 u64 val)
3716{
3717 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3718 if (val)
3719 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3720 else
3721 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3722 return 0;
3723}
3724
Paul Menagebbcb81d2007-10-18 23:39:32 -07003725/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003726 * Unregister event and free resources.
3727 *
3728 * Gets called from workqueue.
3729 */
3730static void cgroup_event_remove(struct work_struct *work)
3731{
3732 struct cgroup_event *event = container_of(work, struct cgroup_event,
3733 remove);
3734 struct cgroup *cgrp = event->cgrp;
3735
Li Zefan810cbee2013-02-18 18:56:14 +08003736 remove_wait_queue(event->wqh, &event->wait);
3737
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003738 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3739
Li Zefan810cbee2013-02-18 18:56:14 +08003740 /* Notify userspace the event is going away. */
3741 eventfd_signal(event->eventfd, 1);
3742
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003743 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003744 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003745 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003746}
3747
3748/*
3749 * Gets called on POLLHUP on eventfd when user closes it.
3750 *
3751 * Called with wqh->lock held and interrupts disabled.
3752 */
3753static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3754 int sync, void *key)
3755{
3756 struct cgroup_event *event = container_of(wait,
3757 struct cgroup_event, wait);
3758 struct cgroup *cgrp = event->cgrp;
3759 unsigned long flags = (unsigned long)key;
3760
3761 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003762 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003763 * If the event has been detached at cgroup removal, we
3764 * can simply return knowing the other side will cleanup
3765 * for us.
3766 *
3767 * We can't race against event freeing since the other
3768 * side will require wqh->lock via remove_wait_queue(),
3769 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003770 */
Li Zefan810cbee2013-02-18 18:56:14 +08003771 spin_lock(&cgrp->event_list_lock);
3772 if (!list_empty(&event->list)) {
3773 list_del_init(&event->list);
3774 /*
3775 * We are in atomic context, but cgroup_event_remove()
3776 * may sleep, so we have to call it in workqueue.
3777 */
3778 schedule_work(&event->remove);
3779 }
3780 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003781 }
3782
3783 return 0;
3784}
3785
3786static void cgroup_event_ptable_queue_proc(struct file *file,
3787 wait_queue_head_t *wqh, poll_table *pt)
3788{
3789 struct cgroup_event *event = container_of(pt,
3790 struct cgroup_event, pt);
3791
3792 event->wqh = wqh;
3793 add_wait_queue(wqh, &event->wait);
3794}
3795
3796/*
3797 * Parse input and register new cgroup event handler.
3798 *
3799 * Input must be in format '<event_fd> <control_fd> <args>'.
3800 * Interpretation of args is defined by control file implementation.
3801 */
3802static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3803 const char *buffer)
3804{
3805 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003806 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003807 unsigned int efd, cfd;
3808 struct file *efile = NULL;
3809 struct file *cfile = NULL;
3810 char *endp;
3811 int ret;
3812
3813 efd = simple_strtoul(buffer, &endp, 10);
3814 if (*endp != ' ')
3815 return -EINVAL;
3816 buffer = endp + 1;
3817
3818 cfd = simple_strtoul(buffer, &endp, 10);
3819 if ((*endp != ' ') && (*endp != '\0'))
3820 return -EINVAL;
3821 buffer = endp + 1;
3822
3823 event = kzalloc(sizeof(*event), GFP_KERNEL);
3824 if (!event)
3825 return -ENOMEM;
3826 event->cgrp = cgrp;
3827 INIT_LIST_HEAD(&event->list);
3828 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3829 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3830 INIT_WORK(&event->remove, cgroup_event_remove);
3831
3832 efile = eventfd_fget(efd);
3833 if (IS_ERR(efile)) {
3834 ret = PTR_ERR(efile);
3835 goto fail;
3836 }
3837
3838 event->eventfd = eventfd_ctx_fileget(efile);
3839 if (IS_ERR(event->eventfd)) {
3840 ret = PTR_ERR(event->eventfd);
3841 goto fail;
3842 }
3843
3844 cfile = fget(cfd);
3845 if (!cfile) {
3846 ret = -EBADF;
3847 goto fail;
3848 }
3849
3850 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003851 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003852 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003853 if (ret < 0)
3854 goto fail;
3855
3856 event->cft = __file_cft(cfile);
3857 if (IS_ERR(event->cft)) {
3858 ret = PTR_ERR(event->cft);
3859 goto fail;
3860 }
3861
Li Zefanf1690072013-02-18 14:13:35 +08003862 /*
3863 * The file to be monitored must be in the same cgroup as
3864 * cgroup.event_control is.
3865 */
3866 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3867 if (cgrp_cfile != cgrp) {
3868 ret = -EINVAL;
3869 goto fail;
3870 }
3871
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003872 if (!event->cft->register_event || !event->cft->unregister_event) {
3873 ret = -EINVAL;
3874 goto fail;
3875 }
3876
3877 ret = event->cft->register_event(cgrp, event->cft,
3878 event->eventfd, buffer);
3879 if (ret)
3880 goto fail;
3881
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003882 /*
3883 * Events should be removed after rmdir of cgroup directory, but before
3884 * destroying subsystem state objects. Let's take reference to cgroup
3885 * directory dentry to do that.
3886 */
3887 dget(cgrp->dentry);
3888
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 spin_lock(&cgrp->event_list_lock);
3890 list_add(&event->list, &cgrp->event_list);
3891 spin_unlock(&cgrp->event_list_lock);
3892
3893 fput(cfile);
3894 fput(efile);
3895
3896 return 0;
3897
3898fail:
3899 if (cfile)
3900 fput(cfile);
3901
3902 if (event && event->eventfd && !IS_ERR(event->eventfd))
3903 eventfd_ctx_put(event->eventfd);
3904
3905 if (!IS_ERR_OR_NULL(efile))
3906 fput(efile);
3907
3908 kfree(event);
3909
3910 return ret;
3911}
3912
Daniel Lezcano97978e62010-10-27 15:33:35 -07003913static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3914 struct cftype *cft)
3915{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003916 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003917}
3918
3919static int cgroup_clone_children_write(struct cgroup *cgrp,
3920 struct cftype *cft,
3921 u64 val)
3922{
3923 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003924 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003925 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003926 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003927 return 0;
3928}
3929
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003930/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003931 * for the common functions, 'private' gives the type of file
3932 */
Ben Blum102a7752009-09-23 15:56:26 -07003933/* for hysterical raisins, we can't put this on the older files */
3934#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003935static struct cftype files[] = {
3936 {
3937 .name = "tasks",
3938 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003939 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003940 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003941 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003942 },
Ben Blum102a7752009-09-23 15:56:26 -07003943 {
3944 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3945 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003946 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003947 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003948 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003949 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003950 {
3951 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003952 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003953 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003954 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 {
3956 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3957 .write_string = cgroup_write_event_control,
3958 .mode = S_IWUGO,
3959 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003960 {
3961 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003962 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003963 .read_u64 = cgroup_clone_children_read,
3964 .write_u64 = cgroup_clone_children_write,
3965 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003966 {
Tejun Heo873fe092013-04-14 20:15:26 -07003967 .name = "cgroup.sane_behavior",
3968 .flags = CFTYPE_ONLY_ON_ROOT,
3969 .read_seq_string = cgroup_sane_behavior_show,
3970 },
3971 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003972 .name = "release_agent",
3973 .flags = CFTYPE_ONLY_ON_ROOT,
3974 .read_seq_string = cgroup_release_agent_show,
3975 .write_string = cgroup_release_agent_write,
3976 .max_write_len = PATH_MAX,
3977 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003978 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003979};
3980
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003981/**
3982 * cgroup_populate_dir - selectively creation of files in a directory
3983 * @cgrp: target cgroup
3984 * @base_files: true if the base files should be added
3985 * @subsys_mask: mask of the subsystem ids whose files should be added
3986 */
3987static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
3988 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003989{
3990 int err;
3991 struct cgroup_subsys *ss;
3992
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003993 if (base_files) {
3994 err = cgroup_addrm_files(cgrp, NULL, files, true);
3995 if (err < 0)
3996 return err;
3997 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07003998
Tejun Heo8e3f6542012-04-01 12:09:55 -07003999 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004000 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004001 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004002 if (!test_bit(ss->subsys_id, &subsys_mask))
4003 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004004
Tejun Heodb0416b2012-04-01 12:09:55 -07004005 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004006 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004007 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004008
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004009 /* This cgroup is ready now */
4010 for_each_subsys(cgrp->root, ss) {
4011 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4012 /*
4013 * Update id->css pointer and make this css visible from
4014 * CSS ID functions. This pointer will be dereferened
4015 * from RCU-read-side without locks.
4016 */
4017 if (css->id)
4018 rcu_assign_pointer(css->id->css, css);
4019 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004020
4021 return 0;
4022}
4023
Tejun Heo48ddbe12012-04-01 12:09:56 -07004024static void css_dput_fn(struct work_struct *work)
4025{
4026 struct cgroup_subsys_state *css =
4027 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004028 struct dentry *dentry = css->cgroup->dentry;
4029 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004030
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004031 atomic_inc(&sb->s_active);
4032 dput(dentry);
4033 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004034}
4035
Paul Menageddbcc7e2007-10-18 23:39:30 -07004036static void init_cgroup_css(struct cgroup_subsys_state *css,
4037 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004038 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039{
Paul Menagebd89aab2007-10-18 23:40:44 -07004040 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004041 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004043 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004044 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004045 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004046 BUG_ON(cgrp->subsys[ss->subsys_id]);
4047 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004048
4049 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004050 * css holds an extra ref to @cgrp->dentry which is put on the last
4051 * css_put(). dput() requires process context, which css_put() may
4052 * be called without. @css->dput_work will be used to invoke
4053 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004054 */
4055 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004056}
4057
Tejun Heob1929db2012-11-19 08:13:38 -08004058/* invoke ->post_create() on a new CSS and mark it online if successful */
4059static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004060{
Tejun Heob1929db2012-11-19 08:13:38 -08004061 int ret = 0;
4062
Tejun Heoa31f2d32012-11-19 08:13:37 -08004063 lockdep_assert_held(&cgroup_mutex);
4064
Tejun Heo92fb9742012-11-19 08:13:38 -08004065 if (ss->css_online)
4066 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004067 if (!ret)
4068 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4069 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004070}
4071
4072/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4073static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4074 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4075{
4076 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4077
4078 lockdep_assert_held(&cgroup_mutex);
4079
4080 if (!(css->flags & CSS_ONLINE))
4081 return;
4082
Li Zefand7eeac12013-03-12 15:35:59 -07004083 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004084 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004085
4086 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4087}
4088
Paul Menageddbcc7e2007-10-18 23:39:30 -07004089/*
Li Zefana043e3b2008-02-23 15:24:09 -08004090 * cgroup_create - create a cgroup
4091 * @parent: cgroup that will be parent of the new cgroup
4092 * @dentry: dentry of the new cgroup
4093 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004094 *
Li Zefana043e3b2008-02-23 15:24:09 -08004095 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004096 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004097static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004098 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004099{
Paul Menagebd89aab2007-10-18 23:40:44 -07004100 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004101 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004102 struct cgroupfs_root *root = parent->root;
4103 int err = 0;
4104 struct cgroup_subsys *ss;
4105 struct super_block *sb = root->sb;
4106
Tejun Heo0a950f62012-11-19 09:02:12 -08004107 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004108 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4109 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110 return -ENOMEM;
4111
Li Zefan65dff752013-03-01 15:01:56 +08004112 name = cgroup_alloc_name(dentry);
4113 if (!name)
4114 goto err_free_cgrp;
4115 rcu_assign_pointer(cgrp->name, name);
4116
Tejun Heo0a950f62012-11-19 09:02:12 -08004117 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4118 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004119 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004120
Tejun Heo976c06b2012-11-05 09:16:59 -08004121 /*
4122 * Only live parents can have children. Note that the liveliness
4123 * check isn't strictly necessary because cgroup_mkdir() and
4124 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4125 * anyway so that locking is contained inside cgroup proper and we
4126 * don't get nasty surprises if we ever grow another caller.
4127 */
4128 if (!cgroup_lock_live_group(parent)) {
4129 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004130 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004131 }
4132
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133 /* Grab a reference on the superblock so the hierarchy doesn't
4134 * get deleted on unmount if there are child cgroups. This
4135 * can be done outside cgroup_mutex, since the sb can't
4136 * disappear while someone has an open control file on the
4137 * fs */
4138 atomic_inc(&sb->s_active);
4139
Paul Menagecc31edc2008-10-18 20:28:04 -07004140 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141
Li Zefanfe1c06c2013-01-24 14:30:22 +08004142 dentry->d_fsdata = cgrp;
4143 cgrp->dentry = dentry;
4144
Paul Menagebd89aab2007-10-18 23:40:44 -07004145 cgrp->parent = parent;
4146 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147
Li Zefanb6abdb02008-03-04 14:28:19 -08004148 if (notify_on_release(parent))
4149 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4150
Tejun Heo2260e7f2012-11-19 08:13:38 -08004151 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4152 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004153
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004155 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004156
Tejun Heo92fb9742012-11-19 08:13:38 -08004157 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 if (IS_ERR(css)) {
4159 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004160 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004162 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004163 if (ss->use_id) {
4164 err = alloc_css_id(ss, parent, cgrp);
4165 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004166 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004167 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004168 }
4169
Tejun Heo4e139af2012-11-19 08:13:36 -08004170 /*
4171 * Create directory. cgroup_create_file() returns with the new
4172 * directory locked on success so that it can be populated without
4173 * dropping cgroup_mutex.
4174 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004175 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004177 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004178 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004179
Tejun Heo4e139af2012-11-19 08:13:36 -08004180 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004181 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4182 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4183 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004184
Tejun Heob1929db2012-11-19 08:13:38 -08004185 /* each css holds a ref to the cgroup's dentry */
4186 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004187 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004188
Li Zefan415cf072013-04-08 14:35:02 +08004189 /* hold a ref to the parent's dentry */
4190 dget(parent->dentry);
4191
Tejun Heob1929db2012-11-19 08:13:38 -08004192 /* creation succeeded, notify subsystems */
4193 for_each_subsys(root, ss) {
4194 err = online_css(ss, cgrp);
4195 if (err)
4196 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004197
4198 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4199 parent->parent) {
4200 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",
4201 current->comm, current->pid, ss->name);
4202 if (!strcmp(ss->name, "memory"))
4203 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4204 ss->warned_broken_hierarchy = true;
4205 }
Tejun Heoa8638032012-11-09 09:12:29 -08004206 }
4207
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004208 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004209 if (err)
4210 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211
4212 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214
4215 return 0;
4216
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004217err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004219 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004220 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 /* Release the reference count that we took on the superblock */
4224 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004225err_free_id:
4226 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004227err_free_name:
4228 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004229err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004230 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004232
4233err_destroy:
4234 cgroup_destroy_locked(cgrp);
4235 mutex_unlock(&cgroup_mutex);
4236 mutex_unlock(&dentry->d_inode->i_mutex);
4237 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238}
4239
Al Viro18bb1db2011-07-26 01:41:39 -04004240static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241{
4242 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4243
4244 /* the vfs holds inode->i_mutex already */
4245 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4246}
4247
Tejun Heo42809dd2012-11-19 08:13:37 -08004248static int cgroup_destroy_locked(struct cgroup *cgrp)
4249 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250{
Tejun Heo42809dd2012-11-19 08:13:37 -08004251 struct dentry *d = cgrp->dentry;
4252 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004253 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004254 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255
Tejun Heo42809dd2012-11-19 08:13:37 -08004256 lockdep_assert_held(&d->d_inode->i_mutex);
4257 lockdep_assert_held(&cgroup_mutex);
4258
4259 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004261
Tejun Heo1a90dd52012-11-05 09:16:59 -08004262 /*
4263 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4264 * removed. This makes future css_tryget() and child creation
4265 * attempts fail thus maintaining the removal conditions verified
4266 * above.
4267 */
Tejun Heoed9577932012-11-05 09:16:58 -08004268 for_each_subsys(cgrp->root, ss) {
4269 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4270
4271 WARN_ON(atomic_read(&css->refcnt) < 0);
4272 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004273 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004274 set_bit(CGRP_REMOVED, &cgrp->flags);
4275
Tejun Heoa31f2d32012-11-19 08:13:37 -08004276 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004277 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004278 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004279
4280 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004281 * Put all the base refs. Each css holds an extra reference to the
4282 * cgroup's dentry and cgroup removal proceeds regardless of css
4283 * refs. On the last put of each css, whenever that may be, the
4284 * extra dentry ref is put so that dentry destruction happens only
4285 * after all css's are released.
4286 */
Tejun Heoe9316082012-11-05 09:16:58 -08004287 for_each_subsys(cgrp->root, ss)
4288 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004290 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004291 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004292 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004293 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004294
Paul Menage999cd8a2009-01-07 18:08:36 -08004295 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004296 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004297 list_del_init(&cgrp->allcg_node);
4298
Tejun Heo42809dd2012-11-19 08:13:37 -08004299 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004300 cgroup_d_remove_dir(d);
4301 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302
Paul Menagebd89aab2007-10-18 23:40:44 -07004303 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004304 check_for_release(parent);
4305
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004306 /*
4307 * Unregister events and notify userspace.
4308 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004309 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004310 */
4311 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004312 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004313 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004314 schedule_work(&event->remove);
4315 }
Li Zefan810cbee2013-02-18 18:56:14 +08004316 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004317
Paul Menageddbcc7e2007-10-18 23:39:30 -07004318 return 0;
4319}
4320
Tejun Heo42809dd2012-11-19 08:13:37 -08004321static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4322{
4323 int ret;
4324
4325 mutex_lock(&cgroup_mutex);
4326 ret = cgroup_destroy_locked(dentry->d_fsdata);
4327 mutex_unlock(&cgroup_mutex);
4328
4329 return ret;
4330}
4331
Tejun Heo8e3f6542012-04-01 12:09:55 -07004332static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4333{
4334 INIT_LIST_HEAD(&ss->cftsets);
4335
4336 /*
4337 * base_cftset is embedded in subsys itself, no need to worry about
4338 * deregistration.
4339 */
4340 if (ss->base_cftypes) {
4341 ss->base_cftset.cfts = ss->base_cftypes;
4342 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4343 }
4344}
4345
Li Zefan06a11922008-04-29 01:00:07 -07004346static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004349
4350 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351
Tejun Heo648bb562012-11-19 08:13:36 -08004352 mutex_lock(&cgroup_mutex);
4353
Tejun Heo8e3f6542012-04-01 12:09:55 -07004354 /* init base cftset */
4355 cgroup_init_cftsets(ss);
4356
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004358 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004360 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361 /* We don't handle early failures gracefully */
4362 BUG_ON(IS_ERR(css));
4363 init_cgroup_css(css, ss, dummytop);
4364
Li Zefane8d55fd2008-04-29 01:00:13 -07004365 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004366 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004367 * newly registered, all tasks and hence the
4368 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004369 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
4371 need_forkexit_callback |= ss->fork || ss->exit;
4372
Li Zefane8d55fd2008-04-29 01:00:13 -07004373 /* At system boot, before all subsystems have been
4374 * registered, no tasks have been forked, so we don't
4375 * need to invoke fork callbacks here. */
4376 BUG_ON(!list_empty(&init_task.tasks));
4377
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004379 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004380
Tejun Heo648bb562012-11-19 08:13:36 -08004381 mutex_unlock(&cgroup_mutex);
4382
Ben Blume6a11052010-03-10 15:22:09 -08004383 /* this function shouldn't be used with modular subsystems, since they
4384 * need to register a subsys_id, among other things */
4385 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386}
4387
4388/**
Ben Blume6a11052010-03-10 15:22:09 -08004389 * cgroup_load_subsys: load and register a modular subsystem at runtime
4390 * @ss: the subsystem to load
4391 *
4392 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004393 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004394 * up for use. If the subsystem is built-in anyway, work is delegated to the
4395 * simpler cgroup_init_subsys.
4396 */
4397int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4398{
Ben Blume6a11052010-03-10 15:22:09 -08004399 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004400 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004401 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004402 struct css_set *cg;
4403 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004404
4405 /* check name and function validity */
4406 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004407 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004408 return -EINVAL;
4409
4410 /*
4411 * we don't support callbacks in modular subsystems. this check is
4412 * before the ss->module check for consistency; a subsystem that could
4413 * be a module should still have no callbacks even if the user isn't
4414 * compiling it as one.
4415 */
4416 if (ss->fork || ss->exit)
4417 return -EINVAL;
4418
4419 /*
4420 * an optionally modular subsystem is built-in: we want to do nothing,
4421 * since cgroup_init_subsys will have already taken care of it.
4422 */
4423 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004424 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004425 BUG_ON(subsys[ss->subsys_id] != ss);
4426 return 0;
4427 }
4428
Tejun Heo8e3f6542012-04-01 12:09:55 -07004429 /* init base cftset */
4430 cgroup_init_cftsets(ss);
4431
Ben Blume6a11052010-03-10 15:22:09 -08004432 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004433 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004434
4435 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004436 * no ss->css_alloc seems to need anything important in the ss
4437 * struct, so this can happen first (i.e. before the rootnode
4438 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004439 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004440 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004441 if (IS_ERR(css)) {
4442 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004443 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004444 mutex_unlock(&cgroup_mutex);
4445 return PTR_ERR(css);
4446 }
4447
4448 list_add(&ss->sibling, &rootnode.subsys_list);
4449 ss->root = &rootnode;
4450
4451 /* our new subsystem will be attached to the dummy hierarchy. */
4452 init_cgroup_css(css, ss, dummytop);
4453 /* init_idr must be after init_cgroup_css because it sets css->id. */
4454 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004455 ret = cgroup_init_idr(ss, css);
4456 if (ret)
4457 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004458 }
4459
4460 /*
4461 * Now we need to entangle the css into the existing css_sets. unlike
4462 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4463 * will need a new pointer to it; done by iterating the css_set_table.
4464 * furthermore, modifying the existing css_sets will corrupt the hash
4465 * table state, so each changed css_set will need its hash recomputed.
4466 * this is all done under the css_set_lock.
4467 */
4468 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004469 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004470 /* skip entries that we already rehashed */
4471 if (cg->subsys[ss->subsys_id])
4472 continue;
4473 /* remove existing entry */
4474 hash_del(&cg->hlist);
4475 /* set new value */
4476 cg->subsys[ss->subsys_id] = css;
4477 /* recompute hash and restore entry */
4478 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004479 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004480 }
4481 write_unlock(&css_set_lock);
4482
Ben Blume6a11052010-03-10 15:22:09 -08004483 ss->active = 1;
Tejun Heob1929db2012-11-19 08:13:38 -08004484 ret = online_css(ss, dummytop);
4485 if (ret)
4486 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004487
Ben Blume6a11052010-03-10 15:22:09 -08004488 /* success! */
4489 mutex_unlock(&cgroup_mutex);
4490 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004491
4492err_unload:
4493 mutex_unlock(&cgroup_mutex);
4494 /* @ss can't be mounted here as try_module_get() would fail */
4495 cgroup_unload_subsys(ss);
4496 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004497}
4498EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4499
4500/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004501 * cgroup_unload_subsys: unload a modular subsystem
4502 * @ss: the subsystem to unload
4503 *
4504 * This function should be called in a modular subsystem's exitcall. When this
4505 * function is invoked, the refcount on the subsystem's module will be 0, so
4506 * the subsystem will not be attached to any hierarchy.
4507 */
4508void cgroup_unload_subsys(struct cgroup_subsys *ss)
4509{
4510 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004511
4512 BUG_ON(ss->module == NULL);
4513
4514 /*
4515 * we shouldn't be called if the subsystem is in use, and the use of
4516 * try_module_get in parse_cgroupfs_options should ensure that it
4517 * doesn't start being used while we're killing it off.
4518 */
4519 BUG_ON(ss->root != &rootnode);
4520
4521 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004522
Tejun Heoa31f2d32012-11-19 08:13:37 -08004523 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004524 ss->active = 0;
4525
Tejun Heoc897ff62013-02-27 17:03:49 -08004526 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004527 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004528
Ben Blumcf5d5942010-03-10 15:22:09 -08004529 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004530 subsys[ss->subsys_id] = NULL;
4531
4532 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004533 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004534
4535 /*
4536 * disentangle the css from all css_sets attached to the dummytop. as
4537 * in loading, we need to pay our respects to the hashtable gods.
4538 */
4539 write_lock(&css_set_lock);
4540 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4541 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004542 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004543
Li Zefan0ac801f2013-01-10 11:49:27 +08004544 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004545 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004546 key = css_set_hash(cg->subsys);
4547 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004548 }
4549 write_unlock(&css_set_lock);
4550
4551 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004552 * remove subsystem's css from the dummytop and free it - need to
4553 * free before marking as null because ss->css_free needs the
4554 * cgrp->subsys pointer to find their state. note that this also
4555 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004556 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004557 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004558 dummytop->subsys[ss->subsys_id] = NULL;
4559
4560 mutex_unlock(&cgroup_mutex);
4561}
4562EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4563
4564/**
Li Zefana043e3b2008-02-23 15:24:09 -08004565 * cgroup_init_early - cgroup initialization at system boot
4566 *
4567 * Initialize cgroups at system boot, and initialize any
4568 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569 */
4570int __init cgroup_init_early(void)
4571{
4572 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004573 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004574 INIT_LIST_HEAD(&init_css_set.cg_links);
4575 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004576 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004577 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004579 root_count = 1;
4580 init_task.cgroups = &init_css_set;
4581
4582 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004583 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004584 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004585 &rootnode.top_cgroup.css_sets);
4586 list_add(&init_css_set_link.cg_link_list,
4587 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004588
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004589 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004590 struct cgroup_subsys *ss = subsys[i];
4591
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004592 /* at bootup time, we don't worry about modular subsystems */
4593 if (!ss || ss->module)
4594 continue;
4595
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596 BUG_ON(!ss->name);
4597 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004598 BUG_ON(!ss->css_alloc);
4599 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004600 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004601 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602 ss->name, ss->subsys_id);
4603 BUG();
4604 }
4605
4606 if (ss->early_init)
4607 cgroup_init_subsys(ss);
4608 }
4609 return 0;
4610}
4611
4612/**
Li Zefana043e3b2008-02-23 15:24:09 -08004613 * cgroup_init - cgroup initialization
4614 *
4615 * Register cgroup filesystem and /proc file, and initialize
4616 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 */
4618int __init cgroup_init(void)
4619{
4620 int err;
4621 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004622 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004623
4624 err = bdi_init(&cgroup_backing_dev_info);
4625 if (err)
4626 return err;
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];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004630
4631 /* at bootup time, we don't worry about modular subsystems */
4632 if (!ss || ss->module)
4633 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634 if (!ss->early_init)
4635 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004636 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004637 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638 }
4639
Li Zefan472b1052008-04-29 01:00:11 -07004640 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004641 key = css_set_hash(init_css_set.subsys);
4642 hash_add(css_set_table, &init_css_set.hlist, key);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004643 BUG_ON(!init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004644
4645 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4646 if (!cgroup_kobj) {
4647 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004648 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004649 }
4650
4651 err = register_filesystem(&cgroup_fs_type);
4652 if (err < 0) {
4653 kobject_put(cgroup_kobj);
4654 goto out;
4655 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004656
Li Zefan46ae2202008-04-29 01:00:08 -07004657 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004658
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659out:
Paul Menagea4243162007-10-18 23:39:35 -07004660 if (err)
4661 bdi_destroy(&cgroup_backing_dev_info);
4662
Paul Menageddbcc7e2007-10-18 23:39:30 -07004663 return err;
4664}
Paul Menageb4f48b62007-10-18 23:39:33 -07004665
Paul Menagea4243162007-10-18 23:39:35 -07004666/*
4667 * proc_cgroup_show()
4668 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4669 * - Used for /proc/<pid>/cgroup.
4670 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4671 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004672 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004673 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4674 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4675 * cgroup to top_cgroup.
4676 */
4677
4678/* TODO: Use a proper seq_file iterator */
4679static int proc_cgroup_show(struct seq_file *m, void *v)
4680{
4681 struct pid *pid;
4682 struct task_struct *tsk;
4683 char *buf;
4684 int retval;
4685 struct cgroupfs_root *root;
4686
4687 retval = -ENOMEM;
4688 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4689 if (!buf)
4690 goto out;
4691
4692 retval = -ESRCH;
4693 pid = m->private;
4694 tsk = get_pid_task(pid, PIDTYPE_PID);
4695 if (!tsk)
4696 goto out_free;
4697
4698 retval = 0;
4699
4700 mutex_lock(&cgroup_mutex);
4701
Li Zefane5f6a862009-01-07 18:07:41 -08004702 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004703 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004704 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004705 int count = 0;
4706
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004707 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004708 for_each_subsys(root, ss)
4709 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004710 if (strlen(root->name))
4711 seq_printf(m, "%sname=%s", count ? "," : "",
4712 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004713 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004714 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004715 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004716 if (retval < 0)
4717 goto out_unlock;
4718 seq_puts(m, buf);
4719 seq_putc(m, '\n');
4720 }
4721
4722out_unlock:
4723 mutex_unlock(&cgroup_mutex);
4724 put_task_struct(tsk);
4725out_free:
4726 kfree(buf);
4727out:
4728 return retval;
4729}
4730
4731static int cgroup_open(struct inode *inode, struct file *file)
4732{
4733 struct pid *pid = PROC_I(inode)->pid;
4734 return single_open(file, proc_cgroup_show, pid);
4735}
4736
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004737const struct file_operations proc_cgroup_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004738 .open = cgroup_open,
4739 .read = seq_read,
4740 .llseek = seq_lseek,
4741 .release = single_release,
4742};
4743
4744/* Display information about each subsystem and each hierarchy */
4745static int proc_cgroupstats_show(struct seq_file *m, void *v)
4746{
4747 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004748
Paul Menage8bab8dd2008-04-04 14:29:57 -07004749 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004750 /*
4751 * ideally we don't want subsystems moving around while we do this.
4752 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4753 * subsys/hierarchy state.
4754 */
Paul Menagea4243162007-10-18 23:39:35 -07004755 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004756 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4757 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004758 if (ss == NULL)
4759 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004760 seq_printf(m, "%s\t%d\t%d\t%d\n",
4761 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004762 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004763 }
4764 mutex_unlock(&cgroup_mutex);
4765 return 0;
4766}
4767
4768static int cgroupstats_open(struct inode *inode, struct file *file)
4769{
Al Viro9dce07f2008-03-29 03:07:28 +00004770 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004771}
4772
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004773static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004774 .open = cgroupstats_open,
4775 .read = seq_read,
4776 .llseek = seq_lseek,
4777 .release = single_release,
4778};
4779
Paul Menageb4f48b62007-10-18 23:39:33 -07004780/**
4781 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004782 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004783 *
4784 * Description: A task inherits its parent's cgroup at fork().
4785 *
4786 * A pointer to the shared css_set was automatically copied in
4787 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004788 * it was not made under the protection of RCU or cgroup_mutex, so
4789 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4790 * have already changed current->cgroups, allowing the previously
4791 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004792 *
4793 * At the point that cgroup_fork() is called, 'current' is the parent
4794 * task, and the passed argument 'child' points to the child task.
4795 */
4796void cgroup_fork(struct task_struct *child)
4797{
Tejun Heo9bb71302012-10-18 17:52:07 -07004798 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004799 child->cgroups = current->cgroups;
4800 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004801 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004802 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004803}
4804
4805/**
Li Zefana043e3b2008-02-23 15:24:09 -08004806 * cgroup_post_fork - called on a new task after adding it to the task list
4807 * @child: the task in question
4808 *
Tejun Heo5edee612012-10-16 15:03:14 -07004809 * Adds the task to the list running through its css_set if necessary and
4810 * call the subsystem fork() callbacks. Has to be after the task is
4811 * visible on the task list in case we race with the first call to
4812 * cgroup_iter_start() - to guarantee that the new task ends up on its
4813 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004814 */
Paul Menage817929e2007-10-18 23:39:36 -07004815void cgroup_post_fork(struct task_struct *child)
4816{
Tejun Heo5edee612012-10-16 15:03:14 -07004817 int i;
4818
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004819 /*
4820 * use_task_css_set_links is set to 1 before we walk the tasklist
4821 * under the tasklist_lock and we read it here after we added the child
4822 * to the tasklist under the tasklist_lock as well. If the child wasn't
4823 * yet in the tasklist when we walked through it from
4824 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4825 * should be visible now due to the paired locking and barriers implied
4826 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4827 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4828 * lock on fork.
4829 */
Paul Menage817929e2007-10-18 23:39:36 -07004830 if (use_task_css_set_links) {
4831 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004832 task_lock(child);
4833 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004834 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004835 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004836 write_unlock(&css_set_lock);
4837 }
Tejun Heo5edee612012-10-16 15:03:14 -07004838
4839 /*
4840 * Call ss->fork(). This must happen after @child is linked on
4841 * css_set; otherwise, @child might change state between ->fork()
4842 * and addition to css_set.
4843 */
4844 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004845 /*
4846 * fork/exit callbacks are supported only for builtin
4847 * subsystems, and the builtin section of the subsys
4848 * array is immutable, so we don't need to lock the
4849 * subsys array here. On the other hand, modular section
4850 * of the array can be freed at module unload, so we
4851 * can't touch that.
4852 */
4853 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004854 struct cgroup_subsys *ss = subsys[i];
4855
Tejun Heo5edee612012-10-16 15:03:14 -07004856 if (ss->fork)
4857 ss->fork(child);
4858 }
4859 }
Paul Menage817929e2007-10-18 23:39:36 -07004860}
Tejun Heo5edee612012-10-16 15:03:14 -07004861
Paul Menage817929e2007-10-18 23:39:36 -07004862/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004863 * cgroup_exit - detach cgroup from exiting task
4864 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004865 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004866 *
4867 * Description: Detach cgroup from @tsk and release it.
4868 *
4869 * Note that cgroups marked notify_on_release force every task in
4870 * them to take the global cgroup_mutex mutex when exiting.
4871 * This could impact scaling on very large systems. Be reluctant to
4872 * use notify_on_release cgroups where very high task exit scaling
4873 * is required on large systems.
4874 *
4875 * the_top_cgroup_hack:
4876 *
4877 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4878 *
4879 * We call cgroup_exit() while the task is still competent to
4880 * handle notify_on_release(), then leave the task attached to the
4881 * root cgroup in each hierarchy for the remainder of its exit.
4882 *
4883 * To do this properly, we would increment the reference count on
4884 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4885 * code we would add a second cgroup function call, to drop that
4886 * reference. This would just create an unnecessary hot spot on
4887 * the top_cgroup reference count, to no avail.
4888 *
4889 * Normally, holding a reference to a cgroup without bumping its
4890 * count is unsafe. The cgroup could go away, or someone could
4891 * attach us to a different cgroup, decrementing the count on
4892 * the first cgroup that we never incremented. But in this case,
4893 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004894 * which wards off any cgroup_attach_task() attempts, or task is a failed
4895 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004896 */
4897void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4898{
Paul Menage817929e2007-10-18 23:39:36 -07004899 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004900 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004901
4902 /*
4903 * Unlink from the css_set task list if necessary.
4904 * Optimistically check cg_list before taking
4905 * css_set_lock
4906 */
4907 if (!list_empty(&tsk->cg_list)) {
4908 write_lock(&css_set_lock);
4909 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004910 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004911 write_unlock(&css_set_lock);
4912 }
4913
Paul Menageb4f48b62007-10-18 23:39:33 -07004914 /* Reassign the task to the init_css_set. */
4915 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004916 cg = tsk->cgroups;
4917 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004918
4919 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004920 /*
4921 * fork/exit callbacks are supported only for builtin
4922 * subsystems, see cgroup_post_fork() for details.
4923 */
4924 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004925 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004926
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004927 if (ss->exit) {
4928 struct cgroup *old_cgrp =
4929 rcu_dereference_raw(cg->subsys[i])->cgroup;
4930 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004931 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004932 }
4933 }
4934 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004935 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004936
Li Zefanb5d646f2013-01-24 14:43:51 +08004937 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004938}
Paul Menage697f4162007-10-18 23:39:34 -07004939
Paul Menagebd89aab2007-10-18 23:40:44 -07004940static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004941{
4942 /* All of these checks rely on RCU to keep the cgroup
4943 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004944 if (cgroup_is_releasable(cgrp) &&
4945 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4946 /*
4947 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004948 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004949 * it now
4950 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004951 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004952
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004953 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004954 if (!cgroup_is_removed(cgrp) &&
4955 list_empty(&cgrp->release_list)) {
4956 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004957 need_schedule_work = 1;
4958 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004959 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004960 if (need_schedule_work)
4961 schedule_work(&release_agent_work);
4962 }
4963}
4964
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004965/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004966bool __css_tryget(struct cgroup_subsys_state *css)
4967{
Tejun Heoe9316082012-11-05 09:16:58 -08004968 while (true) {
4969 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004970
Tejun Heoe9316082012-11-05 09:16:58 -08004971 v = css_refcnt(css);
4972 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4973 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004974 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004975 else if (t < 0)
4976 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004977 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004978 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004979}
4980EXPORT_SYMBOL_GPL(__css_tryget);
4981
4982/* Caller must verify that the css is not for root cgroup */
4983void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004984{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004985 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004986
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004987 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08004988 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08004989 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004990}
Ben Blum67523c42010-03-10 15:22:11 -08004991EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004992
4993/*
4994 * Notify userspace when a cgroup is released, by running the
4995 * configured release agent with the name of the cgroup (path
4996 * relative to the root of cgroup file system) as the argument.
4997 *
4998 * Most likely, this user command will try to rmdir this cgroup.
4999 *
5000 * This races with the possibility that some other task will be
5001 * attached to this cgroup before it is removed, or that some other
5002 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5003 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5004 * unused, and this cgroup will be reprieved from its death sentence,
5005 * to continue to serve a useful existence. Next time it's released,
5006 * we will get notified again, if it still has 'notify_on_release' set.
5007 *
5008 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5009 * means only wait until the task is successfully execve()'d. The
5010 * separate release agent task is forked by call_usermodehelper(),
5011 * then control in this thread returns here, without waiting for the
5012 * release agent task. We don't bother to wait because the caller of
5013 * this routine has no use for the exit status of the release agent
5014 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005015 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005016static void cgroup_release_agent(struct work_struct *work)
5017{
5018 BUG_ON(work != &release_agent_work);
5019 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005020 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005021 while (!list_empty(&release_list)) {
5022 char *argv[3], *envp[3];
5023 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005024 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005025 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005026 struct cgroup,
5027 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005028 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005029 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005030 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005031 if (!pathbuf)
5032 goto continue_free;
5033 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5034 goto continue_free;
5035 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5036 if (!agentbuf)
5037 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005038
5039 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005040 argv[i++] = agentbuf;
5041 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005042 argv[i] = NULL;
5043
5044 i = 0;
5045 /* minimal command environment */
5046 envp[i++] = "HOME=/";
5047 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5048 envp[i] = NULL;
5049
5050 /* Drop the lock while we invoke the usermode helper,
5051 * since the exec could involve hitting disk and hence
5052 * be a slow process */
5053 mutex_unlock(&cgroup_mutex);
5054 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005055 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005056 continue_free:
5057 kfree(pathbuf);
5058 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005059 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005061 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062 mutex_unlock(&cgroup_mutex);
5063}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005064
5065static int __init cgroup_disable(char *str)
5066{
5067 int i;
5068 char *token;
5069
5070 while ((token = strsep(&str, ",")) != NULL) {
5071 if (!*token)
5072 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005073 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005074 struct cgroup_subsys *ss = subsys[i];
5075
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005076 /*
5077 * cgroup_disable, being at boot time, can't
5078 * know about module subsystems, so we don't
5079 * worry about them.
5080 */
5081 if (!ss || ss->module)
5082 continue;
5083
Paul Menage8bab8dd2008-04-04 14:29:57 -07005084 if (!strcmp(token, ss->name)) {
5085 ss->disabled = 1;
5086 printk(KERN_INFO "Disabling %s control group"
5087 " subsystem\n", ss->name);
5088 break;
5089 }
5090 }
5091 }
5092 return 1;
5093}
5094__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005095
5096/*
5097 * Functons for CSS ID.
5098 */
5099
5100/*
5101 *To get ID other than 0, this should be called when !cgroup_is_removed().
5102 */
5103unsigned short css_id(struct cgroup_subsys_state *css)
5104{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005105 struct css_id *cssid;
5106
5107 /*
5108 * This css_id() can return correct value when somone has refcnt
5109 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5110 * it's unchanged until freed.
5111 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005112 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005113
5114 if (cssid)
5115 return cssid->id;
5116 return 0;
5117}
Ben Blum67523c42010-03-10 15:22:11 -08005118EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005119
5120unsigned short css_depth(struct cgroup_subsys_state *css)
5121{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005122 struct css_id *cssid;
5123
Tejun Heo28b4c272012-04-01 12:09:56 -07005124 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005125
5126 if (cssid)
5127 return cssid->depth;
5128 return 0;
5129}
Ben Blum67523c42010-03-10 15:22:11 -08005130EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005131
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005132/**
5133 * css_is_ancestor - test "root" css is an ancestor of "child"
5134 * @child: the css to be tested.
5135 * @root: the css supporsed to be an ancestor of the child.
5136 *
5137 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005138 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005139 * But, considering usual usage, the csses should be valid objects after test.
5140 * Assuming that the caller will do some action to the child if this returns
5141 * returns true, the caller must take "child";s reference count.
5142 * If "child" is valid object and this returns true, "root" is valid, too.
5143 */
5144
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005145bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005146 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005147{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005148 struct css_id *child_id;
5149 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005150
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005151 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005152 if (!child_id)
5153 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005154 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005155 if (!root_id)
5156 return false;
5157 if (child_id->depth < root_id->depth)
5158 return false;
5159 if (child_id->stack[root_id->depth] != root_id->id)
5160 return false;
5161 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005162}
5163
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005164void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5165{
5166 struct css_id *id = css->id;
5167 /* When this is called before css_id initialization, id can be NULL */
5168 if (!id)
5169 return;
5170
5171 BUG_ON(!ss->use_id);
5172
5173 rcu_assign_pointer(id->css, NULL);
5174 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005175 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005176 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005177 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005178 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005179}
Ben Blum67523c42010-03-10 15:22:11 -08005180EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005181
5182/*
5183 * This is called by init or create(). Then, calls to this function are
5184 * always serialized (By cgroup_mutex() at create()).
5185 */
5186
5187static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5188{
5189 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005190 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005191
5192 BUG_ON(!ss->use_id);
5193
5194 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5195 newid = kzalloc(size, GFP_KERNEL);
5196 if (!newid)
5197 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005198
5199 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005200 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005201 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005202 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005203 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005204 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005205
5206 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005207 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005208 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005209
Tejun Heod228d9e2013-02-27 17:04:54 -08005210 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005211 newid->depth = depth;
5212 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005213err_out:
5214 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005215 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005216
5217}
5218
Ben Blume6a11052010-03-10 15:22:09 -08005219static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5220 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005221{
5222 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005223
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005224 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225 idr_init(&ss->idr);
5226
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005227 newid = get_new_cssid(ss, 0);
5228 if (IS_ERR(newid))
5229 return PTR_ERR(newid);
5230
5231 newid->stack[0] = newid->id;
5232 newid->css = rootcss;
5233 rootcss->id = newid;
5234 return 0;
5235}
5236
5237static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5238 struct cgroup *child)
5239{
5240 int subsys_id, i, depth = 0;
5241 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005242 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243
5244 subsys_id = ss->subsys_id;
5245 parent_css = parent->subsys[subsys_id];
5246 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005247 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005248 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005249
5250 child_id = get_new_cssid(ss, depth);
5251 if (IS_ERR(child_id))
5252 return PTR_ERR(child_id);
5253
5254 for (i = 0; i < depth; i++)
5255 child_id->stack[i] = parent_id->stack[i];
5256 child_id->stack[depth] = child_id->id;
5257 /*
5258 * child_id->css pointer will be set after this cgroup is available
5259 * see cgroup_populate_dir()
5260 */
5261 rcu_assign_pointer(child_css->id, child_id);
5262
5263 return 0;
5264}
5265
5266/**
5267 * css_lookup - lookup css by id
5268 * @ss: cgroup subsys to be looked into.
5269 * @id: the id
5270 *
5271 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5272 * NULL if not. Should be called under rcu_read_lock()
5273 */
5274struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5275{
5276 struct css_id *cssid = NULL;
5277
5278 BUG_ON(!ss->use_id);
5279 cssid = idr_find(&ss->idr, id);
5280
5281 if (unlikely(!cssid))
5282 return NULL;
5283
5284 return rcu_dereference(cssid->css);
5285}
Ben Blum67523c42010-03-10 15:22:11 -08005286EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005287
5288/**
5289 * css_get_next - lookup next cgroup under specified hierarchy.
5290 * @ss: pointer to subsystem
5291 * @id: current position of iteration.
5292 * @root: pointer to css. search tree under this.
5293 * @foundid: position of found object.
5294 *
5295 * Search next css under the specified hierarchy of rootid. Calling under
5296 * rcu_read_lock() is necessary. Returns NULL if it reaches the end.
5297 */
5298struct cgroup_subsys_state *
5299css_get_next(struct cgroup_subsys *ss, int id,
5300 struct cgroup_subsys_state *root, int *foundid)
5301{
5302 struct cgroup_subsys_state *ret = NULL;
5303 struct css_id *tmp;
5304 int tmpid;
5305 int rootid = css_id(root);
5306 int depth = css_depth(root);
5307
5308 if (!rootid)
5309 return NULL;
5310
5311 BUG_ON(!ss->use_id);
Hugh Dickinsca464d62012-03-21 16:34:21 -07005312 WARN_ON_ONCE(!rcu_read_lock_held());
5313
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314 /* fill start point for scan */
5315 tmpid = id;
5316 while (1) {
5317 /*
5318 * scan next entry from bitmap(tree), tmpid is updated after
5319 * idr_get_next().
5320 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321 tmp = idr_get_next(&ss->idr, &tmpid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322 if (!tmp)
5323 break;
5324 if (tmp->depth >= depth && tmp->stack[depth] == rootid) {
5325 ret = rcu_dereference(tmp->css);
5326 if (ret) {
5327 *foundid = tmpid;
5328 break;
5329 }
5330 }
5331 /* continue to scan from next id */
5332 tmpid = tmpid + 1;
5333 }
5334 return ret;
5335}
5336
Stephane Eraniane5d13672011-02-14 11:20:01 +02005337/*
5338 * get corresponding css from file open on cgroupfs directory
5339 */
5340struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5341{
5342 struct cgroup *cgrp;
5343 struct inode *inode;
5344 struct cgroup_subsys_state *css;
5345
Al Viro496ad9a2013-01-23 17:07:38 -05005346 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005347 /* check in cgroup filesystem dir */
5348 if (inode->i_op != &cgroup_dir_inode_operations)
5349 return ERR_PTR(-EBADF);
5350
5351 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5352 return ERR_PTR(-EINVAL);
5353
5354 /* get cgroup */
5355 cgrp = __d_cgrp(f->f_dentry);
5356 css = cgrp->subsys[id];
5357 return css ? css : ERR_PTR(-ENOENT);
5358}
5359
Paul Menagefe693432009-09-23 15:56:20 -07005360#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005361static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005362{
5363 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5364
5365 if (!css)
5366 return ERR_PTR(-ENOMEM);
5367
5368 return css;
5369}
5370
Tejun Heo92fb9742012-11-19 08:13:38 -08005371static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005372{
5373 kfree(cont->subsys[debug_subsys_id]);
5374}
5375
5376static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5377{
5378 return atomic_read(&cont->count);
5379}
5380
5381static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5382{
5383 return cgroup_task_count(cont);
5384}
5385
5386static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5387{
5388 return (u64)(unsigned long)current->cgroups;
5389}
5390
5391static u64 current_css_set_refcount_read(struct cgroup *cont,
5392 struct cftype *cft)
5393{
5394 u64 count;
5395
5396 rcu_read_lock();
5397 count = atomic_read(&current->cgroups->refcount);
5398 rcu_read_unlock();
5399 return count;
5400}
5401
Paul Menage7717f7b2009-09-23 15:56:22 -07005402static int current_css_set_cg_links_read(struct cgroup *cont,
5403 struct cftype *cft,
5404 struct seq_file *seq)
5405{
5406 struct cg_cgroup_link *link;
5407 struct css_set *cg;
5408
5409 read_lock(&css_set_lock);
5410 rcu_read_lock();
5411 cg = rcu_dereference(current->cgroups);
5412 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5413 struct cgroup *c = link->cgrp;
5414 const char *name;
5415
5416 if (c->dentry)
5417 name = c->dentry->d_name.name;
5418 else
5419 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005420 seq_printf(seq, "Root %d group %s\n",
5421 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005422 }
5423 rcu_read_unlock();
5424 read_unlock(&css_set_lock);
5425 return 0;
5426}
5427
5428#define MAX_TASKS_SHOWN_PER_CSS 25
5429static int cgroup_css_links_read(struct cgroup *cont,
5430 struct cftype *cft,
5431 struct seq_file *seq)
5432{
5433 struct cg_cgroup_link *link;
5434
5435 read_lock(&css_set_lock);
5436 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5437 struct css_set *cg = link->cg;
5438 struct task_struct *task;
5439 int count = 0;
5440 seq_printf(seq, "css_set %p\n", cg);
5441 list_for_each_entry(task, &cg->tasks, cg_list) {
5442 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5443 seq_puts(seq, " ...\n");
5444 break;
5445 } else {
5446 seq_printf(seq, " task %d\n",
5447 task_pid_vnr(task));
5448 }
5449 }
5450 }
5451 read_unlock(&css_set_lock);
5452 return 0;
5453}
5454
Paul Menagefe693432009-09-23 15:56:20 -07005455static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5456{
5457 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5458}
5459
5460static struct cftype debug_files[] = {
5461 {
5462 .name = "cgroup_refcount",
5463 .read_u64 = cgroup_refcount_read,
5464 },
5465 {
5466 .name = "taskcount",
5467 .read_u64 = debug_taskcount_read,
5468 },
5469
5470 {
5471 .name = "current_css_set",
5472 .read_u64 = current_css_set_read,
5473 },
5474
5475 {
5476 .name = "current_css_set_refcount",
5477 .read_u64 = current_css_set_refcount_read,
5478 },
5479
5480 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005481 .name = "current_css_set_cg_links",
5482 .read_seq_string = current_css_set_cg_links_read,
5483 },
5484
5485 {
5486 .name = "cgroup_css_links",
5487 .read_seq_string = cgroup_css_links_read,
5488 },
5489
5490 {
Paul Menagefe693432009-09-23 15:56:20 -07005491 .name = "releasable",
5492 .read_u64 = releasable_read,
5493 },
Paul Menagefe693432009-09-23 15:56:20 -07005494
Tejun Heo4baf6e32012-04-01 12:09:55 -07005495 { } /* terminate */
5496};
Paul Menagefe693432009-09-23 15:56:20 -07005497
5498struct cgroup_subsys debug_subsys = {
5499 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005500 .css_alloc = debug_css_alloc,
5501 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005502 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005503 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005504};
5505#endif /* CONFIG_CGROUP_DEBUG */