blob: 105f273b6f86a74984d2b62b7d3ba19f95369a78 [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
Tejun Heo30159ec2013-06-25 11:53:37 -0700250 *
251 * Should be called under cgroup_mutex.
252 */
Tejun Heo1c6727a2013-12-06 15:11:56 -0500253#define for_each_css(css, ssid, cgrp) \
254 for ((ssid) = 0; (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
255 if (!((css) = rcu_dereference_check( \
256 (cgrp)->subsys[(ssid)], \
257 lockdep_is_held(&cgroup_mutex)))) { } \
258 else
259
260/**
Tejun Heo30159ec2013-06-25 11:53:37 -0700261 * for_each_subsys - iterate all loaded cgroup subsystems
262 * @ss: the iteration cursor
Tejun Heo780cd8b2013-12-06 15:11:56 -0500263 * @ssid: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
Tejun Heo30159ec2013-06-25 11:53:37 -0700264 *
Tejun Heo780cd8b2013-12-06 15:11:56 -0500265 * Iterates through all loaded subsystems. Should be called under
266 * cgroup_mutex or cgroup_root_mutex.
Tejun Heo30159ec2013-06-25 11:53:37 -0700267 */
Tejun Heo780cd8b2013-12-06 15:11:56 -0500268#define for_each_subsys(ss, ssid) \
269 for (({ cgroup_assert_mutex_or_root_locked(); (ssid) = 0; }); \
270 (ssid) < CGROUP_SUBSYS_COUNT; (ssid)++) \
271 if (!((ss) = cgroup_subsys[(ssid)])) { } \
Tejun Heo30159ec2013-06-25 11:53:37 -0700272 else
273
274/**
275 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
276 * @ss: the iteration cursor
277 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
278 *
279 * Bulit-in subsystems are always present and iteration itself doesn't
280 * require any synchronization.
281 */
282#define for_each_builtin_subsys(ss, i) \
283 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
284 (((ss) = cgroup_subsys[i]) || true); (i)++)
285
Tejun Heo5549c492013-06-24 15:21:48 -0700286/* iterate across the active hierarchies */
287#define for_each_active_root(root) \
288 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700289
Tejun Heof6ea9372012-04-01 12:09:55 -0700290static inline struct cgroup *__d_cgrp(struct dentry *dentry)
291{
292 return dentry->d_fsdata;
293}
294
Tejun Heo05ef1d72012-04-01 12:09:56 -0700295static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700296{
297 return dentry->d_fsdata;
298}
299
Tejun Heo05ef1d72012-04-01 12:09:56 -0700300static inline struct cftype *__d_cft(struct dentry *dentry)
301{
302 return __d_cfe(dentry)->type;
303}
304
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305/**
306 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
307 * @cgrp: the cgroup to be checked for liveness
308 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700309 * On success, returns true; the mutex should be later unlocked. On
310 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700311 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700312static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700313{
314 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700315 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700316 mutex_unlock(&cgroup_mutex);
317 return false;
318 }
319 return true;
320}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700321
Paul Menage81a6a5c2007-10-18 23:39:38 -0700322/* the list of cgroups eligible for automatic release. Protected by
323 * release_list_lock */
324static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200325static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700326static void cgroup_release_agent(struct work_struct *work);
327static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700328static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329
Tejun Heo69d02062013-06-12 21:04:50 -0700330/*
331 * A cgroup can be associated with multiple css_sets as different tasks may
332 * belong to different cgroups on different hierarchies. In the other
333 * direction, a css_set is naturally associated with multiple cgroups.
334 * This M:N relationship is represented by the following link structure
335 * which exists for each association and allows traversing the associations
336 * from both sides.
337 */
338struct cgrp_cset_link {
339 /* the cgroup and css_set this link associates */
340 struct cgroup *cgrp;
341 struct css_set *cset;
342
343 /* list of cgrp_cset_links anchored at cgrp->cset_links */
344 struct list_head cset_link;
345
346 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
347 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700348};
349
350/* The default css_set - used by init and its children prior to any
351 * hierarchies being mounted. It contains a pointer to the root state
352 * for each subsystem. Also used to anchor the list of css_sets. Not
353 * reference-counted, to improve performance when child cgroups
354 * haven't been created.
355 */
356
357static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700358static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700359
Tejun Heo0942eee2013-08-08 20:11:26 -0400360/*
361 * css_set_lock protects the list of css_set objects, and the chain of
362 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400363 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400364 */
Paul Menage817929e2007-10-18 23:39:36 -0700365static DEFINE_RWLOCK(css_set_lock);
366static int css_set_count;
367
Paul Menage7717f7b2009-09-23 15:56:22 -0700368/*
369 * hash table for cgroup groups. This improves the performance to find
370 * an existing css_set. This hash doesn't (currently) take into
371 * account cgroups in empty hierarchies.
372 */
Li Zefan472b1052008-04-29 01:00:11 -0700373#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800374static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700375
Li Zefan0ac801f2013-01-10 11:49:27 +0800376static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700377{
Li Zefan0ac801f2013-01-10 11:49:27 +0800378 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700379 struct cgroup_subsys *ss;
380 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700381
Tejun Heo30159ec2013-06-25 11:53:37 -0700382 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800383 key += (unsigned long)css[i];
384 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700385
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700387}
388
Tejun Heo0942eee2013-08-08 20:11:26 -0400389/*
390 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400391 * until after the first call to css_task_iter_start(). This reduces the
392 * fork()/exit() overhead for people who have cgroups compiled into their
393 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400394 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700395static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700396
Tejun Heo5abb8852013-06-12 21:04:49 -0700397static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700398{
Tejun Heo69d02062013-06-12 21:04:50 -0700399 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700400
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700401 /*
402 * Ensure that the refcount doesn't hit zero while any readers
403 * can see it. Similar to atomic_dec_and_lock(), but for an
404 * rwlock
405 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700406 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 return;
408 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700409 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700410 write_unlock(&css_set_lock);
411 return;
412 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700416 css_set_count--;
417
Tejun Heo69d02062013-06-12 21:04:50 -0700418 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700420
Tejun Heo69d02062013-06-12 21:04:50 -0700421 list_del(&link->cset_link);
422 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800423
Tejun Heoddd69142013-06-12 21:04:54 -0700424 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700425 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700427 set_bit(CGRP_RELEASABLE, &cgrp->flags);
428 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700429 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700430
431 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700433
434 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700435 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700436}
437
438/*
439 * refcounted get/put for css_set objects
440 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700441static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700442{
Tejun Heo5abb8852013-06-12 21:04:49 -0700443 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700444}
445
Tejun Heo5abb8852013-06-12 21:04:49 -0700446static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700447{
Tejun Heo5abb8852013-06-12 21:04:49 -0700448 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700449}
450
Tejun Heo5abb8852013-06-12 21:04:49 -0700451static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452{
Tejun Heo5abb8852013-06-12 21:04:49 -0700453 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700454}
455
Tejun Heob326f9d2013-06-24 15:21:48 -0700456/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700457 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700458 * @cset: candidate css_set being tested
459 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700460 * @new_cgrp: cgroup that's being entered by the task
461 * @template: desired set of css pointers in css_set (pre-calculated)
462 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800463 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
465 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700466static bool compare_css_sets(struct css_set *cset,
467 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700468 struct cgroup *new_cgrp,
469 struct cgroup_subsys_state *template[])
470{
471 struct list_head *l1, *l2;
472
Tejun Heo5abb8852013-06-12 21:04:49 -0700473 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 /* Not all subsystems matched */
475 return false;
476 }
477
478 /*
479 * Compare cgroup pointers in order to distinguish between
480 * different cgroups in heirarchies with no subsystems. We
481 * could get by with just this check alone (and skip the
482 * memcmp above) but on most setups the memcmp check will
483 * avoid the need for this more expensive check on almost all
484 * candidates.
485 */
486
Tejun Heo69d02062013-06-12 21:04:50 -0700487 l1 = &cset->cgrp_links;
488 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700490 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700491 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700492
493 l1 = l1->next;
494 l2 = l2->next;
495 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700496 if (l1 == &cset->cgrp_links) {
497 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700498 break;
499 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700500 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 }
502 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700503 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
504 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
505 cgrp1 = link1->cgrp;
506 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700508 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700509
510 /*
511 * If this hierarchy is the hierarchy of the cgroup
512 * that's changing, then we need to check that this
513 * css_set points to the new cgroup; if it's any other
514 * hierarchy, then this css_set should point to the
515 * same cgroup as the old css_set.
516 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700517 if (cgrp1->root == new_cgrp->root) {
518 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700519 return false;
520 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700521 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700522 return false;
523 }
524 }
525 return true;
526}
527
Tejun Heob326f9d2013-06-24 15:21:48 -0700528/**
529 * find_existing_css_set - init css array and find the matching css_set
530 * @old_cset: the css_set that we're using before the cgroup transition
531 * @cgrp: the cgroup that we're moving into
532 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700533 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700534static struct css_set *find_existing_css_set(struct css_set *old_cset,
535 struct cgroup *cgrp,
536 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700537{
Paul Menagebd89aab2007-10-18 23:40:44 -0700538 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700539 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800541 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700542 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700543
Ben Blumaae8aab2010-03-10 15:22:07 -0800544 /*
545 * Build the set of subsystem state objects that we want to see in the
546 * new css_set. while subsystems can change globally, the entries here
547 * won't change, so no need for locking.
548 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700549 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-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 Zefanc1a71502013-12-17 11:13:39 +0800882
883 /*
884 * XXX: cgrp->id is only used to look up css's. As cgroup
885 * and css's lifetimes will be decoupled, it should be made
886 * per-subsystem and moved to css->id so that lookups are
887 * successful until the target css is released.
888 */
Li Zefan0ab02ca2014-02-11 16:05:46 +0800889 mutex_lock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800890 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +0800891 mutex_unlock(&cgroup_mutex);
Li Zefanc1a71502013-12-17 11:13:39 +0800892 cgrp->id = -1;
893
Li Zefanbe445622013-01-24 14:31:42 +0800894 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700895 } else {
896 struct cfent *cfe = __d_cfe(dentry);
897 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
898
899 WARN_ONCE(!list_empty(&cfe->node) &&
900 cgrp != &cgrp->root->top_cgroup,
901 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700902 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700903 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700904 }
905 iput(inode);
906}
907
908static void remove_dir(struct dentry *d)
909{
910 struct dentry *parent = dget(d->d_parent);
911
912 d_delete(d);
913 simple_rmdir(parent->d_inode, d);
914 dput(parent);
915}
916
Li Zefan2739d3c2013-01-21 18:18:33 +0800917static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920
Tejun Heo05ef1d72012-04-01 12:09:56 -0700921 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
922 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100923
Li Zefan2739d3c2013-01-21 18:18:33 +0800924 /*
925 * If we're doing cleanup due to failure of cgroup_create(),
926 * the corresponding @cfe may not exist.
927 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700928 list_for_each_entry(cfe, &cgrp->files, node) {
929 struct dentry *d = cfe->dentry;
930
931 if (cft && cfe->type != cft)
932 continue;
933
934 dget(d);
935 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700936 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 list_del_init(&cfe->node);
938 dput(d);
939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942}
943
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400944/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700945 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700946 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400947 * @subsys_mask: mask of the subsystem ids whose files should be removed
948 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700949static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700952 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953
Tejun Heob420ba72013-07-12 12:34:02 -0700954 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400955 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700956
957 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400958 continue;
959 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400960 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962}
963
964/*
965 * NOTE : the dentry must have been dget()'ed
966 */
967static void cgroup_d_remove_dir(struct dentry *dentry)
968{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100969 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100971 parent = dentry->d_parent;
972 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800973 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700974 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100975 spin_unlock(&dentry->d_lock);
976 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977 remove_dir(dentry);
978}
979
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700980/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800981 * Call with cgroup_mutex held. Drops reference counts on modules, including
982 * any duplicate ones that parse_cgroupfs_options took. If this function
983 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800984 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700986 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987{
Paul Menagebd89aab2007-10-18 23:40:44 -0700988 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700989 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700990 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700991 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992
Ben Blumaae8aab2010-03-10 15:22:07 -0800993 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800994 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800995
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700997 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700998 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001000
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001001 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001002 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001003 ret = -EBUSY;
1004 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001006
1007 /* pin the module */
1008 if (!try_module_get(ss->module)) {
1009 ret = -ENOENT;
1010 goto out_put;
1011 }
1012 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 }
1014
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001015 /* subsys could be missing if unloaded between parsing and here */
1016 if (added_mask != pinned) {
1017 ret = -ENOENT;
1018 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020
Tejun Heo31261212013-06-28 17:07:30 -07001021 ret = cgroup_populate_dir(cgrp, added_mask);
1022 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001023 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001024
1025 /*
1026 * Nothing can fail from this point on. Remove files for the
1027 * removed subsystems and rebind each subsystem.
1028 */
1029 cgroup_clear_dir(cgrp, removed_mask);
1030
Tejun Heo30159ec2013-06-25 11:53:37 -07001031 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001032 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001033
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001034 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001036 BUG_ON(cgroup_css(cgrp, ss));
1037 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1038 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001039
Tejun Heo73e80ed2013-08-13 11:01:55 -04001040 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001041 cgroup_css(cgroup_dummy_top, ss));
1042 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001043
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001044 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001046 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Ben Blumcf5d5942010-03-10 15:22:09 -08001048 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001049 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001050 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001052 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1053 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001056 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001057
Tejun Heoca8bdca2013-08-26 18:40:56 -04001058 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001059 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1060
Tejun Heo9871bf92013-06-24 15:21:47 -07001061 cgroup_subsys[i]->root = &cgroup_dummy_root;
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /* subsystem is now free - drop reference on module */
1064 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 }
1067 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068
Tejun Heo1672d042013-06-25 18:04:54 -07001069 /*
1070 * Mark @root has finished binding subsystems. @root->subsys_mask
1071 * now matches the bound subsystems.
1072 */
1073 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1074
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001076
1077out_put:
1078 for_each_subsys(ss, i)
1079 if (pinned & (1 << i))
1080 module_put(ss->module);
1081 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082}
1083
Al Viro34c80b12011-12-08 21:32:45 -05001084static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085{
Al Viro34c80b12011-12-08 21:32:45 -05001086 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 struct cgroup_subsys *ss;
Tejun Heob85d2042013-12-06 15:11:57 -05001088 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001090 mutex_lock(&cgroup_root_mutex);
Tejun Heob85d2042013-12-06 15:11:57 -05001091 for_each_subsys(ss, ssid)
1092 if (root->subsys_mask & (1 << ssid))
1093 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001094 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1095 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001096 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001098 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001099 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001100 if (strlen(root->release_agent_path))
1101 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001102 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001103 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001104 if (strlen(root->name))
1105 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001106 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 return 0;
1108}
1109
1110struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001111 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001113 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001114 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001115 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001116 /* User explicitly requested empty subsystem */
1117 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118
1119 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001120
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121};
1122
Ben Blumaae8aab2010-03-10 15:22:07 -08001123/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001124 * Convert a hierarchy specifier into a bitmask of subsystems and
1125 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1126 * array. This function takes refcounts on subsystems to be used, unless it
1127 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001128 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001129static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001131 char *token, *o = data;
1132 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001133 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001134 struct cgroup_subsys *ss;
1135 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136
Ben Blumaae8aab2010-03-10 15:22:07 -08001137 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1138
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139#ifdef CONFIG_CPUSETS
1140 mask = ~(1UL << cpuset_subsys_id);
1141#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142
Paul Menagec6d57f32009-09-23 15:56:19 -07001143 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144
1145 while ((token = strsep(&o, ",")) != NULL) {
1146 if (!*token)
1147 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001149 /* Explicitly have no subsystems */
1150 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001151 continue;
1152 }
1153 if (!strcmp(token, "all")) {
1154 /* Mutually exclusive option 'all' + subsystem name */
1155 if (one_ss)
1156 return -EINVAL;
1157 all_ss = true;
1158 continue;
1159 }
Tejun Heo873fe092013-04-14 20:15:26 -07001160 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1161 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1162 continue;
1163 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001165 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 continue;
1167 }
1168 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001169 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 continue;
1171 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001172 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001173 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001174 continue;
1175 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001177 /* Specifying two release agents is forbidden */
1178 if (opts->release_agent)
1179 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001180 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001181 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001182 if (!opts->release_agent)
1183 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001184 continue;
1185 }
1186 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001187 const char *name = token + 5;
1188 /* Can't specify an empty name */
1189 if (!strlen(name))
1190 return -EINVAL;
1191 /* Must match [\w.-]+ */
1192 for (i = 0; i < strlen(name); i++) {
1193 char c = name[i];
1194 if (isalnum(c))
1195 continue;
1196 if ((c == '.') || (c == '-') || (c == '_'))
1197 continue;
1198 return -EINVAL;
1199 }
1200 /* Specifying two names is forbidden */
1201 if (opts->name)
1202 return -EINVAL;
1203 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001204 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001205 GFP_KERNEL);
1206 if (!opts->name)
1207 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001208
1209 continue;
1210 }
1211
Tejun Heo30159ec2013-06-25 11:53:37 -07001212 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001213 if (strcmp(token, ss->name))
1214 continue;
1215 if (ss->disabled)
1216 continue;
1217
1218 /* Mutually exclusive option 'all' + subsystem name */
1219 if (all_ss)
1220 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001221 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001222 one_ss = true;
1223
1224 break;
1225 }
1226 if (i == CGROUP_SUBSYS_COUNT)
1227 return -ENOENT;
1228 }
1229
1230 /*
1231 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001232 * otherwise if 'none', 'name=' and a subsystem name options
1233 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001235 if (all_ss || (!one_ss && !opts->none && !opts->name))
1236 for_each_subsys(ss, i)
1237 if (!ss->disabled)
1238 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001240 /* Consistency checks */
1241
Tejun Heo873fe092013-04-14 20:15:26 -07001242 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1243 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1244
1245 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1246 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1247 return -EINVAL;
1248 }
1249
1250 if (opts->cpuset_clone_children) {
1251 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1252 return -EINVAL;
1253 }
1254 }
1255
Li Zefanf9ab5b52009-06-17 16:26:33 -07001256 /*
1257 * Option noprefix was introduced just for backward compatibility
1258 * with the old cpuset, so we allow noprefix only if mounting just
1259 * the cpuset subsystem.
1260 */
Tejun Heo93438622013-04-14 20:15:25 -07001261 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 return -EINVAL;
1263
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001264
1265 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001266 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001267 return -EINVAL;
1268
1269 /*
1270 * We either have to specify by name or by subsystems. (So all
1271 * empty hierarchies must have a name).
1272 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001273 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001274 return -EINVAL;
1275
1276 return 0;
1277}
1278
1279static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1280{
1281 int ret = 0;
1282 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001283 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001285 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286
Tejun Heo873fe092013-04-14 20:15:26 -07001287 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1288 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1289 return -EINVAL;
1290 }
1291
Paul Menagebd89aab2007-10-18 23:40:44 -07001292 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001294 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295
1296 /* See what subsystems are wanted */
1297 ret = parse_cgroupfs_options(data, &opts);
1298 if (ret)
1299 goto out_unlock;
1300
Tejun Heoa8a648c2013-06-24 15:21:47 -07001301 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001302 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1303 task_tgid_nr(current), current->comm);
1304
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001305 added_mask = opts.subsys_mask & ~root->subsys_mask;
1306 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001307
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001309 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001311 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1312 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1313 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001315 goto out_unlock;
1316 }
1317
Tejun Heof172e672013-06-28 17:07:30 -07001318 /* remounting is not allowed for populated hierarchies */
1319 if (root->number_of_cgroups > 1) {
1320 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001321 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001325 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327
Paul Menage81a6a5c2007-10-18 23:39:38 -07001328 if (opts.release_agent)
1329 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001331 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001332 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001333 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001335 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336 return ret;
1337}
1338
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001339static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 .statfs = simple_statfs,
1341 .drop_inode = generic_delete_inode,
1342 .show_options = cgroup_show_options,
1343 .remount_fs = cgroup_remount,
1344};
1345
Paul Menagecc31edc2008-10-18 20:28:04 -07001346static void init_cgroup_housekeeping(struct cgroup *cgrp)
1347{
1348 INIT_LIST_HEAD(&cgrp->sibling);
1349 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001350 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001351 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001352 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001353 INIT_LIST_HEAD(&cgrp->pidlists);
1354 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001355 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001356 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001357}
Paul Menagec6d57f32009-09-23 15:56:19 -07001358
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359static void init_cgroup_root(struct cgroupfs_root *root)
1360{
Paul Menagebd89aab2007-10-18 23:40:44 -07001361 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001362
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 INIT_LIST_HEAD(&root->root_list);
1364 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001365 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001366 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001367 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001368 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369}
1370
Tejun Heofc76df72013-06-25 11:53:37 -07001371static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001372{
Tejun Heo1a574232013-04-14 11:36:58 -07001373 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001374
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001375 lockdep_assert_held(&cgroup_mutex);
1376 lockdep_assert_held(&cgroup_root_mutex);
1377
Tejun Heofc76df72013-06-25 11:53:37 -07001378 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1379 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001380 if (id < 0)
1381 return id;
1382
1383 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001384 return 0;
1385}
1386
1387static void cgroup_exit_root_id(struct cgroupfs_root *root)
1388{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001389 lockdep_assert_held(&cgroup_mutex);
1390 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001391
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001392 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001393 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001394 root->hierarchy_id = 0;
1395 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001396}
1397
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398static int cgroup_test_super(struct super_block *sb, void *data)
1399{
Paul Menagec6d57f32009-09-23 15:56:19 -07001400 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001401 struct cgroupfs_root *root = sb->s_fs_info;
1402
Paul Menagec6d57f32009-09-23 15:56:19 -07001403 /* If we asked for a name then it must match */
1404 if (opts->name && strcmp(opts->name, root->name))
1405 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001407 /*
1408 * If we asked for subsystems (or explicitly for no
1409 * subsystems) then they must match
1410 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001411 if ((opts->subsys_mask || opts->none)
1412 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413 return 0;
1414
1415 return 1;
1416}
1417
Paul Menagec6d57f32009-09-23 15:56:19 -07001418static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1419{
1420 struct cgroupfs_root *root;
1421
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001422 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001423 return NULL;
1424
1425 root = kzalloc(sizeof(*root), GFP_KERNEL);
1426 if (!root)
1427 return ERR_PTR(-ENOMEM);
1428
1429 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001430
Tejun Heo1672d042013-06-25 18:04:54 -07001431 /*
1432 * We need to set @root->subsys_mask now so that @root can be
1433 * matched by cgroup_test_super() before it finishes
1434 * initialization; otherwise, competing mounts with the same
1435 * options may try to bind the same subsystems instead of waiting
1436 * for the first one leading to unexpected mount errors.
1437 * SUBSYS_BOUND will be set once actual binding is complete.
1438 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001439 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001440 root->flags = opts->flags;
1441 if (opts->release_agent)
1442 strcpy(root->release_agent_path, opts->release_agent);
1443 if (opts->name)
1444 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001445 if (opts->cpuset_clone_children)
1446 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001447 return root;
1448}
1449
Tejun Heofa3ca072013-04-14 11:36:56 -07001450static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001451{
Tejun Heofa3ca072013-04-14 11:36:56 -07001452 if (root) {
1453 /* hierarhcy ID shoulid already have been released */
1454 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001455
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001456 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001457 kfree(root);
1458 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001459}
1460
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461static int cgroup_set_super(struct super_block *sb, void *data)
1462{
1463 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001464 struct cgroup_sb_opts *opts = data;
1465
1466 /* If we don't have a new root, we can't set up a new sb */
1467 if (!opts->new_root)
1468 return -EINVAL;
1469
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001470 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471
1472 ret = set_anon_super(sb, NULL);
1473 if (ret)
1474 return ret;
1475
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 sb->s_fs_info = opts->new_root;
1477 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478
1479 sb->s_blocksize = PAGE_CACHE_SIZE;
1480 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1481 sb->s_magic = CGROUP_SUPER_MAGIC;
1482 sb->s_op = &cgroup_ops;
1483
1484 return 0;
1485}
1486
1487static int cgroup_get_rootdir(struct super_block *sb)
1488{
Al Viro0df6a632010-12-21 13:29:29 -05001489 static const struct dentry_operations cgroup_dops = {
1490 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001491 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001492 };
1493
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 struct inode *inode =
1495 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496
1497 if (!inode)
1498 return -ENOMEM;
1499
Paul Menageddbcc7e2007-10-18 23:39:30 -07001500 inode->i_fop = &simple_dir_operations;
1501 inode->i_op = &cgroup_dir_inode_operations;
1502 /* directories start off with i_nlink == 2 (for "." entry) */
1503 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001504 sb->s_root = d_make_root(inode);
1505 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001507 /* for everything else we want ->d_op set */
1508 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509 return 0;
1510}
1511
Al Virof7e83572010-07-26 13:23:11 +04001512static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001514 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515{
1516 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001517 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518 int ret = 0;
1519 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001520 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001521 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001522 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001523 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
1525 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001526 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001528 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 if (ret)
1530 goto out_err;
1531
1532 /*
1533 * Allocate a new cgroup root. We may not need it if we're
1534 * reusing an existing hierarchy.
1535 */
1536 new_root = cgroup_root_from_opts(&opts);
1537 if (IS_ERR(new_root)) {
1538 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001539 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001540 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001541 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001544 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001546 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001547 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001548 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 }
1550
Paul Menagec6d57f32009-09-23 15:56:19 -07001551 root = sb->s_fs_info;
1552 BUG_ON(!root);
1553 if (root == opts.new_root) {
1554 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001555 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001556 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001557 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001558 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 BUG_ON(sb->s_root != NULL);
1561
1562 ret = cgroup_get_rootdir(sb);
1563 if (ret)
1564 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001565 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566
Paul Menage817929e2007-10-18 23:39:36 -07001567 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001569 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570
Tejun Heoeb46bf82014-02-08 10:26:33 -05001571 ret = idr_alloc(&root->cgroup_idr, root_cgrp, 0, 1, GFP_KERNEL);
1572 if (ret < 0)
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001573 goto unlock_drop;
Tejun Heoeb46bf82014-02-08 10:26:33 -05001574 root_cgrp->id = ret;
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001575
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001576 /* Check for name clashes with existing mounts */
1577 ret = -EBUSY;
1578 if (strlen(root->name))
1579 for_each_active_root(existing_root)
1580 if (!strcmp(existing_root->name, root->name))
1581 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001582
Paul Menage817929e2007-10-18 23:39:36 -07001583 /*
1584 * We're accessing css_set_count without locking
1585 * css_set_lock here, but that's OK - it can only be
1586 * increased by someone holding cgroup_lock, and
1587 * that's us. The worst that can happen is that we
1588 * have some link structures left over
1589 */
Tejun Heo69d02062013-06-12 21:04:50 -07001590 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001591 if (ret)
1592 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001593
Tejun Heofc76df72013-06-25 11:53:37 -07001594 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1595 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001596 if (ret)
1597 goto unlock_drop;
1598
Tejun Heo31261212013-06-28 17:07:30 -07001599 sb->s_root->d_fsdata = root_cgrp;
1600 root_cgrp->dentry = sb->s_root;
1601
1602 /*
1603 * We're inside get_sb() and will call lookup_one_len() to
1604 * create the root files, which doesn't work if SELinux is
1605 * in use. The following cred dancing somehow works around
1606 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1607 * populating new cgroupfs mount") for more details.
1608 */
1609 cred = override_creds(&init_cred);
1610
Tejun Heo2bb566c2013-08-08 20:11:23 -04001611 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001612 if (ret)
1613 goto rm_base_files;
1614
Tejun Heoa8a648c2013-06-24 15:21:47 -07001615 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001616 if (ret)
1617 goto rm_base_files;
1618
1619 revert_creds(cred);
1620
Ben Blumcf5d5942010-03-10 15:22:09 -08001621 /*
1622 * There must be no failure case after here, since rebinding
1623 * takes care of subsystems' refcounts, which are explicitly
1624 * dropped in the failure exit path.
1625 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Tejun Heo9871bf92013-06-24 15:21:47 -07001627 list_add(&root->root_list, &cgroup_roots);
1628 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001629
Paul Menage817929e2007-10-18 23:39:36 -07001630 /* Link the top cgroup in this hierarchy into all
1631 * the css_set objects */
1632 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001633 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001634 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001635 write_unlock(&css_set_lock);
1636
Tejun Heo69d02062013-06-12 21:04:50 -07001637 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001638
Li Zefanc12f65d2009-01-07 18:07:42 -08001639 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640 BUG_ON(root->number_of_cgroups != 1);
1641
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001642 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001644 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001645 } else {
1646 /*
1647 * We re-used an existing hierarchy - the new root (if
1648 * any) is not needed
1649 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001650 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001651
Tejun Heoc7ba8282013-06-29 14:06:10 -07001652 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001653 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1654 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1655 ret = -EINVAL;
1656 goto drop_new_super;
1657 } else {
1658 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1659 }
Tejun Heo873fe092013-04-14 20:15:26 -07001660 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661 }
1662
Paul Menagec6d57f32009-09-23 15:56:19 -07001663 kfree(opts.release_agent);
1664 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001665 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666
Tejun Heo31261212013-06-28 17:07:30 -07001667 rm_base_files:
1668 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001669 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001670 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001672 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001673 mutex_unlock(&cgroup_root_mutex);
1674 mutex_unlock(&cgroup_mutex);
1675 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001677 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001678 out_err:
1679 kfree(opts.release_agent);
1680 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001681 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682}
1683
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09001684static void cgroup_kill_sb(struct super_block *sb)
1685{
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001687 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001688 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 int ret;
1690
1691 BUG_ON(!root);
1692
1693 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001694 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695
Tejun Heo31261212013-06-28 17:07:30 -07001696 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001698 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
1700 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001701 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1702 ret = rebind_subsystems(root, 0, root->subsys_mask);
1703 /* Shouldn't be able to fail ... */
1704 BUG_ON(ret);
1705 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Paul Menage817929e2007-10-18 23:39:36 -07001707 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001708 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001709 * root cgroup
1710 */
1711 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001712
Tejun Heo69d02062013-06-12 21:04:50 -07001713 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1714 list_del(&link->cset_link);
1715 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001716 kfree(link);
1717 }
1718 write_unlock(&css_set_lock);
1719
Paul Menage839ec542009-01-29 14:25:22 -08001720 if (!list_empty(&root->root_list)) {
1721 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001722 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001723 }
Li Zefane5f6a862009-01-07 18:07:41 -08001724
Tejun Heofa3ca072013-04-14 11:36:56 -07001725 cgroup_exit_root_id(root);
1726
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001729 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001731 simple_xattrs_free(&cgrp->xattrs);
1732
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001734 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735}
1736
1737static struct file_system_type cgroup_fs_type = {
1738 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001739 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740 .kill_sb = cgroup_kill_sb,
1741};
1742
Greg KH676db4a2010-08-05 13:53:35 -07001743static struct kobject *cgroup_kobj;
1744
Li Zefana043e3b2008-02-23 15:24:09 -08001745/**
1746 * cgroup_path - generate the path of a cgroup
1747 * @cgrp: the cgroup in question
1748 * @buf: the buffer to write the path into
1749 * @buflen: the length of the buffer
1750 *
Li Zefan65dff752013-03-01 15:01:56 +08001751 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1752 *
1753 * We can't generate cgroup path using dentry->d_name, as accessing
1754 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1755 * inode's i_mutex, while on the other hand cgroup_path() can be called
1756 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001758int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759{
Li Zefan65dff752013-03-01 15:01:56 +08001760 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001762
Tejun Heoda1f2962013-04-14 10:32:19 -07001763 if (!cgrp->parent) {
1764 if (strlcpy(buf, "/", buflen) >= buflen)
1765 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 return 0;
1767 }
1768
Tao Ma316eb662012-11-08 21:36:38 +08001769 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001770 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001771
Li Zefan65dff752013-03-01 15:01:56 +08001772 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001773 do {
Li Zefan65dff752013-03-01 15:01:56 +08001774 const char *name = cgroup_name(cgrp);
1775 int len;
1776
1777 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001779 goto out;
1780 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001781
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001783 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001785
1786 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001787 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001788 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001790out:
1791 rcu_read_unlock();
1792 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793}
Ben Blum67523c42010-03-10 15:22:11 -08001794EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795
Tejun Heo857a2be2013-04-14 20:50:08 -07001796/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001798 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 * @buf: the buffer to write the path into
1800 * @buflen: the length of the buffer
1801 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001802 * Determine @task's cgroup on the first (the one with the lowest non-zero
1803 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1804 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1805 * cgroup controller callbacks.
1806 *
1807 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001808 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001809int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001810{
1811 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 struct cgroup *cgrp;
1813 int hierarchy_id = 1, ret = 0;
1814
1815 if (buflen < 2)
1816 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001817
1818 mutex_lock(&cgroup_mutex);
1819
Tejun Heo913ffdb2013-07-11 16:34:48 -07001820 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1821
Tejun Heo857a2be2013-04-14 20:50:08 -07001822 if (root) {
1823 cgrp = task_cgroup_from_root(task, root);
1824 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825 } else {
1826 /* if no hierarchy exists, everyone is in "/" */
1827 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001828 }
1829
1830 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001831 return ret;
1832}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001833EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001834
Ben Blum74a11662011-05-26 16:25:20 -07001835/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001836 * Control Group taskset
1837 */
Tejun Heo134d3372011-12-12 18:12:21 -08001838struct task_and_cgroup {
1839 struct task_struct *task;
1840 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001841 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001842};
1843
Tejun Heo2f7ee562011-12-12 18:12:21 -08001844struct cgroup_taskset {
1845 struct task_and_cgroup single;
1846 struct flex_array *tc_array;
1847 int tc_array_len;
1848 int idx;
1849 struct cgroup *cur_cgrp;
1850};
1851
1852/**
1853 * cgroup_taskset_first - reset taskset and return the first task
1854 * @tset: taskset of interest
1855 *
1856 * @tset iteration is initialized and the first task is returned.
1857 */
1858struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1859{
1860 if (tset->tc_array) {
1861 tset->idx = 0;
1862 return cgroup_taskset_next(tset);
1863 } else {
1864 tset->cur_cgrp = tset->single.cgrp;
1865 return tset->single.task;
1866 }
1867}
1868EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1869
1870/**
1871 * cgroup_taskset_next - iterate to the next task in taskset
1872 * @tset: taskset of interest
1873 *
1874 * Return the next task in @tset. Iteration must have been initialized
1875 * with cgroup_taskset_first().
1876 */
1877struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1878{
1879 struct task_and_cgroup *tc;
1880
1881 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1882 return NULL;
1883
1884 tc = flex_array_get(tset->tc_array, tset->idx++);
1885 tset->cur_cgrp = tc->cgrp;
1886 return tc->task;
1887}
1888EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1889
1890/**
Tejun Heod99c8722013-08-08 20:11:27 -04001891 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001892 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001893 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894 *
Tejun Heod99c8722013-08-08 20:11:27 -04001895 * Return the css for the current (last returned) task of @tset for
1896 * subsystem specified by @subsys_id. This function must be preceded by
1897 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001898 */
Tejun Heod99c8722013-08-08 20:11:27 -04001899struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1900 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001901{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001902 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001903}
Tejun Heod99c8722013-08-08 20:11:27 -04001904EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001905
1906/**
1907 * cgroup_taskset_size - return the number of tasks in taskset
1908 * @tset: taskset of interest
1909 */
1910int cgroup_taskset_size(struct cgroup_taskset *tset)
1911{
1912 return tset->tc_array ? tset->tc_array_len : 1;
1913}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1915
1916
Ben Blum74a11662011-05-26 16:25:20 -07001917/*
1918 * cgroup_task_migrate - move a task from one cgroup to another.
1919 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001920 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001921 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001922static void cgroup_task_migrate(struct cgroup *old_cgrp,
1923 struct task_struct *tsk,
1924 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001925{
Tejun Heo5abb8852013-06-12 21:04:49 -07001926 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001927
1928 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001929 * We are synchronized through threadgroup_lock() against PF_EXITING
1930 * setting such that we can't race against cgroup_exit() changing the
1931 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001932 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001933 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001934 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001935
Ben Blum74a11662011-05-26 16:25:20 -07001936 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001937 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001938 task_unlock(tsk);
1939
1940 /* Update the css_set linked lists if we're using them */
1941 write_lock(&css_set_lock);
1942 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001943 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001944 write_unlock(&css_set_lock);
1945
1946 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001947 * We just gained a reference on old_cset by taking it from the
1948 * task. As trading it for new_cset is protected by cgroup_mutex,
1949 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001951 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1952 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001953}
1954
Li Zefana043e3b2008-02-23 15:24:09 -08001955/**
Li Zefan081aa452013-03-13 09:17:09 +08001956 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001957 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001958 * @tsk: the task or the leader of the threadgroup to be attached
1959 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001960 *
Tejun Heo257058a2011-12-12 18:12:21 -08001961 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001962 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001964static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1965 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001966{
1967 int retval, i, group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001968 struct cgroupfs_root *root = cgrp->root;
Tejun Heo1c6727a2013-12-06 15:11:56 -05001969 struct cgroup_subsys_state *css, *failed_css = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001970 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001971 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001972 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001973 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001974 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001975
1976 /*
1977 * step 0: in order to do expensive, possibly blocking operations for
1978 * every thread, we cannot iterate the thread group list, since it needs
1979 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001980 * group - group_rwsem prevents new threads from appearing, and if
1981 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001982 */
Li Zefan081aa452013-03-13 09:17:09 +08001983 if (threadgroup)
1984 group_size = get_nr_threads(tsk);
1985 else
1986 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001987 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001988 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001989 if (!group)
1990 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001991 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001992 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001993 if (retval)
1994 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001995
Ben Blum74a11662011-05-26 16:25:20 -07001996 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001997 /*
1998 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1999 * already PF_EXITING could be freed from underneath us unless we
2000 * take an rcu_read_lock.
2001 */
2002 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002003 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002004 struct task_and_cgroup ent;
2005
Tejun Heocd3d0952011-12-12 18:12:21 -08002006 /* @tsk either already exited or can't exit until the end */
2007 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002008 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002009
Ben Blum74a11662011-05-26 16:25:20 -07002010 /* as per above, nr_threads may decrease, but not increase. */
2011 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002012 ent.task = tsk;
2013 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002014 /* nothing to do if this task is already in the cgroup */
2015 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002016 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002017 /*
2018 * saying GFP_ATOMIC has no effect here because we did prealloc
2019 * earlier, but it's good form to communicate our expectations.
2020 */
Tejun Heo134d3372011-12-12 18:12:21 -08002021 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002022 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002023 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002024 next:
Li Zefan081aa452013-03-13 09:17:09 +08002025 if (!threadgroup)
2026 break;
Ben Blum74a11662011-05-26 16:25:20 -07002027 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002028 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002029 /* remember the number of threads in the array for later. */
2030 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002031 tset.tc_array = group;
2032 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002033
Tejun Heo134d3372011-12-12 18:12:21 -08002034 /* methods shouldn't be called if no task is actually migrating */
2035 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002036 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002037 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002038
Ben Blum74a11662011-05-26 16:25:20 -07002039 /*
2040 * step 1: check that we can legitimately attach to the cgroup.
2041 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002042 for_each_css(css, i, cgrp) {
2043 if (css->ss->can_attach) {
2044 retval = css->ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002045 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002046 failed_css = css;
Ben Blum74a11662011-05-26 16:25:20 -07002047 goto out_cancel_attach;
2048 }
2049 }
Ben Blum74a11662011-05-26 16:25:20 -07002050 }
2051
2052 /*
2053 * step 2: make sure css_sets exist for all threads to be migrated.
2054 * we use find_css_set, which allocates a new one if necessary.
2055 */
Ben Blum74a11662011-05-26 16:25:20 -07002056 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002057 struct css_set *old_cset;
2058
Tejun Heo134d3372011-12-12 18:12:21 -08002059 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002060 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002061 tc->cset = find_css_set(old_cset, cgrp);
2062 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002063 retval = -ENOMEM;
2064 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002065 }
2066 }
2067
2068 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002069 * step 3: now that we're guaranteed success wrt the css_sets,
2070 * proceed to move all tasks to the new cgroup. There are no
2071 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002072 */
Ben Blum74a11662011-05-26 16:25:20 -07002073 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002074 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002075 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002076 }
2077 /* nothing is sensitive to fork() after this point. */
2078
2079 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002080 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002081 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05002082 for_each_css(css, i, cgrp)
2083 if (css->ss->attach)
2084 css->ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002085
2086 /*
2087 * step 5: success! and cleanup
2088 */
Ben Blum74a11662011-05-26 16:25:20 -07002089 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002090out_put_css_set_refs:
2091 if (retval) {
2092 for (i = 0; i < group_size; i++) {
2093 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002094 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002095 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002096 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097 }
Ben Blum74a11662011-05-26 16:25:20 -07002098 }
2099out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (retval) {
Tejun Heo1c6727a2013-12-06 15:11:56 -05002101 for_each_css(css, i, cgrp) {
2102 if (css == failed_css)
Ben Blum74a11662011-05-26 16:25:20 -07002103 break;
Tejun Heo1c6727a2013-12-06 15:11:56 -05002104 if (css->ss->cancel_attach)
2105 css->ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107 }
Ben Blum74a11662011-05-26 16:25:20 -07002108out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002109 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002110 return retval;
2111}
2112
2113/*
2114 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002115 * function to attach either it or all tasks in its threadgroup. Will lock
2116 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002117 */
2118static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002121 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 int ret;
2123
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (!cgroup_lock_live_group(cgrp))
2125 return -ENODEV;
2126
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002127retry_find_task:
2128 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002130 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002131 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 rcu_read_unlock();
SeongJae Parkdd4b0a42014-01-18 16:56:47 +09002133 ret = -ESRCH;
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002134 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136 /*
2137 * even if we're attaching all tasks in the thread group, we
2138 * only need to check permissions on one of them.
2139 */
David Howellsc69e8d92008-11-14 10:39:19 +11002140 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002141 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2142 !uid_eq(cred->euid, tcred->uid) &&
2143 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002144 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002145 ret = -EACCES;
2146 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002147 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 } else
2149 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002150
2151 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002153
2154 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002155 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002156 * trapped in a cpuset, or RT worker may be born in a cgroup
2157 * with no rt_runtime allocated. Just say no.
2158 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002159 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002160 ret = -EINVAL;
2161 rcu_read_unlock();
2162 goto out_unlock_cgroup;
2163 }
2164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 get_task_struct(tsk);
2166 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 threadgroup_lock(tsk);
2169 if (threadgroup) {
2170 if (!thread_group_leader(tsk)) {
2171 /*
2172 * a race with de_thread from another thread's exec()
2173 * may strip us of our leadership, if this happens,
2174 * there is no choice but to throw this task away and
2175 * try again; this is
2176 * "double-double-toil-and-trouble-check locking".
2177 */
2178 threadgroup_unlock(tsk);
2179 put_task_struct(tsk);
2180 goto retry_find_task;
2181 }
Li Zefan081aa452013-03-13 09:17:09 +08002182 }
2183
2184 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2185
Tejun Heocd3d0952011-12-12 18:12:21 -08002186 threadgroup_unlock(tsk);
2187
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002190 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 return ret;
2192}
2193
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002194/**
2195 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2196 * @from: attach to all cgroups of a given task
2197 * @tsk: the task to be attached
2198 */
2199int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2200{
2201 struct cgroupfs_root *root;
2202 int retval = 0;
2203
Tejun Heo47cfcd02013-04-07 09:29:51 -07002204 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002205 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002206 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002207
Li Zefan6f4b7e62013-07-31 16:18:36 +08002208 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002209 if (retval)
2210 break;
2211 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213
2214 return retval;
2215}
2216EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2217
Tejun Heo182446d2013-08-08 20:11:24 -04002218static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2219 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002220{
Tejun Heo182446d2013-08-08 20:11:24 -04002221 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002222}
2223
Tejun Heo182446d2013-08-08 20:11:24 -04002224static int cgroup_procs_write(struct cgroup_subsys_state *css,
2225 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002226{
Tejun Heo182446d2013-08-08 20:11:24 -04002227 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002228}
2229
Tejun Heo182446d2013-08-08 20:11:24 -04002230static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2231 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002232{
Tejun Heo182446d2013-08-08 20:11:24 -04002233 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002234 if (strlen(buffer) >= PATH_MAX)
2235 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002236 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002237 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002238 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002239 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002240 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002241 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002242 return 0;
2243}
2244
Tejun Heo2da8ca82013-12-05 12:28:04 -05002245static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002246{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002247 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002248
Paul Menagee788e062008-07-25 01:46:59 -07002249 if (!cgroup_lock_live_group(cgrp))
2250 return -ENODEV;
2251 seq_puts(seq, cgrp->root->release_agent_path);
2252 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002254 return 0;
2255}
2256
Tejun Heo2da8ca82013-12-05 12:28:04 -05002257static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002258{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002259 struct cgroup *cgrp = seq_css(seq)->cgroup;
2260
2261 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002262 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002263}
2264
Paul Menage84eea842008-07-25 01:47:00 -07002265/* A buffer size big enough for numbers or short strings */
2266#define CGROUP_LOCAL_BUFFER_SIZE 64
2267
Tejun Heoa742c592013-12-05 12:28:03 -05002268static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002269 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002270{
Tejun Heo182446d2013-08-08 20:11:24 -04002271 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002272 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002273 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002274 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2275 char *buf;
2276 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002277
Tejun Heoa742c592013-12-05 12:28:03 -05002278 if (nbytes >= max_bytes)
2279 return -E2BIG;
2280
2281 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2282 if (!buf)
2283 return -ENOMEM;
2284
2285 if (copy_from_user(buf, userbuf, nbytes)) {
2286 ret = -EFAULT;
2287 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002288 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002289
Tejun Heoa742c592013-12-05 12:28:03 -05002290 buf[nbytes] = '\0';
Paul Menageddbcc7e2007-10-18 23:39:30 -07002291
Tejun Heoa742c592013-12-05 12:28:03 -05002292 if (cft->write_string) {
2293 ret = cft->write_string(css, cft, strstrip(buf));
2294 } else if (cft->write_u64) {
2295 unsigned long long v;
2296 ret = kstrtoull(buf, 0, &v);
2297 if (!ret)
2298 ret = cft->write_u64(css, cft, v);
2299 } else if (cft->write_s64) {
2300 long long v;
2301 ret = kstrtoll(buf, 0, &v);
2302 if (!ret)
2303 ret = cft->write_s64(css, cft, v);
2304 } else if (cft->trigger) {
2305 ret = cft->trigger(css, (unsigned int)cft->private);
2306 } else {
2307 ret = -EINVAL;
2308 }
2309out_free:
2310 kfree(buf);
2311 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002312}
2313
Paul Menage91796562008-04-29 01:00:01 -07002314/*
2315 * seqfile ops/methods for returning structured data. Currently just
2316 * supports string->u64 maps, but can be extended in future.
2317 */
2318
Tejun Heo6612f052013-12-05 12:28:04 -05002319static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
Paul Menage91796562008-04-29 01:00:01 -07002320{
Tejun Heo6612f052013-12-05 12:28:04 -05002321 struct cftype *cft = seq_cft(seq);
2322
2323 if (cft->seq_start) {
2324 return cft->seq_start(seq, ppos);
2325 } else {
2326 /*
2327 * The same behavior and code as single_open(). Returns
2328 * !NULL if pos is at the beginning; otherwise, NULL.
2329 */
2330 return NULL + !*ppos;
2331 }
2332}
2333
2334static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2335{
2336 struct cftype *cft = seq_cft(seq);
2337
2338 if (cft->seq_next) {
2339 return cft->seq_next(seq, v, ppos);
2340 } else {
2341 /*
2342 * The same behavior and code as single_open(), always
2343 * terminate after the initial read.
2344 */
2345 ++*ppos;
2346 return NULL;
2347 }
2348}
2349
2350static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2351{
2352 struct cftype *cft = seq_cft(seq);
2353
2354 if (cft->seq_stop)
2355 cft->seq_stop(seq, v);
Paul Menage91796562008-04-29 01:00:01 -07002356}
2357
2358static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2359{
Tejun Heo7da11272013-12-05 12:28:04 -05002360 struct cftype *cft = seq_cft(m);
2361 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002362
Tejun Heo2da8ca82013-12-05 12:28:04 -05002363 if (cft->seq_show)
2364 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002365
Tejun Heo896f5192013-12-05 12:28:04 -05002366 if (cft->read_u64)
2367 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2368 else if (cft->read_s64)
2369 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2370 else
2371 return -EINVAL;
2372 return 0;
Paul Menage91796562008-04-29 01:00:01 -07002373}
2374
Tejun Heo6612f052013-12-05 12:28:04 -05002375static struct seq_operations cgroup_seq_operations = {
2376 .start = cgroup_seqfile_start,
2377 .next = cgroup_seqfile_next,
2378 .stop = cgroup_seqfile_stop,
2379 .show = cgroup_seqfile_show,
Paul Menage91796562008-04-29 01:00:01 -07002380};
2381
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382static int cgroup_file_open(struct inode *inode, struct file *file)
2383{
Tejun Heof7d58812013-08-08 20:11:23 -04002384 struct cfent *cfe = __d_cfe(file->f_dentry);
2385 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002386 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2387 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002388 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390
2391 err = generic_file_open(inode, file);
2392 if (err)
2393 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002394
2395 /*
2396 * If the file belongs to a subsystem, pin the css. Will be
2397 * unpinned either on open failure or release. This ensures that
2398 * @css stays alive for all file operations.
2399 */
Tejun Heo105347b2013-08-13 11:01:55 -04002400 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002401 css = cgroup_css(cgrp, cft->ss);
2402 if (cft->ss && !css_tryget(css))
2403 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002404 rcu_read_unlock();
2405
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002406 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002407 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002408
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002409 /*
2410 * @cfe->css is used by read/write/close to determine the
2411 * associated css. @file->private_data would be a better place but
2412 * that's already used by seqfile. Multiple accessors may use it
2413 * simultaneously which is okay as the association never changes.
2414 */
2415 WARN_ON_ONCE(cfe->css && cfe->css != css);
2416 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417
Tejun Heo6612f052013-12-05 12:28:04 -05002418 of = __seq_open_private(file, &cgroup_seq_operations,
2419 sizeof(struct cgroup_open_file));
2420 if (of) {
2421 of->cfe = cfe;
2422 return 0;
Li Zefane0798ce2013-07-31 17:36:25 +08002423 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424
Tejun Heo6612f052013-12-05 12:28:04 -05002425 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002426 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002427 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428}
2429
2430static int cgroup_file_release(struct inode *inode, struct file *file)
2431{
Tejun Heof7d58812013-08-08 20:11:23 -04002432 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002433 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002434
Tejun Heo67f4c362013-08-08 20:11:24 -04002435 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002436 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002437 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002438}
2439
2440/*
2441 * cgroup_rename - Only allow simple rename of directories in place.
2442 */
2443static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2444 struct inode *new_dir, struct dentry *new_dentry)
2445{
Li Zefan65dff752013-03-01 15:01:56 +08002446 int ret;
2447 struct cgroup_name *name, *old_name;
2448 struct cgroup *cgrp;
2449
2450 /*
2451 * It's convinient to use parent dir's i_mutex to protected
2452 * cgrp->name.
2453 */
2454 lockdep_assert_held(&old_dir->i_mutex);
2455
Paul Menageddbcc7e2007-10-18 23:39:30 -07002456 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2457 return -ENOTDIR;
2458 if (new_dentry->d_inode)
2459 return -EEXIST;
2460 if (old_dir != new_dir)
2461 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002462
2463 cgrp = __d_cgrp(old_dentry);
2464
Tejun Heo6db8e852013-06-14 11:18:22 -07002465 /*
2466 * This isn't a proper migration and its usefulness is very
2467 * limited. Disallow if sane_behavior.
2468 */
2469 if (cgroup_sane_behavior(cgrp))
2470 return -EPERM;
2471
Li Zefan65dff752013-03-01 15:01:56 +08002472 name = cgroup_alloc_name(new_dentry);
2473 if (!name)
2474 return -ENOMEM;
2475
2476 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2477 if (ret) {
2478 kfree(name);
2479 return ret;
2480 }
2481
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002482 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002483 rcu_assign_pointer(cgrp->name, name);
2484
2485 kfree_rcu(old_name, rcu_head);
2486 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002487}
2488
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002489static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2490{
2491 if (S_ISDIR(dentry->d_inode->i_mode))
2492 return &__d_cgrp(dentry)->xattrs;
2493 else
Li Zefan712317a2013-04-18 23:09:52 -07002494 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002495}
2496
2497static inline int xattr_enabled(struct dentry *dentry)
2498{
2499 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002500 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002501}
2502
2503static bool is_valid_xattr(const char *name)
2504{
2505 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2506 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2507 return true;
2508 return false;
2509}
2510
2511static int cgroup_setxattr(struct dentry *dentry, const char *name,
2512 const void *val, size_t size, int flags)
2513{
2514 if (!xattr_enabled(dentry))
2515 return -EOPNOTSUPP;
2516 if (!is_valid_xattr(name))
2517 return -EINVAL;
2518 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2519}
2520
2521static int cgroup_removexattr(struct dentry *dentry, const char *name)
2522{
2523 if (!xattr_enabled(dentry))
2524 return -EOPNOTSUPP;
2525 if (!is_valid_xattr(name))
2526 return -EINVAL;
2527 return simple_xattr_remove(__d_xattrs(dentry), name);
2528}
2529
2530static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2531 void *buf, size_t size)
2532{
2533 if (!xattr_enabled(dentry))
2534 return -EOPNOTSUPP;
2535 if (!is_valid_xattr(name))
2536 return -EINVAL;
2537 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2538}
2539
2540static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2541{
2542 if (!xattr_enabled(dentry))
2543 return -EOPNOTSUPP;
2544 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2545}
2546
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002547static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002548 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002549 .write = cgroup_file_write,
2550 .llseek = generic_file_llseek,
2551 .open = cgroup_file_open,
2552 .release = cgroup_file_release,
2553};
2554
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002555static const struct inode_operations cgroup_file_inode_operations = {
2556 .setxattr = cgroup_setxattr,
2557 .getxattr = cgroup_getxattr,
2558 .listxattr = cgroup_listxattr,
2559 .removexattr = cgroup_removexattr,
2560};
2561
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002562static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002563 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002564 .mkdir = cgroup_mkdir,
2565 .rmdir = cgroup_rmdir,
2566 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002567 .setxattr = cgroup_setxattr,
2568 .getxattr = cgroup_getxattr,
2569 .listxattr = cgroup_listxattr,
2570 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002571};
2572
Al Viroa5e7ed32011-07-26 01:55:55 -04002573static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002574 struct super_block *sb)
2575{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002576 struct inode *inode;
2577
2578 if (!dentry)
2579 return -ENOENT;
2580 if (dentry->d_inode)
2581 return -EEXIST;
2582
2583 inode = cgroup_new_inode(mode, sb);
2584 if (!inode)
2585 return -ENOMEM;
2586
2587 if (S_ISDIR(mode)) {
2588 inode->i_op = &cgroup_dir_inode_operations;
2589 inode->i_fop = &simple_dir_operations;
2590
2591 /* start off with i_nlink == 2 (for "." entry) */
2592 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002593 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002594
Tejun Heob8a2df62012-11-19 08:13:37 -08002595 /*
2596 * Control reaches here with cgroup_mutex held.
2597 * @inode->i_mutex should nest outside cgroup_mutex but we
2598 * want to populate it immediately without releasing
2599 * cgroup_mutex. As @inode isn't visible to anyone else
2600 * yet, trylock will always succeed without affecting
2601 * lockdep checks.
2602 */
2603 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002604 } else if (S_ISREG(mode)) {
2605 inode->i_size = 0;
2606 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002607 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002608 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609 d_instantiate(dentry, inode);
2610 dget(dentry); /* Extra count - pin the dentry in core */
2611 return 0;
2612}
2613
Li Zefan099fca32009-04-02 16:57:29 -07002614/**
2615 * cgroup_file_mode - deduce file mode of a control file
2616 * @cft: the control file in question
2617 *
2618 * returns cft->mode if ->mode is not 0
2619 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2620 * returns S_IRUGO if it has only a read handler
2621 * returns S_IWUSR if it has only a write hander
2622 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002623static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002624{
Al Viroa5e7ed32011-07-26 01:55:55 -04002625 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002626
2627 if (cft->mode)
2628 return cft->mode;
2629
Tejun Heo2da8ca82013-12-05 12:28:04 -05002630 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002631 mode |= S_IRUGO;
2632
Tejun Heo6e0755b2013-12-05 12:28:03 -05002633 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2634 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002635 mode |= S_IWUSR;
2636
2637 return mode;
2638}
2639
Tejun Heo2bb566c2013-08-08 20:11:23 -04002640static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641{
Paul Menagebd89aab2007-10-18 23:40:44 -07002642 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002643 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002644 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002645 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002646 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002647 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002649
Tejun Heo9fa4db32013-08-26 18:40:56 -04002650 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2651 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002652 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002653 strcat(name, ".");
2654 }
2655 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002656
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002658
2659 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2660 if (!cfe)
2661 return -ENOMEM;
2662
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002664 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002666 goto out;
2667 }
2668
Li Zefand6cbf352013-05-14 19:44:20 +08002669 cfe->type = (void *)cft;
2670 cfe->dentry = dentry;
2671 dentry->d_fsdata = cfe;
2672 simple_xattrs_init(&cfe->xattrs);
2673
Tejun Heo05ef1d72012-04-01 12:09:56 -07002674 mode = cgroup_file_mode(cft);
2675 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2676 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002677 list_add_tail(&cfe->node, &parent->files);
2678 cfe = NULL;
2679 }
2680 dput(dentry);
2681out:
2682 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 return error;
2684}
2685
Tejun Heob1f28d32013-06-28 16:24:10 -07002686/**
2687 * cgroup_addrm_files - add or remove files to a cgroup directory
2688 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002689 * @cfts: array of cftypes to be added
2690 * @is_add: whether to add or remove
2691 *
2692 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002693 * For removals, this function never fails. If addition fails, this
2694 * function doesn't remove files already added. The caller is responsible
2695 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002696 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002697static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2698 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002699{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002700 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002701 int ret;
2702
2703 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2704 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002705
2706 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002707 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002708 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2709 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002710 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2711 continue;
2712 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2713 continue;
2714
Li Zefan2739d3c2013-01-21 18:18:33 +08002715 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002716 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002717 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002718 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002719 cft->name, ret);
2720 return ret;
2721 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002722 } else {
2723 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002724 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002725 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002726 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727}
2728
Tejun Heo8e3f6542012-04-01 12:09:55 -07002729static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002730 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002731{
2732 /*
2733 * Thanks to the entanglement with vfs inode locking, we can't walk
2734 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002735 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2736 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002737 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002738 mutex_lock(&cgroup_mutex);
2739}
2740
Tejun Heo2bb566c2013-08-08 20:11:23 -04002741static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002742 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002743{
2744 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002745 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002746 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002747 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002748 struct dentry *prev = NULL;
2749 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002750 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002751 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002752 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002753
2754 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002755 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002756 !atomic_inc_not_zero(&sb->s_active)) {
2757 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002758 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759 }
2760
Li Zefane8c82d22013-06-18 18:48:37 +08002761 /*
2762 * All cgroups which are created after we drop cgroup_mutex will
2763 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002764 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002765 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002766 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002767
Li Zefane8c82d22013-06-18 18:48:37 +08002768 /* add/rm files for all cgroups created before */
Tejun Heoca8bdca2013-08-26 18:40:56 -04002769 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002770 struct cgroup *cgrp = css->cgroup;
2771
Li Zefane8c82d22013-06-18 18:48:37 +08002772 if (cgroup_is_dead(cgrp))
2773 continue;
2774
2775 inode = cgrp->dentry->d_inode;
2776 dget(cgrp->dentry);
Li Zefane8c82d22013-06-18 18:48:37 +08002777 dput(prev);
2778 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002779
Tejun Heo48573a82014-02-08 10:26:34 -05002780 mutex_unlock(&cgroup_mutex);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002781 mutex_lock(&inode->i_mutex);
2782 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002783 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002784 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785 mutex_unlock(&inode->i_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002786 if (ret)
2787 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002788 }
Tejun Heo48573a82014-02-08 10:26:34 -05002789 mutex_unlock(&cgroup_mutex);
Li Zefane8c82d22013-06-18 18:48:37 +08002790 dput(prev);
2791 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002792 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793}
2794
2795/**
2796 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2797 * @ss: target cgroup subsystem
2798 * @cfts: zero-length name terminated array of cftypes
2799 *
2800 * Register @cfts to @ss. Files described by @cfts are created for all
2801 * existing cgroups to which @ss is attached and all future cgroups will
2802 * have them too. This function can be called anytime whether @ss is
2803 * attached or not.
2804 *
2805 * Returns 0 on successful registration, -errno on failure. Note that this
2806 * function currently returns 0 as long as @cfts registration is successful
2807 * even if some file creation attempts on existing cgroups fail.
2808 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002809int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002810{
2811 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002812 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002813 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002814
2815 set = kzalloc(sizeof(*set), GFP_KERNEL);
2816 if (!set)
2817 return -ENOMEM;
2818
Tejun Heo2bb566c2013-08-08 20:11:23 -04002819 for (cft = cfts; cft->name[0] != '\0'; cft++)
2820 cft->ss = ss;
2821
Tejun Heo8e3f6542012-04-01 12:09:55 -07002822 cgroup_cfts_prepare();
2823 set->cfts = cfts;
2824 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002825 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002826 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002827 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002828 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829}
2830EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2831
Li Zefana043e3b2008-02-23 15:24:09 -08002832/**
Tejun Heo79578622012-04-01 12:09:56 -07002833 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002834 * @cfts: zero-length name terminated array of cftypes
2835 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002836 * Unregister @cfts. Files described by @cfts are removed from all
2837 * existing cgroups and all future cgroups won't have them either. This
2838 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002839 *
2840 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002841 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002842 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002843int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002844{
2845 struct cftype_set *set;
2846
Tejun Heo2bb566c2013-08-08 20:11:23 -04002847 if (!cfts || !cfts[0].ss)
2848 return -ENOENT;
2849
Tejun Heo79578622012-04-01 12:09:56 -07002850 cgroup_cfts_prepare();
2851
Tejun Heo2bb566c2013-08-08 20:11:23 -04002852 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002853 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002854 list_del(&set->node);
2855 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002856 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002857 return 0;
2858 }
2859 }
2860
Tejun Heo2bb566c2013-08-08 20:11:23 -04002861 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002862 return -ENOENT;
2863}
2864
2865/**
Li Zefana043e3b2008-02-23 15:24:09 -08002866 * cgroup_task_count - count the number of tasks in a cgroup.
2867 * @cgrp: the cgroup in question
2868 *
2869 * Return the number of tasks in the cgroup.
2870 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002871int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002872{
2873 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002874 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002875
Paul Menage817929e2007-10-18 23:39:36 -07002876 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002877 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2878 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002879 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002880 return count;
2881}
2882
2883/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002884 * To reduce the fork() overhead for systems that are not actually using
2885 * their cgroups capability, we don't maintain the lists running through
2886 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002887 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002888 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002889static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002890{
2891 struct task_struct *p, *g;
2892 write_lock(&css_set_lock);
2893 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002894 /*
2895 * We need tasklist_lock because RCU is not safe against
2896 * while_each_thread(). Besides, a forking task that has passed
2897 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2898 * is not guaranteed to have its child immediately visible in the
2899 * tasklist if we walk through it with RCU.
2900 */
2901 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002902 do_each_thread(g, p) {
2903 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002904 /*
2905 * We should check if the process is exiting, otherwise
2906 * it will race with cgroup_exit() in that the list
2907 * entry won't be deleted though the process has exited.
Tejun Heo532de3f2014-02-13 13:29:31 -05002908 * Do it while holding siglock so that we don't end up
2909 * racing against cgroup_exit().
Li Zefan0e043882008-04-17 11:37:15 +08002910 */
Tejun Heo532de3f2014-02-13 13:29:31 -05002911 spin_lock_irq(&p->sighand->siglock);
Li Zefan0e043882008-04-17 11:37:15 +08002912 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002913 list_add(&p->cg_list, &task_css_set(p)->tasks);
Tejun Heo532de3f2014-02-13 13:29:31 -05002914 spin_unlock_irq(&p->sighand->siglock);
2915
Cliff Wickman31a7df02008-02-07 00:14:42 -08002916 task_unlock(p);
2917 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002918 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002919 write_unlock(&css_set_lock);
2920}
2921
Tejun Heo574bd9f2012-11-09 09:12:29 -08002922/**
Tejun Heo492eb212013-08-08 20:11:25 -04002923 * css_next_child - find the next child of a given css
2924 * @pos_css: the current position (%NULL to initiate traversal)
2925 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002926 *
Tejun Heo492eb212013-08-08 20:11:25 -04002927 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002928 * under either cgroup_mutex or RCU read lock. The only requirement is
2929 * that @parent_css and @pos_css are accessible. The next sibling is
2930 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002931 */
Tejun Heo492eb212013-08-08 20:11:25 -04002932struct cgroup_subsys_state *
2933css_next_child(struct cgroup_subsys_state *pos_css,
2934 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002935{
Tejun Heo492eb212013-08-08 20:11:25 -04002936 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2937 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002938 struct cgroup *next;
2939
Tejun Heo87fb54f2013-12-06 15:11:55 -05002940 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002941
2942 /*
2943 * @pos could already have been removed. Once a cgroup is removed,
2944 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002945 * changes. As CGRP_DEAD assertion is serialized and happens
2946 * before the cgroup is taken off the ->sibling list, if we see it
2947 * unasserted, it's guaranteed that the next sibling hasn't
2948 * finished its grace period even if it's already removed, and thus
2949 * safe to dereference from this RCU critical section. If
2950 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2951 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002952 *
2953 * If @pos is dead, its next pointer can't be dereferenced;
2954 * however, as each cgroup is given a monotonically increasing
2955 * unique serial number and always appended to the sibling list,
2956 * the next one can be found by walking the parent's children until
2957 * we see a cgroup with higher serial number than @pos's. While
2958 * this path can be slower, it's taken only when either the current
2959 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002960 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002961 if (!pos) {
2962 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2963 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002964 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002965 } else {
2966 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2967 if (next->serial_nr > pos->serial_nr)
2968 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002969 }
2970
Tejun Heo492eb212013-08-08 20:11:25 -04002971 if (&next->sibling == &cgrp->children)
2972 return NULL;
2973
Tejun Heoca8bdca2013-08-26 18:40:56 -04002974 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002975}
Tejun Heo492eb212013-08-08 20:11:25 -04002976EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002977
2978/**
Tejun Heo492eb212013-08-08 20:11:25 -04002979 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002981 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002982 *
Tejun Heo492eb212013-08-08 20:11:25 -04002983 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002984 * to visit for pre-order traversal of @root's descendants. @root is
2985 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002986 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002987 * While this function requires cgroup_mutex or RCU read locking, it
2988 * doesn't require the whole traversal to be contained in a single critical
2989 * section. This function will return the correct next descendant as long
2990 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002991 */
Tejun Heo492eb212013-08-08 20:11:25 -04002992struct cgroup_subsys_state *
2993css_next_descendant_pre(struct cgroup_subsys_state *pos,
2994 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002995{
Tejun Heo492eb212013-08-08 20:11:25 -04002996 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002997
Tejun Heo87fb54f2013-12-06 15:11:55 -05002998 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002999
Tejun Heobd8815a2013-08-08 20:11:27 -04003000 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003001 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003002 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003003
3004 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003005 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003006 if (next)
3007 return next;
3008
3009 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003010 while (pos != root) {
3011 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003012 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003013 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003014 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003015 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003016
3017 return NULL;
3018}
Tejun Heo492eb212013-08-08 20:11:25 -04003019EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003020
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003021/**
Tejun Heo492eb212013-08-08 20:11:25 -04003022 * css_rightmost_descendant - return the rightmost descendant of a css
3023 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003024 *
Tejun Heo492eb212013-08-08 20:11:25 -04003025 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3026 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003027 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003028 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003029 * While this function requires cgroup_mutex or RCU read locking, it
3030 * doesn't require the whole traversal to be contained in a single critical
3031 * section. This function will return the correct rightmost descendant as
3032 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003033 */
Tejun Heo492eb212013-08-08 20:11:25 -04003034struct cgroup_subsys_state *
3035css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003036{
Tejun Heo492eb212013-08-08 20:11:25 -04003037 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003038
Tejun Heo87fb54f2013-12-06 15:11:55 -05003039 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003040
3041 do {
3042 last = pos;
3043 /* ->prev isn't RCU safe, walk ->next till the end */
3044 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003045 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003046 pos = tmp;
3047 } while (pos);
3048
3049 return last;
3050}
Tejun Heo492eb212013-08-08 20:11:25 -04003051EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003052
Tejun Heo492eb212013-08-08 20:11:25 -04003053static struct cgroup_subsys_state *
3054css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003055{
Tejun Heo492eb212013-08-08 20:11:25 -04003056 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057
3058 do {
3059 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003060 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061 } while (pos);
3062
3063 return last;
3064}
3065
3066/**
Tejun Heo492eb212013-08-08 20:11:25 -04003067 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003068 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003069 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003070 *
Tejun Heo492eb212013-08-08 20:11:25 -04003071 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003072 * to visit for post-order traversal of @root's descendants. @root is
3073 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003074 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003075 * While this function requires cgroup_mutex or RCU read locking, it
3076 * doesn't require the whole traversal to be contained in a single critical
3077 * section. This function will return the correct next descendant as long
3078 * as both @pos and @cgroup are accessible and @pos is a descendant of
3079 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003080 */
Tejun Heo492eb212013-08-08 20:11:25 -04003081struct cgroup_subsys_state *
3082css_next_descendant_post(struct cgroup_subsys_state *pos,
3083 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003084{
Tejun Heo492eb212013-08-08 20:11:25 -04003085 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086
Tejun Heo87fb54f2013-12-06 15:11:55 -05003087 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088
Tejun Heo58b79a92013-09-06 15:31:08 -04003089 /* if first iteration, visit leftmost descendant which may be @root */
3090 if (!pos)
3091 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003092
Tejun Heobd8815a2013-08-08 20:11:27 -04003093 /* if we visited @root, we're done */
3094 if (pos == root)
3095 return NULL;
3096
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003098 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003099 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003100 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003101
3102 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003103 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003104}
Tejun Heo492eb212013-08-08 20:11:25 -04003105EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003106
Tejun Heo0942eee2013-08-08 20:11:26 -04003107/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003108 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003109 * @it: the iterator to advance
3110 *
3111 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003112 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003113static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003114{
3115 struct list_head *l = it->cset_link;
3116 struct cgrp_cset_link *link;
3117 struct css_set *cset;
3118
3119 /* Advance to the next non-empty css_set */
3120 do {
3121 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003122 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003123 it->cset_link = NULL;
3124 return;
3125 }
3126 link = list_entry(l, struct cgrp_cset_link, cset_link);
3127 cset = link->cset;
3128 } while (list_empty(&cset->tasks));
3129 it->cset_link = l;
3130 it->task = cset->tasks.next;
3131}
3132
Tejun Heo0942eee2013-08-08 20:11:26 -04003133/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003134 * css_task_iter_start - initiate task iteration
3135 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003136 * @it: the task iterator to use
3137 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003138 * Initiate iteration through the tasks of @css. The caller can call
3139 * css_task_iter_next() to walk through the tasks until the function
3140 * returns NULL. On completion of iteration, css_task_iter_end() must be
3141 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003142 *
3143 * Note that this function acquires a lock which is released when the
3144 * iteration finishes. The caller can't sleep while iteration is in
3145 * progress.
3146 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003147void css_task_iter_start(struct cgroup_subsys_state *css,
3148 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003149 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003150{
3151 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003152 * The first time anyone tries to iterate across a css, we need to
3153 * enable the list linking each css_set to its tasks, and fix up
3154 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003155 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003156 if (!use_task_css_set_links)
3157 cgroup_enable_task_cg_lists();
3158
Paul Menage817929e2007-10-18 23:39:36 -07003159 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003160
Tejun Heo72ec7022013-08-08 20:11:26 -04003161 it->origin_css = css;
3162 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003163
Tejun Heo72ec7022013-08-08 20:11:26 -04003164 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003165}
3166
Tejun Heo0942eee2013-08-08 20:11:26 -04003167/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003168 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003169 * @it: the task iterator being iterated
3170 *
3171 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003172 * initialized via css_task_iter_start(). Returns NULL when the iteration
3173 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003174 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003175struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003176{
3177 struct task_struct *res;
3178 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003179 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003180
3181 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003182 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003183 return NULL;
3184 res = list_entry(l, struct task_struct, cg_list);
3185 /* Advance iterator to find next entry */
3186 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003187 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3188 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003189 /*
3190 * We reached the end of this task list - move on to the
3191 * next cgrp_cset_link.
3192 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003193 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003194 } else {
3195 it->task = l;
3196 }
3197 return res;
3198}
3199
Tejun Heo0942eee2013-08-08 20:11:26 -04003200/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003201 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003202 * @it: the task iterator to finish
3203 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003204 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003205 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003206void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003207 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003208{
3209 read_unlock(&css_set_lock);
3210}
3211
Cliff Wickman31a7df02008-02-07 00:14:42 -08003212static inline int started_after_time(struct task_struct *t1,
3213 struct timespec *time,
3214 struct task_struct *t2)
3215{
3216 int start_diff = timespec_compare(&t1->start_time, time);
3217 if (start_diff > 0) {
3218 return 1;
3219 } else if (start_diff < 0) {
3220 return 0;
3221 } else {
3222 /*
3223 * Arbitrarily, if two processes started at the same
3224 * time, we'll say that the lower pointer value
3225 * started first. Note that t2 may have exited by now
3226 * so this may not be a valid pointer any longer, but
3227 * that's fine - it still serves to distinguish
3228 * between two tasks started (effectively) simultaneously.
3229 */
3230 return t1 > t2;
3231 }
3232}
3233
3234/*
3235 * This function is a callback from heap_insert() and is used to order
3236 * the heap.
3237 * In this case we order the heap in descending task start time.
3238 */
3239static inline int started_after(void *p1, void *p2)
3240{
3241 struct task_struct *t1 = p1;
3242 struct task_struct *t2 = p2;
3243 return started_after_time(t1, &t2->start_time, t2);
3244}
3245
3246/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003247 * css_scan_tasks - iterate though all the tasks in a css
3248 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003249 * @test: optional test callback
3250 * @process: process callback
3251 * @data: data passed to @test and @process
3252 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003253 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003254 * Iterate through all the tasks in @css, calling @test for each, and if it
3255 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003256 *
Tejun Heoe5358372013-08-08 20:11:26 -04003257 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003258 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003259 * lock css_set_lock for the call to @process.
3260 *
3261 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003262 * of @css for the duration of this call. This function may or may not
3263 * call @process for tasks that exit or move to a different css during the
3264 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003265 *
3266 * Note that @test may be called with locks held, and may in some
3267 * situations be called multiple times for the same task, so it should be
3268 * cheap.
3269 *
3270 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3271 * heap operations (and its "gt" member will be overwritten), else a
3272 * temporary heap will be used (allocation of which may cause this function
3273 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003274 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003275int css_scan_tasks(struct cgroup_subsys_state *css,
3276 bool (*test)(struct task_struct *, void *),
3277 void (*process)(struct task_struct *, void *),
3278 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279{
3280 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003281 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003282 struct task_struct *p, *dropped;
3283 /* Never dereference latest_task, since it's not refcounted */
3284 struct task_struct *latest_task = NULL;
3285 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003286 struct timespec latest_time = { 0, 0 };
3287
Tejun Heoe5358372013-08-08 20:11:26 -04003288 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003289 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003290 heap->gt = &started_after;
3291 } else {
3292 /* We need to allocate our own heap memory */
3293 heap = &tmp_heap;
3294 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3295 if (retval)
3296 /* cannot allocate the heap */
3297 return retval;
3298 }
3299
3300 again:
3301 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003302 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003303 * which are of interest, and invoking @process callback on the
3304 * ones which need an update. Since we don't want to hold any
3305 * locks during the task updates, gather tasks to be processed in a
3306 * heap structure. The heap is sorted by descending task start
3307 * time. If the statically-sized heap fills up, we overflow tasks
3308 * that started later, and in future iterations only consider tasks
3309 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003310 * guarantees forward progress and that we don't miss any tasks.
3311 */
3312 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003313 css_task_iter_start(css, &it);
3314 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003315 /*
3316 * Only affect tasks that qualify per the caller's callback,
3317 * if he provided one
3318 */
Tejun Heoe5358372013-08-08 20:11:26 -04003319 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003320 continue;
3321 /*
3322 * Only process tasks that started after the last task
3323 * we processed
3324 */
3325 if (!started_after_time(p, &latest_time, latest_task))
3326 continue;
3327 dropped = heap_insert(heap, p);
3328 if (dropped == NULL) {
3329 /*
3330 * The new task was inserted; the heap wasn't
3331 * previously full
3332 */
3333 get_task_struct(p);
3334 } else if (dropped != p) {
3335 /*
3336 * The new task was inserted, and pushed out a
3337 * different task
3338 */
3339 get_task_struct(p);
3340 put_task_struct(dropped);
3341 }
3342 /*
3343 * Else the new task was newer than anything already in
3344 * the heap and wasn't inserted
3345 */
3346 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003347 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003348
3349 if (heap->size) {
3350 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003351 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003352 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003353 latest_time = q->start_time;
3354 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003355 }
3356 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003357 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003358 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003359 }
3360 /*
3361 * If we had to process any tasks at all, scan again
3362 * in case some of them were in the middle of forking
3363 * children that didn't get processed.
3364 * Not the most efficient way to do it, but it avoids
3365 * having to take callback_mutex in the fork path
3366 */
3367 goto again;
3368 }
3369 if (heap == &tmp_heap)
3370 heap_free(&tmp_heap);
3371 return 0;
3372}
3373
Tejun Heoe5358372013-08-08 20:11:26 -04003374static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003375{
Tejun Heoe5358372013-08-08 20:11:26 -04003376 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003377
Tejun Heo47cfcd02013-04-07 09:29:51 -07003378 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003379 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003380 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003381}
3382
3383/**
3384 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3385 * @to: cgroup to which the tasks will be moved
3386 * @from: cgroup in which the tasks currently reside
3387 */
3388int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3389{
Tejun Heo72ec7022013-08-08 20:11:26 -04003390 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3391 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003392}
3393
Paul Menage817929e2007-10-18 23:39:36 -07003394/*
Ben Blum102a7752009-09-23 15:56:26 -07003395 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003396 *
3397 * Reading this file can return large amounts of data if a cgroup has
3398 * *lots* of attached tasks. So it may need several calls to read(),
3399 * but we cannot guarantee that the information we produce is correct
3400 * unless we produce it entirely atomically.
3401 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003402 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003403
Li Zefan24528252012-01-20 11:58:43 +08003404/* which pidlist file are we talking about? */
3405enum cgroup_filetype {
3406 CGROUP_FILE_PROCS,
3407 CGROUP_FILE_TASKS,
3408};
3409
3410/*
3411 * A pidlist is a list of pids that virtually represents the contents of one
3412 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3413 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3414 * to the cgroup.
3415 */
3416struct cgroup_pidlist {
3417 /*
3418 * used to find which pidlist is wanted. doesn't change as long as
3419 * this particular list stays in the list.
3420 */
3421 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3422 /* array of xids */
3423 pid_t *list;
3424 /* how many elements the above list has */
3425 int length;
Li Zefan24528252012-01-20 11:58:43 +08003426 /* each of these stored in a list by its cgroup */
3427 struct list_head links;
3428 /* pointer to the cgroup we belong to, for list removal purposes */
3429 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003430 /* for delayed destruction */
3431 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003432};
3433
Paul Menagebbcb81d2007-10-18 23:39:32 -07003434/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003435 * The following two functions "fix" the issue where there are more pids
3436 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3437 * TODO: replace with a kernel-wide solution to this problem
3438 */
3439#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3440static void *pidlist_allocate(int count)
3441{
3442 if (PIDLIST_TOO_LARGE(count))
3443 return vmalloc(count * sizeof(pid_t));
3444 else
3445 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3446}
Tejun Heob1a21362013-11-29 10:42:58 -05003447
Ben Blumd1d9fd32009-09-23 15:56:28 -07003448static void pidlist_free(void *p)
3449{
3450 if (is_vmalloc_addr(p))
3451 vfree(p);
3452 else
3453 kfree(p);
3454}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003455
3456/*
Tejun Heob1a21362013-11-29 10:42:58 -05003457 * Used to destroy all pidlists lingering waiting for destroy timer. None
3458 * should be left afterwards.
3459 */
3460static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3461{
3462 struct cgroup_pidlist *l, *tmp_l;
3463
3464 mutex_lock(&cgrp->pidlist_mutex);
3465 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3466 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3467 mutex_unlock(&cgrp->pidlist_mutex);
3468
3469 flush_workqueue(cgroup_pidlist_destroy_wq);
3470 BUG_ON(!list_empty(&cgrp->pidlists));
3471}
3472
3473static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3474{
3475 struct delayed_work *dwork = to_delayed_work(work);
3476 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3477 destroy_dwork);
3478 struct cgroup_pidlist *tofree = NULL;
3479
3480 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003481
3482 /*
Tejun Heo04502362013-11-29 10:42:59 -05003483 * Destroy iff we didn't get queued again. The state won't change
3484 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003485 */
Tejun Heo04502362013-11-29 10:42:59 -05003486 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003487 list_del(&l->links);
3488 pidlist_free(l->list);
3489 put_pid_ns(l->key.ns);
3490 tofree = l;
3491 }
3492
Tejun Heob1a21362013-11-29 10:42:58 -05003493 mutex_unlock(&l->owner->pidlist_mutex);
3494 kfree(tofree);
3495}
3496
3497/*
Ben Blum102a7752009-09-23 15:56:26 -07003498 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003499 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003500 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003501static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003502{
Ben Blum102a7752009-09-23 15:56:26 -07003503 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003504
3505 /*
3506 * we presume the 0th element is unique, so i starts at 1. trivial
3507 * edge cases first; no work needs to be done for either
3508 */
3509 if (length == 0 || length == 1)
3510 return length;
3511 /* src and dest walk down the list; dest counts unique elements */
3512 for (src = 1; src < length; src++) {
3513 /* find next unique element */
3514 while (list[src] == list[src-1]) {
3515 src++;
3516 if (src == length)
3517 goto after;
3518 }
3519 /* dest always points to where the next unique element goes */
3520 list[dest] = list[src];
3521 dest++;
3522 }
3523after:
Ben Blum102a7752009-09-23 15:56:26 -07003524 return dest;
3525}
3526
Tejun Heoafb2bc12013-11-29 10:42:59 -05003527/*
3528 * The two pid files - task and cgroup.procs - guaranteed that the result
3529 * is sorted, which forced this whole pidlist fiasco. As pid order is
3530 * different per namespace, each namespace needs differently sorted list,
3531 * making it impossible to use, for example, single rbtree of member tasks
3532 * sorted by task pointer. As pidlists can be fairly large, allocating one
3533 * per open file is dangerous, so cgroup had to implement shared pool of
3534 * pidlists keyed by cgroup and namespace.
3535 *
3536 * All this extra complexity was caused by the original implementation
3537 * committing to an entirely unnecessary property. In the long term, we
3538 * want to do away with it. Explicitly scramble sort order if
3539 * sane_behavior so that no such expectation exists in the new interface.
3540 *
3541 * Scrambling is done by swapping every two consecutive bits, which is
3542 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3543 */
3544static pid_t pid_fry(pid_t pid)
3545{
3546 unsigned a = pid & 0x55555555;
3547 unsigned b = pid & 0xAAAAAAAA;
3548
3549 return (a << 1) | (b >> 1);
3550}
3551
3552static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3553{
3554 if (cgroup_sane_behavior(cgrp))
3555 return pid_fry(pid);
3556 else
3557 return pid;
3558}
3559
Ben Blum102a7752009-09-23 15:56:26 -07003560static int cmppid(const void *a, const void *b)
3561{
3562 return *(pid_t *)a - *(pid_t *)b;
3563}
3564
Tejun Heoafb2bc12013-11-29 10:42:59 -05003565static int fried_cmppid(const void *a, const void *b)
3566{
3567 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3568}
3569
Ben Blum72a8cb32009-09-23 15:56:27 -07003570static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3571 enum cgroup_filetype type)
3572{
3573 struct cgroup_pidlist *l;
3574 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003575 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003576
Tejun Heoe6b81712013-11-29 10:42:59 -05003577 lockdep_assert_held(&cgrp->pidlist_mutex);
3578
3579 list_for_each_entry(l, &cgrp->pidlists, links)
3580 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003581 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003582 return NULL;
3583}
3584
Ben Blum72a8cb32009-09-23 15:56:27 -07003585/*
3586 * find the appropriate pidlist for our purpose (given procs vs tasks)
3587 * returns with the lock on that pidlist already held, and takes care
3588 * of the use count, or returns NULL with no locks held if we're out of
3589 * memory.
3590 */
Tejun Heoe6b81712013-11-29 10:42:59 -05003591static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3592 enum cgroup_filetype type)
Ben Blum72a8cb32009-09-23 15:56:27 -07003593{
3594 struct cgroup_pidlist *l;
Ben Blum72a8cb32009-09-23 15:56:27 -07003595
Tejun Heoe6b81712013-11-29 10:42:59 -05003596 lockdep_assert_held(&cgrp->pidlist_mutex);
3597
3598 l = cgroup_pidlist_find(cgrp, type);
3599 if (l)
3600 return l;
3601
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003603 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003604 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003605 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003606
Tejun Heob1a21362013-11-29 10:42:58 -05003607 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003608 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003609 /* don't need task_nsproxy() if we're looking at ourself */
3610 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003611 l->owner = cgrp;
3612 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003613 return l;
3614}
3615
3616/*
Ben Blum102a7752009-09-23 15:56:26 -07003617 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3618 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003619static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3620 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003621{
3622 pid_t *array;
3623 int length;
3624 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003625 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003626 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003627 struct cgroup_pidlist *l;
3628
Tejun Heo4bac00d2013-11-29 10:42:59 -05003629 lockdep_assert_held(&cgrp->pidlist_mutex);
3630
Ben Blum102a7752009-09-23 15:56:26 -07003631 /*
3632 * If cgroup gets more users after we read count, we won't have
3633 * enough space - tough. This race is indistinguishable to the
3634 * caller from the case that the additional cgroup users didn't
3635 * show up until sometime later on.
3636 */
3637 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003638 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003639 if (!array)
3640 return -ENOMEM;
3641 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003642 css_task_iter_start(&cgrp->dummy_css, &it);
3643 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003644 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003645 break;
Ben Blum102a7752009-09-23 15:56:26 -07003646 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 if (type == CGROUP_FILE_PROCS)
3648 pid = task_tgid_vnr(tsk);
3649 else
3650 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003651 if (pid > 0) /* make sure to only use valid results */
3652 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003653 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003654 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003655 length = n;
3656 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003657 if (cgroup_sane_behavior(cgrp))
3658 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3659 else
3660 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003661 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003662 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003663
Tejun Heoe6b81712013-11-29 10:42:59 -05003664 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003665 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003666 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003667 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003668 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003669 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003670
3671 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003672 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003673 l->list = array;
3674 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003675 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003676 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003677}
3678
Balbir Singh846c7bb2007-10-18 23:39:44 -07003679/**
Li Zefana043e3b2008-02-23 15:24:09 -08003680 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003681 * @stats: cgroupstats to fill information into
3682 * @dentry: A dentry entry belonging to the cgroup for which stats have
3683 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003684 *
3685 * Build and fill cgroupstats so that taskstats can export it to user
3686 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687 */
3688int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3689{
3690 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003691 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003692 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003693 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003694
Balbir Singh846c7bb2007-10-18 23:39:44 -07003695 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003696 * Validate dentry by checking the superblock operations,
3697 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003698 */
Li Zefan33d283b2008-11-19 15:36:48 -08003699 if (dentry->d_sb->s_op != &cgroup_ops ||
3700 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003701 goto err;
3702
3703 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003704 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003705
Tejun Heo72ec7022013-08-08 20:11:26 -04003706 css_task_iter_start(&cgrp->dummy_css, &it);
3707 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003708 switch (tsk->state) {
3709 case TASK_RUNNING:
3710 stats->nr_running++;
3711 break;
3712 case TASK_INTERRUPTIBLE:
3713 stats->nr_sleeping++;
3714 break;
3715 case TASK_UNINTERRUPTIBLE:
3716 stats->nr_uninterruptible++;
3717 break;
3718 case TASK_STOPPED:
3719 stats->nr_stopped++;
3720 break;
3721 default:
3722 if (delayacct_is_task_waiting_on_io(tsk))
3723 stats->nr_io_wait++;
3724 break;
3725 }
3726 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003727 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003728
Balbir Singh846c7bb2007-10-18 23:39:44 -07003729err:
3730 return ret;
3731}
3732
Paul Menage8f3ff202009-09-23 15:56:25 -07003733
Paul Menagecc31edc2008-10-18 20:28:04 -07003734/*
Ben Blum102a7752009-09-23 15:56:26 -07003735 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003736 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003737 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 */
3739
Ben Blum102a7752009-09-23 15:56:26 -07003740static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003741{
3742 /*
3743 * Initially we receive a position value that corresponds to
3744 * one more than the last pid shown (or 0 on the first call or
3745 * after a seek to the start). Use a binary-search to find the
3746 * next pid to display, if any
3747 */
Tejun Heo5d224442013-12-05 12:28:04 -05003748 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003749 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003750 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003751 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003752 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003753 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003754
Tejun Heo4bac00d2013-11-29 10:42:59 -05003755 mutex_lock(&cgrp->pidlist_mutex);
3756
3757 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003758 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003759 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003760 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003761 * could already have been destroyed.
3762 */
Tejun Heo5d224442013-12-05 12:28:04 -05003763 if (of->priv)
3764 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003765
3766 /*
3767 * Either this is the first start() after open or the matching
3768 * pidlist has been destroyed inbetween. Create a new one.
3769 */
Tejun Heo5d224442013-12-05 12:28:04 -05003770 if (!of->priv) {
3771 ret = pidlist_array_load(cgrp, type,
3772 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003773 if (ret)
3774 return ERR_PTR(ret);
3775 }
Tejun Heo5d224442013-12-05 12:28:04 -05003776 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003777
Paul Menagecc31edc2008-10-18 20:28:04 -07003778 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003779 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003780
Paul Menagecc31edc2008-10-18 20:28:04 -07003781 while (index < end) {
3782 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003783 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 index = mid;
3785 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003786 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 index = mid + 1;
3788 else
3789 end = mid;
3790 }
3791 }
3792 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003793 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003794 return NULL;
3795 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003796 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003797 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003799}
3800
Ben Blum102a7752009-09-23 15:56:26 -07003801static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003802{
Tejun Heo5d224442013-12-05 12:28:04 -05003803 struct cgroup_open_file *of = s->private;
3804 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003805
Tejun Heo5d224442013-12-05 12:28:04 -05003806 if (l)
3807 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003808 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003809 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003810}
3811
Ben Blum102a7752009-09-23 15:56:26 -07003812static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003813{
Tejun Heo5d224442013-12-05 12:28:04 -05003814 struct cgroup_open_file *of = s->private;
3815 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003816 pid_t *p = v;
3817 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 /*
3819 * Advance to the next pid in the array. If this goes off the
3820 * end, we're done
3821 */
3822 p++;
3823 if (p >= end) {
3824 return NULL;
3825 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003826 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003827 return p;
3828 }
3829}
3830
Ben Blum102a7752009-09-23 15:56:26 -07003831static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003832{
3833 return seq_printf(s, "%d\n", *(int *)v);
3834}
3835
Ben Blum102a7752009-09-23 15:56:26 -07003836/*
3837 * seq_operations functions for iterating on pidlists through seq_file -
3838 * independent of whether it's tasks or procs
3839 */
3840static const struct seq_operations cgroup_pidlist_seq_operations = {
3841 .start = cgroup_pidlist_start,
3842 .stop = cgroup_pidlist_stop,
3843 .next = cgroup_pidlist_next,
3844 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003845};
3846
Tejun Heo182446d2013-08-08 20:11:24 -04003847static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3848 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003849{
Tejun Heo182446d2013-08-08 20:11:24 -04003850 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003851}
3852
Tejun Heo182446d2013-08-08 20:11:24 -04003853static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3854 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003855{
Tejun Heo182446d2013-08-08 20:11:24 -04003856 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003857 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003858 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003859 else
Tejun Heo182446d2013-08-08 20:11:24 -04003860 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003861 return 0;
3862}
3863
Paul Menagebbcb81d2007-10-18 23:39:32 -07003864/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003865 * When dput() is called asynchronously, if umount has been done and
3866 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3867 * there's a small window that vfs will see the root dentry with non-zero
3868 * refcnt and trigger BUG().
3869 *
3870 * That's why we hold a reference before dput() and drop it right after.
3871 */
3872static void cgroup_dput(struct cgroup *cgrp)
3873{
3874 struct super_block *sb = cgrp->root->sb;
3875
3876 atomic_inc(&sb->s_active);
3877 dput(cgrp->dentry);
3878 deactivate_super(sb);
3879}
3880
Tejun Heo182446d2013-08-08 20:11:24 -04003881static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3882 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003883{
Tejun Heo182446d2013-08-08 20:11:24 -04003884 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003885}
3886
Tejun Heo182446d2013-08-08 20:11:24 -04003887static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3888 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003889{
3890 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003891 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003892 else
Tejun Heo182446d2013-08-08 20:11:24 -04003893 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003894 return 0;
3895}
3896
Tejun Heod5c56ce2013-06-03 19:14:34 -07003897static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003898 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003899 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003900 .seq_start = cgroup_pidlist_start,
3901 .seq_next = cgroup_pidlist_next,
3902 .seq_stop = cgroup_pidlist_stop,
3903 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003904 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003905 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003906 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003907 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003908 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003909 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003910 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003911 .read_u64 = cgroup_clone_children_read,
3912 .write_u64 = cgroup_clone_children_write,
3913 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003914 {
Tejun Heo873fe092013-04-14 20:15:26 -07003915 .name = "cgroup.sane_behavior",
3916 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003917 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003918 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003919
3920 /*
3921 * Historical crazy stuff. These don't have "cgroup." prefix and
3922 * don't exist if sane_behavior. If you're depending on these, be
3923 * prepared to be burned.
3924 */
3925 {
3926 .name = "tasks",
3927 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003928 .seq_start = cgroup_pidlist_start,
3929 .seq_next = cgroup_pidlist_next,
3930 .seq_stop = cgroup_pidlist_stop,
3931 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003932 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003933 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003934 .mode = S_IRUGO | S_IWUSR,
3935 },
3936 {
3937 .name = "notify_on_release",
3938 .flags = CFTYPE_INSANE,
3939 .read_u64 = cgroup_read_notify_on_release,
3940 .write_u64 = cgroup_write_notify_on_release,
3941 },
Tejun Heo873fe092013-04-14 20:15:26 -07003942 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003943 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003944 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003945 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003946 .write_string = cgroup_release_agent_write,
3947 .max_write_len = PATH_MAX,
3948 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003949 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003950};
3951
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003952/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003953 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003954 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003955 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003956 *
3957 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003958 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003959static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003960{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003961 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003962 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003963
Tejun Heo8e3f6542012-04-01 12:09:55 -07003964 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003965 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003966 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003967
3968 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003969 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003970
Tejun Heobee55092013-06-28 16:24:11 -07003971 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003972 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003973 if (ret < 0)
3974 goto err;
3975 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003976 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003977 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003978err:
3979 cgroup_clear_dir(cgrp, subsys_mask);
3980 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003981}
3982
Tejun Heo0c21ead2013-08-13 20:22:51 -04003983/*
3984 * css destruction is four-stage process.
3985 *
3986 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3987 * Implemented in kill_css().
3988 *
3989 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3990 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3991 * by invoking offline_css(). After offlining, the base ref is put.
3992 * Implemented in css_killed_work_fn().
3993 *
3994 * 3. When the percpu_ref reaches zero, the only possible remaining
3995 * accessors are inside RCU read sections. css_release() schedules the
3996 * RCU callback.
3997 *
3998 * 4. After the grace period, the css can be freed. Implemented in
3999 * css_free_work_fn().
4000 *
4001 * It is actually hairier because both step 2 and 4 require process context
4002 * and thus involve punting to css->destroy_work adding two additional
4003 * steps to the already complex sequence.
4004 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004005static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004006{
4007 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004008 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004009 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004010
Tejun Heo0ae78e02013-08-13 11:01:54 -04004011 if (css->parent)
4012 css_put(css->parent);
4013
Tejun Heo0c21ead2013-08-13 20:22:51 -04004014 css->ss->css_free(css);
4015 cgroup_dput(cgrp);
4016}
4017
4018static void css_free_rcu_fn(struct rcu_head *rcu_head)
4019{
4020 struct cgroup_subsys_state *css =
4021 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4022
4023 /*
4024 * css holds an extra ref to @cgrp->dentry which is put on the last
4025 * css_put(). dput() requires process context which we don't have.
4026 */
4027 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004028 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004029}
4030
Tejun Heod3daf282013-06-13 19:39:16 -07004031static void css_release(struct percpu_ref *ref)
4032{
4033 struct cgroup_subsys_state *css =
4034 container_of(ref, struct cgroup_subsys_state, refcnt);
4035
Li Zefanc1a71502013-12-17 11:13:39 +08004036 rcu_assign_pointer(css->cgroup->subsys[css->ss->subsys_id], NULL);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004037 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004038}
4039
Tejun Heo623f9262013-08-13 11:01:55 -04004040static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4041 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004042{
Paul Menagebd89aab2007-10-18 23:40:44 -07004043 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004044 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004045 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004046
Tejun Heo0ae78e02013-08-13 11:01:54 -04004047 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004048 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004049 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004050 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004051
Tejun Heoca8bdca2013-08-26 18:40:56 -04004052 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004053}
4054
Li Zefan2a4ac632013-07-31 16:16:40 +08004055/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004056static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004057{
Tejun Heo623f9262013-08-13 11:01:55 -04004058 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004059 int ret = 0;
4060
Tejun Heoa31f2d32012-11-19 08:13:37 -08004061 lockdep_assert_held(&cgroup_mutex);
4062
Tejun Heo92fb9742012-11-19 08:13:38 -08004063 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004064 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004065 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004066 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004067 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004068 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4069 }
Tejun Heob1929db2012-11-19 08:13:38 -08004070 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004071}
4072
Li Zefan2a4ac632013-07-31 16:16:40 +08004073/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004074static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004075{
Tejun Heo623f9262013-08-13 11:01:55 -04004076 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077
4078 lockdep_assert_held(&cgroup_mutex);
4079
4080 if (!(css->flags & CSS_ONLINE))
4081 return;
4082
Li Zefand7eeac12013-03-12 15:35:59 -07004083 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004084 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004085
Tejun Heoeb954192013-08-08 20:11:23 -04004086 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004087 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004088 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004089}
4090
Tejun Heoc81c925a2013-12-06 15:11:56 -05004091/**
4092 * create_css - create a cgroup_subsys_state
4093 * @cgrp: the cgroup new css will be associated with
4094 * @ss: the subsys of new css
4095 *
4096 * Create a new css associated with @cgrp - @ss pair. On success, the new
4097 * css is online and installed in @cgrp with all interface files created.
4098 * Returns 0 on success, -errno on failure.
4099 */
4100static int create_css(struct cgroup *cgrp, struct cgroup_subsys *ss)
4101{
4102 struct cgroup *parent = cgrp->parent;
4103 struct cgroup_subsys_state *css;
4104 int err;
4105
4106 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
4107 lockdep_assert_held(&cgroup_mutex);
4108
4109 css = ss->css_alloc(cgroup_css(parent, ss));
4110 if (IS_ERR(css))
4111 return PTR_ERR(css);
4112
4113 err = percpu_ref_init(&css->refcnt, css_release);
4114 if (err)
4115 goto err_free;
4116
4117 init_css(css, ss, cgrp);
4118
4119 err = cgroup_populate_dir(cgrp, 1 << ss->subsys_id);
4120 if (err)
4121 goto err_free;
4122
4123 err = online_css(css);
4124 if (err)
4125 goto err_free;
4126
4127 dget(cgrp->dentry);
4128 css_get(css->parent);
4129
4130 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4131 parent->parent) {
4132 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",
4133 current->comm, current->pid, ss->name);
4134 if (!strcmp(ss->name, "memory"))
4135 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4136 ss->warned_broken_hierarchy = true;
4137 }
4138
4139 return 0;
4140
4141err_free:
4142 percpu_ref_cancel_init(&css->refcnt);
4143 ss->css_free(css);
4144 return err;
4145}
4146
Paul Menageddbcc7e2007-10-18 23:39:30 -07004147/*
Li Zefana043e3b2008-02-23 15:24:09 -08004148 * cgroup_create - create a cgroup
4149 * @parent: cgroup that will be parent of the new cgroup
4150 * @dentry: dentry of the new cgroup
4151 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004152 *
Li Zefana043e3b2008-02-23 15:24:09 -08004153 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004155static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004156 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157{
Paul Menagebd89aab2007-10-18 23:40:44 -07004158 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004159 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004160 struct cgroupfs_root *root = parent->root;
Tejun Heob58c8992014-02-08 10:26:33 -05004161 int ssid, err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162 struct cgroup_subsys *ss;
4163 struct super_block *sb = root->sb;
4164
Tejun Heo0a950f62012-11-19 09:02:12 -08004165 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004166 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4167 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004168 return -ENOMEM;
4169
Li Zefan65dff752013-03-01 15:01:56 +08004170 name = cgroup_alloc_name(dentry);
Tejun Heob58c8992014-02-08 10:26:33 -05004171 if (!name) {
4172 err = -ENOMEM;
Li Zefan65dff752013-03-01 15:01:56 +08004173 goto err_free_cgrp;
Tejun Heob58c8992014-02-08 10:26:33 -05004174 }
Li Zefan65dff752013-03-01 15:01:56 +08004175 rcu_assign_pointer(cgrp->name, name);
4176
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004177 /*
Tejun Heo976c06b2012-11-05 09:16:59 -08004178 * Only live parents can have children. Note that the liveliness
4179 * check isn't strictly necessary because cgroup_mkdir() and
4180 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4181 * anyway so that locking is contained inside cgroup proper and we
4182 * don't get nasty surprises if we ever grow another caller.
4183 */
4184 if (!cgroup_lock_live_group(parent)) {
4185 err = -ENODEV;
Li Zefan0ab02ca2014-02-11 16:05:46 +08004186 goto err_free_name;
4187 }
4188
4189 /*
4190 * Temporarily set the pointer to NULL, so idr_find() won't return
4191 * a half-baked cgroup.
4192 */
4193 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
4194 if (cgrp->id < 0) {
4195 err = -ENOMEM;
4196 goto err_unlock;
Tejun Heo976c06b2012-11-05 09:16:59 -08004197 }
4198
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199 /* Grab a reference on the superblock so the hierarchy doesn't
4200 * get deleted on unmount if there are child cgroups. This
4201 * can be done outside cgroup_mutex, since the sb can't
4202 * disappear while someone has an open control file on the
4203 * fs */
4204 atomic_inc(&sb->s_active);
4205
Paul Menagecc31edc2008-10-18 20:28:04 -07004206 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207
Li Zefanfe1c06c2013-01-24 14:30:22 +08004208 dentry->d_fsdata = cgrp;
4209 cgrp->dentry = dentry;
4210
Paul Menagebd89aab2007-10-18 23:40:44 -07004211 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004212 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214
Li Zefanb6abdb02008-03-04 14:28:19 -08004215 if (notify_on_release(parent))
4216 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4217
Tejun Heo2260e7f2012-11-19 08:13:38 -08004218 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4219 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004220
Tejun Heo4e139af2012-11-19 08:13:36 -08004221 /*
4222 * Create directory. cgroup_create_file() returns with the new
4223 * directory locked on success so that it can be populated without
4224 * dropping cgroup_mutex.
4225 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004226 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 if (err < 0)
Li Zefan0ab02ca2014-02-11 16:05:46 +08004228 goto err_free_id;
Tejun Heo4e139af2012-11-19 08:13:36 -08004229 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230
Tejun Heo00356bd2013-06-18 11:14:22 -07004231 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004232
Tejun Heo4e139af2012-11-19 08:13:36 -08004233 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004234 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4235 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004236
Li Zefan415cf072013-04-08 14:35:02 +08004237 /* hold a ref to the parent's dentry */
4238 dget(parent->dentry);
4239
Tejun Heo0d802552013-12-06 15:11:56 -05004240 /*
4241 * @cgrp is now fully operational. If something fails after this
4242 * point, it'll be released via the normal destruction path.
4243 */
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004244 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4245
Tejun Heo2bb566c2013-08-08 20:11:23 -04004246 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004247 if (err)
4248 goto err_destroy;
4249
Tejun Heo9d403e92013-12-06 15:11:56 -05004250 /* let's create and online css's */
Tejun Heob85d2042013-12-06 15:11:57 -05004251 for_each_subsys(ss, ssid) {
4252 if (root->subsys_mask & (1 << ssid)) {
4253 err = create_css(cgrp, ss);
4254 if (err)
4255 goto err_destroy;
4256 }
Tejun Heoa8638032012-11-09 09:12:29 -08004257 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258
4259 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004260 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004261
4262 return 0;
4263
Tejun Heo0a950f62012-11-19 09:02:12 -08004264err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004265 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan0ab02ca2014-02-11 16:05:46 +08004266 /* Release the reference count that we took on the superblock */
4267 deactivate_super(sb);
4268err_unlock:
4269 mutex_unlock(&cgroup_mutex);
Li Zefan65dff752013-03-01 15:01:56 +08004270err_free_name:
4271 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004272err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004273 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004275
4276err_destroy:
4277 cgroup_destroy_locked(cgrp);
4278 mutex_unlock(&cgroup_mutex);
4279 mutex_unlock(&dentry->d_inode->i_mutex);
4280 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281}
4282
Al Viro18bb1db2011-07-26 01:41:39 -04004283static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284{
4285 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4286
4287 /* the vfs holds inode->i_mutex already */
4288 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4289}
4290
Tejun Heo223dbc32013-08-13 20:22:50 -04004291/*
4292 * This is called when the refcnt of a css is confirmed to be killed.
4293 * css_tryget() is now guaranteed to fail.
4294 */
4295static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004296{
Tejun Heo223dbc32013-08-13 20:22:50 -04004297 struct cgroup_subsys_state *css =
4298 container_of(work, struct cgroup_subsys_state, destroy_work);
4299 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004300
Tejun Heof20104d2013-08-13 20:22:50 -04004301 mutex_lock(&cgroup_mutex);
4302
4303 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004304 * css_tryget() is guaranteed to fail now. Tell subsystems to
4305 * initate destruction.
4306 */
4307 offline_css(css);
4308
4309 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004310 * If @cgrp is marked dead, it's waiting for refs of all css's to
4311 * be disabled before proceeding to the second phase of cgroup
4312 * destruction. If we are the last one, kick it off.
4313 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004314 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004315 cgroup_destroy_css_killed(cgrp);
4316
4317 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004318
4319 /*
4320 * Put the css refs from kill_css(). Each css holds an extra
4321 * reference to the cgroup's dentry and cgroup removal proceeds
4322 * regardless of css refs. On the last put of each css, whenever
4323 * that may be, the extra dentry ref is put so that dentry
4324 * destruction happens only after all css's are released.
4325 */
4326 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004327}
4328
Tejun Heo223dbc32013-08-13 20:22:50 -04004329/* css kill confirmation processing requires process context, bounce */
4330static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004331{
4332 struct cgroup_subsys_state *css =
4333 container_of(ref, struct cgroup_subsys_state, refcnt);
4334
Tejun Heo223dbc32013-08-13 20:22:50 -04004335 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004336 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004337}
4338
4339/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004340 * kill_css - destroy a css
4341 * @css: css to destroy
4342 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004343 * This function initiates destruction of @css by removing cgroup interface
4344 * files and putting its base reference. ->css_offline() will be invoked
4345 * asynchronously once css_tryget() is guaranteed to fail and when the
4346 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004347 */
4348static void kill_css(struct cgroup_subsys_state *css)
4349{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004350 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4351
Tejun Heoedae0c32013-08-13 20:22:51 -04004352 /*
4353 * Killing would put the base ref, but we need to keep it alive
4354 * until after ->css_offline().
4355 */
4356 css_get(css);
4357
4358 /*
4359 * cgroup core guarantees that, by the time ->css_offline() is
4360 * invoked, no new css reference will be given out via
4361 * css_tryget(). We can't simply call percpu_ref_kill() and
4362 * proceed to offlining css's because percpu_ref_kill() doesn't
4363 * guarantee that the ref is seen as killed on all CPUs on return.
4364 *
4365 * Use percpu_ref_kill_and_confirm() to get notifications as each
4366 * css is confirmed to be seen as killed on all CPUs.
4367 */
4368 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004369}
4370
4371/**
4372 * cgroup_destroy_locked - the first stage of cgroup destruction
4373 * @cgrp: cgroup to be destroyed
4374 *
4375 * css's make use of percpu refcnts whose killing latency shouldn't be
4376 * exposed to userland and are RCU protected. Also, cgroup core needs to
4377 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4378 * invoked. To satisfy all the requirements, destruction is implemented in
4379 * the following two steps.
4380 *
4381 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4382 * userland visible parts and start killing the percpu refcnts of
4383 * css's. Set up so that the next stage will be kicked off once all
4384 * the percpu refcnts are confirmed to be killed.
4385 *
4386 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4387 * rest of destruction. Once all cgroup references are gone, the
4388 * cgroup is RCU-freed.
4389 *
4390 * This function implements s1. After this step, @cgrp is gone as far as
4391 * the userland is concerned and a new cgroup with the same name may be
4392 * created. As cgroup doesn't care about the names internally, this
4393 * doesn't cause any problem.
4394 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004395static int cgroup_destroy_locked(struct cgroup *cgrp)
4396 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004397{
Tejun Heo42809dd2012-11-19 08:13:37 -08004398 struct dentry *d = cgrp->dentry;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004399 struct cgroup_subsys_state *css;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004400 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004401 bool empty;
Tejun Heo1c6727a2013-12-06 15:11:56 -05004402 int ssid;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403
Tejun Heo42809dd2012-11-19 08:13:37 -08004404 lockdep_assert_held(&d->d_inode->i_mutex);
4405 lockdep_assert_held(&cgroup_mutex);
4406
Tejun Heoddd69142013-06-12 21:04:54 -07004407 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004408 * css_set_lock synchronizes access to ->cset_links and prevents
4409 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004410 */
4411 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004412 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004413 read_unlock(&css_set_lock);
4414 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004416
Tejun Heo1a90dd52012-11-05 09:16:59 -08004417 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004418 * Make sure there's no live children. We can't test ->children
4419 * emptiness as dead children linger on it while being destroyed;
4420 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4421 */
4422 empty = true;
4423 rcu_read_lock();
4424 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4425 empty = cgroup_is_dead(child);
4426 if (!empty)
4427 break;
4428 }
4429 rcu_read_unlock();
4430 if (!empty)
4431 return -EBUSY;
4432
4433 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004434 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4435 * will be invoked to perform the rest of destruction once the
4436 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004437 */
Tejun Heo1c6727a2013-12-06 15:11:56 -05004438 for_each_css(css, ssid, cgrp)
4439 kill_css(css);
Tejun Heo455050d2013-06-13 19:27:41 -07004440
4441 /*
4442 * Mark @cgrp dead. This prevents further task migration and child
4443 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004444 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004445 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004446 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004447 */
Tejun Heo54766d42013-06-12 21:04:53 -07004448 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004449
Tejun Heo455050d2013-06-13 19:27:41 -07004450 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4451 raw_spin_lock(&release_list_lock);
4452 if (!list_empty(&cgrp->release_list))
4453 list_del_init(&cgrp->release_list);
4454 raw_spin_unlock(&release_list_lock);
4455
4456 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004457 * If @cgrp has css's attached, the second stage of cgroup
4458 * destruction is kicked off from css_killed_work_fn() after the
4459 * refs of all attached css's are killed. If @cgrp doesn't have
4460 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004461 */
Tejun Heof20104d2013-08-13 20:22:50 -04004462 if (!cgrp->nr_css)
4463 cgroup_destroy_css_killed(cgrp);
4464
4465 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004466 * Clear the base files and remove @cgrp directory. The removal
4467 * puts the base ref but we aren't quite done with @cgrp yet, so
4468 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004469 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004470 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004471 dget(d);
4472 cgroup_d_remove_dir(d);
4473
Tejun Heoea15f8c2013-06-13 19:27:42 -07004474 return 0;
4475};
4476
Tejun Heod3daf282013-06-13 19:39:16 -07004477/**
Tejun Heof20104d2013-08-13 20:22:50 -04004478 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004479 * @work: cgroup->destroy_free_work
4480 *
4481 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004482 * destroyed after all css's are offlined and performs the rest of
4483 * destruction. This is the second step of destruction described in the
4484 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004485 */
Tejun Heof20104d2013-08-13 20:22:50 -04004486static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004487{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004488 struct cgroup *parent = cgrp->parent;
4489 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004490
Tejun Heof20104d2013-08-13 20:22:50 -04004491 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492
Paul Menage999cd8a2009-01-07 18:08:36 -08004493 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004494 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004495
Paul Menageddbcc7e2007-10-18 23:39:30 -07004496 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497
Paul Menagebd89aab2007-10-18 23:40:44 -07004498 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004499 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004500}
4501
Tejun Heo42809dd2012-11-19 08:13:37 -08004502static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4503{
4504 int ret;
4505
4506 mutex_lock(&cgroup_mutex);
4507 ret = cgroup_destroy_locked(dentry->d_fsdata);
4508 mutex_unlock(&cgroup_mutex);
4509
4510 return ret;
4511}
4512
Tejun Heo8e3f6542012-04-01 12:09:55 -07004513static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4514{
4515 INIT_LIST_HEAD(&ss->cftsets);
4516
4517 /*
4518 * base_cftset is embedded in subsys itself, no need to worry about
4519 * deregistration.
4520 */
4521 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004522 struct cftype *cft;
4523
4524 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4525 cft->ss = ss;
4526
Tejun Heo8e3f6542012-04-01 12:09:55 -07004527 ss->base_cftset.cfts = ss->base_cftypes;
4528 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4529 }
4530}
4531
Li Zefan06a11922008-04-29 01:00:07 -07004532static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004533{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004534 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004535
4536 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004537
Tejun Heo648bb562012-11-19 08:13:36 -08004538 mutex_lock(&cgroup_mutex);
4539
Tejun Heo8e3f6542012-04-01 12:09:55 -07004540 /* init base cftset */
4541 cgroup_init_cftsets(ss);
4542
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004544 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004545 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546 /* We don't handle early failures gracefully */
4547 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004548 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004549
Li Zefane8d55fd2008-04-29 01:00:13 -07004550 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004551 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004552 * newly registered, all tasks and hence the
4553 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004554 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004555
4556 need_forkexit_callback |= ss->fork || ss->exit;
4557
Li Zefane8d55fd2008-04-29 01:00:13 -07004558 /* At system boot, before all subsystems have been
4559 * registered, no tasks have been forked, so we don't
4560 * need to invoke fork callbacks here. */
4561 BUG_ON(!list_empty(&init_task.tasks));
4562
Tejun Heoae7f1642013-08-13 20:22:50 -04004563 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004564
Tejun Heo648bb562012-11-19 08:13:36 -08004565 mutex_unlock(&cgroup_mutex);
4566
Ben Blume6a11052010-03-10 15:22:09 -08004567 /* this function shouldn't be used with modular subsystems, since they
4568 * need to register a subsys_id, among other things */
4569 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004570}
4571
4572/**
Ben Blume6a11052010-03-10 15:22:09 -08004573 * cgroup_load_subsys: load and register a modular subsystem at runtime
4574 * @ss: the subsystem to load
4575 *
4576 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004577 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004578 * up for use. If the subsystem is built-in anyway, work is delegated to the
4579 * simpler cgroup_init_subsys.
4580 */
4581int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4582{
Ben Blume6a11052010-03-10 15:22:09 -08004583 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004584 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004585 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004586 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004587 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004588
4589 /* check name and function validity */
4590 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004591 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004592 return -EINVAL;
4593
4594 /*
4595 * we don't support callbacks in modular subsystems. this check is
4596 * before the ss->module check for consistency; a subsystem that could
4597 * be a module should still have no callbacks even if the user isn't
4598 * compiling it as one.
4599 */
4600 if (ss->fork || ss->exit)
4601 return -EINVAL;
4602
4603 /*
4604 * an optionally modular subsystem is built-in: we want to do nothing,
4605 * since cgroup_init_subsys will have already taken care of it.
4606 */
4607 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004608 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004609 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004610 return 0;
4611 }
4612
Tejun Heo8e3f6542012-04-01 12:09:55 -07004613 /* init base cftset */
4614 cgroup_init_cftsets(ss);
4615
Ben Blume6a11052010-03-10 15:22:09 -08004616 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004617 mutex_lock(&cgroup_root_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004618 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004619
4620 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004621 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004622 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004623 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004624 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004625 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004626 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004627 /* failure case - need to deassign the cgroup_subsys[] slot. */
4628 cgroup_subsys[ss->subsys_id] = NULL;
Wei Yongjun0be86692013-12-09 20:38:29 +08004629 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004630 mutex_unlock(&cgroup_mutex);
4631 return PTR_ERR(css);
4632 }
4633
Tejun Heo9871bf92013-06-24 15:21:47 -07004634 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004635
4636 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004637 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004638
4639 /*
4640 * Now we need to entangle the css into the existing css_sets. unlike
4641 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4642 * will need a new pointer to it; done by iterating the css_set_table.
4643 * furthermore, modifying the existing css_sets will corrupt the hash
4644 * table state, so each changed css_set will need its hash recomputed.
4645 * this is all done under the css_set_lock.
4646 */
4647 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004648 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004649 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004650 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004651 continue;
4652 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004653 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004654 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004655 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004656 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004657 key = css_set_hash(cset->subsys);
4658 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004659 }
4660 write_unlock(&css_set_lock);
4661
Tejun Heoae7f1642013-08-13 20:22:50 -04004662 ret = online_css(css);
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004663 if (ret) {
4664 ss->css_free(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004665 goto err_unload;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004666 }
Tejun Heoa8638032012-11-09 09:12:29 -08004667
Ben Blume6a11052010-03-10 15:22:09 -08004668 /* success! */
Tejun Heo780cd8b2013-12-06 15:11:56 -05004669 mutex_unlock(&cgroup_root_mutex);
Ben Blume6a11052010-03-10 15:22:09 -08004670 mutex_unlock(&cgroup_mutex);
4671 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004672
4673err_unload:
Tejun Heo780cd8b2013-12-06 15:11:56 -05004674 mutex_unlock(&cgroup_root_mutex);
Tejun Heod19e19d2012-11-19 08:13:37 -08004675 mutex_unlock(&cgroup_mutex);
4676 /* @ss can't be mounted here as try_module_get() would fail */
4677 cgroup_unload_subsys(ss);
4678 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004679}
4680EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4681
4682/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004683 * cgroup_unload_subsys: unload a modular subsystem
4684 * @ss: the subsystem to unload
4685 *
4686 * This function should be called in a modular subsystem's exitcall. When this
4687 * function is invoked, the refcount on the subsystem's module will be 0, so
4688 * the subsystem will not be attached to any hierarchy.
4689 */
4690void cgroup_unload_subsys(struct cgroup_subsys *ss)
4691{
Tejun Heo69d02062013-06-12 21:04:50 -07004692 struct cgrp_cset_link *link;
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004693 struct cgroup_subsys_state *css;
Ben Blumcf5d5942010-03-10 15:22:09 -08004694
4695 BUG_ON(ss->module == NULL);
4696
4697 /*
4698 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004699 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004700 * doesn't start being used while we're killing it off.
4701 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004702 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004703
4704 mutex_lock(&cgroup_mutex);
Tejun Heo780cd8b2013-12-06 15:11:56 -05004705 mutex_lock(&cgroup_root_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004706
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004707 css = cgroup_css(cgroup_dummy_top, ss);
4708 if (css)
4709 offline_css(css);
Tejun Heo02ae7482012-11-19 08:13:37 -08004710
Ben Blumcf5d5942010-03-10 15:22:09 -08004711 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004712 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004713
Ben Blumcf5d5942010-03-10 15:22:09 -08004714 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004715 * disentangle the css from all css_sets attached to the dummy
4716 * top. as in loading, we need to pay our respects to the hashtable
4717 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004718 */
4719 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004720 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004721 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004722 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004723
Tejun Heo5abb8852013-06-12 21:04:49 -07004724 hash_del(&cset->hlist);
4725 cset->subsys[ss->subsys_id] = NULL;
4726 key = css_set_hash(cset->subsys);
4727 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004728 }
4729 write_unlock(&css_set_lock);
4730
4731 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004732 * remove subsystem's css from the cgroup_dummy_top and free it -
4733 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004734 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004735 */
Vladimir Davydov10bf2f72013-12-12 23:17:08 +04004736 if (css)
4737 ss->css_free(css);
Tejun Heo73e80ed2013-08-13 11:01:55 -04004738 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004739
Tejun Heo780cd8b2013-12-06 15:11:56 -05004740 mutex_unlock(&cgroup_root_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08004741 mutex_unlock(&cgroup_mutex);
4742}
4743EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4744
4745/**
Li Zefana043e3b2008-02-23 15:24:09 -08004746 * cgroup_init_early - cgroup initialization at system boot
4747 *
4748 * Initialize cgroups at system boot, and initialize any
4749 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004750 */
4751int __init cgroup_init_early(void)
4752{
Tejun Heo30159ec2013-06-25 11:53:37 -07004753 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004754 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004755
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004756 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004757 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004758 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004759 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004760 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004761 init_cgroup_root(&cgroup_dummy_root);
4762 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004763 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004764
Tejun Heo69d02062013-06-12 21:04:50 -07004765 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004766 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4767 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004768 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769
Tejun Heo30159ec2013-06-25 11:53:37 -07004770 /* at bootup time, we don't worry about modular subsystems */
4771 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004772 BUG_ON(!ss->name);
4773 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004774 BUG_ON(!ss->css_alloc);
4775 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004777 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004778 ss->name, ss->subsys_id);
4779 BUG();
4780 }
4781
4782 if (ss->early_init)
4783 cgroup_init_subsys(ss);
4784 }
4785 return 0;
4786}
4787
4788/**
Li Zefana043e3b2008-02-23 15:24:09 -08004789 * cgroup_init - cgroup initialization
4790 *
4791 * Register cgroup filesystem and /proc file, and initialize
4792 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004793 */
4794int __init cgroup_init(void)
4795{
Tejun Heo30159ec2013-06-25 11:53:37 -07004796 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004797 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004798 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004799
4800 err = bdi_init(&cgroup_backing_dev_info);
4801 if (err)
4802 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004803
Tejun Heo30159ec2013-06-25 11:53:37 -07004804 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004805 if (!ss->early_init)
4806 cgroup_init_subsys(ss);
4807 }
4808
Tejun Heofa3ca072013-04-14 11:36:56 -07004809 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004810 mutex_lock(&cgroup_mutex);
4811 mutex_lock(&cgroup_root_mutex);
4812
Tejun Heo82fe9b02013-06-25 11:53:37 -07004813 /* Add init_css_set to the hash table */
4814 key = css_set_hash(init_css_set.subsys);
4815 hash_add(css_set_table, &init_css_set.hlist, key);
4816
Tejun Heofc76df72013-06-25 11:53:37 -07004817 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004818
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004819 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4820 0, 1, GFP_KERNEL);
4821 BUG_ON(err < 0);
4822
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004823 mutex_unlock(&cgroup_root_mutex);
4824 mutex_unlock(&cgroup_mutex);
4825
Greg KH676db4a2010-08-05 13:53:35 -07004826 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4827 if (!cgroup_kobj) {
4828 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004829 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004830 }
4831
4832 err = register_filesystem(&cgroup_fs_type);
4833 if (err < 0) {
4834 kobject_put(cgroup_kobj);
4835 goto out;
4836 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004837
Li Zefan46ae2202008-04-29 01:00:08 -07004838 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004839
Paul Menageddbcc7e2007-10-18 23:39:30 -07004840out:
Paul Menagea4243162007-10-18 23:39:35 -07004841 if (err)
4842 bdi_destroy(&cgroup_backing_dev_info);
4843
Paul Menageddbcc7e2007-10-18 23:39:30 -07004844 return err;
4845}
Paul Menageb4f48b62007-10-18 23:39:33 -07004846
Tejun Heoe5fca242013-11-22 17:14:39 -05004847static int __init cgroup_wq_init(void)
4848{
4849 /*
4850 * There isn't much point in executing destruction path in
4851 * parallel. Good chunk is serialized with cgroup_mutex anyway.
Tejun Heo1a115332014-02-12 19:06:19 -05004852 * Use 1 for @max_active.
Tejun Heoe5fca242013-11-22 17:14:39 -05004853 *
4854 * We would prefer to do this in cgroup_init() above, but that
4855 * is called before init_workqueues(): so leave this until after.
4856 */
Tejun Heo1a115332014-02-12 19:06:19 -05004857 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
Tejun Heoe5fca242013-11-22 17:14:39 -05004858 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004859
4860 /*
4861 * Used to destroy pidlists and separate to serve as flush domain.
4862 * Cap @max_active to 1 too.
4863 */
4864 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4865 0, 1);
4866 BUG_ON(!cgroup_pidlist_destroy_wq);
4867
Tejun Heoe5fca242013-11-22 17:14:39 -05004868 return 0;
4869}
4870core_initcall(cgroup_wq_init);
4871
Paul Menagea4243162007-10-18 23:39:35 -07004872/*
4873 * proc_cgroup_show()
4874 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4875 * - Used for /proc/<pid>/cgroup.
4876 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4877 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004878 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004879 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4880 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4881 * cgroup to top_cgroup.
4882 */
4883
4884/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004885int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004886{
4887 struct pid *pid;
4888 struct task_struct *tsk;
4889 char *buf;
4890 int retval;
4891 struct cgroupfs_root *root;
4892
4893 retval = -ENOMEM;
4894 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4895 if (!buf)
4896 goto out;
4897
4898 retval = -ESRCH;
4899 pid = m->private;
4900 tsk = get_pid_task(pid, PIDTYPE_PID);
4901 if (!tsk)
4902 goto out_free;
4903
4904 retval = 0;
4905
4906 mutex_lock(&cgroup_mutex);
4907
Li Zefane5f6a862009-01-07 18:07:41 -08004908 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004909 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004910 struct cgroup *cgrp;
Tejun Heob85d2042013-12-06 15:11:57 -05004911 int ssid, count = 0;
Paul Menagea4243162007-10-18 23:39:35 -07004912
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004913 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heob85d2042013-12-06 15:11:57 -05004914 for_each_subsys(ss, ssid)
4915 if (root->subsys_mask & (1 << ssid))
4916 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004917 if (strlen(root->name))
4918 seq_printf(m, "%sname=%s", count ? "," : "",
4919 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004920 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004921 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004922 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004923 if (retval < 0)
4924 goto out_unlock;
4925 seq_puts(m, buf);
4926 seq_putc(m, '\n');
4927 }
4928
4929out_unlock:
4930 mutex_unlock(&cgroup_mutex);
4931 put_task_struct(tsk);
4932out_free:
4933 kfree(buf);
4934out:
4935 return retval;
4936}
4937
Paul Menagea4243162007-10-18 23:39:35 -07004938/* Display information about each subsystem and each hierarchy */
4939static int proc_cgroupstats_show(struct seq_file *m, void *v)
4940{
Tejun Heo30159ec2013-06-25 11:53:37 -07004941 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004942 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004943
Paul Menage8bab8dd2008-04-04 14:29:57 -07004944 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004945 /*
4946 * ideally we don't want subsystems moving around while we do this.
4947 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4948 * subsys/hierarchy state.
4949 */
Paul Menagea4243162007-10-18 23:39:35 -07004950 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004951
4952 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004953 seq_printf(m, "%s\t%d\t%d\t%d\n",
4954 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004955 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004956
Paul Menagea4243162007-10-18 23:39:35 -07004957 mutex_unlock(&cgroup_mutex);
4958 return 0;
4959}
4960
4961static int cgroupstats_open(struct inode *inode, struct file *file)
4962{
Al Viro9dce07f2008-03-29 03:07:28 +00004963 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004964}
4965
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004966static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004967 .open = cgroupstats_open,
4968 .read = seq_read,
4969 .llseek = seq_lseek,
4970 .release = single_release,
4971};
4972
Paul Menageb4f48b62007-10-18 23:39:33 -07004973/**
4974 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004975 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004976 *
4977 * Description: A task inherits its parent's cgroup at fork().
4978 *
4979 * A pointer to the shared css_set was automatically copied in
4980 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004981 * it was not made under the protection of RCU or cgroup_mutex, so
4982 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4983 * have already changed current->cgroups, allowing the previously
4984 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004985 *
4986 * At the point that cgroup_fork() is called, 'current' is the parent
4987 * task, and the passed argument 'child' points to the child task.
4988 */
4989void cgroup_fork(struct task_struct *child)
4990{
Tejun Heo9bb71302012-10-18 17:52:07 -07004991 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004992 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004993 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004994 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004995 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004996}
4997
4998/**
Li Zefana043e3b2008-02-23 15:24:09 -08004999 * cgroup_post_fork - called on a new task after adding it to the task list
5000 * @child: the task in question
5001 *
Tejun Heo5edee612012-10-16 15:03:14 -07005002 * Adds the task to the list running through its css_set if necessary and
5003 * call the subsystem fork() callbacks. Has to be after the task is
5004 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005005 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005006 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005007 */
Paul Menage817929e2007-10-18 23:39:36 -07005008void cgroup_post_fork(struct task_struct *child)
5009{
Tejun Heo30159ec2013-06-25 11:53:37 -07005010 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005011 int i;
5012
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005013 /*
5014 * use_task_css_set_links is set to 1 before we walk the tasklist
5015 * under the tasklist_lock and we read it here after we added the child
5016 * to the tasklist under the tasklist_lock as well. If the child wasn't
5017 * yet in the tasklist when we walked through it from
5018 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5019 * should be visible now due to the paired locking and barriers implied
5020 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5021 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5022 * lock on fork.
5023 */
Paul Menage817929e2007-10-18 23:39:36 -07005024 if (use_task_css_set_links) {
5025 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005026 task_lock(child);
5027 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005028 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005029 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005030 write_unlock(&css_set_lock);
5031 }
Tejun Heo5edee612012-10-16 15:03:14 -07005032
5033 /*
5034 * Call ss->fork(). This must happen after @child is linked on
5035 * css_set; otherwise, @child might change state between ->fork()
5036 * and addition to css_set.
5037 */
5038 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005039 /*
5040 * fork/exit callbacks are supported only for builtin
5041 * subsystems, and the builtin section of the subsys
5042 * array is immutable, so we don't need to lock the
5043 * subsys array here. On the other hand, modular section
5044 * of the array can be freed at module unload, so we
5045 * can't touch that.
5046 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005047 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005048 if (ss->fork)
5049 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005050 }
Paul Menage817929e2007-10-18 23:39:36 -07005051}
Tejun Heo5edee612012-10-16 15:03:14 -07005052
Paul Menage817929e2007-10-18 23:39:36 -07005053/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005054 * cgroup_exit - detach cgroup from exiting task
5055 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005056 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005057 *
5058 * Description: Detach cgroup from @tsk and release it.
5059 *
5060 * Note that cgroups marked notify_on_release force every task in
5061 * them to take the global cgroup_mutex mutex when exiting.
5062 * This could impact scaling on very large systems. Be reluctant to
5063 * use notify_on_release cgroups where very high task exit scaling
5064 * is required on large systems.
5065 *
5066 * the_top_cgroup_hack:
5067 *
5068 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5069 *
5070 * We call cgroup_exit() while the task is still competent to
5071 * handle notify_on_release(), then leave the task attached to the
5072 * root cgroup in each hierarchy for the remainder of its exit.
5073 *
5074 * To do this properly, we would increment the reference count on
5075 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5076 * code we would add a second cgroup function call, to drop that
5077 * reference. This would just create an unnecessary hot spot on
5078 * the top_cgroup reference count, to no avail.
5079 *
5080 * Normally, holding a reference to a cgroup without bumping its
5081 * count is unsafe. The cgroup could go away, or someone could
5082 * attach us to a different cgroup, decrementing the count on
5083 * the first cgroup that we never incremented. But in this case,
5084 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005085 * which wards off any cgroup_attach_task() attempts, or task is a failed
5086 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005087 */
5088void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5089{
Tejun Heo30159ec2013-06-25 11:53:37 -07005090 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005091 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005092 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005093
5094 /*
5095 * Unlink from the css_set task list if necessary.
5096 * Optimistically check cg_list before taking
5097 * css_set_lock
5098 */
5099 if (!list_empty(&tsk->cg_list)) {
5100 write_lock(&css_set_lock);
5101 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005102 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005103 write_unlock(&css_set_lock);
5104 }
5105
Paul Menageb4f48b62007-10-18 23:39:33 -07005106 /* Reassign the task to the init_css_set. */
5107 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005108 cset = task_css_set(tsk);
5109 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005110
5111 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005112 /*
5113 * fork/exit callbacks are supported only for builtin
5114 * subsystems, see cgroup_post_fork() for details.
5115 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005116 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005117 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005118 struct cgroup_subsys_state *old_css = cset->subsys[i];
5119 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005120
Tejun Heoeb954192013-08-08 20:11:23 -04005121 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005122 }
5123 }
5124 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005125 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005126
Tejun Heo5abb8852013-06-12 21:04:49 -07005127 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005128}
Paul Menage697f4162007-10-18 23:39:34 -07005129
Paul Menagebd89aab2007-10-18 23:40:44 -07005130static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131{
Li Zefanf50daa72013-03-01 15:06:07 +08005132 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005133 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005134 /*
5135 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005137 * it now
5138 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005139 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005140
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005141 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005142 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005143 list_empty(&cgrp->release_list)) {
5144 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145 need_schedule_work = 1;
5146 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005147 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005148 if (need_schedule_work)
5149 schedule_work(&release_agent_work);
5150 }
5151}
5152
Paul Menage81a6a5c2007-10-18 23:39:38 -07005153/*
5154 * Notify userspace when a cgroup is released, by running the
5155 * configured release agent with the name of the cgroup (path
5156 * relative to the root of cgroup file system) as the argument.
5157 *
5158 * Most likely, this user command will try to rmdir this cgroup.
5159 *
5160 * This races with the possibility that some other task will be
5161 * attached to this cgroup before it is removed, or that some other
5162 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5163 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5164 * unused, and this cgroup will be reprieved from its death sentence,
5165 * to continue to serve a useful existence. Next time it's released,
5166 * we will get notified again, if it still has 'notify_on_release' set.
5167 *
5168 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5169 * means only wait until the task is successfully execve()'d. The
5170 * separate release agent task is forked by call_usermodehelper(),
5171 * then control in this thread returns here, without waiting for the
5172 * release agent task. We don't bother to wait because the caller of
5173 * this routine has no use for the exit status of the release agent
5174 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005175 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005176static void cgroup_release_agent(struct work_struct *work)
5177{
5178 BUG_ON(work != &release_agent_work);
5179 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005180 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005181 while (!list_empty(&release_list)) {
5182 char *argv[3], *envp[3];
5183 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005184 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005185 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005186 struct cgroup,
5187 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005188 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005189 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005190 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005191 if (!pathbuf)
5192 goto continue_free;
5193 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5194 goto continue_free;
5195 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5196 if (!agentbuf)
5197 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005198
5199 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005200 argv[i++] = agentbuf;
5201 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005202 argv[i] = NULL;
5203
5204 i = 0;
5205 /* minimal command environment */
5206 envp[i++] = "HOME=/";
5207 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5208 envp[i] = NULL;
5209
5210 /* Drop the lock while we invoke the usermode helper,
5211 * since the exec could involve hitting disk and hence
5212 * be a slow process */
5213 mutex_unlock(&cgroup_mutex);
5214 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005215 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005216 continue_free:
5217 kfree(pathbuf);
5218 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005219 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005220 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005221 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005222 mutex_unlock(&cgroup_mutex);
5223}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005224
5225static int __init cgroup_disable(char *str)
5226{
Tejun Heo30159ec2013-06-25 11:53:37 -07005227 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005228 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005229 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005230
5231 while ((token = strsep(&str, ",")) != NULL) {
5232 if (!*token)
5233 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005234
Tejun Heo30159ec2013-06-25 11:53:37 -07005235 /*
5236 * cgroup_disable, being at boot time, can't know about
5237 * module subsystems, so we don't worry about them.
5238 */
5239 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005240 if (!strcmp(token, ss->name)) {
5241 ss->disabled = 1;
5242 printk(KERN_INFO "Disabling %s control group"
5243 " subsystem\n", ss->name);
5244 break;
5245 }
5246 }
5247 }
5248 return 1;
5249}
5250__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005251
Tejun Heob77d7b62013-08-13 11:01:54 -04005252/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005253 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5254 * @dentry: directory dentry of interest
5255 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005256 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05005257 * Must be called under cgroup_mutex or RCU read lock. The caller is
5258 * responsible for pinning the returned css if it needs to be accessed
5259 * outside the critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005260 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005261struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5262 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005263{
5264 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005265
Tejun Heo87fb54f2013-12-06 15:11:55 -05005266 cgroup_assert_mutex_or_rcu_locked();
Tejun Heob77d7b62013-08-13 11:01:54 -04005267
Tejun Heo35cf0832013-08-26 18:40:56 -04005268 /* is @dentry a cgroup dir? */
5269 if (!dentry->d_inode ||
5270 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005271 return ERR_PTR(-EBADF);
5272
Tejun Heo35cf0832013-08-26 18:40:56 -04005273 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005274 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005275}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005276
Li Zefan1cb650b2013-08-19 10:05:24 +08005277/**
5278 * css_from_id - lookup css by id
5279 * @id: the cgroup id
5280 * @ss: cgroup subsys to be looked into
5281 *
5282 * Returns the css if there's valid one with @id, otherwise returns NULL.
5283 * Should be called under rcu_read_lock().
5284 */
5285struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5286{
5287 struct cgroup *cgrp;
5288
Tejun Heo87fb54f2013-12-06 15:11:55 -05005289 cgroup_assert_mutex_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005290
5291 cgrp = idr_find(&ss->root->cgroup_idr, id);
5292 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005293 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005294 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005295}
5296
Paul Menagefe693432009-09-23 15:56:20 -07005297#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005298static struct cgroup_subsys_state *
5299debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005300{
5301 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5302
5303 if (!css)
5304 return ERR_PTR(-ENOMEM);
5305
5306 return css;
5307}
5308
Tejun Heoeb954192013-08-08 20:11:23 -04005309static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005310{
Tejun Heoeb954192013-08-08 20:11:23 -04005311 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005312}
5313
Tejun Heo182446d2013-08-08 20:11:24 -04005314static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5315 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005316{
Tejun Heo182446d2013-08-08 20:11:24 -04005317 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005318}
5319
Tejun Heo182446d2013-08-08 20:11:24 -04005320static u64 current_css_set_read(struct cgroup_subsys_state *css,
5321 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005322{
5323 return (u64)(unsigned long)current->cgroups;
5324}
5325
Tejun Heo182446d2013-08-08 20:11:24 -04005326static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005327 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005328{
5329 u64 count;
5330
5331 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005332 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005333 rcu_read_unlock();
5334 return count;
5335}
5336
Tejun Heo2da8ca82013-12-05 12:28:04 -05005337static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005338{
Tejun Heo69d02062013-06-12 21:04:50 -07005339 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005340 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005341
5342 read_lock(&css_set_lock);
5343 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005344 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005345 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005346 struct cgroup *c = link->cgrp;
5347 const char *name;
5348
5349 if (c->dentry)
5350 name = c->dentry->d_name.name;
5351 else
5352 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005353 seq_printf(seq, "Root %d group %s\n",
5354 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005355 }
5356 rcu_read_unlock();
5357 read_unlock(&css_set_lock);
5358 return 0;
5359}
5360
5361#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005362static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005363{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005364 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005365 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005366
5367 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005368 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005369 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005370 struct task_struct *task;
5371 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005372 seq_printf(seq, "css_set %p\n", cset);
5373 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005374 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5375 seq_puts(seq, " ...\n");
5376 break;
5377 } else {
5378 seq_printf(seq, " task %d\n",
5379 task_pid_vnr(task));
5380 }
5381 }
5382 }
5383 read_unlock(&css_set_lock);
5384 return 0;
5385}
5386
Tejun Heo182446d2013-08-08 20:11:24 -04005387static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005388{
Tejun Heo182446d2013-08-08 20:11:24 -04005389 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005390}
5391
5392static struct cftype debug_files[] = {
5393 {
Paul Menagefe693432009-09-23 15:56:20 -07005394 .name = "taskcount",
5395 .read_u64 = debug_taskcount_read,
5396 },
5397
5398 {
5399 .name = "current_css_set",
5400 .read_u64 = current_css_set_read,
5401 },
5402
5403 {
5404 .name = "current_css_set_refcount",
5405 .read_u64 = current_css_set_refcount_read,
5406 },
5407
5408 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005409 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005410 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005411 },
5412
5413 {
5414 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005415 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005416 },
5417
5418 {
Paul Menagefe693432009-09-23 15:56:20 -07005419 .name = "releasable",
5420 .read_u64 = releasable_read,
5421 },
Paul Menagefe693432009-09-23 15:56:20 -07005422
Tejun Heo4baf6e32012-04-01 12:09:55 -07005423 { } /* terminate */
5424};
Paul Menagefe693432009-09-23 15:56:20 -07005425
5426struct cgroup_subsys debug_subsys = {
5427 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005428 .css_alloc = debug_css_alloc,
5429 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005430 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005431 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005432};
5433#endif /* CONFIG_CGROUP_DEBUG */