blob: 7db2940bfc77b79c1ba78f150f18db294719ebe2 [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 Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -080099static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
104 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
105 * subsystems that are otherwise unattached - it never has more than a
106 * single cgroup, and all tasks are part of that cgroup.
107 */
108static struct cgroupfs_root rootnode;
109
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700110/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700111 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
112 */
113struct cfent {
114 struct list_head node;
115 struct dentry *dentry;
116 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700117
118 /* file xattrs */
119 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700120};
121
122/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700123 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
124 * cgroup_subsys->use_id != 0.
125 */
126#define CSS_ID_MAX (65535)
127struct css_id {
128 /*
129 * The css to which this ID points. This pointer is set to valid value
130 * after cgroup is populated. If cgroup is removed, this will be NULL.
131 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800132 * is called after synchronize_rcu(). But for safe use, css_tryget()
133 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700134 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100135 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700136 /*
137 * ID of this css.
138 */
139 unsigned short id;
140 /*
141 * Depth in hierarchy which this ID belongs to.
142 */
143 unsigned short depth;
144 /*
145 * ID is freed by RCU. (and lookup routine is RCU safe.)
146 */
147 struct rcu_head rcu_head;
148 /*
149 * Hierarchy of CSS ID belongs to.
150 */
151 unsigned short stack[0]; /* Array of Length (depth+1) */
152};
153
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800154/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300155 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800156 */
157struct cgroup_event {
158 /*
159 * Cgroup which the event belongs to.
160 */
161 struct cgroup *cgrp;
162 /*
163 * Control file which the event associated.
164 */
165 struct cftype *cft;
166 /*
167 * eventfd to signal userspace about the event.
168 */
169 struct eventfd_ctx *eventfd;
170 /*
171 * Each of these stored in a list by the cgroup.
172 */
173 struct list_head list;
174 /*
175 * All fields below needed to unregister event when
176 * userspace closes eventfd.
177 */
178 poll_table pt;
179 wait_queue_head_t *wqh;
180 wait_queue_t wait;
181 struct work_struct remove;
182};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700183
Paul Menageddbcc7e2007-10-18 23:39:30 -0700184/* The list of hierarchy roots */
185
186static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700187static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700189/*
190 * Hierarchy ID allocation and mapping. It follows the same exclusion
191 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
192 * writes, either for reads.
193 */
Tejun Heo1a574232013-04-14 11:36:58 -0700194static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700195
Paul Menageddbcc7e2007-10-18 23:39:30 -0700196/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
197#define dummytop (&rootnode.top_cgroup)
198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Paul Menageddbcc7e2007-10-18 23:39:30 -0700201/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800202 * check for fork/exit handlers to call. This avoids us having to do
203 * extra work in the fork/exit path if none of the subsystems need to
204 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700205 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700206static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207
Tejun Heoea15f8c2013-06-13 19:27:42 -0700208static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800209static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800210static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
211 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800212
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700214static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215{
Tejun Heo54766d42013-06-12 21:04:53 -0700216 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217}
218
Li Zefan78574cf2013-04-08 19:00:38 -0700219/**
220 * cgroup_is_descendant - test ancestry
221 * @cgrp: the cgroup to be tested
222 * @ancestor: possible ancestor of @cgrp
223 *
224 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
225 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
226 * and @ancestor are accessible.
227 */
228bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
229{
230 while (cgrp) {
231 if (cgrp == ancestor)
232 return true;
233 cgrp = cgrp->parent;
234 }
235 return false;
236}
237EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238
Adrian Bunke9685a02008-02-07 00:13:46 -0800239static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700240{
241 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700242 (1 << CGRP_RELEASABLE) |
243 (1 << CGRP_NOTIFY_ON_RELEASE);
244 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700245}
246
Adrian Bunke9685a02008-02-07 00:13:46 -0800247static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700248{
Paul Menagebd89aab2007-10-18 23:40:44 -0700249 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700250}
251
Paul Menageddbcc7e2007-10-18 23:39:30 -0700252/*
253 * for_each_subsys() allows you to iterate on each subsystem attached to
254 * an active hierarchy
255 */
256#define for_each_subsys(_root, _ss) \
257list_for_each_entry(_ss, &_root->subsys_list, sibling)
258
Li Zefane5f6a862009-01-07 18:07:41 -0800259/* for_each_active_root() allows you to iterate across the active hierarchies */
260#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700261list_for_each_entry(_root, &roots, root_list)
262
Tejun Heof6ea9372012-04-01 12:09:55 -0700263static inline struct cgroup *__d_cgrp(struct dentry *dentry)
264{
265 return dentry->d_fsdata;
266}
267
Tejun Heo05ef1d72012-04-01 12:09:56 -0700268static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700269{
270 return dentry->d_fsdata;
271}
272
Tejun Heo05ef1d72012-04-01 12:09:56 -0700273static inline struct cftype *__d_cft(struct dentry *dentry)
274{
275 return __d_cfe(dentry)->type;
276}
277
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700278/**
279 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
280 * @cgrp: the cgroup to be checked for liveness
281 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700282 * On success, returns true; the mutex should be later unlocked. On
283 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700284 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700285static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700286{
287 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700288 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700289 mutex_unlock(&cgroup_mutex);
290 return false;
291 }
292 return true;
293}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294
Paul Menage81a6a5c2007-10-18 23:39:38 -0700295/* the list of cgroups eligible for automatic release. Protected by
296 * release_list_lock */
297static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200298static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700299static void cgroup_release_agent(struct work_struct *work);
300static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700301static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302
Tejun Heo69d02062013-06-12 21:04:50 -0700303/*
304 * A cgroup can be associated with multiple css_sets as different tasks may
305 * belong to different cgroups on different hierarchies. In the other
306 * direction, a css_set is naturally associated with multiple cgroups.
307 * This M:N relationship is represented by the following link structure
308 * which exists for each association and allows traversing the associations
309 * from both sides.
310 */
311struct cgrp_cset_link {
312 /* the cgroup and css_set this link associates */
313 struct cgroup *cgrp;
314 struct css_set *cset;
315
316 /* list of cgrp_cset_links anchored at cgrp->cset_links */
317 struct list_head cset_link;
318
319 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
320 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700321};
322
323/* The default css_set - used by init and its children prior to any
324 * hierarchies being mounted. It contains a pointer to the root state
325 * for each subsystem. Also used to anchor the list of css_sets. Not
326 * reference-counted, to improve performance when child cgroups
327 * haven't been created.
328 */
329
330static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700331static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700332
Ben Blume6a11052010-03-10 15:22:09 -0800333static int cgroup_init_idr(struct cgroup_subsys *ss,
334 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700335
Paul Menage817929e2007-10-18 23:39:36 -0700336/* css_set_lock protects the list of css_set objects, and the
337 * chain of tasks off each css_set. Nests outside task->alloc_lock
338 * due to cgroup_iter_start() */
339static DEFINE_RWLOCK(css_set_lock);
340static int css_set_count;
341
Paul Menage7717f7b2009-09-23 15:56:22 -0700342/*
343 * hash table for cgroup groups. This improves the performance to find
344 * an existing css_set. This hash doesn't (currently) take into
345 * account cgroups in empty hierarchies.
346 */
Li Zefan472b1052008-04-29 01:00:11 -0700347#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800348static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700349
Li Zefan0ac801f2013-01-10 11:49:27 +0800350static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700351{
352 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800353 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700354
355 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800356 key += (unsigned long)css[i];
357 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700358
Li Zefan0ac801f2013-01-10 11:49:27 +0800359 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700360}
361
Paul Menage817929e2007-10-18 23:39:36 -0700362/* We don't maintain the lists running through each css_set to its
363 * task until after the first call to cgroup_iter_start(). This
364 * reduces the fork()/exit() overhead for people who have cgroups
365 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700366static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700367
Tejun Heo5abb8852013-06-12 21:04:49 -0700368static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700369{
Tejun Heo69d02062013-06-12 21:04:50 -0700370 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700371
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700372 /*
373 * Ensure that the refcount doesn't hit zero while any readers
374 * can see it. Similar to atomic_dec_and_lock(), but for an
375 * rwlock
376 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700377 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700378 return;
379 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700380 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700381 write_unlock(&css_set_lock);
382 return;
383 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700384
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700385 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700386 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700387 css_set_count--;
388
Tejun Heo69d02062013-06-12 21:04:50 -0700389 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700390 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700391
Tejun Heo69d02062013-06-12 21:04:50 -0700392 list_del(&link->cset_link);
393 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800394
Tejun Heoddd69142013-06-12 21:04:54 -0700395 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700396 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700397 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700398 set_bit(CGRP_RELEASABLE, &cgrp->flags);
399 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700400 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700401
402 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700403 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700404
405 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700406 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700407}
408
409/*
410 * refcounted get/put for css_set objects
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700413{
Tejun Heo5abb8852013-06-12 21:04:49 -0700414 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700415}
416
Tejun Heo5abb8852013-06-12 21:04:49 -0700417static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700418{
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700420}
421
Tejun Heo5abb8852013-06-12 21:04:49 -0700422static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700423{
Tejun Heo5abb8852013-06-12 21:04:49 -0700424 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700425}
426
Paul Menage817929e2007-10-18 23:39:36 -0700427/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700428 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700429 * @cset: candidate css_set being tested
430 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700431 * @new_cgrp: cgroup that's being entered by the task
432 * @template: desired set of css pointers in css_set (pre-calculated)
433 *
434 * Returns true if "cg" matches "old_cg" except for the hierarchy
435 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
436 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700437static bool compare_css_sets(struct css_set *cset,
438 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700439 struct cgroup *new_cgrp,
440 struct cgroup_subsys_state *template[])
441{
442 struct list_head *l1, *l2;
443
Tejun Heo5abb8852013-06-12 21:04:49 -0700444 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700445 /* Not all subsystems matched */
446 return false;
447 }
448
449 /*
450 * Compare cgroup pointers in order to distinguish between
451 * different cgroups in heirarchies with no subsystems. We
452 * could get by with just this check alone (and skip the
453 * memcmp above) but on most setups the memcmp check will
454 * avoid the need for this more expensive check on almost all
455 * candidates.
456 */
457
Tejun Heo69d02062013-06-12 21:04:50 -0700458 l1 = &cset->cgrp_links;
459 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700461 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700462 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700463
464 l1 = l1->next;
465 l2 = l2->next;
466 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700467 if (l1 == &cset->cgrp_links) {
468 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700469 break;
470 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700471 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 }
473 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700474 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
475 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
476 cgrp1 = link1->cgrp;
477 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700480
481 /*
482 * If this hierarchy is the hierarchy of the cgroup
483 * that's changing, then we need to check that this
484 * css_set points to the new cgroup; if it's any other
485 * hierarchy, then this css_set should point to the
486 * same cgroup as the old css_set.
487 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700488 if (cgrp1->root == new_cgrp->root) {
489 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700490 return false;
491 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700492 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 return false;
494 }
495 }
496 return true;
497}
498
499/*
Paul Menage817929e2007-10-18 23:39:36 -0700500 * find_existing_css_set() is a helper for
501 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700502 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700503 *
504 * oldcg: the cgroup group that we're using before the cgroup
505 * transition
506 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700507 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700508 *
509 * template: location in which to build the desired set of subsystem
510 * state objects for the new cgroup group
511 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700512static struct css_set *find_existing_css_set(struct css_set *old_cset,
513 struct cgroup *cgrp,
514 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700515{
516 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700517 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700518 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800519 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700520
Ben Blumaae8aab2010-03-10 15:22:07 -0800521 /*
522 * Build the set of subsystem state objects that we want to see in the
523 * new css_set. while subsystems can change globally, the entries here
524 * won't change, so no need for locking.
525 */
Paul Menage817929e2007-10-18 23:39:36 -0700526 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400527 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700528 /* Subsystem is in this hierarchy. So we want
529 * the subsystem state from the new
530 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700531 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700532 } else {
533 /* Subsystem is not in this hierarchy, so we
534 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700535 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700536 }
537 }
538
Li Zefan0ac801f2013-01-10 11:49:27 +0800539 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 hash_for_each_possible(css_set_table, cset, hlist, key) {
541 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700542 continue;
543
544 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700546 }
Paul Menage817929e2007-10-18 23:39:36 -0700547
548 /* No existing cgroup group matched */
549 return NULL;
550}
551
Tejun Heo69d02062013-06-12 21:04:50 -0700552static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700553{
Tejun Heo69d02062013-06-12 21:04:50 -0700554 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700555
Tejun Heo69d02062013-06-12 21:04:50 -0700556 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
557 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700558 kfree(link);
559 }
560}
561
Tejun Heo69d02062013-06-12 21:04:50 -0700562/**
563 * allocate_cgrp_cset_links - allocate cgrp_cset_links
564 * @count: the number of links to allocate
565 * @tmp_links: list_head the allocated links are put on
566 *
567 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
568 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700569 */
Tejun Heo69d02062013-06-12 21:04:50 -0700570static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700571{
Tejun Heo69d02062013-06-12 21:04:50 -0700572 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700573 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700574
575 INIT_LIST_HEAD(tmp_links);
576
Li Zefan36553432008-07-29 22:33:19 -0700577 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700578 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700579 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700580 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700581 return -ENOMEM;
582 }
Tejun Heo69d02062013-06-12 21:04:50 -0700583 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700584 }
585 return 0;
586}
587
Li Zefanc12f65d2009-01-07 18:07:42 -0800588/**
589 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700590 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700591 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800592 * @cgrp: the destination cgroup
593 */
Tejun Heo69d02062013-06-12 21:04:50 -0700594static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
595 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800596{
Tejun Heo69d02062013-06-12 21:04:50 -0700597 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800598
Tejun Heo69d02062013-06-12 21:04:50 -0700599 BUG_ON(list_empty(tmp_links));
600 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
601 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700602 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700603 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700604 /*
605 * Always add links to the tail of the list so that the list
606 * is sorted by order of hierarchy creation
607 */
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800609}
610
Li Zefan36553432008-07-29 22:33:19 -0700611/*
Paul Menage817929e2007-10-18 23:39:36 -0700612 * find_css_set() takes an existing cgroup group and a
613 * cgroup object, and returns a css_set object that's
614 * equivalent to the old group, but with the given cgroup
615 * substituted into the appropriate hierarchy. Must be called with
616 * cgroup_mutex held
617 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700618static struct css_set *find_css_set(struct css_set *old_cset,
619 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700620{
Tejun Heo5abb8852013-06-12 21:04:49 -0700621 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700622 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700623 struct list_head tmp_links;
624 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800625 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700626
Paul Menage817929e2007-10-18 23:39:36 -0700627 /* First see if we already have a cgroup group that matches
628 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700629 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700630 cset = find_existing_css_set(old_cset, cgrp, template);
631 if (cset)
632 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700633 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700634
Tejun Heo5abb8852013-06-12 21:04:49 -0700635 if (cset)
636 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700637
Tejun Heof4f4be22013-06-12 21:04:51 -0700638 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700640 return NULL;
641
Tejun Heo69d02062013-06-12 21:04:50 -0700642 /* Allocate all the cgrp_cset_link objects that we'll need */
643 if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700645 return NULL;
646 }
647
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700649 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 INIT_LIST_HEAD(&cset->tasks);
651 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700652
653 /* Copy the set of subsystem state objects generated in
654 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700656
657 write_lock(&css_set_lock);
658 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700659 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700660 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700661
Paul Menage7717f7b2009-09-23 15:56:22 -0700662 if (c->root == cgrp->root)
663 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700664 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 }
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo69d02062013-06-12 21:04:50 -0700667 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700668
Paul Menage817929e2007-10-18 23:39:36 -0700669 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700670
671 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700672 key = css_set_hash(cset->subsys);
673 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700674
Paul Menage817929e2007-10-18 23:39:36 -0700675 write_unlock(&css_set_lock);
676
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700678}
679
Paul Menageddbcc7e2007-10-18 23:39:30 -0700680/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700681 * Return the cgroup for "task" from the given hierarchy. Must be
682 * called with cgroup_mutex held.
683 */
684static struct cgroup *task_cgroup_from_root(struct task_struct *task,
685 struct cgroupfs_root *root)
686{
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700688 struct cgroup *res = NULL;
689
690 BUG_ON(!mutex_is_locked(&cgroup_mutex));
691 read_lock(&css_set_lock);
692 /*
693 * No need to lock the task - since we hold cgroup_mutex the
694 * task can't change groups, so the only thing that can happen
695 * is that it exits and its css is set back to init_css_set.
696 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 cset = task->cgroups;
698 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700699 res = &root->top_cgroup;
700 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700701 struct cgrp_cset_link *link;
702
703 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700705
Paul Menage7717f7b2009-09-23 15:56:22 -0700706 if (c->root == root) {
707 res = c;
708 break;
709 }
710 }
711 }
712 read_unlock(&css_set_lock);
713 BUG_ON(!res);
714 return res;
715}
716
717/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700718 * There is one global cgroup mutex. We also require taking
719 * task_lock() when dereferencing a task's cgroup subsys pointers.
720 * See "The task_lock() exception", at the end of this comment.
721 *
722 * A task must hold cgroup_mutex to modify cgroups.
723 *
724 * Any task can increment and decrement the count field without lock.
725 * So in general, code holding cgroup_mutex can't rely on the count
726 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800727 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 * means that no tasks are currently attached, therefore there is no
729 * way a task attached to that cgroup can fork (the other way to
730 * increment the count). So code holding cgroup_mutex can safely
731 * assume that if the count is zero, it will stay zero. Similarly, if
732 * a task holds cgroup_mutex on a cgroup with zero count, it
733 * knows that the cgroup won't be removed, as cgroup_rmdir()
734 * needs that mutex.
735 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700736 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
737 * (usually) take cgroup_mutex. These are the two most performance
738 * critical pieces of code here. The exception occurs on cgroup_exit(),
739 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
740 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800741 * to the release agent with the name of the cgroup (path relative to
742 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743 *
744 * A cgroup can only be deleted if both its 'count' of using tasks
745 * is zero, and its list of 'children' cgroups is empty. Since all
746 * tasks in the system use _some_ cgroup, and since there is always at
747 * least one task in the system (init, pid == 1), therefore, top_cgroup
748 * always has either children cgroups and/or using tasks. So we don't
749 * need a special hack to ensure that top_cgroup cannot be deleted.
750 *
751 * The task_lock() exception
752 *
753 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800754 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800755 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 * several performance critical places that need to reference
757 * task->cgroup without the expense of grabbing a system global
758 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800759 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
761 * the task_struct routinely used for such matters.
762 *
763 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800764 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 */
766
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767/*
768 * A couple of forward declarations required, due to cyclic reference loop:
769 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
770 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
771 * -> cgroup_mkdir.
772 */
773
Al Viro18bb1db2011-07-26 01:41:39 -0400774static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400775static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400777static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
778 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700779static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700780static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700781
782static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200783 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700784 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700785};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700787static int alloc_css_id(struct cgroup_subsys *ss,
788 struct cgroup *parent, struct cgroup *child);
789
Al Viroa5e7ed32011-07-26 01:55:55 -0400790static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791{
792 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793
794 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400795 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100797 inode->i_uid = current_fsuid();
798 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
800 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
801 }
802 return inode;
803}
804
Li Zefan65dff752013-03-01 15:01:56 +0800805static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
806{
807 struct cgroup_name *name;
808
809 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
810 if (!name)
811 return NULL;
812 strcpy(name->name, dentry->d_name.name);
813 return name;
814}
815
Li Zefanbe445622013-01-24 14:31:42 +0800816static void cgroup_free_fn(struct work_struct *work)
817{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700818 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800819 struct cgroup_subsys *ss;
820
821 mutex_lock(&cgroup_mutex);
822 /*
823 * Release the subsystem state objects.
824 */
825 for_each_subsys(cgrp->root, ss)
826 ss->css_free(cgrp);
827
828 cgrp->root->number_of_cgroups--;
829 mutex_unlock(&cgroup_mutex);
830
831 /*
Li Zefan415cf072013-04-08 14:35:02 +0800832 * We get a ref to the parent's dentry, and put the ref when
833 * this cgroup is being freed, so it's guaranteed that the
834 * parent won't be destroyed before its children.
835 */
836 dput(cgrp->parent->dentry);
837
Li Zefancc20e012013-04-26 11:58:02 -0700838 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
839
Li Zefan415cf072013-04-08 14:35:02 +0800840 /*
Li Zefanbe445622013-01-24 14:31:42 +0800841 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700842 * created the cgroup. This will free cgrp->root, if we are
843 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800844 */
845 deactivate_super(cgrp->root->sb);
846
847 /*
848 * if we're getting rid of the cgroup, refcount should ensure
849 * that there are no pidlists left.
850 */
851 BUG_ON(!list_empty(&cgrp->pidlists));
852
853 simple_xattrs_free(&cgrp->xattrs);
854
Li Zefan65dff752013-03-01 15:01:56 +0800855 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800856 kfree(cgrp);
857}
858
859static void cgroup_free_rcu(struct rcu_head *head)
860{
861 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
862
Tejun Heoea15f8c2013-06-13 19:27:42 -0700863 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
864 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800865}
866
Paul Menageddbcc7e2007-10-18 23:39:30 -0700867static void cgroup_diput(struct dentry *dentry, struct inode *inode)
868{
869 /* is dentry a directory ? if so, kfree() associated cgroup */
870 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700871 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800872
Tejun Heo54766d42013-06-12 21:04:53 -0700873 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800874 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700875 } else {
876 struct cfent *cfe = __d_cfe(dentry);
877 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
878
879 WARN_ONCE(!list_empty(&cfe->node) &&
880 cgrp != &cgrp->root->top_cgroup,
881 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700882 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700883 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700884 }
885 iput(inode);
886}
887
Al Viroc72a04e2011-01-14 05:31:45 +0000888static int cgroup_delete(const struct dentry *d)
889{
890 return 1;
891}
892
Paul Menageddbcc7e2007-10-18 23:39:30 -0700893static void remove_dir(struct dentry *d)
894{
895 struct dentry *parent = dget(d->d_parent);
896
897 d_delete(d);
898 simple_rmdir(parent->d_inode, d);
899 dput(parent);
900}
901
Li Zefan2739d3c2013-01-21 18:18:33 +0800902static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700905
Tejun Heo05ef1d72012-04-01 12:09:56 -0700906 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
907 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100908
Li Zefan2739d3c2013-01-21 18:18:33 +0800909 /*
910 * If we're doing cleanup due to failure of cgroup_create(),
911 * the corresponding @cfe may not exist.
912 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700913 list_for_each_entry(cfe, &cgrp->files, node) {
914 struct dentry *d = cfe->dentry;
915
916 if (cft && cfe->type != cft)
917 continue;
918
919 dget(d);
920 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700921 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 list_del_init(&cfe->node);
923 dput(d);
924
Li Zefan2739d3c2013-01-21 18:18:33 +0800925 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700926 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927}
928
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400929/**
930 * cgroup_clear_directory - selective removal of base and subsystem files
931 * @dir: directory containing the files
932 * @base_files: true if the base files should be removed
933 * @subsys_mask: mask of the subsystem ids whose files should be removed
934 */
935static void cgroup_clear_directory(struct dentry *dir, bool base_files,
936 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937{
938 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400939 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400941 for_each_subsys(cgrp->root, ss) {
942 struct cftype_set *set;
943 if (!test_bit(ss->subsys_id, &subsys_mask))
944 continue;
945 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800946 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400947 }
948 if (base_files) {
949 while (!list_empty(&cgrp->files))
950 cgroup_rm_file(cgrp, NULL);
951 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952}
953
954/*
955 * NOTE : the dentry must have been dget()'ed
956 */
957static void cgroup_d_remove_dir(struct dentry *dentry)
958{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100959 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100961
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400962 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700963
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100964 parent = dentry->d_parent;
965 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800966 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100968 spin_unlock(&dentry->d_lock);
969 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 remove_dir(dentry);
971}
972
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700973/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800974 * Call with cgroup_mutex held. Drops reference counts on modules, including
975 * any duplicate ones that parse_cgroupfs_options took. If this function
976 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800977 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400979 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980{
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400981 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700982 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983 int i;
984
Ben Blumaae8aab2010-03-10 15:22:07 -0800985 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800986 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800987
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400988 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
989 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 /* Check that any added subsystems are currently free */
991 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800992 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400994 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800996 /*
997 * Nobody should tell us to do a subsys that doesn't exist:
998 * parse_cgroupfs_options should catch that case and refcounts
999 * ensure that subsystems won't disappear once selected.
1000 */
1001 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 if (ss->root != &rootnode) {
1003 /* Subsystem isn't free */
1004 return -EBUSY;
1005 }
1006 }
1007
1008 /* Currently we don't handle adding/removing subsystems when
1009 * any child cgroups exist. This is theoretically supportable
1010 * but involves complex error handling, so it's being left until
1011 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001012 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 return -EBUSY;
1014
1015 /* Process each subsystem */
1016 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1017 struct cgroup_subsys *ss = subsys[i];
1018 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001019 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001021 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001022 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 BUG_ON(!dummytop->subsys[i]);
1024 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001025 cgrp->subsys[i] = dummytop->subsys[i];
1026 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001027 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001028 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001030 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001031 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001032 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001034 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001035 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1036 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001038 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001040 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001041 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001042 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001043 /* subsystem is now free - drop reference on module */
1044 module_put(ss->module);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001045 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001047 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001048 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001049 /*
1050 * a refcount was taken, but we already had one, so
1051 * drop the extra reference.
1052 */
1053 module_put(ss->module);
1054#ifdef CONFIG_MODULE_UNLOAD
1055 BUG_ON(ss->module && !module_refcount(ss->module));
1056#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 } else {
1058 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001059 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 }
1061 }
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001062 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063
1064 return 0;
1065}
1066
Al Viro34c80b12011-12-08 21:32:45 -05001067static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068{
Al Viro34c80b12011-12-08 21:32:45 -05001069 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 struct cgroup_subsys *ss;
1071
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001072 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 for_each_subsys(root, ss)
1074 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001075 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1076 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001077 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001079 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001080 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001081 if (strlen(root->release_agent_path))
1082 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001083 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001084 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001085 if (strlen(root->name))
1086 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001087 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 return 0;
1089}
1090
1091struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001092 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001094 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001095 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001096 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001097 /* User explicitly requested empty subsystem */
1098 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001099
1100 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001101
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102};
1103
Ben Blumaae8aab2010-03-10 15:22:07 -08001104/*
1105 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001106 * with cgroup_mutex held to protect the subsys[] array. This function takes
1107 * refcounts on subsystems to be used, unless it returns error, in which case
1108 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001109 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001110static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001112 char *token, *o = data;
1113 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001114 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001115 int i;
1116 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001117
Ben Blumaae8aab2010-03-10 15:22:07 -08001118 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1119
Li Zefanf9ab5b52009-06-17 16:26:33 -07001120#ifdef CONFIG_CPUSETS
1121 mask = ~(1UL << cpuset_subsys_id);
1122#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123
Paul Menagec6d57f32009-09-23 15:56:19 -07001124 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125
1126 while ((token = strsep(&o, ",")) != NULL) {
1127 if (!*token)
1128 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001129 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001130 /* Explicitly have no subsystems */
1131 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 continue;
1133 }
1134 if (!strcmp(token, "all")) {
1135 /* Mutually exclusive option 'all' + subsystem name */
1136 if (one_ss)
1137 return -EINVAL;
1138 all_ss = true;
1139 continue;
1140 }
Tejun Heo873fe092013-04-14 20:15:26 -07001141 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1142 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1143 continue;
1144 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001145 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001146 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 continue;
1148 }
1149 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001150 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001151 continue;
1152 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001153 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001154 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001155 continue;
1156 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001157 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001158 /* Specifying two release agents is forbidden */
1159 if (opts->release_agent)
1160 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001161 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001162 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001163 if (!opts->release_agent)
1164 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 continue;
1166 }
1167 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001168 const char *name = token + 5;
1169 /* Can't specify an empty name */
1170 if (!strlen(name))
1171 return -EINVAL;
1172 /* Must match [\w.-]+ */
1173 for (i = 0; i < strlen(name); i++) {
1174 char c = name[i];
1175 if (isalnum(c))
1176 continue;
1177 if ((c == '.') || (c == '-') || (c == '_'))
1178 continue;
1179 return -EINVAL;
1180 }
1181 /* Specifying two names is forbidden */
1182 if (opts->name)
1183 return -EINVAL;
1184 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001185 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001186 GFP_KERNEL);
1187 if (!opts->name)
1188 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001189
1190 continue;
1191 }
1192
1193 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1194 struct cgroup_subsys *ss = subsys[i];
1195 if (ss == NULL)
1196 continue;
1197 if (strcmp(token, ss->name))
1198 continue;
1199 if (ss->disabled)
1200 continue;
1201
1202 /* Mutually exclusive option 'all' + subsystem name */
1203 if (all_ss)
1204 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001205 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206 one_ss = true;
1207
1208 break;
1209 }
1210 if (i == CGROUP_SUBSYS_COUNT)
1211 return -ENOENT;
1212 }
1213
1214 /*
1215 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001216 * otherwise if 'none', 'name=' and a subsystem name options
1217 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001219 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1221 struct cgroup_subsys *ss = subsys[i];
1222 if (ss == NULL)
1223 continue;
1224 if (ss->disabled)
1225 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001226 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227 }
1228 }
1229
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001230 /* Consistency checks */
1231
Tejun Heo873fe092013-04-14 20:15:26 -07001232 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1233 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1234
1235 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1236 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1237 return -EINVAL;
1238 }
1239
1240 if (opts->cpuset_clone_children) {
1241 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1242 return -EINVAL;
1243 }
1244 }
1245
Li Zefanf9ab5b52009-06-17 16:26:33 -07001246 /*
1247 * Option noprefix was introduced just for backward compatibility
1248 * with the old cpuset, so we allow noprefix only if mounting just
1249 * the cpuset subsystem.
1250 */
Tejun Heo93438622013-04-14 20:15:25 -07001251 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001252 return -EINVAL;
1253
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001254
1255 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001256 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001257 return -EINVAL;
1258
1259 /*
1260 * We either have to specify by name or by subsystems. (So all
1261 * empty hierarchies must have a name).
1262 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001263 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264 return -EINVAL;
1265
Ben Blumcf5d5942010-03-10 15:22:09 -08001266 /*
1267 * Grab references on all the modules we'll need, so the subsystems
1268 * don't dance around before rebind_subsystems attaches them. This may
1269 * take duplicate reference counts on a subsystem that's already used,
1270 * but rebind_subsystems handles this case.
1271 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001272 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001273 unsigned long bit = 1UL << i;
1274
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001275 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 continue;
1277 if (!try_module_get(subsys[i]->module)) {
1278 module_pin_failed = true;
1279 break;
1280 }
1281 }
1282 if (module_pin_failed) {
1283 /*
1284 * oops, one of the modules was going away. this means that we
1285 * raced with a module_delete call, and to the user this is
1286 * essentially a "subsystem doesn't exist" case.
1287 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001288 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 /* drop refcounts only on the ones we took */
1290 unsigned long bit = 1UL << i;
1291
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001292 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 continue;
1294 module_put(subsys[i]->module);
1295 }
1296 return -ENOENT;
1297 }
1298
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299 return 0;
1300}
1301
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001302static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001303{
1304 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001305 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001306 unsigned long bit = 1UL << i;
1307
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001308 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 continue;
1310 module_put(subsys[i]->module);
1311 }
1312}
1313
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1315{
1316 int ret = 0;
1317 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001318 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001320 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321
Tejun Heo873fe092013-04-14 20:15:26 -07001322 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1323 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1324 return -EINVAL;
1325 }
1326
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001329 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330
1331 /* See what subsystems are wanted */
1332 ret = parse_cgroupfs_options(data, &opts);
1333 if (ret)
1334 goto out_unlock;
1335
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001336 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001337 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1338 task_tgid_nr(current), current->comm);
1339
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001340 added_mask = opts.subsys_mask & ~root->subsys_mask;
1341 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001342
Ben Blumcf5d5942010-03-10 15:22:09 -08001343 /* Don't allow flags or name to change at remount */
1344 if (opts.flags != root->flags ||
1345 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001347 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001348 goto out_unlock;
1349 }
1350
Gao feng7083d032012-12-03 09:28:18 +08001351 /*
1352 * Clear out the files of subsystems that should be removed, do
1353 * this before rebind_subsystems, since rebind_subsystems may
1354 * change this hierarchy's subsys_list.
1355 */
1356 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1357
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001358 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001360 /* rebind_subsystems failed, re-populate the removed files */
1361 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001363 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001364 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001366 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001367 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368
Paul Menage81a6a5c2007-10-18 23:39:38 -07001369 if (opts.release_agent)
1370 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001372 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001373 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001374 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001376 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 return ret;
1378}
1379
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001380static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381 .statfs = simple_statfs,
1382 .drop_inode = generic_delete_inode,
1383 .show_options = cgroup_show_options,
1384 .remount_fs = cgroup_remount,
1385};
1386
Paul Menagecc31edc2008-10-18 20:28:04 -07001387static void init_cgroup_housekeeping(struct cgroup *cgrp)
1388{
1389 INIT_LIST_HEAD(&cgrp->sibling);
1390 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001391 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001392 INIT_LIST_HEAD(&cgrp->cset_links);
Tejun Heo22430762012-11-19 08:13:35 -08001393 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001394 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001395 INIT_LIST_HEAD(&cgrp->pidlists);
1396 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001397 INIT_LIST_HEAD(&cgrp->event_list);
1398 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001399 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001400}
Paul Menagec6d57f32009-09-23 15:56:19 -07001401
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402static void init_cgroup_root(struct cgroupfs_root *root)
1403{
Paul Menagebd89aab2007-10-18 23:40:44 -07001404 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001405
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406 INIT_LIST_HEAD(&root->subsys_list);
1407 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001408 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001409 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001410 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001411 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001412 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001413 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414}
1415
Tejun Heofa3ca072013-04-14 11:36:56 -07001416static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001417{
Tejun Heo1a574232013-04-14 11:36:58 -07001418 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001419
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001420 lockdep_assert_held(&cgroup_mutex);
1421 lockdep_assert_held(&cgroup_root_mutex);
1422
Tejun Heo1a574232013-04-14 11:36:58 -07001423 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1424 if (id < 0)
1425 return id;
1426
1427 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001428 return 0;
1429}
1430
1431static void cgroup_exit_root_id(struct cgroupfs_root *root)
1432{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001433 lockdep_assert_held(&cgroup_mutex);
1434 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001435
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001436 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001437 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001438 root->hierarchy_id = 0;
1439 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440}
1441
Paul Menageddbcc7e2007-10-18 23:39:30 -07001442static int cgroup_test_super(struct super_block *sb, void *data)
1443{
Paul Menagec6d57f32009-09-23 15:56:19 -07001444 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445 struct cgroupfs_root *root = sb->s_fs_info;
1446
Paul Menagec6d57f32009-09-23 15:56:19 -07001447 /* If we asked for a name then it must match */
1448 if (opts->name && strcmp(opts->name, root->name))
1449 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001450
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001451 /*
1452 * If we asked for subsystems (or explicitly for no
1453 * subsystems) then they must match
1454 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001455 if ((opts->subsys_mask || opts->none)
1456 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 return 0;
1458
1459 return 1;
1460}
1461
Paul Menagec6d57f32009-09-23 15:56:19 -07001462static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1463{
1464 struct cgroupfs_root *root;
1465
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001466 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 return NULL;
1468
1469 root = kzalloc(sizeof(*root), GFP_KERNEL);
1470 if (!root)
1471 return ERR_PTR(-ENOMEM);
1472
1473 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001474
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001475 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001477 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001478 if (opts->release_agent)
1479 strcpy(root->release_agent_path, opts->release_agent);
1480 if (opts->name)
1481 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001482 if (opts->cpuset_clone_children)
1483 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001484 return root;
1485}
1486
Tejun Heofa3ca072013-04-14 11:36:56 -07001487static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001488{
Tejun Heofa3ca072013-04-14 11:36:56 -07001489 if (root) {
1490 /* hierarhcy ID shoulid already have been released */
1491 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001492
Tejun Heofa3ca072013-04-14 11:36:56 -07001493 ida_destroy(&root->cgroup_ida);
1494 kfree(root);
1495 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001496}
1497
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498static int cgroup_set_super(struct super_block *sb, void *data)
1499{
1500 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 struct cgroup_sb_opts *opts = data;
1502
1503 /* If we don't have a new root, we can't set up a new sb */
1504 if (!opts->new_root)
1505 return -EINVAL;
1506
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001507 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
1509 ret = set_anon_super(sb, NULL);
1510 if (ret)
1511 return ret;
1512
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 sb->s_fs_info = opts->new_root;
1514 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515
1516 sb->s_blocksize = PAGE_CACHE_SIZE;
1517 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1518 sb->s_magic = CGROUP_SUPER_MAGIC;
1519 sb->s_op = &cgroup_ops;
1520
1521 return 0;
1522}
1523
1524static int cgroup_get_rootdir(struct super_block *sb)
1525{
Al Viro0df6a632010-12-21 13:29:29 -05001526 static const struct dentry_operations cgroup_dops = {
1527 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001528 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001529 };
1530
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531 struct inode *inode =
1532 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
1534 if (!inode)
1535 return -ENOMEM;
1536
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 inode->i_fop = &simple_dir_operations;
1538 inode->i_op = &cgroup_dir_inode_operations;
1539 /* directories start off with i_nlink == 2 (for "." entry) */
1540 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001541 sb->s_root = d_make_root(inode);
1542 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001544 /* for everything else we want ->d_op set */
1545 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 return 0;
1547}
1548
Al Virof7e83572010-07-26 13:23:11 +04001549static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001551 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552{
1553 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001554 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 int ret = 0;
1556 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001558 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001561 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001563 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001564 if (ret)
1565 goto out_err;
1566
1567 /*
1568 * Allocate a new cgroup root. We may not need it if we're
1569 * reusing an existing hierarchy.
1570 */
1571 new_root = cgroup_root_from_opts(&opts);
1572 if (IS_ERR(new_root)) {
1573 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001574 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001575 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001579 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001582 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001583 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 }
1585
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 root = sb->s_fs_info;
1587 BUG_ON(!root);
1588 if (root == opts.new_root) {
1589 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001590 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001591 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001593 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001594 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001595 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596
1597 BUG_ON(sb->s_root != NULL);
1598
1599 ret = cgroup_get_rootdir(sb);
1600 if (ret)
1601 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001602 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603
Paul Menage817929e2007-10-18 23:39:36 -07001604 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001606 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001608 /* Check for name clashes with existing mounts */
1609 ret = -EBUSY;
1610 if (strlen(root->name))
1611 for_each_active_root(existing_root)
1612 if (!strcmp(existing_root->name, root->name))
1613 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001614
Paul Menage817929e2007-10-18 23:39:36 -07001615 /*
1616 * We're accessing css_set_count without locking
1617 * css_set_lock here, but that's OK - it can only be
1618 * increased by someone holding cgroup_lock, and
1619 * that's us. The worst that can happen is that we
1620 * have some link structures left over
1621 */
Tejun Heo69d02062013-06-12 21:04:50 -07001622 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001623 if (ret)
1624 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001625
Tejun Heofa3ca072013-04-14 11:36:56 -07001626 ret = cgroup_init_root_id(root);
1627 if (ret)
1628 goto unlock_drop;
1629
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001630 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001632 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001633 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001635 /*
1636 * There must be no failure case after here, since rebinding
1637 * takes care of subsystems' refcounts, which are explicitly
1638 * dropped in the failure exit path.
1639 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640
1641 /* EBUSY should be the only error here */
1642 BUG_ON(ret);
1643
1644 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001645 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646
Li Zefanc12f65d2009-01-07 18:07:42 -08001647 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 root->top_cgroup.dentry = sb->s_root;
1649
Paul Menage817929e2007-10-18 23:39:36 -07001650 /* Link the top cgroup in this hierarchy into all
1651 * the css_set objects */
1652 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001653 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001654 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001655 write_unlock(&css_set_lock);
1656
Tejun Heo69d02062013-06-12 21:04:50 -07001657 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001658
Li Zefanc12f65d2009-01-07 18:07:42 -08001659 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 BUG_ON(root->number_of_cgroups != 1);
1661
eparis@redhat2ce97382011-06-02 21:20:51 +10001662 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001663 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001664 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001665 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001667 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001668 } else {
1669 /*
1670 * We re-used an existing hierarchy - the new root (if
1671 * any) is not needed
1672 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001673 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001674
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001675 if (root->flags != opts.flags) {
1676 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1677 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1678 ret = -EINVAL;
1679 goto drop_new_super;
1680 } else {
1681 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1682 }
Tejun Heo873fe092013-04-14 20:15:26 -07001683 }
1684
Ben Blumcf5d5942010-03-10 15:22:09 -08001685 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001686 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687 }
1688
Paul Menagec6d57f32009-09-23 15:56:19 -07001689 kfree(opts.release_agent);
1690 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001691 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001693 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001694 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001695 mutex_unlock(&cgroup_root_mutex);
1696 mutex_unlock(&cgroup_mutex);
1697 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001699 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001700 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001701 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001702 out_err:
1703 kfree(opts.release_agent);
1704 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001705 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706}
1707
1708static void cgroup_kill_sb(struct super_block *sb) {
1709 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001710 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001711 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 int ret;
1713
1714 BUG_ON(!root);
1715
1716 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001717 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
1719 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001720 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721
1722 /* Rebind all subsystems back to the default hierarchy */
1723 ret = rebind_subsystems(root, 0);
1724 /* Shouldn't be able to fail ... */
1725 BUG_ON(ret);
1726
Paul Menage817929e2007-10-18 23:39:36 -07001727 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001728 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001729 * root cgroup
1730 */
1731 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001732
Tejun Heo69d02062013-06-12 21:04:50 -07001733 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1734 list_del(&link->cset_link);
1735 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001736 kfree(link);
1737 }
1738 write_unlock(&css_set_lock);
1739
Paul Menage839ec542009-01-29 14:25:22 -08001740 if (!list_empty(&root->root_list)) {
1741 list_del(&root->root_list);
1742 root_count--;
1743 }
Li Zefane5f6a862009-01-07 18:07:41 -08001744
Tejun Heofa3ca072013-04-14 11:36:56 -07001745 cgroup_exit_root_id(root);
1746
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001747 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748 mutex_unlock(&cgroup_mutex);
1749
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001750 simple_xattrs_free(&cgrp->xattrs);
1751
Paul Menageddbcc7e2007-10-18 23:39:30 -07001752 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001753 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754}
1755
1756static struct file_system_type cgroup_fs_type = {
1757 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001758 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 .kill_sb = cgroup_kill_sb,
1760};
1761
Greg KH676db4a2010-08-05 13:53:35 -07001762static struct kobject *cgroup_kobj;
1763
Li Zefana043e3b2008-02-23 15:24:09 -08001764/**
1765 * cgroup_path - generate the path of a cgroup
1766 * @cgrp: the cgroup in question
1767 * @buf: the buffer to write the path into
1768 * @buflen: the length of the buffer
1769 *
Li Zefan65dff752013-03-01 15:01:56 +08001770 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1771 *
1772 * We can't generate cgroup path using dentry->d_name, as accessing
1773 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1774 * inode's i_mutex, while on the other hand cgroup_path() can be called
1775 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001777int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778{
Li Zefan65dff752013-03-01 15:01:56 +08001779 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001781
Tejun Heoda1f2962013-04-14 10:32:19 -07001782 if (!cgrp->parent) {
1783 if (strlcpy(buf, "/", buflen) >= buflen)
1784 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 return 0;
1786 }
1787
Tao Ma316eb662012-11-08 21:36:38 +08001788 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001789 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001790
Li Zefan65dff752013-03-01 15:01:56 +08001791 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001792 do {
Li Zefan65dff752013-03-01 15:01:56 +08001793 const char *name = cgroup_name(cgrp);
1794 int len;
1795
1796 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001798 goto out;
1799 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001800
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001802 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001804
1805 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001806 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001807 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001809out:
1810 rcu_read_unlock();
1811 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812}
Ben Blum67523c42010-03-10 15:22:11 -08001813EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814
Tejun Heo857a2be2013-04-14 20:50:08 -07001815/**
1816 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1817 * @task: target task
1818 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1819 * @buf: the buffer to write the path into
1820 * @buflen: the length of the buffer
1821 *
1822 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1823 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1824 * be used inside locks used by cgroup controller callbacks.
1825 */
1826int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1827 char *buf, size_t buflen)
1828{
1829 struct cgroupfs_root *root;
1830 struct cgroup *cgrp = NULL;
1831 int ret = -ENOENT;
1832
1833 mutex_lock(&cgroup_mutex);
1834
1835 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1836 if (root) {
1837 cgrp = task_cgroup_from_root(task, root);
1838 ret = cgroup_path(cgrp, buf, buflen);
1839 }
1840
1841 mutex_unlock(&cgroup_mutex);
1842
1843 return ret;
1844}
1845EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1846
Ben Blum74a11662011-05-26 16:25:20 -07001847/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001848 * Control Group taskset
1849 */
Tejun Heo134d3372011-12-12 18:12:21 -08001850struct task_and_cgroup {
1851 struct task_struct *task;
1852 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001853 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001854};
1855
Tejun Heo2f7ee562011-12-12 18:12:21 -08001856struct cgroup_taskset {
1857 struct task_and_cgroup single;
1858 struct flex_array *tc_array;
1859 int tc_array_len;
1860 int idx;
1861 struct cgroup *cur_cgrp;
1862};
1863
1864/**
1865 * cgroup_taskset_first - reset taskset and return the first task
1866 * @tset: taskset of interest
1867 *
1868 * @tset iteration is initialized and the first task is returned.
1869 */
1870struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1871{
1872 if (tset->tc_array) {
1873 tset->idx = 0;
1874 return cgroup_taskset_next(tset);
1875 } else {
1876 tset->cur_cgrp = tset->single.cgrp;
1877 return tset->single.task;
1878 }
1879}
1880EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1881
1882/**
1883 * cgroup_taskset_next - iterate to the next task in taskset
1884 * @tset: taskset of interest
1885 *
1886 * Return the next task in @tset. Iteration must have been initialized
1887 * with cgroup_taskset_first().
1888 */
1889struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1890{
1891 struct task_and_cgroup *tc;
1892
1893 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1894 return NULL;
1895
1896 tc = flex_array_get(tset->tc_array, tset->idx++);
1897 tset->cur_cgrp = tc->cgrp;
1898 return tc->task;
1899}
1900EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1901
1902/**
1903 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1904 * @tset: taskset of interest
1905 *
1906 * Return the cgroup for the current (last returned) task of @tset. This
1907 * function must be preceded by either cgroup_taskset_first() or
1908 * cgroup_taskset_next().
1909 */
1910struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1911{
1912 return tset->cur_cgrp;
1913}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1915
1916/**
1917 * cgroup_taskset_size - return the number of tasks in taskset
1918 * @tset: taskset of interest
1919 */
1920int cgroup_taskset_size(struct cgroup_taskset *tset)
1921{
1922 return tset->tc_array ? tset->tc_array_len : 1;
1923}
1924EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1925
1926
Ben Blum74a11662011-05-26 16:25:20 -07001927/*
1928 * cgroup_task_migrate - move a task from one cgroup to another.
1929 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001930 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001932static void cgroup_task_migrate(struct cgroup *old_cgrp,
1933 struct task_struct *tsk,
1934 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001935{
Tejun Heo5abb8852013-06-12 21:04:49 -07001936 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001937
1938 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001939 * We are synchronized through threadgroup_lock() against PF_EXITING
1940 * setting such that we can't race against cgroup_exit() changing the
1941 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001942 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001943 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001944 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001945
Ben Blum74a11662011-05-26 16:25:20 -07001946 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001947 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001948 task_unlock(tsk);
1949
1950 /* Update the css_set linked lists if we're using them */
1951 write_lock(&css_set_lock);
1952 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001953 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001954 write_unlock(&css_set_lock);
1955
1956 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001957 * We just gained a reference on old_cset by taking it from the
1958 * task. As trading it for new_cset is protected by cgroup_mutex,
1959 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001960 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001961 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1962 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001963}
1964
Li Zefana043e3b2008-02-23 15:24:09 -08001965/**
Li Zefan081aa452013-03-13 09:17:09 +08001966 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001967 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001968 * @tsk: the task or the leader of the threadgroup to be attached
1969 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001970 *
Tejun Heo257058a2011-12-12 18:12:21 -08001971 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001972 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001973 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001974static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1975 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001976{
1977 int retval, i, group_size;
1978 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001979 struct cgroupfs_root *root = cgrp->root;
1980 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001981 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001982 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001983 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001984 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001985
1986 /*
1987 * step 0: in order to do expensive, possibly blocking operations for
1988 * every thread, we cannot iterate the thread group list, since it needs
1989 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001990 * group - group_rwsem prevents new threads from appearing, and if
1991 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001992 */
Li Zefan081aa452013-03-13 09:17:09 +08001993 if (threadgroup)
1994 group_size = get_nr_threads(tsk);
1995 else
1996 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001997 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001998 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001999 if (!group)
2000 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002001 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002002 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002003 if (retval)
2004 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002005
Ben Blum74a11662011-05-26 16:25:20 -07002006 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002007 /*
2008 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2009 * already PF_EXITING could be freed from underneath us unless we
2010 * take an rcu_read_lock.
2011 */
2012 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002013 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002014 struct task_and_cgroup ent;
2015
Tejun Heocd3d0952011-12-12 18:12:21 -08002016 /* @tsk either already exited or can't exit until the end */
2017 if (tsk->flags & PF_EXITING)
2018 continue;
2019
Ben Blum74a11662011-05-26 16:25:20 -07002020 /* as per above, nr_threads may decrease, but not increase. */
2021 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002022 ent.task = tsk;
2023 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002024 /* nothing to do if this task is already in the cgroup */
2025 if (ent.cgrp == cgrp)
2026 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002027 /*
2028 * saying GFP_ATOMIC has no effect here because we did prealloc
2029 * earlier, but it's good form to communicate our expectations.
2030 */
Tejun Heo134d3372011-12-12 18:12:21 -08002031 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002032 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002033 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002034
2035 if (!threadgroup)
2036 break;
Ben Blum74a11662011-05-26 16:25:20 -07002037 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002038 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002039 /* remember the number of threads in the array for later. */
2040 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002041 tset.tc_array = group;
2042 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002043
Tejun Heo134d3372011-12-12 18:12:21 -08002044 /* methods shouldn't be called if no task is actually migrating */
2045 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002046 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002047 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002048
Ben Blum74a11662011-05-26 16:25:20 -07002049 /*
2050 * step 1: check that we can legitimately attach to the cgroup.
2051 */
2052 for_each_subsys(root, ss) {
2053 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002054 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002055 if (retval) {
2056 failed_ss = ss;
2057 goto out_cancel_attach;
2058 }
2059 }
Ben Blum74a11662011-05-26 16:25:20 -07002060 }
2061
2062 /*
2063 * step 2: make sure css_sets exist for all threads to be migrated.
2064 * we use find_css_set, which allocates a new one if necessary.
2065 */
Ben Blum74a11662011-05-26 16:25:20 -07002066 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002067 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002068 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2069 if (!tc->cg) {
2070 retval = -ENOMEM;
2071 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002072 }
2073 }
2074
2075 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002076 * step 3: now that we're guaranteed success wrt the css_sets,
2077 * proceed to move all tasks to the new cgroup. There are no
2078 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002079 */
Ben Blum74a11662011-05-26 16:25:20 -07002080 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002081 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002082 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002083 }
2084 /* nothing is sensitive to fork() after this point. */
2085
2086 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002087 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002088 */
2089 for_each_subsys(root, ss) {
2090 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002091 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002092 }
2093
2094 /*
2095 * step 5: success! and cleanup
2096 */
Ben Blum74a11662011-05-26 16:25:20 -07002097 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002098out_put_css_set_refs:
2099 if (retval) {
2100 for (i = 0; i < group_size; i++) {
2101 tc = flex_array_get(group, i);
2102 if (!tc->cg)
2103 break;
2104 put_css_set(tc->cg);
2105 }
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002108 if (retval) {
2109 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002110 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002111 break;
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002113 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002114 }
2115 }
Ben Blum74a11662011-05-26 16:25:20 -07002116out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002117 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002118 return retval;
2119}
2120
2121/*
2122 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002123 * function to attach either it or all tasks in its threadgroup. Will lock
2124 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002125 */
2126static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002127{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002129 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 int ret;
2131
Ben Blum74a11662011-05-26 16:25:20 -07002132 if (!cgroup_lock_live_group(cgrp))
2133 return -ENODEV;
2134
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135retry_find_task:
2136 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002137 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002138 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002139 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141 ret= -ESRCH;
2142 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 }
Ben Blum74a11662011-05-26 16:25:20 -07002144 /*
2145 * even if we're attaching all tasks in the thread group, we
2146 * only need to check permissions on one of them.
2147 */
David Howellsc69e8d92008-11-14 10:39:19 +11002148 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002149 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2150 !uid_eq(cred->euid, tcred->uid) &&
2151 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002152 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 ret = -EACCES;
2154 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002156 } else
2157 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002158
2159 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002160 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002161
2162 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002163 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002164 * trapped in a cpuset, or RT worker may be born in a cgroup
2165 * with no rt_runtime allocated. Just say no.
2166 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002167 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002168 ret = -EINVAL;
2169 rcu_read_unlock();
2170 goto out_unlock_cgroup;
2171 }
2172
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 get_task_struct(tsk);
2174 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002175
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176 threadgroup_lock(tsk);
2177 if (threadgroup) {
2178 if (!thread_group_leader(tsk)) {
2179 /*
2180 * a race with de_thread from another thread's exec()
2181 * may strip us of our leadership, if this happens,
2182 * there is no choice but to throw this task away and
2183 * try again; this is
2184 * "double-double-toil-and-trouble-check locking".
2185 */
2186 threadgroup_unlock(tsk);
2187 put_task_struct(tsk);
2188 goto retry_find_task;
2189 }
Li Zefan081aa452013-03-13 09:17:09 +08002190 }
2191
2192 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2193
Tejun Heocd3d0952011-12-12 18:12:21 -08002194 threadgroup_unlock(tsk);
2195
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002197out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002198 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002199 return ret;
2200}
2201
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002202/**
2203 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2204 * @from: attach to all cgroups of a given task
2205 * @tsk: the task to be attached
2206 */
2207int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2208{
2209 struct cgroupfs_root *root;
2210 int retval = 0;
2211
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213 for_each_active_root(root) {
2214 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2215
2216 retval = cgroup_attach_task(from_cg, tsk, false);
2217 if (retval)
2218 break;
2219 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002220 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002221
2222 return retval;
2223}
2224EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2225
Paul Menageaf351022008-07-25 01:47:01 -07002226static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2227{
Ben Blum74a11662011-05-26 16:25:20 -07002228 return attach_task_by_pid(cgrp, pid, false);
2229}
2230
2231static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2232{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002233 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002234}
2235
Paul Menagee788e062008-07-25 01:46:59 -07002236static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2237 const char *buffer)
2238{
2239 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002240 if (strlen(buffer) >= PATH_MAX)
2241 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002242 if (!cgroup_lock_live_group(cgrp))
2243 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002244 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002245 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002246 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002247 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002248 return 0;
2249}
2250
2251static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2252 struct seq_file *seq)
2253{
2254 if (!cgroup_lock_live_group(cgrp))
2255 return -ENODEV;
2256 seq_puts(seq, cgrp->root->release_agent_path);
2257 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002258 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002259 return 0;
2260}
2261
Tejun Heo873fe092013-04-14 20:15:26 -07002262static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2263 struct seq_file *seq)
2264{
2265 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002266 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002267}
2268
Paul Menage84eea842008-07-25 01:47:00 -07002269/* A buffer size big enough for numbers or short strings */
2270#define CGROUP_LOCAL_BUFFER_SIZE 64
2271
Paul Menagee73d2c62008-04-29 01:00:06 -07002272static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002273 struct file *file,
2274 const char __user *userbuf,
2275 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002276{
Paul Menage84eea842008-07-25 01:47:00 -07002277 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002278 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002279 char *end;
2280
2281 if (!nbytes)
2282 return -EINVAL;
2283 if (nbytes >= sizeof(buffer))
2284 return -E2BIG;
2285 if (copy_from_user(buffer, userbuf, nbytes))
2286 return -EFAULT;
2287
2288 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002289 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002290 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002291 if (*end)
2292 return -EINVAL;
2293 retval = cft->write_u64(cgrp, cft, val);
2294 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002295 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002296 if (*end)
2297 return -EINVAL;
2298 retval = cft->write_s64(cgrp, cft, val);
2299 }
Paul Menage355e0c42007-10-18 23:39:33 -07002300 if (!retval)
2301 retval = nbytes;
2302 return retval;
2303}
2304
Paul Menagedb3b1492008-07-25 01:46:58 -07002305static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2306 struct file *file,
2307 const char __user *userbuf,
2308 size_t nbytes, loff_t *unused_ppos)
2309{
Paul Menage84eea842008-07-25 01:47:00 -07002310 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002311 int retval = 0;
2312 size_t max_bytes = cft->max_write_len;
2313 char *buffer = local_buffer;
2314
2315 if (!max_bytes)
2316 max_bytes = sizeof(local_buffer) - 1;
2317 if (nbytes >= max_bytes)
2318 return -E2BIG;
2319 /* Allocate a dynamic buffer if we need one */
2320 if (nbytes >= sizeof(local_buffer)) {
2321 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2322 if (buffer == NULL)
2323 return -ENOMEM;
2324 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002325 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2326 retval = -EFAULT;
2327 goto out;
2328 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002329
2330 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002331 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002332 if (!retval)
2333 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002334out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002335 if (buffer != local_buffer)
2336 kfree(buffer);
2337 return retval;
2338}
2339
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2341 size_t nbytes, loff_t *ppos)
2342{
2343 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002344 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345
Tejun Heo54766d42013-06-12 21:04:53 -07002346 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002348 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002349 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002350 if (cft->write_u64 || cft->write_s64)
2351 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002352 if (cft->write_string)
2353 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002354 if (cft->trigger) {
2355 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2356 return ret ? ret : nbytes;
2357 }
Paul Menage355e0c42007-10-18 23:39:33 -07002358 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359}
2360
Paul Menagef4c753b2008-04-29 00:59:56 -07002361static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2362 struct file *file,
2363 char __user *buf, size_t nbytes,
2364 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365{
Paul Menage84eea842008-07-25 01:47:00 -07002366 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002367 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002368 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2369
2370 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2371}
2372
Paul Menagee73d2c62008-04-29 01:00:06 -07002373static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2374 struct file *file,
2375 char __user *buf, size_t nbytes,
2376 loff_t *ppos)
2377{
Paul Menage84eea842008-07-25 01:47:00 -07002378 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002379 s64 val = cft->read_s64(cgrp, cft);
2380 int len = sprintf(tmp, "%lld\n", (long long) val);
2381
2382 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2383}
2384
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2386 size_t nbytes, loff_t *ppos)
2387{
2388 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002389 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390
Tejun Heo54766d42013-06-12 21:04:53 -07002391 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return -ENODEV;
2393
2394 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002395 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002396 if (cft->read_u64)
2397 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002398 if (cft->read_s64)
2399 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002400 return -EINVAL;
2401}
2402
Paul Menage91796562008-04-29 01:00:01 -07002403/*
2404 * seqfile ops/methods for returning structured data. Currently just
2405 * supports string->u64 maps, but can be extended in future.
2406 */
2407
2408struct cgroup_seqfile_state {
2409 struct cftype *cft;
2410 struct cgroup *cgroup;
2411};
2412
2413static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2414{
2415 struct seq_file *sf = cb->state;
2416 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2417}
2418
2419static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2420{
2421 struct cgroup_seqfile_state *state = m->private;
2422 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002423 if (cft->read_map) {
2424 struct cgroup_map_cb cb = {
2425 .fill = cgroup_map_add,
2426 .state = m,
2427 };
2428 return cft->read_map(state->cgroup, cft, &cb);
2429 }
2430 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002431}
2432
Adrian Bunk96930a62008-07-25 19:46:21 -07002433static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002434{
2435 struct seq_file *seq = file->private_data;
2436 kfree(seq->private);
2437 return single_release(inode, file);
2438}
2439
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002440static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002441 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002442 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002443 .llseek = seq_lseek,
2444 .release = cgroup_seqfile_release,
2445};
2446
Paul Menageddbcc7e2007-10-18 23:39:30 -07002447static int cgroup_file_open(struct inode *inode, struct file *file)
2448{
2449 int err;
2450 struct cftype *cft;
2451
2452 err = generic_file_open(inode, file);
2453 if (err)
2454 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002455 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002456
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002457 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002458 struct cgroup_seqfile_state *state;
2459
2460 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002461 if (!state)
2462 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002463
Paul Menage91796562008-04-29 01:00:01 -07002464 state->cft = cft;
2465 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2466 file->f_op = &cgroup_seqfile_operations;
2467 err = single_open(file, cgroup_seqfile_show, state);
2468 if (err < 0)
2469 kfree(state);
2470 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002471 err = cft->open(inode, file);
2472 else
2473 err = 0;
2474
2475 return err;
2476}
2477
2478static int cgroup_file_release(struct inode *inode, struct file *file)
2479{
2480 struct cftype *cft = __d_cft(file->f_dentry);
2481 if (cft->release)
2482 return cft->release(inode, file);
2483 return 0;
2484}
2485
2486/*
2487 * cgroup_rename - Only allow simple rename of directories in place.
2488 */
2489static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2490 struct inode *new_dir, struct dentry *new_dentry)
2491{
Li Zefan65dff752013-03-01 15:01:56 +08002492 int ret;
2493 struct cgroup_name *name, *old_name;
2494 struct cgroup *cgrp;
2495
2496 /*
2497 * It's convinient to use parent dir's i_mutex to protected
2498 * cgrp->name.
2499 */
2500 lockdep_assert_held(&old_dir->i_mutex);
2501
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2503 return -ENOTDIR;
2504 if (new_dentry->d_inode)
2505 return -EEXIST;
2506 if (old_dir != new_dir)
2507 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002508
2509 cgrp = __d_cgrp(old_dentry);
2510
Tejun Heo6db8e852013-06-14 11:18:22 -07002511 /*
2512 * This isn't a proper migration and its usefulness is very
2513 * limited. Disallow if sane_behavior.
2514 */
2515 if (cgroup_sane_behavior(cgrp))
2516 return -EPERM;
2517
Li Zefan65dff752013-03-01 15:01:56 +08002518 name = cgroup_alloc_name(new_dentry);
2519 if (!name)
2520 return -ENOMEM;
2521
2522 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2523 if (ret) {
2524 kfree(name);
2525 return ret;
2526 }
2527
2528 old_name = cgrp->name;
2529 rcu_assign_pointer(cgrp->name, name);
2530
2531 kfree_rcu(old_name, rcu_head);
2532 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002533}
2534
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002535static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2536{
2537 if (S_ISDIR(dentry->d_inode->i_mode))
2538 return &__d_cgrp(dentry)->xattrs;
2539 else
Li Zefan712317a2013-04-18 23:09:52 -07002540 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002541}
2542
2543static inline int xattr_enabled(struct dentry *dentry)
2544{
2545 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002546 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002547}
2548
2549static bool is_valid_xattr(const char *name)
2550{
2551 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2552 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2553 return true;
2554 return false;
2555}
2556
2557static int cgroup_setxattr(struct dentry *dentry, const char *name,
2558 const void *val, size_t size, int flags)
2559{
2560 if (!xattr_enabled(dentry))
2561 return -EOPNOTSUPP;
2562 if (!is_valid_xattr(name))
2563 return -EINVAL;
2564 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2565}
2566
2567static int cgroup_removexattr(struct dentry *dentry, const char *name)
2568{
2569 if (!xattr_enabled(dentry))
2570 return -EOPNOTSUPP;
2571 if (!is_valid_xattr(name))
2572 return -EINVAL;
2573 return simple_xattr_remove(__d_xattrs(dentry), name);
2574}
2575
2576static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2577 void *buf, size_t size)
2578{
2579 if (!xattr_enabled(dentry))
2580 return -EOPNOTSUPP;
2581 if (!is_valid_xattr(name))
2582 return -EINVAL;
2583 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2584}
2585
2586static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2587{
2588 if (!xattr_enabled(dentry))
2589 return -EOPNOTSUPP;
2590 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2591}
2592
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002593static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594 .read = cgroup_file_read,
2595 .write = cgroup_file_write,
2596 .llseek = generic_file_llseek,
2597 .open = cgroup_file_open,
2598 .release = cgroup_file_release,
2599};
2600
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002601static const struct inode_operations cgroup_file_inode_operations = {
2602 .setxattr = cgroup_setxattr,
2603 .getxattr = cgroup_getxattr,
2604 .listxattr = cgroup_listxattr,
2605 .removexattr = cgroup_removexattr,
2606};
2607
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002608static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002609 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002610 .mkdir = cgroup_mkdir,
2611 .rmdir = cgroup_rmdir,
2612 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002613 .setxattr = cgroup_setxattr,
2614 .getxattr = cgroup_getxattr,
2615 .listxattr = cgroup_listxattr,
2616 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002617};
2618
Al Viro00cd8dd2012-06-10 17:13:09 -04002619static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002620{
2621 if (dentry->d_name.len > NAME_MAX)
2622 return ERR_PTR(-ENAMETOOLONG);
2623 d_add(dentry, NULL);
2624 return NULL;
2625}
2626
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002627/*
2628 * Check if a file is a control file
2629 */
2630static inline struct cftype *__file_cft(struct file *file)
2631{
Al Viro496ad9a2013-01-23 17:07:38 -05002632 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002633 return ERR_PTR(-EINVAL);
2634 return __d_cft(file->f_dentry);
2635}
2636
Al Viroa5e7ed32011-07-26 01:55:55 -04002637static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002638 struct super_block *sb)
2639{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 struct inode *inode;
2641
2642 if (!dentry)
2643 return -ENOENT;
2644 if (dentry->d_inode)
2645 return -EEXIST;
2646
2647 inode = cgroup_new_inode(mode, sb);
2648 if (!inode)
2649 return -ENOMEM;
2650
2651 if (S_ISDIR(mode)) {
2652 inode->i_op = &cgroup_dir_inode_operations;
2653 inode->i_fop = &simple_dir_operations;
2654
2655 /* start off with i_nlink == 2 (for "." entry) */
2656 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002657 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658
Tejun Heob8a2df62012-11-19 08:13:37 -08002659 /*
2660 * Control reaches here with cgroup_mutex held.
2661 * @inode->i_mutex should nest outside cgroup_mutex but we
2662 * want to populate it immediately without releasing
2663 * cgroup_mutex. As @inode isn't visible to anyone else
2664 * yet, trylock will always succeed without affecting
2665 * lockdep checks.
2666 */
2667 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002668 } else if (S_ISREG(mode)) {
2669 inode->i_size = 0;
2670 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002671 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 d_instantiate(dentry, inode);
2674 dget(dentry); /* Extra count - pin the dentry in core */
2675 return 0;
2676}
2677
Li Zefan099fca32009-04-02 16:57:29 -07002678/**
2679 * cgroup_file_mode - deduce file mode of a control file
2680 * @cft: the control file in question
2681 *
2682 * returns cft->mode if ->mode is not 0
2683 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2684 * returns S_IRUGO if it has only a read handler
2685 * returns S_IWUSR if it has only a write hander
2686 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002687static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002688{
Al Viroa5e7ed32011-07-26 01:55:55 -04002689 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002690
2691 if (cft->mode)
2692 return cft->mode;
2693
2694 if (cft->read || cft->read_u64 || cft->read_s64 ||
2695 cft->read_map || cft->read_seq_string)
2696 mode |= S_IRUGO;
2697
2698 if (cft->write || cft->write_u64 || cft->write_s64 ||
2699 cft->write_string || cft->trigger)
2700 mode |= S_IWUSR;
2701
2702 return mode;
2703}
2704
Tejun Heodb0416b2012-04-01 12:09:55 -07002705static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002706 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707{
Paul Menagebd89aab2007-10-18 23:40:44 -07002708 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002709 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002711 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002713 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002715
Tejun Heo93438622013-04-14 20:15:25 -07002716 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 strcpy(name, subsys->name);
2718 strcat(name, ".");
2719 }
2720 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002721
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723
2724 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2725 if (!cfe)
2726 return -ENOMEM;
2727
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002729 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002730 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731 goto out;
2732 }
2733
Li Zefand6cbf352013-05-14 19:44:20 +08002734 cfe->type = (void *)cft;
2735 cfe->dentry = dentry;
2736 dentry->d_fsdata = cfe;
2737 simple_xattrs_init(&cfe->xattrs);
2738
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 mode = cgroup_file_mode(cft);
2740 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2741 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002742 list_add_tail(&cfe->node, &parent->files);
2743 cfe = NULL;
2744 }
2745 dput(dentry);
2746out:
2747 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 return error;
2749}
2750
Tejun Heo79578622012-04-01 12:09:56 -07002751static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002752 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002754 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002755 int err, ret = 0;
2756
2757 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002758 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002759 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2760 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002761 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2762 continue;
2763 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2764 continue;
2765
Li Zefan2739d3c2013-01-21 18:18:33 +08002766 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002767 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002768 if (err)
2769 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2770 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002771 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002772 } else {
2773 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002774 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002775 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002776 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002777}
2778
Tejun Heo8e3f6542012-04-01 12:09:55 -07002779static DEFINE_MUTEX(cgroup_cft_mutex);
2780
2781static void cgroup_cfts_prepare(void)
2782 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2783{
2784 /*
2785 * Thanks to the entanglement with vfs inode locking, we can't walk
2786 * the existing cgroups under cgroup_mutex and create files.
2787 * Instead, we increment reference on all cgroups and build list of
2788 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2789 * exclusive access to the field.
2790 */
2791 mutex_lock(&cgroup_cft_mutex);
2792 mutex_lock(&cgroup_mutex);
2793}
2794
2795static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002796 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2798{
2799 LIST_HEAD(pending);
2800 struct cgroup *cgrp, *n;
Li Zefan084457f2013-06-18 18:40:19 +08002801 struct super_block *sb = ss->root->sb;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002802
2803 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Li Zefan084457f2013-06-18 18:40:19 +08002804 if (cfts && ss->root != &rootnode &&
2805 atomic_inc_not_zero(sb->s_active)) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07002806 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2807 dget(cgrp->dentry);
2808 list_add_tail(&cgrp->cft_q_node, &pending);
2809 }
Li Zefan084457f2013-06-18 18:40:19 +08002810 } else {
2811 sb = NULL;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002812 }
2813
2814 mutex_unlock(&cgroup_mutex);
2815
2816 /*
2817 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2818 * files for all cgroups which were created before.
2819 */
2820 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2821 struct inode *inode = cgrp->dentry->d_inode;
2822
2823 mutex_lock(&inode->i_mutex);
2824 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -07002825 if (!cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002826 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002827 mutex_unlock(&cgroup_mutex);
2828 mutex_unlock(&inode->i_mutex);
2829
2830 list_del_init(&cgrp->cft_q_node);
2831 dput(cgrp->dentry);
2832 }
2833
Li Zefan084457f2013-06-18 18:40:19 +08002834 if (sb)
2835 deactivate_super(sb);
2836
Tejun Heo8e3f6542012-04-01 12:09:55 -07002837 mutex_unlock(&cgroup_cft_mutex);
2838}
2839
2840/**
2841 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2842 * @ss: target cgroup subsystem
2843 * @cfts: zero-length name terminated array of cftypes
2844 *
2845 * Register @cfts to @ss. Files described by @cfts are created for all
2846 * existing cgroups to which @ss is attached and all future cgroups will
2847 * have them too. This function can be called anytime whether @ss is
2848 * attached or not.
2849 *
2850 * Returns 0 on successful registration, -errno on failure. Note that this
2851 * function currently returns 0 as long as @cfts registration is successful
2852 * even if some file creation attempts on existing cgroups fail.
2853 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002854int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002855{
2856 struct cftype_set *set;
2857
2858 set = kzalloc(sizeof(*set), GFP_KERNEL);
2859 if (!set)
2860 return -ENOMEM;
2861
2862 cgroup_cfts_prepare();
2863 set->cfts = cfts;
2864 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002865 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002866
2867 return 0;
2868}
2869EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2870
Li Zefana043e3b2008-02-23 15:24:09 -08002871/**
Tejun Heo79578622012-04-01 12:09:56 -07002872 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2873 * @ss: target cgroup subsystem
2874 * @cfts: zero-length name terminated array of cftypes
2875 *
2876 * Unregister @cfts from @ss. Files described by @cfts are removed from
2877 * all existing cgroups to which @ss is attached and all future cgroups
2878 * won't have them either. This function can be called anytime whether @ss
2879 * is attached or not.
2880 *
2881 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2882 * registered with @ss.
2883 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002884int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002885{
2886 struct cftype_set *set;
2887
2888 cgroup_cfts_prepare();
2889
2890 list_for_each_entry(set, &ss->cftsets, node) {
2891 if (set->cfts == cfts) {
2892 list_del_init(&set->node);
2893 cgroup_cfts_commit(ss, cfts, false);
2894 return 0;
2895 }
2896 }
2897
2898 cgroup_cfts_commit(ss, NULL, false);
2899 return -ENOENT;
2900}
2901
2902/**
Li Zefana043e3b2008-02-23 15:24:09 -08002903 * cgroup_task_count - count the number of tasks in a cgroup.
2904 * @cgrp: the cgroup in question
2905 *
2906 * Return the number of tasks in the cgroup.
2907 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002908int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002909{
2910 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002911 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002912
Paul Menage817929e2007-10-18 23:39:36 -07002913 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002914 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2915 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002916 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002917 return count;
2918}
2919
2920/*
Paul Menage817929e2007-10-18 23:39:36 -07002921 * Advance a list_head iterator. The iterator should be positioned at
2922 * the start of a css_set
2923 */
Tejun Heo69d02062013-06-12 21:04:50 -07002924static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002925{
Tejun Heo69d02062013-06-12 21:04:50 -07002926 struct list_head *l = it->cset_link;
2927 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002928 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002929
2930 /* Advance to the next non-empty css_set */
2931 do {
2932 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002933 if (l == &cgrp->cset_links) {
2934 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002935 return;
2936 }
Tejun Heo69d02062013-06-12 21:04:50 -07002937 link = list_entry(l, struct cgrp_cset_link, cset_link);
2938 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002939 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002940 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002941 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002942}
2943
Cliff Wickman31a7df02008-02-07 00:14:42 -08002944/*
2945 * To reduce the fork() overhead for systems that are not actually
2946 * using their cgroups capability, we don't maintain the lists running
2947 * through each css_set to its tasks until we see the list actually
2948 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002949 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002950static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002951{
2952 struct task_struct *p, *g;
2953 write_lock(&css_set_lock);
2954 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002955 /*
2956 * We need tasklist_lock because RCU is not safe against
2957 * while_each_thread(). Besides, a forking task that has passed
2958 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2959 * is not guaranteed to have its child immediately visible in the
2960 * tasklist if we walk through it with RCU.
2961 */
2962 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002963 do_each_thread(g, p) {
2964 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002965 /*
2966 * We should check if the process is exiting, otherwise
2967 * it will race with cgroup_exit() in that the list
2968 * entry won't be deleted though the process has exited.
2969 */
2970 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002971 list_add(&p->cg_list, &p->cgroups->tasks);
2972 task_unlock(p);
2973 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002974 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002975 write_unlock(&css_set_lock);
2976}
2977
Tejun Heo574bd9f2012-11-09 09:12:29 -08002978/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002979 * cgroup_next_sibling - find the next sibling of a given cgroup
2980 * @pos: the current cgroup
2981 *
2982 * This function returns the next sibling of @pos and should be called
2983 * under RCU read lock. The only requirement is that @pos is accessible.
2984 * The next sibling is guaranteed to be returned regardless of @pos's
2985 * state.
2986 */
2987struct cgroup *cgroup_next_sibling(struct cgroup *pos)
2988{
2989 struct cgroup *next;
2990
2991 WARN_ON_ONCE(!rcu_read_lock_held());
2992
2993 /*
2994 * @pos could already have been removed. Once a cgroup is removed,
2995 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002996 * changes. As CGRP_DEAD assertion is serialized and happens
2997 * before the cgroup is taken off the ->sibling list, if we see it
2998 * unasserted, it's guaranteed that the next sibling hasn't
2999 * finished its grace period even if it's already removed, and thus
3000 * safe to dereference from this RCU critical section. If
3001 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3002 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003003 */
Tejun Heo54766d42013-06-12 21:04:53 -07003004 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003005 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3006 if (&next->sibling != &pos->parent->children)
3007 return next;
3008 return NULL;
3009 }
3010
3011 /*
3012 * Can't dereference the next pointer. Each cgroup is given a
3013 * monotonically increasing unique serial number and always
3014 * appended to the sibling list, so the next one can be found by
3015 * walking the parent's children until we see a cgroup with higher
3016 * serial number than @pos's.
3017 *
3018 * While this path can be slow, it's taken only when either the
3019 * current cgroup is removed or iteration and removal race.
3020 */
3021 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3022 if (next->serial_nr > pos->serial_nr)
3023 return next;
3024 return NULL;
3025}
3026EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3027
3028/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003029 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3030 * @pos: the current position (%NULL to initiate traversal)
3031 * @cgroup: cgroup whose descendants to walk
3032 *
3033 * To be used by cgroup_for_each_descendant_pre(). Find the next
3034 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003035 *
3036 * While this function requires RCU read locking, it doesn't require the
3037 * whole traversal to be contained in a single RCU critical section. This
3038 * function will return the correct next descendant as long as both @pos
3039 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003040 */
3041struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3042 struct cgroup *cgroup)
3043{
3044 struct cgroup *next;
3045
3046 WARN_ON_ONCE(!rcu_read_lock_held());
3047
3048 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003049 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003050 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003051
3052 /* visit the first child if exists */
3053 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3054 if (next)
3055 return next;
3056
3057 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003058 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003059 next = cgroup_next_sibling(pos);
3060 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003062 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003063 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064
3065 return NULL;
3066}
3067EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3068
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003069/**
3070 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3071 * @pos: cgroup of interest
3072 *
3073 * Return the rightmost descendant of @pos. If there's no descendant,
3074 * @pos is returned. This can be used during pre-order traversal to skip
3075 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003076 *
3077 * While this function requires RCU read locking, it doesn't require the
3078 * whole traversal to be contained in a single RCU critical section. This
3079 * function will return the correct rightmost descendant as long as @pos is
3080 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003081 */
3082struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3083{
3084 struct cgroup *last, *tmp;
3085
3086 WARN_ON_ONCE(!rcu_read_lock_held());
3087
3088 do {
3089 last = pos;
3090 /* ->prev isn't RCU safe, walk ->next till the end */
3091 pos = NULL;
3092 list_for_each_entry_rcu(tmp, &last->children, sibling)
3093 pos = tmp;
3094 } while (pos);
3095
3096 return last;
3097}
3098EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3099
Tejun Heo574bd9f2012-11-09 09:12:29 -08003100static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3101{
3102 struct cgroup *last;
3103
3104 do {
3105 last = pos;
3106 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3107 sibling);
3108 } while (pos);
3109
3110 return last;
3111}
3112
3113/**
3114 * cgroup_next_descendant_post - find the next descendant for post-order walk
3115 * @pos: the current position (%NULL to initiate traversal)
3116 * @cgroup: cgroup whose descendants to walk
3117 *
3118 * To be used by cgroup_for_each_descendant_post(). Find the next
3119 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003120 *
3121 * While this function requires RCU read locking, it doesn't require the
3122 * whole traversal to be contained in a single RCU critical section. This
3123 * function will return the correct next descendant as long as both @pos
3124 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003125 */
3126struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3127 struct cgroup *cgroup)
3128{
3129 struct cgroup *next;
3130
3131 WARN_ON_ONCE(!rcu_read_lock_held());
3132
3133 /* if first iteration, visit the leftmost descendant */
3134 if (!pos) {
3135 next = cgroup_leftmost_descendant(cgroup);
3136 return next != cgroup ? next : NULL;
3137 }
3138
3139 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003140 next = cgroup_next_sibling(pos);
3141 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003142 return cgroup_leftmost_descendant(next);
3143
3144 /* no sibling left, visit parent */
3145 next = pos->parent;
3146 return next != cgroup ? next : NULL;
3147}
3148EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3149
Paul Menagebd89aab2007-10-18 23:40:44 -07003150void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003151 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003152{
3153 /*
3154 * The first time anyone tries to iterate across a cgroup,
3155 * we need to enable the list linking each css_set to its
3156 * tasks, and fix up all existing tasks.
3157 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003158 if (!use_task_css_set_links)
3159 cgroup_enable_task_cg_lists();
3160
Paul Menage817929e2007-10-18 23:39:36 -07003161 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003162 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003163 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003164}
3165
Paul Menagebd89aab2007-10-18 23:40:44 -07003166struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003167 struct cgroup_iter *it)
3168{
3169 struct task_struct *res;
3170 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003171 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003172
3173 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003174 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003175 return NULL;
3176 res = list_entry(l, struct task_struct, cg_list);
3177 /* Advance iterator to find next entry */
3178 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003179 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3180 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003181 /* We reached the end of this task list - move on to
3182 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003183 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003184 } else {
3185 it->task = l;
3186 }
3187 return res;
3188}
3189
Paul Menagebd89aab2007-10-18 23:40:44 -07003190void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003191 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003192{
3193 read_unlock(&css_set_lock);
3194}
3195
Cliff Wickman31a7df02008-02-07 00:14:42 -08003196static inline int started_after_time(struct task_struct *t1,
3197 struct timespec *time,
3198 struct task_struct *t2)
3199{
3200 int start_diff = timespec_compare(&t1->start_time, time);
3201 if (start_diff > 0) {
3202 return 1;
3203 } else if (start_diff < 0) {
3204 return 0;
3205 } else {
3206 /*
3207 * Arbitrarily, if two processes started at the same
3208 * time, we'll say that the lower pointer value
3209 * started first. Note that t2 may have exited by now
3210 * so this may not be a valid pointer any longer, but
3211 * that's fine - it still serves to distinguish
3212 * between two tasks started (effectively) simultaneously.
3213 */
3214 return t1 > t2;
3215 }
3216}
3217
3218/*
3219 * This function is a callback from heap_insert() and is used to order
3220 * the heap.
3221 * In this case we order the heap in descending task start time.
3222 */
3223static inline int started_after(void *p1, void *p2)
3224{
3225 struct task_struct *t1 = p1;
3226 struct task_struct *t2 = p2;
3227 return started_after_time(t1, &t2->start_time, t2);
3228}
3229
3230/**
3231 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3232 * @scan: struct cgroup_scanner containing arguments for the scan
3233 *
3234 * Arguments include pointers to callback functions test_task() and
3235 * process_task().
3236 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3237 * and if it returns true, call process_task() for it also.
3238 * The test_task pointer may be NULL, meaning always true (select all tasks).
3239 * Effectively duplicates cgroup_iter_{start,next,end}()
3240 * but does not lock css_set_lock for the call to process_task().
3241 * The struct cgroup_scanner may be embedded in any structure of the caller's
3242 * creation.
3243 * It is guaranteed that process_task() will act on every task that
3244 * is a member of the cgroup for the duration of this call. This
3245 * function may or may not call process_task() for tasks that exit
3246 * or move to a different cgroup during the call, or are forked or
3247 * move into the cgroup during the call.
3248 *
3249 * Note that test_task() may be called with locks held, and may in some
3250 * situations be called multiple times for the same task, so it should
3251 * be cheap.
3252 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3253 * pre-allocated and will be used for heap operations (and its "gt" member will
3254 * be overwritten), else a temporary heap will be used (allocation of which
3255 * may cause this function to fail).
3256 */
3257int cgroup_scan_tasks(struct cgroup_scanner *scan)
3258{
3259 int retval, i;
3260 struct cgroup_iter it;
3261 struct task_struct *p, *dropped;
3262 /* Never dereference latest_task, since it's not refcounted */
3263 struct task_struct *latest_task = NULL;
3264 struct ptr_heap tmp_heap;
3265 struct ptr_heap *heap;
3266 struct timespec latest_time = { 0, 0 };
3267
3268 if (scan->heap) {
3269 /* The caller supplied our heap and pre-allocated its memory */
3270 heap = scan->heap;
3271 heap->gt = &started_after;
3272 } else {
3273 /* We need to allocate our own heap memory */
3274 heap = &tmp_heap;
3275 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3276 if (retval)
3277 /* cannot allocate the heap */
3278 return retval;
3279 }
3280
3281 again:
3282 /*
3283 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3284 * to determine which are of interest, and using the scanner's
3285 * "process_task" callback to process any of them that need an update.
3286 * Since we don't want to hold any locks during the task updates,
3287 * gather tasks to be processed in a heap structure.
3288 * The heap is sorted by descending task start time.
3289 * If the statically-sized heap fills up, we overflow tasks that
3290 * started later, and in future iterations only consider tasks that
3291 * started after the latest task in the previous pass. This
3292 * guarantees forward progress and that we don't miss any tasks.
3293 */
3294 heap->size = 0;
3295 cgroup_iter_start(scan->cg, &it);
3296 while ((p = cgroup_iter_next(scan->cg, &it))) {
3297 /*
3298 * Only affect tasks that qualify per the caller's callback,
3299 * if he provided one
3300 */
3301 if (scan->test_task && !scan->test_task(p, scan))
3302 continue;
3303 /*
3304 * Only process tasks that started after the last task
3305 * we processed
3306 */
3307 if (!started_after_time(p, &latest_time, latest_task))
3308 continue;
3309 dropped = heap_insert(heap, p);
3310 if (dropped == NULL) {
3311 /*
3312 * The new task was inserted; the heap wasn't
3313 * previously full
3314 */
3315 get_task_struct(p);
3316 } else if (dropped != p) {
3317 /*
3318 * The new task was inserted, and pushed out a
3319 * different task
3320 */
3321 get_task_struct(p);
3322 put_task_struct(dropped);
3323 }
3324 /*
3325 * Else the new task was newer than anything already in
3326 * the heap and wasn't inserted
3327 */
3328 }
3329 cgroup_iter_end(scan->cg, &it);
3330
3331 if (heap->size) {
3332 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003333 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003334 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003335 latest_time = q->start_time;
3336 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003337 }
3338 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003339 scan->process_task(q, scan);
3340 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341 }
3342 /*
3343 * If we had to process any tasks at all, scan again
3344 * in case some of them were in the middle of forking
3345 * children that didn't get processed.
3346 * Not the most efficient way to do it, but it avoids
3347 * having to take callback_mutex in the fork path
3348 */
3349 goto again;
3350 }
3351 if (heap == &tmp_heap)
3352 heap_free(&tmp_heap);
3353 return 0;
3354}
3355
Tejun Heo8cc99342013-04-07 09:29:50 -07003356static void cgroup_transfer_one_task(struct task_struct *task,
3357 struct cgroup_scanner *scan)
3358{
3359 struct cgroup *new_cgroup = scan->data;
3360
Tejun Heo47cfcd02013-04-07 09:29:51 -07003361 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003362 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003363 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003364}
3365
3366/**
3367 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3368 * @to: cgroup to which the tasks will be moved
3369 * @from: cgroup in which the tasks currently reside
3370 */
3371int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3372{
3373 struct cgroup_scanner scan;
3374
3375 scan.cg = from;
3376 scan.test_task = NULL; /* select all tasks in cgroup */
3377 scan.process_task = cgroup_transfer_one_task;
3378 scan.heap = NULL;
3379 scan.data = to;
3380
3381 return cgroup_scan_tasks(&scan);
3382}
3383
Paul Menage817929e2007-10-18 23:39:36 -07003384/*
Ben Blum102a7752009-09-23 15:56:26 -07003385 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003386 *
3387 * Reading this file can return large amounts of data if a cgroup has
3388 * *lots* of attached tasks. So it may need several calls to read(),
3389 * but we cannot guarantee that the information we produce is correct
3390 * unless we produce it entirely atomically.
3391 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003393
Li Zefan24528252012-01-20 11:58:43 +08003394/* which pidlist file are we talking about? */
3395enum cgroup_filetype {
3396 CGROUP_FILE_PROCS,
3397 CGROUP_FILE_TASKS,
3398};
3399
3400/*
3401 * A pidlist is a list of pids that virtually represents the contents of one
3402 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3403 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3404 * to the cgroup.
3405 */
3406struct cgroup_pidlist {
3407 /*
3408 * used to find which pidlist is wanted. doesn't change as long as
3409 * this particular list stays in the list.
3410 */
3411 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3412 /* array of xids */
3413 pid_t *list;
3414 /* how many elements the above list has */
3415 int length;
3416 /* how many files are using the current array */
3417 int use_count;
3418 /* each of these stored in a list by its cgroup */
3419 struct list_head links;
3420 /* pointer to the cgroup we belong to, for list removal purposes */
3421 struct cgroup *owner;
3422 /* protects the other fields */
3423 struct rw_semaphore mutex;
3424};
3425
Paul Menagebbcb81d2007-10-18 23:39:32 -07003426/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003427 * The following two functions "fix" the issue where there are more pids
3428 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3429 * TODO: replace with a kernel-wide solution to this problem
3430 */
3431#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3432static void *pidlist_allocate(int count)
3433{
3434 if (PIDLIST_TOO_LARGE(count))
3435 return vmalloc(count * sizeof(pid_t));
3436 else
3437 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3438}
3439static void pidlist_free(void *p)
3440{
3441 if (is_vmalloc_addr(p))
3442 vfree(p);
3443 else
3444 kfree(p);
3445}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003446
3447/*
Ben Blum102a7752009-09-23 15:56:26 -07003448 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003449 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003450 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003451static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003452{
Ben Blum102a7752009-09-23 15:56:26 -07003453 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003454
3455 /*
3456 * we presume the 0th element is unique, so i starts at 1. trivial
3457 * edge cases first; no work needs to be done for either
3458 */
3459 if (length == 0 || length == 1)
3460 return length;
3461 /* src and dest walk down the list; dest counts unique elements */
3462 for (src = 1; src < length; src++) {
3463 /* find next unique element */
3464 while (list[src] == list[src-1]) {
3465 src++;
3466 if (src == length)
3467 goto after;
3468 }
3469 /* dest always points to where the next unique element goes */
3470 list[dest] = list[src];
3471 dest++;
3472 }
3473after:
Ben Blum102a7752009-09-23 15:56:26 -07003474 return dest;
3475}
3476
3477static int cmppid(const void *a, const void *b)
3478{
3479 return *(pid_t *)a - *(pid_t *)b;
3480}
3481
3482/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003483 * find the appropriate pidlist for our purpose (given procs vs tasks)
3484 * returns with the lock on that pidlist already held, and takes care
3485 * of the use count, or returns NULL with no locks held if we're out of
3486 * memory.
3487 */
3488static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3489 enum cgroup_filetype type)
3490{
3491 struct cgroup_pidlist *l;
3492 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003493 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003494
Ben Blum72a8cb32009-09-23 15:56:27 -07003495 /*
3496 * We can't drop the pidlist_mutex before taking the l->mutex in case
3497 * the last ref-holder is trying to remove l from the list at the same
3498 * time. Holding the pidlist_mutex precludes somebody taking whichever
3499 * list we find out from under us - compare release_pid_array().
3500 */
3501 mutex_lock(&cgrp->pidlist_mutex);
3502 list_for_each_entry(l, &cgrp->pidlists, links) {
3503 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003504 /* make sure l doesn't vanish out from under us */
3505 down_write(&l->mutex);
3506 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003507 return l;
3508 }
3509 }
3510 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003511 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003512 if (!l) {
3513 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003514 return l;
3515 }
3516 init_rwsem(&l->mutex);
3517 down_write(&l->mutex);
3518 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003519 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 l->owner = cgrp;
3521 list_add(&l->links, &cgrp->pidlists);
3522 mutex_unlock(&cgrp->pidlist_mutex);
3523 return l;
3524}
3525
3526/*
Ben Blum102a7752009-09-23 15:56:26 -07003527 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3528 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003529static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3530 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003531{
3532 pid_t *array;
3533 int length;
3534 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003535 struct cgroup_iter it;
3536 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003537 struct cgroup_pidlist *l;
3538
3539 /*
3540 * If cgroup gets more users after we read count, we won't have
3541 * enough space - tough. This race is indistinguishable to the
3542 * caller from the case that the additional cgroup users didn't
3543 * show up until sometime later on.
3544 */
3545 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003546 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003547 if (!array)
3548 return -ENOMEM;
3549 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003550 cgroup_iter_start(cgrp, &it);
3551 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003552 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003553 break;
Ben Blum102a7752009-09-23 15:56:26 -07003554 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003555 if (type == CGROUP_FILE_PROCS)
3556 pid = task_tgid_vnr(tsk);
3557 else
3558 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003559 if (pid > 0) /* make sure to only use valid results */
3560 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003561 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003562 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003563 length = n;
3564 /* now sort & (if procs) strip out duplicates */
3565 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003566 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003567 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003568 l = cgroup_pidlist_find(cgrp, type);
3569 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003570 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003571 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003572 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003573 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003574 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003575 l->list = array;
3576 l->length = length;
3577 l->use_count++;
3578 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003579 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003580 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003581}
3582
Balbir Singh846c7bb2007-10-18 23:39:44 -07003583/**
Li Zefana043e3b2008-02-23 15:24:09 -08003584 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003585 * @stats: cgroupstats to fill information into
3586 * @dentry: A dentry entry belonging to the cgroup for which stats have
3587 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003588 *
3589 * Build and fill cgroupstats so that taskstats can export it to user
3590 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003591 */
3592int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3593{
3594 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003595 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003596 struct cgroup_iter it;
3597 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003598
Balbir Singh846c7bb2007-10-18 23:39:44 -07003599 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003600 * Validate dentry by checking the superblock operations,
3601 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003602 */
Li Zefan33d283b2008-11-19 15:36:48 -08003603 if (dentry->d_sb->s_op != &cgroup_ops ||
3604 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003605 goto err;
3606
3607 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003608 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003609
Paul Menagebd89aab2007-10-18 23:40:44 -07003610 cgroup_iter_start(cgrp, &it);
3611 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003612 switch (tsk->state) {
3613 case TASK_RUNNING:
3614 stats->nr_running++;
3615 break;
3616 case TASK_INTERRUPTIBLE:
3617 stats->nr_sleeping++;
3618 break;
3619 case TASK_UNINTERRUPTIBLE:
3620 stats->nr_uninterruptible++;
3621 break;
3622 case TASK_STOPPED:
3623 stats->nr_stopped++;
3624 break;
3625 default:
3626 if (delayacct_is_task_waiting_on_io(tsk))
3627 stats->nr_io_wait++;
3628 break;
3629 }
3630 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003631 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003632
Balbir Singh846c7bb2007-10-18 23:39:44 -07003633err:
3634 return ret;
3635}
3636
Paul Menage8f3ff202009-09-23 15:56:25 -07003637
Paul Menagecc31edc2008-10-18 20:28:04 -07003638/*
Ben Blum102a7752009-09-23 15:56:26 -07003639 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003640 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003641 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003642 */
3643
Ben Blum102a7752009-09-23 15:56:26 -07003644static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003645{
3646 /*
3647 * Initially we receive a position value that corresponds to
3648 * one more than the last pid shown (or 0 on the first call or
3649 * after a seek to the start). Use a binary-search to find the
3650 * next pid to display, if any
3651 */
Ben Blum102a7752009-09-23 15:56:26 -07003652 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003653 int index = 0, pid = *pos;
3654 int *iter;
3655
Ben Blum102a7752009-09-23 15:56:26 -07003656 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003657 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003658 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003659
Paul Menagecc31edc2008-10-18 20:28:04 -07003660 while (index < end) {
3661 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003662 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003663 index = mid;
3664 break;
Ben Blum102a7752009-09-23 15:56:26 -07003665 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003666 index = mid + 1;
3667 else
3668 end = mid;
3669 }
3670 }
3671 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003672 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 return NULL;
3674 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003675 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 *pos = *iter;
3677 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003678}
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003681{
Ben Blum102a7752009-09-23 15:56:26 -07003682 struct cgroup_pidlist *l = s->private;
3683 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003684}
3685
Ben Blum102a7752009-09-23 15:56:26 -07003686static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003687{
Ben Blum102a7752009-09-23 15:56:26 -07003688 struct cgroup_pidlist *l = s->private;
3689 pid_t *p = v;
3690 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 /*
3692 * Advance to the next pid in the array. If this goes off the
3693 * end, we're done
3694 */
3695 p++;
3696 if (p >= end) {
3697 return NULL;
3698 } else {
3699 *pos = *p;
3700 return p;
3701 }
3702}
3703
Ben Blum102a7752009-09-23 15:56:26 -07003704static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003705{
3706 return seq_printf(s, "%d\n", *(int *)v);
3707}
3708
Ben Blum102a7752009-09-23 15:56:26 -07003709/*
3710 * seq_operations functions for iterating on pidlists through seq_file -
3711 * independent of whether it's tasks or procs
3712 */
3713static const struct seq_operations cgroup_pidlist_seq_operations = {
3714 .start = cgroup_pidlist_start,
3715 .stop = cgroup_pidlist_stop,
3716 .next = cgroup_pidlist_next,
3717 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003718};
3719
Ben Blum102a7752009-09-23 15:56:26 -07003720static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003721{
Ben Blum72a8cb32009-09-23 15:56:27 -07003722 /*
3723 * the case where we're the last user of this particular pidlist will
3724 * have us remove it from the cgroup's list, which entails taking the
3725 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3726 * pidlist_mutex, we have to take pidlist_mutex first.
3727 */
3728 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003729 down_write(&l->mutex);
3730 BUG_ON(!l->use_count);
3731 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003732 /* we're the last user if refcount is 0; remove and free */
3733 list_del(&l->links);
3734 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003735 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003736 put_pid_ns(l->key.ns);
3737 up_write(&l->mutex);
3738 kfree(l);
3739 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003740 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003741 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003742 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003743}
3744
Ben Blum102a7752009-09-23 15:56:26 -07003745static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003746{
Ben Blum102a7752009-09-23 15:56:26 -07003747 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003748 if (!(file->f_mode & FMODE_READ))
3749 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003750 /*
3751 * the seq_file will only be initialized if the file was opened for
3752 * reading; hence we check if it's not null only in that case.
3753 */
3754 l = ((struct seq_file *)file->private_data)->private;
3755 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003756 return seq_release(inode, file);
3757}
3758
Ben Blum102a7752009-09-23 15:56:26 -07003759static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003760 .read = seq_read,
3761 .llseek = seq_lseek,
3762 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003763 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003764};
3765
3766/*
Ben Blum102a7752009-09-23 15:56:26 -07003767 * The following functions handle opens on a file that displays a pidlist
3768 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3769 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003770 */
Ben Blum102a7752009-09-23 15:56:26 -07003771/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003772static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003773{
3774 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003775 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 int retval;
3777
3778 /* Nothing to do for write-only files */
3779 if (!(file->f_mode & FMODE_READ))
3780 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003781
Ben Blum102a7752009-09-23 15:56:26 -07003782 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003783 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003784 if (retval)
3785 return retval;
3786 /* configure file information */
3787 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003788
Ben Blum102a7752009-09-23 15:56:26 -07003789 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003791 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003792 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003793 }
Ben Blum102a7752009-09-23 15:56:26 -07003794 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003795 return 0;
3796}
Ben Blum102a7752009-09-23 15:56:26 -07003797static int cgroup_tasks_open(struct inode *unused, struct file *file)
3798{
Ben Blum72a8cb32009-09-23 15:56:27 -07003799 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003800}
3801static int cgroup_procs_open(struct inode *unused, struct file *file)
3802{
Ben Blum72a8cb32009-09-23 15:56:27 -07003803 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003804}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003805
Paul Menagebd89aab2007-10-18 23:40:44 -07003806static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003807 struct cftype *cft)
3808{
Paul Menagebd89aab2007-10-18 23:40:44 -07003809 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003810}
3811
Paul Menage6379c102008-07-25 01:47:01 -07003812static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3813 struct cftype *cft,
3814 u64 val)
3815{
3816 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3817 if (val)
3818 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3819 else
3820 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3821 return 0;
3822}
3823
Paul Menagebbcb81d2007-10-18 23:39:32 -07003824/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003825 * When dput() is called asynchronously, if umount has been done and
3826 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3827 * there's a small window that vfs will see the root dentry with non-zero
3828 * refcnt and trigger BUG().
3829 *
3830 * That's why we hold a reference before dput() and drop it right after.
3831 */
3832static void cgroup_dput(struct cgroup *cgrp)
3833{
3834 struct super_block *sb = cgrp->root->sb;
3835
3836 atomic_inc(&sb->s_active);
3837 dput(cgrp->dentry);
3838 deactivate_super(sb);
3839}
3840
3841/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003842 * Unregister event and free resources.
3843 *
3844 * Gets called from workqueue.
3845 */
3846static void cgroup_event_remove(struct work_struct *work)
3847{
3848 struct cgroup_event *event = container_of(work, struct cgroup_event,
3849 remove);
3850 struct cgroup *cgrp = event->cgrp;
3851
Li Zefan810cbee2013-02-18 18:56:14 +08003852 remove_wait_queue(event->wqh, &event->wait);
3853
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003854 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3855
Li Zefan810cbee2013-02-18 18:56:14 +08003856 /* Notify userspace the event is going away. */
3857 eventfd_signal(event->eventfd, 1);
3858
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003859 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003860 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003861 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003862}
3863
3864/*
3865 * Gets called on POLLHUP on eventfd when user closes it.
3866 *
3867 * Called with wqh->lock held and interrupts disabled.
3868 */
3869static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3870 int sync, void *key)
3871{
3872 struct cgroup_event *event = container_of(wait,
3873 struct cgroup_event, wait);
3874 struct cgroup *cgrp = event->cgrp;
3875 unsigned long flags = (unsigned long)key;
3876
3877 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003878 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003879 * If the event has been detached at cgroup removal, we
3880 * can simply return knowing the other side will cleanup
3881 * for us.
3882 *
3883 * We can't race against event freeing since the other
3884 * side will require wqh->lock via remove_wait_queue(),
3885 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003886 */
Li Zefan810cbee2013-02-18 18:56:14 +08003887 spin_lock(&cgrp->event_list_lock);
3888 if (!list_empty(&event->list)) {
3889 list_del_init(&event->list);
3890 /*
3891 * We are in atomic context, but cgroup_event_remove()
3892 * may sleep, so we have to call it in workqueue.
3893 */
3894 schedule_work(&event->remove);
3895 }
3896 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003897 }
3898
3899 return 0;
3900}
3901
3902static void cgroup_event_ptable_queue_proc(struct file *file,
3903 wait_queue_head_t *wqh, poll_table *pt)
3904{
3905 struct cgroup_event *event = container_of(pt,
3906 struct cgroup_event, pt);
3907
3908 event->wqh = wqh;
3909 add_wait_queue(wqh, &event->wait);
3910}
3911
3912/*
3913 * Parse input and register new cgroup event handler.
3914 *
3915 * Input must be in format '<event_fd> <control_fd> <args>'.
3916 * Interpretation of args is defined by control file implementation.
3917 */
3918static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3919 const char *buffer)
3920{
3921 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003922 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923 unsigned int efd, cfd;
3924 struct file *efile = NULL;
3925 struct file *cfile = NULL;
3926 char *endp;
3927 int ret;
3928
3929 efd = simple_strtoul(buffer, &endp, 10);
3930 if (*endp != ' ')
3931 return -EINVAL;
3932 buffer = endp + 1;
3933
3934 cfd = simple_strtoul(buffer, &endp, 10);
3935 if ((*endp != ' ') && (*endp != '\0'))
3936 return -EINVAL;
3937 buffer = endp + 1;
3938
3939 event = kzalloc(sizeof(*event), GFP_KERNEL);
3940 if (!event)
3941 return -ENOMEM;
3942 event->cgrp = cgrp;
3943 INIT_LIST_HEAD(&event->list);
3944 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3945 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3946 INIT_WORK(&event->remove, cgroup_event_remove);
3947
3948 efile = eventfd_fget(efd);
3949 if (IS_ERR(efile)) {
3950 ret = PTR_ERR(efile);
3951 goto fail;
3952 }
3953
3954 event->eventfd = eventfd_ctx_fileget(efile);
3955 if (IS_ERR(event->eventfd)) {
3956 ret = PTR_ERR(event->eventfd);
3957 goto fail;
3958 }
3959
3960 cfile = fget(cfd);
3961 if (!cfile) {
3962 ret = -EBADF;
3963 goto fail;
3964 }
3965
3966 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003967 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003968 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003969 if (ret < 0)
3970 goto fail;
3971
3972 event->cft = __file_cft(cfile);
3973 if (IS_ERR(event->cft)) {
3974 ret = PTR_ERR(event->cft);
3975 goto fail;
3976 }
3977
Li Zefanf1690072013-02-18 14:13:35 +08003978 /*
3979 * The file to be monitored must be in the same cgroup as
3980 * cgroup.event_control is.
3981 */
3982 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3983 if (cgrp_cfile != cgrp) {
3984 ret = -EINVAL;
3985 goto fail;
3986 }
3987
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003988 if (!event->cft->register_event || !event->cft->unregister_event) {
3989 ret = -EINVAL;
3990 goto fail;
3991 }
3992
3993 ret = event->cft->register_event(cgrp, event->cft,
3994 event->eventfd, buffer);
3995 if (ret)
3996 goto fail;
3997
Li Zefan7ef70e42013-04-26 11:58:03 -07003998 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004000 /*
4001 * Events should be removed after rmdir of cgroup directory, but before
4002 * destroying subsystem state objects. Let's take reference to cgroup
4003 * directory dentry to do that.
4004 */
4005 dget(cgrp->dentry);
4006
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004007 spin_lock(&cgrp->event_list_lock);
4008 list_add(&event->list, &cgrp->event_list);
4009 spin_unlock(&cgrp->event_list_lock);
4010
4011 fput(cfile);
4012 fput(efile);
4013
4014 return 0;
4015
4016fail:
4017 if (cfile)
4018 fput(cfile);
4019
4020 if (event && event->eventfd && !IS_ERR(event->eventfd))
4021 eventfd_ctx_put(event->eventfd);
4022
4023 if (!IS_ERR_OR_NULL(efile))
4024 fput(efile);
4025
4026 kfree(event);
4027
4028 return ret;
4029}
4030
Daniel Lezcano97978e62010-10-27 15:33:35 -07004031static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4032 struct cftype *cft)
4033{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004034 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004035}
4036
4037static int cgroup_clone_children_write(struct cgroup *cgrp,
4038 struct cftype *cft,
4039 u64 val)
4040{
4041 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004042 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004043 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004044 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004045 return 0;
4046}
4047
Tejun Heod5c56ce2013-06-03 19:14:34 -07004048static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004049 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004050 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004051 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004052 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004053 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004054 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004055 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004056 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004057 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004058 .write_string = cgroup_write_event_control,
4059 .mode = S_IWUGO,
4060 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004061 {
4062 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004063 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004064 .read_u64 = cgroup_clone_children_read,
4065 .write_u64 = cgroup_clone_children_write,
4066 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004067 {
Tejun Heo873fe092013-04-14 20:15:26 -07004068 .name = "cgroup.sane_behavior",
4069 .flags = CFTYPE_ONLY_ON_ROOT,
4070 .read_seq_string = cgroup_sane_behavior_show,
4071 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004072
4073 /*
4074 * Historical crazy stuff. These don't have "cgroup." prefix and
4075 * don't exist if sane_behavior. If you're depending on these, be
4076 * prepared to be burned.
4077 */
4078 {
4079 .name = "tasks",
4080 .flags = CFTYPE_INSANE, /* use "procs" instead */
4081 .open = cgroup_tasks_open,
4082 .write_u64 = cgroup_tasks_write,
4083 .release = cgroup_pidlist_release,
4084 .mode = S_IRUGO | S_IWUSR,
4085 },
4086 {
4087 .name = "notify_on_release",
4088 .flags = CFTYPE_INSANE,
4089 .read_u64 = cgroup_read_notify_on_release,
4090 .write_u64 = cgroup_write_notify_on_release,
4091 },
Tejun Heo873fe092013-04-14 20:15:26 -07004092 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004093 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004094 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004095 .read_seq_string = cgroup_release_agent_show,
4096 .write_string = cgroup_release_agent_write,
4097 .max_write_len = PATH_MAX,
4098 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004099 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004100};
4101
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004102/**
4103 * cgroup_populate_dir - selectively creation of files in a directory
4104 * @cgrp: target cgroup
4105 * @base_files: true if the base files should be added
4106 * @subsys_mask: mask of the subsystem ids whose files should be added
4107 */
4108static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4109 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004110{
4111 int err;
4112 struct cgroup_subsys *ss;
4113
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004114 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004115 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004116 if (err < 0)
4117 return err;
4118 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004119
Tejun Heo8e3f6542012-04-01 12:09:55 -07004120 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004121 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004122 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004123 if (!test_bit(ss->subsys_id, &subsys_mask))
4124 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004125
Tejun Heodb0416b2012-04-01 12:09:55 -07004126 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004127 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004129
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004130 /* This cgroup is ready now */
4131 for_each_subsys(cgrp->root, ss) {
4132 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4133 /*
4134 * Update id->css pointer and make this css visible from
4135 * CSS ID functions. This pointer will be dereferened
4136 * from RCU-read-side without locks.
4137 */
4138 if (css->id)
4139 rcu_assign_pointer(css->id->css, css);
4140 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141
4142 return 0;
4143}
4144
Tejun Heo48ddbe12012-04-01 12:09:56 -07004145static void css_dput_fn(struct work_struct *work)
4146{
4147 struct cgroup_subsys_state *css =
4148 container_of(work, struct cgroup_subsys_state, dput_work);
4149
Li Zefan1c8158e2013-06-18 18:41:10 +08004150 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004151}
4152
Tejun Heod3daf282013-06-13 19:39:16 -07004153static void css_release(struct percpu_ref *ref)
4154{
4155 struct cgroup_subsys_state *css =
4156 container_of(ref, struct cgroup_subsys_state, refcnt);
4157
4158 schedule_work(&css->dput_work);
4159}
4160
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161static void init_cgroup_css(struct cgroup_subsys_state *css,
4162 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004163 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004164{
Paul Menagebd89aab2007-10-18 23:40:44 -07004165 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004166 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004167 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004168 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004169 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004170 BUG_ON(cgrp->subsys[ss->subsys_id]);
4171 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004172
4173 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004174 * css holds an extra ref to @cgrp->dentry which is put on the last
4175 * css_put(). dput() requires process context, which css_put() may
4176 * be called without. @css->dput_work will be used to invoke
4177 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004178 */
4179 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180}
4181
Tejun Heob1929db2012-11-19 08:13:38 -08004182/* invoke ->post_create() on a new CSS and mark it online if successful */
4183static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004184{
Tejun Heob1929db2012-11-19 08:13:38 -08004185 int ret = 0;
4186
Tejun Heoa31f2d32012-11-19 08:13:37 -08004187 lockdep_assert_held(&cgroup_mutex);
4188
Tejun Heo92fb9742012-11-19 08:13:38 -08004189 if (ss->css_online)
4190 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004191 if (!ret)
4192 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4193 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004194}
4195
4196/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4197static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4198 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4199{
4200 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4201
4202 lockdep_assert_held(&cgroup_mutex);
4203
4204 if (!(css->flags & CSS_ONLINE))
4205 return;
4206
Li Zefand7eeac12013-03-12 15:35:59 -07004207 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004208 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004209
4210 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4211}
4212
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213/*
Li Zefana043e3b2008-02-23 15:24:09 -08004214 * cgroup_create - create a cgroup
4215 * @parent: cgroup that will be parent of the new cgroup
4216 * @dentry: dentry of the new cgroup
4217 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004218 *
Li Zefana043e3b2008-02-23 15:24:09 -08004219 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004222 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223{
Tejun Heo53fa5262013-05-24 10:55:38 +09004224 static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0);
Paul Menagebd89aab2007-10-18 23:40:44 -07004225 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004226 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 struct cgroupfs_root *root = parent->root;
4228 int err = 0;
4229 struct cgroup_subsys *ss;
4230 struct super_block *sb = root->sb;
4231
Tejun Heo0a950f62012-11-19 09:02:12 -08004232 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004233 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4234 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235 return -ENOMEM;
4236
Li Zefan65dff752013-03-01 15:01:56 +08004237 name = cgroup_alloc_name(dentry);
4238 if (!name)
4239 goto err_free_cgrp;
4240 rcu_assign_pointer(cgrp->name, name);
4241
Tejun Heo0a950f62012-11-19 09:02:12 -08004242 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4243 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004244 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004245
Tejun Heo976c06b2012-11-05 09:16:59 -08004246 /*
4247 * Only live parents can have children. Note that the liveliness
4248 * check isn't strictly necessary because cgroup_mkdir() and
4249 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4250 * anyway so that locking is contained inside cgroup proper and we
4251 * don't get nasty surprises if we ever grow another caller.
4252 */
4253 if (!cgroup_lock_live_group(parent)) {
4254 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004255 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004256 }
4257
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258 /* Grab a reference on the superblock so the hierarchy doesn't
4259 * get deleted on unmount if there are child cgroups. This
4260 * can be done outside cgroup_mutex, since the sb can't
4261 * disappear while someone has an open control file on the
4262 * fs */
4263 atomic_inc(&sb->s_active);
4264
Paul Menagecc31edc2008-10-18 20:28:04 -07004265 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004266
Li Zefanfe1c06c2013-01-24 14:30:22 +08004267 dentry->d_fsdata = cgrp;
4268 cgrp->dentry = dentry;
4269
Paul Menagebd89aab2007-10-18 23:40:44 -07004270 cgrp->parent = parent;
4271 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272
Li Zefanb6abdb02008-03-04 14:28:19 -08004273 if (notify_on_release(parent))
4274 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4275
Tejun Heo2260e7f2012-11-19 08:13:38 -08004276 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4277 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004278
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004280 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004281
Tejun Heo92fb9742012-11-19 08:13:38 -08004282 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283 if (IS_ERR(css)) {
4284 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004285 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286 }
Tejun Heod3daf282013-06-13 19:39:16 -07004287
4288 err = percpu_ref_init(&css->refcnt, css_release);
4289 if (err)
4290 goto err_free_all;
4291
Paul Menagebd89aab2007-10-18 23:40:44 -07004292 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004293
Li Zefan4528fd02010-02-02 13:44:10 -08004294 if (ss->use_id) {
4295 err = alloc_css_id(ss, parent, cgrp);
4296 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004297 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004298 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299 }
4300
Tejun Heo4e139af2012-11-19 08:13:36 -08004301 /*
4302 * Create directory. cgroup_create_file() returns with the new
4303 * directory locked on success so that it can be populated without
4304 * dropping cgroup_mutex.
4305 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004306 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004308 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004309 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004310
Tejun Heo53fa5262013-05-24 10:55:38 +09004311 /*
4312 * Assign a monotonically increasing serial number. With the list
4313 * appending below, it guarantees that sibling cgroups are always
4314 * sorted in the ascending serial number order on the parent's
4315 * ->children.
4316 */
4317 cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor);
4318
Tejun Heo4e139af2012-11-19 08:13:36 -08004319 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004320 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4321 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4322 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004323
Tejun Heob1929db2012-11-19 08:13:38 -08004324 /* each css holds a ref to the cgroup's dentry */
4325 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004326 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004327
Li Zefan415cf072013-04-08 14:35:02 +08004328 /* hold a ref to the parent's dentry */
4329 dget(parent->dentry);
4330
Tejun Heob1929db2012-11-19 08:13:38 -08004331 /* creation succeeded, notify subsystems */
4332 for_each_subsys(root, ss) {
4333 err = online_css(ss, cgrp);
4334 if (err)
4335 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004336
4337 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4338 parent->parent) {
4339 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",
4340 current->comm, current->pid, ss->name);
4341 if (!strcmp(ss->name, "memory"))
4342 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4343 ss->warned_broken_hierarchy = true;
4344 }
Tejun Heoa8638032012-11-09 09:12:29 -08004345 }
4346
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004347 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004348 if (err)
4349 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004350
4351 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004352 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004353
4354 return 0;
4355
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004356err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004357 for_each_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004358 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4359
4360 if (css) {
4361 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004362 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004363 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004364 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 /* Release the reference count that we took on the superblock */
4367 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004368err_free_id:
4369 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004370err_free_name:
4371 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004372err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004373 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004375
4376err_destroy:
4377 cgroup_destroy_locked(cgrp);
4378 mutex_unlock(&cgroup_mutex);
4379 mutex_unlock(&dentry->d_inode->i_mutex);
4380 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381}
4382
Al Viro18bb1db2011-07-26 01:41:39 -04004383static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384{
4385 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4386
4387 /* the vfs holds inode->i_mutex already */
4388 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4389}
4390
Tejun Heod3daf282013-06-13 19:39:16 -07004391static void cgroup_css_killed(struct cgroup *cgrp)
4392{
4393 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4394 return;
4395
4396 /* percpu ref's of all css's are killed, kick off the next step */
4397 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4398 schedule_work(&cgrp->destroy_work);
4399}
4400
4401static void css_ref_killed_fn(struct percpu_ref *ref)
4402{
4403 struct cgroup_subsys_state *css =
4404 container_of(ref, struct cgroup_subsys_state, refcnt);
4405
4406 cgroup_css_killed(css->cgroup);
4407}
4408
4409/**
4410 * cgroup_destroy_locked - the first stage of cgroup destruction
4411 * @cgrp: cgroup to be destroyed
4412 *
4413 * css's make use of percpu refcnts whose killing latency shouldn't be
4414 * exposed to userland and are RCU protected. Also, cgroup core needs to
4415 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4416 * invoked. To satisfy all the requirements, destruction is implemented in
4417 * the following two steps.
4418 *
4419 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4420 * userland visible parts and start killing the percpu refcnts of
4421 * css's. Set up so that the next stage will be kicked off once all
4422 * the percpu refcnts are confirmed to be killed.
4423 *
4424 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4425 * rest of destruction. Once all cgroup references are gone, the
4426 * cgroup is RCU-freed.
4427 *
4428 * This function implements s1. After this step, @cgrp is gone as far as
4429 * the userland is concerned and a new cgroup with the same name may be
4430 * created. As cgroup doesn't care about the names internally, this
4431 * doesn't cause any problem.
4432 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004433static int cgroup_destroy_locked(struct cgroup *cgrp)
4434 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004435{
Tejun Heo42809dd2012-11-19 08:13:37 -08004436 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004437 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004438 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004439 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440
Tejun Heo42809dd2012-11-19 08:13:37 -08004441 lockdep_assert_held(&d->d_inode->i_mutex);
4442 lockdep_assert_held(&cgroup_mutex);
4443
Tejun Heoddd69142013-06-12 21:04:54 -07004444 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004445 * css_set_lock synchronizes access to ->cset_links and prevents
4446 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004447 */
4448 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004449 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004450 read_unlock(&css_set_lock);
4451 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004453
Tejun Heo1a90dd52012-11-05 09:16:59 -08004454 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004455 * Block new css_tryget() by killing css refcnts. cgroup core
4456 * guarantees that, by the time ->css_offline() is invoked, no new
4457 * css reference will be given out via css_tryget(). We can't
4458 * simply call percpu_ref_kill() and proceed to offlining css's
4459 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4460 * as killed on all CPUs on return.
4461 *
4462 * Use percpu_ref_kill_and_confirm() to get notifications as each
4463 * css is confirmed to be seen as killed on all CPUs. The
4464 * notification callback keeps track of the number of css's to be
4465 * killed and schedules cgroup_offline_fn() to perform the rest of
4466 * destruction once the percpu refs of all css's are confirmed to
4467 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004468 */
Tejun Heod3daf282013-06-13 19:39:16 -07004469 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heoed9577932012-11-05 09:16:58 -08004470 for_each_subsys(cgrp->root, ss) {
4471 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4472
Tejun Heod3daf282013-06-13 19:39:16 -07004473 /*
4474 * Killing would put the base ref, but we need to keep it
4475 * alive until after ->css_offline.
4476 */
4477 percpu_ref_get(&css->refcnt);
4478
4479 atomic_inc(&cgrp->css_kill_cnt);
4480 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004481 }
Tejun Heod3daf282013-06-13 19:39:16 -07004482 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004483
4484 /*
4485 * Mark @cgrp dead. This prevents further task migration and child
4486 * creation by disabling cgroup_lock_live_group(). Note that
4487 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4488 * resume iteration after dropping RCU read lock. See
4489 * cgroup_next_sibling() for details.
4490 */
Tejun Heo54766d42013-06-12 21:04:53 -07004491 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004492
Tejun Heo455050d2013-06-13 19:27:41 -07004493 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4494 raw_spin_lock(&release_list_lock);
4495 if (!list_empty(&cgrp->release_list))
4496 list_del_init(&cgrp->release_list);
4497 raw_spin_unlock(&release_list_lock);
4498
4499 /*
4500 * Remove @cgrp directory. The removal puts the base ref but we
4501 * aren't quite done with @cgrp yet, so hold onto it.
4502 */
4503 dget(d);
4504 cgroup_d_remove_dir(d);
4505
4506 /*
4507 * Unregister events and notify userspace.
4508 * Notify userspace about cgroup removing only after rmdir of cgroup
4509 * directory to avoid race between userspace and kernelspace.
4510 */
4511 spin_lock(&cgrp->event_list_lock);
4512 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4513 list_del_init(&event->list);
4514 schedule_work(&event->remove);
4515 }
4516 spin_unlock(&cgrp->event_list_lock);
4517
Tejun Heoea15f8c2013-06-13 19:27:42 -07004518 return 0;
4519};
4520
Tejun Heod3daf282013-06-13 19:39:16 -07004521/**
4522 * cgroup_offline_fn - the second step of cgroup destruction
4523 * @work: cgroup->destroy_free_work
4524 *
4525 * This function is invoked from a work item for a cgroup which is being
4526 * destroyed after the percpu refcnts of all css's are guaranteed to be
4527 * seen as killed on all CPUs, and performs the rest of destruction. This
4528 * is the second step of destruction described in the comment above
4529 * cgroup_destroy_locked().
4530 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004531static void cgroup_offline_fn(struct work_struct *work)
4532{
4533 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4534 struct cgroup *parent = cgrp->parent;
4535 struct dentry *d = cgrp->dentry;
4536 struct cgroup_subsys *ss;
4537
4538 mutex_lock(&cgroup_mutex);
4539
Tejun Heod3daf282013-06-13 19:39:16 -07004540 /*
4541 * css_tryget() is guaranteed to fail now. Tell subsystems to
4542 * initate destruction.
4543 */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004544 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004545 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004546
4547 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004548 * Put the css refs from cgroup_destroy_locked(). Each css holds
4549 * an extra reference to the cgroup's dentry and cgroup removal
4550 * proceeds regardless of css refs. On the last put of each css,
4551 * whenever that may be, the extra dentry ref is put so that dentry
4552 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004553 */
Tejun Heoe9316082012-11-05 09:16:58 -08004554 for_each_subsys(cgrp->root, ss)
4555 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556
Paul Menage999cd8a2009-01-07 18:08:36 -08004557 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004558 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004559 list_del_init(&cgrp->allcg_node);
4560
Paul Menageddbcc7e2007-10-18 23:39:30 -07004561 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562
Paul Menagebd89aab2007-10-18 23:40:44 -07004563 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004564 check_for_release(parent);
4565
Tejun Heoea15f8c2013-06-13 19:27:42 -07004566 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004567}
4568
Tejun Heo42809dd2012-11-19 08:13:37 -08004569static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4570{
4571 int ret;
4572
4573 mutex_lock(&cgroup_mutex);
4574 ret = cgroup_destroy_locked(dentry->d_fsdata);
4575 mutex_unlock(&cgroup_mutex);
4576
4577 return ret;
4578}
4579
Tejun Heo8e3f6542012-04-01 12:09:55 -07004580static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4581{
4582 INIT_LIST_HEAD(&ss->cftsets);
4583
4584 /*
4585 * base_cftset is embedded in subsys itself, no need to worry about
4586 * deregistration.
4587 */
4588 if (ss->base_cftypes) {
4589 ss->base_cftset.cfts = ss->base_cftypes;
4590 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4591 }
4592}
4593
Li Zefan06a11922008-04-29 01:00:07 -07004594static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004595{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004597
4598 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004599
Tejun Heo648bb562012-11-19 08:13:36 -08004600 mutex_lock(&cgroup_mutex);
4601
Tejun Heo8e3f6542012-04-01 12:09:55 -07004602 /* init base cftset */
4603 cgroup_init_cftsets(ss);
4604
Paul Menageddbcc7e2007-10-18 23:39:30 -07004605 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004606 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004608 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609 /* We don't handle early failures gracefully */
4610 BUG_ON(IS_ERR(css));
4611 init_cgroup_css(css, ss, dummytop);
4612
Li Zefane8d55fd2008-04-29 01:00:13 -07004613 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004614 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004615 * newly registered, all tasks and hence the
4616 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004617 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004618
4619 need_forkexit_callback |= ss->fork || ss->exit;
4620
Li Zefane8d55fd2008-04-29 01:00:13 -07004621 /* At system boot, before all subsystems have been
4622 * registered, no tasks have been forked, so we don't
4623 * need to invoke fork callbacks here. */
4624 BUG_ON(!list_empty(&init_task.tasks));
4625
Tejun Heob1929db2012-11-19 08:13:38 -08004626 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004627
Tejun Heo648bb562012-11-19 08:13:36 -08004628 mutex_unlock(&cgroup_mutex);
4629
Ben Blume6a11052010-03-10 15:22:09 -08004630 /* this function shouldn't be used with modular subsystems, since they
4631 * need to register a subsys_id, among other things */
4632 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004633}
4634
4635/**
Ben Blume6a11052010-03-10 15:22:09 -08004636 * cgroup_load_subsys: load and register a modular subsystem at runtime
4637 * @ss: the subsystem to load
4638 *
4639 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004640 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004641 * up for use. If the subsystem is built-in anyway, work is delegated to the
4642 * simpler cgroup_init_subsys.
4643 */
4644int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4645{
Ben Blume6a11052010-03-10 15:22:09 -08004646 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004647 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004648 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004649 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004650 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004651
4652 /* check name and function validity */
4653 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004654 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004655 return -EINVAL;
4656
4657 /*
4658 * we don't support callbacks in modular subsystems. this check is
4659 * before the ss->module check for consistency; a subsystem that could
4660 * be a module should still have no callbacks even if the user isn't
4661 * compiling it as one.
4662 */
4663 if (ss->fork || ss->exit)
4664 return -EINVAL;
4665
4666 /*
4667 * an optionally modular subsystem is built-in: we want to do nothing,
4668 * since cgroup_init_subsys will have already taken care of it.
4669 */
4670 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004671 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004672 BUG_ON(subsys[ss->subsys_id] != ss);
4673 return 0;
4674 }
4675
Tejun Heo8e3f6542012-04-01 12:09:55 -07004676 /* init base cftset */
4677 cgroup_init_cftsets(ss);
4678
Ben Blume6a11052010-03-10 15:22:09 -08004679 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004680 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004681
4682 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004683 * no ss->css_alloc seems to need anything important in the ss
4684 * struct, so this can happen first (i.e. before the rootnode
4685 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004686 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004687 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004688 if (IS_ERR(css)) {
4689 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004690 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004691 mutex_unlock(&cgroup_mutex);
4692 return PTR_ERR(css);
4693 }
4694
4695 list_add(&ss->sibling, &rootnode.subsys_list);
4696 ss->root = &rootnode;
4697
4698 /* our new subsystem will be attached to the dummy hierarchy. */
4699 init_cgroup_css(css, ss, dummytop);
4700 /* init_idr must be after init_cgroup_css because it sets css->id. */
4701 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004702 ret = cgroup_init_idr(ss, css);
4703 if (ret)
4704 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004705 }
4706
4707 /*
4708 * Now we need to entangle the css into the existing css_sets. unlike
4709 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4710 * will need a new pointer to it; done by iterating the css_set_table.
4711 * furthermore, modifying the existing css_sets will corrupt the hash
4712 * table state, so each changed css_set will need its hash recomputed.
4713 * this is all done under the css_set_lock.
4714 */
4715 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004716 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004717 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004718 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004719 continue;
4720 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004721 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004722 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004723 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004724 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004725 key = css_set_hash(cset->subsys);
4726 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004727 }
4728 write_unlock(&css_set_lock);
4729
Tejun Heob1929db2012-11-19 08:13:38 -08004730 ret = online_css(ss, dummytop);
4731 if (ret)
4732 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004733
Ben Blume6a11052010-03-10 15:22:09 -08004734 /* success! */
4735 mutex_unlock(&cgroup_mutex);
4736 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004737
4738err_unload:
4739 mutex_unlock(&cgroup_mutex);
4740 /* @ss can't be mounted here as try_module_get() would fail */
4741 cgroup_unload_subsys(ss);
4742 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004743}
4744EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4745
4746/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004747 * cgroup_unload_subsys: unload a modular subsystem
4748 * @ss: the subsystem to unload
4749 *
4750 * This function should be called in a modular subsystem's exitcall. When this
4751 * function is invoked, the refcount on the subsystem's module will be 0, so
4752 * the subsystem will not be attached to any hierarchy.
4753 */
4754void cgroup_unload_subsys(struct cgroup_subsys *ss)
4755{
Tejun Heo69d02062013-06-12 21:04:50 -07004756 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004757
4758 BUG_ON(ss->module == NULL);
4759
4760 /*
4761 * we shouldn't be called if the subsystem is in use, and the use of
4762 * try_module_get in parse_cgroupfs_options should ensure that it
4763 * doesn't start being used while we're killing it off.
4764 */
4765 BUG_ON(ss->root != &rootnode);
4766
4767 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004768
Tejun Heoa31f2d32012-11-19 08:13:37 -08004769 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004770
Tejun Heoc897ff62013-02-27 17:03:49 -08004771 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004772 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004773
Ben Blumcf5d5942010-03-10 15:22:09 -08004774 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004775 subsys[ss->subsys_id] = NULL;
4776
4777 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004778 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004779
4780 /*
4781 * disentangle the css from all css_sets attached to the dummytop. as
4782 * in loading, we need to pay our respects to the hashtable gods.
4783 */
4784 write_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004785 list_for_each_entry(link, &dummytop->cset_links, cset_link) {
4786 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004787 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004788
Tejun Heo5abb8852013-06-12 21:04:49 -07004789 hash_del(&cset->hlist);
4790 cset->subsys[ss->subsys_id] = NULL;
4791 key = css_set_hash(cset->subsys);
4792 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004793 }
4794 write_unlock(&css_set_lock);
4795
4796 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004797 * remove subsystem's css from the dummytop and free it - need to
4798 * free before marking as null because ss->css_free needs the
4799 * cgrp->subsys pointer to find their state. note that this also
4800 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004801 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004802 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004803 dummytop->subsys[ss->subsys_id] = NULL;
4804
4805 mutex_unlock(&cgroup_mutex);
4806}
4807EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4808
4809/**
Li Zefana043e3b2008-02-23 15:24:09 -08004810 * cgroup_init_early - cgroup initialization at system boot
4811 *
4812 * Initialize cgroups at system boot, and initialize any
4813 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004814 */
4815int __init cgroup_init_early(void)
4816{
4817 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004818 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004819 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004820 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004821 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004822 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004823 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004824 root_count = 1;
4825 init_task.cgroups = &init_css_set;
4826
Tejun Heo69d02062013-06-12 21:04:50 -07004827 init_cgrp_cset_link.cset = &init_css_set;
4828 init_cgrp_cset_link.cgrp = dummytop;
4829 list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links);
4830 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004831
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004832 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004833 struct cgroup_subsys *ss = subsys[i];
4834
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004835 /* at bootup time, we don't worry about modular subsystems */
4836 if (!ss || ss->module)
4837 continue;
4838
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839 BUG_ON(!ss->name);
4840 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004841 BUG_ON(!ss->css_alloc);
4842 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004843 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004844 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004845 ss->name, ss->subsys_id);
4846 BUG();
4847 }
4848
4849 if (ss->early_init)
4850 cgroup_init_subsys(ss);
4851 }
4852 return 0;
4853}
4854
4855/**
Li Zefana043e3b2008-02-23 15:24:09 -08004856 * cgroup_init - cgroup initialization
4857 *
4858 * Register cgroup filesystem and /proc file, and initialize
4859 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860 */
4861int __init cgroup_init(void)
4862{
4863 int err;
4864 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004865 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004866
4867 err = bdi_init(&cgroup_backing_dev_info);
4868 if (err)
4869 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004870
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004871 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004872 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004873
4874 /* at bootup time, we don't worry about modular subsystems */
4875 if (!ss || ss->module)
4876 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004877 if (!ss->early_init)
4878 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004879 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004880 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004881 }
4882
Li Zefan472b1052008-04-29 01:00:11 -07004883 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004884 key = css_set_hash(init_css_set.subsys);
4885 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004886
4887 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004888 mutex_lock(&cgroup_mutex);
4889 mutex_lock(&cgroup_root_mutex);
4890
Tejun Heofa3ca072013-04-14 11:36:56 -07004891 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004892
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004893 mutex_unlock(&cgroup_root_mutex);
4894 mutex_unlock(&cgroup_mutex);
4895
Greg KH676db4a2010-08-05 13:53:35 -07004896 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4897 if (!cgroup_kobj) {
4898 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004900 }
4901
4902 err = register_filesystem(&cgroup_fs_type);
4903 if (err < 0) {
4904 kobject_put(cgroup_kobj);
4905 goto out;
4906 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004907
Li Zefan46ae2202008-04-29 01:00:08 -07004908 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004909
Paul Menageddbcc7e2007-10-18 23:39:30 -07004910out:
Paul Menagea4243162007-10-18 23:39:35 -07004911 if (err)
4912 bdi_destroy(&cgroup_backing_dev_info);
4913
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914 return err;
4915}
Paul Menageb4f48b62007-10-18 23:39:33 -07004916
Paul Menagea4243162007-10-18 23:39:35 -07004917/*
4918 * proc_cgroup_show()
4919 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4920 * - Used for /proc/<pid>/cgroup.
4921 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4922 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004923 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004924 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4925 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4926 * cgroup to top_cgroup.
4927 */
4928
4929/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004930int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004931{
4932 struct pid *pid;
4933 struct task_struct *tsk;
4934 char *buf;
4935 int retval;
4936 struct cgroupfs_root *root;
4937
4938 retval = -ENOMEM;
4939 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4940 if (!buf)
4941 goto out;
4942
4943 retval = -ESRCH;
4944 pid = m->private;
4945 tsk = get_pid_task(pid, PIDTYPE_PID);
4946 if (!tsk)
4947 goto out_free;
4948
4949 retval = 0;
4950
4951 mutex_lock(&cgroup_mutex);
4952
Li Zefane5f6a862009-01-07 18:07:41 -08004953 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004954 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004955 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004956 int count = 0;
4957
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004958 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004959 for_each_subsys(root, ss)
4960 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004961 if (strlen(root->name))
4962 seq_printf(m, "%sname=%s", count ? "," : "",
4963 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004964 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004965 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004966 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004967 if (retval < 0)
4968 goto out_unlock;
4969 seq_puts(m, buf);
4970 seq_putc(m, '\n');
4971 }
4972
4973out_unlock:
4974 mutex_unlock(&cgroup_mutex);
4975 put_task_struct(tsk);
4976out_free:
4977 kfree(buf);
4978out:
4979 return retval;
4980}
4981
Paul Menagea4243162007-10-18 23:39:35 -07004982/* Display information about each subsystem and each hierarchy */
4983static int proc_cgroupstats_show(struct seq_file *m, void *v)
4984{
4985 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004986
Paul Menage8bab8dd2008-04-04 14:29:57 -07004987 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004988 /*
4989 * ideally we don't want subsystems moving around while we do this.
4990 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4991 * subsys/hierarchy state.
4992 */
Paul Menagea4243162007-10-18 23:39:35 -07004993 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004994 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4995 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004996 if (ss == NULL)
4997 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004998 seq_printf(m, "%s\t%d\t%d\t%d\n",
4999 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005000 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07005001 }
5002 mutex_unlock(&cgroup_mutex);
5003 return 0;
5004}
5005
5006static int cgroupstats_open(struct inode *inode, struct file *file)
5007{
Al Viro9dce07f2008-03-29 03:07:28 +00005008 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005009}
5010
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005011static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005012 .open = cgroupstats_open,
5013 .read = seq_read,
5014 .llseek = seq_lseek,
5015 .release = single_release,
5016};
5017
Paul Menageb4f48b62007-10-18 23:39:33 -07005018/**
5019 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005020 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005021 *
5022 * Description: A task inherits its parent's cgroup at fork().
5023 *
5024 * A pointer to the shared css_set was automatically copied in
5025 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005026 * it was not made under the protection of RCU or cgroup_mutex, so
5027 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5028 * have already changed current->cgroups, allowing the previously
5029 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005030 *
5031 * At the point that cgroup_fork() is called, 'current' is the parent
5032 * task, and the passed argument 'child' points to the child task.
5033 */
5034void cgroup_fork(struct task_struct *child)
5035{
Tejun Heo9bb71302012-10-18 17:52:07 -07005036 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005037 child->cgroups = current->cgroups;
5038 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005039 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005040 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005041}
5042
5043/**
Li Zefana043e3b2008-02-23 15:24:09 -08005044 * cgroup_post_fork - called on a new task after adding it to the task list
5045 * @child: the task in question
5046 *
Tejun Heo5edee612012-10-16 15:03:14 -07005047 * Adds the task to the list running through its css_set if necessary and
5048 * call the subsystem fork() callbacks. Has to be after the task is
5049 * visible on the task list in case we race with the first call to
5050 * cgroup_iter_start() - to guarantee that the new task ends up on its
5051 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005052 */
Paul Menage817929e2007-10-18 23:39:36 -07005053void cgroup_post_fork(struct task_struct *child)
5054{
Tejun Heo5edee612012-10-16 15:03:14 -07005055 int i;
5056
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005057 /*
5058 * use_task_css_set_links is set to 1 before we walk the tasklist
5059 * under the tasklist_lock and we read it here after we added the child
5060 * to the tasklist under the tasklist_lock as well. If the child wasn't
5061 * yet in the tasklist when we walked through it from
5062 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5063 * should be visible now due to the paired locking and barriers implied
5064 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5065 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5066 * lock on fork.
5067 */
Paul Menage817929e2007-10-18 23:39:36 -07005068 if (use_task_css_set_links) {
5069 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005070 task_lock(child);
5071 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005072 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005073 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005074 write_unlock(&css_set_lock);
5075 }
Tejun Heo5edee612012-10-16 15:03:14 -07005076
5077 /*
5078 * Call ss->fork(). This must happen after @child is linked on
5079 * css_set; otherwise, @child might change state between ->fork()
5080 * and addition to css_set.
5081 */
5082 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005083 /*
5084 * fork/exit callbacks are supported only for builtin
5085 * subsystems, and the builtin section of the subsys
5086 * array is immutable, so we don't need to lock the
5087 * subsys array here. On the other hand, modular section
5088 * of the array can be freed at module unload, so we
5089 * can't touch that.
5090 */
5091 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07005092 struct cgroup_subsys *ss = subsys[i];
5093
Tejun Heo5edee612012-10-16 15:03:14 -07005094 if (ss->fork)
5095 ss->fork(child);
5096 }
5097 }
Paul Menage817929e2007-10-18 23:39:36 -07005098}
Tejun Heo5edee612012-10-16 15:03:14 -07005099
Paul Menage817929e2007-10-18 23:39:36 -07005100/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005101 * cgroup_exit - detach cgroup from exiting task
5102 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005103 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005104 *
5105 * Description: Detach cgroup from @tsk and release it.
5106 *
5107 * Note that cgroups marked notify_on_release force every task in
5108 * them to take the global cgroup_mutex mutex when exiting.
5109 * This could impact scaling on very large systems. Be reluctant to
5110 * use notify_on_release cgroups where very high task exit scaling
5111 * is required on large systems.
5112 *
5113 * the_top_cgroup_hack:
5114 *
5115 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5116 *
5117 * We call cgroup_exit() while the task is still competent to
5118 * handle notify_on_release(), then leave the task attached to the
5119 * root cgroup in each hierarchy for the remainder of its exit.
5120 *
5121 * To do this properly, we would increment the reference count on
5122 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5123 * code we would add a second cgroup function call, to drop that
5124 * reference. This would just create an unnecessary hot spot on
5125 * the top_cgroup reference count, to no avail.
5126 *
5127 * Normally, holding a reference to a cgroup without bumping its
5128 * count is unsafe. The cgroup could go away, or someone could
5129 * attach us to a different cgroup, decrementing the count on
5130 * the first cgroup that we never incremented. But in this case,
5131 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005132 * which wards off any cgroup_attach_task() attempts, or task is a failed
5133 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005134 */
5135void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5136{
Tejun Heo5abb8852013-06-12 21:04:49 -07005137 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005138 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005139
5140 /*
5141 * Unlink from the css_set task list if necessary.
5142 * Optimistically check cg_list before taking
5143 * css_set_lock
5144 */
5145 if (!list_empty(&tsk->cg_list)) {
5146 write_lock(&css_set_lock);
5147 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005148 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005149 write_unlock(&css_set_lock);
5150 }
5151
Paul Menageb4f48b62007-10-18 23:39:33 -07005152 /* Reassign the task to the init_css_set. */
5153 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005154 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005155 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005156
5157 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005158 /*
5159 * fork/exit callbacks are supported only for builtin
5160 * subsystems, see cgroup_post_fork() for details.
5161 */
5162 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005163 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005164
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005165 if (ss->exit) {
5166 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005167 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005168 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005169 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005170 }
5171 }
5172 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005173 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005174
Tejun Heo5abb8852013-06-12 21:04:49 -07005175 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005176}
Paul Menage697f4162007-10-18 23:39:34 -07005177
Paul Menagebd89aab2007-10-18 23:40:44 -07005178static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005179{
Li Zefanf50daa72013-03-01 15:06:07 +08005180 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005181 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005182 /*
5183 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005184 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005185 * it now
5186 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005187 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005188
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005189 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005190 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005191 list_empty(&cgrp->release_list)) {
5192 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005193 need_schedule_work = 1;
5194 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005195 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005196 if (need_schedule_work)
5197 schedule_work(&release_agent_work);
5198 }
5199}
5200
Paul Menage81a6a5c2007-10-18 23:39:38 -07005201/*
5202 * Notify userspace when a cgroup is released, by running the
5203 * configured release agent with the name of the cgroup (path
5204 * relative to the root of cgroup file system) as the argument.
5205 *
5206 * Most likely, this user command will try to rmdir this cgroup.
5207 *
5208 * This races with the possibility that some other task will be
5209 * attached to this cgroup before it is removed, or that some other
5210 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5211 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5212 * unused, and this cgroup will be reprieved from its death sentence,
5213 * to continue to serve a useful existence. Next time it's released,
5214 * we will get notified again, if it still has 'notify_on_release' set.
5215 *
5216 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5217 * means only wait until the task is successfully execve()'d. The
5218 * separate release agent task is forked by call_usermodehelper(),
5219 * then control in this thread returns here, without waiting for the
5220 * release agent task. We don't bother to wait because the caller of
5221 * this routine has no use for the exit status of the release agent
5222 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005223 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005224static void cgroup_release_agent(struct work_struct *work)
5225{
5226 BUG_ON(work != &release_agent_work);
5227 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005228 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005229 while (!list_empty(&release_list)) {
5230 char *argv[3], *envp[3];
5231 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005232 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005233 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005234 struct cgroup,
5235 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005236 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005237 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005238 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005239 if (!pathbuf)
5240 goto continue_free;
5241 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5242 goto continue_free;
5243 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5244 if (!agentbuf)
5245 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005246
5247 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005248 argv[i++] = agentbuf;
5249 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005250 argv[i] = NULL;
5251
5252 i = 0;
5253 /* minimal command environment */
5254 envp[i++] = "HOME=/";
5255 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5256 envp[i] = NULL;
5257
5258 /* Drop the lock while we invoke the usermode helper,
5259 * since the exec could involve hitting disk and hence
5260 * be a slow process */
5261 mutex_unlock(&cgroup_mutex);
5262 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005263 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005264 continue_free:
5265 kfree(pathbuf);
5266 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005267 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005268 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005269 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005270 mutex_unlock(&cgroup_mutex);
5271}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005272
5273static int __init cgroup_disable(char *str)
5274{
5275 int i;
5276 char *token;
5277
5278 while ((token = strsep(&str, ",")) != NULL) {
5279 if (!*token)
5280 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005281 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005282 struct cgroup_subsys *ss = subsys[i];
5283
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005284 /*
5285 * cgroup_disable, being at boot time, can't
5286 * know about module subsystems, so we don't
5287 * worry about them.
5288 */
5289 if (!ss || ss->module)
5290 continue;
5291
Paul Menage8bab8dd2008-04-04 14:29:57 -07005292 if (!strcmp(token, ss->name)) {
5293 ss->disabled = 1;
5294 printk(KERN_INFO "Disabling %s control group"
5295 " subsystem\n", ss->name);
5296 break;
5297 }
5298 }
5299 }
5300 return 1;
5301}
5302__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005303
5304/*
5305 * Functons for CSS ID.
5306 */
5307
Tejun Heo54766d42013-06-12 21:04:53 -07005308/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005309unsigned short css_id(struct cgroup_subsys_state *css)
5310{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005311 struct css_id *cssid;
5312
5313 /*
5314 * This css_id() can return correct value when somone has refcnt
5315 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5316 * it's unchanged until freed.
5317 */
Tejun Heod3daf282013-06-13 19:39:16 -07005318 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005319
5320 if (cssid)
5321 return cssid->id;
5322 return 0;
5323}
Ben Blum67523c42010-03-10 15:22:11 -08005324EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005325
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005326/**
5327 * css_is_ancestor - test "root" css is an ancestor of "child"
5328 * @child: the css to be tested.
5329 * @root: the css supporsed to be an ancestor of the child.
5330 *
5331 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005332 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005333 * But, considering usual usage, the csses should be valid objects after test.
5334 * Assuming that the caller will do some action to the child if this returns
5335 * returns true, the caller must take "child";s reference count.
5336 * If "child" is valid object and this returns true, "root" is valid, too.
5337 */
5338
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005339bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005340 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005341{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005342 struct css_id *child_id;
5343 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005345 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005346 if (!child_id)
5347 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005348 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005349 if (!root_id)
5350 return false;
5351 if (child_id->depth < root_id->depth)
5352 return false;
5353 if (child_id->stack[root_id->depth] != root_id->id)
5354 return false;
5355 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005356}
5357
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005358void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5359{
5360 struct css_id *id = css->id;
5361 /* When this is called before css_id initialization, id can be NULL */
5362 if (!id)
5363 return;
5364
5365 BUG_ON(!ss->use_id);
5366
5367 rcu_assign_pointer(id->css, NULL);
5368 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005369 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005370 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005371 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005372 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373}
Ben Blum67523c42010-03-10 15:22:11 -08005374EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005375
5376/*
5377 * This is called by init or create(). Then, calls to this function are
5378 * always serialized (By cgroup_mutex() at create()).
5379 */
5380
5381static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5382{
5383 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005384 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005385
5386 BUG_ON(!ss->use_id);
5387
5388 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5389 newid = kzalloc(size, GFP_KERNEL);
5390 if (!newid)
5391 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005392
5393 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005394 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005395 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005396 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005397 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005398 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399
5400 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005401 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005402 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005403
Tejun Heod228d9e2013-02-27 17:04:54 -08005404 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005405 newid->depth = depth;
5406 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005407err_out:
5408 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005409 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410
5411}
5412
Ben Blume6a11052010-03-10 15:22:09 -08005413static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5414 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005415{
5416 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005418 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005419 idr_init(&ss->idr);
5420
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 newid = get_new_cssid(ss, 0);
5422 if (IS_ERR(newid))
5423 return PTR_ERR(newid);
5424
5425 newid->stack[0] = newid->id;
5426 newid->css = rootcss;
5427 rootcss->id = newid;
5428 return 0;
5429}
5430
5431static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5432 struct cgroup *child)
5433{
5434 int subsys_id, i, depth = 0;
5435 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005436 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005437
5438 subsys_id = ss->subsys_id;
5439 parent_css = parent->subsys[subsys_id];
5440 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005441 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005442 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005443
5444 child_id = get_new_cssid(ss, depth);
5445 if (IS_ERR(child_id))
5446 return PTR_ERR(child_id);
5447
5448 for (i = 0; i < depth; i++)
5449 child_id->stack[i] = parent_id->stack[i];
5450 child_id->stack[depth] = child_id->id;
5451 /*
5452 * child_id->css pointer will be set after this cgroup is available
5453 * see cgroup_populate_dir()
5454 */
5455 rcu_assign_pointer(child_css->id, child_id);
5456
5457 return 0;
5458}
5459
5460/**
5461 * css_lookup - lookup css by id
5462 * @ss: cgroup subsys to be looked into.
5463 * @id: the id
5464 *
5465 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5466 * NULL if not. Should be called under rcu_read_lock()
5467 */
5468struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5469{
5470 struct css_id *cssid = NULL;
5471
5472 BUG_ON(!ss->use_id);
5473 cssid = idr_find(&ss->idr, id);
5474
5475 if (unlikely(!cssid))
5476 return NULL;
5477
5478 return rcu_dereference(cssid->css);
5479}
Ben Blum67523c42010-03-10 15:22:11 -08005480EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005481
Stephane Eraniane5d13672011-02-14 11:20:01 +02005482/*
5483 * get corresponding css from file open on cgroupfs directory
5484 */
5485struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5486{
5487 struct cgroup *cgrp;
5488 struct inode *inode;
5489 struct cgroup_subsys_state *css;
5490
Al Viro496ad9a2013-01-23 17:07:38 -05005491 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005492 /* check in cgroup filesystem dir */
5493 if (inode->i_op != &cgroup_dir_inode_operations)
5494 return ERR_PTR(-EBADF);
5495
5496 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5497 return ERR_PTR(-EINVAL);
5498
5499 /* get cgroup */
5500 cgrp = __d_cgrp(f->f_dentry);
5501 css = cgrp->subsys[id];
5502 return css ? css : ERR_PTR(-ENOENT);
5503}
5504
Paul Menagefe693432009-09-23 15:56:20 -07005505#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005506static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005507{
5508 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5509
5510 if (!css)
5511 return ERR_PTR(-ENOMEM);
5512
5513 return css;
5514}
5515
Tejun Heo92fb9742012-11-19 08:13:38 -08005516static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005517{
5518 kfree(cont->subsys[debug_subsys_id]);
5519}
5520
Paul Menagefe693432009-09-23 15:56:20 -07005521static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5522{
5523 return cgroup_task_count(cont);
5524}
5525
5526static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5527{
5528 return (u64)(unsigned long)current->cgroups;
5529}
5530
5531static u64 current_css_set_refcount_read(struct cgroup *cont,
5532 struct cftype *cft)
5533{
5534 u64 count;
5535
5536 rcu_read_lock();
5537 count = atomic_read(&current->cgroups->refcount);
5538 rcu_read_unlock();
5539 return count;
5540}
5541
Paul Menage7717f7b2009-09-23 15:56:22 -07005542static int current_css_set_cg_links_read(struct cgroup *cont,
5543 struct cftype *cft,
5544 struct seq_file *seq)
5545{
Tejun Heo69d02062013-06-12 21:04:50 -07005546 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005547 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005548
5549 read_lock(&css_set_lock);
5550 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005551 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005552 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005553 struct cgroup *c = link->cgrp;
5554 const char *name;
5555
5556 if (c->dentry)
5557 name = c->dentry->d_name.name;
5558 else
5559 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005560 seq_printf(seq, "Root %d group %s\n",
5561 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005562 }
5563 rcu_read_unlock();
5564 read_unlock(&css_set_lock);
5565 return 0;
5566}
5567
5568#define MAX_TASKS_SHOWN_PER_CSS 25
5569static int cgroup_css_links_read(struct cgroup *cont,
5570 struct cftype *cft,
5571 struct seq_file *seq)
5572{
Tejun Heo69d02062013-06-12 21:04:50 -07005573 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005574
5575 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07005576 list_for_each_entry(link, &cont->cset_links, cset_link) {
5577 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005578 struct task_struct *task;
5579 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005580 seq_printf(seq, "css_set %p\n", cset);
5581 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005582 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5583 seq_puts(seq, " ...\n");
5584 break;
5585 } else {
5586 seq_printf(seq, " task %d\n",
5587 task_pid_vnr(task));
5588 }
5589 }
5590 }
5591 read_unlock(&css_set_lock);
5592 return 0;
5593}
5594
Paul Menagefe693432009-09-23 15:56:20 -07005595static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5596{
5597 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5598}
5599
5600static struct cftype debug_files[] = {
5601 {
Paul Menagefe693432009-09-23 15:56:20 -07005602 .name = "taskcount",
5603 .read_u64 = debug_taskcount_read,
5604 },
5605
5606 {
5607 .name = "current_css_set",
5608 .read_u64 = current_css_set_read,
5609 },
5610
5611 {
5612 .name = "current_css_set_refcount",
5613 .read_u64 = current_css_set_refcount_read,
5614 },
5615
5616 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005617 .name = "current_css_set_cg_links",
5618 .read_seq_string = current_css_set_cg_links_read,
5619 },
5620
5621 {
5622 .name = "cgroup_css_links",
5623 .read_seq_string = cgroup_css_links_read,
5624 },
5625
5626 {
Paul Menagefe693432009-09-23 15:56:20 -07005627 .name = "releasable",
5628 .read_u64 = releasable_read,
5629 },
Paul Menagefe693432009-09-23 15:56:20 -07005630
Tejun Heo4baf6e32012-04-01 12:09:55 -07005631 { } /* terminate */
5632};
Paul Menagefe693432009-09-23 15:56:20 -07005633
5634struct cgroup_subsys debug_subsys = {
5635 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005636 .css_alloc = debug_css_alloc,
5637 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005638 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005639 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005640};
5641#endif /* CONFIG_CGROUP_DEBUG */