blob: ef97bd0cd5460e6970c3b5e6a84d228b2174a7cb [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 Heobdc71192013-05-24 10:55:38 +0900229static inline bool cgroup_is_removed(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700230{
Paul Menagebd89aab2007-10-18 23:40:44 -0700231 return test_bit(CGRP_REMOVED, &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);
303 if (cgroup_is_removed(cgrp)) {
304 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 Gleixnercdcc136f2009-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
410 /*
411 * We may not be holding cgroup_mutex, and if cgrp->count is
412 * dropped to 0 the cgroup can be destroyed at any time, hence
413 * rcu_read_lock is used to keep it alive.
414 */
415 rcu_read_lock();
Paul Menagebd89aab2007-10-18 23:40:44 -0700416 if (atomic_dec_and_test(&cgrp->count) &&
417 notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700419 set_bit(CGRP_RELEASABLE, &cgrp->flags);
420 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700421 }
Li Zefan71b57072013-01-24 14:43:28 +0800422 rcu_read_unlock();
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700423
424 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700425 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700426
427 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700428 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700429}
430
431/*
432 * refcounted get/put for css_set objects
433 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700434static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700435{
Tejun Heo5abb8852013-06-12 21:04:49 -0700436 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700437}
438
Tejun Heo5abb8852013-06-12 21:04:49 -0700439static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700440{
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
Tejun Heo5abb8852013-06-12 21:04:49 -0700444static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700445{
Tejun Heo5abb8852013-06-12 21:04:49 -0700446 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700447}
448
Paul Menage817929e2007-10-18 23:39:36 -0700449/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700450 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700451 * @cset: candidate css_set being tested
452 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700453 * @new_cgrp: cgroup that's being entered by the task
454 * @template: desired set of css pointers in css_set (pre-calculated)
455 *
456 * Returns true if "cg" matches "old_cg" except for the hierarchy
457 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
458 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700459static bool compare_css_sets(struct css_set *cset,
460 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700461 struct cgroup *new_cgrp,
462 struct cgroup_subsys_state *template[])
463{
464 struct list_head *l1, *l2;
465
Tejun Heo5abb8852013-06-12 21:04:49 -0700466 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 /* Not all subsystems matched */
468 return false;
469 }
470
471 /*
472 * Compare cgroup pointers in order to distinguish between
473 * different cgroups in heirarchies with no subsystems. We
474 * could get by with just this check alone (and skip the
475 * memcmp above) but on most setups the memcmp check will
476 * avoid the need for this more expensive check on almost all
477 * candidates.
478 */
479
Tejun Heo69d02062013-06-12 21:04:50 -0700480 l1 = &cset->cgrp_links;
481 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700482 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700483 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700484 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700485
486 l1 = l1->next;
487 l2 = l2->next;
488 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700489 if (l1 == &cset->cgrp_links) {
490 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700491 break;
492 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700493 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700494 }
495 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700496 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
497 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
498 cgrp1 = link1->cgrp;
499 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700500 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700501 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700502
503 /*
504 * If this hierarchy is the hierarchy of the cgroup
505 * that's changing, then we need to check that this
506 * css_set points to the new cgroup; if it's any other
507 * hierarchy, then this css_set should point to the
508 * same cgroup as the old css_set.
509 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700510 if (cgrp1->root == new_cgrp->root) {
511 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700512 return false;
513 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700515 return false;
516 }
517 }
518 return true;
519}
520
521/*
Paul Menage817929e2007-10-18 23:39:36 -0700522 * find_existing_css_set() is a helper for
523 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700524 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700525 *
526 * oldcg: the cgroup group that we're using before the cgroup
527 * transition
528 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700529 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700530 *
531 * template: location in which to build the desired set of subsystem
532 * state objects for the new cgroup group
533 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700534static struct css_set *find_existing_css_set(struct css_set *old_cset,
535 struct cgroup *cgrp,
536 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700537{
538 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700539 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800541 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700542
Ben Blumaae8aab2010-03-10 15:22:07 -0800543 /*
544 * Build the set of subsystem state objects that we want to see in the
545 * new css_set. while subsystems can change globally, the entries here
546 * won't change, so no need for locking.
547 */
Paul Menage817929e2007-10-18 23:39:36 -0700548 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400549 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700550 /* Subsystem is in this hierarchy. So we want
551 * the subsystem state from the new
552 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700553 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700554 } else {
555 /* Subsystem is not in this hierarchy, so we
556 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700557 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700558 }
559 }
560
Li Zefan0ac801f2013-01-10 11:49:27 +0800561 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700562 hash_for_each_possible(css_set_table, cset, hlist, key) {
563 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700564 continue;
565
566 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700567 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700568 }
Paul Menage817929e2007-10-18 23:39:36 -0700569
570 /* No existing cgroup group matched */
571 return NULL;
572}
573
Tejun Heo69d02062013-06-12 21:04:50 -0700574static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700575{
Tejun Heo69d02062013-06-12 21:04:50 -0700576 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700577
Tejun Heo69d02062013-06-12 21:04:50 -0700578 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
579 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700580 kfree(link);
581 }
582}
583
Tejun Heo69d02062013-06-12 21:04:50 -0700584/**
585 * allocate_cgrp_cset_links - allocate cgrp_cset_links
586 * @count: the number of links to allocate
587 * @tmp_links: list_head the allocated links are put on
588 *
589 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
590 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700591 */
Tejun Heo69d02062013-06-12 21:04:50 -0700592static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700593{
Tejun Heo69d02062013-06-12 21:04:50 -0700594 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700595 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700596
597 INIT_LIST_HEAD(tmp_links);
598
Li Zefan36553432008-07-29 22:33:19 -0700599 for (i = 0; i < count; i++) {
600 link = kmalloc(sizeof(*link), GFP_KERNEL);
601 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700602 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700603 return -ENOMEM;
604 }
Tejun Heo69d02062013-06-12 21:04:50 -0700605 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700606 }
607 return 0;
608}
609
Li Zefanc12f65d2009-01-07 18:07:42 -0800610/**
611 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700612 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700613 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800614 * @cgrp: the destination cgroup
615 */
Tejun Heo69d02062013-06-12 21:04:50 -0700616static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
617 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800618{
Tejun Heo69d02062013-06-12 21:04:50 -0700619 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800620
Tejun Heo69d02062013-06-12 21:04:50 -0700621 BUG_ON(list_empty(tmp_links));
622 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
623 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700624 link->cgrp = cgrp;
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700625 atomic_inc(&cgrp->count);
Tejun Heo69d02062013-06-12 21:04:50 -0700626 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700627 /*
628 * Always add links to the tail of the list so that the list
629 * is sorted by order of hierarchy creation
630 */
Tejun Heo69d02062013-06-12 21:04:50 -0700631 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800632}
633
Li Zefan36553432008-07-29 22:33:19 -0700634/*
Paul Menage817929e2007-10-18 23:39:36 -0700635 * find_css_set() takes an existing cgroup group and a
636 * cgroup object, and returns a css_set object that's
637 * equivalent to the old group, but with the given cgroup
638 * substituted into the appropriate hierarchy. Must be called with
639 * cgroup_mutex held
640 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700641static struct css_set *find_css_set(struct css_set *old_cset,
642 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700643{
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700645 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700646 struct list_head tmp_links;
647 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800648 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700649
Paul Menage817929e2007-10-18 23:39:36 -0700650 /* First see if we already have a cgroup group that matches
651 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700652 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 cset = find_existing_css_set(old_cset, cgrp, template);
654 if (cset)
655 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700656 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700657
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 if (cset)
659 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700660
Tejun Heo5abb8852013-06-12 21:04:49 -0700661 cset = kmalloc(sizeof(*cset), GFP_KERNEL);
662 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700663 return NULL;
664
Tejun Heo69d02062013-06-12 21:04:50 -0700665 /* Allocate all the cgrp_cset_link objects that we'll need */
666 if (allocate_cgrp_cset_links(root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700668 return NULL;
669 }
670
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700672 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700673 INIT_LIST_HEAD(&cset->tasks);
674 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700675
676 /* Copy the set of subsystem state objects generated in
677 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700679
680 write_lock(&css_set_lock);
681 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700682 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700683 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700684
Paul Menage7717f7b2009-09-23 15:56:22 -0700685 if (c->root == cgrp->root)
686 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700687 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700688 }
Paul Menage817929e2007-10-18 23:39:36 -0700689
Tejun Heo69d02062013-06-12 21:04:50 -0700690 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700691
Paul Menage817929e2007-10-18 23:39:36 -0700692 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700693
694 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700695 key = css_set_hash(cset->subsys);
696 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700697
Paul Menage817929e2007-10-18 23:39:36 -0700698 write_unlock(&css_set_lock);
699
Tejun Heo5abb8852013-06-12 21:04:49 -0700700 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700701}
702
Paul Menageddbcc7e2007-10-18 23:39:30 -0700703/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 * Return the cgroup for "task" from the given hierarchy. Must be
705 * called with cgroup_mutex held.
706 */
707static struct cgroup *task_cgroup_from_root(struct task_struct *task,
708 struct cgroupfs_root *root)
709{
Tejun Heo5abb8852013-06-12 21:04:49 -0700710 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 struct cgroup *res = NULL;
712
713 BUG_ON(!mutex_is_locked(&cgroup_mutex));
714 read_lock(&css_set_lock);
715 /*
716 * No need to lock the task - since we hold cgroup_mutex the
717 * task can't change groups, so the only thing that can happen
718 * is that it exits and its css is set back to init_css_set.
719 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700720 cset = task->cgroups;
721 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700722 res = &root->top_cgroup;
723 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700724 struct cgrp_cset_link *link;
725
726 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700727 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700728
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 if (c->root == root) {
730 res = c;
731 break;
732 }
733 }
734 }
735 read_unlock(&css_set_lock);
736 BUG_ON(!res);
737 return res;
738}
739
740/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700741 * There is one global cgroup mutex. We also require taking
742 * task_lock() when dereferencing a task's cgroup subsys pointers.
743 * See "The task_lock() exception", at the end of this comment.
744 *
745 * A task must hold cgroup_mutex to modify cgroups.
746 *
747 * Any task can increment and decrement the count field without lock.
748 * So in general, code holding cgroup_mutex can't rely on the count
749 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800750 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * means that no tasks are currently attached, therefore there is no
752 * way a task attached to that cgroup can fork (the other way to
753 * increment the count). So code holding cgroup_mutex can safely
754 * assume that if the count is zero, it will stay zero. Similarly, if
755 * a task holds cgroup_mutex on a cgroup with zero count, it
756 * knows that the cgroup won't be removed, as cgroup_rmdir()
757 * needs that mutex.
758 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
760 * (usually) take cgroup_mutex. These are the two most performance
761 * critical pieces of code here. The exception occurs on cgroup_exit(),
762 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
763 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800764 * to the release agent with the name of the cgroup (path relative to
765 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 *
767 * A cgroup can only be deleted if both its 'count' of using tasks
768 * is zero, and its list of 'children' cgroups is empty. Since all
769 * tasks in the system use _some_ cgroup, and since there is always at
770 * least one task in the system (init, pid == 1), therefore, top_cgroup
771 * always has either children cgroups and/or using tasks. So we don't
772 * need a special hack to ensure that top_cgroup cannot be deleted.
773 *
774 * The task_lock() exception
775 *
776 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800777 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800778 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779 * several performance critical places that need to reference
780 * task->cgroup without the expense of grabbing a system global
781 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800782 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700783 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
784 * the task_struct routinely used for such matters.
785 *
786 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800787 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 */
789
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790/*
791 * A couple of forward declarations required, due to cyclic reference loop:
792 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
793 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
794 * -> cgroup_mkdir.
795 */
796
Al Viro18bb1db2011-07-26 01:41:39 -0400797static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400798static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400800static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
801 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700802static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700803static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700804
805static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200806 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700807 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700808};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700810static int alloc_css_id(struct cgroup_subsys *ss,
811 struct cgroup *parent, struct cgroup *child);
812
Al Viroa5e7ed32011-07-26 01:55:55 -0400813static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814{
815 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816
817 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400818 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100820 inode->i_uid = current_fsuid();
821 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
823 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
824 }
825 return inode;
826}
827
Li Zefan65dff752013-03-01 15:01:56 +0800828static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
829{
830 struct cgroup_name *name;
831
832 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
833 if (!name)
834 return NULL;
835 strcpy(name->name, dentry->d_name.name);
836 return name;
837}
838
Li Zefanbe445622013-01-24 14:31:42 +0800839static void cgroup_free_fn(struct work_struct *work)
840{
841 struct cgroup *cgrp = container_of(work, struct cgroup, free_work);
842 struct cgroup_subsys *ss;
843
844 mutex_lock(&cgroup_mutex);
845 /*
846 * Release the subsystem state objects.
847 */
848 for_each_subsys(cgrp->root, ss)
849 ss->css_free(cgrp);
850
851 cgrp->root->number_of_cgroups--;
852 mutex_unlock(&cgroup_mutex);
853
854 /*
Li Zefan415cf072013-04-08 14:35:02 +0800855 * We get a ref to the parent's dentry, and put the ref when
856 * this cgroup is being freed, so it's guaranteed that the
857 * parent won't be destroyed before its children.
858 */
859 dput(cgrp->parent->dentry);
860
Li Zefancc20e012013-04-26 11:58:02 -0700861 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
862
Li Zefan415cf072013-04-08 14:35:02 +0800863 /*
Li Zefanbe445622013-01-24 14:31:42 +0800864 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700865 * created the cgroup. This will free cgrp->root, if we are
866 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800867 */
868 deactivate_super(cgrp->root->sb);
869
870 /*
871 * if we're getting rid of the cgroup, refcount should ensure
872 * that there are no pidlists left.
873 */
874 BUG_ON(!list_empty(&cgrp->pidlists));
875
876 simple_xattrs_free(&cgrp->xattrs);
877
Li Zefan65dff752013-03-01 15:01:56 +0800878 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800879 kfree(cgrp);
880}
881
882static void cgroup_free_rcu(struct rcu_head *head)
883{
884 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
885
886 schedule_work(&cgrp->free_work);
887}
888
Paul Menageddbcc7e2007-10-18 23:39:30 -0700889static void cgroup_diput(struct dentry *dentry, struct inode *inode)
890{
891 /* is dentry a directory ? if so, kfree() associated cgroup */
892 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700893 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800894
Paul Menagebd89aab2007-10-18 23:40:44 -0700895 BUG_ON(!(cgroup_is_removed(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800896 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700897 } else {
898 struct cfent *cfe = __d_cfe(dentry);
899 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
900
901 WARN_ONCE(!list_empty(&cfe->node) &&
902 cgrp != &cgrp->root->top_cgroup,
903 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700904 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700905 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700906 }
907 iput(inode);
908}
909
Al Viroc72a04e2011-01-14 05:31:45 +0000910static int cgroup_delete(const struct dentry *d)
911{
912 return 1;
913}
914
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915static void remove_dir(struct dentry *d)
916{
917 struct dentry *parent = dget(d->d_parent);
918
919 d_delete(d);
920 simple_rmdir(parent->d_inode, d);
921 dput(parent);
922}
923
Li Zefan2739d3c2013-01-21 18:18:33 +0800924static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700927
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
929 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100930
Li Zefan2739d3c2013-01-21 18:18:33 +0800931 /*
932 * If we're doing cleanup due to failure of cgroup_create(),
933 * the corresponding @cfe may not exist.
934 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 list_for_each_entry(cfe, &cgrp->files, node) {
936 struct dentry *d = cfe->dentry;
937
938 if (cft && cfe->type != cft)
939 continue;
940
941 dget(d);
942 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700943 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_del_init(&cfe->node);
945 dput(d);
946
Li Zefan2739d3c2013-01-21 18:18:33 +0800947 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700948 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949}
950
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951/**
952 * cgroup_clear_directory - selective removal of base and subsystem files
953 * @dir: directory containing the files
954 * @base_files: true if the base files should be removed
955 * @subsys_mask: mask of the subsystem ids whose files should be removed
956 */
957static void cgroup_clear_directory(struct dentry *dir, bool base_files,
958 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959{
960 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700962
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 for_each_subsys(cgrp->root, ss) {
964 struct cftype_set *set;
965 if (!test_bit(ss->subsys_id, &subsys_mask))
966 continue;
967 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800968 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 }
970 if (base_files) {
971 while (!list_empty(&cgrp->files))
972 cgroup_rm_file(cgrp, NULL);
973 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974}
975
976/*
977 * NOTE : the dentry must have been dget()'ed
978 */
979static void cgroup_d_remove_dir(struct dentry *dentry)
980{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100981 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400982 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400984 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100986 parent = dentry->d_parent;
987 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800988 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100990 spin_unlock(&dentry->d_lock);
991 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 remove_dir(dentry);
993}
994
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700995/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800996 * Call with cgroup_mutex held. Drops reference counts on modules, including
997 * any duplicate ones that parse_cgroupfs_options took. If this function
998 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800999 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000static int rebind_subsystems(struct cgroupfs_root *root,
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001001 unsigned long final_subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002{
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001003 unsigned long added_mask, removed_mask;
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 int i;
1006
Ben Blumaae8aab2010-03-10 15:22:07 -08001007 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001008 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001009
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001010 removed_mask = root->actual_subsys_mask & ~final_subsys_mask;
1011 added_mask = final_subsys_mask & ~root->actual_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 /* Check that any added subsystems are currently free */
1013 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -08001014 unsigned long bit = 1UL << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 struct cgroup_subsys *ss = subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001016 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001018 /*
1019 * Nobody should tell us to do a subsys that doesn't exist:
1020 * parse_cgroupfs_options should catch that case and refcounts
1021 * ensure that subsystems won't disappear once selected.
1022 */
1023 BUG_ON(ss == NULL);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 if (ss->root != &rootnode) {
1025 /* Subsystem isn't free */
1026 return -EBUSY;
1027 }
1028 }
1029
1030 /* Currently we don't handle adding/removing subsystems when
1031 * any child cgroups exist. This is theoretically supportable
1032 * but involves complex error handling, so it's being left until
1033 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001034 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 return -EBUSY;
1036
1037 /* Process each subsystem */
1038 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1039 struct cgroup_subsys *ss = subsys[i];
1040 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001041 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001043 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 BUG_ON(!dummytop->subsys[i]);
1046 BUG_ON(dummytop->subsys[i]->cgroup != dummytop);
Paul Menagebd89aab2007-10-18 23:40:44 -07001047 cgrp->subsys[i] = dummytop->subsys[i];
1048 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001049 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001050 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001052 ss->bind(cgrp);
Ben Blumcf5d5942010-03-10 15:22:09 -08001053 /* refcount was already taken, and we're keeping it */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001054 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001056 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 BUG_ON(cgrp->subsys[i] != dummytop->subsys[i]);
1058 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 if (ss->bind)
Li Zefan761b3ef52012-01-31 13:47:36 +08001060 ss->bind(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 dummytop->subsys[i]->cgroup = dummytop;
Paul Menagebd89aab2007-10-18 23:40:44 -07001062 cgrp->subsys[i] = NULL;
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001063 subsys[i]->root = &rootnode;
Li Zefan33a68ac2009-01-07 18:07:42 -08001064 list_move(&ss->sibling, &rootnode.subsys_list);
Ben Blumcf5d5942010-03-10 15:22:09 -08001065 /* subsystem is now free - drop reference on module */
1066 module_put(ss->module);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001067 } else if (bit & final_subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001069 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001070 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001071 /*
1072 * a refcount was taken, but we already had one, so
1073 * drop the extra reference.
1074 */
1075 module_put(ss->module);
1076#ifdef CONFIG_MODULE_UNLOAD
1077 BUG_ON(ss->module && !module_refcount(ss->module));
1078#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 } else {
1080 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001081 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 }
1083 }
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001084 root->subsys_mask = root->actual_subsys_mask = final_subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085
1086 return 0;
1087}
1088
Al Viro34c80b12011-12-08 21:32:45 -05001089static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090{
Al Viro34c80b12011-12-08 21:32:45 -05001091 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 struct cgroup_subsys *ss;
1093
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001094 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 for_each_subsys(root, ss)
1096 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001097 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1098 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001099 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001101 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001102 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 if (strlen(root->release_agent_path))
1104 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001105 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001106 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001107 if (strlen(root->name))
1108 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001109 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 return 0;
1111}
1112
1113struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001114 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001116 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001117 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001119 /* User explicitly requested empty subsystem */
1120 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121
1122 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124};
1125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126/*
1127 * Convert a hierarchy specifier into a bitmask of subsystems and flags. Call
Ben Blumcf5d5942010-03-10 15:22:09 -08001128 * with cgroup_mutex held to protect the subsys[] array. This function takes
1129 * refcounts on subsystems to be used, unless it returns error, in which case
1130 * no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001131 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001132static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001134 char *token, *o = data;
1135 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001137 int i;
1138 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1141
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142#ifdef CONFIG_CPUSETS
1143 mask = ~(1UL << cpuset_subsys_id);
1144#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001145
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147
1148 while ((token = strsep(&o, ",")) != NULL) {
1149 if (!*token)
1150 return -EINVAL;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001151 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001152 /* Explicitly have no subsystems */
1153 opts->none = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001154 continue;
1155 }
1156 if (!strcmp(token, "all")) {
1157 /* Mutually exclusive option 'all' + subsystem name */
1158 if (one_ss)
1159 return -EINVAL;
1160 all_ss = true;
1161 continue;
1162 }
Tejun Heo873fe092013-04-14 20:15:26 -07001163 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1164 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1165 continue;
1166 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001167 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001168 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001169 continue;
1170 }
1171 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001172 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001173 continue;
1174 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001175 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001176 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001177 continue;
1178 }
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001179 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001180 /* Specifying two release agents is forbidden */
1181 if (opts->release_agent)
1182 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001183 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001184 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001185 if (!opts->release_agent)
1186 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001187 continue;
1188 }
1189 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001190 const char *name = token + 5;
1191 /* Can't specify an empty name */
1192 if (!strlen(name))
1193 return -EINVAL;
1194 /* Must match [\w.-]+ */
1195 for (i = 0; i < strlen(name); i++) {
1196 char c = name[i];
1197 if (isalnum(c))
1198 continue;
1199 if ((c == '.') || (c == '-') || (c == '_'))
1200 continue;
1201 return -EINVAL;
1202 }
1203 /* Specifying two names is forbidden */
1204 if (opts->name)
1205 return -EINVAL;
1206 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001207 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001208 GFP_KERNEL);
1209 if (!opts->name)
1210 return -ENOMEM;
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001211
1212 continue;
1213 }
1214
1215 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1216 struct cgroup_subsys *ss = subsys[i];
1217 if (ss == NULL)
1218 continue;
1219 if (strcmp(token, ss->name))
1220 continue;
1221 if (ss->disabled)
1222 continue;
1223
1224 /* Mutually exclusive option 'all' + subsystem name */
1225 if (all_ss)
1226 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001227 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001228 one_ss = true;
1229
1230 break;
1231 }
1232 if (i == CGROUP_SUBSYS_COUNT)
1233 return -ENOENT;
1234 }
1235
1236 /*
1237 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001238 * otherwise if 'none', 'name=' and a subsystem name options
1239 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001240 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001241 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf232010-10-27 15:33:37 -07001242 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
1243 struct cgroup_subsys *ss = subsys[i];
1244 if (ss == NULL)
1245 continue;
1246 if (ss->disabled)
1247 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001248 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001249 }
1250 }
1251
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001252 /* Consistency checks */
1253
Tejun Heo873fe092013-04-14 20:15:26 -07001254 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1255 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1256
1257 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1258 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1259 return -EINVAL;
1260 }
1261
1262 if (opts->cpuset_clone_children) {
1263 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1264 return -EINVAL;
1265 }
1266 }
1267
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 /*
1269 * Option noprefix was introduced just for backward compatibility
1270 * with the old cpuset, so we allow noprefix only if mounting just
1271 * the cpuset subsystem.
1272 */
Tejun Heo93438622013-04-14 20:15:25 -07001273 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001274 return -EINVAL;
1275
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001276
1277 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001278 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001279 return -EINVAL;
1280
1281 /*
1282 * We either have to specify by name or by subsystems. (So all
1283 * empty hierarchies must have a name).
1284 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001285 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 return -EINVAL;
1287
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 /*
1289 * Grab references on all the modules we'll need, so the subsystems
1290 * don't dance around before rebind_subsystems attaches them. This may
1291 * take duplicate reference counts on a subsystem that's already used,
1292 * but rebind_subsystems handles this case.
1293 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001294 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001295 unsigned long bit = 1UL << i;
1296
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001297 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 continue;
1299 if (!try_module_get(subsys[i]->module)) {
1300 module_pin_failed = true;
1301 break;
1302 }
1303 }
1304 if (module_pin_failed) {
1305 /*
1306 * oops, one of the modules was going away. this means that we
1307 * raced with a module_delete call, and to the user this is
1308 * essentially a "subsystem doesn't exist" case.
1309 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001310 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 /* drop refcounts only on the ones we took */
1312 unsigned long bit = 1UL << i;
1313
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001314 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001315 continue;
1316 module_put(subsys[i]->module);
1317 }
1318 return -ENOENT;
1319 }
1320
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321 return 0;
1322}
1323
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001324static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001325{
1326 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001327 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001328 unsigned long bit = 1UL << i;
1329
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001330 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001331 continue;
1332 module_put(subsys[i]->module);
1333 }
1334}
1335
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1337{
1338 int ret = 0;
1339 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001340 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001342 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343
Tejun Heo873fe092013-04-14 20:15:26 -07001344 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1345 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1346 return -EINVAL;
1347 }
1348
Paul Menagebd89aab2007-10-18 23:40:44 -07001349 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001351 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352
1353 /* See what subsystems are wanted */
1354 ret = parse_cgroupfs_options(data, &opts);
1355 if (ret)
1356 goto out_unlock;
1357
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001358 if (opts.subsys_mask != root->actual_subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001359 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1360 task_tgid_nr(current), current->comm);
1361
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001362 added_mask = opts.subsys_mask & ~root->subsys_mask;
1363 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001364
Ben Blumcf5d5942010-03-10 15:22:09 -08001365 /* Don't allow flags or name to change at remount */
1366 if (opts.flags != root->flags ||
1367 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001369 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001370 goto out_unlock;
1371 }
1372
Gao feng7083d032012-12-03 09:28:18 +08001373 /*
1374 * Clear out the files of subsystems that should be removed, do
1375 * this before rebind_subsystems, since rebind_subsystems may
1376 * change this hierarchy's subsys_list.
1377 */
1378 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1379
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001380 ret = rebind_subsystems(root, opts.subsys_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001381 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001382 /* rebind_subsystems failed, re-populate the removed files */
1383 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001384 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001385 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001386 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001388 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001389 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390
Paul Menage81a6a5c2007-10-18 23:39:38 -07001391 if (opts.release_agent)
1392 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001394 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001395 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001396 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001398 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399 return ret;
1400}
1401
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001402static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 .statfs = simple_statfs,
1404 .drop_inode = generic_delete_inode,
1405 .show_options = cgroup_show_options,
1406 .remount_fs = cgroup_remount,
1407};
1408
Paul Menagecc31edc2008-10-18 20:28:04 -07001409static void init_cgroup_housekeeping(struct cgroup *cgrp)
1410{
1411 INIT_LIST_HEAD(&cgrp->sibling);
1412 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001413 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001414 INIT_LIST_HEAD(&cgrp->cset_links);
Tejun Heo22430762012-11-19 08:13:35 -08001415 INIT_LIST_HEAD(&cgrp->allcg_node);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001417 INIT_LIST_HEAD(&cgrp->pidlists);
Li Zefanbe445622013-01-24 14:31:42 +08001418 INIT_WORK(&cgrp->free_work, cgroup_free_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07001419 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001420 INIT_LIST_HEAD(&cgrp->event_list);
1421 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001422 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001423}
Paul Menagec6d57f32009-09-23 15:56:19 -07001424
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425static void init_cgroup_root(struct cgroupfs_root *root)
1426{
Paul Menagebd89aab2007-10-18 23:40:44 -07001427 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001428
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 INIT_LIST_HEAD(&root->subsys_list);
1430 INIT_LIST_HEAD(&root->root_list);
Tejun Heob0ca5a82012-04-01 12:09:54 -07001431 INIT_LIST_HEAD(&root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001433 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001434 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001435 init_cgroup_housekeeping(cgrp);
Li Zhongfddfb022012-11-28 17:15:21 +08001436 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001437}
1438
Tejun Heofa3ca072013-04-14 11:36:56 -07001439static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440{
Tejun Heo1a574232013-04-14 11:36:58 -07001441 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001442
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001443 lockdep_assert_held(&cgroup_mutex);
1444 lockdep_assert_held(&cgroup_root_mutex);
1445
Tejun Heo1a574232013-04-14 11:36:58 -07001446 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1447 if (id < 0)
1448 return id;
1449
1450 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001451 return 0;
1452}
1453
1454static void cgroup_exit_root_id(struct cgroupfs_root *root)
1455{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001456 lockdep_assert_held(&cgroup_mutex);
1457 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001458
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001459 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001460 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001461 root->hierarchy_id = 0;
1462 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463}
1464
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465static int cgroup_test_super(struct super_block *sb, void *data)
1466{
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001468 struct cgroupfs_root *root = sb->s_fs_info;
1469
Paul Menagec6d57f32009-09-23 15:56:19 -07001470 /* If we asked for a name then it must match */
1471 if (opts->name && strcmp(opts->name, root->name))
1472 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001474 /*
1475 * If we asked for subsystems (or explicitly for no
1476 * subsystems) then they must match
1477 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001478 if ((opts->subsys_mask || opts->none)
1479 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480 return 0;
1481
1482 return 1;
1483}
1484
Paul Menagec6d57f32009-09-23 15:56:19 -07001485static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1486{
1487 struct cgroupfs_root *root;
1488
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001489 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 return NULL;
1491
1492 root = kzalloc(sizeof(*root), GFP_KERNEL);
1493 if (!root)
1494 return ERR_PTR(-ENOMEM);
1495
1496 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001497
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001498 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001500 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 if (opts->release_agent)
1502 strcpy(root->release_agent_path, opts->release_agent);
1503 if (opts->name)
1504 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001505 if (opts->cpuset_clone_children)
1506 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 return root;
1508}
1509
Tejun Heofa3ca072013-04-14 11:36:56 -07001510static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511{
Tejun Heofa3ca072013-04-14 11:36:56 -07001512 if (root) {
1513 /* hierarhcy ID shoulid already have been released */
1514 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001515
Tejun Heofa3ca072013-04-14 11:36:56 -07001516 ida_destroy(&root->cgroup_ida);
1517 kfree(root);
1518 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001519}
1520
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521static int cgroup_set_super(struct super_block *sb, void *data)
1522{
1523 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001524 struct cgroup_sb_opts *opts = data;
1525
1526 /* If we don't have a new root, we can't set up a new sb */
1527 if (!opts->new_root)
1528 return -EINVAL;
1529
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001530 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 ret = set_anon_super(sb, NULL);
1533 if (ret)
1534 return ret;
1535
Paul Menagec6d57f32009-09-23 15:56:19 -07001536 sb->s_fs_info = opts->new_root;
1537 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
1539 sb->s_blocksize = PAGE_CACHE_SIZE;
1540 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1541 sb->s_magic = CGROUP_SUPER_MAGIC;
1542 sb->s_op = &cgroup_ops;
1543
1544 return 0;
1545}
1546
1547static int cgroup_get_rootdir(struct super_block *sb)
1548{
Al Viro0df6a632010-12-21 13:29:29 -05001549 static const struct dentry_operations cgroup_dops = {
1550 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001551 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001552 };
1553
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 struct inode *inode =
1555 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
1557 if (!inode)
1558 return -ENOMEM;
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 inode->i_fop = &simple_dir_operations;
1561 inode->i_op = &cgroup_dir_inode_operations;
1562 /* directories start off with i_nlink == 2 (for "." entry) */
1563 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001564 sb->s_root = d_make_root(inode);
1565 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001567 /* for everything else we want ->d_op set */
1568 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 return 0;
1570}
1571
Al Virof7e83572010-07-26 13:23:11 +04001572static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001574 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575{
1576 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 int ret = 0;
1579 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001581 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
1583 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001584 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001586 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 if (ret)
1588 goto out_err;
1589
1590 /*
1591 * Allocate a new cgroup root. We may not need it if we're
1592 * reusing an existing hierarchy.
1593 */
1594 new_root = cgroup_root_from_opts(&opts);
1595 if (IS_ERR(new_root)) {
1596 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001597 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001598 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001602 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001605 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001606 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 }
1608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 root = sb->s_fs_info;
1610 BUG_ON(!root);
1611 if (root == opts.new_root) {
1612 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001613 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001614 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001615 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001616 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001617 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001618 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
1620 BUG_ON(sb->s_root != NULL);
1621
1622 ret = cgroup_get_rootdir(sb);
1623 if (ret)
1624 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001625 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Paul Menage817929e2007-10-18 23:39:36 -07001627 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001629 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001631 /* Check for name clashes with existing mounts */
1632 ret = -EBUSY;
1633 if (strlen(root->name))
1634 for_each_active_root(existing_root)
1635 if (!strcmp(existing_root->name, root->name))
1636 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001637
Paul Menage817929e2007-10-18 23:39:36 -07001638 /*
1639 * We're accessing css_set_count without locking
1640 * css_set_lock here, but that's OK - it can only be
1641 * increased by someone holding cgroup_lock, and
1642 * that's us. The worst that can happen is that we
1643 * have some link structures left over
1644 */
Tejun Heo69d02062013-06-12 21:04:50 -07001645 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001646 if (ret)
1647 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001648
Tejun Heofa3ca072013-04-14 11:36:56 -07001649 ret = cgroup_init_root_id(root);
1650 if (ret)
1651 goto unlock_drop;
1652
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001653 ret = rebind_subsystems(root, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001655 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001656 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001658 /*
1659 * There must be no failure case after here, since rebinding
1660 * takes care of subsystems' refcounts, which are explicitly
1661 * dropped in the failure exit path.
1662 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663
1664 /* EBUSY should be the only error here */
1665 BUG_ON(ret);
1666
1667 list_add(&root->root_list, &roots);
Paul Menage817929e2007-10-18 23:39:36 -07001668 root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669
Li Zefanc12f65d2009-01-07 18:07:42 -08001670 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001671 root->top_cgroup.dentry = sb->s_root;
1672
Paul Menage817929e2007-10-18 23:39:36 -07001673 /* Link the top cgroup in this hierarchy into all
1674 * the css_set objects */
1675 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001676 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001677 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001678 write_unlock(&css_set_lock);
1679
Tejun Heo69d02062013-06-12 21:04:50 -07001680 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001681
Li Zefanc12f65d2009-01-07 18:07:42 -08001682 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683 BUG_ON(root->number_of_cgroups != 1);
1684
eparis@redhat2ce97382011-06-02 21:20:51 +10001685 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001686 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001687 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001688 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001690 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001691 } else {
1692 /*
1693 * We re-used an existing hierarchy - the new root (if
1694 * any) is not needed
1695 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001696 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001697
1698 if (((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) &&
1699 root->flags != opts.flags) {
1700 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1701 ret = -EINVAL;
1702 goto drop_new_super;
1703 }
1704
Ben Blumcf5d5942010-03-10 15:22:09 -08001705 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001706 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 }
1708
Paul Menagec6d57f32009-09-23 15:56:19 -07001709 kfree(opts.release_agent);
1710 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001711 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001713 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001714 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001715 mutex_unlock(&cgroup_root_mutex);
1716 mutex_unlock(&cgroup_mutex);
1717 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001719 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001720 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001721 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001722 out_err:
1723 kfree(opts.release_agent);
1724 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001725 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726}
1727
1728static void cgroup_kill_sb(struct super_block *sb) {
1729 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001730 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001731 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 int ret;
1733
1734 BUG_ON(!root);
1735
1736 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001737 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738
1739 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001740 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
1742 /* Rebind all subsystems back to the default hierarchy */
1743 ret = rebind_subsystems(root, 0);
1744 /* Shouldn't be able to fail ... */
1745 BUG_ON(ret);
1746
Paul Menage817929e2007-10-18 23:39:36 -07001747 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001748 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001749 * root cgroup
1750 */
1751 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001752
Tejun Heo69d02062013-06-12 21:04:50 -07001753 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1754 list_del(&link->cset_link);
1755 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001756 kfree(link);
1757 }
1758 write_unlock(&css_set_lock);
1759
Paul Menage839ec542009-01-29 14:25:22 -08001760 if (!list_empty(&root->root_list)) {
1761 list_del(&root->root_list);
1762 root_count--;
1763 }
Li Zefane5f6a862009-01-07 18:07:41 -08001764
Tejun Heofa3ca072013-04-14 11:36:56 -07001765 cgroup_exit_root_id(root);
1766
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001767 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 mutex_unlock(&cgroup_mutex);
1769
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001770 simple_xattrs_free(&cgrp->xattrs);
1771
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001773 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774}
1775
1776static struct file_system_type cgroup_fs_type = {
1777 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001778 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 .kill_sb = cgroup_kill_sb,
1780};
1781
Greg KH676db4a2010-08-05 13:53:35 -07001782static struct kobject *cgroup_kobj;
1783
Li Zefana043e3b2008-02-23 15:24:09 -08001784/**
1785 * cgroup_path - generate the path of a cgroup
1786 * @cgrp: the cgroup in question
1787 * @buf: the buffer to write the path into
1788 * @buflen: the length of the buffer
1789 *
Li Zefan65dff752013-03-01 15:01:56 +08001790 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1791 *
1792 * We can't generate cgroup path using dentry->d_name, as accessing
1793 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1794 * inode's i_mutex, while on the other hand cgroup_path() can be called
1795 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001797int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798{
Li Zefan65dff752013-03-01 15:01:56 +08001799 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001801
Tejun Heoda1f2962013-04-14 10:32:19 -07001802 if (!cgrp->parent) {
1803 if (strlcpy(buf, "/", buflen) >= buflen)
1804 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805 return 0;
1806 }
1807
Tao Ma316eb662012-11-08 21:36:38 +08001808 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001809 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001810
Li Zefan65dff752013-03-01 15:01:56 +08001811 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001812 do {
Li Zefan65dff752013-03-01 15:01:56 +08001813 const char *name = cgroup_name(cgrp);
1814 int len;
1815
1816 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001818 goto out;
1819 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001820
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001822 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001824
1825 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001826 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001827 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001829out:
1830 rcu_read_unlock();
1831 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001832}
Ben Blum67523c42010-03-10 15:22:11 -08001833EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001834
Tejun Heo857a2be2013-04-14 20:50:08 -07001835/**
1836 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1837 * @task: target task
1838 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1839 * @buf: the buffer to write the path into
1840 * @buflen: the length of the buffer
1841 *
1842 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1843 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1844 * be used inside locks used by cgroup controller callbacks.
1845 */
1846int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1847 char *buf, size_t buflen)
1848{
1849 struct cgroupfs_root *root;
1850 struct cgroup *cgrp = NULL;
1851 int ret = -ENOENT;
1852
1853 mutex_lock(&cgroup_mutex);
1854
1855 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1856 if (root) {
1857 cgrp = task_cgroup_from_root(task, root);
1858 ret = cgroup_path(cgrp, buf, buflen);
1859 }
1860
1861 mutex_unlock(&cgroup_mutex);
1862
1863 return ret;
1864}
1865EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1866
Ben Blum74a11662011-05-26 16:25:20 -07001867/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001868 * Control Group taskset
1869 */
Tejun Heo134d3372011-12-12 18:12:21 -08001870struct task_and_cgroup {
1871 struct task_struct *task;
1872 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001873 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001874};
1875
Tejun Heo2f7ee562011-12-12 18:12:21 -08001876struct cgroup_taskset {
1877 struct task_and_cgroup single;
1878 struct flex_array *tc_array;
1879 int tc_array_len;
1880 int idx;
1881 struct cgroup *cur_cgrp;
1882};
1883
1884/**
1885 * cgroup_taskset_first - reset taskset and return the first task
1886 * @tset: taskset of interest
1887 *
1888 * @tset iteration is initialized and the first task is returned.
1889 */
1890struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1891{
1892 if (tset->tc_array) {
1893 tset->idx = 0;
1894 return cgroup_taskset_next(tset);
1895 } else {
1896 tset->cur_cgrp = tset->single.cgrp;
1897 return tset->single.task;
1898 }
1899}
1900EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1901
1902/**
1903 * cgroup_taskset_next - iterate to the next task in taskset
1904 * @tset: taskset of interest
1905 *
1906 * Return the next task in @tset. Iteration must have been initialized
1907 * with cgroup_taskset_first().
1908 */
1909struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1910{
1911 struct task_and_cgroup *tc;
1912
1913 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1914 return NULL;
1915
1916 tc = flex_array_get(tset->tc_array, tset->idx++);
1917 tset->cur_cgrp = tc->cgrp;
1918 return tc->task;
1919}
1920EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1921
1922/**
1923 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1924 * @tset: taskset of interest
1925 *
1926 * Return the cgroup for the current (last returned) task of @tset. This
1927 * function must be preceded by either cgroup_taskset_first() or
1928 * cgroup_taskset_next().
1929 */
1930struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1931{
1932 return tset->cur_cgrp;
1933}
1934EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1935
1936/**
1937 * cgroup_taskset_size - return the number of tasks in taskset
1938 * @tset: taskset of interest
1939 */
1940int cgroup_taskset_size(struct cgroup_taskset *tset)
1941{
1942 return tset->tc_array ? tset->tc_array_len : 1;
1943}
1944EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1945
1946
Ben Blum74a11662011-05-26 16:25:20 -07001947/*
1948 * cgroup_task_migrate - move a task from one cgroup to another.
1949 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001950 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001951 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001952static void cgroup_task_migrate(struct cgroup *old_cgrp,
1953 struct task_struct *tsk,
1954 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001955{
Tejun Heo5abb8852013-06-12 21:04:49 -07001956 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001957
1958 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001959 * We are synchronized through threadgroup_lock() against PF_EXITING
1960 * setting such that we can't race against cgroup_exit() changing the
1961 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001963 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001964 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001965
Ben Blum74a11662011-05-26 16:25:20 -07001966 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001967 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001968 task_unlock(tsk);
1969
1970 /* Update the css_set linked lists if we're using them */
1971 write_lock(&css_set_lock);
1972 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001973 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001974 write_unlock(&css_set_lock);
1975
1976 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001977 * We just gained a reference on old_cset by taking it from the
1978 * task. As trading it for new_cset is protected by cgroup_mutex,
1979 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001980 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001981 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1982 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001983}
1984
Li Zefana043e3b2008-02-23 15:24:09 -08001985/**
Li Zefan081aa452013-03-13 09:17:09 +08001986 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001987 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001988 * @tsk: the task or the leader of the threadgroup to be attached
1989 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001990 *
Tejun Heo257058a2011-12-12 18:12:21 -08001991 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001992 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001993 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001994static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1995 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001996{
1997 int retval, i, group_size;
1998 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001999 struct cgroupfs_root *root = cgrp->root;
2000 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002001 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002002 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002003 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002004 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002005
2006 /*
2007 * step 0: in order to do expensive, possibly blocking operations for
2008 * every thread, we cannot iterate the thread group list, since it needs
2009 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002010 * group - group_rwsem prevents new threads from appearing, and if
2011 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002012 */
Li Zefan081aa452013-03-13 09:17:09 +08002013 if (threadgroup)
2014 group_size = get_nr_threads(tsk);
2015 else
2016 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002017 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002018 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002019 if (!group)
2020 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002021 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002022 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002023 if (retval)
2024 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002025
Ben Blum74a11662011-05-26 16:25:20 -07002026 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002027 /*
2028 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2029 * already PF_EXITING could be freed from underneath us unless we
2030 * take an rcu_read_lock.
2031 */
2032 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002033 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002034 struct task_and_cgroup ent;
2035
Tejun Heocd3d0952011-12-12 18:12:21 -08002036 /* @tsk either already exited or can't exit until the end */
2037 if (tsk->flags & PF_EXITING)
2038 continue;
2039
Ben Blum74a11662011-05-26 16:25:20 -07002040 /* as per above, nr_threads may decrease, but not increase. */
2041 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002042 ent.task = tsk;
2043 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002044 /* nothing to do if this task is already in the cgroup */
2045 if (ent.cgrp == cgrp)
2046 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002047 /*
2048 * saying GFP_ATOMIC has no effect here because we did prealloc
2049 * earlier, but it's good form to communicate our expectations.
2050 */
Tejun Heo134d3372011-12-12 18:12:21 -08002051 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002052 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002053 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002054
2055 if (!threadgroup)
2056 break;
Ben Blum74a11662011-05-26 16:25:20 -07002057 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002058 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002059 /* remember the number of threads in the array for later. */
2060 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002061 tset.tc_array = group;
2062 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002063
Tejun Heo134d3372011-12-12 18:12:21 -08002064 /* methods shouldn't be called if no task is actually migrating */
2065 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002066 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002067 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002068
Ben Blum74a11662011-05-26 16:25:20 -07002069 /*
2070 * step 1: check that we can legitimately attach to the cgroup.
2071 */
2072 for_each_subsys(root, ss) {
2073 if (ss->can_attach) {
Li Zefan761b3ef52012-01-31 13:47:36 +08002074 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002075 if (retval) {
2076 failed_ss = ss;
2077 goto out_cancel_attach;
2078 }
2079 }
Ben Blum74a11662011-05-26 16:25:20 -07002080 }
2081
2082 /*
2083 * step 2: make sure css_sets exist for all threads to be migrated.
2084 * we use find_css_set, which allocates a new one if necessary.
2085 */
Ben Blum74a11662011-05-26 16:25:20 -07002086 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002087 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002088 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2089 if (!tc->cg) {
2090 retval = -ENOMEM;
2091 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002092 }
2093 }
2094
2095 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002096 * step 3: now that we're guaranteed success wrt the css_sets,
2097 * proceed to move all tasks to the new cgroup. There are no
2098 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002099 */
Ben Blum74a11662011-05-26 16:25:20 -07002100 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002101 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002102 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002103 }
2104 /* nothing is sensitive to fork() after this point. */
2105
2106 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002107 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002108 */
2109 for_each_subsys(root, ss) {
2110 if (ss->attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002111 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002112 }
2113
2114 /*
2115 * step 5: success! and cleanup
2116 */
Ben Blum74a11662011-05-26 16:25:20 -07002117 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002118out_put_css_set_refs:
2119 if (retval) {
2120 for (i = 0; i < group_size; i++) {
2121 tc = flex_array_get(group, i);
2122 if (!tc->cg)
2123 break;
2124 put_css_set(tc->cg);
2125 }
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002128 if (retval) {
2129 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002130 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002131 break;
Ben Blum74a11662011-05-26 16:25:20 -07002132 if (ss->cancel_attach)
Li Zefan761b3ef52012-01-31 13:47:36 +08002133 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002134 }
2135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002137 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002138 return retval;
2139}
2140
2141/*
2142 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002143 * function to attach either it or all tasks in its threadgroup. Will lock
2144 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002145 */
2146static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002147{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002148 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002149 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150 int ret;
2151
Ben Blum74a11662011-05-26 16:25:20 -07002152 if (!cgroup_lock_live_group(cgrp))
2153 return -ENODEV;
2154
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155retry_find_task:
2156 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002158 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002159 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002161 ret= -ESRCH;
2162 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002163 }
Ben Blum74a11662011-05-26 16:25:20 -07002164 /*
2165 * even if we're attaching all tasks in the thread group, we
2166 * only need to check permissions on one of them.
2167 */
David Howellsc69e8d92008-11-14 10:39:19 +11002168 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002169 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2170 !uid_eq(cred->euid, tcred->uid) &&
2171 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002172 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 ret = -EACCES;
2174 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176 } else
2177 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002178
2179 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002180 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002181
2182 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002183 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002184 * trapped in a cpuset, or RT worker may be born in a cgroup
2185 * with no rt_runtime allocated. Just say no.
2186 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002187 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002188 ret = -EINVAL;
2189 rcu_read_unlock();
2190 goto out_unlock_cgroup;
2191 }
2192
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193 get_task_struct(tsk);
2194 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002195
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002196 threadgroup_lock(tsk);
2197 if (threadgroup) {
2198 if (!thread_group_leader(tsk)) {
2199 /*
2200 * a race with de_thread from another thread's exec()
2201 * may strip us of our leadership, if this happens,
2202 * there is no choice but to throw this task away and
2203 * try again; this is
2204 * "double-double-toil-and-trouble-check locking".
2205 */
2206 threadgroup_unlock(tsk);
2207 put_task_struct(tsk);
2208 goto retry_find_task;
2209 }
Li Zefan081aa452013-03-13 09:17:09 +08002210 }
2211
2212 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2213
Tejun Heocd3d0952011-12-12 18:12:21 -08002214 threadgroup_unlock(tsk);
2215
Paul Menagebbcb81d2007-10-18 23:39:32 -07002216 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002217out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002218 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002219 return ret;
2220}
2221
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002222/**
2223 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2224 * @from: attach to all cgroups of a given task
2225 * @tsk: the task to be attached
2226 */
2227int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2228{
2229 struct cgroupfs_root *root;
2230 int retval = 0;
2231
Tejun Heo47cfcd02013-04-07 09:29:51 -07002232 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233 for_each_active_root(root) {
2234 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2235
2236 retval = cgroup_attach_task(from_cg, tsk, false);
2237 if (retval)
2238 break;
2239 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002241
2242 return retval;
2243}
2244EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2245
Paul Menageaf351022008-07-25 01:47:01 -07002246static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2247{
Ben Blum74a11662011-05-26 16:25:20 -07002248 return attach_task_by_pid(cgrp, pid, false);
2249}
2250
2251static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2252{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002253 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002254}
2255
Paul Menagee788e0662008-07-25 01:46:59 -07002256static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2257 const char *buffer)
2258{
2259 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002260 if (strlen(buffer) >= PATH_MAX)
2261 return -EINVAL;
Paul Menagee788e0662008-07-25 01:46:59 -07002262 if (!cgroup_lock_live_group(cgrp))
2263 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002264 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002265 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002266 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002267 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002268 return 0;
2269}
2270
2271static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2272 struct seq_file *seq)
2273{
2274 if (!cgroup_lock_live_group(cgrp))
2275 return -ENODEV;
2276 seq_puts(seq, cgrp->root->release_agent_path);
2277 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002278 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002279 return 0;
2280}
2281
Tejun Heo873fe092013-04-14 20:15:26 -07002282static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2283 struct seq_file *seq)
2284{
2285 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002286 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002287}
2288
Paul Menage84eea842008-07-25 01:47:00 -07002289/* A buffer size big enough for numbers or short strings */
2290#define CGROUP_LOCAL_BUFFER_SIZE 64
2291
Paul Menagee73d2c62008-04-29 01:00:06 -07002292static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002293 struct file *file,
2294 const char __user *userbuf,
2295 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002296{
Paul Menage84eea842008-07-25 01:47:00 -07002297 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002298 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002299 char *end;
2300
2301 if (!nbytes)
2302 return -EINVAL;
2303 if (nbytes >= sizeof(buffer))
2304 return -E2BIG;
2305 if (copy_from_user(buffer, userbuf, nbytes))
2306 return -EFAULT;
2307
2308 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002309 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002310 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002311 if (*end)
2312 return -EINVAL;
2313 retval = cft->write_u64(cgrp, cft, val);
2314 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002315 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002316 if (*end)
2317 return -EINVAL;
2318 retval = cft->write_s64(cgrp, cft, val);
2319 }
Paul Menage355e0c42007-10-18 23:39:33 -07002320 if (!retval)
2321 retval = nbytes;
2322 return retval;
2323}
2324
Paul Menagedb3b1492008-07-25 01:46:58 -07002325static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2326 struct file *file,
2327 const char __user *userbuf,
2328 size_t nbytes, loff_t *unused_ppos)
2329{
Paul Menage84eea842008-07-25 01:47:00 -07002330 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002331 int retval = 0;
2332 size_t max_bytes = cft->max_write_len;
2333 char *buffer = local_buffer;
2334
2335 if (!max_bytes)
2336 max_bytes = sizeof(local_buffer) - 1;
2337 if (nbytes >= max_bytes)
2338 return -E2BIG;
2339 /* Allocate a dynamic buffer if we need one */
2340 if (nbytes >= sizeof(local_buffer)) {
2341 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2342 if (buffer == NULL)
2343 return -ENOMEM;
2344 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002345 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2346 retval = -EFAULT;
2347 goto out;
2348 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002349
2350 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002351 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002352 if (!retval)
2353 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002354out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002355 if (buffer != local_buffer)
2356 kfree(buffer);
2357 return retval;
2358}
2359
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2361 size_t nbytes, loff_t *ppos)
2362{
2363 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002364 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365
Li Zefan75139b82009-01-07 18:07:33 -08002366 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002368 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002369 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002370 if (cft->write_u64 || cft->write_s64)
2371 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002372 if (cft->write_string)
2373 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002374 if (cft->trigger) {
2375 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2376 return ret ? ret : nbytes;
2377 }
Paul Menage355e0c42007-10-18 23:39:33 -07002378 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379}
2380
Paul Menagef4c753b2008-04-29 00:59:56 -07002381static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2382 struct file *file,
2383 char __user *buf, size_t nbytes,
2384 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385{
Paul Menage84eea842008-07-25 01:47:00 -07002386 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002387 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2389
2390 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2391}
2392
Paul Menagee73d2c62008-04-29 01:00:06 -07002393static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2394 struct file *file,
2395 char __user *buf, size_t nbytes,
2396 loff_t *ppos)
2397{
Paul Menage84eea842008-07-25 01:47:00 -07002398 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002399 s64 val = cft->read_s64(cgrp, cft);
2400 int len = sprintf(tmp, "%lld\n", (long long) val);
2401
2402 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2403}
2404
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2406 size_t nbytes, loff_t *ppos)
2407{
2408 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410
Li Zefan75139b82009-01-07 18:07:33 -08002411 if (cgroup_is_removed(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 return -ENODEV;
2413
2414 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002415 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002416 if (cft->read_u64)
2417 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002418 if (cft->read_s64)
2419 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 return -EINVAL;
2421}
2422
Paul Menage91796562008-04-29 01:00:01 -07002423/*
2424 * seqfile ops/methods for returning structured data. Currently just
2425 * supports string->u64 maps, but can be extended in future.
2426 */
2427
2428struct cgroup_seqfile_state {
2429 struct cftype *cft;
2430 struct cgroup *cgroup;
2431};
2432
2433static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2434{
2435 struct seq_file *sf = cb->state;
2436 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2437}
2438
2439static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2440{
2441 struct cgroup_seqfile_state *state = m->private;
2442 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002443 if (cft->read_map) {
2444 struct cgroup_map_cb cb = {
2445 .fill = cgroup_map_add,
2446 .state = m,
2447 };
2448 return cft->read_map(state->cgroup, cft, &cb);
2449 }
2450 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002451}
2452
Adrian Bunk96930a62008-07-25 19:46:21 -07002453static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002454{
2455 struct seq_file *seq = file->private_data;
2456 kfree(seq->private);
2457 return single_release(inode, file);
2458}
2459
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002460static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002461 .read = seq_read,
Paul Menagee788e0662008-07-25 01:46:59 -07002462 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002463 .llseek = seq_lseek,
2464 .release = cgroup_seqfile_release,
2465};
2466
Paul Menageddbcc7e2007-10-18 23:39:30 -07002467static int cgroup_file_open(struct inode *inode, struct file *file)
2468{
2469 int err;
2470 struct cftype *cft;
2471
2472 err = generic_file_open(inode, file);
2473 if (err)
2474 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002476
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002477 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002478 struct cgroup_seqfile_state *state =
2479 kzalloc(sizeof(*state), GFP_USER);
2480 if (!state)
2481 return -ENOMEM;
2482 state->cft = cft;
2483 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2484 file->f_op = &cgroup_seqfile_operations;
2485 err = single_open(file, cgroup_seqfile_show, state);
2486 if (err < 0)
2487 kfree(state);
2488 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002489 err = cft->open(inode, file);
2490 else
2491 err = 0;
2492
2493 return err;
2494}
2495
2496static int cgroup_file_release(struct inode *inode, struct file *file)
2497{
2498 struct cftype *cft = __d_cft(file->f_dentry);
2499 if (cft->release)
2500 return cft->release(inode, file);
2501 return 0;
2502}
2503
2504/*
2505 * cgroup_rename - Only allow simple rename of directories in place.
2506 */
2507static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2508 struct inode *new_dir, struct dentry *new_dentry)
2509{
Li Zefan65dff752013-03-01 15:01:56 +08002510 int ret;
2511 struct cgroup_name *name, *old_name;
2512 struct cgroup *cgrp;
2513
2514 /*
2515 * It's convinient to use parent dir's i_mutex to protected
2516 * cgrp->name.
2517 */
2518 lockdep_assert_held(&old_dir->i_mutex);
2519
Paul Menageddbcc7e2007-10-18 23:39:30 -07002520 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2521 return -ENOTDIR;
2522 if (new_dentry->d_inode)
2523 return -EEXIST;
2524 if (old_dir != new_dir)
2525 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002526
2527 cgrp = __d_cgrp(old_dentry);
2528
2529 name = cgroup_alloc_name(new_dentry);
2530 if (!name)
2531 return -ENOMEM;
2532
2533 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2534 if (ret) {
2535 kfree(name);
2536 return ret;
2537 }
2538
2539 old_name = cgrp->name;
2540 rcu_assign_pointer(cgrp->name, name);
2541
2542 kfree_rcu(old_name, rcu_head);
2543 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002544}
2545
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002546static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2547{
2548 if (S_ISDIR(dentry->d_inode->i_mode))
2549 return &__d_cgrp(dentry)->xattrs;
2550 else
Li Zefan712317a2013-04-18 23:09:52 -07002551 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002552}
2553
2554static inline int xattr_enabled(struct dentry *dentry)
2555{
2556 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002557 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002558}
2559
2560static bool is_valid_xattr(const char *name)
2561{
2562 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2563 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2564 return true;
2565 return false;
2566}
2567
2568static int cgroup_setxattr(struct dentry *dentry, const char *name,
2569 const void *val, size_t size, int flags)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 if (!is_valid_xattr(name))
2574 return -EINVAL;
2575 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2576}
2577
2578static int cgroup_removexattr(struct dentry *dentry, const char *name)
2579{
2580 if (!xattr_enabled(dentry))
2581 return -EOPNOTSUPP;
2582 if (!is_valid_xattr(name))
2583 return -EINVAL;
2584 return simple_xattr_remove(__d_xattrs(dentry), name);
2585}
2586
2587static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2588 void *buf, size_t size)
2589{
2590 if (!xattr_enabled(dentry))
2591 return -EOPNOTSUPP;
2592 if (!is_valid_xattr(name))
2593 return -EINVAL;
2594 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2595}
2596
2597static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2598{
2599 if (!xattr_enabled(dentry))
2600 return -EOPNOTSUPP;
2601 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2602}
2603
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002604static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002605 .read = cgroup_file_read,
2606 .write = cgroup_file_write,
2607 .llseek = generic_file_llseek,
2608 .open = cgroup_file_open,
2609 .release = cgroup_file_release,
2610};
2611
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002612static const struct inode_operations cgroup_file_inode_operations = {
2613 .setxattr = cgroup_setxattr,
2614 .getxattr = cgroup_getxattr,
2615 .listxattr = cgroup_listxattr,
2616 .removexattr = cgroup_removexattr,
2617};
2618
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002619static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002620 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002621 .mkdir = cgroup_mkdir,
2622 .rmdir = cgroup_rmdir,
2623 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002624 .setxattr = cgroup_setxattr,
2625 .getxattr = cgroup_getxattr,
2626 .listxattr = cgroup_listxattr,
2627 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628};
2629
Al Viro00cd8dd2012-06-10 17:13:09 -04002630static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002631{
2632 if (dentry->d_name.len > NAME_MAX)
2633 return ERR_PTR(-ENAMETOOLONG);
2634 d_add(dentry, NULL);
2635 return NULL;
2636}
2637
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002638/*
2639 * Check if a file is a control file
2640 */
2641static inline struct cftype *__file_cft(struct file *file)
2642{
Al Viro496ad9a2013-01-23 17:07:38 -05002643 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002644 return ERR_PTR(-EINVAL);
2645 return __d_cft(file->f_dentry);
2646}
2647
Al Viroa5e7ed32011-07-26 01:55:55 -04002648static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002649 struct super_block *sb)
2650{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651 struct inode *inode;
2652
2653 if (!dentry)
2654 return -ENOENT;
2655 if (dentry->d_inode)
2656 return -EEXIST;
2657
2658 inode = cgroup_new_inode(mode, sb);
2659 if (!inode)
2660 return -ENOMEM;
2661
2662 if (S_ISDIR(mode)) {
2663 inode->i_op = &cgroup_dir_inode_operations;
2664 inode->i_fop = &simple_dir_operations;
2665
2666 /* start off with i_nlink == 2 (for "." entry) */
2667 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002668 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669
Tejun Heob8a2df6a2012-11-19 08:13:37 -08002670 /*
2671 * Control reaches here with cgroup_mutex held.
2672 * @inode->i_mutex should nest outside cgroup_mutex but we
2673 * want to populate it immediately without releasing
2674 * cgroup_mutex. As @inode isn't visible to anyone else
2675 * yet, trylock will always succeed without affecting
2676 * lockdep checks.
2677 */
2678 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002679 } else if (S_ISREG(mode)) {
2680 inode->i_size = 0;
2681 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002682 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684 d_instantiate(dentry, inode);
2685 dget(dentry); /* Extra count - pin the dentry in core */
2686 return 0;
2687}
2688
Li Zefan099fca32009-04-02 16:57:29 -07002689/**
2690 * cgroup_file_mode - deduce file mode of a control file
2691 * @cft: the control file in question
2692 *
2693 * returns cft->mode if ->mode is not 0
2694 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2695 * returns S_IRUGO if it has only a read handler
2696 * returns S_IWUSR if it has only a write hander
2697 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002698static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002699{
Al Viroa5e7ed32011-07-26 01:55:55 -04002700 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002701
2702 if (cft->mode)
2703 return cft->mode;
2704
2705 if (cft->read || cft->read_u64 || cft->read_s64 ||
2706 cft->read_map || cft->read_seq_string)
2707 mode |= S_IRUGO;
2708
2709 if (cft->write || cft->write_u64 || cft->write_s64 ||
2710 cft->write_string || cft->trigger)
2711 mode |= S_IWUSR;
2712
2713 return mode;
2714}
2715
Tejun Heodb0416b2012-04-01 12:09:55 -07002716static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002717 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718{
Paul Menagebd89aab2007-10-18 23:40:44 -07002719 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002722 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002724 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002726
Tejun Heo93438622013-04-14 20:15:25 -07002727 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 strcpy(name, subsys->name);
2729 strcat(name, ".");
2730 }
2731 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002732
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002734
2735 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2736 if (!cfe)
2737 return -ENOMEM;
2738
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002740 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002742 goto out;
2743 }
2744
Li Zefand6cbf352013-05-14 19:44:20 +08002745 cfe->type = (void *)cft;
2746 cfe->dentry = dentry;
2747 dentry->d_fsdata = cfe;
2748 simple_xattrs_init(&cfe->xattrs);
2749
Tejun Heo05ef1d72012-04-01 12:09:56 -07002750 mode = cgroup_file_mode(cft);
2751 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2752 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002753 list_add_tail(&cfe->node, &parent->files);
2754 cfe = NULL;
2755 }
2756 dput(dentry);
2757out:
2758 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002759 return error;
2760}
2761
Tejun Heo79578622012-04-01 12:09:56 -07002762static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002763 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002765 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002766 int err, ret = 0;
2767
2768 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002769 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002770 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2771 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002772 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2773 continue;
2774 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2775 continue;
2776
Li Zefan2739d3c2013-01-21 18:18:33 +08002777 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002778 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002779 if (err)
2780 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2781 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002782 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002783 } else {
2784 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002785 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002787 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002788}
2789
Tejun Heo8e3f6542012-04-01 12:09:55 -07002790static DEFINE_MUTEX(cgroup_cft_mutex);
2791
2792static void cgroup_cfts_prepare(void)
2793 __acquires(&cgroup_cft_mutex) __acquires(&cgroup_mutex)
2794{
2795 /*
2796 * Thanks to the entanglement with vfs inode locking, we can't walk
2797 * the existing cgroups under cgroup_mutex and create files.
2798 * Instead, we increment reference on all cgroups and build list of
2799 * them using @cgrp->cft_q_node. Grab cgroup_cft_mutex to ensure
2800 * exclusive access to the field.
2801 */
2802 mutex_lock(&cgroup_cft_mutex);
2803 mutex_lock(&cgroup_mutex);
2804}
2805
2806static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002807 struct cftype *cfts, bool is_add)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808 __releases(&cgroup_mutex) __releases(&cgroup_cft_mutex)
2809{
2810 LIST_HEAD(pending);
2811 struct cgroup *cgrp, *n;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002812
2813 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
2814 if (cfts && ss->root != &rootnode) {
2815 list_for_each_entry(cgrp, &ss->root->allcg_list, allcg_node) {
2816 dget(cgrp->dentry);
2817 list_add_tail(&cgrp->cft_q_node, &pending);
2818 }
2819 }
2820
2821 mutex_unlock(&cgroup_mutex);
2822
2823 /*
2824 * All new cgroups will see @cfts update on @ss->cftsets. Add/rm
2825 * files for all cgroups which were created before.
2826 */
2827 list_for_each_entry_safe(cgrp, n, &pending, cft_q_node) {
2828 struct inode *inode = cgrp->dentry->d_inode;
2829
2830 mutex_lock(&inode->i_mutex);
2831 mutex_lock(&cgroup_mutex);
2832 if (!cgroup_is_removed(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002833 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834 mutex_unlock(&cgroup_mutex);
2835 mutex_unlock(&inode->i_mutex);
2836
2837 list_del_init(&cgrp->cft_q_node);
2838 dput(cgrp->dentry);
2839 }
2840
2841 mutex_unlock(&cgroup_cft_mutex);
2842}
2843
2844/**
2845 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2846 * @ss: target cgroup subsystem
2847 * @cfts: zero-length name terminated array of cftypes
2848 *
2849 * Register @cfts to @ss. Files described by @cfts are created for all
2850 * existing cgroups to which @ss is attached and all future cgroups will
2851 * have them too. This function can be called anytime whether @ss is
2852 * attached or not.
2853 *
2854 * Returns 0 on successful registration, -errno on failure. Note that this
2855 * function currently returns 0 as long as @cfts registration is successful
2856 * even if some file creation attempts on existing cgroups fail.
2857 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002858int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859{
2860 struct cftype_set *set;
2861
2862 set = kzalloc(sizeof(*set), GFP_KERNEL);
2863 if (!set)
2864 return -ENOMEM;
2865
2866 cgroup_cfts_prepare();
2867 set->cfts = cfts;
2868 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002869 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002870
2871 return 0;
2872}
2873EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2874
Li Zefana043e3b2008-02-23 15:24:09 -08002875/**
Tejun Heo79578622012-04-01 12:09:56 -07002876 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2877 * @ss: target cgroup subsystem
2878 * @cfts: zero-length name terminated array of cftypes
2879 *
2880 * Unregister @cfts from @ss. Files described by @cfts are removed from
2881 * all existing cgroups to which @ss is attached and all future cgroups
2882 * won't have them either. This function can be called anytime whether @ss
2883 * is attached or not.
2884 *
2885 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2886 * registered with @ss.
2887 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002888int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002889{
2890 struct cftype_set *set;
2891
2892 cgroup_cfts_prepare();
2893
2894 list_for_each_entry(set, &ss->cftsets, node) {
2895 if (set->cfts == cfts) {
2896 list_del_init(&set->node);
2897 cgroup_cfts_commit(ss, cfts, false);
2898 return 0;
2899 }
2900 }
2901
2902 cgroup_cfts_commit(ss, NULL, false);
2903 return -ENOENT;
2904}
2905
2906/**
Li Zefana043e3b2008-02-23 15:24:09 -08002907 * cgroup_task_count - count the number of tasks in a cgroup.
2908 * @cgrp: the cgroup in question
2909 *
2910 * Return the number of tasks in the cgroup.
2911 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002912int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002913{
2914 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002915 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002916
Paul Menage817929e2007-10-18 23:39:36 -07002917 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002918 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2919 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002920 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002921 return count;
2922}
2923
2924/*
Paul Menage817929e2007-10-18 23:39:36 -07002925 * Advance a list_head iterator. The iterator should be positioned at
2926 * the start of a css_set
2927 */
Tejun Heo69d02062013-06-12 21:04:50 -07002928static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002929{
Tejun Heo69d02062013-06-12 21:04:50 -07002930 struct list_head *l = it->cset_link;
2931 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002932 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002933
2934 /* Advance to the next non-empty css_set */
2935 do {
2936 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002937 if (l == &cgrp->cset_links) {
2938 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002939 return;
2940 }
Tejun Heo69d02062013-06-12 21:04:50 -07002941 link = list_entry(l, struct cgrp_cset_link, cset_link);
2942 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002943 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002944 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002945 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002946}
2947
Cliff Wickman31a7df02008-02-07 00:14:42 -08002948/*
2949 * To reduce the fork() overhead for systems that are not actually
2950 * using their cgroups capability, we don't maintain the lists running
2951 * through each css_set to its tasks until we see the list actually
2952 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002953 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002954static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002955{
2956 struct task_struct *p, *g;
2957 write_lock(&css_set_lock);
2958 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002959 /*
2960 * We need tasklist_lock because RCU is not safe against
2961 * while_each_thread(). Besides, a forking task that has passed
2962 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2963 * is not guaranteed to have its child immediately visible in the
2964 * tasklist if we walk through it with RCU.
2965 */
2966 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967 do_each_thread(g, p) {
2968 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002969 /*
2970 * We should check if the process is exiting, otherwise
2971 * it will race with cgroup_exit() in that the list
2972 * entry won't be deleted though the process has exited.
2973 */
2974 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002975 list_add(&p->cg_list, &p->cgroups->tasks);
2976 task_unlock(p);
2977 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002978 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979 write_unlock(&css_set_lock);
2980}
2981
Tejun Heo574bd9f2012-11-09 09:12:29 -08002982/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002983 * cgroup_next_sibling - find the next sibling of a given cgroup
2984 * @pos: the current cgroup
2985 *
2986 * This function returns the next sibling of @pos and should be called
2987 * under RCU read lock. The only requirement is that @pos is accessible.
2988 * The next sibling is guaranteed to be returned regardless of @pos's
2989 * state.
2990 */
2991struct cgroup *cgroup_next_sibling(struct cgroup *pos)
2992{
2993 struct cgroup *next;
2994
2995 WARN_ON_ONCE(!rcu_read_lock_held());
2996
2997 /*
2998 * @pos could already have been removed. Once a cgroup is removed,
2999 * its ->sibling.next is no longer updated when its next sibling
3000 * changes. As CGRP_REMOVED is set on removal which is fully
3001 * serialized, if we see it unasserted, it's guaranteed that the
3002 * next sibling hasn't finished its grace period even if it's
3003 * already removed, and thus safe to dereference from this RCU
3004 * critical section. If ->sibling.next is inaccessible,
3005 * cgroup_is_removed() is guaranteed to be visible as %true here.
3006 */
3007 if (likely(!cgroup_is_removed(pos))) {
3008 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3009 if (&next->sibling != &pos->parent->children)
3010 return next;
3011 return NULL;
3012 }
3013
3014 /*
3015 * Can't dereference the next pointer. Each cgroup is given a
3016 * monotonically increasing unique serial number and always
3017 * appended to the sibling list, so the next one can be found by
3018 * walking the parent's children until we see a cgroup with higher
3019 * serial number than @pos's.
3020 *
3021 * While this path can be slow, it's taken only when either the
3022 * current cgroup is removed or iteration and removal race.
3023 */
3024 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3025 if (next->serial_nr > pos->serial_nr)
3026 return next;
3027 return NULL;
3028}
3029EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3030
3031/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003032 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3033 * @pos: the current position (%NULL to initiate traversal)
3034 * @cgroup: cgroup whose descendants to walk
3035 *
3036 * To be used by cgroup_for_each_descendant_pre(). Find the next
3037 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003038 *
3039 * While this function requires RCU read locking, it doesn't require the
3040 * whole traversal to be contained in a single RCU critical section. This
3041 * function will return the correct next descendant as long as both @pos
3042 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003043 */
3044struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3045 struct cgroup *cgroup)
3046{
3047 struct cgroup *next;
3048
3049 WARN_ON_ONCE(!rcu_read_lock_held());
3050
3051 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003052 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003053 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003054
3055 /* visit the first child if exists */
3056 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3057 if (next)
3058 return next;
3059
3060 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003061 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003062 next = cgroup_next_sibling(pos);
3063 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003065 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003066 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067
3068 return NULL;
3069}
3070EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3071
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003072/**
3073 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3074 * @pos: cgroup of interest
3075 *
3076 * Return the rightmost descendant of @pos. If there's no descendant,
3077 * @pos is returned. This can be used during pre-order traversal to skip
3078 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003079 *
3080 * While this function requires RCU read locking, it doesn't require the
3081 * whole traversal to be contained in a single RCU critical section. This
3082 * function will return the correct rightmost descendant as long as @pos is
3083 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003084 */
3085struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3086{
3087 struct cgroup *last, *tmp;
3088
3089 WARN_ON_ONCE(!rcu_read_lock_held());
3090
3091 do {
3092 last = pos;
3093 /* ->prev isn't RCU safe, walk ->next till the end */
3094 pos = NULL;
3095 list_for_each_entry_rcu(tmp, &last->children, sibling)
3096 pos = tmp;
3097 } while (pos);
3098
3099 return last;
3100}
3101EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3102
Tejun Heo574bd9f2012-11-09 09:12:29 -08003103static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3104{
3105 struct cgroup *last;
3106
3107 do {
3108 last = pos;
3109 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3110 sibling);
3111 } while (pos);
3112
3113 return last;
3114}
3115
3116/**
3117 * cgroup_next_descendant_post - find the next descendant for post-order walk
3118 * @pos: the current position (%NULL to initiate traversal)
3119 * @cgroup: cgroup whose descendants to walk
3120 *
3121 * To be used by cgroup_for_each_descendant_post(). Find the next
3122 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003123 *
3124 * While this function requires RCU read locking, it doesn't require the
3125 * whole traversal to be contained in a single RCU critical section. This
3126 * function will return the correct next descendant as long as both @pos
3127 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003128 */
3129struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3130 struct cgroup *cgroup)
3131{
3132 struct cgroup *next;
3133
3134 WARN_ON_ONCE(!rcu_read_lock_held());
3135
3136 /* if first iteration, visit the leftmost descendant */
3137 if (!pos) {
3138 next = cgroup_leftmost_descendant(cgroup);
3139 return next != cgroup ? next : NULL;
3140 }
3141
3142 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003143 next = cgroup_next_sibling(pos);
3144 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003145 return cgroup_leftmost_descendant(next);
3146
3147 /* no sibling left, visit parent */
3148 next = pos->parent;
3149 return next != cgroup ? next : NULL;
3150}
3151EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3152
Paul Menagebd89aab2007-10-18 23:40:44 -07003153void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003154 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003155{
3156 /*
3157 * The first time anyone tries to iterate across a cgroup,
3158 * we need to enable the list linking each css_set to its
3159 * tasks, and fix up all existing tasks.
3160 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003161 if (!use_task_css_set_links)
3162 cgroup_enable_task_cg_lists();
3163
Paul Menage817929e2007-10-18 23:39:36 -07003164 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003165 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003166 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003167}
3168
Paul Menagebd89aab2007-10-18 23:40:44 -07003169struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003170 struct cgroup_iter *it)
3171{
3172 struct task_struct *res;
3173 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003174 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003175
3176 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003177 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003178 return NULL;
3179 res = list_entry(l, struct task_struct, cg_list);
3180 /* Advance iterator to find next entry */
3181 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003182 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3183 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003184 /* We reached the end of this task list - move on to
3185 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003186 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003187 } else {
3188 it->task = l;
3189 }
3190 return res;
3191}
3192
Paul Menagebd89aab2007-10-18 23:40:44 -07003193void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003194 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003195{
3196 read_unlock(&css_set_lock);
3197}
3198
Cliff Wickman31a7df02008-02-07 00:14:42 -08003199static inline int started_after_time(struct task_struct *t1,
3200 struct timespec *time,
3201 struct task_struct *t2)
3202{
3203 int start_diff = timespec_compare(&t1->start_time, time);
3204 if (start_diff > 0) {
3205 return 1;
3206 } else if (start_diff < 0) {
3207 return 0;
3208 } else {
3209 /*
3210 * Arbitrarily, if two processes started at the same
3211 * time, we'll say that the lower pointer value
3212 * started first. Note that t2 may have exited by now
3213 * so this may not be a valid pointer any longer, but
3214 * that's fine - it still serves to distinguish
3215 * between two tasks started (effectively) simultaneously.
3216 */
3217 return t1 > t2;
3218 }
3219}
3220
3221/*
3222 * This function is a callback from heap_insert() and is used to order
3223 * the heap.
3224 * In this case we order the heap in descending task start time.
3225 */
3226static inline int started_after(void *p1, void *p2)
3227{
3228 struct task_struct *t1 = p1;
3229 struct task_struct *t2 = p2;
3230 return started_after_time(t1, &t2->start_time, t2);
3231}
3232
3233/**
3234 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3235 * @scan: struct cgroup_scanner containing arguments for the scan
3236 *
3237 * Arguments include pointers to callback functions test_task() and
3238 * process_task().
3239 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3240 * and if it returns true, call process_task() for it also.
3241 * The test_task pointer may be NULL, meaning always true (select all tasks).
3242 * Effectively duplicates cgroup_iter_{start,next,end}()
3243 * but does not lock css_set_lock for the call to process_task().
3244 * The struct cgroup_scanner may be embedded in any structure of the caller's
3245 * creation.
3246 * It is guaranteed that process_task() will act on every task that
3247 * is a member of the cgroup for the duration of this call. This
3248 * function may or may not call process_task() for tasks that exit
3249 * or move to a different cgroup during the call, or are forked or
3250 * move into the cgroup during the call.
3251 *
3252 * Note that test_task() may be called with locks held, and may in some
3253 * situations be called multiple times for the same task, so it should
3254 * be cheap.
3255 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3256 * pre-allocated and will be used for heap operations (and its "gt" member will
3257 * be overwritten), else a temporary heap will be used (allocation of which
3258 * may cause this function to fail).
3259 */
3260int cgroup_scan_tasks(struct cgroup_scanner *scan)
3261{
3262 int retval, i;
3263 struct cgroup_iter it;
3264 struct task_struct *p, *dropped;
3265 /* Never dereference latest_task, since it's not refcounted */
3266 struct task_struct *latest_task = NULL;
3267 struct ptr_heap tmp_heap;
3268 struct ptr_heap *heap;
3269 struct timespec latest_time = { 0, 0 };
3270
3271 if (scan->heap) {
3272 /* The caller supplied our heap and pre-allocated its memory */
3273 heap = scan->heap;
3274 heap->gt = &started_after;
3275 } else {
3276 /* We need to allocate our own heap memory */
3277 heap = &tmp_heap;
3278 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3279 if (retval)
3280 /* cannot allocate the heap */
3281 return retval;
3282 }
3283
3284 again:
3285 /*
3286 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3287 * to determine which are of interest, and using the scanner's
3288 * "process_task" callback to process any of them that need an update.
3289 * Since we don't want to hold any locks during the task updates,
3290 * gather tasks to be processed in a heap structure.
3291 * The heap is sorted by descending task start time.
3292 * If the statically-sized heap fills up, we overflow tasks that
3293 * started later, and in future iterations only consider tasks that
3294 * started after the latest task in the previous pass. This
3295 * guarantees forward progress and that we don't miss any tasks.
3296 */
3297 heap->size = 0;
3298 cgroup_iter_start(scan->cg, &it);
3299 while ((p = cgroup_iter_next(scan->cg, &it))) {
3300 /*
3301 * Only affect tasks that qualify per the caller's callback,
3302 * if he provided one
3303 */
3304 if (scan->test_task && !scan->test_task(p, scan))
3305 continue;
3306 /*
3307 * Only process tasks that started after the last task
3308 * we processed
3309 */
3310 if (!started_after_time(p, &latest_time, latest_task))
3311 continue;
3312 dropped = heap_insert(heap, p);
3313 if (dropped == NULL) {
3314 /*
3315 * The new task was inserted; the heap wasn't
3316 * previously full
3317 */
3318 get_task_struct(p);
3319 } else if (dropped != p) {
3320 /*
3321 * The new task was inserted, and pushed out a
3322 * different task
3323 */
3324 get_task_struct(p);
3325 put_task_struct(dropped);
3326 }
3327 /*
3328 * Else the new task was newer than anything already in
3329 * the heap and wasn't inserted
3330 */
3331 }
3332 cgroup_iter_end(scan->cg, &it);
3333
3334 if (heap->size) {
3335 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003336 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003337 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003338 latest_time = q->start_time;
3339 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003340 }
3341 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003342 scan->process_task(q, scan);
3343 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003344 }
3345 /*
3346 * If we had to process any tasks at all, scan again
3347 * in case some of them were in the middle of forking
3348 * children that didn't get processed.
3349 * Not the most efficient way to do it, but it avoids
3350 * having to take callback_mutex in the fork path
3351 */
3352 goto again;
3353 }
3354 if (heap == &tmp_heap)
3355 heap_free(&tmp_heap);
3356 return 0;
3357}
3358
Tejun Heo8cc99342013-04-07 09:29:50 -07003359static void cgroup_transfer_one_task(struct task_struct *task,
3360 struct cgroup_scanner *scan)
3361{
3362 struct cgroup *new_cgroup = scan->data;
3363
Tejun Heo47cfcd02013-04-07 09:29:51 -07003364 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003365 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003366 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003367}
3368
3369/**
3370 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3371 * @to: cgroup to which the tasks will be moved
3372 * @from: cgroup in which the tasks currently reside
3373 */
3374int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3375{
3376 struct cgroup_scanner scan;
3377
3378 scan.cg = from;
3379 scan.test_task = NULL; /* select all tasks in cgroup */
3380 scan.process_task = cgroup_transfer_one_task;
3381 scan.heap = NULL;
3382 scan.data = to;
3383
3384 return cgroup_scan_tasks(&scan);
3385}
3386
Paul Menage817929e2007-10-18 23:39:36 -07003387/*
Ben Blum102a7752009-09-23 15:56:26 -07003388 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003389 *
3390 * Reading this file can return large amounts of data if a cgroup has
3391 * *lots* of attached tasks. So it may need several calls to read(),
3392 * but we cannot guarantee that the information we produce is correct
3393 * unless we produce it entirely atomically.
3394 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003395 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003396
Li Zefan24528252012-01-20 11:58:43 +08003397/* which pidlist file are we talking about? */
3398enum cgroup_filetype {
3399 CGROUP_FILE_PROCS,
3400 CGROUP_FILE_TASKS,
3401};
3402
3403/*
3404 * A pidlist is a list of pids that virtually represents the contents of one
3405 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3406 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3407 * to the cgroup.
3408 */
3409struct cgroup_pidlist {
3410 /*
3411 * used to find which pidlist is wanted. doesn't change as long as
3412 * this particular list stays in the list.
3413 */
3414 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3415 /* array of xids */
3416 pid_t *list;
3417 /* how many elements the above list has */
3418 int length;
3419 /* how many files are using the current array */
3420 int use_count;
3421 /* each of these stored in a list by its cgroup */
3422 struct list_head links;
3423 /* pointer to the cgroup we belong to, for list removal purposes */
3424 struct cgroup *owner;
3425 /* protects the other fields */
3426 struct rw_semaphore mutex;
3427};
3428
Paul Menagebbcb81d2007-10-18 23:39:32 -07003429/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003430 * The following two functions "fix" the issue where there are more pids
3431 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3432 * TODO: replace with a kernel-wide solution to this problem
3433 */
3434#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3435static void *pidlist_allocate(int count)
3436{
3437 if (PIDLIST_TOO_LARGE(count))
3438 return vmalloc(count * sizeof(pid_t));
3439 else
3440 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3441}
3442static void pidlist_free(void *p)
3443{
3444 if (is_vmalloc_addr(p))
3445 vfree(p);
3446 else
3447 kfree(p);
3448}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003449
3450/*
Ben Blum102a7752009-09-23 15:56:26 -07003451 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003452 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003453 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003454static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003455{
Ben Blum102a7752009-09-23 15:56:26 -07003456 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003457
3458 /*
3459 * we presume the 0th element is unique, so i starts at 1. trivial
3460 * edge cases first; no work needs to be done for either
3461 */
3462 if (length == 0 || length == 1)
3463 return length;
3464 /* src and dest walk down the list; dest counts unique elements */
3465 for (src = 1; src < length; src++) {
3466 /* find next unique element */
3467 while (list[src] == list[src-1]) {
3468 src++;
3469 if (src == length)
3470 goto after;
3471 }
3472 /* dest always points to where the next unique element goes */
3473 list[dest] = list[src];
3474 dest++;
3475 }
3476after:
Ben Blum102a7752009-09-23 15:56:26 -07003477 return dest;
3478}
3479
3480static int cmppid(const void *a, const void *b)
3481{
3482 return *(pid_t *)a - *(pid_t *)b;
3483}
3484
3485/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003486 * find the appropriate pidlist for our purpose (given procs vs tasks)
3487 * returns with the lock on that pidlist already held, and takes care
3488 * of the use count, or returns NULL with no locks held if we're out of
3489 * memory.
3490 */
3491static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3492 enum cgroup_filetype type)
3493{
3494 struct cgroup_pidlist *l;
3495 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003496 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003497
Ben Blum72a8cb32009-09-23 15:56:27 -07003498 /*
3499 * We can't drop the pidlist_mutex before taking the l->mutex in case
3500 * the last ref-holder is trying to remove l from the list at the same
3501 * time. Holding the pidlist_mutex precludes somebody taking whichever
3502 * list we find out from under us - compare release_pid_array().
3503 */
3504 mutex_lock(&cgrp->pidlist_mutex);
3505 list_for_each_entry(l, &cgrp->pidlists, links) {
3506 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003507 /* make sure l doesn't vanish out from under us */
3508 down_write(&l->mutex);
3509 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003510 return l;
3511 }
3512 }
3513 /* entry not found; create a new one */
3514 l = kmalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
3515 if (!l) {
3516 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003517 return l;
3518 }
3519 init_rwsem(&l->mutex);
3520 down_write(&l->mutex);
3521 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003522 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003523 l->use_count = 0; /* don't increment here */
3524 l->list = NULL;
3525 l->owner = cgrp;
3526 list_add(&l->links, &cgrp->pidlists);
3527 mutex_unlock(&cgrp->pidlist_mutex);
3528 return l;
3529}
3530
3531/*
Ben Blum102a7752009-09-23 15:56:26 -07003532 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3533 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003534static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3535 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003536{
3537 pid_t *array;
3538 int length;
3539 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003540 struct cgroup_iter it;
3541 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003542 struct cgroup_pidlist *l;
3543
3544 /*
3545 * If cgroup gets more users after we read count, we won't have
3546 * enough space - tough. This race is indistinguishable to the
3547 * caller from the case that the additional cgroup users didn't
3548 * show up until sometime later on.
3549 */
3550 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003551 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003552 if (!array)
3553 return -ENOMEM;
3554 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003555 cgroup_iter_start(cgrp, &it);
3556 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003557 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003558 break;
Ben Blum102a7752009-09-23 15:56:26 -07003559 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003560 if (type == CGROUP_FILE_PROCS)
3561 pid = task_tgid_vnr(tsk);
3562 else
3563 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003564 if (pid > 0) /* make sure to only use valid results */
3565 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003566 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003567 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003568 length = n;
3569 /* now sort & (if procs) strip out duplicates */
3570 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003571 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003572 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003573 l = cgroup_pidlist_find(cgrp, type);
3574 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003575 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003576 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003577 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003578 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003579 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003580 l->list = array;
3581 l->length = length;
3582 l->use_count++;
3583 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003585 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003586}
3587
Balbir Singh846c7bb2007-10-18 23:39:44 -07003588/**
Li Zefana043e3b2008-02-23 15:24:09 -08003589 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003590 * @stats: cgroupstats to fill information into
3591 * @dentry: A dentry entry belonging to the cgroup for which stats have
3592 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003593 *
3594 * Build and fill cgroupstats so that taskstats can export it to user
3595 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003596 */
3597int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3598{
3599 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003600 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003601 struct cgroup_iter it;
3602 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003603
Balbir Singh846c7bb2007-10-18 23:39:44 -07003604 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003605 * Validate dentry by checking the superblock operations,
3606 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003607 */
Li Zefan33d283b2008-11-19 15:36:48 -08003608 if (dentry->d_sb->s_op != &cgroup_ops ||
3609 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003610 goto err;
3611
3612 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003613 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003614
Paul Menagebd89aab2007-10-18 23:40:44 -07003615 cgroup_iter_start(cgrp, &it);
3616 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003617 switch (tsk->state) {
3618 case TASK_RUNNING:
3619 stats->nr_running++;
3620 break;
3621 case TASK_INTERRUPTIBLE:
3622 stats->nr_sleeping++;
3623 break;
3624 case TASK_UNINTERRUPTIBLE:
3625 stats->nr_uninterruptible++;
3626 break;
3627 case TASK_STOPPED:
3628 stats->nr_stopped++;
3629 break;
3630 default:
3631 if (delayacct_is_task_waiting_on_io(tsk))
3632 stats->nr_io_wait++;
3633 break;
3634 }
3635 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003636 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003637
Balbir Singh846c7bb2007-10-18 23:39:44 -07003638err:
3639 return ret;
3640}
3641
Paul Menage8f3ff202009-09-23 15:56:25 -07003642
Paul Menagecc31edc2008-10-18 20:28:04 -07003643/*
Ben Blum102a7752009-09-23 15:56:26 -07003644 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003645 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003646 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003647 */
3648
Ben Blum102a7752009-09-23 15:56:26 -07003649static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003650{
3651 /*
3652 * Initially we receive a position value that corresponds to
3653 * one more than the last pid shown (or 0 on the first call or
3654 * after a seek to the start). Use a binary-search to find the
3655 * next pid to display, if any
3656 */
Ben Blum102a7752009-09-23 15:56:26 -07003657 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 int index = 0, pid = *pos;
3659 int *iter;
3660
Ben Blum102a7752009-09-23 15:56:26 -07003661 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003662 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003663 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003664
Paul Menagecc31edc2008-10-18 20:28:04 -07003665 while (index < end) {
3666 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003667 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 index = mid;
3669 break;
Ben Blum102a7752009-09-23 15:56:26 -07003670 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003671 index = mid + 1;
3672 else
3673 end = mid;
3674 }
3675 }
3676 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003677 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 return NULL;
3679 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003680 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 *pos = *iter;
3682 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003683}
3684
Ben Blum102a7752009-09-23 15:56:26 -07003685static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003686{
Ben Blum102a7752009-09-23 15:56:26 -07003687 struct cgroup_pidlist *l = s->private;
3688 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003689}
3690
Ben Blum102a7752009-09-23 15:56:26 -07003691static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003692{
Ben Blum102a7752009-09-23 15:56:26 -07003693 struct cgroup_pidlist *l = s->private;
3694 pid_t *p = v;
3695 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 /*
3697 * Advance to the next pid in the array. If this goes off the
3698 * end, we're done
3699 */
3700 p++;
3701 if (p >= end) {
3702 return NULL;
3703 } else {
3704 *pos = *p;
3705 return p;
3706 }
3707}
3708
Ben Blum102a7752009-09-23 15:56:26 -07003709static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003710{
3711 return seq_printf(s, "%d\n", *(int *)v);
3712}
3713
Ben Blum102a7752009-09-23 15:56:26 -07003714/*
3715 * seq_operations functions for iterating on pidlists through seq_file -
3716 * independent of whether it's tasks or procs
3717 */
3718static const struct seq_operations cgroup_pidlist_seq_operations = {
3719 .start = cgroup_pidlist_start,
3720 .stop = cgroup_pidlist_stop,
3721 .next = cgroup_pidlist_next,
3722 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003723};
3724
Ben Blum102a7752009-09-23 15:56:26 -07003725static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003726{
Ben Blum72a8cb32009-09-23 15:56:27 -07003727 /*
3728 * the case where we're the last user of this particular pidlist will
3729 * have us remove it from the cgroup's list, which entails taking the
3730 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3731 * pidlist_mutex, we have to take pidlist_mutex first.
3732 */
3733 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003734 down_write(&l->mutex);
3735 BUG_ON(!l->use_count);
3736 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003737 /* we're the last user if refcount is 0; remove and free */
3738 list_del(&l->links);
3739 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003740 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003741 put_pid_ns(l->key.ns);
3742 up_write(&l->mutex);
3743 kfree(l);
3744 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003745 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003746 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003747 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003748}
3749
Ben Blum102a7752009-09-23 15:56:26 -07003750static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003751{
Ben Blum102a7752009-09-23 15:56:26 -07003752 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003753 if (!(file->f_mode & FMODE_READ))
3754 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003755 /*
3756 * the seq_file will only be initialized if the file was opened for
3757 * reading; hence we check if it's not null only in that case.
3758 */
3759 l = ((struct seq_file *)file->private_data)->private;
3760 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003761 return seq_release(inode, file);
3762}
3763
Ben Blum102a7752009-09-23 15:56:26 -07003764static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003765 .read = seq_read,
3766 .llseek = seq_lseek,
3767 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003768 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003769};
3770
3771/*
Ben Blum102a7752009-09-23 15:56:26 -07003772 * The following functions handle opens on a file that displays a pidlist
3773 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3774 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003775 */
Ben Blum102a7752009-09-23 15:56:26 -07003776/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003777static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003778{
3779 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003780 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003781 int retval;
3782
3783 /* Nothing to do for write-only files */
3784 if (!(file->f_mode & FMODE_READ))
3785 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003786
Ben Blum102a7752009-09-23 15:56:26 -07003787 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003788 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003789 if (retval)
3790 return retval;
3791 /* configure file information */
3792 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003793
Ben Blum102a7752009-09-23 15:56:26 -07003794 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003795 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003796 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003797 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003798 }
Ben Blum102a7752009-09-23 15:56:26 -07003799 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003800 return 0;
3801}
Ben Blum102a7752009-09-23 15:56:26 -07003802static int cgroup_tasks_open(struct inode *unused, struct file *file)
3803{
Ben Blum72a8cb32009-09-23 15:56:27 -07003804 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003805}
3806static int cgroup_procs_open(struct inode *unused, struct file *file)
3807{
Ben Blum72a8cb32009-09-23 15:56:27 -07003808 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003809}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003810
Paul Menagebd89aab2007-10-18 23:40:44 -07003811static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003812 struct cftype *cft)
3813{
Paul Menagebd89aab2007-10-18 23:40:44 -07003814 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003815}
3816
Paul Menage6379c102008-07-25 01:47:01 -07003817static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3818 struct cftype *cft,
3819 u64 val)
3820{
3821 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3822 if (val)
3823 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3824 else
3825 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3826 return 0;
3827}
3828
Paul Menagebbcb81d2007-10-18 23:39:32 -07003829/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003830 * Unregister event and free resources.
3831 *
3832 * Gets called from workqueue.
3833 */
3834static void cgroup_event_remove(struct work_struct *work)
3835{
3836 struct cgroup_event *event = container_of(work, struct cgroup_event,
3837 remove);
3838 struct cgroup *cgrp = event->cgrp;
3839
Li Zefan810cbee2013-02-18 18:56:14 +08003840 remove_wait_queue(event->wqh, &event->wait);
3841
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003842 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3843
Li Zefan810cbee2013-02-18 18:56:14 +08003844 /* Notify userspace the event is going away. */
3845 eventfd_signal(event->eventfd, 1);
3846
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003847 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003848 kfree(event);
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003849 dput(cgrp->dentry);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003850}
3851
3852/*
3853 * Gets called on POLLHUP on eventfd when user closes it.
3854 *
3855 * Called with wqh->lock held and interrupts disabled.
3856 */
3857static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3858 int sync, void *key)
3859{
3860 struct cgroup_event *event = container_of(wait,
3861 struct cgroup_event, wait);
3862 struct cgroup *cgrp = event->cgrp;
3863 unsigned long flags = (unsigned long)key;
3864
3865 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003866 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003867 * If the event has been detached at cgroup removal, we
3868 * can simply return knowing the other side will cleanup
3869 * for us.
3870 *
3871 * We can't race against event freeing since the other
3872 * side will require wqh->lock via remove_wait_queue(),
3873 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003874 */
Li Zefan810cbee2013-02-18 18:56:14 +08003875 spin_lock(&cgrp->event_list_lock);
3876 if (!list_empty(&event->list)) {
3877 list_del_init(&event->list);
3878 /*
3879 * We are in atomic context, but cgroup_event_remove()
3880 * may sleep, so we have to call it in workqueue.
3881 */
3882 schedule_work(&event->remove);
3883 }
3884 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003885 }
3886
3887 return 0;
3888}
3889
3890static void cgroup_event_ptable_queue_proc(struct file *file,
3891 wait_queue_head_t *wqh, poll_table *pt)
3892{
3893 struct cgroup_event *event = container_of(pt,
3894 struct cgroup_event, pt);
3895
3896 event->wqh = wqh;
3897 add_wait_queue(wqh, &event->wait);
3898}
3899
3900/*
3901 * Parse input and register new cgroup event handler.
3902 *
3903 * Input must be in format '<event_fd> <control_fd> <args>'.
3904 * Interpretation of args is defined by control file implementation.
3905 */
3906static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3907 const char *buffer)
3908{
3909 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003910 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911 unsigned int efd, cfd;
3912 struct file *efile = NULL;
3913 struct file *cfile = NULL;
3914 char *endp;
3915 int ret;
3916
3917 efd = simple_strtoul(buffer, &endp, 10);
3918 if (*endp != ' ')
3919 return -EINVAL;
3920 buffer = endp + 1;
3921
3922 cfd = simple_strtoul(buffer, &endp, 10);
3923 if ((*endp != ' ') && (*endp != '\0'))
3924 return -EINVAL;
3925 buffer = endp + 1;
3926
3927 event = kzalloc(sizeof(*event), GFP_KERNEL);
3928 if (!event)
3929 return -ENOMEM;
3930 event->cgrp = cgrp;
3931 INIT_LIST_HEAD(&event->list);
3932 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3933 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3934 INIT_WORK(&event->remove, cgroup_event_remove);
3935
3936 efile = eventfd_fget(efd);
3937 if (IS_ERR(efile)) {
3938 ret = PTR_ERR(efile);
3939 goto fail;
3940 }
3941
3942 event->eventfd = eventfd_ctx_fileget(efile);
3943 if (IS_ERR(event->eventfd)) {
3944 ret = PTR_ERR(event->eventfd);
3945 goto fail;
3946 }
3947
3948 cfile = fget(cfd);
3949 if (!cfile) {
3950 ret = -EBADF;
3951 goto fail;
3952 }
3953
3954 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003955 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003956 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003957 if (ret < 0)
3958 goto fail;
3959
3960 event->cft = __file_cft(cfile);
3961 if (IS_ERR(event->cft)) {
3962 ret = PTR_ERR(event->cft);
3963 goto fail;
3964 }
3965
Li Zefanf1690072013-02-18 14:13:35 +08003966 /*
3967 * The file to be monitored must be in the same cgroup as
3968 * cgroup.event_control is.
3969 */
3970 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3971 if (cgrp_cfile != cgrp) {
3972 ret = -EINVAL;
3973 goto fail;
3974 }
3975
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003976 if (!event->cft->register_event || !event->cft->unregister_event) {
3977 ret = -EINVAL;
3978 goto fail;
3979 }
3980
3981 ret = event->cft->register_event(cgrp, event->cft,
3982 event->eventfd, buffer);
3983 if (ret)
3984 goto fail;
3985
Li Zefan7ef70e42013-04-26 11:58:03 -07003986 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003987
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08003988 /*
3989 * Events should be removed after rmdir of cgroup directory, but before
3990 * destroying subsystem state objects. Let's take reference to cgroup
3991 * directory dentry to do that.
3992 */
3993 dget(cgrp->dentry);
3994
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003995 spin_lock(&cgrp->event_list_lock);
3996 list_add(&event->list, &cgrp->event_list);
3997 spin_unlock(&cgrp->event_list_lock);
3998
3999 fput(cfile);
4000 fput(efile);
4001
4002 return 0;
4003
4004fail:
4005 if (cfile)
4006 fput(cfile);
4007
4008 if (event && event->eventfd && !IS_ERR(event->eventfd))
4009 eventfd_ctx_put(event->eventfd);
4010
4011 if (!IS_ERR_OR_NULL(efile))
4012 fput(efile);
4013
4014 kfree(event);
4015
4016 return ret;
4017}
4018
Daniel Lezcano97978e62010-10-27 15:33:35 -07004019static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4020 struct cftype *cft)
4021{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004022 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004023}
4024
4025static int cgroup_clone_children_write(struct cgroup *cgrp,
4026 struct cftype *cft,
4027 u64 val)
4028{
4029 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004030 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004031 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004032 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004033 return 0;
4034}
4035
Tejun Heod5c56ce2013-06-03 19:14:34 -07004036static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004037 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004038 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004039 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004040 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004041 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004042 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004043 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004044 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004045 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004046 .write_string = cgroup_write_event_control,
4047 .mode = S_IWUGO,
4048 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004049 {
4050 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004051 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004052 .read_u64 = cgroup_clone_children_read,
4053 .write_u64 = cgroup_clone_children_write,
4054 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004055 {
Tejun Heo873fe092013-04-14 20:15:26 -07004056 .name = "cgroup.sane_behavior",
4057 .flags = CFTYPE_ONLY_ON_ROOT,
4058 .read_seq_string = cgroup_sane_behavior_show,
4059 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004060
4061 /*
4062 * Historical crazy stuff. These don't have "cgroup." prefix and
4063 * don't exist if sane_behavior. If you're depending on these, be
4064 * prepared to be burned.
4065 */
4066 {
4067 .name = "tasks",
4068 .flags = CFTYPE_INSANE, /* use "procs" instead */
4069 .open = cgroup_tasks_open,
4070 .write_u64 = cgroup_tasks_write,
4071 .release = cgroup_pidlist_release,
4072 .mode = S_IRUGO | S_IWUSR,
4073 },
4074 {
4075 .name = "notify_on_release",
4076 .flags = CFTYPE_INSANE,
4077 .read_u64 = cgroup_read_notify_on_release,
4078 .write_u64 = cgroup_write_notify_on_release,
4079 },
Tejun Heo873fe092013-04-14 20:15:26 -07004080 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004081 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004082 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004083 .read_seq_string = cgroup_release_agent_show,
4084 .write_string = cgroup_release_agent_write,
4085 .max_write_len = PATH_MAX,
4086 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004087 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004088};
4089
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004090/**
4091 * cgroup_populate_dir - selectively creation of files in a directory
4092 * @cgrp: target cgroup
4093 * @base_files: true if the base files should be added
4094 * @subsys_mask: mask of the subsystem ids whose files should be added
4095 */
4096static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4097 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004098{
4099 int err;
4100 struct cgroup_subsys *ss;
4101
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004102 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004103 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004104 if (err < 0)
4105 return err;
4106 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004107
Tejun Heo8e3f6542012-04-01 12:09:55 -07004108 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004109 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004110 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004111 if (!test_bit(ss->subsys_id, &subsys_mask))
4112 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004113
Tejun Heodb0416b2012-04-01 12:09:55 -07004114 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004115 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004117
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004118 /* This cgroup is ready now */
4119 for_each_subsys(cgrp->root, ss) {
4120 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4121 /*
4122 * Update id->css pointer and make this css visible from
4123 * CSS ID functions. This pointer will be dereferened
4124 * from RCU-read-side without locks.
4125 */
4126 if (css->id)
4127 rcu_assign_pointer(css->id->css, css);
4128 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129
4130 return 0;
4131}
4132
Tejun Heo48ddbe12012-04-01 12:09:56 -07004133static void css_dput_fn(struct work_struct *work)
4134{
4135 struct cgroup_subsys_state *css =
4136 container_of(work, struct cgroup_subsys_state, dput_work);
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004137 struct dentry *dentry = css->cgroup->dentry;
4138 struct super_block *sb = dentry->d_sb;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004139
Tejun Heo5db9a4d2012-07-07 16:08:18 -07004140 atomic_inc(&sb->s_active);
4141 dput(dentry);
4142 deactivate_super(sb);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004143}
4144
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145static void init_cgroup_css(struct cgroup_subsys_state *css,
4146 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004147 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004148{
Paul Menagebd89aab2007-10-18 23:40:44 -07004149 css->cgroup = cgrp;
Paul Menagee7c5ec92009-01-07 18:08:38 -08004150 atomic_set(&css->refcnt, 1);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004152 css->id = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07004153 if (cgrp == dummytop)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004154 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004155 BUG_ON(cgrp->subsys[ss->subsys_id]);
4156 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004157
4158 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004159 * css holds an extra ref to @cgrp->dentry which is put on the last
4160 * css_put(). dput() requires process context, which css_put() may
4161 * be called without. @css->dput_work will be used to invoke
4162 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004163 */
4164 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165}
4166
Tejun Heob1929db2012-11-19 08:13:38 -08004167/* invoke ->post_create() on a new CSS and mark it online if successful */
4168static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004169{
Tejun Heob1929db2012-11-19 08:13:38 -08004170 int ret = 0;
4171
Tejun Heoa31f2d32012-11-19 08:13:37 -08004172 lockdep_assert_held(&cgroup_mutex);
4173
Tejun Heo92fb9742012-11-19 08:13:38 -08004174 if (ss->css_online)
4175 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004176 if (!ret)
4177 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4178 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004179}
4180
4181/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4182static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4183 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4184{
4185 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4186
4187 lockdep_assert_held(&cgroup_mutex);
4188
4189 if (!(css->flags & CSS_ONLINE))
4190 return;
4191
Li Zefand7eeac12013-03-12 15:35:59 -07004192 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004193 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004194
4195 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4196}
4197
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198/*
Li Zefana043e3b2008-02-23 15:24:09 -08004199 * cgroup_create - create a cgroup
4200 * @parent: cgroup that will be parent of the new cgroup
4201 * @dentry: dentry of the new cgroup
4202 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203 *
Li Zefana043e3b2008-02-23 15:24:09 -08004204 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004207 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208{
Tejun Heo53fa5262013-05-24 10:55:38 +09004209 static atomic64_t serial_nr_cursor = ATOMIC64_INIT(0);
Paul Menagebd89aab2007-10-18 23:40:44 -07004210 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004211 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212 struct cgroupfs_root *root = parent->root;
4213 int err = 0;
4214 struct cgroup_subsys *ss;
4215 struct super_block *sb = root->sb;
4216
Tejun Heo0a950f62012-11-19 09:02:12 -08004217 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004218 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4219 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220 return -ENOMEM;
4221
Li Zefan65dff752013-03-01 15:01:56 +08004222 name = cgroup_alloc_name(dentry);
4223 if (!name)
4224 goto err_free_cgrp;
4225 rcu_assign_pointer(cgrp->name, name);
4226
Tejun Heo0a950f62012-11-19 09:02:12 -08004227 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4228 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004229 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004230
Tejun Heo976c06b2012-11-05 09:16:59 -08004231 /*
4232 * Only live parents can have children. Note that the liveliness
4233 * check isn't strictly necessary because cgroup_mkdir() and
4234 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4235 * anyway so that locking is contained inside cgroup proper and we
4236 * don't get nasty surprises if we ever grow another caller.
4237 */
4238 if (!cgroup_lock_live_group(parent)) {
4239 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004240 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004241 }
4242
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243 /* Grab a reference on the superblock so the hierarchy doesn't
4244 * get deleted on unmount if there are child cgroups. This
4245 * can be done outside cgroup_mutex, since the sb can't
4246 * disappear while someone has an open control file on the
4247 * fs */
4248 atomic_inc(&sb->s_active);
4249
Paul Menagecc31edc2008-10-18 20:28:04 -07004250 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251
Li Zefanfe1c06c2013-01-24 14:30:22 +08004252 dentry->d_fsdata = cgrp;
4253 cgrp->dentry = dentry;
4254
Paul Menagebd89aab2007-10-18 23:40:44 -07004255 cgrp->parent = parent;
4256 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257
Li Zefanb6abdb02008-03-04 14:28:19 -08004258 if (notify_on_release(parent))
4259 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4260
Tejun Heo2260e7f2012-11-19 08:13:38 -08004261 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4262 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004263
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004265 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004266
Tejun Heo92fb9742012-11-19 08:13:38 -08004267 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268 if (IS_ERR(css)) {
4269 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004270 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271 }
Paul Menagebd89aab2007-10-18 23:40:44 -07004272 init_cgroup_css(css, ss, cgrp);
Li Zefan4528fd02010-02-02 13:44:10 -08004273 if (ss->use_id) {
4274 err = alloc_css_id(ss, parent, cgrp);
4275 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004276 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004277 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278 }
4279
Tejun Heo4e139af2012-11-19 08:13:36 -08004280 /*
4281 * Create directory. cgroup_create_file() returns with the new
4282 * directory locked on success so that it can be populated without
4283 * dropping cgroup_mutex.
4284 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004285 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004287 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004288 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004289
Tejun Heo53fa5262013-05-24 10:55:38 +09004290 /*
4291 * Assign a monotonically increasing serial number. With the list
4292 * appending below, it guarantees that sibling cgroups are always
4293 * sorted in the ascending serial number order on the parent's
4294 * ->children.
4295 */
4296 cgrp->serial_nr = atomic64_inc_return(&serial_nr_cursor);
4297
Tejun Heo4e139af2012-11-19 08:13:36 -08004298 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004299 list_add_tail(&cgrp->allcg_node, &root->allcg_list);
4300 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4301 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004302
Tejun Heob1929db2012-11-19 08:13:38 -08004303 /* each css holds a ref to the cgroup's dentry */
4304 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004305 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004306
Li Zefan415cf072013-04-08 14:35:02 +08004307 /* hold a ref to the parent's dentry */
4308 dget(parent->dentry);
4309
Tejun Heob1929db2012-11-19 08:13:38 -08004310 /* creation succeeded, notify subsystems */
4311 for_each_subsys(root, ss) {
4312 err = online_css(ss, cgrp);
4313 if (err)
4314 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004315
4316 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4317 parent->parent) {
4318 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",
4319 current->comm, current->pid, ss->name);
4320 if (!strcmp(ss->name, "memory"))
4321 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4322 ss->warned_broken_hierarchy = true;
4323 }
Tejun Heoa8638032012-11-09 09:12:29 -08004324 }
4325
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004326 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004327 if (err)
4328 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329
4330 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004331 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332
4333 return 0;
4334
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004335err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 for_each_subsys(root, ss) {
Paul Menagebd89aab2007-10-18 23:40:44 -07004337 if (cgrp->subsys[ss->subsys_id])
Tejun Heo92fb9742012-11-19 08:13:38 -08004338 ss->css_free(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004341 /* Release the reference count that we took on the superblock */
4342 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004343err_free_id:
4344 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004345err_free_name:
4346 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004347err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004348 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004349 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004350
4351err_destroy:
4352 cgroup_destroy_locked(cgrp);
4353 mutex_unlock(&cgroup_mutex);
4354 mutex_unlock(&dentry->d_inode->i_mutex);
4355 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356}
4357
Al Viro18bb1db2011-07-26 01:41:39 -04004358static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359{
4360 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4361
4362 /* the vfs holds inode->i_mutex already */
4363 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4364}
4365
Tejun Heo42809dd2012-11-19 08:13:37 -08004366static int cgroup_destroy_locked(struct cgroup *cgrp)
4367 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368{
Tejun Heo42809dd2012-11-19 08:13:37 -08004369 struct dentry *d = cgrp->dentry;
4370 struct cgroup *parent = cgrp->parent;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004371 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004372 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
Tejun Heo42809dd2012-11-19 08:13:37 -08004374 lockdep_assert_held(&d->d_inode->i_mutex);
4375 lockdep_assert_held(&cgroup_mutex);
4376
4377 if (atomic_read(&cgrp->count) || !list_empty(&cgrp->children))
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
4382 * removed. This makes future css_tryget() and child creation
4383 * attempts fail thus maintaining the removal conditions verified
4384 * above.
Tejun Heo53fa5262013-05-24 10:55:38 +09004385 *
4386 * Note that CGRP_REMVOED clearing is depended upon by
4387 * cgroup_next_sibling() to resume iteration after dropping RCU
4388 * read lock. See cgroup_next_sibling() for details.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004389 */
Tejun Heoed9577932012-11-05 09:16:58 -08004390 for_each_subsys(cgrp->root, ss) {
4391 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4392
4393 WARN_ON(atomic_read(&css->refcnt) < 0);
4394 atomic_add(CSS_DEACT_BIAS, &css->refcnt);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004395 }
Tejun Heo1a90dd52012-11-05 09:16:59 -08004396 set_bit(CGRP_REMOVED, &cgrp->flags);
4397
Tejun Heoa31f2d32012-11-19 08:13:37 -08004398 /* tell subsystems to initate destruction */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004399 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004400 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004401
4402 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004403 * Put all the base refs. Each css holds an extra reference to the
4404 * cgroup's dentry and cgroup removal proceeds regardless of css
4405 * refs. On the last put of each css, whenever that may be, the
4406 * extra dentry ref is put so that dentry destruction happens only
4407 * after all css's are released.
4408 */
Tejun Heoe9316082012-11-05 09:16:58 -08004409 for_each_subsys(cgrp->root, ss)
4410 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004412 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07004413 if (!list_empty(&cgrp->release_list))
Phil Carmody8d258792011-03-22 16:30:13 -07004414 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02004415 raw_spin_unlock(&release_list_lock);
Paul Menage999cd8a2009-01-07 18:08:36 -08004416
Paul Menage999cd8a2009-01-07 18:08:36 -08004417 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004418 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004419 list_del_init(&cgrp->allcg_node);
4420
Tejun Heo42809dd2012-11-19 08:13:37 -08004421 dget(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422 cgroup_d_remove_dir(d);
4423 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004424
Paul Menagebd89aab2007-10-18 23:40:44 -07004425 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004426 check_for_release(parent);
4427
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004428 /*
4429 * Unregister events and notify userspace.
4430 * Notify userspace about cgroup removing only after rmdir of cgroup
Li Zefan810cbee2013-02-18 18:56:14 +08004431 * directory to avoid race between userspace and kernelspace.
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004432 */
4433 spin_lock(&cgrp->event_list_lock);
Li Zefan810cbee2013-02-18 18:56:14 +08004434 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
Greg Thelen9718ceb2012-11-28 13:50:45 -08004435 list_del_init(&event->list);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004436 schedule_work(&event->remove);
4437 }
Li Zefan810cbee2013-02-18 18:56:14 +08004438 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004439
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440 return 0;
4441}
4442
Tejun Heo42809dd2012-11-19 08:13:37 -08004443static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4444{
4445 int ret;
4446
4447 mutex_lock(&cgroup_mutex);
4448 ret = cgroup_destroy_locked(dentry->d_fsdata);
4449 mutex_unlock(&cgroup_mutex);
4450
4451 return ret;
4452}
4453
Tejun Heo8e3f6542012-04-01 12:09:55 -07004454static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4455{
4456 INIT_LIST_HEAD(&ss->cftsets);
4457
4458 /*
4459 * base_cftset is embedded in subsys itself, no need to worry about
4460 * deregistration.
4461 */
4462 if (ss->base_cftypes) {
4463 ss->base_cftset.cfts = ss->base_cftypes;
4464 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4465 }
4466}
4467
Li Zefan06a11922008-04-29 01:00:07 -07004468static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004469{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004470 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004471
4472 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004473
Tejun Heo648bb562012-11-19 08:13:36 -08004474 mutex_lock(&cgroup_mutex);
4475
Tejun Heo8e3f6542012-04-01 12:09:55 -07004476 /* init base cftset */
4477 cgroup_init_cftsets(ss);
4478
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479 /* Create the top cgroup state for this subsystem */
Li Zefan33a68ac2009-01-07 18:07:42 -08004480 list_add(&ss->sibling, &rootnode.subsys_list);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004481 ss->root = &rootnode;
Tejun Heo92fb9742012-11-19 08:13:38 -08004482 css = ss->css_alloc(dummytop);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004483 /* We don't handle early failures gracefully */
4484 BUG_ON(IS_ERR(css));
4485 init_cgroup_css(css, ss, dummytop);
4486
Li Zefane8d55fd2008-04-29 01:00:13 -07004487 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004488 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004489 * newly registered, all tasks and hence the
4490 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004491 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492
4493 need_forkexit_callback |= ss->fork || ss->exit;
4494
Li Zefane8d55fd2008-04-29 01:00:13 -07004495 /* At system boot, before all subsystems have been
4496 * registered, no tasks have been forked, so we don't
4497 * need to invoke fork callbacks here. */
4498 BUG_ON(!list_empty(&init_task.tasks));
4499
Tejun Heob1929db2012-11-19 08:13:38 -08004500 BUG_ON(online_css(ss, dummytop));
Tejun Heoa8638032012-11-09 09:12:29 -08004501
Tejun Heo648bb562012-11-19 08:13:36 -08004502 mutex_unlock(&cgroup_mutex);
4503
Ben Blume6a11052010-03-10 15:22:09 -08004504 /* this function shouldn't be used with modular subsystems, since they
4505 * need to register a subsys_id, among other things */
4506 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004507}
4508
4509/**
Ben Blume6a11052010-03-10 15:22:09 -08004510 * cgroup_load_subsys: load and register a modular subsystem at runtime
4511 * @ss: the subsystem to load
4512 *
4513 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004514 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004515 * up for use. If the subsystem is built-in anyway, work is delegated to the
4516 * simpler cgroup_init_subsys.
4517 */
4518int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4519{
Ben Blume6a11052010-03-10 15:22:09 -08004520 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004521 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004522 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004523 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004524 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004525
4526 /* check name and function validity */
4527 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004528 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004529 return -EINVAL;
4530
4531 /*
4532 * we don't support callbacks in modular subsystems. this check is
4533 * before the ss->module check for consistency; a subsystem that could
4534 * be a module should still have no callbacks even if the user isn't
4535 * compiling it as one.
4536 */
4537 if (ss->fork || ss->exit)
4538 return -EINVAL;
4539
4540 /*
4541 * an optionally modular subsystem is built-in: we want to do nothing,
4542 * since cgroup_init_subsys will have already taken care of it.
4543 */
4544 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004545 /* a sanity check */
Ben Blume6a11052010-03-10 15:22:09 -08004546 BUG_ON(subsys[ss->subsys_id] != ss);
4547 return 0;
4548 }
4549
Tejun Heo8e3f6542012-04-01 12:09:55 -07004550 /* init base cftset */
4551 cgroup_init_cftsets(ss);
4552
Ben Blume6a11052010-03-10 15:22:09 -08004553 mutex_lock(&cgroup_mutex);
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004554 subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004555
4556 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004557 * no ss->css_alloc seems to need anything important in the ss
4558 * struct, so this can happen first (i.e. before the rootnode
4559 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004560 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004561 css = ss->css_alloc(dummytop);
Ben Blume6a11052010-03-10 15:22:09 -08004562 if (IS_ERR(css)) {
4563 /* failure case - need to deassign the subsys[] slot. */
Daniel Wagner8a8e04d2012-09-12 16:12:07 +02004564 subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004565 mutex_unlock(&cgroup_mutex);
4566 return PTR_ERR(css);
4567 }
4568
4569 list_add(&ss->sibling, &rootnode.subsys_list);
4570 ss->root = &rootnode;
4571
4572 /* our new subsystem will be attached to the dummy hierarchy. */
4573 init_cgroup_css(css, ss, dummytop);
4574 /* init_idr must be after init_cgroup_css because it sets css->id. */
4575 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004576 ret = cgroup_init_idr(ss, css);
4577 if (ret)
4578 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004579 }
4580
4581 /*
4582 * Now we need to entangle the css into the existing css_sets. unlike
4583 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4584 * will need a new pointer to it; done by iterating the css_set_table.
4585 * furthermore, modifying the existing css_sets will corrupt the hash
4586 * table state, so each changed css_set will need its hash recomputed.
4587 * this is all done under the css_set_lock.
4588 */
4589 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004590 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004591 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004592 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004593 continue;
4594 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004595 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004596 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004597 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004598 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004599 key = css_set_hash(cset->subsys);
4600 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004601 }
4602 write_unlock(&css_set_lock);
4603
Tejun Heob1929db2012-11-19 08:13:38 -08004604 ret = online_css(ss, dummytop);
4605 if (ret)
4606 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004607
Ben Blume6a11052010-03-10 15:22:09 -08004608 /* success! */
4609 mutex_unlock(&cgroup_mutex);
4610 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004611
4612err_unload:
4613 mutex_unlock(&cgroup_mutex);
4614 /* @ss can't be mounted here as try_module_get() would fail */
4615 cgroup_unload_subsys(ss);
4616 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004617}
4618EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4619
4620/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004621 * cgroup_unload_subsys: unload a modular subsystem
4622 * @ss: the subsystem to unload
4623 *
4624 * This function should be called in a modular subsystem's exitcall. When this
4625 * function is invoked, the refcount on the subsystem's module will be 0, so
4626 * the subsystem will not be attached to any hierarchy.
4627 */
4628void cgroup_unload_subsys(struct cgroup_subsys *ss)
4629{
Tejun Heo69d02062013-06-12 21:04:50 -07004630 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004631
4632 BUG_ON(ss->module == NULL);
4633
4634 /*
4635 * we shouldn't be called if the subsystem is in use, and the use of
4636 * try_module_get in parse_cgroupfs_options should ensure that it
4637 * doesn't start being used while we're killing it off.
4638 */
4639 BUG_ON(ss->root != &rootnode);
4640
4641 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004642
Tejun Heoa31f2d32012-11-19 08:13:37 -08004643 offline_css(ss, dummytop);
Tejun Heo02ae7482012-11-19 08:13:37 -08004644
Tejun Heoc897ff62013-02-27 17:03:49 -08004645 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004646 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004647
Ben Blumcf5d5942010-03-10 15:22:09 -08004648 /* deassign the subsys_id */
Ben Blumcf5d5942010-03-10 15:22:09 -08004649 subsys[ss->subsys_id] = NULL;
4650
4651 /* remove subsystem from rootnode's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004652 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004653
4654 /*
4655 * disentangle the css from all css_sets attached to the dummytop. as
4656 * in loading, we need to pay our respects to the hashtable gods.
4657 */
4658 write_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07004659 list_for_each_entry(link, &dummytop->cset_links, cset_link) {
4660 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004661 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004662
Tejun Heo5abb8852013-06-12 21:04:49 -07004663 hash_del(&cset->hlist);
4664 cset->subsys[ss->subsys_id] = NULL;
4665 key = css_set_hash(cset->subsys);
4666 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004667 }
4668 write_unlock(&css_set_lock);
4669
4670 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004671 * remove subsystem's css from the dummytop and free it - need to
4672 * free before marking as null because ss->css_free needs the
4673 * cgrp->subsys pointer to find their state. note that this also
4674 * takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004675 */
Tejun Heo92fb9742012-11-19 08:13:38 -08004676 ss->css_free(dummytop);
Ben Blumcf5d5942010-03-10 15:22:09 -08004677 dummytop->subsys[ss->subsys_id] = NULL;
4678
4679 mutex_unlock(&cgroup_mutex);
4680}
4681EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4682
4683/**
Li Zefana043e3b2008-02-23 15:24:09 -08004684 * cgroup_init_early - cgroup initialization at system boot
4685 *
4686 * Initialize cgroups at system boot, and initialize any
4687 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688 */
4689int __init cgroup_init_early(void)
4690{
4691 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004692 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004693 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004694 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004695 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004696 css_set_count = 1;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004697 init_cgroup_root(&rootnode);
Paul Menage817929e2007-10-18 23:39:36 -07004698 root_count = 1;
4699 init_task.cgroups = &init_css_set;
4700
Tejun Heo69d02062013-06-12 21:04:50 -07004701 init_cgrp_cset_link.cset = &init_css_set;
4702 init_cgrp_cset_link.cgrp = dummytop;
4703 list_add(&init_cgrp_cset_link.cset_link, &rootnode.top_cgroup.cset_links);
4704 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004705
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004706 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004707 struct cgroup_subsys *ss = subsys[i];
4708
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004709 /* at bootup time, we don't worry about modular subsystems */
4710 if (!ss || ss->module)
4711 continue;
4712
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713 BUG_ON(!ss->name);
4714 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004715 BUG_ON(!ss->css_alloc);
4716 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004717 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004718 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004719 ss->name, ss->subsys_id);
4720 BUG();
4721 }
4722
4723 if (ss->early_init)
4724 cgroup_init_subsys(ss);
4725 }
4726 return 0;
4727}
4728
4729/**
Li Zefana043e3b2008-02-23 15:24:09 -08004730 * cgroup_init - cgroup initialization
4731 *
4732 * Register cgroup filesystem and /proc file, and initialize
4733 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004734 */
4735int __init cgroup_init(void)
4736{
4737 int err;
4738 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004739 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004740
4741 err = bdi_init(&cgroup_backing_dev_info);
4742 if (err)
4743 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004744
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004745 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004746 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004747
4748 /* at bootup time, we don't worry about modular subsystems */
4749 if (!ss || ss->module)
4750 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004751 if (!ss->early_init)
4752 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004753 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004754 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004755 }
4756
Li Zefan472b1052008-04-29 01:00:11 -07004757 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004758 key = css_set_hash(init_css_set.subsys);
4759 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004760
4761 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004762 mutex_lock(&cgroup_mutex);
4763 mutex_lock(&cgroup_root_mutex);
4764
Tejun Heofa3ca072013-04-14 11:36:56 -07004765 BUG_ON(cgroup_init_root_id(&rootnode));
Greg KH676db4a2010-08-05 13:53:35 -07004766
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004767 mutex_unlock(&cgroup_root_mutex);
4768 mutex_unlock(&cgroup_mutex);
4769
Greg KH676db4a2010-08-05 13:53:35 -07004770 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4771 if (!cgroup_kobj) {
4772 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004773 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004774 }
4775
4776 err = register_filesystem(&cgroup_fs_type);
4777 if (err < 0) {
4778 kobject_put(cgroup_kobj);
4779 goto out;
4780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004781
Li Zefan46ae2202008-04-29 01:00:08 -07004782 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004783
Paul Menageddbcc7e2007-10-18 23:39:30 -07004784out:
Paul Menagea4243162007-10-18 23:39:35 -07004785 if (err)
4786 bdi_destroy(&cgroup_backing_dev_info);
4787
Paul Menageddbcc7e2007-10-18 23:39:30 -07004788 return err;
4789}
Paul Menageb4f48b62007-10-18 23:39:33 -07004790
Paul Menagea4243162007-10-18 23:39:35 -07004791/*
4792 * proc_cgroup_show()
4793 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4794 * - Used for /proc/<pid>/cgroup.
4795 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4796 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004797 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004798 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4799 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4800 * cgroup to top_cgroup.
4801 */
4802
4803/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004804int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004805{
4806 struct pid *pid;
4807 struct task_struct *tsk;
4808 char *buf;
4809 int retval;
4810 struct cgroupfs_root *root;
4811
4812 retval = -ENOMEM;
4813 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4814 if (!buf)
4815 goto out;
4816
4817 retval = -ESRCH;
4818 pid = m->private;
4819 tsk = get_pid_task(pid, PIDTYPE_PID);
4820 if (!tsk)
4821 goto out_free;
4822
4823 retval = 0;
4824
4825 mutex_lock(&cgroup_mutex);
4826
Li Zefane5f6a862009-01-07 18:07:41 -08004827 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004828 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004829 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004830 int count = 0;
4831
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004832 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004833 for_each_subsys(root, ss)
4834 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004835 if (strlen(root->name))
4836 seq_printf(m, "%sname=%s", count ? "," : "",
4837 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004838 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004839 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004840 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004841 if (retval < 0)
4842 goto out_unlock;
4843 seq_puts(m, buf);
4844 seq_putc(m, '\n');
4845 }
4846
4847out_unlock:
4848 mutex_unlock(&cgroup_mutex);
4849 put_task_struct(tsk);
4850out_free:
4851 kfree(buf);
4852out:
4853 return retval;
4854}
4855
Paul Menagea4243162007-10-18 23:39:35 -07004856/* Display information about each subsystem and each hierarchy */
4857static int proc_cgroupstats_show(struct seq_file *m, void *v)
4858{
4859 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004860
Paul Menage8bab8dd2008-04-04 14:29:57 -07004861 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004862 /*
4863 * ideally we don't want subsystems moving around while we do this.
4864 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4865 * subsys/hierarchy state.
4866 */
Paul Menagea4243162007-10-18 23:39:35 -07004867 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07004868 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
4869 struct cgroup_subsys *ss = subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08004870 if (ss == NULL)
4871 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004872 seq_printf(m, "%s\t%d\t%d\t%d\n",
4873 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004874 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07004875 }
4876 mutex_unlock(&cgroup_mutex);
4877 return 0;
4878}
4879
4880static int cgroupstats_open(struct inode *inode, struct file *file)
4881{
Al Viro9dce07f2008-03-29 03:07:28 +00004882 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004883}
4884
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004885static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004886 .open = cgroupstats_open,
4887 .read = seq_read,
4888 .llseek = seq_lseek,
4889 .release = single_release,
4890};
4891
Paul Menageb4f48b62007-10-18 23:39:33 -07004892/**
4893 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004894 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004895 *
4896 * Description: A task inherits its parent's cgroup at fork().
4897 *
4898 * A pointer to the shared css_set was automatically copied in
4899 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004900 * it was not made under the protection of RCU or cgroup_mutex, so
4901 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4902 * have already changed current->cgroups, allowing the previously
4903 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004904 *
4905 * At the point that cgroup_fork() is called, 'current' is the parent
4906 * task, and the passed argument 'child' points to the child task.
4907 */
4908void cgroup_fork(struct task_struct *child)
4909{
Tejun Heo9bb71302012-10-18 17:52:07 -07004910 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004911 child->cgroups = current->cgroups;
4912 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07004913 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004914 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004915}
4916
4917/**
Li Zefana043e3b2008-02-23 15:24:09 -08004918 * cgroup_post_fork - called on a new task after adding it to the task list
4919 * @child: the task in question
4920 *
Tejun Heo5edee612012-10-16 15:03:14 -07004921 * Adds the task to the list running through its css_set if necessary and
4922 * call the subsystem fork() callbacks. Has to be after the task is
4923 * visible on the task list in case we race with the first call to
4924 * cgroup_iter_start() - to guarantee that the new task ends up on its
4925 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004926 */
Paul Menage817929e2007-10-18 23:39:36 -07004927void cgroup_post_fork(struct task_struct *child)
4928{
Tejun Heo5edee612012-10-16 15:03:14 -07004929 int i;
4930
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004931 /*
4932 * use_task_css_set_links is set to 1 before we walk the tasklist
4933 * under the tasklist_lock and we read it here after we added the child
4934 * to the tasklist under the tasklist_lock as well. If the child wasn't
4935 * yet in the tasklist when we walked through it from
4936 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4937 * should be visible now due to the paired locking and barriers implied
4938 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4939 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4940 * lock on fork.
4941 */
Paul Menage817929e2007-10-18 23:39:36 -07004942 if (use_task_css_set_links) {
4943 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004944 task_lock(child);
4945 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07004946 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004947 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004948 write_unlock(&css_set_lock);
4949 }
Tejun Heo5edee612012-10-16 15:03:14 -07004950
4951 /*
4952 * Call ss->fork(). This must happen after @child is linked on
4953 * css_set; otherwise, @child might change state between ->fork()
4954 * and addition to css_set.
4955 */
4956 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08004957 /*
4958 * fork/exit callbacks are supported only for builtin
4959 * subsystems, and the builtin section of the subsys
4960 * array is immutable, so we don't need to lock the
4961 * subsys array here. On the other hand, modular section
4962 * of the array can be freed at module unload, so we
4963 * can't touch that.
4964 */
4965 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo5edee612012-10-16 15:03:14 -07004966 struct cgroup_subsys *ss = subsys[i];
4967
Tejun Heo5edee612012-10-16 15:03:14 -07004968 if (ss->fork)
4969 ss->fork(child);
4970 }
4971 }
Paul Menage817929e2007-10-18 23:39:36 -07004972}
Tejun Heo5edee612012-10-16 15:03:14 -07004973
Paul Menage817929e2007-10-18 23:39:36 -07004974/**
Paul Menageb4f48b62007-10-18 23:39:33 -07004975 * cgroup_exit - detach cgroup from exiting task
4976 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08004977 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07004978 *
4979 * Description: Detach cgroup from @tsk and release it.
4980 *
4981 * Note that cgroups marked notify_on_release force every task in
4982 * them to take the global cgroup_mutex mutex when exiting.
4983 * This could impact scaling on very large systems. Be reluctant to
4984 * use notify_on_release cgroups where very high task exit scaling
4985 * is required on large systems.
4986 *
4987 * the_top_cgroup_hack:
4988 *
4989 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
4990 *
4991 * We call cgroup_exit() while the task is still competent to
4992 * handle notify_on_release(), then leave the task attached to the
4993 * root cgroup in each hierarchy for the remainder of its exit.
4994 *
4995 * To do this properly, we would increment the reference count on
4996 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
4997 * code we would add a second cgroup function call, to drop that
4998 * reference. This would just create an unnecessary hot spot on
4999 * the top_cgroup reference count, to no avail.
5000 *
5001 * Normally, holding a reference to a cgroup without bumping its
5002 * count is unsafe. The cgroup could go away, or someone could
5003 * attach us to a different cgroup, decrementing the count on
5004 * the first cgroup that we never incremented. But in this case,
5005 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005006 * which wards off any cgroup_attach_task() attempts, or task is a failed
5007 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005008 */
5009void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5010{
Tejun Heo5abb8852013-06-12 21:04:49 -07005011 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005012 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005013
5014 /*
5015 * Unlink from the css_set task list if necessary.
5016 * Optimistically check cg_list before taking
5017 * css_set_lock
5018 */
5019 if (!list_empty(&tsk->cg_list)) {
5020 write_lock(&css_set_lock);
5021 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005022 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005023 write_unlock(&css_set_lock);
5024 }
5025
Paul Menageb4f48b62007-10-18 23:39:33 -07005026 /* Reassign the task to the init_css_set. */
5027 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005028 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005029 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005030
5031 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005032 /*
5033 * fork/exit callbacks are supported only for builtin
5034 * subsystems, see cgroup_post_fork() for details.
5035 */
5036 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005037 struct cgroup_subsys *ss = subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005038
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005039 if (ss->exit) {
5040 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005041 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005042 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef52012-01-31 13:47:36 +08005043 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005044 }
5045 }
5046 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005047 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005048
Tejun Heo5abb8852013-06-12 21:04:49 -07005049 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005050}
Paul Menage697f4162007-10-18 23:39:34 -07005051
Paul Menagebd89aab2007-10-18 23:40:44 -07005052static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005053{
5054 /* All of these checks rely on RCU to keep the cgroup
5055 * structure alive */
Li Zefanf50daa72013-03-01 15:06:07 +08005056 if (cgroup_is_releasable(cgrp) &&
5057 !atomic_read(&cgrp->count) && list_empty(&cgrp->children)) {
5058 /*
5059 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005060 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005061 * it now
5062 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005063 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005064
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005065 raw_spin_lock(&release_list_lock);
Paul Menagebd89aab2007-10-18 23:40:44 -07005066 if (!cgroup_is_removed(cgrp) &&
5067 list_empty(&cgrp->release_list)) {
5068 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005069 need_schedule_work = 1;
5070 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005071 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005072 if (need_schedule_work)
5073 schedule_work(&release_agent_work);
5074 }
5075}
5076
Daisuke Nishimurad7b9fff2010-03-10 15:22:05 -08005077/* Caller must verify that the css is not for root cgroup */
Tejun Heo28b4c272012-04-01 12:09:56 -07005078bool __css_tryget(struct cgroup_subsys_state *css)
5079{
Tejun Heoe9316082012-11-05 09:16:58 -08005080 while (true) {
5081 int t, v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005082
Tejun Heoe9316082012-11-05 09:16:58 -08005083 v = css_refcnt(css);
5084 t = atomic_cmpxchg(&css->refcnt, v, v + 1);
5085 if (likely(t == v))
Tejun Heo28b4c272012-04-01 12:09:56 -07005086 return true;
Tejun Heoe9316082012-11-05 09:16:58 -08005087 else if (t < 0)
5088 return false;
Tejun Heo28b4c272012-04-01 12:09:56 -07005089 cpu_relax();
Tejun Heoe9316082012-11-05 09:16:58 -08005090 }
Tejun Heo28b4c272012-04-01 12:09:56 -07005091}
5092EXPORT_SYMBOL_GPL(__css_tryget);
5093
5094/* Caller must verify that the css is not for root cgroup */
5095void __css_put(struct cgroup_subsys_state *css)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005096{
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005097 int v;
Tejun Heo28b4c272012-04-01 12:09:56 -07005098
Salman Qazi8e3bbf42012-06-14 14:55:30 -07005099 v = css_unbias_refcnt(atomic_dec_return(&css->refcnt));
Li Zefanf50daa72013-03-01 15:06:07 +08005100 if (v == 0)
Tejun Heoed9577932012-11-05 09:16:58 -08005101 schedule_work(&css->dput_work);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005102}
Ben Blum67523c42010-03-10 15:22:11 -08005103EXPORT_SYMBOL_GPL(__css_put);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005104
5105/*
5106 * Notify userspace when a cgroup is released, by running the
5107 * configured release agent with the name of the cgroup (path
5108 * relative to the root of cgroup file system) as the argument.
5109 *
5110 * Most likely, this user command will try to rmdir this cgroup.
5111 *
5112 * This races with the possibility that some other task will be
5113 * attached to this cgroup before it is removed, or that some other
5114 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5115 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5116 * unused, and this cgroup will be reprieved from its death sentence,
5117 * to continue to serve a useful existence. Next time it's released,
5118 * we will get notified again, if it still has 'notify_on_release' set.
5119 *
5120 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5121 * means only wait until the task is successfully execve()'d. The
5122 * separate release agent task is forked by call_usermodehelper(),
5123 * then control in this thread returns here, without waiting for the
5124 * release agent task. We don't bother to wait because the caller of
5125 * this routine has no use for the exit status of the release agent
5126 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005127 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005128static void cgroup_release_agent(struct work_struct *work)
5129{
5130 BUG_ON(work != &release_agent_work);
5131 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005132 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005133 while (!list_empty(&release_list)) {
5134 char *argv[3], *envp[3];
5135 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07005136 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005137 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 struct cgroup,
5139 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005140 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005141 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005142 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005143 if (!pathbuf)
5144 goto continue_free;
5145 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5146 goto continue_free;
5147 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5148 if (!agentbuf)
5149 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005150
5151 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005152 argv[i++] = agentbuf;
5153 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005154 argv[i] = NULL;
5155
5156 i = 0;
5157 /* minimal command environment */
5158 envp[i++] = "HOME=/";
5159 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5160 envp[i] = NULL;
5161
5162 /* Drop the lock while we invoke the usermode helper,
5163 * since the exec could involve hitting disk and hence
5164 * be a slow process */
5165 mutex_unlock(&cgroup_mutex);
5166 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005167 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005168 continue_free:
5169 kfree(pathbuf);
5170 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005171 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005172 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005173 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005174 mutex_unlock(&cgroup_mutex);
5175}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005176
5177static int __init cgroup_disable(char *str)
5178{
5179 int i;
5180 char *token;
5181
5182 while ((token = strsep(&str, ",")) != NULL) {
5183 if (!*token)
5184 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005185 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005186 struct cgroup_subsys *ss = subsys[i];
5187
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005188 /*
5189 * cgroup_disable, being at boot time, can't
5190 * know about module subsystems, so we don't
5191 * worry about them.
5192 */
5193 if (!ss || ss->module)
5194 continue;
5195
Paul Menage8bab8dd2008-04-04 14:29:57 -07005196 if (!strcmp(token, ss->name)) {
5197 ss->disabled = 1;
5198 printk(KERN_INFO "Disabling %s control group"
5199 " subsystem\n", ss->name);
5200 break;
5201 }
5202 }
5203 }
5204 return 1;
5205}
5206__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005207
5208/*
5209 * Functons for CSS ID.
5210 */
5211
5212/*
5213 *To get ID other than 0, this should be called when !cgroup_is_removed().
5214 */
5215unsigned short css_id(struct cgroup_subsys_state *css)
5216{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005217 struct css_id *cssid;
5218
5219 /*
5220 * This css_id() can return correct value when somone has refcnt
5221 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5222 * it's unchanged until freed.
5223 */
Tejun Heo28b4c272012-04-01 12:09:56 -07005224 cssid = rcu_dereference_check(css->id, css_refcnt(css));
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005225
5226 if (cssid)
5227 return cssid->id;
5228 return 0;
5229}
Ben Blum67523c42010-03-10 15:22:11 -08005230EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005231
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005232/**
5233 * css_is_ancestor - test "root" css is an ancestor of "child"
5234 * @child: the css to be tested.
5235 * @root: the css supporsed to be an ancestor of the child.
5236 *
5237 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005238 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005239 * But, considering usual usage, the csses should be valid objects after test.
5240 * Assuming that the caller will do some action to the child if this returns
5241 * returns true, the caller must take "child";s reference count.
5242 * If "child" is valid object and this returns true, "root" is valid, too.
5243 */
5244
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005245bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005246 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005247{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005248 struct css_id *child_id;
5249 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005250
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005251 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005252 if (!child_id)
5253 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005254 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005255 if (!root_id)
5256 return false;
5257 if (child_id->depth < root_id->depth)
5258 return false;
5259 if (child_id->stack[root_id->depth] != root_id->id)
5260 return false;
5261 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005262}
5263
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005264void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5265{
5266 struct css_id *id = css->id;
5267 /* When this is called before css_id initialization, id can be NULL */
5268 if (!id)
5269 return;
5270
5271 BUG_ON(!ss->use_id);
5272
5273 rcu_assign_pointer(id->css, NULL);
5274 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005275 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005276 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005277 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005278 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005279}
Ben Blum67523c42010-03-10 15:22:11 -08005280EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005281
5282/*
5283 * This is called by init or create(). Then, calls to this function are
5284 * always serialized (By cgroup_mutex() at create()).
5285 */
5286
5287static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5288{
5289 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005290 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005291
5292 BUG_ON(!ss->use_id);
5293
5294 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5295 newid = kzalloc(size, GFP_KERNEL);
5296 if (!newid)
5297 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005298
5299 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005300 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005301 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005302 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005303 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005304 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005305
5306 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005307 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005309
Tejun Heod228d9e2013-02-27 17:04:54 -08005310 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005311 newid->depth = depth;
5312 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005313err_out:
5314 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005315 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005316
5317}
5318
Ben Blume6a11052010-03-10 15:22:09 -08005319static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5320 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321{
5322 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005323
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005324 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005325 idr_init(&ss->idr);
5326
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005327 newid = get_new_cssid(ss, 0);
5328 if (IS_ERR(newid))
5329 return PTR_ERR(newid);
5330
5331 newid->stack[0] = newid->id;
5332 newid->css = rootcss;
5333 rootcss->id = newid;
5334 return 0;
5335}
5336
5337static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5338 struct cgroup *child)
5339{
5340 int subsys_id, i, depth = 0;
5341 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005342 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005343
5344 subsys_id = ss->subsys_id;
5345 parent_css = parent->subsys[subsys_id];
5346 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005348 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005349
5350 child_id = get_new_cssid(ss, depth);
5351 if (IS_ERR(child_id))
5352 return PTR_ERR(child_id);
5353
5354 for (i = 0; i < depth; i++)
5355 child_id->stack[i] = parent_id->stack[i];
5356 child_id->stack[depth] = child_id->id;
5357 /*
5358 * child_id->css pointer will be set after this cgroup is available
5359 * see cgroup_populate_dir()
5360 */
5361 rcu_assign_pointer(child_css->id, child_id);
5362
5363 return 0;
5364}
5365
5366/**
5367 * css_lookup - lookup css by id
5368 * @ss: cgroup subsys to be looked into.
5369 * @id: the id
5370 *
5371 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5372 * NULL if not. Should be called under rcu_read_lock()
5373 */
5374struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5375{
5376 struct css_id *cssid = NULL;
5377
5378 BUG_ON(!ss->use_id);
5379 cssid = idr_find(&ss->idr, id);
5380
5381 if (unlikely(!cssid))
5382 return NULL;
5383
5384 return rcu_dereference(cssid->css);
5385}
Ben Blum67523c42010-03-10 15:22:11 -08005386EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005387
Stephane Eraniane5d13672011-02-14 11:20:01 +02005388/*
5389 * get corresponding css from file open on cgroupfs directory
5390 */
5391struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5392{
5393 struct cgroup *cgrp;
5394 struct inode *inode;
5395 struct cgroup_subsys_state *css;
5396
Al Viro496ad9a2013-01-23 17:07:38 -05005397 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005398 /* check in cgroup filesystem dir */
5399 if (inode->i_op != &cgroup_dir_inode_operations)
5400 return ERR_PTR(-EBADF);
5401
5402 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5403 return ERR_PTR(-EINVAL);
5404
5405 /* get cgroup */
5406 cgrp = __d_cgrp(f->f_dentry);
5407 css = cgrp->subsys[id];
5408 return css ? css : ERR_PTR(-ENOENT);
5409}
5410
Paul Menagefe693432009-09-23 15:56:20 -07005411#ifdef CONFIG_CGROUP_DEBUG
Tejun Heo92fb9742012-11-19 08:13:38 -08005412static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005413{
5414 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5415
5416 if (!css)
5417 return ERR_PTR(-ENOMEM);
5418
5419 return css;
5420}
5421
Tejun Heo92fb9742012-11-19 08:13:38 -08005422static void debug_css_free(struct cgroup *cont)
Paul Menagefe693432009-09-23 15:56:20 -07005423{
5424 kfree(cont->subsys[debug_subsys_id]);
5425}
5426
5427static u64 cgroup_refcount_read(struct cgroup *cont, struct cftype *cft)
5428{
5429 return atomic_read(&cont->count);
5430}
5431
5432static 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 {
5513 .name = "cgroup_refcount",
5514 .read_u64 = cgroup_refcount_read,
5515 },
5516 {
5517 .name = "taskcount",
5518 .read_u64 = debug_taskcount_read,
5519 },
5520
5521 {
5522 .name = "current_css_set",
5523 .read_u64 = current_css_set_read,
5524 },
5525
5526 {
5527 .name = "current_css_set_refcount",
5528 .read_u64 = current_css_set_refcount_read,
5529 },
5530
5531 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005532 .name = "current_css_set_cg_links",
5533 .read_seq_string = current_css_set_cg_links_read,
5534 },
5535
5536 {
5537 .name = "cgroup_css_links",
5538 .read_seq_string = cgroup_css_links_read,
5539 },
5540
5541 {
Paul Menagefe693432009-09-23 15:56:20 -07005542 .name = "releasable",
5543 .read_u64 = releasable_read,
5544 },
Paul Menagefe693432009-09-23 15:56:20 -07005545
Tejun Heo4baf6e32012-04-01 12:09:55 -07005546 { } /* terminate */
5547};
Paul Menagefe693432009-09-23 15:56:20 -07005548
5549struct cgroup_subsys debug_subsys = {
5550 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005551 .css_alloc = debug_css_alloc,
5552 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005553 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005554 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005555};
5556#endif /* CONFIG_CGROUP_DEBUG */