blob: 3ef677d314bc87b89d029d043edd92fbf9921d08 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heo28b4c272012-04-01 12:09:56 -070066/* css deactivation bias, makes css->refcnt negative to deny new trygets */
67#define CSS_DEACT_BIAS INT_MIN
68
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
73 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
74 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
75 * release_agent_path and so on. Modifying requires both cgroup_mutex and
76 * cgroup_root_mutex. Readers can acquire either of the two. This is to
77 * break the following locking order cycle.
78 *
79 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
80 * B. namespace_sem -> cgroup_mutex
81 *
82 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 * breaks it.
84 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
88#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heoe25e2cb2011-12-12 18:12:21 -080092static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Ben Blumaae8aab2010-03-10 15:22:07 -080094/*
95 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020096 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080097 * registered after that. The mutable section of this array is protected by
98 * cgroup_mutex.
99 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200100#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200101#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800102static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103#include <linux/cgroup_subsys.h>
104};
105
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106/*
107 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
108 * subsystems that are otherwise unattached - it never has more than a
109 * single cgroup, and all tasks are part of that cgroup.
110 */
111static struct cgroupfs_root rootnode;
112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
189static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700190static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700197static DEFINE_IDA(hierarchy_ida);
198static int next_hierarchy_id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700199
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
201#define dummytop (&rootnode.top_cgroup)
202
Li Zefan65dff752013-03-01 15:01:56 +0800203static struct cgroup_name root_cgroup_name = { .name = "/" };
204
Paul Menageddbcc7e2007-10-18 23:39:30 -0700205/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800206 * check for fork/exit handlers to call. This avoids us having to do
207 * extra work in the fork/exit path if none of the subsystems need to
208 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700210static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211
Tejun Heo42809dd2012-11-19 08:13:37 -0800212static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800213static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
214 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800215
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700216static int css_unbias_refcnt(int refcnt)
217{
218 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
219}
220
Tejun Heo28b4c272012-04-01 12:09:56 -0700221/* the current nr of refs, always >= 0 whether @css is deactivated or not */
222static int css_refcnt(struct cgroup_subsys_state *css)
223{
224 int v = atomic_read(&css->refcnt);
225
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700226 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700227}
228
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229/* convenient tests for these bits */
Paul Menagebd89aab2007-10-18 23:40:44 -0700230inline int cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700231{
Paul Menagebd89aab2007-10-18 23:40:44 -0700232 return test_bit(CGRP_REMOVED, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700233}
234
Li Zefan78574cf2013-04-08 19:00:38 -0700235/**
236 * cgroup_is_descendant - test ancestry
237 * @cgrp: the cgroup to be tested
238 * @ancestor: possible ancestor of @cgrp
239 *
240 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
241 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
242 * and @ancestor are accessible.
243 */
244bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
245{
246 while (cgrp) {
247 if (cgrp == ancestor)
248 return true;
249 cgrp = cgrp->parent;
250 }
251 return false;
252}
253EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700254
Adrian Bunke9685a02008-02-07 00:13:46 -0800255static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256{
257 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700258 (1 << CGRP_RELEASABLE) |
259 (1 << CGRP_NOTIFY_ON_RELEASE);
260 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700261}
262
Adrian Bunke9685a02008-02-07 00:13:46 -0800263static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700264{
Paul Menagebd89aab2007-10-18 23:40:44 -0700265 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700266}
267
Paul Menageddbcc7e2007-10-18 23:39:30 -0700268/*
269 * for_each_subsys() allows you to iterate on each subsystem attached to
270 * an active hierarchy
271 */
272#define for_each_subsys(_root, _ss) \
273list_for_each_entry(_ss, &_root->subsys_list, sibling)
274
Li Zefane5f6a862009-01-07 18:07:41 -0800275/* for_each_active_root() allows you to iterate across the active hierarchies */
276#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700277list_for_each_entry(_root, &roots, root_list)
278
Tejun Heof6ea9372012-04-01 12:09:55 -0700279static inline struct cgroup *__d_cgrp(struct dentry *dentry)
280{
281 return dentry->d_fsdata;
282}
283
Tejun Heo05ef1d72012-04-01 12:09:56 -0700284static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700285{
286 return dentry->d_fsdata;
287}
288
Tejun Heo05ef1d72012-04-01 12:09:56 -0700289static inline struct cftype *__d_cft(struct dentry *dentry)
290{
291 return __d_cfe(dentry)->type;
292}
293
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294/**
295 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
296 * @cgrp: the cgroup to be checked for liveness
297 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700298 * On success, returns true; the mutex should be later unlocked. On
299 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700301static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700302{
303 mutex_lock(&cgroup_mutex);
304 if (cgroup_is_removed(cgrp)) {
305 mutex_unlock(&cgroup_mutex);
306 return false;
307 }
308 return true;
309}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311/* the list of cgroups eligible for automatic release. Protected by
312 * release_list_lock */
313static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200314static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315static void cgroup_release_agent(struct work_struct *work);
316static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700317static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700318
Paul Menage817929e2007-10-18 23:39:36 -0700319/* Link structure for associating css_set objects with cgroups */
320struct cg_cgroup_link {
321 /*
322 * List running through cg_cgroup_links associated with a
323 * cgroup, anchored on cgroup->css_sets
324 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700325 struct list_head cgrp_link_list;
Paul Menage7717f7b2009-09-23 15:56:22 -0700326 struct cgroup *cgrp;
Paul Menage817929e2007-10-18 23:39:36 -0700327 /*
328 * List running through cg_cgroup_links pointing at a
329 * single css_set object, anchored on css_set->cg_links
330 */
331 struct list_head cg_link_list;
332 struct css_set *cg;
333};
334
335/* The default css_set - used by init and its children prior to any
336 * hierarchies being mounted. It contains a pointer to the root state
337 * for each subsystem. Also used to anchor the list of css_sets. Not
338 * reference-counted, to improve performance when child cgroups
339 * haven't been created.
340 */
341
342static struct css_set init_css_set;
343static struct cg_cgroup_link init_css_set_link;
344
Ben Blume6a11052010-03-10 15:22:09 -0800345static int cgroup_init_idr(struct cgroup_subsys *ss,
346 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700347
Paul Menage817929e2007-10-18 23:39:36 -0700348/* css_set_lock protects the list of css_set objects, and the
349 * chain of tasks off each css_set. Nests outside task->alloc_lock
350 * due to cgroup_iter_start() */
351static DEFINE_RWLOCK(css_set_lock);
352static int css_set_count;
353
Paul Menage7717f7b2009-09-23 15:56:22 -0700354/*
355 * hash table for cgroup groups. This improves the performance to find
356 * an existing css_set. This hash doesn't (currently) take into
357 * account cgroups in empty hierarchies.
358 */
Li Zefan472b1052008-04-29 01:00:11 -0700359#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800360static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700361
Li Zefan0ac801f2013-01-10 11:49:27 +0800362static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700363{
364 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800365 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700366
367 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800368 key += (unsigned long)css[i];
369 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700370
Li Zefan0ac801f2013-01-10 11:49:27 +0800371 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700372}
373
Paul Menage817929e2007-10-18 23:39:36 -0700374/* We don't maintain the lists running through each css_set to its
375 * task until after the first call to cgroup_iter_start(). This
376 * reduces the fork()/exit() overhead for people who have cgroups
377 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700378static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700379
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700380static void __put_css_set(struct css_set *cg, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700381{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700382 struct cg_cgroup_link *link;
383 struct cg_cgroup_link *saved_link;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700384 /*
385 * Ensure that the refcount doesn't hit zero while any readers
386 * can see it. Similar to atomic_dec_and_lock(), but for an
387 * rwlock
388 */
389 if (atomic_add_unless(&cg->refcount, -1, 1))
390 return;
391 write_lock(&css_set_lock);
392 if (!atomic_dec_and_test(&cg->refcount)) {
393 write_unlock(&css_set_lock);
394 return;
395 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700396
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397 /* This css_set is dead. unlink it and release cgroup refcounts */
Li Zefan0ac801f2013-01-10 11:49:27 +0800398 hash_del(&cg->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399 css_set_count--;
400
401 list_for_each_entry_safe(link, saved_link, &cg->cg_links,
402 cg_link_list) {
403 struct cgroup *cgrp = link->cgrp;
404 list_del(&link->cg_link_list);
405 list_del(&link->cgrp_link_list);
Li Zefan71b57072013-01-24 14:43:28 +0800406
407 /*
408 * We may not be holding cgroup_mutex, and if cgrp->count is
409 * dropped to 0 the cgroup can be destroyed at any time, hence
410 * rcu_read_lock is used to keep it alive.
411 */
412 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700413 if (atomic_dec_and_test(&cgrp->count) &&
414 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700416 set_bit(CGRP_RELEASABLE, &cgrp->flags);
417 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418 }
Li Zefan71b57072013-01-24 14:43:28 +0800419 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420
421 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700422 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700423
424 write_unlock(&css_set_lock);
Lai Jiangshan30088ad2011-03-15 17:53:46 +0800425 kfree_rcu(cg, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700426}
427
428/*
429 * refcounted get/put for css_set objects
430 */
431static inline void get_css_set(struct css_set *cg)
432{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700433 atomic_inc(&cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700434}
435
436static inline void put_css_set(struct css_set *cg)
437{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700438 __put_css_set(cg, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700439}
440
Paul Menage81a6a5c2007-10-18 23:39:38 -0700441static inline void put_css_set_taskexit(struct css_set *cg)
442{
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700443 __put_css_set(cg, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700444}
445
Paul Menage817929e2007-10-18 23:39:36 -0700446/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700447 * compare_css_sets - helper function for find_existing_css_set().
448 * @cg: candidate css_set being tested
449 * @old_cg: existing css_set for a task
450 * @new_cgrp: cgroup that's being entered by the task
451 * @template: desired set of css pointers in css_set (pre-calculated)
452 *
453 * Returns true if "cg" matches "old_cg" except for the hierarchy
454 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
455 */
456static bool compare_css_sets(struct css_set *cg,
457 struct css_set *old_cg,
458 struct cgroup *new_cgrp,
459 struct cgroup_subsys_state *template[])
460{
461 struct list_head *l1, *l2;
462
463 if (memcmp(template, cg->subsys, sizeof(cg->subsys))) {
464 /* Not all subsystems matched */
465 return false;
466 }
467
468 /*
469 * Compare cgroup pointers in order to distinguish between
470 * different cgroups in heirarchies with no subsystems. We
471 * could get by with just this check alone (and skip the
472 * memcmp above) but on most setups the memcmp check will
473 * avoid the need for this more expensive check on almost all
474 * candidates.
475 */
476
477 l1 = &cg->cg_links;
478 l2 = &old_cg->cg_links;
479 while (1) {
480 struct cg_cgroup_link *cgl1, *cgl2;
481 struct cgroup *cg1, *cg2;
482
483 l1 = l1->next;
484 l2 = l2->next;
485 /* See if we reached the end - both lists are equal length. */
486 if (l1 == &cg->cg_links) {
487 BUG_ON(l2 != &old_cg->cg_links);
488 break;
489 } else {
490 BUG_ON(l2 == &old_cg->cg_links);
491 }
492 /* Locate the cgroups associated with these links. */
493 cgl1 = list_entry(l1, struct cg_cgroup_link, cg_link_list);
494 cgl2 = list_entry(l2, struct cg_cgroup_link, cg_link_list);
495 cg1 = cgl1->cgrp;
496 cg2 = cgl2->cgrp;
497 /* Hierarchies should be linked in the same order. */
498 BUG_ON(cg1->root != cg2->root);
499
500 /*
501 * If this hierarchy is the hierarchy of the cgroup
502 * that's changing, then we need to check that this
503 * css_set points to the new cgroup; if it's any other
504 * hierarchy, then this css_set should point to the
505 * same cgroup as the old css_set.
506 */
507 if (cg1->root == new_cgrp->root) {
508 if (cg1 != new_cgrp)
509 return false;
510 } else {
511 if (cg1 != cg2)
512 return false;
513 }
514 }
515 return true;
516}
517
518/*
Paul Menage817929e2007-10-18 23:39:36 -0700519 * find_existing_css_set() is a helper for
520 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700521 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700522 *
523 * oldcg: the cgroup group that we're using before the cgroup
524 * transition
525 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700526 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700527 *
528 * template: location in which to build the desired set of subsystem
529 * state objects for the new cgroup group
530 */
Paul Menage817929e2007-10-18 23:39:36 -0700531static struct css_set *find_existing_css_set(
532 struct css_set *oldcg,
Paul Menagebd89aab2007-10-18 23:40:44 -0700533 struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -0700534 struct cgroup_subsys_state *template[])
535{
536 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700537 struct cgroupfs_root *root = cgrp->root;
Li Zefan472b1052008-04-29 01:00:11 -0700538 struct css_set *cg;
Li Zefan0ac801f2013-01-10 11:49:27 +0800539 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700540
Ben Blumaae8aab2010-03-10 15:22:07 -0800541 /*
542 * Build the set of subsystem state objects that we want to see in the
543 * new css_set. while subsystems can change globally, the entries here
544 * won't change, so no need for locking.
545 */
Paul Menage817929e2007-10-18 23:39:36 -0700546 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400547 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700548 /* Subsystem is in this hierarchy. So we want
549 * the subsystem state from the new
550 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700551 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700552 } else {
553 /* Subsystem is not in this hierarchy, so we
554 * don't want to change the subsystem state */
555 template[i] = oldcg->subsys[i];
556 }
557 }
558
Li Zefan0ac801f2013-01-10 11:49:27 +0800559 key = css_set_hash(template);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800560 hash_for_each_possible(css_set_table, cg, hlist, key) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700561 if (!compare_css_sets(cg, oldcg, cgrp, template))
562 continue;
563
564 /* This css_set matches what we need */
565 return cg;
Li Zefan472b1052008-04-29 01:00:11 -0700566 }
Paul Menage817929e2007-10-18 23:39:36 -0700567
568 /* No existing cgroup group matched */
569 return NULL;
570}
571
Paul Menage817929e2007-10-18 23:39:36 -0700572static void free_cg_links(struct list_head *tmp)
573{
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700574 struct cg_cgroup_link *link;
575 struct cg_cgroup_link *saved_link;
576
577 list_for_each_entry_safe(link, saved_link, tmp, cgrp_link_list) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700578 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -0700579 kfree(link);
580 }
581}
582
583/*
Li Zefan36553432008-07-29 22:33:19 -0700584 * allocate_cg_links() allocates "count" cg_cgroup_link structures
585 * and chains them on tmp through their cgrp_link_list fields. Returns 0 on
586 * success or a negative error
587 */
588static int allocate_cg_links(int count, struct list_head *tmp)
589{
590 struct cg_cgroup_link *link;
591 int i;
592 INIT_LIST_HEAD(tmp);
593 for (i = 0; i < count; i++) {
594 link = kmalloc(sizeof(*link), GFP_KERNEL);
595 if (!link) {
596 free_cg_links(tmp);
597 return -ENOMEM;
598 }
599 list_add(&link->cgrp_link_list, tmp);
600 }
601 return 0;
602}
603
Li Zefanc12f65d2009-01-07 18:07:42 -0800604/**
605 * link_css_set - a helper function to link a css_set to a cgroup
606 * @tmp_cg_links: cg_cgroup_link objects allocated by allocate_cg_links()
607 * @cg: the css_set to be linked
608 * @cgrp: the destination cgroup
609 */
610static void link_css_set(struct list_head *tmp_cg_links,
611 struct css_set *cg, struct cgroup *cgrp)
612{
613 struct cg_cgroup_link *link;
614
615 BUG_ON(list_empty(tmp_cg_links));
616 link = list_first_entry(tmp_cg_links, struct cg_cgroup_link,
617 cgrp_link_list);
618 link->cg = cg;
Paul Menage7717f7b2009-09-23 15:56:22 -0700619 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700620 atomic_inc(&cgrp->count);
Li Zefanc12f65d2009-01-07 18:07:42 -0800621 list_move(&link->cgrp_link_list, &cgrp->css_sets);
Paul Menage7717f7b2009-09-23 15:56:22 -0700622 /*
623 * Always add links to the tail of the list so that the list
624 * is sorted by order of hierarchy creation
625 */
626 list_add_tail(&link->cg_link_list, &cg->cg_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800627}
628
Li Zefan36553432008-07-29 22:33:19 -0700629/*
Paul Menage817929e2007-10-18 23:39:36 -0700630 * find_css_set() takes an existing cgroup group and a
631 * cgroup object, and returns a css_set object that's
632 * equivalent to the old group, but with the given cgroup
633 * substituted into the appropriate hierarchy. Must be called with
634 * cgroup_mutex held
635 */
Paul Menage817929e2007-10-18 23:39:36 -0700636static struct css_set *find_css_set(
Paul Menagebd89aab2007-10-18 23:40:44 -0700637 struct css_set *oldcg, struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700638{
639 struct css_set *res;
640 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Paul Menage817929e2007-10-18 23:39:36 -0700641
642 struct list_head tmp_cg_links;
Paul Menage817929e2007-10-18 23:39:36 -0700643
Paul Menage7717f7b2009-09-23 15:56:22 -0700644 struct cg_cgroup_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800645 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700646
Paul Menage817929e2007-10-18 23:39:36 -0700647 /* First see if we already have a cgroup group that matches
648 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700649 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -0700650 res = find_existing_css_set(oldcg, cgrp, template);
Paul Menage817929e2007-10-18 23:39:36 -0700651 if (res)
652 get_css_set(res);
Li Zefan7e9abd82008-07-25 01:46:54 -0700653 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700654
655 if (res)
656 return res;
657
658 res = kmalloc(sizeof(*res), GFP_KERNEL);
659 if (!res)
660 return NULL;
661
662 /* Allocate all the cg_cgroup_link objects that we'll need */
663 if (allocate_cg_links(root_count, &tmp_cg_links) < 0) {
664 kfree(res);
665 return NULL;
666 }
667
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700668 atomic_set(&res->refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -0700669 INIT_LIST_HEAD(&res->cg_links);
670 INIT_LIST_HEAD(&res->tasks);
Li Zefan472b1052008-04-29 01:00:11 -0700671 INIT_HLIST_NODE(&res->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700672
673 /* Copy the set of subsystem state objects generated in
674 * find_existing_css_set() */
675 memcpy(res->subsys, template, sizeof(res->subsys));
676
677 write_lock(&css_set_lock);
678 /* Add reference counts and links from the new css_set. */
Paul Menage7717f7b2009-09-23 15:56:22 -0700679 list_for_each_entry(link, &oldcg->cg_links, cg_link_list) {
680 struct cgroup *c = link->cgrp;
681 if (c->root == cgrp->root)
682 c = cgrp;
683 link_css_set(&tmp_cg_links, res, c);
684 }
Paul Menage817929e2007-10-18 23:39:36 -0700685
686 BUG_ON(!list_empty(&tmp_cg_links));
687
Paul Menage817929e2007-10-18 23:39:36 -0700688 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700689
690 /* Add this cgroup group to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +0800691 key = css_set_hash(res->subsys);
692 hash_add(css_set_table, &res->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700693
Paul Menage817929e2007-10-18 23:39:36 -0700694 write_unlock(&css_set_lock);
695
696 return res;
Paul Menageb4f48b62007-10-18 23:39:33 -0700697}
698
Paul Menageddbcc7e2007-10-18 23:39:30 -0700699/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700700 * Return the cgroup for "task" from the given hierarchy. Must be
701 * called with cgroup_mutex held.
702 */
703static struct cgroup *task_cgroup_from_root(struct task_struct *task,
704 struct cgroupfs_root *root)
705{
706 struct css_set *css;
707 struct cgroup *res = NULL;
708
709 BUG_ON(!mutex_is_locked(&cgroup_mutex));
710 read_lock(&css_set_lock);
711 /*
712 * No need to lock the task - since we hold cgroup_mutex the
713 * task can't change groups, so the only thing that can happen
714 * is that it exits and its css is set back to init_css_set.
715 */
716 css = task->cgroups;
717 if (css == &init_css_set) {
718 res = &root->top_cgroup;
719 } else {
720 struct cg_cgroup_link *link;
721 list_for_each_entry(link, &css->cg_links, cg_link_list) {
722 struct cgroup *c = link->cgrp;
723 if (c->root == root) {
724 res = c;
725 break;
726 }
727 }
728 }
729 read_unlock(&css_set_lock);
730 BUG_ON(!res);
731 return res;
732}
733
734/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700735 * There is one global cgroup mutex. We also require taking
736 * task_lock() when dereferencing a task's cgroup subsys pointers.
737 * See "The task_lock() exception", at the end of this comment.
738 *
739 * A task must hold cgroup_mutex to modify cgroups.
740 *
741 * Any task can increment and decrement the count field without lock.
742 * So in general, code holding cgroup_mutex can't rely on the count
743 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800744 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745 * means that no tasks are currently attached, therefore there is no
746 * way a task attached to that cgroup can fork (the other way to
747 * increment the count). So code holding cgroup_mutex can safely
748 * assume that if the count is zero, it will stay zero. Similarly, if
749 * a task holds cgroup_mutex on a cgroup with zero count, it
750 * knows that the cgroup won't be removed, as cgroup_rmdir()
751 * needs that mutex.
752 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
754 * (usually) take cgroup_mutex. These are the two most performance
755 * critical pieces of code here. The exception occurs on cgroup_exit(),
756 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
757 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800758 * to the release agent with the name of the cgroup (path relative to
759 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 *
761 * A cgroup can only be deleted if both its 'count' of using tasks
762 * is zero, and its list of 'children' cgroups is empty. Since all
763 * tasks in the system use _some_ cgroup, and since there is always at
764 * least one task in the system (init, pid == 1), therefore, top_cgroup
765 * always has either children cgroups and/or using tasks. So we don't
766 * need a special hack to ensure that top_cgroup cannot be deleted.
767 *
768 * The task_lock() exception
769 *
770 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800771 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800772 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 * several performance critical places that need to reference
774 * task->cgroup without the expense of grabbing a system global
775 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800776 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
778 * the task_struct routinely used for such matters.
779 *
780 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800781 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782 */
783
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784/*
785 * A couple of forward declarations required, due to cyclic reference loop:
786 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
787 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
788 * -> cgroup_mkdir.
789 */
790
Al Viro18bb1db2011-07-26 01:41:39 -0400791static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400792static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400794static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
795 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700796static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700797static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700798
799static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200800 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700801 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700802};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700804static int alloc_css_id(struct cgroup_subsys *ss,
805 struct cgroup *parent, struct cgroup *child);
806
Al Viroa5e7ed32011-07-26 01:55:55 -0400807static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808{
809 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810
811 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400812 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700813 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100814 inode->i_uid = current_fsuid();
815 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
817 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
818 }
819 return inode;
820}
821
Li Zefan65dff752013-03-01 15:01:56 +0800822static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
823{
824 struct cgroup_name *name;
825
826 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
827 if (!name)
828 return NULL;
829 strcpy(name->name, dentry->d_name.name);
830 return name;
831}
832
Li Zefanbe445622013-01-24 14:31:42 +0800833static void cgroup_free_fn(struct work_struct *work)
834{
835 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
836 struct cgroup_subsys *ss;
837
838 mutex_lock(&cgroup_mutex);
839 /*
840 * Release the subsystem state objects.
841 */
842 for_each_subsys(cgrp->root, ss)
843 ss->css_free(cgrp);
844
845 cgrp->root->number_of_cgroups--;
846 mutex_unlock(&cgroup_mutex);
847
848 /*
Li Zefan415cf072013-04-08 14:35:02 +0800849 * We get a ref to the parent's dentry, and put the ref when
850 * this cgroup is being freed, so it's guaranteed that the
851 * parent won't be destroyed before its children.
852 */
853 dput(cgrp->parent->dentry);
854
Li Zefancc20e012013-04-26 11:58:02 -0700855 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
856
Li Zefan415cf072013-04-08 14:35:02 +0800857 /*
Li Zefanbe445622013-01-24 14:31:42 +0800858 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700859 * created the cgroup. This will free cgrp->root, if we are
860 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800861 */
862 deactivate_super(cgrp->root->sb);
863
864 /*
865 * if we're getting rid of the cgroup, refcount should ensure
866 * that there are no pidlists left.
867 */
868 BUG_ON(!list_empty(&cgrp->pidlists));
869
870 simple_xattrs_free(&cgrp->xattrs);
871
Li Zefan65dff752013-03-01 15:01:56 +0800872 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800873 kfree(cgrp);
874}
875
876static void cgroup_free_rcu(struct rcu_head *head)
877{
878 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
879
880 schedule_work(&cgrp->free_work);
881}
882
Paul Menageddbcc7e2007-10-18 23:39:30 -0700883static void cgroup_diput(struct dentry *dentry, struct inode *inode)
884{
885 /* is dentry a directory ? if so, kfree() associated cgroup */
886 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700887 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800888
Paul Menagebd89aab2007-10-18 23:40:44 -0700889 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800890 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700891 } else {
892 struct cfent *cfe = __d_cfe(dentry);
893 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
894
895 WARN_ONCE(!list_empty(&cfe->node) &&
896 cgrp != &cgrp->root->top_cgroup,
897 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700898 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700899 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700900 }
901 iput(inode);
902}
903
Al Viroc72a04e2011-01-14 05:31:45 +0000904static int cgroup_delete(const struct dentry *d)
905{
906 return 1;
907}
908
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909static void remove_dir(struct dentry *d)
910{
911 struct dentry *parent = dget(d->d_parent);
912
913 d_delete(d);
914 simple_rmdir(parent->d_inode, d);
915 dput(parent);
916}
917
Li Zefan2739d3c2013-01-21 18:18:33 +0800918static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700921
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
923 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100924
Li Zefan2739d3c2013-01-21 18:18:33 +0800925 /*
926 * If we're doing cleanup due to failure of cgroup_create(),
927 * the corresponding @cfe may not exist.
928 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 list_for_each_entry(cfe, &cgrp->files, node) {
930 struct dentry *d = cfe->dentry;
931
932 if (cft && cfe->type != cft)
933 continue;
934
935 dget(d);
936 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700937 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 list_del_init(&cfe->node);
939 dput(d);
940
Li Zefan2739d3c2013-01-21 18:18:33 +0800941 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943}
944
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400945/**
946 * cgroup_clear_directory - selective removal of base and subsystem files
947 * @dir: directory containing the files
948 * @base_files: true if the base files should be removed
949 * @subsys_mask: mask of the subsystem ids whose files should be removed
950 */
951static void cgroup_clear_directory(struct dentry *dir, bool base_files,
952 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953{
954 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400955 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400957 for_each_subsys(cgrp->root, ss) {
958 struct cftype_set *set;
959 if (!test_bit(ss->subsys_id, &subsys_mask))
960 continue;
961 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800962 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 }
964 if (base_files) {
965 while (!list_empty(&cgrp->files))
966 cgroup_rm_file(cgrp, NULL);
967 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968}
969
970/*
971 * NOTE : the dentry must have been dget()'ed
972 */
973static void cgroup_d_remove_dir(struct dentry *dentry)
974{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100975 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100977
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400978 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980 parent = dentry->d_parent;
981 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800982 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100984 spin_unlock(&dentry->d_lock);
985 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986 remove_dir(dentry);
987}
988
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700989/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800990 * Call with cgroup_mutex held. Drops reference counts on modules, including
991 * any duplicate ones that parse_cgroupfs_options took. If this function
992 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800993 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400995 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400997 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700998 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 int i;
1000
Ben Blumaae8aab2010-03-10 15:22:07 -08001001 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001002 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001003
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001004 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1005 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 /* Check that any added subsystems are currently free */
1007 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001008 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001010 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001012 /*
1013 * Nobody should tell us to do a subsys that doesn't exist:
1014 * parse_cgroupfs_options should catch that case and refcounts
1015 * ensure that subsystems won't disappear once selected.
1016 */
1017 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 if (ss->root != &rootnode) {
1019 /* Subsystem isn't free */
1020 return -EBUSY;
1021 }
1022 }
1023
1024 /* Currently we don't handle adding/removing subsystems when
1025 * any child cgroups exist. This is theoretically supportable
1026 * but involves complex error handling, so it's being left until
1027 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001028 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 return -EBUSY;
1030
1031 /* Process each subsystem */
1032 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1033 struct cgroup_subsys *ss = subsys[i];
1034 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001035 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001037 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001038 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 BUG_ON(!dummytop->subsys[i]);
1040 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001041 cgrp->subsys[i] = dummytop->subsys[i];
1042 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001043 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001044 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001046 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001047 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001048 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001050 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001051 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1052 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001054 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001056 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001057 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001058 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001059 /* subsystem is now free - drop reference on module */
1060 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001061 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001063 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001064 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001065 /*
1066 * a refcount was taken, but we already had one, so
1067 * drop the extra reference.
1068 */
1069 module_put(ss->module);
1070#ifdef CONFIG_MODULE_UNLOAD
1071 BUG_ON(ss->module && !module_refcount(ss->module));
1072#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 } else {
1074 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001075 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 }
1077 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001078 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079
1080 return 0;
1081}
1082
Al Viro34c80b12011-12-08 21:32:45 -05001083static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084{
Al Viro34c80b12011-12-08 21:32:45 -05001085 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 struct cgroup_subsys *ss;
1087
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001088 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 for_each_subsys(root, ss)
1090 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001091 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1092 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001093 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001095 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001096 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001097 if (strlen(root->release_agent_path))
1098 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001099 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001100 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001101 if (strlen(root->name))
1102 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001103 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 return 0;
1105}
1106
1107struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001108 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001110 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001111 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001112 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001113 /* User explicitly requested empty subsystem */
1114 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001115
1116 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118};
1119
Ben Blumaae8aab2010-03-10 15:22:07 -08001120/*
1121 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001122 * with cgroup_mutex held to protect the subsys[] array. This function takes
1123 * refcounts on subsystems to be used, unless it returns error, in which case
1124 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001125 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001126static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001128 char *token, *o = data;
1129 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001130 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001131 int i;
1132 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001133
Ben Blumaae8aab2010-03-10 15:22:07 -08001134 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1135
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136#ifdef CONFIG_CPUSETS
1137 mask = ~(1UL << cpuset_subsys_id);
1138#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
Paul Menagec6d57f32009-09-23 15:56:19 -07001140 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
1142 while ((token = strsep(&o, ",")) != NULL) {
1143 if (!*token)
1144 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001145 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001146 /* Explicitly have no subsystems */
1147 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 continue;
1149 }
1150 if (!strcmp(token, "all")) {
1151 /* Mutually exclusive option 'all' + subsystem name */
1152 if (one_ss)
1153 return -EINVAL;
1154 all_ss = true;
1155 continue;
1156 }
Tejun Heo873fe092013-04-14 20:15:26 -07001157 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1158 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1159 continue;
1160 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001162 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
1165 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001166 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001167 continue;
1168 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001169 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001170 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001171 continue;
1172 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001174 /* Specifying two release agents is forbidden */
1175 if (opts->release_agent)
1176 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001177 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001178 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001179 if (!opts->release_agent)
1180 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 continue;
1182 }
1183 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001184 const char *name = token + 5;
1185 /* Can't specify an empty name */
1186 if (!strlen(name))
1187 return -EINVAL;
1188 /* Must match [\w.-]+ */
1189 for (i = 0; i < strlen(name); i++) {
1190 char c = name[i];
1191 if (isalnum(c))
1192 continue;
1193 if ((c == '.') || (c == '-') || (c == '_'))
1194 continue;
1195 return -EINVAL;
1196 }
1197 /* Specifying two names is forbidden */
1198 if (opts->name)
1199 return -EINVAL;
1200 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001201 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001202 GFP_KERNEL);
1203 if (!opts->name)
1204 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001205
1206 continue;
1207 }
1208
1209 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1210 struct cgroup_subsys *ss = subsys[i];
1211 if (ss == NULL)
1212 continue;
1213 if (strcmp(token, ss->name))
1214 continue;
1215 if (ss->disabled)
1216 continue;
1217
1218 /* Mutually exclusive option 'all' + subsystem name */
1219 if (all_ss)
1220 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001221 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 one_ss = true;
1223
1224 break;
1225 }
1226 if (i == CGROUP_SUBSYS_COUNT)
1227 return -ENOENT;
1228 }
1229
1230 /*
1231 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001235 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001236 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1237 struct cgroup_subsys *ss = subsys[i];
1238 if (ss == NULL)
1239 continue;
1240 if (ss->disabled)
1241 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001242 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243 }
1244 }
1245
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001246 /* Consistency checks */
1247
Tejun Heo873fe092013-04-14 20:15:26 -07001248 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1249 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1250
1251 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1252 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1253 return -EINVAL;
1254 }
1255
1256 if (opts->cpuset_clone_children) {
1257 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1258 return -EINVAL;
1259 }
1260 }
1261
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 /*
1263 * Option noprefix was introduced just for backward compatibility
1264 * with the old cpuset, so we allow noprefix only if mounting just
1265 * the cpuset subsystem.
1266 */
Tejun Heo93438622013-04-14 20:15:25 -07001267 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 return -EINVAL;
1269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270
1271 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001272 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001273 return -EINVAL;
1274
1275 /*
1276 * We either have to specify by name or by subsystems. (So all
1277 * empty hierarchies must have a name).
1278 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001279 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 return -EINVAL;
1281
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 /*
1283 * Grab references on all the modules we'll need, so the subsystems
1284 * don't dance around before rebind_subsystems attaches them. This may
1285 * take duplicate reference counts on a subsystem that's already used,
1286 * but rebind_subsystems handles this case.
1287 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001288 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 unsigned long bit = 1UL << i;
1290
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001291 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 continue;
1293 if (!try_module_get(subsys[i]->module)) {
1294 module_pin_failed = true;
1295 break;
1296 }
1297 }
1298 if (module_pin_failed) {
1299 /*
1300 * oops, one of the modules was going away. this means that we
1301 * raced with a module_delete call, and to the user this is
1302 * essentially a "subsystem doesn't exist" case.
1303 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001304 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 /* drop refcounts only on the ones we took */
1306 unsigned long bit = 1UL << i;
1307
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001308 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 continue;
1310 module_put(subsys[i]->module);
1311 }
1312 return -ENOENT;
1313 }
1314
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 return 0;
1316}
1317
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001318static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001319{
1320 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001321 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 unsigned long bit = 1UL << i;
1323
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001324 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 continue;
1326 module_put(subsys[i]->module);
1327 }
1328}
1329
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1331{
1332 int ret = 0;
1333 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001336 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337
Tejun Heo873fe092013-04-14 20:15:26 -07001338 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1339 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1340 return -EINVAL;
1341 }
1342
Paul Menagebd89aab2007-10-18 23:40:44 -07001343 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001345 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346
1347 /* See what subsystems are wanted */
1348 ret = parse_cgroupfs_options(data, &opts);
1349 if (ret)
1350 goto out_unlock;
1351
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001352 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001353 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1354 task_tgid_nr(current), current->comm);
1355
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001356 added_mask = opts.subsys_mask & ~root->subsys_mask;
1357 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001358
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 /* Don't allow flags or name to change at remount */
1360 if (opts.flags != root->flags ||
1361 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001363 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 goto out_unlock;
1365 }
1366
Gao feng7083d032012-12-03 09:28:18 +08001367 /*
1368 * Clear out the files of subsystems that should be removed, do
1369 * this before rebind_subsystems, since rebind_subsystems may
1370 * change this hierarchy's subsys_list.
1371 */
1372 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1373
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001374 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001375 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001376 /* rebind_subsystems failed, re-populate the removed files */
1377 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001378 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001379 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001380 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001382 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001383 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384
Paul Menage81a6a5c2007-10-18 23:39:38 -07001385 if (opts.release_agent)
1386 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001388 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001389 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001390 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001392 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 return ret;
1394}
1395
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001396static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 .statfs = simple_statfs,
1398 .drop_inode = generic_delete_inode,
1399 .show_options = cgroup_show_options,
1400 .remount_fs = cgroup_remount,
1401};
1402
Paul Menagecc31edc2008-10-18 20:28:04 -07001403static void init_cgroup_housekeeping(struct cgroup *cgrp)
1404{
1405 INIT_LIST_HEAD(&cgrp->sibling);
1406 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001407 INIT_LIST_HEAD(&cgrp->files);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408 INIT_LIST_HEAD(&cgrp->css_sets);
Tejun Heo22430762012-11-19 08:13:35 -08001409 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001410 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001411 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001412 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001413 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001414 INIT_LIST_HEAD(&cgrp->event_list);
1415 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001416 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001417}
Paul Menagec6d57f32009-09-23 15:56:19 -07001418
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419static void init_cgroup_root(struct cgroupfs_root *root)
1420{
Paul Menagebd89aab2007-10-18 23:40:44 -07001421 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001422
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423 INIT_LIST_HEAD(&root->subsys_list);
1424 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001425 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001427 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001428 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001429 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001430 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431}
1432
Tejun Heofa3ca072013-04-14 11:36:56 -07001433static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001434{
Tejun Heofa3ca072013-04-14 11:36:56 -07001435 int ret;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001436
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001437 lockdep_assert_held(&cgroup_mutex);
1438 lockdep_assert_held(&cgroup_root_mutex);
1439
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440 do {
1441 if (!ida_pre_get(&hierarchy_ida, GFP_KERNEL))
Tejun Heofa3ca072013-04-14 11:36:56 -07001442 return -ENOMEM;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443 /* Try to allocate the next unused ID */
1444 ret = ida_get_new_above(&hierarchy_ida, next_hierarchy_id,
1445 &root->hierarchy_id);
1446 if (ret == -ENOSPC)
1447 /* Try again starting from 0 */
1448 ret = ida_get_new(&hierarchy_ida, &root->hierarchy_id);
1449 if (!ret) {
1450 next_hierarchy_id = root->hierarchy_id + 1;
1451 } else if (ret != -EAGAIN) {
1452 /* Can only get here if the 31-bit IDR is full ... */
1453 BUG_ON(ret);
1454 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001455 } while (ret);
Tejun Heofa3ca072013-04-14 11:36:56 -07001456 return 0;
1457}
1458
1459static void cgroup_exit_root_id(struct cgroupfs_root *root)
1460{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001461 lockdep_assert_held(&cgroup_mutex);
1462 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001463
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001464 if (root->hierarchy_id) {
1465 ida_remove(&hierarchy_ida, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001466 root->hierarchy_id = 0;
1467 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001468}
1469
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470static int cgroup_test_super(struct super_block *sb, void *data)
1471{
Paul Menagec6d57f32009-09-23 15:56:19 -07001472 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 struct cgroupfs_root *root = sb->s_fs_info;
1474
Paul Menagec6d57f32009-09-23 15:56:19 -07001475 /* If we asked for a name then it must match */
1476 if (opts->name && strcmp(opts->name, root->name))
1477 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001479 /*
1480 * If we asked for subsystems (or explicitly for no
1481 * subsystems) then they must match
1482 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001483 if ((opts->subsys_mask || opts->none)
1484 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485 return 0;
1486
1487 return 1;
1488}
1489
Paul Menagec6d57f32009-09-23 15:56:19 -07001490static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1491{
1492 struct cgroupfs_root *root;
1493
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001494 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001495 return NULL;
1496
1497 root = kzalloc(sizeof(*root), GFP_KERNEL);
1498 if (!root)
1499 return ERR_PTR(-ENOMEM);
1500
1501 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001502
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001503 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001504 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001505 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001506 if (opts->release_agent)
1507 strcpy(root->release_agent_path, opts->release_agent);
1508 if (opts->name)
1509 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001510 if (opts->cpuset_clone_children)
1511 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001512 return root;
1513}
1514
Tejun Heofa3ca072013-04-14 11:36:56 -07001515static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001516{
Tejun Heofa3ca072013-04-14 11:36:56 -07001517 if (root) {
1518 /* hierarhcy ID shoulid already have been released */
1519 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001520
Tejun Heofa3ca072013-04-14 11:36:56 -07001521 ida_destroy(&root->cgroup_ida);
1522 kfree(root);
1523 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001524}
1525
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526static int cgroup_set_super(struct super_block *sb, void *data)
1527{
1528 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 struct cgroup_sb_opts *opts = data;
1530
1531 /* If we don't have a new root, we can't set up a new sb */
1532 if (!opts->new_root)
1533 return -EINVAL;
1534
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001535 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536
1537 ret = set_anon_super(sb, NULL);
1538 if (ret)
1539 return ret;
1540
Paul Menagec6d57f32009-09-23 15:56:19 -07001541 sb->s_fs_info = opts->new_root;
1542 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543
1544 sb->s_blocksize = PAGE_CACHE_SIZE;
1545 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1546 sb->s_magic = CGROUP_SUPER_MAGIC;
1547 sb->s_op = &cgroup_ops;
1548
1549 return 0;
1550}
1551
1552static int cgroup_get_rootdir(struct super_block *sb)
1553{
Al Viro0df6a632010-12-21 13:29:29 -05001554 static const struct dentry_operations cgroup_dops = {
1555 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001556 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001557 };
1558
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 struct inode *inode =
1560 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
1562 if (!inode)
1563 return -ENOMEM;
1564
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 inode->i_fop = &simple_dir_operations;
1566 inode->i_op = &cgroup_dir_inode_operations;
1567 /* directories start off with i_nlink == 2 (for "." entry) */
1568 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001569 sb->s_root = d_make_root(inode);
1570 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001572 /* for everything else we want ->d_op set */
1573 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 return 0;
1575}
1576
Al Virof7e83572010-07-26 13:23:11 +04001577static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001579 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580{
1581 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 int ret = 0;
1584 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001586 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001587
1588 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001589 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001591 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 if (ret)
1593 goto out_err;
1594
1595 /*
1596 * Allocate a new cgroup root. We may not need it if we're
1597 * reusing an existing hierarchy.
1598 */
1599 new_root = cgroup_root_from_opts(&opts);
1600 if (IS_ERR(new_root)) {
1601 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001602 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001603 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Paul Menagec6d57f32009-09-23 15:56:19 -07001606 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001607 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001610 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001611 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 }
1613
Paul Menagec6d57f32009-09-23 15:56:19 -07001614 root = sb->s_fs_info;
1615 BUG_ON(!root);
1616 if (root == opts.new_root) {
1617 /* We used the new root structure, so this is a new hierarchy */
1618 struct list_head tmp_cg_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001619 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001620 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001621 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001622 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08001623 struct css_set *cg;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
1625 BUG_ON(sb->s_root != NULL);
1626
1627 ret = cgroup_get_rootdir(sb);
1628 if (ret)
1629 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001630 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631
Paul Menage817929e2007-10-18 23:39:36 -07001632 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001634 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001636 /* Check for name clashes with existing mounts */
1637 ret = -EBUSY;
1638 if (strlen(root->name))
1639 for_each_active_root(existing_root)
1640 if (!strcmp(existing_root->name, root->name))
1641 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001642
Paul Menage817929e2007-10-18 23:39:36 -07001643 /*
1644 * We're accessing css_set_count without locking
1645 * css_set_lock here, but that's OK - it can only be
1646 * increased by someone holding cgroup_lock, and
1647 * that's us. The worst that can happen is that we
1648 * have some link structures left over
1649 */
1650 ret = allocate_cg_links(css_set_count, &tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001651 if (ret)
1652 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001653
Tejun Heofa3ca072013-04-14 11:36:56 -07001654 ret = cgroup_init_root_id(root);
1655 if (ret)
1656 goto unlock_drop;
1657
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001658 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 if (ret == -EBUSY) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001660 free_cg_links(&tmp_cg_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001661 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001663 /*
1664 * There must be no failure case after here, since rebinding
1665 * takes care of subsystems' refcounts, which are explicitly
1666 * dropped in the failure exit path.
1667 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
1669 /* EBUSY should be the only error here */
1670 BUG_ON(ret);
1671
1672 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001673 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674
Li Zefanc12f65d2009-01-07 18:07:42 -08001675 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 root->top_cgroup.dentry = sb->s_root;
1677
Paul Menage817929e2007-10-18 23:39:36 -07001678 /* Link the top cgroup in this hierarchy into all
1679 * the css_set objects */
1680 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001681 hash_for_each(css_set_table, i, cg, hlist)
Li Zefan0ac801f2013-01-10 11:49:27 +08001682 link_css_set(&tmp_cg_links, cg, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001683 write_unlock(&css_set_lock);
1684
1685 free_cg_links(&tmp_cg_links);
1686
Li Zefanc12f65d2009-01-07 18:07:42 -08001687 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 BUG_ON(root->number_of_cgroups != 1);
1689
eparis@redhat2ce97382011-06-02 21:20:51 +10001690 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001691 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001692 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001693 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001695 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 } else {
1697 /*
1698 * We re-used an existing hierarchy - the new root (if
1699 * any) is not needed
1700 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001701 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001702
1703 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1704 root->flags != opts.flags) {
1705 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1706 ret = -EINVAL;
1707 goto drop_new_super;
1708 }
1709
Ben Blumcf5d5942010-03-10 15:22:09 -08001710 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001711 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 }
1713
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 kfree(opts.release_agent);
1715 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001716 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001718 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001719 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001720 mutex_unlock(&cgroup_root_mutex);
1721 mutex_unlock(&cgroup_mutex);
1722 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001724 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001725 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001726 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001727 out_err:
1728 kfree(opts.release_agent);
1729 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001730 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731}
1732
1733static void cgroup_kill_sb(struct super_block *sb) {
1734 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001735 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 int ret;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001737 struct cg_cgroup_link *link;
1738 struct cg_cgroup_link *saved_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
1740 BUG_ON(!root);
1741
1742 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001743 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744
1745 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001746 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747
1748 /* Rebind all subsystems back to the default hierarchy */
1749 ret = rebind_subsystems(root, 0);
1750 /* Shouldn't be able to fail ... */
1751 BUG_ON(ret);
1752
Paul Menage817929e2007-10-18 23:39:36 -07001753 /*
1754 * Release all the links from css_sets to this hierarchy's
1755 * root cgroup
1756 */
1757 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001758
1759 list_for_each_entry_safe(link, saved_link, &cgrp->css_sets,
1760 cgrp_link_list) {
Paul Menage817929e2007-10-18 23:39:36 -07001761 list_del(&link->cg_link_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07001762 list_del(&link->cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07001763 kfree(link);
1764 }
1765 write_unlock(&css_set_lock);
1766
Paul Menage839ec542009-01-29 14:25:22 -08001767 if (!list_empty(&root->root_list)) {
1768 list_del(&root->root_list);
1769 root_count--;
1770 }
Li Zefane5f6a862009-01-07 18:07:41 -08001771
Tejun Heofa3ca072013-04-14 11:36:56 -07001772 cgroup_exit_root_id(root);
1773
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001774 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 mutex_unlock(&cgroup_mutex);
1776
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001777 simple_xattrs_free(&cgrp->xattrs);
1778
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001780 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781}
1782
1783static struct file_system_type cgroup_fs_type = {
1784 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001785 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 .kill_sb = cgroup_kill_sb,
1787};
1788
Greg KH676db4a2010-08-05 13:53:35 -07001789static struct kobject *cgroup_kobj;
1790
Li Zefana043e3b2008-02-23 15:24:09 -08001791/**
1792 * cgroup_path - generate the path of a cgroup
1793 * @cgrp: the cgroup in question
1794 * @buf: the buffer to write the path into
1795 * @buflen: the length of the buffer
1796 *
Li Zefan65dff752013-03-01 15:01:56 +08001797 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1798 *
1799 * We can't generate cgroup path using dentry->d_name, as accessing
1800 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1801 * inode's i_mutex, while on the other hand cgroup_path() can be called
1802 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001804int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805{
Li Zefan65dff752013-03-01 15:01:56 +08001806 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001808
Tejun Heoda1f2962013-04-14 10:32:19 -07001809 if (!cgrp->parent) {
1810 if (strlcpy(buf, "/", buflen) >= buflen)
1811 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 return 0;
1813 }
1814
Tao Ma316eb662012-11-08 21:36:38 +08001815 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001816 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001817
Li Zefan65dff752013-03-01 15:01:56 +08001818 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001819 do {
Li Zefan65dff752013-03-01 15:01:56 +08001820 const char *name = cgroup_name(cgrp);
1821 int len;
1822
1823 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001825 goto out;
1826 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001827
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001829 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001830 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001831
1832 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001833 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001834 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001836out:
1837 rcu_read_unlock();
1838 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839}
Ben Blum67523c42010-03-10 15:22:11 -08001840EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001841
Ben Blum74a11662011-05-26 16:25:20 -07001842/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001843 * Control Group taskset
1844 */
Tejun Heo134d3372011-12-12 18:12:21 -08001845struct task_and_cgroup {
1846 struct task_struct *task;
1847 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001848 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001849};
1850
Tejun Heo2f7ee562011-12-12 18:12:21 -08001851struct cgroup_taskset {
1852 struct task_and_cgroup single;
1853 struct flex_array *tc_array;
1854 int tc_array_len;
1855 int idx;
1856 struct cgroup *cur_cgrp;
1857};
1858
1859/**
1860 * cgroup_taskset_first - reset taskset and return the first task
1861 * @tset: taskset of interest
1862 *
1863 * @tset iteration is initialized and the first task is returned.
1864 */
1865struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1866{
1867 if (tset->tc_array) {
1868 tset->idx = 0;
1869 return cgroup_taskset_next(tset);
1870 } else {
1871 tset->cur_cgrp = tset->single.cgrp;
1872 return tset->single.task;
1873 }
1874}
1875EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1876
1877/**
1878 * cgroup_taskset_next - iterate to the next task in taskset
1879 * @tset: taskset of interest
1880 *
1881 * Return the next task in @tset. Iteration must have been initialized
1882 * with cgroup_taskset_first().
1883 */
1884struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1885{
1886 struct task_and_cgroup *tc;
1887
1888 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1889 return NULL;
1890
1891 tc = flex_array_get(tset->tc_array, tset->idx++);
1892 tset->cur_cgrp = tc->cgrp;
1893 return tc->task;
1894}
1895EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1896
1897/**
1898 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1899 * @tset: taskset of interest
1900 *
1901 * Return the cgroup for the current (last returned) task of @tset. This
1902 * function must be preceded by either cgroup_taskset_first() or
1903 * cgroup_taskset_next().
1904 */
1905struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1906{
1907 return tset->cur_cgrp;
1908}
1909EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1910
1911/**
1912 * cgroup_taskset_size - return the number of tasks in taskset
1913 * @tset: taskset of interest
1914 */
1915int cgroup_taskset_size(struct cgroup_taskset *tset)
1916{
1917 return tset->tc_array ? tset->tc_array_len : 1;
1918}
1919EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1920
1921
Ben Blum74a11662011-05-26 16:25:20 -07001922/*
1923 * cgroup_task_migrate - move a task from one cgroup to another.
1924 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001925 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001926 */
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03001927static void cgroup_task_migrate(struct cgroup *oldcgrp,
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001928 struct task_struct *tsk, struct css_set *newcg)
Ben Blum74a11662011-05-26 16:25:20 -07001929{
1930 struct css_set *oldcg;
Ben Blum74a11662011-05-26 16:25:20 -07001931
1932 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001933 * We are synchronized through threadgroup_lock() against PF_EXITING
1934 * setting such that we can't race against cgroup_exit() changing the
1935 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001936 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001937 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Ben Blum74a11662011-05-26 16:25:20 -07001938 oldcg = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001939
Ben Blum74a11662011-05-26 16:25:20 -07001940 task_lock(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001941 rcu_assign_pointer(tsk->cgroups, newcg);
1942 task_unlock(tsk);
1943
1944 /* Update the css_set linked lists if we're using them */
1945 write_lock(&css_set_lock);
1946 if (!list_empty(&tsk->cg_list))
1947 list_move(&tsk->cg_list, &newcg->tasks);
1948 write_unlock(&css_set_lock);
1949
1950 /*
1951 * We just gained a reference on oldcg by taking it from the task. As
1952 * trading it for newcg is protected by cgroup_mutex, we're safe to drop
1953 * it here; it will be freed under RCU.
1954 */
Ben Blum74a11662011-05-26 16:25:20 -07001955 set_bit(CGRP_RELEASABLE, &oldcgrp->flags);
Daisuke Nishimura1f5320d2012-10-04 16:37:16 +09001956 put_css_set(oldcg);
Ben Blum74a11662011-05-26 16:25:20 -07001957}
1958
Li Zefana043e3b2008-02-23 15:24:09 -08001959/**
Li Zefan081aa452013-03-13 09:17:09 +08001960 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001961 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001962 * @tsk: the task or the leader of the threadgroup to be attached
1963 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001964 *
Tejun Heo257058a2011-12-12 18:12:21 -08001965 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001966 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001967 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001968static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1969 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001970{
1971 int retval, i, group_size;
1972 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001973 struct cgroupfs_root *root = cgrp->root;
1974 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001975 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001976 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001977 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001978 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001979
1980 /*
1981 * step 0: in order to do expensive, possibly blocking operations for
1982 * every thread, we cannot iterate the thread group list, since it needs
1983 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001984 * group - group_rwsem prevents new threads from appearing, and if
1985 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001986 */
Li Zefan081aa452013-03-13 09:17:09 +08001987 if (threadgroup)
1988 group_size = get_nr_threads(tsk);
1989 else
1990 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001991 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001992 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001993 if (!group)
1994 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001995 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001996 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001997 if (retval)
1998 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001999
Ben Blum74a11662011-05-26 16:25:20 -07002000 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002001 /*
2002 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2003 * already PF_EXITING could be freed from underneath us unless we
2004 * take an rcu_read_lock.
2005 */
2006 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002007 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002008 struct task_and_cgroup ent;
2009
Tejun Heocd3d0952011-12-12 18:12:21 -08002010 /* @tsk either already exited or can't exit until the end */
2011 if (tsk->flags & PF_EXITING)
2012 continue;
2013
Ben Blum74a11662011-05-26 16:25:20 -07002014 /* as per above, nr_threads may decrease, but not increase. */
2015 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002016 ent.task = tsk;
2017 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002018 /* nothing to do if this task is already in the cgroup */
2019 if (ent.cgrp == cgrp)
2020 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002021 /*
2022 * saying GFP_ATOMIC has no effect here because we did prealloc
2023 * earlier, but it's good form to communicate our expectations.
2024 */
Tejun Heo134d3372011-12-12 18:12:21 -08002025 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002026 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002027 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002028
2029 if (!threadgroup)
2030 break;
Ben Blum74a11662011-05-26 16:25:20 -07002031 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002032 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002033 /* remember the number of threads in the array for later. */
2034 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002035 tset.tc_array = group;
2036 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002037
Tejun Heo134d3372011-12-12 18:12:21 -08002038 /* methods shouldn't be called if no task is actually migrating */
2039 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002040 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002041 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002042
Ben Blum74a11662011-05-26 16:25:20 -07002043 /*
2044 * step 1: check that we can legitimately attach to the cgroup.
2045 */
2046 for_each_subsys(root, ss) {
2047 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002048 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002049 if (retval) {
2050 failed_ss = ss;
2051 goto out_cancel_attach;
2052 }
2053 }
Ben Blum74a11662011-05-26 16:25:20 -07002054 }
2055
2056 /*
2057 * step 2: make sure css_sets exist for all threads to be migrated.
2058 * we use find_css_set, which allocates a new one if necessary.
2059 */
Ben Blum74a11662011-05-26 16:25:20 -07002060 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002061 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002062 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2063 if (!tc->cg) {
2064 retval = -ENOMEM;
2065 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002066 }
2067 }
2068
2069 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002070 * step 3: now that we're guaranteed success wrt the css_sets,
2071 * proceed to move all tasks to the new cgroup. There are no
2072 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002073 */
Ben Blum74a11662011-05-26 16:25:20 -07002074 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002075 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002076 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002077 }
2078 /* nothing is sensitive to fork() after this point. */
2079
2080 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002081 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002082 */
2083 for_each_subsys(root, ss) {
2084 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002085 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087
2088 /*
2089 * step 5: success! and cleanup
2090 */
Ben Blum74a11662011-05-26 16:25:20 -07002091 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002092out_put_css_set_refs:
2093 if (retval) {
2094 for (i = 0; i < group_size; i++) {
2095 tc = flex_array_get(group, i);
2096 if (!tc->cg)
2097 break;
2098 put_css_set(tc->cg);
2099 }
Ben Blum74a11662011-05-26 16:25:20 -07002100 }
2101out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002102 if (retval) {
2103 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002104 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002105 break;
Ben Blum74a11662011-05-26 16:25:20 -07002106 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002107 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002108 }
2109 }
Ben Blum74a11662011-05-26 16:25:20 -07002110out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002111 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002112 return retval;
2113}
2114
2115/*
2116 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002117 * function to attach either it or all tasks in its threadgroup. Will lock
2118 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002119 */
2120static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002121{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002123 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002124 int ret;
2125
Ben Blum74a11662011-05-26 16:25:20 -07002126 if (!cgroup_lock_live_group(cgrp))
2127 return -ENODEV;
2128
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002129retry_find_task:
2130 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002132 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002133 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135 ret= -ESRCH;
2136 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002137 }
Ben Blum74a11662011-05-26 16:25:20 -07002138 /*
2139 * even if we're attaching all tasks in the thread group, we
2140 * only need to check permissions on one of them.
2141 */
David Howellsc69e8d92008-11-14 10:39:19 +11002142 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002143 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2144 !uid_eq(cred->euid, tcred->uid) &&
2145 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002146 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147 ret = -EACCES;
2148 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002150 } else
2151 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002152
2153 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002154 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002155
2156 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002157 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002158 * trapped in a cpuset, or RT worker may be born in a cgroup
2159 * with no rt_runtime allocated. Just say no.
2160 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002161 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002162 ret = -EINVAL;
2163 rcu_read_unlock();
2164 goto out_unlock_cgroup;
2165 }
2166
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167 get_task_struct(tsk);
2168 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002169
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002170 threadgroup_lock(tsk);
2171 if (threadgroup) {
2172 if (!thread_group_leader(tsk)) {
2173 /*
2174 * a race with de_thread from another thread's exec()
2175 * may strip us of our leadership, if this happens,
2176 * there is no choice but to throw this task away and
2177 * try again; this is
2178 * "double-double-toil-and-trouble-check locking".
2179 */
2180 threadgroup_unlock(tsk);
2181 put_task_struct(tsk);
2182 goto retry_find_task;
2183 }
Li Zefan081aa452013-03-13 09:17:09 +08002184 }
2185
2186 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2187
Tejun Heocd3d0952011-12-12 18:12:21 -08002188 threadgroup_unlock(tsk);
2189
Paul Menagebbcb81d2007-10-18 23:39:32 -07002190 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002191out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002192 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002193 return ret;
2194}
2195
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002196/**
2197 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2198 * @from: attach to all cgroups of a given task
2199 * @tsk: the task to be attached
2200 */
2201int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2202{
2203 struct cgroupfs_root *root;
2204 int retval = 0;
2205
Tejun Heo47cfcd02013-04-07 09:29:51 -07002206 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002207 for_each_active_root(root) {
2208 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2209
2210 retval = cgroup_attach_task(from_cg, tsk, false);
2211 if (retval)
2212 break;
2213 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002214 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215
2216 return retval;
2217}
2218EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2219
Paul Menageaf351022008-07-25 01:47:01 -07002220static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2221{
Ben Blum74a11662011-05-26 16:25:20 -07002222 return attach_task_by_pid(cgrp, pid, false);
2223}
2224
2225static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2226{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002227 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002228}
2229
Paul Menagee788e062008-07-25 01:46:59 -07002230static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2231 const char *buffer)
2232{
2233 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002234 if (strlen(buffer) >= PATH_MAX)
2235 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002236 if (!cgroup_lock_live_group(cgrp))
2237 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002238 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002239 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002240 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002241 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002242 return 0;
2243}
2244
2245static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2246 struct seq_file *seq)
2247{
2248 if (!cgroup_lock_live_group(cgrp))
2249 return -ENODEV;
2250 seq_puts(seq, cgrp->root->release_agent_path);
2251 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002252 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002253 return 0;
2254}
2255
Tejun Heo873fe092013-04-14 20:15:26 -07002256static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2257 struct seq_file *seq)
2258{
2259 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002260 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002261}
2262
Paul Menage84eea842008-07-25 01:47:00 -07002263/* A buffer size big enough for numbers or short strings */
2264#define CGROUP_LOCAL_BUFFER_SIZE 64
2265
Paul Menagee73d2c62008-04-29 01:00:06 -07002266static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002267 struct file *file,
2268 const char __user *userbuf,
2269 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002270{
Paul Menage84eea842008-07-25 01:47:00 -07002271 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002272 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002273 char *end;
2274
2275 if (!nbytes)
2276 return -EINVAL;
2277 if (nbytes >= sizeof(buffer))
2278 return -E2BIG;
2279 if (copy_from_user(buffer, userbuf, nbytes))
2280 return -EFAULT;
2281
2282 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002283 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002284 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002285 if (*end)
2286 return -EINVAL;
2287 retval = cft->write_u64(cgrp, cft, val);
2288 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002289 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002290 if (*end)
2291 return -EINVAL;
2292 retval = cft->write_s64(cgrp, cft, val);
2293 }
Paul Menage355e0c42007-10-18 23:39:33 -07002294 if (!retval)
2295 retval = nbytes;
2296 return retval;
2297}
2298
Paul Menagedb3b1492008-07-25 01:46:58 -07002299static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2300 struct file *file,
2301 const char __user *userbuf,
2302 size_t nbytes, loff_t *unused_ppos)
2303{
Paul Menage84eea842008-07-25 01:47:00 -07002304 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002305 int retval = 0;
2306 size_t max_bytes = cft->max_write_len;
2307 char *buffer = local_buffer;
2308
2309 if (!max_bytes)
2310 max_bytes = sizeof(local_buffer) - 1;
2311 if (nbytes >= max_bytes)
2312 return -E2BIG;
2313 /* Allocate a dynamic buffer if we need one */
2314 if (nbytes >= sizeof(local_buffer)) {
2315 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2316 if (buffer == NULL)
2317 return -ENOMEM;
2318 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002319 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2320 retval = -EFAULT;
2321 goto out;
2322 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002323
2324 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002325 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002326 if (!retval)
2327 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002328out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002329 if (buffer != local_buffer)
2330 kfree(buffer);
2331 return retval;
2332}
2333
Paul Menageddbcc7e2007-10-18 23:39:30 -07002334static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2335 size_t nbytes, loff_t *ppos)
2336{
2337 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002338 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339
Li Zefan75139b82009-01-07 18:07:33 -08002340 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002341 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002342 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002343 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002344 if (cft->write_u64 || cft->write_s64)
2345 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002346 if (cft->write_string)
2347 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002348 if (cft->trigger) {
2349 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2350 return ret ? ret : nbytes;
2351 }
Paul Menage355e0c42007-10-18 23:39:33 -07002352 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353}
2354
Paul Menagef4c753b2008-04-29 00:59:56 -07002355static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2356 struct file *file,
2357 char __user *buf, size_t nbytes,
2358 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359{
Paul Menage84eea842008-07-25 01:47:00 -07002360 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002361 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2363
2364 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2365}
2366
Paul Menagee73d2c62008-04-29 01:00:06 -07002367static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2368 struct file *file,
2369 char __user *buf, size_t nbytes,
2370 loff_t *ppos)
2371{
Paul Menage84eea842008-07-25 01:47:00 -07002372 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002373 s64 val = cft->read_s64(cgrp, cft);
2374 int len = sprintf(tmp, "%lld\n", (long long) val);
2375
2376 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2377}
2378
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2380 size_t nbytes, loff_t *ppos)
2381{
2382 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002383 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384
Li Zefan75139b82009-01-07 18:07:33 -08002385 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386 return -ENODEV;
2387
2388 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002389 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002390 if (cft->read_u64)
2391 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002392 if (cft->read_s64)
2393 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394 return -EINVAL;
2395}
2396
Paul Menage91796562008-04-29 01:00:01 -07002397/*
2398 * seqfile ops/methods for returning structured data. Currently just
2399 * supports string->u64 maps, but can be extended in future.
2400 */
2401
2402struct cgroup_seqfile_state {
2403 struct cftype *cft;
2404 struct cgroup *cgroup;
2405};
2406
2407static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2408{
2409 struct seq_file *sf = cb->state;
2410 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2411}
2412
2413static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2414{
2415 struct cgroup_seqfile_state *state = m->private;
2416 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002417 if (cft->read_map) {
2418 struct cgroup_map_cb cb = {
2419 .fill = cgroup_map_add,
2420 .state = m,
2421 };
2422 return cft->read_map(state->cgroup, cft, &cb);
2423 }
2424 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002425}
2426
Adrian Bunk96930a62008-07-25 19:46:21 -07002427static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002428{
2429 struct seq_file *seq = file->private_data;
2430 kfree(seq->private);
2431 return single_release(inode, file);
2432}
2433
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002434static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002435 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002436 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002437 .llseek = seq_lseek,
2438 .release = cgroup_seqfile_release,
2439};
2440
Paul Menageddbcc7e2007-10-18 23:39:30 -07002441static int cgroup_file_open(struct inode *inode, struct file *file)
2442{
2443 int err;
2444 struct cftype *cft;
2445
2446 err = generic_file_open(inode, file);
2447 if (err)
2448 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002450
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002451 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002452 struct cgroup_seqfile_state *state =
2453 kzalloc(sizeof(*state), GFP_USER);
2454 if (!state)
2455 return -ENOMEM;
2456 state->cft = cft;
2457 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2458 file->f_op = &cgroup_seqfile_operations;
2459 err = single_open(file, cgroup_seqfile_show, state);
2460 if (err < 0)
2461 kfree(state);
2462 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002463 err = cft->open(inode, file);
2464 else
2465 err = 0;
2466
2467 return err;
2468}
2469
2470static int cgroup_file_release(struct inode *inode, struct file *file)
2471{
2472 struct cftype *cft = __d_cft(file->f_dentry);
2473 if (cft->release)
2474 return cft->release(inode, file);
2475 return 0;
2476}
2477
2478/*
2479 * cgroup_rename - Only allow simple rename of directories in place.
2480 */
2481static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2482 struct inode *new_dir, struct dentry *new_dentry)
2483{
Li Zefan65dff752013-03-01 15:01:56 +08002484 int ret;
2485 struct cgroup_name *name, *old_name;
2486 struct cgroup *cgrp;
2487
2488 /*
2489 * It's convinient to use parent dir's i_mutex to protected
2490 * cgrp->name.
2491 */
2492 lockdep_assert_held(&old_dir->i_mutex);
2493
Paul Menageddbcc7e2007-10-18 23:39:30 -07002494 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2495 return -ENOTDIR;
2496 if (new_dentry->d_inode)
2497 return -EEXIST;
2498 if (old_dir != new_dir)
2499 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002500
2501 cgrp = __d_cgrp(old_dentry);
2502
2503 name = cgroup_alloc_name(new_dentry);
2504 if (!name)
2505 return -ENOMEM;
2506
2507 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2508 if (ret) {
2509 kfree(name);
2510 return ret;
2511 }
2512
2513 old_name = cgrp->name;
2514 rcu_assign_pointer(cgrp->name, name);
2515
2516 kfree_rcu(old_name, rcu_head);
2517 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002518}
2519
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002520static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2521{
2522 if (S_ISDIR(dentry->d_inode->i_mode))
2523 return &__d_cgrp(dentry)->xattrs;
2524 else
Li Zefan712317a2013-04-18 23:09:52 -07002525 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002526}
2527
2528static inline int xattr_enabled(struct dentry *dentry)
2529{
2530 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002531 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002532}
2533
2534static bool is_valid_xattr(const char *name)
2535{
2536 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2537 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2538 return true;
2539 return false;
2540}
2541
2542static int cgroup_setxattr(struct dentry *dentry, const char *name,
2543 const void *val, size_t size, int flags)
2544{
2545 if (!xattr_enabled(dentry))
2546 return -EOPNOTSUPP;
2547 if (!is_valid_xattr(name))
2548 return -EINVAL;
2549 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2550}
2551
2552static int cgroup_removexattr(struct dentry *dentry, const char *name)
2553{
2554 if (!xattr_enabled(dentry))
2555 return -EOPNOTSUPP;
2556 if (!is_valid_xattr(name))
2557 return -EINVAL;
2558 return simple_xattr_remove(__d_xattrs(dentry), name);
2559}
2560
2561static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2562 void *buf, size_t size)
2563{
2564 if (!xattr_enabled(dentry))
2565 return -EOPNOTSUPP;
2566 if (!is_valid_xattr(name))
2567 return -EINVAL;
2568 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2569}
2570
2571static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2572{
2573 if (!xattr_enabled(dentry))
2574 return -EOPNOTSUPP;
2575 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2576}
2577
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002578static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002579 .read = cgroup_file_read,
2580 .write = cgroup_file_write,
2581 .llseek = generic_file_llseek,
2582 .open = cgroup_file_open,
2583 .release = cgroup_file_release,
2584};
2585
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002586static const struct inode_operations cgroup_file_inode_operations = {
2587 .setxattr = cgroup_setxattr,
2588 .getxattr = cgroup_getxattr,
2589 .listxattr = cgroup_listxattr,
2590 .removexattr = cgroup_removexattr,
2591};
2592
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002593static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002594 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002595 .mkdir = cgroup_mkdir,
2596 .rmdir = cgroup_rmdir,
2597 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002598 .setxattr = cgroup_setxattr,
2599 .getxattr = cgroup_getxattr,
2600 .listxattr = cgroup_listxattr,
2601 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002602};
2603
Al Viro00cd8dd2012-06-10 17:13:09 -04002604static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002605{
2606 if (dentry->d_name.len > NAME_MAX)
2607 return ERR_PTR(-ENAMETOOLONG);
2608 d_add(dentry, NULL);
2609 return NULL;
2610}
2611
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002612/*
2613 * Check if a file is a control file
2614 */
2615static inline struct cftype *__file_cft(struct file *file)
2616{
Al Viro496ad9a2013-01-23 17:07:38 -05002617 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002618 return ERR_PTR(-EINVAL);
2619 return __d_cft(file->f_dentry);
2620}
2621
Al Viroa5e7ed32011-07-26 01:55:55 -04002622static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002623 struct super_block *sb)
2624{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002625 struct inode *inode;
2626
2627 if (!dentry)
2628 return -ENOENT;
2629 if (dentry->d_inode)
2630 return -EEXIST;
2631
2632 inode = cgroup_new_inode(mode, sb);
2633 if (!inode)
2634 return -ENOMEM;
2635
2636 if (S_ISDIR(mode)) {
2637 inode->i_op = &cgroup_dir_inode_operations;
2638 inode->i_fop = &simple_dir_operations;
2639
2640 /* start off with i_nlink == 2 (for "." entry) */
2641 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002642 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002643
Tejun Heob8a2df62012-11-19 08:13:37 -08002644 /*
2645 * Control reaches here with cgroup_mutex held.
2646 * @inode->i_mutex should nest outside cgroup_mutex but we
2647 * want to populate it immediately without releasing
2648 * cgroup_mutex. As @inode isn't visible to anyone else
2649 * yet, trylock will always succeed without affecting
2650 * lockdep checks.
2651 */
2652 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002653 } else if (S_ISREG(mode)) {
2654 inode->i_size = 0;
2655 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002656 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 d_instantiate(dentry, inode);
2659 dget(dentry); /* Extra count - pin the dentry in core */
2660 return 0;
2661}
2662
Li Zefan099fca32009-04-02 16:57:29 -07002663/**
2664 * cgroup_file_mode - deduce file mode of a control file
2665 * @cft: the control file in question
2666 *
2667 * returns cft->mode if ->mode is not 0
2668 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2669 * returns S_IRUGO if it has only a read handler
2670 * returns S_IWUSR if it has only a write hander
2671 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002672static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002673{
Al Viroa5e7ed32011-07-26 01:55:55 -04002674 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002675
2676 if (cft->mode)
2677 return cft->mode;
2678
2679 if (cft->read || cft->read_u64 || cft->read_s64 ||
2680 cft->read_map || cft->read_seq_string)
2681 mode |= S_IRUGO;
2682
2683 if (cft->write || cft->write_u64 || cft->write_s64 ||
2684 cft->write_string || cft->trigger)
2685 mode |= S_IWUSR;
2686
2687 return mode;
2688}
2689
Tejun Heodb0416b2012-04-01 12:09:55 -07002690static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002691 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002692{
Paul Menagebd89aab2007-10-18 23:40:44 -07002693 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002694 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002696 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002697 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002698 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002699 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002700
Tejun Heo93438622013-04-14 20:15:25 -07002701 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702 strcpy(name, subsys->name);
2703 strcat(name, ".");
2704 }
2705 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002706
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002708
2709 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2710 if (!cfe)
2711 return -ENOMEM;
2712
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002714 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002716 goto out;
2717 }
2718
2719 mode = cgroup_file_mode(cft);
2720 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2721 if (!error) {
2722 cfe->type = (void *)cft;
2723 cfe->dentry = dentry;
2724 dentry->d_fsdata = cfe;
Li Zefan712317a2013-04-18 23:09:52 -07002725 simple_xattrs_init(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002726 list_add_tail(&cfe->node, &parent->files);
2727 cfe = NULL;
2728 }
2729 dput(dentry);
2730out:
2731 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 return error;
2733}
2734
Tejun Heo79578622012-04-01 12:09:56 -07002735static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002736 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002738 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002739 int err, ret = 0;
2740
2741 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002742 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002743 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2744 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002745 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2746 continue;
2747 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2748 continue;
2749
Li Zefan2739d3c2013-01-21 18:18:33 +08002750 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002751 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002752 if (err)
2753 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2754 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002755 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002756 } else {
2757 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002758 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002760 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761}
2762
Tejun Heo8e3f6542012-04-01 12:09:55 -07002763static DEFINE_MUTEX(cgroup_cft_mutex);
2764
2765static void cgroup_cfts_prepare(void)
2766 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2767{
2768 /*
2769 * Thanks to the entanglement with vfs inode locking, we can't walk
2770 * the existing cgroups under cgroup_mutex and create files.
2771 * Instead, we increment reference on all cgroups and build list of
2772 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2773 * exclusive access to the field.
2774 */
2775 mutex_lock(&cgroup_cft_mutex);
2776 mutex_lock(&cgroup_mutex);
2777}
2778
2779static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002780 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002781 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2782{
2783 LIST_HEAD(pending);
2784 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785
2786 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2787 if (cfts && ss->root != &rootnode) {
2788 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2789 dget(cgrp->dentry);
2790 list_add_tail(&cgrp->cft_q_node, &pending);
2791 }
2792 }
2793
2794 mutex_unlock(&cgroup_mutex);
2795
2796 /*
2797 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2798 * files for all cgroups which were created before.
2799 */
2800 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2801 struct inode *inode = cgrp->dentry->d_inode;
2802
2803 mutex_lock(&inode->i_mutex);
2804 mutex_lock(&cgroup_mutex);
2805 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002806 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807 mutex_unlock(&cgroup_mutex);
2808 mutex_unlock(&inode->i_mutex);
2809
2810 list_del_init(&cgrp->cft_q_node);
2811 dput(cgrp->dentry);
2812 }
2813
2814 mutex_unlock(&cgroup_cft_mutex);
2815}
2816
2817/**
2818 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2819 * @ss: target cgroup subsystem
2820 * @cfts: zero-length name terminated array of cftypes
2821 *
2822 * Register @cfts to @ss. Files described by @cfts are created for all
2823 * existing cgroups to which @ss is attached and all future cgroups will
2824 * have them too. This function can be called anytime whether @ss is
2825 * attached or not.
2826 *
2827 * Returns 0 on successful registration, -errno on failure. Note that this
2828 * function currently returns 0 as long as @cfts registration is successful
2829 * even if some file creation attempts on existing cgroups fail.
2830 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002831int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002832{
2833 struct cftype_set *set;
2834
2835 set = kzalloc(sizeof(*set), GFP_KERNEL);
2836 if (!set)
2837 return -ENOMEM;
2838
2839 cgroup_cfts_prepare();
2840 set->cfts = cfts;
2841 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002842 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002843
2844 return 0;
2845}
2846EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2847
Li Zefana043e3b2008-02-23 15:24:09 -08002848/**
Tejun Heo79578622012-04-01 12:09:56 -07002849 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2850 * @ss: target cgroup subsystem
2851 * @cfts: zero-length name terminated array of cftypes
2852 *
2853 * Unregister @cfts from @ss. Files described by @cfts are removed from
2854 * all existing cgroups to which @ss is attached and all future cgroups
2855 * won't have them either. This function can be called anytime whether @ss
2856 * is attached or not.
2857 *
2858 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2859 * registered with @ss.
2860 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002861int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002862{
2863 struct cftype_set *set;
2864
2865 cgroup_cfts_prepare();
2866
2867 list_for_each_entry(set, &ss->cftsets, node) {
2868 if (set->cfts == cfts) {
2869 list_del_init(&set->node);
2870 cgroup_cfts_commit(ss, cfts, false);
2871 return 0;
2872 }
2873 }
2874
2875 cgroup_cfts_commit(ss, NULL, false);
2876 return -ENOENT;
2877}
2878
2879/**
Li Zefana043e3b2008-02-23 15:24:09 -08002880 * cgroup_task_count - count the number of tasks in a cgroup.
2881 * @cgrp: the cgroup in question
2882 *
2883 * Return the number of tasks in the cgroup.
2884 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002885int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002886{
2887 int count = 0;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002888 struct cg_cgroup_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002889
Paul Menage817929e2007-10-18 23:39:36 -07002890 read_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07002891 list_for_each_entry(link, &cgrp->css_sets, cgrp_link_list) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07002892 count += atomic_read(&link->cg->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002893 }
2894 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002895 return count;
2896}
2897
2898/*
Paul Menage817929e2007-10-18 23:39:36 -07002899 * Advance a list_head iterator. The iterator should be positioned at
2900 * the start of a css_set
2901 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002902static void cgroup_advance_iter(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07002903 struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002904{
2905 struct list_head *l = it->cg_link;
2906 struct cg_cgroup_link *link;
2907 struct css_set *cg;
2908
2909 /* Advance to the next non-empty css_set */
2910 do {
2911 l = l->next;
Paul Menagebd89aab2007-10-18 23:40:44 -07002912 if (l == &cgrp->css_sets) {
Paul Menage817929e2007-10-18 23:39:36 -07002913 it->cg_link = NULL;
2914 return;
2915 }
Paul Menagebd89aab2007-10-18 23:40:44 -07002916 link = list_entry(l, struct cg_cgroup_link, cgrp_link_list);
Paul Menage817929e2007-10-18 23:39:36 -07002917 cg = link->cg;
2918 } while (list_empty(&cg->tasks));
2919 it->cg_link = l;
2920 it->task = cg->tasks.next;
2921}
2922
Cliff Wickman31a7df02008-02-07 00:14:42 -08002923/*
2924 * To reduce the fork() overhead for systems that are not actually
2925 * using their cgroups capability, we don't maintain the lists running
2926 * through each css_set to its tasks until we see the list actually
2927 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002928 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002929static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002930{
2931 struct task_struct *p, *g;
2932 write_lock(&css_set_lock);
2933 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002934 /*
2935 * We need tasklist_lock because RCU is not safe against
2936 * while_each_thread(). Besides, a forking task that has passed
2937 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2938 * is not guaranteed to have its child immediately visible in the
2939 * tasklist if we walk through it with RCU.
2940 */
2941 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002942 do_each_thread(g, p) {
2943 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002944 /*
2945 * We should check if the process is exiting, otherwise
2946 * it will race with cgroup_exit() in that the list
2947 * entry won't be deleted though the process has exited.
2948 */
2949 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002950 list_add(&p->cg_list, &p->cgroups->tasks);
2951 task_unlock(p);
2952 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002953 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002954 write_unlock(&css_set_lock);
2955}
2956
Tejun Heo574bd9f2012-11-09 09:12:29 -08002957/**
2958 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
2959 * @pos: the current position (%NULL to initiate traversal)
2960 * @cgroup: cgroup whose descendants to walk
2961 *
2962 * To be used by cgroup_for_each_descendant_pre(). Find the next
2963 * descendant to visit for pre-order traversal of @cgroup's descendants.
2964 */
2965struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
2966 struct cgroup *cgroup)
2967{
2968 struct cgroup *next;
2969
2970 WARN_ON_ONCE(!rcu_read_lock_held());
2971
2972 /* if first iteration, pretend we just visited @cgroup */
2973 if (!pos) {
2974 if (list_empty(&cgroup->children))
2975 return NULL;
2976 pos = cgroup;
2977 }
2978
2979 /* visit the first child if exists */
2980 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
2981 if (next)
2982 return next;
2983
2984 /* no child, visit my or the closest ancestor's next sibling */
2985 do {
2986 next = list_entry_rcu(pos->sibling.next, struct cgroup,
2987 sibling);
2988 if (&next->sibling != &pos->parent->children)
2989 return next;
2990
2991 pos = pos->parent;
2992 } while (pos != cgroup);
2993
2994 return NULL;
2995}
2996EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
2997
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002998/**
2999 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3000 * @pos: cgroup of interest
3001 *
3002 * Return the rightmost descendant of @pos. If there's no descendant,
3003 * @pos is returned. This can be used during pre-order traversal to skip
3004 * subtree of @pos.
3005 */
3006struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3007{
3008 struct cgroup *last, *tmp;
3009
3010 WARN_ON_ONCE(!rcu_read_lock_held());
3011
3012 do {
3013 last = pos;
3014 /* ->prev isn't RCU safe, walk ->next till the end */
3015 pos = NULL;
3016 list_for_each_entry_rcu(tmp, &last->children, sibling)
3017 pos = tmp;
3018 } while (pos);
3019
3020 return last;
3021}
3022EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3023
Tejun Heo574bd9f2012-11-09 09:12:29 -08003024static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3025{
3026 struct cgroup *last;
3027
3028 do {
3029 last = pos;
3030 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3031 sibling);
3032 } while (pos);
3033
3034 return last;
3035}
3036
3037/**
3038 * cgroup_next_descendant_post - find the next descendant for post-order walk
3039 * @pos: the current position (%NULL to initiate traversal)
3040 * @cgroup: cgroup whose descendants to walk
3041 *
3042 * To be used by cgroup_for_each_descendant_post(). Find the next
3043 * descendant to visit for post-order traversal of @cgroup's descendants.
3044 */
3045struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3046 struct cgroup *cgroup)
3047{
3048 struct cgroup *next;
3049
3050 WARN_ON_ONCE(!rcu_read_lock_held());
3051
3052 /* if first iteration, visit the leftmost descendant */
3053 if (!pos) {
3054 next = cgroup_leftmost_descendant(cgroup);
3055 return next != cgroup ? next : NULL;
3056 }
3057
3058 /* if there's an unvisited sibling, visit its leftmost descendant */
3059 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3060 if (&next->sibling != &pos->parent->children)
3061 return cgroup_leftmost_descendant(next);
3062
3063 /* no sibling left, visit parent */
3064 next = pos->parent;
3065 return next != cgroup ? next : NULL;
3066}
3067EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3068
Paul Menagebd89aab2007-10-18 23:40:44 -07003069void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003070 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003071{
3072 /*
3073 * The first time anyone tries to iterate across a cgroup,
3074 * we need to enable the list linking each css_set to its
3075 * tasks, and fix up all existing tasks.
3076 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003077 if (!use_task_css_set_links)
3078 cgroup_enable_task_cg_lists();
3079
Paul Menage817929e2007-10-18 23:39:36 -07003080 read_lock(&css_set_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07003081 it->cg_link = &cgrp->css_sets;
3082 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003083}
3084
Paul Menagebd89aab2007-10-18 23:40:44 -07003085struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003086 struct cgroup_iter *it)
3087{
3088 struct task_struct *res;
3089 struct list_head *l = it->task;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003090 struct cg_cgroup_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003091
3092 /* If the iterator cg is NULL, we have no tasks */
3093 if (!it->cg_link)
3094 return NULL;
3095 res = list_entry(l, struct task_struct, cg_list);
3096 /* Advance iterator to find next entry */
3097 l = l->next;
Lai Jiangshan2019f632009-01-07 18:07:36 -08003098 link = list_entry(it->cg_link, struct cg_cgroup_link, cgrp_link_list);
3099 if (l == &link->cg->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003100 /* We reached the end of this task list - move on to
3101 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003102 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003103 } else {
3104 it->task = l;
3105 }
3106 return res;
3107}
3108
Paul Menagebd89aab2007-10-18 23:40:44 -07003109void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003110 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003111{
3112 read_unlock(&css_set_lock);
3113}
3114
Cliff Wickman31a7df02008-02-07 00:14:42 -08003115static inline int started_after_time(struct task_struct *t1,
3116 struct timespec *time,
3117 struct task_struct *t2)
3118{
3119 int start_diff = timespec_compare(&t1->start_time, time);
3120 if (start_diff > 0) {
3121 return 1;
3122 } else if (start_diff < 0) {
3123 return 0;
3124 } else {
3125 /*
3126 * Arbitrarily, if two processes started at the same
3127 * time, we'll say that the lower pointer value
3128 * started first. Note that t2 may have exited by now
3129 * so this may not be a valid pointer any longer, but
3130 * that's fine - it still serves to distinguish
3131 * between two tasks started (effectively) simultaneously.
3132 */
3133 return t1 > t2;
3134 }
3135}
3136
3137/*
3138 * This function is a callback from heap_insert() and is used to order
3139 * the heap.
3140 * In this case we order the heap in descending task start time.
3141 */
3142static inline int started_after(void *p1, void *p2)
3143{
3144 struct task_struct *t1 = p1;
3145 struct task_struct *t2 = p2;
3146 return started_after_time(t1, &t2->start_time, t2);
3147}
3148
3149/**
3150 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3151 * @scan: struct cgroup_scanner containing arguments for the scan
3152 *
3153 * Arguments include pointers to callback functions test_task() and
3154 * process_task().
3155 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3156 * and if it returns true, call process_task() for it also.
3157 * The test_task pointer may be NULL, meaning always true (select all tasks).
3158 * Effectively duplicates cgroup_iter_{start,next,end}()
3159 * but does not lock css_set_lock for the call to process_task().
3160 * The struct cgroup_scanner may be embedded in any structure of the caller's
3161 * creation.
3162 * It is guaranteed that process_task() will act on every task that
3163 * is a member of the cgroup for the duration of this call. This
3164 * function may or may not call process_task() for tasks that exit
3165 * or move to a different cgroup during the call, or are forked or
3166 * move into the cgroup during the call.
3167 *
3168 * Note that test_task() may be called with locks held, and may in some
3169 * situations be called multiple times for the same task, so it should
3170 * be cheap.
3171 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3172 * pre-allocated and will be used for heap operations (and its "gt" member will
3173 * be overwritten), else a temporary heap will be used (allocation of which
3174 * may cause this function to fail).
3175 */
3176int cgroup_scan_tasks(struct cgroup_scanner *scan)
3177{
3178 int retval, i;
3179 struct cgroup_iter it;
3180 struct task_struct *p, *dropped;
3181 /* Never dereference latest_task, since it's not refcounted */
3182 struct task_struct *latest_task = NULL;
3183 struct ptr_heap tmp_heap;
3184 struct ptr_heap *heap;
3185 struct timespec latest_time = { 0, 0 };
3186
3187 if (scan->heap) {
3188 /* The caller supplied our heap and pre-allocated its memory */
3189 heap = scan->heap;
3190 heap->gt = &started_after;
3191 } else {
3192 /* We need to allocate our own heap memory */
3193 heap = &tmp_heap;
3194 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3195 if (retval)
3196 /* cannot allocate the heap */
3197 return retval;
3198 }
3199
3200 again:
3201 /*
3202 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3203 * to determine which are of interest, and using the scanner's
3204 * "process_task" callback to process any of them that need an update.
3205 * Since we don't want to hold any locks during the task updates,
3206 * gather tasks to be processed in a heap structure.
3207 * The heap is sorted by descending task start time.
3208 * If the statically-sized heap fills up, we overflow tasks that
3209 * started later, and in future iterations only consider tasks that
3210 * started after the latest task in the previous pass. This
3211 * guarantees forward progress and that we don't miss any tasks.
3212 */
3213 heap->size = 0;
3214 cgroup_iter_start(scan->cg, &it);
3215 while ((p = cgroup_iter_next(scan->cg, &it))) {
3216 /*
3217 * Only affect tasks that qualify per the caller's callback,
3218 * if he provided one
3219 */
3220 if (scan->test_task && !scan->test_task(p, scan))
3221 continue;
3222 /*
3223 * Only process tasks that started after the last task
3224 * we processed
3225 */
3226 if (!started_after_time(p, &latest_time, latest_task))
3227 continue;
3228 dropped = heap_insert(heap, p);
3229 if (dropped == NULL) {
3230 /*
3231 * The new task was inserted; the heap wasn't
3232 * previously full
3233 */
3234 get_task_struct(p);
3235 } else if (dropped != p) {
3236 /*
3237 * The new task was inserted, and pushed out a
3238 * different task
3239 */
3240 get_task_struct(p);
3241 put_task_struct(dropped);
3242 }
3243 /*
3244 * Else the new task was newer than anything already in
3245 * the heap and wasn't inserted
3246 */
3247 }
3248 cgroup_iter_end(scan->cg, &it);
3249
3250 if (heap->size) {
3251 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003252 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003253 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003254 latest_time = q->start_time;
3255 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003256 }
3257 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003258 scan->process_task(q, scan);
3259 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003260 }
3261 /*
3262 * If we had to process any tasks at all, scan again
3263 * in case some of them were in the middle of forking
3264 * children that didn't get processed.
3265 * Not the most efficient way to do it, but it avoids
3266 * having to take callback_mutex in the fork path
3267 */
3268 goto again;
3269 }
3270 if (heap == &tmp_heap)
3271 heap_free(&tmp_heap);
3272 return 0;
3273}
3274
Tejun Heo8cc99342013-04-07 09:29:50 -07003275static void cgroup_transfer_one_task(struct task_struct *task,
3276 struct cgroup_scanner *scan)
3277{
3278 struct cgroup *new_cgroup = scan->data;
3279
Tejun Heo47cfcd02013-04-07 09:29:51 -07003280 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003281 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003282 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003283}
3284
3285/**
3286 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3287 * @to: cgroup to which the tasks will be moved
3288 * @from: cgroup in which the tasks currently reside
3289 */
3290int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3291{
3292 struct cgroup_scanner scan;
3293
3294 scan.cg = from;
3295 scan.test_task = NULL; /* select all tasks in cgroup */
3296 scan.process_task = cgroup_transfer_one_task;
3297 scan.heap = NULL;
3298 scan.data = to;
3299
3300 return cgroup_scan_tasks(&scan);
3301}
3302
Paul Menage817929e2007-10-18 23:39:36 -07003303/*
Ben Blum102a7752009-09-23 15:56:26 -07003304 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003305 *
3306 * Reading this file can return large amounts of data if a cgroup has
3307 * *lots* of attached tasks. So it may need several calls to read(),
3308 * but we cannot guarantee that the information we produce is correct
3309 * unless we produce it entirely atomically.
3310 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003311 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003312
Li Zefan24528252012-01-20 11:58:43 +08003313/* which pidlist file are we talking about? */
3314enum cgroup_filetype {
3315 CGROUP_FILE_PROCS,
3316 CGROUP_FILE_TASKS,
3317};
3318
3319/*
3320 * A pidlist is a list of pids that virtually represents the contents of one
3321 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3322 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3323 * to the cgroup.
3324 */
3325struct cgroup_pidlist {
3326 /*
3327 * used to find which pidlist is wanted. doesn't change as long as
3328 * this particular list stays in the list.
3329 */
3330 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3331 /* array of xids */
3332 pid_t *list;
3333 /* how many elements the above list has */
3334 int length;
3335 /* how many files are using the current array */
3336 int use_count;
3337 /* each of these stored in a list by its cgroup */
3338 struct list_head links;
3339 /* pointer to the cgroup we belong to, for list removal purposes */
3340 struct cgroup *owner;
3341 /* protects the other fields */
3342 struct rw_semaphore mutex;
3343};
3344
Paul Menagebbcb81d2007-10-18 23:39:32 -07003345/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003346 * The following two functions "fix" the issue where there are more pids
3347 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3348 * TODO: replace with a kernel-wide solution to this problem
3349 */
3350#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3351static void *pidlist_allocate(int count)
3352{
3353 if (PIDLIST_TOO_LARGE(count))
3354 return vmalloc(count * sizeof(pid_t));
3355 else
3356 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3357}
3358static void pidlist_free(void *p)
3359{
3360 if (is_vmalloc_addr(p))
3361 vfree(p);
3362 else
3363 kfree(p);
3364}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003365
3366/*
Ben Blum102a7752009-09-23 15:56:26 -07003367 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003368 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003369 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003370static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003371{
Ben Blum102a7752009-09-23 15:56:26 -07003372 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003373
3374 /*
3375 * we presume the 0th element is unique, so i starts at 1. trivial
3376 * edge cases first; no work needs to be done for either
3377 */
3378 if (length == 0 || length == 1)
3379 return length;
3380 /* src and dest walk down the list; dest counts unique elements */
3381 for (src = 1; src < length; src++) {
3382 /* find next unique element */
3383 while (list[src] == list[src-1]) {
3384 src++;
3385 if (src == length)
3386 goto after;
3387 }
3388 /* dest always points to where the next unique element goes */
3389 list[dest] = list[src];
3390 dest++;
3391 }
3392after:
Ben Blum102a7752009-09-23 15:56:26 -07003393 return dest;
3394}
3395
3396static int cmppid(const void *a, const void *b)
3397{
3398 return *(pid_t *)a - *(pid_t *)b;
3399}
3400
3401/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003402 * find the appropriate pidlist for our purpose (given procs vs tasks)
3403 * returns with the lock on that pidlist already held, and takes care
3404 * of the use count, or returns NULL with no locks held if we're out of
3405 * memory.
3406 */
3407static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3408 enum cgroup_filetype type)
3409{
3410 struct cgroup_pidlist *l;
3411 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003412 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003413
Ben Blum72a8cb32009-09-23 15:56:27 -07003414 /*
3415 * We can't drop the pidlist_mutex before taking the l->mutex in case
3416 * the last ref-holder is trying to remove l from the list at the same
3417 * time. Holding the pidlist_mutex precludes somebody taking whichever
3418 * list we find out from under us - compare release_pid_array().
3419 */
3420 mutex_lock(&cgrp->pidlist_mutex);
3421 list_for_each_entry(l, &cgrp->pidlists, links) {
3422 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003423 /* make sure l doesn't vanish out from under us */
3424 down_write(&l->mutex);
3425 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003426 return l;
3427 }
3428 }
3429 /* entry not found; create a new one */
3430 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3431 if (!l) {
3432 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003433 return l;
3434 }
3435 init_rwsem(&l->mutex);
3436 down_write(&l->mutex);
3437 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003438 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003439 l->use_count = 0; /* don't increment here */
3440 l->list = NULL;
3441 l->owner = cgrp;
3442 list_add(&l->links, &cgrp->pidlists);
3443 mutex_unlock(&cgrp->pidlist_mutex);
3444 return l;
3445}
3446
3447/*
Ben Blum102a7752009-09-23 15:56:26 -07003448 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3449 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003450static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3451 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003452{
3453 pid_t *array;
3454 int length;
3455 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003456 struct cgroup_iter it;
3457 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003458 struct cgroup_pidlist *l;
3459
3460 /*
3461 * If cgroup gets more users after we read count, we won't have
3462 * enough space - tough. This race is indistinguishable to the
3463 * caller from the case that the additional cgroup users didn't
3464 * show up until sometime later on.
3465 */
3466 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003467 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003468 if (!array)
3469 return -ENOMEM;
3470 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003471 cgroup_iter_start(cgrp, &it);
3472 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003473 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003474 break;
Ben Blum102a7752009-09-23 15:56:26 -07003475 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003476 if (type == CGROUP_FILE_PROCS)
3477 pid = task_tgid_vnr(tsk);
3478 else
3479 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003480 if (pid > 0) /* make sure to only use valid results */
3481 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003482 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003483 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003484 length = n;
3485 /* now sort & (if procs) strip out duplicates */
3486 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003487 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003488 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003489 l = cgroup_pidlist_find(cgrp, type);
3490 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003491 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003492 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003493 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003494 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003495 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003496 l->list = array;
3497 l->length = length;
3498 l->use_count++;
3499 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003500 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003501 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003502}
3503
Balbir Singh846c7bb2007-10-18 23:39:44 -07003504/**
Li Zefana043e3b2008-02-23 15:24:09 -08003505 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003506 * @stats: cgroupstats to fill information into
3507 * @dentry: A dentry entry belonging to the cgroup for which stats have
3508 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003509 *
3510 * Build and fill cgroupstats so that taskstats can export it to user
3511 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003512 */
3513int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3514{
3515 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003516 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003517 struct cgroup_iter it;
3518 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003519
Balbir Singh846c7bb2007-10-18 23:39:44 -07003520 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003521 * Validate dentry by checking the superblock operations,
3522 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003523 */
Li Zefan33d283b2008-11-19 15:36:48 -08003524 if (dentry->d_sb->s_op != &cgroup_ops ||
3525 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003526 goto err;
3527
3528 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003529 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003530
Paul Menagebd89aab2007-10-18 23:40:44 -07003531 cgroup_iter_start(cgrp, &it);
3532 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003533 switch (tsk->state) {
3534 case TASK_RUNNING:
3535 stats->nr_running++;
3536 break;
3537 case TASK_INTERRUPTIBLE:
3538 stats->nr_sleeping++;
3539 break;
3540 case TASK_UNINTERRUPTIBLE:
3541 stats->nr_uninterruptible++;
3542 break;
3543 case TASK_STOPPED:
3544 stats->nr_stopped++;
3545 break;
3546 default:
3547 if (delayacct_is_task_waiting_on_io(tsk))
3548 stats->nr_io_wait++;
3549 break;
3550 }
3551 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003552 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003553
Balbir Singh846c7bb2007-10-18 23:39:44 -07003554err:
3555 return ret;
3556}
3557
Paul Menage8f3ff202009-09-23 15:56:25 -07003558
Paul Menagecc31edc2008-10-18 20:28:04 -07003559/*
Ben Blum102a7752009-09-23 15:56:26 -07003560 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003561 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003562 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003563 */
3564
Ben Blum102a7752009-09-23 15:56:26 -07003565static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003566{
3567 /*
3568 * Initially we receive a position value that corresponds to
3569 * one more than the last pid shown (or 0 on the first call or
3570 * after a seek to the start). Use a binary-search to find the
3571 * next pid to display, if any
3572 */
Ben Blum102a7752009-09-23 15:56:26 -07003573 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003574 int index = 0, pid = *pos;
3575 int *iter;
3576
Ben Blum102a7752009-09-23 15:56:26 -07003577 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003578 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003579 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003580
Paul Menagecc31edc2008-10-18 20:28:04 -07003581 while (index < end) {
3582 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003583 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003584 index = mid;
3585 break;
Ben Blum102a7752009-09-23 15:56:26 -07003586 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003587 index = mid + 1;
3588 else
3589 end = mid;
3590 }
3591 }
3592 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003593 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003594 return NULL;
3595 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003596 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003597 *pos = *iter;
3598 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003599}
3600
Ben Blum102a7752009-09-23 15:56:26 -07003601static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003602{
Ben Blum102a7752009-09-23 15:56:26 -07003603 struct cgroup_pidlist *l = s->private;
3604 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003605}
3606
Ben Blum102a7752009-09-23 15:56:26 -07003607static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003608{
Ben Blum102a7752009-09-23 15:56:26 -07003609 struct cgroup_pidlist *l = s->private;
3610 pid_t *p = v;
3611 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003612 /*
3613 * Advance to the next pid in the array. If this goes off the
3614 * end, we're done
3615 */
3616 p++;
3617 if (p >= end) {
3618 return NULL;
3619 } else {
3620 *pos = *p;
3621 return p;
3622 }
3623}
3624
Ben Blum102a7752009-09-23 15:56:26 -07003625static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003626{
3627 return seq_printf(s, "%d\n", *(int *)v);
3628}
3629
Ben Blum102a7752009-09-23 15:56:26 -07003630/*
3631 * seq_operations functions for iterating on pidlists through seq_file -
3632 * independent of whether it's tasks or procs
3633 */
3634static const struct seq_operations cgroup_pidlist_seq_operations = {
3635 .start = cgroup_pidlist_start,
3636 .stop = cgroup_pidlist_stop,
3637 .next = cgroup_pidlist_next,
3638 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003639};
3640
Ben Blum102a7752009-09-23 15:56:26 -07003641static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003642{
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /*
3644 * the case where we're the last user of this particular pidlist will
3645 * have us remove it from the cgroup's list, which entails taking the
3646 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3647 * pidlist_mutex, we have to take pidlist_mutex first.
3648 */
3649 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003650 down_write(&l->mutex);
3651 BUG_ON(!l->use_count);
3652 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003653 /* we're the last user if refcount is 0; remove and free */
3654 list_del(&l->links);
3655 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003656 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 put_pid_ns(l->key.ns);
3658 up_write(&l->mutex);
3659 kfree(l);
3660 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003661 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003662 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003663 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003664}
3665
Ben Blum102a7752009-09-23 15:56:26 -07003666static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003667{
Ben Blum102a7752009-09-23 15:56:26 -07003668 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003669 if (!(file->f_mode & FMODE_READ))
3670 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003671 /*
3672 * the seq_file will only be initialized if the file was opened for
3673 * reading; hence we check if it's not null only in that case.
3674 */
3675 l = ((struct seq_file *)file->private_data)->private;
3676 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003677 return seq_release(inode, file);
3678}
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 .read = seq_read,
3682 .llseek = seq_lseek,
3683 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003684 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003685};
3686
3687/*
Ben Blum102a7752009-09-23 15:56:26 -07003688 * The following functions handle opens on a file that displays a pidlist
3689 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3690 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 */
Ben Blum102a7752009-09-23 15:56:26 -07003692/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003693static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003694{
3695 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003697 int retval;
3698
3699 /* Nothing to do for write-only files */
3700 if (!(file->f_mode & FMODE_READ))
3701 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003702
Ben Blum102a7752009-09-23 15:56:26 -07003703 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003704 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003705 if (retval)
3706 return retval;
3707 /* configure file information */
3708 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003709
Ben Blum102a7752009-09-23 15:56:26 -07003710 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003712 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003713 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003714 }
Ben Blum102a7752009-09-23 15:56:26 -07003715 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003716 return 0;
3717}
Ben Blum102a7752009-09-23 15:56:26 -07003718static int cgroup_tasks_open(struct inode *unused, struct file *file)
3719{
Ben Blum72a8cb32009-09-23 15:56:27 -07003720 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003721}
3722static int cgroup_procs_open(struct inode *unused, struct file *file)
3723{
Ben Blum72a8cb32009-09-23 15:56:27 -07003724 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003725}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003726
Paul Menagebd89aab2007-10-18 23:40:44 -07003727static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003728 struct cftype *cft)
3729{
Paul Menagebd89aab2007-10-18 23:40:44 -07003730 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003731}
3732
Paul Menage6379c102008-07-25 01:47:01 -07003733static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3734 struct cftype *cft,
3735 u64 val)
3736{
3737 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3738 if (val)
3739 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3740 else
3741 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3742 return 0;
3743}
3744
Paul Menagebbcb81d2007-10-18 23:39:32 -07003745/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003746 * Unregister event and free resources.
3747 *
3748 * Gets called from workqueue.
3749 */
3750static void cgroup_event_remove(struct work_struct *work)
3751{
3752 struct cgroup_event *event = container_of(work, struct cgroup_event,
3753 remove);
3754 struct cgroup *cgrp = event->cgrp;
3755
Li Zefan810cbee2013-02-18 18:56:14 +08003756 remove_wait_queue(event->wqh, &event->wait);
3757
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003758 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3759
Li Zefan810cbee2013-02-18 18:56:14 +08003760 /* Notify userspace the event is going away. */
3761 eventfd_signal(event->eventfd, 1);
3762
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003763 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003764 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003765 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003766}
3767
3768/*
3769 * Gets called on POLLHUP on eventfd when user closes it.
3770 *
3771 * Called with wqh->lock held and interrupts disabled.
3772 */
3773static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3774 int sync, void *key)
3775{
3776 struct cgroup_event *event = container_of(wait,
3777 struct cgroup_event, wait);
3778 struct cgroup *cgrp = event->cgrp;
3779 unsigned long flags = (unsigned long)key;
3780
3781 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003782 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003783 * If the event has been detached at cgroup removal, we
3784 * can simply return knowing the other side will cleanup
3785 * for us.
3786 *
3787 * We can't race against event freeing since the other
3788 * side will require wqh->lock via remove_wait_queue(),
3789 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003790 */
Li Zefan810cbee2013-02-18 18:56:14 +08003791 spin_lock(&cgrp->event_list_lock);
3792 if (!list_empty(&event->list)) {
3793 list_del_init(&event->list);
3794 /*
3795 * We are in atomic context, but cgroup_event_remove()
3796 * may sleep, so we have to call it in workqueue.
3797 */
3798 schedule_work(&event->remove);
3799 }
3800 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003801 }
3802
3803 return 0;
3804}
3805
3806static void cgroup_event_ptable_queue_proc(struct file *file,
3807 wait_queue_head_t *wqh, poll_table *pt)
3808{
3809 struct cgroup_event *event = container_of(pt,
3810 struct cgroup_event, pt);
3811
3812 event->wqh = wqh;
3813 add_wait_queue(wqh, &event->wait);
3814}
3815
3816/*
3817 * Parse input and register new cgroup event handler.
3818 *
3819 * Input must be in format '<event_fd> <control_fd> <args>'.
3820 * Interpretation of args is defined by control file implementation.
3821 */
3822static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3823 const char *buffer)
3824{
3825 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003826 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003827 unsigned int efd, cfd;
3828 struct file *efile = NULL;
3829 struct file *cfile = NULL;
3830 char *endp;
3831 int ret;
3832
3833 efd = simple_strtoul(buffer, &endp, 10);
3834 if (*endp != ' ')
3835 return -EINVAL;
3836 buffer = endp + 1;
3837
3838 cfd = simple_strtoul(buffer, &endp, 10);
3839 if ((*endp != ' ') && (*endp != '\0'))
3840 return -EINVAL;
3841 buffer = endp + 1;
3842
3843 event = kzalloc(sizeof(*event), GFP_KERNEL);
3844 if (!event)
3845 return -ENOMEM;
3846 event->cgrp = cgrp;
3847 INIT_LIST_HEAD(&event->list);
3848 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3849 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3850 INIT_WORK(&event->remove, cgroup_event_remove);
3851
3852 efile = eventfd_fget(efd);
3853 if (IS_ERR(efile)) {
3854 ret = PTR_ERR(efile);
3855 goto fail;
3856 }
3857
3858 event->eventfd = eventfd_ctx_fileget(efile);
3859 if (IS_ERR(event->eventfd)) {
3860 ret = PTR_ERR(event->eventfd);
3861 goto fail;
3862 }
3863
3864 cfile = fget(cfd);
3865 if (!cfile) {
3866 ret = -EBADF;
3867 goto fail;
3868 }
3869
3870 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003871 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003872 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003873 if (ret < 0)
3874 goto fail;
3875
3876 event->cft = __file_cft(cfile);
3877 if (IS_ERR(event->cft)) {
3878 ret = PTR_ERR(event->cft);
3879 goto fail;
3880 }
3881
Li Zefanf1690072013-02-18 14:13:35 +08003882 /*
3883 * The file to be monitored must be in the same cgroup as
3884 * cgroup.event_control is.
3885 */
3886 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3887 if (cgrp_cfile != cgrp) {
3888 ret = -EINVAL;
3889 goto fail;
3890 }
3891
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003892 if (!event->cft->register_event || !event->cft->unregister_event) {
3893 ret = -EINVAL;
3894 goto fail;
3895 }
3896
3897 ret = event->cft->register_event(cgrp, event->cft,
3898 event->eventfd, buffer);
3899 if (ret)
3900 goto fail;
3901
Li Zefan7ef70e42013-04-26 11:58:03 -07003902 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003903
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003904 /*
3905 * Events should be removed after rmdir of cgroup directory, but before
3906 * destroying subsystem state objects. Let's take reference to cgroup
3907 * directory dentry to do that.
3908 */
3909 dget(cgrp->dentry);
3910
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911 spin_lock(&cgrp->event_list_lock);
3912 list_add(&event->list, &cgrp->event_list);
3913 spin_unlock(&cgrp->event_list_lock);
3914
3915 fput(cfile);
3916 fput(efile);
3917
3918 return 0;
3919
3920fail:
3921 if (cfile)
3922 fput(cfile);
3923
3924 if (event && event->eventfd && !IS_ERR(event->eventfd))
3925 eventfd_ctx_put(event->eventfd);
3926
3927 if (!IS_ERR_OR_NULL(efile))
3928 fput(efile);
3929
3930 kfree(event);
3931
3932 return ret;
3933}
3934
Daniel Lezcano97978e62010-10-27 15:33:35 -07003935static u64 cgroup_clone_children_read(struct cgroup *cgrp,
3936 struct cftype *cft)
3937{
Tejun Heo2260e7f2012-11-19 08:13:38 -08003938 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003939}
3940
3941static int cgroup_clone_children_write(struct cgroup *cgrp,
3942 struct cftype *cft,
3943 u64 val)
3944{
3945 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08003946 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003947 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08003948 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003949 return 0;
3950}
3951
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003952/*
Paul Menagebbcb81d2007-10-18 23:39:32 -07003953 * for the common functions, 'private' gives the type of file
3954 */
Ben Blum102a7752009-09-23 15:56:26 -07003955/* for hysterical raisins, we can't put this on the older files */
3956#define CGROUP_FILE_GENERIC_PREFIX "cgroup."
Paul Menage81a6a5c2007-10-18 23:39:38 -07003957static struct cftype files[] = {
3958 {
3959 .name = "tasks",
3960 .open = cgroup_tasks_open,
Paul Menageaf351022008-07-25 01:47:01 -07003961 .write_u64 = cgroup_tasks_write,
Ben Blum102a7752009-09-23 15:56:26 -07003962 .release = cgroup_pidlist_release,
Li Zefan099fca32009-04-02 16:57:29 -07003963 .mode = S_IRUGO | S_IWUSR,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003964 },
Ben Blum102a7752009-09-23 15:56:26 -07003965 {
3966 .name = CGROUP_FILE_GENERIC_PREFIX "procs",
3967 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003968 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003969 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003970 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003971 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003972 {
3973 .name = "notify_on_release",
Paul Menagef4c753b2008-04-29 00:59:56 -07003974 .read_u64 = cgroup_read_notify_on_release,
Paul Menage6379c102008-07-25 01:47:01 -07003975 .write_u64 = cgroup_write_notify_on_release,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003976 },
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003977 {
3978 .name = CGROUP_FILE_GENERIC_PREFIX "event_control",
3979 .write_string = cgroup_write_event_control,
3980 .mode = S_IWUGO,
3981 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07003982 {
3983 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003984 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003985 .read_u64 = cgroup_clone_children_read,
3986 .write_u64 = cgroup_clone_children_write,
3987 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003988 {
Tejun Heo873fe092013-04-14 20:15:26 -07003989 .name = "cgroup.sane_behavior",
3990 .flags = CFTYPE_ONLY_ON_ROOT,
3991 .read_seq_string = cgroup_sane_behavior_show,
3992 },
3993 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003994 .name = "release_agent",
3995 .flags = CFTYPE_ONLY_ON_ROOT,
3996 .read_seq_string = cgroup_release_agent_show,
3997 .write_string = cgroup_release_agent_write,
3998 .max_write_len = PATH_MAX,
3999 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004000 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004001};
4002
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004003/**
4004 * cgroup_populate_dir - selectively creation of files in a directory
4005 * @cgrp: target cgroup
4006 * @base_files: true if the base files should be added
4007 * @subsys_mask: mask of the subsystem ids whose files should be added
4008 */
4009static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4010 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004011{
4012 int err;
4013 struct cgroup_subsys *ss;
4014
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004015 if (base_files) {
4016 err = cgroup_addrm_files(cgrp, NULL, files, true);
4017 if (err < 0)
4018 return err;
4019 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004020
Tejun Heo8e3f6542012-04-01 12:09:55 -07004021 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004022 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004023 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004024 if (!test_bit(ss->subsys_id, &subsys_mask))
4025 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004026
Tejun Heodb0416b2012-04-01 12:09:55 -07004027 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004028 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004029 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004030
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004031 /* This cgroup is ready now */
4032 for_each_subsys(cgrp->root, ss) {
4033 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4034 /*
4035 * Update id->css pointer and make this css visible from
4036 * CSS ID functions. This pointer will be dereferened
4037 * from RCU-read-side without locks.
4038 */
4039 if (css->id)
4040 rcu_assign_pointer(css->id->css, css);
4041 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042
4043 return 0;
4044}
4045
Tejun Heo48ddbe12012-04-01 12:09:56 -07004046static void css_dput_fn(struct work_struct *work)
4047{
4048 struct cgroup_subsys_state *css =
4049 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004050 struct dentry *dentry = css->cgroup->dentry;
4051 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004052
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004053 atomic_inc(&sb->s_active);
4054 dput(dentry);
4055 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004056}
4057
Paul Menageddbcc7e2007-10-18 23:39:30 -07004058static void init_cgroup_css(struct cgroup_subsys_state *css,
4059 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004060 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004061{
Paul Menagebd89aab2007-10-18 23:40:44 -07004062 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004063 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004064 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004065 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004066 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004067 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004068 BUG_ON(cgrp->subsys[ss->subsys_id]);
4069 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004070
4071 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004072 * css holds an extra ref to @cgrp->dentry which is put on the last
4073 * css_put(). dput() requires process context, which css_put() may
4074 * be called without. @css->dput_work will be used to invoke
4075 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004076 */
4077 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004078}
4079
Tejun Heob1929db2012-11-19 08:13:38 -08004080/* invoke ->post_create() on a new CSS and mark it online if successful */
4081static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004082{
Tejun Heob1929db2012-11-19 08:13:38 -08004083 int ret = 0;
4084
Tejun Heoa31f2d32012-11-19 08:13:37 -08004085 lockdep_assert_held(&cgroup_mutex);
4086
Tejun Heo92fb9742012-11-19 08:13:38 -08004087 if (ss->css_online)
4088 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004089 if (!ret)
4090 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4091 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004092}
4093
4094/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4095static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4096 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4097{
4098 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4099
4100 lockdep_assert_held(&cgroup_mutex);
4101
4102 if (!(css->flags & CSS_ONLINE))
4103 return;
4104
Li Zefand7eeac12013-03-12 15:35:59 -07004105 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004106 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004107
4108 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4109}
4110
Paul Menageddbcc7e2007-10-18 23:39:30 -07004111/*
Li Zefana043e3b2008-02-23 15:24:09 -08004112 * cgroup_create - create a cgroup
4113 * @parent: cgroup that will be parent of the new cgroup
4114 * @dentry: dentry of the new cgroup
4115 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116 *
Li Zefana043e3b2008-02-23 15:24:09 -08004117 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004118 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004119static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004120 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121{
Paul Menagebd89aab2007-10-18 23:40:44 -07004122 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004123 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004124 struct cgroupfs_root *root = parent->root;
4125 int err = 0;
4126 struct cgroup_subsys *ss;
4127 struct super_block *sb = root->sb;
4128
Tejun Heo0a950f62012-11-19 09:02:12 -08004129 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004130 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4131 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004132 return -ENOMEM;
4133
Li Zefan65dff752013-03-01 15:01:56 +08004134 name = cgroup_alloc_name(dentry);
4135 if (!name)
4136 goto err_free_cgrp;
4137 rcu_assign_pointer(cgrp->name, name);
4138
Tejun Heo0a950f62012-11-19 09:02:12 -08004139 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4140 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004141 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004142
Tejun Heo976c06b2012-11-05 09:16:59 -08004143 /*
4144 * Only live parents can have children. Note that the liveliness
4145 * check isn't strictly necessary because cgroup_mkdir() and
4146 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4147 * anyway so that locking is contained inside cgroup proper and we
4148 * don't get nasty surprises if we ever grow another caller.
4149 */
4150 if (!cgroup_lock_live_group(parent)) {
4151 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004152 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004153 }
4154
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155 /* Grab a reference on the superblock so the hierarchy doesn't
4156 * get deleted on unmount if there are child cgroups. This
4157 * can be done outside cgroup_mutex, since the sb can't
4158 * disappear while someone has an open control file on the
4159 * fs */
4160 atomic_inc(&sb->s_active);
4161
Paul Menagecc31edc2008-10-18 20:28:04 -07004162 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004163
Li Zefanfe1c06c2013-01-24 14:30:22 +08004164 dentry->d_fsdata = cgrp;
4165 cgrp->dentry = dentry;
4166
Paul Menagebd89aab2007-10-18 23:40:44 -07004167 cgrp->parent = parent;
4168 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169
Li Zefanb6abdb02008-03-04 14:28:19 -08004170 if (notify_on_release(parent))
4171 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4172
Tejun Heo2260e7f2012-11-19 08:13:38 -08004173 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4174 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004175
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004177 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004178
Tejun Heo92fb9742012-11-19 08:13:38 -08004179 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180 if (IS_ERR(css)) {
4181 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004182 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004184 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004185 if (ss->use_id) {
4186 err = alloc_css_id(ss, parent, cgrp);
4187 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004188 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004189 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190 }
4191
Tejun Heo4e139af2012-11-19 08:13:36 -08004192 /*
4193 * Create directory. cgroup_create_file() returns with the new
4194 * directory locked on success so that it can be populated without
4195 * dropping cgroup_mutex.
4196 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004197 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004199 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004200 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201
Tejun Heo4e139af2012-11-19 08:13:36 -08004202 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004203 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4204 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4205 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004206
Tejun Heob1929db2012-11-19 08:13:38 -08004207 /* each css holds a ref to the cgroup's dentry */
4208 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004209 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004210
Li Zefan415cf072013-04-08 14:35:02 +08004211 /* hold a ref to the parent's dentry */
4212 dget(parent->dentry);
4213
Tejun Heob1929db2012-11-19 08:13:38 -08004214 /* creation succeeded, notify subsystems */
4215 for_each_subsys(root, ss) {
4216 err = online_css(ss, cgrp);
4217 if (err)
4218 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004219
4220 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4221 parent->parent) {
4222 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",
4223 current->comm, current->pid, ss->name);
4224 if (!strcmp(ss->name, "memory"))
4225 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4226 ss->warned_broken_hierarchy = true;
4227 }
Tejun Heoa8638032012-11-09 09:12:29 -08004228 }
4229
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004230 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004231 if (err)
4232 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233
4234 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004235 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236
4237 return 0;
4238
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004239err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004241 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004242 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245 /* Release the reference count that we took on the superblock */
4246 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004247err_free_id:
4248 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004249err_free_name:
4250 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004251err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004252 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004253 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004254
4255err_destroy:
4256 cgroup_destroy_locked(cgrp);
4257 mutex_unlock(&cgroup_mutex);
4258 mutex_unlock(&dentry->d_inode->i_mutex);
4259 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260}
4261
Al Viro18bb1db2011-07-26 01:41:39 -04004262static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263{
4264 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4265
4266 /* the vfs holds inode->i_mutex already */
4267 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4268}
4269
Tejun Heo42809dd2012-11-19 08:13:37 -08004270static int cgroup_destroy_locked(struct cgroup *cgrp)
4271 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272{
Tejun Heo42809dd2012-11-19 08:13:37 -08004273 struct dentry *d = cgrp->dentry;
4274 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004275 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004276 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277
Tejun Heo42809dd2012-11-19 08:13:37 -08004278 lockdep_assert_held(&d->d_inode->i_mutex);
4279 lockdep_assert_held(&cgroup_mutex);
4280
4281 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004283
Tejun Heo1a90dd52012-11-05 09:16:59 -08004284 /*
4285 * Block new css_tryget() by deactivating refcnt and mark @cgrp
4286 * removed. This makes future css_tryget() and child creation
4287 * attempts fail thus maintaining the removal conditions verified
4288 * above.
4289 */
Tejun Heoed9577932012-11-05 09:16:58 -08004290 for_each_subsys(cgrp->root, ss) {
4291 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4292
4293 WARN_ON(atomic_read(&css->refcnt) < 0);
4294 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004295 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004296 set_bit(CGRP_REMOVED, &cgrp->flags);
4297
Tejun Heoa31f2d32012-11-19 08:13:37 -08004298 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004299 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004300 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004301
4302 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004303 * Put all the base refs. Each css holds an extra reference to the
4304 * cgroup's dentry and cgroup removal proceeds regardless of css
4305 * refs. On the last put of each css, whenever that may be, the
4306 * extra dentry ref is put so that dentry destruction happens only
4307 * after all css's are released.
4308 */
Tejun Heoe9316082012-11-05 09:16:58 -08004309 for_each_subsys(cgrp->root, ss)
4310 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004312 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004314 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004315 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004316
Paul Menage999cd8a2009-01-07 18:08:36 -08004317 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004318 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004319 list_del_init(&cgrp->allcg_node);
4320
Tejun Heo42809dd2012-11-19 08:13:37 -08004321 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322 cgroup_d_remove_dir(d);
4323 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324
Paul Menagebd89aab2007-10-18 23:40:44 -07004325 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004326 check_for_release(parent);
4327
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004328 /*
4329 * Unregister events and notify userspace.
4330 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004331 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004332 */
4333 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004334 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004335 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004336 schedule_work(&event->remove);
4337 }
Li Zefan810cbee2013-02-18 18:56:14 +08004338 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004339
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 return 0;
4341}
4342
Tejun Heo42809dd2012-11-19 08:13:37 -08004343static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4344{
4345 int ret;
4346
4347 mutex_lock(&cgroup_mutex);
4348 ret = cgroup_destroy_locked(dentry->d_fsdata);
4349 mutex_unlock(&cgroup_mutex);
4350
4351 return ret;
4352}
4353
Tejun Heo8e3f6542012-04-01 12:09:55 -07004354static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4355{
4356 INIT_LIST_HEAD(&ss->cftsets);
4357
4358 /*
4359 * base_cftset is embedded in subsys itself, no need to worry about
4360 * deregistration.
4361 */
4362 if (ss->base_cftypes) {
4363 ss->base_cftset.cfts = ss->base_cftypes;
4364 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4365 }
4366}
4367
Li Zefan06a11922008-04-29 01:00:07 -07004368static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004369{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004371
4372 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
Tejun Heo648bb562012-11-19 08:13:36 -08004374 mutex_lock(&cgroup_mutex);
4375
Tejun Heo8e3f6542012-04-01 12:09:55 -07004376 /* init base cftset */
4377 cgroup_init_cftsets(ss);
4378
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004380 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004382 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004383 /* We don't handle early failures gracefully */
4384 BUG_ON(IS_ERR(css));
4385 init_cgroup_css(css, ss, dummytop);
4386
Li Zefane8d55fd2008-04-29 01:00:13 -07004387 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004388 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004389 * newly registered, all tasks and hence the
4390 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004391 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392
4393 need_forkexit_callback |= ss->fork || ss->exit;
4394
Li Zefane8d55fd2008-04-29 01:00:13 -07004395 /* At system boot, before all subsystems have been
4396 * registered, no tasks have been forked, so we don't
4397 * need to invoke fork callbacks here. */
4398 BUG_ON(!list_empty(&init_task.tasks));
4399
Tejun Heob1929db2012-11-19 08:13:38 -08004400 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004401
Tejun Heo648bb562012-11-19 08:13:36 -08004402 mutex_unlock(&cgroup_mutex);
4403
Ben Blume6a11052010-03-10 15:22:09 -08004404 /* this function shouldn't be used with modular subsystems, since they
4405 * need to register a subsys_id, among other things */
4406 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407}
4408
4409/**
Ben Blume6a11052010-03-10 15:22:09 -08004410 * cgroup_load_subsys: load and register a modular subsystem at runtime
4411 * @ss: the subsystem to load
4412 *
4413 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004414 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004415 * up for use. If the subsystem is built-in anyway, work is delegated to the
4416 * simpler cgroup_init_subsys.
4417 */
4418int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4419{
Ben Blume6a11052010-03-10 15:22:09 -08004420 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004421 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004422 struct hlist_node *tmp;
Li Zefan0ac801f2013-01-10 11:49:27 +08004423 struct css_set *cg;
4424 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004425
4426 /* check name and function validity */
4427 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004428 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004429 return -EINVAL;
4430
4431 /*
4432 * we don't support callbacks in modular subsystems. this check is
4433 * before the ss->module check for consistency; a subsystem that could
4434 * be a module should still have no callbacks even if the user isn't
4435 * compiling it as one.
4436 */
4437 if (ss->fork || ss->exit)
4438 return -EINVAL;
4439
4440 /*
4441 * an optionally modular subsystem is built-in: we want to do nothing,
4442 * since cgroup_init_subsys will have already taken care of it.
4443 */
4444 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004445 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004446 BUG_ON(subsys[ss->subsys_id] != ss);
4447 return 0;
4448 }
4449
Tejun Heo8e3f6542012-04-01 12:09:55 -07004450 /* init base cftset */
4451 cgroup_init_cftsets(ss);
4452
Ben Blume6a11052010-03-10 15:22:09 -08004453 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004454 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004455
4456 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004457 * no ss->css_alloc seems to need anything important in the ss
4458 * struct, so this can happen first (i.e. before the rootnode
4459 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004460 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004461 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004462 if (IS_ERR(css)) {
4463 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004464 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004465 mutex_unlock(&cgroup_mutex);
4466 return PTR_ERR(css);
4467 }
4468
4469 list_add(&ss->sibling, &rootnode.subsys_list);
4470 ss->root = &rootnode;
4471
4472 /* our new subsystem will be attached to the dummy hierarchy. */
4473 init_cgroup_css(css, ss, dummytop);
4474 /* init_idr must be after init_cgroup_css because it sets css->id. */
4475 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004476 ret = cgroup_init_idr(ss, css);
4477 if (ret)
4478 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004479 }
4480
4481 /*
4482 * Now we need to entangle the css into the existing css_sets. unlike
4483 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4484 * will need a new pointer to it; done by iterating the css_set_table.
4485 * furthermore, modifying the existing css_sets will corrupt the hash
4486 * table state, so each changed css_set will need its hash recomputed.
4487 * this is all done under the css_set_lock.
4488 */
4489 write_lock(&css_set_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004490 hash_for_each_safe(css_set_table, i, tmp, cg, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004491 /* skip entries that we already rehashed */
4492 if (cg->subsys[ss->subsys_id])
4493 continue;
4494 /* remove existing entry */
4495 hash_del(&cg->hlist);
4496 /* set new value */
4497 cg->subsys[ss->subsys_id] = css;
4498 /* recompute hash and restore entry */
4499 key = css_set_hash(cg->subsys);
Sasha Levinb67bfe02013-02-27 17:06:00 -08004500 hash_add(css_set_table, &cg->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004501 }
4502 write_unlock(&css_set_lock);
4503
Tejun Heob1929db2012-11-19 08:13:38 -08004504 ret = online_css(ss, dummytop);
4505 if (ret)
4506 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004507
Ben Blume6a11052010-03-10 15:22:09 -08004508 /* success! */
4509 mutex_unlock(&cgroup_mutex);
4510 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004511
4512err_unload:
4513 mutex_unlock(&cgroup_mutex);
4514 /* @ss can't be mounted here as try_module_get() would fail */
4515 cgroup_unload_subsys(ss);
4516 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004517}
4518EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4519
4520/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004521 * cgroup_unload_subsys: unload a modular subsystem
4522 * @ss: the subsystem to unload
4523 *
4524 * This function should be called in a modular subsystem's exitcall. When this
4525 * function is invoked, the refcount on the subsystem's module will be 0, so
4526 * the subsystem will not be attached to any hierarchy.
4527 */
4528void cgroup_unload_subsys(struct cgroup_subsys *ss)
4529{
4530 struct cg_cgroup_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004531
4532 BUG_ON(ss->module == NULL);
4533
4534 /*
4535 * we shouldn't be called if the subsystem is in use, and the use of
4536 * try_module_get in parse_cgroupfs_options should ensure that it
4537 * doesn't start being used while we're killing it off.
4538 */
4539 BUG_ON(ss->root != &rootnode);
4540
4541 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004542
Tejun Heoa31f2d32012-11-19 08:13:37 -08004543 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004544
Tejun Heoc897ff62013-02-27 17:03:49 -08004545 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004546 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004547
Ben Blumcf5d5942010-03-10 15:22:09 -08004548 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004549 subsys[ss->subsys_id] = NULL;
4550
4551 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004552 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004553
4554 /*
4555 * disentangle the css from all css_sets attached to the dummytop. as
4556 * in loading, we need to pay our respects to the hashtable gods.
4557 */
4558 write_lock(&css_set_lock);
4559 list_for_each_entry(link, &dummytop->css_sets, cgrp_link_list) {
4560 struct css_set *cg = link->cg;
Li Zefan0ac801f2013-01-10 11:49:27 +08004561 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004562
Li Zefan0ac801f2013-01-10 11:49:27 +08004563 hash_del(&cg->hlist);
Ben Blumcf5d5942010-03-10 15:22:09 -08004564 cg->subsys[ss->subsys_id] = NULL;
Li Zefan0ac801f2013-01-10 11:49:27 +08004565 key = css_set_hash(cg->subsys);
4566 hash_add(css_set_table, &cg->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004567 }
4568 write_unlock(&css_set_lock);
4569
4570 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004571 * remove subsystem's css from the dummytop and free it - need to
4572 * free before marking as null because ss->css_free needs the
4573 * cgrp->subsys pointer to find their state. note that this also
4574 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004575 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004576 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004577 dummytop->subsys[ss->subsys_id] = NULL;
4578
4579 mutex_unlock(&cgroup_mutex);
4580}
4581EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4582
4583/**
Li Zefana043e3b2008-02-23 15:24:09 -08004584 * cgroup_init_early - cgroup initialization at system boot
4585 *
4586 * Initialize cgroups at system boot, and initialize any
4587 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004588 */
4589int __init cgroup_init_early(void)
4590{
4591 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004592 atomic_set(&init_css_set.refcount, 1);
Paul Menage817929e2007-10-18 23:39:36 -07004593 INIT_LIST_HEAD(&init_css_set.cg_links);
4594 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004595 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004596 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004597 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004598 root_count = 1;
4599 init_task.cgroups = &init_css_set;
4600
4601 init_css_set_link.cg = &init_css_set;
Paul Menage7717f7b2009-09-23 15:56:22 -07004602 init_css_set_link.cgrp = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07004603 list_add(&init_css_set_link.cgrp_link_list,
Paul Menage817929e2007-10-18 23:39:36 -07004604 &rootnode.top_cgroup.css_sets);
4605 list_add(&init_css_set_link.cg_link_list,
4606 &init_css_set.cg_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004608 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609 struct cgroup_subsys *ss = subsys[i];
4610
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004611 /* at bootup time, we don't worry about modular subsystems */
4612 if (!ss || ss->module)
4613 continue;
4614
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615 BUG_ON(!ss->name);
4616 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004617 BUG_ON(!ss->css_alloc);
4618 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004620 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621 ss->name, ss->subsys_id);
4622 BUG();
4623 }
4624
4625 if (ss->early_init)
4626 cgroup_init_subsys(ss);
4627 }
4628 return 0;
4629}
4630
4631/**
Li Zefana043e3b2008-02-23 15:24:09 -08004632 * cgroup_init - cgroup initialization
4633 *
4634 * Register cgroup filesystem and /proc file, and initialize
4635 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636 */
4637int __init cgroup_init(void)
4638{
4639 int err;
4640 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004641 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004642
4643 err = bdi_init(&cgroup_backing_dev_info);
4644 if (err)
4645 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004647 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004648 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004649
4650 /* at bootup time, we don't worry about modular subsystems */
4651 if (!ss || ss->module)
4652 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653 if (!ss->early_init)
4654 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004655 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004656 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657 }
4658
Li Zefan472b1052008-04-29 01:00:11 -07004659 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004660 key = css_set_hash(init_css_set.subsys);
4661 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004662
4663 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004664 mutex_lock(&cgroup_mutex);
4665 mutex_lock(&cgroup_root_mutex);
4666
Tejun Heofa3ca072013-04-14 11:36:56 -07004667 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004668
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004669 mutex_unlock(&cgroup_root_mutex);
4670 mutex_unlock(&cgroup_mutex);
4671
Greg KH676db4a2010-08-05 13:53:35 -07004672 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4673 if (!cgroup_kobj) {
4674 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004676 }
4677
4678 err = register_filesystem(&cgroup_fs_type);
4679 if (err < 0) {
4680 kobject_put(cgroup_kobj);
4681 goto out;
4682 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683
Li Zefan46ae2202008-04-29 01:00:08 -07004684 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004685
Paul Menageddbcc7e2007-10-18 23:39:30 -07004686out:
Paul Menagea4243162007-10-18 23:39:35 -07004687 if (err)
4688 bdi_destroy(&cgroup_backing_dev_info);
4689
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690 return err;
4691}
Paul Menageb4f48b62007-10-18 23:39:33 -07004692
Paul Menagea4243162007-10-18 23:39:35 -07004693/*
4694 * proc_cgroup_show()
4695 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4696 * - Used for /proc/<pid>/cgroup.
4697 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4698 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004699 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004700 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4701 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4702 * cgroup to top_cgroup.
4703 */
4704
4705/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004706int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004707{
4708 struct pid *pid;
4709 struct task_struct *tsk;
4710 char *buf;
4711 int retval;
4712 struct cgroupfs_root *root;
4713
4714 retval = -ENOMEM;
4715 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4716 if (!buf)
4717 goto out;
4718
4719 retval = -ESRCH;
4720 pid = m->private;
4721 tsk = get_pid_task(pid, PIDTYPE_PID);
4722 if (!tsk)
4723 goto out_free;
4724
4725 retval = 0;
4726
4727 mutex_lock(&cgroup_mutex);
4728
Li Zefane5f6a862009-01-07 18:07:41 -08004729 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004730 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004731 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004732 int count = 0;
4733
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004734 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004735 for_each_subsys(root, ss)
4736 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004737 if (strlen(root->name))
4738 seq_printf(m, "%sname=%s", count ? "," : "",
4739 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004740 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004741 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004742 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004743 if (retval < 0)
4744 goto out_unlock;
4745 seq_puts(m, buf);
4746 seq_putc(m, '\n');
4747 }
4748
4749out_unlock:
4750 mutex_unlock(&cgroup_mutex);
4751 put_task_struct(tsk);
4752out_free:
4753 kfree(buf);
4754out:
4755 return retval;
4756}
4757
Paul Menagea4243162007-10-18 23:39:35 -07004758/* Display information about each subsystem and each hierarchy */
4759static int proc_cgroupstats_show(struct seq_file *m, void *v)
4760{
4761 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004762
Paul Menage8bab8dd2008-04-04 14:29:57 -07004763 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004764 /*
4765 * ideally we don't want subsystems moving around while we do this.
4766 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4767 * subsys/hierarchy state.
4768 */
Paul Menagea4243162007-10-18 23:39:35 -07004769 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004770 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4771 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004772 if (ss == NULL)
4773 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004774 seq_printf(m, "%s\t%d\t%d\t%d\n",
4775 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004776 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004777 }
4778 mutex_unlock(&cgroup_mutex);
4779 return 0;
4780}
4781
4782static int cgroupstats_open(struct inode *inode, struct file *file)
4783{
Al Viro9dce07f2008-03-29 03:07:28 +00004784 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004785}
4786
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004787static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004788 .open = cgroupstats_open,
4789 .read = seq_read,
4790 .llseek = seq_lseek,
4791 .release = single_release,
4792};
4793
Paul Menageb4f48b62007-10-18 23:39:33 -07004794/**
4795 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004796 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004797 *
4798 * Description: A task inherits its parent's cgroup at fork().
4799 *
4800 * A pointer to the shared css_set was automatically copied in
4801 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004802 * it was not made under the protection of RCU or cgroup_mutex, so
4803 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4804 * have already changed current->cgroups, allowing the previously
4805 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004806 *
4807 * At the point that cgroup_fork() is called, 'current' is the parent
4808 * task, and the passed argument 'child' points to the child task.
4809 */
4810void cgroup_fork(struct task_struct *child)
4811{
Tejun Heo9bb71302012-10-18 17:52:07 -07004812 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004813 child->cgroups = current->cgroups;
4814 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004815 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004816 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004817}
4818
4819/**
Li Zefana043e3b2008-02-23 15:24:09 -08004820 * cgroup_post_fork - called on a new task after adding it to the task list
4821 * @child: the task in question
4822 *
Tejun Heo5edee612012-10-16 15:03:14 -07004823 * Adds the task to the list running through its css_set if necessary and
4824 * call the subsystem fork() callbacks. Has to be after the task is
4825 * visible on the task list in case we race with the first call to
4826 * cgroup_iter_start() - to guarantee that the new task ends up on its
4827 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004828 */
Paul Menage817929e2007-10-18 23:39:36 -07004829void cgroup_post_fork(struct task_struct *child)
4830{
Tejun Heo5edee612012-10-16 15:03:14 -07004831 int i;
4832
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004833 /*
4834 * use_task_css_set_links is set to 1 before we walk the tasklist
4835 * under the tasklist_lock and we read it here after we added the child
4836 * to the tasklist under the tasklist_lock as well. If the child wasn't
4837 * yet in the tasklist when we walked through it from
4838 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4839 * should be visible now due to the paired locking and barriers implied
4840 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4841 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4842 * lock on fork.
4843 */
Paul Menage817929e2007-10-18 23:39:36 -07004844 if (use_task_css_set_links) {
4845 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004846 task_lock(child);
4847 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004848 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004849 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004850 write_unlock(&css_set_lock);
4851 }
Tejun Heo5edee612012-10-16 15:03:14 -07004852
4853 /*
4854 * Call ss->fork(). This must happen after @child is linked on
4855 * css_set; otherwise, @child might change state between ->fork()
4856 * and addition to css_set.
4857 */
4858 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004859 /*
4860 * fork/exit callbacks are supported only for builtin
4861 * subsystems, and the builtin section of the subsys
4862 * array is immutable, so we don't need to lock the
4863 * subsys array here. On the other hand, modular section
4864 * of the array can be freed at module unload, so we
4865 * can't touch that.
4866 */
4867 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004868 struct cgroup_subsys *ss = subsys[i];
4869
Tejun Heo5edee612012-10-16 15:03:14 -07004870 if (ss->fork)
4871 ss->fork(child);
4872 }
4873 }
Paul Menage817929e2007-10-18 23:39:36 -07004874}
Tejun Heo5edee612012-10-16 15:03:14 -07004875
Paul Menage817929e2007-10-18 23:39:36 -07004876/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004877 * cgroup_exit - detach cgroup from exiting task
4878 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004879 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004880 *
4881 * Description: Detach cgroup from @tsk and release it.
4882 *
4883 * Note that cgroups marked notify_on_release force every task in
4884 * them to take the global cgroup_mutex mutex when exiting.
4885 * This could impact scaling on very large systems. Be reluctant to
4886 * use notify_on_release cgroups where very high task exit scaling
4887 * is required on large systems.
4888 *
4889 * the_top_cgroup_hack:
4890 *
4891 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4892 *
4893 * We call cgroup_exit() while the task is still competent to
4894 * handle notify_on_release(), then leave the task attached to the
4895 * root cgroup in each hierarchy for the remainder of its exit.
4896 *
4897 * To do this properly, we would increment the reference count on
4898 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4899 * code we would add a second cgroup function call, to drop that
4900 * reference. This would just create an unnecessary hot spot on
4901 * the top_cgroup reference count, to no avail.
4902 *
4903 * Normally, holding a reference to a cgroup without bumping its
4904 * count is unsafe. The cgroup could go away, or someone could
4905 * attach us to a different cgroup, decrementing the count on
4906 * the first cgroup that we never incremented. But in this case,
4907 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004908 * which wards off any cgroup_attach_task() attempts, or task is a failed
4909 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07004910 */
4911void cgroup_exit(struct task_struct *tsk, int run_callbacks)
4912{
Paul Menage817929e2007-10-18 23:39:36 -07004913 struct css_set *cg;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004914 int i;
Paul Menage817929e2007-10-18 23:39:36 -07004915
4916 /*
4917 * Unlink from the css_set task list if necessary.
4918 * Optimistically check cg_list before taking
4919 * css_set_lock
4920 */
4921 if (!list_empty(&tsk->cg_list)) {
4922 write_lock(&css_set_lock);
4923 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004924 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07004925 write_unlock(&css_set_lock);
4926 }
4927
Paul Menageb4f48b62007-10-18 23:39:33 -07004928 /* Reassign the task to the init_css_set. */
4929 task_lock(tsk);
Paul Menage817929e2007-10-18 23:39:36 -07004930 cg = tsk->cgroups;
4931 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004932
4933 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004934 /*
4935 * fork/exit callbacks are supported only for builtin
4936 * subsystems, see cgroup_post_fork() for details.
4937 */
4938 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004939 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004940
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004941 if (ss->exit) {
4942 struct cgroup *old_cgrp =
4943 rcu_dereference_raw(cg->subsys[i])->cgroup;
4944 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08004945 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004946 }
4947 }
4948 }
Paul Menageb4f48b62007-10-18 23:39:33 -07004949 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01004950
Li Zefanb5d646f2013-01-24 14:43:51 +08004951 put_css_set_taskexit(cg);
Paul Menageb4f48b62007-10-18 23:39:33 -07004952}
Paul Menage697f4162007-10-18 23:39:34 -07004953
Paul Menagebd89aab2007-10-18 23:40:44 -07004954static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004955{
4956 /* All of these checks rely on RCU to keep the cgroup
4957 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08004958 if (cgroup_is_releasable(cgrp) &&
4959 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
4960 /*
4961 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07004962 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08004963 * it now
4964 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07004965 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08004966
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004967 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004968 if (!cgroup_is_removed(cgrp) &&
4969 list_empty(&cgrp->release_list)) {
4970 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004971 need_schedule_work = 1;
4972 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02004973 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004974 if (need_schedule_work)
4975 schedule_work(&release_agent_work);
4976 }
4977}
4978
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08004979/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07004980bool __css_tryget(struct cgroup_subsys_state *css)
4981{
Tejun Heoe9316082012-11-05 09:16:58 -08004982 while (true) {
4983 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07004984
Tejun Heoe9316082012-11-05 09:16:58 -08004985 v = css_refcnt(css);
4986 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
4987 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07004988 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08004989 else if (t < 0)
4990 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07004991 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08004992 }
Tejun Heo28b4c272012-04-01 12:09:56 -07004993}
4994EXPORT_SYMBOL_GPL(__css_tryget);
4995
4996/* Caller must verify that the css is not for root cgroup */
4997void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07004998{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07004999 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005000
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005001 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005002 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005003 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005004}
Ben Blum67523c42010-03-10 15:22:11 -08005005EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005006
5007/*
5008 * Notify userspace when a cgroup is released, by running the
5009 * configured release agent with the name of the cgroup (path
5010 * relative to the root of cgroup file system) as the argument.
5011 *
5012 * Most likely, this user command will try to rmdir this cgroup.
5013 *
5014 * This races with the possibility that some other task will be
5015 * attached to this cgroup before it is removed, or that some other
5016 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5017 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5018 * unused, and this cgroup will be reprieved from its death sentence,
5019 * to continue to serve a useful existence. Next time it's released,
5020 * we will get notified again, if it still has 'notify_on_release' set.
5021 *
5022 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5023 * means only wait until the task is successfully execve()'d. The
5024 * separate release agent task is forked by call_usermodehelper(),
5025 * then control in this thread returns here, without waiting for the
5026 * release agent task. We don't bother to wait because the caller of
5027 * this routine has no use for the exit status of the release agent
5028 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005029 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005030static void cgroup_release_agent(struct work_struct *work)
5031{
5032 BUG_ON(work != &release_agent_work);
5033 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005034 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005035 while (!list_empty(&release_list)) {
5036 char *argv[3], *envp[3];
5037 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005038 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005039 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005040 struct cgroup,
5041 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005042 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005043 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005044 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005045 if (!pathbuf)
5046 goto continue_free;
5047 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5048 goto continue_free;
5049 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5050 if (!agentbuf)
5051 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005052
5053 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005054 argv[i++] = agentbuf;
5055 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005056 argv[i] = NULL;
5057
5058 i = 0;
5059 /* minimal command environment */
5060 envp[i++] = "HOME=/";
5061 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5062 envp[i] = NULL;
5063
5064 /* Drop the lock while we invoke the usermode helper,
5065 * since the exec could involve hitting disk and hence
5066 * be a slow process */
5067 mutex_unlock(&cgroup_mutex);
5068 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005070 continue_free:
5071 kfree(pathbuf);
5072 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005073 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005074 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005075 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 mutex_unlock(&cgroup_mutex);
5077}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005078
5079static int __init cgroup_disable(char *str)
5080{
5081 int i;
5082 char *token;
5083
5084 while ((token = strsep(&str, ",")) != NULL) {
5085 if (!*token)
5086 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005087 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005088 struct cgroup_subsys *ss = subsys[i];
5089
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005090 /*
5091 * cgroup_disable, being at boot time, can't
5092 * know about module subsystems, so we don't
5093 * worry about them.
5094 */
5095 if (!ss || ss->module)
5096 continue;
5097
Paul Menage8bab8dd2008-04-04 14:29:57 -07005098 if (!strcmp(token, ss->name)) {
5099 ss->disabled = 1;
5100 printk(KERN_INFO "Disabling %s control group"
5101 " subsystem\n", ss->name);
5102 break;
5103 }
5104 }
5105 }
5106 return 1;
5107}
5108__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005109
5110/*
5111 * Functons for CSS ID.
5112 */
5113
5114/*
5115 *To get ID other than 0, this should be called when !cgroup_is_removed().
5116 */
5117unsigned short css_id(struct cgroup_subsys_state *css)
5118{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005119 struct css_id *cssid;
5120
5121 /*
5122 * This css_id() can return correct value when somone has refcnt
5123 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5124 * it's unchanged until freed.
5125 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005126 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005127
5128 if (cssid)
5129 return cssid->id;
5130 return 0;
5131}
Ben Blum67523c42010-03-10 15:22:11 -08005132EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005133
5134unsigned short css_depth(struct cgroup_subsys_state *css)
5135{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005136 struct css_id *cssid;
5137
Tejun Heo28b4c272012-04-01 12:09:56 -07005138 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005139
5140 if (cssid)
5141 return cssid->depth;
5142 return 0;
5143}
Ben Blum67523c42010-03-10 15:22:11 -08005144EXPORT_SYMBOL_GPL(css_depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005145
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005146/**
5147 * css_is_ancestor - test "root" css is an ancestor of "child"
5148 * @child: the css to be tested.
5149 * @root: the css supporsed to be an ancestor of the child.
5150 *
5151 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005152 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005153 * But, considering usual usage, the csses should be valid objects after test.
5154 * Assuming that the caller will do some action to the child if this returns
5155 * returns true, the caller must take "child";s reference count.
5156 * If "child" is valid object and this returns true, "root" is valid, too.
5157 */
5158
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005159bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005160 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005161{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005162 struct css_id *child_id;
5163 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005164
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005165 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005166 if (!child_id)
5167 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005168 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005169 if (!root_id)
5170 return false;
5171 if (child_id->depth < root_id->depth)
5172 return false;
5173 if (child_id->stack[root_id->depth] != root_id->id)
5174 return false;
5175 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005176}
5177
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005178void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5179{
5180 struct css_id *id = css->id;
5181 /* When this is called before css_id initialization, id can be NULL */
5182 if (!id)
5183 return;
5184
5185 BUG_ON(!ss->use_id);
5186
5187 rcu_assign_pointer(id->css, NULL);
5188 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005189 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005190 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005191 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005192 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005193}
Ben Blum67523c42010-03-10 15:22:11 -08005194EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005195
5196/*
5197 * This is called by init or create(). Then, calls to this function are
5198 * always serialized (By cgroup_mutex() at create()).
5199 */
5200
5201static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5202{
5203 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005204 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005205
5206 BUG_ON(!ss->use_id);
5207
5208 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5209 newid = kzalloc(size, GFP_KERNEL);
5210 if (!newid)
5211 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005212
5213 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005214 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005215 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005216 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005217 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005218 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005219
5220 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005221 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005222 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005223
Tejun Heod228d9e2013-02-27 17:04:54 -08005224 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225 newid->depth = depth;
5226 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005227err_out:
5228 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005229 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230
5231}
5232
Ben Blume6a11052010-03-10 15:22:09 -08005233static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5234 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005235{
5236 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005237
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005238 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005239 idr_init(&ss->idr);
5240
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005241 newid = get_new_cssid(ss, 0);
5242 if (IS_ERR(newid))
5243 return PTR_ERR(newid);
5244
5245 newid->stack[0] = newid->id;
5246 newid->css = rootcss;
5247 rootcss->id = newid;
5248 return 0;
5249}
5250
5251static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5252 struct cgroup *child)
5253{
5254 int subsys_id, i, depth = 0;
5255 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005256 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005257
5258 subsys_id = ss->subsys_id;
5259 parent_css = parent->subsys[subsys_id];
5260 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005261 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005262 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005263
5264 child_id = get_new_cssid(ss, depth);
5265 if (IS_ERR(child_id))
5266 return PTR_ERR(child_id);
5267
5268 for (i = 0; i < depth; i++)
5269 child_id->stack[i] = parent_id->stack[i];
5270 child_id->stack[depth] = child_id->id;
5271 /*
5272 * child_id->css pointer will be set after this cgroup is available
5273 * see cgroup_populate_dir()
5274 */
5275 rcu_assign_pointer(child_css->id, child_id);
5276
5277 return 0;
5278}
5279
5280/**
5281 * css_lookup - lookup css by id
5282 * @ss: cgroup subsys to be looked into.
5283 * @id: the id
5284 *
5285 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5286 * NULL if not. Should be called under rcu_read_lock()
5287 */
5288struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5289{
5290 struct css_id *cssid = NULL;
5291
5292 BUG_ON(!ss->use_id);
5293 cssid = idr_find(&ss->idr, id);
5294
5295 if (unlikely(!cssid))
5296 return NULL;
5297
5298 return rcu_dereference(cssid->css);
5299}
Ben Blum67523c42010-03-10 15:22:11 -08005300EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005301
Stephane Eraniane5d13672011-02-14 11:20:01 +02005302/*
5303 * get corresponding css from file open on cgroupfs directory
5304 */
5305struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5306{
5307 struct cgroup *cgrp;
5308 struct inode *inode;
5309 struct cgroup_subsys_state *css;
5310
Al Viro496ad9a2013-01-23 17:07:38 -05005311 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005312 /* check in cgroup filesystem dir */
5313 if (inode->i_op != &cgroup_dir_inode_operations)
5314 return ERR_PTR(-EBADF);
5315
5316 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5317 return ERR_PTR(-EINVAL);
5318
5319 /* get cgroup */
5320 cgrp = __d_cgrp(f->f_dentry);
5321 css = cgrp->subsys[id];
5322 return css ? css : ERR_PTR(-ENOENT);
5323}
5324
Paul Menagefe693432009-09-23 15:56:20 -07005325#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005326static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005327{
5328 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5329
5330 if (!css)
5331 return ERR_PTR(-ENOMEM);
5332
5333 return css;
5334}
5335
Tejun Heo92fb9742012-11-19 08:13:38 -08005336static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005337{
5338 kfree(cont->subsys[debug_subsys_id]);
5339}
5340
5341static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5342{
5343 return atomic_read(&cont->count);
5344}
5345
5346static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5347{
5348 return cgroup_task_count(cont);
5349}
5350
5351static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5352{
5353 return (u64)(unsigned long)current->cgroups;
5354}
5355
5356static u64 current_css_set_refcount_read(struct cgroup *cont,
5357 struct cftype *cft)
5358{
5359 u64 count;
5360
5361 rcu_read_lock();
5362 count = atomic_read(&current->cgroups->refcount);
5363 rcu_read_unlock();
5364 return count;
5365}
5366
Paul Menage7717f7b2009-09-23 15:56:22 -07005367static int current_css_set_cg_links_read(struct cgroup *cont,
5368 struct cftype *cft,
5369 struct seq_file *seq)
5370{
5371 struct cg_cgroup_link *link;
5372 struct css_set *cg;
5373
5374 read_lock(&css_set_lock);
5375 rcu_read_lock();
5376 cg = rcu_dereference(current->cgroups);
5377 list_for_each_entry(link, &cg->cg_links, cg_link_list) {
5378 struct cgroup *c = link->cgrp;
5379 const char *name;
5380
5381 if (c->dentry)
5382 name = c->dentry->d_name.name;
5383 else
5384 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005385 seq_printf(seq, "Root %d group %s\n",
5386 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005387 }
5388 rcu_read_unlock();
5389 read_unlock(&css_set_lock);
5390 return 0;
5391}
5392
5393#define MAX_TASKS_SHOWN_PER_CSS 25
5394static int cgroup_css_links_read(struct cgroup *cont,
5395 struct cftype *cft,
5396 struct seq_file *seq)
5397{
5398 struct cg_cgroup_link *link;
5399
5400 read_lock(&css_set_lock);
5401 list_for_each_entry(link, &cont->css_sets, cgrp_link_list) {
5402 struct css_set *cg = link->cg;
5403 struct task_struct *task;
5404 int count = 0;
5405 seq_printf(seq, "css_set %p\n", cg);
5406 list_for_each_entry(task, &cg->tasks, cg_list) {
5407 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5408 seq_puts(seq, " ...\n");
5409 break;
5410 } else {
5411 seq_printf(seq, " task %d\n",
5412 task_pid_vnr(task));
5413 }
5414 }
5415 }
5416 read_unlock(&css_set_lock);
5417 return 0;
5418}
5419
Paul Menagefe693432009-09-23 15:56:20 -07005420static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5421{
5422 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5423}
5424
5425static struct cftype debug_files[] = {
5426 {
5427 .name = "cgroup_refcount",
5428 .read_u64 = cgroup_refcount_read,
5429 },
5430 {
5431 .name = "taskcount",
5432 .read_u64 = debug_taskcount_read,
5433 },
5434
5435 {
5436 .name = "current_css_set",
5437 .read_u64 = current_css_set_read,
5438 },
5439
5440 {
5441 .name = "current_css_set_refcount",
5442 .read_u64 = current_css_set_refcount_read,
5443 },
5444
5445 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005446 .name = "current_css_set_cg_links",
5447 .read_seq_string = current_css_set_cg_links_read,
5448 },
5449
5450 {
5451 .name = "cgroup_css_links",
5452 .read_seq_string = cgroup_css_links_read,
5453 },
5454
5455 {
Paul Menagefe693432009-09-23 15:56:20 -07005456 .name = "releasable",
5457 .read_u64 = releasable_read,
5458 },
Paul Menagefe693432009-09-23 15:56:20 -07005459
Tejun Heo4baf6e32012-04-01 12:09:55 -07005460 { } /* terminate */
5461};
Paul Menagefe693432009-09-23 15:56:20 -07005462
5463struct cgroup_subsys debug_subsys = {
5464 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005465 .css_alloc = debug_css_alloc,
5466 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005467 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005468 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005469};
5470#endif /* CONFIG_CGROUP_DEBUG */