blob: eb34caf981240f0452f0d5bdf20f53cb4f27d6cb [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080059#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020060#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070061
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070063
Tejun Heoe25e2cb2011-12-12 18:12:21 -080064/*
Tejun Heob1a21362013-11-29 10:42:58 -050065 * pidlists linger the following amount before being destroyed. The goal
66 * is avoiding frequent destruction in the middle of consecutive read calls
67 * Expiring in the middle is a performance problem not a correctness one.
68 * 1 sec should be enough.
69 */
70#define CGROUP_PIDLIST_DESTROY_DELAY HZ
71
72/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080073 * cgroup_mutex is the master lock. Any modification to cgroup or its
74 * hierarchy must be performed while holding it.
75 *
76 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
77 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
78 * release_agent_path and so on. Modifying requires both cgroup_mutex and
79 * cgroup_root_mutex. Readers can acquire either of the two. This is to
80 * break the following locking order cycle.
81 *
82 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
83 * B. namespace_sem -> cgroup_mutex
84 *
85 * B happens only through cgroup_show_options() and using cgroup_root_mutex
86 * breaks it.
87 */
Tejun Heo22194492013-04-07 09:29:51 -070088#ifdef CONFIG_PROVE_RCU
89DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040090EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070091#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070092static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070093#endif
94
Tejun Heoe25e2cb2011-12-12 18:12:21 -080095static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070096
Ben Blumaae8aab2010-03-10 15:22:07 -080097/*
Tejun Heoe5fca242013-11-22 17:14:39 -050098 * cgroup destruction makes heavy use of work items and there can be a lot
99 * of concurrent destructions. Use a separate workqueue so that cgroup
100 * destruction work items don't end up filling up max_active of system_wq
101 * which may lead to deadlock.
102 */
103static struct workqueue_struct *cgroup_destroy_wq;
104
105/*
Tejun Heob1a21362013-11-29 10:42:58 -0500106 * pidlist destructions need to be flushed on cgroup destruction. Use a
107 * separate workqueue as flush domain.
108 */
109static struct workqueue_struct *cgroup_pidlist_destroy_wq;
110
111/*
Ben Blumaae8aab2010-03-10 15:22:07 -0800112 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +0200113 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800114 * registered after that. The mutable section of this array is protected by
115 * cgroup_mutex.
116 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200117#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200118#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700119static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700120#include <linux/cgroup_subsys.h>
121};
122
Paul Menageddbcc7e2007-10-18 23:39:30 -0700123/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700124 * The dummy hierarchy, reserved for the subsystems that are otherwise
125 * unattached - it never has more than a single cgroup, and all tasks are
126 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700127 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700128static struct cgroupfs_root cgroup_dummy_root;
129
130/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
131static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700133/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700134 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
135 */
136struct cfent {
137 struct list_head node;
138 struct dentry *dentry;
139 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400140 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700141
142 /* file xattrs */
143 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700144};
145
Paul Menageddbcc7e2007-10-18 23:39:30 -0700146/* The list of hierarchy roots */
147
Tejun Heo9871bf92013-06-24 15:21:47 -0700148static LIST_HEAD(cgroup_roots);
149static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700150
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700151/*
152 * Hierarchy ID allocation and mapping. It follows the same exclusion
153 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
154 * writes, either for reads.
155 */
Tejun Heo1a574232013-04-14 11:36:58 -0700156static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700157
Li Zefan65dff752013-03-01 15:01:56 +0800158static struct cgroup_name root_cgroup_name = { .name = "/" };
159
Li Zefan794611a2013-06-18 18:53:53 +0800160/*
161 * Assign a monotonically increasing serial number to cgroups. It
162 * guarantees cgroups with bigger numbers are newer than those with smaller
163 * numbers. Also, as cgroups are always appended to the parent's
164 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700165 * the ascending serial number order on the list. Protected by
166 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800167 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700168static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800169
Paul Menageddbcc7e2007-10-18 23:39:30 -0700170/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800171 * check for fork/exit handlers to call. This avoids us having to do
172 * extra work in the fork/exit path if none of the subsystems need to
173 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700174 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700175static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700176
Tejun Heo628f7cd2013-06-28 16:24:11 -0700177static struct cftype cgroup_base_files[];
178
Tejun Heof20104d2013-08-13 20:22:50 -0400179static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800180static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400181static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
182 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500183static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500184static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800185
Tejun Heo95109b62013-08-08 20:11:27 -0400186/**
187 * cgroup_css - obtain a cgroup's css for the specified subsystem
188 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400189 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400190 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400191 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
192 * function must be called either under cgroup_mutex or rcu_read_lock() and
193 * the caller is responsible for pinning the returned css if it wants to
194 * keep accessing it outside the said locks. This function may return
195 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400196 */
197static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400198 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400199{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400200 if (ss)
201 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
202 lockdep_is_held(&cgroup_mutex));
203 else
204 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400205}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700206
Paul Menageddbcc7e2007-10-18 23:39:30 -0700207/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700208static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700209{
Tejun Heo54766d42013-06-12 21:04:53 -0700210 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211}
212
Li Zefan78574cf2013-04-08 19:00:38 -0700213/**
214 * cgroup_is_descendant - test ancestry
215 * @cgrp: the cgroup to be tested
216 * @ancestor: possible ancestor of @cgrp
217 *
218 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
219 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
220 * and @ancestor are accessible.
221 */
222bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
223{
224 while (cgrp) {
225 if (cgrp == ancestor)
226 return true;
227 cgrp = cgrp->parent;
228 }
229 return false;
230}
231EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700232
Adrian Bunke9685a02008-02-07 00:13:46 -0800233static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700234{
235 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700236 (1 << CGRP_RELEASABLE) |
237 (1 << CGRP_NOTIFY_ON_RELEASE);
238 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700239}
240
Adrian Bunke9685a02008-02-07 00:13:46 -0800241static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700242{
Paul Menagebd89aab2007-10-18 23:40:44 -0700243 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700244}
245
Tejun Heo30159ec2013-06-25 11:53:37 -0700246/**
247 * for_each_subsys - iterate all loaded cgroup subsystems
248 * @ss: the iteration cursor
249 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
250 *
251 * Should be called under cgroup_mutex.
252 */
253#define for_each_subsys(ss, i) \
254 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
255 if (({ lockdep_assert_held(&cgroup_mutex); \
256 !((ss) = cgroup_subsys[i]); })) { } \
257 else
258
259/**
260 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
261 * @ss: the iteration cursor
262 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
263 *
264 * Bulit-in subsystems are always present and iteration itself doesn't
265 * require any synchronization.
266 */
267#define for_each_builtin_subsys(ss, i) \
268 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
269 (((ss) = cgroup_subsys[i]) || true); (i)++)
270
Tejun Heo5549c492013-06-24 15:21:48 -0700271/* iterate each subsystem attached to a hierarchy */
272#define for_each_root_subsys(root, ss) \
273 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700274
Tejun Heo5549c492013-06-24 15:21:48 -0700275/* iterate across the active hierarchies */
276#define for_each_active_root(root) \
277 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700278
Tejun Heof6ea9372012-04-01 12:09:55 -0700279static inline struct cgroup *__d_cgrp(struct dentry *dentry)
280{
281 return dentry->d_fsdata;
282}
283
Tejun Heo05ef1d72012-04-01 12:09:56 -0700284static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700285{
286 return dentry->d_fsdata;
287}
288
Tejun Heo05ef1d72012-04-01 12:09:56 -0700289static inline struct cftype *__d_cft(struct dentry *dentry)
290{
291 return __d_cfe(dentry)->type;
292}
293
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294/**
295 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
296 * @cgrp: the cgroup to be checked for liveness
297 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700298 * On success, returns true; the mutex should be later unlocked. On
299 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700300 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700301static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700302{
303 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700304 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700305 mutex_unlock(&cgroup_mutex);
306 return false;
307 }
308 return true;
309}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310
Paul Menage81a6a5c2007-10-18 23:39:38 -0700311/* the list of cgroups eligible for automatic release. Protected by
312 * release_list_lock */
313static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200314static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700315static void cgroup_release_agent(struct work_struct *work);
316static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700317static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700318
Tejun Heo69d02062013-06-12 21:04:50 -0700319/*
320 * A cgroup can be associated with multiple css_sets as different tasks may
321 * belong to different cgroups on different hierarchies. In the other
322 * direction, a css_set is naturally associated with multiple cgroups.
323 * This M:N relationship is represented by the following link structure
324 * which exists for each association and allows traversing the associations
325 * from both sides.
326 */
327struct cgrp_cset_link {
328 /* the cgroup and css_set this link associates */
329 struct cgroup *cgrp;
330 struct css_set *cset;
331
332 /* list of cgrp_cset_links anchored at cgrp->cset_links */
333 struct list_head cset_link;
334
335 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
336 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700337};
338
339/* The default css_set - used by init and its children prior to any
340 * hierarchies being mounted. It contains a pointer to the root state
341 * for each subsystem. Also used to anchor the list of css_sets. Not
342 * reference-counted, to improve performance when child cgroups
343 * haven't been created.
344 */
345
346static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700347static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700348
Tejun Heo0942eee2013-08-08 20:11:26 -0400349/*
350 * css_set_lock protects the list of css_set objects, and the chain of
351 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400352 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400353 */
Paul Menage817929e2007-10-18 23:39:36 -0700354static DEFINE_RWLOCK(css_set_lock);
355static int css_set_count;
356
Paul Menage7717f7b2009-09-23 15:56:22 -0700357/*
358 * hash table for cgroup groups. This improves the performance to find
359 * an existing css_set. This hash doesn't (currently) take into
360 * account cgroups in empty hierarchies.
361 */
Li Zefan472b1052008-04-29 01:00:11 -0700362#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800363static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700364
Li Zefan0ac801f2013-01-10 11:49:27 +0800365static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700366{
Li Zefan0ac801f2013-01-10 11:49:27 +0800367 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700368 struct cgroup_subsys *ss;
369 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700370
Tejun Heo30159ec2013-06-25 11:53:37 -0700371 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800372 key += (unsigned long)css[i];
373 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700374
Li Zefan0ac801f2013-01-10 11:49:27 +0800375 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700376}
377
Tejun Heo0942eee2013-08-08 20:11:26 -0400378/*
379 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400380 * until after the first call to css_task_iter_start(). This reduces the
381 * fork()/exit() overhead for people who have cgroups compiled into their
382 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400383 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700384static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700385
Tejun Heo5abb8852013-06-12 21:04:49 -0700386static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700387{
Tejun Heo69d02062013-06-12 21:04:50 -0700388 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700389
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700390 /*
391 * Ensure that the refcount doesn't hit zero while any readers
392 * can see it. Similar to atomic_dec_and_lock(), but for an
393 * rwlock
394 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700395 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700396 return;
397 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700398 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700399 write_unlock(&css_set_lock);
400 return;
401 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700402
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700403 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700404 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700405 css_set_count--;
406
Tejun Heo69d02062013-06-12 21:04:50 -0700407 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700408 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700409
Tejun Heo69d02062013-06-12 21:04:50 -0700410 list_del(&link->cset_link);
411 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800412
Tejun Heoddd69142013-06-12 21:04:54 -0700413 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700414 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700415 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700416 set_bit(CGRP_RELEASABLE, &cgrp->flags);
417 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700418 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700419
420 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700421 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422
423 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700424 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700425}
426
427/*
428 * refcounted get/put for css_set objects
429 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700430static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700431{
Tejun Heo5abb8852013-06-12 21:04:49 -0700432 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700433}
434
Tejun Heo5abb8852013-06-12 21:04:49 -0700435static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700436{
Tejun Heo5abb8852013-06-12 21:04:49 -0700437 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700438}
439
Tejun Heo5abb8852013-06-12 21:04:49 -0700440static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700441{
Tejun Heo5abb8852013-06-12 21:04:49 -0700442 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700443}
444
Tejun Heob326f9d2013-06-24 15:21:48 -0700445/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700447 * @cset: candidate css_set being tested
448 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700449 * @new_cgrp: cgroup that's being entered by the task
450 * @template: desired set of css pointers in css_set (pre-calculated)
451 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800452 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700453 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
454 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700455static bool compare_css_sets(struct css_set *cset,
456 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700457 struct cgroup *new_cgrp,
458 struct cgroup_subsys_state *template[])
459{
460 struct list_head *l1, *l2;
461
Tejun Heo5abb8852013-06-12 21:04:49 -0700462 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 /* Not all subsystems matched */
464 return false;
465 }
466
467 /*
468 * Compare cgroup pointers in order to distinguish between
469 * different cgroups in heirarchies with no subsystems. We
470 * could get by with just this check alone (and skip the
471 * memcmp above) but on most setups the memcmp check will
472 * avoid the need for this more expensive check on almost all
473 * candidates.
474 */
475
Tejun Heo69d02062013-06-12 21:04:50 -0700476 l1 = &cset->cgrp_links;
477 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700479 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700480 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700481
482 l1 = l1->next;
483 l2 = l2->next;
484 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700485 if (l1 == &cset->cgrp_links) {
486 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 break;
488 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700489 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700490 }
491 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700492 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
493 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
494 cgrp1 = link1->cgrp;
495 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 /*
500 * If this hierarchy is the hierarchy of the cgroup
501 * that's changing, then we need to check that this
502 * css_set points to the new cgroup; if it's any other
503 * hierarchy, then this css_set should point to the
504 * same cgroup as the old css_set.
505 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700506 if (cgrp1->root == new_cgrp->root) {
507 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700508 return false;
509 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700510 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700511 return false;
512 }
513 }
514 return true;
515}
516
Tejun Heob326f9d2013-06-24 15:21:48 -0700517/**
518 * find_existing_css_set - init css array and find the matching css_set
519 * @old_cset: the css_set that we're using before the cgroup transition
520 * @cgrp: the cgroup that we're moving into
521 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523static struct css_set *find_existing_css_set(struct css_set *old_cset,
524 struct cgroup *cgrp,
525 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700526{
Paul Menagebd89aab2007-10-18 23:40:44 -0700527 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700528 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700529 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800530 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700531 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700532
Ben Blumaae8aab2010-03-10 15:22:07 -0800533 /*
534 * Build the set of subsystem state objects that we want to see in the
535 * new css_set. while subsystems can change globally, the entries here
536 * won't change, so no need for locking.
537 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700538 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400539 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700540 /* Subsystem is in this hierarchy. So we want
541 * the subsystem state from the new
542 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400543 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700544 } else {
545 /* Subsystem is not in this hierarchy, so we
546 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700548 }
549 }
550
Li Zefan0ac801f2013-01-10 11:49:27 +0800551 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700552 hash_for_each_possible(css_set_table, cset, hlist, key) {
553 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700554 continue;
555
556 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700557 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700558 }
Paul Menage817929e2007-10-18 23:39:36 -0700559
560 /* No existing cgroup group matched */
561 return NULL;
562}
563
Tejun Heo69d02062013-06-12 21:04:50 -0700564static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700565{
Tejun Heo69d02062013-06-12 21:04:50 -0700566 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700567
Tejun Heo69d02062013-06-12 21:04:50 -0700568 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
569 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700570 kfree(link);
571 }
572}
573
Tejun Heo69d02062013-06-12 21:04:50 -0700574/**
575 * allocate_cgrp_cset_links - allocate cgrp_cset_links
576 * @count: the number of links to allocate
577 * @tmp_links: list_head the allocated links are put on
578 *
579 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
580 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700581 */
Tejun Heo69d02062013-06-12 21:04:50 -0700582static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700583{
Tejun Heo69d02062013-06-12 21:04:50 -0700584 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700585 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700586
587 INIT_LIST_HEAD(tmp_links);
588
Li Zefan36553432008-07-29 22:33:19 -0700589 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700590 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700591 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700592 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700593 return -ENOMEM;
594 }
Tejun Heo69d02062013-06-12 21:04:50 -0700595 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700596 }
597 return 0;
598}
599
Li Zefanc12f65d2009-01-07 18:07:42 -0800600/**
601 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700602 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700603 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800604 * @cgrp: the destination cgroup
605 */
Tejun Heo69d02062013-06-12 21:04:50 -0700606static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
607 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800608{
Tejun Heo69d02062013-06-12 21:04:50 -0700609 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800610
Tejun Heo69d02062013-06-12 21:04:50 -0700611 BUG_ON(list_empty(tmp_links));
612 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
613 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700615 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700616 /*
617 * Always add links to the tail of the list so that the list
618 * is sorted by order of hierarchy creation
619 */
Tejun Heo69d02062013-06-12 21:04:50 -0700620 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800621}
622
Tejun Heob326f9d2013-06-24 15:21:48 -0700623/**
624 * find_css_set - return a new css_set with one cgroup updated
625 * @old_cset: the baseline css_set
626 * @cgrp: the cgroup to be updated
627 *
628 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
629 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700630 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700631static struct css_set *find_css_set(struct css_set *old_cset,
632 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700633{
Tejun Heob326f9d2013-06-24 15:21:48 -0700634 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700635 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700636 struct list_head tmp_links;
637 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800638 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700639
Tejun Heob326f9d2013-06-24 15:21:48 -0700640 lockdep_assert_held(&cgroup_mutex);
641
Paul Menage817929e2007-10-18 23:39:36 -0700642 /* First see if we already have a cgroup group that matches
643 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700644 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 cset = find_existing_css_set(old_cset, cgrp, template);
646 if (cset)
647 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700648 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700649
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 if (cset)
651 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700652
Tejun Heof4f4be22013-06-12 21:04:51 -0700653 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700655 return NULL;
656
Tejun Heo69d02062013-06-12 21:04:50 -0700657 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700658 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700659 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700660 return NULL;
661 }
662
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700664 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 INIT_LIST_HEAD(&cset->tasks);
666 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700667
668 /* Copy the set of subsystem state objects generated in
669 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700670 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700671
672 write_lock(&css_set_lock);
673 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700674 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700676
Paul Menage7717f7b2009-09-23 15:56:22 -0700677 if (c->root == cgrp->root)
678 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700679 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700680 }
Paul Menage817929e2007-10-18 23:39:36 -0700681
Tejun Heo69d02062013-06-12 21:04:50 -0700682 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700683
Paul Menage817929e2007-10-18 23:39:36 -0700684 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700685
686 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 key = css_set_hash(cset->subsys);
688 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700689
Paul Menage817929e2007-10-18 23:39:36 -0700690 write_unlock(&css_set_lock);
691
Tejun Heo5abb8852013-06-12 21:04:49 -0700692 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700693}
694
Paul Menageddbcc7e2007-10-18 23:39:30 -0700695/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700696 * Return the cgroup for "task" from the given hierarchy. Must be
697 * called with cgroup_mutex held.
698 */
699static struct cgroup *task_cgroup_from_root(struct task_struct *task,
700 struct cgroupfs_root *root)
701{
Tejun Heo5abb8852013-06-12 21:04:49 -0700702 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700703 struct cgroup *res = NULL;
704
705 BUG_ON(!mutex_is_locked(&cgroup_mutex));
706 read_lock(&css_set_lock);
707 /*
708 * No need to lock the task - since we hold cgroup_mutex the
709 * task can't change groups, so the only thing that can happen
710 * is that it exits and its css is set back to init_css_set.
711 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700712 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 res = &root->top_cgroup;
715 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700716 struct cgrp_cset_link *link;
717
718 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700719 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700720
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 if (c->root == root) {
722 res = c;
723 break;
724 }
725 }
726 }
727 read_unlock(&css_set_lock);
728 BUG_ON(!res);
729 return res;
730}
731
732/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700733 * There is one global cgroup mutex. We also require taking
734 * task_lock() when dereferencing a task's cgroup subsys pointers.
735 * See "The task_lock() exception", at the end of this comment.
736 *
737 * A task must hold cgroup_mutex to modify cgroups.
738 *
739 * Any task can increment and decrement the count field without lock.
740 * So in general, code holding cgroup_mutex can't rely on the count
741 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800742 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743 * means that no tasks are currently attached, therefore there is no
744 * way a task attached to that cgroup can fork (the other way to
745 * increment the count). So code holding cgroup_mutex can safely
746 * assume that if the count is zero, it will stay zero. Similarly, if
747 * a task holds cgroup_mutex on a cgroup with zero count, it
748 * knows that the cgroup won't be removed, as cgroup_rmdir()
749 * needs that mutex.
750 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
752 * (usually) take cgroup_mutex. These are the two most performance
753 * critical pieces of code here. The exception occurs on cgroup_exit(),
754 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
755 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800756 * to the release agent with the name of the cgroup (path relative to
757 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 *
759 * A cgroup can only be deleted if both its 'count' of using tasks
760 * is zero, and its list of 'children' cgroups is empty. Since all
761 * tasks in the system use _some_ cgroup, and since there is always at
762 * least one task in the system (init, pid == 1), therefore, top_cgroup
763 * always has either children cgroups and/or using tasks. So we don't
764 * need a special hack to ensure that top_cgroup cannot be deleted.
765 *
766 * The task_lock() exception
767 *
768 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800769 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800770 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700771 * several performance critical places that need to reference
772 * task->cgroup without the expense of grabbing a system global
773 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800774 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
776 * the task_struct routinely used for such matters.
777 *
778 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800779 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780 */
781
Paul Menageddbcc7e2007-10-18 23:39:30 -0700782/*
783 * A couple of forward declarations required, due to cyclic reference loop:
784 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
785 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
786 * -> cgroup_mkdir.
787 */
788
Al Viro18bb1db2011-07-26 01:41:39 -0400789static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700791static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700792static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700793static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700794
795static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200796 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700797 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700798};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799
Al Viroa5e7ed32011-07-26 01:55:55 -0400800static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801{
802 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803
804 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400805 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100807 inode->i_uid = current_fsuid();
808 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
810 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
811 }
812 return inode;
813}
814
Li Zefan65dff752013-03-01 15:01:56 +0800815static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
816{
817 struct cgroup_name *name;
818
819 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
820 if (!name)
821 return NULL;
822 strcpy(name->name, dentry->d_name.name);
823 return name;
824}
825
Li Zefanbe445622013-01-24 14:31:42 +0800826static void cgroup_free_fn(struct work_struct *work)
827{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700828 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800829
830 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800831 cgrp->root->number_of_cgroups--;
832 mutex_unlock(&cgroup_mutex);
833
834 /*
Li Zefan415cf072013-04-08 14:35:02 +0800835 * We get a ref to the parent's dentry, and put the ref when
836 * this cgroup is being freed, so it's guaranteed that the
837 * parent won't be destroyed before its children.
838 */
839 dput(cgrp->parent->dentry);
840
841 /*
Li Zefanbe445622013-01-24 14:31:42 +0800842 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700843 * created the cgroup. This will free cgrp->root, if we are
844 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800845 */
846 deactivate_super(cgrp->root->sb);
847
Tejun Heob1a21362013-11-29 10:42:58 -0500848 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800849
850 simple_xattrs_free(&cgrp->xattrs);
851
Li Zefan65dff752013-03-01 15:01:56 +0800852 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800853 kfree(cgrp);
854}
855
856static void cgroup_free_rcu(struct rcu_head *head)
857{
858 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
859
Tejun Heoea15f8c2013-06-13 19:27:42 -0700860 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500861 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800862}
863
Paul Menageddbcc7e2007-10-18 23:39:30 -0700864static void cgroup_diput(struct dentry *dentry, struct inode *inode)
865{
866 /* is dentry a directory ? if so, kfree() associated cgroup */
867 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700868 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800869
Tejun Heo54766d42013-06-12 21:04:53 -0700870 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800871 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700872 } else {
873 struct cfent *cfe = __d_cfe(dentry);
874 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
875
876 WARN_ONCE(!list_empty(&cfe->node) &&
877 cgrp != &cgrp->root->top_cgroup,
878 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700879 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700880 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700881 }
882 iput(inode);
883}
884
885static void remove_dir(struct dentry *d)
886{
887 struct dentry *parent = dget(d->d_parent);
888
889 d_delete(d);
890 simple_rmdir(parent->d_inode, d);
891 dput(parent);
892}
893
Li Zefan2739d3c2013-01-21 18:18:33 +0800894static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700895{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700896 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700897
Tejun Heo05ef1d72012-04-01 12:09:56 -0700898 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
899 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100900
Li Zefan2739d3c2013-01-21 18:18:33 +0800901 /*
902 * If we're doing cleanup due to failure of cgroup_create(),
903 * the corresponding @cfe may not exist.
904 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700905 list_for_each_entry(cfe, &cgrp->files, node) {
906 struct dentry *d = cfe->dentry;
907
908 if (cft && cfe->type != cft)
909 continue;
910
911 dget(d);
912 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700913 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 list_del_init(&cfe->node);
915 dput(d);
916
Li Zefan2739d3c2013-01-21 18:18:33 +0800917 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919}
920
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400921/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700922 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700923 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400924 * @subsys_mask: mask of the subsystem ids whose files should be removed
925 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700926static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400928 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700929 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930
Tejun Heob420ba72013-07-12 12:34:02 -0700931 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400932 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700933
934 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400935 continue;
936 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400937 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400938 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939}
940
941/*
942 * NOTE : the dentry must have been dget()'ed
943 */
944static void cgroup_d_remove_dir(struct dentry *dentry)
945{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100946 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700947
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100948 parent = dentry->d_parent;
949 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800950 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100952 spin_unlock(&dentry->d_lock);
953 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954 remove_dir(dentry);
955}
956
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700957/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800958 * Call with cgroup_mutex held. Drops reference counts on modules, including
959 * any duplicate ones that parse_cgroupfs_options took. If this function
960 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800961 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700963 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964{
Paul Menagebd89aab2007-10-18 23:40:44 -0700965 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700966 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700967 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700968 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969
Ben Blumaae8aab2010-03-10 15:22:07 -0800970 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800971 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800972
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700974 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700975 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700977
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700978 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700979 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700980 ret = -EBUSY;
981 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700983
984 /* pin the module */
985 if (!try_module_get(ss->module)) {
986 ret = -ENOENT;
987 goto out_put;
988 }
989 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 }
991
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700992 /* subsys could be missing if unloaded between parsing and here */
993 if (added_mask != pinned) {
994 ret = -ENOENT;
995 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997
Tejun Heo31261212013-06-28 17:07:30 -0700998 ret = cgroup_populate_dir(cgrp, added_mask);
999 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001000 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001001
1002 /*
1003 * Nothing can fail from this point on. Remove files for the
1004 * removed subsystems and rebind each subsystem.
1005 */
1006 cgroup_clear_dir(cgrp, removed_mask);
1007
Tejun Heo30159ec2013-06-25 11:53:37 -07001008 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001010
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001011 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001013 BUG_ON(cgroup_css(cgrp, ss));
1014 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1015 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001016
Tejun Heo73e80ed2013-08-13 11:01:55 -04001017 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001018 cgroup_css(cgroup_dummy_top, ss));
1019 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001020
Li Zefan33a68ac2009-01-07 18:07:42 -08001021 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001022 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001024 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001025
Ben Blumcf5d5942010-03-10 15:22:09 -08001026 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001027 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001028 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001030 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1031 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001032
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001034 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001035
Tejun Heoca8bdca2013-08-26 18:40:56 -04001036 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001037 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1038
Tejun Heo9871bf92013-06-24 15:21:47 -07001039 cgroup_subsys[i]->root = &cgroup_dummy_root;
1040 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001041
Ben Blumcf5d5942010-03-10 15:22:09 -08001042 /* subsystem is now free - drop reference on module */
1043 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001044 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 }
1046 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047
Tejun Heo1672d042013-06-25 18:04:54 -07001048 /*
1049 * Mark @root has finished binding subsystems. @root->subsys_mask
1050 * now matches the bound subsystems.
1051 */
1052 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1053
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001055
1056out_put:
1057 for_each_subsys(ss, i)
1058 if (pinned & (1 << i))
1059 module_put(ss->module);
1060 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061}
1062
Al Viro34c80b12011-12-08 21:32:45 -05001063static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064{
Al Viro34c80b12011-12-08 21:32:45 -05001065 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 struct cgroup_subsys *ss;
1067
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001068 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001069 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001071 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1072 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001073 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001075 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001076 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001077 if (strlen(root->release_agent_path))
1078 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001079 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001080 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001081 if (strlen(root->name))
1082 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001083 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 return 0;
1085}
1086
1087struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001088 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001090 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001091 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001092 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001093 /* User explicitly requested empty subsystem */
1094 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001095
1096 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001097
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098};
1099
Ben Blumaae8aab2010-03-10 15:22:07 -08001100/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001101 * Convert a hierarchy specifier into a bitmask of subsystems and
1102 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1103 * array. This function takes refcounts on subsystems to be used, unless it
1104 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001105 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001106static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001108 char *token, *o = data;
1109 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001110 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001111 struct cgroup_subsys *ss;
1112 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001113
Ben Blumaae8aab2010-03-10 15:22:07 -08001114 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1115
Li Zefanf9ab5b52009-06-17 16:26:33 -07001116#ifdef CONFIG_CPUSETS
1117 mask = ~(1UL << cpuset_subsys_id);
1118#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119
Paul Menagec6d57f32009-09-23 15:56:19 -07001120 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121
1122 while ((token = strsep(&o, ",")) != NULL) {
1123 if (!*token)
1124 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001125 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001126 /* Explicitly have no subsystems */
1127 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001128 continue;
1129 }
1130 if (!strcmp(token, "all")) {
1131 /* Mutually exclusive option 'all' + subsystem name */
1132 if (one_ss)
1133 return -EINVAL;
1134 all_ss = true;
1135 continue;
1136 }
Tejun Heo873fe092013-04-14 20:15:26 -07001137 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1138 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1139 continue;
1140 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001142 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 continue;
1144 }
1145 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001146 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 continue;
1148 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001149 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001150 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001151 continue;
1152 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001153 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001154 /* Specifying two release agents is forbidden */
1155 if (opts->release_agent)
1156 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001157 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001158 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001159 if (!opts->release_agent)
1160 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 continue;
1162 }
1163 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001164 const char *name = token + 5;
1165 /* Can't specify an empty name */
1166 if (!strlen(name))
1167 return -EINVAL;
1168 /* Must match [\w.-]+ */
1169 for (i = 0; i < strlen(name); i++) {
1170 char c = name[i];
1171 if (isalnum(c))
1172 continue;
1173 if ((c == '.') || (c == '-') || (c == '_'))
1174 continue;
1175 return -EINVAL;
1176 }
1177 /* Specifying two names is forbidden */
1178 if (opts->name)
1179 return -EINVAL;
1180 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001181 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001182 GFP_KERNEL);
1183 if (!opts->name)
1184 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001185
1186 continue;
1187 }
1188
Tejun Heo30159ec2013-06-25 11:53:37 -07001189 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001190 if (strcmp(token, ss->name))
1191 continue;
1192 if (ss->disabled)
1193 continue;
1194
1195 /* Mutually exclusive option 'all' + subsystem name */
1196 if (all_ss)
1197 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001198 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001199 one_ss = true;
1200
1201 break;
1202 }
1203 if (i == CGROUP_SUBSYS_COUNT)
1204 return -ENOENT;
1205 }
1206
1207 /*
1208 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001209 * otherwise if 'none', 'name=' and a subsystem name options
1210 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001211 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001212 if (all_ss || (!one_ss && !opts->none && !opts->name))
1213 for_each_subsys(ss, i)
1214 if (!ss->disabled)
1215 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001216
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001217 /* Consistency checks */
1218
Tejun Heo873fe092013-04-14 20:15:26 -07001219 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1220 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1221
1222 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1223 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1224 return -EINVAL;
1225 }
1226
1227 if (opts->cpuset_clone_children) {
1228 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1229 return -EINVAL;
1230 }
1231 }
1232
Li Zefanf9ab5b52009-06-17 16:26:33 -07001233 /*
1234 * Option noprefix was introduced just for backward compatibility
1235 * with the old cpuset, so we allow noprefix only if mounting just
1236 * the cpuset subsystem.
1237 */
Tejun Heo93438622013-04-14 20:15:25 -07001238 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001239 return -EINVAL;
1240
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001241
1242 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001243 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001244 return -EINVAL;
1245
1246 /*
1247 * We either have to specify by name or by subsystems. (So all
1248 * empty hierarchies must have a name).
1249 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001250 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251 return -EINVAL;
1252
1253 return 0;
1254}
1255
1256static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1257{
1258 int ret = 0;
1259 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001260 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001262 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263
Tejun Heo873fe092013-04-14 20:15:26 -07001264 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1265 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1266 return -EINVAL;
1267 }
1268
Paul Menagebd89aab2007-10-18 23:40:44 -07001269 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001271 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272
1273 /* See what subsystems are wanted */
1274 ret = parse_cgroupfs_options(data, &opts);
1275 if (ret)
1276 goto out_unlock;
1277
Tejun Heoa8a648c2013-06-24 15:21:47 -07001278 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001279 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1280 task_tgid_nr(current), current->comm);
1281
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001282 added_mask = opts.subsys_mask & ~root->subsys_mask;
1283 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001284
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001286 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001287 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001288 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1289 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1290 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001292 goto out_unlock;
1293 }
1294
Tejun Heof172e672013-06-28 17:07:30 -07001295 /* remounting is not allowed for populated hierarchies */
1296 if (root->number_of_cgroups > 1) {
1297 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001298 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001299 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001300
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001302 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304
Paul Menage81a6a5c2007-10-18 23:39:38 -07001305 if (opts.release_agent)
1306 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001307 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001308 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001309 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001310 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001312 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 return ret;
1314}
1315
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001316static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317 .statfs = simple_statfs,
1318 .drop_inode = generic_delete_inode,
1319 .show_options = cgroup_show_options,
1320 .remount_fs = cgroup_remount,
1321};
1322
Paul Menagecc31edc2008-10-18 20:28:04 -07001323static void init_cgroup_housekeeping(struct cgroup *cgrp)
1324{
1325 INIT_LIST_HEAD(&cgrp->sibling);
1326 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001327 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001328 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001329 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001330 INIT_LIST_HEAD(&cgrp->pidlists);
1331 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001332 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001333 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001334}
Paul Menagec6d57f32009-09-23 15:56:19 -07001335
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336static void init_cgroup_root(struct cgroupfs_root *root)
1337{
Paul Menagebd89aab2007-10-18 23:40:44 -07001338 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001339
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 INIT_LIST_HEAD(&root->subsys_list);
1341 INIT_LIST_HEAD(&root->root_list);
1342 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001343 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001344 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001345 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001346 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347}
1348
Tejun Heofc76df72013-06-25 11:53:37 -07001349static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001350{
Tejun Heo1a574232013-04-14 11:36:58 -07001351 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001352
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001353 lockdep_assert_held(&cgroup_mutex);
1354 lockdep_assert_held(&cgroup_root_mutex);
1355
Tejun Heofc76df72013-06-25 11:53:37 -07001356 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1357 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001358 if (id < 0)
1359 return id;
1360
1361 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001362 return 0;
1363}
1364
1365static void cgroup_exit_root_id(struct cgroupfs_root *root)
1366{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001367 lockdep_assert_held(&cgroup_mutex);
1368 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001369
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001370 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001371 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001372 root->hierarchy_id = 0;
1373 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001374}
1375
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376static int cgroup_test_super(struct super_block *sb, void *data)
1377{
Paul Menagec6d57f32009-09-23 15:56:19 -07001378 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001379 struct cgroupfs_root *root = sb->s_fs_info;
1380
Paul Menagec6d57f32009-09-23 15:56:19 -07001381 /* If we asked for a name then it must match */
1382 if (opts->name && strcmp(opts->name, root->name))
1383 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001385 /*
1386 * If we asked for subsystems (or explicitly for no
1387 * subsystems) then they must match
1388 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001389 if ((opts->subsys_mask || opts->none)
1390 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391 return 0;
1392
1393 return 1;
1394}
1395
Paul Menagec6d57f32009-09-23 15:56:19 -07001396static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1397{
1398 struct cgroupfs_root *root;
1399
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001400 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001401 return NULL;
1402
1403 root = kzalloc(sizeof(*root), GFP_KERNEL);
1404 if (!root)
1405 return ERR_PTR(-ENOMEM);
1406
1407 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408
Tejun Heo1672d042013-06-25 18:04:54 -07001409 /*
1410 * We need to set @root->subsys_mask now so that @root can be
1411 * matched by cgroup_test_super() before it finishes
1412 * initialization; otherwise, competing mounts with the same
1413 * options may try to bind the same subsystems instead of waiting
1414 * for the first one leading to unexpected mount errors.
1415 * SUBSYS_BOUND will be set once actual binding is complete.
1416 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001417 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001418 root->flags = opts->flags;
1419 if (opts->release_agent)
1420 strcpy(root->release_agent_path, opts->release_agent);
1421 if (opts->name)
1422 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001423 if (opts->cpuset_clone_children)
1424 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001425 return root;
1426}
1427
Tejun Heofa3ca072013-04-14 11:36:56 -07001428static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429{
Tejun Heofa3ca072013-04-14 11:36:56 -07001430 if (root) {
1431 /* hierarhcy ID shoulid already have been released */
1432 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001433
Li Zefan4e96ee82013-07-31 09:50:50 +08001434 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001435 kfree(root);
1436 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001437}
1438
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439static int cgroup_set_super(struct super_block *sb, void *data)
1440{
1441 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 struct cgroup_sb_opts *opts = data;
1443
1444 /* If we don't have a new root, we can't set up a new sb */
1445 if (!opts->new_root)
1446 return -EINVAL;
1447
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001448 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001449
1450 ret = set_anon_super(sb, NULL);
1451 if (ret)
1452 return ret;
1453
Paul Menagec6d57f32009-09-23 15:56:19 -07001454 sb->s_fs_info = opts->new_root;
1455 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001456
1457 sb->s_blocksize = PAGE_CACHE_SIZE;
1458 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1459 sb->s_magic = CGROUP_SUPER_MAGIC;
1460 sb->s_op = &cgroup_ops;
1461
1462 return 0;
1463}
1464
1465static int cgroup_get_rootdir(struct super_block *sb)
1466{
Al Viro0df6a632010-12-21 13:29:29 -05001467 static const struct dentry_operations cgroup_dops = {
1468 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001469 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001470 };
1471
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 struct inode *inode =
1473 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001474
1475 if (!inode)
1476 return -ENOMEM;
1477
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 inode->i_fop = &simple_dir_operations;
1479 inode->i_op = &cgroup_dir_inode_operations;
1480 /* directories start off with i_nlink == 2 (for "." entry) */
1481 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001482 sb->s_root = d_make_root(inode);
1483 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001485 /* for everything else we want ->d_op set */
1486 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 return 0;
1488}
1489
Al Virof7e83572010-07-26 13:23:11 +04001490static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001492 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493{
1494 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001495 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496 int ret = 0;
1497 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001499 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001500 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001501 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502
1503 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001504 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001506 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 if (ret)
1508 goto out_err;
1509
1510 /*
1511 * Allocate a new cgroup root. We may not need it if we're
1512 * reusing an existing hierarchy.
1513 */
1514 new_root = cgroup_root_from_opts(&opts);
1515 if (IS_ERR(new_root)) {
1516 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001517 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001518 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
Paul Menagec6d57f32009-09-23 15:56:19 -07001521 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001522 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001524 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001525 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001526 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 }
1528
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 root = sb->s_fs_info;
1530 BUG_ON(!root);
1531 if (root == opts.new_root) {
1532 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001533 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001535 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001536 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
1538 BUG_ON(sb->s_root != NULL);
1539
1540 ret = cgroup_get_rootdir(sb);
1541 if (ret)
1542 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001543 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
Paul Menage817929e2007-10-18 23:39:36 -07001545 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001547 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548
Li Zefan4e96ee82013-07-31 09:50:50 +08001549 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1550 0, 1, GFP_KERNEL);
1551 if (root_cgrp->id < 0)
1552 goto unlock_drop;
1553
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001554 /* Check for name clashes with existing mounts */
1555 ret = -EBUSY;
1556 if (strlen(root->name))
1557 for_each_active_root(existing_root)
1558 if (!strcmp(existing_root->name, root->name))
1559 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001560
Paul Menage817929e2007-10-18 23:39:36 -07001561 /*
1562 * We're accessing css_set_count without locking
1563 * css_set_lock here, but that's OK - it can only be
1564 * increased by someone holding cgroup_lock, and
1565 * that's us. The worst that can happen is that we
1566 * have some link structures left over
1567 */
Tejun Heo69d02062013-06-12 21:04:50 -07001568 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001569 if (ret)
1570 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001571
Tejun Heofc76df72013-06-25 11:53:37 -07001572 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1573 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001574 if (ret)
1575 goto unlock_drop;
1576
Tejun Heo31261212013-06-28 17:07:30 -07001577 sb->s_root->d_fsdata = root_cgrp;
1578 root_cgrp->dentry = sb->s_root;
1579
1580 /*
1581 * We're inside get_sb() and will call lookup_one_len() to
1582 * create the root files, which doesn't work if SELinux is
1583 * in use. The following cred dancing somehow works around
1584 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1585 * populating new cgroupfs mount") for more details.
1586 */
1587 cred = override_creds(&init_cred);
1588
Tejun Heo2bb566c2013-08-08 20:11:23 -04001589 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001590 if (ret)
1591 goto rm_base_files;
1592
Tejun Heoa8a648c2013-06-24 15:21:47 -07001593 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001594 if (ret)
1595 goto rm_base_files;
1596
1597 revert_creds(cred);
1598
Ben Blumcf5d5942010-03-10 15:22:09 -08001599 /*
1600 * There must be no failure case after here, since rebinding
1601 * takes care of subsystems' refcounts, which are explicitly
1602 * dropped in the failure exit path.
1603 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Tejun Heo9871bf92013-06-24 15:21:47 -07001605 list_add(&root->root_list, &cgroup_roots);
1606 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607
Paul Menage817929e2007-10-18 23:39:36 -07001608 /* Link the top cgroup in this hierarchy into all
1609 * the css_set objects */
1610 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001611 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001612 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001613 write_unlock(&css_set_lock);
1614
Tejun Heo69d02062013-06-12 21:04:50 -07001615 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001616
Li Zefanc12f65d2009-01-07 18:07:42 -08001617 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618 BUG_ON(root->number_of_cgroups != 1);
1619
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001620 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001622 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001623 } else {
1624 /*
1625 * We re-used an existing hierarchy - the new root (if
1626 * any) is not needed
1627 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001628 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001629
Tejun Heoc7ba8282013-06-29 14:06:10 -07001630 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001631 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1632 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1633 ret = -EINVAL;
1634 goto drop_new_super;
1635 } else {
1636 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1637 }
Tejun Heo873fe092013-04-14 20:15:26 -07001638 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001639 }
1640
Paul Menagec6d57f32009-09-23 15:56:19 -07001641 kfree(opts.release_agent);
1642 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001643 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644
Tejun Heo31261212013-06-28 17:07:30 -07001645 rm_base_files:
1646 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001647 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001648 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001649 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001650 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001651 mutex_unlock(&cgroup_root_mutex);
1652 mutex_unlock(&cgroup_mutex);
1653 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001655 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001656 out_err:
1657 kfree(opts.release_agent);
1658 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001659 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660}
1661
1662static void cgroup_kill_sb(struct super_block *sb) {
1663 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001664 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001665 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001666 int ret;
1667
1668 BUG_ON(!root);
1669
1670 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001671 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672
Tejun Heo31261212013-06-28 17:07:30 -07001673 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001675 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676
1677 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001678 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1679 ret = rebind_subsystems(root, 0, root->subsys_mask);
1680 /* Shouldn't be able to fail ... */
1681 BUG_ON(ret);
1682 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683
Paul Menage817929e2007-10-18 23:39:36 -07001684 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001685 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001686 * root cgroup
1687 */
1688 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001689
Tejun Heo69d02062013-06-12 21:04:50 -07001690 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1691 list_del(&link->cset_link);
1692 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001693 kfree(link);
1694 }
1695 write_unlock(&css_set_lock);
1696
Paul Menage839ec542009-01-29 14:25:22 -08001697 if (!list_empty(&root->root_list)) {
1698 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001699 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001700 }
Li Zefane5f6a862009-01-07 18:07:41 -08001701
Tejun Heofa3ca072013-04-14 11:36:56 -07001702 cgroup_exit_root_id(root);
1703
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001704 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001706 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001708 simple_xattrs_free(&cgrp->xattrs);
1709
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001711 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712}
1713
1714static struct file_system_type cgroup_fs_type = {
1715 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001716 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717 .kill_sb = cgroup_kill_sb,
1718};
1719
Greg KH676db4a2010-08-05 13:53:35 -07001720static struct kobject *cgroup_kobj;
1721
Li Zefana043e3b2008-02-23 15:24:09 -08001722/**
1723 * cgroup_path - generate the path of a cgroup
1724 * @cgrp: the cgroup in question
1725 * @buf: the buffer to write the path into
1726 * @buflen: the length of the buffer
1727 *
Li Zefan65dff752013-03-01 15:01:56 +08001728 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1729 *
1730 * We can't generate cgroup path using dentry->d_name, as accessing
1731 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1732 * inode's i_mutex, while on the other hand cgroup_path() can be called
1733 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001735int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736{
Li Zefan65dff752013-03-01 15:01:56 +08001737 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001739
Tejun Heoda1f2962013-04-14 10:32:19 -07001740 if (!cgrp->parent) {
1741 if (strlcpy(buf, "/", buflen) >= buflen)
1742 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 return 0;
1744 }
1745
Tao Ma316eb662012-11-08 21:36:38 +08001746 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001747 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001748
Li Zefan65dff752013-03-01 15:01:56 +08001749 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001750 do {
Li Zefan65dff752013-03-01 15:01:56 +08001751 const char *name = cgroup_name(cgrp);
1752 int len;
1753
1754 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001756 goto out;
1757 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001758
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001760 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001762
1763 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001764 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001765 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001767out:
1768 rcu_read_unlock();
1769 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770}
Ben Blum67523c42010-03-10 15:22:11 -08001771EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772
Tejun Heo857a2be2013-04-14 20:50:08 -07001773/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001774 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001775 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001776 * @buf: the buffer to write the path into
1777 * @buflen: the length of the buffer
1778 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001779 * Determine @task's cgroup on the first (the one with the lowest non-zero
1780 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1781 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1782 * cgroup controller callbacks.
1783 *
1784 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001785 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001786int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001787{
1788 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001789 struct cgroup *cgrp;
1790 int hierarchy_id = 1, ret = 0;
1791
1792 if (buflen < 2)
1793 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001794
1795 mutex_lock(&cgroup_mutex);
1796
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1798
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 if (root) {
1800 cgrp = task_cgroup_from_root(task, root);
1801 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001802 } else {
1803 /* if no hierarchy exists, everyone is in "/" */
1804 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001805 }
1806
1807 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001808 return ret;
1809}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001810EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001811
Ben Blum74a11662011-05-26 16:25:20 -07001812/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001813 * Control Group taskset
1814 */
Tejun Heo134d3372011-12-12 18:12:21 -08001815struct task_and_cgroup {
1816 struct task_struct *task;
1817 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001818 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001819};
1820
Tejun Heo2f7ee562011-12-12 18:12:21 -08001821struct cgroup_taskset {
1822 struct task_and_cgroup single;
1823 struct flex_array *tc_array;
1824 int tc_array_len;
1825 int idx;
1826 struct cgroup *cur_cgrp;
1827};
1828
1829/**
1830 * cgroup_taskset_first - reset taskset and return the first task
1831 * @tset: taskset of interest
1832 *
1833 * @tset iteration is initialized and the first task is returned.
1834 */
1835struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1836{
1837 if (tset->tc_array) {
1838 tset->idx = 0;
1839 return cgroup_taskset_next(tset);
1840 } else {
1841 tset->cur_cgrp = tset->single.cgrp;
1842 return tset->single.task;
1843 }
1844}
1845EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1846
1847/**
1848 * cgroup_taskset_next - iterate to the next task in taskset
1849 * @tset: taskset of interest
1850 *
1851 * Return the next task in @tset. Iteration must have been initialized
1852 * with cgroup_taskset_first().
1853 */
1854struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1855{
1856 struct task_and_cgroup *tc;
1857
1858 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1859 return NULL;
1860
1861 tc = flex_array_get(tset->tc_array, tset->idx++);
1862 tset->cur_cgrp = tc->cgrp;
1863 return tc->task;
1864}
1865EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1866
1867/**
Tejun Heod99c8722013-08-08 20:11:27 -04001868 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001870 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001871 *
Tejun Heod99c8722013-08-08 20:11:27 -04001872 * Return the css for the current (last returned) task of @tset for
1873 * subsystem specified by @subsys_id. This function must be preceded by
1874 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875 */
Tejun Heod99c8722013-08-08 20:11:27 -04001876struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1877 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001878{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001879 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001880}
Tejun Heod99c8722013-08-08 20:11:27 -04001881EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001882
1883/**
1884 * cgroup_taskset_size - return the number of tasks in taskset
1885 * @tset: taskset of interest
1886 */
1887int cgroup_taskset_size(struct cgroup_taskset *tset)
1888{
1889 return tset->tc_array ? tset->tc_array_len : 1;
1890}
1891EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1892
1893
Ben Blum74a11662011-05-26 16:25:20 -07001894/*
1895 * cgroup_task_migrate - move a task from one cgroup to another.
1896 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001897 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001898 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001899static void cgroup_task_migrate(struct cgroup *old_cgrp,
1900 struct task_struct *tsk,
1901 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001902{
Tejun Heo5abb8852013-06-12 21:04:49 -07001903 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001904
1905 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001906 * We are synchronized through threadgroup_lock() against PF_EXITING
1907 * setting such that we can't race against cgroup_exit() changing the
1908 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001909 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001910 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001911 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001912
Ben Blum74a11662011-05-26 16:25:20 -07001913 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001914 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001915 task_unlock(tsk);
1916
1917 /* Update the css_set linked lists if we're using them */
1918 write_lock(&css_set_lock);
1919 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001920 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001921 write_unlock(&css_set_lock);
1922
1923 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001924 * We just gained a reference on old_cset by taking it from the
1925 * task. As trading it for new_cset is protected by cgroup_mutex,
1926 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001927 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001928 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1929 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001930}
1931
Li Zefana043e3b2008-02-23 15:24:09 -08001932/**
Li Zefan081aa452013-03-13 09:17:09 +08001933 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001934 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001935 * @tsk: the task or the leader of the threadgroup to be attached
1936 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001937 *
Tejun Heo257058a2011-12-12 18:12:21 -08001938 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001939 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001940 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001941static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1942 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001943{
1944 int retval, i, group_size;
1945 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001946 struct cgroupfs_root *root = cgrp->root;
1947 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001948 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001949 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001950 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001951 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001952
1953 /*
1954 * step 0: in order to do expensive, possibly blocking operations for
1955 * every thread, we cannot iterate the thread group list, since it needs
1956 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001957 * group - group_rwsem prevents new threads from appearing, and if
1958 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001959 */
Li Zefan081aa452013-03-13 09:17:09 +08001960 if (threadgroup)
1961 group_size = get_nr_threads(tsk);
1962 else
1963 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001964 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001965 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001966 if (!group)
1967 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001968 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001969 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001970 if (retval)
1971 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001972
Ben Blum74a11662011-05-26 16:25:20 -07001973 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001974 /*
1975 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1976 * already PF_EXITING could be freed from underneath us unless we
1977 * take an rcu_read_lock.
1978 */
1979 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001980 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001981 struct task_and_cgroup ent;
1982
Tejun Heocd3d0952011-12-12 18:12:21 -08001983 /* @tsk either already exited or can't exit until the end */
1984 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001985 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001986
Ben Blum74a11662011-05-26 16:25:20 -07001987 /* as per above, nr_threads may decrease, but not increase. */
1988 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001989 ent.task = tsk;
1990 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001991 /* nothing to do if this task is already in the cgroup */
1992 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001993 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001994 /*
1995 * saying GFP_ATOMIC has no effect here because we did prealloc
1996 * earlier, but it's good form to communicate our expectations.
1997 */
Tejun Heo134d3372011-12-12 18:12:21 -08001998 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001999 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002000 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002001 next:
Li Zefan081aa452013-03-13 09:17:09 +08002002 if (!threadgroup)
2003 break;
Ben Blum74a11662011-05-26 16:25:20 -07002004 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002005 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002006 /* remember the number of threads in the array for later. */
2007 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002008 tset.tc_array = group;
2009 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002010
Tejun Heo134d3372011-12-12 18:12:21 -08002011 /* methods shouldn't be called if no task is actually migrating */
2012 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002013 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002014 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002015
Ben Blum74a11662011-05-26 16:25:20 -07002016 /*
2017 * step 1: check that we can legitimately attach to the cgroup.
2018 */
Tejun Heo5549c492013-06-24 15:21:48 -07002019 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002020 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002021
Ben Blum74a11662011-05-26 16:25:20 -07002022 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002023 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002024 if (retval) {
2025 failed_ss = ss;
2026 goto out_cancel_attach;
2027 }
2028 }
Ben Blum74a11662011-05-26 16:25:20 -07002029 }
2030
2031 /*
2032 * step 2: make sure css_sets exist for all threads to be migrated.
2033 * we use find_css_set, which allocates a new one if necessary.
2034 */
Ben Blum74a11662011-05-26 16:25:20 -07002035 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002036 struct css_set *old_cset;
2037
Tejun Heo134d3372011-12-12 18:12:21 -08002038 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002039 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002040 tc->cset = find_css_set(old_cset, cgrp);
2041 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002042 retval = -ENOMEM;
2043 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002044 }
2045 }
2046
2047 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002048 * step 3: now that we're guaranteed success wrt the css_sets,
2049 * proceed to move all tasks to the new cgroup. There are no
2050 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002051 */
Ben Blum74a11662011-05-26 16:25:20 -07002052 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002053 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002054 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002055 }
2056 /* nothing is sensitive to fork() after this point. */
2057
2058 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002059 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002060 */
Tejun Heo5549c492013-06-24 15:21:48 -07002061 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002062 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002063
Ben Blum74a11662011-05-26 16:25:20 -07002064 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002065 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002066 }
2067
2068 /*
2069 * step 5: success! and cleanup
2070 */
Ben Blum74a11662011-05-26 16:25:20 -07002071 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002072out_put_css_set_refs:
2073 if (retval) {
2074 for (i = 0; i < group_size; i++) {
2075 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002076 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002077 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002078 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002079 }
Ben Blum74a11662011-05-26 16:25:20 -07002080 }
2081out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002082 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002083 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002084 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002085
Tejun Heo494c1672011-12-12 18:12:22 -08002086 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002087 break;
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002089 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002090 }
2091 }
Ben Blum74a11662011-05-26 16:25:20 -07002092out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002093 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002094 return retval;
2095}
2096
2097/*
2098 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002099 * function to attach either it or all tasks in its threadgroup. Will lock
2100 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002101 */
2102static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002103{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002105 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002106 int ret;
2107
Ben Blum74a11662011-05-26 16:25:20 -07002108 if (!cgroup_lock_live_group(cgrp))
2109 return -ENODEV;
2110
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002111retry_find_task:
2112 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002113 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002114 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002115 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002116 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002117 ret= -ESRCH;
2118 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119 }
Ben Blum74a11662011-05-26 16:25:20 -07002120 /*
2121 * even if we're attaching all tasks in the thread group, we
2122 * only need to check permissions on one of them.
2123 */
David Howellsc69e8d92008-11-14 10:39:19 +11002124 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002125 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2126 !uid_eq(cred->euid, tcred->uid) &&
2127 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002128 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002129 ret = -EACCES;
2130 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002132 } else
2133 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002134
2135 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002137
2138 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002139 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002140 * trapped in a cpuset, or RT worker may be born in a cgroup
2141 * with no rt_runtime allocated. Just say no.
2142 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002143 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002144 ret = -EINVAL;
2145 rcu_read_unlock();
2146 goto out_unlock_cgroup;
2147 }
2148
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002149 get_task_struct(tsk);
2150 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002151
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 threadgroup_lock(tsk);
2153 if (threadgroup) {
2154 if (!thread_group_leader(tsk)) {
2155 /*
2156 * a race with de_thread from another thread's exec()
2157 * may strip us of our leadership, if this happens,
2158 * there is no choice but to throw this task away and
2159 * try again; this is
2160 * "double-double-toil-and-trouble-check locking".
2161 */
2162 threadgroup_unlock(tsk);
2163 put_task_struct(tsk);
2164 goto retry_find_task;
2165 }
Li Zefan081aa452013-03-13 09:17:09 +08002166 }
2167
2168 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2169
Tejun Heocd3d0952011-12-12 18:12:21 -08002170 threadgroup_unlock(tsk);
2171
Paul Menagebbcb81d2007-10-18 23:39:32 -07002172 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002174 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 return ret;
2176}
2177
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002178/**
2179 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2180 * @from: attach to all cgroups of a given task
2181 * @tsk: the task to be attached
2182 */
2183int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2184{
2185 struct cgroupfs_root *root;
2186 int retval = 0;
2187
Tejun Heo47cfcd02013-04-07 09:29:51 -07002188 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002189 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002190 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002191
Li Zefan6f4b7e62013-07-31 16:18:36 +08002192 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002193 if (retval)
2194 break;
2195 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002196 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002197
2198 return retval;
2199}
2200EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2201
Tejun Heo182446d2013-08-08 20:11:24 -04002202static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2203 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002204{
Tejun Heo182446d2013-08-08 20:11:24 -04002205 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002206}
2207
Tejun Heo182446d2013-08-08 20:11:24 -04002208static int cgroup_procs_write(struct cgroup_subsys_state *css,
2209 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002210{
Tejun Heo182446d2013-08-08 20:11:24 -04002211 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002212}
2213
Tejun Heo182446d2013-08-08 20:11:24 -04002214static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2215 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002216{
Tejun Heo182446d2013-08-08 20:11:24 -04002217 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002218 if (strlen(buffer) >= PATH_MAX)
2219 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002220 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002221 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002222 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002223 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002224 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002225 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002226 return 0;
2227}
2228
Tejun Heo182446d2013-08-08 20:11:24 -04002229static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2230 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002231{
Tejun Heo182446d2013-08-08 20:11:24 -04002232 struct cgroup *cgrp = css->cgroup;
2233
Paul Menagee788e062008-07-25 01:46:59 -07002234 if (!cgroup_lock_live_group(cgrp))
2235 return -ENODEV;
2236 seq_puts(seq, cgrp->root->release_agent_path);
2237 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002238 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002239 return 0;
2240}
2241
Tejun Heo182446d2013-08-08 20:11:24 -04002242static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2243 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002244{
Tejun Heo182446d2013-08-08 20:11:24 -04002245 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002246 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247}
2248
Paul Menage84eea842008-07-25 01:47:00 -07002249/* A buffer size big enough for numbers or short strings */
2250#define CGROUP_LOCAL_BUFFER_SIZE 64
2251
Tejun Heoa742c592013-12-05 12:28:03 -05002252static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002253 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002254{
Tejun Heo182446d2013-08-08 20:11:24 -04002255 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002256 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002257 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002258 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2259 char *buf;
2260 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002261
Tejun Heoa742c592013-12-05 12:28:03 -05002262 if (nbytes >= max_bytes)
2263 return -E2BIG;
2264
2265 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2266 if (!buf)
2267 return -ENOMEM;
2268
2269 if (copy_from_user(buf, userbuf, nbytes)) {
2270 ret = -EFAULT;
2271 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002272 }
Tejun Heoa742c592013-12-05 12:28:03 -05002273
2274 buf[nbytes] = '\0';
2275
2276 if (cft->write_string) {
2277 ret = cft->write_string(css, cft, strstrip(buf));
2278 } else if (cft->write_u64) {
2279 unsigned long long v;
2280 ret = kstrtoull(buf, 0, &v);
2281 if (!ret)
2282 ret = cft->write_u64(css, cft, v);
2283 } else if (cft->write_s64) {
2284 long long v;
2285 ret = kstrtoll(buf, 0, &v);
2286 if (!ret)
2287 ret = cft->write_s64(css, cft, v);
2288 } else if (cft->trigger) {
2289 ret = cft->trigger(css, (unsigned int)cft->private);
2290 } else {
2291 ret = -EINVAL;
2292 }
2293out_free:
2294 kfree(buf);
2295 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002296}
2297
Tejun Heo182446d2013-08-08 20:11:24 -04002298static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2299 struct cftype *cft, struct file *file,
2300 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002301{
Paul Menage84eea842008-07-25 01:47:00 -07002302 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002303 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002304 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2305
2306 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2307}
2308
Tejun Heo182446d2013-08-08 20:11:24 -04002309static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2310 struct cftype *cft, struct file *file,
2311 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002312{
Paul Menage84eea842008-07-25 01:47:00 -07002313 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002314 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002315 int len = sprintf(tmp, "%lld\n", (long long) val);
2316
2317 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2318}
2319
Paul Menageddbcc7e2007-10-18 23:39:30 -07002320static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002321 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002322{
Tejun Heo182446d2013-08-08 20:11:24 -04002323 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002324 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002325 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002326
Paul Menagef4c753b2008-04-29 00:59:56 -07002327 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002328 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002330 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002331 return -EINVAL;
2332}
2333
Paul Menage91796562008-04-29 01:00:01 -07002334/*
2335 * seqfile ops/methods for returning structured data. Currently just
2336 * supports string->u64 maps, but can be extended in future.
2337 */
2338
Paul Menage91796562008-04-29 01:00:01 -07002339static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2340{
Li Zefane0798ce2013-07-31 17:36:25 +08002341 struct cfent *cfe = m->private;
2342 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002343 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002344
Tejun Heo182446d2013-08-08 20:11:24 -04002345 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002346}
2347
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002348static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002349 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002350 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002351 .llseek = seq_lseek,
Tejun Heoe605b362013-11-27 18:16:21 -05002352 .release = cgroup_file_release,
Paul Menage91796562008-04-29 01:00:01 -07002353};
2354
Paul Menageddbcc7e2007-10-18 23:39:30 -07002355static int cgroup_file_open(struct inode *inode, struct file *file)
2356{
Tejun Heof7d58812013-08-08 20:11:23 -04002357 struct cfent *cfe = __d_cfe(file->f_dentry);
2358 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002359 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2360 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362
2363 err = generic_file_open(inode, file);
2364 if (err)
2365 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002366
2367 /*
2368 * If the file belongs to a subsystem, pin the css. Will be
2369 * unpinned either on open failure or release. This ensures that
2370 * @css stays alive for all file operations.
2371 */
Tejun Heo105347b2013-08-13 11:01:55 -04002372 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002373 css = cgroup_css(cgrp, cft->ss);
2374 if (cft->ss && !css_tryget(css))
2375 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002376 rcu_read_unlock();
2377
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002378 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002379 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002380
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002381 /*
2382 * @cfe->css is used by read/write/close to determine the
2383 * associated css. @file->private_data would be a better place but
2384 * that's already used by seqfile. Multiple accessors may use it
2385 * simultaneously which is okay as the association never changes.
2386 */
2387 WARN_ON_ONCE(cfe->css && cfe->css != css);
2388 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389
Tejun Heo6e0755b2013-12-05 12:28:03 -05002390 if (cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002391 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002392 err = single_open(file, cgroup_seqfile_show, cfe);
2393 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002395 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396
Tejun Heo67f4c362013-08-08 20:11:24 -04002397 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002398 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399 return err;
2400}
2401
2402static int cgroup_file_release(struct inode *inode, struct file *file)
2403{
Tejun Heof7d58812013-08-08 20:11:23 -04002404 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002405 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002406
Tejun Heo67f4c362013-08-08 20:11:24 -04002407 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002408 css_put(css);
Tejun Heoe605b362013-11-27 18:16:21 -05002409 if (file->f_op == &cgroup_seqfile_operations)
2410 single_release(inode, file);
Tejun Heob1a21362013-11-29 10:42:58 -05002411 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412}
2413
2414/*
2415 * cgroup_rename - Only allow simple rename of directories in place.
2416 */
2417static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2418 struct inode *new_dir, struct dentry *new_dentry)
2419{
Li Zefan65dff752013-03-01 15:01:56 +08002420 int ret;
2421 struct cgroup_name *name, *old_name;
2422 struct cgroup *cgrp;
2423
2424 /*
2425 * It's convinient to use parent dir's i_mutex to protected
2426 * cgrp->name.
2427 */
2428 lockdep_assert_held(&old_dir->i_mutex);
2429
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2431 return -ENOTDIR;
2432 if (new_dentry->d_inode)
2433 return -EEXIST;
2434 if (old_dir != new_dir)
2435 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002436
2437 cgrp = __d_cgrp(old_dentry);
2438
Tejun Heo6db8e852013-06-14 11:18:22 -07002439 /*
2440 * This isn't a proper migration and its usefulness is very
2441 * limited. Disallow if sane_behavior.
2442 */
2443 if (cgroup_sane_behavior(cgrp))
2444 return -EPERM;
2445
Li Zefan65dff752013-03-01 15:01:56 +08002446 name = cgroup_alloc_name(new_dentry);
2447 if (!name)
2448 return -ENOMEM;
2449
2450 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2451 if (ret) {
2452 kfree(name);
2453 return ret;
2454 }
2455
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002456 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002457 rcu_assign_pointer(cgrp->name, name);
2458
2459 kfree_rcu(old_name, rcu_head);
2460 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461}
2462
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002463static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2464{
2465 if (S_ISDIR(dentry->d_inode->i_mode))
2466 return &__d_cgrp(dentry)->xattrs;
2467 else
Li Zefan712317a2013-04-18 23:09:52 -07002468 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002469}
2470
2471static inline int xattr_enabled(struct dentry *dentry)
2472{
2473 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002474 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002475}
2476
2477static bool is_valid_xattr(const char *name)
2478{
2479 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2480 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2481 return true;
2482 return false;
2483}
2484
2485static int cgroup_setxattr(struct dentry *dentry, const char *name,
2486 const void *val, size_t size, int flags)
2487{
2488 if (!xattr_enabled(dentry))
2489 return -EOPNOTSUPP;
2490 if (!is_valid_xattr(name))
2491 return -EINVAL;
2492 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2493}
2494
2495static int cgroup_removexattr(struct dentry *dentry, const char *name)
2496{
2497 if (!xattr_enabled(dentry))
2498 return -EOPNOTSUPP;
2499 if (!is_valid_xattr(name))
2500 return -EINVAL;
2501 return simple_xattr_remove(__d_xattrs(dentry), name);
2502}
2503
2504static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2505 void *buf, size_t size)
2506{
2507 if (!xattr_enabled(dentry))
2508 return -EOPNOTSUPP;
2509 if (!is_valid_xattr(name))
2510 return -EINVAL;
2511 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2512}
2513
2514static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2515{
2516 if (!xattr_enabled(dentry))
2517 return -EOPNOTSUPP;
2518 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2519}
2520
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002521static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 .read = cgroup_file_read,
2523 .write = cgroup_file_write,
2524 .llseek = generic_file_llseek,
2525 .open = cgroup_file_open,
2526 .release = cgroup_file_release,
2527};
2528
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002529static const struct inode_operations cgroup_file_inode_operations = {
2530 .setxattr = cgroup_setxattr,
2531 .getxattr = cgroup_getxattr,
2532 .listxattr = cgroup_listxattr,
2533 .removexattr = cgroup_removexattr,
2534};
2535
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002536static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002537 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002538 .mkdir = cgroup_mkdir,
2539 .rmdir = cgroup_rmdir,
2540 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002541 .setxattr = cgroup_setxattr,
2542 .getxattr = cgroup_getxattr,
2543 .listxattr = cgroup_listxattr,
2544 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545};
2546
Al Viroa5e7ed32011-07-26 01:55:55 -04002547static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002548 struct super_block *sb)
2549{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002550 struct inode *inode;
2551
2552 if (!dentry)
2553 return -ENOENT;
2554 if (dentry->d_inode)
2555 return -EEXIST;
2556
2557 inode = cgroup_new_inode(mode, sb);
2558 if (!inode)
2559 return -ENOMEM;
2560
2561 if (S_ISDIR(mode)) {
2562 inode->i_op = &cgroup_dir_inode_operations;
2563 inode->i_fop = &simple_dir_operations;
2564
2565 /* start off with i_nlink == 2 (for "." entry) */
2566 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002567 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002568
Tejun Heob8a2df62012-11-19 08:13:37 -08002569 /*
2570 * Control reaches here with cgroup_mutex held.
2571 * @inode->i_mutex should nest outside cgroup_mutex but we
2572 * want to populate it immediately without releasing
2573 * cgroup_mutex. As @inode isn't visible to anyone else
2574 * yet, trylock will always succeed without affecting
2575 * lockdep checks.
2576 */
2577 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002578 } else if (S_ISREG(mode)) {
2579 inode->i_size = 0;
2580 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002581 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002582 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002583 d_instantiate(dentry, inode);
2584 dget(dentry); /* Extra count - pin the dentry in core */
2585 return 0;
2586}
2587
Li Zefan099fca32009-04-02 16:57:29 -07002588/**
2589 * cgroup_file_mode - deduce file mode of a control file
2590 * @cft: the control file in question
2591 *
2592 * returns cft->mode if ->mode is not 0
2593 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2594 * returns S_IRUGO if it has only a read handler
2595 * returns S_IWUSR if it has only a write hander
2596 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002597static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002598{
Al Viroa5e7ed32011-07-26 01:55:55 -04002599 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002600
2601 if (cft->mode)
2602 return cft->mode;
2603
Tejun Heo6e0755b2013-12-05 12:28:03 -05002604 if (cft->read_u64 || cft->read_s64 || cft->read_seq_string)
Li Zefan099fca32009-04-02 16:57:29 -07002605 mode |= S_IRUGO;
2606
Tejun Heo6e0755b2013-12-05 12:28:03 -05002607 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2608 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002609 mode |= S_IWUSR;
2610
2611 return mode;
2612}
2613
Tejun Heo2bb566c2013-08-08 20:11:23 -04002614static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002615{
Paul Menagebd89aab2007-10-18 23:40:44 -07002616 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002617 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002618 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002619 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002620 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002621 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002623
Tejun Heo9fa4db32013-08-26 18:40:56 -04002624 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2625 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002626 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002627 strcat(name, ".");
2628 }
2629 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002630
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002632
2633 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2634 if (!cfe)
2635 return -ENOMEM;
2636
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002638 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002640 goto out;
2641 }
2642
Li Zefand6cbf352013-05-14 19:44:20 +08002643 cfe->type = (void *)cft;
2644 cfe->dentry = dentry;
2645 dentry->d_fsdata = cfe;
2646 simple_xattrs_init(&cfe->xattrs);
2647
Tejun Heo05ef1d72012-04-01 12:09:56 -07002648 mode = cgroup_file_mode(cft);
2649 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2650 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002651 list_add_tail(&cfe->node, &parent->files);
2652 cfe = NULL;
2653 }
2654 dput(dentry);
2655out:
2656 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657 return error;
2658}
2659
Tejun Heob1f28d32013-06-28 16:24:10 -07002660/**
2661 * cgroup_addrm_files - add or remove files to a cgroup directory
2662 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002663 * @cfts: array of cftypes to be added
2664 * @is_add: whether to add or remove
2665 *
2666 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002667 * For removals, this function never fails. If addition fails, this
2668 * function doesn't remove files already added. The caller is responsible
2669 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002670 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002671static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2672 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002674 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002675 int ret;
2676
2677 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2678 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002679
2680 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002681 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002682 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2683 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002684 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2685 continue;
2686 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2687 continue;
2688
Li Zefan2739d3c2013-01-21 18:18:33 +08002689 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002690 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002691 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002692 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002693 cft->name, ret);
2694 return ret;
2695 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002696 } else {
2697 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002698 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002699 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002700 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701}
2702
Tejun Heo8e3f6542012-04-01 12:09:55 -07002703static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002704 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002705{
2706 /*
2707 * Thanks to the entanglement with vfs inode locking, we can't walk
2708 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002709 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2710 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002711 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002712 mutex_lock(&cgroup_mutex);
2713}
2714
Tejun Heo2bb566c2013-08-08 20:11:23 -04002715static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002716 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002717{
2718 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002719 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002720 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002721 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002722 struct dentry *prev = NULL;
2723 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002724 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002725 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002726 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002727
2728 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002729 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002730 !atomic_inc_not_zero(&sb->s_active)) {
2731 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002732 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002733 }
2734
Li Zefane8c82d22013-06-18 18:48:37 +08002735 /*
2736 * All cgroups which are created after we drop cgroup_mutex will
2737 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002738 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002739 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002740 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002741
Tejun Heo8e3f6542012-04-01 12:09:55 -07002742 mutex_unlock(&cgroup_mutex);
2743
Li Zefane8c82d22013-06-18 18:48:37 +08002744 /* add/rm files for all cgroups created before */
2745 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002746 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002747 struct cgroup *cgrp = css->cgroup;
2748
Li Zefane8c82d22013-06-18 18:48:37 +08002749 if (cgroup_is_dead(cgrp))
2750 continue;
2751
2752 inode = cgrp->dentry->d_inode;
2753 dget(cgrp->dentry);
2754 rcu_read_unlock();
2755
2756 dput(prev);
2757 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002758
2759 mutex_lock(&inode->i_mutex);
2760 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002761 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002762 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002763 mutex_unlock(&cgroup_mutex);
2764 mutex_unlock(&inode->i_mutex);
2765
Li Zefane8c82d22013-06-18 18:48:37 +08002766 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002767 if (ret)
2768 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002769 }
Li Zefane8c82d22013-06-18 18:48:37 +08002770 rcu_read_unlock();
2771 dput(prev);
2772 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002773 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002774}
2775
2776/**
2777 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2778 * @ss: target cgroup subsystem
2779 * @cfts: zero-length name terminated array of cftypes
2780 *
2781 * Register @cfts to @ss. Files described by @cfts are created for all
2782 * existing cgroups to which @ss is attached and all future cgroups will
2783 * have them too. This function can be called anytime whether @ss is
2784 * attached or not.
2785 *
2786 * Returns 0 on successful registration, -errno on failure. Note that this
2787 * function currently returns 0 as long as @cfts registration is successful
2788 * even if some file creation attempts on existing cgroups fail.
2789 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002790int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791{
2792 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002793 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002794 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002795
2796 set = kzalloc(sizeof(*set), GFP_KERNEL);
2797 if (!set)
2798 return -ENOMEM;
2799
Tejun Heo2bb566c2013-08-08 20:11:23 -04002800 for (cft = cfts; cft->name[0] != '\0'; cft++)
2801 cft->ss = ss;
2802
Tejun Heo8e3f6542012-04-01 12:09:55 -07002803 cgroup_cfts_prepare();
2804 set->cfts = cfts;
2805 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002806 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002807 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002808 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002809 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002810}
2811EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2812
Li Zefana043e3b2008-02-23 15:24:09 -08002813/**
Tejun Heo79578622012-04-01 12:09:56 -07002814 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002815 * @cfts: zero-length name terminated array of cftypes
2816 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002817 * Unregister @cfts. Files described by @cfts are removed from all
2818 * existing cgroups and all future cgroups won't have them either. This
2819 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002820 *
2821 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002822 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002823 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002824int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002825{
2826 struct cftype_set *set;
2827
Tejun Heo2bb566c2013-08-08 20:11:23 -04002828 if (!cfts || !cfts[0].ss)
2829 return -ENOENT;
2830
Tejun Heo79578622012-04-01 12:09:56 -07002831 cgroup_cfts_prepare();
2832
Tejun Heo2bb566c2013-08-08 20:11:23 -04002833 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002834 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002835 list_del(&set->node);
2836 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002837 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002838 return 0;
2839 }
2840 }
2841
Tejun Heo2bb566c2013-08-08 20:11:23 -04002842 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002843 return -ENOENT;
2844}
2845
2846/**
Li Zefana043e3b2008-02-23 15:24:09 -08002847 * cgroup_task_count - count the number of tasks in a cgroup.
2848 * @cgrp: the cgroup in question
2849 *
2850 * Return the number of tasks in the cgroup.
2851 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002852int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002853{
2854 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002855 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002856
Paul Menage817929e2007-10-18 23:39:36 -07002857 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002858 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2859 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002860 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002861 return count;
2862}
2863
2864/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002865 * To reduce the fork() overhead for systems that are not actually using
2866 * their cgroups capability, we don't maintain the lists running through
2867 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002868 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002869 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002870static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002871{
2872 struct task_struct *p, *g;
2873 write_lock(&css_set_lock);
2874 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002875 /*
2876 * We need tasklist_lock because RCU is not safe against
2877 * while_each_thread(). Besides, a forking task that has passed
2878 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2879 * is not guaranteed to have its child immediately visible in the
2880 * tasklist if we walk through it with RCU.
2881 */
2882 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002883 do_each_thread(g, p) {
2884 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002885 /*
2886 * We should check if the process is exiting, otherwise
2887 * it will race with cgroup_exit() in that the list
2888 * entry won't be deleted though the process has exited.
2889 */
2890 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002891 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002892 task_unlock(p);
2893 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002894 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002895 write_unlock(&css_set_lock);
2896}
2897
Tejun Heo574bd9f2012-11-09 09:12:29 -08002898/**
Tejun Heo492eb212013-08-08 20:11:25 -04002899 * css_next_child - find the next child of a given css
2900 * @pos_css: the current position (%NULL to initiate traversal)
2901 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002902 *
Tejun Heo492eb212013-08-08 20:11:25 -04002903 * This function returns the next child of @parent_css and should be called
2904 * under RCU read lock. The only requirement is that @parent_css and
2905 * @pos_css are accessible. The next sibling is guaranteed to be returned
2906 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002907 */
Tejun Heo492eb212013-08-08 20:11:25 -04002908struct cgroup_subsys_state *
2909css_next_child(struct cgroup_subsys_state *pos_css,
2910 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002911{
Tejun Heo492eb212013-08-08 20:11:25 -04002912 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2913 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002914 struct cgroup *next;
2915
2916 WARN_ON_ONCE(!rcu_read_lock_held());
2917
2918 /*
2919 * @pos could already have been removed. Once a cgroup is removed,
2920 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002921 * changes. As CGRP_DEAD assertion is serialized and happens
2922 * before the cgroup is taken off the ->sibling list, if we see it
2923 * unasserted, it's guaranteed that the next sibling hasn't
2924 * finished its grace period even if it's already removed, and thus
2925 * safe to dereference from this RCU critical section. If
2926 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2927 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002928 *
2929 * If @pos is dead, its next pointer can't be dereferenced;
2930 * however, as each cgroup is given a monotonically increasing
2931 * unique serial number and always appended to the sibling list,
2932 * the next one can be found by walking the parent's children until
2933 * we see a cgroup with higher serial number than @pos's. While
2934 * this path can be slower, it's taken only when either the current
2935 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002936 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002937 if (!pos) {
2938 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2939 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002940 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002941 } else {
2942 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2943 if (next->serial_nr > pos->serial_nr)
2944 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002945 }
2946
Tejun Heo492eb212013-08-08 20:11:25 -04002947 if (&next->sibling == &cgrp->children)
2948 return NULL;
2949
Tejun Heoca8bdca2013-08-26 18:40:56 -04002950 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002951}
Tejun Heo492eb212013-08-08 20:11:25 -04002952EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002953
2954/**
Tejun Heo492eb212013-08-08 20:11:25 -04002955 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002956 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002957 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002958 *
Tejun Heo492eb212013-08-08 20:11:25 -04002959 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002960 * to visit for pre-order traversal of @root's descendants. @root is
2961 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002962 *
2963 * While this function requires RCU read locking, it doesn't require the
2964 * whole traversal to be contained in a single RCU critical section. This
2965 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04002966 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002967 */
Tejun Heo492eb212013-08-08 20:11:25 -04002968struct cgroup_subsys_state *
2969css_next_descendant_pre(struct cgroup_subsys_state *pos,
2970 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002971{
Tejun Heo492eb212013-08-08 20:11:25 -04002972 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002973
2974 WARN_ON_ONCE(!rcu_read_lock_held());
2975
Tejun Heobd8815a2013-08-08 20:11:27 -04002976 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002977 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002978 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002979
2980 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002981 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002982 if (next)
2983 return next;
2984
2985 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002986 while (pos != root) {
2987 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002988 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002989 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002990 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002991 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002992
2993 return NULL;
2994}
Tejun Heo492eb212013-08-08 20:11:25 -04002995EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002996
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002997/**
Tejun Heo492eb212013-08-08 20:11:25 -04002998 * css_rightmost_descendant - return the rightmost descendant of a css
2999 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003000 *
Tejun Heo492eb212013-08-08 20:11:25 -04003001 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3002 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003003 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003004 *
3005 * While this function requires RCU read locking, it doesn't require the
3006 * whole traversal to be contained in a single RCU critical section. This
3007 * function will return the correct rightmost descendant as long as @pos is
3008 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003009 */
Tejun Heo492eb212013-08-08 20:11:25 -04003010struct cgroup_subsys_state *
3011css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003012{
Tejun Heo492eb212013-08-08 20:11:25 -04003013 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003014
3015 WARN_ON_ONCE(!rcu_read_lock_held());
3016
3017 do {
3018 last = pos;
3019 /* ->prev isn't RCU safe, walk ->next till the end */
3020 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003021 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003022 pos = tmp;
3023 } while (pos);
3024
3025 return last;
3026}
Tejun Heo492eb212013-08-08 20:11:25 -04003027EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003028
Tejun Heo492eb212013-08-08 20:11:25 -04003029static struct cgroup_subsys_state *
3030css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003031{
Tejun Heo492eb212013-08-08 20:11:25 -04003032 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003033
3034 do {
3035 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003036 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003037 } while (pos);
3038
3039 return last;
3040}
3041
3042/**
Tejun Heo492eb212013-08-08 20:11:25 -04003043 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003044 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003045 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046 *
Tejun Heo492eb212013-08-08 20:11:25 -04003047 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003048 * to visit for post-order traversal of @root's descendants. @root is
3049 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003050 *
3051 * While this function requires RCU read locking, it doesn't require the
3052 * whole traversal to be contained in a single RCU critical section. This
3053 * function will return the correct next descendant as long as both @pos
3054 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003055 */
Tejun Heo492eb212013-08-08 20:11:25 -04003056struct cgroup_subsys_state *
3057css_next_descendant_post(struct cgroup_subsys_state *pos,
3058 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003059{
Tejun Heo492eb212013-08-08 20:11:25 -04003060 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061
3062 WARN_ON_ONCE(!rcu_read_lock_held());
3063
Tejun Heo58b79a92013-09-06 15:31:08 -04003064 /* if first iteration, visit leftmost descendant which may be @root */
3065 if (!pos)
3066 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067
Tejun Heobd8815a2013-08-08 20:11:27 -04003068 /* if we visited @root, we're done */
3069 if (pos == root)
3070 return NULL;
3071
Tejun Heo574bd9f2012-11-09 09:12:29 -08003072 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003073 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003074 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003075 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003076
3077 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003078 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003079}
Tejun Heo492eb212013-08-08 20:11:25 -04003080EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081
Tejun Heo0942eee2013-08-08 20:11:26 -04003082/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003083 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003084 * @it: the iterator to advance
3085 *
3086 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003087 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003088static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003089{
3090 struct list_head *l = it->cset_link;
3091 struct cgrp_cset_link *link;
3092 struct css_set *cset;
3093
3094 /* Advance to the next non-empty css_set */
3095 do {
3096 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003097 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003098 it->cset_link = NULL;
3099 return;
3100 }
3101 link = list_entry(l, struct cgrp_cset_link, cset_link);
3102 cset = link->cset;
3103 } while (list_empty(&cset->tasks));
3104 it->cset_link = l;
3105 it->task = cset->tasks.next;
3106}
3107
Tejun Heo0942eee2013-08-08 20:11:26 -04003108/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003109 * css_task_iter_start - initiate task iteration
3110 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003111 * @it: the task iterator to use
3112 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003113 * Initiate iteration through the tasks of @css. The caller can call
3114 * css_task_iter_next() to walk through the tasks until the function
3115 * returns NULL. On completion of iteration, css_task_iter_end() must be
3116 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003117 *
3118 * Note that this function acquires a lock which is released when the
3119 * iteration finishes. The caller can't sleep while iteration is in
3120 * progress.
3121 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003122void css_task_iter_start(struct cgroup_subsys_state *css,
3123 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003124 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003125{
3126 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003127 * The first time anyone tries to iterate across a css, we need to
3128 * enable the list linking each css_set to its tasks, and fix up
3129 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003130 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003131 if (!use_task_css_set_links)
3132 cgroup_enable_task_cg_lists();
3133
Paul Menage817929e2007-10-18 23:39:36 -07003134 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003135
Tejun Heo72ec7022013-08-08 20:11:26 -04003136 it->origin_css = css;
3137 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003138
Tejun Heo72ec7022013-08-08 20:11:26 -04003139 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003140}
3141
Tejun Heo0942eee2013-08-08 20:11:26 -04003142/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003143 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003144 * @it: the task iterator being iterated
3145 *
3146 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003147 * initialized via css_task_iter_start(). Returns NULL when the iteration
3148 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003149 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003150struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003151{
3152 struct task_struct *res;
3153 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003154 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003155
3156 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003157 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003158 return NULL;
3159 res = list_entry(l, struct task_struct, cg_list);
3160 /* Advance iterator to find next entry */
3161 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003162 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3163 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003164 /*
3165 * We reached the end of this task list - move on to the
3166 * next cgrp_cset_link.
3167 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003168 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003169 } else {
3170 it->task = l;
3171 }
3172 return res;
3173}
3174
Tejun Heo0942eee2013-08-08 20:11:26 -04003175/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003176 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003177 * @it: the task iterator to finish
3178 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003179 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003180 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003181void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003182 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003183{
3184 read_unlock(&css_set_lock);
3185}
3186
Cliff Wickman31a7df02008-02-07 00:14:42 -08003187static inline int started_after_time(struct task_struct *t1,
3188 struct timespec *time,
3189 struct task_struct *t2)
3190{
3191 int start_diff = timespec_compare(&t1->start_time, time);
3192 if (start_diff > 0) {
3193 return 1;
3194 } else if (start_diff < 0) {
3195 return 0;
3196 } else {
3197 /*
3198 * Arbitrarily, if two processes started at the same
3199 * time, we'll say that the lower pointer value
3200 * started first. Note that t2 may have exited by now
3201 * so this may not be a valid pointer any longer, but
3202 * that's fine - it still serves to distinguish
3203 * between two tasks started (effectively) simultaneously.
3204 */
3205 return t1 > t2;
3206 }
3207}
3208
3209/*
3210 * This function is a callback from heap_insert() and is used to order
3211 * the heap.
3212 * In this case we order the heap in descending task start time.
3213 */
3214static inline int started_after(void *p1, void *p2)
3215{
3216 struct task_struct *t1 = p1;
3217 struct task_struct *t2 = p2;
3218 return started_after_time(t1, &t2->start_time, t2);
3219}
3220
3221/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003222 * css_scan_tasks - iterate though all the tasks in a css
3223 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003224 * @test: optional test callback
3225 * @process: process callback
3226 * @data: data passed to @test and @process
3227 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003228 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003229 * Iterate through all the tasks in @css, calling @test for each, and if it
3230 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003231 *
Tejun Heoe5358372013-08-08 20:11:26 -04003232 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003233 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003234 * lock css_set_lock for the call to @process.
3235 *
3236 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003237 * of @css for the duration of this call. This function may or may not
3238 * call @process for tasks that exit or move to a different css during the
3239 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003240 *
3241 * Note that @test may be called with locks held, and may in some
3242 * situations be called multiple times for the same task, so it should be
3243 * cheap.
3244 *
3245 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3246 * heap operations (and its "gt" member will be overwritten), else a
3247 * temporary heap will be used (allocation of which may cause this function
3248 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003249 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003250int css_scan_tasks(struct cgroup_subsys_state *css,
3251 bool (*test)(struct task_struct *, void *),
3252 void (*process)(struct task_struct *, void *),
3253 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003254{
3255 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003256 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003257 struct task_struct *p, *dropped;
3258 /* Never dereference latest_task, since it's not refcounted */
3259 struct task_struct *latest_task = NULL;
3260 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003261 struct timespec latest_time = { 0, 0 };
3262
Tejun Heoe5358372013-08-08 20:11:26 -04003263 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003264 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003265 heap->gt = &started_after;
3266 } else {
3267 /* We need to allocate our own heap memory */
3268 heap = &tmp_heap;
3269 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3270 if (retval)
3271 /* cannot allocate the heap */
3272 return retval;
3273 }
3274
3275 again:
3276 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003277 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003278 * which are of interest, and invoking @process callback on the
3279 * ones which need an update. Since we don't want to hold any
3280 * locks during the task updates, gather tasks to be processed in a
3281 * heap structure. The heap is sorted by descending task start
3282 * time. If the statically-sized heap fills up, we overflow tasks
3283 * that started later, and in future iterations only consider tasks
3284 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003285 * guarantees forward progress and that we don't miss any tasks.
3286 */
3287 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003288 css_task_iter_start(css, &it);
3289 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003290 /*
3291 * Only affect tasks that qualify per the caller's callback,
3292 * if he provided one
3293 */
Tejun Heoe5358372013-08-08 20:11:26 -04003294 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003295 continue;
3296 /*
3297 * Only process tasks that started after the last task
3298 * we processed
3299 */
3300 if (!started_after_time(p, &latest_time, latest_task))
3301 continue;
3302 dropped = heap_insert(heap, p);
3303 if (dropped == NULL) {
3304 /*
3305 * The new task was inserted; the heap wasn't
3306 * previously full
3307 */
3308 get_task_struct(p);
3309 } else if (dropped != p) {
3310 /*
3311 * The new task was inserted, and pushed out a
3312 * different task
3313 */
3314 get_task_struct(p);
3315 put_task_struct(dropped);
3316 }
3317 /*
3318 * Else the new task was newer than anything already in
3319 * the heap and wasn't inserted
3320 */
3321 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003322 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003323
3324 if (heap->size) {
3325 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003326 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003327 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003328 latest_time = q->start_time;
3329 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003330 }
3331 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003332 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003333 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003334 }
3335 /*
3336 * If we had to process any tasks at all, scan again
3337 * in case some of them were in the middle of forking
3338 * children that didn't get processed.
3339 * Not the most efficient way to do it, but it avoids
3340 * having to take callback_mutex in the fork path
3341 */
3342 goto again;
3343 }
3344 if (heap == &tmp_heap)
3345 heap_free(&tmp_heap);
3346 return 0;
3347}
3348
Tejun Heoe5358372013-08-08 20:11:26 -04003349static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003350{
Tejun Heoe5358372013-08-08 20:11:26 -04003351 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003352
Tejun Heo47cfcd02013-04-07 09:29:51 -07003353 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003354 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003355 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003356}
3357
3358/**
3359 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3360 * @to: cgroup to which the tasks will be moved
3361 * @from: cgroup in which the tasks currently reside
3362 */
3363int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3364{
Tejun Heo72ec7022013-08-08 20:11:26 -04003365 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3366 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003367}
3368
Paul Menage817929e2007-10-18 23:39:36 -07003369/*
Ben Blum102a7752009-09-23 15:56:26 -07003370 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003371 *
3372 * Reading this file can return large amounts of data if a cgroup has
3373 * *lots* of attached tasks. So it may need several calls to read(),
3374 * but we cannot guarantee that the information we produce is correct
3375 * unless we produce it entirely atomically.
3376 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003377 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003378
Li Zefan24528252012-01-20 11:58:43 +08003379/* which pidlist file are we talking about? */
3380enum cgroup_filetype {
3381 CGROUP_FILE_PROCS,
3382 CGROUP_FILE_TASKS,
3383};
3384
3385/*
3386 * A pidlist is a list of pids that virtually represents the contents of one
3387 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3388 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3389 * to the cgroup.
3390 */
3391struct cgroup_pidlist {
3392 /*
3393 * used to find which pidlist is wanted. doesn't change as long as
3394 * this particular list stays in the list.
3395 */
3396 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3397 /* array of xids */
3398 pid_t *list;
3399 /* how many elements the above list has */
3400 int length;
Li Zefan24528252012-01-20 11:58:43 +08003401 /* each of these stored in a list by its cgroup */
3402 struct list_head links;
3403 /* pointer to the cgroup we belong to, for list removal purposes */
3404 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003405 /* for delayed destruction */
3406 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003407};
3408
Tejun Heo62236852013-11-29 10:42:58 -05003409/* seq_file->private points to the following */
3410struct cgroup_pidlist_open_file {
3411 enum cgroup_filetype type;
3412 struct cgroup *cgrp;
3413 struct cgroup_pidlist *pidlist;
3414};
3415
Paul Menagebbcb81d2007-10-18 23:39:32 -07003416/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003417 * The following two functions "fix" the issue where there are more pids
3418 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3419 * TODO: replace with a kernel-wide solution to this problem
3420 */
3421#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3422static void *pidlist_allocate(int count)
3423{
3424 if (PIDLIST_TOO_LARGE(count))
3425 return vmalloc(count * sizeof(pid_t));
3426 else
3427 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3428}
Tejun Heob1a21362013-11-29 10:42:58 -05003429
Ben Blumd1d9fd32009-09-23 15:56:28 -07003430static void pidlist_free(void *p)
3431{
3432 if (is_vmalloc_addr(p))
3433 vfree(p);
3434 else
3435 kfree(p);
3436}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003437
3438/*
Tejun Heob1a21362013-11-29 10:42:58 -05003439 * Used to destroy all pidlists lingering waiting for destroy timer. None
3440 * should be left afterwards.
3441 */
3442static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3443{
3444 struct cgroup_pidlist *l, *tmp_l;
3445
3446 mutex_lock(&cgrp->pidlist_mutex);
3447 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3448 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3449 mutex_unlock(&cgrp->pidlist_mutex);
3450
3451 flush_workqueue(cgroup_pidlist_destroy_wq);
3452 BUG_ON(!list_empty(&cgrp->pidlists));
3453}
3454
3455static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3456{
3457 struct delayed_work *dwork = to_delayed_work(work);
3458 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3459 destroy_dwork);
3460 struct cgroup_pidlist *tofree = NULL;
3461
3462 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003463
3464 /*
Tejun Heo04502362013-11-29 10:42:59 -05003465 * Destroy iff we didn't get queued again. The state won't change
3466 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003467 */
Tejun Heo04502362013-11-29 10:42:59 -05003468 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003469 list_del(&l->links);
3470 pidlist_free(l->list);
3471 put_pid_ns(l->key.ns);
3472 tofree = l;
3473 }
3474
Tejun Heob1a21362013-11-29 10:42:58 -05003475 mutex_unlock(&l->owner->pidlist_mutex);
3476 kfree(tofree);
3477}
3478
3479/*
Ben Blum102a7752009-09-23 15:56:26 -07003480 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003481 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003482 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003483static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003484{
Ben Blum102a7752009-09-23 15:56:26 -07003485 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003486
3487 /*
3488 * we presume the 0th element is unique, so i starts at 1. trivial
3489 * edge cases first; no work needs to be done for either
3490 */
3491 if (length == 0 || length == 1)
3492 return length;
3493 /* src and dest walk down the list; dest counts unique elements */
3494 for (src = 1; src < length; src++) {
3495 /* find next unique element */
3496 while (list[src] == list[src-1]) {
3497 src++;
3498 if (src == length)
3499 goto after;
3500 }
3501 /* dest always points to where the next unique element goes */
3502 list[dest] = list[src];
3503 dest++;
3504 }
3505after:
Ben Blum102a7752009-09-23 15:56:26 -07003506 return dest;
3507}
3508
Tejun Heoafb2bc12013-11-29 10:42:59 -05003509/*
3510 * The two pid files - task and cgroup.procs - guaranteed that the result
3511 * is sorted, which forced this whole pidlist fiasco. As pid order is
3512 * different per namespace, each namespace needs differently sorted list,
3513 * making it impossible to use, for example, single rbtree of member tasks
3514 * sorted by task pointer. As pidlists can be fairly large, allocating one
3515 * per open file is dangerous, so cgroup had to implement shared pool of
3516 * pidlists keyed by cgroup and namespace.
3517 *
3518 * All this extra complexity was caused by the original implementation
3519 * committing to an entirely unnecessary property. In the long term, we
3520 * want to do away with it. Explicitly scramble sort order if
3521 * sane_behavior so that no such expectation exists in the new interface.
3522 *
3523 * Scrambling is done by swapping every two consecutive bits, which is
3524 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3525 */
3526static pid_t pid_fry(pid_t pid)
3527{
3528 unsigned a = pid & 0x55555555;
3529 unsigned b = pid & 0xAAAAAAAA;
3530
3531 return (a << 1) | (b >> 1);
3532}
3533
3534static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3535{
3536 if (cgroup_sane_behavior(cgrp))
3537 return pid_fry(pid);
3538 else
3539 return pid;
3540}
3541
Ben Blum102a7752009-09-23 15:56:26 -07003542static int cmppid(const void *a, const void *b)
3543{
3544 return *(pid_t *)a - *(pid_t *)b;
3545}
3546
Tejun Heoafb2bc12013-11-29 10:42:59 -05003547static int fried_cmppid(const void *a, const void *b)
3548{
3549 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3550}
3551
Ben Blum72a8cb32009-09-23 15:56:27 -07003552static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3553 enum cgroup_filetype type)
3554{
3555 struct cgroup_pidlist *l;
3556 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003557 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003558
Tejun Heoe6b81712013-11-29 10:42:59 -05003559 lockdep_assert_held(&cgrp->pidlist_mutex);
3560
3561 list_for_each_entry(l, &cgrp->pidlists, links)
3562 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003563 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003564 return NULL;
3565}
3566
3567/*
3568 * find the appropriate pidlist for our purpose (given procs vs tasks)
3569 * returns with the lock on that pidlist already held, and takes care
3570 * of the use count, or returns NULL with no locks held if we're out of
3571 * memory.
3572 */
3573static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3574 enum cgroup_filetype type)
3575{
3576 struct cgroup_pidlist *l;
3577
3578 lockdep_assert_held(&cgrp->pidlist_mutex);
3579
3580 l = cgroup_pidlist_find(cgrp, type);
3581 if (l)
3582 return l;
3583
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003585 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003586 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003587 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003588
Tejun Heob1a21362013-11-29 10:42:58 -05003589 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003591 /* don't need task_nsproxy() if we're looking at ourself */
3592 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003593 l->owner = cgrp;
3594 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 return l;
3596}
3597
3598/*
Ben Blum102a7752009-09-23 15:56:26 -07003599 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3600 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003601static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3602 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003603{
3604 pid_t *array;
3605 int length;
3606 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003607 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003608 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003609 struct cgroup_pidlist *l;
3610
Tejun Heo4bac00d2013-11-29 10:42:59 -05003611 lockdep_assert_held(&cgrp->pidlist_mutex);
3612
Ben Blum102a7752009-09-23 15:56:26 -07003613 /*
3614 * If cgroup gets more users after we read count, we won't have
3615 * enough space - tough. This race is indistinguishable to the
3616 * caller from the case that the additional cgroup users didn't
3617 * show up until sometime later on.
3618 */
3619 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003620 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003621 if (!array)
3622 return -ENOMEM;
3623 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003624 css_task_iter_start(&cgrp->dummy_css, &it);
3625 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003626 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003627 break;
Ben Blum102a7752009-09-23 15:56:26 -07003628 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 if (type == CGROUP_FILE_PROCS)
3630 pid = task_tgid_vnr(tsk);
3631 else
3632 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003633 if (pid > 0) /* make sure to only use valid results */
3634 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003635 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003636 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003637 length = n;
3638 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003639 if (cgroup_sane_behavior(cgrp))
3640 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3641 else
3642 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003644 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003645
Tejun Heoe6b81712013-11-29 10:42:59 -05003646 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003648 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003649 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003651 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003652
3653 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003654 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003655 l->list = array;
3656 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003658 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003659}
3660
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661/**
Li Zefana043e3b2008-02-23 15:24:09 -08003662 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003663 * @stats: cgroupstats to fill information into
3664 * @dentry: A dentry entry belonging to the cgroup for which stats have
3665 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003666 *
3667 * Build and fill cgroupstats so that taskstats can export it to user
3668 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669 */
3670int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3671{
3672 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003673 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003674 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003675 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003676
Balbir Singh846c7bb2007-10-18 23:39:44 -07003677 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003678 * Validate dentry by checking the superblock operations,
3679 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680 */
Li Zefan33d283b2008-11-19 15:36:48 -08003681 if (dentry->d_sb->s_op != &cgroup_ops ||
3682 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003683 goto err;
3684
3685 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003686 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687
Tejun Heo72ec7022013-08-08 20:11:26 -04003688 css_task_iter_start(&cgrp->dummy_css, &it);
3689 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690 switch (tsk->state) {
3691 case TASK_RUNNING:
3692 stats->nr_running++;
3693 break;
3694 case TASK_INTERRUPTIBLE:
3695 stats->nr_sleeping++;
3696 break;
3697 case TASK_UNINTERRUPTIBLE:
3698 stats->nr_uninterruptible++;
3699 break;
3700 case TASK_STOPPED:
3701 stats->nr_stopped++;
3702 break;
3703 default:
3704 if (delayacct_is_task_waiting_on_io(tsk))
3705 stats->nr_io_wait++;
3706 break;
3707 }
3708 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003709 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710
Balbir Singh846c7bb2007-10-18 23:39:44 -07003711err:
3712 return ret;
3713}
3714
Paul Menage8f3ff202009-09-23 15:56:25 -07003715
Paul Menagecc31edc2008-10-18 20:28:04 -07003716/*
Ben Blum102a7752009-09-23 15:56:26 -07003717 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003718 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003719 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003720 */
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003723{
3724 /*
3725 * Initially we receive a position value that corresponds to
3726 * one more than the last pid shown (or 0 on the first call or
3727 * after a seek to the start). Use a binary-search to find the
3728 * next pid to display, if any
3729 */
Tejun Heo62236852013-11-29 10:42:58 -05003730 struct cgroup_pidlist_open_file *of = s->private;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003731 struct cgroup *cgrp = of->cgrp;
3732 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003733 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003734 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003735
Tejun Heo4bac00d2013-11-29 10:42:59 -05003736 mutex_lock(&cgrp->pidlist_mutex);
3737
3738 /*
3739 * !NULL @of->pidlist indicates that this isn't the first start()
3740 * after open. If the matching pidlist is around, we can use that.
3741 * Look for it. Note that @of->pidlist can't be used directly. It
3742 * could already have been destroyed.
3743 */
3744 if (of->pidlist)
3745 of->pidlist = cgroup_pidlist_find(cgrp, of->type);
3746
3747 /*
3748 * Either this is the first start() after open or the matching
3749 * pidlist has been destroyed inbetween. Create a new one.
3750 */
3751 if (!of->pidlist) {
3752 ret = pidlist_array_load(of->cgrp, of->type, &of->pidlist);
3753 if (ret)
3754 return ERR_PTR(ret);
3755 }
3756 l = of->pidlist;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003757
Paul Menagecc31edc2008-10-18 20:28:04 -07003758 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003759 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003760
Paul Menagecc31edc2008-10-18 20:28:04 -07003761 while (index < end) {
3762 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003763 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003764 index = mid;
3765 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003766 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 index = mid + 1;
3768 else
3769 end = mid;
3770 }
3771 }
3772 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003773 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 return NULL;
3775 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003776 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003777 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003778 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003779}
3780
Ben Blum102a7752009-09-23 15:56:26 -07003781static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003782{
Tejun Heo62236852013-11-29 10:42:58 -05003783 struct cgroup_pidlist_open_file *of = s->private;
3784
Tejun Heo4bac00d2013-11-29 10:42:59 -05003785 if (of->pidlist)
Tejun Heo04502362013-11-29 10:42:59 -05003786 mod_delayed_work(cgroup_pidlist_destroy_wq,
3787 &of->pidlist->destroy_dwork,
3788 CGROUP_PIDLIST_DESTROY_DELAY);
3789 mutex_unlock(&of->cgrp->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003790}
3791
Ben Blum102a7752009-09-23 15:56:26 -07003792static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003793{
Tejun Heo62236852013-11-29 10:42:58 -05003794 struct cgroup_pidlist_open_file *of = s->private;
3795 struct cgroup_pidlist *l = of->pidlist;
Ben Blum102a7752009-09-23 15:56:26 -07003796 pid_t *p = v;
3797 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 /*
3799 * Advance to the next pid in the array. If this goes off the
3800 * end, we're done
3801 */
3802 p++;
3803 if (p >= end) {
3804 return NULL;
3805 } else {
Tejun Heoafb2bc12013-11-29 10:42:59 -05003806 *pos = cgroup_pid_fry(of->cgrp, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003807 return p;
3808 }
3809}
3810
Ben Blum102a7752009-09-23 15:56:26 -07003811static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003812{
3813 return seq_printf(s, "%d\n", *(int *)v);
3814}
3815
Ben Blum102a7752009-09-23 15:56:26 -07003816/*
3817 * seq_operations functions for iterating on pidlists through seq_file -
3818 * independent of whether it's tasks or procs
3819 */
3820static const struct seq_operations cgroup_pidlist_seq_operations = {
3821 .start = cgroup_pidlist_start,
3822 .stop = cgroup_pidlist_stop,
3823 .next = cgroup_pidlist_next,
3824 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003825};
3826
Ben Blum102a7752009-09-23 15:56:26 -07003827static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003828 .read = seq_read,
3829 .llseek = seq_lseek,
3830 .write = cgroup_file_write,
Tejun Heo4bac00d2013-11-29 10:42:59 -05003831 .release = seq_release_private,
Paul Menagecc31edc2008-10-18 20:28:04 -07003832};
3833
3834/*
Ben Blum102a7752009-09-23 15:56:26 -07003835 * The following functions handle opens on a file that displays a pidlist
3836 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3837 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 */
Ben Blum102a7752009-09-23 15:56:26 -07003839/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003840static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003841{
3842 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Tejun Heo62236852013-11-29 10:42:58 -05003843 struct cgroup_pidlist_open_file *of;
Paul Menagecc31edc2008-10-18 20:28:04 -07003844
Ben Blum102a7752009-09-23 15:56:26 -07003845 /* configure file information */
3846 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003847
Tejun Heo62236852013-11-29 10:42:58 -05003848 of = __seq_open_private(file, &cgroup_pidlist_seq_operations,
3849 sizeof(*of));
Tejun Heo4bac00d2013-11-29 10:42:59 -05003850 if (!of)
Tejun Heo62236852013-11-29 10:42:58 -05003851 return -ENOMEM;
Tejun Heo62236852013-11-29 10:42:58 -05003852
3853 of->type = type;
3854 of->cgrp = cgrp;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003855 return 0;
3856}
Ben Blum102a7752009-09-23 15:56:26 -07003857static int cgroup_tasks_open(struct inode *unused, struct file *file)
3858{
Ben Blum72a8cb32009-09-23 15:56:27 -07003859 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003860}
3861static int cgroup_procs_open(struct inode *unused, struct file *file)
3862{
Ben Blum72a8cb32009-09-23 15:56:27 -07003863 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003864}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003865
Tejun Heo182446d2013-08-08 20:11:24 -04003866static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3867 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003868{
Tejun Heo182446d2013-08-08 20:11:24 -04003869 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003870}
3871
Tejun Heo182446d2013-08-08 20:11:24 -04003872static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3873 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003874{
Tejun Heo182446d2013-08-08 20:11:24 -04003875 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003876 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003877 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003878 else
Tejun Heo182446d2013-08-08 20:11:24 -04003879 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003880 return 0;
3881}
3882
Paul Menagebbcb81d2007-10-18 23:39:32 -07003883/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003884 * When dput() is called asynchronously, if umount has been done and
3885 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3886 * there's a small window that vfs will see the root dentry with non-zero
3887 * refcnt and trigger BUG().
3888 *
3889 * That's why we hold a reference before dput() and drop it right after.
3890 */
3891static void cgroup_dput(struct cgroup *cgrp)
3892{
3893 struct super_block *sb = cgrp->root->sb;
3894
3895 atomic_inc(&sb->s_active);
3896 dput(cgrp->dentry);
3897 deactivate_super(sb);
3898}
3899
Tejun Heo182446d2013-08-08 20:11:24 -04003900static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3901 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003902{
Tejun Heo182446d2013-08-08 20:11:24 -04003903 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003904}
3905
Tejun Heo182446d2013-08-08 20:11:24 -04003906static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3907 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003908{
3909 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003910 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003911 else
Tejun Heo182446d2013-08-08 20:11:24 -04003912 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003913 return 0;
3914}
3915
Tejun Heod5c56ce2013-06-03 19:14:34 -07003916static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003917 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003918 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07003919 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003920 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003921 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003922 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003923 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003924 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003925 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003926 .read_u64 = cgroup_clone_children_read,
3927 .write_u64 = cgroup_clone_children_write,
3928 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003929 {
Tejun Heo873fe092013-04-14 20:15:26 -07003930 .name = "cgroup.sane_behavior",
3931 .flags = CFTYPE_ONLY_ON_ROOT,
3932 .read_seq_string = cgroup_sane_behavior_show,
3933 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003934
3935 /*
3936 * Historical crazy stuff. These don't have "cgroup." prefix and
3937 * don't exist if sane_behavior. If you're depending on these, be
3938 * prepared to be burned.
3939 */
3940 {
3941 .name = "tasks",
3942 .flags = CFTYPE_INSANE, /* use "procs" instead */
3943 .open = cgroup_tasks_open,
3944 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003945 .mode = S_IRUGO | S_IWUSR,
3946 },
3947 {
3948 .name = "notify_on_release",
3949 .flags = CFTYPE_INSANE,
3950 .read_u64 = cgroup_read_notify_on_release,
3951 .write_u64 = cgroup_write_notify_on_release,
3952 },
Tejun Heo873fe092013-04-14 20:15:26 -07003953 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003954 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003955 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003956 .read_seq_string = cgroup_release_agent_show,
3957 .write_string = cgroup_release_agent_write,
3958 .max_write_len = PATH_MAX,
3959 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003960 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003961};
3962
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003963/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003964 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003965 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003966 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003967 *
3968 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003969 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003970static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003971{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003972 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003973 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003974
Tejun Heo8e3f6542012-04-01 12:09:55 -07003975 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003976 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003977 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003978
3979 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003980 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003981
Tejun Heobee55092013-06-28 16:24:11 -07003982 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003983 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003984 if (ret < 0)
3985 goto err;
3986 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003987 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003988 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003989err:
3990 cgroup_clear_dir(cgrp, subsys_mask);
3991 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003992}
3993
Tejun Heo0c21ead2013-08-13 20:22:51 -04003994/*
3995 * css destruction is four-stage process.
3996 *
3997 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3998 * Implemented in kill_css().
3999 *
4000 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4001 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4002 * by invoking offline_css(). After offlining, the base ref is put.
4003 * Implemented in css_killed_work_fn().
4004 *
4005 * 3. When the percpu_ref reaches zero, the only possible remaining
4006 * accessors are inside RCU read sections. css_release() schedules the
4007 * RCU callback.
4008 *
4009 * 4. After the grace period, the css can be freed. Implemented in
4010 * css_free_work_fn().
4011 *
4012 * It is actually hairier because both step 2 and 4 require process context
4013 * and thus involve punting to css->destroy_work adding two additional
4014 * steps to the already complex sequence.
4015 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004016static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004017{
4018 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004019 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004020 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004021
Tejun Heo0ae78e02013-08-13 11:01:54 -04004022 if (css->parent)
4023 css_put(css->parent);
4024
Tejun Heo0c21ead2013-08-13 20:22:51 -04004025 css->ss->css_free(css);
4026 cgroup_dput(cgrp);
4027}
4028
4029static void css_free_rcu_fn(struct rcu_head *rcu_head)
4030{
4031 struct cgroup_subsys_state *css =
4032 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4033
4034 /*
4035 * css holds an extra ref to @cgrp->dentry which is put on the last
4036 * css_put(). dput() requires process context which we don't have.
4037 */
4038 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004039 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004040}
4041
Tejun Heod3daf282013-06-13 19:39:16 -07004042static void css_release(struct percpu_ref *ref)
4043{
4044 struct cgroup_subsys_state *css =
4045 container_of(ref, struct cgroup_subsys_state, refcnt);
4046
Tejun Heo0c21ead2013-08-13 20:22:51 -04004047 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004048}
4049
Tejun Heo623f9262013-08-13 11:01:55 -04004050static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4051 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004052{
Paul Menagebd89aab2007-10-18 23:40:44 -07004053 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004054 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004055 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004056
Tejun Heo0ae78e02013-08-13 11:01:54 -04004057 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004058 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004059 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004060 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004061
Tejun Heoca8bdca2013-08-26 18:40:56 -04004062 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004063}
4064
Li Zefan2a4ac632013-07-31 16:16:40 +08004065/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004066static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004067{
Tejun Heo623f9262013-08-13 11:01:55 -04004068 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004069 int ret = 0;
4070
Tejun Heoa31f2d32012-11-19 08:13:37 -08004071 lockdep_assert_held(&cgroup_mutex);
4072
Tejun Heo92fb9742012-11-19 08:13:38 -08004073 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004074 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004075 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004076 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004077 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004078 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4079 }
Tejun Heob1929db2012-11-19 08:13:38 -08004080 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004081}
4082
Li Zefan2a4ac632013-07-31 16:16:40 +08004083/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004084static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004085{
Tejun Heo623f9262013-08-13 11:01:55 -04004086 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004087
4088 lockdep_assert_held(&cgroup_mutex);
4089
4090 if (!(css->flags & CSS_ONLINE))
4091 return;
4092
Li Zefand7eeac12013-03-12 15:35:59 -07004093 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004094 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004095
Tejun Heoeb954192013-08-08 20:11:23 -04004096 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004097 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004098 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004099}
4100
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101/*
Li Zefana043e3b2008-02-23 15:24:09 -08004102 * cgroup_create - create a cgroup
4103 * @parent: cgroup that will be parent of the new cgroup
4104 * @dentry: dentry of the new cgroup
4105 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004106 *
Li Zefana043e3b2008-02-23 15:24:09 -08004107 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004108 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004109static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004110 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004111{
Tejun Heoae7f1642013-08-13 20:22:50 -04004112 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004113 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004114 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004115 struct cgroupfs_root *root = parent->root;
4116 int err = 0;
4117 struct cgroup_subsys *ss;
4118 struct super_block *sb = root->sb;
4119
Tejun Heo0a950f62012-11-19 09:02:12 -08004120 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004121 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4122 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123 return -ENOMEM;
4124
Li Zefan65dff752013-03-01 15:01:56 +08004125 name = cgroup_alloc_name(dentry);
4126 if (!name)
4127 goto err_free_cgrp;
4128 rcu_assign_pointer(cgrp->name, name);
4129
Li Zefan4e96ee82013-07-31 09:50:50 +08004130 /*
4131 * Temporarily set the pointer to NULL, so idr_find() won't return
4132 * a half-baked cgroup.
4133 */
4134 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004135 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004136 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004137
Tejun Heo976c06b2012-11-05 09:16:59 -08004138 /*
4139 * Only live parents can have children. Note that the liveliness
4140 * check isn't strictly necessary because cgroup_mkdir() and
4141 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4142 * anyway so that locking is contained inside cgroup proper and we
4143 * don't get nasty surprises if we ever grow another caller.
4144 */
4145 if (!cgroup_lock_live_group(parent)) {
4146 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004147 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004148 }
4149
Paul Menageddbcc7e2007-10-18 23:39:30 -07004150 /* Grab a reference on the superblock so the hierarchy doesn't
4151 * get deleted on unmount if there are child cgroups. This
4152 * can be done outside cgroup_mutex, since the sb can't
4153 * disappear while someone has an open control file on the
4154 * fs */
4155 atomic_inc(&sb->s_active);
4156
Paul Menagecc31edc2008-10-18 20:28:04 -07004157 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158
Li Zefanfe1c06c2013-01-24 14:30:22 +08004159 dentry->d_fsdata = cgrp;
4160 cgrp->dentry = dentry;
4161
Paul Menagebd89aab2007-10-18 23:40:44 -07004162 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004163 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004164 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165
Li Zefanb6abdb02008-03-04 14:28:19 -08004166 if (notify_on_release(parent))
4167 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4168
Tejun Heo2260e7f2012-11-19 08:13:38 -08004169 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4170 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004171
Tejun Heo5549c492013-06-24 15:21:48 -07004172 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004173 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004174
Tejun Heoca8bdca2013-08-26 18:40:56 -04004175 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176 if (IS_ERR(css)) {
4177 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004178 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004179 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004180 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004181
4182 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004183 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004184 goto err_free_all;
4185
Tejun Heo623f9262013-08-13 11:01:55 -04004186 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187 }
4188
Tejun Heo4e139af2012-11-19 08:13:36 -08004189 /*
4190 * Create directory. cgroup_create_file() returns with the new
4191 * directory locked on success so that it can be populated without
4192 * dropping cgroup_mutex.
4193 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004194 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004196 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004197 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004198
Tejun Heo00356bd2013-06-18 11:14:22 -07004199 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004200
Tejun Heo4e139af2012-11-19 08:13:36 -08004201 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004202 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4203 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004204
Tejun Heo0ae78e02013-08-13 11:01:54 -04004205 /* each css holds a ref to the cgroup's dentry and the parent css */
4206 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004207 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004208
Tejun Heoed9577932012-11-05 09:16:58 -08004209 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004210 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004211 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004212
Li Zefan415cf072013-04-08 14:35:02 +08004213 /* hold a ref to the parent's dentry */
4214 dget(parent->dentry);
4215
Tejun Heob1929db2012-11-19 08:13:38 -08004216 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004217 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004218 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004219
4220 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004221 if (err)
4222 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004223
4224 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4225 parent->parent) {
4226 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",
4227 current->comm, current->pid, ss->name);
4228 if (!strcmp(ss->name, "memory"))
4229 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4230 ss->warned_broken_hierarchy = true;
4231 }
Tejun Heoa8638032012-11-09 09:12:29 -08004232 }
4233
Li Zefan4e96ee82013-07-31 09:50:50 +08004234 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4235
Tejun Heo2bb566c2013-08-08 20:11:23 -04004236 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004237 if (err)
4238 goto err_destroy;
4239
4240 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004241 if (err)
4242 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243
4244 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004245 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246
4247 return 0;
4248
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004249err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004250 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004251 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004252
4253 if (css) {
4254 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004255 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004256 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004258 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259 /* Release the reference count that we took on the superblock */
4260 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004261err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004262 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004263err_free_name:
4264 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004265err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004266 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004268
4269err_destroy:
4270 cgroup_destroy_locked(cgrp);
4271 mutex_unlock(&cgroup_mutex);
4272 mutex_unlock(&dentry->d_inode->i_mutex);
4273 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274}
4275
Al Viro18bb1db2011-07-26 01:41:39 -04004276static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277{
4278 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4279
4280 /* the vfs holds inode->i_mutex already */
4281 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4282}
4283
Tejun Heo223dbc32013-08-13 20:22:50 -04004284/*
4285 * This is called when the refcnt of a css is confirmed to be killed.
4286 * css_tryget() is now guaranteed to fail.
4287 */
4288static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004289{
Tejun Heo223dbc32013-08-13 20:22:50 -04004290 struct cgroup_subsys_state *css =
4291 container_of(work, struct cgroup_subsys_state, destroy_work);
4292 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004293
Tejun Heof20104d2013-08-13 20:22:50 -04004294 mutex_lock(&cgroup_mutex);
4295
4296 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004297 * css_tryget() is guaranteed to fail now. Tell subsystems to
4298 * initate destruction.
4299 */
4300 offline_css(css);
4301
4302 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004303 * If @cgrp is marked dead, it's waiting for refs of all css's to
4304 * be disabled before proceeding to the second phase of cgroup
4305 * destruction. If we are the last one, kick it off.
4306 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004307 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004308 cgroup_destroy_css_killed(cgrp);
4309
4310 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004311
4312 /*
4313 * Put the css refs from kill_css(). Each css holds an extra
4314 * reference to the cgroup's dentry and cgroup removal proceeds
4315 * regardless of css refs. On the last put of each css, whenever
4316 * that may be, the extra dentry ref is put so that dentry
4317 * destruction happens only after all css's are released.
4318 */
4319 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004320}
4321
Tejun Heo223dbc32013-08-13 20:22:50 -04004322/* css kill confirmation processing requires process context, bounce */
4323static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004324{
4325 struct cgroup_subsys_state *css =
4326 container_of(ref, struct cgroup_subsys_state, refcnt);
4327
Tejun Heo223dbc32013-08-13 20:22:50 -04004328 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004329 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004330}
4331
4332/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004333 * kill_css - destroy a css
4334 * @css: css to destroy
4335 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004336 * This function initiates destruction of @css by removing cgroup interface
4337 * files and putting its base reference. ->css_offline() will be invoked
4338 * asynchronously once css_tryget() is guaranteed to fail and when the
4339 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004340 */
4341static void kill_css(struct cgroup_subsys_state *css)
4342{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004343 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4344
Tejun Heoedae0c32013-08-13 20:22:51 -04004345 /*
4346 * Killing would put the base ref, but we need to keep it alive
4347 * until after ->css_offline().
4348 */
4349 css_get(css);
4350
4351 /*
4352 * cgroup core guarantees that, by the time ->css_offline() is
4353 * invoked, no new css reference will be given out via
4354 * css_tryget(). We can't simply call percpu_ref_kill() and
4355 * proceed to offlining css's because percpu_ref_kill() doesn't
4356 * guarantee that the ref is seen as killed on all CPUs on return.
4357 *
4358 * Use percpu_ref_kill_and_confirm() to get notifications as each
4359 * css is confirmed to be seen as killed on all CPUs.
4360 */
4361 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004362}
4363
4364/**
4365 * cgroup_destroy_locked - the first stage of cgroup destruction
4366 * @cgrp: cgroup to be destroyed
4367 *
4368 * css's make use of percpu refcnts whose killing latency shouldn't be
4369 * exposed to userland and are RCU protected. Also, cgroup core needs to
4370 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4371 * invoked. To satisfy all the requirements, destruction is implemented in
4372 * the following two steps.
4373 *
4374 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4375 * userland visible parts and start killing the percpu refcnts of
4376 * css's. Set up so that the next stage will be kicked off once all
4377 * the percpu refcnts are confirmed to be killed.
4378 *
4379 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4380 * rest of destruction. Once all cgroup references are gone, the
4381 * cgroup is RCU-freed.
4382 *
4383 * This function implements s1. After this step, @cgrp is gone as far as
4384 * the userland is concerned and a new cgroup with the same name may be
4385 * created. As cgroup doesn't care about the names internally, this
4386 * doesn't cause any problem.
4387 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004388static int cgroup_destroy_locked(struct cgroup *cgrp)
4389 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390{
Tejun Heo42809dd2012-11-19 08:13:37 -08004391 struct dentry *d = cgrp->dentry;
Tejun Heoed9577932012-11-05 09:16:58 -08004392 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004393 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004394 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395
Tejun Heo42809dd2012-11-19 08:13:37 -08004396 lockdep_assert_held(&d->d_inode->i_mutex);
4397 lockdep_assert_held(&cgroup_mutex);
4398
Tejun Heoddd69142013-06-12 21:04:54 -07004399 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004400 * css_set_lock synchronizes access to ->cset_links and prevents
4401 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004402 */
4403 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004404 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004405 read_unlock(&css_set_lock);
4406 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004408
Tejun Heo1a90dd52012-11-05 09:16:59 -08004409 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004410 * Make sure there's no live children. We can't test ->children
4411 * emptiness as dead children linger on it while being destroyed;
4412 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4413 */
4414 empty = true;
4415 rcu_read_lock();
4416 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4417 empty = cgroup_is_dead(child);
4418 if (!empty)
4419 break;
4420 }
4421 rcu_read_unlock();
4422 if (!empty)
4423 return -EBUSY;
4424
4425 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004426 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4427 * will be invoked to perform the rest of destruction once the
4428 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004429 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004430 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004431 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004432
4433 /*
4434 * Mark @cgrp dead. This prevents further task migration and child
4435 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004436 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004437 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004438 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004439 */
Tejun Heo54766d42013-06-12 21:04:53 -07004440 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004441
Tejun Heo455050d2013-06-13 19:27:41 -07004442 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4443 raw_spin_lock(&release_list_lock);
4444 if (!list_empty(&cgrp->release_list))
4445 list_del_init(&cgrp->release_list);
4446 raw_spin_unlock(&release_list_lock);
4447
4448 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004449 * If @cgrp has css's attached, the second stage of cgroup
4450 * destruction is kicked off from css_killed_work_fn() after the
4451 * refs of all attached css's are killed. If @cgrp doesn't have
4452 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004453 */
Tejun Heof20104d2013-08-13 20:22:50 -04004454 if (!cgrp->nr_css)
4455 cgroup_destroy_css_killed(cgrp);
4456
4457 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004458 * Clear the base files and remove @cgrp directory. The removal
4459 * puts the base ref but we aren't quite done with @cgrp yet, so
4460 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004461 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004462 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004463 dget(d);
4464 cgroup_d_remove_dir(d);
4465
Tejun Heoea15f8c2013-06-13 19:27:42 -07004466 return 0;
4467};
4468
Tejun Heod3daf282013-06-13 19:39:16 -07004469/**
Tejun Heof20104d2013-08-13 20:22:50 -04004470 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004471 * @work: cgroup->destroy_free_work
4472 *
4473 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004474 * destroyed after all css's are offlined and performs the rest of
4475 * destruction. This is the second step of destruction described in the
4476 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004477 */
Tejun Heof20104d2013-08-13 20:22:50 -04004478static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004479{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004480 struct cgroup *parent = cgrp->parent;
4481 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004482
Tejun Heof20104d2013-08-13 20:22:50 -04004483 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004484
Paul Menage999cd8a2009-01-07 18:08:36 -08004485 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004486 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004487
Li Zefan4e96ee82013-07-31 09:50:50 +08004488 /*
4489 * We should remove the cgroup object from idr before its grace
4490 * period starts, so we won't be looking up a cgroup while the
4491 * cgroup is being freed.
4492 */
4493 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4494 cgrp->id = -1;
4495
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 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4545 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004546 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004547 /* We don't handle early failures gracefully */
4548 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004549 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004550
Li Zefane8d55fd2008-04-29 01:00:13 -07004551 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004552 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004553 * newly registered, all tasks and hence the
4554 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004555 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556
4557 need_forkexit_callback |= ss->fork || ss->exit;
4558
Li Zefane8d55fd2008-04-29 01:00:13 -07004559 /* At system boot, before all subsystems have been
4560 * registered, no tasks have been forked, so we don't
4561 * need to invoke fork callbacks here. */
4562 BUG_ON(!list_empty(&init_task.tasks));
4563
Tejun Heoae7f1642013-08-13 20:22:50 -04004564 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004565
Tejun Heo648bb562012-11-19 08:13:36 -08004566 mutex_unlock(&cgroup_mutex);
4567
Ben Blume6a11052010-03-10 15:22:09 -08004568 /* this function shouldn't be used with modular subsystems, since they
4569 * need to register a subsys_id, among other things */
4570 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004571}
4572
4573/**
Ben Blume6a11052010-03-10 15:22:09 -08004574 * cgroup_load_subsys: load and register a modular subsystem at runtime
4575 * @ss: the subsystem to load
4576 *
4577 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004578 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004579 * up for use. If the subsystem is built-in anyway, work is delegated to the
4580 * simpler cgroup_init_subsys.
4581 */
4582int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4583{
Ben Blume6a11052010-03-10 15:22:09 -08004584 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004585 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004586 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004587 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004588 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004589
4590 /* check name and function validity */
4591 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004592 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004593 return -EINVAL;
4594
4595 /*
4596 * we don't support callbacks in modular subsystems. this check is
4597 * before the ss->module check for consistency; a subsystem that could
4598 * be a module should still have no callbacks even if the user isn't
4599 * compiling it as one.
4600 */
4601 if (ss->fork || ss->exit)
4602 return -EINVAL;
4603
4604 /*
4605 * an optionally modular subsystem is built-in: we want to do nothing,
4606 * since cgroup_init_subsys will have already taken care of it.
4607 */
4608 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004609 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004610 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004611 return 0;
4612 }
4613
Tejun Heo8e3f6542012-04-01 12:09:55 -07004614 /* init base cftset */
4615 cgroup_init_cftsets(ss);
4616
Ben Blume6a11052010-03-10 15:22:09 -08004617 mutex_lock(&cgroup_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;
Ben Blume6a11052010-03-10 15:22:09 -08004629 mutex_unlock(&cgroup_mutex);
4630 return PTR_ERR(css);
4631 }
4632
Tejun Heo9871bf92013-06-24 15:21:47 -07004633 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4634 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);
Tejun Heob1929db2012-11-19 08:13:38 -08004663 if (ret)
4664 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004665
Ben Blume6a11052010-03-10 15:22:09 -08004666 /* success! */
4667 mutex_unlock(&cgroup_mutex);
4668 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004669
4670err_unload:
4671 mutex_unlock(&cgroup_mutex);
4672 /* @ss can't be mounted here as try_module_get() would fail */
4673 cgroup_unload_subsys(ss);
4674 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004675}
4676EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4677
4678/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004679 * cgroup_unload_subsys: unload a modular subsystem
4680 * @ss: the subsystem to unload
4681 *
4682 * This function should be called in a modular subsystem's exitcall. When this
4683 * function is invoked, the refcount on the subsystem's module will be 0, so
4684 * the subsystem will not be attached to any hierarchy.
4685 */
4686void cgroup_unload_subsys(struct cgroup_subsys *ss)
4687{
Tejun Heo69d02062013-06-12 21:04:50 -07004688 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004689
4690 BUG_ON(ss->module == NULL);
4691
4692 /*
4693 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004694 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004695 * doesn't start being used while we're killing it off.
4696 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004697 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004698
4699 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004700
Tejun Heoca8bdca2013-08-26 18:40:56 -04004701 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004702
Ben Blumcf5d5942010-03-10 15:22:09 -08004703 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004704 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004705
Tejun Heo9871bf92013-06-24 15:21:47 -07004706 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004707 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004708
4709 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004710 * disentangle the css from all css_sets attached to the dummy
4711 * top. as in loading, we need to pay our respects to the hashtable
4712 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004713 */
4714 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004715 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004716 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004717 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004718
Tejun Heo5abb8852013-06-12 21:04:49 -07004719 hash_del(&cset->hlist);
4720 cset->subsys[ss->subsys_id] = NULL;
4721 key = css_set_hash(cset->subsys);
4722 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004723 }
4724 write_unlock(&css_set_lock);
4725
4726 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004727 * remove subsystem's css from the cgroup_dummy_top and free it -
4728 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004729 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004730 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004731 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004732 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004733
4734 mutex_unlock(&cgroup_mutex);
4735}
4736EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4737
4738/**
Li Zefana043e3b2008-02-23 15:24:09 -08004739 * cgroup_init_early - cgroup initialization at system boot
4740 *
4741 * Initialize cgroups at system boot, and initialize any
4742 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743 */
4744int __init cgroup_init_early(void)
4745{
Tejun Heo30159ec2013-06-25 11:53:37 -07004746 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004747 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004748
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004749 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004750 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004751 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004752 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004753 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004754 init_cgroup_root(&cgroup_dummy_root);
4755 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004756 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004757
Tejun Heo69d02062013-06-12 21:04:50 -07004758 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004759 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4760 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004761 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004762
Tejun Heo30159ec2013-06-25 11:53:37 -07004763 /* at bootup time, we don't worry about modular subsystems */
4764 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765 BUG_ON(!ss->name);
4766 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004767 BUG_ON(!ss->css_alloc);
4768 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004770 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771 ss->name, ss->subsys_id);
4772 BUG();
4773 }
4774
4775 if (ss->early_init)
4776 cgroup_init_subsys(ss);
4777 }
4778 return 0;
4779}
4780
4781/**
Li Zefana043e3b2008-02-23 15:24:09 -08004782 * cgroup_init - cgroup initialization
4783 *
4784 * Register cgroup filesystem and /proc file, and initialize
4785 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004786 */
4787int __init cgroup_init(void)
4788{
Tejun Heo30159ec2013-06-25 11:53:37 -07004789 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004790 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004791 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004792
4793 err = bdi_init(&cgroup_backing_dev_info);
4794 if (err)
4795 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004796
Tejun Heo30159ec2013-06-25 11:53:37 -07004797 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004798 if (!ss->early_init)
4799 cgroup_init_subsys(ss);
4800 }
4801
Tejun Heofa3ca072013-04-14 11:36:56 -07004802 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004803 mutex_lock(&cgroup_mutex);
4804 mutex_lock(&cgroup_root_mutex);
4805
Tejun Heo82fe9b02013-06-25 11:53:37 -07004806 /* Add init_css_set to the hash table */
4807 key = css_set_hash(init_css_set.subsys);
4808 hash_add(css_set_table, &init_css_set.hlist, key);
4809
Tejun Heofc76df72013-06-25 11:53:37 -07004810 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004811
Li Zefan4e96ee82013-07-31 09:50:50 +08004812 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4813 0, 1, GFP_KERNEL);
4814 BUG_ON(err < 0);
4815
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004816 mutex_unlock(&cgroup_root_mutex);
4817 mutex_unlock(&cgroup_mutex);
4818
Greg KH676db4a2010-08-05 13:53:35 -07004819 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4820 if (!cgroup_kobj) {
4821 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004822 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004823 }
4824
4825 err = register_filesystem(&cgroup_fs_type);
4826 if (err < 0) {
4827 kobject_put(cgroup_kobj);
4828 goto out;
4829 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830
Li Zefan46ae2202008-04-29 01:00:08 -07004831 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004832
Paul Menageddbcc7e2007-10-18 23:39:30 -07004833out:
Paul Menagea4243162007-10-18 23:39:35 -07004834 if (err)
4835 bdi_destroy(&cgroup_backing_dev_info);
4836
Paul Menageddbcc7e2007-10-18 23:39:30 -07004837 return err;
4838}
Paul Menageb4f48b62007-10-18 23:39:33 -07004839
Tejun Heoe5fca242013-11-22 17:14:39 -05004840static int __init cgroup_wq_init(void)
4841{
4842 /*
4843 * There isn't much point in executing destruction path in
4844 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4845 * Use 1 for @max_active.
4846 *
4847 * We would prefer to do this in cgroup_init() above, but that
4848 * is called before init_workqueues(): so leave this until after.
4849 */
4850 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4851 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004852
4853 /*
4854 * Used to destroy pidlists and separate to serve as flush domain.
4855 * Cap @max_active to 1 too.
4856 */
4857 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4858 0, 1);
4859 BUG_ON(!cgroup_pidlist_destroy_wq);
4860
Tejun Heoe5fca242013-11-22 17:14:39 -05004861 return 0;
4862}
4863core_initcall(cgroup_wq_init);
4864
Paul Menagea4243162007-10-18 23:39:35 -07004865/*
4866 * proc_cgroup_show()
4867 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4868 * - Used for /proc/<pid>/cgroup.
4869 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4870 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004871 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004872 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4873 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4874 * cgroup to top_cgroup.
4875 */
4876
4877/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004878int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004879{
4880 struct pid *pid;
4881 struct task_struct *tsk;
4882 char *buf;
4883 int retval;
4884 struct cgroupfs_root *root;
4885
4886 retval = -ENOMEM;
4887 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4888 if (!buf)
4889 goto out;
4890
4891 retval = -ESRCH;
4892 pid = m->private;
4893 tsk = get_pid_task(pid, PIDTYPE_PID);
4894 if (!tsk)
4895 goto out_free;
4896
4897 retval = 0;
4898
4899 mutex_lock(&cgroup_mutex);
4900
Li Zefane5f6a862009-01-07 18:07:41 -08004901 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004902 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004903 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004904 int count = 0;
4905
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004906 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004907 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004908 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004909 if (strlen(root->name))
4910 seq_printf(m, "%sname=%s", count ? "," : "",
4911 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004912 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004913 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004914 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004915 if (retval < 0)
4916 goto out_unlock;
4917 seq_puts(m, buf);
4918 seq_putc(m, '\n');
4919 }
4920
4921out_unlock:
4922 mutex_unlock(&cgroup_mutex);
4923 put_task_struct(tsk);
4924out_free:
4925 kfree(buf);
4926out:
4927 return retval;
4928}
4929
Paul Menagea4243162007-10-18 23:39:35 -07004930/* Display information about each subsystem and each hierarchy */
4931static int proc_cgroupstats_show(struct seq_file *m, void *v)
4932{
Tejun Heo30159ec2013-06-25 11:53:37 -07004933 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004934 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004935
Paul Menage8bab8dd2008-04-04 14:29:57 -07004936 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004937 /*
4938 * ideally we don't want subsystems moving around while we do this.
4939 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4940 * subsys/hierarchy state.
4941 */
Paul Menagea4243162007-10-18 23:39:35 -07004942 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004943
4944 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004945 seq_printf(m, "%s\t%d\t%d\t%d\n",
4946 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004947 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004948
Paul Menagea4243162007-10-18 23:39:35 -07004949 mutex_unlock(&cgroup_mutex);
4950 return 0;
4951}
4952
4953static int cgroupstats_open(struct inode *inode, struct file *file)
4954{
Al Viro9dce07f2008-03-29 03:07:28 +00004955 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004956}
4957
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004958static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004959 .open = cgroupstats_open,
4960 .read = seq_read,
4961 .llseek = seq_lseek,
4962 .release = single_release,
4963};
4964
Paul Menageb4f48b62007-10-18 23:39:33 -07004965/**
4966 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004967 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004968 *
4969 * Description: A task inherits its parent's cgroup at fork().
4970 *
4971 * A pointer to the shared css_set was automatically copied in
4972 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004973 * it was not made under the protection of RCU or cgroup_mutex, so
4974 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4975 * have already changed current->cgroups, allowing the previously
4976 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004977 *
4978 * At the point that cgroup_fork() is called, 'current' is the parent
4979 * task, and the passed argument 'child' points to the child task.
4980 */
4981void cgroup_fork(struct task_struct *child)
4982{
Tejun Heo9bb71302012-10-18 17:52:07 -07004983 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004984 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004985 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004986 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004987 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004988}
4989
4990/**
Li Zefana043e3b2008-02-23 15:24:09 -08004991 * cgroup_post_fork - called on a new task after adding it to the task list
4992 * @child: the task in question
4993 *
Tejun Heo5edee612012-10-16 15:03:14 -07004994 * Adds the task to the list running through its css_set if necessary and
4995 * call the subsystem fork() callbacks. Has to be after the task is
4996 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004997 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004998 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004999 */
Paul Menage817929e2007-10-18 23:39:36 -07005000void cgroup_post_fork(struct task_struct *child)
5001{
Tejun Heo30159ec2013-06-25 11:53:37 -07005002 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005003 int i;
5004
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005005 /*
5006 * use_task_css_set_links is set to 1 before we walk the tasklist
5007 * under the tasklist_lock and we read it here after we added the child
5008 * to the tasklist under the tasklist_lock as well. If the child wasn't
5009 * yet in the tasklist when we walked through it from
5010 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5011 * should be visible now due to the paired locking and barriers implied
5012 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5013 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5014 * lock on fork.
5015 */
Paul Menage817929e2007-10-18 23:39:36 -07005016 if (use_task_css_set_links) {
5017 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005018 task_lock(child);
5019 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005020 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005021 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005022 write_unlock(&css_set_lock);
5023 }
Tejun Heo5edee612012-10-16 15:03:14 -07005024
5025 /*
5026 * Call ss->fork(). This must happen after @child is linked on
5027 * css_set; otherwise, @child might change state between ->fork()
5028 * and addition to css_set.
5029 */
5030 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005031 /*
5032 * fork/exit callbacks are supported only for builtin
5033 * subsystems, and the builtin section of the subsys
5034 * array is immutable, so we don't need to lock the
5035 * subsys array here. On the other hand, modular section
5036 * of the array can be freed at module unload, so we
5037 * can't touch that.
5038 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005039 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005040 if (ss->fork)
5041 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005042 }
Paul Menage817929e2007-10-18 23:39:36 -07005043}
Tejun Heo5edee612012-10-16 15:03:14 -07005044
Paul Menage817929e2007-10-18 23:39:36 -07005045/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005046 * cgroup_exit - detach cgroup from exiting task
5047 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005048 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005049 *
5050 * Description: Detach cgroup from @tsk and release it.
5051 *
5052 * Note that cgroups marked notify_on_release force every task in
5053 * them to take the global cgroup_mutex mutex when exiting.
5054 * This could impact scaling on very large systems. Be reluctant to
5055 * use notify_on_release cgroups where very high task exit scaling
5056 * is required on large systems.
5057 *
5058 * the_top_cgroup_hack:
5059 *
5060 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5061 *
5062 * We call cgroup_exit() while the task is still competent to
5063 * handle notify_on_release(), then leave the task attached to the
5064 * root cgroup in each hierarchy for the remainder of its exit.
5065 *
5066 * To do this properly, we would increment the reference count on
5067 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5068 * code we would add a second cgroup function call, to drop that
5069 * reference. This would just create an unnecessary hot spot on
5070 * the top_cgroup reference count, to no avail.
5071 *
5072 * Normally, holding a reference to a cgroup without bumping its
5073 * count is unsafe. The cgroup could go away, or someone could
5074 * attach us to a different cgroup, decrementing the count on
5075 * the first cgroup that we never incremented. But in this case,
5076 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005077 * which wards off any cgroup_attach_task() attempts, or task is a failed
5078 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005079 */
5080void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5081{
Tejun Heo30159ec2013-06-25 11:53:37 -07005082 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005083 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005084 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005085
5086 /*
5087 * Unlink from the css_set task list if necessary.
5088 * Optimistically check cg_list before taking
5089 * css_set_lock
5090 */
5091 if (!list_empty(&tsk->cg_list)) {
5092 write_lock(&css_set_lock);
5093 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005094 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005095 write_unlock(&css_set_lock);
5096 }
5097
Paul Menageb4f48b62007-10-18 23:39:33 -07005098 /* Reassign the task to the init_css_set. */
5099 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005100 cset = task_css_set(tsk);
5101 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005102
5103 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005104 /*
5105 * fork/exit callbacks are supported only for builtin
5106 * subsystems, see cgroup_post_fork() for details.
5107 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005108 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005109 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005110 struct cgroup_subsys_state *old_css = cset->subsys[i];
5111 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005112
Tejun Heoeb954192013-08-08 20:11:23 -04005113 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005114 }
5115 }
5116 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005117 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005118
Tejun Heo5abb8852013-06-12 21:04:49 -07005119 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005120}
Paul Menage697f4162007-10-18 23:39:34 -07005121
Paul Menagebd89aab2007-10-18 23:40:44 -07005122static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005123{
Li Zefanf50daa72013-03-01 15:06:07 +08005124 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005125 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005126 /*
5127 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005128 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005129 * it now
5130 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005131 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005132
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005133 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005134 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005135 list_empty(&cgrp->release_list)) {
5136 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005137 need_schedule_work = 1;
5138 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005139 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005140 if (need_schedule_work)
5141 schedule_work(&release_agent_work);
5142 }
5143}
5144
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145/*
5146 * Notify userspace when a cgroup is released, by running the
5147 * configured release agent with the name of the cgroup (path
5148 * relative to the root of cgroup file system) as the argument.
5149 *
5150 * Most likely, this user command will try to rmdir this cgroup.
5151 *
5152 * This races with the possibility that some other task will be
5153 * attached to this cgroup before it is removed, or that some other
5154 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5155 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5156 * unused, and this cgroup will be reprieved from its death sentence,
5157 * to continue to serve a useful existence. Next time it's released,
5158 * we will get notified again, if it still has 'notify_on_release' set.
5159 *
5160 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5161 * means only wait until the task is successfully execve()'d. The
5162 * separate release agent task is forked by call_usermodehelper(),
5163 * then control in this thread returns here, without waiting for the
5164 * release agent task. We don't bother to wait because the caller of
5165 * this routine has no use for the exit status of the release agent
5166 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005167 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005168static void cgroup_release_agent(struct work_struct *work)
5169{
5170 BUG_ON(work != &release_agent_work);
5171 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005172 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005173 while (!list_empty(&release_list)) {
5174 char *argv[3], *envp[3];
5175 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005176 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005177 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005178 struct cgroup,
5179 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005180 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005181 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005182 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005183 if (!pathbuf)
5184 goto continue_free;
5185 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5186 goto continue_free;
5187 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5188 if (!agentbuf)
5189 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005190
5191 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005192 argv[i++] = agentbuf;
5193 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005194 argv[i] = NULL;
5195
5196 i = 0;
5197 /* minimal command environment */
5198 envp[i++] = "HOME=/";
5199 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5200 envp[i] = NULL;
5201
5202 /* Drop the lock while we invoke the usermode helper,
5203 * since the exec could involve hitting disk and hence
5204 * be a slow process */
5205 mutex_unlock(&cgroup_mutex);
5206 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005207 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005208 continue_free:
5209 kfree(pathbuf);
5210 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005211 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005212 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005213 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005214 mutex_unlock(&cgroup_mutex);
5215}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005216
5217static int __init cgroup_disable(char *str)
5218{
Tejun Heo30159ec2013-06-25 11:53:37 -07005219 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005220 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005221 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005222
5223 while ((token = strsep(&str, ",")) != NULL) {
5224 if (!*token)
5225 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005226
Tejun Heo30159ec2013-06-25 11:53:37 -07005227 /*
5228 * cgroup_disable, being at boot time, can't know about
5229 * module subsystems, so we don't worry about them.
5230 */
5231 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005232 if (!strcmp(token, ss->name)) {
5233 ss->disabled = 1;
5234 printk(KERN_INFO "Disabling %s control group"
5235 " subsystem\n", ss->name);
5236 break;
5237 }
5238 }
5239 }
5240 return 1;
5241}
5242__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005243
Tejun Heob77d7b62013-08-13 11:01:54 -04005244/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005245 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5246 * @dentry: directory dentry of interest
5247 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005248 *
5249 * Must be called under RCU read lock. The caller is responsible for
5250 * pinning the returned css if it needs to be accessed outside the RCU
5251 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005252 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005253struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5254 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005255{
5256 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005257
Tejun Heob77d7b62013-08-13 11:01:54 -04005258 WARN_ON_ONCE(!rcu_read_lock_held());
5259
Tejun Heo35cf0832013-08-26 18:40:56 -04005260 /* is @dentry a cgroup dir? */
5261 if (!dentry->d_inode ||
5262 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005263 return ERR_PTR(-EBADF);
5264
Tejun Heo35cf0832013-08-26 18:40:56 -04005265 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005266 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005267}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005268
Li Zefan1cb650b2013-08-19 10:05:24 +08005269/**
5270 * css_from_id - lookup css by id
5271 * @id: the cgroup id
5272 * @ss: cgroup subsys to be looked into
5273 *
5274 * Returns the css if there's valid one with @id, otherwise returns NULL.
5275 * Should be called under rcu_read_lock().
5276 */
5277struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5278{
5279 struct cgroup *cgrp;
5280
5281 rcu_lockdep_assert(rcu_read_lock_held() ||
5282 lockdep_is_held(&cgroup_mutex),
5283 "css_from_id() needs proper protection");
5284
5285 cgrp = idr_find(&ss->root->cgroup_idr, id);
5286 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005287 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005288 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005289}
5290
Paul Menagefe693432009-09-23 15:56:20 -07005291#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005292static struct cgroup_subsys_state *
5293debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005294{
5295 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5296
5297 if (!css)
5298 return ERR_PTR(-ENOMEM);
5299
5300 return css;
5301}
5302
Tejun Heoeb954192013-08-08 20:11:23 -04005303static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005304{
Tejun Heoeb954192013-08-08 20:11:23 -04005305 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005306}
5307
Tejun Heo182446d2013-08-08 20:11:24 -04005308static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5309 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005310{
Tejun Heo182446d2013-08-08 20:11:24 -04005311 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005312}
5313
Tejun Heo182446d2013-08-08 20:11:24 -04005314static u64 current_css_set_read(struct cgroup_subsys_state *css,
5315 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005316{
5317 return (u64)(unsigned long)current->cgroups;
5318}
5319
Tejun Heo182446d2013-08-08 20:11:24 -04005320static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005321 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005322{
5323 u64 count;
5324
5325 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005326 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005327 rcu_read_unlock();
5328 return count;
5329}
5330
Tejun Heo182446d2013-08-08 20:11:24 -04005331static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005332 struct cftype *cft,
5333 struct seq_file *seq)
5334{
Tejun Heo69d02062013-06-12 21:04:50 -07005335 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005336 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005337
5338 read_lock(&css_set_lock);
5339 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005340 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005341 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005342 struct cgroup *c = link->cgrp;
5343 const char *name;
5344
5345 if (c->dentry)
5346 name = c->dentry->d_name.name;
5347 else
5348 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005349 seq_printf(seq, "Root %d group %s\n",
5350 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005351 }
5352 rcu_read_unlock();
5353 read_unlock(&css_set_lock);
5354 return 0;
5355}
5356
5357#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005358static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5359 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005360{
Tejun Heo69d02062013-06-12 21:04:50 -07005361 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005362
5363 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005364 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005365 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005366 struct task_struct *task;
5367 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005368 seq_printf(seq, "css_set %p\n", cset);
5369 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005370 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5371 seq_puts(seq, " ...\n");
5372 break;
5373 } else {
5374 seq_printf(seq, " task %d\n",
5375 task_pid_vnr(task));
5376 }
5377 }
5378 }
5379 read_unlock(&css_set_lock);
5380 return 0;
5381}
5382
Tejun Heo182446d2013-08-08 20:11:24 -04005383static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005384{
Tejun Heo182446d2013-08-08 20:11:24 -04005385 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005386}
5387
5388static struct cftype debug_files[] = {
5389 {
Paul Menagefe693432009-09-23 15:56:20 -07005390 .name = "taskcount",
5391 .read_u64 = debug_taskcount_read,
5392 },
5393
5394 {
5395 .name = "current_css_set",
5396 .read_u64 = current_css_set_read,
5397 },
5398
5399 {
5400 .name = "current_css_set_refcount",
5401 .read_u64 = current_css_set_refcount_read,
5402 },
5403
5404 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005405 .name = "current_css_set_cg_links",
5406 .read_seq_string = current_css_set_cg_links_read,
5407 },
5408
5409 {
5410 .name = "cgroup_css_links",
5411 .read_seq_string = cgroup_css_links_read,
5412 },
5413
5414 {
Paul Menagefe693432009-09-23 15:56:20 -07005415 .name = "releasable",
5416 .read_u64 = releasable_read,
5417 },
Paul Menagefe693432009-09-23 15:56:20 -07005418
Tejun Heo4baf6e32012-04-01 12:09:55 -07005419 { } /* terminate */
5420};
Paul Menagefe693432009-09-23 15:56:20 -07005421
5422struct cgroup_subsys debug_subsys = {
5423 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005424 .css_alloc = debug_css_alloc,
5425 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005426 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005427 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005428};
5429#endif /* CONFIG_CGROUP_DEBUG */