blob: 7030f04f2890219fc428163e10b0056d207a3815 [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/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080050#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080053#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040054#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080058#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020059#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070060
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070062
Tejun Heoe25e2cb2011-12-12 18:12:21 -080063/*
Tejun Heob1a21362013-11-29 10:42:58 -050064 * pidlists linger the following amount before being destroyed. The goal
65 * is avoiding frequent destruction in the middle of consecutive read calls
66 * Expiring in the middle is a performance problem not a correctness one.
67 * 1 sec should be enough.
68 */
69#define CGROUP_PIDLIST_DESTROY_DELAY HZ
70
71/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080072 * cgroup_mutex is the master lock. Any modification to cgroup or its
73 * hierarchy must be performed while holding it.
74 *
75 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
76 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
77 * release_agent_path and so on. Modifying requires both cgroup_mutex and
78 * cgroup_root_mutex. Readers can acquire either of the two. This is to
79 * break the following locking order cycle.
80 *
81 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
82 * B. namespace_sem -> cgroup_mutex
83 *
84 * B happens only through cgroup_show_options() and using cgroup_root_mutex
85 * breaks it.
86 */
Tejun Heo22194492013-04-07 09:29:51 -070087#ifdef CONFIG_PROVE_RCU
88DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040089EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070092#endif
93
Tejun Heoe25e2cb2011-12-12 18:12:21 -080094static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070095
Tejun Heo87fb54f2013-12-06 15:11:55 -050096#define cgroup_assert_mutex_or_rcu_locked() \
97 rcu_lockdep_assert(rcu_read_lock_held() || \
98 lockdep_is_held(&cgroup_mutex), \
99 "cgroup_mutex or RCU read lock required");
100
Tejun Heo780cd8b2013-12-06 15:11:56 -0500101#ifdef CONFIG_LOCKDEP
102#define cgroup_assert_mutex_or_root_locked() \
103 WARN_ON_ONCE(debug_locks && (!lockdep_is_held(&cgroup_mutex) && \
104 !lockdep_is_held(&cgroup_root_mutex)))
105#else
106#define cgroup_assert_mutex_or_root_locked() do { } while (0)
107#endif
108
Ben Blumaae8aab2010-03-10 15:22:07 -0800109/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500110 * cgroup destruction makes heavy use of work items and there can be a lot
111 * of concurrent destructions. Use a separate workqueue so that cgroup
112 * destruction work items don't end up filling up max_active of system_wq
113 * which may lead to deadlock.
114 */
115static struct workqueue_struct *cgroup_destroy_wq;
116
117/*
Tejun Heob1a21362013-11-29 10:42:58 -0500118 * pidlist destructions need to be flushed on cgroup destruction. Use a
119 * separate workqueue as flush domain.
120 */
121static struct workqueue_struct *cgroup_pidlist_destroy_wq;
122
123/*
Ben Blumaae8aab2010-03-10 15:22:07 -0800124 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +0200125 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800126 * registered after that. The mutable section of this array is protected by
127 * cgroup_mutex.
128 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200129#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200130#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700131static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132#include <linux/cgroup_subsys.h>
133};
134
Paul Menageddbcc7e2007-10-18 23:39:30 -0700135/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700136 * The dummy hierarchy, reserved for the subsystems that are otherwise
137 * unattached - it never has more than a single cgroup, and all tasks are
138 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700139 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700140static struct cgroupfs_root cgroup_dummy_root;
141
142/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
143static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700144
145/* The list of hierarchy roots */
146
Tejun Heo9871bf92013-06-24 15:21:47 -0700147static LIST_HEAD(cgroup_roots);
148static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700149
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700150/*
151 * Hierarchy ID allocation and mapping. It follows the same exclusion
152 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
153 * writes, either for reads.
154 */
Tejun Heo1a574232013-04-14 11:36:58 -0700155static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700156
Li Zefan65dff752013-03-01 15:01:56 +0800157static struct cgroup_name root_cgroup_name = { .name = "/" };
158
Li Zefan794611a2013-06-18 18:53:53 +0800159/*
160 * Assign a monotonically increasing serial number to cgroups. It
161 * guarantees cgroups with bigger numbers are newer than those with smaller
162 * numbers. Also, as cgroups are always appended to the parent's
163 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700164 * the ascending serial number order on the list. Protected by
165 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800166 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700167static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800168
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800170 * check for fork/exit handlers to call. This avoids us having to do
171 * extra work in the fork/exit path if none of the subsystems need to
172 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700173 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700174static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700175
Tejun Heo628f7cd2013-06-28 16:24:11 -0700176static struct cftype cgroup_base_files[];
177
Tejun Heof20104d2013-08-13 20:22:50 -0400178static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800179static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400180static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
181 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500182static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500183static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800184
Tejun Heo95109b62013-08-08 20:11:27 -0400185/**
186 * cgroup_css - obtain a cgroup's css for the specified subsystem
187 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400188 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400189 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400190 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
191 * function must be called either under cgroup_mutex or rcu_read_lock() and
192 * the caller is responsible for pinning the returned css if it wants to
193 * keep accessing it outside the said locks. This function may return
194 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400195 */
196static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400197 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400198{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400199 if (ss)
200 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
201 lockdep_is_held(&cgroup_mutex));
202 else
203 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400204}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700205
Paul Menageddbcc7e2007-10-18 23:39:30 -0700206/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700207static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700208{
Tejun Heo54766d42013-06-12 21:04:53 -0700209 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700210}
211
Li Zefan78574cf2013-04-08 19:00:38 -0700212/**
213 * cgroup_is_descendant - test ancestry
214 * @cgrp: the cgroup to be tested
215 * @ancestor: possible ancestor of @cgrp
216 *
217 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
218 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
219 * and @ancestor are accessible.
220 */
221bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
222{
223 while (cgrp) {
224 if (cgrp == ancestor)
225 return true;
226 cgrp = cgrp->parent;
227 }
228 return false;
229}
230EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700231
Adrian Bunke9685a02008-02-07 00:13:46 -0800232static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700233{
234 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700235 (1 << CGRP_RELEASABLE) |
236 (1 << CGRP_NOTIFY_ON_RELEASE);
237 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700238}
239
Adrian Bunke9685a02008-02-07 00:13:46 -0800240static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700241{
Paul Menagebd89aab2007-10-18 23:40:44 -0700242 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700243}
244
Tejun Heo30159ec2013-06-25 11:53:37 -0700245/**
Tejun Heo1c6727a2013-12-06 15:11:56 -0500246 * for_each_css - iterate all css's of a cgroup
247 * @css: the iteration cursor
248 * @ssid: the index of the subsystem, CGROUP_SUBSYS_COUNT after reaching the end
249 * @cgrp: the target cgroup to iterate css's of
250 *
251 * Should be called under cgroup_mutex.
252 */
253#define for_each_css(css, ssid, cgrp) \
254 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
255 if (!((css) = rcu_dereference_check( \
256 (cgrp)->subsys[(ssid)], \
257 lockdep_is_held(&cgroup_mutex)))) { } \
258 else
259
260/**
Tejun Heo30159ec2013-06-25 11:53:37 -0700261 * for_each_subsys - iterate all loaded cgroup subsystems
262 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500263 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700264 *
Tejun Heo780cd8b2013-12-06 15:11:56 -0500265 * Iterates through all loaded subsystems. Should be called under
266 * cgroup_mutex or cgroup_root_mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700267 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500268#define for_each_subsys(ss, ssid) \
269 for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \
270 (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
271 if (!((ss) = cgroup_subsys[(ssid)])) { } \
Tejun Heo30159ec2013-06-25 11:53:37 -0700272 else
273
274/**
275 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
276 * @ss: the iteration cursor
277 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
278 *
279 * Bulit-in subsystems are always present and iteration itself doesn't
280 * require any synchronization.
281 */
282#define for_each_builtin_subsys(ss, i) \
283 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
284 (((ss) = cgroup_subsys[i]) || true); (i)++)
285
Tejun Heo5549c492013-06-24 15:21:48 -0700286/* iterate across the active hierarchies */
287#define for_each_active_root(root) \
288 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700289
Tejun Heof6ea9372012-04-01 12:09:55 -0700290static inline struct cgroup *__d_cgrp(struct dentry *dentry)
291{
292 return dentry->d_fsdata;
293}
294
Tejun Heo05ef1d72012-04-01 12:09:56 -0700295static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700296{
297 return dentry->d_fsdata;
298}
299
Tejun Heo05ef1d72012-04-01 12:09:56 -0700300static inline struct cftype *__d_cft(struct dentry *dentry)
301{
302 return __d_cfe(dentry)->type;
303}
304
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305/**
306 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
307 * @cgrp: the cgroup to be checked for liveness
308 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700309 * On success, returns true; the mutex should be later unlocked. On
310 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700311 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700312static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700313{
314 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700315 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700316 mutex_unlock(&cgroup_mutex);
317 return false;
318 }
319 return true;
320}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700321
Paul Menage81a6a5c2007-10-18 23:39:38 -0700322/* the list of cgroups eligible for automatic release. Protected by
323 * release_list_lock */
324static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200325static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700326static void cgroup_release_agent(struct work_struct *work);
327static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700328static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329
Tejun Heo69d02062013-06-12 21:04:50 -0700330/*
331 * A cgroup can be associated with multiple css_sets as different tasks may
332 * belong to different cgroups on different hierarchies. In the other
333 * direction, a css_set is naturally associated with multiple cgroups.
334 * This M:N relationship is represented by the following link structure
335 * which exists for each association and allows traversing the associations
336 * from both sides.
337 */
338struct cgrp_cset_link {
339 /* the cgroup and css_set this link associates */
340 struct cgroup *cgrp;
341 struct css_set *cset;
342
343 /* list of cgrp_cset_links anchored at cgrp->cset_links */
344 struct list_head cset_link;
345
346 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
347 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700348};
349
350/* The default css_set - used by init and its children prior to any
351 * hierarchies being mounted. It contains a pointer to the root state
352 * for each subsystem. Also used to anchor the list of css_sets. Not
353 * reference-counted, to improve performance when child cgroups
354 * haven't been created.
355 */
356
357static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700358static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700359
Tejun Heo0942eee2013-08-08 20:11:26 -0400360/*
361 * css_set_lock protects the list of css_set objects, and the chain of
362 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400363 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400364 */
Paul Menage817929e2007-10-18 23:39:36 -0700365static DEFINE_RWLOCK(css_set_lock);
366static int css_set_count;
367
Paul Menage7717f7b2009-09-23 15:56:22 -0700368/*
369 * hash table for cgroup groups. This improves the performance to find
370 * an existing css_set. This hash doesn't (currently) take into
371 * account cgroups in empty hierarchies.
372 */
Li Zefan472b1052008-04-29 01:00:11 -0700373#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800374static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700375
Li Zefan0ac801f2013-01-10 11:49:27 +0800376static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700377{
Li Zefan0ac801f2013-01-10 11:49:27 +0800378 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700379 struct cgroup_subsys *ss;
380 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700381
Tejun Heo30159ec2013-06-25 11:53:37 -0700382 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800383 key += (unsigned long)css[i];
384 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700385
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700387}
388
Tejun Heo0942eee2013-08-08 20:11:26 -0400389/*
390 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400391 * until after the first call to css_task_iter_start(). This reduces the
392 * fork()/exit() overhead for people who have cgroups compiled into their
393 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400394 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700395static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700396
Tejun Heo5abb8852013-06-12 21:04:49 -0700397static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700398{
Tejun Heo69d02062013-06-12 21:04:50 -0700399 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700400
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700401 /*
402 * Ensure that the refcount doesn't hit zero while any readers
403 * can see it. Similar to atomic_dec_and_lock(), but for an
404 * rwlock
405 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700406 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 return;
408 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700409 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700410 write_unlock(&css_set_lock);
411 return;
412 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416 css_set_count--;
417
Tejun Heo69d02062013-06-12 21:04:50 -0700418 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700420
Tejun Heo69d02062013-06-12 21:04:50 -0700421 list_del(&link->cset_link);
422 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800423
Tejun Heoddd69142013-06-12 21:04:54 -0700424 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700425 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 set_bit(CGRP_RELEASABLE, &cgrp->flags);
428 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700430
431 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700433
434 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
438/*
439 * refcounted get/put for css_set objects
440 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700441static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700442{
Tejun Heo5abb8852013-06-12 21:04:49 -0700443 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700444}
445
Tejun Heo5abb8852013-06-12 21:04:49 -0700446static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700447{
Tejun Heo5abb8852013-06-12 21:04:49 -0700448 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
Tejun Heo5abb8852013-06-12 21:04:49 -0700451static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452{
Tejun Heo5abb8852013-06-12 21:04:49 -0700453 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454}
455
Tejun Heob326f9d2013-06-24 15:21:48 -0700456/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700457 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700458 * @cset: candidate css_set being tested
459 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 * @new_cgrp: cgroup that's being entered by the task
461 * @template: desired set of css pointers in css_set (pre-calculated)
462 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800463 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
465 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700466static bool compare_css_sets(struct css_set *cset,
467 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700468 struct cgroup *new_cgrp,
469 struct cgroup_subsys_state *template[])
470{
471 struct list_head *l1, *l2;
472
Tejun Heo5abb8852013-06-12 21:04:49 -0700473 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 /* Not all subsystems matched */
475 return false;
476 }
477
478 /*
479 * Compare cgroup pointers in order to distinguish between
480 * different cgroups in heirarchies with no subsystems. We
481 * could get by with just this check alone (and skip the
482 * memcmp above) but on most setups the memcmp check will
483 * avoid the need for this more expensive check on almost all
484 * candidates.
485 */
486
Tejun Heo69d02062013-06-12 21:04:50 -0700487 l1 = &cset->cgrp_links;
488 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700490 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700491 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700492
493 l1 = l1->next;
494 l2 = l2->next;
495 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700496 if (l1 == &cset->cgrp_links) {
497 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700498 break;
499 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700500 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 }
502 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700503 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
504 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
505 cgrp1 = link1->cgrp;
506 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700508 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700509
510 /*
511 * If this hierarchy is the hierarchy of the cgroup
512 * that's changing, then we need to check that this
513 * css_set points to the new cgroup; if it's any other
514 * hierarchy, then this css_set should point to the
515 * same cgroup as the old css_set.
516 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700517 if (cgrp1->root == new_cgrp->root) {
518 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700519 return false;
520 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700521 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700522 return false;
523 }
524 }
525 return true;
526}
527
Tejun Heob326f9d2013-06-24 15:21:48 -0700528/**
529 * find_existing_css_set - init css array and find the matching css_set
530 * @old_cset: the css_set that we're using before the cgroup transition
531 * @cgrp: the cgroup that we're moving into
532 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700533 */
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{
Paul Menagebd89aab2007-10-18 23:40:44 -0700538 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700539 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800541 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700542 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700543
Ben Blumaae8aab2010-03-10 15:22:07 -0800544 /*
545 * Build the set of subsystem state objects that we want to see in the
546 * new css_set. while subsystems can change globally, the entries here
547 * won't change, so no need for locking.
548 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700549 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400550 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700551 /* Subsystem is in this hierarchy. So we want
552 * the subsystem state from the new
553 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400554 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700555 } else {
556 /* Subsystem is not in this hierarchy, so we
557 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700558 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700559 }
560 }
561
Li Zefan0ac801f2013-01-10 11:49:27 +0800562 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700563 hash_for_each_possible(css_set_table, cset, hlist, key) {
564 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700565 continue;
566
567 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700568 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700569 }
Paul Menage817929e2007-10-18 23:39:36 -0700570
571 /* No existing cgroup group matched */
572 return NULL;
573}
574
Tejun Heo69d02062013-06-12 21:04:50 -0700575static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700576{
Tejun Heo69d02062013-06-12 21:04:50 -0700577 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700578
Tejun Heo69d02062013-06-12 21:04:50 -0700579 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
580 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700581 kfree(link);
582 }
583}
584
Tejun Heo69d02062013-06-12 21:04:50 -0700585/**
586 * allocate_cgrp_cset_links - allocate cgrp_cset_links
587 * @count: the number of links to allocate
588 * @tmp_links: list_head the allocated links are put on
589 *
590 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
591 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700592 */
Tejun Heo69d02062013-06-12 21:04:50 -0700593static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700594{
Tejun Heo69d02062013-06-12 21:04:50 -0700595 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700596 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700597
598 INIT_LIST_HEAD(tmp_links);
599
Li Zefan36553432008-07-29 22:33:19 -0700600 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700601 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700602 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700603 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700604 return -ENOMEM;
605 }
Tejun Heo69d02062013-06-12 21:04:50 -0700606 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700607 }
608 return 0;
609}
610
Li Zefanc12f65d2009-01-07 18:07:42 -0800611/**
612 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700613 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700614 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800615 * @cgrp: the destination cgroup
616 */
Tejun Heo69d02062013-06-12 21:04:50 -0700617static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
618 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800619{
Tejun Heo69d02062013-06-12 21:04:50 -0700620 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800621
Tejun Heo69d02062013-06-12 21:04:50 -0700622 BUG_ON(list_empty(tmp_links));
623 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
624 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700625 link->cgrp = cgrp;
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
Tejun Heob326f9d2013-06-24 15:21:48 -0700634/**
635 * find_css_set - return a new css_set with one cgroup updated
636 * @old_cset: the baseline css_set
637 * @cgrp: the cgroup to be updated
638 *
639 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
640 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700641 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700642static struct css_set *find_css_set(struct css_set *old_cset,
643 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700644{
Tejun Heob326f9d2013-06-24 15:21:48 -0700645 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700646 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700647 struct list_head tmp_links;
648 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800649 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700650
Tejun Heob326f9d2013-06-24 15:21:48 -0700651 lockdep_assert_held(&cgroup_mutex);
652
Paul Menage817929e2007-10-18 23:39:36 -0700653 /* First see if we already have a cgroup group that matches
654 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700655 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 cset = find_existing_css_set(old_cset, cgrp, template);
657 if (cset)
658 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700659 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700660
Tejun Heo5abb8852013-06-12 21:04:49 -0700661 if (cset)
662 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700663
Tejun Heof4f4be22013-06-12 21:04:51 -0700664 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700666 return NULL;
667
Tejun Heo69d02062013-06-12 21:04:50 -0700668 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700669 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700670 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700671 return NULL;
672 }
673
Tejun Heo5abb8852013-06-12 21:04:49 -0700674 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700675 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700676 INIT_LIST_HEAD(&cset->tasks);
677 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700678
679 /* Copy the set of subsystem state objects generated in
680 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700681 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700682
683 write_lock(&css_set_lock);
684 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700685 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700686 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700687
Paul Menage7717f7b2009-09-23 15:56:22 -0700688 if (c->root == cgrp->root)
689 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700690 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700691 }
Paul Menage817929e2007-10-18 23:39:36 -0700692
Tejun Heo69d02062013-06-12 21:04:50 -0700693 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700694
Paul Menage817929e2007-10-18 23:39:36 -0700695 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700696
697 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700698 key = css_set_hash(cset->subsys);
699 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700700
Paul Menage817929e2007-10-18 23:39:36 -0700701 write_unlock(&css_set_lock);
702
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700704}
705
Paul Menageddbcc7e2007-10-18 23:39:30 -0700706/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700707 * Return the cgroup for "task" from the given hierarchy. Must be
708 * called with cgroup_mutex held.
709 */
710static struct cgroup *task_cgroup_from_root(struct task_struct *task,
711 struct cgroupfs_root *root)
712{
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 struct cgroup *res = NULL;
715
716 BUG_ON(!mutex_is_locked(&cgroup_mutex));
717 read_lock(&css_set_lock);
718 /*
719 * No need to lock the task - since we hold cgroup_mutex the
720 * task can't change groups, so the only thing that can happen
721 * is that it exits and its css is set back to init_css_set.
722 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700723 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700724 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700725 res = &root->top_cgroup;
726 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700727 struct cgrp_cset_link *link;
728
729 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700730 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700731
Paul Menage7717f7b2009-09-23 15:56:22 -0700732 if (c->root == root) {
733 res = c;
734 break;
735 }
736 }
737 }
738 read_unlock(&css_set_lock);
739 BUG_ON(!res);
740 return res;
741}
742
743/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700744 * There is one global cgroup mutex. We also require taking
745 * task_lock() when dereferencing a task's cgroup subsys pointers.
746 * See "The task_lock() exception", at the end of this comment.
747 *
748 * A task must hold cgroup_mutex to modify cgroups.
749 *
750 * Any task can increment and decrement the count field without lock.
751 * So in general, code holding cgroup_mutex can't rely on the count
752 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800753 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700754 * means that no tasks are currently attached, therefore there is no
755 * way a task attached to that cgroup can fork (the other way to
756 * increment the count). So code holding cgroup_mutex can safely
757 * assume that if the count is zero, it will stay zero. Similarly, if
758 * a task holds cgroup_mutex on a cgroup with zero count, it
759 * knows that the cgroup won't be removed, as cgroup_rmdir()
760 * needs that mutex.
761 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700762 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
763 * (usually) take cgroup_mutex. These are the two most performance
764 * critical pieces of code here. The exception occurs on cgroup_exit(),
765 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
766 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800767 * to the release agent with the name of the cgroup (path relative to
768 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 *
770 * A cgroup can only be deleted if both its 'count' of using tasks
771 * is zero, and its list of 'children' cgroups is empty. Since all
772 * tasks in the system use _some_ cgroup, and since there is always at
773 * least one task in the system (init, pid == 1), therefore, top_cgroup
774 * always has either children cgroups and/or using tasks. So we don't
775 * need a special hack to ensure that top_cgroup cannot be deleted.
776 *
777 * The task_lock() exception
778 *
779 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800780 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800781 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782 * several performance critical places that need to reference
783 * task->cgroup without the expense of grabbing a system global
784 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800785 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
787 * the task_struct routinely used for such matters.
788 *
789 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800790 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 */
792
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793/*
794 * A couple of forward declarations required, due to cyclic reference loop:
795 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
796 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
797 * -> cgroup_mkdir.
798 */
799
Al Viro18bb1db2011-07-26 01:41:39 -0400800static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700802static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700803static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700804static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700805
806static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200807 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700808 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700809};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810
Al Viroa5e7ed32011-07-26 01:55:55 -0400811static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812{
813 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814
815 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400816 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100818 inode->i_uid = current_fsuid();
819 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
821 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
822 }
823 return inode;
824}
825
Li Zefan65dff752013-03-01 15:01:56 +0800826static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
827{
828 struct cgroup_name *name;
829
830 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
831 if (!name)
832 return NULL;
833 strcpy(name->name, dentry->d_name.name);
834 return name;
835}
836
Li Zefanbe445622013-01-24 14:31:42 +0800837static void cgroup_free_fn(struct work_struct *work)
838{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700839 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800840
841 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800842 cgrp->root->number_of_cgroups--;
843 mutex_unlock(&cgroup_mutex);
844
845 /*
Li Zefan415cf072013-04-08 14:35:02 +0800846 * We get a ref to the parent's dentry, and put the ref when
847 * this cgroup is being freed, so it's guaranteed that the
848 * parent won't be destroyed before its children.
849 */
850 dput(cgrp->parent->dentry);
851
852 /*
Li Zefanbe445622013-01-24 14:31:42 +0800853 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700854 * created the cgroup. This will free cgrp->root, if we are
855 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800856 */
857 deactivate_super(cgrp->root->sb);
858
Tejun Heob1a21362013-11-29 10:42:58 -0500859 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800860
861 simple_xattrs_free(&cgrp->xattrs);
862
Li Zefan65dff752013-03-01 15:01:56 +0800863 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800864 kfree(cgrp);
865}
866
867static void cgroup_free_rcu(struct rcu_head *head)
868{
869 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
870
Tejun Heoea15f8c2013-06-13 19:27:42 -0700871 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500872 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800873}
874
Paul Menageddbcc7e2007-10-18 23:39:30 -0700875static void cgroup_diput(struct dentry *dentry, struct inode *inode)
876{
877 /* is dentry a directory ? if so, kfree() associated cgroup */
878 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700879 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800880
Tejun Heo54766d42013-06-12 21:04:53 -0700881 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800882 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700883 } else {
884 struct cfent *cfe = __d_cfe(dentry);
885 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
886
887 WARN_ONCE(!list_empty(&cfe->node) &&
888 cgrp != &cgrp->root->top_cgroup,
889 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700890 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700891 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700892 }
893 iput(inode);
894}
895
896static void remove_dir(struct dentry *d)
897{
898 struct dentry *parent = dget(d->d_parent);
899
900 d_delete(d);
901 simple_rmdir(parent->d_inode, d);
902 dput(parent);
903}
904
Li Zefan2739d3c2013-01-21 18:18:33 +0800905static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700906{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700907 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908
Tejun Heo05ef1d72012-04-01 12:09:56 -0700909 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
910 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100911
Li Zefan2739d3c2013-01-21 18:18:33 +0800912 /*
913 * If we're doing cleanup due to failure of cgroup_create(),
914 * the corresponding @cfe may not exist.
915 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 list_for_each_entry(cfe, &cgrp->files, node) {
917 struct dentry *d = cfe->dentry;
918
919 if (cft && cfe->type != cft)
920 continue;
921
922 dget(d);
923 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700924 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700925 list_del_init(&cfe->node);
926 dput(d);
927
Li Zefan2739d3c2013-01-21 18:18:33 +0800928 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930}
931
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400932/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700933 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700934 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400935 * @subsys_mask: mask of the subsystem ids whose files should be removed
936 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700937static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400939 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700940 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941
Tejun Heob420ba72013-07-12 12:34:02 -0700942 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400943 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700944
945 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400946 continue;
947 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400948 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950}
951
952/*
953 * NOTE : the dentry must have been dget()'ed
954 */
955static void cgroup_d_remove_dir(struct dentry *dentry)
956{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100957 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100959 parent = dentry->d_parent;
960 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800961 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100963 spin_unlock(&dentry->d_lock);
964 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965 remove_dir(dentry);
966}
967
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700968/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800969 * Call with cgroup_mutex held. Drops reference counts on modules, including
970 * any duplicate ones that parse_cgroupfs_options took. If this function
971 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800972 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700974 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975{
Paul Menagebd89aab2007-10-18 23:40:44 -0700976 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700977 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700978 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700979 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980
Ben Blumaae8aab2010-03-10 15:22:07 -0800981 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800982 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800983
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700985 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700986 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700988
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700989 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700990 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700991 ret = -EBUSY;
992 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700994
995 /* pin the module */
996 if (!try_module_get(ss->module)) {
997 ret = -ENOENT;
998 goto out_put;
999 }
1000 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 }
1002
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001003 /* subsys could be missing if unloaded between parsing and here */
1004 if (added_mask != pinned) {
1005 ret = -ENOENT;
1006 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008
Tejun Heo31261212013-06-28 17:07:30 -07001009 ret = cgroup_populate_dir(cgrp, added_mask);
1010 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001011 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001012
1013 /*
1014 * Nothing can fail from this point on. Remove files for the
1015 * removed subsystems and rebind each subsystem.
1016 */
1017 cgroup_clear_dir(cgrp, removed_mask);
1018
Tejun Heo30159ec2013-06-25 11:53:37 -07001019 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001021
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001022 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001024 BUG_ON(cgroup_css(cgrp, ss));
1025 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1026 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001027
Tejun Heo73e80ed2013-08-13 11:01:55 -04001028 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001029 cgroup_css(cgroup_dummy_top, ss));
1030 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001031
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001032 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001034 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001035
Ben Blumcf5d5942010-03-10 15:22:09 -08001036 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001037 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001038 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001040 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1041 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001042
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001044 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001045
Tejun Heoca8bdca2013-08-26 18:40:56 -04001046 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001047 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1048
Tejun Heo9871bf92013-06-24 15:21:47 -07001049 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001050
Ben Blumcf5d5942010-03-10 15:22:09 -08001051 /* subsystem is now free - drop reference on module */
1052 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001053 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 }
1055 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056
Tejun Heo1672d042013-06-25 18:04:54 -07001057 /*
1058 * Mark @root has finished binding subsystems. @root->subsys_mask
1059 * now matches the bound subsystems.
1060 */
1061 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1062
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001064
1065out_put:
1066 for_each_subsys(ss, i)
1067 if (pinned & (1 << i))
1068 module_put(ss->module);
1069 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070}
1071
Al Viro34c80b12011-12-08 21:32:45 -05001072static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073{
Al Viro34c80b12011-12-08 21:32:45 -05001074 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001076 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001078 mutex_lock(&cgroup_root_mutex);
Tejun Heob85d2042013-12-06 15:11:57 -05001079 for_each_subsys(ss, ssid)
1080 if (root->subsys_mask & (1 << ssid))
1081 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001082 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1083 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001084 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001086 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001087 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001088 if (strlen(root->release_agent_path))
1089 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001090 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001091 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001092 if (strlen(root->name))
1093 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001094 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 return 0;
1096}
1097
1098struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001099 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001101 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001102 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001103 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001104 /* User explicitly requested empty subsystem */
1105 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001106
1107 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001108
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109};
1110
Ben Blumaae8aab2010-03-10 15:22:07 -08001111/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001112 * Convert a hierarchy specifier into a bitmask of subsystems and
1113 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1114 * array. This function takes refcounts on subsystems to be used, unless it
1115 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001116 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001117static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001119 char *token, *o = data;
1120 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001121 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001122 struct cgroup_subsys *ss;
1123 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001124
Ben Blumaae8aab2010-03-10 15:22:07 -08001125 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1126
Li Zefanf9ab5b52009-06-17 16:26:33 -07001127#ifdef CONFIG_CPUSETS
1128 mask = ~(1UL << cpuset_subsys_id);
1129#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130
Paul Menagec6d57f32009-09-23 15:56:19 -07001131 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132
1133 while ((token = strsep(&o, ",")) != NULL) {
1134 if (!*token)
1135 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001136 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001137 /* Explicitly have no subsystems */
1138 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001139 continue;
1140 }
1141 if (!strcmp(token, "all")) {
1142 /* Mutually exclusive option 'all' + subsystem name */
1143 if (one_ss)
1144 return -EINVAL;
1145 all_ss = true;
1146 continue;
1147 }
Tejun Heo873fe092013-04-14 20:15:26 -07001148 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1149 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1150 continue;
1151 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001153 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 continue;
1155 }
1156 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001157 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001158 continue;
1159 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001160 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001161 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001162 continue;
1163 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001165 /* Specifying two release agents is forbidden */
1166 if (opts->release_agent)
1167 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001168 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001169 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001170 if (!opts->release_agent)
1171 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 continue;
1173 }
1174 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001175 const char *name = token + 5;
1176 /* Can't specify an empty name */
1177 if (!strlen(name))
1178 return -EINVAL;
1179 /* Must match [\w.-]+ */
1180 for (i = 0; i < strlen(name); i++) {
1181 char c = name[i];
1182 if (isalnum(c))
1183 continue;
1184 if ((c == '.') || (c == '-') || (c == '_'))
1185 continue;
1186 return -EINVAL;
1187 }
1188 /* Specifying two names is forbidden */
1189 if (opts->name)
1190 return -EINVAL;
1191 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001192 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 GFP_KERNEL);
1194 if (!opts->name)
1195 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196
1197 continue;
1198 }
1199
Tejun Heo30159ec2013-06-25 11:53:37 -07001200 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201 if (strcmp(token, ss->name))
1202 continue;
1203 if (ss->disabled)
1204 continue;
1205
1206 /* Mutually exclusive option 'all' + subsystem name */
1207 if (all_ss)
1208 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001209 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001210 one_ss = true;
1211
1212 break;
1213 }
1214 if (i == CGROUP_SUBSYS_COUNT)
1215 return -ENOENT;
1216 }
1217
1218 /*
1219 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001220 * otherwise if 'none', 'name=' and a subsystem name options
1221 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001223 if (all_ss || (!one_ss && !opts->none && !opts->name))
1224 for_each_subsys(ss, i)
1225 if (!ss->disabled)
1226 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001227
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001228 /* Consistency checks */
1229
Tejun Heo873fe092013-04-14 20:15:26 -07001230 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1231 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1232
1233 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1234 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1235 return -EINVAL;
1236 }
1237
1238 if (opts->cpuset_clone_children) {
1239 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1240 return -EINVAL;
1241 }
1242 }
1243
Li Zefanf9ab5b52009-06-17 16:26:33 -07001244 /*
1245 * Option noprefix was introduced just for backward compatibility
1246 * with the old cpuset, so we allow noprefix only if mounting just
1247 * the cpuset subsystem.
1248 */
Tejun Heo93438622013-04-14 20:15:25 -07001249 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001250 return -EINVAL;
1251
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001252
1253 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001254 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001255 return -EINVAL;
1256
1257 /*
1258 * We either have to specify by name or by subsystems. (So all
1259 * empty hierarchies must have a name).
1260 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001261 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001262 return -EINVAL;
1263
1264 return 0;
1265}
1266
1267static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1268{
1269 int ret = 0;
1270 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001271 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001273 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001274
Tejun Heo873fe092013-04-14 20:15:26 -07001275 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1276 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1277 return -EINVAL;
1278 }
1279
Paul Menagebd89aab2007-10-18 23:40:44 -07001280 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001282 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283
1284 /* See what subsystems are wanted */
1285 ret = parse_cgroupfs_options(data, &opts);
1286 if (ret)
1287 goto out_unlock;
1288
Tejun Heoa8a648c2013-06-24 15:21:47 -07001289 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001290 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1291 task_tgid_nr(current), current->comm);
1292
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001293 added_mask = opts.subsys_mask & ~root->subsys_mask;
1294 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001295
Ben Blumcf5d5942010-03-10 15:22:09 -08001296 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001297 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001299 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1300 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1301 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001303 goto out_unlock;
1304 }
1305
Tejun Heof172e672013-06-28 17:07:30 -07001306 /* remounting is not allowed for populated hierarchies */
1307 if (root->number_of_cgroups > 1) {
1308 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001309 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311
Paul Menageddbcc7e2007-10-18 23:39:30 -07001312 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001313 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315
Paul Menage81a6a5c2007-10-18 23:39:38 -07001316 if (opts.release_agent)
1317 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001319 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001320 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001321 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 return ret;
1325}
1326
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001327static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 .statfs = simple_statfs,
1329 .drop_inode = generic_delete_inode,
1330 .show_options = cgroup_show_options,
1331 .remount_fs = cgroup_remount,
1332};
1333
Paul Menagecc31edc2008-10-18 20:28:04 -07001334static void init_cgroup_housekeeping(struct cgroup *cgrp)
1335{
1336 INIT_LIST_HEAD(&cgrp->sibling);
1337 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001338 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001339 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001340 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001341 INIT_LIST_HEAD(&cgrp->pidlists);
1342 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001343 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001344 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001345}
Paul Menagec6d57f32009-09-23 15:56:19 -07001346
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347static void init_cgroup_root(struct cgroupfs_root *root)
1348{
Paul Menagebd89aab2007-10-18 23:40:44 -07001349 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001350
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351 INIT_LIST_HEAD(&root->root_list);
1352 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001353 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001354 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001355 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001356 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357}
1358
Tejun Heofc76df72013-06-25 11:53:37 -07001359static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001360{
Tejun Heo1a574232013-04-14 11:36:58 -07001361 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001362
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001363 lockdep_assert_held(&cgroup_mutex);
1364 lockdep_assert_held(&cgroup_root_mutex);
1365
Tejun Heofc76df72013-06-25 11:53:37 -07001366 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1367 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001368 if (id < 0)
1369 return id;
1370
1371 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001372 return 0;
1373}
1374
1375static void cgroup_exit_root_id(struct cgroupfs_root *root)
1376{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001377 lockdep_assert_held(&cgroup_mutex);
1378 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001379
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001380 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001381 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001382 root->hierarchy_id = 0;
1383 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384}
1385
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386static int cgroup_test_super(struct super_block *sb, void *data)
1387{
Paul Menagec6d57f32009-09-23 15:56:19 -07001388 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 struct cgroupfs_root *root = sb->s_fs_info;
1390
Paul Menagec6d57f32009-09-23 15:56:19 -07001391 /* If we asked for a name then it must match */
1392 if (opts->name && strcmp(opts->name, root->name))
1393 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001395 /*
1396 * If we asked for subsystems (or explicitly for no
1397 * subsystems) then they must match
1398 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001399 if ((opts->subsys_mask || opts->none)
1400 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 return 0;
1402
1403 return 1;
1404}
1405
Paul Menagec6d57f32009-09-23 15:56:19 -07001406static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1407{
1408 struct cgroupfs_root *root;
1409
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001410 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001411 return NULL;
1412
1413 root = kzalloc(sizeof(*root), GFP_KERNEL);
1414 if (!root)
1415 return ERR_PTR(-ENOMEM);
1416
1417 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001418
Tejun Heo1672d042013-06-25 18:04:54 -07001419 /*
1420 * We need to set @root->subsys_mask now so that @root can be
1421 * matched by cgroup_test_super() before it finishes
1422 * initialization; otherwise, competing mounts with the same
1423 * options may try to bind the same subsystems instead of waiting
1424 * for the first one leading to unexpected mount errors.
1425 * SUBSYS_BOUND will be set once actual binding is complete.
1426 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001427 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001428 root->flags = opts->flags;
1429 if (opts->release_agent)
1430 strcpy(root->release_agent_path, opts->release_agent);
1431 if (opts->name)
1432 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001433 if (opts->cpuset_clone_children)
1434 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 return root;
1436}
1437
Tejun Heofa3ca072013-04-14 11:36:56 -07001438static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001439{
Tejun Heofa3ca072013-04-14 11:36:56 -07001440 if (root) {
1441 /* hierarhcy ID shoulid already have been released */
1442 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443
Li Zefan4e96ee82013-07-31 09:50:50 +08001444 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001445 kfree(root);
1446 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001447}
1448
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449static int cgroup_set_super(struct super_block *sb, void *data)
1450{
1451 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 struct cgroup_sb_opts *opts = data;
1453
1454 /* If we don't have a new root, we can't set up a new sb */
1455 if (!opts->new_root)
1456 return -EINVAL;
1457
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001458 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459
1460 ret = set_anon_super(sb, NULL);
1461 if (ret)
1462 return ret;
1463
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 sb->s_fs_info = opts->new_root;
1465 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466
1467 sb->s_blocksize = PAGE_CACHE_SIZE;
1468 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1469 sb->s_magic = CGROUP_SUPER_MAGIC;
1470 sb->s_op = &cgroup_ops;
1471
1472 return 0;
1473}
1474
1475static int cgroup_get_rootdir(struct super_block *sb)
1476{
Al Viro0df6a632010-12-21 13:29:29 -05001477 static const struct dentry_operations cgroup_dops = {
1478 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001479 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001480 };
1481
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482 struct inode *inode =
1483 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484
1485 if (!inode)
1486 return -ENOMEM;
1487
Paul Menageddbcc7e2007-10-18 23:39:30 -07001488 inode->i_fop = &simple_dir_operations;
1489 inode->i_op = &cgroup_dir_inode_operations;
1490 /* directories start off with i_nlink == 2 (for "." entry) */
1491 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001492 sb->s_root = d_make_root(inode);
1493 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001495 /* for everything else we want ->d_op set */
1496 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497 return 0;
1498}
1499
Al Virof7e83572010-07-26 13:23:11 +04001500static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001502 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001503{
1504 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 int ret = 0;
1507 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001508 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001509 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001510 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001511 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
1513 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001514 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001516 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001517 if (ret)
1518 goto out_err;
1519
1520 /*
1521 * Allocate a new cgroup root. We may not need it if we're
1522 * reusing an existing hierarchy.
1523 */
1524 new_root = cgroup_root_from_opts(&opts);
1525 if (IS_ERR(new_root)) {
1526 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001527 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001528 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001532 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001535 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001536 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 }
1538
Paul Menagec6d57f32009-09-23 15:56:19 -07001539 root = sb->s_fs_info;
1540 BUG_ON(!root);
1541 if (root == opts.new_root) {
1542 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001543 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001545 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001546 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547
1548 BUG_ON(sb->s_root != NULL);
1549
1550 ret = cgroup_get_rootdir(sb);
1551 if (ret)
1552 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001553 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
Paul Menage817929e2007-10-18 23:39:36 -07001555 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001557 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558
Li Zefan4e96ee82013-07-31 09:50:50 +08001559 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1560 0, 1, GFP_KERNEL);
1561 if (root_cgrp->id < 0)
1562 goto unlock_drop;
1563
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001564 /* Check for name clashes with existing mounts */
1565 ret = -EBUSY;
1566 if (strlen(root->name))
1567 for_each_active_root(existing_root)
1568 if (!strcmp(existing_root->name, root->name))
1569 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001570
Paul Menage817929e2007-10-18 23:39:36 -07001571 /*
1572 * We're accessing css_set_count without locking
1573 * css_set_lock here, but that's OK - it can only be
1574 * increased by someone holding cgroup_lock, and
1575 * that's us. The worst that can happen is that we
1576 * have some link structures left over
1577 */
Tejun Heo69d02062013-06-12 21:04:50 -07001578 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001579 if (ret)
1580 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001581
Tejun Heofc76df72013-06-25 11:53:37 -07001582 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1583 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001584 if (ret)
1585 goto unlock_drop;
1586
Tejun Heo31261212013-06-28 17:07:30 -07001587 sb->s_root->d_fsdata = root_cgrp;
1588 root_cgrp->dentry = sb->s_root;
1589
1590 /*
1591 * We're inside get_sb() and will call lookup_one_len() to
1592 * create the root files, which doesn't work if SELinux is
1593 * in use. The following cred dancing somehow works around
1594 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1595 * populating new cgroupfs mount") for more details.
1596 */
1597 cred = override_creds(&init_cred);
1598
Tejun Heo2bb566c2013-08-08 20:11:23 -04001599 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001600 if (ret)
1601 goto rm_base_files;
1602
Tejun Heoa8a648c2013-06-24 15:21:47 -07001603 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001604 if (ret)
1605 goto rm_base_files;
1606
1607 revert_creds(cred);
1608
Ben Blumcf5d5942010-03-10 15:22:09 -08001609 /*
1610 * There must be no failure case after here, since rebinding
1611 * takes care of subsystems' refcounts, which are explicitly
1612 * dropped in the failure exit path.
1613 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614
Tejun Heo9871bf92013-06-24 15:21:47 -07001615 list_add(&root->root_list, &cgroup_roots);
1616 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617
Paul Menage817929e2007-10-18 23:39:36 -07001618 /* Link the top cgroup in this hierarchy into all
1619 * the css_set objects */
1620 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001621 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001622 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001623 write_unlock(&css_set_lock);
1624
Tejun Heo69d02062013-06-12 21:04:50 -07001625 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001626
Li Zefanc12f65d2009-01-07 18:07:42 -08001627 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 BUG_ON(root->number_of_cgroups != 1);
1629
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001630 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001632 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001633 } else {
1634 /*
1635 * We re-used an existing hierarchy - the new root (if
1636 * any) is not needed
1637 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001638 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001639
Tejun Heoc7ba8282013-06-29 14:06:10 -07001640 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001641 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1642 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1643 ret = -EINVAL;
1644 goto drop_new_super;
1645 } else {
1646 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1647 }
Tejun Heo873fe092013-04-14 20:15:26 -07001648 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 }
1650
Paul Menagec6d57f32009-09-23 15:56:19 -07001651 kfree(opts.release_agent);
1652 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001653 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
Tejun Heo31261212013-06-28 17:07:30 -07001655 rm_base_files:
1656 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001657 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001658 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001659 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001660 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001661 mutex_unlock(&cgroup_root_mutex);
1662 mutex_unlock(&cgroup_mutex);
1663 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001665 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001666 out_err:
1667 kfree(opts.release_agent);
1668 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001669 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670}
1671
1672static void cgroup_kill_sb(struct super_block *sb) {
1673 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001674 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001675 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 int ret;
1677
1678 BUG_ON(!root);
1679
1680 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001681 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682
Tejun Heo31261212013-06-28 17:07:30 -07001683 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001685 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686
1687 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001688 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1689 ret = rebind_subsystems(root, 0, root->subsys_mask);
1690 /* Shouldn't be able to fail ... */
1691 BUG_ON(ret);
1692 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693
Paul Menage817929e2007-10-18 23:39:36 -07001694 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001695 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001696 * root cgroup
1697 */
1698 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001699
Tejun Heo69d02062013-06-12 21:04:50 -07001700 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1701 list_del(&link->cset_link);
1702 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001703 kfree(link);
1704 }
1705 write_unlock(&css_set_lock);
1706
Paul Menage839ec542009-01-29 14:25:22 -08001707 if (!list_empty(&root->root_list)) {
1708 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001709 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001710 }
Li Zefane5f6a862009-01-07 18:07:41 -08001711
Tejun Heofa3ca072013-04-14 11:36:56 -07001712 cgroup_exit_root_id(root);
1713
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001714 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001716 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001718 simple_xattrs_free(&cgrp->xattrs);
1719
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001721 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722}
1723
1724static struct file_system_type cgroup_fs_type = {
1725 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001726 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727 .kill_sb = cgroup_kill_sb,
1728};
1729
Greg KH676db4a2010-08-05 13:53:35 -07001730static struct kobject *cgroup_kobj;
1731
Li Zefana043e3b2008-02-23 15:24:09 -08001732/**
1733 * cgroup_path - generate the path of a cgroup
1734 * @cgrp: the cgroup in question
1735 * @buf: the buffer to write the path into
1736 * @buflen: the length of the buffer
1737 *
Li Zefan65dff752013-03-01 15:01:56 +08001738 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1739 *
1740 * We can't generate cgroup path using dentry->d_name, as accessing
1741 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1742 * inode's i_mutex, while on the other hand cgroup_path() can be called
1743 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001745int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746{
Li Zefan65dff752013-03-01 15:01:56 +08001747 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001749
Tejun Heoda1f2962013-04-14 10:32:19 -07001750 if (!cgrp->parent) {
1751 if (strlcpy(buf, "/", buflen) >= buflen)
1752 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 return 0;
1754 }
1755
Tao Ma316eb662012-11-08 21:36:38 +08001756 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001757 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001758
Li Zefan65dff752013-03-01 15:01:56 +08001759 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001760 do {
Li Zefan65dff752013-03-01 15:01:56 +08001761 const char *name = cgroup_name(cgrp);
1762 int len;
1763
1764 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001766 goto out;
1767 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001768
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001770 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001772
1773 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001774 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001775 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001777out:
1778 rcu_read_unlock();
1779 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780}
Ben Blum67523c42010-03-10 15:22:11 -08001781EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782
Tejun Heo857a2be2013-04-14 20:50:08 -07001783/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001784 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001785 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001786 * @buf: the buffer to write the path into
1787 * @buflen: the length of the buffer
1788 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001789 * Determine @task's cgroup on the first (the one with the lowest non-zero
1790 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1791 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1792 * cgroup controller callbacks.
1793 *
1794 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001795 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001796int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001797{
1798 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001799 struct cgroup *cgrp;
1800 int hierarchy_id = 1, ret = 0;
1801
1802 if (buflen < 2)
1803 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001804
1805 mutex_lock(&cgroup_mutex);
1806
Tejun Heo913ffdb2013-07-11 16:34:48 -07001807 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1808
Tejun Heo857a2be2013-04-14 20:50:08 -07001809 if (root) {
1810 cgrp = task_cgroup_from_root(task, root);
1811 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 } else {
1813 /* if no hierarchy exists, everyone is in "/" */
1814 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001815 }
1816
1817 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001818 return ret;
1819}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001820EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001821
Ben Blum74a11662011-05-26 16:25:20 -07001822/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001823 * Control Group taskset
1824 */
Tejun Heo134d3372011-12-12 18:12:21 -08001825struct task_and_cgroup {
1826 struct task_struct *task;
1827 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001828 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001829};
1830
Tejun Heo2f7ee562011-12-12 18:12:21 -08001831struct cgroup_taskset {
1832 struct task_and_cgroup single;
1833 struct flex_array *tc_array;
1834 int tc_array_len;
1835 int idx;
1836 struct cgroup *cur_cgrp;
1837};
1838
1839/**
1840 * cgroup_taskset_first - reset taskset and return the first task
1841 * @tset: taskset of interest
1842 *
1843 * @tset iteration is initialized and the first task is returned.
1844 */
1845struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1846{
1847 if (tset->tc_array) {
1848 tset->idx = 0;
1849 return cgroup_taskset_next(tset);
1850 } else {
1851 tset->cur_cgrp = tset->single.cgrp;
1852 return tset->single.task;
1853 }
1854}
1855EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1856
1857/**
1858 * cgroup_taskset_next - iterate to the next task in taskset
1859 * @tset: taskset of interest
1860 *
1861 * Return the next task in @tset. Iteration must have been initialized
1862 * with cgroup_taskset_first().
1863 */
1864struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1865{
1866 struct task_and_cgroup *tc;
1867
1868 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1869 return NULL;
1870
1871 tc = flex_array_get(tset->tc_array, tset->idx++);
1872 tset->cur_cgrp = tc->cgrp;
1873 return tc->task;
1874}
1875EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1876
1877/**
Tejun Heod99c8722013-08-08 20:11:27 -04001878 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001879 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001880 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881 *
Tejun Heod99c8722013-08-08 20:11:27 -04001882 * Return the css for the current (last returned) task of @tset for
1883 * subsystem specified by @subsys_id. This function must be preceded by
1884 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001885 */
Tejun Heod99c8722013-08-08 20:11:27 -04001886struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1887 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001888{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001889 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001890}
Tejun Heod99c8722013-08-08 20:11:27 -04001891EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001892
1893/**
1894 * cgroup_taskset_size - return the number of tasks in taskset
1895 * @tset: taskset of interest
1896 */
1897int cgroup_taskset_size(struct cgroup_taskset *tset)
1898{
1899 return tset->tc_array ? tset->tc_array_len : 1;
1900}
1901EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1902
1903
Ben Blum74a11662011-05-26 16:25:20 -07001904/*
1905 * cgroup_task_migrate - move a task from one cgroup to another.
1906 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001907 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001908 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001909static void cgroup_task_migrate(struct cgroup *old_cgrp,
1910 struct task_struct *tsk,
1911 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001912{
Tejun Heo5abb8852013-06-12 21:04:49 -07001913 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001914
1915 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001916 * We are synchronized through threadgroup_lock() against PF_EXITING
1917 * setting such that we can't race against cgroup_exit() changing the
1918 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001919 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001920 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001921 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001922
Ben Blum74a11662011-05-26 16:25:20 -07001923 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001924 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001925 task_unlock(tsk);
1926
1927 /* Update the css_set linked lists if we're using them */
1928 write_lock(&css_set_lock);
1929 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001930 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001931 write_unlock(&css_set_lock);
1932
1933 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001934 * We just gained a reference on old_cset by taking it from the
1935 * task. As trading it for new_cset is protected by cgroup_mutex,
1936 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001937 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001938 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1939 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001940}
1941
Li Zefana043e3b2008-02-23 15:24:09 -08001942/**
Li Zefan081aa452013-03-13 09:17:09 +08001943 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001944 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001945 * @tsk: the task or the leader of the threadgroup to be attached
1946 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001947 *
Tejun Heo257058a2011-12-12 18:12:21 -08001948 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001949 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001951static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1952 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001953{
1954 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001955 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001956 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001957 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001958 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001959 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001960 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001961 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001962
1963 /*
1964 * step 0: in order to do expensive, possibly blocking operations for
1965 * every thread, we cannot iterate the thread group list, since it needs
1966 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001967 * group - group_rwsem prevents new threads from appearing, and if
1968 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001969 */
Li Zefan081aa452013-03-13 09:17:09 +08001970 if (threadgroup)
1971 group_size = get_nr_threads(tsk);
1972 else
1973 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001974 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001975 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001976 if (!group)
1977 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001978 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001979 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001980 if (retval)
1981 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001982
Ben Blum74a11662011-05-26 16:25:20 -07001983 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001984 /*
1985 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1986 * already PF_EXITING could be freed from underneath us unless we
1987 * take an rcu_read_lock.
1988 */
1989 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001990 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001991 struct task_and_cgroup ent;
1992
Tejun Heocd3d0952011-12-12 18:12:21 -08001993 /* @tsk either already exited or can't exit until the end */
1994 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001995 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001996
Ben Blum74a11662011-05-26 16:25:20 -07001997 /* as per above, nr_threads may decrease, but not increase. */
1998 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001999 ent.task = tsk;
2000 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002001 /* nothing to do if this task is already in the cgroup */
2002 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002003 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002004 /*
2005 * saying GFP_ATOMIC has no effect here because we did prealloc
2006 * earlier, but it's good form to communicate our expectations.
2007 */
Tejun Heo134d3372011-12-12 18:12:21 -08002008 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002009 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002010 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002011 next:
Li Zefan081aa452013-03-13 09:17:09 +08002012 if (!threadgroup)
2013 break;
Ben Blum74a11662011-05-26 16:25:20 -07002014 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002015 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002016 /* remember the number of threads in the array for later. */
2017 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002018 tset.tc_array = group;
2019 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002020
Tejun Heo134d3372011-12-12 18:12:21 -08002021 /* methods shouldn't be called if no task is actually migrating */
2022 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002023 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002024 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002025
Ben Blum74a11662011-05-26 16:25:20 -07002026 /*
2027 * step 1: check that we can legitimately attach to the cgroup.
2028 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002029 for_each_css(css, i, cgrp) {
2030 if (css->ss->can_attach) {
2031 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002032 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002033 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002034 goto out_cancel_attach;
2035 }
2036 }
Ben Blum74a11662011-05-26 16:25:20 -07002037 }
2038
2039 /*
2040 * step 2: make sure css_sets exist for all threads to be migrated.
2041 * we use find_css_set, which allocates a new one if necessary.
2042 */
Ben Blum74a11662011-05-26 16:25:20 -07002043 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002044 struct css_set *old_cset;
2045
Tejun Heo134d3372011-12-12 18:12:21 -08002046 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002047 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002048 tc->cset = find_css_set(old_cset, cgrp);
2049 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002050 retval = -ENOMEM;
2051 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053 }
2054
2055 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002056 * step 3: now that we're guaranteed success wrt the css_sets,
2057 * proceed to move all tasks to the new cgroup. There are no
2058 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002059 */
Ben Blum74a11662011-05-26 16:25:20 -07002060 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002061 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002062 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002063 }
2064 /* nothing is sensitive to fork() after this point. */
2065
2066 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002067 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002068 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002069 for_each_css(css, i, cgrp)
2070 if (css->ss->attach)
2071 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002072
2073 /*
2074 * step 5: success! and cleanup
2075 */
Ben Blum74a11662011-05-26 16:25:20 -07002076 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002077out_put_css_set_refs:
2078 if (retval) {
2079 for (i = 0; i < group_size; i++) {
2080 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002081 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002082 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002083 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002084 }
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
2086out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002087 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002088 for_each_css(css, i, cgrp) {
2089 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002090 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002091 if (css->ss->cancel_attach)
2092 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002093 }
2094 }
Ben Blum74a11662011-05-26 16:25:20 -07002095out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002096 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002097 return retval;
2098}
2099
2100/*
2101 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002102 * function to attach either it or all tasks in its threadgroup. Will lock
2103 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002104 */
2105static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002106{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002108 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002109 int ret;
2110
Ben Blum74a11662011-05-26 16:25:20 -07002111 if (!cgroup_lock_live_group(cgrp))
2112 return -ENODEV;
2113
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002114retry_find_task:
2115 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002116 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002117 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002118 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002120 ret= -ESRCH;
2121 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 }
Ben Blum74a11662011-05-26 16:25:20 -07002123 /*
2124 * even if we're attaching all tasks in the thread group, we
2125 * only need to check permissions on one of them.
2126 */
David Howellsc69e8d92008-11-14 10:39:19 +11002127 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002128 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2129 !uid_eq(cred->euid, tcred->uid) &&
2130 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002131 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002132 ret = -EACCES;
2133 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135 } else
2136 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002137
2138 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002139 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002140
2141 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002142 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002143 * trapped in a cpuset, or RT worker may be born in a cgroup
2144 * with no rt_runtime allocated. Just say no.
2145 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002146 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002147 ret = -EINVAL;
2148 rcu_read_unlock();
2149 goto out_unlock_cgroup;
2150 }
2151
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 get_task_struct(tsk);
2153 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002154
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 threadgroup_lock(tsk);
2156 if (threadgroup) {
2157 if (!thread_group_leader(tsk)) {
2158 /*
2159 * a race with de_thread from another thread's exec()
2160 * may strip us of our leadership, if this happens,
2161 * there is no choice but to throw this task away and
2162 * try again; this is
2163 * "double-double-toil-and-trouble-check locking".
2164 */
2165 threadgroup_unlock(tsk);
2166 put_task_struct(tsk);
2167 goto retry_find_task;
2168 }
Li Zefan081aa452013-03-13 09:17:09 +08002169 }
2170
2171 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2172
Tejun Heocd3d0952011-12-12 18:12:21 -08002173 threadgroup_unlock(tsk);
2174
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002177 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178 return ret;
2179}
2180
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002181/**
2182 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2183 * @from: attach to all cgroups of a given task
2184 * @tsk: the task to be attached
2185 */
2186int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2187{
2188 struct cgroupfs_root *root;
2189 int retval = 0;
2190
Tejun Heo47cfcd02013-04-07 09:29:51 -07002191 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002192 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002193 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002194
Li Zefan6f4b7e62013-07-31 16:18:36 +08002195 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002196 if (retval)
2197 break;
2198 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002199 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002200
2201 return retval;
2202}
2203EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2204
Tejun Heo182446d2013-08-08 20:11:24 -04002205static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2206 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002207{
Tejun Heo182446d2013-08-08 20:11:24 -04002208 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002209}
2210
Tejun Heo182446d2013-08-08 20:11:24 -04002211static int cgroup_procs_write(struct cgroup_subsys_state *css,
2212 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002213{
Tejun Heo182446d2013-08-08 20:11:24 -04002214 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002215}
2216
Tejun Heo182446d2013-08-08 20:11:24 -04002217static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2218 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002219{
Tejun Heo182446d2013-08-08 20:11:24 -04002220 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002221 if (strlen(buffer) >= PATH_MAX)
2222 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002223 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002224 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002225 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002226 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002227 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002228 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002229 return 0;
2230}
2231
Tejun Heo2da8ca82013-12-05 12:28:04 -05002232static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002233{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002234 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002235
Paul Menagee788e062008-07-25 01:46:59 -07002236 if (!cgroup_lock_live_group(cgrp))
2237 return -ENODEV;
2238 seq_puts(seq, cgrp->root->release_agent_path);
2239 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002241 return 0;
2242}
2243
Tejun Heo2da8ca82013-12-05 12:28:04 -05002244static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002245{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002246 struct cgroup *cgrp = seq_css(seq)->cgroup;
2247
2248 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002249 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002250}
2251
Paul Menage84eea842008-07-25 01:47:00 -07002252/* A buffer size big enough for numbers or short strings */
2253#define CGROUP_LOCAL_BUFFER_SIZE 64
2254
Tejun Heoa742c592013-12-05 12:28:03 -05002255static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002256 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002257{
Tejun Heo182446d2013-08-08 20:11:24 -04002258 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002259 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002260 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002261 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2262 char *buf;
2263 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002264
Tejun Heoa742c592013-12-05 12:28:03 -05002265 if (nbytes >= max_bytes)
2266 return -E2BIG;
2267
2268 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2269 if (!buf)
2270 return -ENOMEM;
2271
2272 if (copy_from_user(buf, userbuf, nbytes)) {
2273 ret = -EFAULT;
2274 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002275 }
Tejun Heoa742c592013-12-05 12:28:03 -05002276
2277 buf[nbytes] = '\0';
2278
2279 if (cft->write_string) {
2280 ret = cft->write_string(css, cft, strstrip(buf));
2281 } else if (cft->write_u64) {
2282 unsigned long long v;
2283 ret = kstrtoull(buf, 0, &v);
2284 if (!ret)
2285 ret = cft->write_u64(css, cft, v);
2286 } else if (cft->write_s64) {
2287 long long v;
2288 ret = kstrtoll(buf, 0, &v);
2289 if (!ret)
2290 ret = cft->write_s64(css, cft, v);
2291 } else if (cft->trigger) {
2292 ret = cft->trigger(css, (unsigned int)cft->private);
2293 } else {
2294 ret = -EINVAL;
2295 }
2296out_free:
2297 kfree(buf);
2298 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002299}
2300
Paul Menage91796562008-04-29 01:00:01 -07002301/*
2302 * seqfile ops/methods for returning structured data. Currently just
2303 * supports string->u64 maps, but can be extended in future.
2304 */
2305
Tejun Heo6612f052013-12-05 12:28:04 -05002306static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2307{
2308 struct cftype *cft = seq_cft(seq);
2309
2310 if (cft->seq_start) {
2311 return cft->seq_start(seq, ppos);
2312 } else {
2313 /*
2314 * The same behavior and code as single_open(). Returns
2315 * !NULL if pos is at the beginning; otherwise, NULL.
2316 */
2317 return NULL + !*ppos;
2318 }
2319}
2320
2321static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2322{
2323 struct cftype *cft = seq_cft(seq);
2324
2325 if (cft->seq_next) {
2326 return cft->seq_next(seq, v, ppos);
2327 } else {
2328 /*
2329 * The same behavior and code as single_open(), always
2330 * terminate after the initial read.
2331 */
2332 ++*ppos;
2333 return NULL;
2334 }
2335}
2336
2337static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2338{
2339 struct cftype *cft = seq_cft(seq);
2340
2341 if (cft->seq_stop)
2342 cft->seq_stop(seq, v);
2343}
2344
Paul Menage91796562008-04-29 01:00:01 -07002345static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2346{
Tejun Heo7da11272013-12-05 12:28:04 -05002347 struct cftype *cft = seq_cft(m);
2348 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002349
Tejun Heo2da8ca82013-12-05 12:28:04 -05002350 if (cft->seq_show)
2351 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002352
Tejun Heo896f5192013-12-05 12:28:04 -05002353 if (cft->read_u64)
2354 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2355 else if (cft->read_s64)
2356 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2357 else
2358 return -EINVAL;
2359 return 0;
2360}
Paul Menage91796562008-04-29 01:00:01 -07002361
Tejun Heo6612f052013-12-05 12:28:04 -05002362static struct seq_operations cgroup_seq_operations = {
2363 .start = cgroup_seqfile_start,
2364 .next = cgroup_seqfile_next,
2365 .stop = cgroup_seqfile_stop,
2366 .show = cgroup_seqfile_show,
2367};
2368
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369static int cgroup_file_open(struct inode *inode, struct file *file)
2370{
Tejun Heof7d58812013-08-08 20:11:23 -04002371 struct cfent *cfe = __d_cfe(file->f_dentry);
2372 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002373 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2374 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002375 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377
2378 err = generic_file_open(inode, file);
2379 if (err)
2380 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002381
2382 /*
2383 * If the file belongs to a subsystem, pin the css. Will be
2384 * unpinned either on open failure or release. This ensures that
2385 * @css stays alive for all file operations.
2386 */
Tejun Heo105347b2013-08-13 11:01:55 -04002387 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002388 css = cgroup_css(cgrp, cft->ss);
2389 if (cft->ss && !css_tryget(css))
2390 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002391 rcu_read_unlock();
2392
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002393 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002394 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002395
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002396 /*
2397 * @cfe->css is used by read/write/close to determine the
2398 * associated css. @file->private_data would be a better place but
2399 * that's already used by seqfile. Multiple accessors may use it
2400 * simultaneously which is okay as the association never changes.
2401 */
2402 WARN_ON_ONCE(cfe->css && cfe->css != css);
2403 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404
Tejun Heo6612f052013-12-05 12:28:04 -05002405 of = __seq_open_private(file, &cgroup_seq_operations,
2406 sizeof(struct cgroup_open_file));
2407 if (of) {
2408 of->cfe = cfe;
2409 return 0;
Tejun Heo7da11272013-12-05 12:28:04 -05002410 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411
Tejun Heo6612f052013-12-05 12:28:04 -05002412 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002413 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002414 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415}
2416
2417static int cgroup_file_release(struct inode *inode, struct file *file)
2418{
Tejun Heof7d58812013-08-08 20:11:23 -04002419 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002420 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002421
Tejun Heo67f4c362013-08-08 20:11:24 -04002422 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002423 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002424 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425}
2426
2427/*
2428 * cgroup_rename - Only allow simple rename of directories in place.
2429 */
2430static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2431 struct inode *new_dir, struct dentry *new_dentry)
2432{
Li Zefan65dff752013-03-01 15:01:56 +08002433 int ret;
2434 struct cgroup_name *name, *old_name;
2435 struct cgroup *cgrp;
2436
2437 /*
2438 * It's convinient to use parent dir's i_mutex to protected
2439 * cgrp->name.
2440 */
2441 lockdep_assert_held(&old_dir->i_mutex);
2442
Paul Menageddbcc7e2007-10-18 23:39:30 -07002443 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2444 return -ENOTDIR;
2445 if (new_dentry->d_inode)
2446 return -EEXIST;
2447 if (old_dir != new_dir)
2448 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002449
2450 cgrp = __d_cgrp(old_dentry);
2451
Tejun Heo6db8e852013-06-14 11:18:22 -07002452 /*
2453 * This isn't a proper migration and its usefulness is very
2454 * limited. Disallow if sane_behavior.
2455 */
2456 if (cgroup_sane_behavior(cgrp))
2457 return -EPERM;
2458
Li Zefan65dff752013-03-01 15:01:56 +08002459 name = cgroup_alloc_name(new_dentry);
2460 if (!name)
2461 return -ENOMEM;
2462
2463 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2464 if (ret) {
2465 kfree(name);
2466 return ret;
2467 }
2468
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002469 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002470 rcu_assign_pointer(cgrp->name, name);
2471
2472 kfree_rcu(old_name, rcu_head);
2473 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474}
2475
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002476static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2477{
2478 if (S_ISDIR(dentry->d_inode->i_mode))
2479 return &__d_cgrp(dentry)->xattrs;
2480 else
Li Zefan712317a2013-04-18 23:09:52 -07002481 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002482}
2483
2484static inline int xattr_enabled(struct dentry *dentry)
2485{
2486 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002487 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002488}
2489
2490static bool is_valid_xattr(const char *name)
2491{
2492 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2493 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2494 return true;
2495 return false;
2496}
2497
2498static int cgroup_setxattr(struct dentry *dentry, const char *name,
2499 const void *val, size_t size, int flags)
2500{
2501 if (!xattr_enabled(dentry))
2502 return -EOPNOTSUPP;
2503 if (!is_valid_xattr(name))
2504 return -EINVAL;
2505 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2506}
2507
2508static int cgroup_removexattr(struct dentry *dentry, const char *name)
2509{
2510 if (!xattr_enabled(dentry))
2511 return -EOPNOTSUPP;
2512 if (!is_valid_xattr(name))
2513 return -EINVAL;
2514 return simple_xattr_remove(__d_xattrs(dentry), name);
2515}
2516
2517static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2518 void *buf, size_t size)
2519{
2520 if (!xattr_enabled(dentry))
2521 return -EOPNOTSUPP;
2522 if (!is_valid_xattr(name))
2523 return -EINVAL;
2524 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2525}
2526
2527static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2528{
2529 if (!xattr_enabled(dentry))
2530 return -EOPNOTSUPP;
2531 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2532}
2533
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002534static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002535 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002536 .write = cgroup_file_write,
2537 .llseek = generic_file_llseek,
2538 .open = cgroup_file_open,
2539 .release = cgroup_file_release,
2540};
2541
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002542static const struct inode_operations cgroup_file_inode_operations = {
2543 .setxattr = cgroup_setxattr,
2544 .getxattr = cgroup_getxattr,
2545 .listxattr = cgroup_listxattr,
2546 .removexattr = cgroup_removexattr,
2547};
2548
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002549static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002550 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002551 .mkdir = cgroup_mkdir,
2552 .rmdir = cgroup_rmdir,
2553 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002554 .setxattr = cgroup_setxattr,
2555 .getxattr = cgroup_getxattr,
2556 .listxattr = cgroup_listxattr,
2557 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002558};
2559
Al Viroa5e7ed32011-07-26 01:55:55 -04002560static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002561 struct super_block *sb)
2562{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563 struct inode *inode;
2564
2565 if (!dentry)
2566 return -ENOENT;
2567 if (dentry->d_inode)
2568 return -EEXIST;
2569
2570 inode = cgroup_new_inode(mode, sb);
2571 if (!inode)
2572 return -ENOMEM;
2573
2574 if (S_ISDIR(mode)) {
2575 inode->i_op = &cgroup_dir_inode_operations;
2576 inode->i_fop = &simple_dir_operations;
2577
2578 /* start off with i_nlink == 2 (for "." entry) */
2579 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002580 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002581
Tejun Heob8a2df62012-11-19 08:13:37 -08002582 /*
2583 * Control reaches here with cgroup_mutex held.
2584 * @inode->i_mutex should nest outside cgroup_mutex but we
2585 * want to populate it immediately without releasing
2586 * cgroup_mutex. As @inode isn't visible to anyone else
2587 * yet, trylock will always succeed without affecting
2588 * lockdep checks.
2589 */
2590 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002591 } else if (S_ISREG(mode)) {
2592 inode->i_size = 0;
2593 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002594 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002595 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 d_instantiate(dentry, inode);
2597 dget(dentry); /* Extra count - pin the dentry in core */
2598 return 0;
2599}
2600
Li Zefan099fca32009-04-02 16:57:29 -07002601/**
2602 * cgroup_file_mode - deduce file mode of a control file
2603 * @cft: the control file in question
2604 *
2605 * returns cft->mode if ->mode is not 0
2606 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2607 * returns S_IRUGO if it has only a read handler
2608 * returns S_IWUSR if it has only a write hander
2609 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002610static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002611{
Al Viroa5e7ed32011-07-26 01:55:55 -04002612 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002613
2614 if (cft->mode)
2615 return cft->mode;
2616
Tejun Heo2da8ca82013-12-05 12:28:04 -05002617 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002618 mode |= S_IRUGO;
2619
Tejun Heo6e0755b2013-12-05 12:28:03 -05002620 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2621 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002622 mode |= S_IWUSR;
2623
2624 return mode;
2625}
2626
Tejun Heo2bb566c2013-08-08 20:11:23 -04002627static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628{
Paul Menagebd89aab2007-10-18 23:40:44 -07002629 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002630 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002632 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002633 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002634 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002635 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002636
Tejun Heo9fa4db32013-08-26 18:40:56 -04002637 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2638 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002639 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 strcat(name, ".");
2641 }
2642 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002643
Paul Menageddbcc7e2007-10-18 23:39:30 -07002644 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002645
2646 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2647 if (!cfe)
2648 return -ENOMEM;
2649
Paul Menageddbcc7e2007-10-18 23:39:30 -07002650 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002651 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002653 goto out;
2654 }
2655
Li Zefand6cbf352013-05-14 19:44:20 +08002656 cfe->type = (void *)cft;
2657 cfe->dentry = dentry;
2658 dentry->d_fsdata = cfe;
2659 simple_xattrs_init(&cfe->xattrs);
2660
Tejun Heo05ef1d72012-04-01 12:09:56 -07002661 mode = cgroup_file_mode(cft);
2662 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2663 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002664 list_add_tail(&cfe->node, &parent->files);
2665 cfe = NULL;
2666 }
2667 dput(dentry);
2668out:
2669 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 return error;
2671}
2672
Tejun Heob1f28d32013-06-28 16:24:10 -07002673/**
2674 * cgroup_addrm_files - add or remove files to a cgroup directory
2675 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002676 * @cfts: array of cftypes to be added
2677 * @is_add: whether to add or remove
2678 *
2679 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002680 * For removals, this function never fails. If addition fails, this
2681 * function doesn't remove files already added. The caller is responsible
2682 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002683 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002684static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2685 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002687 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002688 int ret;
2689
2690 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2691 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002692
2693 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002694 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002695 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2696 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002697 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2698 continue;
2699 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2700 continue;
2701
Li Zefan2739d3c2013-01-21 18:18:33 +08002702 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002703 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002704 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002705 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002706 cft->name, ret);
2707 return ret;
2708 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002709 } else {
2710 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002711 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002713 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714}
2715
Tejun Heo8e3f6542012-04-01 12:09:55 -07002716static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002717 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002718{
2719 /*
2720 * Thanks to the entanglement with vfs inode locking, we can't walk
2721 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002722 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2723 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002724 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002725 mutex_lock(&cgroup_mutex);
2726}
2727
Tejun Heo2bb566c2013-08-08 20:11:23 -04002728static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002729 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002730{
2731 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002732 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002733 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002734 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002735 struct dentry *prev = NULL;
2736 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002737 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002738 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002739 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002740
2741 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002742 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002743 !atomic_inc_not_zero(&sb->s_active)) {
2744 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002745 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002746 }
2747
Li Zefane8c82d22013-06-18 18:48:37 +08002748 /*
2749 * All cgroups which are created after we drop cgroup_mutex will
2750 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002751 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002752 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002753 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002754
Tejun Heo8e3f6542012-04-01 12:09:55 -07002755 mutex_unlock(&cgroup_mutex);
2756
Li Zefane8c82d22013-06-18 18:48:37 +08002757 /* add/rm files for all cgroups created before */
2758 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002759 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002760 struct cgroup *cgrp = css->cgroup;
2761
Li Zefane8c82d22013-06-18 18:48:37 +08002762 if (cgroup_is_dead(cgrp))
2763 continue;
2764
2765 inode = cgrp->dentry->d_inode;
2766 dget(cgrp->dentry);
2767 rcu_read_unlock();
2768
2769 dput(prev);
2770 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002771
2772 mutex_lock(&inode->i_mutex);
2773 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002774 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002775 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002776 mutex_unlock(&cgroup_mutex);
2777 mutex_unlock(&inode->i_mutex);
2778
Li Zefane8c82d22013-06-18 18:48:37 +08002779 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002780 if (ret)
2781 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002782 }
Li Zefane8c82d22013-06-18 18:48:37 +08002783 rcu_read_unlock();
2784 dput(prev);
2785 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002786 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002787}
2788
2789/**
2790 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2791 * @ss: target cgroup subsystem
2792 * @cfts: zero-length name terminated array of cftypes
2793 *
2794 * Register @cfts to @ss. Files described by @cfts are created for all
2795 * existing cgroups to which @ss is attached and all future cgroups will
2796 * have them too. This function can be called anytime whether @ss is
2797 * attached or not.
2798 *
2799 * Returns 0 on successful registration, -errno on failure. Note that this
2800 * function currently returns 0 as long as @cfts registration is successful
2801 * even if some file creation attempts on existing cgroups fail.
2802 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002803int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804{
2805 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002806 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002807 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808
2809 set = kzalloc(sizeof(*set), GFP_KERNEL);
2810 if (!set)
2811 return -ENOMEM;
2812
Tejun Heo2bb566c2013-08-08 20:11:23 -04002813 for (cft = cfts; cft->name[0] != '\0'; cft++)
2814 cft->ss = ss;
2815
Tejun Heo8e3f6542012-04-01 12:09:55 -07002816 cgroup_cfts_prepare();
2817 set->cfts = cfts;
2818 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002819 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002820 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002821 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002822 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823}
2824EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2825
Li Zefana043e3b2008-02-23 15:24:09 -08002826/**
Tejun Heo79578622012-04-01 12:09:56 -07002827 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002828 * @cfts: zero-length name terminated array of cftypes
2829 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002830 * Unregister @cfts. Files described by @cfts are removed from all
2831 * existing cgroups and all future cgroups won't have them either. This
2832 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002833 *
2834 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002835 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002836 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002837int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002838{
2839 struct cftype_set *set;
2840
Tejun Heo2bb566c2013-08-08 20:11:23 -04002841 if (!cfts || !cfts[0].ss)
2842 return -ENOENT;
2843
Tejun Heo79578622012-04-01 12:09:56 -07002844 cgroup_cfts_prepare();
2845
Tejun Heo2bb566c2013-08-08 20:11:23 -04002846 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002847 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002848 list_del(&set->node);
2849 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002850 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002851 return 0;
2852 }
2853 }
2854
Tejun Heo2bb566c2013-08-08 20:11:23 -04002855 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002856 return -ENOENT;
2857}
2858
2859/**
Li Zefana043e3b2008-02-23 15:24:09 -08002860 * cgroup_task_count - count the number of tasks in a cgroup.
2861 * @cgrp: the cgroup in question
2862 *
2863 * Return the number of tasks in the cgroup.
2864 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002865int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002866{
2867 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002868 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002869
Paul Menage817929e2007-10-18 23:39:36 -07002870 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002871 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2872 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002873 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002874 return count;
2875}
2876
2877/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002878 * To reduce the fork() overhead for systems that are not actually using
2879 * their cgroups capability, we don't maintain the lists running through
2880 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002881 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002882 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002883static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002884{
2885 struct task_struct *p, *g;
2886 write_lock(&css_set_lock);
2887 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002888 /*
2889 * We need tasklist_lock because RCU is not safe against
2890 * while_each_thread(). Besides, a forking task that has passed
2891 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2892 * is not guaranteed to have its child immediately visible in the
2893 * tasklist if we walk through it with RCU.
2894 */
2895 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002896 do_each_thread(g, p) {
2897 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002898 /*
2899 * We should check if the process is exiting, otherwise
2900 * it will race with cgroup_exit() in that the list
2901 * entry won't be deleted though the process has exited.
2902 */
2903 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002904 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002905 task_unlock(p);
2906 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002907 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002908 write_unlock(&css_set_lock);
2909}
2910
Tejun Heo574bd9f2012-11-09 09:12:29 -08002911/**
Tejun Heo492eb212013-08-08 20:11:25 -04002912 * css_next_child - find the next child of a given css
2913 * @pos_css: the current position (%NULL to initiate traversal)
2914 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002915 *
Tejun Heo492eb212013-08-08 20:11:25 -04002916 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002917 * under either cgroup_mutex or RCU read lock. The only requirement is
2918 * that @parent_css and @pos_css are accessible. The next sibling is
2919 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002920 */
Tejun Heo492eb212013-08-08 20:11:25 -04002921struct cgroup_subsys_state *
2922css_next_child(struct cgroup_subsys_state *pos_css,
2923 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002924{
Tejun Heo492eb212013-08-08 20:11:25 -04002925 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2926 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002927 struct cgroup *next;
2928
Tejun Heo87fb54f2013-12-06 15:11:55 -05002929 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002930
2931 /*
2932 * @pos could already have been removed. Once a cgroup is removed,
2933 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002934 * changes. As CGRP_DEAD assertion is serialized and happens
2935 * before the cgroup is taken off the ->sibling list, if we see it
2936 * unasserted, it's guaranteed that the next sibling hasn't
2937 * finished its grace period even if it's already removed, and thus
2938 * safe to dereference from this RCU critical section. If
2939 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2940 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002941 *
2942 * If @pos is dead, its next pointer can't be dereferenced;
2943 * however, as each cgroup is given a monotonically increasing
2944 * unique serial number and always appended to the sibling list,
2945 * the next one can be found by walking the parent's children until
2946 * we see a cgroup with higher serial number than @pos's. While
2947 * this path can be slower, it's taken only when either the current
2948 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002949 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002950 if (!pos) {
2951 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2952 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002953 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002954 } else {
2955 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2956 if (next->serial_nr > pos->serial_nr)
2957 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002958 }
2959
Tejun Heo492eb212013-08-08 20:11:25 -04002960 if (&next->sibling == &cgrp->children)
2961 return NULL;
2962
Tejun Heoca8bdca2013-08-26 18:40:56 -04002963 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002964}
Tejun Heo492eb212013-08-08 20:11:25 -04002965EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002966
2967/**
Tejun Heo492eb212013-08-08 20:11:25 -04002968 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002969 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002970 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002971 *
Tejun Heo492eb212013-08-08 20:11:25 -04002972 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002973 * to visit for pre-order traversal of @root's descendants. @root is
2974 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002975 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002976 * While this function requires cgroup_mutex or RCU read locking, it
2977 * doesn't require the whole traversal to be contained in a single critical
2978 * section. This function will return the correct next descendant as long
2979 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980 */
Tejun Heo492eb212013-08-08 20:11:25 -04002981struct cgroup_subsys_state *
2982css_next_descendant_pre(struct cgroup_subsys_state *pos,
2983 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002984{
Tejun Heo492eb212013-08-08 20:11:25 -04002985 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002986
Tejun Heo87fb54f2013-12-06 15:11:55 -05002987 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002988
Tejun Heobd8815a2013-08-08 20:11:27 -04002989 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002990 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002991 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002992
2993 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002994 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002995 if (next)
2996 return next;
2997
2998 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002999 while (pos != root) {
3000 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003001 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003002 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003003 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003004 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003005
3006 return NULL;
3007}
Tejun Heo492eb212013-08-08 20:11:25 -04003008EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003009
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003010/**
Tejun Heo492eb212013-08-08 20:11:25 -04003011 * css_rightmost_descendant - return the rightmost descendant of a css
3012 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003013 *
Tejun Heo492eb212013-08-08 20:11:25 -04003014 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3015 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003016 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003017 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003018 * While this function requires cgroup_mutex or RCU read locking, it
3019 * doesn't require the whole traversal to be contained in a single critical
3020 * section. This function will return the correct rightmost descendant as
3021 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003022 */
Tejun Heo492eb212013-08-08 20:11:25 -04003023struct cgroup_subsys_state *
3024css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003025{
Tejun Heo492eb212013-08-08 20:11:25 -04003026 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003027
Tejun Heo87fb54f2013-12-06 15:11:55 -05003028 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003029
3030 do {
3031 last = pos;
3032 /* ->prev isn't RCU safe, walk ->next till the end */
3033 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003034 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003035 pos = tmp;
3036 } while (pos);
3037
3038 return last;
3039}
Tejun Heo492eb212013-08-08 20:11:25 -04003040EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003041
Tejun Heo492eb212013-08-08 20:11:25 -04003042static struct cgroup_subsys_state *
3043css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003044{
Tejun Heo492eb212013-08-08 20:11:25 -04003045 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046
3047 do {
3048 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003049 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003050 } while (pos);
3051
3052 return last;
3053}
3054
3055/**
Tejun Heo492eb212013-08-08 20:11:25 -04003056 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003058 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003059 *
Tejun Heo492eb212013-08-08 20:11:25 -04003060 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003061 * to visit for post-order traversal of @root's descendants. @root is
3062 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003063 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003064 * While this function requires cgroup_mutex or RCU read locking, it
3065 * doesn't require the whole traversal to be contained in a single critical
3066 * section. This function will return the correct next descendant as long
3067 * as both @pos and @cgroup are accessible and @pos is a descendant of
3068 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003069 */
Tejun Heo492eb212013-08-08 20:11:25 -04003070struct cgroup_subsys_state *
3071css_next_descendant_post(struct cgroup_subsys_state *pos,
3072 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003073{
Tejun Heo492eb212013-08-08 20:11:25 -04003074 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075
Tejun Heo87fb54f2013-12-06 15:11:55 -05003076 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077
Tejun Heo58b79a92013-09-06 15:31:08 -04003078 /* if first iteration, visit leftmost descendant which may be @root */
3079 if (!pos)
3080 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081
Tejun Heobd8815a2013-08-08 20:11:27 -04003082 /* if we visited @root, we're done */
3083 if (pos == root)
3084 return NULL;
3085
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003087 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003088 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003089 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090
3091 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003092 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003093}
Tejun Heo492eb212013-08-08 20:11:25 -04003094EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003095
Tejun Heo0942eee2013-08-08 20:11:26 -04003096/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003097 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003098 * @it: the iterator to advance
3099 *
3100 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003101 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003102static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003103{
3104 struct list_head *l = it->cset_link;
3105 struct cgrp_cset_link *link;
3106 struct css_set *cset;
3107
3108 /* Advance to the next non-empty css_set */
3109 do {
3110 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003111 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003112 it->cset_link = NULL;
3113 return;
3114 }
3115 link = list_entry(l, struct cgrp_cset_link, cset_link);
3116 cset = link->cset;
3117 } while (list_empty(&cset->tasks));
3118 it->cset_link = l;
3119 it->task = cset->tasks.next;
3120}
3121
Tejun Heo0942eee2013-08-08 20:11:26 -04003122/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003123 * css_task_iter_start - initiate task iteration
3124 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003125 * @it: the task iterator to use
3126 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003127 * Initiate iteration through the tasks of @css. The caller can call
3128 * css_task_iter_next() to walk through the tasks until the function
3129 * returns NULL. On completion of iteration, css_task_iter_end() must be
3130 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003131 *
3132 * Note that this function acquires a lock which is released when the
3133 * iteration finishes. The caller can't sleep while iteration is in
3134 * progress.
3135 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003136void css_task_iter_start(struct cgroup_subsys_state *css,
3137 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003138 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003139{
3140 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003141 * The first time anyone tries to iterate across a css, we need to
3142 * enable the list linking each css_set to its tasks, and fix up
3143 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003144 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003145 if (!use_task_css_set_links)
3146 cgroup_enable_task_cg_lists();
3147
Paul Menage817929e2007-10-18 23:39:36 -07003148 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003149
Tejun Heo72ec7022013-08-08 20:11:26 -04003150 it->origin_css = css;
3151 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003152
Tejun Heo72ec7022013-08-08 20:11:26 -04003153 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003154}
3155
Tejun Heo0942eee2013-08-08 20:11:26 -04003156/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003157 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003158 * @it: the task iterator being iterated
3159 *
3160 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003161 * initialized via css_task_iter_start(). Returns NULL when the iteration
3162 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003163 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003164struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003165{
3166 struct task_struct *res;
3167 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003168 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003169
3170 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003171 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003172 return NULL;
3173 res = list_entry(l, struct task_struct, cg_list);
3174 /* Advance iterator to find next entry */
3175 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003176 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3177 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003178 /*
3179 * We reached the end of this task list - move on to the
3180 * next cgrp_cset_link.
3181 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003182 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003183 } else {
3184 it->task = l;
3185 }
3186 return res;
3187}
3188
Tejun Heo0942eee2013-08-08 20:11:26 -04003189/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003190 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003191 * @it: the task iterator to finish
3192 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003193 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003194 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003195void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003196 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003197{
3198 read_unlock(&css_set_lock);
3199}
3200
Cliff Wickman31a7df02008-02-07 00:14:42 -08003201static inline int started_after_time(struct task_struct *t1,
3202 struct timespec *time,
3203 struct task_struct *t2)
3204{
3205 int start_diff = timespec_compare(&t1->start_time, time);
3206 if (start_diff > 0) {
3207 return 1;
3208 } else if (start_diff < 0) {
3209 return 0;
3210 } else {
3211 /*
3212 * Arbitrarily, if two processes started at the same
3213 * time, we'll say that the lower pointer value
3214 * started first. Note that t2 may have exited by now
3215 * so this may not be a valid pointer any longer, but
3216 * that's fine - it still serves to distinguish
3217 * between two tasks started (effectively) simultaneously.
3218 */
3219 return t1 > t2;
3220 }
3221}
3222
3223/*
3224 * This function is a callback from heap_insert() and is used to order
3225 * the heap.
3226 * In this case we order the heap in descending task start time.
3227 */
3228static inline int started_after(void *p1, void *p2)
3229{
3230 struct task_struct *t1 = p1;
3231 struct task_struct *t2 = p2;
3232 return started_after_time(t1, &t2->start_time, t2);
3233}
3234
3235/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003236 * css_scan_tasks - iterate though all the tasks in a css
3237 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003238 * @test: optional test callback
3239 * @process: process callback
3240 * @data: data passed to @test and @process
3241 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003242 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003243 * Iterate through all the tasks in @css, calling @test for each, and if it
3244 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003245 *
Tejun Heoe5358372013-08-08 20:11:26 -04003246 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003247 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003248 * lock css_set_lock for the call to @process.
3249 *
3250 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003251 * of @css for the duration of this call. This function may or may not
3252 * call @process for tasks that exit or move to a different css during the
3253 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003254 *
3255 * Note that @test may be called with locks held, and may in some
3256 * situations be called multiple times for the same task, so it should be
3257 * cheap.
3258 *
3259 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3260 * heap operations (and its "gt" member will be overwritten), else a
3261 * temporary heap will be used (allocation of which may cause this function
3262 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003263 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003264int css_scan_tasks(struct cgroup_subsys_state *css,
3265 bool (*test)(struct task_struct *, void *),
3266 void (*process)(struct task_struct *, void *),
3267 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003268{
3269 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003270 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003271 struct task_struct *p, *dropped;
3272 /* Never dereference latest_task, since it's not refcounted */
3273 struct task_struct *latest_task = NULL;
3274 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003275 struct timespec latest_time = { 0, 0 };
3276
Tejun Heoe5358372013-08-08 20:11:26 -04003277 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003278 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279 heap->gt = &started_after;
3280 } else {
3281 /* We need to allocate our own heap memory */
3282 heap = &tmp_heap;
3283 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3284 if (retval)
3285 /* cannot allocate the heap */
3286 return retval;
3287 }
3288
3289 again:
3290 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003291 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003292 * which are of interest, and invoking @process callback on the
3293 * ones which need an update. Since we don't want to hold any
3294 * locks during the task updates, gather tasks to be processed in a
3295 * heap structure. The heap is sorted by descending task start
3296 * time. If the statically-sized heap fills up, we overflow tasks
3297 * that started later, and in future iterations only consider tasks
3298 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003299 * guarantees forward progress and that we don't miss any tasks.
3300 */
3301 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003302 css_task_iter_start(css, &it);
3303 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003304 /*
3305 * Only affect tasks that qualify per the caller's callback,
3306 * if he provided one
3307 */
Tejun Heoe5358372013-08-08 20:11:26 -04003308 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003309 continue;
3310 /*
3311 * Only process tasks that started after the last task
3312 * we processed
3313 */
3314 if (!started_after_time(p, &latest_time, latest_task))
3315 continue;
3316 dropped = heap_insert(heap, p);
3317 if (dropped == NULL) {
3318 /*
3319 * The new task was inserted; the heap wasn't
3320 * previously full
3321 */
3322 get_task_struct(p);
3323 } else if (dropped != p) {
3324 /*
3325 * The new task was inserted, and pushed out a
3326 * different task
3327 */
3328 get_task_struct(p);
3329 put_task_struct(dropped);
3330 }
3331 /*
3332 * Else the new task was newer than anything already in
3333 * the heap and wasn't inserted
3334 */
3335 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003336 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003337
3338 if (heap->size) {
3339 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003340 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003342 latest_time = q->start_time;
3343 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003344 }
3345 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003346 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003347 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003348 }
3349 /*
3350 * If we had to process any tasks at all, scan again
3351 * in case some of them were in the middle of forking
3352 * children that didn't get processed.
3353 * Not the most efficient way to do it, but it avoids
3354 * having to take callback_mutex in the fork path
3355 */
3356 goto again;
3357 }
3358 if (heap == &tmp_heap)
3359 heap_free(&tmp_heap);
3360 return 0;
3361}
3362
Tejun Heoe5358372013-08-08 20:11:26 -04003363static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003364{
Tejun Heoe5358372013-08-08 20:11:26 -04003365 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003366
Tejun Heo47cfcd02013-04-07 09:29:51 -07003367 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003368 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003369 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003370}
3371
3372/**
3373 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3374 * @to: cgroup to which the tasks will be moved
3375 * @from: cgroup in which the tasks currently reside
3376 */
3377int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3378{
Tejun Heo72ec7022013-08-08 20:11:26 -04003379 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3380 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003381}
3382
Paul Menage817929e2007-10-18 23:39:36 -07003383/*
Ben Blum102a7752009-09-23 15:56:26 -07003384 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003385 *
3386 * Reading this file can return large amounts of data if a cgroup has
3387 * *lots* of attached tasks. So it may need several calls to read(),
3388 * but we cannot guarantee that the information we produce is correct
3389 * unless we produce it entirely atomically.
3390 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003391 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392
Li Zefan24528252012-01-20 11:58:43 +08003393/* which pidlist file are we talking about? */
3394enum cgroup_filetype {
3395 CGROUP_FILE_PROCS,
3396 CGROUP_FILE_TASKS,
3397};
3398
3399/*
3400 * A pidlist is a list of pids that virtually represents the contents of one
3401 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3402 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3403 * to the cgroup.
3404 */
3405struct cgroup_pidlist {
3406 /*
3407 * used to find which pidlist is wanted. doesn't change as long as
3408 * this particular list stays in the list.
3409 */
3410 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3411 /* array of xids */
3412 pid_t *list;
3413 /* how many elements the above list has */
3414 int length;
Li Zefan24528252012-01-20 11:58:43 +08003415 /* each of these stored in a list by its cgroup */
3416 struct list_head links;
3417 /* pointer to the cgroup we belong to, for list removal purposes */
3418 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003419 /* for delayed destruction */
3420 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003421};
3422
Paul Menagebbcb81d2007-10-18 23:39:32 -07003423/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003424 * The following two functions "fix" the issue where there are more pids
3425 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3426 * TODO: replace with a kernel-wide solution to this problem
3427 */
3428#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3429static void *pidlist_allocate(int count)
3430{
3431 if (PIDLIST_TOO_LARGE(count))
3432 return vmalloc(count * sizeof(pid_t));
3433 else
3434 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3435}
Tejun Heob1a21362013-11-29 10:42:58 -05003436
Ben Blumd1d9fd32009-09-23 15:56:28 -07003437static void pidlist_free(void *p)
3438{
3439 if (is_vmalloc_addr(p))
3440 vfree(p);
3441 else
3442 kfree(p);
3443}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003444
3445/*
Tejun Heob1a21362013-11-29 10:42:58 -05003446 * Used to destroy all pidlists lingering waiting for destroy timer. None
3447 * should be left afterwards.
3448 */
3449static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3450{
3451 struct cgroup_pidlist *l, *tmp_l;
3452
3453 mutex_lock(&cgrp->pidlist_mutex);
3454 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3455 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3456 mutex_unlock(&cgrp->pidlist_mutex);
3457
3458 flush_workqueue(cgroup_pidlist_destroy_wq);
3459 BUG_ON(!list_empty(&cgrp->pidlists));
3460}
3461
3462static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3463{
3464 struct delayed_work *dwork = to_delayed_work(work);
3465 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3466 destroy_dwork);
3467 struct cgroup_pidlist *tofree = NULL;
3468
3469 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003470
3471 /*
Tejun Heo04502362013-11-29 10:42:59 -05003472 * Destroy iff we didn't get queued again. The state won't change
3473 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003474 */
Tejun Heo04502362013-11-29 10:42:59 -05003475 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003476 list_del(&l->links);
3477 pidlist_free(l->list);
3478 put_pid_ns(l->key.ns);
3479 tofree = l;
3480 }
3481
Tejun Heob1a21362013-11-29 10:42:58 -05003482 mutex_unlock(&l->owner->pidlist_mutex);
3483 kfree(tofree);
3484}
3485
3486/*
Ben Blum102a7752009-09-23 15:56:26 -07003487 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003488 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003489 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003490static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003491{
Ben Blum102a7752009-09-23 15:56:26 -07003492 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003493
3494 /*
3495 * we presume the 0th element is unique, so i starts at 1. trivial
3496 * edge cases first; no work needs to be done for either
3497 */
3498 if (length == 0 || length == 1)
3499 return length;
3500 /* src and dest walk down the list; dest counts unique elements */
3501 for (src = 1; src < length; src++) {
3502 /* find next unique element */
3503 while (list[src] == list[src-1]) {
3504 src++;
3505 if (src == length)
3506 goto after;
3507 }
3508 /* dest always points to where the next unique element goes */
3509 list[dest] = list[src];
3510 dest++;
3511 }
3512after:
Ben Blum102a7752009-09-23 15:56:26 -07003513 return dest;
3514}
3515
Tejun Heoafb2bc12013-11-29 10:42:59 -05003516/*
3517 * The two pid files - task and cgroup.procs - guaranteed that the result
3518 * is sorted, which forced this whole pidlist fiasco. As pid order is
3519 * different per namespace, each namespace needs differently sorted list,
3520 * making it impossible to use, for example, single rbtree of member tasks
3521 * sorted by task pointer. As pidlists can be fairly large, allocating one
3522 * per open file is dangerous, so cgroup had to implement shared pool of
3523 * pidlists keyed by cgroup and namespace.
3524 *
3525 * All this extra complexity was caused by the original implementation
3526 * committing to an entirely unnecessary property. In the long term, we
3527 * want to do away with it. Explicitly scramble sort order if
3528 * sane_behavior so that no such expectation exists in the new interface.
3529 *
3530 * Scrambling is done by swapping every two consecutive bits, which is
3531 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3532 */
3533static pid_t pid_fry(pid_t pid)
3534{
3535 unsigned a = pid & 0x55555555;
3536 unsigned b = pid & 0xAAAAAAAA;
3537
3538 return (a << 1) | (b >> 1);
3539}
3540
3541static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3542{
3543 if (cgroup_sane_behavior(cgrp))
3544 return pid_fry(pid);
3545 else
3546 return pid;
3547}
3548
Ben Blum102a7752009-09-23 15:56:26 -07003549static int cmppid(const void *a, const void *b)
3550{
3551 return *(pid_t *)a - *(pid_t *)b;
3552}
3553
Tejun Heoafb2bc12013-11-29 10:42:59 -05003554static int fried_cmppid(const void *a, const void *b)
3555{
3556 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3557}
3558
Ben Blum72a8cb32009-09-23 15:56:27 -07003559static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3560 enum cgroup_filetype type)
3561{
3562 struct cgroup_pidlist *l;
3563 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003564 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003565
Tejun Heoe6b81712013-11-29 10:42:59 -05003566 lockdep_assert_held(&cgrp->pidlist_mutex);
3567
3568 list_for_each_entry(l, &cgrp->pidlists, links)
3569 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003570 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003571 return NULL;
3572}
3573
3574/*
3575 * find the appropriate pidlist for our purpose (given procs vs tasks)
3576 * returns with the lock on that pidlist already held, and takes care
3577 * of the use count, or returns NULL with no locks held if we're out of
3578 * memory.
3579 */
3580static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3581 enum cgroup_filetype type)
3582{
3583 struct cgroup_pidlist *l;
3584
3585 lockdep_assert_held(&cgrp->pidlist_mutex);
3586
3587 l = cgroup_pidlist_find(cgrp, type);
3588 if (l)
3589 return l;
3590
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003592 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003593 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003595
Tejun Heob1a21362013-11-29 10:42:58 -05003596 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003598 /* don't need task_nsproxy() if we're looking at ourself */
3599 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003600 l->owner = cgrp;
3601 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 return l;
3603}
3604
3605/*
Ben Blum102a7752009-09-23 15:56:26 -07003606 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3607 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003608static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3609 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003610{
3611 pid_t *array;
3612 int length;
3613 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003614 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003615 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003616 struct cgroup_pidlist *l;
3617
Tejun Heo4bac00d2013-11-29 10:42:59 -05003618 lockdep_assert_held(&cgrp->pidlist_mutex);
3619
Ben Blum102a7752009-09-23 15:56:26 -07003620 /*
3621 * If cgroup gets more users after we read count, we won't have
3622 * enough space - tough. This race is indistinguishable to the
3623 * caller from the case that the additional cgroup users didn't
3624 * show up until sometime later on.
3625 */
3626 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003627 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003628 if (!array)
3629 return -ENOMEM;
3630 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003631 css_task_iter_start(&cgrp->dummy_css, &it);
3632 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003633 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003634 break;
Ben Blum102a7752009-09-23 15:56:26 -07003635 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (type == CGROUP_FILE_PROCS)
3637 pid = task_tgid_vnr(tsk);
3638 else
3639 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003640 if (pid > 0) /* make sure to only use valid results */
3641 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003642 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003643 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003644 length = n;
3645 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003646 if (cgroup_sane_behavior(cgrp))
3647 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3648 else
3649 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003651 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003652
Tejun Heoe6b81712013-11-29 10:42:59 -05003653 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003655 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003656 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003658 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003659
3660 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003661 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003662 l->list = array;
3663 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003664 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003665 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003666}
3667
Balbir Singh846c7bb2007-10-18 23:39:44 -07003668/**
Li Zefana043e3b2008-02-23 15:24:09 -08003669 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003670 * @stats: cgroupstats to fill information into
3671 * @dentry: A dentry entry belonging to the cgroup for which stats have
3672 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003673 *
3674 * Build and fill cgroupstats so that taskstats can export it to user
3675 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003676 */
3677int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3678{
3679 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003680 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003681 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003683
Balbir Singh846c7bb2007-10-18 23:39:44 -07003684 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003685 * Validate dentry by checking the superblock operations,
3686 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687 */
Li Zefan33d283b2008-11-19 15:36:48 -08003688 if (dentry->d_sb->s_op != &cgroup_ops ||
3689 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690 goto err;
3691
3692 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003693 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003694
Tejun Heo72ec7022013-08-08 20:11:26 -04003695 css_task_iter_start(&cgrp->dummy_css, &it);
3696 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003697 switch (tsk->state) {
3698 case TASK_RUNNING:
3699 stats->nr_running++;
3700 break;
3701 case TASK_INTERRUPTIBLE:
3702 stats->nr_sleeping++;
3703 break;
3704 case TASK_UNINTERRUPTIBLE:
3705 stats->nr_uninterruptible++;
3706 break;
3707 case TASK_STOPPED:
3708 stats->nr_stopped++;
3709 break;
3710 default:
3711 if (delayacct_is_task_waiting_on_io(tsk))
3712 stats->nr_io_wait++;
3713 break;
3714 }
3715 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003716 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003717
Balbir Singh846c7bb2007-10-18 23:39:44 -07003718err:
3719 return ret;
3720}
3721
Paul Menage8f3ff202009-09-23 15:56:25 -07003722
Paul Menagecc31edc2008-10-18 20:28:04 -07003723/*
Ben Blum102a7752009-09-23 15:56:26 -07003724 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003726 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 */
3728
Ben Blum102a7752009-09-23 15:56:26 -07003729static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003730{
3731 /*
3732 * Initially we receive a position value that corresponds to
3733 * one more than the last pid shown (or 0 on the first call or
3734 * after a seek to the start). Use a binary-search to find the
3735 * next pid to display, if any
3736 */
Tejun Heo5d224442013-12-05 12:28:04 -05003737 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003738 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003739 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003740 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003742 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003743
Tejun Heo4bac00d2013-11-29 10:42:59 -05003744 mutex_lock(&cgrp->pidlist_mutex);
3745
3746 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003747 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003748 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003749 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003750 * could already have been destroyed.
3751 */
Tejun Heo5d224442013-12-05 12:28:04 -05003752 if (of->priv)
3753 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003754
3755 /*
3756 * Either this is the first start() after open or the matching
3757 * pidlist has been destroyed inbetween. Create a new one.
3758 */
Tejun Heo5d224442013-12-05 12:28:04 -05003759 if (!of->priv) {
3760 ret = pidlist_array_load(cgrp, type,
3761 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003762 if (ret)
3763 return ERR_PTR(ret);
3764 }
Tejun Heo5d224442013-12-05 12:28:04 -05003765 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003766
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003768 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003769
Paul Menagecc31edc2008-10-18 20:28:04 -07003770 while (index < end) {
3771 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003772 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003773 index = mid;
3774 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003775 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 index = mid + 1;
3777 else
3778 end = mid;
3779 }
3780 }
3781 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003782 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783 return NULL;
3784 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003785 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003786 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003788}
3789
Ben Blum102a7752009-09-23 15:56:26 -07003790static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003791{
Tejun Heo5d224442013-12-05 12:28:04 -05003792 struct cgroup_open_file *of = s->private;
3793 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003794
Tejun Heo5d224442013-12-05 12:28:04 -05003795 if (l)
3796 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003797 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003798 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003799}
3800
Ben Blum102a7752009-09-23 15:56:26 -07003801static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003802{
Tejun Heo5d224442013-12-05 12:28:04 -05003803 struct cgroup_open_file *of = s->private;
3804 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003805 pid_t *p = v;
3806 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003807 /*
3808 * Advance to the next pid in the array. If this goes off the
3809 * end, we're done
3810 */
3811 p++;
3812 if (p >= end) {
3813 return NULL;
3814 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003815 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003816 return p;
3817 }
3818}
3819
Ben Blum102a7752009-09-23 15:56:26 -07003820static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003821{
3822 return seq_printf(s, "%d\n", *(int *)v);
3823}
3824
Ben Blum102a7752009-09-23 15:56:26 -07003825/*
3826 * seq_operations functions for iterating on pidlists through seq_file -
3827 * independent of whether it's tasks or procs
3828 */
3829static const struct seq_operations cgroup_pidlist_seq_operations = {
3830 .start = cgroup_pidlist_start,
3831 .stop = cgroup_pidlist_stop,
3832 .next = cgroup_pidlist_next,
3833 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003834};
3835
Tejun Heo182446d2013-08-08 20:11:24 -04003836static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3837 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003838{
Tejun Heo182446d2013-08-08 20:11:24 -04003839 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003840}
3841
Tejun Heo182446d2013-08-08 20:11:24 -04003842static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3843 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003844{
Tejun Heo182446d2013-08-08 20:11:24 -04003845 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003846 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003847 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003848 else
Tejun Heo182446d2013-08-08 20:11:24 -04003849 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003850 return 0;
3851}
3852
Paul Menagebbcb81d2007-10-18 23:39:32 -07003853/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003854 * When dput() is called asynchronously, if umount has been done and
3855 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3856 * there's a small window that vfs will see the root dentry with non-zero
3857 * refcnt and trigger BUG().
3858 *
3859 * That's why we hold a reference before dput() and drop it right after.
3860 */
3861static void cgroup_dput(struct cgroup *cgrp)
3862{
3863 struct super_block *sb = cgrp->root->sb;
3864
3865 atomic_inc(&sb->s_active);
3866 dput(cgrp->dentry);
3867 deactivate_super(sb);
3868}
3869
Tejun Heo182446d2013-08-08 20:11:24 -04003870static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3871 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003872{
Tejun Heo182446d2013-08-08 20:11:24 -04003873 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003874}
3875
Tejun Heo182446d2013-08-08 20:11:24 -04003876static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3877 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003878{
3879 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003880 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003881 else
Tejun Heo182446d2013-08-08 20:11:24 -04003882 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003883 return 0;
3884}
3885
Tejun Heod5c56ce2013-06-03 19:14:34 -07003886static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003887 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003888 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003889 .seq_start = cgroup_pidlist_start,
3890 .seq_next = cgroup_pidlist_next,
3891 .seq_stop = cgroup_pidlist_stop,
3892 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003893 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003894 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003895 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003896 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003897 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003898 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003899 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003900 .read_u64 = cgroup_clone_children_read,
3901 .write_u64 = cgroup_clone_children_write,
3902 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003903 {
Tejun Heo873fe092013-04-14 20:15:26 -07003904 .name = "cgroup.sane_behavior",
3905 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003906 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003907 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003908
3909 /*
3910 * Historical crazy stuff. These don't have "cgroup." prefix and
3911 * don't exist if sane_behavior. If you're depending on these, be
3912 * prepared to be burned.
3913 */
3914 {
3915 .name = "tasks",
3916 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003917 .seq_start = cgroup_pidlist_start,
3918 .seq_next = cgroup_pidlist_next,
3919 .seq_stop = cgroup_pidlist_stop,
3920 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003921 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003922 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003923 .mode = S_IRUGO | S_IWUSR,
3924 },
3925 {
3926 .name = "notify_on_release",
3927 .flags = CFTYPE_INSANE,
3928 .read_u64 = cgroup_read_notify_on_release,
3929 .write_u64 = cgroup_write_notify_on_release,
3930 },
Tejun Heo873fe092013-04-14 20:15:26 -07003931 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003932 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003933 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003934 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003935 .write_string = cgroup_release_agent_write,
3936 .max_write_len = PATH_MAX,
3937 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003938 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003939};
3940
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003941/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003942 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003943 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003944 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003945 *
3946 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003947 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003948static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003949{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003950 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003951 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003952
Tejun Heo8e3f6542012-04-01 12:09:55 -07003953 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003954 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003955 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003956
3957 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003958 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003959
Tejun Heobee55092013-06-28 16:24:11 -07003960 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003961 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003962 if (ret < 0)
3963 goto err;
3964 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003965 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003966 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003967err:
3968 cgroup_clear_dir(cgrp, subsys_mask);
3969 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003970}
3971
Tejun Heo0c21ead2013-08-13 20:22:51 -04003972/*
3973 * css destruction is four-stage process.
3974 *
3975 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3976 * Implemented in kill_css().
3977 *
3978 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3979 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3980 * by invoking offline_css(). After offlining, the base ref is put.
3981 * Implemented in css_killed_work_fn().
3982 *
3983 * 3. When the percpu_ref reaches zero, the only possible remaining
3984 * accessors are inside RCU read sections. css_release() schedules the
3985 * RCU callback.
3986 *
3987 * 4. After the grace period, the css can be freed. Implemented in
3988 * css_free_work_fn().
3989 *
3990 * It is actually hairier because both step 2 and 4 require process context
3991 * and thus involve punting to css->destroy_work adding two additional
3992 * steps to the already complex sequence.
3993 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003994static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003995{
3996 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003997 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003998 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003999
Tejun Heo0ae78e02013-08-13 11:01:54 -04004000 if (css->parent)
4001 css_put(css->parent);
4002
Tejun Heo0c21ead2013-08-13 20:22:51 -04004003 css->ss->css_free(css);
4004 cgroup_dput(cgrp);
4005}
4006
4007static void css_free_rcu_fn(struct rcu_head *rcu_head)
4008{
4009 struct cgroup_subsys_state *css =
4010 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4011
4012 /*
4013 * css holds an extra ref to @cgrp->dentry which is put on the last
4014 * css_put(). dput() requires process context which we don't have.
4015 */
4016 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004017 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004018}
4019
Tejun Heod3daf282013-06-13 19:39:16 -07004020static void css_release(struct percpu_ref *ref)
4021{
4022 struct cgroup_subsys_state *css =
4023 container_of(ref, struct cgroup_subsys_state, refcnt);
4024
Tejun Heo0c21ead2013-08-13 20:22:51 -04004025 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004026}
4027
Tejun Heo623f9262013-08-13 11:01:55 -04004028static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4029 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004030{
Paul Menagebd89aab2007-10-18 23:40:44 -07004031 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004032 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004033 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004034
Tejun Heo0ae78e02013-08-13 11:01:54 -04004035 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004036 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004037 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004038 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004039
Tejun Heoca8bdca2013-08-26 18:40:56 -04004040 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041}
4042
Li Zefan2a4ac632013-07-31 16:16:40 +08004043/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004044static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004045{
Tejun Heo623f9262013-08-13 11:01:55 -04004046 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004047 int ret = 0;
4048
Tejun Heoa31f2d32012-11-19 08:13:37 -08004049 lockdep_assert_held(&cgroup_mutex);
4050
Tejun Heo92fb9742012-11-19 08:13:38 -08004051 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004052 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004053 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004054 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004055 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004056 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4057 }
Tejun Heob1929db2012-11-19 08:13:38 -08004058 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004059}
4060
Li Zefan2a4ac632013-07-31 16:16:40 +08004061/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004062static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004063{
Tejun Heo623f9262013-08-13 11:01:55 -04004064 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004065
4066 lockdep_assert_held(&cgroup_mutex);
4067
4068 if (!(css->flags & CSS_ONLINE))
4069 return;
4070
Li Zefand7eeac12013-03-12 15:35:59 -07004071 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004072 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004073
Tejun Heoeb954192013-08-08 20:11:23 -04004074 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004075 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004076 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077}
4078
Tejun Heoc81c925a2013-12-06 15:11:56 -05004079/**
4080 * create_css - create a cgroup_subsys_state
4081 * @cgrp: the cgroup new css will be associated with
4082 * @ss: the subsys of new css
4083 *
4084 * Create a new css associated with @cgrp - @ss pair. On success, the new
4085 * css is online and installed in @cgrp with all interface files created.
4086 * Returns 0 on success, -errno on failure.
4087 */
4088static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4089{
4090 struct cgroup *parent = cgrp->parent;
4091 struct cgroup_subsys_state *css;
4092 int err;
4093
4094 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4095 lockdep_assert_held(&cgroup_mutex);
4096
4097 css = ss->css_alloc(cgroup_css(parent, ss));
4098 if (IS_ERR(css))
4099 return PTR_ERR(css);
4100
4101 err = percpu_ref_init(&css->refcnt, css_release);
4102 if (err)
4103 goto err_free;
4104
4105 init_css(css, ss, cgrp);
4106
4107 err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id);
4108 if (err)
4109 goto err_free;
4110
4111 err = online_css(css);
4112 if (err)
4113 goto err_free;
4114
4115 dget(cgrp->dentry);
4116 css_get(css->parent);
4117
4118 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4119 parent->parent) {
4120 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",
4121 current->comm, current->pid, ss->name);
4122 if (!strcmp(ss->name, "memory"))
4123 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4124 ss->warned_broken_hierarchy = true;
4125 }
4126
4127 return 0;
4128
4129err_free:
4130 percpu_ref_cancel_init(&css->refcnt);
4131 ss->css_free(css);
4132 return err;
4133}
4134
Paul Menageddbcc7e2007-10-18 23:39:30 -07004135/*
Li Zefana043e3b2008-02-23 15:24:09 -08004136 * cgroup_create - create a cgroup
4137 * @parent: cgroup that will be parent of the new cgroup
4138 * @dentry: dentry of the new cgroup
4139 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004140 *
Li Zefana043e3b2008-02-23 15:24:09 -08004141 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004144 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145{
Paul Menagebd89aab2007-10-18 23:40:44 -07004146 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004147 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004148 struct cgroupfs_root *root = parent->root;
Tejun Heob85d2042013-12-06 15:11:57 -05004149 int ssid, err = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004150 struct cgroup_subsys *ss;
4151 struct super_block *sb = root->sb;
4152
Tejun Heo0a950f62012-11-19 09:02:12 -08004153 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004154 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4155 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156 return -ENOMEM;
4157
Li Zefan65dff752013-03-01 15:01:56 +08004158 name = cgroup_alloc_name(dentry);
4159 if (!name)
4160 goto err_free_cgrp;
4161 rcu_assign_pointer(cgrp->name, name);
4162
Li Zefan4e96ee82013-07-31 09:50:50 +08004163 /*
4164 * Temporarily set the pointer to NULL, so idr_find() won't return
4165 * a half-baked cgroup.
4166 */
4167 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004168 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004169 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004170
Tejun Heo976c06b2012-11-05 09:16:59 -08004171 /*
4172 * Only live parents can have children. Note that the liveliness
4173 * check isn't strictly necessary because cgroup_mkdir() and
4174 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4175 * anyway so that locking is contained inside cgroup proper and we
4176 * don't get nasty surprises if we ever grow another caller.
4177 */
4178 if (!cgroup_lock_live_group(parent)) {
4179 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004180 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004181 }
4182
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183 /* Grab a reference on the superblock so the hierarchy doesn't
4184 * get deleted on unmount if there are child cgroups. This
4185 * can be done outside cgroup_mutex, since the sb can't
4186 * disappear while someone has an open control file on the
4187 * fs */
4188 atomic_inc(&sb->s_active);
4189
Paul Menagecc31edc2008-10-18 20:28:04 -07004190 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191
Li Zefanfe1c06c2013-01-24 14:30:22 +08004192 dentry->d_fsdata = cgrp;
4193 cgrp->dentry = dentry;
4194
Paul Menagebd89aab2007-10-18 23:40:44 -07004195 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004196 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004197 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198
Li Zefanb6abdb02008-03-04 14:28:19 -08004199 if (notify_on_release(parent))
4200 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4201
Tejun Heo2260e7f2012-11-19 08:13:38 -08004202 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4203 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004204
Tejun Heo4e139af2012-11-19 08:13:36 -08004205 /*
4206 * Create directory. cgroup_create_file() returns with the new
4207 * directory locked on success so that it can be populated without
4208 * dropping cgroup_mutex.
4209 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004210 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211 if (err < 0)
Tejun Heo0d802552013-12-06 15:11:56 -05004212 goto err_unlock;
Tejun Heo4e139af2012-11-19 08:13:36 -08004213 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214
Tejun Heo00356bd2013-06-18 11:14:22 -07004215 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004216
Tejun Heo4e139af2012-11-19 08:13:36 -08004217 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004218 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4219 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004220
Li Zefan415cf072013-04-08 14:35:02 +08004221 /* hold a ref to the parent's dentry */
4222 dget(parent->dentry);
4223
Tejun Heo0d802552013-12-06 15:11:56 -05004224 /*
4225 * @cgrp is now fully operational. If something fails after this
4226 * point, it'll be released via the normal destruction path.
4227 */
4228 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4229
4230 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4231 if (err)
4232 goto err_destroy;
4233
Tejun Heo9d403e92013-12-06 15:11:56 -05004234 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004235 for_each_subsys(ss, ssid) {
4236 if (root->subsys_mask & (1 << ssid)) {
4237 err = create_css(cgrp, ss);
4238 if (err)
4239 goto err_destroy;
4240 }
Tejun Heoa8638032012-11-09 09:12:29 -08004241 }
4242
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004244 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245
4246 return 0;
4247
Tejun Heo0d802552013-12-06 15:11:56 -05004248err_unlock:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250 /* Release the reference count that we took on the superblock */
4251 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004252err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004253 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004254err_free_name:
4255 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004256err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004257 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004259
4260err_destroy:
4261 cgroup_destroy_locked(cgrp);
4262 mutex_unlock(&cgroup_mutex);
4263 mutex_unlock(&dentry->d_inode->i_mutex);
4264 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265}
4266
Al Viro18bb1db2011-07-26 01:41:39 -04004267static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268{
4269 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4270
4271 /* the vfs holds inode->i_mutex already */
4272 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4273}
4274
Tejun Heo223dbc32013-08-13 20:22:50 -04004275/*
4276 * This is called when the refcnt of a css is confirmed to be killed.
4277 * css_tryget() is now guaranteed to fail.
4278 */
4279static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004280{
Tejun Heo223dbc32013-08-13 20:22:50 -04004281 struct cgroup_subsys_state *css =
4282 container_of(work, struct cgroup_subsys_state, destroy_work);
4283 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004284
Tejun Heof20104d2013-08-13 20:22:50 -04004285 mutex_lock(&cgroup_mutex);
4286
4287 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004288 * css_tryget() is guaranteed to fail now. Tell subsystems to
4289 * initate destruction.
4290 */
4291 offline_css(css);
4292
4293 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004294 * If @cgrp is marked dead, it's waiting for refs of all css's to
4295 * be disabled before proceeding to the second phase of cgroup
4296 * destruction. If we are the last one, kick it off.
4297 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004298 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004299 cgroup_destroy_css_killed(cgrp);
4300
4301 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004302
4303 /*
4304 * Put the css refs from kill_css(). Each css holds an extra
4305 * reference to the cgroup's dentry and cgroup removal proceeds
4306 * regardless of css refs. On the last put of each css, whenever
4307 * that may be, the extra dentry ref is put so that dentry
4308 * destruction happens only after all css's are released.
4309 */
4310 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004311}
4312
Tejun Heo223dbc32013-08-13 20:22:50 -04004313/* css kill confirmation processing requires process context, bounce */
4314static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004315{
4316 struct cgroup_subsys_state *css =
4317 container_of(ref, struct cgroup_subsys_state, refcnt);
4318
Tejun Heo223dbc32013-08-13 20:22:50 -04004319 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004320 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004321}
4322
4323/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004324 * kill_css - destroy a css
4325 * @css: css to destroy
4326 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004327 * This function initiates destruction of @css by removing cgroup interface
4328 * files and putting its base reference. ->css_offline() will be invoked
4329 * asynchronously once css_tryget() is guaranteed to fail and when the
4330 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004331 */
4332static void kill_css(struct cgroup_subsys_state *css)
4333{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004334 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4335
Tejun Heoedae0c32013-08-13 20:22:51 -04004336 /*
4337 * Killing would put the base ref, but we need to keep it alive
4338 * until after ->css_offline().
4339 */
4340 css_get(css);
4341
4342 /*
4343 * cgroup core guarantees that, by the time ->css_offline() is
4344 * invoked, no new css reference will be given out via
4345 * css_tryget(). We can't simply call percpu_ref_kill() and
4346 * proceed to offlining css's because percpu_ref_kill() doesn't
4347 * guarantee that the ref is seen as killed on all CPUs on return.
4348 *
4349 * Use percpu_ref_kill_and_confirm() to get notifications as each
4350 * css is confirmed to be seen as killed on all CPUs.
4351 */
4352 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004353}
4354
4355/**
4356 * cgroup_destroy_locked - the first stage of cgroup destruction
4357 * @cgrp: cgroup to be destroyed
4358 *
4359 * css's make use of percpu refcnts whose killing latency shouldn't be
4360 * exposed to userland and are RCU protected. Also, cgroup core needs to
4361 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4362 * invoked. To satisfy all the requirements, destruction is implemented in
4363 * the following two steps.
4364 *
4365 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4366 * userland visible parts and start killing the percpu refcnts of
4367 * css's. Set up so that the next stage will be kicked off once all
4368 * the percpu refcnts are confirmed to be killed.
4369 *
4370 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4371 * rest of destruction. Once all cgroup references are gone, the
4372 * cgroup is RCU-freed.
4373 *
4374 * This function implements s1. After this step, @cgrp is gone as far as
4375 * the userland is concerned and a new cgroup with the same name may be
4376 * created. As cgroup doesn't care about the names internally, this
4377 * doesn't cause any problem.
4378 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004379static int cgroup_destroy_locked(struct cgroup *cgrp)
4380 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381{
Tejun Heo42809dd2012-11-19 08:13:37 -08004382 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004383 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004384 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004385 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004386 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387
Tejun Heo42809dd2012-11-19 08:13:37 -08004388 lockdep_assert_held(&d->d_inode->i_mutex);
4389 lockdep_assert_held(&cgroup_mutex);
4390
Tejun Heoddd69142013-06-12 21:04:54 -07004391 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004392 * css_set_lock synchronizes access to ->cset_links and prevents
4393 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004394 */
4395 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004396 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004397 read_unlock(&css_set_lock);
4398 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004400
Tejun Heo1a90dd52012-11-05 09:16:59 -08004401 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004402 * Make sure there's no live children. We can't test ->children
4403 * emptiness as dead children linger on it while being destroyed;
4404 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4405 */
4406 empty = true;
4407 rcu_read_lock();
4408 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4409 empty = cgroup_is_dead(child);
4410 if (!empty)
4411 break;
4412 }
4413 rcu_read_unlock();
4414 if (!empty)
4415 return -EBUSY;
4416
4417 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004418 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4419 * will be invoked to perform the rest of destruction once the
4420 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004421 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05004422 for_each_css(css, ssid, cgrp)
4423 kill_css(css);
Tejun Heo455050d2013-06-13 19:27:41 -07004424
4425 /*
4426 * Mark @cgrp dead. This prevents further task migration and child
4427 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004428 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004429 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004430 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004431 */
Tejun Heo54766d42013-06-12 21:04:53 -07004432 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004433
Tejun Heo455050d2013-06-13 19:27:41 -07004434 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4435 raw_spin_lock(&release_list_lock);
4436 if (!list_empty(&cgrp->release_list))
4437 list_del_init(&cgrp->release_list);
4438 raw_spin_unlock(&release_list_lock);
4439
4440 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004441 * If @cgrp has css's attached, the second stage of cgroup
4442 * destruction is kicked off from css_killed_work_fn() after the
4443 * refs of all attached css's are killed. If @cgrp doesn't have
4444 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004445 */
Tejun Heof20104d2013-08-13 20:22:50 -04004446 if (!cgrp->nr_css)
4447 cgroup_destroy_css_killed(cgrp);
4448
4449 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004450 * Clear the base files and remove @cgrp directory. The removal
4451 * puts the base ref but we aren't quite done with @cgrp yet, so
4452 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004453 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004454 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004455 dget(d);
4456 cgroup_d_remove_dir(d);
4457
Tejun Heoea15f8c2013-06-13 19:27:42 -07004458 return 0;
4459};
4460
Tejun Heod3daf282013-06-13 19:39:16 -07004461/**
Tejun Heof20104d2013-08-13 20:22:50 -04004462 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004463 * @work: cgroup->destroy_free_work
4464 *
4465 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004466 * destroyed after all css's are offlined and performs the rest of
4467 * destruction. This is the second step of destruction described in the
4468 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004469 */
Tejun Heof20104d2013-08-13 20:22:50 -04004470static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004471{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004472 struct cgroup *parent = cgrp->parent;
4473 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004474
Tejun Heof20104d2013-08-13 20:22:50 -04004475 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004476
Paul Menage999cd8a2009-01-07 18:08:36 -08004477 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004478 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004479
Li Zefan4e96ee82013-07-31 09:50:50 +08004480 /*
4481 * We should remove the cgroup object from idr before its grace
4482 * period starts, so we won't be looking up a cgroup while the
4483 * cgroup is being freed.
4484 */
4485 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4486 cgrp->id = -1;
4487
Paul Menageddbcc7e2007-10-18 23:39:30 -07004488 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004489
Paul Menagebd89aab2007-10-18 23:40:44 -07004490 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004491 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492}
4493
Tejun Heo42809dd2012-11-19 08:13:37 -08004494static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4495{
4496 int ret;
4497
4498 mutex_lock(&cgroup_mutex);
4499 ret = cgroup_destroy_locked(dentry->d_fsdata);
4500 mutex_unlock(&cgroup_mutex);
4501
4502 return ret;
4503}
4504
Tejun Heo8e3f6542012-04-01 12:09:55 -07004505static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4506{
4507 INIT_LIST_HEAD(&ss->cftsets);
4508
4509 /*
4510 * base_cftset is embedded in subsys itself, no need to worry about
4511 * deregistration.
4512 */
4513 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004514 struct cftype *cft;
4515
4516 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4517 cft->ss = ss;
4518
Tejun Heo8e3f6542012-04-01 12:09:55 -07004519 ss->base_cftset.cfts = ss->base_cftypes;
4520 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4521 }
4522}
4523
Li Zefan06a11922008-04-29 01:00:07 -07004524static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004525{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004526 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004527
4528 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004529
Tejun Heo648bb562012-11-19 08:13:36 -08004530 mutex_lock(&cgroup_mutex);
4531
Tejun Heo8e3f6542012-04-01 12:09:55 -07004532 /* init base cftset */
4533 cgroup_init_cftsets(ss);
4534
Paul Menageddbcc7e2007-10-18 23:39:30 -07004535 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004536 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004537 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004538 /* We don't handle early failures gracefully */
4539 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004540 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004541
Li Zefane8d55fd2008-04-29 01:00:13 -07004542 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004543 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004544 * newly registered, all tasks and hence the
4545 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004546 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004547
4548 need_forkexit_callback |= ss->fork || ss->exit;
4549
Li Zefane8d55fd2008-04-29 01:00:13 -07004550 /* At system boot, before all subsystems have been
4551 * registered, no tasks have been forked, so we don't
4552 * need to invoke fork callbacks here. */
4553 BUG_ON(!list_empty(&init_task.tasks));
4554
Tejun Heoae7f1642013-08-13 20:22:50 -04004555 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004556
Tejun Heo648bb562012-11-19 08:13:36 -08004557 mutex_unlock(&cgroup_mutex);
4558
Ben Blume6a11052010-03-10 15:22:09 -08004559 /* this function shouldn't be used with modular subsystems, since they
4560 * need to register a subsys_id, among other things */
4561 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562}
4563
4564/**
Ben Blume6a11052010-03-10 15:22:09 -08004565 * cgroup_load_subsys: load and register a modular subsystem at runtime
4566 * @ss: the subsystem to load
4567 *
4568 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004569 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004570 * up for use. If the subsystem is built-in anyway, work is delegated to the
4571 * simpler cgroup_init_subsys.
4572 */
4573int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4574{
Ben Blume6a11052010-03-10 15:22:09 -08004575 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004576 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004577 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004578 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004579 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004580
4581 /* check name and function validity */
4582 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004583 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004584 return -EINVAL;
4585
4586 /*
4587 * we don't support callbacks in modular subsystems. this check is
4588 * before the ss->module check for consistency; a subsystem that could
4589 * be a module should still have no callbacks even if the user isn't
4590 * compiling it as one.
4591 */
4592 if (ss->fork || ss->exit)
4593 return -EINVAL;
4594
4595 /*
4596 * an optionally modular subsystem is built-in: we want to do nothing,
4597 * since cgroup_init_subsys will have already taken care of it.
4598 */
4599 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004600 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004601 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004602 return 0;
4603 }
4604
Tejun Heo8e3f6542012-04-01 12:09:55 -07004605 /* init base cftset */
4606 cgroup_init_cftsets(ss);
4607
Ben Blume6a11052010-03-10 15:22:09 -08004608 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004609 mutex_lock(&cgroup_root_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004610 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004611
4612 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004613 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004614 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004615 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004616 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004617 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004618 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004619 /* failure case - need to deassign the cgroup_subsys[] slot. */
4620 cgroup_subsys[ss->subsys_id] = NULL;
Wei Yongjun0be86692013-12-09 20:38:29 +08004621 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004622 mutex_unlock(&cgroup_mutex);
4623 return PTR_ERR(css);
4624 }
4625
Tejun Heo9871bf92013-06-24 15:21:47 -07004626 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004627
4628 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004629 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004630
4631 /*
4632 * Now we need to entangle the css into the existing css_sets. unlike
4633 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4634 * will need a new pointer to it; done by iterating the css_set_table.
4635 * furthermore, modifying the existing css_sets will corrupt the hash
4636 * table state, so each changed css_set will need its hash recomputed.
4637 * this is all done under the css_set_lock.
4638 */
4639 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004640 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004641 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004642 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004643 continue;
4644 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004645 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004646 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004647 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004648 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004649 key = css_set_hash(cset->subsys);
4650 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004651 }
4652 write_unlock(&css_set_lock);
4653
Tejun Heoae7f1642013-08-13 20:22:50 -04004654 ret = online_css(css);
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004655 if (ret) {
4656 ss->css_free(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004657 goto err_unload;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004658 }
Tejun Heoa8638032012-11-09 09:12:29 -08004659
Ben Blume6a11052010-03-10 15:22:09 -08004660 /* success! */
Tejun Heo780cd8b2013-12-06 15:11:56 -05004661 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004662 mutex_unlock(&cgroup_mutex);
4663 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004664
4665err_unload:
Tejun Heo780cd8b2013-12-06 15:11:56 -05004666 mutex_unlock(&cgroup_root_mutex);
Tejun Heod19e19d2012-11-19 08:13:37 -08004667 mutex_unlock(&cgroup_mutex);
4668 /* @ss can't be mounted here as try_module_get() would fail */
4669 cgroup_unload_subsys(ss);
4670 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004671}
4672EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4673
4674/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004675 * cgroup_unload_subsys: unload a modular subsystem
4676 * @ss: the subsystem to unload
4677 *
4678 * This function should be called in a modular subsystem's exitcall. When this
4679 * function is invoked, the refcount on the subsystem's module will be 0, so
4680 * the subsystem will not be attached to any hierarchy.
4681 */
4682void cgroup_unload_subsys(struct cgroup_subsys *ss)
4683{
Tejun Heo69d02062013-06-12 21:04:50 -07004684 struct cgrp_cset_link *link;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004685 struct cgroup_subsys_state *css;
Ben Blumcf5d5942010-03-10 15:22:09 -08004686
4687 BUG_ON(ss->module == NULL);
4688
4689 /*
4690 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004691 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004692 * doesn't start being used while we're killing it off.
4693 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004694 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004695
4696 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004697 mutex_lock(&cgroup_root_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004698
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004699 css = cgroup_css(cgroup_dummy_top, ss);
4700 if (css)
4701 offline_css(css);
Tejun Heo02ae7482012-11-19 08:13:37 -08004702
Ben Blumcf5d5942010-03-10 15:22:09 -08004703 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004704 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004705
Ben Blumcf5d5942010-03-10 15:22:09 -08004706 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004707 * disentangle the css from all css_sets attached to the dummy
4708 * top. as in loading, we need to pay our respects to the hashtable
4709 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004710 */
4711 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004712 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004713 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004714 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004715
Tejun Heo5abb8852013-06-12 21:04:49 -07004716 hash_del(&cset->hlist);
4717 cset->subsys[ss->subsys_id] = NULL;
4718 key = css_set_hash(cset->subsys);
4719 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004720 }
4721 write_unlock(&css_set_lock);
4722
4723 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004724 * remove subsystem's css from the cgroup_dummy_top and free it -
4725 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004726 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004727 */
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004728 if (css)
4729 ss->css_free(css);
Tejun Heo73e80ed2013-08-13 11:01:55 -04004730 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004731
Tejun Heo780cd8b2013-12-06 15:11:56 -05004732 mutex_unlock(&cgroup_root_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08004733 mutex_unlock(&cgroup_mutex);
4734}
4735EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4736
4737/**
Li Zefana043e3b2008-02-23 15:24:09 -08004738 * cgroup_init_early - cgroup initialization at system boot
4739 *
4740 * Initialize cgroups at system boot, and initialize any
4741 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742 */
4743int __init cgroup_init_early(void)
4744{
Tejun Heo30159ec2013-06-25 11:53:37 -07004745 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004746 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004747
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004748 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004749 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004750 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004751 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004752 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004753 init_cgroup_root(&cgroup_dummy_root);
4754 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004755 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004756
Tejun Heo69d02062013-06-12 21:04:50 -07004757 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004758 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4759 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004760 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004761
Tejun Heo30159ec2013-06-25 11:53:37 -07004762 /* at bootup time, we don't worry about modular subsystems */
4763 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004764 BUG_ON(!ss->name);
4765 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004766 BUG_ON(!ss->css_alloc);
4767 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004768 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004769 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004770 ss->name, ss->subsys_id);
4771 BUG();
4772 }
4773
4774 if (ss->early_init)
4775 cgroup_init_subsys(ss);
4776 }
4777 return 0;
4778}
4779
4780/**
Li Zefana043e3b2008-02-23 15:24:09 -08004781 * cgroup_init - cgroup initialization
4782 *
4783 * Register cgroup filesystem and /proc file, and initialize
4784 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004785 */
4786int __init cgroup_init(void)
4787{
Tejun Heo30159ec2013-06-25 11:53:37 -07004788 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004789 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004790 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004791
4792 err = bdi_init(&cgroup_backing_dev_info);
4793 if (err)
4794 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004795
Tejun Heo30159ec2013-06-25 11:53:37 -07004796 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004797 if (!ss->early_init)
4798 cgroup_init_subsys(ss);
4799 }
4800
Tejun Heofa3ca072013-04-14 11:36:56 -07004801 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004802 mutex_lock(&cgroup_mutex);
4803 mutex_lock(&cgroup_root_mutex);
4804
Tejun Heo82fe9b02013-06-25 11:53:37 -07004805 /* Add init_css_set to the hash table */
4806 key = css_set_hash(init_css_set.subsys);
4807 hash_add(css_set_table, &init_css_set.hlist, key);
4808
Tejun Heofc76df72013-06-25 11:53:37 -07004809 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004810
Li Zefan4e96ee82013-07-31 09:50:50 +08004811 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4812 0, 1, GFP_KERNEL);
4813 BUG_ON(err < 0);
4814
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004815 mutex_unlock(&cgroup_root_mutex);
4816 mutex_unlock(&cgroup_mutex);
4817
Greg KH676db4a2010-08-05 13:53:35 -07004818 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4819 if (!cgroup_kobj) {
4820 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004821 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004822 }
4823
4824 err = register_filesystem(&cgroup_fs_type);
4825 if (err < 0) {
4826 kobject_put(cgroup_kobj);
4827 goto out;
4828 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004829
Li Zefan46ae2202008-04-29 01:00:08 -07004830 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004831
Paul Menageddbcc7e2007-10-18 23:39:30 -07004832out:
Paul Menagea4243162007-10-18 23:39:35 -07004833 if (err)
4834 bdi_destroy(&cgroup_backing_dev_info);
4835
Paul Menageddbcc7e2007-10-18 23:39:30 -07004836 return err;
4837}
Paul Menageb4f48b62007-10-18 23:39:33 -07004838
Tejun Heoe5fca242013-11-22 17:14:39 -05004839static int __init cgroup_wq_init(void)
4840{
4841 /*
4842 * There isn't much point in executing destruction path in
4843 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4844 * Use 1 for @max_active.
4845 *
4846 * We would prefer to do this in cgroup_init() above, but that
4847 * is called before init_workqueues(): so leave this until after.
4848 */
4849 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4850 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004851
4852 /*
4853 * Used to destroy pidlists and separate to serve as flush domain.
4854 * Cap @max_active to 1 too.
4855 */
4856 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4857 0, 1);
4858 BUG_ON(!cgroup_pidlist_destroy_wq);
4859
Tejun Heoe5fca242013-11-22 17:14:39 -05004860 return 0;
4861}
4862core_initcall(cgroup_wq_init);
4863
Paul Menagea4243162007-10-18 23:39:35 -07004864/*
4865 * proc_cgroup_show()
4866 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4867 * - Used for /proc/<pid>/cgroup.
4868 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4869 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004870 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004871 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4872 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4873 * cgroup to top_cgroup.
4874 */
4875
4876/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004877int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004878{
4879 struct pid *pid;
4880 struct task_struct *tsk;
4881 char *buf;
4882 int retval;
4883 struct cgroupfs_root *root;
4884
4885 retval = -ENOMEM;
4886 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4887 if (!buf)
4888 goto out;
4889
4890 retval = -ESRCH;
4891 pid = m->private;
4892 tsk = get_pid_task(pid, PIDTYPE_PID);
4893 if (!tsk)
4894 goto out_free;
4895
4896 retval = 0;
4897
4898 mutex_lock(&cgroup_mutex);
4899
Li Zefane5f6a862009-01-07 18:07:41 -08004900 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004901 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004902 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004903 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004904
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004905 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004906 for_each_subsys(ss, ssid)
4907 if (root->subsys_mask & (1 << ssid))
4908 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004909 if (strlen(root->name))
4910 seq_printf(m, "%sname=%s", count ? "," : "",
4911 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004912 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004913 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004914 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004915 if (retval < 0)
4916 goto out_unlock;
4917 seq_puts(m, buf);
4918 seq_putc(m, '\n');
4919 }
4920
4921out_unlock:
4922 mutex_unlock(&cgroup_mutex);
4923 put_task_struct(tsk);
4924out_free:
4925 kfree(buf);
4926out:
4927 return retval;
4928}
4929
Paul Menagea4243162007-10-18 23:39:35 -07004930/* Display information about each subsystem and each hierarchy */
4931static int proc_cgroupstats_show(struct seq_file *m, void *v)
4932{
Tejun Heo30159ec2013-06-25 11:53:37 -07004933 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004934 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004935
Paul Menage8bab8dd2008-04-04 14:29:57 -07004936 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004937 /*
4938 * ideally we don't want subsystems moving around while we do this.
4939 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4940 * subsys/hierarchy state.
4941 */
Paul Menagea4243162007-10-18 23:39:35 -07004942 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004943
4944 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004945 seq_printf(m, "%s\t%d\t%d\t%d\n",
4946 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004947 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004948
Paul Menagea4243162007-10-18 23:39:35 -07004949 mutex_unlock(&cgroup_mutex);
4950 return 0;
4951}
4952
4953static int cgroupstats_open(struct inode *inode, struct file *file)
4954{
Al Viro9dce07f2008-03-29 03:07:28 +00004955 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004956}
4957
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004958static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004959 .open = cgroupstats_open,
4960 .read = seq_read,
4961 .llseek = seq_lseek,
4962 .release = single_release,
4963};
4964
Paul Menageb4f48b62007-10-18 23:39:33 -07004965/**
4966 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004967 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004968 *
4969 * Description: A task inherits its parent's cgroup at fork().
4970 *
4971 * A pointer to the shared css_set was automatically copied in
4972 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004973 * it was not made under the protection of RCU or cgroup_mutex, so
4974 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4975 * have already changed current->cgroups, allowing the previously
4976 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004977 *
4978 * At the point that cgroup_fork() is called, 'current' is the parent
4979 * task, and the passed argument 'child' points to the child task.
4980 */
4981void cgroup_fork(struct task_struct *child)
4982{
Tejun Heo9bb71302012-10-18 17:52:07 -07004983 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004984 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004985 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004986 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004987 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004988}
4989
4990/**
Li Zefana043e3b2008-02-23 15:24:09 -08004991 * cgroup_post_fork - called on a new task after adding it to the task list
4992 * @child: the task in question
4993 *
Tejun Heo5edee612012-10-16 15:03:14 -07004994 * Adds the task to the list running through its css_set if necessary and
4995 * call the subsystem fork() callbacks. Has to be after the task is
4996 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004997 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004998 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004999 */
Paul Menage817929e2007-10-18 23:39:36 -07005000void cgroup_post_fork(struct task_struct *child)
5001{
Tejun Heo30159ec2013-06-25 11:53:37 -07005002 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005003 int i;
5004
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005005 /*
5006 * use_task_css_set_links is set to 1 before we walk the tasklist
5007 * under the tasklist_lock and we read it here after we added the child
5008 * to the tasklist under the tasklist_lock as well. If the child wasn't
5009 * yet in the tasklist when we walked through it from
5010 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5011 * should be visible now due to the paired locking and barriers implied
5012 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5013 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5014 * lock on fork.
5015 */
Paul Menage817929e2007-10-18 23:39:36 -07005016 if (use_task_css_set_links) {
5017 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005018 task_lock(child);
5019 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005020 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005021 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005022 write_unlock(&css_set_lock);
5023 }
Tejun Heo5edee612012-10-16 15:03:14 -07005024
5025 /*
5026 * Call ss->fork(). This must happen after @child is linked on
5027 * css_set; otherwise, @child might change state between ->fork()
5028 * and addition to css_set.
5029 */
5030 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005031 /*
5032 * fork/exit callbacks are supported only for builtin
5033 * subsystems, and the builtin section of the subsys
5034 * array is immutable, so we don't need to lock the
5035 * subsys array here. On the other hand, modular section
5036 * of the array can be freed at module unload, so we
5037 * can't touch that.
5038 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005039 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005040 if (ss->fork)
5041 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005042 }
Paul Menage817929e2007-10-18 23:39:36 -07005043}
Tejun Heo5edee612012-10-16 15:03:14 -07005044
Paul Menage817929e2007-10-18 23:39:36 -07005045/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005046 * cgroup_exit - detach cgroup from exiting task
5047 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005048 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005049 *
5050 * Description: Detach cgroup from @tsk and release it.
5051 *
5052 * Note that cgroups marked notify_on_release force every task in
5053 * them to take the global cgroup_mutex mutex when exiting.
5054 * This could impact scaling on very large systems. Be reluctant to
5055 * use notify_on_release cgroups where very high task exit scaling
5056 * is required on large systems.
5057 *
5058 * the_top_cgroup_hack:
5059 *
5060 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5061 *
5062 * We call cgroup_exit() while the task is still competent to
5063 * handle notify_on_release(), then leave the task attached to the
5064 * root cgroup in each hierarchy for the remainder of its exit.
5065 *
5066 * To do this properly, we would increment the reference count on
5067 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5068 * code we would add a second cgroup function call, to drop that
5069 * reference. This would just create an unnecessary hot spot on
5070 * the top_cgroup reference count, to no avail.
5071 *
5072 * Normally, holding a reference to a cgroup without bumping its
5073 * count is unsafe. The cgroup could go away, or someone could
5074 * attach us to a different cgroup, decrementing the count on
5075 * the first cgroup that we never incremented. But in this case,
5076 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005077 * which wards off any cgroup_attach_task() attempts, or task is a failed
5078 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005079 */
5080void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5081{
Tejun Heo30159ec2013-06-25 11:53:37 -07005082 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005083 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005084 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005085
5086 /*
5087 * Unlink from the css_set task list if necessary.
5088 * Optimistically check cg_list before taking
5089 * css_set_lock
5090 */
5091 if (!list_empty(&tsk->cg_list)) {
5092 write_lock(&css_set_lock);
5093 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005094 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005095 write_unlock(&css_set_lock);
5096 }
5097
Paul Menageb4f48b62007-10-18 23:39:33 -07005098 /* Reassign the task to the init_css_set. */
5099 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005100 cset = task_css_set(tsk);
5101 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005102
5103 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005104 /*
5105 * fork/exit callbacks are supported only for builtin
5106 * subsystems, see cgroup_post_fork() for details.
5107 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005108 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005109 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005110 struct cgroup_subsys_state *old_css = cset->subsys[i];
5111 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005112
Tejun Heoeb954192013-08-08 20:11:23 -04005113 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005114 }
5115 }
5116 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005117 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005118
Tejun Heo5abb8852013-06-12 21:04:49 -07005119 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005120}
Paul Menage697f4162007-10-18 23:39:34 -07005121
Paul Menagebd89aab2007-10-18 23:40:44 -07005122static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005123{
Li Zefanf50daa72013-03-01 15:06:07 +08005124 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005125 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005126 /*
5127 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005128 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005129 * it now
5130 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005132
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005133 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005134 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005135 list_empty(&cgrp->release_list)) {
5136 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005137 need_schedule_work = 1;
5138 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005139 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005140 if (need_schedule_work)
5141 schedule_work(&release_agent_work);
5142 }
5143}
5144
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145/*
5146 * Notify userspace when a cgroup is released, by running the
5147 * configured release agent with the name of the cgroup (path
5148 * relative to the root of cgroup file system) as the argument.
5149 *
5150 * Most likely, this user command will try to rmdir this cgroup.
5151 *
5152 * This races with the possibility that some other task will be
5153 * attached to this cgroup before it is removed, or that some other
5154 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5155 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5156 * unused, and this cgroup will be reprieved from its death sentence,
5157 * to continue to serve a useful existence. Next time it's released,
5158 * we will get notified again, if it still has 'notify_on_release' set.
5159 *
5160 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5161 * means only wait until the task is successfully execve()'d. The
5162 * separate release agent task is forked by call_usermodehelper(),
5163 * then control in this thread returns here, without waiting for the
5164 * release agent task. We don't bother to wait because the caller of
5165 * this routine has no use for the exit status of the release agent
5166 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005167 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005168static void cgroup_release_agent(struct work_struct *work)
5169{
5170 BUG_ON(work != &release_agent_work);
5171 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005172 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005173 while (!list_empty(&release_list)) {
5174 char *argv[3], *envp[3];
5175 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005176 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005177 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005178 struct cgroup,
5179 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005180 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005181 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005182 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005183 if (!pathbuf)
5184 goto continue_free;
5185 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5186 goto continue_free;
5187 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5188 if (!agentbuf)
5189 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005190
5191 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005192 argv[i++] = agentbuf;
5193 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005194 argv[i] = NULL;
5195
5196 i = 0;
5197 /* minimal command environment */
5198 envp[i++] = "HOME=/";
5199 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5200 envp[i] = NULL;
5201
5202 /* Drop the lock while we invoke the usermode helper,
5203 * since the exec could involve hitting disk and hence
5204 * be a slow process */
5205 mutex_unlock(&cgroup_mutex);
5206 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005207 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005208 continue_free:
5209 kfree(pathbuf);
5210 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005211 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005212 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005213 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005214 mutex_unlock(&cgroup_mutex);
5215}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005216
5217static int __init cgroup_disable(char *str)
5218{
Tejun Heo30159ec2013-06-25 11:53:37 -07005219 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005220 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005221 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005222
5223 while ((token = strsep(&str, ",")) != NULL) {
5224 if (!*token)
5225 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005226
Tejun Heo30159ec2013-06-25 11:53:37 -07005227 /*
5228 * cgroup_disable, being at boot time, can't know about
5229 * module subsystems, so we don't worry about them.
5230 */
5231 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005232 if (!strcmp(token, ss->name)) {
5233 ss->disabled = 1;
5234 printk(KERN_INFO "Disabling %s control group"
5235 " subsystem\n", ss->name);
5236 break;
5237 }
5238 }
5239 }
5240 return 1;
5241}
5242__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243
Tejun Heob77d7b62013-08-13 11:01:54 -04005244/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005245 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5246 * @dentry: directory dentry of interest
5247 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005248 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05005249 * Must be called under cgroup_mutex or RCU read lock. The caller is
5250 * responsible for pinning the returned css if it needs to be accessed
5251 * outside the critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005252 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005253struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5254 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005255{
5256 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005257
Tejun Heo87fb54f2013-12-06 15:11:55 -05005258 cgroup_assert_mutex_or_rcu_locked();
Tejun Heob77d7b62013-08-13 11:01:54 -04005259
Tejun Heo35cf0832013-08-26 18:40:56 -04005260 /* is @dentry a cgroup dir? */
5261 if (!dentry->d_inode ||
5262 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005263 return ERR_PTR(-EBADF);
5264
Tejun Heo35cf0832013-08-26 18:40:56 -04005265 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005266 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005267}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005268
Li Zefan1cb650b2013-08-19 10:05:24 +08005269/**
5270 * css_from_id - lookup css by id
5271 * @id: the cgroup id
5272 * @ss: cgroup subsys to be looked into
5273 *
5274 * Returns the css if there's valid one with @id, otherwise returns NULL.
5275 * Should be called under rcu_read_lock().
5276 */
5277struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5278{
5279 struct cgroup *cgrp;
5280
Tejun Heo87fb54f2013-12-06 15:11:55 -05005281 cgroup_assert_mutex_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005282
5283 cgrp = idr_find(&ss->root->cgroup_idr, id);
5284 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005285 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005286 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005287}
5288
Paul Menagefe693432009-09-23 15:56:20 -07005289#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005290static struct cgroup_subsys_state *
5291debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005292{
5293 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5294
5295 if (!css)
5296 return ERR_PTR(-ENOMEM);
5297
5298 return css;
5299}
5300
Tejun Heoeb954192013-08-08 20:11:23 -04005301static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005302{
Tejun Heoeb954192013-08-08 20:11:23 -04005303 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005304}
5305
Tejun Heo182446d2013-08-08 20:11:24 -04005306static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5307 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005308{
Tejun Heo182446d2013-08-08 20:11:24 -04005309 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005310}
5311
Tejun Heo182446d2013-08-08 20:11:24 -04005312static u64 current_css_set_read(struct cgroup_subsys_state *css,
5313 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005314{
5315 return (u64)(unsigned long)current->cgroups;
5316}
5317
Tejun Heo182446d2013-08-08 20:11:24 -04005318static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005319 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005320{
5321 u64 count;
5322
5323 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005324 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005325 rcu_read_unlock();
5326 return count;
5327}
5328
Tejun Heo2da8ca82013-12-05 12:28:04 -05005329static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005330{
Tejun Heo69d02062013-06-12 21:04:50 -07005331 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005332 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005333
5334 read_lock(&css_set_lock);
5335 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005336 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005337 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005338 struct cgroup *c = link->cgrp;
5339 const char *name;
5340
5341 if (c->dentry)
5342 name = c->dentry->d_name.name;
5343 else
5344 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005345 seq_printf(seq, "Root %d group %s\n",
5346 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005347 }
5348 rcu_read_unlock();
5349 read_unlock(&css_set_lock);
5350 return 0;
5351}
5352
5353#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005354static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005355{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005356 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005357 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005358
5359 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005360 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005361 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005362 struct task_struct *task;
5363 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005364 seq_printf(seq, "css_set %p\n", cset);
5365 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005366 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5367 seq_puts(seq, " ...\n");
5368 break;
5369 } else {
5370 seq_printf(seq, " task %d\n",
5371 task_pid_vnr(task));
5372 }
5373 }
5374 }
5375 read_unlock(&css_set_lock);
5376 return 0;
5377}
5378
Tejun Heo182446d2013-08-08 20:11:24 -04005379static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005380{
Tejun Heo182446d2013-08-08 20:11:24 -04005381 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005382}
5383
5384static struct cftype debug_files[] = {
5385 {
Paul Menagefe693432009-09-23 15:56:20 -07005386 .name = "taskcount",
5387 .read_u64 = debug_taskcount_read,
5388 },
5389
5390 {
5391 .name = "current_css_set",
5392 .read_u64 = current_css_set_read,
5393 },
5394
5395 {
5396 .name = "current_css_set_refcount",
5397 .read_u64 = current_css_set_refcount_read,
5398 },
5399
5400 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005401 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005402 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005403 },
5404
5405 {
5406 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005407 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005408 },
5409
5410 {
Paul Menagefe693432009-09-23 15:56:20 -07005411 .name = "releasable",
5412 .read_u64 = releasable_read,
5413 },
Paul Menagefe693432009-09-23 15:56:20 -07005414
Tejun Heo4baf6e32012-04-01 12:09:55 -07005415 { } /* terminate */
5416};
Paul Menagefe693432009-09-23 15:56:20 -07005417
5418struct cgroup_subsys debug_subsys = {
5419 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005420 .css_alloc = debug_css_alloc,
5421 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005422 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005423 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005424};
5425#endif /* CONFIG_CGROUP_DEBUG */