blob: 680e2be234c418b5882dfd2ac5d6f16eba5f25c0 [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 Gleixnercdcc136f2009-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-10-27 15:33:37 -07001152 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001153 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-10-27 15:33:37 -07001196
1197 continue;
1198 }
1199
Tejun Heo30159ec2013-06-25 11:53:37 -07001200 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Lezcano32a8cf232010-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 Zefan4e96ee8e2013-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 Zefan4e96ee8e2013-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 Zefan4e96ee8e2013-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
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001672static void cgroup_kill_sb(struct super_block *sb)
1673{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001675 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001676 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 int ret;
1678
1679 BUG_ON(!root);
1680
1681 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001682 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683
Tejun Heo31261212013-06-28 17:07:30 -07001684 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001686 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687
1688 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001689 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1690 ret = rebind_subsystems(root, 0, root->subsys_mask);
1691 /* Shouldn't be able to fail ... */
1692 BUG_ON(ret);
1693 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694
Paul Menage817929e2007-10-18 23:39:36 -07001695 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001696 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001697 * root cgroup
1698 */
1699 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001700
Tejun Heo69d02062013-06-12 21:04:50 -07001701 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1702 list_del(&link->cset_link);
1703 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001704 kfree(link);
1705 }
1706 write_unlock(&css_set_lock);
1707
Paul Menage839ec542009-01-29 14:25:22 -08001708 if (!list_empty(&root->root_list)) {
1709 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001710 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001711 }
Li Zefane5f6a862009-01-07 18:07:41 -08001712
Tejun Heofa3ca072013-04-14 11:36:56 -07001713 cgroup_exit_root_id(root);
1714
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001715 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001717 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001719 simple_xattrs_free(&cgrp->xattrs);
1720
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001722 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723}
1724
1725static struct file_system_type cgroup_fs_type = {
1726 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001727 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 .kill_sb = cgroup_kill_sb,
1729};
1730
Greg KH676db4a2010-08-05 13:53:35 -07001731static struct kobject *cgroup_kobj;
1732
Li Zefana043e3b2008-02-23 15:24:09 -08001733/**
1734 * cgroup_path - generate the path of a cgroup
1735 * @cgrp: the cgroup in question
1736 * @buf: the buffer to write the path into
1737 * @buflen: the length of the buffer
1738 *
Li Zefan65dff752013-03-01 15:01:56 +08001739 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1740 *
1741 * We can't generate cgroup path using dentry->d_name, as accessing
1742 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1743 * inode's i_mutex, while on the other hand cgroup_path() can be called
1744 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001746int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747{
Li Zefan65dff752013-03-01 15:01:56 +08001748 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001750
Tejun Heoda1f2962013-04-14 10:32:19 -07001751 if (!cgrp->parent) {
1752 if (strlcpy(buf, "/", buflen) >= buflen)
1753 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 return 0;
1755 }
1756
Tao Ma316eb662012-11-08 21:36:38 +08001757 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001758 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001759
Li Zefan65dff752013-03-01 15:01:56 +08001760 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001761 do {
Li Zefan65dff752013-03-01 15:01:56 +08001762 const char *name = cgroup_name(cgrp);
1763 int len;
1764
1765 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001767 goto out;
1768 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001769
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001771 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001773
1774 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001775 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001776 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001778out:
1779 rcu_read_unlock();
1780 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781}
Ben Blum67523c42010-03-10 15:22:11 -08001782EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783
Tejun Heo857a2be2013-04-14 20:50:08 -07001784/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001785 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001786 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001787 * @buf: the buffer to write the path into
1788 * @buflen: the length of the buffer
1789 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001790 * Determine @task's cgroup on the first (the one with the lowest non-zero
1791 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1792 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1793 * cgroup controller callbacks.
1794 *
1795 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001796 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001798{
1799 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001800 struct cgroup *cgrp;
1801 int hierarchy_id = 1, ret = 0;
1802
1803 if (buflen < 2)
1804 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001805
1806 mutex_lock(&cgroup_mutex);
1807
Tejun Heo913ffdb2013-07-11 16:34:48 -07001808 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1809
Tejun Heo857a2be2013-04-14 20:50:08 -07001810 if (root) {
1811 cgrp = task_cgroup_from_root(task, root);
1812 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001813 } else {
1814 /* if no hierarchy exists, everyone is in "/" */
1815 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001816 }
1817
1818 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001819 return ret;
1820}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001821EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001822
Ben Blum74a11662011-05-26 16:25:20 -07001823/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001824 * Control Group taskset
1825 */
Tejun Heo134d3372011-12-12 18:12:21 -08001826struct task_and_cgroup {
1827 struct task_struct *task;
1828 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001829 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001830};
1831
Tejun Heo2f7ee562011-12-12 18:12:21 -08001832struct cgroup_taskset {
1833 struct task_and_cgroup single;
1834 struct flex_array *tc_array;
1835 int tc_array_len;
1836 int idx;
1837 struct cgroup *cur_cgrp;
1838};
1839
1840/**
1841 * cgroup_taskset_first - reset taskset and return the first task
1842 * @tset: taskset of interest
1843 *
1844 * @tset iteration is initialized and the first task is returned.
1845 */
1846struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1847{
1848 if (tset->tc_array) {
1849 tset->idx = 0;
1850 return cgroup_taskset_next(tset);
1851 } else {
1852 tset->cur_cgrp = tset->single.cgrp;
1853 return tset->single.task;
1854 }
1855}
1856EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1857
1858/**
1859 * cgroup_taskset_next - iterate to the next task in taskset
1860 * @tset: taskset of interest
1861 *
1862 * Return the next task in @tset. Iteration must have been initialized
1863 * with cgroup_taskset_first().
1864 */
1865struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1866{
1867 struct task_and_cgroup *tc;
1868
1869 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1870 return NULL;
1871
1872 tc = flex_array_get(tset->tc_array, tset->idx++);
1873 tset->cur_cgrp = tc->cgrp;
1874 return tc->task;
1875}
1876EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1877
1878/**
Tejun Heod99c8722013-08-08 20:11:27 -04001879 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001880 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001881 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001882 *
Tejun Heod99c8722013-08-08 20:11:27 -04001883 * Return the css for the current (last returned) task of @tset for
1884 * subsystem specified by @subsys_id. This function must be preceded by
1885 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886 */
Tejun Heod99c8722013-08-08 20:11:27 -04001887struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1888 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001889{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001890 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001891}
Tejun Heod99c8722013-08-08 20:11:27 -04001892EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001893
1894/**
1895 * cgroup_taskset_size - return the number of tasks in taskset
1896 * @tset: taskset of interest
1897 */
1898int cgroup_taskset_size(struct cgroup_taskset *tset)
1899{
1900 return tset->tc_array ? tset->tc_array_len : 1;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1903
1904
Ben Blum74a11662011-05-26 16:25:20 -07001905/*
1906 * cgroup_task_migrate - move a task from one cgroup to another.
1907 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001908 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001909 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001910static void cgroup_task_migrate(struct cgroup *old_cgrp,
1911 struct task_struct *tsk,
1912 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001913{
Tejun Heo5abb8852013-06-12 21:04:49 -07001914 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001915
1916 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001917 * We are synchronized through threadgroup_lock() against PF_EXITING
1918 * setting such that we can't race against cgroup_exit() changing the
1919 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001920 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001921 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001922 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001923
Ben Blum74a11662011-05-26 16:25:20 -07001924 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001925 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001926 task_unlock(tsk);
1927
1928 /* Update the css_set linked lists if we're using them */
1929 write_lock(&css_set_lock);
1930 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001931 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001932 write_unlock(&css_set_lock);
1933
1934 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001935 * We just gained a reference on old_cset by taking it from the
1936 * task. As trading it for new_cset is protected by cgroup_mutex,
1937 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001938 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001939 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1940 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001941}
1942
Li Zefana043e3b2008-02-23 15:24:09 -08001943/**
Li Zefan081aa452013-03-13 09:17:09 +08001944 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001945 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001946 * @tsk: the task or the leader of the threadgroup to be attached
1947 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001948 *
Tejun Heo257058a2011-12-12 18:12:21 -08001949 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001950 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001951 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001952static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1953 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001954{
1955 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001956 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001957 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001958 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001959 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001960 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001961 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001962 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001963
1964 /*
1965 * step 0: in order to do expensive, possibly blocking operations for
1966 * every thread, we cannot iterate the thread group list, since it needs
1967 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001968 * group - group_rwsem prevents new threads from appearing, and if
1969 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001970 */
Li Zefan081aa452013-03-13 09:17:09 +08001971 if (threadgroup)
1972 group_size = get_nr_threads(tsk);
1973 else
1974 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001975 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001976 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001977 if (!group)
1978 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001979 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001980 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001981 if (retval)
1982 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001983
Ben Blum74a11662011-05-26 16:25:20 -07001984 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001985 /*
1986 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1987 * already PF_EXITING could be freed from underneath us unless we
1988 * take an rcu_read_lock.
1989 */
1990 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001991 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001992 struct task_and_cgroup ent;
1993
Tejun Heocd3d0952011-12-12 18:12:21 -08001994 /* @tsk either already exited or can't exit until the end */
1995 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001996 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001997
Ben Blum74a11662011-05-26 16:25:20 -07001998 /* as per above, nr_threads may decrease, but not increase. */
1999 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002000 ent.task = tsk;
2001 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002002 /* nothing to do if this task is already in the cgroup */
2003 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002004 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002005 /*
2006 * saying GFP_ATOMIC has no effect here because we did prealloc
2007 * earlier, but it's good form to communicate our expectations.
2008 */
Tejun Heo134d3372011-12-12 18:12:21 -08002009 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002010 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002011 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002012 next:
Li Zefan081aa452013-03-13 09:17:09 +08002013 if (!threadgroup)
2014 break;
Ben Blum74a11662011-05-26 16:25:20 -07002015 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002016 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002017 /* remember the number of threads in the array for later. */
2018 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002019 tset.tc_array = group;
2020 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002021
Tejun Heo134d3372011-12-12 18:12:21 -08002022 /* methods shouldn't be called if no task is actually migrating */
2023 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002024 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002025 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002026
Ben Blum74a11662011-05-26 16:25:20 -07002027 /*
2028 * step 1: check that we can legitimately attach to the cgroup.
2029 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002030 for_each_css(css, i, cgrp) {
2031 if (css->ss->can_attach) {
2032 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002033 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002034 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002035 goto out_cancel_attach;
2036 }
2037 }
Ben Blum74a11662011-05-26 16:25:20 -07002038 }
2039
2040 /*
2041 * step 2: make sure css_sets exist for all threads to be migrated.
2042 * we use find_css_set, which allocates a new one if necessary.
2043 */
Ben Blum74a11662011-05-26 16:25:20 -07002044 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002045 struct css_set *old_cset;
2046
Tejun Heo134d3372011-12-12 18:12:21 -08002047 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002048 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002049 tc->cset = find_css_set(old_cset, cgrp);
2050 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002051 retval = -ENOMEM;
2052 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002053 }
2054 }
2055
2056 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002057 * step 3: now that we're guaranteed success wrt the css_sets,
2058 * proceed to move all tasks to the new cgroup. There are no
2059 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002060 */
Ben Blum74a11662011-05-26 16:25:20 -07002061 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002062 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002063 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002064 }
2065 /* nothing is sensitive to fork() after this point. */
2066
2067 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002068 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002069 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002070 for_each_css(css, i, cgrp)
2071 if (css->ss->attach)
2072 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002073
2074 /*
2075 * step 5: success! and cleanup
2076 */
Ben Blum74a11662011-05-26 16:25:20 -07002077 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002078out_put_css_set_refs:
2079 if (retval) {
2080 for (i = 0; i < group_size; i++) {
2081 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002082 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002083 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002084 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002085 }
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002089 for_each_css(css, i, cgrp) {
2090 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002091 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002092 if (css->ss->cancel_attach)
2093 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002094 }
2095 }
Ben Blum74a11662011-05-26 16:25:20 -07002096out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002097 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002098 return retval;
2099}
2100
2101/*
2102 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002103 * function to attach either it or all tasks in its threadgroup. Will lock
2104 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002105 */
2106static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002108 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002109 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002110 int ret;
2111
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (!cgroup_lock_live_group(cgrp))
2113 return -ENODEV;
2114
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002115retry_find_task:
2116 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002117 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002118 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002119 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002121 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002122 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002123 }
Ben Blum74a11662011-05-26 16:25:20 -07002124 /*
2125 * even if we're attaching all tasks in the thread group, we
2126 * only need to check permissions on one of them.
2127 */
David Howellsc69e8d92008-11-14 10:39:19 +11002128 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002129 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2130 !uid_eq(cred->euid, tcred->uid) &&
2131 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002132 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 ret = -EACCES;
2134 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136 } else
2137 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002138
2139 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002141
2142 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002143 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002144 * trapped in a cpuset, or RT worker may be born in a cgroup
2145 * with no rt_runtime allocated. Just say no.
2146 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002147 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002148 ret = -EINVAL;
2149 rcu_read_unlock();
2150 goto out_unlock_cgroup;
2151 }
2152
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 get_task_struct(tsk);
2154 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002155
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002156 threadgroup_lock(tsk);
2157 if (threadgroup) {
2158 if (!thread_group_leader(tsk)) {
2159 /*
2160 * a race with de_thread from another thread's exec()
2161 * may strip us of our leadership, if this happens,
2162 * there is no choice but to throw this task away and
2163 * try again; this is
2164 * "double-double-toil-and-trouble-check locking".
2165 */
2166 threadgroup_unlock(tsk);
2167 put_task_struct(tsk);
2168 goto retry_find_task;
2169 }
Li Zefan081aa452013-03-13 09:17:09 +08002170 }
2171
2172 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2173
Tejun Heocd3d0952011-12-12 18:12:21 -08002174 threadgroup_unlock(tsk);
2175
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002177out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002178 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179 return ret;
2180}
2181
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002182/**
2183 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2184 * @from: attach to all cgroups of a given task
2185 * @tsk: the task to be attached
2186 */
2187int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2188{
2189 struct cgroupfs_root *root;
2190 int retval = 0;
2191
Tejun Heo47cfcd02013-04-07 09:29:51 -07002192 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002193 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002194 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002195
Li Zefan6f4b7e62013-07-31 16:18:36 +08002196 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002197 if (retval)
2198 break;
2199 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002201
2202 return retval;
2203}
2204EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2205
Tejun Heo182446d2013-08-08 20:11:24 -04002206static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2207 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002208{
Tejun Heo182446d2013-08-08 20:11:24 -04002209 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002210}
2211
Tejun Heo182446d2013-08-08 20:11:24 -04002212static int cgroup_procs_write(struct cgroup_subsys_state *css,
2213 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002214{
Tejun Heo182446d2013-08-08 20:11:24 -04002215 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002216}
2217
Tejun Heo182446d2013-08-08 20:11:24 -04002218static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2219 struct cftype *cft, const char *buffer)
Paul Menagee788e0662008-07-25 01:46:59 -07002220{
Tejun Heo182446d2013-08-08 20:11:24 -04002221 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002222 if (strlen(buffer) >= PATH_MAX)
2223 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002224 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e0662008-07-25 01:46:59 -07002225 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002226 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002227 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002228 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002229 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002230 return 0;
2231}
2232
Tejun Heo2da8ca82013-12-05 12:28:04 -05002233static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e0662008-07-25 01:46:59 -07002234{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002235 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002236
Paul Menagee788e0662008-07-25 01:46:59 -07002237 if (!cgroup_lock_live_group(cgrp))
2238 return -ENODEV;
2239 seq_puts(seq, cgrp->root->release_agent_path);
2240 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002241 mutex_unlock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07002242 return 0;
2243}
2244
Tejun Heo2da8ca82013-12-05 12:28:04 -05002245static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002246{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002247 struct cgroup *cgrp = seq_css(seq)->cgroup;
2248
2249 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002250 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002251}
2252
Paul Menage84eea842008-07-25 01:47:00 -07002253/* A buffer size big enough for numbers or short strings */
2254#define CGROUP_LOCAL_BUFFER_SIZE 64
2255
Tejun Heoa742c592013-12-05 12:28:03 -05002256static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002257 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002258{
Tejun Heo182446d2013-08-08 20:11:24 -04002259 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002260 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002261 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002262 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2263 char *buf;
2264 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002265
Tejun Heoa742c592013-12-05 12:28:03 -05002266 if (nbytes >= max_bytes)
2267 return -E2BIG;
2268
2269 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2270 if (!buf)
2271 return -ENOMEM;
2272
2273 if (copy_from_user(buf, userbuf, nbytes)) {
2274 ret = -EFAULT;
2275 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002276 }
Tejun Heoa742c592013-12-05 12:28:03 -05002277
2278 buf[nbytes] = '\0';
2279
2280 if (cft->write_string) {
2281 ret = cft->write_string(css, cft, strstrip(buf));
2282 } else if (cft->write_u64) {
2283 unsigned long long v;
2284 ret = kstrtoull(buf, 0, &v);
2285 if (!ret)
2286 ret = cft->write_u64(css, cft, v);
2287 } else if (cft->write_s64) {
2288 long long v;
2289 ret = kstrtoll(buf, 0, &v);
2290 if (!ret)
2291 ret = cft->write_s64(css, cft, v);
2292 } else if (cft->trigger) {
2293 ret = cft->trigger(css, (unsigned int)cft->private);
2294 } else {
2295 ret = -EINVAL;
2296 }
2297out_free:
2298 kfree(buf);
2299 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002300}
2301
Paul Menage91796562008-04-29 01:00:01 -07002302/*
2303 * seqfile ops/methods for returning structured data. Currently just
2304 * supports string->u64 maps, but can be extended in future.
2305 */
2306
Tejun Heo6612f052013-12-05 12:28:04 -05002307static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2308{
2309 struct cftype *cft = seq_cft(seq);
2310
2311 if (cft->seq_start) {
2312 return cft->seq_start(seq, ppos);
2313 } else {
2314 /*
2315 * The same behavior and code as single_open(). Returns
2316 * !NULL if pos is at the beginning; otherwise, NULL.
2317 */
2318 return NULL + !*ppos;
2319 }
2320}
2321
2322static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2323{
2324 struct cftype *cft = seq_cft(seq);
2325
2326 if (cft->seq_next) {
2327 return cft->seq_next(seq, v, ppos);
2328 } else {
2329 /*
2330 * The same behavior and code as single_open(), always
2331 * terminate after the initial read.
2332 */
2333 ++*ppos;
2334 return NULL;
2335 }
2336}
2337
2338static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2339{
2340 struct cftype *cft = seq_cft(seq);
2341
2342 if (cft->seq_stop)
2343 cft->seq_stop(seq, v);
2344}
2345
Paul Menage91796562008-04-29 01:00:01 -07002346static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2347{
Tejun Heo7da11272013-12-05 12:28:04 -05002348 struct cftype *cft = seq_cft(m);
2349 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002350
Tejun Heo2da8ca82013-12-05 12:28:04 -05002351 if (cft->seq_show)
2352 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002353
Tejun Heo896f5192013-12-05 12:28:04 -05002354 if (cft->read_u64)
2355 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2356 else if (cft->read_s64)
2357 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2358 else
2359 return -EINVAL;
2360 return 0;
2361}
Paul Menage91796562008-04-29 01:00:01 -07002362
Tejun Heo6612f052013-12-05 12:28:04 -05002363static struct seq_operations cgroup_seq_operations = {
2364 .start = cgroup_seqfile_start,
2365 .next = cgroup_seqfile_next,
2366 .stop = cgroup_seqfile_stop,
2367 .show = cgroup_seqfile_show,
2368};
2369
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370static int cgroup_file_open(struct inode *inode, struct file *file)
2371{
Tejun Heof7d58812013-08-08 20:11:23 -04002372 struct cfent *cfe = __d_cfe(file->f_dentry);
2373 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002374 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2375 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002376 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378
2379 err = generic_file_open(inode, file);
2380 if (err)
2381 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002382
2383 /*
2384 * If the file belongs to a subsystem, pin the css. Will be
2385 * unpinned either on open failure or release. This ensures that
2386 * @css stays alive for all file operations.
2387 */
Tejun Heo105347b2013-08-13 11:01:55 -04002388 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002389 css = cgroup_css(cgrp, cft->ss);
2390 if (cft->ss && !css_tryget(css))
2391 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002392 rcu_read_unlock();
2393
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002394 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002395 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002396
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002397 /*
2398 * @cfe->css is used by read/write/close to determine the
2399 * associated css. @file->private_data would be a better place but
2400 * that's already used by seqfile. Multiple accessors may use it
2401 * simultaneously which is okay as the association never changes.
2402 */
2403 WARN_ON_ONCE(cfe->css && cfe->css != css);
2404 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002405
Tejun Heo6612f052013-12-05 12:28:04 -05002406 of = __seq_open_private(file, &cgroup_seq_operations,
2407 sizeof(struct cgroup_open_file));
2408 if (of) {
2409 of->cfe = cfe;
2410 return 0;
Tejun Heo7da11272013-12-05 12:28:04 -05002411 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412
Tejun Heo6612f052013-12-05 12:28:04 -05002413 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002414 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002415 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002416}
2417
2418static int cgroup_file_release(struct inode *inode, struct file *file)
2419{
Tejun Heof7d58812013-08-08 20:11:23 -04002420 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002421 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002422
Tejun Heo67f4c362013-08-08 20:11:24 -04002423 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002424 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002425 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426}
2427
2428/*
2429 * cgroup_rename - Only allow simple rename of directories in place.
2430 */
2431static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2432 struct inode *new_dir, struct dentry *new_dentry)
2433{
Li Zefan65dff752013-03-01 15:01:56 +08002434 int ret;
2435 struct cgroup_name *name, *old_name;
2436 struct cgroup *cgrp;
2437
2438 /*
2439 * It's convinient to use parent dir's i_mutex to protected
2440 * cgrp->name.
2441 */
2442 lockdep_assert_held(&old_dir->i_mutex);
2443
Paul Menageddbcc7e2007-10-18 23:39:30 -07002444 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2445 return -ENOTDIR;
2446 if (new_dentry->d_inode)
2447 return -EEXIST;
2448 if (old_dir != new_dir)
2449 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002450
2451 cgrp = __d_cgrp(old_dentry);
2452
Tejun Heo6db8e852013-06-14 11:18:22 -07002453 /*
2454 * This isn't a proper migration and its usefulness is very
2455 * limited. Disallow if sane_behavior.
2456 */
2457 if (cgroup_sane_behavior(cgrp))
2458 return -EPERM;
2459
Li Zefan65dff752013-03-01 15:01:56 +08002460 name = cgroup_alloc_name(new_dentry);
2461 if (!name)
2462 return -ENOMEM;
2463
2464 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2465 if (ret) {
2466 kfree(name);
2467 return ret;
2468 }
2469
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002470 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002471 rcu_assign_pointer(cgrp->name, name);
2472
2473 kfree_rcu(old_name, rcu_head);
2474 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475}
2476
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002477static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2478{
2479 if (S_ISDIR(dentry->d_inode->i_mode))
2480 return &__d_cgrp(dentry)->xattrs;
2481 else
Li Zefan712317a2013-04-18 23:09:52 -07002482 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002483}
2484
2485static inline int xattr_enabled(struct dentry *dentry)
2486{
2487 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002488 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002489}
2490
2491static bool is_valid_xattr(const char *name)
2492{
2493 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2494 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2495 return true;
2496 return false;
2497}
2498
2499static int cgroup_setxattr(struct dentry *dentry, const char *name,
2500 const void *val, size_t size, int flags)
2501{
2502 if (!xattr_enabled(dentry))
2503 return -EOPNOTSUPP;
2504 if (!is_valid_xattr(name))
2505 return -EINVAL;
2506 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2507}
2508
2509static int cgroup_removexattr(struct dentry *dentry, const char *name)
2510{
2511 if (!xattr_enabled(dentry))
2512 return -EOPNOTSUPP;
2513 if (!is_valid_xattr(name))
2514 return -EINVAL;
2515 return simple_xattr_remove(__d_xattrs(dentry), name);
2516}
2517
2518static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2519 void *buf, size_t size)
2520{
2521 if (!xattr_enabled(dentry))
2522 return -EOPNOTSUPP;
2523 if (!is_valid_xattr(name))
2524 return -EINVAL;
2525 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2526}
2527
2528static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2529{
2530 if (!xattr_enabled(dentry))
2531 return -EOPNOTSUPP;
2532 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2533}
2534
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002535static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002536 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002537 .write = cgroup_file_write,
2538 .llseek = generic_file_llseek,
2539 .open = cgroup_file_open,
2540 .release = cgroup_file_release,
2541};
2542
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002543static const struct inode_operations cgroup_file_inode_operations = {
2544 .setxattr = cgroup_setxattr,
2545 .getxattr = cgroup_getxattr,
2546 .listxattr = cgroup_listxattr,
2547 .removexattr = cgroup_removexattr,
2548};
2549
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002550static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002551 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002552 .mkdir = cgroup_mkdir,
2553 .rmdir = cgroup_rmdir,
2554 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002555 .setxattr = cgroup_setxattr,
2556 .getxattr = cgroup_getxattr,
2557 .listxattr = cgroup_listxattr,
2558 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002559};
2560
Al Viroa5e7ed32011-07-26 01:55:55 -04002561static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002562 struct super_block *sb)
2563{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002564 struct inode *inode;
2565
2566 if (!dentry)
2567 return -ENOENT;
2568 if (dentry->d_inode)
2569 return -EEXIST;
2570
2571 inode = cgroup_new_inode(mode, sb);
2572 if (!inode)
2573 return -ENOMEM;
2574
2575 if (S_ISDIR(mode)) {
2576 inode->i_op = &cgroup_dir_inode_operations;
2577 inode->i_fop = &simple_dir_operations;
2578
2579 /* start off with i_nlink == 2 (for "." entry) */
2580 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002581 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002582
Tejun Heob8a2df6a2012-11-19 08:13:37 -08002583 /*
2584 * Control reaches here with cgroup_mutex held.
2585 * @inode->i_mutex should nest outside cgroup_mutex but we
2586 * want to populate it immediately without releasing
2587 * cgroup_mutex. As @inode isn't visible to anyone else
2588 * yet, trylock will always succeed without affecting
2589 * lockdep checks.
2590 */
2591 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002592 } else if (S_ISREG(mode)) {
2593 inode->i_size = 0;
2594 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002595 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002597 d_instantiate(dentry, inode);
2598 dget(dentry); /* Extra count - pin the dentry in core */
2599 return 0;
2600}
2601
Li Zefan099fca32009-04-02 16:57:29 -07002602/**
2603 * cgroup_file_mode - deduce file mode of a control file
2604 * @cft: the control file in question
2605 *
2606 * returns cft->mode if ->mode is not 0
2607 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2608 * returns S_IRUGO if it has only a read handler
2609 * returns S_IWUSR if it has only a write hander
2610 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002611static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002612{
Al Viroa5e7ed32011-07-26 01:55:55 -04002613 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002614
2615 if (cft->mode)
2616 return cft->mode;
2617
Tejun Heo2da8ca82013-12-05 12:28:04 -05002618 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002619 mode |= S_IRUGO;
2620
Tejun Heo6e0755b2013-12-05 12:28:03 -05002621 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2622 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002623 mode |= S_IWUSR;
2624
2625 return mode;
2626}
2627
Tejun Heo2bb566c2013-08-08 20:11:23 -04002628static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002629{
Paul Menagebd89aab2007-10-18 23:40:44 -07002630 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002631 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002633 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002635 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002637
Tejun Heo9fa4db32013-08-26 18:40:56 -04002638 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2639 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002640 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641 strcat(name, ".");
2642 }
2643 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002644
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002646
2647 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2648 if (!cfe)
2649 return -ENOMEM;
2650
Paul Menageddbcc7e2007-10-18 23:39:30 -07002651 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002652 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002653 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002654 goto out;
2655 }
2656
Li Zefand6cbf352013-05-14 19:44:20 +08002657 cfe->type = (void *)cft;
2658 cfe->dentry = dentry;
2659 dentry->d_fsdata = cfe;
2660 simple_xattrs_init(&cfe->xattrs);
2661
Tejun Heo05ef1d72012-04-01 12:09:56 -07002662 mode = cgroup_file_mode(cft);
2663 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2664 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002665 list_add_tail(&cfe->node, &parent->files);
2666 cfe = NULL;
2667 }
2668 dput(dentry);
2669out:
2670 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671 return error;
2672}
2673
Tejun Heob1f28d32013-06-28 16:24:10 -07002674/**
2675 * cgroup_addrm_files - add or remove files to a cgroup directory
2676 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002677 * @cfts: array of cftypes to be added
2678 * @is_add: whether to add or remove
2679 *
2680 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002681 * For removals, this function never fails. If addition fails, this
2682 * function doesn't remove files already added. The caller is responsible
2683 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002684 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002685static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2686 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002688 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002689 int ret;
2690
2691 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2692 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002693
2694 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002695 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002696 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2697 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002698 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2699 continue;
2700 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2701 continue;
2702
Li Zefan2739d3c2013-01-21 18:18:33 +08002703 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002704 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002705 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002706 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002707 cft->name, ret);
2708 return ret;
2709 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002710 } else {
2711 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002712 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002714 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715}
2716
Tejun Heo8e3f6542012-04-01 12:09:55 -07002717static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002718 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002719{
2720 /*
2721 * Thanks to the entanglement with vfs inode locking, we can't walk
2722 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002723 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2724 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002725 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002726 mutex_lock(&cgroup_mutex);
2727}
2728
Tejun Heo2bb566c2013-08-08 20:11:23 -04002729static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002730 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002731{
2732 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002733 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002734 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002735 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002736 struct dentry *prev = NULL;
2737 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002738 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002739 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002740 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002741
2742 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002743 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002744 !atomic_inc_not_zero(&sb->s_active)) {
2745 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002746 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002747 }
2748
Li Zefane8c82d22013-06-18 18:48:37 +08002749 /*
2750 * All cgroups which are created after we drop cgroup_mutex will
2751 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002752 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002753 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002754 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002755
Tejun Heo8e3f6542012-04-01 12:09:55 -07002756 mutex_unlock(&cgroup_mutex);
2757
Li Zefane8c82d22013-06-18 18:48:37 +08002758 /* add/rm files for all cgroups created before */
2759 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002760 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002761 struct cgroup *cgrp = css->cgroup;
2762
Li Zefane8c82d22013-06-18 18:48:37 +08002763 if (cgroup_is_dead(cgrp))
2764 continue;
2765
2766 inode = cgrp->dentry->d_inode;
2767 dget(cgrp->dentry);
2768 rcu_read_unlock();
2769
2770 dput(prev);
2771 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002772
2773 mutex_lock(&inode->i_mutex);
2774 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002775 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002776 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002777 mutex_unlock(&cgroup_mutex);
2778 mutex_unlock(&inode->i_mutex);
2779
Li Zefane8c82d22013-06-18 18:48:37 +08002780 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002781 if (ret)
2782 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002783 }
Li Zefane8c82d22013-06-18 18:48:37 +08002784 rcu_read_unlock();
2785 dput(prev);
2786 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002787 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002788}
2789
2790/**
2791 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2792 * @ss: target cgroup subsystem
2793 * @cfts: zero-length name terminated array of cftypes
2794 *
2795 * Register @cfts to @ss. Files described by @cfts are created for all
2796 * existing cgroups to which @ss is attached and all future cgroups will
2797 * have them too. This function can be called anytime whether @ss is
2798 * attached or not.
2799 *
2800 * Returns 0 on successful registration, -errno on failure. Note that this
2801 * function currently returns 0 as long as @cfts registration is successful
2802 * even if some file creation attempts on existing cgroups fail.
2803 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002804int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002805{
2806 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002807 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002808 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809
2810 set = kzalloc(sizeof(*set), GFP_KERNEL);
2811 if (!set)
2812 return -ENOMEM;
2813
Tejun Heo2bb566c2013-08-08 20:11:23 -04002814 for (cft = cfts; cft->name[0] != '\0'; cft++)
2815 cft->ss = ss;
2816
Tejun Heo8e3f6542012-04-01 12:09:55 -07002817 cgroup_cfts_prepare();
2818 set->cfts = cfts;
2819 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002820 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002821 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002822 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002823 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002824}
2825EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2826
Li Zefana043e3b2008-02-23 15:24:09 -08002827/**
Tejun Heo79578622012-04-01 12:09:56 -07002828 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002829 * @cfts: zero-length name terminated array of cftypes
2830 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002831 * Unregister @cfts. Files described by @cfts are removed from all
2832 * existing cgroups and all future cgroups won't have them either. This
2833 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002834 *
2835 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002836 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002837 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002838int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002839{
2840 struct cftype_set *set;
2841
Tejun Heo2bb566c2013-08-08 20:11:23 -04002842 if (!cfts || !cfts[0].ss)
2843 return -ENOENT;
2844
Tejun Heo79578622012-04-01 12:09:56 -07002845 cgroup_cfts_prepare();
2846
Tejun Heo2bb566c2013-08-08 20:11:23 -04002847 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002848 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002849 list_del(&set->node);
2850 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002851 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002852 return 0;
2853 }
2854 }
2855
Tejun Heo2bb566c2013-08-08 20:11:23 -04002856 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002857 return -ENOENT;
2858}
2859
2860/**
Li Zefana043e3b2008-02-23 15:24:09 -08002861 * cgroup_task_count - count the number of tasks in a cgroup.
2862 * @cgrp: the cgroup in question
2863 *
2864 * Return the number of tasks in the cgroup.
2865 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002866int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002867{
2868 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002869 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002870
Paul Menage817929e2007-10-18 23:39:36 -07002871 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002872 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2873 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002874 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002875 return count;
2876}
2877
2878/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002879 * To reduce the fork() overhead for systems that are not actually using
2880 * their cgroups capability, we don't maintain the lists running through
2881 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002882 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002883 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002884static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002885{
2886 struct task_struct *p, *g;
2887 write_lock(&css_set_lock);
2888 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002889 /*
2890 * We need tasklist_lock because RCU is not safe against
2891 * while_each_thread(). Besides, a forking task that has passed
2892 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2893 * is not guaranteed to have its child immediately visible in the
2894 * tasklist if we walk through it with RCU.
2895 */
2896 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002897 do_each_thread(g, p) {
2898 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002899 /*
2900 * We should check if the process is exiting, otherwise
2901 * it will race with cgroup_exit() in that the list
2902 * entry won't be deleted though the process has exited.
2903 */
2904 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002905 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002906 task_unlock(p);
2907 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002908 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002909 write_unlock(&css_set_lock);
2910}
2911
Tejun Heo574bd9f2012-11-09 09:12:29 -08002912/**
Tejun Heo492eb212013-08-08 20:11:25 -04002913 * css_next_child - find the next child of a given css
2914 * @pos_css: the current position (%NULL to initiate traversal)
2915 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002916 *
Tejun Heo492eb212013-08-08 20:11:25 -04002917 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002918 * under either cgroup_mutex or RCU read lock. The only requirement is
2919 * that @parent_css and @pos_css are accessible. The next sibling is
2920 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002921 */
Tejun Heo492eb212013-08-08 20:11:25 -04002922struct cgroup_subsys_state *
2923css_next_child(struct cgroup_subsys_state *pos_css,
2924 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002925{
Tejun Heo492eb212013-08-08 20:11:25 -04002926 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2927 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002928 struct cgroup *next;
2929
Tejun Heo87fb54f2013-12-06 15:11:55 -05002930 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002931
2932 /*
2933 * @pos could already have been removed. Once a cgroup is removed,
2934 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002935 * changes. As CGRP_DEAD assertion is serialized and happens
2936 * before the cgroup is taken off the ->sibling list, if we see it
2937 * unasserted, it's guaranteed that the next sibling hasn't
2938 * finished its grace period even if it's already removed, and thus
2939 * safe to dereference from this RCU critical section. If
2940 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2941 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002942 *
2943 * If @pos is dead, its next pointer can't be dereferenced;
2944 * however, as each cgroup is given a monotonically increasing
2945 * unique serial number and always appended to the sibling list,
2946 * the next one can be found by walking the parent's children until
2947 * we see a cgroup with higher serial number than @pos's. While
2948 * this path can be slower, it's taken only when either the current
2949 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002950 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002951 if (!pos) {
2952 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2953 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002954 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002955 } else {
2956 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2957 if (next->serial_nr > pos->serial_nr)
2958 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002959 }
2960
Tejun Heo492eb212013-08-08 20:11:25 -04002961 if (&next->sibling == &cgrp->children)
2962 return NULL;
2963
Tejun Heoca8bdca2013-08-26 18:40:56 -04002964 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002965}
Tejun Heo492eb212013-08-08 20:11:25 -04002966EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002967
2968/**
Tejun Heo492eb212013-08-08 20:11:25 -04002969 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002970 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002971 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002972 *
Tejun Heo492eb212013-08-08 20:11:25 -04002973 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002974 * to visit for pre-order traversal of @root's descendants. @root is
2975 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002976 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002977 * While this function requires cgroup_mutex or RCU read locking, it
2978 * doesn't require the whole traversal to be contained in a single critical
2979 * section. This function will return the correct next descendant as long
2980 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002981 */
Tejun Heo492eb212013-08-08 20:11:25 -04002982struct cgroup_subsys_state *
2983css_next_descendant_pre(struct cgroup_subsys_state *pos,
2984 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002985{
Tejun Heo492eb212013-08-08 20:11:25 -04002986 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002987
Tejun Heo87fb54f2013-12-06 15:11:55 -05002988 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002989
Tejun Heobd8815a2013-08-08 20:11:27 -04002990 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002991 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002992 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002993
2994 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002995 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002996 if (next)
2997 return next;
2998
2999 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003000 while (pos != root) {
3001 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003002 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003003 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003004 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003005 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003006
3007 return NULL;
3008}
Tejun Heo492eb212013-08-08 20:11:25 -04003009EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003010
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003011/**
Tejun Heo492eb212013-08-08 20:11:25 -04003012 * css_rightmost_descendant - return the rightmost descendant of a css
3013 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003014 *
Tejun Heo492eb212013-08-08 20:11:25 -04003015 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3016 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003017 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003018 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003019 * While this function requires cgroup_mutex or RCU read locking, it
3020 * doesn't require the whole traversal to be contained in a single critical
3021 * section. This function will return the correct rightmost descendant as
3022 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003023 */
Tejun Heo492eb212013-08-08 20:11:25 -04003024struct cgroup_subsys_state *
3025css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003026{
Tejun Heo492eb212013-08-08 20:11:25 -04003027 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003028
Tejun Heo87fb54f2013-12-06 15:11:55 -05003029 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003030
3031 do {
3032 last = pos;
3033 /* ->prev isn't RCU safe, walk ->next till the end */
3034 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003035 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003036 pos = tmp;
3037 } while (pos);
3038
3039 return last;
3040}
Tejun Heo492eb212013-08-08 20:11:25 -04003041EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003042
Tejun Heo492eb212013-08-08 20:11:25 -04003043static struct cgroup_subsys_state *
3044css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003045{
Tejun Heo492eb212013-08-08 20:11:25 -04003046 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003047
3048 do {
3049 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003050 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003051 } while (pos);
3052
3053 return last;
3054}
3055
3056/**
Tejun Heo492eb212013-08-08 20:11:25 -04003057 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003058 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003059 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003060 *
Tejun Heo492eb212013-08-08 20:11:25 -04003061 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003062 * to visit for post-order traversal of @root's descendants. @root is
3063 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003064 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003065 * While this function requires cgroup_mutex or RCU read locking, it
3066 * doesn't require the whole traversal to be contained in a single critical
3067 * section. This function will return the correct next descendant as long
3068 * as both @pos and @cgroup are accessible and @pos is a descendant of
3069 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003070 */
Tejun Heo492eb212013-08-08 20:11:25 -04003071struct cgroup_subsys_state *
3072css_next_descendant_post(struct cgroup_subsys_state *pos,
3073 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074{
Tejun Heo492eb212013-08-08 20:11:25 -04003075 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003076
Tejun Heo87fb54f2013-12-06 15:11:55 -05003077 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078
Tejun Heo58b79a92013-09-06 15:31:08 -04003079 /* if first iteration, visit leftmost descendant which may be @root */
3080 if (!pos)
3081 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082
Tejun Heobd8815a2013-08-08 20:11:27 -04003083 /* if we visited @root, we're done */
3084 if (pos == root)
3085 return NULL;
3086
Tejun Heo574bd9f2012-11-09 09:12:29 -08003087 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003088 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003089 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003090 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003091
3092 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003093 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003094}
Tejun Heo492eb212013-08-08 20:11:25 -04003095EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003096
Tejun Heo0942eee2013-08-08 20:11:26 -04003097/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003098 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003099 * @it: the iterator to advance
3100 *
3101 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003102 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003103static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003104{
3105 struct list_head *l = it->cset_link;
3106 struct cgrp_cset_link *link;
3107 struct css_set *cset;
3108
3109 /* Advance to the next non-empty css_set */
3110 do {
3111 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003112 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003113 it->cset_link = NULL;
3114 return;
3115 }
3116 link = list_entry(l, struct cgrp_cset_link, cset_link);
3117 cset = link->cset;
3118 } while (list_empty(&cset->tasks));
3119 it->cset_link = l;
3120 it->task = cset->tasks.next;
3121}
3122
Tejun Heo0942eee2013-08-08 20:11:26 -04003123/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003124 * css_task_iter_start - initiate task iteration
3125 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003126 * @it: the task iterator to use
3127 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003128 * Initiate iteration through the tasks of @css. The caller can call
3129 * css_task_iter_next() to walk through the tasks until the function
3130 * returns NULL. On completion of iteration, css_task_iter_end() must be
3131 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003132 *
3133 * Note that this function acquires a lock which is released when the
3134 * iteration finishes. The caller can't sleep while iteration is in
3135 * progress.
3136 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003137void css_task_iter_start(struct cgroup_subsys_state *css,
3138 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003139 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003140{
3141 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003142 * The first time anyone tries to iterate across a css, we need to
3143 * enable the list linking each css_set to its tasks, and fix up
3144 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003145 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003146 if (!use_task_css_set_links)
3147 cgroup_enable_task_cg_lists();
3148
Paul Menage817929e2007-10-18 23:39:36 -07003149 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003150
Tejun Heo72ec7022013-08-08 20:11:26 -04003151 it->origin_css = css;
3152 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003153
Tejun Heo72ec7022013-08-08 20:11:26 -04003154 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003155}
3156
Tejun Heo0942eee2013-08-08 20:11:26 -04003157/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003158 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003159 * @it: the task iterator being iterated
3160 *
3161 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003162 * initialized via css_task_iter_start(). Returns NULL when the iteration
3163 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003164 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003165struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003166{
3167 struct task_struct *res;
3168 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003169 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003170
3171 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003172 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003173 return NULL;
3174 res = list_entry(l, struct task_struct, cg_list);
3175 /* Advance iterator to find next entry */
3176 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003177 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3178 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003179 /*
3180 * We reached the end of this task list - move on to the
3181 * next cgrp_cset_link.
3182 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003183 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003184 } else {
3185 it->task = l;
3186 }
3187 return res;
3188}
3189
Tejun Heo0942eee2013-08-08 20:11:26 -04003190/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003191 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003192 * @it: the task iterator to finish
3193 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003194 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003195 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003196void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003197 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003198{
3199 read_unlock(&css_set_lock);
3200}
3201
Cliff Wickman31a7df02008-02-07 00:14:42 -08003202static inline int started_after_time(struct task_struct *t1,
3203 struct timespec *time,
3204 struct task_struct *t2)
3205{
3206 int start_diff = timespec_compare(&t1->start_time, time);
3207 if (start_diff > 0) {
3208 return 1;
3209 } else if (start_diff < 0) {
3210 return 0;
3211 } else {
3212 /*
3213 * Arbitrarily, if two processes started at the same
3214 * time, we'll say that the lower pointer value
3215 * started first. Note that t2 may have exited by now
3216 * so this may not be a valid pointer any longer, but
3217 * that's fine - it still serves to distinguish
3218 * between two tasks started (effectively) simultaneously.
3219 */
3220 return t1 > t2;
3221 }
3222}
3223
3224/*
3225 * This function is a callback from heap_insert() and is used to order
3226 * the heap.
3227 * In this case we order the heap in descending task start time.
3228 */
3229static inline int started_after(void *p1, void *p2)
3230{
3231 struct task_struct *t1 = p1;
3232 struct task_struct *t2 = p2;
3233 return started_after_time(t1, &t2->start_time, t2);
3234}
3235
3236/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003237 * css_scan_tasks - iterate though all the tasks in a css
3238 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003239 * @test: optional test callback
3240 * @process: process callback
3241 * @data: data passed to @test and @process
3242 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003243 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003244 * Iterate through all the tasks in @css, calling @test for each, and if it
3245 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003246 *
Tejun Heoe5358372013-08-08 20:11:26 -04003247 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003248 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003249 * lock css_set_lock for the call to @process.
3250 *
3251 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003252 * of @css for the duration of this call. This function may or may not
3253 * call @process for tasks that exit or move to a different css during the
3254 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003255 *
3256 * Note that @test may be called with locks held, and may in some
3257 * situations be called multiple times for the same task, so it should be
3258 * cheap.
3259 *
3260 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3261 * heap operations (and its "gt" member will be overwritten), else a
3262 * temporary heap will be used (allocation of which may cause this function
3263 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003264 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003265int css_scan_tasks(struct cgroup_subsys_state *css,
3266 bool (*test)(struct task_struct *, void *),
3267 void (*process)(struct task_struct *, void *),
3268 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003269{
3270 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003271 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003272 struct task_struct *p, *dropped;
3273 /* Never dereference latest_task, since it's not refcounted */
3274 struct task_struct *latest_task = NULL;
3275 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003276 struct timespec latest_time = { 0, 0 };
3277
Tejun Heoe5358372013-08-08 20:11:26 -04003278 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003280 heap->gt = &started_after;
3281 } else {
3282 /* We need to allocate our own heap memory */
3283 heap = &tmp_heap;
3284 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3285 if (retval)
3286 /* cannot allocate the heap */
3287 return retval;
3288 }
3289
3290 again:
3291 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003292 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003293 * which are of interest, and invoking @process callback on the
3294 * ones which need an update. Since we don't want to hold any
3295 * locks during the task updates, gather tasks to be processed in a
3296 * heap structure. The heap is sorted by descending task start
3297 * time. If the statically-sized heap fills up, we overflow tasks
3298 * that started later, and in future iterations only consider tasks
3299 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003300 * guarantees forward progress and that we don't miss any tasks.
3301 */
3302 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003303 css_task_iter_start(css, &it);
3304 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003305 /*
3306 * Only affect tasks that qualify per the caller's callback,
3307 * if he provided one
3308 */
Tejun Heoe5358372013-08-08 20:11:26 -04003309 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003310 continue;
3311 /*
3312 * Only process tasks that started after the last task
3313 * we processed
3314 */
3315 if (!started_after_time(p, &latest_time, latest_task))
3316 continue;
3317 dropped = heap_insert(heap, p);
3318 if (dropped == NULL) {
3319 /*
3320 * The new task was inserted; the heap wasn't
3321 * previously full
3322 */
3323 get_task_struct(p);
3324 } else if (dropped != p) {
3325 /*
3326 * The new task was inserted, and pushed out a
3327 * different task
3328 */
3329 get_task_struct(p);
3330 put_task_struct(dropped);
3331 }
3332 /*
3333 * Else the new task was newer than anything already in
3334 * the heap and wasn't inserted
3335 */
3336 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003337 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003338
3339 if (heap->size) {
3340 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003341 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003342 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003343 latest_time = q->start_time;
3344 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003345 }
3346 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003347 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003348 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003349 }
3350 /*
3351 * If we had to process any tasks at all, scan again
3352 * in case some of them were in the middle of forking
3353 * children that didn't get processed.
3354 * Not the most efficient way to do it, but it avoids
3355 * having to take callback_mutex in the fork path
3356 */
3357 goto again;
3358 }
3359 if (heap == &tmp_heap)
3360 heap_free(&tmp_heap);
3361 return 0;
3362}
3363
Tejun Heoe5358372013-08-08 20:11:26 -04003364static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003365{
Tejun Heoe5358372013-08-08 20:11:26 -04003366 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003367
Tejun Heo47cfcd02013-04-07 09:29:51 -07003368 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003369 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003370 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003371}
3372
3373/**
3374 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3375 * @to: cgroup to which the tasks will be moved
3376 * @from: cgroup in which the tasks currently reside
3377 */
3378int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3379{
Tejun Heo72ec7022013-08-08 20:11:26 -04003380 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3381 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003382}
3383
Paul Menage817929e2007-10-18 23:39:36 -07003384/*
Ben Blum102a7752009-09-23 15:56:26 -07003385 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003386 *
3387 * Reading this file can return large amounts of data if a cgroup has
3388 * *lots* of attached tasks. So it may need several calls to read(),
3389 * but we cannot guarantee that the information we produce is correct
3390 * unless we produce it entirely atomically.
3391 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003392 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003393
Li Zefan24528252012-01-20 11:58:43 +08003394/* which pidlist file are we talking about? */
3395enum cgroup_filetype {
3396 CGROUP_FILE_PROCS,
3397 CGROUP_FILE_TASKS,
3398};
3399
3400/*
3401 * A pidlist is a list of pids that virtually represents the contents of one
3402 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3403 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3404 * to the cgroup.
3405 */
3406struct cgroup_pidlist {
3407 /*
3408 * used to find which pidlist is wanted. doesn't change as long as
3409 * this particular list stays in the list.
3410 */
3411 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3412 /* array of xids */
3413 pid_t *list;
3414 /* how many elements the above list has */
3415 int length;
Li Zefan24528252012-01-20 11:58:43 +08003416 /* each of these stored in a list by its cgroup */
3417 struct list_head links;
3418 /* pointer to the cgroup we belong to, for list removal purposes */
3419 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003420 /* for delayed destruction */
3421 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003422};
3423
Paul Menagebbcb81d2007-10-18 23:39:32 -07003424/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003425 * The following two functions "fix" the issue where there are more pids
3426 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3427 * TODO: replace with a kernel-wide solution to this problem
3428 */
3429#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3430static void *pidlist_allocate(int count)
3431{
3432 if (PIDLIST_TOO_LARGE(count))
3433 return vmalloc(count * sizeof(pid_t));
3434 else
3435 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3436}
Tejun Heob1a21362013-11-29 10:42:58 -05003437
Ben Blumd1d9fd32009-09-23 15:56:28 -07003438static void pidlist_free(void *p)
3439{
3440 if (is_vmalloc_addr(p))
3441 vfree(p);
3442 else
3443 kfree(p);
3444}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003445
3446/*
Tejun Heob1a21362013-11-29 10:42:58 -05003447 * Used to destroy all pidlists lingering waiting for destroy timer. None
3448 * should be left afterwards.
3449 */
3450static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3451{
3452 struct cgroup_pidlist *l, *tmp_l;
3453
3454 mutex_lock(&cgrp->pidlist_mutex);
3455 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3456 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3457 mutex_unlock(&cgrp->pidlist_mutex);
3458
3459 flush_workqueue(cgroup_pidlist_destroy_wq);
3460 BUG_ON(!list_empty(&cgrp->pidlists));
3461}
3462
3463static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3464{
3465 struct delayed_work *dwork = to_delayed_work(work);
3466 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3467 destroy_dwork);
3468 struct cgroup_pidlist *tofree = NULL;
3469
3470 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003471
3472 /*
Tejun Heo04502362013-11-29 10:42:59 -05003473 * Destroy iff we didn't get queued again. The state won't change
3474 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003475 */
Tejun Heo04502362013-11-29 10:42:59 -05003476 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003477 list_del(&l->links);
3478 pidlist_free(l->list);
3479 put_pid_ns(l->key.ns);
3480 tofree = l;
3481 }
3482
Tejun Heob1a21362013-11-29 10:42:58 -05003483 mutex_unlock(&l->owner->pidlist_mutex);
3484 kfree(tofree);
3485}
3486
3487/*
Ben Blum102a7752009-09-23 15:56:26 -07003488 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003489 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003490 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003491static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003492{
Ben Blum102a7752009-09-23 15:56:26 -07003493 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003494
3495 /*
3496 * we presume the 0th element is unique, so i starts at 1. trivial
3497 * edge cases first; no work needs to be done for either
3498 */
3499 if (length == 0 || length == 1)
3500 return length;
3501 /* src and dest walk down the list; dest counts unique elements */
3502 for (src = 1; src < length; src++) {
3503 /* find next unique element */
3504 while (list[src] == list[src-1]) {
3505 src++;
3506 if (src == length)
3507 goto after;
3508 }
3509 /* dest always points to where the next unique element goes */
3510 list[dest] = list[src];
3511 dest++;
3512 }
3513after:
Ben Blum102a7752009-09-23 15:56:26 -07003514 return dest;
3515}
3516
Tejun Heoafb2bc12013-11-29 10:42:59 -05003517/*
3518 * The two pid files - task and cgroup.procs - guaranteed that the result
3519 * is sorted, which forced this whole pidlist fiasco. As pid order is
3520 * different per namespace, each namespace needs differently sorted list,
3521 * making it impossible to use, for example, single rbtree of member tasks
3522 * sorted by task pointer. As pidlists can be fairly large, allocating one
3523 * per open file is dangerous, so cgroup had to implement shared pool of
3524 * pidlists keyed by cgroup and namespace.
3525 *
3526 * All this extra complexity was caused by the original implementation
3527 * committing to an entirely unnecessary property. In the long term, we
3528 * want to do away with it. Explicitly scramble sort order if
3529 * sane_behavior so that no such expectation exists in the new interface.
3530 *
3531 * Scrambling is done by swapping every two consecutive bits, which is
3532 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3533 */
3534static pid_t pid_fry(pid_t pid)
3535{
3536 unsigned a = pid & 0x55555555;
3537 unsigned b = pid & 0xAAAAAAAA;
3538
3539 return (a << 1) | (b >> 1);
3540}
3541
3542static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3543{
3544 if (cgroup_sane_behavior(cgrp))
3545 return pid_fry(pid);
3546 else
3547 return pid;
3548}
3549
Ben Blum102a7752009-09-23 15:56:26 -07003550static int cmppid(const void *a, const void *b)
3551{
3552 return *(pid_t *)a - *(pid_t *)b;
3553}
3554
Tejun Heoafb2bc12013-11-29 10:42:59 -05003555static int fried_cmppid(const void *a, const void *b)
3556{
3557 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3558}
3559
Ben Blum72a8cb32009-09-23 15:56:27 -07003560static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3561 enum cgroup_filetype type)
3562{
3563 struct cgroup_pidlist *l;
3564 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003565 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003566
Tejun Heoe6b81712013-11-29 10:42:59 -05003567 lockdep_assert_held(&cgrp->pidlist_mutex);
3568
3569 list_for_each_entry(l, &cgrp->pidlists, links)
3570 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003571 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003572 return NULL;
3573}
3574
3575/*
3576 * find the appropriate pidlist for our purpose (given procs vs tasks)
3577 * returns with the lock on that pidlist already held, and takes care
3578 * of the use count, or returns NULL with no locks held if we're out of
3579 * memory.
3580 */
3581static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3582 enum cgroup_filetype type)
3583{
3584 struct cgroup_pidlist *l;
3585
3586 lockdep_assert_held(&cgrp->pidlist_mutex);
3587
3588 l = cgroup_pidlist_find(cgrp, type);
3589 if (l)
3590 return l;
3591
Ben Blum72a8cb32009-09-23 15:56:27 -07003592 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003593 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003594 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003596
Tejun Heob1a21362013-11-29 10:42:58 -05003597 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003598 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003599 /* don't need task_nsproxy() if we're looking at ourself */
3600 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003601 l->owner = cgrp;
3602 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003603 return l;
3604}
3605
3606/*
Ben Blum102a7752009-09-23 15:56:26 -07003607 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3608 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003609static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3610 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003611{
3612 pid_t *array;
3613 int length;
3614 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003615 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003616 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003617 struct cgroup_pidlist *l;
3618
Tejun Heo4bac00d2013-11-29 10:42:59 -05003619 lockdep_assert_held(&cgrp->pidlist_mutex);
3620
Ben Blum102a7752009-09-23 15:56:26 -07003621 /*
3622 * If cgroup gets more users after we read count, we won't have
3623 * enough space - tough. This race is indistinguishable to the
3624 * caller from the case that the additional cgroup users didn't
3625 * show up until sometime later on.
3626 */
3627 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003628 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003629 if (!array)
3630 return -ENOMEM;
3631 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003632 css_task_iter_start(&cgrp->dummy_css, &it);
3633 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003634 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003635 break;
Ben Blum102a7752009-09-23 15:56:26 -07003636 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 if (type == CGROUP_FILE_PROCS)
3638 pid = task_tgid_vnr(tsk);
3639 else
3640 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003641 if (pid > 0) /* make sure to only use valid results */
3642 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003643 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003644 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003645 length = n;
3646 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003647 if (cgroup_sane_behavior(cgrp))
3648 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3649 else
3650 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003651 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003652 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003653
Tejun Heoe6b81712013-11-29 10:42:59 -05003654 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003655 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003656 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003657 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003658 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003659 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003660
3661 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003662 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003663 l->list = array;
3664 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003665 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003666 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003667}
3668
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669/**
Li Zefana043e3b2008-02-23 15:24:09 -08003670 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003671 * @stats: cgroupstats to fill information into
3672 * @dentry: A dentry entry belonging to the cgroup for which stats have
3673 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003674 *
3675 * Build and fill cgroupstats so that taskstats can export it to user
3676 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003677 */
3678int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3679{
3680 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003681 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003682 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003683 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003684
Balbir Singh846c7bb2007-10-18 23:39:44 -07003685 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003686 * Validate dentry by checking the superblock operations,
3687 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003688 */
Li Zefan33d283b2008-11-19 15:36:48 -08003689 if (dentry->d_sb->s_op != &cgroup_ops ||
3690 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003691 goto err;
3692
3693 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003694 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003695
Tejun Heo72ec7022013-08-08 20:11:26 -04003696 css_task_iter_start(&cgrp->dummy_css, &it);
3697 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003698 switch (tsk->state) {
3699 case TASK_RUNNING:
3700 stats->nr_running++;
3701 break;
3702 case TASK_INTERRUPTIBLE:
3703 stats->nr_sleeping++;
3704 break;
3705 case TASK_UNINTERRUPTIBLE:
3706 stats->nr_uninterruptible++;
3707 break;
3708 case TASK_STOPPED:
3709 stats->nr_stopped++;
3710 break;
3711 default:
3712 if (delayacct_is_task_waiting_on_io(tsk))
3713 stats->nr_io_wait++;
3714 break;
3715 }
3716 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003717 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003718
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719err:
3720 return ret;
3721}
3722
Paul Menage8f3ff202009-09-23 15:56:25 -07003723
Paul Menagecc31edc2008-10-18 20:28:04 -07003724/*
Ben Blum102a7752009-09-23 15:56:26 -07003725 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003726 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003727 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003728 */
3729
Ben Blum102a7752009-09-23 15:56:26 -07003730static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003731{
3732 /*
3733 * Initially we receive a position value that corresponds to
3734 * one more than the last pid shown (or 0 on the first call or
3735 * after a seek to the start). Use a binary-search to find the
3736 * next pid to display, if any
3737 */
Tejun Heo5d224442013-12-05 12:28:04 -05003738 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003739 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003740 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003741 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003742 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003743 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003744
Tejun Heo4bac00d2013-11-29 10:42:59 -05003745 mutex_lock(&cgrp->pidlist_mutex);
3746
3747 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003748 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003749 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003750 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003751 * could already have been destroyed.
3752 */
Tejun Heo5d224442013-12-05 12:28:04 -05003753 if (of->priv)
3754 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003755
3756 /*
3757 * Either this is the first start() after open or the matching
3758 * pidlist has been destroyed inbetween. Create a new one.
3759 */
Tejun Heo5d224442013-12-05 12:28:04 -05003760 if (!of->priv) {
3761 ret = pidlist_array_load(cgrp, type,
3762 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003763 if (ret)
3764 return ERR_PTR(ret);
3765 }
Tejun Heo5d224442013-12-05 12:28:04 -05003766 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003767
Paul Menagecc31edc2008-10-18 20:28:04 -07003768 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003769 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003770
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 while (index < end) {
3772 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003773 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 index = mid;
3775 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003776 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 index = mid + 1;
3778 else
3779 end = mid;
3780 }
3781 }
3782 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003783 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 return NULL;
3785 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003786 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003787 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003788 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003789}
3790
Ben Blum102a7752009-09-23 15:56:26 -07003791static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003792{
Tejun Heo5d224442013-12-05 12:28:04 -05003793 struct cgroup_open_file *of = s->private;
3794 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003795
Tejun Heo5d224442013-12-05 12:28:04 -05003796 if (l)
3797 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003798 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003799 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003800}
3801
Ben Blum102a7752009-09-23 15:56:26 -07003802static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003803{
Tejun Heo5d224442013-12-05 12:28:04 -05003804 struct cgroup_open_file *of = s->private;
3805 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003806 pid_t *p = v;
3807 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 /*
3809 * Advance to the next pid in the array. If this goes off the
3810 * end, we're done
3811 */
3812 p++;
3813 if (p >= end) {
3814 return NULL;
3815 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003816 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003817 return p;
3818 }
3819}
3820
Ben Blum102a7752009-09-23 15:56:26 -07003821static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003822{
3823 return seq_printf(s, "%d\n", *(int *)v);
3824}
3825
Ben Blum102a7752009-09-23 15:56:26 -07003826/*
3827 * seq_operations functions for iterating on pidlists through seq_file -
3828 * independent of whether it's tasks or procs
3829 */
3830static const struct seq_operations cgroup_pidlist_seq_operations = {
3831 .start = cgroup_pidlist_start,
3832 .stop = cgroup_pidlist_stop,
3833 .next = cgroup_pidlist_next,
3834 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003835};
3836
Tejun Heo182446d2013-08-08 20:11:24 -04003837static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3838 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003839{
Tejun Heo182446d2013-08-08 20:11:24 -04003840 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003841}
3842
Tejun Heo182446d2013-08-08 20:11:24 -04003843static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3844 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003845{
Tejun Heo182446d2013-08-08 20:11:24 -04003846 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003847 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003848 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003849 else
Tejun Heo182446d2013-08-08 20:11:24 -04003850 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003851 return 0;
3852}
3853
Paul Menagebbcb81d2007-10-18 23:39:32 -07003854/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003855 * When dput() is called asynchronously, if umount has been done and
3856 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3857 * there's a small window that vfs will see the root dentry with non-zero
3858 * refcnt and trigger BUG().
3859 *
3860 * That's why we hold a reference before dput() and drop it right after.
3861 */
3862static void cgroup_dput(struct cgroup *cgrp)
3863{
3864 struct super_block *sb = cgrp->root->sb;
3865
3866 atomic_inc(&sb->s_active);
3867 dput(cgrp->dentry);
3868 deactivate_super(sb);
3869}
3870
Tejun Heo182446d2013-08-08 20:11:24 -04003871static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3872 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003873{
Tejun Heo182446d2013-08-08 20:11:24 -04003874 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003875}
3876
Tejun Heo182446d2013-08-08 20:11:24 -04003877static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3878 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003879{
3880 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003881 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003882 else
Tejun Heo182446d2013-08-08 20:11:24 -04003883 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003884 return 0;
3885}
3886
Tejun Heod5c56ce2013-06-03 19:14:34 -07003887static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003888 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003889 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003890 .seq_start = cgroup_pidlist_start,
3891 .seq_next = cgroup_pidlist_next,
3892 .seq_stop = cgroup_pidlist_stop,
3893 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003894 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003895 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003896 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003897 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003898 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003899 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003900 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003901 .read_u64 = cgroup_clone_children_read,
3902 .write_u64 = cgroup_clone_children_write,
3903 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003904 {
Tejun Heo873fe092013-04-14 20:15:26 -07003905 .name = "cgroup.sane_behavior",
3906 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003907 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003908 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003909
3910 /*
3911 * Historical crazy stuff. These don't have "cgroup." prefix and
3912 * don't exist if sane_behavior. If you're depending on these, be
3913 * prepared to be burned.
3914 */
3915 {
3916 .name = "tasks",
3917 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003918 .seq_start = cgroup_pidlist_start,
3919 .seq_next = cgroup_pidlist_next,
3920 .seq_stop = cgroup_pidlist_stop,
3921 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003922 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003923 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003924 .mode = S_IRUGO | S_IWUSR,
3925 },
3926 {
3927 .name = "notify_on_release",
3928 .flags = CFTYPE_INSANE,
3929 .read_u64 = cgroup_read_notify_on_release,
3930 .write_u64 = cgroup_write_notify_on_release,
3931 },
Tejun Heo873fe092013-04-14 20:15:26 -07003932 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003933 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003934 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003935 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003936 .write_string = cgroup_release_agent_write,
3937 .max_write_len = PATH_MAX,
3938 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003939 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003940};
3941
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003942/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003943 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003944 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003945 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003946 *
3947 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003948 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003949static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003950{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003951 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003952 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003953
Tejun Heo8e3f6542012-04-01 12:09:55 -07003954 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003955 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003956 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003957
3958 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003959 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003960
Tejun Heobee55092013-06-28 16:24:11 -07003961 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003962 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003963 if (ret < 0)
3964 goto err;
3965 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003966 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003967 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003968err:
3969 cgroup_clear_dir(cgrp, subsys_mask);
3970 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003971}
3972
Tejun Heo0c21ead2013-08-13 20:22:51 -04003973/*
3974 * css destruction is four-stage process.
3975 *
3976 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3977 * Implemented in kill_css().
3978 *
3979 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3980 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3981 * by invoking offline_css(). After offlining, the base ref is put.
3982 * Implemented in css_killed_work_fn().
3983 *
3984 * 3. When the percpu_ref reaches zero, the only possible remaining
3985 * accessors are inside RCU read sections. css_release() schedules the
3986 * RCU callback.
3987 *
3988 * 4. After the grace period, the css can be freed. Implemented in
3989 * css_free_work_fn().
3990 *
3991 * It is actually hairier because both step 2 and 4 require process context
3992 * and thus involve punting to css->destroy_work adding two additional
3993 * steps to the already complex sequence.
3994 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003995static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003996{
3997 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003998 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003999 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004000
Tejun Heo0ae78e02013-08-13 11:01:54 -04004001 if (css->parent)
4002 css_put(css->parent);
4003
Tejun Heo0c21ead2013-08-13 20:22:51 -04004004 css->ss->css_free(css);
4005 cgroup_dput(cgrp);
4006}
4007
4008static void css_free_rcu_fn(struct rcu_head *rcu_head)
4009{
4010 struct cgroup_subsys_state *css =
4011 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4012
4013 /*
4014 * css holds an extra ref to @cgrp->dentry which is put on the last
4015 * css_put(). dput() requires process context which we don't have.
4016 */
4017 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004018 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004019}
4020
Tejun Heod3daf282013-06-13 19:39:16 -07004021static void css_release(struct percpu_ref *ref)
4022{
4023 struct cgroup_subsys_state *css =
4024 container_of(ref, struct cgroup_subsys_state, refcnt);
4025
Tejun Heo0c21ead2013-08-13 20:22:51 -04004026 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004027}
4028
Tejun Heo623f9262013-08-13 11:01:55 -04004029static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4030 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004031{
Paul Menagebd89aab2007-10-18 23:40:44 -07004032 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004033 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004034 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004035
Tejun Heo0ae78e02013-08-13 11:01:54 -04004036 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004037 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004038 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004039 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004040
Tejun Heoca8bdca2013-08-26 18:40:56 -04004041 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042}
4043
Li Zefan2a4ac632013-07-31 16:16:40 +08004044/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004045static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004046{
Tejun Heo623f9262013-08-13 11:01:55 -04004047 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004048 int ret = 0;
4049
Tejun Heoa31f2d32012-11-19 08:13:37 -08004050 lockdep_assert_held(&cgroup_mutex);
4051
Tejun Heo92fb9742012-11-19 08:13:38 -08004052 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004053 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004054 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004055 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004056 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004057 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4058 }
Tejun Heob1929db2012-11-19 08:13:38 -08004059 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004060}
4061
Li Zefan2a4ac632013-07-31 16:16:40 +08004062/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004063static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004064{
Tejun Heo623f9262013-08-13 11:01:55 -04004065 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004066
4067 lockdep_assert_held(&cgroup_mutex);
4068
4069 if (!(css->flags & CSS_ONLINE))
4070 return;
4071
Li Zefand7eeac12013-03-12 15:35:59 -07004072 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004073 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004074
Tejun Heoeb954192013-08-08 20:11:23 -04004075 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004076 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004077 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004078}
4079
Tejun Heoc81c925a2013-12-06 15:11:56 -05004080/**
4081 * create_css - create a cgroup_subsys_state
4082 * @cgrp: the cgroup new css will be associated with
4083 * @ss: the subsys of new css
4084 *
4085 * Create a new css associated with @cgrp - @ss pair. On success, the new
4086 * css is online and installed in @cgrp with all interface files created.
4087 * Returns 0 on success, -errno on failure.
4088 */
4089static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4090{
4091 struct cgroup *parent = cgrp->parent;
4092 struct cgroup_subsys_state *css;
4093 int err;
4094
4095 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4096 lockdep_assert_held(&cgroup_mutex);
4097
4098 css = ss->css_alloc(cgroup_css(parent, ss));
4099 if (IS_ERR(css))
4100 return PTR_ERR(css);
4101
4102 err = percpu_ref_init(&css->refcnt, css_release);
4103 if (err)
4104 goto err_free;
4105
4106 init_css(css, ss, cgrp);
4107
4108 err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id);
4109 if (err)
4110 goto err_free;
4111
4112 err = online_css(css);
4113 if (err)
4114 goto err_free;
4115
4116 dget(cgrp->dentry);
4117 css_get(css->parent);
4118
4119 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4120 parent->parent) {
4121 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",
4122 current->comm, current->pid, ss->name);
4123 if (!strcmp(ss->name, "memory"))
4124 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4125 ss->warned_broken_hierarchy = true;
4126 }
4127
4128 return 0;
4129
4130err_free:
4131 percpu_ref_cancel_init(&css->refcnt);
4132 ss->css_free(css);
4133 return err;
4134}
4135
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136/*
Li Zefana043e3b2008-02-23 15:24:09 -08004137 * cgroup_create - create a cgroup
4138 * @parent: cgroup that will be parent of the new cgroup
4139 * @dentry: dentry of the new cgroup
4140 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004141 *
Li Zefana043e3b2008-02-23 15:24:09 -08004142 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004145 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004146{
Paul Menagebd89aab2007-10-18 23:40:44 -07004147 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004148 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004149 struct cgroupfs_root *root = parent->root;
Tejun Heob85d2042013-12-06 15:11:57 -05004150 int ssid, err = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 struct cgroup_subsys *ss;
4152 struct super_block *sb = root->sb;
4153
Tejun Heo0a950f62012-11-19 09:02:12 -08004154 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004155 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4156 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157 return -ENOMEM;
4158
Li Zefan65dff752013-03-01 15:01:56 +08004159 name = cgroup_alloc_name(dentry);
4160 if (!name)
4161 goto err_free_cgrp;
4162 rcu_assign_pointer(cgrp->name, name);
4163
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004164 /*
4165 * Temporarily set the pointer to NULL, so idr_find() won't return
4166 * a half-baked cgroup.
4167 */
4168 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004169 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004170 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004171
Tejun Heo976c06b2012-11-05 09:16:59 -08004172 /*
4173 * Only live parents can have children. Note that the liveliness
4174 * check isn't strictly necessary because cgroup_mkdir() and
4175 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4176 * anyway so that locking is contained inside cgroup proper and we
4177 * don't get nasty surprises if we ever grow another caller.
4178 */
4179 if (!cgroup_lock_live_group(parent)) {
4180 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004181 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004182 }
4183
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184 /* Grab a reference on the superblock so the hierarchy doesn't
4185 * get deleted on unmount if there are child cgroups. This
4186 * can be done outside cgroup_mutex, since the sb can't
4187 * disappear while someone has an open control file on the
4188 * fs */
4189 atomic_inc(&sb->s_active);
4190
Paul Menagecc31edc2008-10-18 20:28:04 -07004191 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192
Li Zefanfe1c06c2013-01-24 14:30:22 +08004193 dentry->d_fsdata = cgrp;
4194 cgrp->dentry = dentry;
4195
Paul Menagebd89aab2007-10-18 23:40:44 -07004196 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004197 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004198 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
Li Zefanb6abdb02008-03-04 14:28:19 -08004200 if (notify_on_release(parent))
4201 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4202
Tejun Heo2260e7f2012-11-19 08:13:38 -08004203 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4204 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004205
Tejun Heo4e139af2012-11-19 08:13:36 -08004206 /*
4207 * Create directory. cgroup_create_file() returns with the new
4208 * directory locked on success so that it can be populated without
4209 * dropping cgroup_mutex.
4210 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004211 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212 if (err < 0)
Tejun Heo0d802552013-12-06 15:11:56 -05004213 goto err_unlock;
Tejun Heo4e139af2012-11-19 08:13:36 -08004214 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215
Tejun Heo00356bd2013-06-18 11:14:22 -07004216 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004217
Tejun Heo4e139af2012-11-19 08:13:36 -08004218 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004219 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4220 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004221
Li Zefan415cf072013-04-08 14:35:02 +08004222 /* hold a ref to the parent's dentry */
4223 dget(parent->dentry);
4224
Tejun Heo0d802552013-12-06 15:11:56 -05004225 /*
4226 * @cgrp is now fully operational. If something fails after this
4227 * point, it'll be released via the normal destruction path.
4228 */
4229 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4230
4231 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
4232 if (err)
4233 goto err_destroy;
4234
Tejun Heo9d403e92013-12-06 15:11:56 -05004235 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004236 for_each_subsys(ss, ssid) {
4237 if (root->subsys_mask & (1 << ssid)) {
4238 err = create_css(cgrp, ss);
4239 if (err)
4240 goto err_destroy;
4241 }
Tejun Heoa8638032012-11-09 09:12:29 -08004242 }
4243
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004245 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246
4247 return 0;
4248
Tejun Heo0d802552013-12-06 15:11:56 -05004249err_unlock:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251 /* Release the reference count that we took on the superblock */
4252 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004253err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004254 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004255err_free_name:
4256 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004257err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004258 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004260
4261err_destroy:
4262 cgroup_destroy_locked(cgrp);
4263 mutex_unlock(&cgroup_mutex);
4264 mutex_unlock(&dentry->d_inode->i_mutex);
4265 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004266}
4267
Al Viro18bb1db2011-07-26 01:41:39 -04004268static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004269{
4270 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4271
4272 /* the vfs holds inode->i_mutex already */
4273 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4274}
4275
Tejun Heo223dbc32013-08-13 20:22:50 -04004276/*
4277 * This is called when the refcnt of a css is confirmed to be killed.
4278 * css_tryget() is now guaranteed to fail.
4279 */
4280static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004281{
Tejun Heo223dbc32013-08-13 20:22:50 -04004282 struct cgroup_subsys_state *css =
4283 container_of(work, struct cgroup_subsys_state, destroy_work);
4284 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004285
Tejun Heof20104d2013-08-13 20:22:50 -04004286 mutex_lock(&cgroup_mutex);
4287
4288 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004289 * css_tryget() is guaranteed to fail now. Tell subsystems to
4290 * initate destruction.
4291 */
4292 offline_css(css);
4293
4294 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004295 * If @cgrp is marked dead, it's waiting for refs of all css's to
4296 * be disabled before proceeding to the second phase of cgroup
4297 * destruction. If we are the last one, kick it off.
4298 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004299 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004300 cgroup_destroy_css_killed(cgrp);
4301
4302 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004303
4304 /*
4305 * Put the css refs from kill_css(). Each css holds an extra
4306 * reference to the cgroup's dentry and cgroup removal proceeds
4307 * regardless of css refs. On the last put of each css, whenever
4308 * that may be, the extra dentry ref is put so that dentry
4309 * destruction happens only after all css's are released.
4310 */
4311 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004312}
4313
Tejun Heo223dbc32013-08-13 20:22:50 -04004314/* css kill confirmation processing requires process context, bounce */
4315static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004316{
4317 struct cgroup_subsys_state *css =
4318 container_of(ref, struct cgroup_subsys_state, refcnt);
4319
Tejun Heo223dbc32013-08-13 20:22:50 -04004320 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004321 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004322}
4323
4324/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004325 * kill_css - destroy a css
4326 * @css: css to destroy
4327 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004328 * This function initiates destruction of @css by removing cgroup interface
4329 * files and putting its base reference. ->css_offline() will be invoked
4330 * asynchronously once css_tryget() is guaranteed to fail and when the
4331 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004332 */
4333static void kill_css(struct cgroup_subsys_state *css)
4334{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004335 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4336
Tejun Heoedae0c32013-08-13 20:22:51 -04004337 /*
4338 * Killing would put the base ref, but we need to keep it alive
4339 * until after ->css_offline().
4340 */
4341 css_get(css);
4342
4343 /*
4344 * cgroup core guarantees that, by the time ->css_offline() is
4345 * invoked, no new css reference will be given out via
4346 * css_tryget(). We can't simply call percpu_ref_kill() and
4347 * proceed to offlining css's because percpu_ref_kill() doesn't
4348 * guarantee that the ref is seen as killed on all CPUs on return.
4349 *
4350 * Use percpu_ref_kill_and_confirm() to get notifications as each
4351 * css is confirmed to be seen as killed on all CPUs.
4352 */
4353 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004354}
4355
4356/**
4357 * cgroup_destroy_locked - the first stage of cgroup destruction
4358 * @cgrp: cgroup to be destroyed
4359 *
4360 * css's make use of percpu refcnts whose killing latency shouldn't be
4361 * exposed to userland and are RCU protected. Also, cgroup core needs to
4362 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4363 * invoked. To satisfy all the requirements, destruction is implemented in
4364 * the following two steps.
4365 *
4366 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4367 * userland visible parts and start killing the percpu refcnts of
4368 * css's. Set up so that the next stage will be kicked off once all
4369 * the percpu refcnts are confirmed to be killed.
4370 *
4371 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4372 * rest of destruction. Once all cgroup references are gone, the
4373 * cgroup is RCU-freed.
4374 *
4375 * This function implements s1. After this step, @cgrp is gone as far as
4376 * the userland is concerned and a new cgroup with the same name may be
4377 * created. As cgroup doesn't care about the names internally, this
4378 * doesn't cause any problem.
4379 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004380static int cgroup_destroy_locked(struct cgroup *cgrp)
4381 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382{
Tejun Heo42809dd2012-11-19 08:13:37 -08004383 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004384 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004385 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004386 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004387 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388
Tejun Heo42809dd2012-11-19 08:13:37 -08004389 lockdep_assert_held(&d->d_inode->i_mutex);
4390 lockdep_assert_held(&cgroup_mutex);
4391
Tejun Heoddd69142013-06-12 21:04:54 -07004392 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004393 * css_set_lock synchronizes access to ->cset_links and prevents
4394 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004395 */
4396 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004397 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004398 read_unlock(&css_set_lock);
4399 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004401
Tejun Heo1a90dd52012-11-05 09:16:59 -08004402 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004403 * Make sure there's no live children. We can't test ->children
4404 * emptiness as dead children linger on it while being destroyed;
4405 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4406 */
4407 empty = true;
4408 rcu_read_lock();
4409 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4410 empty = cgroup_is_dead(child);
4411 if (!empty)
4412 break;
4413 }
4414 rcu_read_unlock();
4415 if (!empty)
4416 return -EBUSY;
4417
4418 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004419 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4420 * will be invoked to perform the rest of destruction once the
4421 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004422 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05004423 for_each_css(css, ssid, cgrp)
4424 kill_css(css);
Tejun Heo455050d2013-06-13 19:27:41 -07004425
4426 /*
4427 * Mark @cgrp dead. This prevents further task migration and child
4428 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004429 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004430 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004431 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004432 */
Tejun Heo54766d42013-06-12 21:04:53 -07004433 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004434
Tejun Heo455050d2013-06-13 19:27:41 -07004435 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4436 raw_spin_lock(&release_list_lock);
4437 if (!list_empty(&cgrp->release_list))
4438 list_del_init(&cgrp->release_list);
4439 raw_spin_unlock(&release_list_lock);
4440
4441 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004442 * If @cgrp has css's attached, the second stage of cgroup
4443 * destruction is kicked off from css_killed_work_fn() after the
4444 * refs of all attached css's are killed. If @cgrp doesn't have
4445 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004446 */
Tejun Heof20104d2013-08-13 20:22:50 -04004447 if (!cgrp->nr_css)
4448 cgroup_destroy_css_killed(cgrp);
4449
4450 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004451 * Clear the base files and remove @cgrp directory. The removal
4452 * puts the base ref but we aren't quite done with @cgrp yet, so
4453 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004454 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004455 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004456 dget(d);
4457 cgroup_d_remove_dir(d);
4458
Tejun Heoea15f8c2013-06-13 19:27:42 -07004459 return 0;
4460};
4461
Tejun Heod3daf282013-06-13 19:39:16 -07004462/**
Tejun Heof20104d2013-08-13 20:22:50 -04004463 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004464 * @work: cgroup->destroy_free_work
4465 *
4466 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004467 * destroyed after all css's are offlined and performs the rest of
4468 * destruction. This is the second step of destruction described in the
4469 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004470 */
Tejun Heof20104d2013-08-13 20:22:50 -04004471static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004472{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004473 struct cgroup *parent = cgrp->parent;
4474 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004475
Tejun Heof20104d2013-08-13 20:22:50 -04004476 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477
Paul Menage999cd8a2009-01-07 18:08:36 -08004478 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004479 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004480
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004481 /*
4482 * We should remove the cgroup object from idr before its grace
4483 * period starts, so we won't be looking up a cgroup while the
4484 * cgroup is being freed.
4485 */
4486 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4487 cgrp->id = -1;
4488
Paul Menageddbcc7e2007-10-18 23:39:30 -07004489 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004490
Paul Menagebd89aab2007-10-18 23:40:44 -07004491 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004492 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004493}
4494
Tejun Heo42809dd2012-11-19 08:13:37 -08004495static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4496{
4497 int ret;
4498
4499 mutex_lock(&cgroup_mutex);
4500 ret = cgroup_destroy_locked(dentry->d_fsdata);
4501 mutex_unlock(&cgroup_mutex);
4502
4503 return ret;
4504}
4505
Tejun Heo8e3f6542012-04-01 12:09:55 -07004506static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4507{
4508 INIT_LIST_HEAD(&ss->cftsets);
4509
4510 /*
4511 * base_cftset is embedded in subsys itself, no need to worry about
4512 * deregistration.
4513 */
4514 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004515 struct cftype *cft;
4516
4517 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4518 cft->ss = ss;
4519
Tejun Heo8e3f6542012-04-01 12:09:55 -07004520 ss->base_cftset.cfts = ss->base_cftypes;
4521 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4522 }
4523}
4524
Li Zefan06a11922008-04-29 01:00:07 -07004525static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004526{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004527 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004528
4529 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004530
Tejun Heo648bb562012-11-19 08:13:36 -08004531 mutex_lock(&cgroup_mutex);
4532
Tejun Heo8e3f6542012-04-01 12:09:55 -07004533 /* init base cftset */
4534 cgroup_init_cftsets(ss);
4535
Paul Menageddbcc7e2007-10-18 23:39:30 -07004536 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004537 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004538 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004539 /* We don't handle early failures gracefully */
4540 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004541 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004542
Li Zefane8d55fd2008-04-29 01:00:13 -07004543 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004544 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004545 * newly registered, all tasks and hence the
4546 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004547 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004548
4549 need_forkexit_callback |= ss->fork || ss->exit;
4550
Li Zefane8d55fd2008-04-29 01:00:13 -07004551 /* At system boot, before all subsystems have been
4552 * registered, no tasks have been forked, so we don't
4553 * need to invoke fork callbacks here. */
4554 BUG_ON(!list_empty(&init_task.tasks));
4555
Tejun Heoae7f1642013-08-13 20:22:50 -04004556 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004557
Tejun Heo648bb562012-11-19 08:13:36 -08004558 mutex_unlock(&cgroup_mutex);
4559
Ben Blume6a11052010-03-10 15:22:09 -08004560 /* this function shouldn't be used with modular subsystems, since they
4561 * need to register a subsys_id, among other things */
4562 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004563}
4564
4565/**
Ben Blume6a11052010-03-10 15:22:09 -08004566 * cgroup_load_subsys: load and register a modular subsystem at runtime
4567 * @ss: the subsystem to load
4568 *
4569 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004570 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004571 * up for use. If the subsystem is built-in anyway, work is delegated to the
4572 * simpler cgroup_init_subsys.
4573 */
4574int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4575{
Ben Blume6a11052010-03-10 15:22:09 -08004576 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004577 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004578 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004579 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004580 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004581
4582 /* check name and function validity */
4583 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004584 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004585 return -EINVAL;
4586
4587 /*
4588 * we don't support callbacks in modular subsystems. this check is
4589 * before the ss->module check for consistency; a subsystem that could
4590 * be a module should still have no callbacks even if the user isn't
4591 * compiling it as one.
4592 */
4593 if (ss->fork || ss->exit)
4594 return -EINVAL;
4595
4596 /*
4597 * an optionally modular subsystem is built-in: we want to do nothing,
4598 * since cgroup_init_subsys will have already taken care of it.
4599 */
4600 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004601 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004602 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004603 return 0;
4604 }
4605
Tejun Heo8e3f6542012-04-01 12:09:55 -07004606 /* init base cftset */
4607 cgroup_init_cftsets(ss);
4608
Ben Blume6a11052010-03-10 15:22:09 -08004609 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004610 mutex_lock(&cgroup_root_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004611 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004612
4613 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004614 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004615 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004616 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004617 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004618 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004619 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004620 /* failure case - need to deassign the cgroup_subsys[] slot. */
4621 cgroup_subsys[ss->subsys_id] = NULL;
Wei Yongjun0be86692013-12-09 20:38:29 +08004622 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004623 mutex_unlock(&cgroup_mutex);
4624 return PTR_ERR(css);
4625 }
4626
Tejun Heo9871bf92013-06-24 15:21:47 -07004627 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004628
4629 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004630 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004631
4632 /*
4633 * Now we need to entangle the css into the existing css_sets. unlike
4634 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4635 * will need a new pointer to it; done by iterating the css_set_table.
4636 * furthermore, modifying the existing css_sets will corrupt the hash
4637 * table state, so each changed css_set will need its hash recomputed.
4638 * this is all done under the css_set_lock.
4639 */
4640 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004641 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004642 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004643 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004644 continue;
4645 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004646 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004647 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004648 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004649 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004650 key = css_set_hash(cset->subsys);
4651 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004652 }
4653 write_unlock(&css_set_lock);
4654
Tejun Heoae7f1642013-08-13 20:22:50 -04004655 ret = online_css(css);
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004656 if (ret) {
4657 ss->css_free(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004658 goto err_unload;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004659 }
Tejun Heoa8638032012-11-09 09:12:29 -08004660
Ben Blume6a11052010-03-10 15:22:09 -08004661 /* success! */
Tejun Heo780cd8b2013-12-06 15:11:56 -05004662 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004663 mutex_unlock(&cgroup_mutex);
4664 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004665
4666err_unload:
Tejun Heo780cd8b2013-12-06 15:11:56 -05004667 mutex_unlock(&cgroup_root_mutex);
Tejun Heod19e19d2012-11-19 08:13:37 -08004668 mutex_unlock(&cgroup_mutex);
4669 /* @ss can't be mounted here as try_module_get() would fail */
4670 cgroup_unload_subsys(ss);
4671 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004672}
4673EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4674
4675/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004676 * cgroup_unload_subsys: unload a modular subsystem
4677 * @ss: the subsystem to unload
4678 *
4679 * This function should be called in a modular subsystem's exitcall. When this
4680 * function is invoked, the refcount on the subsystem's module will be 0, so
4681 * the subsystem will not be attached to any hierarchy.
4682 */
4683void cgroup_unload_subsys(struct cgroup_subsys *ss)
4684{
Tejun Heo69d02062013-06-12 21:04:50 -07004685 struct cgrp_cset_link *link;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004686 struct cgroup_subsys_state *css;
Ben Blumcf5d5942010-03-10 15:22:09 -08004687
4688 BUG_ON(ss->module == NULL);
4689
4690 /*
4691 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004692 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004693 * doesn't start being used while we're killing it off.
4694 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004695 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004696
4697 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004698 mutex_lock(&cgroup_root_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004699
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004700 css = cgroup_css(cgroup_dummy_top, ss);
4701 if (css)
4702 offline_css(css);
Tejun Heo02ae7482012-11-19 08:13:37 -08004703
Ben Blumcf5d5942010-03-10 15:22:09 -08004704 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004705 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004706
Ben Blumcf5d5942010-03-10 15:22:09 -08004707 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004708 * disentangle the css from all css_sets attached to the dummy
4709 * top. as in loading, we need to pay our respects to the hashtable
4710 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004711 */
4712 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004713 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004714 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004715 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004716
Tejun Heo5abb8852013-06-12 21:04:49 -07004717 hash_del(&cset->hlist);
4718 cset->subsys[ss->subsys_id] = NULL;
4719 key = css_set_hash(cset->subsys);
4720 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004721 }
4722 write_unlock(&css_set_lock);
4723
4724 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004725 * remove subsystem's css from the cgroup_dummy_top and free it -
4726 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004727 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004728 */
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004729 if (css)
4730 ss->css_free(css);
Tejun Heo73e80ed2013-08-13 11:01:55 -04004731 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004732
Tejun Heo780cd8b2013-12-06 15:11:56 -05004733 mutex_unlock(&cgroup_root_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08004734 mutex_unlock(&cgroup_mutex);
4735}
4736EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4737
4738/**
Li Zefana043e3b2008-02-23 15:24:09 -08004739 * cgroup_init_early - cgroup initialization at system boot
4740 *
4741 * Initialize cgroups at system boot, and initialize any
4742 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743 */
4744int __init cgroup_init_early(void)
4745{
Tejun Heo30159ec2013-06-25 11:53:37 -07004746 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004747 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004748
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004749 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004750 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004751 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004752 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004753 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004754 init_cgroup_root(&cgroup_dummy_root);
4755 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004756 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004757
Tejun Heo69d02062013-06-12 21:04:50 -07004758 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004759 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4760 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004761 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004762
Tejun Heo30159ec2013-06-25 11:53:37 -07004763 /* at bootup time, we don't worry about modular subsystems */
4764 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765 BUG_ON(!ss->name);
4766 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004767 BUG_ON(!ss->css_alloc);
4768 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004770 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771 ss->name, ss->subsys_id);
4772 BUG();
4773 }
4774
4775 if (ss->early_init)
4776 cgroup_init_subsys(ss);
4777 }
4778 return 0;
4779}
4780
4781/**
Li Zefana043e3b2008-02-23 15:24:09 -08004782 * cgroup_init - cgroup initialization
4783 *
4784 * Register cgroup filesystem and /proc file, and initialize
4785 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004786 */
4787int __init cgroup_init(void)
4788{
Tejun Heo30159ec2013-06-25 11:53:37 -07004789 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004790 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004791 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004792
4793 err = bdi_init(&cgroup_backing_dev_info);
4794 if (err)
4795 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004796
Tejun Heo30159ec2013-06-25 11:53:37 -07004797 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004798 if (!ss->early_init)
4799 cgroup_init_subsys(ss);
4800 }
4801
Tejun Heofa3ca072013-04-14 11:36:56 -07004802 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004803 mutex_lock(&cgroup_mutex);
4804 mutex_lock(&cgroup_root_mutex);
4805
Tejun Heo82fe9b02013-06-25 11:53:37 -07004806 /* Add init_css_set to the hash table */
4807 key = css_set_hash(init_css_set.subsys);
4808 hash_add(css_set_table, &init_css_set.hlist, key);
4809
Tejun Heofc76df72013-06-25 11:53:37 -07004810 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004811
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004812 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4813 0, 1, GFP_KERNEL);
4814 BUG_ON(err < 0);
4815
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004816 mutex_unlock(&cgroup_root_mutex);
4817 mutex_unlock(&cgroup_mutex);
4818
Greg KH676db4a2010-08-05 13:53:35 -07004819 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4820 if (!cgroup_kobj) {
4821 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004822 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004823 }
4824
4825 err = register_filesystem(&cgroup_fs_type);
4826 if (err < 0) {
4827 kobject_put(cgroup_kobj);
4828 goto out;
4829 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830
Li Zefan46ae2202008-04-29 01:00:08 -07004831 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004832
Paul Menageddbcc7e2007-10-18 23:39:30 -07004833out:
Paul Menagea4243162007-10-18 23:39:35 -07004834 if (err)
4835 bdi_destroy(&cgroup_backing_dev_info);
4836
Paul Menageddbcc7e2007-10-18 23:39:30 -07004837 return err;
4838}
Paul Menageb4f48b62007-10-18 23:39:33 -07004839
Tejun Heoe5fca242013-11-22 17:14:39 -05004840static int __init cgroup_wq_init(void)
4841{
4842 /*
4843 * There isn't much point in executing destruction path in
4844 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4845 * Use 1 for @max_active.
4846 *
4847 * We would prefer to do this in cgroup_init() above, but that
4848 * is called before init_workqueues(): so leave this until after.
4849 */
4850 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4851 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004852
4853 /*
4854 * Used to destroy pidlists and separate to serve as flush domain.
4855 * Cap @max_active to 1 too.
4856 */
4857 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4858 0, 1);
4859 BUG_ON(!cgroup_pidlist_destroy_wq);
4860
Tejun Heoe5fca242013-11-22 17:14:39 -05004861 return 0;
4862}
4863core_initcall(cgroup_wq_init);
4864
Paul Menagea4243162007-10-18 23:39:35 -07004865/*
4866 * proc_cgroup_show()
4867 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4868 * - Used for /proc/<pid>/cgroup.
4869 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4870 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004871 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004872 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4873 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4874 * cgroup to top_cgroup.
4875 */
4876
4877/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004878int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004879{
4880 struct pid *pid;
4881 struct task_struct *tsk;
4882 char *buf;
4883 int retval;
4884 struct cgroupfs_root *root;
4885
4886 retval = -ENOMEM;
4887 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4888 if (!buf)
4889 goto out;
4890
4891 retval = -ESRCH;
4892 pid = m->private;
4893 tsk = get_pid_task(pid, PIDTYPE_PID);
4894 if (!tsk)
4895 goto out_free;
4896
4897 retval = 0;
4898
4899 mutex_lock(&cgroup_mutex);
4900
Li Zefane5f6a862009-01-07 18:07:41 -08004901 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004902 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004903 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004904 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004905
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004906 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004907 for_each_subsys(ss, ssid)
4908 if (root->subsys_mask & (1 << ssid))
4909 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004910 if (strlen(root->name))
4911 seq_printf(m, "%sname=%s", count ? "," : "",
4912 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004913 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004914 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004915 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004916 if (retval < 0)
4917 goto out_unlock;
4918 seq_puts(m, buf);
4919 seq_putc(m, '\n');
4920 }
4921
4922out_unlock:
4923 mutex_unlock(&cgroup_mutex);
4924 put_task_struct(tsk);
4925out_free:
4926 kfree(buf);
4927out:
4928 return retval;
4929}
4930
Paul Menagea4243162007-10-18 23:39:35 -07004931/* Display information about each subsystem and each hierarchy */
4932static int proc_cgroupstats_show(struct seq_file *m, void *v)
4933{
Tejun Heo30159ec2013-06-25 11:53:37 -07004934 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004935 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004936
Paul Menage8bab8dd2008-04-04 14:29:57 -07004937 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004938 /*
4939 * ideally we don't want subsystems moving around while we do this.
4940 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4941 * subsys/hierarchy state.
4942 */
Paul Menagea4243162007-10-18 23:39:35 -07004943 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004944
4945 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004946 seq_printf(m, "%s\t%d\t%d\t%d\n",
4947 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004948 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004949
Paul Menagea4243162007-10-18 23:39:35 -07004950 mutex_unlock(&cgroup_mutex);
4951 return 0;
4952}
4953
4954static int cgroupstats_open(struct inode *inode, struct file *file)
4955{
Al Viro9dce07f2008-03-29 03:07:28 +00004956 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004957}
4958
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004959static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004960 .open = cgroupstats_open,
4961 .read = seq_read,
4962 .llseek = seq_lseek,
4963 .release = single_release,
4964};
4965
Paul Menageb4f48b62007-10-18 23:39:33 -07004966/**
4967 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004968 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004969 *
4970 * Description: A task inherits its parent's cgroup at fork().
4971 *
4972 * A pointer to the shared css_set was automatically copied in
4973 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004974 * it was not made under the protection of RCU or cgroup_mutex, so
4975 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4976 * have already changed current->cgroups, allowing the previously
4977 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004978 *
4979 * At the point that cgroup_fork() is called, 'current' is the parent
4980 * task, and the passed argument 'child' points to the child task.
4981 */
4982void cgroup_fork(struct task_struct *child)
4983{
Tejun Heo9bb71302012-10-18 17:52:07 -07004984 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004985 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004986 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004987 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004988 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004989}
4990
4991/**
Li Zefana043e3b2008-02-23 15:24:09 -08004992 * cgroup_post_fork - called on a new task after adding it to the task list
4993 * @child: the task in question
4994 *
Tejun Heo5edee612012-10-16 15:03:14 -07004995 * Adds the task to the list running through its css_set if necessary and
4996 * call the subsystem fork() callbacks. Has to be after the task is
4997 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004998 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004999 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005000 */
Paul Menage817929e2007-10-18 23:39:36 -07005001void cgroup_post_fork(struct task_struct *child)
5002{
Tejun Heo30159ec2013-06-25 11:53:37 -07005003 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005004 int i;
5005
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005006 /*
5007 * use_task_css_set_links is set to 1 before we walk the tasklist
5008 * under the tasklist_lock and we read it here after we added the child
5009 * to the tasklist under the tasklist_lock as well. If the child wasn't
5010 * yet in the tasklist when we walked through it from
5011 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5012 * should be visible now due to the paired locking and barriers implied
5013 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5014 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5015 * lock on fork.
5016 */
Paul Menage817929e2007-10-18 23:39:36 -07005017 if (use_task_css_set_links) {
5018 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005019 task_lock(child);
5020 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005021 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005022 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005023 write_unlock(&css_set_lock);
5024 }
Tejun Heo5edee612012-10-16 15:03:14 -07005025
5026 /*
5027 * Call ss->fork(). This must happen after @child is linked on
5028 * css_set; otherwise, @child might change state between ->fork()
5029 * and addition to css_set.
5030 */
5031 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005032 /*
5033 * fork/exit callbacks are supported only for builtin
5034 * subsystems, and the builtin section of the subsys
5035 * array is immutable, so we don't need to lock the
5036 * subsys array here. On the other hand, modular section
5037 * of the array can be freed at module unload, so we
5038 * can't touch that.
5039 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005040 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005041 if (ss->fork)
5042 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005043 }
Paul Menage817929e2007-10-18 23:39:36 -07005044}
Tejun Heo5edee612012-10-16 15:03:14 -07005045
Paul Menage817929e2007-10-18 23:39:36 -07005046/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005047 * cgroup_exit - detach cgroup from exiting task
5048 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005049 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005050 *
5051 * Description: Detach cgroup from @tsk and release it.
5052 *
5053 * Note that cgroups marked notify_on_release force every task in
5054 * them to take the global cgroup_mutex mutex when exiting.
5055 * This could impact scaling on very large systems. Be reluctant to
5056 * use notify_on_release cgroups where very high task exit scaling
5057 * is required on large systems.
5058 *
5059 * the_top_cgroup_hack:
5060 *
5061 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5062 *
5063 * We call cgroup_exit() while the task is still competent to
5064 * handle notify_on_release(), then leave the task attached to the
5065 * root cgroup in each hierarchy for the remainder of its exit.
5066 *
5067 * To do this properly, we would increment the reference count on
5068 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5069 * code we would add a second cgroup function call, to drop that
5070 * reference. This would just create an unnecessary hot spot on
5071 * the top_cgroup reference count, to no avail.
5072 *
5073 * Normally, holding a reference to a cgroup without bumping its
5074 * count is unsafe. The cgroup could go away, or someone could
5075 * attach us to a different cgroup, decrementing the count on
5076 * the first cgroup that we never incremented. But in this case,
5077 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005078 * which wards off any cgroup_attach_task() attempts, or task is a failed
5079 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005080 */
5081void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5082{
Tejun Heo30159ec2013-06-25 11:53:37 -07005083 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005084 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005085 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005086
5087 /*
5088 * Unlink from the css_set task list if necessary.
5089 * Optimistically check cg_list before taking
5090 * css_set_lock
5091 */
5092 if (!list_empty(&tsk->cg_list)) {
5093 write_lock(&css_set_lock);
5094 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005095 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005096 write_unlock(&css_set_lock);
5097 }
5098
Paul Menageb4f48b62007-10-18 23:39:33 -07005099 /* Reassign the task to the init_css_set. */
5100 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005101 cset = task_css_set(tsk);
5102 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005103
5104 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005105 /*
5106 * fork/exit callbacks are supported only for builtin
5107 * subsystems, see cgroup_post_fork() for details.
5108 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005109 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005110 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005111 struct cgroup_subsys_state *old_css = cset->subsys[i];
5112 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005113
Tejun Heoeb954192013-08-08 20:11:23 -04005114 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005115 }
5116 }
5117 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005118 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005119
Tejun Heo5abb8852013-06-12 21:04:49 -07005120 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005121}
Paul Menage697f4162007-10-18 23:39:34 -07005122
Paul Menagebd89aab2007-10-18 23:40:44 -07005123static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005124{
Li Zefanf50daa72013-03-01 15:06:07 +08005125 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005126 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005127 /*
5128 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005129 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005130 * it now
5131 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005132 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005133
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005134 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005135 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005136 list_empty(&cgrp->release_list)) {
5137 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005138 need_schedule_work = 1;
5139 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005140 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005141 if (need_schedule_work)
5142 schedule_work(&release_agent_work);
5143 }
5144}
5145
Paul Menage81a6a5c2007-10-18 23:39:38 -07005146/*
5147 * Notify userspace when a cgroup is released, by running the
5148 * configured release agent with the name of the cgroup (path
5149 * relative to the root of cgroup file system) as the argument.
5150 *
5151 * Most likely, this user command will try to rmdir this cgroup.
5152 *
5153 * This races with the possibility that some other task will be
5154 * attached to this cgroup before it is removed, or that some other
5155 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5156 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5157 * unused, and this cgroup will be reprieved from its death sentence,
5158 * to continue to serve a useful existence. Next time it's released,
5159 * we will get notified again, if it still has 'notify_on_release' set.
5160 *
5161 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5162 * means only wait until the task is successfully execve()'d. The
5163 * separate release agent task is forked by call_usermodehelper(),
5164 * then control in this thread returns here, without waiting for the
5165 * release agent task. We don't bother to wait because the caller of
5166 * this routine has no use for the exit status of the release agent
5167 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005168 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005169static void cgroup_release_agent(struct work_struct *work)
5170{
5171 BUG_ON(work != &release_agent_work);
5172 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005173 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005174 while (!list_empty(&release_list)) {
5175 char *argv[3], *envp[3];
5176 int i;
Paul Menagee788e0662008-07-25 01:46:59 -07005177 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005178 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005179 struct cgroup,
5180 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005181 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005182 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005183 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e0662008-07-25 01:46:59 -07005184 if (!pathbuf)
5185 goto continue_free;
5186 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5187 goto continue_free;
5188 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5189 if (!agentbuf)
5190 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005191
5192 i = 0;
Paul Menagee788e0662008-07-25 01:46:59 -07005193 argv[i++] = agentbuf;
5194 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005195 argv[i] = NULL;
5196
5197 i = 0;
5198 /* minimal command environment */
5199 envp[i++] = "HOME=/";
5200 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5201 envp[i] = NULL;
5202
5203 /* Drop the lock while we invoke the usermode helper,
5204 * since the exec could involve hitting disk and hence
5205 * be a slow process */
5206 mutex_unlock(&cgroup_mutex);
5207 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005208 mutex_lock(&cgroup_mutex);
Paul Menagee788e0662008-07-25 01:46:59 -07005209 continue_free:
5210 kfree(pathbuf);
5211 kfree(agentbuf);
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005212 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005213 }
Thomas Gleixnercdcc136f2009-07-25 16:47:45 +02005214 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005215 mutex_unlock(&cgroup_mutex);
5216}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005217
5218static int __init cgroup_disable(char *str)
5219{
Tejun Heo30159ec2013-06-25 11:53:37 -07005220 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005221 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005222 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005223
5224 while ((token = strsep(&str, ",")) != NULL) {
5225 if (!*token)
5226 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005227
Tejun Heo30159ec2013-06-25 11:53:37 -07005228 /*
5229 * cgroup_disable, being at boot time, can't know about
5230 * module subsystems, so we don't worry about them.
5231 */
5232 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005233 if (!strcmp(token, ss->name)) {
5234 ss->disabled = 1;
5235 printk(KERN_INFO "Disabling %s control group"
5236 " subsystem\n", ss->name);
5237 break;
5238 }
5239 }
5240 }
5241 return 1;
5242}
5243__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005244
Tejun Heob77d7b62013-08-13 11:01:54 -04005245/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005246 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5247 * @dentry: directory dentry of interest
5248 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005249 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05005250 * Must be called under cgroup_mutex or RCU read lock. The caller is
5251 * responsible for pinning the returned css if it needs to be accessed
5252 * outside the critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005253 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005254struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5255 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005256{
5257 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005258
Tejun Heo87fb54f2013-12-06 15:11:55 -05005259 cgroup_assert_mutex_or_rcu_locked();
Tejun Heob77d7b62013-08-13 11:01:54 -04005260
Tejun Heo35cf0832013-08-26 18:40:56 -04005261 /* is @dentry a cgroup dir? */
5262 if (!dentry->d_inode ||
5263 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005264 return ERR_PTR(-EBADF);
5265
Tejun Heo35cf0832013-08-26 18:40:56 -04005266 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005267 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005268}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005269
Li Zefan1cb650b2013-08-19 10:05:24 +08005270/**
5271 * css_from_id - lookup css by id
5272 * @id: the cgroup id
5273 * @ss: cgroup subsys to be looked into
5274 *
5275 * Returns the css if there's valid one with @id, otherwise returns NULL.
5276 * Should be called under rcu_read_lock().
5277 */
5278struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5279{
5280 struct cgroup *cgrp;
5281
Tejun Heo87fb54f2013-12-06 15:11:55 -05005282 cgroup_assert_mutex_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005283
5284 cgrp = idr_find(&ss->root->cgroup_idr, id);
5285 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005286 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005287 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005288}
5289
Paul Menagefe693432009-09-23 15:56:20 -07005290#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005291static struct cgroup_subsys_state *
5292debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005293{
5294 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5295
5296 if (!css)
5297 return ERR_PTR(-ENOMEM);
5298
5299 return css;
5300}
5301
Tejun Heoeb954192013-08-08 20:11:23 -04005302static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005303{
Tejun Heoeb954192013-08-08 20:11:23 -04005304 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005305}
5306
Tejun Heo182446d2013-08-08 20:11:24 -04005307static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5308 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005309{
Tejun Heo182446d2013-08-08 20:11:24 -04005310 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005311}
5312
Tejun Heo182446d2013-08-08 20:11:24 -04005313static u64 current_css_set_read(struct cgroup_subsys_state *css,
5314 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005315{
5316 return (u64)(unsigned long)current->cgroups;
5317}
5318
Tejun Heo182446d2013-08-08 20:11:24 -04005319static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005320 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005321{
5322 u64 count;
5323
5324 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005325 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005326 rcu_read_unlock();
5327 return count;
5328}
5329
Tejun Heo2da8ca82013-12-05 12:28:04 -05005330static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005331{
Tejun Heo69d02062013-06-12 21:04:50 -07005332 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005333 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005334
5335 read_lock(&css_set_lock);
5336 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005337 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005338 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005339 struct cgroup *c = link->cgrp;
5340 const char *name;
5341
5342 if (c->dentry)
5343 name = c->dentry->d_name.name;
5344 else
5345 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005346 seq_printf(seq, "Root %d group %s\n",
5347 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005348 }
5349 rcu_read_unlock();
5350 read_unlock(&css_set_lock);
5351 return 0;
5352}
5353
5354#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005355static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005356{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005357 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005358 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005359
5360 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005361 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005362 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005363 struct task_struct *task;
5364 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005365 seq_printf(seq, "css_set %p\n", cset);
5366 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005367 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5368 seq_puts(seq, " ...\n");
5369 break;
5370 } else {
5371 seq_printf(seq, " task %d\n",
5372 task_pid_vnr(task));
5373 }
5374 }
5375 }
5376 read_unlock(&css_set_lock);
5377 return 0;
5378}
5379
Tejun Heo182446d2013-08-08 20:11:24 -04005380static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005381{
Tejun Heo182446d2013-08-08 20:11:24 -04005382 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005383}
5384
5385static struct cftype debug_files[] = {
5386 {
Paul Menagefe693432009-09-23 15:56:20 -07005387 .name = "taskcount",
5388 .read_u64 = debug_taskcount_read,
5389 },
5390
5391 {
5392 .name = "current_css_set",
5393 .read_u64 = current_css_set_read,
5394 },
5395
5396 {
5397 .name = "current_css_set_refcount",
5398 .read_u64 = current_css_set_refcount_read,
5399 },
5400
5401 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005402 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005403 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005404 },
5405
5406 {
5407 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005408 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005409 },
5410
5411 {
Paul Menagefe693432009-09-23 15:56:20 -07005412 .name = "releasable",
5413 .read_u64 = releasable_read,
5414 },
Paul Menagefe693432009-09-23 15:56:20 -07005415
Tejun Heo4baf6e32012-04-01 12:09:55 -07005416 { } /* terminate */
5417};
Paul Menagefe693432009-09-23 15:56:20 -07005418
5419struct cgroup_subsys debug_subsys = {
5420 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005421 .css_alloc = debug_css_alloc,
5422 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005423 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005424 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005425};
5426#endif /* CONFIG_CGROUP_DEBUG */