blob: 0ed7d8db65081a184381c1c42db82d74a2f84338 [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
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
206 * the ascending serial number order on the list.
207 */
208static atomic64_t cgroup_serial_nr_cursor = ATOMIC64_INIT(0);
209
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800211 * check for fork/exit handlers to call. This avoids us having to do
212 * extra work in the fork/exit path if none of the subsystems need to
213 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700214 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700215static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216
Tejun Heoea15f8c2013-06-13 19:27:42 -0700217static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800218static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800219static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
220 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221
Paul Menageddbcc7e2007-10-18 23:39:30 -0700222/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700223static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224{
Tejun Heo54766d42013-06-12 21:04:53 -0700225 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226}
227
Li Zefan78574cf2013-04-08 19:00:38 -0700228/**
229 * cgroup_is_descendant - test ancestry
230 * @cgrp: the cgroup to be tested
231 * @ancestor: possible ancestor of @cgrp
232 *
233 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
234 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
235 * and @ancestor are accessible.
236 */
237bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
238{
239 while (cgrp) {
240 if (cgrp == ancestor)
241 return true;
242 cgrp = cgrp->parent;
243 }
244 return false;
245}
246EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247
Adrian Bunke9685a02008-02-07 00:13:46 -0800248static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700249{
250 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700251 (1 << CGRP_RELEASABLE) |
252 (1 << CGRP_NOTIFY_ON_RELEASE);
253 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700254}
255
Adrian Bunke9685a02008-02-07 00:13:46 -0800256static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257{
Paul Menagebd89aab2007-10-18 23:40:44 -0700258 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700259}
260
Paul Menageddbcc7e2007-10-18 23:39:30 -0700261/*
262 * for_each_subsys() allows you to iterate on each subsystem attached to
263 * an active hierarchy
264 */
265#define for_each_subsys(_root, _ss) \
266list_for_each_entry(_ss, &_root->subsys_list, sibling)
267
Li Zefane5f6a862009-01-07 18:07:41 -0800268/* for_each_active_root() allows you to iterate across the active hierarchies */
269#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700270list_for_each_entry(_root, &roots, root_list)
271
Tejun Heof6ea9372012-04-01 12:09:55 -0700272static inline struct cgroup *__d_cgrp(struct dentry *dentry)
273{
274 return dentry->d_fsdata;
275}
276
Tejun Heo05ef1d72012-04-01 12:09:56 -0700277static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700278{
279 return dentry->d_fsdata;
280}
281
Tejun Heo05ef1d72012-04-01 12:09:56 -0700282static inline struct cftype *__d_cft(struct dentry *dentry)
283{
284 return __d_cfe(dentry)->type;
285}
286
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700287/**
288 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
289 * @cgrp: the cgroup to be checked for liveness
290 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700291 * On success, returns true; the mutex should be later unlocked. On
292 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700294static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295{
296 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700297 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700298 mutex_unlock(&cgroup_mutex);
299 return false;
300 }
301 return true;
302}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700303
Paul Menage81a6a5c2007-10-18 23:39:38 -0700304/* the list of cgroups eligible for automatic release. Protected by
305 * release_list_lock */
306static LIST_HEAD(release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +0200307static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700308static void cgroup_release_agent(struct work_struct *work);
309static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700310static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311
Tejun Heo69d02062013-06-12 21:04:50 -0700312/*
313 * A cgroup can be associated with multiple css_sets as different tasks may
314 * belong to different cgroups on different hierarchies. In the other
315 * direction, a css_set is naturally associated with multiple cgroups.
316 * This M:N relationship is represented by the following link structure
317 * which exists for each association and allows traversing the associations
318 * from both sides.
319 */
320struct cgrp_cset_link {
321 /* the cgroup and css_set this link associates */
322 struct cgroup *cgrp;
323 struct css_set *cset;
324
325 /* list of cgrp_cset_links anchored at cgrp->cset_links */
326 struct list_head cset_link;
327
328 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
329 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700330};
331
332/* The default css_set - used by init and its children prior to any
333 * hierarchies being mounted. It contains a pointer to the root state
334 * for each subsystem. Also used to anchor the list of css_sets. Not
335 * reference-counted, to improve performance when child cgroups
336 * haven't been created.
337 */
338
339static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700340static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700341
Ben Blume6a11052010-03-10 15:22:09 -0800342static int cgroup_init_idr(struct cgroup_subsys *ss,
343 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700344
Paul Menage817929e2007-10-18 23:39:36 -0700345/* css_set_lock protects the list of css_set objects, and the
346 * chain of tasks off each css_set. Nests outside task->alloc_lock
347 * due to cgroup_iter_start() */
348static DEFINE_RWLOCK(css_set_lock);
349static int css_set_count;
350
Paul Menage7717f7b2009-09-23 15:56:22 -0700351/*
352 * hash table for cgroup groups. This improves the performance to find
353 * an existing css_set. This hash doesn't (currently) take into
354 * account cgroups in empty hierarchies.
355 */
Li Zefan472b1052008-04-29 01:00:11 -0700356#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800357static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700358
Li Zefan0ac801f2013-01-10 11:49:27 +0800359static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700360{
361 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800362 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700363
364 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800365 key += (unsigned long)css[i];
366 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700367
Li Zefan0ac801f2013-01-10 11:49:27 +0800368 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700369}
370
Paul Menage817929e2007-10-18 23:39:36 -0700371/* We don't maintain the lists running through each css_set to its
372 * task until after the first call to cgroup_iter_start(). This
373 * reduces the fork()/exit() overhead for people who have cgroups
374 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700375static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700376
Tejun Heo5abb8852013-06-12 21:04:49 -0700377static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700378{
Tejun Heo69d02062013-06-12 21:04:50 -0700379 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700380
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700381 /*
382 * Ensure that the refcount doesn't hit zero while any readers
383 * can see it. Similar to atomic_dec_and_lock(), but for an
384 * rwlock
385 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700386 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700387 return;
388 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700389 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700390 write_unlock(&css_set_lock);
391 return;
392 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700395 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700396 css_set_count--;
397
Tejun Heo69d02062013-06-12 21:04:50 -0700398 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700400
Tejun Heo69d02062013-06-12 21:04:50 -0700401 list_del(&link->cset_link);
402 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800403
Tejun Heoddd69142013-06-12 21:04:54 -0700404 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700405 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700406 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700407 set_bit(CGRP_RELEASABLE, &cgrp->flags);
408 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700409 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700410
411 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700412 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700413
414 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700416}
417
418/*
419 * refcounted get/put for css_set objects
420 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700422{
Tejun Heo5abb8852013-06-12 21:04:49 -0700423 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700424}
425
Tejun Heo5abb8852013-06-12 21:04:49 -0700426static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700427{
Tejun Heo5abb8852013-06-12 21:04:49 -0700428 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700429}
430
Tejun Heo5abb8852013-06-12 21:04:49 -0700431static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432{
Tejun Heo5abb8852013-06-12 21:04:49 -0700433 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434}
435
Paul Menage817929e2007-10-18 23:39:36 -0700436/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700437 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 * @cset: candidate css_set being tested
439 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700440 * @new_cgrp: cgroup that's being entered by the task
441 * @template: desired set of css pointers in css_set (pre-calculated)
442 *
443 * Returns true if "cg" matches "old_cg" except for the hierarchy
444 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
445 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700446static bool compare_css_sets(struct css_set *cset,
447 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700448 struct cgroup *new_cgrp,
449 struct cgroup_subsys_state *template[])
450{
451 struct list_head *l1, *l2;
452
Tejun Heo5abb8852013-06-12 21:04:49 -0700453 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700454 /* Not all subsystems matched */
455 return false;
456 }
457
458 /*
459 * Compare cgroup pointers in order to distinguish between
460 * different cgroups in heirarchies with no subsystems. We
461 * could get by with just this check alone (and skip the
462 * memcmp above) but on most setups the memcmp check will
463 * avoid the need for this more expensive check on almost all
464 * candidates.
465 */
466
Tejun Heo69d02062013-06-12 21:04:50 -0700467 l1 = &cset->cgrp_links;
468 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700469 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700470 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700471 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700472
473 l1 = l1->next;
474 l2 = l2->next;
475 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700476 if (l1 == &cset->cgrp_links) {
477 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 break;
479 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700480 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 }
482 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700483 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
484 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
485 cgrp1 = link1->cgrp;
486 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700488 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700489
490 /*
491 * If this hierarchy is the hierarchy of the cgroup
492 * that's changing, then we need to check that this
493 * css_set points to the new cgroup; if it's any other
494 * hierarchy, then this css_set should point to the
495 * same cgroup as the old css_set.
496 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 if (cgrp1->root == new_cgrp->root) {
498 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700499 return false;
500 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700501 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700502 return false;
503 }
504 }
505 return true;
506}
507
508/*
Paul Menage817929e2007-10-18 23:39:36 -0700509 * find_existing_css_set() is a helper for
510 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700511 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700512 *
513 * oldcg: the cgroup group that we're using before the cgroup
514 * transition
515 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700516 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700517 *
518 * template: location in which to build the desired set of subsystem
519 * state objects for the new cgroup group
520 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700521static struct css_set *find_existing_css_set(struct css_set *old_cset,
522 struct cgroup *cgrp,
523 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700524{
525 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700526 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800528 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700529
Ben Blumaae8aab2010-03-10 15:22:07 -0800530 /*
531 * Build the set of subsystem state objects that we want to see in the
532 * new css_set. while subsystems can change globally, the entries here
533 * won't change, so no need for locking.
534 */
Paul Menage817929e2007-10-18 23:39:36 -0700535 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400536 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700537 /* Subsystem is in this hierarchy. So we want
538 * the subsystem state from the new
539 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700540 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700541 } else {
542 /* Subsystem is not in this hierarchy, so we
543 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700545 }
546 }
547
Li Zefan0ac801f2013-01-10 11:49:27 +0800548 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700549 hash_for_each_possible(css_set_table, cset, hlist, key) {
550 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 continue;
552
553 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700554 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700555 }
Paul Menage817929e2007-10-18 23:39:36 -0700556
557 /* No existing cgroup group matched */
558 return NULL;
559}
560
Tejun Heo69d02062013-06-12 21:04:50 -0700561static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700562{
Tejun Heo69d02062013-06-12 21:04:50 -0700563 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700564
Tejun Heo69d02062013-06-12 21:04:50 -0700565 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
566 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700567 kfree(link);
568 }
569}
570
Tejun Heo69d02062013-06-12 21:04:50 -0700571/**
572 * allocate_cgrp_cset_links - allocate cgrp_cset_links
573 * @count: the number of links to allocate
574 * @tmp_links: list_head the allocated links are put on
575 *
576 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
577 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700578 */
Tejun Heo69d02062013-06-12 21:04:50 -0700579static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700580{
Tejun Heo69d02062013-06-12 21:04:50 -0700581 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700582 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700583
584 INIT_LIST_HEAD(tmp_links);
585
Li Zefan36553432008-07-29 22:33:19 -0700586 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700587 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700588 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700589 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700590 return -ENOMEM;
591 }
Tejun Heo69d02062013-06-12 21:04:50 -0700592 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700593 }
594 return 0;
595}
596
Li Zefanc12f65d2009-01-07 18:07:42 -0800597/**
598 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700599 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700600 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800601 * @cgrp: the destination cgroup
602 */
Tejun Heo69d02062013-06-12 21:04:50 -0700603static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
604 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800605{
Tejun Heo69d02062013-06-12 21:04:50 -0700606 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800607
Tejun Heo69d02062013-06-12 21:04:50 -0700608 BUG_ON(list_empty(tmp_links));
609 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
610 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700611 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700613 /*
614 * Always add links to the tail of the list so that the list
615 * is sorted by order of hierarchy creation
616 */
Tejun Heo69d02062013-06-12 21:04:50 -0700617 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800618}
619
Li Zefan36553432008-07-29 22:33:19 -0700620/*
Paul Menage817929e2007-10-18 23:39:36 -0700621 * find_css_set() takes an existing cgroup group and a
622 * cgroup object, and returns a css_set object that's
623 * equivalent to the old group, but with the given cgroup
624 * substituted into the appropriate hierarchy. Must be called with
625 * cgroup_mutex held
626 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700627static struct css_set *find_css_set(struct css_set *old_cset,
628 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700629{
Tejun Heo5abb8852013-06-12 21:04:49 -0700630 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700631 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700632 struct list_head tmp_links;
633 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800634 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700635
Paul Menage817929e2007-10-18 23:39:36 -0700636 /* First see if we already have a cgroup group that matches
637 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700638 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 cset = find_existing_css_set(old_cset, cgrp, template);
640 if (cset)
641 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700642 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700643
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 if (cset)
645 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700646
Tejun Heof4f4be22013-06-12 21:04:51 -0700647 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700649 return NULL;
650
Tejun Heo69d02062013-06-12 21:04:50 -0700651 /* Allocate all the cgrp_cset_link objects that we'll need */
652 if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700654 return NULL;
655 }
656
Tejun Heo5abb8852013-06-12 21:04:49 -0700657 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700658 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700659 INIT_LIST_HEAD(&cset->tasks);
660 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700661
662 /* Copy the set of subsystem state objects generated in
663 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700664 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700665
666 write_lock(&css_set_lock);
667 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700668 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700669 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700670
Paul Menage7717f7b2009-09-23 15:56:22 -0700671 if (c->root == cgrp->root)
672 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700673 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700674 }
Paul Menage817929e2007-10-18 23:39:36 -0700675
Tejun Heo69d02062013-06-12 21:04:50 -0700676 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700677
Paul Menage817929e2007-10-18 23:39:36 -0700678 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700679
680 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700681 key = css_set_hash(cset->subsys);
682 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700683
Paul Menage817929e2007-10-18 23:39:36 -0700684 write_unlock(&css_set_lock);
685
Tejun Heo5abb8852013-06-12 21:04:49 -0700686 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700687}
688
Paul Menageddbcc7e2007-10-18 23:39:30 -0700689/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700690 * Return the cgroup for "task" from the given hierarchy. Must be
691 * called with cgroup_mutex held.
692 */
693static struct cgroup *task_cgroup_from_root(struct task_struct *task,
694 struct cgroupfs_root *root)
695{
Tejun Heo5abb8852013-06-12 21:04:49 -0700696 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 struct cgroup *res = NULL;
698
699 BUG_ON(!mutex_is_locked(&cgroup_mutex));
700 read_lock(&css_set_lock);
701 /*
702 * No need to lock the task - since we hold cgroup_mutex the
703 * task can't change groups, so the only thing that can happen
704 * is that it exits and its css is set back to init_css_set.
705 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700706 cset = task->cgroups;
707 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700708 res = &root->top_cgroup;
709 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700710 struct cgrp_cset_link *link;
711
712 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700714
Paul Menage7717f7b2009-09-23 15:56:22 -0700715 if (c->root == root) {
716 res = c;
717 break;
718 }
719 }
720 }
721 read_unlock(&css_set_lock);
722 BUG_ON(!res);
723 return res;
724}
725
726/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700727 * There is one global cgroup mutex. We also require taking
728 * task_lock() when dereferencing a task's cgroup subsys pointers.
729 * See "The task_lock() exception", at the end of this comment.
730 *
731 * A task must hold cgroup_mutex to modify cgroups.
732 *
733 * Any task can increment and decrement the count field without lock.
734 * So in general, code holding cgroup_mutex can't rely on the count
735 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800736 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700737 * means that no tasks are currently attached, therefore there is no
738 * way a task attached to that cgroup can fork (the other way to
739 * increment the count). So code holding cgroup_mutex can safely
740 * assume that if the count is zero, it will stay zero. Similarly, if
741 * a task holds cgroup_mutex on a cgroup with zero count, it
742 * knows that the cgroup won't be removed, as cgroup_rmdir()
743 * needs that mutex.
744 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700745 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
746 * (usually) take cgroup_mutex. These are the two most performance
747 * critical pieces of code here. The exception occurs on cgroup_exit(),
748 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
749 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800750 * to the release agent with the name of the cgroup (path relative to
751 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 *
753 * A cgroup can only be deleted if both its 'count' of using tasks
754 * is zero, and its list of 'children' cgroups is empty. Since all
755 * tasks in the system use _some_ cgroup, and since there is always at
756 * least one task in the system (init, pid == 1), therefore, top_cgroup
757 * always has either children cgroups and/or using tasks. So we don't
758 * need a special hack to ensure that top_cgroup cannot be deleted.
759 *
760 * The task_lock() exception
761 *
762 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800763 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800764 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 * several performance critical places that need to reference
766 * task->cgroup without the expense of grabbing a system global
767 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800768 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
770 * the task_struct routinely used for such matters.
771 *
772 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800773 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700774 */
775
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776/*
777 * A couple of forward declarations required, due to cyclic reference loop:
778 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
779 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
780 * -> cgroup_mkdir.
781 */
782
Al Viro18bb1db2011-07-26 01:41:39 -0400783static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400784static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400786static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
787 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700788static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700789static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700790
791static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200792 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700793 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700794};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700796static int alloc_css_id(struct cgroup_subsys *ss,
797 struct cgroup *parent, struct cgroup *child);
798
Al Viroa5e7ed32011-07-26 01:55:55 -0400799static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800{
801 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802
803 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400804 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100806 inode->i_uid = current_fsuid();
807 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
809 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
810 }
811 return inode;
812}
813
Li Zefan65dff752013-03-01 15:01:56 +0800814static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
815{
816 struct cgroup_name *name;
817
818 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
819 if (!name)
820 return NULL;
821 strcpy(name->name, dentry->d_name.name);
822 return name;
823}
824
Li Zefanbe445622013-01-24 14:31:42 +0800825static void cgroup_free_fn(struct work_struct *work)
826{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700827 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800828 struct cgroup_subsys *ss;
829
830 mutex_lock(&cgroup_mutex);
831 /*
832 * Release the subsystem state objects.
833 */
834 for_each_subsys(cgrp->root, ss)
835 ss->css_free(cgrp);
836
837 cgrp->root->number_of_cgroups--;
838 mutex_unlock(&cgroup_mutex);
839
840 /*
Li Zefan415cf072013-04-08 14:35:02 +0800841 * We get a ref to the parent's dentry, and put the ref when
842 * this cgroup is being freed, so it's guaranteed that the
843 * parent won't be destroyed before its children.
844 */
845 dput(cgrp->parent->dentry);
846
Li Zefancc20e012013-04-26 11:58:02 -0700847 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
848
Li Zefan415cf072013-04-08 14:35:02 +0800849 /*
Li Zefanbe445622013-01-24 14:31:42 +0800850 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700851 * created the cgroup. This will free cgrp->root, if we are
852 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800853 */
854 deactivate_super(cgrp->root->sb);
855
856 /*
857 * if we're getting rid of the cgroup, refcount should ensure
858 * that there are no pidlists left.
859 */
860 BUG_ON(!list_empty(&cgrp->pidlists));
861
862 simple_xattrs_free(&cgrp->xattrs);
863
Li Zefan65dff752013-03-01 15:01:56 +0800864 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800865 kfree(cgrp);
866}
867
868static void cgroup_free_rcu(struct rcu_head *head)
869{
870 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
871
Tejun Heoea15f8c2013-06-13 19:27:42 -0700872 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
873 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800874}
875
Paul Menageddbcc7e2007-10-18 23:39:30 -0700876static void cgroup_diput(struct dentry *dentry, struct inode *inode)
877{
878 /* is dentry a directory ? if so, kfree() associated cgroup */
879 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700880 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800881
Tejun Heo54766d42013-06-12 21:04:53 -0700882 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800883 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700884 } else {
885 struct cfent *cfe = __d_cfe(dentry);
886 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
887
888 WARN_ONCE(!list_empty(&cfe->node) &&
889 cgrp != &cgrp->root->top_cgroup,
890 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700891 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700892 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700893 }
894 iput(inode);
895}
896
Al Viroc72a04e2011-01-14 05:31:45 +0000897static int cgroup_delete(const struct dentry *d)
898{
899 return 1;
900}
901
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902static void remove_dir(struct dentry *d)
903{
904 struct dentry *parent = dget(d->d_parent);
905
906 d_delete(d);
907 simple_rmdir(parent->d_inode, d);
908 dput(parent);
909}
910
Li Zefan2739d3c2013-01-21 18:18:33 +0800911static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700912{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700913 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914
Tejun Heo05ef1d72012-04-01 12:09:56 -0700915 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
916 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100917
Li Zefan2739d3c2013-01-21 18:18:33 +0800918 /*
919 * If we're doing cleanup due to failure of cgroup_create(),
920 * the corresponding @cfe may not exist.
921 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 list_for_each_entry(cfe, &cgrp->files, node) {
923 struct dentry *d = cfe->dentry;
924
925 if (cft && cfe->type != cft)
926 continue;
927
928 dget(d);
929 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700930 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700931 list_del_init(&cfe->node);
932 dput(d);
933
Li Zefan2739d3c2013-01-21 18:18:33 +0800934 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936}
937
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400938/**
939 * cgroup_clear_directory - selective removal of base and subsystem files
940 * @dir: directory containing the files
941 * @base_files: true if the base files should be removed
942 * @subsys_mask: mask of the subsystem ids whose files should be removed
943 */
944static void cgroup_clear_directory(struct dentry *dir, bool base_files,
945 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700946{
947 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400948 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400950 for_each_subsys(cgrp->root, ss) {
951 struct cftype_set *set;
952 if (!test_bit(ss->subsys_id, &subsys_mask))
953 continue;
954 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800955 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400956 }
957 if (base_files) {
958 while (!list_empty(&cgrp->files))
959 cgroup_rm_file(cgrp, NULL);
960 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961}
962
963/*
964 * NOTE : the dentry must have been dget()'ed
965 */
966static void cgroup_d_remove_dir(struct dentry *dentry)
967{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100968 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100970
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400971 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100973 parent = dentry->d_parent;
974 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800975 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100977 spin_unlock(&dentry->d_lock);
978 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 remove_dir(dentry);
980}
981
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700982/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800983 * Call with cgroup_mutex held. Drops reference counts on modules, including
984 * any duplicate ones that parse_cgroupfs_options took. If this function
985 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800986 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400988 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400990 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700991 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 int i;
993
Ben Blumaae8aab2010-03-10 15:22:07 -0800994 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800995 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800996
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400997 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
998 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 /* Check that any added subsystems are currently free */
1000 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001001 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001003 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001005 /*
1006 * Nobody should tell us to do a subsys that doesn't exist:
1007 * parse_cgroupfs_options should catch that case and refcounts
1008 * ensure that subsystems won't disappear once selected.
1009 */
1010 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 if (ss->root != &rootnode) {
1012 /* Subsystem isn't free */
1013 return -EBUSY;
1014 }
1015 }
1016
1017 /* Currently we don't handle adding/removing subsystems when
1018 * any child cgroups exist. This is theoretically supportable
1019 * but involves complex error handling, so it's being left until
1020 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001021 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 return -EBUSY;
1023
1024 /* Process each subsystem */
1025 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1026 struct cgroup_subsys *ss = subsys[i];
1027 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001028 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001030 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001031 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 BUG_ON(!dummytop->subsys[i]);
1033 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001034 cgrp->subsys[i] = dummytop->subsys[i];
1035 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001036 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001037 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001039 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001040 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001041 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001043 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1045 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001047 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001049 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001050 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001051 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001052 /* subsystem is now free - drop reference on module */
1053 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001054 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001056 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001058 /*
1059 * a refcount was taken, but we already had one, so
1060 * drop the extra reference.
1061 */
1062 module_put(ss->module);
1063#ifdef CONFIG_MODULE_UNLOAD
1064 BUG_ON(ss->module && !module_refcount(ss->module));
1065#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 } else {
1067 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001068 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 }
1070 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001071 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072
1073 return 0;
1074}
1075
Al Viro34c80b12011-12-08 21:32:45 -05001076static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077{
Al Viro34c80b12011-12-08 21:32:45 -05001078 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 struct cgroup_subsys *ss;
1080
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001081 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 for_each_subsys(root, ss)
1083 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001084 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1085 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001086 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001088 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001089 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001090 if (strlen(root->release_agent_path))
1091 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001092 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001093 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001094 if (strlen(root->name))
1095 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001096 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 return 0;
1098}
1099
1100struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001101 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001104 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001105 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001106 /* User explicitly requested empty subsystem */
1107 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001108
1109 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001110
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111};
1112
Ben Blumaae8aab2010-03-10 15:22:07 -08001113/*
1114 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001115 * with cgroup_mutex held to protect the subsys[] array. This function takes
1116 * refcounts on subsystems to be used, unless it returns error, in which case
1117 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001118 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001119static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001121 char *token, *o = data;
1122 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001123 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001124 int i;
1125 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001126
Ben Blumaae8aab2010-03-10 15:22:07 -08001127 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1128
Li Zefanf9ab5b52009-06-17 16:26:33 -07001129#ifdef CONFIG_CPUSETS
1130 mask = ~(1UL << cpuset_subsys_id);
1131#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132
Paul Menagec6d57f32009-09-23 15:56:19 -07001133 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134
1135 while ((token = strsep(&o, ",")) != NULL) {
1136 if (!*token)
1137 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001138 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001139 /* Explicitly have no subsystems */
1140 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001141 continue;
1142 }
1143 if (!strcmp(token, "all")) {
1144 /* Mutually exclusive option 'all' + subsystem name */
1145 if (one_ss)
1146 return -EINVAL;
1147 all_ss = true;
1148 continue;
1149 }
Tejun Heo873fe092013-04-14 20:15:26 -07001150 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1151 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1152 continue;
1153 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001154 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001155 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001156 continue;
1157 }
1158 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001159 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001160 continue;
1161 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001162 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001163 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001164 continue;
1165 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001166 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001167 /* Specifying two release agents is forbidden */
1168 if (opts->release_agent)
1169 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001170 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001171 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001172 if (!opts->release_agent)
1173 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001174 continue;
1175 }
1176 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001177 const char *name = token + 5;
1178 /* Can't specify an empty name */
1179 if (!strlen(name))
1180 return -EINVAL;
1181 /* Must match [\w.-]+ */
1182 for (i = 0; i < strlen(name); i++) {
1183 char c = name[i];
1184 if (isalnum(c))
1185 continue;
1186 if ((c == '.') || (c == '-') || (c == '_'))
1187 continue;
1188 return -EINVAL;
1189 }
1190 /* Specifying two names is forbidden */
1191 if (opts->name)
1192 return -EINVAL;
1193 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001194 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001195 GFP_KERNEL);
1196 if (!opts->name)
1197 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001198
1199 continue;
1200 }
1201
1202 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1203 struct cgroup_subsys *ss = subsys[i];
1204 if (ss == NULL)
1205 continue;
1206 if (strcmp(token, ss->name))
1207 continue;
1208 if (ss->disabled)
1209 continue;
1210
1211 /* Mutually exclusive option 'all' + subsystem name */
1212 if (all_ss)
1213 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001214 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001215 one_ss = true;
1216
1217 break;
1218 }
1219 if (i == CGROUP_SUBSYS_COUNT)
1220 return -ENOENT;
1221 }
1222
1223 /*
1224 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001225 * otherwise if 'none', 'name=' and a subsystem name options
1226 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001227 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001228 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001229 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1230 struct cgroup_subsys *ss = subsys[i];
1231 if (ss == NULL)
1232 continue;
1233 if (ss->disabled)
1234 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001235 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001236 }
1237 }
1238
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001239 /* Consistency checks */
1240
Tejun Heo873fe092013-04-14 20:15:26 -07001241 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1242 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1243
1244 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1245 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1246 return -EINVAL;
1247 }
1248
1249 if (opts->cpuset_clone_children) {
1250 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1251 return -EINVAL;
1252 }
1253 }
1254
Li Zefanf9ab5b52009-06-17 16:26:33 -07001255 /*
1256 * Option noprefix was introduced just for backward compatibility
1257 * with the old cpuset, so we allow noprefix only if mounting just
1258 * the cpuset subsystem.
1259 */
Tejun Heo93438622013-04-14 20:15:25 -07001260 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001261 return -EINVAL;
1262
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001263
1264 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001265 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001266 return -EINVAL;
1267
1268 /*
1269 * We either have to specify by name or by subsystems. (So all
1270 * empty hierarchies must have a name).
1271 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001272 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273 return -EINVAL;
1274
Ben Blumcf5d5942010-03-10 15:22:09 -08001275 /*
1276 * Grab references on all the modules we'll need, so the subsystems
1277 * don't dance around before rebind_subsystems attaches them. This may
1278 * take duplicate reference counts on a subsystem that's already used,
1279 * but rebind_subsystems handles this case.
1280 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001281 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 unsigned long bit = 1UL << i;
1283
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001284 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 continue;
1286 if (!try_module_get(subsys[i]->module)) {
1287 module_pin_failed = true;
1288 break;
1289 }
1290 }
1291 if (module_pin_failed) {
1292 /*
1293 * oops, one of the modules was going away. this means that we
1294 * raced with a module_delete call, and to the user this is
1295 * essentially a "subsystem doesn't exist" case.
1296 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001297 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 /* drop refcounts only on the ones we took */
1299 unsigned long bit = 1UL << i;
1300
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001301 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001302 continue;
1303 module_put(subsys[i]->module);
1304 }
1305 return -ENOENT;
1306 }
1307
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 return 0;
1309}
1310
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001311static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001312{
1313 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001314 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001315 unsigned long bit = 1UL << i;
1316
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001317 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 continue;
1319 module_put(subsys[i]->module);
1320 }
1321}
1322
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1324{
1325 int ret = 0;
1326 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001329 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330
Tejun Heo873fe092013-04-14 20:15:26 -07001331 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1332 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1333 return -EINVAL;
1334 }
1335
Paul Menagebd89aab2007-10-18 23:40:44 -07001336 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001338 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339
1340 /* See what subsystems are wanted */
1341 ret = parse_cgroupfs_options(data, &opts);
1342 if (ret)
1343 goto out_unlock;
1344
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001345 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001346 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1347 task_tgid_nr(current), current->comm);
1348
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001349 added_mask = opts.subsys_mask & ~root->subsys_mask;
1350 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001351
Ben Blumcf5d5942010-03-10 15:22:09 -08001352 /* Don't allow flags or name to change at remount */
1353 if (opts.flags != root->flags ||
1354 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001356 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001357 goto out_unlock;
1358 }
1359
Gao feng7083d032012-12-03 09:28:18 +08001360 /*
1361 * Clear out the files of subsystems that should be removed, do
1362 * this before rebind_subsystems, since rebind_subsystems may
1363 * change this hierarchy's subsys_list.
1364 */
1365 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1366
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001367 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001368 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001369 /* rebind_subsystems failed, re-populate the removed files */
1370 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001371 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001372 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001373 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001375 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001376 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377
Paul Menage81a6a5c2007-10-18 23:39:38 -07001378 if (opts.release_agent)
1379 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001381 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001382 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001383 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001385 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 return ret;
1387}
1388
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001389static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 .statfs = simple_statfs,
1391 .drop_inode = generic_delete_inode,
1392 .show_options = cgroup_show_options,
1393 .remount_fs = cgroup_remount,
1394};
1395
Paul Menagecc31edc2008-10-18 20:28:04 -07001396static void init_cgroup_housekeeping(struct cgroup *cgrp)
1397{
1398 INIT_LIST_HEAD(&cgrp->sibling);
1399 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001400 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001401 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001402 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001403 INIT_LIST_HEAD(&cgrp->pidlists);
1404 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001405 INIT_LIST_HEAD(&cgrp->event_list);
1406 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001407 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408}
Paul Menagec6d57f32009-09-23 15:56:19 -07001409
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410static void init_cgroup_root(struct cgroupfs_root *root)
1411{
Paul Menagebd89aab2007-10-18 23:40:44 -07001412 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001413
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 INIT_LIST_HEAD(&root->subsys_list);
1415 INIT_LIST_HEAD(&root->root_list);
1416 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001417 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001418 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001419 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420}
1421
Tejun Heofa3ca072013-04-14 11:36:56 -07001422static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001423{
Tejun Heo1a574232013-04-14 11:36:58 -07001424 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001425
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001426 lockdep_assert_held(&cgroup_mutex);
1427 lockdep_assert_held(&cgroup_root_mutex);
1428
Tejun Heo1a574232013-04-14 11:36:58 -07001429 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1430 if (id < 0)
1431 return id;
1432
1433 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001434 return 0;
1435}
1436
1437static void cgroup_exit_root_id(struct cgroupfs_root *root)
1438{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001439 lockdep_assert_held(&cgroup_mutex);
1440 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001441
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001442 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001443 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001444 root->hierarchy_id = 0;
1445 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001446}
1447
Paul Menageddbcc7e2007-10-18 23:39:30 -07001448static int cgroup_test_super(struct super_block *sb, void *data)
1449{
Paul Menagec6d57f32009-09-23 15:56:19 -07001450 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451 struct cgroupfs_root *root = sb->s_fs_info;
1452
Paul Menagec6d57f32009-09-23 15:56:19 -07001453 /* If we asked for a name then it must match */
1454 if (opts->name && strcmp(opts->name, root->name))
1455 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001457 /*
1458 * If we asked for subsystems (or explicitly for no
1459 * subsystems) then they must match
1460 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001461 if ((opts->subsys_mask || opts->none)
1462 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463 return 0;
1464
1465 return 1;
1466}
1467
Paul Menagec6d57f32009-09-23 15:56:19 -07001468static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1469{
1470 struct cgroupfs_root *root;
1471
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001472 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001473 return NULL;
1474
1475 root = kzalloc(sizeof(*root), GFP_KERNEL);
1476 if (!root)
1477 return ERR_PTR(-ENOMEM);
1478
1479 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001480
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001481 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001483 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001484 if (opts->release_agent)
1485 strcpy(root->release_agent_path, opts->release_agent);
1486 if (opts->name)
1487 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001488 if (opts->cpuset_clone_children)
1489 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 return root;
1491}
1492
Tejun Heofa3ca072013-04-14 11:36:56 -07001493static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001494{
Tejun Heofa3ca072013-04-14 11:36:56 -07001495 if (root) {
1496 /* hierarhcy ID shoulid already have been released */
1497 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001498
Tejun Heofa3ca072013-04-14 11:36:56 -07001499 ida_destroy(&root->cgroup_ida);
1500 kfree(root);
1501 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001502}
1503
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504static int cgroup_set_super(struct super_block *sb, void *data)
1505{
1506 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 struct cgroup_sb_opts *opts = data;
1508
1509 /* If we don't have a new root, we can't set up a new sb */
1510 if (!opts->new_root)
1511 return -EINVAL;
1512
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001513 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514
1515 ret = set_anon_super(sb, NULL);
1516 if (ret)
1517 return ret;
1518
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 sb->s_fs_info = opts->new_root;
1520 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
1522 sb->s_blocksize = PAGE_CACHE_SIZE;
1523 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1524 sb->s_magic = CGROUP_SUPER_MAGIC;
1525 sb->s_op = &cgroup_ops;
1526
1527 return 0;
1528}
1529
1530static int cgroup_get_rootdir(struct super_block *sb)
1531{
Al Viro0df6a632010-12-21 13:29:29 -05001532 static const struct dentry_operations cgroup_dops = {
1533 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001534 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001535 };
1536
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 struct inode *inode =
1538 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
1540 if (!inode)
1541 return -ENOMEM;
1542
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 inode->i_fop = &simple_dir_operations;
1544 inode->i_op = &cgroup_dir_inode_operations;
1545 /* directories start off with i_nlink == 2 (for "." entry) */
1546 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001547 sb->s_root = d_make_root(inode);
1548 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001550 /* for everything else we want ->d_op set */
1551 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 return 0;
1553}
1554
Al Virof7e83572010-07-26 13:23:11 +04001555static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001557 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558{
1559 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 int ret = 0;
1562 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001564 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565
1566 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001567 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001569 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 if (ret)
1571 goto out_err;
1572
1573 /*
1574 * Allocate a new cgroup root. We may not need it if we're
1575 * reusing an existing hierarchy.
1576 */
1577 new_root = cgroup_root_from_opts(&opts);
1578 if (IS_ERR(new_root)) {
1579 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001580 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001581 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001582 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001585 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001588 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001589 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590 }
1591
Paul Menagec6d57f32009-09-23 15:56:19 -07001592 root = sb->s_fs_info;
1593 BUG_ON(!root);
1594 if (root == opts.new_root) {
1595 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001596 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001597 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001599 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001600 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001601 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602
1603 BUG_ON(sb->s_root != NULL);
1604
1605 ret = cgroup_get_rootdir(sb);
1606 if (ret)
1607 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001608 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
Paul Menage817929e2007-10-18 23:39:36 -07001610 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001612 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001614 /* Check for name clashes with existing mounts */
1615 ret = -EBUSY;
1616 if (strlen(root->name))
1617 for_each_active_root(existing_root)
1618 if (!strcmp(existing_root->name, root->name))
1619 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001620
Paul Menage817929e2007-10-18 23:39:36 -07001621 /*
1622 * We're accessing css_set_count without locking
1623 * css_set_lock here, but that's OK - it can only be
1624 * increased by someone holding cgroup_lock, and
1625 * that's us. The worst that can happen is that we
1626 * have some link structures left over
1627 */
Tejun Heo69d02062013-06-12 21:04:50 -07001628 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001629 if (ret)
1630 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001631
Tejun Heofa3ca072013-04-14 11:36:56 -07001632 ret = cgroup_init_root_id(root);
1633 if (ret)
1634 goto unlock_drop;
1635
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001636 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001637 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001638 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001639 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001641 /*
1642 * There must be no failure case after here, since rebinding
1643 * takes care of subsystems' refcounts, which are explicitly
1644 * dropped in the failure exit path.
1645 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646
1647 /* EBUSY should be the only error here */
1648 BUG_ON(ret);
1649
1650 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001651 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652
Li Zefanc12f65d2009-01-07 18:07:42 -08001653 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 root->top_cgroup.dentry = sb->s_root;
1655
Paul Menage817929e2007-10-18 23:39:36 -07001656 /* Link the top cgroup in this hierarchy into all
1657 * the css_set objects */
1658 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001659 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001660 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001661 write_unlock(&css_set_lock);
1662
Tejun Heo69d02062013-06-12 21:04:50 -07001663 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001664
Li Zefanc12f65d2009-01-07 18:07:42 -08001665 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 BUG_ON(root->number_of_cgroups != 1);
1667
eparis@redhat2ce97382011-06-02 21:20:51 +10001668 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001669 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001670 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001673 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001674 } else {
1675 /*
1676 * We re-used an existing hierarchy - the new root (if
1677 * any) is not needed
1678 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001679 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001680
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001681 if (root->flags != opts.flags) {
1682 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1683 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1684 ret = -EINVAL;
1685 goto drop_new_super;
1686 } else {
1687 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1688 }
Tejun Heo873fe092013-04-14 20:15:26 -07001689 }
1690
Ben Blumcf5d5942010-03-10 15:22:09 -08001691 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001692 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 }
1694
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 kfree(opts.release_agent);
1696 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001697 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001699 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001700 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001701 mutex_unlock(&cgroup_root_mutex);
1702 mutex_unlock(&cgroup_mutex);
1703 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001705 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001706 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001707 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001708 out_err:
1709 kfree(opts.release_agent);
1710 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001711 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712}
1713
1714static void cgroup_kill_sb(struct super_block *sb) {
1715 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001716 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001717 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 int ret;
1719
1720 BUG_ON(!root);
1721
1722 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001723 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724
1725 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001726 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 /* Rebind all subsystems back to the default hierarchy */
1729 ret = rebind_subsystems(root, 0);
1730 /* Shouldn't be able to fail ... */
1731 BUG_ON(ret);
1732
Paul Menage817929e2007-10-18 23:39:36 -07001733 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001734 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001735 * root cgroup
1736 */
1737 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001738
Tejun Heo69d02062013-06-12 21:04:50 -07001739 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1740 list_del(&link->cset_link);
1741 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001742 kfree(link);
1743 }
1744 write_unlock(&css_set_lock);
1745
Paul Menage839ec542009-01-29 14:25:22 -08001746 if (!list_empty(&root->root_list)) {
1747 list_del(&root->root_list);
1748 root_count--;
1749 }
Li Zefane5f6a862009-01-07 18:07:41 -08001750
Tejun Heofa3ca072013-04-14 11:36:56 -07001751 cgroup_exit_root_id(root);
1752
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001753 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 mutex_unlock(&cgroup_mutex);
1755
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001756 simple_xattrs_free(&cgrp->xattrs);
1757
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001759 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760}
1761
1762static struct file_system_type cgroup_fs_type = {
1763 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001764 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 .kill_sb = cgroup_kill_sb,
1766};
1767
Greg KH676db4a2010-08-05 13:53:35 -07001768static struct kobject *cgroup_kobj;
1769
Li Zefana043e3b2008-02-23 15:24:09 -08001770/**
1771 * cgroup_path - generate the path of a cgroup
1772 * @cgrp: the cgroup in question
1773 * @buf: the buffer to write the path into
1774 * @buflen: the length of the buffer
1775 *
Li Zefan65dff752013-03-01 15:01:56 +08001776 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1777 *
1778 * We can't generate cgroup path using dentry->d_name, as accessing
1779 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1780 * inode's i_mutex, while on the other hand cgroup_path() can be called
1781 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001783int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784{
Li Zefan65dff752013-03-01 15:01:56 +08001785 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001787
Tejun Heoda1f2962013-04-14 10:32:19 -07001788 if (!cgrp->parent) {
1789 if (strlcpy(buf, "/", buflen) >= buflen)
1790 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 return 0;
1792 }
1793
Tao Ma316eb662012-11-08 21:36:38 +08001794 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001795 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001796
Li Zefan65dff752013-03-01 15:01:56 +08001797 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001798 do {
Li Zefan65dff752013-03-01 15:01:56 +08001799 const char *name = cgroup_name(cgrp);
1800 int len;
1801
1802 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001804 goto out;
1805 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001806
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001808 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001810
1811 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001812 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001813 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001815out:
1816 rcu_read_unlock();
1817 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818}
Ben Blum67523c42010-03-10 15:22:11 -08001819EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820
Tejun Heo857a2be2013-04-14 20:50:08 -07001821/**
1822 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1823 * @task: target task
1824 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1825 * @buf: the buffer to write the path into
1826 * @buflen: the length of the buffer
1827 *
1828 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1829 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1830 * be used inside locks used by cgroup controller callbacks.
1831 */
1832int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1833 char *buf, size_t buflen)
1834{
1835 struct cgroupfs_root *root;
1836 struct cgroup *cgrp = NULL;
1837 int ret = -ENOENT;
1838
1839 mutex_lock(&cgroup_mutex);
1840
1841 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1842 if (root) {
1843 cgrp = task_cgroup_from_root(task, root);
1844 ret = cgroup_path(cgrp, buf, buflen);
1845 }
1846
1847 mutex_unlock(&cgroup_mutex);
1848
1849 return ret;
1850}
1851EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1852
Ben Blum74a11662011-05-26 16:25:20 -07001853/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001854 * Control Group taskset
1855 */
Tejun Heo134d3372011-12-12 18:12:21 -08001856struct task_and_cgroup {
1857 struct task_struct *task;
1858 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001859 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001860};
1861
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862struct cgroup_taskset {
1863 struct task_and_cgroup single;
1864 struct flex_array *tc_array;
1865 int tc_array_len;
1866 int idx;
1867 struct cgroup *cur_cgrp;
1868};
1869
1870/**
1871 * cgroup_taskset_first - reset taskset and return the first task
1872 * @tset: taskset of interest
1873 *
1874 * @tset iteration is initialized and the first task is returned.
1875 */
1876struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1877{
1878 if (tset->tc_array) {
1879 tset->idx = 0;
1880 return cgroup_taskset_next(tset);
1881 } else {
1882 tset->cur_cgrp = tset->single.cgrp;
1883 return tset->single.task;
1884 }
1885}
1886EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1887
1888/**
1889 * cgroup_taskset_next - iterate to the next task in taskset
1890 * @tset: taskset of interest
1891 *
1892 * Return the next task in @tset. Iteration must have been initialized
1893 * with cgroup_taskset_first().
1894 */
1895struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1896{
1897 struct task_and_cgroup *tc;
1898
1899 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1900 return NULL;
1901
1902 tc = flex_array_get(tset->tc_array, tset->idx++);
1903 tset->cur_cgrp = tc->cgrp;
1904 return tc->task;
1905}
1906EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1907
1908/**
1909 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1910 * @tset: taskset of interest
1911 *
1912 * Return the cgroup for the current (last returned) task of @tset. This
1913 * function must be preceded by either cgroup_taskset_first() or
1914 * cgroup_taskset_next().
1915 */
1916struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1917{
1918 return tset->cur_cgrp;
1919}
1920EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1921
1922/**
1923 * cgroup_taskset_size - return the number of tasks in taskset
1924 * @tset: taskset of interest
1925 */
1926int cgroup_taskset_size(struct cgroup_taskset *tset)
1927{
1928 return tset->tc_array ? tset->tc_array_len : 1;
1929}
1930EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1931
1932
Ben Blum74a11662011-05-26 16:25:20 -07001933/*
1934 * cgroup_task_migrate - move a task from one cgroup to another.
1935 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001936 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001937 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001938static void cgroup_task_migrate(struct cgroup *old_cgrp,
1939 struct task_struct *tsk,
1940 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001941{
Tejun Heo5abb8852013-06-12 21:04:49 -07001942 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001943
1944 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001945 * We are synchronized through threadgroup_lock() against PF_EXITING
1946 * setting such that we can't race against cgroup_exit() changing the
1947 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001948 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001949 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001951
Ben Blum74a11662011-05-26 16:25:20 -07001952 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001953 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001954 task_unlock(tsk);
1955
1956 /* Update the css_set linked lists if we're using them */
1957 write_lock(&css_set_lock);
1958 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001960 write_unlock(&css_set_lock);
1961
1962 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001963 * We just gained a reference on old_cset by taking it from the
1964 * task. As trading it for new_cset is protected by cgroup_mutex,
1965 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001966 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001967 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1968 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001969}
1970
Li Zefana043e3b2008-02-23 15:24:09 -08001971/**
Li Zefan081aa452013-03-13 09:17:09 +08001972 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001973 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001974 * @tsk: the task or the leader of the threadgroup to be attached
1975 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001976 *
Tejun Heo257058a2011-12-12 18:12:21 -08001977 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001978 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001979 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001980static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1981 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001982{
1983 int retval, i, group_size;
1984 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001985 struct cgroupfs_root *root = cgrp->root;
1986 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001987 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001988 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001989 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001990 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001991
1992 /*
1993 * step 0: in order to do expensive, possibly blocking operations for
1994 * every thread, we cannot iterate the thread group list, since it needs
1995 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001996 * group - group_rwsem prevents new threads from appearing, and if
1997 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001998 */
Li Zefan081aa452013-03-13 09:17:09 +08001999 if (threadgroup)
2000 group_size = get_nr_threads(tsk);
2001 else
2002 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002003 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002004 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002005 if (!group)
2006 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002007 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002008 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002009 if (retval)
2010 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002011
Ben Blum74a11662011-05-26 16:25:20 -07002012 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002013 /*
2014 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2015 * already PF_EXITING could be freed from underneath us unless we
2016 * take an rcu_read_lock.
2017 */
2018 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002019 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002020 struct task_and_cgroup ent;
2021
Tejun Heocd3d0952011-12-12 18:12:21 -08002022 /* @tsk either already exited or can't exit until the end */
2023 if (tsk->flags & PF_EXITING)
2024 continue;
2025
Ben Blum74a11662011-05-26 16:25:20 -07002026 /* as per above, nr_threads may decrease, but not increase. */
2027 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002028 ent.task = tsk;
2029 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002030 /* nothing to do if this task is already in the cgroup */
2031 if (ent.cgrp == cgrp)
2032 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002033 /*
2034 * saying GFP_ATOMIC has no effect here because we did prealloc
2035 * earlier, but it's good form to communicate our expectations.
2036 */
Tejun Heo134d3372011-12-12 18:12:21 -08002037 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002038 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002039 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002040
2041 if (!threadgroup)
2042 break;
Ben Blum74a11662011-05-26 16:25:20 -07002043 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002044 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002045 /* remember the number of threads in the array for later. */
2046 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002047 tset.tc_array = group;
2048 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002049
Tejun Heo134d3372011-12-12 18:12:21 -08002050 /* methods shouldn't be called if no task is actually migrating */
2051 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002052 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002053 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002054
Ben Blum74a11662011-05-26 16:25:20 -07002055 /*
2056 * step 1: check that we can legitimately attach to the cgroup.
2057 */
2058 for_each_subsys(root, ss) {
2059 if (ss->can_attach) {
Li Zefan761b3ef52012-01-31 13:47:36 +08002060 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002061 if (retval) {
2062 failed_ss = ss;
2063 goto out_cancel_attach;
2064 }
2065 }
Ben Blum74a11662011-05-26 16:25:20 -07002066 }
2067
2068 /*
2069 * step 2: make sure css_sets exist for all threads to be migrated.
2070 * we use find_css_set, which allocates a new one if necessary.
2071 */
Ben Blum74a11662011-05-26 16:25:20 -07002072 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002073 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002074 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2075 if (!tc->cg) {
2076 retval = -ENOMEM;
2077 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002078 }
2079 }
2080
2081 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002082 * step 3: now that we're guaranteed success wrt the css_sets,
2083 * proceed to move all tasks to the new cgroup. There are no
2084 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002085 */
Ben Blum74a11662011-05-26 16:25:20 -07002086 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002087 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002088 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002089 }
2090 /* nothing is sensitive to fork() after this point. */
2091
2092 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002093 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002094 */
2095 for_each_subsys(root, ss) {
2096 if (ss->attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002097 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002098 }
2099
2100 /*
2101 * step 5: success! and cleanup
2102 */
Ben Blum74a11662011-05-26 16:25:20 -07002103 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002104out_put_css_set_refs:
2105 if (retval) {
2106 for (i = 0; i < group_size; i++) {
2107 tc = flex_array_get(group, i);
2108 if (!tc->cg)
2109 break;
2110 put_css_set(tc->cg);
2111 }
Ben Blum74a11662011-05-26 16:25:20 -07002112 }
2113out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002114 if (retval) {
2115 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002116 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002117 break;
Ben Blum74a11662011-05-26 16:25:20 -07002118 if (ss->cancel_attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002119 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121 }
Ben Blum74a11662011-05-26 16:25:20 -07002122out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002123 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002124 return retval;
2125}
2126
2127/*
2128 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002129 * function to attach either it or all tasks in its threadgroup. Will lock
2130 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002131 */
2132static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002133{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002135 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136 int ret;
2137
Ben Blum74a11662011-05-26 16:25:20 -07002138 if (!cgroup_lock_live_group(cgrp))
2139 return -ENODEV;
2140
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141retry_find_task:
2142 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002144 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002145 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002146 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147 ret= -ESRCH;
2148 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 }
Ben Blum74a11662011-05-26 16:25:20 -07002150 /*
2151 * even if we're attaching all tasks in the thread group, we
2152 * only need to check permissions on one of them.
2153 */
David Howellsc69e8d92008-11-14 10:39:19 +11002154 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002155 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2156 !uid_eq(cred->euid, tcred->uid) &&
2157 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002158 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002159 ret = -EACCES;
2160 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002162 } else
2163 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002164
2165 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002166 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002167
2168 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002169 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002170 * trapped in a cpuset, or RT worker may be born in a cgroup
2171 * with no rt_runtime allocated. Just say no.
2172 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002173 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002174 ret = -EINVAL;
2175 rcu_read_unlock();
2176 goto out_unlock_cgroup;
2177 }
2178
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002179 get_task_struct(tsk);
2180 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002181
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002182 threadgroup_lock(tsk);
2183 if (threadgroup) {
2184 if (!thread_group_leader(tsk)) {
2185 /*
2186 * a race with de_thread from another thread's exec()
2187 * may strip us of our leadership, if this happens,
2188 * there is no choice but to throw this task away and
2189 * try again; this is
2190 * "double-double-toil-and-trouble-check locking".
2191 */
2192 threadgroup_unlock(tsk);
2193 put_task_struct(tsk);
2194 goto retry_find_task;
2195 }
Li Zefan081aa452013-03-13 09:17:09 +08002196 }
2197
2198 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2199
Tejun Heocd3d0952011-12-12 18:12:21 -08002200 threadgroup_unlock(tsk);
2201
Paul Menagebbcb81d2007-10-18 23:39:32 -07002202 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002204 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002205 return ret;
2206}
2207
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002208/**
2209 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2210 * @from: attach to all cgroups of a given task
2211 * @tsk: the task to be attached
2212 */
2213int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2214{
2215 struct cgroupfs_root *root;
2216 int retval = 0;
2217
Tejun Heo47cfcd02013-04-07 09:29:51 -07002218 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002219 for_each_active_root(root) {
2220 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2221
2222 retval = cgroup_attach_task(from_cg, tsk, false);
2223 if (retval)
2224 break;
2225 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002226 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002227
2228 return retval;
2229}
2230EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2231
Paul Menageaf351022008-07-25 01:47:01 -07002232static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2233{
Ben Blum74a11662011-05-26 16:25:20 -07002234 return attach_task_by_pid(cgrp, pid, false);
2235}
2236
2237static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2238{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002239 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002240}
2241
Paul Menagee788e0662008-07-25 01:46:59 -07002242static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2243 const char *buffer)
2244{
2245 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002246 if (strlen(buffer) >= PATH_MAX)
2247 return -EINVAL;
Paul Menagee788e0662008-07-25 01:46:59 -07002248 if (!cgroup_lock_live_group(cgrp))
2249 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002250 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002251 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002252 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002254 return 0;
2255}
2256
2257static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2258 struct seq_file *seq)
2259{
2260 if (!cgroup_lock_live_group(cgrp))
2261 return -ENODEV;
2262 seq_puts(seq, cgrp->root->release_agent_path);
2263 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002264 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002265 return 0;
2266}
2267
Tejun Heo873fe092013-04-14 20:15:26 -07002268static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2269 struct seq_file *seq)
2270{
2271 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002272 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002273}
2274
Paul Menage84eea842008-07-25 01:47:00 -07002275/* A buffer size big enough for numbers or short strings */
2276#define CGROUP_LOCAL_BUFFER_SIZE 64
2277
Paul Menagee73d2c62008-04-29 01:00:06 -07002278static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002279 struct file *file,
2280 const char __user *userbuf,
2281 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002282{
Paul Menage84eea842008-07-25 01:47:00 -07002283 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002284 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002285 char *end;
2286
2287 if (!nbytes)
2288 return -EINVAL;
2289 if (nbytes >= sizeof(buffer))
2290 return -E2BIG;
2291 if (copy_from_user(buffer, userbuf, nbytes))
2292 return -EFAULT;
2293
2294 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002295 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002296 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002297 if (*end)
2298 return -EINVAL;
2299 retval = cft->write_u64(cgrp, cft, val);
2300 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002301 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002302 if (*end)
2303 return -EINVAL;
2304 retval = cft->write_s64(cgrp, cft, val);
2305 }
Paul Menage355e0c42007-10-18 23:39:33 -07002306 if (!retval)
2307 retval = nbytes;
2308 return retval;
2309}
2310
Paul Menagedb3b1492008-07-25 01:46:58 -07002311static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2312 struct file *file,
2313 const char __user *userbuf,
2314 size_t nbytes, loff_t *unused_ppos)
2315{
Paul Menage84eea842008-07-25 01:47:00 -07002316 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002317 int retval = 0;
2318 size_t max_bytes = cft->max_write_len;
2319 char *buffer = local_buffer;
2320
2321 if (!max_bytes)
2322 max_bytes = sizeof(local_buffer) - 1;
2323 if (nbytes >= max_bytes)
2324 return -E2BIG;
2325 /* Allocate a dynamic buffer if we need one */
2326 if (nbytes >= sizeof(local_buffer)) {
2327 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2328 if (buffer == NULL)
2329 return -ENOMEM;
2330 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002331 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2332 retval = -EFAULT;
2333 goto out;
2334 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002335
2336 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002337 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002338 if (!retval)
2339 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002340out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002341 if (buffer != local_buffer)
2342 kfree(buffer);
2343 return retval;
2344}
2345
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2347 size_t nbytes, loff_t *ppos)
2348{
2349 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002350 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351
Tejun Heo54766d42013-06-12 21:04:53 -07002352 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002354 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002355 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002356 if (cft->write_u64 || cft->write_s64)
2357 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002358 if (cft->write_string)
2359 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002360 if (cft->trigger) {
2361 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2362 return ret ? ret : nbytes;
2363 }
Paul Menage355e0c42007-10-18 23:39:33 -07002364 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365}
2366
Paul Menagef4c753b2008-04-29 00:59:56 -07002367static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2368 struct file *file,
2369 char __user *buf, size_t nbytes,
2370 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371{
Paul Menage84eea842008-07-25 01:47:00 -07002372 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002373 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2375
2376 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2377}
2378
Paul Menagee73d2c62008-04-29 01:00:06 -07002379static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2380 struct file *file,
2381 char __user *buf, size_t nbytes,
2382 loff_t *ppos)
2383{
Paul Menage84eea842008-07-25 01:47:00 -07002384 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002385 s64 val = cft->read_s64(cgrp, cft);
2386 int len = sprintf(tmp, "%lld\n", (long long) val);
2387
2388 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2389}
2390
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2392 size_t nbytes, loff_t *ppos)
2393{
2394 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002395 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396
Tejun Heo54766d42013-06-12 21:04:53 -07002397 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398 return -ENODEV;
2399
2400 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002401 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002402 if (cft->read_u64)
2403 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002404 if (cft->read_s64)
2405 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406 return -EINVAL;
2407}
2408
Paul Menage91796562008-04-29 01:00:01 -07002409/*
2410 * seqfile ops/methods for returning structured data. Currently just
2411 * supports string->u64 maps, but can be extended in future.
2412 */
2413
2414struct cgroup_seqfile_state {
2415 struct cftype *cft;
2416 struct cgroup *cgroup;
2417};
2418
2419static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2420{
2421 struct seq_file *sf = cb->state;
2422 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2423}
2424
2425static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2426{
2427 struct cgroup_seqfile_state *state = m->private;
2428 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002429 if (cft->read_map) {
2430 struct cgroup_map_cb cb = {
2431 .fill = cgroup_map_add,
2432 .state = m,
2433 };
2434 return cft->read_map(state->cgroup, cft, &cb);
2435 }
2436 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002437}
2438
Adrian Bunk96930a62008-07-25 19:46:21 -07002439static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002440{
2441 struct seq_file *seq = file->private_data;
2442 kfree(seq->private);
2443 return single_release(inode, file);
2444}
2445
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002446static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002447 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07002448 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002449 .llseek = seq_lseek,
2450 .release = cgroup_seqfile_release,
2451};
2452
Paul Menageddbcc7e2007-10-18 23:39:30 -07002453static int cgroup_file_open(struct inode *inode, struct file *file)
2454{
2455 int err;
2456 struct cftype *cft;
2457
2458 err = generic_file_open(inode, file);
2459 if (err)
2460 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002462
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002463 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002464 struct cgroup_seqfile_state *state;
2465
2466 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002467 if (!state)
2468 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002469
Paul Menage91796562008-04-29 01:00:01 -07002470 state->cft = cft;
2471 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2472 file->f_op = &cgroup_seqfile_operations;
2473 err = single_open(file, cgroup_seqfile_show, state);
2474 if (err < 0)
2475 kfree(state);
2476 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477 err = cft->open(inode, file);
2478 else
2479 err = 0;
2480
2481 return err;
2482}
2483
2484static int cgroup_file_release(struct inode *inode, struct file *file)
2485{
2486 struct cftype *cft = __d_cft(file->f_dentry);
2487 if (cft->release)
2488 return cft->release(inode, file);
2489 return 0;
2490}
2491
2492/*
2493 * cgroup_rename - Only allow simple rename of directories in place.
2494 */
2495static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2496 struct inode *new_dir, struct dentry *new_dentry)
2497{
Li Zefan65dff752013-03-01 15:01:56 +08002498 int ret;
2499 struct cgroup_name *name, *old_name;
2500 struct cgroup *cgrp;
2501
2502 /*
2503 * It's convinient to use parent dir's i_mutex to protected
2504 * cgrp->name.
2505 */
2506 lockdep_assert_held(&old_dir->i_mutex);
2507
Paul Menageddbcc7e2007-10-18 23:39:30 -07002508 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2509 return -ENOTDIR;
2510 if (new_dentry->d_inode)
2511 return -EEXIST;
2512 if (old_dir != new_dir)
2513 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002514
2515 cgrp = __d_cgrp(old_dentry);
2516
Tejun Heo6db8e852013-06-14 11:18:22 -07002517 /*
2518 * This isn't a proper migration and its usefulness is very
2519 * limited. Disallow if sane_behavior.
2520 */
2521 if (cgroup_sane_behavior(cgrp))
2522 return -EPERM;
2523
Li Zefan65dff752013-03-01 15:01:56 +08002524 name = cgroup_alloc_name(new_dentry);
2525 if (!name)
2526 return -ENOMEM;
2527
2528 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2529 if (ret) {
2530 kfree(name);
2531 return ret;
2532 }
2533
2534 old_name = cgrp->name;
2535 rcu_assign_pointer(cgrp->name, name);
2536
2537 kfree_rcu(old_name, rcu_head);
2538 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002539}
2540
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002541static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2542{
2543 if (S_ISDIR(dentry->d_inode->i_mode))
2544 return &__d_cgrp(dentry)->xattrs;
2545 else
Li Zefan712317a2013-04-18 23:09:52 -07002546 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002547}
2548
2549static inline int xattr_enabled(struct dentry *dentry)
2550{
2551 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002552 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002553}
2554
2555static bool is_valid_xattr(const char *name)
2556{
2557 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2558 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2559 return true;
2560 return false;
2561}
2562
2563static int cgroup_setxattr(struct dentry *dentry, const char *name,
2564 const void *val, size_t size, int flags)
2565{
2566 if (!xattr_enabled(dentry))
2567 return -EOPNOTSUPP;
2568 if (!is_valid_xattr(name))
2569 return -EINVAL;
2570 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2571}
2572
2573static int cgroup_removexattr(struct dentry *dentry, const char *name)
2574{
2575 if (!xattr_enabled(dentry))
2576 return -EOPNOTSUPP;
2577 if (!is_valid_xattr(name))
2578 return -EINVAL;
2579 return simple_xattr_remove(__d_xattrs(dentry), name);
2580}
2581
2582static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2583 void *buf, size_t size)
2584{
2585 if (!xattr_enabled(dentry))
2586 return -EOPNOTSUPP;
2587 if (!is_valid_xattr(name))
2588 return -EINVAL;
2589 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2590}
2591
2592static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2593{
2594 if (!xattr_enabled(dentry))
2595 return -EOPNOTSUPP;
2596 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2597}
2598
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002599static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600 .read = cgroup_file_read,
2601 .write = cgroup_file_write,
2602 .llseek = generic_file_llseek,
2603 .open = cgroup_file_open,
2604 .release = cgroup_file_release,
2605};
2606
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002607static const struct inode_operations cgroup_file_inode_operations = {
2608 .setxattr = cgroup_setxattr,
2609 .getxattr = cgroup_getxattr,
2610 .listxattr = cgroup_listxattr,
2611 .removexattr = cgroup_removexattr,
2612};
2613
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002614static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002615 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 .mkdir = cgroup_mkdir,
2617 .rmdir = cgroup_rmdir,
2618 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002619 .setxattr = cgroup_setxattr,
2620 .getxattr = cgroup_getxattr,
2621 .listxattr = cgroup_listxattr,
2622 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623};
2624
Al Viro00cd8dd2012-06-10 17:13:09 -04002625static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002626{
2627 if (dentry->d_name.len > NAME_MAX)
2628 return ERR_PTR(-ENAMETOOLONG);
2629 d_add(dentry, NULL);
2630 return NULL;
2631}
2632
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002633/*
2634 * Check if a file is a control file
2635 */
2636static inline struct cftype *__file_cft(struct file *file)
2637{
Al Viro496ad9a2013-01-23 17:07:38 -05002638 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002639 return ERR_PTR(-EINVAL);
2640 return __d_cft(file->f_dentry);
2641}
2642
Al Viroa5e7ed32011-07-26 01:55:55 -04002643static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002644 struct super_block *sb)
2645{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002646 struct inode *inode;
2647
2648 if (!dentry)
2649 return -ENOENT;
2650 if (dentry->d_inode)
2651 return -EEXIST;
2652
2653 inode = cgroup_new_inode(mode, sb);
2654 if (!inode)
2655 return -ENOMEM;
2656
2657 if (S_ISDIR(mode)) {
2658 inode->i_op = &cgroup_dir_inode_operations;
2659 inode->i_fop = &simple_dir_operations;
2660
2661 /* start off with i_nlink == 2 (for "." entry) */
2662 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002663 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002664
Tejun Heob8a2df6a2012-11-19 08:13:37 -08002665 /*
2666 * Control reaches here with cgroup_mutex held.
2667 * @inode->i_mutex should nest outside cgroup_mutex but we
2668 * want to populate it immediately without releasing
2669 * cgroup_mutex. As @inode isn't visible to anyone else
2670 * yet, trylock will always succeed without affecting
2671 * lockdep checks.
2672 */
2673 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 } else if (S_ISREG(mode)) {
2675 inode->i_size = 0;
2676 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002677 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002679 d_instantiate(dentry, inode);
2680 dget(dentry); /* Extra count - pin the dentry in core */
2681 return 0;
2682}
2683
Li Zefan099fca32009-04-02 16:57:29 -07002684/**
2685 * cgroup_file_mode - deduce file mode of a control file
2686 * @cft: the control file in question
2687 *
2688 * returns cft->mode if ->mode is not 0
2689 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2690 * returns S_IRUGO if it has only a read handler
2691 * returns S_IWUSR if it has only a write hander
2692 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002693static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002694{
Al Viroa5e7ed32011-07-26 01:55:55 -04002695 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002696
2697 if (cft->mode)
2698 return cft->mode;
2699
2700 if (cft->read || cft->read_u64 || cft->read_s64 ||
2701 cft->read_map || cft->read_seq_string)
2702 mode |= S_IRUGO;
2703
2704 if (cft->write || cft->write_u64 || cft->write_s64 ||
2705 cft->write_string || cft->trigger)
2706 mode |= S_IWUSR;
2707
2708 return mode;
2709}
2710
Tejun Heodb0416b2012-04-01 12:09:55 -07002711static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002712 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713{
Paul Menagebd89aab2007-10-18 23:40:44 -07002714 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002717 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002719 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002721
Tejun Heo93438622013-04-14 20:15:25 -07002722 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 strcpy(name, subsys->name);
2724 strcat(name, ".");
2725 }
2726 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002727
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002729
2730 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2731 if (!cfe)
2732 return -ENOMEM;
2733
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002735 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737 goto out;
2738 }
2739
Li Zefand6cbf352013-05-14 19:44:20 +08002740 cfe->type = (void *)cft;
2741 cfe->dentry = dentry;
2742 dentry->d_fsdata = cfe;
2743 simple_xattrs_init(&cfe->xattrs);
2744
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 mode = cgroup_file_mode(cft);
2746 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2747 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002748 list_add_tail(&cfe->node, &parent->files);
2749 cfe = NULL;
2750 }
2751 dput(dentry);
2752out:
2753 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 return error;
2755}
2756
Tejun Heo79578622012-04-01 12:09:56 -07002757static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002758 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002760 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002761 int err, ret = 0;
2762
2763 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002764 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002765 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2766 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002767 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2768 continue;
2769 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2770 continue;
2771
Li Zefan2739d3c2013-01-21 18:18:33 +08002772 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002773 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002774 if (err)
2775 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2776 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002777 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002778 } else {
2779 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002782 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783}
2784
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002786 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002787{
2788 /*
2789 * Thanks to the entanglement with vfs inode locking, we can't walk
2790 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002791 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2792 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002794 mutex_lock(&cgroup_mutex);
2795}
2796
2797static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002798 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002799 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002800{
2801 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002802 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002803 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002804 struct dentry *prev = NULL;
2805 struct inode *inode;
2806 u64 update_upto;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807
2808 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Li Zefane8c82d22013-06-18 18:48:37 +08002809 if (!cfts || ss->root == &rootnode ||
2810 !atomic_inc_not_zero(&sb->s_active)) {
2811 mutex_unlock(&cgroup_mutex);
2812 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813 }
2814
Li Zefane8c82d22013-06-18 18:48:37 +08002815 /*
2816 * All cgroups which are created after we drop cgroup_mutex will
2817 * have the updated set of files, so we only need to update the
2818 * cgroups created before the current @cgroup_serial_nr_cursor.
2819 */
2820 update_upto = atomic64_read(&cgroup_serial_nr_cursor);
2821
Tejun Heo8e3f6542012-04-01 12:09:55 -07002822 mutex_unlock(&cgroup_mutex);
2823
Li Zefane8c82d22013-06-18 18:48:37 +08002824 /* @root always needs to be updated */
2825 inode = root->dentry->d_inode;
2826 mutex_lock(&inode->i_mutex);
2827 mutex_lock(&cgroup_mutex);
2828 cgroup_addrm_files(root, ss, cfts, is_add);
2829 mutex_unlock(&cgroup_mutex);
2830 mutex_unlock(&inode->i_mutex);
2831
2832 /* add/rm files for all cgroups created before */
2833 rcu_read_lock();
2834 cgroup_for_each_descendant_pre(cgrp, root) {
2835 if (cgroup_is_dead(cgrp))
2836 continue;
2837
2838 inode = cgrp->dentry->d_inode;
2839 dget(cgrp->dentry);
2840 rcu_read_unlock();
2841
2842 dput(prev);
2843 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002844
2845 mutex_lock(&inode->i_mutex);
2846 mutex_lock(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08002847 if (cgrp->serial_nr <= update_upto && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002848 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002849 mutex_unlock(&cgroup_mutex);
2850 mutex_unlock(&inode->i_mutex);
2851
Li Zefane8c82d22013-06-18 18:48:37 +08002852 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853 }
Li Zefane8c82d22013-06-18 18:48:37 +08002854 rcu_read_unlock();
2855 dput(prev);
2856 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002857}
2858
2859/**
2860 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2861 * @ss: target cgroup subsystem
2862 * @cfts: zero-length name terminated array of cftypes
2863 *
2864 * Register @cfts to @ss. Files described by @cfts are created for all
2865 * existing cgroups to which @ss is attached and all future cgroups will
2866 * have them too. This function can be called anytime whether @ss is
2867 * attached or not.
2868 *
2869 * Returns 0 on successful registration, -errno on failure. Note that this
2870 * function currently returns 0 as long as @cfts registration is successful
2871 * even if some file creation attempts on existing cgroups fail.
2872 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002873int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002874{
2875 struct cftype_set *set;
2876
2877 set = kzalloc(sizeof(*set), GFP_KERNEL);
2878 if (!set)
2879 return -ENOMEM;
2880
2881 cgroup_cfts_prepare();
2882 set->cfts = cfts;
2883 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002884 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885
2886 return 0;
2887}
2888EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2889
Li Zefana043e3b2008-02-23 15:24:09 -08002890/**
Tejun Heo79578622012-04-01 12:09:56 -07002891 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2892 * @ss: target cgroup subsystem
2893 * @cfts: zero-length name terminated array of cftypes
2894 *
2895 * Unregister @cfts from @ss. Files described by @cfts are removed from
2896 * all existing cgroups to which @ss is attached and all future cgroups
2897 * won't have them either. This function can be called anytime whether @ss
2898 * is attached or not.
2899 *
2900 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2901 * registered with @ss.
2902 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002903int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002904{
2905 struct cftype_set *set;
2906
2907 cgroup_cfts_prepare();
2908
2909 list_for_each_entry(set, &ss->cftsets, node) {
2910 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002911 list_del(&set->node);
2912 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002913 cgroup_cfts_commit(ss, cfts, false);
2914 return 0;
2915 }
2916 }
2917
2918 cgroup_cfts_commit(ss, NULL, false);
2919 return -ENOENT;
2920}
2921
2922/**
Li Zefana043e3b2008-02-23 15:24:09 -08002923 * cgroup_task_count - count the number of tasks in a cgroup.
2924 * @cgrp: the cgroup in question
2925 *
2926 * Return the number of tasks in the cgroup.
2927 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002928int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002929{
2930 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002931 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002932
Paul Menage817929e2007-10-18 23:39:36 -07002933 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002934 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2935 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002936 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002937 return count;
2938}
2939
2940/*
Paul Menage817929e2007-10-18 23:39:36 -07002941 * Advance a list_head iterator. The iterator should be positioned at
2942 * the start of a css_set
2943 */
Tejun Heo69d02062013-06-12 21:04:50 -07002944static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002945{
Tejun Heo69d02062013-06-12 21:04:50 -07002946 struct list_head *l = it->cset_link;
2947 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002948 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002949
2950 /* Advance to the next non-empty css_set */
2951 do {
2952 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002953 if (l == &cgrp->cset_links) {
2954 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002955 return;
2956 }
Tejun Heo69d02062013-06-12 21:04:50 -07002957 link = list_entry(l, struct cgrp_cset_link, cset_link);
2958 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002959 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002960 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002961 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002962}
2963
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964/*
2965 * To reduce the fork() overhead for systems that are not actually
2966 * using their cgroups capability, we don't maintain the lists running
2967 * through each css_set to its tasks until we see the list actually
2968 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002969 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002970static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002971{
2972 struct task_struct *p, *g;
2973 write_lock(&css_set_lock);
2974 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002975 /*
2976 * We need tasklist_lock because RCU is not safe against
2977 * while_each_thread(). Besides, a forking task that has passed
2978 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2979 * is not guaranteed to have its child immediately visible in the
2980 * tasklist if we walk through it with RCU.
2981 */
2982 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002983 do_each_thread(g, p) {
2984 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002985 /*
2986 * We should check if the process is exiting, otherwise
2987 * it will race with cgroup_exit() in that the list
2988 * entry won't be deleted though the process has exited.
2989 */
2990 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991 list_add(&p->cg_list, &p->cgroups->tasks);
2992 task_unlock(p);
2993 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002994 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002995 write_unlock(&css_set_lock);
2996}
2997
Tejun Heo574bd9f2012-11-09 09:12:29 -08002998/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002999 * cgroup_next_sibling - find the next sibling of a given cgroup
3000 * @pos: the current cgroup
3001 *
3002 * This function returns the next sibling of @pos and should be called
3003 * under RCU read lock. The only requirement is that @pos is accessible.
3004 * The next sibling is guaranteed to be returned regardless of @pos's
3005 * state.
3006 */
3007struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3008{
3009 struct cgroup *next;
3010
3011 WARN_ON_ONCE(!rcu_read_lock_held());
3012
3013 /*
3014 * @pos could already have been removed. Once a cgroup is removed,
3015 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003016 * changes. As CGRP_DEAD assertion is serialized and happens
3017 * before the cgroup is taken off the ->sibling list, if we see it
3018 * unasserted, it's guaranteed that the next sibling hasn't
3019 * finished its grace period even if it's already removed, and thus
3020 * safe to dereference from this RCU critical section. If
3021 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3022 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003023 */
Tejun Heo54766d42013-06-12 21:04:53 -07003024 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003025 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3026 if (&next->sibling != &pos->parent->children)
3027 return next;
3028 return NULL;
3029 }
3030
3031 /*
3032 * Can't dereference the next pointer. Each cgroup is given a
3033 * monotonically increasing unique serial number and always
3034 * appended to the sibling list, so the next one can be found by
3035 * walking the parent's children until we see a cgroup with higher
3036 * serial number than @pos's.
3037 *
3038 * While this path can be slow, it's taken only when either the
3039 * current cgroup is removed or iteration and removal race.
3040 */
3041 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3042 if (next->serial_nr > pos->serial_nr)
3043 return next;
3044 return NULL;
3045}
3046EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3047
3048/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003049 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3050 * @pos: the current position (%NULL to initiate traversal)
3051 * @cgroup: cgroup whose descendants to walk
3052 *
3053 * To be used by cgroup_for_each_descendant_pre(). Find the next
3054 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003055 *
3056 * While this function requires RCU read locking, it doesn't require the
3057 * whole traversal to be contained in a single RCU critical section. This
3058 * function will return the correct next descendant as long as both @pos
3059 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003060 */
3061struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3062 struct cgroup *cgroup)
3063{
3064 struct cgroup *next;
3065
3066 WARN_ON_ONCE(!rcu_read_lock_held());
3067
3068 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003069 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003070 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003071
3072 /* visit the first child if exists */
3073 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3074 if (next)
3075 return next;
3076
3077 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003078 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003079 next = cgroup_next_sibling(pos);
3080 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003083 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003084
3085 return NULL;
3086}
3087EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3088
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003089/**
3090 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3091 * @pos: cgroup of interest
3092 *
3093 * Return the rightmost descendant of @pos. If there's no descendant,
3094 * @pos is returned. This can be used during pre-order traversal to skip
3095 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003096 *
3097 * While this function requires RCU read locking, it doesn't require the
3098 * whole traversal to be contained in a single RCU critical section. This
3099 * function will return the correct rightmost descendant as long as @pos is
3100 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003101 */
3102struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3103{
3104 struct cgroup *last, *tmp;
3105
3106 WARN_ON_ONCE(!rcu_read_lock_held());
3107
3108 do {
3109 last = pos;
3110 /* ->prev isn't RCU safe, walk ->next till the end */
3111 pos = NULL;
3112 list_for_each_entry_rcu(tmp, &last->children, sibling)
3113 pos = tmp;
3114 } while (pos);
3115
3116 return last;
3117}
3118EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3119
Tejun Heo574bd9f2012-11-09 09:12:29 -08003120static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3121{
3122 struct cgroup *last;
3123
3124 do {
3125 last = pos;
3126 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3127 sibling);
3128 } while (pos);
3129
3130 return last;
3131}
3132
3133/**
3134 * cgroup_next_descendant_post - find the next descendant for post-order walk
3135 * @pos: the current position (%NULL to initiate traversal)
3136 * @cgroup: cgroup whose descendants to walk
3137 *
3138 * To be used by cgroup_for_each_descendant_post(). Find the next
3139 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003140 *
3141 * While this function requires RCU read locking, it doesn't require the
3142 * whole traversal to be contained in a single RCU critical section. This
3143 * function will return the correct next descendant as long as both @pos
3144 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003145 */
3146struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3147 struct cgroup *cgroup)
3148{
3149 struct cgroup *next;
3150
3151 WARN_ON_ONCE(!rcu_read_lock_held());
3152
3153 /* if first iteration, visit the leftmost descendant */
3154 if (!pos) {
3155 next = cgroup_leftmost_descendant(cgroup);
3156 return next != cgroup ? next : NULL;
3157 }
3158
3159 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003160 next = cgroup_next_sibling(pos);
3161 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003162 return cgroup_leftmost_descendant(next);
3163
3164 /* no sibling left, visit parent */
3165 next = pos->parent;
3166 return next != cgroup ? next : NULL;
3167}
3168EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3169
Paul Menagebd89aab2007-10-18 23:40:44 -07003170void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003171 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003172{
3173 /*
3174 * The first time anyone tries to iterate across a cgroup,
3175 * we need to enable the list linking each css_set to its
3176 * tasks, and fix up all existing tasks.
3177 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003178 if (!use_task_css_set_links)
3179 cgroup_enable_task_cg_lists();
3180
Paul Menage817929e2007-10-18 23:39:36 -07003181 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003182 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003183 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003184}
3185
Paul Menagebd89aab2007-10-18 23:40:44 -07003186struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003187 struct cgroup_iter *it)
3188{
3189 struct task_struct *res;
3190 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003191 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003192
3193 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003194 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003195 return NULL;
3196 res = list_entry(l, struct task_struct, cg_list);
3197 /* Advance iterator to find next entry */
3198 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003199 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3200 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003201 /* We reached the end of this task list - move on to
3202 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003203 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003204 } else {
3205 it->task = l;
3206 }
3207 return res;
3208}
3209
Paul Menagebd89aab2007-10-18 23:40:44 -07003210void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003211 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003212{
3213 read_unlock(&css_set_lock);
3214}
3215
Cliff Wickman31a7df02008-02-07 00:14:42 -08003216static inline int started_after_time(struct task_struct *t1,
3217 struct timespec *time,
3218 struct task_struct *t2)
3219{
3220 int start_diff = timespec_compare(&t1->start_time, time);
3221 if (start_diff > 0) {
3222 return 1;
3223 } else if (start_diff < 0) {
3224 return 0;
3225 } else {
3226 /*
3227 * Arbitrarily, if two processes started at the same
3228 * time, we'll say that the lower pointer value
3229 * started first. Note that t2 may have exited by now
3230 * so this may not be a valid pointer any longer, but
3231 * that's fine - it still serves to distinguish
3232 * between two tasks started (effectively) simultaneously.
3233 */
3234 return t1 > t2;
3235 }
3236}
3237
3238/*
3239 * This function is a callback from heap_insert() and is used to order
3240 * the heap.
3241 * In this case we order the heap in descending task start time.
3242 */
3243static inline int started_after(void *p1, void *p2)
3244{
3245 struct task_struct *t1 = p1;
3246 struct task_struct *t2 = p2;
3247 return started_after_time(t1, &t2->start_time, t2);
3248}
3249
3250/**
3251 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3252 * @scan: struct cgroup_scanner containing arguments for the scan
3253 *
3254 * Arguments include pointers to callback functions test_task() and
3255 * process_task().
3256 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3257 * and if it returns true, call process_task() for it also.
3258 * The test_task pointer may be NULL, meaning always true (select all tasks).
3259 * Effectively duplicates cgroup_iter_{start,next,end}()
3260 * but does not lock css_set_lock for the call to process_task().
3261 * The struct cgroup_scanner may be embedded in any structure of the caller's
3262 * creation.
3263 * It is guaranteed that process_task() will act on every task that
3264 * is a member of the cgroup for the duration of this call. This
3265 * function may or may not call process_task() for tasks that exit
3266 * or move to a different cgroup during the call, or are forked or
3267 * move into the cgroup during the call.
3268 *
3269 * Note that test_task() may be called with locks held, and may in some
3270 * situations be called multiple times for the same task, so it should
3271 * be cheap.
3272 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3273 * pre-allocated and will be used for heap operations (and its "gt" member will
3274 * be overwritten), else a temporary heap will be used (allocation of which
3275 * may cause this function to fail).
3276 */
3277int cgroup_scan_tasks(struct cgroup_scanner *scan)
3278{
3279 int retval, i;
3280 struct cgroup_iter it;
3281 struct task_struct *p, *dropped;
3282 /* Never dereference latest_task, since it's not refcounted */
3283 struct task_struct *latest_task = NULL;
3284 struct ptr_heap tmp_heap;
3285 struct ptr_heap *heap;
3286 struct timespec latest_time = { 0, 0 };
3287
3288 if (scan->heap) {
3289 /* The caller supplied our heap and pre-allocated its memory */
3290 heap = scan->heap;
3291 heap->gt = &started_after;
3292 } else {
3293 /* We need to allocate our own heap memory */
3294 heap = &tmp_heap;
3295 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3296 if (retval)
3297 /* cannot allocate the heap */
3298 return retval;
3299 }
3300
3301 again:
3302 /*
3303 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3304 * to determine which are of interest, and using the scanner's
3305 * "process_task" callback to process any of them that need an update.
3306 * Since we don't want to hold any locks during the task updates,
3307 * gather tasks to be processed in a heap structure.
3308 * The heap is sorted by descending task start time.
3309 * If the statically-sized heap fills up, we overflow tasks that
3310 * started later, and in future iterations only consider tasks that
3311 * started after the latest task in the previous pass. This
3312 * guarantees forward progress and that we don't miss any tasks.
3313 */
3314 heap->size = 0;
3315 cgroup_iter_start(scan->cg, &it);
3316 while ((p = cgroup_iter_next(scan->cg, &it))) {
3317 /*
3318 * Only affect tasks that qualify per the caller's callback,
3319 * if he provided one
3320 */
3321 if (scan->test_task && !scan->test_task(p, scan))
3322 continue;
3323 /*
3324 * Only process tasks that started after the last task
3325 * we processed
3326 */
3327 if (!started_after_time(p, &latest_time, latest_task))
3328 continue;
3329 dropped = heap_insert(heap, p);
3330 if (dropped == NULL) {
3331 /*
3332 * The new task was inserted; the heap wasn't
3333 * previously full
3334 */
3335 get_task_struct(p);
3336 } else if (dropped != p) {
3337 /*
3338 * The new task was inserted, and pushed out a
3339 * different task
3340 */
3341 get_task_struct(p);
3342 put_task_struct(dropped);
3343 }
3344 /*
3345 * Else the new task was newer than anything already in
3346 * the heap and wasn't inserted
3347 */
3348 }
3349 cgroup_iter_end(scan->cg, &it);
3350
3351 if (heap->size) {
3352 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003353 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003354 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003355 latest_time = q->start_time;
3356 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 }
3358 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003359 scan->process_task(q, scan);
3360 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003361 }
3362 /*
3363 * If we had to process any tasks at all, scan again
3364 * in case some of them were in the middle of forking
3365 * children that didn't get processed.
3366 * Not the most efficient way to do it, but it avoids
3367 * having to take callback_mutex in the fork path
3368 */
3369 goto again;
3370 }
3371 if (heap == &tmp_heap)
3372 heap_free(&tmp_heap);
3373 return 0;
3374}
3375
Tejun Heo8cc99342013-04-07 09:29:50 -07003376static void cgroup_transfer_one_task(struct task_struct *task,
3377 struct cgroup_scanner *scan)
3378{
3379 struct cgroup *new_cgroup = scan->data;
3380
Tejun Heo47cfcd02013-04-07 09:29:51 -07003381 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003382 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003383 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003384}
3385
3386/**
3387 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3388 * @to: cgroup to which the tasks will be moved
3389 * @from: cgroup in which the tasks currently reside
3390 */
3391int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3392{
3393 struct cgroup_scanner scan;
3394
3395 scan.cg = from;
3396 scan.test_task = NULL; /* select all tasks in cgroup */
3397 scan.process_task = cgroup_transfer_one_task;
3398 scan.heap = NULL;
3399 scan.data = to;
3400
3401 return cgroup_scan_tasks(&scan);
3402}
3403
Paul Menage817929e2007-10-18 23:39:36 -07003404/*
Ben Blum102a7752009-09-23 15:56:26 -07003405 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003406 *
3407 * Reading this file can return large amounts of data if a cgroup has
3408 * *lots* of attached tasks. So it may need several calls to read(),
3409 * but we cannot guarantee that the information we produce is correct
3410 * unless we produce it entirely atomically.
3411 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003412 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003413
Li Zefan24528252012-01-20 11:58:43 +08003414/* which pidlist file are we talking about? */
3415enum cgroup_filetype {
3416 CGROUP_FILE_PROCS,
3417 CGROUP_FILE_TASKS,
3418};
3419
3420/*
3421 * A pidlist is a list of pids that virtually represents the contents of one
3422 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3423 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3424 * to the cgroup.
3425 */
3426struct cgroup_pidlist {
3427 /*
3428 * used to find which pidlist is wanted. doesn't change as long as
3429 * this particular list stays in the list.
3430 */
3431 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3432 /* array of xids */
3433 pid_t *list;
3434 /* how many elements the above list has */
3435 int length;
3436 /* how many files are using the current array */
3437 int use_count;
3438 /* each of these stored in a list by its cgroup */
3439 struct list_head links;
3440 /* pointer to the cgroup we belong to, for list removal purposes */
3441 struct cgroup *owner;
3442 /* protects the other fields */
3443 struct rw_semaphore mutex;
3444};
3445
Paul Menagebbcb81d2007-10-18 23:39:32 -07003446/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003447 * The following two functions "fix" the issue where there are more pids
3448 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3449 * TODO: replace with a kernel-wide solution to this problem
3450 */
3451#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3452static void *pidlist_allocate(int count)
3453{
3454 if (PIDLIST_TOO_LARGE(count))
3455 return vmalloc(count * sizeof(pid_t));
3456 else
3457 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3458}
3459static void pidlist_free(void *p)
3460{
3461 if (is_vmalloc_addr(p))
3462 vfree(p);
3463 else
3464 kfree(p);
3465}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003466
3467/*
Ben Blum102a7752009-09-23 15:56:26 -07003468 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003469 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003470 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003471static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003472{
Ben Blum102a7752009-09-23 15:56:26 -07003473 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003474
3475 /*
3476 * we presume the 0th element is unique, so i starts at 1. trivial
3477 * edge cases first; no work needs to be done for either
3478 */
3479 if (length == 0 || length == 1)
3480 return length;
3481 /* src and dest walk down the list; dest counts unique elements */
3482 for (src = 1; src < length; src++) {
3483 /* find next unique element */
3484 while (list[src] == list[src-1]) {
3485 src++;
3486 if (src == length)
3487 goto after;
3488 }
3489 /* dest always points to where the next unique element goes */
3490 list[dest] = list[src];
3491 dest++;
3492 }
3493after:
Ben Blum102a7752009-09-23 15:56:26 -07003494 return dest;
3495}
3496
3497static int cmppid(const void *a, const void *b)
3498{
3499 return *(pid_t *)a - *(pid_t *)b;
3500}
3501
3502/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003503 * find the appropriate pidlist for our purpose (given procs vs tasks)
3504 * returns with the lock on that pidlist already held, and takes care
3505 * of the use count, or returns NULL with no locks held if we're out of
3506 * memory.
3507 */
3508static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3509 enum cgroup_filetype type)
3510{
3511 struct cgroup_pidlist *l;
3512 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003513 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003514
Ben Blum72a8cb32009-09-23 15:56:27 -07003515 /*
3516 * We can't drop the pidlist_mutex before taking the l->mutex in case
3517 * the last ref-holder is trying to remove l from the list at the same
3518 * time. Holding the pidlist_mutex precludes somebody taking whichever
3519 * list we find out from under us - compare release_pid_array().
3520 */
3521 mutex_lock(&cgrp->pidlist_mutex);
3522 list_for_each_entry(l, &cgrp->pidlists, links) {
3523 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003524 /* make sure l doesn't vanish out from under us */
3525 down_write(&l->mutex);
3526 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 return l;
3528 }
3529 }
3530 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003531 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003532 if (!l) {
3533 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003534 return l;
3535 }
3536 init_rwsem(&l->mutex);
3537 down_write(&l->mutex);
3538 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003539 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003540 l->owner = cgrp;
3541 list_add(&l->links, &cgrp->pidlists);
3542 mutex_unlock(&cgrp->pidlist_mutex);
3543 return l;
3544}
3545
3546/*
Ben Blum102a7752009-09-23 15:56:26 -07003547 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3548 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003549static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3550 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003551{
3552 pid_t *array;
3553 int length;
3554 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003555 struct cgroup_iter it;
3556 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003557 struct cgroup_pidlist *l;
3558
3559 /*
3560 * If cgroup gets more users after we read count, we won't have
3561 * enough space - tough. This race is indistinguishable to the
3562 * caller from the case that the additional cgroup users didn't
3563 * show up until sometime later on.
3564 */
3565 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003566 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003567 if (!array)
3568 return -ENOMEM;
3569 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003570 cgroup_iter_start(cgrp, &it);
3571 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003572 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003573 break;
Ben Blum102a7752009-09-23 15:56:26 -07003574 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003575 if (type == CGROUP_FILE_PROCS)
3576 pid = task_tgid_vnr(tsk);
3577 else
3578 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003579 if (pid > 0) /* make sure to only use valid results */
3580 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003581 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003582 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003583 length = n;
3584 /* now sort & (if procs) strip out duplicates */
3585 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003586 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003587 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003588 l = cgroup_pidlist_find(cgrp, type);
3589 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003590 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003592 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003593 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003594 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003595 l->list = array;
3596 l->length = length;
3597 l->use_count++;
3598 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003599 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003600 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003601}
3602
Balbir Singh846c7bb2007-10-18 23:39:44 -07003603/**
Li Zefana043e3b2008-02-23 15:24:09 -08003604 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003605 * @stats: cgroupstats to fill information into
3606 * @dentry: A dentry entry belonging to the cgroup for which stats have
3607 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003608 *
3609 * Build and fill cgroupstats so that taskstats can export it to user
3610 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003611 */
3612int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3613{
3614 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003615 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003616 struct cgroup_iter it;
3617 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003618
Balbir Singh846c7bb2007-10-18 23:39:44 -07003619 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003620 * Validate dentry by checking the superblock operations,
3621 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003622 */
Li Zefan33d283b2008-11-19 15:36:48 -08003623 if (dentry->d_sb->s_op != &cgroup_ops ||
3624 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625 goto err;
3626
3627 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003628 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003629
Paul Menagebd89aab2007-10-18 23:40:44 -07003630 cgroup_iter_start(cgrp, &it);
3631 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003632 switch (tsk->state) {
3633 case TASK_RUNNING:
3634 stats->nr_running++;
3635 break;
3636 case TASK_INTERRUPTIBLE:
3637 stats->nr_sleeping++;
3638 break;
3639 case TASK_UNINTERRUPTIBLE:
3640 stats->nr_uninterruptible++;
3641 break;
3642 case TASK_STOPPED:
3643 stats->nr_stopped++;
3644 break;
3645 default:
3646 if (delayacct_is_task_waiting_on_io(tsk))
3647 stats->nr_io_wait++;
3648 break;
3649 }
3650 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003651 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003652
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653err:
3654 return ret;
3655}
3656
Paul Menage8f3ff202009-09-23 15:56:25 -07003657
Paul Menagecc31edc2008-10-18 20:28:04 -07003658/*
Ben Blum102a7752009-09-23 15:56:26 -07003659 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003660 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003661 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003662 */
3663
Ben Blum102a7752009-09-23 15:56:26 -07003664static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003665{
3666 /*
3667 * Initially we receive a position value that corresponds to
3668 * one more than the last pid shown (or 0 on the first call or
3669 * after a seek to the start). Use a binary-search to find the
3670 * next pid to display, if any
3671 */
Ben Blum102a7752009-09-23 15:56:26 -07003672 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 int index = 0, pid = *pos;
3674 int *iter;
3675
Ben Blum102a7752009-09-23 15:56:26 -07003676 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003677 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003678 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003679
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 while (index < end) {
3681 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003682 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003683 index = mid;
3684 break;
Ben Blum102a7752009-09-23 15:56:26 -07003685 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 index = mid + 1;
3687 else
3688 end = mid;
3689 }
3690 }
3691 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003692 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003693 return NULL;
3694 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003695 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 *pos = *iter;
3697 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003698}
3699
Ben Blum102a7752009-09-23 15:56:26 -07003700static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003701{
Ben Blum102a7752009-09-23 15:56:26 -07003702 struct cgroup_pidlist *l = s->private;
3703 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003704}
3705
Ben Blum102a7752009-09-23 15:56:26 -07003706static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003707{
Ben Blum102a7752009-09-23 15:56:26 -07003708 struct cgroup_pidlist *l = s->private;
3709 pid_t *p = v;
3710 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 /*
3712 * Advance to the next pid in the array. If this goes off the
3713 * end, we're done
3714 */
3715 p++;
3716 if (p >= end) {
3717 return NULL;
3718 } else {
3719 *pos = *p;
3720 return p;
3721 }
3722}
3723
Ben Blum102a7752009-09-23 15:56:26 -07003724static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003725{
3726 return seq_printf(s, "%d\n", *(int *)v);
3727}
3728
Ben Blum102a7752009-09-23 15:56:26 -07003729/*
3730 * seq_operations functions for iterating on pidlists through seq_file -
3731 * independent of whether it's tasks or procs
3732 */
3733static const struct seq_operations cgroup_pidlist_seq_operations = {
3734 .start = cgroup_pidlist_start,
3735 .stop = cgroup_pidlist_stop,
3736 .next = cgroup_pidlist_next,
3737 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003738};
3739
Ben Blum102a7752009-09-23 15:56:26 -07003740static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003741{
Ben Blum72a8cb32009-09-23 15:56:27 -07003742 /*
3743 * the case where we're the last user of this particular pidlist will
3744 * have us remove it from the cgroup's list, which entails taking the
3745 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3746 * pidlist_mutex, we have to take pidlist_mutex first.
3747 */
3748 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003749 down_write(&l->mutex);
3750 BUG_ON(!l->use_count);
3751 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003752 /* we're the last user if refcount is 0; remove and free */
3753 list_del(&l->links);
3754 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003755 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003756 put_pid_ns(l->key.ns);
3757 up_write(&l->mutex);
3758 kfree(l);
3759 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003760 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003761 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003762 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003763}
3764
Ben Blum102a7752009-09-23 15:56:26 -07003765static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003766{
Ben Blum102a7752009-09-23 15:56:26 -07003767 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003768 if (!(file->f_mode & FMODE_READ))
3769 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003770 /*
3771 * the seq_file will only be initialized if the file was opened for
3772 * reading; hence we check if it's not null only in that case.
3773 */
3774 l = ((struct seq_file *)file->private_data)->private;
3775 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 return seq_release(inode, file);
3777}
3778
Ben Blum102a7752009-09-23 15:56:26 -07003779static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 .read = seq_read,
3781 .llseek = seq_lseek,
3782 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003783 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003784};
3785
3786/*
Ben Blum102a7752009-09-23 15:56:26 -07003787 * The following functions handle opens on a file that displays a pidlist
3788 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3789 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 */
Ben Blum102a7752009-09-23 15:56:26 -07003791/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003792static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003793{
3794 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003795 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003796 int retval;
3797
3798 /* Nothing to do for write-only files */
3799 if (!(file->f_mode & FMODE_READ))
3800 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003801
Ben Blum102a7752009-09-23 15:56:26 -07003802 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003803 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003804 if (retval)
3805 return retval;
3806 /* configure file information */
3807 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003808
Ben Blum102a7752009-09-23 15:56:26 -07003809 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003810 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003811 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003812 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003813 }
Ben Blum102a7752009-09-23 15:56:26 -07003814 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003815 return 0;
3816}
Ben Blum102a7752009-09-23 15:56:26 -07003817static int cgroup_tasks_open(struct inode *unused, struct file *file)
3818{
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003820}
3821static int cgroup_procs_open(struct inode *unused, struct file *file)
3822{
Ben Blum72a8cb32009-09-23 15:56:27 -07003823 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003824}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003825
Paul Menagebd89aab2007-10-18 23:40:44 -07003826static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003827 struct cftype *cft)
3828{
Paul Menagebd89aab2007-10-18 23:40:44 -07003829 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003830}
3831
Paul Menage6379c102008-07-25 01:47:01 -07003832static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3833 struct cftype *cft,
3834 u64 val)
3835{
3836 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3837 if (val)
3838 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3839 else
3840 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3841 return 0;
3842}
3843
Paul Menagebbcb81d2007-10-18 23:39:32 -07003844/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003845 * When dput() is called asynchronously, if umount has been done and
3846 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3847 * there's a small window that vfs will see the root dentry with non-zero
3848 * refcnt and trigger BUG().
3849 *
3850 * That's why we hold a reference before dput() and drop it right after.
3851 */
3852static void cgroup_dput(struct cgroup *cgrp)
3853{
3854 struct super_block *sb = cgrp->root->sb;
3855
3856 atomic_inc(&sb->s_active);
3857 dput(cgrp->dentry);
3858 deactivate_super(sb);
3859}
3860
3861/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003862 * Unregister event and free resources.
3863 *
3864 * Gets called from workqueue.
3865 */
3866static void cgroup_event_remove(struct work_struct *work)
3867{
3868 struct cgroup_event *event = container_of(work, struct cgroup_event,
3869 remove);
3870 struct cgroup *cgrp = event->cgrp;
3871
Li Zefan810cbee2013-02-18 18:56:14 +08003872 remove_wait_queue(event->wqh, &event->wait);
3873
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003874 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3875
Li Zefan810cbee2013-02-18 18:56:14 +08003876 /* Notify userspace the event is going away. */
3877 eventfd_signal(event->eventfd, 1);
3878
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003879 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003880 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003881 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882}
3883
3884/*
3885 * Gets called on POLLHUP on eventfd when user closes it.
3886 *
3887 * Called with wqh->lock held and interrupts disabled.
3888 */
3889static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3890 int sync, void *key)
3891{
3892 struct cgroup_event *event = container_of(wait,
3893 struct cgroup_event, wait);
3894 struct cgroup *cgrp = event->cgrp;
3895 unsigned long flags = (unsigned long)key;
3896
3897 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003898 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003899 * If the event has been detached at cgroup removal, we
3900 * can simply return knowing the other side will cleanup
3901 * for us.
3902 *
3903 * We can't race against event freeing since the other
3904 * side will require wqh->lock via remove_wait_queue(),
3905 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003906 */
Li Zefan810cbee2013-02-18 18:56:14 +08003907 spin_lock(&cgrp->event_list_lock);
3908 if (!list_empty(&event->list)) {
3909 list_del_init(&event->list);
3910 /*
3911 * We are in atomic context, but cgroup_event_remove()
3912 * may sleep, so we have to call it in workqueue.
3913 */
3914 schedule_work(&event->remove);
3915 }
3916 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003917 }
3918
3919 return 0;
3920}
3921
3922static void cgroup_event_ptable_queue_proc(struct file *file,
3923 wait_queue_head_t *wqh, poll_table *pt)
3924{
3925 struct cgroup_event *event = container_of(pt,
3926 struct cgroup_event, pt);
3927
3928 event->wqh = wqh;
3929 add_wait_queue(wqh, &event->wait);
3930}
3931
3932/*
3933 * Parse input and register new cgroup event handler.
3934 *
3935 * Input must be in format '<event_fd> <control_fd> <args>'.
3936 * Interpretation of args is defined by control file implementation.
3937 */
3938static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3939 const char *buffer)
3940{
3941 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003942 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003943 unsigned int efd, cfd;
3944 struct file *efile = NULL;
3945 struct file *cfile = NULL;
3946 char *endp;
3947 int ret;
3948
3949 efd = simple_strtoul(buffer, &endp, 10);
3950 if (*endp != ' ')
3951 return -EINVAL;
3952 buffer = endp + 1;
3953
3954 cfd = simple_strtoul(buffer, &endp, 10);
3955 if ((*endp != ' ') && (*endp != '\0'))
3956 return -EINVAL;
3957 buffer = endp + 1;
3958
3959 event = kzalloc(sizeof(*event), GFP_KERNEL);
3960 if (!event)
3961 return -ENOMEM;
3962 event->cgrp = cgrp;
3963 INIT_LIST_HEAD(&event->list);
3964 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3965 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3966 INIT_WORK(&event->remove, cgroup_event_remove);
3967
3968 efile = eventfd_fget(efd);
3969 if (IS_ERR(efile)) {
3970 ret = PTR_ERR(efile);
3971 goto fail;
3972 }
3973
3974 event->eventfd = eventfd_ctx_fileget(efile);
3975 if (IS_ERR(event->eventfd)) {
3976 ret = PTR_ERR(event->eventfd);
3977 goto fail;
3978 }
3979
3980 cfile = fget(cfd);
3981 if (!cfile) {
3982 ret = -EBADF;
3983 goto fail;
3984 }
3985
3986 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003987 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003988 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003989 if (ret < 0)
3990 goto fail;
3991
3992 event->cft = __file_cft(cfile);
3993 if (IS_ERR(event->cft)) {
3994 ret = PTR_ERR(event->cft);
3995 goto fail;
3996 }
3997
Li Zefanf1690072013-02-18 14:13:35 +08003998 /*
3999 * The file to be monitored must be in the same cgroup as
4000 * cgroup.event_control is.
4001 */
4002 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4003 if (cgrp_cfile != cgrp) {
4004 ret = -EINVAL;
4005 goto fail;
4006 }
4007
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004008 if (!event->cft->register_event || !event->cft->unregister_event) {
4009 ret = -EINVAL;
4010 goto fail;
4011 }
4012
4013 ret = event->cft->register_event(cgrp, event->cft,
4014 event->eventfd, buffer);
4015 if (ret)
4016 goto fail;
4017
Li Zefan7ef70e42013-04-26 11:58:03 -07004018 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004019
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004020 /*
4021 * Events should be removed after rmdir of cgroup directory, but before
4022 * destroying subsystem state objects. Let's take reference to cgroup
4023 * directory dentry to do that.
4024 */
4025 dget(cgrp->dentry);
4026
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004027 spin_lock(&cgrp->event_list_lock);
4028 list_add(&event->list, &cgrp->event_list);
4029 spin_unlock(&cgrp->event_list_lock);
4030
4031 fput(cfile);
4032 fput(efile);
4033
4034 return 0;
4035
4036fail:
4037 if (cfile)
4038 fput(cfile);
4039
4040 if (event && event->eventfd && !IS_ERR(event->eventfd))
4041 eventfd_ctx_put(event->eventfd);
4042
4043 if (!IS_ERR_OR_NULL(efile))
4044 fput(efile);
4045
4046 kfree(event);
4047
4048 return ret;
4049}
4050
Daniel Lezcano97978e62010-10-27 15:33:35 -07004051static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4052 struct cftype *cft)
4053{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004054 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004055}
4056
4057static int cgroup_clone_children_write(struct cgroup *cgrp,
4058 struct cftype *cft,
4059 u64 val)
4060{
4061 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004062 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004063 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004064 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004065 return 0;
4066}
4067
Tejun Heod5c56ce2013-06-03 19:14:34 -07004068static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004069 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004070 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004071 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004072 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004073 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004074 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004075 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004076 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004077 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004078 .write_string = cgroup_write_event_control,
4079 .mode = S_IWUGO,
4080 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004081 {
4082 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004083 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004084 .read_u64 = cgroup_clone_children_read,
4085 .write_u64 = cgroup_clone_children_write,
4086 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004087 {
Tejun Heo873fe092013-04-14 20:15:26 -07004088 .name = "cgroup.sane_behavior",
4089 .flags = CFTYPE_ONLY_ON_ROOT,
4090 .read_seq_string = cgroup_sane_behavior_show,
4091 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004092
4093 /*
4094 * Historical crazy stuff. These don't have "cgroup." prefix and
4095 * don't exist if sane_behavior. If you're depending on these, be
4096 * prepared to be burned.
4097 */
4098 {
4099 .name = "tasks",
4100 .flags = CFTYPE_INSANE, /* use "procs" instead */
4101 .open = cgroup_tasks_open,
4102 .write_u64 = cgroup_tasks_write,
4103 .release = cgroup_pidlist_release,
4104 .mode = S_IRUGO | S_IWUSR,
4105 },
4106 {
4107 .name = "notify_on_release",
4108 .flags = CFTYPE_INSANE,
4109 .read_u64 = cgroup_read_notify_on_release,
4110 .write_u64 = cgroup_write_notify_on_release,
4111 },
Tejun Heo873fe092013-04-14 20:15:26 -07004112 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004113 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004114 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004115 .read_seq_string = cgroup_release_agent_show,
4116 .write_string = cgroup_release_agent_write,
4117 .max_write_len = PATH_MAX,
4118 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004119 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004120};
4121
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004122/**
4123 * cgroup_populate_dir - selectively creation of files in a directory
4124 * @cgrp: target cgroup
4125 * @base_files: true if the base files should be added
4126 * @subsys_mask: mask of the subsystem ids whose files should be added
4127 */
4128static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4129 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004130{
4131 int err;
4132 struct cgroup_subsys *ss;
4133
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004134 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004135 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004136 if (err < 0)
4137 return err;
4138 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004139
Tejun Heo8e3f6542012-04-01 12:09:55 -07004140 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004141 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004142 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004143 if (!test_bit(ss->subsys_id, &subsys_mask))
4144 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004145
Tejun Heodb0416b2012-04-01 12:09:55 -07004146 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004147 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004148 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004149
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004150 /* This cgroup is ready now */
4151 for_each_subsys(cgrp->root, ss) {
4152 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4153 /*
4154 * Update id->css pointer and make this css visible from
4155 * CSS ID functions. This pointer will be dereferened
4156 * from RCU-read-side without locks.
4157 */
4158 if (css->id)
4159 rcu_assign_pointer(css->id->css, css);
4160 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161
4162 return 0;
4163}
4164
Tejun Heo48ddbe12012-04-01 12:09:56 -07004165static void css_dput_fn(struct work_struct *work)
4166{
4167 struct cgroup_subsys_state *css =
4168 container_of(work, struct cgroup_subsys_state, dput_work);
4169
Li Zefan1c8158e2013-06-18 18:41:10 +08004170 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004171}
4172
Tejun Heod3daf282013-06-13 19:39:16 -07004173static void css_release(struct percpu_ref *ref)
4174{
4175 struct cgroup_subsys_state *css =
4176 container_of(ref, struct cgroup_subsys_state, refcnt);
4177
4178 schedule_work(&css->dput_work);
4179}
4180
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181static void init_cgroup_css(struct cgroup_subsys_state *css,
4182 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004183 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184{
Paul Menagebd89aab2007-10-18 23:40:44 -07004185 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004186 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004187 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004188 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004189 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004190 BUG_ON(cgrp->subsys[ss->subsys_id]);
4191 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004192
4193 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004194 * css holds an extra ref to @cgrp->dentry which is put on the last
4195 * css_put(). dput() requires process context, which css_put() may
4196 * be called without. @css->dput_work will be used to invoke
4197 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004198 */
4199 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200}
4201
Tejun Heob1929db2012-11-19 08:13:38 -08004202/* invoke ->post_create() on a new CSS and mark it online if successful */
4203static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004204{
Tejun Heob1929db2012-11-19 08:13:38 -08004205 int ret = 0;
4206
Tejun Heoa31f2d32012-11-19 08:13:37 -08004207 lockdep_assert_held(&cgroup_mutex);
4208
Tejun Heo92fb9742012-11-19 08:13:38 -08004209 if (ss->css_online)
4210 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004211 if (!ret)
4212 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4213 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004214}
4215
4216/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4217static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4218 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4219{
4220 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4221
4222 lockdep_assert_held(&cgroup_mutex);
4223
4224 if (!(css->flags & CSS_ONLINE))
4225 return;
4226
Li Zefand7eeac12013-03-12 15:35:59 -07004227 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004228 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004229
4230 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4231}
4232
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233/*
Li Zefana043e3b2008-02-23 15:24:09 -08004234 * cgroup_create - create a cgroup
4235 * @parent: cgroup that will be parent of the new cgroup
4236 * @dentry: dentry of the new cgroup
4237 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238 *
Li Zefana043e3b2008-02-23 15:24:09 -08004239 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004242 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243{
Paul Menagebd89aab2007-10-18 23:40:44 -07004244 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004245 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246 struct cgroupfs_root *root = parent->root;
4247 int err = 0;
4248 struct cgroup_subsys *ss;
4249 struct super_block *sb = root->sb;
4250
Tejun Heo0a950f62012-11-19 09:02:12 -08004251 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004252 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4253 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004254 return -ENOMEM;
4255
Li Zefan65dff752013-03-01 15:01:56 +08004256 name = cgroup_alloc_name(dentry);
4257 if (!name)
4258 goto err_free_cgrp;
4259 rcu_assign_pointer(cgrp->name, name);
4260
Tejun Heo0a950f62012-11-19 09:02:12 -08004261 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4262 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004263 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004264
Tejun Heo976c06b2012-11-05 09:16:59 -08004265 /*
4266 * Only live parents can have children. Note that the liveliness
4267 * check isn't strictly necessary because cgroup_mkdir() and
4268 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4269 * anyway so that locking is contained inside cgroup proper and we
4270 * don't get nasty surprises if we ever grow another caller.
4271 */
4272 if (!cgroup_lock_live_group(parent)) {
4273 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004274 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004275 }
4276
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277 /* Grab a reference on the superblock so the hierarchy doesn't
4278 * get deleted on unmount if there are child cgroups. This
4279 * can be done outside cgroup_mutex, since the sb can't
4280 * disappear while someone has an open control file on the
4281 * fs */
4282 atomic_inc(&sb->s_active);
4283
Paul Menagecc31edc2008-10-18 20:28:04 -07004284 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285
Li Zefanfe1c06c2013-01-24 14:30:22 +08004286 dentry->d_fsdata = cgrp;
4287 cgrp->dentry = dentry;
4288
Paul Menagebd89aab2007-10-18 23:40:44 -07004289 cgrp->parent = parent;
4290 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291
Li Zefanb6abdb02008-03-04 14:28:19 -08004292 if (notify_on_release(parent))
4293 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4294
Tejun Heo2260e7f2012-11-19 08:13:38 -08004295 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4296 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004297
Paul Menageddbcc7e2007-10-18 23:39:30 -07004298 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004299 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004300
Tejun Heo92fb9742012-11-19 08:13:38 -08004301 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302 if (IS_ERR(css)) {
4303 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004304 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305 }
Tejun Heod3daf282013-06-13 19:39:16 -07004306
4307 err = percpu_ref_init(&css->refcnt, css_release);
4308 if (err)
4309 goto err_free_all;
4310
Paul Menagebd89aab2007-10-18 23:40:44 -07004311 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004312
Li Zefan4528fd02010-02-02 13:44:10 -08004313 if (ss->use_id) {
4314 err = alloc_css_id(ss, parent, cgrp);
4315 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004316 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004317 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004318 }
4319
Tejun Heo4e139af2012-11-19 08:13:36 -08004320 /*
4321 * Create directory. cgroup_create_file() returns with the new
4322 * directory locked on success so that it can be populated without
4323 * dropping cgroup_mutex.
4324 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004325 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004327 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004328 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
Li Zefan794611a2013-06-18 18:53:53 +08004330 cgrp->serial_nr = atomic64_inc_return(&cgroup_serial_nr_cursor);
Tejun Heo53fa5262013-05-24 10:55:38 +09004331
Tejun Heo4e139af2012-11-19 08:13:36 -08004332 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004333 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4334 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004335
Tejun Heob1929db2012-11-19 08:13:38 -08004336 /* each css holds a ref to the cgroup's dentry */
4337 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004338 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004339
Li Zefan415cf072013-04-08 14:35:02 +08004340 /* hold a ref to the parent's dentry */
4341 dget(parent->dentry);
4342
Tejun Heob1929db2012-11-19 08:13:38 -08004343 /* creation succeeded, notify subsystems */
4344 for_each_subsys(root, ss) {
4345 err = online_css(ss, cgrp);
4346 if (err)
4347 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004348
4349 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4350 parent->parent) {
4351 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",
4352 current->comm, current->pid, ss->name);
4353 if (!strcmp(ss->name, "memory"))
4354 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4355 ss->warned_broken_hierarchy = true;
4356 }
Tejun Heoa8638032012-11-09 09:12:29 -08004357 }
4358
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004359 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004360 if (err)
4361 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004362
4363 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004364 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365
4366 return 0;
4367
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004368err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004369 for_each_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004370 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4371
4372 if (css) {
4373 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004374 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004375 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378 /* Release the reference count that we took on the superblock */
4379 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004380err_free_id:
4381 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004382err_free_name:
4383 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004384err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004385 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004387
4388err_destroy:
4389 cgroup_destroy_locked(cgrp);
4390 mutex_unlock(&cgroup_mutex);
4391 mutex_unlock(&dentry->d_inode->i_mutex);
4392 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393}
4394
Al Viro18bb1db2011-07-26 01:41:39 -04004395static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396{
4397 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4398
4399 /* the vfs holds inode->i_mutex already */
4400 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4401}
4402
Tejun Heod3daf282013-06-13 19:39:16 -07004403static void cgroup_css_killed(struct cgroup *cgrp)
4404{
4405 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4406 return;
4407
4408 /* percpu ref's of all css's are killed, kick off the next step */
4409 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4410 schedule_work(&cgrp->destroy_work);
4411}
4412
4413static void css_ref_killed_fn(struct percpu_ref *ref)
4414{
4415 struct cgroup_subsys_state *css =
4416 container_of(ref, struct cgroup_subsys_state, refcnt);
4417
4418 cgroup_css_killed(css->cgroup);
4419}
4420
4421/**
4422 * cgroup_destroy_locked - the first stage of cgroup destruction
4423 * @cgrp: cgroup to be destroyed
4424 *
4425 * css's make use of percpu refcnts whose killing latency shouldn't be
4426 * exposed to userland and are RCU protected. Also, cgroup core needs to
4427 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4428 * invoked. To satisfy all the requirements, destruction is implemented in
4429 * the following two steps.
4430 *
4431 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4432 * userland visible parts and start killing the percpu refcnts of
4433 * css's. Set up so that the next stage will be kicked off once all
4434 * the percpu refcnts are confirmed to be killed.
4435 *
4436 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4437 * rest of destruction. Once all cgroup references are gone, the
4438 * cgroup is RCU-freed.
4439 *
4440 * This function implements s1. After this step, @cgrp is gone as far as
4441 * the userland is concerned and a new cgroup with the same name may be
4442 * created. As cgroup doesn't care about the names internally, this
4443 * doesn't cause any problem.
4444 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004445static int cgroup_destroy_locked(struct cgroup *cgrp)
4446 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004447{
Tejun Heo42809dd2012-11-19 08:13:37 -08004448 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004449 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004450 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004451 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452
Tejun Heo42809dd2012-11-19 08:13:37 -08004453 lockdep_assert_held(&d->d_inode->i_mutex);
4454 lockdep_assert_held(&cgroup_mutex);
4455
Tejun Heoddd69142013-06-12 21:04:54 -07004456 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004457 * css_set_lock synchronizes access to ->cset_links and prevents
4458 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004459 */
4460 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004461 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004462 read_unlock(&css_set_lock);
4463 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004464 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004465
Tejun Heo1a90dd52012-11-05 09:16:59 -08004466 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004467 * Block new css_tryget() by killing css refcnts. cgroup core
4468 * guarantees that, by the time ->css_offline() is invoked, no new
4469 * css reference will be given out via css_tryget(). We can't
4470 * simply call percpu_ref_kill() and proceed to offlining css's
4471 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4472 * as killed on all CPUs on return.
4473 *
4474 * Use percpu_ref_kill_and_confirm() to get notifications as each
4475 * css is confirmed to be seen as killed on all CPUs. The
4476 * notification callback keeps track of the number of css's to be
4477 * killed and schedules cgroup_offline_fn() to perform the rest of
4478 * destruction once the percpu refs of all css's are confirmed to
4479 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004480 */
Tejun Heod3daf282013-06-13 19:39:16 -07004481 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heoed9577932012-11-05 09:16:58 -08004482 for_each_subsys(cgrp->root, ss) {
4483 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4484
Tejun Heod3daf282013-06-13 19:39:16 -07004485 /*
4486 * Killing would put the base ref, but we need to keep it
4487 * alive until after ->css_offline.
4488 */
4489 percpu_ref_get(&css->refcnt);
4490
4491 atomic_inc(&cgrp->css_kill_cnt);
4492 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004493 }
Tejun Heod3daf282013-06-13 19:39:16 -07004494 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004495
4496 /*
4497 * Mark @cgrp dead. This prevents further task migration and child
4498 * creation by disabling cgroup_lock_live_group(). Note that
4499 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4500 * resume iteration after dropping RCU read lock. See
4501 * cgroup_next_sibling() for details.
4502 */
Tejun Heo54766d42013-06-12 21:04:53 -07004503 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004504
Tejun Heo455050d2013-06-13 19:27:41 -07004505 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4506 raw_spin_lock(&release_list_lock);
4507 if (!list_empty(&cgrp->release_list))
4508 list_del_init(&cgrp->release_list);
4509 raw_spin_unlock(&release_list_lock);
4510
4511 /*
4512 * Remove @cgrp directory. The removal puts the base ref but we
4513 * aren't quite done with @cgrp yet, so hold onto it.
4514 */
4515 dget(d);
4516 cgroup_d_remove_dir(d);
4517
4518 /*
4519 * Unregister events and notify userspace.
4520 * Notify userspace about cgroup removing only after rmdir of cgroup
4521 * directory to avoid race between userspace and kernelspace.
4522 */
4523 spin_lock(&cgrp->event_list_lock);
4524 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4525 list_del_init(&event->list);
4526 schedule_work(&event->remove);
4527 }
4528 spin_unlock(&cgrp->event_list_lock);
4529
Tejun Heoea15f8c2013-06-13 19:27:42 -07004530 return 0;
4531};
4532
Tejun Heod3daf282013-06-13 19:39:16 -07004533/**
4534 * cgroup_offline_fn - the second step of cgroup destruction
4535 * @work: cgroup->destroy_free_work
4536 *
4537 * This function is invoked from a work item for a cgroup which is being
4538 * destroyed after the percpu refcnts of all css's are guaranteed to be
4539 * seen as killed on all CPUs, and performs the rest of destruction. This
4540 * is the second step of destruction described in the comment above
4541 * cgroup_destroy_locked().
4542 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004543static void cgroup_offline_fn(struct work_struct *work)
4544{
4545 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4546 struct cgroup *parent = cgrp->parent;
4547 struct dentry *d = cgrp->dentry;
4548 struct cgroup_subsys *ss;
4549
4550 mutex_lock(&cgroup_mutex);
4551
Tejun Heod3daf282013-06-13 19:39:16 -07004552 /*
4553 * css_tryget() is guaranteed to fail now. Tell subsystems to
4554 * initate destruction.
4555 */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004556 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004557 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004558
4559 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004560 * Put the css refs from cgroup_destroy_locked(). Each css holds
4561 * an extra reference to the cgroup's dentry and cgroup removal
4562 * proceeds regardless of css refs. On the last put of each css,
4563 * whenever that may be, the extra dentry ref is put so that dentry
4564 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004565 */
Tejun Heoe9316082012-11-05 09:16:58 -08004566 for_each_subsys(cgrp->root, ss)
4567 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004568
Paul Menage999cd8a2009-01-07 18:08:36 -08004569 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004570 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004571
Paul Menageddbcc7e2007-10-18 23:39:30 -07004572 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004573
Paul Menagebd89aab2007-10-18 23:40:44 -07004574 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004575 check_for_release(parent);
4576
Tejun Heoea15f8c2013-06-13 19:27:42 -07004577 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578}
4579
Tejun Heo42809dd2012-11-19 08:13:37 -08004580static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4581{
4582 int ret;
4583
4584 mutex_lock(&cgroup_mutex);
4585 ret = cgroup_destroy_locked(dentry->d_fsdata);
4586 mutex_unlock(&cgroup_mutex);
4587
4588 return ret;
4589}
4590
Tejun Heo8e3f6542012-04-01 12:09:55 -07004591static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4592{
4593 INIT_LIST_HEAD(&ss->cftsets);
4594
4595 /*
4596 * base_cftset is embedded in subsys itself, no need to worry about
4597 * deregistration.
4598 */
4599 if (ss->base_cftypes) {
4600 ss->base_cftset.cfts = ss->base_cftypes;
4601 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4602 }
4603}
4604
Li Zefan06a11922008-04-29 01:00:07 -07004605static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004608
4609 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610
Tejun Heo648bb562012-11-19 08:13:36 -08004611 mutex_lock(&cgroup_mutex);
4612
Tejun Heo8e3f6542012-04-01 12:09:55 -07004613 /* init base cftset */
4614 cgroup_init_cftsets(ss);
4615
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004617 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004618 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004619 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004620 /* We don't handle early failures gracefully */
4621 BUG_ON(IS_ERR(css));
4622 init_cgroup_css(css, ss, dummytop);
4623
Li Zefane8d55fd2008-04-29 01:00:13 -07004624 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004625 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004626 * newly registered, all tasks and hence the
4627 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004628 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629
4630 need_forkexit_callback |= ss->fork || ss->exit;
4631
Li Zefane8d55fd2008-04-29 01:00:13 -07004632 /* At system boot, before all subsystems have been
4633 * registered, no tasks have been forked, so we don't
4634 * need to invoke fork callbacks here. */
4635 BUG_ON(!list_empty(&init_task.tasks));
4636
Tejun Heob1929db2012-11-19 08:13:38 -08004637 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004638
Tejun Heo648bb562012-11-19 08:13:36 -08004639 mutex_unlock(&cgroup_mutex);
4640
Ben Blume6a11052010-03-10 15:22:09 -08004641 /* this function shouldn't be used with modular subsystems, since they
4642 * need to register a subsys_id, among other things */
4643 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004644}
4645
4646/**
Ben Blume6a11052010-03-10 15:22:09 -08004647 * cgroup_load_subsys: load and register a modular subsystem at runtime
4648 * @ss: the subsystem to load
4649 *
4650 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004651 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004652 * up for use. If the subsystem is built-in anyway, work is delegated to the
4653 * simpler cgroup_init_subsys.
4654 */
4655int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4656{
Ben Blume6a11052010-03-10 15:22:09 -08004657 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004658 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004659 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004660 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004661 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004662
4663 /* check name and function validity */
4664 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004665 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004666 return -EINVAL;
4667
4668 /*
4669 * we don't support callbacks in modular subsystems. this check is
4670 * before the ss->module check for consistency; a subsystem that could
4671 * be a module should still have no callbacks even if the user isn't
4672 * compiling it as one.
4673 */
4674 if (ss->fork || ss->exit)
4675 return -EINVAL;
4676
4677 /*
4678 * an optionally modular subsystem is built-in: we want to do nothing,
4679 * since cgroup_init_subsys will have already taken care of it.
4680 */
4681 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004682 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004683 BUG_ON(subsys[ss->subsys_id] != ss);
4684 return 0;
4685 }
4686
Tejun Heo8e3f6542012-04-01 12:09:55 -07004687 /* init base cftset */
4688 cgroup_init_cftsets(ss);
4689
Ben Blume6a11052010-03-10 15:22:09 -08004690 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004691 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004692
4693 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004694 * no ss->css_alloc seems to need anything important in the ss
4695 * struct, so this can happen first (i.e. before the rootnode
4696 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004697 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004698 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004699 if (IS_ERR(css)) {
4700 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004701 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004702 mutex_unlock(&cgroup_mutex);
4703 return PTR_ERR(css);
4704 }
4705
4706 list_add(&ss->sibling, &rootnode.subsys_list);
4707 ss->root = &rootnode;
4708
4709 /* our new subsystem will be attached to the dummy hierarchy. */
4710 init_cgroup_css(css, ss, dummytop);
4711 /* init_idr must be after init_cgroup_css because it sets css->id. */
4712 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004713 ret = cgroup_init_idr(ss, css);
4714 if (ret)
4715 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004716 }
4717
4718 /*
4719 * Now we need to entangle the css into the existing css_sets. unlike
4720 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4721 * will need a new pointer to it; done by iterating the css_set_table.
4722 * furthermore, modifying the existing css_sets will corrupt the hash
4723 * table state, so each changed css_set will need its hash recomputed.
4724 * this is all done under the css_set_lock.
4725 */
4726 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004727 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004728 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004729 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004730 continue;
4731 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004732 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004733 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004734 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004735 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004736 key = css_set_hash(cset->subsys);
4737 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004738 }
4739 write_unlock(&css_set_lock);
4740
Tejun Heob1929db2012-11-19 08:13:38 -08004741 ret = online_css(ss, dummytop);
4742 if (ret)
4743 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004744
Ben Blume6a11052010-03-10 15:22:09 -08004745 /* success! */
4746 mutex_unlock(&cgroup_mutex);
4747 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004748
4749err_unload:
4750 mutex_unlock(&cgroup_mutex);
4751 /* @ss can't be mounted here as try_module_get() would fail */
4752 cgroup_unload_subsys(ss);
4753 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004754}
4755EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4756
4757/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004758 * cgroup_unload_subsys: unload a modular subsystem
4759 * @ss: the subsystem to unload
4760 *
4761 * This function should be called in a modular subsystem's exitcall. When this
4762 * function is invoked, the refcount on the subsystem's module will be 0, so
4763 * the subsystem will not be attached to any hierarchy.
4764 */
4765void cgroup_unload_subsys(struct cgroup_subsys *ss)
4766{
Tejun Heo69d02062013-06-12 21:04:50 -07004767 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004768
4769 BUG_ON(ss->module == NULL);
4770
4771 /*
4772 * we shouldn't be called if the subsystem is in use, and the use of
4773 * try_module_get in parse_cgroupfs_options should ensure that it
4774 * doesn't start being used while we're killing it off.
4775 */
4776 BUG_ON(ss->root != &rootnode);
4777
4778 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004779
Tejun Heoa31f2d32012-11-19 08:13:37 -08004780 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004781
Tejun Heoc897ff62013-02-27 17:03:49 -08004782 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004783 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004784
Ben Blumcf5d5942010-03-10 15:22:09 -08004785 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004786 subsys[ss->subsys_id] = NULL;
4787
4788 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004789 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004790
4791 /*
4792 * disentangle the css from all css_sets attached to the dummytop. as
4793 * in loading, we need to pay our respects to the hashtable gods.
4794 */
4795 write_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004796 list_for_each_entry(link, &dummytop->cset_links, cset_link) {
4797 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004798 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004799
Tejun Heo5abb8852013-06-12 21:04:49 -07004800 hash_del(&cset->hlist);
4801 cset->subsys[ss->subsys_id] = NULL;
4802 key = css_set_hash(cset->subsys);
4803 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004804 }
4805 write_unlock(&css_set_lock);
4806
4807 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004808 * remove subsystem's css from the dummytop and free it - need to
4809 * free before marking as null because ss->css_free needs the
4810 * cgrp->subsys pointer to find their state. note that this also
4811 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004812 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004813 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004814 dummytop->subsys[ss->subsys_id] = NULL;
4815
4816 mutex_unlock(&cgroup_mutex);
4817}
4818EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4819
4820/**
Li Zefana043e3b2008-02-23 15:24:09 -08004821 * cgroup_init_early - cgroup initialization at system boot
4822 *
4823 * Initialize cgroups at system boot, and initialize any
4824 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004825 */
4826int __init cgroup_init_early(void)
4827{
4828 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004829 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004830 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004831 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004832 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004833 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004834 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004835 root_count = 1;
4836 init_task.cgroups = &init_css_set;
4837
Tejun Heo69d02062013-06-12 21:04:50 -07004838 init_cgrp_cset_link.cset = &init_css_set;
4839 init_cgrp_cset_link.cgrp = dummytop;
4840 list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links);
4841 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004842
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004843 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004844 struct cgroup_subsys *ss = subsys[i];
4845
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004846 /* at bootup time, we don't worry about modular subsystems */
4847 if (!ss || ss->module)
4848 continue;
4849
Paul Menageddbcc7e2007-10-18 23:39:30 -07004850 BUG_ON(!ss->name);
4851 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004852 BUG_ON(!ss->css_alloc);
4853 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004854 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004855 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004856 ss->name, ss->subsys_id);
4857 BUG();
4858 }
4859
4860 if (ss->early_init)
4861 cgroup_init_subsys(ss);
4862 }
4863 return 0;
4864}
4865
4866/**
Li Zefana043e3b2008-02-23 15:24:09 -08004867 * cgroup_init - cgroup initialization
4868 *
4869 * Register cgroup filesystem and /proc file, and initialize
4870 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004871 */
4872int __init cgroup_init(void)
4873{
4874 int err;
4875 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004876 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004877
4878 err = bdi_init(&cgroup_backing_dev_info);
4879 if (err)
4880 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004881
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004882 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004883 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004884
4885 /* at bootup time, we don't worry about modular subsystems */
4886 if (!ss || ss->module)
4887 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004888 if (!ss->early_init)
4889 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004890 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004891 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004892 }
4893
Li Zefan472b1052008-04-29 01:00:11 -07004894 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004895 key = css_set_hash(init_css_set.subsys);
4896 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004897
4898 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004899 mutex_lock(&cgroup_mutex);
4900 mutex_lock(&cgroup_root_mutex);
4901
Tejun Heofa3ca072013-04-14 11:36:56 -07004902 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004903
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004904 mutex_unlock(&cgroup_root_mutex);
4905 mutex_unlock(&cgroup_mutex);
4906
Greg KH676db4a2010-08-05 13:53:35 -07004907 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4908 if (!cgroup_kobj) {
4909 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004910 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004911 }
4912
4913 err = register_filesystem(&cgroup_fs_type);
4914 if (err < 0) {
4915 kobject_put(cgroup_kobj);
4916 goto out;
4917 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004918
Li Zefan46ae2202008-04-29 01:00:08 -07004919 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004920
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921out:
Paul Menagea4243162007-10-18 23:39:35 -07004922 if (err)
4923 bdi_destroy(&cgroup_backing_dev_info);
4924
Paul Menageddbcc7e2007-10-18 23:39:30 -07004925 return err;
4926}
Paul Menageb4f48b62007-10-18 23:39:33 -07004927
Paul Menagea4243162007-10-18 23:39:35 -07004928/*
4929 * proc_cgroup_show()
4930 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4931 * - Used for /proc/<pid>/cgroup.
4932 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4933 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004934 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004935 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4936 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4937 * cgroup to top_cgroup.
4938 */
4939
4940/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004941int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004942{
4943 struct pid *pid;
4944 struct task_struct *tsk;
4945 char *buf;
4946 int retval;
4947 struct cgroupfs_root *root;
4948
4949 retval = -ENOMEM;
4950 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4951 if (!buf)
4952 goto out;
4953
4954 retval = -ESRCH;
4955 pid = m->private;
4956 tsk = get_pid_task(pid, PIDTYPE_PID);
4957 if (!tsk)
4958 goto out_free;
4959
4960 retval = 0;
4961
4962 mutex_lock(&cgroup_mutex);
4963
Li Zefane5f6a862009-01-07 18:07:41 -08004964 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004965 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004966 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004967 int count = 0;
4968
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004969 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004970 for_each_subsys(root, ss)
4971 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004972 if (strlen(root->name))
4973 seq_printf(m, "%sname=%s", count ? "," : "",
4974 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004975 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004976 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004977 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004978 if (retval < 0)
4979 goto out_unlock;
4980 seq_puts(m, buf);
4981 seq_putc(m, '\n');
4982 }
4983
4984out_unlock:
4985 mutex_unlock(&cgroup_mutex);
4986 put_task_struct(tsk);
4987out_free:
4988 kfree(buf);
4989out:
4990 return retval;
4991}
4992
Paul Menagea4243162007-10-18 23:39:35 -07004993/* Display information about each subsystem and each hierarchy */
4994static int proc_cgroupstats_show(struct seq_file *m, void *v)
4995{
4996 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004997
Paul Menage8bab8dd2008-04-04 14:29:57 -07004998 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004999 /*
5000 * ideally we don't want subsystems moving around while we do this.
5001 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5002 * subsys/hierarchy state.
5003 */
Paul Menagea4243162007-10-18 23:39:35 -07005004 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005005 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
5006 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08005007 if (ss == NULL)
5008 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005009 seq_printf(m, "%s\t%d\t%d\t%d\n",
5010 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005011 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07005012 }
5013 mutex_unlock(&cgroup_mutex);
5014 return 0;
5015}
5016
5017static int cgroupstats_open(struct inode *inode, struct file *file)
5018{
Al Viro9dce07f2008-03-29 03:07:28 +00005019 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005020}
5021
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005022static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005023 .open = cgroupstats_open,
5024 .read = seq_read,
5025 .llseek = seq_lseek,
5026 .release = single_release,
5027};
5028
Paul Menageb4f48b62007-10-18 23:39:33 -07005029/**
5030 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005031 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005032 *
5033 * Description: A task inherits its parent's cgroup at fork().
5034 *
5035 * A pointer to the shared css_set was automatically copied in
5036 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005037 * it was not made under the protection of RCU or cgroup_mutex, so
5038 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5039 * have already changed current->cgroups, allowing the previously
5040 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005041 *
5042 * At the point that cgroup_fork() is called, 'current' is the parent
5043 * task, and the passed argument 'child' points to the child task.
5044 */
5045void cgroup_fork(struct task_struct *child)
5046{
Tejun Heo9bb71302012-10-18 17:52:07 -07005047 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005048 child->cgroups = current->cgroups;
5049 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005050 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005051 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005052}
5053
5054/**
Li Zefana043e3b2008-02-23 15:24:09 -08005055 * cgroup_post_fork - called on a new task after adding it to the task list
5056 * @child: the task in question
5057 *
Tejun Heo5edee612012-10-16 15:03:14 -07005058 * Adds the task to the list running through its css_set if necessary and
5059 * call the subsystem fork() callbacks. Has to be after the task is
5060 * visible on the task list in case we race with the first call to
5061 * cgroup_iter_start() - to guarantee that the new task ends up on its
5062 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005063 */
Paul Menage817929e2007-10-18 23:39:36 -07005064void cgroup_post_fork(struct task_struct *child)
5065{
Tejun Heo5edee612012-10-16 15:03:14 -07005066 int i;
5067
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005068 /*
5069 * use_task_css_set_links is set to 1 before we walk the tasklist
5070 * under the tasklist_lock and we read it here after we added the child
5071 * to the tasklist under the tasklist_lock as well. If the child wasn't
5072 * yet in the tasklist when we walked through it from
5073 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5074 * should be visible now due to the paired locking and barriers implied
5075 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5076 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5077 * lock on fork.
5078 */
Paul Menage817929e2007-10-18 23:39:36 -07005079 if (use_task_css_set_links) {
5080 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005081 task_lock(child);
5082 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005083 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005084 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005085 write_unlock(&css_set_lock);
5086 }
Tejun Heo5edee612012-10-16 15:03:14 -07005087
5088 /*
5089 * Call ss->fork(). This must happen after @child is linked on
5090 * css_set; otherwise, @child might change state between ->fork()
5091 * and addition to css_set.
5092 */
5093 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005094 /*
5095 * fork/exit callbacks are supported only for builtin
5096 * subsystems, and the builtin section of the subsys
5097 * array is immutable, so we don't need to lock the
5098 * subsys array here. On the other hand, modular section
5099 * of the array can be freed at module unload, so we
5100 * can't touch that.
5101 */
5102 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07005103 struct cgroup_subsys *ss = subsys[i];
5104
Tejun Heo5edee612012-10-16 15:03:14 -07005105 if (ss->fork)
5106 ss->fork(child);
5107 }
5108 }
Paul Menage817929e2007-10-18 23:39:36 -07005109}
Tejun Heo5edee612012-10-16 15:03:14 -07005110
Paul Menage817929e2007-10-18 23:39:36 -07005111/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005112 * cgroup_exit - detach cgroup from exiting task
5113 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005114 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005115 *
5116 * Description: Detach cgroup from @tsk and release it.
5117 *
5118 * Note that cgroups marked notify_on_release force every task in
5119 * them to take the global cgroup_mutex mutex when exiting.
5120 * This could impact scaling on very large systems. Be reluctant to
5121 * use notify_on_release cgroups where very high task exit scaling
5122 * is required on large systems.
5123 *
5124 * the_top_cgroup_hack:
5125 *
5126 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5127 *
5128 * We call cgroup_exit() while the task is still competent to
5129 * handle notify_on_release(), then leave the task attached to the
5130 * root cgroup in each hierarchy for the remainder of its exit.
5131 *
5132 * To do this properly, we would increment the reference count on
5133 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5134 * code we would add a second cgroup function call, to drop that
5135 * reference. This would just create an unnecessary hot spot on
5136 * the top_cgroup reference count, to no avail.
5137 *
5138 * Normally, holding a reference to a cgroup without bumping its
5139 * count is unsafe. The cgroup could go away, or someone could
5140 * attach us to a different cgroup, decrementing the count on
5141 * the first cgroup that we never incremented. But in this case,
5142 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005143 * which wards off any cgroup_attach_task() attempts, or task is a failed
5144 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005145 */
5146void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5147{
Tejun Heo5abb8852013-06-12 21:04:49 -07005148 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005149 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005150
5151 /*
5152 * Unlink from the css_set task list if necessary.
5153 * Optimistically check cg_list before taking
5154 * css_set_lock
5155 */
5156 if (!list_empty(&tsk->cg_list)) {
5157 write_lock(&css_set_lock);
5158 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005159 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005160 write_unlock(&css_set_lock);
5161 }
5162
Paul Menageb4f48b62007-10-18 23:39:33 -07005163 /* Reassign the task to the init_css_set. */
5164 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005165 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005166 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005167
5168 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005169 /*
5170 * fork/exit callbacks are supported only for builtin
5171 * subsystems, see cgroup_post_fork() for details.
5172 */
5173 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005174 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005175
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005176 if (ss->exit) {
5177 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005178 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005179 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef52012-01-31 13:47:36 +08005180 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005181 }
5182 }
5183 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005184 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005185
Tejun Heo5abb8852013-06-12 21:04:49 -07005186 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005187}
Paul Menage697f4162007-10-18 23:39:34 -07005188
Paul Menagebd89aab2007-10-18 23:40:44 -07005189static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005190{
Li Zefanf50daa72013-03-01 15:06:07 +08005191 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005192 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005193 /*
5194 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005195 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005196 * it now
5197 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005198 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005199
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005200 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005201 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005202 list_empty(&cgrp->release_list)) {
5203 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005204 need_schedule_work = 1;
5205 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005206 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005207 if (need_schedule_work)
5208 schedule_work(&release_agent_work);
5209 }
5210}
5211
Paul Menage81a6a5c2007-10-18 23:39:38 -07005212/*
5213 * Notify userspace when a cgroup is released, by running the
5214 * configured release agent with the name of the cgroup (path
5215 * relative to the root of cgroup file system) as the argument.
5216 *
5217 * Most likely, this user command will try to rmdir this cgroup.
5218 *
5219 * This races with the possibility that some other task will be
5220 * attached to this cgroup before it is removed, or that some other
5221 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5222 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5223 * unused, and this cgroup will be reprieved from its death sentence,
5224 * to continue to serve a useful existence. Next time it's released,
5225 * we will get notified again, if it still has 'notify_on_release' set.
5226 *
5227 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5228 * means only wait until the task is successfully execve()'d. The
5229 * separate release agent task is forked by call_usermodehelper(),
5230 * then control in this thread returns here, without waiting for the
5231 * release agent task. We don't bother to wait because the caller of
5232 * this routine has no use for the exit status of the release agent
5233 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005234 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005235static void cgroup_release_agent(struct work_struct *work)
5236{
5237 BUG_ON(work != &release_agent_work);
5238 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005239 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005240 while (!list_empty(&release_list)) {
5241 char *argv[3], *envp[3];
5242 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07005243 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005244 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005245 struct cgroup,
5246 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005247 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005248 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005249 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005250 if (!pathbuf)
5251 goto continue_free;
5252 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5253 goto continue_free;
5254 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5255 if (!agentbuf)
5256 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005257
5258 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005259 argv[i++] = agentbuf;
5260 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005261 argv[i] = NULL;
5262
5263 i = 0;
5264 /* minimal command environment */
5265 envp[i++] = "HOME=/";
5266 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5267 envp[i] = NULL;
5268
5269 /* Drop the lock while we invoke the usermode helper,
5270 * since the exec could involve hitting disk and hence
5271 * be a slow process */
5272 mutex_unlock(&cgroup_mutex);
5273 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005274 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005275 continue_free:
5276 kfree(pathbuf);
5277 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005278 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005279 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005280 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005281 mutex_unlock(&cgroup_mutex);
5282}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005283
5284static int __init cgroup_disable(char *str)
5285{
5286 int i;
5287 char *token;
5288
5289 while ((token = strsep(&str, ",")) != NULL) {
5290 if (!*token)
5291 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005292 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005293 struct cgroup_subsys *ss = subsys[i];
5294
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005295 /*
5296 * cgroup_disable, being at boot time, can't
5297 * know about module subsystems, so we don't
5298 * worry about them.
5299 */
5300 if (!ss || ss->module)
5301 continue;
5302
Paul Menage8bab8dd2008-04-04 14:29:57 -07005303 if (!strcmp(token, ss->name)) {
5304 ss->disabled = 1;
5305 printk(KERN_INFO "Disabling %s control group"
5306 " subsystem\n", ss->name);
5307 break;
5308 }
5309 }
5310 }
5311 return 1;
5312}
5313__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314
5315/*
5316 * Functons for CSS ID.
5317 */
5318
Tejun Heo54766d42013-06-12 21:04:53 -07005319/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005320unsigned short css_id(struct cgroup_subsys_state *css)
5321{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005322 struct css_id *cssid;
5323
5324 /*
5325 * This css_id() can return correct value when somone has refcnt
5326 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5327 * it's unchanged until freed.
5328 */
Tejun Heod3daf282013-06-13 19:39:16 -07005329 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
5331 if (cssid)
5332 return cssid->id;
5333 return 0;
5334}
Ben Blum67523c42010-03-10 15:22:11 -08005335EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005336
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005337/**
5338 * css_is_ancestor - test "root" css is an ancestor of "child"
5339 * @child: the css to be tested.
5340 * @root: the css supporsed to be an ancestor of the child.
5341 *
5342 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005343 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005344 * But, considering usual usage, the csses should be valid objects after test.
5345 * Assuming that the caller will do some action to the child if this returns
5346 * returns true, the caller must take "child";s reference count.
5347 * If "child" is valid object and this returns true, "root" is valid, too.
5348 */
5349
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005350bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005351 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005353 struct css_id *child_id;
5354 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005355
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005356 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005357 if (!child_id)
5358 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005359 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005360 if (!root_id)
5361 return false;
5362 if (child_id->depth < root_id->depth)
5363 return false;
5364 if (child_id->stack[root_id->depth] != root_id->id)
5365 return false;
5366 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005367}
5368
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005369void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5370{
5371 struct css_id *id = css->id;
5372 /* When this is called before css_id initialization, id can be NULL */
5373 if (!id)
5374 return;
5375
5376 BUG_ON(!ss->use_id);
5377
5378 rcu_assign_pointer(id->css, NULL);
5379 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005380 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005381 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005382 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005383 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005384}
Ben Blum67523c42010-03-10 15:22:11 -08005385EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005386
5387/*
5388 * This is called by init or create(). Then, calls to this function are
5389 * always serialized (By cgroup_mutex() at create()).
5390 */
5391
5392static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5393{
5394 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005395 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005396
5397 BUG_ON(!ss->use_id);
5398
5399 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5400 newid = kzalloc(size, GFP_KERNEL);
5401 if (!newid)
5402 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005403
5404 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005405 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005406 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005407 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005408 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005409 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410
5411 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005412 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005414
Tejun Heod228d9e2013-02-27 17:04:54 -08005415 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005416 newid->depth = depth;
5417 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418err_out:
5419 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005420 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421
5422}
5423
Ben Blume6a11052010-03-10 15:22:09 -08005424static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5425 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426{
5427 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005428
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005429 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005430 idr_init(&ss->idr);
5431
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005432 newid = get_new_cssid(ss, 0);
5433 if (IS_ERR(newid))
5434 return PTR_ERR(newid);
5435
5436 newid->stack[0] = newid->id;
5437 newid->css = rootcss;
5438 rootcss->id = newid;
5439 return 0;
5440}
5441
5442static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5443 struct cgroup *child)
5444{
5445 int subsys_id, i, depth = 0;
5446 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005447 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005448
5449 subsys_id = ss->subsys_id;
5450 parent_css = parent->subsys[subsys_id];
5451 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005452 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005453 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005454
5455 child_id = get_new_cssid(ss, depth);
5456 if (IS_ERR(child_id))
5457 return PTR_ERR(child_id);
5458
5459 for (i = 0; i < depth; i++)
5460 child_id->stack[i] = parent_id->stack[i];
5461 child_id->stack[depth] = child_id->id;
5462 /*
5463 * child_id->css pointer will be set after this cgroup is available
5464 * see cgroup_populate_dir()
5465 */
5466 rcu_assign_pointer(child_css->id, child_id);
5467
5468 return 0;
5469}
5470
5471/**
5472 * css_lookup - lookup css by id
5473 * @ss: cgroup subsys to be looked into.
5474 * @id: the id
5475 *
5476 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5477 * NULL if not. Should be called under rcu_read_lock()
5478 */
5479struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5480{
5481 struct css_id *cssid = NULL;
5482
5483 BUG_ON(!ss->use_id);
5484 cssid = idr_find(&ss->idr, id);
5485
5486 if (unlikely(!cssid))
5487 return NULL;
5488
5489 return rcu_dereference(cssid->css);
5490}
Ben Blum67523c42010-03-10 15:22:11 -08005491EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005492
Stephane Eraniane5d13672011-02-14 11:20:01 +02005493/*
5494 * get corresponding css from file open on cgroupfs directory
5495 */
5496struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5497{
5498 struct cgroup *cgrp;
5499 struct inode *inode;
5500 struct cgroup_subsys_state *css;
5501
Al Viro496ad9a2013-01-23 17:07:38 -05005502 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005503 /* check in cgroup filesystem dir */
5504 if (inode->i_op != &cgroup_dir_inode_operations)
5505 return ERR_PTR(-EBADF);
5506
5507 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5508 return ERR_PTR(-EINVAL);
5509
5510 /* get cgroup */
5511 cgrp = __d_cgrp(f->f_dentry);
5512 css = cgrp->subsys[id];
5513 return css ? css : ERR_PTR(-ENOENT);
5514}
5515
Paul Menagefe693432009-09-23 15:56:20 -07005516#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005517static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005518{
5519 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5520
5521 if (!css)
5522 return ERR_PTR(-ENOMEM);
5523
5524 return css;
5525}
5526
Tejun Heo92fb9742012-11-19 08:13:38 -08005527static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005528{
5529 kfree(cont->subsys[debug_subsys_id]);
5530}
5531
Paul Menagefe693432009-09-23 15:56:20 -07005532static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5533{
5534 return cgroup_task_count(cont);
5535}
5536
5537static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5538{
5539 return (u64)(unsigned long)current->cgroups;
5540}
5541
5542static u64 current_css_set_refcount_read(struct cgroup *cont,
5543 struct cftype *cft)
5544{
5545 u64 count;
5546
5547 rcu_read_lock();
5548 count = atomic_read(&current->cgroups->refcount);
5549 rcu_read_unlock();
5550 return count;
5551}
5552
Paul Menage7717f7b2009-09-23 15:56:22 -07005553static int current_css_set_cg_links_read(struct cgroup *cont,
5554 struct cftype *cft,
5555 struct seq_file *seq)
5556{
Tejun Heo69d02062013-06-12 21:04:50 -07005557 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005558 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005559
5560 read_lock(&css_set_lock);
5561 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005562 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005563 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005564 struct cgroup *c = link->cgrp;
5565 const char *name;
5566
5567 if (c->dentry)
5568 name = c->dentry->d_name.name;
5569 else
5570 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005571 seq_printf(seq, "Root %d group %s\n",
5572 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005573 }
5574 rcu_read_unlock();
5575 read_unlock(&css_set_lock);
5576 return 0;
5577}
5578
5579#define MAX_TASKS_SHOWN_PER_CSS 25
5580static int cgroup_css_links_read(struct cgroup *cont,
5581 struct cftype *cft,
5582 struct seq_file *seq)
5583{
Tejun Heo69d02062013-06-12 21:04:50 -07005584 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005585
5586 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07005587 list_for_each_entry(link, &cont->cset_links, cset_link) {
5588 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005589 struct task_struct *task;
5590 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005591 seq_printf(seq, "css_set %p\n", cset);
5592 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005593 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5594 seq_puts(seq, " ...\n");
5595 break;
5596 } else {
5597 seq_printf(seq, " task %d\n",
5598 task_pid_vnr(task));
5599 }
5600 }
5601 }
5602 read_unlock(&css_set_lock);
5603 return 0;
5604}
5605
Paul Menagefe693432009-09-23 15:56:20 -07005606static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5607{
5608 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5609}
5610
5611static struct cftype debug_files[] = {
5612 {
Paul Menagefe693432009-09-23 15:56:20 -07005613 .name = "taskcount",
5614 .read_u64 = debug_taskcount_read,
5615 },
5616
5617 {
5618 .name = "current_css_set",
5619 .read_u64 = current_css_set_read,
5620 },
5621
5622 {
5623 .name = "current_css_set_refcount",
5624 .read_u64 = current_css_set_refcount_read,
5625 },
5626
5627 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005628 .name = "current_css_set_cg_links",
5629 .read_seq_string = current_css_set_cg_links_read,
5630 },
5631
5632 {
5633 .name = "cgroup_css_links",
5634 .read_seq_string = cgroup_css_links_read,
5635 },
5636
5637 {
Paul Menagefe693432009-09-23 15:56:20 -07005638 .name = "releasable",
5639 .read_u64 = releasable_read,
5640 },
Paul Menagefe693432009-09-23 15:56:20 -07005641
Tejun Heo4baf6e32012-04-01 12:09:55 -07005642 { } /* terminate */
5643};
Paul Menagefe693432009-09-23 15:56:20 -07005644
5645struct cgroup_subsys debug_subsys = {
5646 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005647 .css_alloc = debug_css_alloc,
5648 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005649 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005650 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005651};
5652#endif /* CONFIG_CGROUP_DEBUG */