blob: 5a1ddecc3cfa49f4152ba21478bf792f07edc8d3 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heo28b4c272012-04-01 12:09:56 -070066/* css deactivation bias, makes css->refcnt negative to deny new trygets */
67#define CSS_DEACT_BIAS INT_MIN
68
Tejun Heoe25e2cb2011-12-12 18:12:21 -080069/*
70 * cgroup_mutex is the master lock. Any modification to cgroup or its
71 * hierarchy must be performed while holding it.
72 *
73 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
74 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
75 * release_agent_path and so on. Modifying requires both cgroup_mutex and
76 * cgroup_root_mutex. Readers can acquire either of the two. This is to
77 * break the following locking order cycle.
78 *
79 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
80 * B. namespace_sem -> cgroup_mutex
81 *
82 * B happens only through cgroup_show_options() and using cgroup_root_mutex
83 * breaks it.
84 */
Tejun Heo22194492013-04-07 09:29:51 -070085#ifdef CONFIG_PROVE_RCU
86DEFINE_MUTEX(cgroup_mutex);
87EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
88#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070089static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070090#endif
91
Tejun Heoe25e2cb2011-12-12 18:12:21 -080092static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070093
Ben Blumaae8aab2010-03-10 15:22:07 -080094/*
95 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020096 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080097 * registered after that. The mutable section of this array is protected by
98 * cgroup_mutex.
99 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200100#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200101#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Ben Blumaae8aab2010-03-10 15:22:07 -0800102static struct cgroup_subsys *subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103#include <linux/cgroup_subsys.h>
104};
105
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106/*
107 * The "rootnode" hierarchy is the "dummy hierarchy", reserved for the
108 * subsystems that are otherwise unattached - it never has more than a
109 * single cgroup, and all tasks are part of that cgroup.
110 */
111static struct cgroupfs_root rootnode;
112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
189static LIST_HEAD(roots);
Paul Menage817929e2007-10-18 23:39:36 -0700190static int root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Paul Menageddbcc7e2007-10-18 23:39:30 -0700199/* dummytop is a shorthand for the dummy hierarchy's top cgroup */
200#define dummytop (&rootnode.top_cgroup)
201
Li Zefan65dff752013-03-01 15:01:56 +0800202static struct cgroup_name root_cgroup_name = { .name = "/" };
203
Paul Menageddbcc7e2007-10-18 23:39:30 -0700204/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800205 * check for fork/exit handlers to call. This avoids us having to do
206 * extra work in the fork/exit path if none of the subsystems need to
207 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700209static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210
Tejun Heo42809dd2012-11-19 08:13:37 -0800211static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800212static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
213 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800214
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700215static int css_unbias_refcnt(int refcnt)
216{
217 return refcnt >= 0 ? refcnt : refcnt - CSS_DEACT_BIAS;
218}
219
Tejun Heo28b4c272012-04-01 12:09:56 -0700220/* the current nr of refs, always >= 0 whether @css is deactivated or not */
221static int css_refcnt(struct cgroup_subsys_state *css)
222{
223 int v = atomic_read(&css->refcnt);
224
Salman Qazi8e3bbf42012-06-14 14:55:30 -0700225 return css_unbias_refcnt(v);
Tejun Heo28b4c272012-04-01 12:09:56 -0700226}
227
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700229static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230{
Tejun Heo54766d42013-06-12 21:04:53 -0700231 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700232}
233
Li Zefan78574cf2013-04-08 19:00:38 -0700234/**
235 * cgroup_is_descendant - test ancestry
236 * @cgrp: the cgroup to be tested
237 * @ancestor: possible ancestor of @cgrp
238 *
239 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
240 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
241 * and @ancestor are accessible.
242 */
243bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
244{
245 while (cgrp) {
246 if (cgrp == ancestor)
247 return true;
248 cgrp = cgrp->parent;
249 }
250 return false;
251}
252EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700253
Adrian Bunke9685a02008-02-07 00:13:46 -0800254static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255{
256 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700257 (1 << CGRP_RELEASABLE) |
258 (1 << CGRP_NOTIFY_ON_RELEASE);
259 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260}
261
Adrian Bunke9685a02008-02-07 00:13:46 -0800262static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700263{
Paul Menagebd89aab2007-10-18 23:40:44 -0700264 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700265}
266
Paul Menageddbcc7e2007-10-18 23:39:30 -0700267/*
268 * for_each_subsys() allows you to iterate on each subsystem attached to
269 * an active hierarchy
270 */
271#define for_each_subsys(_root, _ss) \
272list_for_each_entry(_ss, &_root->subsys_list, sibling)
273
Li Zefane5f6a862009-01-07 18:07:41 -0800274/* for_each_active_root() allows you to iterate across the active hierarchies */
275#define for_each_active_root(_root) \
Paul Menageddbcc7e2007-10-18 23:39:30 -0700276list_for_each_entry(_root, &roots, root_list)
277
Tejun Heof6ea9372012-04-01 12:09:55 -0700278static inline struct cgroup *__d_cgrp(struct dentry *dentry)
279{
280 return dentry->d_fsdata;
281}
282
Tejun Heo05ef1d72012-04-01 12:09:56 -0700283static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700284{
285 return dentry->d_fsdata;
286}
287
Tejun Heo05ef1d72012-04-01 12:09:56 -0700288static inline struct cftype *__d_cft(struct dentry *dentry)
289{
290 return __d_cfe(dentry)->type;
291}
292
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293/**
294 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
295 * @cgrp: the cgroup to be checked for liveness
296 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700297 * On success, returns true; the mutex should be later unlocked. On
298 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700299 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700300static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700301{
302 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700303 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700304 mutex_unlock(&cgroup_mutex);
305 return false;
306 }
307 return true;
308}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700309
Paul Menage81a6a5c2007-10-18 23:39:38 -0700310/* the list of cgroups eligible for automatic release. Protected by
311 * release_list_lock */
312static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200313static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700314static void cgroup_release_agent(struct work_struct *work);
315static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700316static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700317
Tejun Heo69d02062013-06-12 21:04:50 -0700318/*
319 * A cgroup can be associated with multiple css_sets as different tasks may
320 * belong to different cgroups on different hierarchies. In the other
321 * direction, a css_set is naturally associated with multiple cgroups.
322 * This M:N relationship is represented by the following link structure
323 * which exists for each association and allows traversing the associations
324 * from both sides.
325 */
326struct cgrp_cset_link {
327 /* the cgroup and css_set this link associates */
328 struct cgroup *cgrp;
329 struct css_set *cset;
330
331 /* list of cgrp_cset_links anchored at cgrp->cset_links */
332 struct list_head cset_link;
333
334 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
335 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700336};
337
338/* The default css_set - used by init and its children prior to any
339 * hierarchies being mounted. It contains a pointer to the root state
340 * for each subsystem. Also used to anchor the list of css_sets. Not
341 * reference-counted, to improve performance when child cgroups
342 * haven't been created.
343 */
344
345static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700346static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700347
Ben Blume6a11052010-03-10 15:22:09 -0800348static int cgroup_init_idr(struct cgroup_subsys *ss,
349 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700350
Paul Menage817929e2007-10-18 23:39:36 -0700351/* css_set_lock protects the list of css_set objects, and the
352 * chain of tasks off each css_set. Nests outside task->alloc_lock
353 * due to cgroup_iter_start() */
354static DEFINE_RWLOCK(css_set_lock);
355static int css_set_count;
356
Paul Menage7717f7b2009-09-23 15:56:22 -0700357/*
358 * hash table for cgroup groups. This improves the performance to find
359 * an existing css_set. This hash doesn't (currently) take into
360 * account cgroups in empty hierarchies.
361 */
Li Zefan472b1052008-04-29 01:00:11 -0700362#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800363static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700364
Li Zefan0ac801f2013-01-10 11:49:27 +0800365static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700366{
367 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800368 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700369
370 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800371 key += (unsigned long)css[i];
372 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700373
Li Zefan0ac801f2013-01-10 11:49:27 +0800374 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700375}
376
Paul Menage817929e2007-10-18 23:39:36 -0700377/* We don't maintain the lists running through each css_set to its
378 * task until after the first call to cgroup_iter_start(). This
379 * reduces the fork()/exit() overhead for people who have cgroups
380 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700381static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700382
Tejun Heo5abb8852013-06-12 21:04:49 -0700383static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700384{
Tejun Heo69d02062013-06-12 21:04:50 -0700385 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700386
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700387 /*
388 * Ensure that the refcount doesn't hit zero while any readers
389 * can see it. Similar to atomic_dec_and_lock(), but for an
390 * rwlock
391 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700392 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700393 return;
394 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700395 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700396 write_unlock(&css_set_lock);
397 return;
398 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700399
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700400 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700401 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700402 css_set_count--;
403
Tejun Heo69d02062013-06-12 21:04:50 -0700404 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700405 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Tejun Heo69d02062013-06-12 21:04:50 -0700407 list_del(&link->cset_link);
408 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800409
Tejun Heoddd69142013-06-12 21:04:54 -0700410 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d8282013-06-12 21:04:55 -0700411 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700412 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700413 set_bit(CGRP_RELEASABLE, &cgrp->flags);
414 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416
417 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419
420 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700422}
423
424/*
425 * refcounted get/put for css_set objects
426 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700427static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700428{
Tejun Heo5abb8852013-06-12 21:04:49 -0700429 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700430}
431
Tejun Heo5abb8852013-06-12 21:04:49 -0700432static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700433{
Tejun Heo5abb8852013-06-12 21:04:49 -0700434 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700435}
436
Tejun Heo5abb8852013-06-12 21:04:49 -0700437static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438{
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700440}
441
Paul Menage817929e2007-10-18 23:39:36 -0700442/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700443 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700444 * @cset: candidate css_set being tested
445 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 * @new_cgrp: cgroup that's being entered by the task
447 * @template: desired set of css pointers in css_set (pre-calculated)
448 *
449 * Returns true if "cg" matches "old_cg" except for the hierarchy
450 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
451 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static bool compare_css_sets(struct css_set *cset,
453 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700454 struct cgroup *new_cgrp,
455 struct cgroup_subsys_state *template[])
456{
457 struct list_head *l1, *l2;
458
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 /* Not all subsystems matched */
461 return false;
462 }
463
464 /*
465 * Compare cgroup pointers in order to distinguish between
466 * different cgroups in heirarchies with no subsystems. We
467 * could get by with just this check alone (and skip the
468 * memcmp above) but on most setups the memcmp check will
469 * avoid the need for this more expensive check on almost all
470 * candidates.
471 */
472
Tejun Heo69d02062013-06-12 21:04:50 -0700473 l1 = &cset->cgrp_links;
474 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700475 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700476 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700478
479 l1 = l1->next;
480 l2 = l2->next;
481 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700482 if (l1 == &cset->cgrp_links) {
483 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700484 break;
485 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700486 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 }
488 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700489 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
490 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
491 cgrp1 = link1->cgrp;
492 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700494 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700495
496 /*
497 * If this hierarchy is the hierarchy of the cgroup
498 * that's changing, then we need to check that this
499 * css_set points to the new cgroup; if it's any other
500 * hierarchy, then this css_set should point to the
501 * same cgroup as the old css_set.
502 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700503 if (cgrp1->root == new_cgrp->root) {
504 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 return false;
506 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700507 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700508 return false;
509 }
510 }
511 return true;
512}
513
514/*
Paul Menage817929e2007-10-18 23:39:36 -0700515 * find_existing_css_set() is a helper for
516 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700517 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700518 *
519 * oldcg: the cgroup group that we're using before the cgroup
520 * transition
521 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700522 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700523 *
524 * template: location in which to build the desired set of subsystem
525 * state objects for the new cgroup group
526 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700527static struct css_set *find_existing_css_set(struct css_set *old_cset,
528 struct cgroup *cgrp,
529 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700530{
531 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700532 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700533 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800534 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700535
Ben Blumaae8aab2010-03-10 15:22:07 -0800536 /*
537 * Build the set of subsystem state objects that we want to see in the
538 * new css_set. while subsystems can change globally, the entries here
539 * won't change, so no need for locking.
540 */
Paul Menage817929e2007-10-18 23:39:36 -0700541 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400542 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700543 /* Subsystem is in this hierarchy. So we want
544 * the subsystem state from the new
545 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700546 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700547 } else {
548 /* Subsystem is not in this hierarchy, so we
549 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700551 }
552 }
553
Li Zefan0ac801f2013-01-10 11:49:27 +0800554 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700555 hash_for_each_possible(css_set_table, cset, hlist, key) {
556 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700557 continue;
558
559 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700560 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700561 }
Paul Menage817929e2007-10-18 23:39:36 -0700562
563 /* No existing cgroup group matched */
564 return NULL;
565}
566
Tejun Heo69d02062013-06-12 21:04:50 -0700567static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700568{
Tejun Heo69d02062013-06-12 21:04:50 -0700569 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700570
Tejun Heo69d02062013-06-12 21:04:50 -0700571 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
572 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700573 kfree(link);
574 }
575}
576
Tejun Heo69d02062013-06-12 21:04:50 -0700577/**
578 * allocate_cgrp_cset_links - allocate cgrp_cset_links
579 * @count: the number of links to allocate
580 * @tmp_links: list_head the allocated links are put on
581 *
582 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
583 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700584 */
Tejun Heo69d02062013-06-12 21:04:50 -0700585static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700586{
Tejun Heo69d02062013-06-12 21:04:50 -0700587 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700588 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700589
590 INIT_LIST_HEAD(tmp_links);
591
Li Zefan36553432008-07-29 22:33:19 -0700592 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700593 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700594 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700595 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700596 return -ENOMEM;
597 }
Tejun Heo69d02062013-06-12 21:04:50 -0700598 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700599 }
600 return 0;
601}
602
Li Zefanc12f65d2009-01-07 18:07:42 -0800603/**
604 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700605 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700606 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800607 * @cgrp: the destination cgroup
608 */
Tejun Heo69d02062013-06-12 21:04:50 -0700609static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
610 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800611{
Tejun Heo69d02062013-06-12 21:04:50 -0700612 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800613
Tejun Heo69d02062013-06-12 21:04:50 -0700614 BUG_ON(list_empty(tmp_links));
615 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
616 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700617 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700618 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700619 /*
620 * Always add links to the tail of the list so that the list
621 * is sorted by order of hierarchy creation
622 */
Tejun Heo69d02062013-06-12 21:04:50 -0700623 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800624}
625
Li Zefan36553432008-07-29 22:33:19 -0700626/*
Paul Menage817929e2007-10-18 23:39:36 -0700627 * find_css_set() takes an existing cgroup group and a
628 * cgroup object, and returns a css_set object that's
629 * equivalent to the old group, but with the given cgroup
630 * substituted into the appropriate hierarchy. Must be called with
631 * cgroup_mutex held
632 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700633static struct css_set *find_css_set(struct css_set *old_cset,
634 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700635{
Tejun Heo5abb8852013-06-12 21:04:49 -0700636 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700637 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700638 struct list_head tmp_links;
639 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800640 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700641
Paul Menage817929e2007-10-18 23:39:36 -0700642 /* First see if we already have a cgroup group that matches
643 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700644 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 cset = find_existing_css_set(old_cset, cgrp, template);
646 if (cset)
647 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700648 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700649
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 if (cset)
651 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700652
Tejun Heof4f4be22013-06-12 21:04:51 -0700653 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700655 return NULL;
656
Tejun Heo69d02062013-06-12 21:04:50 -0700657 /* Allocate all the cgrp_cset_link objects that we'll need */
658 if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700659 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700660 return NULL;
661 }
662
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700664 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 INIT_LIST_HEAD(&cset->tasks);
666 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700667
668 /* Copy the set of subsystem state objects generated in
669 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700670 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700671
672 write_lock(&css_set_lock);
673 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700674 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700676
Paul Menage7717f7b2009-09-23 15:56:22 -0700677 if (c->root == cgrp->root)
678 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700679 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700680 }
Paul Menage817929e2007-10-18 23:39:36 -0700681
Tejun Heo69d02062013-06-12 21:04:50 -0700682 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700683
Paul Menage817929e2007-10-18 23:39:36 -0700684 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700685
686 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 key = css_set_hash(cset->subsys);
688 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700689
Paul Menage817929e2007-10-18 23:39:36 -0700690 write_unlock(&css_set_lock);
691
Tejun Heo5abb8852013-06-12 21:04:49 -0700692 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700693}
694
Paul Menageddbcc7e2007-10-18 23:39:30 -0700695/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700696 * Return the cgroup for "task" from the given hierarchy. Must be
697 * called with cgroup_mutex held.
698 */
699static struct cgroup *task_cgroup_from_root(struct task_struct *task,
700 struct cgroupfs_root *root)
701{
Tejun Heo5abb8852013-06-12 21:04:49 -0700702 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700703 struct cgroup *res = NULL;
704
705 BUG_ON(!mutex_is_locked(&cgroup_mutex));
706 read_lock(&css_set_lock);
707 /*
708 * No need to lock the task - since we hold cgroup_mutex the
709 * task can't change groups, so the only thing that can happen
710 * is that it exits and its css is set back to init_css_set.
711 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700712 cset = task->cgroups;
713 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 res = &root->top_cgroup;
715 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700716 struct cgrp_cset_link *link;
717
718 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700719 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700720
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 if (c->root == root) {
722 res = c;
723 break;
724 }
725 }
726 }
727 read_unlock(&css_set_lock);
728 BUG_ON(!res);
729 return res;
730}
731
732/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 * There is one global cgroup mutex. We also require taking
734 * task_lock() when dereferencing a task's cgroup subsys pointers.
735 * See "The task_lock() exception", at the end of this comment.
736 *
737 * A task must hold cgroup_mutex to modify cgroups.
738 *
739 * Any task can increment and decrement the count field without lock.
740 * So in general, code holding cgroup_mutex can't rely on the count
741 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800742 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743 * means that no tasks are currently attached, therefore there is no
744 * way a task attached to that cgroup can fork (the other way to
745 * increment the count). So code holding cgroup_mutex can safely
746 * assume that if the count is zero, it will stay zero. Similarly, if
747 * a task holds cgroup_mutex on a cgroup with zero count, it
748 * knows that the cgroup won't be removed, as cgroup_rmdir()
749 * needs that mutex.
750 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
752 * (usually) take cgroup_mutex. These are the two most performance
753 * critical pieces of code here. The exception occurs on cgroup_exit(),
754 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
755 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800756 * to the release agent with the name of the cgroup (path relative to
757 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 *
759 * A cgroup can only be deleted if both its 'count' of using tasks
760 * is zero, and its list of 'children' cgroups is empty. Since all
761 * tasks in the system use _some_ cgroup, and since there is always at
762 * least one task in the system (init, pid == 1), therefore, top_cgroup
763 * always has either children cgroups and/or using tasks. So we don't
764 * need a special hack to ensure that top_cgroup cannot be deleted.
765 *
766 * The task_lock() exception
767 *
768 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800769 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800770 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700771 * several performance critical places that need to reference
772 * task->cgroup without the expense of grabbing a system global
773 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800774 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
776 * the task_struct routinely used for such matters.
777 *
778 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800779 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780 */
781
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782/*
783 * A couple of forward declarations required, due to cyclic reference loop:
784 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
785 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
786 * -> cgroup_mkdir.
787 */
788
Al Viro18bb1db2011-07-26 01:41:39 -0400789static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400790static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400792static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
793 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700794static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700795static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700796
797static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200798 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700799 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700800};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700802static int alloc_css_id(struct cgroup_subsys *ss,
803 struct cgroup *parent, struct cgroup *child);
804
Al Viroa5e7ed32011-07-26 01:55:55 -0400805static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806{
807 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808
809 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400810 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700811 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100812 inode->i_uid = current_fsuid();
813 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
815 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
816 }
817 return inode;
818}
819
Li Zefan65dff752013-03-01 15:01:56 +0800820static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
821{
822 struct cgroup_name *name;
823
824 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
825 if (!name)
826 return NULL;
827 strcpy(name->name, dentry->d_name.name);
828 return name;
829}
830
Li Zefanbe445622013-01-24 14:31:42 +0800831static void cgroup_free_fn(struct work_struct *work)
832{
833 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
834 struct cgroup_subsys *ss;
835
836 mutex_lock(&cgroup_mutex);
837 /*
838 * Release the subsystem state objects.
839 */
840 for_each_subsys(cgrp->root, ss)
841 ss->css_free(cgrp);
842
843 cgrp->root->number_of_cgroups--;
844 mutex_unlock(&cgroup_mutex);
845
846 /*
Li Zefan415cf072013-04-08 14:35:02 +0800847 * We get a ref to the parent's dentry, and put the ref when
848 * this cgroup is being freed, so it's guaranteed that the
849 * parent won't be destroyed before its children.
850 */
851 dput(cgrp->parent->dentry);
852
Li Zefancc20e012013-04-26 11:58:02 -0700853 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
854
Li Zefan415cf072013-04-08 14:35:02 +0800855 /*
Li Zefanbe445622013-01-24 14:31:42 +0800856 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700857 * created the cgroup. This will free cgrp->root, if we are
858 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800859 */
860 deactivate_super(cgrp->root->sb);
861
862 /*
863 * if we're getting rid of the cgroup, refcount should ensure
864 * that there are no pidlists left.
865 */
866 BUG_ON(!list_empty(&cgrp->pidlists));
867
868 simple_xattrs_free(&cgrp->xattrs);
869
Li Zefan65dff752013-03-01 15:01:56 +0800870 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800871 kfree(cgrp);
872}
873
874static void cgroup_free_rcu(struct rcu_head *head)
875{
876 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
877
878 schedule_work(&cgrp->free_work);
879}
880
Paul Menageddbcc7e2007-10-18 23:39:30 -0700881static void cgroup_diput(struct dentry *dentry, struct inode *inode)
882{
883 /* is dentry a directory ? if so, kfree() associated cgroup */
884 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700885 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800886
Tejun Heo54766d42013-06-12 21:04:53 -0700887 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800888 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700889 } else {
890 struct cfent *cfe = __d_cfe(dentry);
891 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
892
893 WARN_ONCE(!list_empty(&cfe->node) &&
894 cgrp != &cgrp->root->top_cgroup,
895 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700896 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700897 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700898 }
899 iput(inode);
900}
901
Al Viroc72a04e2011-01-14 05:31:45 +0000902static int cgroup_delete(const struct dentry *d)
903{
904 return 1;
905}
906
Paul Menageddbcc7e2007-10-18 23:39:30 -0700907static void remove_dir(struct dentry *d)
908{
909 struct dentry *parent = dget(d->d_parent);
910
911 d_delete(d);
912 simple_rmdir(parent->d_inode, d);
913 dput(parent);
914}
915
Li Zefan2739d3c2013-01-21 18:18:33 +0800916static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
921 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100922
Li Zefan2739d3c2013-01-21 18:18:33 +0800923 /*
924 * If we're doing cleanup due to failure of cgroup_create(),
925 * the corresponding @cfe may not exist.
926 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 list_for_each_entry(cfe, &cgrp->files, node) {
928 struct dentry *d = cfe->dentry;
929
930 if (cft && cfe->type != cft)
931 continue;
932
933 dget(d);
934 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700935 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 list_del_init(&cfe->node);
937 dput(d);
938
Li Zefan2739d3c2013-01-21 18:18:33 +0800939 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941}
942
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400943/**
944 * cgroup_clear_directory - selective removal of base and subsystem files
945 * @dir: directory containing the files
946 * @base_files: true if the base files should be removed
947 * @subsys_mask: mask of the subsystem ids whose files should be removed
948 */
949static void cgroup_clear_directory(struct dentry *dir, bool base_files,
950 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951{
952 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400953 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700954
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400955 for_each_subsys(cgrp->root, ss) {
956 struct cftype_set *set;
957 if (!test_bit(ss->subsys_id, &subsys_mask))
958 continue;
959 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800960 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 }
962 if (base_files) {
963 while (!list_empty(&cgrp->files))
964 cgroup_rm_file(cgrp, NULL);
965 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966}
967
968/*
969 * NOTE : the dentry must have been dget()'ed
970 */
971static void cgroup_d_remove_dir(struct dentry *dentry)
972{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100973 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400974 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100975
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400976 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100978 parent = dentry->d_parent;
979 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800980 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100982 spin_unlock(&dentry->d_lock);
983 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 remove_dir(dentry);
985}
986
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700987/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800988 * Call with cgroup_mutex held. Drops reference counts on modules, including
989 * any duplicate ones that parse_cgroupfs_options took. If this function
990 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800991 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400993 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400995 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -0700996 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 int i;
998
Ben Blumaae8aab2010-03-10 15:22:07 -0800999 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001000 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001001
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001002 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1003 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 /* Check that any added subsystems are currently free */
1005 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001006 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001008 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001010 /*
1011 * Nobody should tell us to do a subsys that doesn't exist:
1012 * parse_cgroupfs_options should catch that case and refcounts
1013 * ensure that subsystems won't disappear once selected.
1014 */
1015 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 if (ss->root != &rootnode) {
1017 /* Subsystem isn't free */
1018 return -EBUSY;
1019 }
1020 }
1021
1022 /* Currently we don't handle adding/removing subsystems when
1023 * any child cgroups exist. This is theoretically supportable
1024 * but involves complex error handling, so it's being left until
1025 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001026 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 return -EBUSY;
1028
1029 /* Process each subsystem */
1030 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1031 struct cgroup_subsys *ss = subsys[i];
1032 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001033 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001035 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001036 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 BUG_ON(!dummytop->subsys[i]);
1038 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001039 cgrp->subsys[i] = dummytop->subsys[i];
1040 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001041 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001042 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001044 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001045 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001046 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001048 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001049 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1050 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001052 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001054 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001055 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001056 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001057 /* subsystem is now free - drop reference on module */
1058 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001059 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001061 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001062 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /*
1064 * a refcount was taken, but we already had one, so
1065 * drop the extra reference.
1066 */
1067 module_put(ss->module);
1068#ifdef CONFIG_MODULE_UNLOAD
1069 BUG_ON(ss->module && !module_refcount(ss->module));
1070#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 } else {
1072 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 }
1075 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001076 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077
1078 return 0;
1079}
1080
Al Viro34c80b12011-12-08 21:32:45 -05001081static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082{
Al Viro34c80b12011-12-08 21:32:45 -05001083 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 struct cgroup_subsys *ss;
1085
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001086 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 for_each_subsys(root, ss)
1088 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001089 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1090 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001091 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001093 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001094 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001095 if (strlen(root->release_agent_path))
1096 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001097 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001098 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001099 if (strlen(root->name))
1100 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001101 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 return 0;
1103}
1104
1105struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001106 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001108 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001109 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001110 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001111 /* User explicitly requested empty subsystem */
1112 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001113
1114 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001115
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116};
1117
Ben Blumaae8aab2010-03-10 15:22:07 -08001118/*
1119 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001120 * with cgroup_mutex held to protect the subsys[] array. This function takes
1121 * refcounts on subsystems to be used, unless it returns error, in which case
1122 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001123 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001124static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001126 char *token, *o = data;
1127 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001128 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001129 int i;
1130 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001131
Ben Blumaae8aab2010-03-10 15:22:07 -08001132 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1133
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134#ifdef CONFIG_CPUSETS
1135 mask = ~(1UL << cpuset_subsys_id);
1136#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137
Paul Menagec6d57f32009-09-23 15:56:19 -07001138 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
1140 while ((token = strsep(&o, ",")) != NULL) {
1141 if (!*token)
1142 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001144 /* Explicitly have no subsystems */
1145 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 continue;
1147 }
1148 if (!strcmp(token, "all")) {
1149 /* Mutually exclusive option 'all' + subsystem name */
1150 if (one_ss)
1151 return -EINVAL;
1152 all_ss = true;
1153 continue;
1154 }
Tejun Heo873fe092013-04-14 20:15:26 -07001155 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1156 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1157 continue;
1158 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001160 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 continue;
1162 }
1163 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001164 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 continue;
1166 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001167 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001168 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001169 continue;
1170 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001172 /* Specifying two release agents is forbidden */
1173 if (opts->release_agent)
1174 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001175 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001176 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001177 if (!opts->release_agent)
1178 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001182 const char *name = token + 5;
1183 /* Can't specify an empty name */
1184 if (!strlen(name))
1185 return -EINVAL;
1186 /* Must match [\w.-]+ */
1187 for (i = 0; i < strlen(name); i++) {
1188 char c = name[i];
1189 if (isalnum(c))
1190 continue;
1191 if ((c == '.') || (c == '-') || (c == '_'))
1192 continue;
1193 return -EINVAL;
1194 }
1195 /* Specifying two names is forbidden */
1196 if (opts->name)
1197 return -EINVAL;
1198 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001199 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 GFP_KERNEL);
1201 if (!opts->name)
1202 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001203
1204 continue;
1205 }
1206
1207 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1208 struct cgroup_subsys *ss = subsys[i];
1209 if (ss == NULL)
1210 continue;
1211 if (strcmp(token, ss->name))
1212 continue;
1213 if (ss->disabled)
1214 continue;
1215
1216 /* Mutually exclusive option 'all' + subsystem name */
1217 if (all_ss)
1218 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001219 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220 one_ss = true;
1221
1222 break;
1223 }
1224 if (i == CGROUP_SUBSYS_COUNT)
1225 return -ENOENT;
1226 }
1227
1228 /*
1229 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001230 * otherwise if 'none', 'name=' and a subsystem name options
1231 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001233 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1235 struct cgroup_subsys *ss = subsys[i];
1236 if (ss == NULL)
1237 continue;
1238 if (ss->disabled)
1239 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001240 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001241 }
1242 }
1243
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001244 /* Consistency checks */
1245
Tejun Heo873fe092013-04-14 20:15:26 -07001246 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1247 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1248
1249 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1250 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1251 return -EINVAL;
1252 }
1253
1254 if (opts->cpuset_clone_children) {
1255 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1256 return -EINVAL;
1257 }
1258 }
1259
Li Zefanf9ab5b52009-06-17 16:26:33 -07001260 /*
1261 * Option noprefix was introduced just for backward compatibility
1262 * with the old cpuset, so we allow noprefix only if mounting just
1263 * the cpuset subsystem.
1264 */
Tejun Heo93438622013-04-14 20:15:25 -07001265 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001266 return -EINVAL;
1267
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001268
1269 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001270 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271 return -EINVAL;
1272
1273 /*
1274 * We either have to specify by name or by subsystems. (So all
1275 * empty hierarchies must have a name).
1276 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001277 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001278 return -EINVAL;
1279
Ben Blumcf5d5942010-03-10 15:22:09 -08001280 /*
1281 * Grab references on all the modules we'll need, so the subsystems
1282 * don't dance around before rebind_subsystems attaches them. This may
1283 * take duplicate reference counts on a subsystem that's already used,
1284 * but rebind_subsystems handles this case.
1285 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001286 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001287 unsigned long bit = 1UL << i;
1288
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001289 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 continue;
1291 if (!try_module_get(subsys[i]->module)) {
1292 module_pin_failed = true;
1293 break;
1294 }
1295 }
1296 if (module_pin_failed) {
1297 /*
1298 * oops, one of the modules was going away. this means that we
1299 * raced with a module_delete call, and to the user this is
1300 * essentially a "subsystem doesn't exist" case.
1301 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001302 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001303 /* drop refcounts only on the ones we took */
1304 unsigned long bit = 1UL << i;
1305
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001306 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 continue;
1308 module_put(subsys[i]->module);
1309 }
1310 return -ENOENT;
1311 }
1312
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 return 0;
1314}
1315
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001316static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001317{
1318 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001319 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 unsigned long bit = 1UL << i;
1321
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001322 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001323 continue;
1324 module_put(subsys[i]->module);
1325 }
1326}
1327
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1329{
1330 int ret = 0;
1331 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001334 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
Tejun Heo873fe092013-04-14 20:15:26 -07001336 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1337 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1338 return -EINVAL;
1339 }
1340
Paul Menagebd89aab2007-10-18 23:40:44 -07001341 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001343 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344
1345 /* See what subsystems are wanted */
1346 ret = parse_cgroupfs_options(data, &opts);
1347 if (ret)
1348 goto out_unlock;
1349
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001350 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001351 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1352 task_tgid_nr(current), current->comm);
1353
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001354 added_mask = opts.subsys_mask & ~root->subsys_mask;
1355 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001356
Ben Blumcf5d5942010-03-10 15:22:09 -08001357 /* Don't allow flags or name to change at remount */
1358 if (opts.flags != root->flags ||
1359 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001361 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 goto out_unlock;
1363 }
1364
Gao feng7083d032012-12-03 09:28:18 +08001365 /*
1366 * Clear out the files of subsystems that should be removed, do
1367 * this before rebind_subsystems, since rebind_subsystems may
1368 * change this hierarchy's subsys_list.
1369 */
1370 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1371
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001372 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001373 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001374 /* rebind_subsystems failed, re-populate the removed files */
1375 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001376 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001377 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001378 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001380 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001381 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382
Paul Menage81a6a5c2007-10-18 23:39:38 -07001383 if (opts.release_agent)
1384 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001386 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001387 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001388 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001390 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 return ret;
1392}
1393
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001394static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 .statfs = simple_statfs,
1396 .drop_inode = generic_delete_inode,
1397 .show_options = cgroup_show_options,
1398 .remount_fs = cgroup_remount,
1399};
1400
Paul Menagecc31edc2008-10-18 20:28:04 -07001401static void init_cgroup_housekeeping(struct cgroup *cgrp)
1402{
1403 INIT_LIST_HEAD(&cgrp->sibling);
1404 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001405 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001406 INIT_LIST_HEAD(&cgrp->cset_links);
Tejun Heo22430762012-11-19 08:13:35 -08001407 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001410 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001411 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001412 INIT_LIST_HEAD(&cgrp->event_list);
1413 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001414 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001415}
Paul Menagec6d57f32009-09-23 15:56:19 -07001416
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417static void init_cgroup_root(struct cgroupfs_root *root)
1418{
Paul Menagebd89aab2007-10-18 23:40:44 -07001419 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001420
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 INIT_LIST_HEAD(&root->subsys_list);
1422 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001423 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001425 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001426 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001427 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001428 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429}
1430
Tejun Heofa3ca072013-04-14 11:36:56 -07001431static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001432{
Tejun Heo1a574232013-04-14 11:36:58 -07001433 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001434
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001435 lockdep_assert_held(&cgroup_mutex);
1436 lockdep_assert_held(&cgroup_root_mutex);
1437
Tejun Heo1a574232013-04-14 11:36:58 -07001438 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1439 if (id < 0)
1440 return id;
1441
1442 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001443 return 0;
1444}
1445
1446static void cgroup_exit_root_id(struct cgroupfs_root *root)
1447{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001448 lockdep_assert_held(&cgroup_mutex);
1449 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001450
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001451 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001452 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001453 root->hierarchy_id = 0;
1454 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001455}
1456
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457static int cgroup_test_super(struct super_block *sb, void *data)
1458{
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460 struct cgroupfs_root *root = sb->s_fs_info;
1461
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 /* If we asked for a name then it must match */
1463 if (opts->name && strcmp(opts->name, root->name))
1464 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001466 /*
1467 * If we asked for subsystems (or explicitly for no
1468 * subsystems) then they must match
1469 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001470 if ((opts->subsys_mask || opts->none)
1471 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 return 0;
1473
1474 return 1;
1475}
1476
Paul Menagec6d57f32009-09-23 15:56:19 -07001477static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1478{
1479 struct cgroupfs_root *root;
1480
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001481 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 return NULL;
1483
1484 root = kzalloc(sizeof(*root), GFP_KERNEL);
1485 if (!root)
1486 return ERR_PTR(-ENOMEM);
1487
1488 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001489
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001490 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001491 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001492 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001493 if (opts->release_agent)
1494 strcpy(root->release_agent_path, opts->release_agent);
1495 if (opts->name)
1496 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001497 if (opts->cpuset_clone_children)
1498 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 return root;
1500}
1501
Tejun Heofa3ca072013-04-14 11:36:56 -07001502static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001503{
Tejun Heofa3ca072013-04-14 11:36:56 -07001504 if (root) {
1505 /* hierarhcy ID shoulid already have been released */
1506 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001507
Tejun Heofa3ca072013-04-14 11:36:56 -07001508 ida_destroy(&root->cgroup_ida);
1509 kfree(root);
1510 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511}
1512
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513static int cgroup_set_super(struct super_block *sb, void *data)
1514{
1515 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001516 struct cgroup_sb_opts *opts = data;
1517
1518 /* If we don't have a new root, we can't set up a new sb */
1519 if (!opts->new_root)
1520 return -EINVAL;
1521
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001522 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523
1524 ret = set_anon_super(sb, NULL);
1525 if (ret)
1526 return ret;
1527
Paul Menagec6d57f32009-09-23 15:56:19 -07001528 sb->s_fs_info = opts->new_root;
1529 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530
1531 sb->s_blocksize = PAGE_CACHE_SIZE;
1532 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1533 sb->s_magic = CGROUP_SUPER_MAGIC;
1534 sb->s_op = &cgroup_ops;
1535
1536 return 0;
1537}
1538
1539static int cgroup_get_rootdir(struct super_block *sb)
1540{
Al Viro0df6a632010-12-21 13:29:29 -05001541 static const struct dentry_operations cgroup_dops = {
1542 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001543 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001544 };
1545
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 struct inode *inode =
1547 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548
1549 if (!inode)
1550 return -ENOMEM;
1551
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 inode->i_fop = &simple_dir_operations;
1553 inode->i_op = &cgroup_dir_inode_operations;
1554 /* directories start off with i_nlink == 2 (for "." entry) */
1555 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001556 sb->s_root = d_make_root(inode);
1557 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001559 /* for everything else we want ->d_op set */
1560 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 return 0;
1562}
1563
Al Virof7e83572010-07-26 13:23:11 +04001564static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001566 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567{
1568 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001569 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 int ret = 0;
1571 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001573 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
1575 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001576 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001578 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001579 if (ret)
1580 goto out_err;
1581
1582 /*
1583 * Allocate a new cgroup root. We may not need it if we're
1584 * reusing an existing hierarchy.
1585 */
1586 new_root = cgroup_root_from_opts(&opts);
1587 if (IS_ERR(new_root)) {
1588 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001589 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001590 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001594 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001596 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001597 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001598 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599 }
1600
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 root = sb->s_fs_info;
1602 BUG_ON(!root);
1603 if (root == opts.new_root) {
1604 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001605 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001606 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001608 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001609 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001610 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611
1612 BUG_ON(sb->s_root != NULL);
1613
1614 ret = cgroup_get_rootdir(sb);
1615 if (ret)
1616 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001617 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618
Paul Menage817929e2007-10-18 23:39:36 -07001619 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001621 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001623 /* Check for name clashes with existing mounts */
1624 ret = -EBUSY;
1625 if (strlen(root->name))
1626 for_each_active_root(existing_root)
1627 if (!strcmp(existing_root->name, root->name))
1628 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001629
Paul Menage817929e2007-10-18 23:39:36 -07001630 /*
1631 * We're accessing css_set_count without locking
1632 * css_set_lock here, but that's OK - it can only be
1633 * increased by someone holding cgroup_lock, and
1634 * that's us. The worst that can happen is that we
1635 * have some link structures left over
1636 */
Tejun Heo69d02062013-06-12 21:04:50 -07001637 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001638 if (ret)
1639 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001640
Tejun Heofa3ca072013-04-14 11:36:56 -07001641 ret = cgroup_init_root_id(root);
1642 if (ret)
1643 goto unlock_drop;
1644
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001645 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001647 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001648 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001650 /*
1651 * There must be no failure case after here, since rebinding
1652 * takes care of subsystems' refcounts, which are explicitly
1653 * dropped in the failure exit path.
1654 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655
1656 /* EBUSY should be the only error here */
1657 BUG_ON(ret);
1658
1659 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001660 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661
Li Zefanc12f65d2009-01-07 18:07:42 -08001662 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 root->top_cgroup.dentry = sb->s_root;
1664
Paul Menage817929e2007-10-18 23:39:36 -07001665 /* Link the top cgroup in this hierarchy into all
1666 * the css_set objects */
1667 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001668 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001669 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001670 write_unlock(&css_set_lock);
1671
Tejun Heo69d02062013-06-12 21:04:50 -07001672 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001673
Li Zefanc12f65d2009-01-07 18:07:42 -08001674 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 BUG_ON(root->number_of_cgroups != 1);
1676
eparis@redhat2ce97382011-06-02 21:20:51 +10001677 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001678 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001679 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001680 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001682 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001683 } else {
1684 /*
1685 * We re-used an existing hierarchy - the new root (if
1686 * any) is not needed
1687 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001688 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001689
1690 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1691 root->flags != opts.flags) {
1692 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1693 ret = -EINVAL;
1694 goto drop_new_super;
1695 }
1696
Ben Blumcf5d5942010-03-10 15:22:09 -08001697 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001698 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699 }
1700
Paul Menagec6d57f32009-09-23 15:56:19 -07001701 kfree(opts.release_agent);
1702 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001703 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001705 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001706 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 mutex_unlock(&cgroup_root_mutex);
1708 mutex_unlock(&cgroup_mutex);
1709 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001711 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001712 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001713 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 out_err:
1715 kfree(opts.release_agent);
1716 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001717 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718}
1719
1720static void cgroup_kill_sb(struct super_block *sb) {
1721 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001723 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 int ret;
1725
1726 BUG_ON(!root);
1727
1728 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001729 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
1731 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001732 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
1734 /* Rebind all subsystems back to the default hierarchy */
1735 ret = rebind_subsystems(root, 0);
1736 /* Shouldn't be able to fail ... */
1737 BUG_ON(ret);
1738
Paul Menage817929e2007-10-18 23:39:36 -07001739 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001740 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001741 * root cgroup
1742 */
1743 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001744
Tejun Heo69d02062013-06-12 21:04:50 -07001745 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1746 list_del(&link->cset_link);
1747 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001748 kfree(link);
1749 }
1750 write_unlock(&css_set_lock);
1751
Paul Menage839ec542009-01-29 14:25:22 -08001752 if (!list_empty(&root->root_list)) {
1753 list_del(&root->root_list);
1754 root_count--;
1755 }
Li Zefane5f6a862009-01-07 18:07:41 -08001756
Tejun Heofa3ca072013-04-14 11:36:56 -07001757 cgroup_exit_root_id(root);
1758
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001759 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 mutex_unlock(&cgroup_mutex);
1761
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001762 simple_xattrs_free(&cgrp->xattrs);
1763
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001765 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766}
1767
1768static struct file_system_type cgroup_fs_type = {
1769 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001770 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 .kill_sb = cgroup_kill_sb,
1772};
1773
Greg KH676db4a2010-08-05 13:53:35 -07001774static struct kobject *cgroup_kobj;
1775
Li Zefana043e3b2008-02-23 15:24:09 -08001776/**
1777 * cgroup_path - generate the path of a cgroup
1778 * @cgrp: the cgroup in question
1779 * @buf: the buffer to write the path into
1780 * @buflen: the length of the buffer
1781 *
Li Zefan65dff752013-03-01 15:01:56 +08001782 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1783 *
1784 * We can't generate cgroup path using dentry->d_name, as accessing
1785 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1786 * inode's i_mutex, while on the other hand cgroup_path() can be called
1787 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001789int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790{
Li Zefan65dff752013-03-01 15:01:56 +08001791 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001793
Tejun Heoda1f2962013-04-14 10:32:19 -07001794 if (!cgrp->parent) {
1795 if (strlcpy(buf, "/", buflen) >= buflen)
1796 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 return 0;
1798 }
1799
Tao Ma316eb662012-11-08 21:36:38 +08001800 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001801 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001802
Li Zefan65dff752013-03-01 15:01:56 +08001803 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001804 do {
Li Zefan65dff752013-03-01 15:01:56 +08001805 const char *name = cgroup_name(cgrp);
1806 int len;
1807
1808 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001810 goto out;
1811 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001812
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001814 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001816
1817 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001818 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001819 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001821out:
1822 rcu_read_unlock();
1823 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824}
Ben Blum67523c42010-03-10 15:22:11 -08001825EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826
Tejun Heo857a2be2013-04-14 20:50:08 -07001827/**
1828 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1829 * @task: target task
1830 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1831 * @buf: the buffer to write the path into
1832 * @buflen: the length of the buffer
1833 *
1834 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1835 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1836 * be used inside locks used by cgroup controller callbacks.
1837 */
1838int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1839 char *buf, size_t buflen)
1840{
1841 struct cgroupfs_root *root;
1842 struct cgroup *cgrp = NULL;
1843 int ret = -ENOENT;
1844
1845 mutex_lock(&cgroup_mutex);
1846
1847 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1848 if (root) {
1849 cgrp = task_cgroup_from_root(task, root);
1850 ret = cgroup_path(cgrp, buf, buflen);
1851 }
1852
1853 mutex_unlock(&cgroup_mutex);
1854
1855 return ret;
1856}
1857EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1858
Ben Blum74a11662011-05-26 16:25:20 -07001859/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001860 * Control Group taskset
1861 */
Tejun Heo134d3372011-12-12 18:12:21 -08001862struct task_and_cgroup {
1863 struct task_struct *task;
1864 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001865 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001866};
1867
Tejun Heo2f7ee562011-12-12 18:12:21 -08001868struct cgroup_taskset {
1869 struct task_and_cgroup single;
1870 struct flex_array *tc_array;
1871 int tc_array_len;
1872 int idx;
1873 struct cgroup *cur_cgrp;
1874};
1875
1876/**
1877 * cgroup_taskset_first - reset taskset and return the first task
1878 * @tset: taskset of interest
1879 *
1880 * @tset iteration is initialized and the first task is returned.
1881 */
1882struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1883{
1884 if (tset->tc_array) {
1885 tset->idx = 0;
1886 return cgroup_taskset_next(tset);
1887 } else {
1888 tset->cur_cgrp = tset->single.cgrp;
1889 return tset->single.task;
1890 }
1891}
1892EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1893
1894/**
1895 * cgroup_taskset_next - iterate to the next task in taskset
1896 * @tset: taskset of interest
1897 *
1898 * Return the next task in @tset. Iteration must have been initialized
1899 * with cgroup_taskset_first().
1900 */
1901struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1902{
1903 struct task_and_cgroup *tc;
1904
1905 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1906 return NULL;
1907
1908 tc = flex_array_get(tset->tc_array, tset->idx++);
1909 tset->cur_cgrp = tc->cgrp;
1910 return tc->task;
1911}
1912EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1913
1914/**
1915 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1916 * @tset: taskset of interest
1917 *
1918 * Return the cgroup for the current (last returned) task of @tset. This
1919 * function must be preceded by either cgroup_taskset_first() or
1920 * cgroup_taskset_next().
1921 */
1922struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1923{
1924 return tset->cur_cgrp;
1925}
1926EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1927
1928/**
1929 * cgroup_taskset_size - return the number of tasks in taskset
1930 * @tset: taskset of interest
1931 */
1932int cgroup_taskset_size(struct cgroup_taskset *tset)
1933{
1934 return tset->tc_array ? tset->tc_array_len : 1;
1935}
1936EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1937
1938
Ben Blum74a11662011-05-26 16:25:20 -07001939/*
1940 * cgroup_task_migrate - move a task from one cgroup to another.
1941 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001942 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001943 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001944static void cgroup_task_migrate(struct cgroup *old_cgrp,
1945 struct task_struct *tsk,
1946 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001947{
Tejun Heo5abb8852013-06-12 21:04:49 -07001948 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001949
1950 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001951 * We are synchronized through threadgroup_lock() against PF_EXITING
1952 * setting such that we can't race against cgroup_exit() changing the
1953 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001954 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001955 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001956 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001957
Ben Blum74a11662011-05-26 16:25:20 -07001958 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001960 task_unlock(tsk);
1961
1962 /* Update the css_set linked lists if we're using them */
1963 write_lock(&css_set_lock);
1964 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001965 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001966 write_unlock(&css_set_lock);
1967
1968 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001969 * We just gained a reference on old_cset by taking it from the
1970 * task. As trading it for new_cset is protected by cgroup_mutex,
1971 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001973 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1974 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001975}
1976
Li Zefana043e3b2008-02-23 15:24:09 -08001977/**
Li Zefan081aa452013-03-13 09:17:09 +08001978 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001979 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001980 * @tsk: the task or the leader of the threadgroup to be attached
1981 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001982 *
Tejun Heo257058a2011-12-12 18:12:21 -08001983 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001984 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001985 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001986static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1987 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001988{
1989 int retval, i, group_size;
1990 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001991 struct cgroupfs_root *root = cgrp->root;
1992 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001993 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001994 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001995 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001996 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001997
1998 /*
1999 * step 0: in order to do expensive, possibly blocking operations for
2000 * every thread, we cannot iterate the thread group list, since it needs
2001 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002002 * group - group_rwsem prevents new threads from appearing, and if
2003 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002004 */
Li Zefan081aa452013-03-13 09:17:09 +08002005 if (threadgroup)
2006 group_size = get_nr_threads(tsk);
2007 else
2008 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002009 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002010 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002011 if (!group)
2012 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002013 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002014 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002015 if (retval)
2016 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002017
Ben Blum74a11662011-05-26 16:25:20 -07002018 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002019 /*
2020 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2021 * already PF_EXITING could be freed from underneath us unless we
2022 * take an rcu_read_lock.
2023 */
2024 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002025 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002026 struct task_and_cgroup ent;
2027
Tejun Heocd3d0952011-12-12 18:12:21 -08002028 /* @tsk either already exited or can't exit until the end */
2029 if (tsk->flags & PF_EXITING)
2030 continue;
2031
Ben Blum74a11662011-05-26 16:25:20 -07002032 /* as per above, nr_threads may decrease, but not increase. */
2033 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002034 ent.task = tsk;
2035 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002036 /* nothing to do if this task is already in the cgroup */
2037 if (ent.cgrp == cgrp)
2038 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002039 /*
2040 * saying GFP_ATOMIC has no effect here because we did prealloc
2041 * earlier, but it's good form to communicate our expectations.
2042 */
Tejun Heo134d3372011-12-12 18:12:21 -08002043 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002044 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002045 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002046
2047 if (!threadgroup)
2048 break;
Ben Blum74a11662011-05-26 16:25:20 -07002049 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002050 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002051 /* remember the number of threads in the array for later. */
2052 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002053 tset.tc_array = group;
2054 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002055
Tejun Heo134d3372011-12-12 18:12:21 -08002056 /* methods shouldn't be called if no task is actually migrating */
2057 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002058 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002059 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002060
Ben Blum74a11662011-05-26 16:25:20 -07002061 /*
2062 * step 1: check that we can legitimately attach to the cgroup.
2063 */
2064 for_each_subsys(root, ss) {
2065 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002066 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002067 if (retval) {
2068 failed_ss = ss;
2069 goto out_cancel_attach;
2070 }
2071 }
Ben Blum74a11662011-05-26 16:25:20 -07002072 }
2073
2074 /*
2075 * step 2: make sure css_sets exist for all threads to be migrated.
2076 * we use find_css_set, which allocates a new one if necessary.
2077 */
Ben Blum74a11662011-05-26 16:25:20 -07002078 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002079 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002080 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2081 if (!tc->cg) {
2082 retval = -ENOMEM;
2083 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085 }
2086
2087 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002088 * step 3: now that we're guaranteed success wrt the css_sets,
2089 * proceed to move all tasks to the new cgroup. There are no
2090 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002091 */
Ben Blum74a11662011-05-26 16:25:20 -07002092 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002093 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002094 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002095 }
2096 /* nothing is sensitive to fork() after this point. */
2097
2098 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002099 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002100 */
2101 for_each_subsys(root, ss) {
2102 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002103 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002104 }
2105
2106 /*
2107 * step 5: success! and cleanup
2108 */
Ben Blum74a11662011-05-26 16:25:20 -07002109 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002110out_put_css_set_refs:
2111 if (retval) {
2112 for (i = 0; i < group_size; i++) {
2113 tc = flex_array_get(group, i);
2114 if (!tc->cg)
2115 break;
2116 put_css_set(tc->cg);
2117 }
Ben Blum74a11662011-05-26 16:25:20 -07002118 }
2119out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (retval) {
2121 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002122 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002123 break;
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002125 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127 }
Ben Blum74a11662011-05-26 16:25:20 -07002128out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002129 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002130 return retval;
2131}
2132
2133/*
2134 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002135 * function to attach either it or all tasks in its threadgroup. Will lock
2136 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
2138static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002141 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 int ret;
2143
Ben Blum74a11662011-05-26 16:25:20 -07002144 if (!cgroup_lock_live_group(cgrp))
2145 return -ENODEV;
2146
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147retry_find_task:
2148 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002150 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002151 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002152 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 ret= -ESRCH;
2154 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155 }
Ben Blum74a11662011-05-26 16:25:20 -07002156 /*
2157 * even if we're attaching all tasks in the thread group, we
2158 * only need to check permissions on one of them.
2159 */
David Howellsc69e8d92008-11-14 10:39:19 +11002160 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002161 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2162 !uid_eq(cred->euid, tcred->uid) &&
2163 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002164 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 ret = -EACCES;
2166 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 } else
2169 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002170
2171 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002173
2174 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002175 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002176 * trapped in a cpuset, or RT worker may be born in a cgroup
2177 * with no rt_runtime allocated. Just say no.
2178 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002179 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002180 ret = -EINVAL;
2181 rcu_read_unlock();
2182 goto out_unlock_cgroup;
2183 }
2184
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185 get_task_struct(tsk);
2186 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002187
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 threadgroup_lock(tsk);
2189 if (threadgroup) {
2190 if (!thread_group_leader(tsk)) {
2191 /*
2192 * a race with de_thread from another thread's exec()
2193 * may strip us of our leadership, if this happens,
2194 * there is no choice but to throw this task away and
2195 * try again; this is
2196 * "double-double-toil-and-trouble-check locking".
2197 */
2198 threadgroup_unlock(tsk);
2199 put_task_struct(tsk);
2200 goto retry_find_task;
2201 }
Li Zefan081aa452013-03-13 09:17:09 +08002202 }
2203
2204 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2205
Tejun Heocd3d0952011-12-12 18:12:21 -08002206 threadgroup_unlock(tsk);
2207
Paul Menagebbcb81d2007-10-18 23:39:32 -07002208 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002209out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002210 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002211 return ret;
2212}
2213
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002214/**
2215 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2216 * @from: attach to all cgroups of a given task
2217 * @tsk: the task to be attached
2218 */
2219int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2220{
2221 struct cgroupfs_root *root;
2222 int retval = 0;
2223
Tejun Heo47cfcd02013-04-07 09:29:51 -07002224 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002225 for_each_active_root(root) {
2226 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2227
2228 retval = cgroup_attach_task(from_cg, tsk, false);
2229 if (retval)
2230 break;
2231 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002232 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233
2234 return retval;
2235}
2236EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2237
Paul Menageaf351022008-07-25 01:47:01 -07002238static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2239{
Ben Blum74a11662011-05-26 16:25:20 -07002240 return attach_task_by_pid(cgrp, pid, false);
2241}
2242
2243static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2244{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002245 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002246}
2247
Paul Menagee788e062008-07-25 01:46:59 -07002248static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2249 const char *buffer)
2250{
2251 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002252 if (strlen(buffer) >= PATH_MAX)
2253 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002254 if (!cgroup_lock_live_group(cgrp))
2255 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002256 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002257 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002258 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002259 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002260 return 0;
2261}
2262
2263static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2264 struct seq_file *seq)
2265{
2266 if (!cgroup_lock_live_group(cgrp))
2267 return -ENODEV;
2268 seq_puts(seq, cgrp->root->release_agent_path);
2269 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002270 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002271 return 0;
2272}
2273
Tejun Heo873fe092013-04-14 20:15:26 -07002274static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2275 struct seq_file *seq)
2276{
2277 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002278 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002279}
2280
Paul Menage84eea842008-07-25 01:47:00 -07002281/* A buffer size big enough for numbers or short strings */
2282#define CGROUP_LOCAL_BUFFER_SIZE 64
2283
Paul Menagee73d2c62008-04-29 01:00:06 -07002284static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002285 struct file *file,
2286 const char __user *userbuf,
2287 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002288{
Paul Menage84eea842008-07-25 01:47:00 -07002289 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002290 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002291 char *end;
2292
2293 if (!nbytes)
2294 return -EINVAL;
2295 if (nbytes >= sizeof(buffer))
2296 return -E2BIG;
2297 if (copy_from_user(buffer, userbuf, nbytes))
2298 return -EFAULT;
2299
2300 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002301 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002302 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002303 if (*end)
2304 return -EINVAL;
2305 retval = cft->write_u64(cgrp, cft, val);
2306 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002307 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002308 if (*end)
2309 return -EINVAL;
2310 retval = cft->write_s64(cgrp, cft, val);
2311 }
Paul Menage355e0c42007-10-18 23:39:33 -07002312 if (!retval)
2313 retval = nbytes;
2314 return retval;
2315}
2316
Paul Menagedb3b1492008-07-25 01:46:58 -07002317static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2318 struct file *file,
2319 const char __user *userbuf,
2320 size_t nbytes, loff_t *unused_ppos)
2321{
Paul Menage84eea842008-07-25 01:47:00 -07002322 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002323 int retval = 0;
2324 size_t max_bytes = cft->max_write_len;
2325 char *buffer = local_buffer;
2326
2327 if (!max_bytes)
2328 max_bytes = sizeof(local_buffer) - 1;
2329 if (nbytes >= max_bytes)
2330 return -E2BIG;
2331 /* Allocate a dynamic buffer if we need one */
2332 if (nbytes >= sizeof(local_buffer)) {
2333 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2334 if (buffer == NULL)
2335 return -ENOMEM;
2336 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002337 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2338 retval = -EFAULT;
2339 goto out;
2340 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002341
2342 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002343 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 if (!retval)
2345 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002346out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002347 if (buffer != local_buffer)
2348 kfree(buffer);
2349 return retval;
2350}
2351
Paul Menageddbcc7e2007-10-18 23:39:30 -07002352static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2353 size_t nbytes, loff_t *ppos)
2354{
2355 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002356 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357
Tejun Heo54766d42013-06-12 21:04:53 -07002358 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002360 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002361 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002362 if (cft->write_u64 || cft->write_s64)
2363 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002364 if (cft->write_string)
2365 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002366 if (cft->trigger) {
2367 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2368 return ret ? ret : nbytes;
2369 }
Paul Menage355e0c42007-10-18 23:39:33 -07002370 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371}
2372
Paul Menagef4c753b2008-04-29 00:59:56 -07002373static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2374 struct file *file,
2375 char __user *buf, size_t nbytes,
2376 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377{
Paul Menage84eea842008-07-25 01:47:00 -07002378 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002379 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2381
2382 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2383}
2384
Paul Menagee73d2c62008-04-29 01:00:06 -07002385static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2386 struct file *file,
2387 char __user *buf, size_t nbytes,
2388 loff_t *ppos)
2389{
Paul Menage84eea842008-07-25 01:47:00 -07002390 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002391 s64 val = cft->read_s64(cgrp, cft);
2392 int len = sprintf(tmp, "%lld\n", (long long) val);
2393
2394 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2395}
2396
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2398 size_t nbytes, loff_t *ppos)
2399{
2400 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002401 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402
Tejun Heo54766d42013-06-12 21:04:53 -07002403 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404 return -ENODEV;
2405
2406 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002407 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002408 if (cft->read_u64)
2409 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002410 if (cft->read_s64)
2411 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 return -EINVAL;
2413}
2414
Paul Menage91796562008-04-29 01:00:01 -07002415/*
2416 * seqfile ops/methods for returning structured data. Currently just
2417 * supports string->u64 maps, but can be extended in future.
2418 */
2419
2420struct cgroup_seqfile_state {
2421 struct cftype *cft;
2422 struct cgroup *cgroup;
2423};
2424
2425static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2426{
2427 struct seq_file *sf = cb->state;
2428 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2429}
2430
2431static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2432{
2433 struct cgroup_seqfile_state *state = m->private;
2434 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002435 if (cft->read_map) {
2436 struct cgroup_map_cb cb = {
2437 .fill = cgroup_map_add,
2438 .state = m,
2439 };
2440 return cft->read_map(state->cgroup, cft, &cb);
2441 }
2442 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002443}
2444
Adrian Bunk96930a62008-07-25 19:46:21 -07002445static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002446{
2447 struct seq_file *seq = file->private_data;
2448 kfree(seq->private);
2449 return single_release(inode, file);
2450}
2451
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002452static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002453 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002454 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002455 .llseek = seq_lseek,
2456 .release = cgroup_seqfile_release,
2457};
2458
Paul Menageddbcc7e2007-10-18 23:39:30 -07002459static int cgroup_file_open(struct inode *inode, struct file *file)
2460{
2461 int err;
2462 struct cftype *cft;
2463
2464 err = generic_file_open(inode, file);
2465 if (err)
2466 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002467 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002468
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002469 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002470 struct cgroup_seqfile_state *state;
2471
2472 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002473 if (!state)
2474 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002475
Paul Menage91796562008-04-29 01:00:01 -07002476 state->cft = cft;
2477 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2478 file->f_op = &cgroup_seqfile_operations;
2479 err = single_open(file, cgroup_seqfile_show, state);
2480 if (err < 0)
2481 kfree(state);
2482 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 err = cft->open(inode, file);
2484 else
2485 err = 0;
2486
2487 return err;
2488}
2489
2490static int cgroup_file_release(struct inode *inode, struct file *file)
2491{
2492 struct cftype *cft = __d_cft(file->f_dentry);
2493 if (cft->release)
2494 return cft->release(inode, file);
2495 return 0;
2496}
2497
2498/*
2499 * cgroup_rename - Only allow simple rename of directories in place.
2500 */
2501static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2502 struct inode *new_dir, struct dentry *new_dentry)
2503{
Li Zefan65dff752013-03-01 15:01:56 +08002504 int ret;
2505 struct cgroup_name *name, *old_name;
2506 struct cgroup *cgrp;
2507
2508 /*
2509 * It's convinient to use parent dir's i_mutex to protected
2510 * cgrp->name.
2511 */
2512 lockdep_assert_held(&old_dir->i_mutex);
2513
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2515 return -ENOTDIR;
2516 if (new_dentry->d_inode)
2517 return -EEXIST;
2518 if (old_dir != new_dir)
2519 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002520
2521 cgrp = __d_cgrp(old_dentry);
2522
2523 name = cgroup_alloc_name(new_dentry);
2524 if (!name)
2525 return -ENOMEM;
2526
2527 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2528 if (ret) {
2529 kfree(name);
2530 return ret;
2531 }
2532
2533 old_name = cgrp->name;
2534 rcu_assign_pointer(cgrp->name, name);
2535
2536 kfree_rcu(old_name, rcu_head);
2537 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538}
2539
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002540static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2541{
2542 if (S_ISDIR(dentry->d_inode->i_mode))
2543 return &__d_cgrp(dentry)->xattrs;
2544 else
Li Zefan712317a2013-04-18 23:09:52 -07002545 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002546}
2547
2548static inline int xattr_enabled(struct dentry *dentry)
2549{
2550 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002551 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002552}
2553
2554static bool is_valid_xattr(const char *name)
2555{
2556 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2557 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2558 return true;
2559 return false;
2560}
2561
2562static int cgroup_setxattr(struct dentry *dentry, const char *name,
2563 const void *val, size_t size, int flags)
2564{
2565 if (!xattr_enabled(dentry))
2566 return -EOPNOTSUPP;
2567 if (!is_valid_xattr(name))
2568 return -EINVAL;
2569 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2570}
2571
2572static int cgroup_removexattr(struct dentry *dentry, const char *name)
2573{
2574 if (!xattr_enabled(dentry))
2575 return -EOPNOTSUPP;
2576 if (!is_valid_xattr(name))
2577 return -EINVAL;
2578 return simple_xattr_remove(__d_xattrs(dentry), name);
2579}
2580
2581static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2582 void *buf, size_t size)
2583{
2584 if (!xattr_enabled(dentry))
2585 return -EOPNOTSUPP;
2586 if (!is_valid_xattr(name))
2587 return -EINVAL;
2588 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2589}
2590
2591static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2592{
2593 if (!xattr_enabled(dentry))
2594 return -EOPNOTSUPP;
2595 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2596}
2597
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002598static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002599 .read = cgroup_file_read,
2600 .write = cgroup_file_write,
2601 .llseek = generic_file_llseek,
2602 .open = cgroup_file_open,
2603 .release = cgroup_file_release,
2604};
2605
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002606static const struct inode_operations cgroup_file_inode_operations = {
2607 .setxattr = cgroup_setxattr,
2608 .getxattr = cgroup_getxattr,
2609 .listxattr = cgroup_listxattr,
2610 .removexattr = cgroup_removexattr,
2611};
2612
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002613static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002614 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002615 .mkdir = cgroup_mkdir,
2616 .rmdir = cgroup_rmdir,
2617 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002618 .setxattr = cgroup_setxattr,
2619 .getxattr = cgroup_getxattr,
2620 .listxattr = cgroup_listxattr,
2621 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622};
2623
Al Viro00cd8dd2012-06-10 17:13:09 -04002624static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002625{
2626 if (dentry->d_name.len > NAME_MAX)
2627 return ERR_PTR(-ENAMETOOLONG);
2628 d_add(dentry, NULL);
2629 return NULL;
2630}
2631
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002632/*
2633 * Check if a file is a control file
2634 */
2635static inline struct cftype *__file_cft(struct file *file)
2636{
Al Viro496ad9a2013-01-23 17:07:38 -05002637 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002638 return ERR_PTR(-EINVAL);
2639 return __d_cft(file->f_dentry);
2640}
2641
Al Viroa5e7ed32011-07-26 01:55:55 -04002642static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002643 struct super_block *sb)
2644{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 struct inode *inode;
2646
2647 if (!dentry)
2648 return -ENOENT;
2649 if (dentry->d_inode)
2650 return -EEXIST;
2651
2652 inode = cgroup_new_inode(mode, sb);
2653 if (!inode)
2654 return -ENOMEM;
2655
2656 if (S_ISDIR(mode)) {
2657 inode->i_op = &cgroup_dir_inode_operations;
2658 inode->i_fop = &simple_dir_operations;
2659
2660 /* start off with i_nlink == 2 (for "." entry) */
2661 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002662 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663
Tejun Heob8a2df62012-11-19 08:13:37 -08002664 /*
2665 * Control reaches here with cgroup_mutex held.
2666 * @inode->i_mutex should nest outside cgroup_mutex but we
2667 * want to populate it immediately without releasing
2668 * cgroup_mutex. As @inode isn't visible to anyone else
2669 * yet, trylock will always succeed without affecting
2670 * lockdep checks.
2671 */
2672 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 } else if (S_ISREG(mode)) {
2674 inode->i_size = 0;
2675 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002676 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 d_instantiate(dentry, inode);
2679 dget(dentry); /* Extra count - pin the dentry in core */
2680 return 0;
2681}
2682
Li Zefan099fca32009-04-02 16:57:29 -07002683/**
2684 * cgroup_file_mode - deduce file mode of a control file
2685 * @cft: the control file in question
2686 *
2687 * returns cft->mode if ->mode is not 0
2688 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2689 * returns S_IRUGO if it has only a read handler
2690 * returns S_IWUSR if it has only a write hander
2691 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002692static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002693{
Al Viroa5e7ed32011-07-26 01:55:55 -04002694 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002695
2696 if (cft->mode)
2697 return cft->mode;
2698
2699 if (cft->read || cft->read_u64 || cft->read_s64 ||
2700 cft->read_map || cft->read_seq_string)
2701 mode |= S_IRUGO;
2702
2703 if (cft->write || cft->write_u64 || cft->write_s64 ||
2704 cft->write_string || cft->trigger)
2705 mode |= S_IWUSR;
2706
2707 return mode;
2708}
2709
Tejun Heodb0416b2012-04-01 12:09:55 -07002710static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002711 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712{
Paul Menagebd89aab2007-10-18 23:40:44 -07002713 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002714 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002716 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002718 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002720
Tejun Heo93438622013-04-14 20:15:25 -07002721 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 strcpy(name, subsys->name);
2723 strcat(name, ".");
2724 }
2725 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002726
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728
2729 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2730 if (!cfe)
2731 return -ENOMEM;
2732
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002734 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002736 goto out;
2737 }
2738
Li Zefand6cbf352013-05-14 19:44:20 +08002739 cfe->type = (void *)cft;
2740 cfe->dentry = dentry;
2741 dentry->d_fsdata = cfe;
2742 simple_xattrs_init(&cfe->xattrs);
2743
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 mode = cgroup_file_mode(cft);
2745 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2746 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 list_add_tail(&cfe->node, &parent->files);
2748 cfe = NULL;
2749 }
2750 dput(dentry);
2751out:
2752 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753 return error;
2754}
2755
Tejun Heo79578622012-04-01 12:09:56 -07002756static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002757 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002759 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002760 int err, ret = 0;
2761
2762 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002763 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002764 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2765 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002766 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2767 continue;
2768 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2769 continue;
2770
Li Zefan2739d3c2013-01-21 18:18:33 +08002771 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002772 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002773 if (err)
2774 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2775 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002776 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002777 } else {
2778 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002779 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002780 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002781 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002782}
2783
Tejun Heo8e3f6542012-04-01 12:09:55 -07002784static DEFINE_MUTEX(cgroup_cft_mutex);
2785
2786static void cgroup_cfts_prepare(void)
2787 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2788{
2789 /*
2790 * Thanks to the entanglement with vfs inode locking, we can't walk
2791 * the existing cgroups under cgroup_mutex and create files.
2792 * Instead, we increment reference on all cgroups and build list of
2793 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2794 * exclusive access to the field.
2795 */
2796 mutex_lock(&cgroup_cft_mutex);
2797 mutex_lock(&cgroup_mutex);
2798}
2799
2800static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002801 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002802 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2803{
2804 LIST_HEAD(pending);
2805 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002806
2807 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2808 if (cfts && ss->root != &rootnode) {
2809 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2810 dget(cgrp->dentry);
2811 list_add_tail(&cgrp->cft_q_node, &pending);
2812 }
2813 }
2814
2815 mutex_unlock(&cgroup_mutex);
2816
2817 /*
2818 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2819 * files for all cgroups which were created before.
2820 */
2821 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2822 struct inode *inode = cgrp->dentry->d_inode;
2823
2824 mutex_lock(&inode->i_mutex);
2825 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -07002826 if (!cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002827 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002828 mutex_unlock(&cgroup_mutex);
2829 mutex_unlock(&inode->i_mutex);
2830
2831 list_del_init(&cgrp->cft_q_node);
2832 dput(cgrp->dentry);
2833 }
2834
2835 mutex_unlock(&cgroup_cft_mutex);
2836}
2837
2838/**
2839 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2840 * @ss: target cgroup subsystem
2841 * @cfts: zero-length name terminated array of cftypes
2842 *
2843 * Register @cfts to @ss. Files described by @cfts are created for all
2844 * existing cgroups to which @ss is attached and all future cgroups will
2845 * have them too. This function can be called anytime whether @ss is
2846 * attached or not.
2847 *
2848 * Returns 0 on successful registration, -errno on failure. Note that this
2849 * function currently returns 0 as long as @cfts registration is successful
2850 * even if some file creation attempts on existing cgroups fail.
2851 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002852int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853{
2854 struct cftype_set *set;
2855
2856 set = kzalloc(sizeof(*set), GFP_KERNEL);
2857 if (!set)
2858 return -ENOMEM;
2859
2860 cgroup_cfts_prepare();
2861 set->cfts = cfts;
2862 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002863 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002864
2865 return 0;
2866}
2867EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2868
Li Zefana043e3b2008-02-23 15:24:09 -08002869/**
Tejun Heo79578622012-04-01 12:09:56 -07002870 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2871 * @ss: target cgroup subsystem
2872 * @cfts: zero-length name terminated array of cftypes
2873 *
2874 * Unregister @cfts from @ss. Files described by @cfts are removed from
2875 * all existing cgroups to which @ss is attached and all future cgroups
2876 * won't have them either. This function can be called anytime whether @ss
2877 * is attached or not.
2878 *
2879 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2880 * registered with @ss.
2881 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002882int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002883{
2884 struct cftype_set *set;
2885
2886 cgroup_cfts_prepare();
2887
2888 list_for_each_entry(set, &ss->cftsets, node) {
2889 if (set->cfts == cfts) {
2890 list_del_init(&set->node);
2891 cgroup_cfts_commit(ss, cfts, false);
2892 return 0;
2893 }
2894 }
2895
2896 cgroup_cfts_commit(ss, NULL, false);
2897 return -ENOENT;
2898}
2899
2900/**
Li Zefana043e3b2008-02-23 15:24:09 -08002901 * cgroup_task_count - count the number of tasks in a cgroup.
2902 * @cgrp: the cgroup in question
2903 *
2904 * Return the number of tasks in the cgroup.
2905 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002906int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002907{
2908 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002909 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002910
Paul Menage817929e2007-10-18 23:39:36 -07002911 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002912 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2913 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002914 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002915 return count;
2916}
2917
2918/*
Paul Menage817929e2007-10-18 23:39:36 -07002919 * Advance a list_head iterator. The iterator should be positioned at
2920 * the start of a css_set
2921 */
Tejun Heo69d02062013-06-12 21:04:50 -07002922static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002923{
Tejun Heo69d02062013-06-12 21:04:50 -07002924 struct list_head *l = it->cset_link;
2925 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002926 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002927
2928 /* Advance to the next non-empty css_set */
2929 do {
2930 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002931 if (l == &cgrp->cset_links) {
2932 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002933 return;
2934 }
Tejun Heo69d02062013-06-12 21:04:50 -07002935 link = list_entry(l, struct cgrp_cset_link, cset_link);
2936 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002937 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002938 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002939 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002940}
2941
Cliff Wickman31a7df02008-02-07 00:14:42 -08002942/*
2943 * To reduce the fork() overhead for systems that are not actually
2944 * using their cgroups capability, we don't maintain the lists running
2945 * through each css_set to its tasks until we see the list actually
2946 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002947 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002948static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002949{
2950 struct task_struct *p, *g;
2951 write_lock(&css_set_lock);
2952 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002953 /*
2954 * We need tasklist_lock because RCU is not safe against
2955 * while_each_thread(). Besides, a forking task that has passed
2956 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2957 * is not guaranteed to have its child immediately visible in the
2958 * tasklist if we walk through it with RCU.
2959 */
2960 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002961 do_each_thread(g, p) {
2962 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002963 /*
2964 * We should check if the process is exiting, otherwise
2965 * it will race with cgroup_exit() in that the list
2966 * entry won't be deleted though the process has exited.
2967 */
2968 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002969 list_add(&p->cg_list, &p->cgroups->tasks);
2970 task_unlock(p);
2971 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002972 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002973 write_unlock(&css_set_lock);
2974}
2975
Tejun Heo574bd9f2012-11-09 09:12:29 -08002976/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002977 * cgroup_next_sibling - find the next sibling of a given cgroup
2978 * @pos: the current cgroup
2979 *
2980 * This function returns the next sibling of @pos and should be called
2981 * under RCU read lock. The only requirement is that @pos is accessible.
2982 * The next sibling is guaranteed to be returned regardless of @pos's
2983 * state.
2984 */
2985struct cgroup *cgroup_next_sibling(struct cgroup *pos)
2986{
2987 struct cgroup *next;
2988
2989 WARN_ON_ONCE(!rcu_read_lock_held());
2990
2991 /*
2992 * @pos could already have been removed. Once a cgroup is removed,
2993 * its ->sibling.next is no longer updated when its next sibling
Tejun Heo54766d42013-06-12 21:04:53 -07002994 * changes. As CGRP_DEAD is set on removal which is fully
Tejun Heo53fa5262013-05-24 10:55:38 +09002995 * serialized, if we see it unasserted, it's guaranteed that the
2996 * next sibling hasn't finished its grace period even if it's
2997 * already removed, and thus safe to dereference from this RCU
2998 * critical section. If ->sibling.next is inaccessible,
Tejun Heo54766d42013-06-12 21:04:53 -07002999 * cgroup_is_dead() is guaranteed to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003000 */
Tejun Heo54766d42013-06-12 21:04:53 -07003001 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003002 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3003 if (&next->sibling != &pos->parent->children)
3004 return next;
3005 return NULL;
3006 }
3007
3008 /*
3009 * Can't dereference the next pointer. Each cgroup is given a
3010 * monotonically increasing unique serial number and always
3011 * appended to the sibling list, so the next one can be found by
3012 * walking the parent's children until we see a cgroup with higher
3013 * serial number than @pos's.
3014 *
3015 * While this path can be slow, it's taken only when either the
3016 * current cgroup is removed or iteration and removal race.
3017 */
3018 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3019 if (next->serial_nr > pos->serial_nr)
3020 return next;
3021 return NULL;
3022}
3023EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3024
3025/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003026 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3027 * @pos: the current position (%NULL to initiate traversal)
3028 * @cgroup: cgroup whose descendants to walk
3029 *
3030 * To be used by cgroup_for_each_descendant_pre(). Find the next
3031 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003032 *
3033 * While this function requires RCU read locking, it doesn't require the
3034 * whole traversal to be contained in a single RCU critical section. This
3035 * function will return the correct next descendant as long as both @pos
3036 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003037 */
3038struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3039 struct cgroup *cgroup)
3040{
3041 struct cgroup *next;
3042
3043 WARN_ON_ONCE(!rcu_read_lock_held());
3044
3045 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003046 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003047 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003048
3049 /* visit the first child if exists */
3050 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3051 if (next)
3052 return next;
3053
3054 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003055 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003056 next = cgroup_next_sibling(pos);
3057 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003058 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003059 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003060 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061
3062 return NULL;
3063}
3064EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3065
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003066/**
3067 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3068 * @pos: cgroup of interest
3069 *
3070 * Return the rightmost descendant of @pos. If there's no descendant,
3071 * @pos is returned. This can be used during pre-order traversal to skip
3072 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003073 *
3074 * While this function requires RCU read locking, it doesn't require the
3075 * whole traversal to be contained in a single RCU critical section. This
3076 * function will return the correct rightmost descendant as long as @pos is
3077 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003078 */
3079struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3080{
3081 struct cgroup *last, *tmp;
3082
3083 WARN_ON_ONCE(!rcu_read_lock_held());
3084
3085 do {
3086 last = pos;
3087 /* ->prev isn't RCU safe, walk ->next till the end */
3088 pos = NULL;
3089 list_for_each_entry_rcu(tmp, &last->children, sibling)
3090 pos = tmp;
3091 } while (pos);
3092
3093 return last;
3094}
3095EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3096
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3098{
3099 struct cgroup *last;
3100
3101 do {
3102 last = pos;
3103 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3104 sibling);
3105 } while (pos);
3106
3107 return last;
3108}
3109
3110/**
3111 * cgroup_next_descendant_post - find the next descendant for post-order walk
3112 * @pos: the current position (%NULL to initiate traversal)
3113 * @cgroup: cgroup whose descendants to walk
3114 *
3115 * To be used by cgroup_for_each_descendant_post(). Find the next
3116 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003117 *
3118 * While this function requires RCU read locking, it doesn't require the
3119 * whole traversal to be contained in a single RCU critical section. This
3120 * function will return the correct next descendant as long as both @pos
3121 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003122 */
3123struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3124 struct cgroup *cgroup)
3125{
3126 struct cgroup *next;
3127
3128 WARN_ON_ONCE(!rcu_read_lock_held());
3129
3130 /* if first iteration, visit the leftmost descendant */
3131 if (!pos) {
3132 next = cgroup_leftmost_descendant(cgroup);
3133 return next != cgroup ? next : NULL;
3134 }
3135
3136 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003137 next = cgroup_next_sibling(pos);
3138 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003139 return cgroup_leftmost_descendant(next);
3140
3141 /* no sibling left, visit parent */
3142 next = pos->parent;
3143 return next != cgroup ? next : NULL;
3144}
3145EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3146
Paul Menagebd89aab2007-10-18 23:40:44 -07003147void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003148 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003149{
3150 /*
3151 * The first time anyone tries to iterate across a cgroup,
3152 * we need to enable the list linking each css_set to its
3153 * tasks, and fix up all existing tasks.
3154 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003155 if (!use_task_css_set_links)
3156 cgroup_enable_task_cg_lists();
3157
Paul Menage817929e2007-10-18 23:39:36 -07003158 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003159 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003160 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003161}
3162
Paul Menagebd89aab2007-10-18 23:40:44 -07003163struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003164 struct cgroup_iter *it)
3165{
3166 struct task_struct *res;
3167 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003168 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003169
3170 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003171 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003172 return NULL;
3173 res = list_entry(l, struct task_struct, cg_list);
3174 /* Advance iterator to find next entry */
3175 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003176 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3177 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003178 /* We reached the end of this task list - move on to
3179 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003180 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003181 } else {
3182 it->task = l;
3183 }
3184 return res;
3185}
3186
Paul Menagebd89aab2007-10-18 23:40:44 -07003187void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003188 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003189{
3190 read_unlock(&css_set_lock);
3191}
3192
Cliff Wickman31a7df02008-02-07 00:14:42 -08003193static inline int started_after_time(struct task_struct *t1,
3194 struct timespec *time,
3195 struct task_struct *t2)
3196{
3197 int start_diff = timespec_compare(&t1->start_time, time);
3198 if (start_diff > 0) {
3199 return 1;
3200 } else if (start_diff < 0) {
3201 return 0;
3202 } else {
3203 /*
3204 * Arbitrarily, if two processes started at the same
3205 * time, we'll say that the lower pointer value
3206 * started first. Note that t2 may have exited by now
3207 * so this may not be a valid pointer any longer, but
3208 * that's fine - it still serves to distinguish
3209 * between two tasks started (effectively) simultaneously.
3210 */
3211 return t1 > t2;
3212 }
3213}
3214
3215/*
3216 * This function is a callback from heap_insert() and is used to order
3217 * the heap.
3218 * In this case we order the heap in descending task start time.
3219 */
3220static inline int started_after(void *p1, void *p2)
3221{
3222 struct task_struct *t1 = p1;
3223 struct task_struct *t2 = p2;
3224 return started_after_time(t1, &t2->start_time, t2);
3225}
3226
3227/**
3228 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3229 * @scan: struct cgroup_scanner containing arguments for the scan
3230 *
3231 * Arguments include pointers to callback functions test_task() and
3232 * process_task().
3233 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3234 * and if it returns true, call process_task() for it also.
3235 * The test_task pointer may be NULL, meaning always true (select all tasks).
3236 * Effectively duplicates cgroup_iter_{start,next,end}()
3237 * but does not lock css_set_lock for the call to process_task().
3238 * The struct cgroup_scanner may be embedded in any structure of the caller's
3239 * creation.
3240 * It is guaranteed that process_task() will act on every task that
3241 * is a member of the cgroup for the duration of this call. This
3242 * function may or may not call process_task() for tasks that exit
3243 * or move to a different cgroup during the call, or are forked or
3244 * move into the cgroup during the call.
3245 *
3246 * Note that test_task() may be called with locks held, and may in some
3247 * situations be called multiple times for the same task, so it should
3248 * be cheap.
3249 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3250 * pre-allocated and will be used for heap operations (and its "gt" member will
3251 * be overwritten), else a temporary heap will be used (allocation of which
3252 * may cause this function to fail).
3253 */
3254int cgroup_scan_tasks(struct cgroup_scanner *scan)
3255{
3256 int retval, i;
3257 struct cgroup_iter it;
3258 struct task_struct *p, *dropped;
3259 /* Never dereference latest_task, since it's not refcounted */
3260 struct task_struct *latest_task = NULL;
3261 struct ptr_heap tmp_heap;
3262 struct ptr_heap *heap;
3263 struct timespec latest_time = { 0, 0 };
3264
3265 if (scan->heap) {
3266 /* The caller supplied our heap and pre-allocated its memory */
3267 heap = scan->heap;
3268 heap->gt = &started_after;
3269 } else {
3270 /* We need to allocate our own heap memory */
3271 heap = &tmp_heap;
3272 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3273 if (retval)
3274 /* cannot allocate the heap */
3275 return retval;
3276 }
3277
3278 again:
3279 /*
3280 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3281 * to determine which are of interest, and using the scanner's
3282 * "process_task" callback to process any of them that need an update.
3283 * Since we don't want to hold any locks during the task updates,
3284 * gather tasks to be processed in a heap structure.
3285 * The heap is sorted by descending task start time.
3286 * If the statically-sized heap fills up, we overflow tasks that
3287 * started later, and in future iterations only consider tasks that
3288 * started after the latest task in the previous pass. This
3289 * guarantees forward progress and that we don't miss any tasks.
3290 */
3291 heap->size = 0;
3292 cgroup_iter_start(scan->cg, &it);
3293 while ((p = cgroup_iter_next(scan->cg, &it))) {
3294 /*
3295 * Only affect tasks that qualify per the caller's callback,
3296 * if he provided one
3297 */
3298 if (scan->test_task && !scan->test_task(p, scan))
3299 continue;
3300 /*
3301 * Only process tasks that started after the last task
3302 * we processed
3303 */
3304 if (!started_after_time(p, &latest_time, latest_task))
3305 continue;
3306 dropped = heap_insert(heap, p);
3307 if (dropped == NULL) {
3308 /*
3309 * The new task was inserted; the heap wasn't
3310 * previously full
3311 */
3312 get_task_struct(p);
3313 } else if (dropped != p) {
3314 /*
3315 * The new task was inserted, and pushed out a
3316 * different task
3317 */
3318 get_task_struct(p);
3319 put_task_struct(dropped);
3320 }
3321 /*
3322 * Else the new task was newer than anything already in
3323 * the heap and wasn't inserted
3324 */
3325 }
3326 cgroup_iter_end(scan->cg, &it);
3327
3328 if (heap->size) {
3329 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003330 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003331 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003332 latest_time = q->start_time;
3333 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003334 }
3335 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003336 scan->process_task(q, scan);
3337 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003338 }
3339 /*
3340 * If we had to process any tasks at all, scan again
3341 * in case some of them were in the middle of forking
3342 * children that didn't get processed.
3343 * Not the most efficient way to do it, but it avoids
3344 * having to take callback_mutex in the fork path
3345 */
3346 goto again;
3347 }
3348 if (heap == &tmp_heap)
3349 heap_free(&tmp_heap);
3350 return 0;
3351}
3352
Tejun Heo8cc99342013-04-07 09:29:50 -07003353static void cgroup_transfer_one_task(struct task_struct *task,
3354 struct cgroup_scanner *scan)
3355{
3356 struct cgroup *new_cgroup = scan->data;
3357
Tejun Heo47cfcd02013-04-07 09:29:51 -07003358 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003359 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003360 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003361}
3362
3363/**
3364 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3365 * @to: cgroup to which the tasks will be moved
3366 * @from: cgroup in which the tasks currently reside
3367 */
3368int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3369{
3370 struct cgroup_scanner scan;
3371
3372 scan.cg = from;
3373 scan.test_task = NULL; /* select all tasks in cgroup */
3374 scan.process_task = cgroup_transfer_one_task;
3375 scan.heap = NULL;
3376 scan.data = to;
3377
3378 return cgroup_scan_tasks(&scan);
3379}
3380
Paul Menage817929e2007-10-18 23:39:36 -07003381/*
Ben Blum102a7752009-09-23 15:56:26 -07003382 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003383 *
3384 * Reading this file can return large amounts of data if a cgroup has
3385 * *lots* of attached tasks. So it may need several calls to read(),
3386 * but we cannot guarantee that the information we produce is correct
3387 * unless we produce it entirely atomically.
3388 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003389 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003390
Li Zefan24528252012-01-20 11:58:43 +08003391/* which pidlist file are we talking about? */
3392enum cgroup_filetype {
3393 CGROUP_FILE_PROCS,
3394 CGROUP_FILE_TASKS,
3395};
3396
3397/*
3398 * A pidlist is a list of pids that virtually represents the contents of one
3399 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3400 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3401 * to the cgroup.
3402 */
3403struct cgroup_pidlist {
3404 /*
3405 * used to find which pidlist is wanted. doesn't change as long as
3406 * this particular list stays in the list.
3407 */
3408 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3409 /* array of xids */
3410 pid_t *list;
3411 /* how many elements the above list has */
3412 int length;
3413 /* how many files are using the current array */
3414 int use_count;
3415 /* each of these stored in a list by its cgroup */
3416 struct list_head links;
3417 /* pointer to the cgroup we belong to, for list removal purposes */
3418 struct cgroup *owner;
3419 /* protects the other fields */
3420 struct rw_semaphore mutex;
3421};
3422
Paul Menagebbcb81d2007-10-18 23:39:32 -07003423/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003424 * The following two functions "fix" the issue where there are more pids
3425 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3426 * TODO: replace with a kernel-wide solution to this problem
3427 */
3428#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3429static void *pidlist_allocate(int count)
3430{
3431 if (PIDLIST_TOO_LARGE(count))
3432 return vmalloc(count * sizeof(pid_t));
3433 else
3434 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3435}
3436static void pidlist_free(void *p)
3437{
3438 if (is_vmalloc_addr(p))
3439 vfree(p);
3440 else
3441 kfree(p);
3442}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003443
3444/*
Ben Blum102a7752009-09-23 15:56:26 -07003445 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003446 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003447 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003448static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003449{
Ben Blum102a7752009-09-23 15:56:26 -07003450 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003451
3452 /*
3453 * we presume the 0th element is unique, so i starts at 1. trivial
3454 * edge cases first; no work needs to be done for either
3455 */
3456 if (length == 0 || length == 1)
3457 return length;
3458 /* src and dest walk down the list; dest counts unique elements */
3459 for (src = 1; src < length; src++) {
3460 /* find next unique element */
3461 while (list[src] == list[src-1]) {
3462 src++;
3463 if (src == length)
3464 goto after;
3465 }
3466 /* dest always points to where the next unique element goes */
3467 list[dest] = list[src];
3468 dest++;
3469 }
3470after:
Ben Blum102a7752009-09-23 15:56:26 -07003471 return dest;
3472}
3473
3474static int cmppid(const void *a, const void *b)
3475{
3476 return *(pid_t *)a - *(pid_t *)b;
3477}
3478
3479/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003480 * find the appropriate pidlist for our purpose (given procs vs tasks)
3481 * returns with the lock on that pidlist already held, and takes care
3482 * of the use count, or returns NULL with no locks held if we're out of
3483 * memory.
3484 */
3485static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3486 enum cgroup_filetype type)
3487{
3488 struct cgroup_pidlist *l;
3489 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003490 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003491
Ben Blum72a8cb32009-09-23 15:56:27 -07003492 /*
3493 * We can't drop the pidlist_mutex before taking the l->mutex in case
3494 * the last ref-holder is trying to remove l from the list at the same
3495 * time. Holding the pidlist_mutex precludes somebody taking whichever
3496 * list we find out from under us - compare release_pid_array().
3497 */
3498 mutex_lock(&cgrp->pidlist_mutex);
3499 list_for_each_entry(l, &cgrp->pidlists, links) {
3500 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003501 /* make sure l doesn't vanish out from under us */
3502 down_write(&l->mutex);
3503 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003504 return l;
3505 }
3506 }
3507 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003508 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003509 if (!l) {
3510 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003511 return l;
3512 }
3513 init_rwsem(&l->mutex);
3514 down_write(&l->mutex);
3515 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003516 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003517 l->owner = cgrp;
3518 list_add(&l->links, &cgrp->pidlists);
3519 mutex_unlock(&cgrp->pidlist_mutex);
3520 return l;
3521}
3522
3523/*
Ben Blum102a7752009-09-23 15:56:26 -07003524 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3525 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003526static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3527 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003528{
3529 pid_t *array;
3530 int length;
3531 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003532 struct cgroup_iter it;
3533 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003534 struct cgroup_pidlist *l;
3535
3536 /*
3537 * If cgroup gets more users after we read count, we won't have
3538 * enough space - tough. This race is indistinguishable to the
3539 * caller from the case that the additional cgroup users didn't
3540 * show up until sometime later on.
3541 */
3542 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003543 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003544 if (!array)
3545 return -ENOMEM;
3546 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003547 cgroup_iter_start(cgrp, &it);
3548 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003549 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003550 break;
Ben Blum102a7752009-09-23 15:56:26 -07003551 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003552 if (type == CGROUP_FILE_PROCS)
3553 pid = task_tgid_vnr(tsk);
3554 else
3555 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003556 if (pid > 0) /* make sure to only use valid results */
3557 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003558 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003559 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003560 length = n;
3561 /* now sort & (if procs) strip out duplicates */
3562 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003563 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003564 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003565 l = cgroup_pidlist_find(cgrp, type);
3566 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003567 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003568 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003569 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003570 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003571 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003572 l->list = array;
3573 l->length = length;
3574 l->use_count++;
3575 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003576 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003577 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003578}
3579
Balbir Singh846c7bb2007-10-18 23:39:44 -07003580/**
Li Zefana043e3b2008-02-23 15:24:09 -08003581 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003582 * @stats: cgroupstats to fill information into
3583 * @dentry: A dentry entry belonging to the cgroup for which stats have
3584 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003585 *
3586 * Build and fill cgroupstats so that taskstats can export it to user
3587 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003588 */
3589int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3590{
3591 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003592 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003593 struct cgroup_iter it;
3594 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003595
Balbir Singh846c7bb2007-10-18 23:39:44 -07003596 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003597 * Validate dentry by checking the superblock operations,
3598 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003599 */
Li Zefan33d283b2008-11-19 15:36:48 -08003600 if (dentry->d_sb->s_op != &cgroup_ops ||
3601 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003602 goto err;
3603
3604 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003605 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003606
Paul Menagebd89aab2007-10-18 23:40:44 -07003607 cgroup_iter_start(cgrp, &it);
3608 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003609 switch (tsk->state) {
3610 case TASK_RUNNING:
3611 stats->nr_running++;
3612 break;
3613 case TASK_INTERRUPTIBLE:
3614 stats->nr_sleeping++;
3615 break;
3616 case TASK_UNINTERRUPTIBLE:
3617 stats->nr_uninterruptible++;
3618 break;
3619 case TASK_STOPPED:
3620 stats->nr_stopped++;
3621 break;
3622 default:
3623 if (delayacct_is_task_waiting_on_io(tsk))
3624 stats->nr_io_wait++;
3625 break;
3626 }
3627 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003628 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003629
Balbir Singh846c7bb2007-10-18 23:39:44 -07003630err:
3631 return ret;
3632}
3633
Paul Menage8f3ff202009-09-23 15:56:25 -07003634
Paul Menagecc31edc2008-10-18 20:28:04 -07003635/*
Ben Blum102a7752009-09-23 15:56:26 -07003636 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003637 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003638 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003639 */
3640
Ben Blum102a7752009-09-23 15:56:26 -07003641static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003642{
3643 /*
3644 * Initially we receive a position value that corresponds to
3645 * one more than the last pid shown (or 0 on the first call or
3646 * after a seek to the start). Use a binary-search to find the
3647 * next pid to display, if any
3648 */
Ben Blum102a7752009-09-23 15:56:26 -07003649 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003650 int index = 0, pid = *pos;
3651 int *iter;
3652
Ben Blum102a7752009-09-23 15:56:26 -07003653 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003654 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003655 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003656
Paul Menagecc31edc2008-10-18 20:28:04 -07003657 while (index < end) {
3658 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003659 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003660 index = mid;
3661 break;
Ben Blum102a7752009-09-23 15:56:26 -07003662 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003663 index = mid + 1;
3664 else
3665 end = mid;
3666 }
3667 }
3668 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003669 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003670 return NULL;
3671 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003672 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 *pos = *iter;
3674 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003675}
3676
Ben Blum102a7752009-09-23 15:56:26 -07003677static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003678{
Ben Blum102a7752009-09-23 15:56:26 -07003679 struct cgroup_pidlist *l = s->private;
3680 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003681}
3682
Ben Blum102a7752009-09-23 15:56:26 -07003683static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003684{
Ben Blum102a7752009-09-23 15:56:26 -07003685 struct cgroup_pidlist *l = s->private;
3686 pid_t *p = v;
3687 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 /*
3689 * Advance to the next pid in the array. If this goes off the
3690 * end, we're done
3691 */
3692 p++;
3693 if (p >= end) {
3694 return NULL;
3695 } else {
3696 *pos = *p;
3697 return p;
3698 }
3699}
3700
Ben Blum102a7752009-09-23 15:56:26 -07003701static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003702{
3703 return seq_printf(s, "%d\n", *(int *)v);
3704}
3705
Ben Blum102a7752009-09-23 15:56:26 -07003706/*
3707 * seq_operations functions for iterating on pidlists through seq_file -
3708 * independent of whether it's tasks or procs
3709 */
3710static const struct seq_operations cgroup_pidlist_seq_operations = {
3711 .start = cgroup_pidlist_start,
3712 .stop = cgroup_pidlist_stop,
3713 .next = cgroup_pidlist_next,
3714 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003715};
3716
Ben Blum102a7752009-09-23 15:56:26 -07003717static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003718{
Ben Blum72a8cb32009-09-23 15:56:27 -07003719 /*
3720 * the case where we're the last user of this particular pidlist will
3721 * have us remove it from the cgroup's list, which entails taking the
3722 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3723 * pidlist_mutex, we have to take pidlist_mutex first.
3724 */
3725 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003726 down_write(&l->mutex);
3727 BUG_ON(!l->use_count);
3728 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003729 /* we're the last user if refcount is 0; remove and free */
3730 list_del(&l->links);
3731 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003732 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003733 put_pid_ns(l->key.ns);
3734 up_write(&l->mutex);
3735 kfree(l);
3736 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003737 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003739 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003740}
3741
Ben Blum102a7752009-09-23 15:56:26 -07003742static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003743{
Ben Blum102a7752009-09-23 15:56:26 -07003744 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003745 if (!(file->f_mode & FMODE_READ))
3746 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003747 /*
3748 * the seq_file will only be initialized if the file was opened for
3749 * reading; hence we check if it's not null only in that case.
3750 */
3751 l = ((struct seq_file *)file->private_data)->private;
3752 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 return seq_release(inode, file);
3754}
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003757 .read = seq_read,
3758 .llseek = seq_lseek,
3759 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003760 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003761};
3762
3763/*
Ben Blum102a7752009-09-23 15:56:26 -07003764 * The following functions handle opens on a file that displays a pidlist
3765 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3766 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 */
Ben Blum102a7752009-09-23 15:56:26 -07003768/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003769static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003770{
3771 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003772 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003773 int retval;
3774
3775 /* Nothing to do for write-only files */
3776 if (!(file->f_mode & FMODE_READ))
3777 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003778
Ben Blum102a7752009-09-23 15:56:26 -07003779 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003780 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003781 if (retval)
3782 return retval;
3783 /* configure file information */
3784 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003785
Ben Blum102a7752009-09-23 15:56:26 -07003786 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003788 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003789 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003790 }
Ben Blum102a7752009-09-23 15:56:26 -07003791 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003792 return 0;
3793}
Ben Blum102a7752009-09-23 15:56:26 -07003794static int cgroup_tasks_open(struct inode *unused, struct file *file)
3795{
Ben Blum72a8cb32009-09-23 15:56:27 -07003796 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003797}
3798static int cgroup_procs_open(struct inode *unused, struct file *file)
3799{
Ben Blum72a8cb32009-09-23 15:56:27 -07003800 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003801}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003802
Paul Menagebd89aab2007-10-18 23:40:44 -07003803static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003804 struct cftype *cft)
3805{
Paul Menagebd89aab2007-10-18 23:40:44 -07003806 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003807}
3808
Paul Menage6379c102008-07-25 01:47:01 -07003809static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3810 struct cftype *cft,
3811 u64 val)
3812{
3813 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3814 if (val)
3815 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3816 else
3817 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3818 return 0;
3819}
3820
Paul Menagebbcb81d2007-10-18 23:39:32 -07003821/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003822 * Unregister event and free resources.
3823 *
3824 * Gets called from workqueue.
3825 */
3826static void cgroup_event_remove(struct work_struct *work)
3827{
3828 struct cgroup_event *event = container_of(work, struct cgroup_event,
3829 remove);
3830 struct cgroup *cgrp = event->cgrp;
3831
Li Zefan810cbee2013-02-18 18:56:14 +08003832 remove_wait_queue(event->wqh, &event->wait);
3833
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003834 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3835
Li Zefan810cbee2013-02-18 18:56:14 +08003836 /* Notify userspace the event is going away. */
3837 eventfd_signal(event->eventfd, 1);
3838
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003839 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003840 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003841 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003842}
3843
3844/*
3845 * Gets called on POLLHUP on eventfd when user closes it.
3846 *
3847 * Called with wqh->lock held and interrupts disabled.
3848 */
3849static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3850 int sync, void *key)
3851{
3852 struct cgroup_event *event = container_of(wait,
3853 struct cgroup_event, wait);
3854 struct cgroup *cgrp = event->cgrp;
3855 unsigned long flags = (unsigned long)key;
3856
3857 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003858 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003859 * If the event has been detached at cgroup removal, we
3860 * can simply return knowing the other side will cleanup
3861 * for us.
3862 *
3863 * We can't race against event freeing since the other
3864 * side will require wqh->lock via remove_wait_queue(),
3865 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003866 */
Li Zefan810cbee2013-02-18 18:56:14 +08003867 spin_lock(&cgrp->event_list_lock);
3868 if (!list_empty(&event->list)) {
3869 list_del_init(&event->list);
3870 /*
3871 * We are in atomic context, but cgroup_event_remove()
3872 * may sleep, so we have to call it in workqueue.
3873 */
3874 schedule_work(&event->remove);
3875 }
3876 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003877 }
3878
3879 return 0;
3880}
3881
3882static void cgroup_event_ptable_queue_proc(struct file *file,
3883 wait_queue_head_t *wqh, poll_table *pt)
3884{
3885 struct cgroup_event *event = container_of(pt,
3886 struct cgroup_event, pt);
3887
3888 event->wqh = wqh;
3889 add_wait_queue(wqh, &event->wait);
3890}
3891
3892/*
3893 * Parse input and register new cgroup event handler.
3894 *
3895 * Input must be in format '<event_fd> <control_fd> <args>'.
3896 * Interpretation of args is defined by control file implementation.
3897 */
3898static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3899 const char *buffer)
3900{
3901 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003902 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003903 unsigned int efd, cfd;
3904 struct file *efile = NULL;
3905 struct file *cfile = NULL;
3906 char *endp;
3907 int ret;
3908
3909 efd = simple_strtoul(buffer, &endp, 10);
3910 if (*endp != ' ')
3911 return -EINVAL;
3912 buffer = endp + 1;
3913
3914 cfd = simple_strtoul(buffer, &endp, 10);
3915 if ((*endp != ' ') && (*endp != '\0'))
3916 return -EINVAL;
3917 buffer = endp + 1;
3918
3919 event = kzalloc(sizeof(*event), GFP_KERNEL);
3920 if (!event)
3921 return -ENOMEM;
3922 event->cgrp = cgrp;
3923 INIT_LIST_HEAD(&event->list);
3924 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3925 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3926 INIT_WORK(&event->remove, cgroup_event_remove);
3927
3928 efile = eventfd_fget(efd);
3929 if (IS_ERR(efile)) {
3930 ret = PTR_ERR(efile);
3931 goto fail;
3932 }
3933
3934 event->eventfd = eventfd_ctx_fileget(efile);
3935 if (IS_ERR(event->eventfd)) {
3936 ret = PTR_ERR(event->eventfd);
3937 goto fail;
3938 }
3939
3940 cfile = fget(cfd);
3941 if (!cfile) {
3942 ret = -EBADF;
3943 goto fail;
3944 }
3945
3946 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003947 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003948 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003949 if (ret < 0)
3950 goto fail;
3951
3952 event->cft = __file_cft(cfile);
3953 if (IS_ERR(event->cft)) {
3954 ret = PTR_ERR(event->cft);
3955 goto fail;
3956 }
3957
Li Zefanf1690072013-02-18 14:13:35 +08003958 /*
3959 * The file to be monitored must be in the same cgroup as
3960 * cgroup.event_control is.
3961 */
3962 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3963 if (cgrp_cfile != cgrp) {
3964 ret = -EINVAL;
3965 goto fail;
3966 }
3967
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003968 if (!event->cft->register_event || !event->cft->unregister_event) {
3969 ret = -EINVAL;
3970 goto fail;
3971 }
3972
3973 ret = event->cft->register_event(cgrp, event->cft,
3974 event->eventfd, buffer);
3975 if (ret)
3976 goto fail;
3977
Li Zefan7ef70e42013-04-26 11:58:03 -07003978 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003979
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003980 /*
3981 * Events should be removed after rmdir of cgroup directory, but before
3982 * destroying subsystem state objects. Let's take reference to cgroup
3983 * directory dentry to do that.
3984 */
3985 dget(cgrp->dentry);
3986
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003987 spin_lock(&cgrp->event_list_lock);
3988 list_add(&event->list, &cgrp->event_list);
3989 spin_unlock(&cgrp->event_list_lock);
3990
3991 fput(cfile);
3992 fput(efile);
3993
3994 return 0;
3995
3996fail:
3997 if (cfile)
3998 fput(cfile);
3999
4000 if (event && event->eventfd && !IS_ERR(event->eventfd))
4001 eventfd_ctx_put(event->eventfd);
4002
4003 if (!IS_ERR_OR_NULL(efile))
4004 fput(efile);
4005
4006 kfree(event);
4007
4008 return ret;
4009}
4010
Daniel Lezcano97978e62010-10-27 15:33:35 -07004011static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4012 struct cftype *cft)
4013{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004014 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004015}
4016
4017static int cgroup_clone_children_write(struct cgroup *cgrp,
4018 struct cftype *cft,
4019 u64 val)
4020{
4021 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004022 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004023 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004024 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004025 return 0;
4026}
4027
Tejun Heod5c56ce2013-06-03 19:14:34 -07004028static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004029 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004030 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004031 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004032 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004033 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004034 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004035 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004036 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004037 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004038 .write_string = cgroup_write_event_control,
4039 .mode = S_IWUGO,
4040 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004041 {
4042 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004043 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004044 .read_u64 = cgroup_clone_children_read,
4045 .write_u64 = cgroup_clone_children_write,
4046 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004047 {
Tejun Heo873fe092013-04-14 20:15:26 -07004048 .name = "cgroup.sane_behavior",
4049 .flags = CFTYPE_ONLY_ON_ROOT,
4050 .read_seq_string = cgroup_sane_behavior_show,
4051 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004052
4053 /*
4054 * Historical crazy stuff. These don't have "cgroup." prefix and
4055 * don't exist if sane_behavior. If you're depending on these, be
4056 * prepared to be burned.
4057 */
4058 {
4059 .name = "tasks",
4060 .flags = CFTYPE_INSANE, /* use "procs" instead */
4061 .open = cgroup_tasks_open,
4062 .write_u64 = cgroup_tasks_write,
4063 .release = cgroup_pidlist_release,
4064 .mode = S_IRUGO | S_IWUSR,
4065 },
4066 {
4067 .name = "notify_on_release",
4068 .flags = CFTYPE_INSANE,
4069 .read_u64 = cgroup_read_notify_on_release,
4070 .write_u64 = cgroup_write_notify_on_release,
4071 },
Tejun Heo873fe092013-04-14 20:15:26 -07004072 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004073 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004074 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004075 .read_seq_string = cgroup_release_agent_show,
4076 .write_string = cgroup_release_agent_write,
4077 .max_write_len = PATH_MAX,
4078 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004079 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004080};
4081
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004082/**
4083 * cgroup_populate_dir - selectively creation of files in a directory
4084 * @cgrp: target cgroup
4085 * @base_files: true if the base files should be added
4086 * @subsys_mask: mask of the subsystem ids whose files should be added
4087 */
4088static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4089 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004090{
4091 int err;
4092 struct cgroup_subsys *ss;
4093
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004094 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004095 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004096 if (err < 0)
4097 return err;
4098 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004099
Tejun Heo8e3f6542012-04-01 12:09:55 -07004100 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004101 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004102 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004103 if (!test_bit(ss->subsys_id, &subsys_mask))
4104 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004105
Tejun Heodb0416b2012-04-01 12:09:55 -07004106 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004107 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004109
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004110 /* This cgroup is ready now */
4111 for_each_subsys(cgrp->root, ss) {
4112 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4113 /*
4114 * Update id->css pointer and make this css visible from
4115 * CSS ID functions. This pointer will be dereferened
4116 * from RCU-read-side without locks.
4117 */
4118 if (css->id)
4119 rcu_assign_pointer(css->id->css, css);
4120 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004121
4122 return 0;
4123}
4124
Tejun Heo48ddbe12012-04-01 12:09:56 -07004125static void css_dput_fn(struct work_struct *work)
4126{
4127 struct cgroup_subsys_state *css =
4128 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004129 struct dentry *dentry = css->cgroup->dentry;
4130 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004131
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004132 atomic_inc(&sb->s_active);
4133 dput(dentry);
4134 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004135}
4136
Paul Menageddbcc7e2007-10-18 23:39:30 -07004137static void init_cgroup_css(struct cgroup_subsys_state *css,
4138 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004139 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004140{
Paul Menagebd89aab2007-10-18 23:40:44 -07004141 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004142 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004144 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004145 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004146 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004147 BUG_ON(cgrp->subsys[ss->subsys_id]);
4148 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004149
4150 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004151 * css holds an extra ref to @cgrp->dentry which is put on the last
4152 * css_put(). dput() requires process context, which css_put() may
4153 * be called without. @css->dput_work will be used to invoke
4154 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004155 */
4156 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157}
4158
Tejun Heob1929db2012-11-19 08:13:38 -08004159/* invoke ->post_create() on a new CSS and mark it online if successful */
4160static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004161{
Tejun Heob1929db2012-11-19 08:13:38 -08004162 int ret = 0;
4163
Tejun Heoa31f2d32012-11-19 08:13:37 -08004164 lockdep_assert_held(&cgroup_mutex);
4165
Tejun Heo92fb9742012-11-19 08:13:38 -08004166 if (ss->css_online)
4167 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004168 if (!ret)
4169 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4170 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004171}
4172
4173/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4174static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4175 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4176{
4177 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4178
4179 lockdep_assert_held(&cgroup_mutex);
4180
4181 if (!(css->flags & CSS_ONLINE))
4182 return;
4183
Li Zefand7eeac12013-03-12 15:35:59 -07004184 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004185 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004186
4187 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4188}
4189
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190/*
Li Zefana043e3b2008-02-23 15:24:09 -08004191 * cgroup_create - create a cgroup
4192 * @parent: cgroup that will be parent of the new cgroup
4193 * @dentry: dentry of the new cgroup
4194 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195 *
Li Zefana043e3b2008-02-23 15:24:09 -08004196 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004197 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004199 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200{
Tejun Heo53fa5262013-05-24 10:55:38 +09004201 static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0);
Paul Menagebd89aab2007-10-18 23:40:44 -07004202 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004203 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004204 struct cgroupfs_root *root = parent->root;
4205 int err = 0;
4206 struct cgroup_subsys *ss;
4207 struct super_block *sb = root->sb;
4208
Tejun Heo0a950f62012-11-19 09:02:12 -08004209 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004210 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4211 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212 return -ENOMEM;
4213
Li Zefan65dff752013-03-01 15:01:56 +08004214 name = cgroup_alloc_name(dentry);
4215 if (!name)
4216 goto err_free_cgrp;
4217 rcu_assign_pointer(cgrp->name, name);
4218
Tejun Heo0a950f62012-11-19 09:02:12 -08004219 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4220 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004221 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004222
Tejun Heo976c06b2012-11-05 09:16:59 -08004223 /*
4224 * Only live parents can have children. Note that the liveliness
4225 * check isn't strictly necessary because cgroup_mkdir() and
4226 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4227 * anyway so that locking is contained inside cgroup proper and we
4228 * don't get nasty surprises if we ever grow another caller.
4229 */
4230 if (!cgroup_lock_live_group(parent)) {
4231 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004232 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004233 }
4234
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235 /* Grab a reference on the superblock so the hierarchy doesn't
4236 * get deleted on unmount if there are child cgroups. This
4237 * can be done outside cgroup_mutex, since the sb can't
4238 * disappear while someone has an open control file on the
4239 * fs */
4240 atomic_inc(&sb->s_active);
4241
Paul Menagecc31edc2008-10-18 20:28:04 -07004242 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243
Li Zefanfe1c06c2013-01-24 14:30:22 +08004244 dentry->d_fsdata = cgrp;
4245 cgrp->dentry = dentry;
4246
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 cgrp->parent = parent;
4248 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249
Li Zefanb6abdb02008-03-04 14:28:19 -08004250 if (notify_on_release(parent))
4251 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4252
Tejun Heo2260e7f2012-11-19 08:13:38 -08004253 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4254 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004255
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004257 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004258
Tejun Heo92fb9742012-11-19 08:13:38 -08004259 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260 if (IS_ERR(css)) {
4261 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004262 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004264 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004265 if (ss->use_id) {
4266 err = alloc_css_id(ss, parent, cgrp);
4267 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004268 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004269 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270 }
4271
Tejun Heo4e139af2012-11-19 08:13:36 -08004272 /*
4273 * Create directory. cgroup_create_file() returns with the new
4274 * directory locked on success so that it can be populated without
4275 * dropping cgroup_mutex.
4276 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004277 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004279 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004280 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
Tejun Heo53fa5262013-05-24 10:55:38 +09004282 /*
4283 * Assign a monotonically increasing serial number. With the list
4284 * appending below, it guarantees that sibling cgroups are always
4285 * sorted in the ascending serial number order on the parent's
4286 * ->children.
4287 */
4288 cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor);
4289
Tejun Heo4e139af2012-11-19 08:13:36 -08004290 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004291 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4292 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4293 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004294
Tejun Heob1929db2012-11-19 08:13:38 -08004295 /* each css holds a ref to the cgroup's dentry */
4296 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004297 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004298
Li Zefan415cf072013-04-08 14:35:02 +08004299 /* hold a ref to the parent's dentry */
4300 dget(parent->dentry);
4301
Tejun Heob1929db2012-11-19 08:13:38 -08004302 /* creation succeeded, notify subsystems */
4303 for_each_subsys(root, ss) {
4304 err = online_css(ss, cgrp);
4305 if (err)
4306 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004307
4308 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4309 parent->parent) {
4310 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",
4311 current->comm, current->pid, ss->name);
4312 if (!strcmp(ss->name, "memory"))
4313 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4314 ss->warned_broken_hierarchy = true;
4315 }
Tejun Heoa8638032012-11-09 09:12:29 -08004316 }
4317
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004318 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004319 if (err)
4320 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321
4322 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004323 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324
4325 return 0;
4326
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004327err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004329 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004330 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333 /* Release the reference count that we took on the superblock */
4334 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004335err_free_id:
4336 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004337err_free_name:
4338 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004339err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004340 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004341 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004342
4343err_destroy:
4344 cgroup_destroy_locked(cgrp);
4345 mutex_unlock(&cgroup_mutex);
4346 mutex_unlock(&dentry->d_inode->i_mutex);
4347 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348}
4349
Al Viro18bb1db2011-07-26 01:41:39 -04004350static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351{
4352 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4353
4354 /* the vfs holds inode->i_mutex already */
4355 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4356}
4357
Tejun Heo42809dd2012-11-19 08:13:37 -08004358static int cgroup_destroy_locked(struct cgroup *cgrp)
4359 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004360{
Tejun Heo42809dd2012-11-19 08:13:37 -08004361 struct dentry *d = cgrp->dentry;
4362 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004363 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004364 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004365 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366
Tejun Heo42809dd2012-11-19 08:13:37 -08004367 lockdep_assert_held(&d->d_inode->i_mutex);
4368 lockdep_assert_held(&cgroup_mutex);
4369
Tejun Heoddd69142013-06-12 21:04:54 -07004370 /*
Tejun Heo6f3d8282013-06-12 21:04:55 -07004371 * css_set_lock synchronizes access to ->cset_links and prevents
4372 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004373 */
4374 read_lock(&css_set_lock);
Tejun Heo6f3d8282013-06-12 21:04:55 -07004375 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004376 read_unlock(&css_set_lock);
4377 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004379
Tejun Heo1a90dd52012-11-05 09:16:59 -08004380 /*
4381 * Block new css_tryget() by deactivating refcnt and mark @cgrp
Tejun Heo455050d2013-06-13 19:27:41 -07004382 * removed. This makes future css_tryget() attempts fail which we
4383 * guarantee to ->css_offline() callbacks.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004384 */
Tejun Heoed9577932012-11-05 09:16:58 -08004385 for_each_subsys(cgrp->root, ss) {
4386 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4387
4388 WARN_ON(atomic_read(&css->refcnt) < 0);
4389 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004390 }
Tejun Heo455050d2013-06-13 19:27:41 -07004391
4392 /*
4393 * Mark @cgrp dead. This prevents further task migration and child
4394 * creation by disabling cgroup_lock_live_group(). Note that
4395 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4396 * resume iteration after dropping RCU read lock. See
4397 * cgroup_next_sibling() for details.
4398 */
Tejun Heo54766d42013-06-12 21:04:53 -07004399 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004400
Tejun Heo455050d2013-06-13 19:27:41 -07004401 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4402 raw_spin_lock(&release_list_lock);
4403 if (!list_empty(&cgrp->release_list))
4404 list_del_init(&cgrp->release_list);
4405 raw_spin_unlock(&release_list_lock);
4406
4407 /*
4408 * Remove @cgrp directory. The removal puts the base ref but we
4409 * aren't quite done with @cgrp yet, so hold onto it.
4410 */
4411 dget(d);
4412 cgroup_d_remove_dir(d);
4413
4414 /*
4415 * Unregister events and notify userspace.
4416 * Notify userspace about cgroup removing only after rmdir of cgroup
4417 * directory to avoid race between userspace and kernelspace.
4418 */
4419 spin_lock(&cgrp->event_list_lock);
4420 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4421 list_del_init(&event->list);
4422 schedule_work(&event->remove);
4423 }
4424 spin_unlock(&cgrp->event_list_lock);
4425
Tejun Heoa31f2d32012-11-19 08:13:37 -08004426 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004427 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004428 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004429
4430 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004431 * Put all the base refs. Each css holds an extra reference to the
4432 * cgroup's dentry and cgroup removal proceeds regardless of css
4433 * refs. On the last put of each css, whenever that may be, the
4434 * extra dentry ref is put so that dentry destruction happens only
4435 * after all css's are released.
4436 */
Tejun Heoe9316082012-11-05 09:16:58 -08004437 for_each_subsys(cgrp->root, ss)
4438 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004439
Paul Menage999cd8a2009-01-07 18:08:36 -08004440 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004441 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004442 list_del_init(&cgrp->allcg_node);
4443
Paul Menageddbcc7e2007-10-18 23:39:30 -07004444 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004445
Paul Menagebd89aab2007-10-18 23:40:44 -07004446 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004447 check_for_release(parent);
4448
Paul Menageddbcc7e2007-10-18 23:39:30 -07004449 return 0;
4450}
4451
Tejun Heo42809dd2012-11-19 08:13:37 -08004452static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4453{
4454 int ret;
4455
4456 mutex_lock(&cgroup_mutex);
4457 ret = cgroup_destroy_locked(dentry->d_fsdata);
4458 mutex_unlock(&cgroup_mutex);
4459
4460 return ret;
4461}
4462
Tejun Heo8e3f6542012-04-01 12:09:55 -07004463static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4464{
4465 INIT_LIST_HEAD(&ss->cftsets);
4466
4467 /*
4468 * base_cftset is embedded in subsys itself, no need to worry about
4469 * deregistration.
4470 */
4471 if (ss->base_cftypes) {
4472 ss->base_cftset.cfts = ss->base_cftypes;
4473 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4474 }
4475}
4476
Li Zefan06a11922008-04-29 01:00:07 -07004477static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004478{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004480
4481 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482
Tejun Heo648bb562012-11-19 08:13:36 -08004483 mutex_lock(&cgroup_mutex);
4484
Tejun Heo8e3f6542012-04-01 12:09:55 -07004485 /* init base cftset */
4486 cgroup_init_cftsets(ss);
4487
Paul Menageddbcc7e2007-10-18 23:39:30 -07004488 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004489 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004490 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004491 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492 /* We don't handle early failures gracefully */
4493 BUG_ON(IS_ERR(css));
4494 init_cgroup_css(css, ss, dummytop);
4495
Li Zefane8d55fd2008-04-29 01:00:13 -07004496 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004497 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004498 * newly registered, all tasks and hence the
4499 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004500 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004501
4502 need_forkexit_callback |= ss->fork || ss->exit;
4503
Li Zefane8d55fd2008-04-29 01:00:13 -07004504 /* At system boot, before all subsystems have been
4505 * registered, no tasks have been forked, so we don't
4506 * need to invoke fork callbacks here. */
4507 BUG_ON(!list_empty(&init_task.tasks));
4508
Tejun Heob1929db2012-11-19 08:13:38 -08004509 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004510
Tejun Heo648bb562012-11-19 08:13:36 -08004511 mutex_unlock(&cgroup_mutex);
4512
Ben Blume6a11052010-03-10 15:22:09 -08004513 /* this function shouldn't be used with modular subsystems, since they
4514 * need to register a subsys_id, among other things */
4515 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004516}
4517
4518/**
Ben Blume6a11052010-03-10 15:22:09 -08004519 * cgroup_load_subsys: load and register a modular subsystem at runtime
4520 * @ss: the subsystem to load
4521 *
4522 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004523 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004524 * up for use. If the subsystem is built-in anyway, work is delegated to the
4525 * simpler cgroup_init_subsys.
4526 */
4527int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4528{
Ben Blume6a11052010-03-10 15:22:09 -08004529 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004530 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004531 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004532 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004533 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004534
4535 /* check name and function validity */
4536 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004537 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004538 return -EINVAL;
4539
4540 /*
4541 * we don't support callbacks in modular subsystems. this check is
4542 * before the ss->module check for consistency; a subsystem that could
4543 * be a module should still have no callbacks even if the user isn't
4544 * compiling it as one.
4545 */
4546 if (ss->fork || ss->exit)
4547 return -EINVAL;
4548
4549 /*
4550 * an optionally modular subsystem is built-in: we want to do nothing,
4551 * since cgroup_init_subsys will have already taken care of it.
4552 */
4553 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004554 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004555 BUG_ON(subsys[ss->subsys_id] != ss);
4556 return 0;
4557 }
4558
Tejun Heo8e3f6542012-04-01 12:09:55 -07004559 /* init base cftset */
4560 cgroup_init_cftsets(ss);
4561
Ben Blume6a11052010-03-10 15:22:09 -08004562 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004563 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004564
4565 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004566 * no ss->css_alloc seems to need anything important in the ss
4567 * struct, so this can happen first (i.e. before the rootnode
4568 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004569 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004570 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004571 if (IS_ERR(css)) {
4572 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004573 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004574 mutex_unlock(&cgroup_mutex);
4575 return PTR_ERR(css);
4576 }
4577
4578 list_add(&ss->sibling, &rootnode.subsys_list);
4579 ss->root = &rootnode;
4580
4581 /* our new subsystem will be attached to the dummy hierarchy. */
4582 init_cgroup_css(css, ss, dummytop);
4583 /* init_idr must be after init_cgroup_css because it sets css->id. */
4584 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004585 ret = cgroup_init_idr(ss, css);
4586 if (ret)
4587 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004588 }
4589
4590 /*
4591 * Now we need to entangle the css into the existing css_sets. unlike
4592 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4593 * will need a new pointer to it; done by iterating the css_set_table.
4594 * furthermore, modifying the existing css_sets will corrupt the hash
4595 * table state, so each changed css_set will need its hash recomputed.
4596 * this is all done under the css_set_lock.
4597 */
4598 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004599 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004600 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004601 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004602 continue;
4603 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004604 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004605 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004606 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004607 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004608 key = css_set_hash(cset->subsys);
4609 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004610 }
4611 write_unlock(&css_set_lock);
4612
Tejun Heob1929db2012-11-19 08:13:38 -08004613 ret = online_css(ss, dummytop);
4614 if (ret)
4615 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004616
Ben Blume6a11052010-03-10 15:22:09 -08004617 /* success! */
4618 mutex_unlock(&cgroup_mutex);
4619 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004620
4621err_unload:
4622 mutex_unlock(&cgroup_mutex);
4623 /* @ss can't be mounted here as try_module_get() would fail */
4624 cgroup_unload_subsys(ss);
4625 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004626}
4627EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4628
4629/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004630 * cgroup_unload_subsys: unload a modular subsystem
4631 * @ss: the subsystem to unload
4632 *
4633 * This function should be called in a modular subsystem's exitcall. When this
4634 * function is invoked, the refcount on the subsystem's module will be 0, so
4635 * the subsystem will not be attached to any hierarchy.
4636 */
4637void cgroup_unload_subsys(struct cgroup_subsys *ss)
4638{
Tejun Heo69d02062013-06-12 21:04:50 -07004639 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004640
4641 BUG_ON(ss->module == NULL);
4642
4643 /*
4644 * we shouldn't be called if the subsystem is in use, and the use of
4645 * try_module_get in parse_cgroupfs_options should ensure that it
4646 * doesn't start being used while we're killing it off.
4647 */
4648 BUG_ON(ss->root != &rootnode);
4649
4650 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004651
Tejun Heoa31f2d32012-11-19 08:13:37 -08004652 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004653
Tejun Heoc897ff62013-02-27 17:03:49 -08004654 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004655 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004656
Ben Blumcf5d5942010-03-10 15:22:09 -08004657 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004658 subsys[ss->subsys_id] = NULL;
4659
4660 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004661 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004662
4663 /*
4664 * disentangle the css from all css_sets attached to the dummytop. as
4665 * in loading, we need to pay our respects to the hashtable gods.
4666 */
4667 write_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004668 list_for_each_entry(link, &dummytop->cset_links, cset_link) {
4669 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004670 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004671
Tejun Heo5abb8852013-06-12 21:04:49 -07004672 hash_del(&cset->hlist);
4673 cset->subsys[ss->subsys_id] = NULL;
4674 key = css_set_hash(cset->subsys);
4675 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004676 }
4677 write_unlock(&css_set_lock);
4678
4679 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004680 * remove subsystem's css from the dummytop and free it - need to
4681 * free before marking as null because ss->css_free needs the
4682 * cgrp->subsys pointer to find their state. note that this also
4683 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004684 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004685 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004686 dummytop->subsys[ss->subsys_id] = NULL;
4687
4688 mutex_unlock(&cgroup_mutex);
4689}
4690EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4691
4692/**
Li Zefana043e3b2008-02-23 15:24:09 -08004693 * cgroup_init_early - cgroup initialization at system boot
4694 *
4695 * Initialize cgroups at system boot, and initialize any
4696 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004697 */
4698int __init cgroup_init_early(void)
4699{
4700 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004701 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004702 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004703 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004704 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004705 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004706 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004707 root_count = 1;
4708 init_task.cgroups = &init_css_set;
4709
Tejun Heo69d02062013-06-12 21:04:50 -07004710 init_cgrp_cset_link.cset = &init_css_set;
4711 init_cgrp_cset_link.cgrp = dummytop;
4712 list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links);
4713 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004714
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004715 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004716 struct cgroup_subsys *ss = subsys[i];
4717
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004718 /* at bootup time, we don't worry about modular subsystems */
4719 if (!ss || ss->module)
4720 continue;
4721
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 BUG_ON(!ss->name);
4723 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004724 BUG_ON(!ss->css_alloc);
4725 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004726 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004727 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004728 ss->name, ss->subsys_id);
4729 BUG();
4730 }
4731
4732 if (ss->early_init)
4733 cgroup_init_subsys(ss);
4734 }
4735 return 0;
4736}
4737
4738/**
Li Zefana043e3b2008-02-23 15:24:09 -08004739 * cgroup_init - cgroup initialization
4740 *
4741 * Register cgroup filesystem and /proc file, and initialize
4742 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743 */
4744int __init cgroup_init(void)
4745{
4746 int err;
4747 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004748 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004749
4750 err = bdi_init(&cgroup_backing_dev_info);
4751 if (err)
4752 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004753
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004754 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004755 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004756
4757 /* at bootup time, we don't worry about modular subsystems */
4758 if (!ss || ss->module)
4759 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004760 if (!ss->early_init)
4761 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004762 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004763 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004764 }
4765
Li Zefan472b1052008-04-29 01:00:11 -07004766 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004767 key = css_set_hash(init_css_set.subsys);
4768 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004769
4770 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004771 mutex_lock(&cgroup_mutex);
4772 mutex_lock(&cgroup_root_mutex);
4773
Tejun Heofa3ca072013-04-14 11:36:56 -07004774 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004775
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004776 mutex_unlock(&cgroup_root_mutex);
4777 mutex_unlock(&cgroup_mutex);
4778
Greg KH676db4a2010-08-05 13:53:35 -07004779 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4780 if (!cgroup_kobj) {
4781 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004782 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004783 }
4784
4785 err = register_filesystem(&cgroup_fs_type);
4786 if (err < 0) {
4787 kobject_put(cgroup_kobj);
4788 goto out;
4789 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004790
Li Zefan46ae2202008-04-29 01:00:08 -07004791 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004792
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793out:
Paul Menagea4243162007-10-18 23:39:35 -07004794 if (err)
4795 bdi_destroy(&cgroup_backing_dev_info);
4796
Paul Menageddbcc7e2007-10-18 23:39:30 -07004797 return err;
4798}
Paul Menageb4f48b62007-10-18 23:39:33 -07004799
Paul Menagea4243162007-10-18 23:39:35 -07004800/*
4801 * proc_cgroup_show()
4802 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4803 * - Used for /proc/<pid>/cgroup.
4804 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4805 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004806 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004807 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4808 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4809 * cgroup to top_cgroup.
4810 */
4811
4812/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004813int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004814{
4815 struct pid *pid;
4816 struct task_struct *tsk;
4817 char *buf;
4818 int retval;
4819 struct cgroupfs_root *root;
4820
4821 retval = -ENOMEM;
4822 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4823 if (!buf)
4824 goto out;
4825
4826 retval = -ESRCH;
4827 pid = m->private;
4828 tsk = get_pid_task(pid, PIDTYPE_PID);
4829 if (!tsk)
4830 goto out_free;
4831
4832 retval = 0;
4833
4834 mutex_lock(&cgroup_mutex);
4835
Li Zefane5f6a862009-01-07 18:07:41 -08004836 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004837 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004838 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004839 int count = 0;
4840
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004841 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004842 for_each_subsys(root, ss)
4843 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004844 if (strlen(root->name))
4845 seq_printf(m, "%sname=%s", count ? "," : "",
4846 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004847 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004848 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004849 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004850 if (retval < 0)
4851 goto out_unlock;
4852 seq_puts(m, buf);
4853 seq_putc(m, '\n');
4854 }
4855
4856out_unlock:
4857 mutex_unlock(&cgroup_mutex);
4858 put_task_struct(tsk);
4859out_free:
4860 kfree(buf);
4861out:
4862 return retval;
4863}
4864
Paul Menagea4243162007-10-18 23:39:35 -07004865/* Display information about each subsystem and each hierarchy */
4866static int proc_cgroupstats_show(struct seq_file *m, void *v)
4867{
4868 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004869
Paul Menage8bab8dd2008-04-04 14:29:57 -07004870 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004871 /*
4872 * ideally we don't want subsystems moving around while we do this.
4873 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4874 * subsys/hierarchy state.
4875 */
Paul Menagea4243162007-10-18 23:39:35 -07004876 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004877 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4878 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004879 if (ss == NULL)
4880 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004881 seq_printf(m, "%s\t%d\t%d\t%d\n",
4882 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004883 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004884 }
4885 mutex_unlock(&cgroup_mutex);
4886 return 0;
4887}
4888
4889static int cgroupstats_open(struct inode *inode, struct file *file)
4890{
Al Viro9dce07f2008-03-29 03:07:28 +00004891 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004892}
4893
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004894static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004895 .open = cgroupstats_open,
4896 .read = seq_read,
4897 .llseek = seq_lseek,
4898 .release = single_release,
4899};
4900
Paul Menageb4f48b62007-10-18 23:39:33 -07004901/**
4902 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004903 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004904 *
4905 * Description: A task inherits its parent's cgroup at fork().
4906 *
4907 * A pointer to the shared css_set was automatically copied in
4908 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004909 * it was not made under the protection of RCU or cgroup_mutex, so
4910 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4911 * have already changed current->cgroups, allowing the previously
4912 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004913 *
4914 * At the point that cgroup_fork() is called, 'current' is the parent
4915 * task, and the passed argument 'child' points to the child task.
4916 */
4917void cgroup_fork(struct task_struct *child)
4918{
Tejun Heo9bb71302012-10-18 17:52:07 -07004919 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004920 child->cgroups = current->cgroups;
4921 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004922 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004923 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004924}
4925
4926/**
Li Zefana043e3b2008-02-23 15:24:09 -08004927 * cgroup_post_fork - called on a new task after adding it to the task list
4928 * @child: the task in question
4929 *
Tejun Heo5edee612012-10-16 15:03:14 -07004930 * Adds the task to the list running through its css_set if necessary and
4931 * call the subsystem fork() callbacks. Has to be after the task is
4932 * visible on the task list in case we race with the first call to
4933 * cgroup_iter_start() - to guarantee that the new task ends up on its
4934 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004935 */
Paul Menage817929e2007-10-18 23:39:36 -07004936void cgroup_post_fork(struct task_struct *child)
4937{
Tejun Heo5edee612012-10-16 15:03:14 -07004938 int i;
4939
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004940 /*
4941 * use_task_css_set_links is set to 1 before we walk the tasklist
4942 * under the tasklist_lock and we read it here after we added the child
4943 * to the tasklist under the tasklist_lock as well. If the child wasn't
4944 * yet in the tasklist when we walked through it from
4945 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4946 * should be visible now due to the paired locking and barriers implied
4947 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4948 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4949 * lock on fork.
4950 */
Paul Menage817929e2007-10-18 23:39:36 -07004951 if (use_task_css_set_links) {
4952 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004953 task_lock(child);
4954 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004955 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004956 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004957 write_unlock(&css_set_lock);
4958 }
Tejun Heo5edee612012-10-16 15:03:14 -07004959
4960 /*
4961 * Call ss->fork(). This must happen after @child is linked on
4962 * css_set; otherwise, @child might change state between ->fork()
4963 * and addition to css_set.
4964 */
4965 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004966 /*
4967 * fork/exit callbacks are supported only for builtin
4968 * subsystems, and the builtin section of the subsys
4969 * array is immutable, so we don't need to lock the
4970 * subsys array here. On the other hand, modular section
4971 * of the array can be freed at module unload, so we
4972 * can't touch that.
4973 */
4974 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004975 struct cgroup_subsys *ss = subsys[i];
4976
Tejun Heo5edee612012-10-16 15:03:14 -07004977 if (ss->fork)
4978 ss->fork(child);
4979 }
4980 }
Paul Menage817929e2007-10-18 23:39:36 -07004981}
Tejun Heo5edee612012-10-16 15:03:14 -07004982
Paul Menage817929e2007-10-18 23:39:36 -07004983/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004984 * cgroup_exit - detach cgroup from exiting task
4985 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004986 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004987 *
4988 * Description: Detach cgroup from @tsk and release it.
4989 *
4990 * Note that cgroups marked notify_on_release force every task in
4991 * them to take the global cgroup_mutex mutex when exiting.
4992 * This could impact scaling on very large systems. Be reluctant to
4993 * use notify_on_release cgroups where very high task exit scaling
4994 * is required on large systems.
4995 *
4996 * the_top_cgroup_hack:
4997 *
4998 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4999 *
5000 * We call cgroup_exit() while the task is still competent to
5001 * handle notify_on_release(), then leave the task attached to the
5002 * root cgroup in each hierarchy for the remainder of its exit.
5003 *
5004 * To do this properly, we would increment the reference count on
5005 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5006 * code we would add a second cgroup function call, to drop that
5007 * reference. This would just create an unnecessary hot spot on
5008 * the top_cgroup reference count, to no avail.
5009 *
5010 * Normally, holding a reference to a cgroup without bumping its
5011 * count is unsafe. The cgroup could go away, or someone could
5012 * attach us to a different cgroup, decrementing the count on
5013 * the first cgroup that we never incremented. But in this case,
5014 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005015 * which wards off any cgroup_attach_task() attempts, or task is a failed
5016 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005017 */
5018void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5019{
Tejun Heo5abb8852013-06-12 21:04:49 -07005020 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005021 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005022
5023 /*
5024 * Unlink from the css_set task list if necessary.
5025 * Optimistically check cg_list before taking
5026 * css_set_lock
5027 */
5028 if (!list_empty(&tsk->cg_list)) {
5029 write_lock(&css_set_lock);
5030 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005031 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005032 write_unlock(&css_set_lock);
5033 }
5034
Paul Menageb4f48b62007-10-18 23:39:33 -07005035 /* Reassign the task to the init_css_set. */
5036 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005037 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005038 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005039
5040 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005041 /*
5042 * fork/exit callbacks are supported only for builtin
5043 * subsystems, see cgroup_post_fork() for details.
5044 */
5045 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005046 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005047
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005048 if (ss->exit) {
5049 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005050 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005051 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005052 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005053 }
5054 }
5055 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005056 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005057
Tejun Heo5abb8852013-06-12 21:04:49 -07005058 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005059}
Paul Menage697f4162007-10-18 23:39:34 -07005060
Paul Menagebd89aab2007-10-18 23:40:44 -07005061static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005062{
Li Zefanf50daa72013-03-01 15:06:07 +08005063 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d8282013-06-12 21:04:55 -07005064 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005065 /*
5066 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005067 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005068 * it now
5069 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005070 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005071
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005072 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005073 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005074 list_empty(&cgrp->release_list)) {
5075 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005076 need_schedule_work = 1;
5077 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005078 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005079 if (need_schedule_work)
5080 schedule_work(&release_agent_work);
5081 }
5082}
5083
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005084/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005085bool __css_tryget(struct cgroup_subsys_state *css)
5086{
Tejun Heoe9316082012-11-05 09:16:58 -08005087 while (true) {
5088 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005089
Tejun Heoe9316082012-11-05 09:16:58 -08005090 v = css_refcnt(css);
5091 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5092 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005093 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005094 else if (t < 0)
5095 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005096 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005097 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005098}
5099EXPORT_SYMBOL_GPL(__css_tryget);
5100
5101/* Caller must verify that the css is not for root cgroup */
5102void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005103{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005104 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005105
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005106 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005107 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005108 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005109}
Ben Blum67523c42010-03-10 15:22:11 -08005110EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005111
5112/*
5113 * Notify userspace when a cgroup is released, by running the
5114 * configured release agent with the name of the cgroup (path
5115 * relative to the root of cgroup file system) as the argument.
5116 *
5117 * Most likely, this user command will try to rmdir this cgroup.
5118 *
5119 * This races with the possibility that some other task will be
5120 * attached to this cgroup before it is removed, or that some other
5121 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5122 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5123 * unused, and this cgroup will be reprieved from its death sentence,
5124 * to continue to serve a useful existence. Next time it's released,
5125 * we will get notified again, if it still has 'notify_on_release' set.
5126 *
5127 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5128 * means only wait until the task is successfully execve()'d. The
5129 * separate release agent task is forked by call_usermodehelper(),
5130 * then control in this thread returns here, without waiting for the
5131 * release agent task. We don't bother to wait because the caller of
5132 * this routine has no use for the exit status of the release agent
5133 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005134 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005135static void cgroup_release_agent(struct work_struct *work)
5136{
5137 BUG_ON(work != &release_agent_work);
5138 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005139 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005140 while (!list_empty(&release_list)) {
5141 char *argv[3], *envp[3];
5142 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005143 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005144 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145 struct cgroup,
5146 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005147 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005148 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005149 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005150 if (!pathbuf)
5151 goto continue_free;
5152 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5153 goto continue_free;
5154 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5155 if (!agentbuf)
5156 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005157
5158 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005159 argv[i++] = agentbuf;
5160 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005161 argv[i] = NULL;
5162
5163 i = 0;
5164 /* minimal command environment */
5165 envp[i++] = "HOME=/";
5166 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5167 envp[i] = NULL;
5168
5169 /* Drop the lock while we invoke the usermode helper,
5170 * since the exec could involve hitting disk and hence
5171 * be a slow process */
5172 mutex_unlock(&cgroup_mutex);
5173 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005174 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005175 continue_free:
5176 kfree(pathbuf);
5177 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005178 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005179 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005180 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005181 mutex_unlock(&cgroup_mutex);
5182}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005183
5184static int __init cgroup_disable(char *str)
5185{
5186 int i;
5187 char *token;
5188
5189 while ((token = strsep(&str, ",")) != NULL) {
5190 if (!*token)
5191 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005192 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005193 struct cgroup_subsys *ss = subsys[i];
5194
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005195 /*
5196 * cgroup_disable, being at boot time, can't
5197 * know about module subsystems, so we don't
5198 * worry about them.
5199 */
5200 if (!ss || ss->module)
5201 continue;
5202
Paul Menage8bab8dd2008-04-04 14:29:57 -07005203 if (!strcmp(token, ss->name)) {
5204 ss->disabled = 1;
5205 printk(KERN_INFO "Disabling %s control group"
5206 " subsystem\n", ss->name);
5207 break;
5208 }
5209 }
5210 }
5211 return 1;
5212}
5213__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005214
5215/*
5216 * Functons for CSS ID.
5217 */
5218
Tejun Heo54766d42013-06-12 21:04:53 -07005219/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005220unsigned short css_id(struct cgroup_subsys_state *css)
5221{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005222 struct css_id *cssid;
5223
5224 /*
5225 * This css_id() can return correct value when somone has refcnt
5226 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5227 * it's unchanged until freed.
5228 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005229 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005230
5231 if (cssid)
5232 return cssid->id;
5233 return 0;
5234}
Ben Blum67523c42010-03-10 15:22:11 -08005235EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005236
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005237/**
5238 * css_is_ancestor - test "root" css is an ancestor of "child"
5239 * @child: the css to be tested.
5240 * @root: the css supporsed to be an ancestor of the child.
5241 *
5242 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005243 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005244 * But, considering usual usage, the csses should be valid objects after test.
5245 * Assuming that the caller will do some action to the child if this returns
5246 * returns true, the caller must take "child";s reference count.
5247 * If "child" is valid object and this returns true, "root" is valid, too.
5248 */
5249
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005251 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005252{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005253 struct css_id *child_id;
5254 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005255
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005256 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005257 if (!child_id)
5258 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005259 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005260 if (!root_id)
5261 return false;
5262 if (child_id->depth < root_id->depth)
5263 return false;
5264 if (child_id->stack[root_id->depth] != root_id->id)
5265 return false;
5266 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005267}
5268
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005269void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5270{
5271 struct css_id *id = css->id;
5272 /* When this is called before css_id initialization, id can be NULL */
5273 if (!id)
5274 return;
5275
5276 BUG_ON(!ss->use_id);
5277
5278 rcu_assign_pointer(id->css, NULL);
5279 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005280 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005282 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005283 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005284}
Ben Blum67523c42010-03-10 15:22:11 -08005285EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005286
5287/*
5288 * This is called by init or create(). Then, calls to this function are
5289 * always serialized (By cgroup_mutex() at create()).
5290 */
5291
5292static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5293{
5294 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005295 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005296
5297 BUG_ON(!ss->use_id);
5298
5299 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5300 newid = kzalloc(size, GFP_KERNEL);
5301 if (!newid)
5302 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005303
5304 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005305 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005306 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005307 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005308 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005309 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005310
5311 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005312 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005313 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314
Tejun Heod228d9e2013-02-27 17:04:54 -08005315 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005316 newid->depth = depth;
5317 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005318err_out:
5319 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005320 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321
5322}
5323
Ben Blume6a11052010-03-10 15:22:09 -08005324static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5325 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005326{
5327 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005329 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330 idr_init(&ss->idr);
5331
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005332 newid = get_new_cssid(ss, 0);
5333 if (IS_ERR(newid))
5334 return PTR_ERR(newid);
5335
5336 newid->stack[0] = newid->id;
5337 newid->css = rootcss;
5338 rootcss->id = newid;
5339 return 0;
5340}
5341
5342static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5343 struct cgroup *child)
5344{
5345 int subsys_id, i, depth = 0;
5346 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005347 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005348
5349 subsys_id = ss->subsys_id;
5350 parent_css = parent->subsys[subsys_id];
5351 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005353 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005354
5355 child_id = get_new_cssid(ss, depth);
5356 if (IS_ERR(child_id))
5357 return PTR_ERR(child_id);
5358
5359 for (i = 0; i < depth; i++)
5360 child_id->stack[i] = parent_id->stack[i];
5361 child_id->stack[depth] = child_id->id;
5362 /*
5363 * child_id->css pointer will be set after this cgroup is available
5364 * see cgroup_populate_dir()
5365 */
5366 rcu_assign_pointer(child_css->id, child_id);
5367
5368 return 0;
5369}
5370
5371/**
5372 * css_lookup - lookup css by id
5373 * @ss: cgroup subsys to be looked into.
5374 * @id: the id
5375 *
5376 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5377 * NULL if not. Should be called under rcu_read_lock()
5378 */
5379struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5380{
5381 struct css_id *cssid = NULL;
5382
5383 BUG_ON(!ss->use_id);
5384 cssid = idr_find(&ss->idr, id);
5385
5386 if (unlikely(!cssid))
5387 return NULL;
5388
5389 return rcu_dereference(cssid->css);
5390}
Ben Blum67523c42010-03-10 15:22:11 -08005391EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005392
Stephane Eraniane5d13672011-02-14 11:20:01 +02005393/*
5394 * get corresponding css from file open on cgroupfs directory
5395 */
5396struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5397{
5398 struct cgroup *cgrp;
5399 struct inode *inode;
5400 struct cgroup_subsys_state *css;
5401
Al Viro496ad9a2013-01-23 17:07:38 -05005402 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005403 /* check in cgroup filesystem dir */
5404 if (inode->i_op != &cgroup_dir_inode_operations)
5405 return ERR_PTR(-EBADF);
5406
5407 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5408 return ERR_PTR(-EINVAL);
5409
5410 /* get cgroup */
5411 cgrp = __d_cgrp(f->f_dentry);
5412 css = cgrp->subsys[id];
5413 return css ? css : ERR_PTR(-ENOENT);
5414}
5415
Paul Menagefe693432009-09-23 15:56:20 -07005416#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005417static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005418{
5419 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5420
5421 if (!css)
5422 return ERR_PTR(-ENOMEM);
5423
5424 return css;
5425}
5426
Tejun Heo92fb9742012-11-19 08:13:38 -08005427static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005428{
5429 kfree(cont->subsys[debug_subsys_id]);
5430}
5431
Paul Menagefe693432009-09-23 15:56:20 -07005432static u64 debug_taskcount_read(struct cgroup *cont, struct cftype *cft)
5433{
5434 return cgroup_task_count(cont);
5435}
5436
5437static u64 current_css_set_read(struct cgroup *cont, struct cftype *cft)
5438{
5439 return (u64)(unsigned long)current->cgroups;
5440}
5441
5442static u64 current_css_set_refcount_read(struct cgroup *cont,
5443 struct cftype *cft)
5444{
5445 u64 count;
5446
5447 rcu_read_lock();
5448 count = atomic_read(&current->cgroups->refcount);
5449 rcu_read_unlock();
5450 return count;
5451}
5452
Paul Menage7717f7b2009-09-23 15:56:22 -07005453static int current_css_set_cg_links_read(struct cgroup *cont,
5454 struct cftype *cft,
5455 struct seq_file *seq)
5456{
Tejun Heo69d02062013-06-12 21:04:50 -07005457 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005458 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005459
5460 read_lock(&css_set_lock);
5461 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005462 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005463 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005464 struct cgroup *c = link->cgrp;
5465 const char *name;
5466
5467 if (c->dentry)
5468 name = c->dentry->d_name.name;
5469 else
5470 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005471 seq_printf(seq, "Root %d group %s\n",
5472 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005473 }
5474 rcu_read_unlock();
5475 read_unlock(&css_set_lock);
5476 return 0;
5477}
5478
5479#define MAX_TASKS_SHOWN_PER_CSS 25
5480static int cgroup_css_links_read(struct cgroup *cont,
5481 struct cftype *cft,
5482 struct seq_file *seq)
5483{
Tejun Heo69d02062013-06-12 21:04:50 -07005484 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005485
5486 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07005487 list_for_each_entry(link, &cont->cset_links, cset_link) {
5488 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005489 struct task_struct *task;
5490 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005491 seq_printf(seq, "css_set %p\n", cset);
5492 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005493 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5494 seq_puts(seq, " ...\n");
5495 break;
5496 } else {
5497 seq_printf(seq, " task %d\n",
5498 task_pid_vnr(task));
5499 }
5500 }
5501 }
5502 read_unlock(&css_set_lock);
5503 return 0;
5504}
5505
Paul Menagefe693432009-09-23 15:56:20 -07005506static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5507{
5508 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5509}
5510
5511static struct cftype debug_files[] = {
5512 {
Paul Menagefe693432009-09-23 15:56:20 -07005513 .name = "taskcount",
5514 .read_u64 = debug_taskcount_read,
5515 },
5516
5517 {
5518 .name = "current_css_set",
5519 .read_u64 = current_css_set_read,
5520 },
5521
5522 {
5523 .name = "current_css_set_refcount",
5524 .read_u64 = current_css_set_refcount_read,
5525 },
5526
5527 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005528 .name = "current_css_set_cg_links",
5529 .read_seq_string = current_css_set_cg_links_read,
5530 },
5531
5532 {
5533 .name = "cgroup_css_links",
5534 .read_seq_string = cgroup_css_links_read,
5535 },
5536
5537 {
Paul Menagefe693432009-09-23 15:56:20 -07005538 .name = "releasable",
5539 .read_u64 = releasable_read,
5540 },
Paul Menagefe693432009-09-23 15:56:20 -07005541
Tejun Heo4baf6e32012-04-01 12:09:55 -07005542 { } /* terminate */
5543};
Paul Menagefe693432009-09-23 15:56:20 -07005544
5545struct cgroup_subsys debug_subsys = {
5546 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005547 .css_alloc = debug_css_alloc,
5548 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005549 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005550 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005551};
5552#endif /* CONFIG_CGROUP_DEBUG */