blob: c22eecbbbc1c56b350b3ecad3ab840979940670c [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/slab.h>
45#include <linux/magic.h>
46#include <linux/spinlock.h>
47#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070048#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070049#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080050#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070051#include <linux/delayacct.h>
52#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080053#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040054#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070055#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070056#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070057#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Li Zefan081aa452013-03-13 09:17:09 +080058#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020059#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070060
Arun Sharma600634972011-07-26 16:09:06 -070061#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070062
Tejun Heoe25e2cb2011-12-12 18:12:21 -080063/*
Tejun Heob1a21362013-11-29 10:42:58 -050064 * pidlists linger the following amount before being destroyed. The goal
65 * is avoiding frequent destruction in the middle of consecutive read calls
66 * Expiring in the middle is a performance problem not a correctness one.
67 * 1 sec should be enough.
68 */
69#define CGROUP_PIDLIST_DESTROY_DELAY HZ
70
71/*
Tejun Heoe25e2cb2011-12-12 18:12:21 -080072 * cgroup_mutex is the master lock. Any modification to cgroup or its
73 * hierarchy must be performed while holding it.
74 *
75 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
76 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
77 * release_agent_path and so on. Modifying requires both cgroup_mutex and
78 * cgroup_root_mutex. Readers can acquire either of the two. This is to
79 * break the following locking order cycle.
80 *
81 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
82 * B. namespace_sem -> cgroup_mutex
83 *
84 * B happens only through cgroup_show_options() and using cgroup_root_mutex
85 * breaks it.
86 */
Tejun Heo22194492013-04-07 09:29:51 -070087#ifdef CONFIG_PROVE_RCU
88DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040089EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070090#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070091static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070092#endif
93
Tejun Heoe25e2cb2011-12-12 18:12:21 -080094static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070095
Tejun Heo87fb54f2013-12-06 15:11:55 -050096#define cgroup_assert_mutex_or_rcu_locked() \
97 rcu_lockdep_assert(rcu_read_lock_held() || \
98 lockdep_is_held(&cgroup_mutex), \
99 "cgroup_mutex or RCU read lock required");
100
Ben Blumaae8aab2010-03-10 15:22:07 -0800101/*
Tejun Heoe5fca242013-11-22 17:14:39 -0500102 * cgroup destruction makes heavy use of work items and there can be a lot
103 * of concurrent destructions. Use a separate workqueue so that cgroup
104 * destruction work items don't end up filling up max_active of system_wq
105 * which may lead to deadlock.
106 */
107static struct workqueue_struct *cgroup_destroy_wq;
108
109/*
Tejun Heob1a21362013-11-29 10:42:58 -0500110 * pidlist destructions need to be flushed on cgroup destruction. Use a
111 * separate workqueue as flush domain.
112 */
113static struct workqueue_struct *cgroup_pidlist_destroy_wq;
114
115/*
Ben Blumaae8aab2010-03-10 15:22:07 -0800116 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +0200117 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800118 * registered after that. The mutable section of this array is protected by
119 * cgroup_mutex.
120 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200121#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200122#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700123static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700124#include <linux/cgroup_subsys.h>
125};
126
Paul Menageddbcc7e2007-10-18 23:39:30 -0700127/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700128 * The dummy hierarchy, reserved for the subsystems that are otherwise
129 * unattached - it never has more than a single cgroup, and all tasks are
130 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700131 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700132static struct cgroupfs_root cgroup_dummy_root;
133
134/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
135static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700136
137/* The list of hierarchy roots */
138
Tejun Heo9871bf92013-06-24 15:21:47 -0700139static LIST_HEAD(cgroup_roots);
140static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700141
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700142/*
143 * Hierarchy ID allocation and mapping. It follows the same exclusion
144 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
145 * writes, either for reads.
146 */
Tejun Heo1a574232013-04-14 11:36:58 -0700147static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700148
Li Zefan65dff752013-03-01 15:01:56 +0800149static struct cgroup_name root_cgroup_name = { .name = "/" };
150
Li Zefan794611a2013-06-18 18:53:53 +0800151/*
152 * Assign a monotonically increasing serial number to cgroups. It
153 * guarantees cgroups with bigger numbers are newer than those with smaller
154 * numbers. Also, as cgroups are always appended to the parent's
155 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700156 * the ascending serial number order on the list. Protected by
157 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800158 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700159static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800160
Paul Menageddbcc7e2007-10-18 23:39:30 -0700161/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800162 * check for fork/exit handlers to call. This avoids us having to do
163 * extra work in the fork/exit path if none of the subsystems need to
164 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700166static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700167
Tejun Heo628f7cd2013-06-28 16:24:11 -0700168static struct cftype cgroup_base_files[];
169
Tejun Heof20104d2013-08-13 20:22:50 -0400170static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800171static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400172static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
173 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500174static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heob1a21362013-11-29 10:42:58 -0500175static void cgroup_pidlist_destroy_all(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800176
Tejun Heo95109b62013-08-08 20:11:27 -0400177/**
178 * cgroup_css - obtain a cgroup's css for the specified subsystem
179 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400180 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400181 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400182 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
183 * function must be called either under cgroup_mutex or rcu_read_lock() and
184 * the caller is responsible for pinning the returned css if it wants to
185 * keep accessing it outside the said locks. This function may return
186 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400187 */
188static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400189 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400190{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400191 if (ss)
192 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
193 lockdep_is_held(&cgroup_mutex));
194 else
195 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400196}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700197
Paul Menageddbcc7e2007-10-18 23:39:30 -0700198/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700199static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700200{
Tejun Heo54766d42013-06-12 21:04:53 -0700201 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700202}
203
Li Zefan78574cf2013-04-08 19:00:38 -0700204/**
205 * cgroup_is_descendant - test ancestry
206 * @cgrp: the cgroup to be tested
207 * @ancestor: possible ancestor of @cgrp
208 *
209 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
210 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
211 * and @ancestor are accessible.
212 */
213bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
214{
215 while (cgrp) {
216 if (cgrp == ancestor)
217 return true;
218 cgrp = cgrp->parent;
219 }
220 return false;
221}
222EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223
Adrian Bunke9685a02008-02-07 00:13:46 -0800224static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700225{
226 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700227 (1 << CGRP_RELEASABLE) |
228 (1 << CGRP_NOTIFY_ON_RELEASE);
229 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700230}
231
Adrian Bunke9685a02008-02-07 00:13:46 -0800232static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700233{
Paul Menagebd89aab2007-10-18 23:40:44 -0700234 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700235}
236
Tejun Heo30159ec2013-06-25 11:53:37 -0700237/**
238 * for_each_subsys - iterate all loaded cgroup subsystems
239 * @ss: the iteration cursor
240 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
241 *
242 * Should be called under cgroup_mutex.
243 */
244#define for_each_subsys(ss, i) \
245 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
246 if (({ lockdep_assert_held(&cgroup_mutex); \
247 !((ss) = cgroup_subsys[i]); })) { } \
248 else
249
250/**
251 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
252 * @ss: the iteration cursor
253 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
254 *
255 * Bulit-in subsystems are always present and iteration itself doesn't
256 * require any synchronization.
257 */
258#define for_each_builtin_subsys(ss, i) \
259 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
260 (((ss) = cgroup_subsys[i]) || true); (i)++)
261
Tejun Heo5549c492013-06-24 15:21:48 -0700262/* iterate each subsystem attached to a hierarchy */
263#define for_each_root_subsys(root, ss) \
264 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700265
Tejun Heo5549c492013-06-24 15:21:48 -0700266/* iterate across the active hierarchies */
267#define for_each_active_root(root) \
268 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700269
Tejun Heof6ea9372012-04-01 12:09:55 -0700270static inline struct cgroup *__d_cgrp(struct dentry *dentry)
271{
272 return dentry->d_fsdata;
273}
274
Tejun Heo05ef1d72012-04-01 12:09:56 -0700275static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700276{
277 return dentry->d_fsdata;
278}
279
Tejun Heo05ef1d72012-04-01 12:09:56 -0700280static inline struct cftype *__d_cft(struct dentry *dentry)
281{
282 return __d_cfe(dentry)->type;
283}
284
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700285/**
286 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
287 * @cgrp: the cgroup to be checked for liveness
288 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700289 * On success, returns true; the mutex should be later unlocked. On
290 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700291 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700292static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293{
294 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700295 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700296 mutex_unlock(&cgroup_mutex);
297 return false;
298 }
299 return true;
300}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700301
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302/* the list of cgroups eligible for automatic release. Protected by
303 * release_list_lock */
304static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200305static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306static void cgroup_release_agent(struct work_struct *work);
307static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700308static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309
Tejun Heo69d02062013-06-12 21:04:50 -0700310/*
311 * A cgroup can be associated with multiple css_sets as different tasks may
312 * belong to different cgroups on different hierarchies. In the other
313 * direction, a css_set is naturally associated with multiple cgroups.
314 * This M:N relationship is represented by the following link structure
315 * which exists for each association and allows traversing the associations
316 * from both sides.
317 */
318struct cgrp_cset_link {
319 /* the cgroup and css_set this link associates */
320 struct cgroup *cgrp;
321 struct css_set *cset;
322
323 /* list of cgrp_cset_links anchored at cgrp->cset_links */
324 struct list_head cset_link;
325
326 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
327 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700328};
329
330/* The default css_set - used by init and its children prior to any
331 * hierarchies being mounted. It contains a pointer to the root state
332 * for each subsystem. Also used to anchor the list of css_sets. Not
333 * reference-counted, to improve performance when child cgroups
334 * haven't been created.
335 */
336
337static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700338static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700339
Tejun Heo0942eee2013-08-08 20:11:26 -0400340/*
341 * css_set_lock protects the list of css_set objects, and the chain of
342 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400343 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400344 */
Paul Menage817929e2007-10-18 23:39:36 -0700345static DEFINE_RWLOCK(css_set_lock);
346static int css_set_count;
347
Paul Menage7717f7b2009-09-23 15:56:22 -0700348/*
349 * hash table for cgroup groups. This improves the performance to find
350 * an existing css_set. This hash doesn't (currently) take into
351 * account cgroups in empty hierarchies.
352 */
Li Zefan472b1052008-04-29 01:00:11 -0700353#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800354static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700355
Li Zefan0ac801f2013-01-10 11:49:27 +0800356static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700357{
Li Zefan0ac801f2013-01-10 11:49:27 +0800358 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700359 struct cgroup_subsys *ss;
360 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700361
Tejun Heo30159ec2013-06-25 11:53:37 -0700362 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800363 key += (unsigned long)css[i];
364 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700365
Li Zefan0ac801f2013-01-10 11:49:27 +0800366 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700367}
368
Tejun Heo0942eee2013-08-08 20:11:26 -0400369/*
370 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400371 * until after the first call to css_task_iter_start(). This reduces the
372 * fork()/exit() overhead for people who have cgroups compiled into their
373 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400374 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700375static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700376
Tejun Heo5abb8852013-06-12 21:04:49 -0700377static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700378{
Tejun Heo69d02062013-06-12 21:04:50 -0700379 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700380
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700381 /*
382 * Ensure that the refcount doesn't hit zero while any readers
383 * can see it. Similar to atomic_dec_and_lock(), but for an
384 * rwlock
385 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700386 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700387 return;
388 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700389 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700390 write_unlock(&css_set_lock);
391 return;
392 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700393
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700395 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700396 css_set_count--;
397
Tejun Heo69d02062013-06-12 21:04:50 -0700398 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700399 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700400
Tejun Heo69d02062013-06-12 21:04:50 -0700401 list_del(&link->cset_link);
402 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800403
Tejun Heoddd69142013-06-12 21:04:54 -0700404 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700405 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700406 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700407 set_bit(CGRP_RELEASABLE, &cgrp->flags);
408 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700409 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700410
411 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700412 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700413
414 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700416}
417
418/*
419 * refcounted get/put for css_set objects
420 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700422{
Tejun Heo5abb8852013-06-12 21:04:49 -0700423 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700424}
425
Tejun Heo5abb8852013-06-12 21:04:49 -0700426static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700427{
Tejun Heo5abb8852013-06-12 21:04:49 -0700428 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700429}
430
Tejun Heo5abb8852013-06-12 21:04:49 -0700431static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432{
Tejun Heo5abb8852013-06-12 21:04:49 -0700433 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700434}
435
Tejun Heob326f9d2013-06-24 15:21:48 -0700436/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700437 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 * @cset: candidate css_set being tested
439 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700440 * @new_cgrp: cgroup that's being entered by the task
441 * @template: desired set of css pointers in css_set (pre-calculated)
442 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800443 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700444 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
445 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700446static bool compare_css_sets(struct css_set *cset,
447 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700448 struct cgroup *new_cgrp,
449 struct cgroup_subsys_state *template[])
450{
451 struct list_head *l1, *l2;
452
Tejun Heo5abb8852013-06-12 21:04:49 -0700453 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700454 /* Not all subsystems matched */
455 return false;
456 }
457
458 /*
459 * Compare cgroup pointers in order to distinguish between
460 * different cgroups in heirarchies with no subsystems. We
461 * could get by with just this check alone (and skip the
462 * memcmp above) but on most setups the memcmp check will
463 * avoid the need for this more expensive check on almost all
464 * candidates.
465 */
466
Tejun Heo69d02062013-06-12 21:04:50 -0700467 l1 = &cset->cgrp_links;
468 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700469 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700470 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700471 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700472
473 l1 = l1->next;
474 l2 = l2->next;
475 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700476 if (l1 == &cset->cgrp_links) {
477 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 break;
479 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700480 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 }
482 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700483 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
484 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
485 cgrp1 = link1->cgrp;
486 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700488 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700489
490 /*
491 * If this hierarchy is the hierarchy of the cgroup
492 * that's changing, then we need to check that this
493 * css_set points to the new cgroup; if it's any other
494 * hierarchy, then this css_set should point to the
495 * same cgroup as the old css_set.
496 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 if (cgrp1->root == new_cgrp->root) {
498 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700499 return false;
500 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700501 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700502 return false;
503 }
504 }
505 return true;
506}
507
Tejun Heob326f9d2013-06-24 15:21:48 -0700508/**
509 * find_existing_css_set - init css array and find the matching css_set
510 * @old_cset: the css_set that we're using before the cgroup transition
511 * @cgrp: the cgroup that we're moving into
512 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700513 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514static struct css_set *find_existing_css_set(struct css_set *old_cset,
515 struct cgroup *cgrp,
516 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700517{
Paul Menagebd89aab2007-10-18 23:40:44 -0700518 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700519 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700520 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800521 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700522 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700523
Ben Blumaae8aab2010-03-10 15:22:07 -0800524 /*
525 * Build the set of subsystem state objects that we want to see in the
526 * new css_set. while subsystems can change globally, the entries here
527 * won't change, so no need for locking.
528 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700529 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400530 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700531 /* Subsystem is in this hierarchy. So we want
532 * the subsystem state from the new
533 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400534 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700535 } else {
536 /* Subsystem is not in this hierarchy, so we
537 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700538 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700539 }
540 }
541
Li Zefan0ac801f2013-01-10 11:49:27 +0800542 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700543 hash_for_each_possible(css_set_table, cset, hlist, key) {
544 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700545 continue;
546
547 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700548 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700549 }
Paul Menage817929e2007-10-18 23:39:36 -0700550
551 /* No existing cgroup group matched */
552 return NULL;
553}
554
Tejun Heo69d02062013-06-12 21:04:50 -0700555static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700556{
Tejun Heo69d02062013-06-12 21:04:50 -0700557 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700558
Tejun Heo69d02062013-06-12 21:04:50 -0700559 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
560 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700561 kfree(link);
562 }
563}
564
Tejun Heo69d02062013-06-12 21:04:50 -0700565/**
566 * allocate_cgrp_cset_links - allocate cgrp_cset_links
567 * @count: the number of links to allocate
568 * @tmp_links: list_head the allocated links are put on
569 *
570 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
571 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700572 */
Tejun Heo69d02062013-06-12 21:04:50 -0700573static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700574{
Tejun Heo69d02062013-06-12 21:04:50 -0700575 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700576 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700577
578 INIT_LIST_HEAD(tmp_links);
579
Li Zefan36553432008-07-29 22:33:19 -0700580 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700581 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700582 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700583 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700584 return -ENOMEM;
585 }
Tejun Heo69d02062013-06-12 21:04:50 -0700586 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700587 }
588 return 0;
589}
590
Li Zefanc12f65d2009-01-07 18:07:42 -0800591/**
592 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700593 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700594 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800595 * @cgrp: the destination cgroup
596 */
Tejun Heo69d02062013-06-12 21:04:50 -0700597static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
598 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800599{
Tejun Heo69d02062013-06-12 21:04:50 -0700600 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800601
Tejun Heo69d02062013-06-12 21:04:50 -0700602 BUG_ON(list_empty(tmp_links));
603 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
604 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700605 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700606 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700607 /*
608 * Always add links to the tail of the list so that the list
609 * is sorted by order of hierarchy creation
610 */
Tejun Heo69d02062013-06-12 21:04:50 -0700611 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800612}
613
Tejun Heob326f9d2013-06-24 15:21:48 -0700614/**
615 * find_css_set - return a new css_set with one cgroup updated
616 * @old_cset: the baseline css_set
617 * @cgrp: the cgroup to be updated
618 *
619 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
620 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700621 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700622static struct css_set *find_css_set(struct css_set *old_cset,
623 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700624{
Tejun Heob326f9d2013-06-24 15:21:48 -0700625 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700626 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700627 struct list_head tmp_links;
628 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800629 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700630
Tejun Heob326f9d2013-06-24 15:21:48 -0700631 lockdep_assert_held(&cgroup_mutex);
632
Paul Menage817929e2007-10-18 23:39:36 -0700633 /* First see if we already have a cgroup group that matches
634 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700635 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700636 cset = find_existing_css_set(old_cset, cgrp, template);
637 if (cset)
638 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700639 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700640
Tejun Heo5abb8852013-06-12 21:04:49 -0700641 if (cset)
642 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700643
Tejun Heof4f4be22013-06-12 21:04:51 -0700644 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700646 return NULL;
647
Tejun Heo69d02062013-06-12 21:04:50 -0700648 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700649 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700651 return NULL;
652 }
653
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700655 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 INIT_LIST_HEAD(&cset->tasks);
657 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700658
659 /* Copy the set of subsystem state objects generated in
660 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700661 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700662
663 write_lock(&css_set_lock);
664 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700665 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700666 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700667
Paul Menage7717f7b2009-09-23 15:56:22 -0700668 if (c->root == cgrp->root)
669 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700670 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700671 }
Paul Menage817929e2007-10-18 23:39:36 -0700672
Tejun Heo69d02062013-06-12 21:04:50 -0700673 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700674
Paul Menage817929e2007-10-18 23:39:36 -0700675 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700676
677 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 key = css_set_hash(cset->subsys);
679 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700680
Paul Menage817929e2007-10-18 23:39:36 -0700681 write_unlock(&css_set_lock);
682
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700684}
685
Paul Menageddbcc7e2007-10-18 23:39:30 -0700686/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700687 * Return the cgroup for "task" from the given hierarchy. Must be
688 * called with cgroup_mutex held.
689 */
690static struct cgroup *task_cgroup_from_root(struct task_struct *task,
691 struct cgroupfs_root *root)
692{
Tejun Heo5abb8852013-06-12 21:04:49 -0700693 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 struct cgroup *res = NULL;
695
696 BUG_ON(!mutex_is_locked(&cgroup_mutex));
697 read_lock(&css_set_lock);
698 /*
699 * No need to lock the task - since we hold cgroup_mutex the
700 * task can't change groups, so the only thing that can happen
701 * is that it exits and its css is set back to init_css_set.
702 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700703 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700705 res = &root->top_cgroup;
706 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700707 struct cgrp_cset_link *link;
708
709 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700710 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700711
Paul Menage7717f7b2009-09-23 15:56:22 -0700712 if (c->root == root) {
713 res = c;
714 break;
715 }
716 }
717 }
718 read_unlock(&css_set_lock);
719 BUG_ON(!res);
720 return res;
721}
722
723/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700724 * There is one global cgroup mutex. We also require taking
725 * task_lock() when dereferencing a task's cgroup subsys pointers.
726 * See "The task_lock() exception", at the end of this comment.
727 *
728 * A task must hold cgroup_mutex to modify cgroups.
729 *
730 * Any task can increment and decrement the count field without lock.
731 * So in general, code holding cgroup_mutex can't rely on the count
732 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800733 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700734 * means that no tasks are currently attached, therefore there is no
735 * way a task attached to that cgroup can fork (the other way to
736 * increment the count). So code holding cgroup_mutex can safely
737 * assume that if the count is zero, it will stay zero. Similarly, if
738 * a task holds cgroup_mutex on a cgroup with zero count, it
739 * knows that the cgroup won't be removed, as cgroup_rmdir()
740 * needs that mutex.
741 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700742 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
743 * (usually) take cgroup_mutex. These are the two most performance
744 * critical pieces of code here. The exception occurs on cgroup_exit(),
745 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
746 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800747 * to the release agent with the name of the cgroup (path relative to
748 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700749 *
750 * A cgroup can only be deleted if both its 'count' of using tasks
751 * is zero, and its list of 'children' cgroups is empty. Since all
752 * tasks in the system use _some_ cgroup, and since there is always at
753 * least one task in the system (init, pid == 1), therefore, top_cgroup
754 * always has either children cgroups and/or using tasks. So we don't
755 * need a special hack to ensure that top_cgroup cannot be deleted.
756 *
757 * The task_lock() exception
758 *
759 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800760 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800761 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700762 * several performance critical places that need to reference
763 * task->cgroup without the expense of grabbing a system global
764 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800765 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
767 * the task_struct routinely used for such matters.
768 *
769 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800770 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700771 */
772
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773/*
774 * A couple of forward declarations required, due to cyclic reference loop:
775 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
776 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
777 * -> cgroup_mkdir.
778 */
779
Al Viro18bb1db2011-07-26 01:41:39 -0400780static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700781static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700782static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700783static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700784static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700785
786static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200787 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700788 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700789};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790
Al Viroa5e7ed32011-07-26 01:55:55 -0400791static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792{
793 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794
795 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400796 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100798 inode->i_uid = current_fsuid();
799 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
801 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
802 }
803 return inode;
804}
805
Li Zefan65dff752013-03-01 15:01:56 +0800806static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
807{
808 struct cgroup_name *name;
809
810 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
811 if (!name)
812 return NULL;
813 strcpy(name->name, dentry->d_name.name);
814 return name;
815}
816
Li Zefanbe445622013-01-24 14:31:42 +0800817static void cgroup_free_fn(struct work_struct *work)
818{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700819 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800820
821 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800822 cgrp->root->number_of_cgroups--;
823 mutex_unlock(&cgroup_mutex);
824
825 /*
Li Zefan415cf072013-04-08 14:35:02 +0800826 * We get a ref to the parent's dentry, and put the ref when
827 * this cgroup is being freed, so it's guaranteed that the
828 * parent won't be destroyed before its children.
829 */
830 dput(cgrp->parent->dentry);
831
832 /*
Li Zefanbe445622013-01-24 14:31:42 +0800833 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700834 * created the cgroup. This will free cgrp->root, if we are
835 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800836 */
837 deactivate_super(cgrp->root->sb);
838
Tejun Heob1a21362013-11-29 10:42:58 -0500839 cgroup_pidlist_destroy_all(cgrp);
Li Zefanbe445622013-01-24 14:31:42 +0800840
841 simple_xattrs_free(&cgrp->xattrs);
842
Li Zefan65dff752013-03-01 15:01:56 +0800843 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800844 kfree(cgrp);
845}
846
847static void cgroup_free_rcu(struct rcu_head *head)
848{
849 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
850
Tejun Heoea15f8c2013-06-13 19:27:42 -0700851 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500852 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800853}
854
Paul Menageddbcc7e2007-10-18 23:39:30 -0700855static void cgroup_diput(struct dentry *dentry, struct inode *inode)
856{
857 /* is dentry a directory ? if so, kfree() associated cgroup */
858 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700859 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800860
Tejun Heo54766d42013-06-12 21:04:53 -0700861 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800862 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700863 } else {
864 struct cfent *cfe = __d_cfe(dentry);
865 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
866
867 WARN_ONCE(!list_empty(&cfe->node) &&
868 cgrp != &cgrp->root->top_cgroup,
869 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700870 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700871 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700872 }
873 iput(inode);
874}
875
876static void remove_dir(struct dentry *d)
877{
878 struct dentry *parent = dget(d->d_parent);
879
880 d_delete(d);
881 simple_rmdir(parent->d_inode, d);
882 dput(parent);
883}
884
Li Zefan2739d3c2013-01-21 18:18:33 +0800885static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700887 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700888
Tejun Heo05ef1d72012-04-01 12:09:56 -0700889 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
890 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100891
Li Zefan2739d3c2013-01-21 18:18:33 +0800892 /*
893 * If we're doing cleanup due to failure of cgroup_create(),
894 * the corresponding @cfe may not exist.
895 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700896 list_for_each_entry(cfe, &cgrp->files, node) {
897 struct dentry *d = cfe->dentry;
898
899 if (cft && cfe->type != cft)
900 continue;
901
902 dget(d);
903 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700904 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700905 list_del_init(&cfe->node);
906 dput(d);
907
Li Zefan2739d3c2013-01-21 18:18:33 +0800908 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700909 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700910}
911
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400912/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700913 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700914 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400915 * @subsys_mask: mask of the subsystem ids whose files should be removed
916 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700917static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400919 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700920 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700921
Tejun Heob420ba72013-07-12 12:34:02 -0700922 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400923 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700924
925 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400926 continue;
927 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400928 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400929 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930}
931
932/*
933 * NOTE : the dentry must have been dget()'ed
934 */
935static void cgroup_d_remove_dir(struct dentry *dentry)
936{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100937 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700938
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100939 parent = dentry->d_parent;
940 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800941 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100943 spin_unlock(&dentry->d_lock);
944 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700945 remove_dir(dentry);
946}
947
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700948/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800949 * Call with cgroup_mutex held. Drops reference counts on modules, including
950 * any duplicate ones that parse_cgroupfs_options took. If this function
951 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800952 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700954 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955{
Paul Menagebd89aab2007-10-18 23:40:44 -0700956 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700957 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700958 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700959 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960
Ben Blumaae8aab2010-03-10 15:22:07 -0800961 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800962 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800963
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700965 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700966 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700968
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700969 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700970 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700971 ret = -EBUSY;
972 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700974
975 /* pin the module */
976 if (!try_module_get(ss->module)) {
977 ret = -ENOENT;
978 goto out_put;
979 }
980 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981 }
982
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700983 /* subsys could be missing if unloaded between parsing and here */
984 if (added_mask != pinned) {
985 ret = -ENOENT;
986 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988
Tejun Heo31261212013-06-28 17:07:30 -0700989 ret = cgroup_populate_dir(cgrp, added_mask);
990 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700991 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -0700992
993 /*
994 * Nothing can fail from this point on. Remove files for the
995 * removed subsystems and rebind each subsystem.
996 */
997 cgroup_clear_dir(cgrp, removed_mask);
998
Tejun Heo30159ec2013-06-25 11:53:37 -0700999 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001001
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001002 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001004 BUG_ON(cgroup_css(cgrp, ss));
1005 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1006 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001007
Tejun Heo73e80ed2013-08-13 11:01:55 -04001008 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001009 cgroup_css(cgroup_dummy_top, ss));
1010 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001011
Li Zefan33a68ac2009-01-07 18:07:42 -08001012 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001013 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001015 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001016
Ben Blumcf5d5942010-03-10 15:22:09 -08001017 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001018 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001019 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001021 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1022 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001023
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001025 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001026
Tejun Heoca8bdca2013-08-26 18:40:56 -04001027 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001028 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1029
Tejun Heo9871bf92013-06-24 15:21:47 -07001030 cgroup_subsys[i]->root = &cgroup_dummy_root;
1031 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001032
Ben Blumcf5d5942010-03-10 15:22:09 -08001033 /* subsystem is now free - drop reference on module */
1034 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001035 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 }
1037 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038
Tejun Heo1672d042013-06-25 18:04:54 -07001039 /*
1040 * Mark @root has finished binding subsystems. @root->subsys_mask
1041 * now matches the bound subsystems.
1042 */
1043 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1044
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001046
1047out_put:
1048 for_each_subsys(ss, i)
1049 if (pinned & (1 << i))
1050 module_put(ss->module);
1051 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052}
1053
Al Viro34c80b12011-12-08 21:32:45 -05001054static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055{
Al Viro34c80b12011-12-08 21:32:45 -05001056 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 struct cgroup_subsys *ss;
1058
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001059 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001060 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001062 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1063 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001064 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001066 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001067 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001068 if (strlen(root->release_agent_path))
1069 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001070 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001071 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001072 if (strlen(root->name))
1073 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001074 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 return 0;
1076}
1077
1078struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001079 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001081 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001082 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001083 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001084 /* User explicitly requested empty subsystem */
1085 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001086
1087 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001088
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089};
1090
Ben Blumaae8aab2010-03-10 15:22:07 -08001091/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001092 * Convert a hierarchy specifier into a bitmask of subsystems and
1093 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1094 * array. This function takes refcounts on subsystems to be used, unless it
1095 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001096 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001097static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001099 char *token, *o = data;
1100 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001101 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001102 struct cgroup_subsys *ss;
1103 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001104
Ben Blumaae8aab2010-03-10 15:22:07 -08001105 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1106
Li Zefanf9ab5b52009-06-17 16:26:33 -07001107#ifdef CONFIG_CPUSETS
1108 mask = ~(1UL << cpuset_subsys_id);
1109#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110
Paul Menagec6d57f32009-09-23 15:56:19 -07001111 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112
1113 while ((token = strsep(&o, ",")) != NULL) {
1114 if (!*token)
1115 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001116 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001117 /* Explicitly have no subsystems */
1118 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001119 continue;
1120 }
1121 if (!strcmp(token, "all")) {
1122 /* Mutually exclusive option 'all' + subsystem name */
1123 if (one_ss)
1124 return -EINVAL;
1125 all_ss = true;
1126 continue;
1127 }
Tejun Heo873fe092013-04-14 20:15:26 -07001128 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1129 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1130 continue;
1131 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001133 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 continue;
1135 }
1136 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001137 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001138 continue;
1139 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001140 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001141 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001142 continue;
1143 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001145 /* Specifying two release agents is forbidden */
1146 if (opts->release_agent)
1147 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001148 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001149 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001150 if (!opts->release_agent)
1151 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 continue;
1153 }
1154 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 const char *name = token + 5;
1156 /* Can't specify an empty name */
1157 if (!strlen(name))
1158 return -EINVAL;
1159 /* Must match [\w.-]+ */
1160 for (i = 0; i < strlen(name); i++) {
1161 char c = name[i];
1162 if (isalnum(c))
1163 continue;
1164 if ((c == '.') || (c == '-') || (c == '_'))
1165 continue;
1166 return -EINVAL;
1167 }
1168 /* Specifying two names is forbidden */
1169 if (opts->name)
1170 return -EINVAL;
1171 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001172 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 GFP_KERNEL);
1174 if (!opts->name)
1175 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176
1177 continue;
1178 }
1179
Tejun Heo30159ec2013-06-25 11:53:37 -07001180 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 if (strcmp(token, ss->name))
1182 continue;
1183 if (ss->disabled)
1184 continue;
1185
1186 /* Mutually exclusive option 'all' + subsystem name */
1187 if (all_ss)
1188 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001189 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001190 one_ss = true;
1191
1192 break;
1193 }
1194 if (i == CGROUP_SUBSYS_COUNT)
1195 return -ENOENT;
1196 }
1197
1198 /*
1199 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001200 * otherwise if 'none', 'name=' and a subsystem name options
1201 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001203 if (all_ss || (!one_ss && !opts->none && !opts->name))
1204 for_each_subsys(ss, i)
1205 if (!ss->disabled)
1206 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001207
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001208 /* Consistency checks */
1209
Tejun Heo873fe092013-04-14 20:15:26 -07001210 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1211 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1212
1213 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1214 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1215 return -EINVAL;
1216 }
1217
1218 if (opts->cpuset_clone_children) {
1219 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1220 return -EINVAL;
1221 }
1222 }
1223
Li Zefanf9ab5b52009-06-17 16:26:33 -07001224 /*
1225 * Option noprefix was introduced just for backward compatibility
1226 * with the old cpuset, so we allow noprefix only if mounting just
1227 * the cpuset subsystem.
1228 */
Tejun Heo93438622013-04-14 20:15:25 -07001229 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001230 return -EINVAL;
1231
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001232
1233 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001234 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001235 return -EINVAL;
1236
1237 /*
1238 * We either have to specify by name or by subsystems. (So all
1239 * empty hierarchies must have a name).
1240 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001241 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001242 return -EINVAL;
1243
1244 return 0;
1245}
1246
1247static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1248{
1249 int ret = 0;
1250 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001251 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001253 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001254
Tejun Heo873fe092013-04-14 20:15:26 -07001255 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1256 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1257 return -EINVAL;
1258 }
1259
Paul Menagebd89aab2007-10-18 23:40:44 -07001260 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001262 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001263
1264 /* See what subsystems are wanted */
1265 ret = parse_cgroupfs_options(data, &opts);
1266 if (ret)
1267 goto out_unlock;
1268
Tejun Heoa8a648c2013-06-24 15:21:47 -07001269 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001270 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1271 task_tgid_nr(current), current->comm);
1272
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001273 added_mask = opts.subsys_mask & ~root->subsys_mask;
1274 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001275
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001277 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001279 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1280 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1281 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001283 goto out_unlock;
1284 }
1285
Tejun Heof172e672013-06-28 17:07:30 -07001286 /* remounting is not allowed for populated hierarchies */
1287 if (root->number_of_cgroups > 1) {
1288 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001289 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001293 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001294 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001295
Paul Menage81a6a5c2007-10-18 23:39:38 -07001296 if (opts.release_agent)
1297 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001299 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001300 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001301 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001303 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 return ret;
1305}
1306
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001307static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308 .statfs = simple_statfs,
1309 .drop_inode = generic_delete_inode,
1310 .show_options = cgroup_show_options,
1311 .remount_fs = cgroup_remount,
1312};
1313
Paul Menagecc31edc2008-10-18 20:28:04 -07001314static void init_cgroup_housekeeping(struct cgroup *cgrp)
1315{
1316 INIT_LIST_HEAD(&cgrp->sibling);
1317 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001318 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001319 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001320 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001321 INIT_LIST_HEAD(&cgrp->pidlists);
1322 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001323 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001324 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001325}
Paul Menagec6d57f32009-09-23 15:56:19 -07001326
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327static void init_cgroup_root(struct cgroupfs_root *root)
1328{
Paul Menagebd89aab2007-10-18 23:40:44 -07001329 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001330
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 INIT_LIST_HEAD(&root->subsys_list);
1332 INIT_LIST_HEAD(&root->root_list);
1333 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001335 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001336 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001337 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338}
1339
Tejun Heofc76df72013-06-25 11:53:37 -07001340static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001341{
Tejun Heo1a574232013-04-14 11:36:58 -07001342 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001343
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001344 lockdep_assert_held(&cgroup_mutex);
1345 lockdep_assert_held(&cgroup_root_mutex);
1346
Tejun Heofc76df72013-06-25 11:53:37 -07001347 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1348 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001349 if (id < 0)
1350 return id;
1351
1352 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001353 return 0;
1354}
1355
1356static void cgroup_exit_root_id(struct cgroupfs_root *root)
1357{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001358 lockdep_assert_held(&cgroup_mutex);
1359 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001360
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001361 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001362 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001363 root->hierarchy_id = 0;
1364 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001365}
1366
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367static int cgroup_test_super(struct super_block *sb, void *data)
1368{
Paul Menagec6d57f32009-09-23 15:56:19 -07001369 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370 struct cgroupfs_root *root = sb->s_fs_info;
1371
Paul Menagec6d57f32009-09-23 15:56:19 -07001372 /* If we asked for a name then it must match */
1373 if (opts->name && strcmp(opts->name, root->name))
1374 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001375
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001376 /*
1377 * If we asked for subsystems (or explicitly for no
1378 * subsystems) then they must match
1379 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001380 if ((opts->subsys_mask || opts->none)
1381 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 return 0;
1383
1384 return 1;
1385}
1386
Paul Menagec6d57f32009-09-23 15:56:19 -07001387static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1388{
1389 struct cgroupfs_root *root;
1390
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001391 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001392 return NULL;
1393
1394 root = kzalloc(sizeof(*root), GFP_KERNEL);
1395 if (!root)
1396 return ERR_PTR(-ENOMEM);
1397
1398 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001399
Tejun Heo1672d042013-06-25 18:04:54 -07001400 /*
1401 * We need to set @root->subsys_mask now so that @root can be
1402 * matched by cgroup_test_super() before it finishes
1403 * initialization; otherwise, competing mounts with the same
1404 * options may try to bind the same subsystems instead of waiting
1405 * for the first one leading to unexpected mount errors.
1406 * SUBSYS_BOUND will be set once actual binding is complete.
1407 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001408 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001409 root->flags = opts->flags;
1410 if (opts->release_agent)
1411 strcpy(root->release_agent_path, opts->release_agent);
1412 if (opts->name)
1413 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001414 if (opts->cpuset_clone_children)
1415 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001416 return root;
1417}
1418
Tejun Heofa3ca072013-04-14 11:36:56 -07001419static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001420{
Tejun Heofa3ca072013-04-14 11:36:56 -07001421 if (root) {
1422 /* hierarhcy ID shoulid already have been released */
1423 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001424
Li Zefan4e96ee82013-07-31 09:50:50 +08001425 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001426 kfree(root);
1427 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428}
1429
Paul Menageddbcc7e2007-10-18 23:39:30 -07001430static int cgroup_set_super(struct super_block *sb, void *data)
1431{
1432 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 struct cgroup_sb_opts *opts = data;
1434
1435 /* If we don't have a new root, we can't set up a new sb */
1436 if (!opts->new_root)
1437 return -EINVAL;
1438
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001439 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440
1441 ret = set_anon_super(sb, NULL);
1442 if (ret)
1443 return ret;
1444
Paul Menagec6d57f32009-09-23 15:56:19 -07001445 sb->s_fs_info = opts->new_root;
1446 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447
1448 sb->s_blocksize = PAGE_CACHE_SIZE;
1449 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1450 sb->s_magic = CGROUP_SUPER_MAGIC;
1451 sb->s_op = &cgroup_ops;
1452
1453 return 0;
1454}
1455
1456static int cgroup_get_rootdir(struct super_block *sb)
1457{
Al Viro0df6a632010-12-21 13:29:29 -05001458 static const struct dentry_operations cgroup_dops = {
1459 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001460 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001461 };
1462
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463 struct inode *inode =
1464 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465
1466 if (!inode)
1467 return -ENOMEM;
1468
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469 inode->i_fop = &simple_dir_operations;
1470 inode->i_op = &cgroup_dir_inode_operations;
1471 /* directories start off with i_nlink == 2 (for "." entry) */
1472 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001473 sb->s_root = d_make_root(inode);
1474 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001476 /* for everything else we want ->d_op set */
1477 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 return 0;
1479}
1480
Al Virof7e83572010-07-26 13:23:11 +04001481static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001483 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001484{
1485 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001486 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 int ret = 0;
1488 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001490 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001491 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001492 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493
1494 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001495 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001497 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 if (ret)
1499 goto out_err;
1500
1501 /*
1502 * Allocate a new cgroup root. We may not need it if we're
1503 * reusing an existing hierarchy.
1504 */
1505 new_root = cgroup_root_from_opts(&opts);
1506 if (IS_ERR(new_root)) {
1507 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001508 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001509 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511
Paul Menagec6d57f32009-09-23 15:56:19 -07001512 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001513 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001516 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001517 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518 }
1519
Paul Menagec6d57f32009-09-23 15:56:19 -07001520 root = sb->s_fs_info;
1521 BUG_ON(!root);
1522 if (root == opts.new_root) {
1523 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001524 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001525 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001526 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001527 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528
1529 BUG_ON(sb->s_root != NULL);
1530
1531 ret = cgroup_get_rootdir(sb);
1532 if (ret)
1533 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001534 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535
Paul Menage817929e2007-10-18 23:39:36 -07001536 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001538 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
Li Zefan4e96ee82013-07-31 09:50:50 +08001540 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1541 0, 1, GFP_KERNEL);
1542 if (root_cgrp->id < 0)
1543 goto unlock_drop;
1544
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001545 /* Check for name clashes with existing mounts */
1546 ret = -EBUSY;
1547 if (strlen(root->name))
1548 for_each_active_root(existing_root)
1549 if (!strcmp(existing_root->name, root->name))
1550 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001551
Paul Menage817929e2007-10-18 23:39:36 -07001552 /*
1553 * We're accessing css_set_count without locking
1554 * css_set_lock here, but that's OK - it can only be
1555 * increased by someone holding cgroup_lock, and
1556 * that's us. The worst that can happen is that we
1557 * have some link structures left over
1558 */
Tejun Heo69d02062013-06-12 21:04:50 -07001559 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001560 if (ret)
1561 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001562
Tejun Heofc76df72013-06-25 11:53:37 -07001563 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1564 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001565 if (ret)
1566 goto unlock_drop;
1567
Tejun Heo31261212013-06-28 17:07:30 -07001568 sb->s_root->d_fsdata = root_cgrp;
1569 root_cgrp->dentry = sb->s_root;
1570
1571 /*
1572 * We're inside get_sb() and will call lookup_one_len() to
1573 * create the root files, which doesn't work if SELinux is
1574 * in use. The following cred dancing somehow works around
1575 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1576 * populating new cgroupfs mount") for more details.
1577 */
1578 cred = override_creds(&init_cred);
1579
Tejun Heo2bb566c2013-08-08 20:11:23 -04001580 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001581 if (ret)
1582 goto rm_base_files;
1583
Tejun Heoa8a648c2013-06-24 15:21:47 -07001584 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001585 if (ret)
1586 goto rm_base_files;
1587
1588 revert_creds(cred);
1589
Ben Blumcf5d5942010-03-10 15:22:09 -08001590 /*
1591 * There must be no failure case after here, since rebinding
1592 * takes care of subsystems' refcounts, which are explicitly
1593 * dropped in the failure exit path.
1594 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001595
Tejun Heo9871bf92013-06-24 15:21:47 -07001596 list_add(&root->root_list, &cgroup_roots);
1597 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
Paul Menage817929e2007-10-18 23:39:36 -07001599 /* Link the top cgroup in this hierarchy into all
1600 * the css_set objects */
1601 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001602 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001603 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001604 write_unlock(&css_set_lock);
1605
Tejun Heo69d02062013-06-12 21:04:50 -07001606 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001607
Li Zefanc12f65d2009-01-07 18:07:42 -08001608 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609 BUG_ON(root->number_of_cgroups != 1);
1610
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001613 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001614 } else {
1615 /*
1616 * We re-used an existing hierarchy - the new root (if
1617 * any) is not needed
1618 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001619 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001620
Tejun Heoc7ba8282013-06-29 14:06:10 -07001621 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001622 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1623 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1624 ret = -EINVAL;
1625 goto drop_new_super;
1626 } else {
1627 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1628 }
Tejun Heo873fe092013-04-14 20:15:26 -07001629 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630 }
1631
Paul Menagec6d57f32009-09-23 15:56:19 -07001632 kfree(opts.release_agent);
1633 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001634 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635
Tejun Heo31261212013-06-28 17:07:30 -07001636 rm_base_files:
1637 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001638 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001639 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001640 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001641 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001642 mutex_unlock(&cgroup_root_mutex);
1643 mutex_unlock(&cgroup_mutex);
1644 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001646 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001647 out_err:
1648 kfree(opts.release_agent);
1649 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001650 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001651}
1652
1653static void cgroup_kill_sb(struct super_block *sb) {
1654 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001655 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001656 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 int ret;
1658
1659 BUG_ON(!root);
1660
1661 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001662 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663
Tejun Heo31261212013-06-28 17:07:30 -07001664 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001666 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667
1668 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001669 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1670 ret = rebind_subsystems(root, 0, root->subsys_mask);
1671 /* Shouldn't be able to fail ... */
1672 BUG_ON(ret);
1673 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674
Paul Menage817929e2007-10-18 23:39:36 -07001675 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001676 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001677 * root cgroup
1678 */
1679 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001680
Tejun Heo69d02062013-06-12 21:04:50 -07001681 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1682 list_del(&link->cset_link);
1683 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001684 kfree(link);
1685 }
1686 write_unlock(&css_set_lock);
1687
Paul Menage839ec542009-01-29 14:25:22 -08001688 if (!list_empty(&root->root_list)) {
1689 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001690 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001691 }
Li Zefane5f6a862009-01-07 18:07:41 -08001692
Tejun Heofa3ca072013-04-14 11:36:56 -07001693 cgroup_exit_root_id(root);
1694
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001695 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001697 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001699 simple_xattrs_free(&cgrp->xattrs);
1700
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001702 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703}
1704
1705static struct file_system_type cgroup_fs_type = {
1706 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001707 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 .kill_sb = cgroup_kill_sb,
1709};
1710
Greg KH676db4a2010-08-05 13:53:35 -07001711static struct kobject *cgroup_kobj;
1712
Li Zefana043e3b2008-02-23 15:24:09 -08001713/**
1714 * cgroup_path - generate the path of a cgroup
1715 * @cgrp: the cgroup in question
1716 * @buf: the buffer to write the path into
1717 * @buflen: the length of the buffer
1718 *
Li Zefan65dff752013-03-01 15:01:56 +08001719 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1720 *
1721 * We can't generate cgroup path using dentry->d_name, as accessing
1722 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1723 * inode's i_mutex, while on the other hand cgroup_path() can be called
1724 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001726int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727{
Li Zefan65dff752013-03-01 15:01:56 +08001728 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001730
Tejun Heoda1f2962013-04-14 10:32:19 -07001731 if (!cgrp->parent) {
1732 if (strlcpy(buf, "/", buflen) >= buflen)
1733 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 return 0;
1735 }
1736
Tao Ma316eb662012-11-08 21:36:38 +08001737 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001738 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001739
Li Zefan65dff752013-03-01 15:01:56 +08001740 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001741 do {
Li Zefan65dff752013-03-01 15:01:56 +08001742 const char *name = cgroup_name(cgrp);
1743 int len;
1744
1745 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001747 goto out;
1748 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001749
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001751 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001752 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001753
1754 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001755 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001756 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001758out:
1759 rcu_read_unlock();
1760 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761}
Ben Blum67523c42010-03-10 15:22:11 -08001762EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763
Tejun Heo857a2be2013-04-14 20:50:08 -07001764/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001765 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001766 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001767 * @buf: the buffer to write the path into
1768 * @buflen: the length of the buffer
1769 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001770 * Determine @task's cgroup on the first (the one with the lowest non-zero
1771 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1772 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1773 * cgroup controller callbacks.
1774 *
1775 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001776 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001777int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001778{
1779 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001780 struct cgroup *cgrp;
1781 int hierarchy_id = 1, ret = 0;
1782
1783 if (buflen < 2)
1784 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001785
1786 mutex_lock(&cgroup_mutex);
1787
Tejun Heo913ffdb2013-07-11 16:34:48 -07001788 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1789
Tejun Heo857a2be2013-04-14 20:50:08 -07001790 if (root) {
1791 cgrp = task_cgroup_from_root(task, root);
1792 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001793 } else {
1794 /* if no hierarchy exists, everyone is in "/" */
1795 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001796 }
1797
1798 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 return ret;
1800}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001801EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001802
Ben Blum74a11662011-05-26 16:25:20 -07001803/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001804 * Control Group taskset
1805 */
Tejun Heo134d3372011-12-12 18:12:21 -08001806struct task_and_cgroup {
1807 struct task_struct *task;
1808 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001809 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001810};
1811
Tejun Heo2f7ee562011-12-12 18:12:21 -08001812struct cgroup_taskset {
1813 struct task_and_cgroup single;
1814 struct flex_array *tc_array;
1815 int tc_array_len;
1816 int idx;
1817 struct cgroup *cur_cgrp;
1818};
1819
1820/**
1821 * cgroup_taskset_first - reset taskset and return the first task
1822 * @tset: taskset of interest
1823 *
1824 * @tset iteration is initialized and the first task is returned.
1825 */
1826struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1827{
1828 if (tset->tc_array) {
1829 tset->idx = 0;
1830 return cgroup_taskset_next(tset);
1831 } else {
1832 tset->cur_cgrp = tset->single.cgrp;
1833 return tset->single.task;
1834 }
1835}
1836EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1837
1838/**
1839 * cgroup_taskset_next - iterate to the next task in taskset
1840 * @tset: taskset of interest
1841 *
1842 * Return the next task in @tset. Iteration must have been initialized
1843 * with cgroup_taskset_first().
1844 */
1845struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1846{
1847 struct task_and_cgroup *tc;
1848
1849 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1850 return NULL;
1851
1852 tc = flex_array_get(tset->tc_array, tset->idx++);
1853 tset->cur_cgrp = tc->cgrp;
1854 return tc->task;
1855}
1856EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1857
1858/**
Tejun Heod99c8722013-08-08 20:11:27 -04001859 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001860 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001861 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862 *
Tejun Heod99c8722013-08-08 20:11:27 -04001863 * Return the css for the current (last returned) task of @tset for
1864 * subsystem specified by @subsys_id. This function must be preceded by
1865 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001866 */
Tejun Heod99c8722013-08-08 20:11:27 -04001867struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1868 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001870 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001871}
Tejun Heod99c8722013-08-08 20:11:27 -04001872EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001873
1874/**
1875 * cgroup_taskset_size - return the number of tasks in taskset
1876 * @tset: taskset of interest
1877 */
1878int cgroup_taskset_size(struct cgroup_taskset *tset)
1879{
1880 return tset->tc_array ? tset->tc_array_len : 1;
1881}
1882EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1883
1884
Ben Blum74a11662011-05-26 16:25:20 -07001885/*
1886 * cgroup_task_migrate - move a task from one cgroup to another.
1887 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001888 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001889 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001890static void cgroup_task_migrate(struct cgroup *old_cgrp,
1891 struct task_struct *tsk,
1892 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001893{
Tejun Heo5abb8852013-06-12 21:04:49 -07001894 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001895
1896 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001897 * We are synchronized through threadgroup_lock() against PF_EXITING
1898 * setting such that we can't race against cgroup_exit() changing the
1899 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001900 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001901 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001902 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001903
Ben Blum74a11662011-05-26 16:25:20 -07001904 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001905 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001906 task_unlock(tsk);
1907
1908 /* Update the css_set linked lists if we're using them */
1909 write_lock(&css_set_lock);
1910 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001911 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001912 write_unlock(&css_set_lock);
1913
1914 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001915 * We just gained a reference on old_cset by taking it from the
1916 * task. As trading it for new_cset is protected by cgroup_mutex,
1917 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001918 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001919 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1920 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001921}
1922
Li Zefana043e3b2008-02-23 15:24:09 -08001923/**
Li Zefan081aa452013-03-13 09:17:09 +08001924 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001925 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001926 * @tsk: the task or the leader of the threadgroup to be attached
1927 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001928 *
Tejun Heo257058a2011-12-12 18:12:21 -08001929 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001930 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001932static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1933 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001934{
1935 int retval, i, group_size;
1936 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001937 struct cgroupfs_root *root = cgrp->root;
1938 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001939 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001940 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001941 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001942 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001943
1944 /*
1945 * step 0: in order to do expensive, possibly blocking operations for
1946 * every thread, we cannot iterate the thread group list, since it needs
1947 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001948 * group - group_rwsem prevents new threads from appearing, and if
1949 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Li Zefan081aa452013-03-13 09:17:09 +08001951 if (threadgroup)
1952 group_size = get_nr_threads(tsk);
1953 else
1954 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001955 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001956 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001957 if (!group)
1958 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001959 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001960 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001961 if (retval)
1962 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001963
Ben Blum74a11662011-05-26 16:25:20 -07001964 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001965 /*
1966 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1967 * already PF_EXITING could be freed from underneath us unless we
1968 * take an rcu_read_lock.
1969 */
1970 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001971 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001972 struct task_and_cgroup ent;
1973
Tejun Heocd3d0952011-12-12 18:12:21 -08001974 /* @tsk either already exited or can't exit until the end */
1975 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001976 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001977
Ben Blum74a11662011-05-26 16:25:20 -07001978 /* as per above, nr_threads may decrease, but not increase. */
1979 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001980 ent.task = tsk;
1981 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001982 /* nothing to do if this task is already in the cgroup */
1983 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001984 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001985 /*
1986 * saying GFP_ATOMIC has no effect here because we did prealloc
1987 * earlier, but it's good form to communicate our expectations.
1988 */
Tejun Heo134d3372011-12-12 18:12:21 -08001989 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001990 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001991 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001992 next:
Li Zefan081aa452013-03-13 09:17:09 +08001993 if (!threadgroup)
1994 break;
Ben Blum74a11662011-05-26 16:25:20 -07001995 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001996 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001997 /* remember the number of threads in the array for later. */
1998 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001999 tset.tc_array = group;
2000 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002001
Tejun Heo134d3372011-12-12 18:12:21 -08002002 /* methods shouldn't be called if no task is actually migrating */
2003 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002004 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002005 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002006
Ben Blum74a11662011-05-26 16:25:20 -07002007 /*
2008 * step 1: check that we can legitimately attach to the cgroup.
2009 */
Tejun Heo5549c492013-06-24 15:21:48 -07002010 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002011 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002012
Ben Blum74a11662011-05-26 16:25:20 -07002013 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002014 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002015 if (retval) {
2016 failed_ss = ss;
2017 goto out_cancel_attach;
2018 }
2019 }
Ben Blum74a11662011-05-26 16:25:20 -07002020 }
2021
2022 /*
2023 * step 2: make sure css_sets exist for all threads to be migrated.
2024 * we use find_css_set, which allocates a new one if necessary.
2025 */
Ben Blum74a11662011-05-26 16:25:20 -07002026 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002027 struct css_set *old_cset;
2028
Tejun Heo134d3372011-12-12 18:12:21 -08002029 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002030 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002031 tc->cset = find_css_set(old_cset, cgrp);
2032 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002033 retval = -ENOMEM;
2034 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002035 }
2036 }
2037
2038 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002039 * step 3: now that we're guaranteed success wrt the css_sets,
2040 * proceed to move all tasks to the new cgroup. There are no
2041 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002042 */
Ben Blum74a11662011-05-26 16:25:20 -07002043 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002044 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002045 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002046 }
2047 /* nothing is sensitive to fork() after this point. */
2048
2049 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002050 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002051 */
Tejun Heo5549c492013-06-24 15:21:48 -07002052 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002053 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002054
Ben Blum74a11662011-05-26 16:25:20 -07002055 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002056 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002057 }
2058
2059 /*
2060 * step 5: success! and cleanup
2061 */
Ben Blum74a11662011-05-26 16:25:20 -07002062 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002063out_put_css_set_refs:
2064 if (retval) {
2065 for (i = 0; i < group_size; i++) {
2066 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002067 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002068 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002069 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002070 }
Ben Blum74a11662011-05-26 16:25:20 -07002071 }
2072out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002073 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002074 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002075 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002076
Tejun Heo494c1672011-12-12 18:12:22 -08002077 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002078 break;
Ben Blum74a11662011-05-26 16:25:20 -07002079 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002080 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002081 }
2082 }
Ben Blum74a11662011-05-26 16:25:20 -07002083out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002084 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002085 return retval;
2086}
2087
2088/*
2089 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002090 * function to attach either it or all tasks in its threadgroup. Will lock
2091 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002092 */
2093static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002094{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002095 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002096 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002097 int ret;
2098
Ben Blum74a11662011-05-26 16:25:20 -07002099 if (!cgroup_lock_live_group(cgrp))
2100 return -ENODEV;
2101
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002102retry_find_task:
2103 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002104 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002105 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002106 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002107 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002108 ret= -ESRCH;
2109 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002110 }
Ben Blum74a11662011-05-26 16:25:20 -07002111 /*
2112 * even if we're attaching all tasks in the thread group, we
2113 * only need to check permissions on one of them.
2114 */
David Howellsc69e8d92008-11-14 10:39:19 +11002115 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002116 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2117 !uid_eq(cred->euid, tcred->uid) &&
2118 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002119 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002120 ret = -EACCES;
2121 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002123 } else
2124 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002125
2126 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002127 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002128
2129 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002130 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002131 * trapped in a cpuset, or RT worker may be born in a cgroup
2132 * with no rt_runtime allocated. Just say no.
2133 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002134 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002135 ret = -EINVAL;
2136 rcu_read_unlock();
2137 goto out_unlock_cgroup;
2138 }
2139
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140 get_task_struct(tsk);
2141 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002142
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002143 threadgroup_lock(tsk);
2144 if (threadgroup) {
2145 if (!thread_group_leader(tsk)) {
2146 /*
2147 * a race with de_thread from another thread's exec()
2148 * may strip us of our leadership, if this happens,
2149 * there is no choice but to throw this task away and
2150 * try again; this is
2151 * "double-double-toil-and-trouble-check locking".
2152 */
2153 threadgroup_unlock(tsk);
2154 put_task_struct(tsk);
2155 goto retry_find_task;
2156 }
Li Zefan081aa452013-03-13 09:17:09 +08002157 }
2158
2159 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2160
Tejun Heocd3d0952011-12-12 18:12:21 -08002161 threadgroup_unlock(tsk);
2162
Paul Menagebbcb81d2007-10-18 23:39:32 -07002163 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002164out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002165 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002166 return ret;
2167}
2168
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002169/**
2170 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2171 * @from: attach to all cgroups of a given task
2172 * @tsk: the task to be attached
2173 */
2174int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2175{
2176 struct cgroupfs_root *root;
2177 int retval = 0;
2178
Tejun Heo47cfcd02013-04-07 09:29:51 -07002179 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002180 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002181 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002182
Li Zefan6f4b7e62013-07-31 16:18:36 +08002183 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002184 if (retval)
2185 break;
2186 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002187 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002188
2189 return retval;
2190}
2191EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2192
Tejun Heo182446d2013-08-08 20:11:24 -04002193static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2194 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002195{
Tejun Heo182446d2013-08-08 20:11:24 -04002196 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002197}
2198
Tejun Heo182446d2013-08-08 20:11:24 -04002199static int cgroup_procs_write(struct cgroup_subsys_state *css,
2200 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002201{
Tejun Heo182446d2013-08-08 20:11:24 -04002202 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002203}
2204
Tejun Heo182446d2013-08-08 20:11:24 -04002205static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2206 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002207{
Tejun Heo182446d2013-08-08 20:11:24 -04002208 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002209 if (strlen(buffer) >= PATH_MAX)
2210 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002211 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002212 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002213 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002214 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002215 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002216 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002217 return 0;
2218}
2219
Tejun Heo2da8ca82013-12-05 12:28:04 -05002220static int cgroup_release_agent_show(struct seq_file *seq, void *v)
Paul Menagee788e062008-07-25 01:46:59 -07002221{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002222 struct cgroup *cgrp = seq_css(seq)->cgroup;
Tejun Heo182446d2013-08-08 20:11:24 -04002223
Paul Menagee788e062008-07-25 01:46:59 -07002224 if (!cgroup_lock_live_group(cgrp))
2225 return -ENODEV;
2226 seq_puts(seq, cgrp->root->release_agent_path);
2227 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002228 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002229 return 0;
2230}
2231
Tejun Heo2da8ca82013-12-05 12:28:04 -05002232static int cgroup_sane_behavior_show(struct seq_file *seq, void *v)
Tejun Heo873fe092013-04-14 20:15:26 -07002233{
Tejun Heo2da8ca82013-12-05 12:28:04 -05002234 struct cgroup *cgrp = seq_css(seq)->cgroup;
2235
2236 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002237 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002238}
2239
Paul Menage84eea842008-07-25 01:47:00 -07002240/* A buffer size big enough for numbers or short strings */
2241#define CGROUP_LOCAL_BUFFER_SIZE 64
2242
Tejun Heoa742c592013-12-05 12:28:03 -05002243static ssize_t cgroup_file_write(struct file *file, const char __user *userbuf,
Tejun Heo182446d2013-08-08 20:11:24 -04002244 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002245{
Tejun Heo182446d2013-08-08 20:11:24 -04002246 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002247 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002248 struct cgroup_subsys_state *css = cfe->css;
Tejun Heoa742c592013-12-05 12:28:03 -05002249 size_t max_bytes = cft->max_write_len ?: CGROUP_LOCAL_BUFFER_SIZE - 1;
2250 char *buf;
2251 int ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002252
Tejun Heoa742c592013-12-05 12:28:03 -05002253 if (nbytes >= max_bytes)
2254 return -E2BIG;
2255
2256 buf = kmalloc(nbytes + 1, GFP_KERNEL);
2257 if (!buf)
2258 return -ENOMEM;
2259
2260 if (copy_from_user(buf, userbuf, nbytes)) {
2261 ret = -EFAULT;
2262 goto out_free;
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002263 }
Tejun Heoa742c592013-12-05 12:28:03 -05002264
2265 buf[nbytes] = '\0';
2266
2267 if (cft->write_string) {
2268 ret = cft->write_string(css, cft, strstrip(buf));
2269 } else if (cft->write_u64) {
2270 unsigned long long v;
2271 ret = kstrtoull(buf, 0, &v);
2272 if (!ret)
2273 ret = cft->write_u64(css, cft, v);
2274 } else if (cft->write_s64) {
2275 long long v;
2276 ret = kstrtoll(buf, 0, &v);
2277 if (!ret)
2278 ret = cft->write_s64(css, cft, v);
2279 } else if (cft->trigger) {
2280 ret = cft->trigger(css, (unsigned int)cft->private);
2281 } else {
2282 ret = -EINVAL;
2283 }
2284out_free:
2285 kfree(buf);
2286 return ret ?: nbytes;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002287}
2288
Paul Menage91796562008-04-29 01:00:01 -07002289/*
2290 * seqfile ops/methods for returning structured data. Currently just
2291 * supports string->u64 maps, but can be extended in future.
2292 */
2293
Tejun Heo6612f052013-12-05 12:28:04 -05002294static void *cgroup_seqfile_start(struct seq_file *seq, loff_t *ppos)
2295{
2296 struct cftype *cft = seq_cft(seq);
2297
2298 if (cft->seq_start) {
2299 return cft->seq_start(seq, ppos);
2300 } else {
2301 /*
2302 * The same behavior and code as single_open(). Returns
2303 * !NULL if pos is at the beginning; otherwise, NULL.
2304 */
2305 return NULL + !*ppos;
2306 }
2307}
2308
2309static void *cgroup_seqfile_next(struct seq_file *seq, void *v, loff_t *ppos)
2310{
2311 struct cftype *cft = seq_cft(seq);
2312
2313 if (cft->seq_next) {
2314 return cft->seq_next(seq, v, ppos);
2315 } else {
2316 /*
2317 * The same behavior and code as single_open(), always
2318 * terminate after the initial read.
2319 */
2320 ++*ppos;
2321 return NULL;
2322 }
2323}
2324
2325static void cgroup_seqfile_stop(struct seq_file *seq, void *v)
2326{
2327 struct cftype *cft = seq_cft(seq);
2328
2329 if (cft->seq_stop)
2330 cft->seq_stop(seq, v);
2331}
2332
Paul Menage91796562008-04-29 01:00:01 -07002333static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2334{
Tejun Heo7da11272013-12-05 12:28:04 -05002335 struct cftype *cft = seq_cft(m);
2336 struct cgroup_subsys_state *css = seq_css(m);
Li Zefane0798ce2013-07-31 17:36:25 +08002337
Tejun Heo2da8ca82013-12-05 12:28:04 -05002338 if (cft->seq_show)
2339 return cft->seq_show(m, arg);
Paul Menage91796562008-04-29 01:00:01 -07002340
Tejun Heo896f5192013-12-05 12:28:04 -05002341 if (cft->read_u64)
2342 seq_printf(m, "%llu\n", cft->read_u64(css, cft));
2343 else if (cft->read_s64)
2344 seq_printf(m, "%lld\n", cft->read_s64(css, cft));
2345 else
2346 return -EINVAL;
2347 return 0;
2348}
Paul Menage91796562008-04-29 01:00:01 -07002349
Tejun Heo6612f052013-12-05 12:28:04 -05002350static struct seq_operations cgroup_seq_operations = {
2351 .start = cgroup_seqfile_start,
2352 .next = cgroup_seqfile_next,
2353 .stop = cgroup_seqfile_stop,
2354 .show = cgroup_seqfile_show,
2355};
2356
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357static int cgroup_file_open(struct inode *inode, struct file *file)
2358{
Tejun Heof7d58812013-08-08 20:11:23 -04002359 struct cfent *cfe = __d_cfe(file->f_dentry);
2360 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002361 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2362 struct cgroup_subsys_state *css;
Tejun Heo6612f052013-12-05 12:28:04 -05002363 struct cgroup_open_file *of;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002364 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002365
2366 err = generic_file_open(inode, file);
2367 if (err)
2368 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002369
2370 /*
2371 * If the file belongs to a subsystem, pin the css. Will be
2372 * unpinned either on open failure or release. This ensures that
2373 * @css stays alive for all file operations.
2374 */
Tejun Heo105347b2013-08-13 11:01:55 -04002375 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002376 css = cgroup_css(cgrp, cft->ss);
2377 if (cft->ss && !css_tryget(css))
2378 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002379 rcu_read_unlock();
2380
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002381 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002382 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002383
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002384 /*
2385 * @cfe->css is used by read/write/close to determine the
2386 * associated css. @file->private_data would be a better place but
2387 * that's already used by seqfile. Multiple accessors may use it
2388 * simultaneously which is okay as the association never changes.
2389 */
2390 WARN_ON_ONCE(cfe->css && cfe->css != css);
2391 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392
Tejun Heo6612f052013-12-05 12:28:04 -05002393 of = __seq_open_private(file, &cgroup_seq_operations,
2394 sizeof(struct cgroup_open_file));
2395 if (of) {
2396 of->cfe = cfe;
2397 return 0;
Tejun Heo7da11272013-12-05 12:28:04 -05002398 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399
Tejun Heo6612f052013-12-05 12:28:04 -05002400 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002401 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002402 return -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403}
2404
2405static int cgroup_file_release(struct inode *inode, struct file *file)
2406{
Tejun Heof7d58812013-08-08 20:11:23 -04002407 struct cfent *cfe = __d_cfe(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002408 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002409
Tejun Heo67f4c362013-08-08 20:11:24 -04002410 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002411 css_put(css);
Tejun Heo6612f052013-12-05 12:28:04 -05002412 return seq_release_private(inode, file);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413}
2414
2415/*
2416 * cgroup_rename - Only allow simple rename of directories in place.
2417 */
2418static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2419 struct inode *new_dir, struct dentry *new_dentry)
2420{
Li Zefan65dff752013-03-01 15:01:56 +08002421 int ret;
2422 struct cgroup_name *name, *old_name;
2423 struct cgroup *cgrp;
2424
2425 /*
2426 * It's convinient to use parent dir's i_mutex to protected
2427 * cgrp->name.
2428 */
2429 lockdep_assert_held(&old_dir->i_mutex);
2430
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2432 return -ENOTDIR;
2433 if (new_dentry->d_inode)
2434 return -EEXIST;
2435 if (old_dir != new_dir)
2436 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002437
2438 cgrp = __d_cgrp(old_dentry);
2439
Tejun Heo6db8e852013-06-14 11:18:22 -07002440 /*
2441 * This isn't a proper migration and its usefulness is very
2442 * limited. Disallow if sane_behavior.
2443 */
2444 if (cgroup_sane_behavior(cgrp))
2445 return -EPERM;
2446
Li Zefan65dff752013-03-01 15:01:56 +08002447 name = cgroup_alloc_name(new_dentry);
2448 if (!name)
2449 return -ENOMEM;
2450
2451 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2452 if (ret) {
2453 kfree(name);
2454 return ret;
2455 }
2456
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002457 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002458 rcu_assign_pointer(cgrp->name, name);
2459
2460 kfree_rcu(old_name, rcu_head);
2461 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002462}
2463
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002464static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2465{
2466 if (S_ISDIR(dentry->d_inode->i_mode))
2467 return &__d_cgrp(dentry)->xattrs;
2468 else
Li Zefan712317a2013-04-18 23:09:52 -07002469 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002470}
2471
2472static inline int xattr_enabled(struct dentry *dentry)
2473{
2474 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002475 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002476}
2477
2478static bool is_valid_xattr(const char *name)
2479{
2480 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2481 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2482 return true;
2483 return false;
2484}
2485
2486static int cgroup_setxattr(struct dentry *dentry, const char *name,
2487 const void *val, size_t size, int flags)
2488{
2489 if (!xattr_enabled(dentry))
2490 return -EOPNOTSUPP;
2491 if (!is_valid_xattr(name))
2492 return -EINVAL;
2493 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2494}
2495
2496static int cgroup_removexattr(struct dentry *dentry, const char *name)
2497{
2498 if (!xattr_enabled(dentry))
2499 return -EOPNOTSUPP;
2500 if (!is_valid_xattr(name))
2501 return -EINVAL;
2502 return simple_xattr_remove(__d_xattrs(dentry), name);
2503}
2504
2505static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2506 void *buf, size_t size)
2507{
2508 if (!xattr_enabled(dentry))
2509 return -EOPNOTSUPP;
2510 if (!is_valid_xattr(name))
2511 return -EINVAL;
2512 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2513}
2514
2515static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2516{
2517 if (!xattr_enabled(dentry))
2518 return -EOPNOTSUPP;
2519 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2520}
2521
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002522static const struct file_operations cgroup_file_operations = {
Tejun Heo896f5192013-12-05 12:28:04 -05002523 .read = seq_read,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002524 .write = cgroup_file_write,
2525 .llseek = generic_file_llseek,
2526 .open = cgroup_file_open,
2527 .release = cgroup_file_release,
2528};
2529
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002530static const struct inode_operations cgroup_file_inode_operations = {
2531 .setxattr = cgroup_setxattr,
2532 .getxattr = cgroup_getxattr,
2533 .listxattr = cgroup_listxattr,
2534 .removexattr = cgroup_removexattr,
2535};
2536
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002537static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002538 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002539 .mkdir = cgroup_mkdir,
2540 .rmdir = cgroup_rmdir,
2541 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002542 .setxattr = cgroup_setxattr,
2543 .getxattr = cgroup_getxattr,
2544 .listxattr = cgroup_listxattr,
2545 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546};
2547
Al Viroa5e7ed32011-07-26 01:55:55 -04002548static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002549 struct super_block *sb)
2550{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002551 struct inode *inode;
2552
2553 if (!dentry)
2554 return -ENOENT;
2555 if (dentry->d_inode)
2556 return -EEXIST;
2557
2558 inode = cgroup_new_inode(mode, sb);
2559 if (!inode)
2560 return -ENOMEM;
2561
2562 if (S_ISDIR(mode)) {
2563 inode->i_op = &cgroup_dir_inode_operations;
2564 inode->i_fop = &simple_dir_operations;
2565
2566 /* start off with i_nlink == 2 (for "." entry) */
2567 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002568 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002569
Tejun Heob8a2df62012-11-19 08:13:37 -08002570 /*
2571 * Control reaches here with cgroup_mutex held.
2572 * @inode->i_mutex should nest outside cgroup_mutex but we
2573 * want to populate it immediately without releasing
2574 * cgroup_mutex. As @inode isn't visible to anyone else
2575 * yet, trylock will always succeed without affecting
2576 * lockdep checks.
2577 */
2578 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002579 } else if (S_ISREG(mode)) {
2580 inode->i_size = 0;
2581 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002582 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002583 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002584 d_instantiate(dentry, inode);
2585 dget(dentry); /* Extra count - pin the dentry in core */
2586 return 0;
2587}
2588
Li Zefan099fca32009-04-02 16:57:29 -07002589/**
2590 * cgroup_file_mode - deduce file mode of a control file
2591 * @cft: the control file in question
2592 *
2593 * returns cft->mode if ->mode is not 0
2594 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2595 * returns S_IRUGO if it has only a read handler
2596 * returns S_IWUSR if it has only a write hander
2597 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002598static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002599{
Al Viroa5e7ed32011-07-26 01:55:55 -04002600 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002601
2602 if (cft->mode)
2603 return cft->mode;
2604
Tejun Heo2da8ca82013-12-05 12:28:04 -05002605 if (cft->read_u64 || cft->read_s64 || cft->seq_show)
Li Zefan099fca32009-04-02 16:57:29 -07002606 mode |= S_IRUGO;
2607
Tejun Heo6e0755b2013-12-05 12:28:03 -05002608 if (cft->write_u64 || cft->write_s64 || cft->write_string ||
2609 cft->trigger)
Li Zefan099fca32009-04-02 16:57:29 -07002610 mode |= S_IWUSR;
2611
2612 return mode;
2613}
2614
Tejun Heo2bb566c2013-08-08 20:11:23 -04002615static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616{
Paul Menagebd89aab2007-10-18 23:40:44 -07002617 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002618 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002620 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002621 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002622 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002624
Tejun Heo9fa4db32013-08-26 18:40:56 -04002625 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2626 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002627 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628 strcat(name, ".");
2629 }
2630 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002631
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002633
2634 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2635 if (!cfe)
2636 return -ENOMEM;
2637
Paul Menageddbcc7e2007-10-18 23:39:30 -07002638 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002639 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002641 goto out;
2642 }
2643
Li Zefand6cbf352013-05-14 19:44:20 +08002644 cfe->type = (void *)cft;
2645 cfe->dentry = dentry;
2646 dentry->d_fsdata = cfe;
2647 simple_xattrs_init(&cfe->xattrs);
2648
Tejun Heo05ef1d72012-04-01 12:09:56 -07002649 mode = cgroup_file_mode(cft);
2650 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2651 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002652 list_add_tail(&cfe->node, &parent->files);
2653 cfe = NULL;
2654 }
2655 dput(dentry);
2656out:
2657 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 return error;
2659}
2660
Tejun Heob1f28d32013-06-28 16:24:10 -07002661/**
2662 * cgroup_addrm_files - add or remove files to a cgroup directory
2663 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002664 * @cfts: array of cftypes to be added
2665 * @is_add: whether to add or remove
2666 *
2667 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002668 * For removals, this function never fails. If addition fails, this
2669 * function doesn't remove files already added. The caller is responsible
2670 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002671 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002672static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2673 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002675 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002676 int ret;
2677
2678 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2679 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002680
2681 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002682 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002683 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2684 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002685 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2686 continue;
2687 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2688 continue;
2689
Li Zefan2739d3c2013-01-21 18:18:33 +08002690 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002691 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002692 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002693 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002694 cft->name, ret);
2695 return ret;
2696 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002697 } else {
2698 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002699 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002700 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002701 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702}
2703
Tejun Heo8e3f6542012-04-01 12:09:55 -07002704static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002705 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002706{
2707 /*
2708 * Thanks to the entanglement with vfs inode locking, we can't walk
2709 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002710 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2711 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002712 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002713 mutex_lock(&cgroup_mutex);
2714}
2715
Tejun Heo2bb566c2013-08-08 20:11:23 -04002716static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002717 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002718{
2719 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002720 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002721 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002722 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002723 struct dentry *prev = NULL;
2724 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002725 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002726 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002727 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002728
2729 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002730 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002731 !atomic_inc_not_zero(&sb->s_active)) {
2732 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002733 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002734 }
2735
Li Zefane8c82d22013-06-18 18:48:37 +08002736 /*
2737 * All cgroups which are created after we drop cgroup_mutex will
2738 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002739 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002740 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002741 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002742
Tejun Heo8e3f6542012-04-01 12:09:55 -07002743 mutex_unlock(&cgroup_mutex);
2744
Li Zefane8c82d22013-06-18 18:48:37 +08002745 /* add/rm files for all cgroups created before */
2746 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002747 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002748 struct cgroup *cgrp = css->cgroup;
2749
Li Zefane8c82d22013-06-18 18:48:37 +08002750 if (cgroup_is_dead(cgrp))
2751 continue;
2752
2753 inode = cgrp->dentry->d_inode;
2754 dget(cgrp->dentry);
2755 rcu_read_unlock();
2756
2757 dput(prev);
2758 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002759
2760 mutex_lock(&inode->i_mutex);
2761 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002762 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002763 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002764 mutex_unlock(&cgroup_mutex);
2765 mutex_unlock(&inode->i_mutex);
2766
Li Zefane8c82d22013-06-18 18:48:37 +08002767 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002768 if (ret)
2769 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002770 }
Li Zefane8c82d22013-06-18 18:48:37 +08002771 rcu_read_unlock();
2772 dput(prev);
2773 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002774 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002775}
2776
2777/**
2778 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2779 * @ss: target cgroup subsystem
2780 * @cfts: zero-length name terminated array of cftypes
2781 *
2782 * Register @cfts to @ss. Files described by @cfts are created for all
2783 * existing cgroups to which @ss is attached and all future cgroups will
2784 * have them too. This function can be called anytime whether @ss is
2785 * attached or not.
2786 *
2787 * Returns 0 on successful registration, -errno on failure. Note that this
2788 * function currently returns 0 as long as @cfts registration is successful
2789 * even if some file creation attempts on existing cgroups fail.
2790 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002791int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002792{
2793 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002794 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002795 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796
2797 set = kzalloc(sizeof(*set), GFP_KERNEL);
2798 if (!set)
2799 return -ENOMEM;
2800
Tejun Heo2bb566c2013-08-08 20:11:23 -04002801 for (cft = cfts; cft->name[0] != '\0'; cft++)
2802 cft->ss = ss;
2803
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804 cgroup_cfts_prepare();
2805 set->cfts = cfts;
2806 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002807 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002808 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002809 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002810 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002811}
2812EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2813
Li Zefana043e3b2008-02-23 15:24:09 -08002814/**
Tejun Heo79578622012-04-01 12:09:56 -07002815 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002816 * @cfts: zero-length name terminated array of cftypes
2817 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002818 * Unregister @cfts. Files described by @cfts are removed from all
2819 * existing cgroups and all future cgroups won't have them either. This
2820 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002821 *
2822 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002823 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002824 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002825int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002826{
2827 struct cftype_set *set;
2828
Tejun Heo2bb566c2013-08-08 20:11:23 -04002829 if (!cfts || !cfts[0].ss)
2830 return -ENOENT;
2831
Tejun Heo79578622012-04-01 12:09:56 -07002832 cgroup_cfts_prepare();
2833
Tejun Heo2bb566c2013-08-08 20:11:23 -04002834 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002835 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002836 list_del(&set->node);
2837 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002838 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002839 return 0;
2840 }
2841 }
2842
Tejun Heo2bb566c2013-08-08 20:11:23 -04002843 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002844 return -ENOENT;
2845}
2846
2847/**
Li Zefana043e3b2008-02-23 15:24:09 -08002848 * cgroup_task_count - count the number of tasks in a cgroup.
2849 * @cgrp: the cgroup in question
2850 *
2851 * Return the number of tasks in the cgroup.
2852 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002853int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002854{
2855 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002856 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002857
Paul Menage817929e2007-10-18 23:39:36 -07002858 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002859 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2860 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002861 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002862 return count;
2863}
2864
2865/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002866 * To reduce the fork() overhead for systems that are not actually using
2867 * their cgroups capability, we don't maintain the lists running through
2868 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002869 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002870 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002871static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002872{
2873 struct task_struct *p, *g;
2874 write_lock(&css_set_lock);
2875 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002876 /*
2877 * We need tasklist_lock because RCU is not safe against
2878 * while_each_thread(). Besides, a forking task that has passed
2879 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2880 * is not guaranteed to have its child immediately visible in the
2881 * tasklist if we walk through it with RCU.
2882 */
2883 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002884 do_each_thread(g, p) {
2885 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002886 /*
2887 * We should check if the process is exiting, otherwise
2888 * it will race with cgroup_exit() in that the list
2889 * entry won't be deleted though the process has exited.
2890 */
2891 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002892 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002893 task_unlock(p);
2894 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002895 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002896 write_unlock(&css_set_lock);
2897}
2898
Tejun Heo574bd9f2012-11-09 09:12:29 -08002899/**
Tejun Heo492eb212013-08-08 20:11:25 -04002900 * css_next_child - find the next child of a given css
2901 * @pos_css: the current position (%NULL to initiate traversal)
2902 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002903 *
Tejun Heo492eb212013-08-08 20:11:25 -04002904 * This function returns the next child of @parent_css and should be called
Tejun Heo87fb54f2013-12-06 15:11:55 -05002905 * under either cgroup_mutex or RCU read lock. The only requirement is
2906 * that @parent_css and @pos_css are accessible. The next sibling is
2907 * guaranteed to be returned regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002908 */
Tejun Heo492eb212013-08-08 20:11:25 -04002909struct cgroup_subsys_state *
2910css_next_child(struct cgroup_subsys_state *pos_css,
2911 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002912{
Tejun Heo492eb212013-08-08 20:11:25 -04002913 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2914 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002915 struct cgroup *next;
2916
Tejun Heo87fb54f2013-12-06 15:11:55 -05002917 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo53fa5262013-05-24 10:55:38 +09002918
2919 /*
2920 * @pos could already have been removed. Once a cgroup is removed,
2921 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002922 * changes. As CGRP_DEAD assertion is serialized and happens
2923 * before the cgroup is taken off the ->sibling list, if we see it
2924 * unasserted, it's guaranteed that the next sibling hasn't
2925 * finished its grace period even if it's already removed, and thus
2926 * safe to dereference from this RCU critical section. If
2927 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2928 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002929 *
2930 * If @pos is dead, its next pointer can't be dereferenced;
2931 * however, as each cgroup is given a monotonically increasing
2932 * unique serial number and always appended to the sibling list,
2933 * the next one can be found by walking the parent's children until
2934 * we see a cgroup with higher serial number than @pos's. While
2935 * this path can be slower, it's taken only when either the current
2936 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002937 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002938 if (!pos) {
2939 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2940 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002941 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002942 } else {
2943 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2944 if (next->serial_nr > pos->serial_nr)
2945 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002946 }
2947
Tejun Heo492eb212013-08-08 20:11:25 -04002948 if (&next->sibling == &cgrp->children)
2949 return NULL;
2950
Tejun Heoca8bdca2013-08-26 18:40:56 -04002951 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09002952}
Tejun Heo492eb212013-08-08 20:11:25 -04002953EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09002954
2955/**
Tejun Heo492eb212013-08-08 20:11:25 -04002956 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002957 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04002958 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08002959 *
Tejun Heo492eb212013-08-08 20:11:25 -04002960 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04002961 * to visit for pre-order traversal of @root's descendants. @root is
2962 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09002963 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05002964 * While this function requires cgroup_mutex or RCU read locking, it
2965 * doesn't require the whole traversal to be contained in a single critical
2966 * section. This function will return the correct next descendant as long
2967 * as both @pos and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08002968 */
Tejun Heo492eb212013-08-08 20:11:25 -04002969struct cgroup_subsys_state *
2970css_next_descendant_pre(struct cgroup_subsys_state *pos,
2971 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002972{
Tejun Heo492eb212013-08-08 20:11:25 -04002973 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002974
Tejun Heo87fb54f2013-12-06 15:11:55 -05002975 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08002976
Tejun Heobd8815a2013-08-08 20:11:27 -04002977 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09002978 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04002979 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08002980
2981 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04002982 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002983 if (next)
2984 return next;
2985
2986 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04002987 while (pos != root) {
2988 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09002989 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08002990 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04002991 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09002992 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08002993
2994 return NULL;
2995}
Tejun Heo492eb212013-08-08 20:11:25 -04002996EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08002997
Tejun Heo12a9d2f2013-01-07 08:49:33 -08002998/**
Tejun Heo492eb212013-08-08 20:11:25 -04002999 * css_rightmost_descendant - return the rightmost descendant of a css
3000 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003001 *
Tejun Heo492eb212013-08-08 20:11:25 -04003002 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3003 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003004 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003005 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003006 * While this function requires cgroup_mutex or RCU read locking, it
3007 * doesn't require the whole traversal to be contained in a single critical
3008 * section. This function will return the correct rightmost descendant as
3009 * long as @pos is accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003010 */
Tejun Heo492eb212013-08-08 20:11:25 -04003011struct cgroup_subsys_state *
3012css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003013{
Tejun Heo492eb212013-08-08 20:11:25 -04003014 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003015
Tejun Heo87fb54f2013-12-06 15:11:55 -05003016 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003017
3018 do {
3019 last = pos;
3020 /* ->prev isn't RCU safe, walk ->next till the end */
3021 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003022 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003023 pos = tmp;
3024 } while (pos);
3025
3026 return last;
3027}
Tejun Heo492eb212013-08-08 20:11:25 -04003028EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003029
Tejun Heo492eb212013-08-08 20:11:25 -04003030static struct cgroup_subsys_state *
3031css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003032{
Tejun Heo492eb212013-08-08 20:11:25 -04003033 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003034
3035 do {
3036 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003037 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003038 } while (pos);
3039
3040 return last;
3041}
3042
3043/**
Tejun Heo492eb212013-08-08 20:11:25 -04003044 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003045 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003046 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003047 *
Tejun Heo492eb212013-08-08 20:11:25 -04003048 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003049 * to visit for post-order traversal of @root's descendants. @root is
3050 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003051 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05003052 * While this function requires cgroup_mutex or RCU read locking, it
3053 * doesn't require the whole traversal to be contained in a single critical
3054 * section. This function will return the correct next descendant as long
3055 * as both @pos and @cgroup are accessible and @pos is a descendant of
3056 * @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057 */
Tejun Heo492eb212013-08-08 20:11:25 -04003058struct cgroup_subsys_state *
3059css_next_descendant_post(struct cgroup_subsys_state *pos,
3060 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061{
Tejun Heo492eb212013-08-08 20:11:25 -04003062 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063
Tejun Heo87fb54f2013-12-06 15:11:55 -05003064 cgroup_assert_mutex_or_rcu_locked();
Tejun Heo574bd9f2012-11-09 09:12:29 -08003065
Tejun Heo58b79a92013-09-06 15:31:08 -04003066 /* if first iteration, visit leftmost descendant which may be @root */
3067 if (!pos)
3068 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003069
Tejun Heobd8815a2013-08-08 20:11:27 -04003070 /* if we visited @root, we're done */
3071 if (pos == root)
3072 return NULL;
3073
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003075 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003076 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003077 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078
3079 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003080 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081}
Tejun Heo492eb212013-08-08 20:11:25 -04003082EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003083
Tejun Heo0942eee2013-08-08 20:11:26 -04003084/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003085 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003086 * @it: the iterator to advance
3087 *
3088 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003089 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003090static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003091{
3092 struct list_head *l = it->cset_link;
3093 struct cgrp_cset_link *link;
3094 struct css_set *cset;
3095
3096 /* Advance to the next non-empty css_set */
3097 do {
3098 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003099 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003100 it->cset_link = NULL;
3101 return;
3102 }
3103 link = list_entry(l, struct cgrp_cset_link, cset_link);
3104 cset = link->cset;
3105 } while (list_empty(&cset->tasks));
3106 it->cset_link = l;
3107 it->task = cset->tasks.next;
3108}
3109
Tejun Heo0942eee2013-08-08 20:11:26 -04003110/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003111 * css_task_iter_start - initiate task iteration
3112 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003113 * @it: the task iterator to use
3114 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003115 * Initiate iteration through the tasks of @css. The caller can call
3116 * css_task_iter_next() to walk through the tasks until the function
3117 * returns NULL. On completion of iteration, css_task_iter_end() must be
3118 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003119 *
3120 * Note that this function acquires a lock which is released when the
3121 * iteration finishes. The caller can't sleep while iteration is in
3122 * progress.
3123 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003124void css_task_iter_start(struct cgroup_subsys_state *css,
3125 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003126 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003127{
3128 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003129 * The first time anyone tries to iterate across a css, we need to
3130 * enable the list linking each css_set to its tasks, and fix up
3131 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003132 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003133 if (!use_task_css_set_links)
3134 cgroup_enable_task_cg_lists();
3135
Paul Menage817929e2007-10-18 23:39:36 -07003136 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003137
Tejun Heo72ec7022013-08-08 20:11:26 -04003138 it->origin_css = css;
3139 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003140
Tejun Heo72ec7022013-08-08 20:11:26 -04003141 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003142}
3143
Tejun Heo0942eee2013-08-08 20:11:26 -04003144/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003145 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003146 * @it: the task iterator being iterated
3147 *
3148 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003149 * initialized via css_task_iter_start(). Returns NULL when the iteration
3150 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003151 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003152struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003153{
3154 struct task_struct *res;
3155 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003156 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003157
3158 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003159 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003160 return NULL;
3161 res = list_entry(l, struct task_struct, cg_list);
3162 /* Advance iterator to find next entry */
3163 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003164 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3165 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003166 /*
3167 * We reached the end of this task list - move on to the
3168 * next cgrp_cset_link.
3169 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003170 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003171 } else {
3172 it->task = l;
3173 }
3174 return res;
3175}
3176
Tejun Heo0942eee2013-08-08 20:11:26 -04003177/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003178 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003179 * @it: the task iterator to finish
3180 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003181 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003182 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003183void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003184 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003185{
3186 read_unlock(&css_set_lock);
3187}
3188
Cliff Wickman31a7df02008-02-07 00:14:42 -08003189static inline int started_after_time(struct task_struct *t1,
3190 struct timespec *time,
3191 struct task_struct *t2)
3192{
3193 int start_diff = timespec_compare(&t1->start_time, time);
3194 if (start_diff > 0) {
3195 return 1;
3196 } else if (start_diff < 0) {
3197 return 0;
3198 } else {
3199 /*
3200 * Arbitrarily, if two processes started at the same
3201 * time, we'll say that the lower pointer value
3202 * started first. Note that t2 may have exited by now
3203 * so this may not be a valid pointer any longer, but
3204 * that's fine - it still serves to distinguish
3205 * between two tasks started (effectively) simultaneously.
3206 */
3207 return t1 > t2;
3208 }
3209}
3210
3211/*
3212 * This function is a callback from heap_insert() and is used to order
3213 * the heap.
3214 * In this case we order the heap in descending task start time.
3215 */
3216static inline int started_after(void *p1, void *p2)
3217{
3218 struct task_struct *t1 = p1;
3219 struct task_struct *t2 = p2;
3220 return started_after_time(t1, &t2->start_time, t2);
3221}
3222
3223/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003224 * css_scan_tasks - iterate though all the tasks in a css
3225 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003226 * @test: optional test callback
3227 * @process: process callback
3228 * @data: data passed to @test and @process
3229 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003230 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003231 * Iterate through all the tasks in @css, calling @test for each, and if it
3232 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003233 *
Tejun Heoe5358372013-08-08 20:11:26 -04003234 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003235 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003236 * lock css_set_lock for the call to @process.
3237 *
3238 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003239 * of @css for the duration of this call. This function may or may not
3240 * call @process for tasks that exit or move to a different css during the
3241 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003242 *
3243 * Note that @test may be called with locks held, and may in some
3244 * situations be called multiple times for the same task, so it should be
3245 * cheap.
3246 *
3247 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3248 * heap operations (and its "gt" member will be overwritten), else a
3249 * temporary heap will be used (allocation of which may cause this function
3250 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003251 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003252int css_scan_tasks(struct cgroup_subsys_state *css,
3253 bool (*test)(struct task_struct *, void *),
3254 void (*process)(struct task_struct *, void *),
3255 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003256{
3257 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003258 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003259 struct task_struct *p, *dropped;
3260 /* Never dereference latest_task, since it's not refcounted */
3261 struct task_struct *latest_task = NULL;
3262 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003263 struct timespec latest_time = { 0, 0 };
3264
Tejun Heoe5358372013-08-08 20:11:26 -04003265 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003266 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003267 heap->gt = &started_after;
3268 } else {
3269 /* We need to allocate our own heap memory */
3270 heap = &tmp_heap;
3271 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3272 if (retval)
3273 /* cannot allocate the heap */
3274 return retval;
3275 }
3276
3277 again:
3278 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003279 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003280 * which are of interest, and invoking @process callback on the
3281 * ones which need an update. Since we don't want to hold any
3282 * locks during the task updates, gather tasks to be processed in a
3283 * heap structure. The heap is sorted by descending task start
3284 * time. If the statically-sized heap fills up, we overflow tasks
3285 * that started later, and in future iterations only consider tasks
3286 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003287 * guarantees forward progress and that we don't miss any tasks.
3288 */
3289 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003290 css_task_iter_start(css, &it);
3291 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003292 /*
3293 * Only affect tasks that qualify per the caller's callback,
3294 * if he provided one
3295 */
Tejun Heoe5358372013-08-08 20:11:26 -04003296 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003297 continue;
3298 /*
3299 * Only process tasks that started after the last task
3300 * we processed
3301 */
3302 if (!started_after_time(p, &latest_time, latest_task))
3303 continue;
3304 dropped = heap_insert(heap, p);
3305 if (dropped == NULL) {
3306 /*
3307 * The new task was inserted; the heap wasn't
3308 * previously full
3309 */
3310 get_task_struct(p);
3311 } else if (dropped != p) {
3312 /*
3313 * The new task was inserted, and pushed out a
3314 * different task
3315 */
3316 get_task_struct(p);
3317 put_task_struct(dropped);
3318 }
3319 /*
3320 * Else the new task was newer than anything already in
3321 * the heap and wasn't inserted
3322 */
3323 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003324 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003325
3326 if (heap->size) {
3327 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003328 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003329 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003330 latest_time = q->start_time;
3331 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003332 }
3333 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003334 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003335 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003336 }
3337 /*
3338 * If we had to process any tasks at all, scan again
3339 * in case some of them were in the middle of forking
3340 * children that didn't get processed.
3341 * Not the most efficient way to do it, but it avoids
3342 * having to take callback_mutex in the fork path
3343 */
3344 goto again;
3345 }
3346 if (heap == &tmp_heap)
3347 heap_free(&tmp_heap);
3348 return 0;
3349}
3350
Tejun Heoe5358372013-08-08 20:11:26 -04003351static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003352{
Tejun Heoe5358372013-08-08 20:11:26 -04003353 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003354
Tejun Heo47cfcd02013-04-07 09:29:51 -07003355 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003356 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003357 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003358}
3359
3360/**
3361 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3362 * @to: cgroup to which the tasks will be moved
3363 * @from: cgroup in which the tasks currently reside
3364 */
3365int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3366{
Tejun Heo72ec7022013-08-08 20:11:26 -04003367 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3368 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003369}
3370
Paul Menage817929e2007-10-18 23:39:36 -07003371/*
Ben Blum102a7752009-09-23 15:56:26 -07003372 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003373 *
3374 * Reading this file can return large amounts of data if a cgroup has
3375 * *lots* of attached tasks. So it may need several calls to read(),
3376 * but we cannot guarantee that the information we produce is correct
3377 * unless we produce it entirely atomically.
3378 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003379 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003380
Li Zefan24528252012-01-20 11:58:43 +08003381/* which pidlist file are we talking about? */
3382enum cgroup_filetype {
3383 CGROUP_FILE_PROCS,
3384 CGROUP_FILE_TASKS,
3385};
3386
3387/*
3388 * A pidlist is a list of pids that virtually represents the contents of one
3389 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3390 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3391 * to the cgroup.
3392 */
3393struct cgroup_pidlist {
3394 /*
3395 * used to find which pidlist is wanted. doesn't change as long as
3396 * this particular list stays in the list.
3397 */
3398 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3399 /* array of xids */
3400 pid_t *list;
3401 /* how many elements the above list has */
3402 int length;
Li Zefan24528252012-01-20 11:58:43 +08003403 /* each of these stored in a list by its cgroup */
3404 struct list_head links;
3405 /* pointer to the cgroup we belong to, for list removal purposes */
3406 struct cgroup *owner;
Tejun Heob1a21362013-11-29 10:42:58 -05003407 /* for delayed destruction */
3408 struct delayed_work destroy_dwork;
Li Zefan24528252012-01-20 11:58:43 +08003409};
3410
Paul Menagebbcb81d2007-10-18 23:39:32 -07003411/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003412 * The following two functions "fix" the issue where there are more pids
3413 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3414 * TODO: replace with a kernel-wide solution to this problem
3415 */
3416#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3417static void *pidlist_allocate(int count)
3418{
3419 if (PIDLIST_TOO_LARGE(count))
3420 return vmalloc(count * sizeof(pid_t));
3421 else
3422 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3423}
Tejun Heob1a21362013-11-29 10:42:58 -05003424
Ben Blumd1d9fd32009-09-23 15:56:28 -07003425static void pidlist_free(void *p)
3426{
3427 if (is_vmalloc_addr(p))
3428 vfree(p);
3429 else
3430 kfree(p);
3431}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003432
3433/*
Tejun Heob1a21362013-11-29 10:42:58 -05003434 * Used to destroy all pidlists lingering waiting for destroy timer. None
3435 * should be left afterwards.
3436 */
3437static void cgroup_pidlist_destroy_all(struct cgroup *cgrp)
3438{
3439 struct cgroup_pidlist *l, *tmp_l;
3440
3441 mutex_lock(&cgrp->pidlist_mutex);
3442 list_for_each_entry_safe(l, tmp_l, &cgrp->pidlists, links)
3443 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork, 0);
3444 mutex_unlock(&cgrp->pidlist_mutex);
3445
3446 flush_workqueue(cgroup_pidlist_destroy_wq);
3447 BUG_ON(!list_empty(&cgrp->pidlists));
3448}
3449
3450static void cgroup_pidlist_destroy_work_fn(struct work_struct *work)
3451{
3452 struct delayed_work *dwork = to_delayed_work(work);
3453 struct cgroup_pidlist *l = container_of(dwork, struct cgroup_pidlist,
3454 destroy_dwork);
3455 struct cgroup_pidlist *tofree = NULL;
3456
3457 mutex_lock(&l->owner->pidlist_mutex);
Tejun Heob1a21362013-11-29 10:42:58 -05003458
3459 /*
Tejun Heo04502362013-11-29 10:42:59 -05003460 * Destroy iff we didn't get queued again. The state won't change
3461 * as destroy_dwork can only be queued while locked.
Tejun Heob1a21362013-11-29 10:42:58 -05003462 */
Tejun Heo04502362013-11-29 10:42:59 -05003463 if (!delayed_work_pending(dwork)) {
Tejun Heob1a21362013-11-29 10:42:58 -05003464 list_del(&l->links);
3465 pidlist_free(l->list);
3466 put_pid_ns(l->key.ns);
3467 tofree = l;
3468 }
3469
Tejun Heob1a21362013-11-29 10:42:58 -05003470 mutex_unlock(&l->owner->pidlist_mutex);
3471 kfree(tofree);
3472}
3473
3474/*
Ben Blum102a7752009-09-23 15:56:26 -07003475 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003476 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003477 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003478static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003479{
Ben Blum102a7752009-09-23 15:56:26 -07003480 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003481
3482 /*
3483 * we presume the 0th element is unique, so i starts at 1. trivial
3484 * edge cases first; no work needs to be done for either
3485 */
3486 if (length == 0 || length == 1)
3487 return length;
3488 /* src and dest walk down the list; dest counts unique elements */
3489 for (src = 1; src < length; src++) {
3490 /* find next unique element */
3491 while (list[src] == list[src-1]) {
3492 src++;
3493 if (src == length)
3494 goto after;
3495 }
3496 /* dest always points to where the next unique element goes */
3497 list[dest] = list[src];
3498 dest++;
3499 }
3500after:
Ben Blum102a7752009-09-23 15:56:26 -07003501 return dest;
3502}
3503
Tejun Heoafb2bc12013-11-29 10:42:59 -05003504/*
3505 * The two pid files - task and cgroup.procs - guaranteed that the result
3506 * is sorted, which forced this whole pidlist fiasco. As pid order is
3507 * different per namespace, each namespace needs differently sorted list,
3508 * making it impossible to use, for example, single rbtree of member tasks
3509 * sorted by task pointer. As pidlists can be fairly large, allocating one
3510 * per open file is dangerous, so cgroup had to implement shared pool of
3511 * pidlists keyed by cgroup and namespace.
3512 *
3513 * All this extra complexity was caused by the original implementation
3514 * committing to an entirely unnecessary property. In the long term, we
3515 * want to do away with it. Explicitly scramble sort order if
3516 * sane_behavior so that no such expectation exists in the new interface.
3517 *
3518 * Scrambling is done by swapping every two consecutive bits, which is
3519 * non-identity one-to-one mapping which disturbs sort order sufficiently.
3520 */
3521static pid_t pid_fry(pid_t pid)
3522{
3523 unsigned a = pid & 0x55555555;
3524 unsigned b = pid & 0xAAAAAAAA;
3525
3526 return (a << 1) | (b >> 1);
3527}
3528
3529static pid_t cgroup_pid_fry(struct cgroup *cgrp, pid_t pid)
3530{
3531 if (cgroup_sane_behavior(cgrp))
3532 return pid_fry(pid);
3533 else
3534 return pid;
3535}
3536
Ben Blum102a7752009-09-23 15:56:26 -07003537static int cmppid(const void *a, const void *b)
3538{
3539 return *(pid_t *)a - *(pid_t *)b;
3540}
3541
Tejun Heoafb2bc12013-11-29 10:42:59 -05003542static int fried_cmppid(const void *a, const void *b)
3543{
3544 return pid_fry(*(pid_t *)a) - pid_fry(*(pid_t *)b);
3545}
3546
Ben Blum72a8cb32009-09-23 15:56:27 -07003547static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3548 enum cgroup_filetype type)
3549{
3550 struct cgroup_pidlist *l;
3551 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003552 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003553
Tejun Heoe6b81712013-11-29 10:42:59 -05003554 lockdep_assert_held(&cgrp->pidlist_mutex);
3555
3556 list_for_each_entry(l, &cgrp->pidlists, links)
3557 if (l->key.type == type && l->key.ns == ns)
Ben Blum72a8cb32009-09-23 15:56:27 -07003558 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003559 return NULL;
3560}
3561
3562/*
3563 * find the appropriate pidlist for our purpose (given procs vs tasks)
3564 * returns with the lock on that pidlist already held, and takes care
3565 * of the use count, or returns NULL with no locks held if we're out of
3566 * memory.
3567 */
3568static struct cgroup_pidlist *cgroup_pidlist_find_create(struct cgroup *cgrp,
3569 enum cgroup_filetype type)
3570{
3571 struct cgroup_pidlist *l;
3572
3573 lockdep_assert_held(&cgrp->pidlist_mutex);
3574
3575 l = cgroup_pidlist_find(cgrp, type);
3576 if (l)
3577 return l;
3578
Ben Blum72a8cb32009-09-23 15:56:27 -07003579 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003580 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Tejun Heoe6b81712013-11-29 10:42:59 -05003581 if (!l)
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 return l;
Tejun Heoe6b81712013-11-29 10:42:59 -05003583
Tejun Heob1a21362013-11-29 10:42:58 -05003584 INIT_DELAYED_WORK(&l->destroy_dwork, cgroup_pidlist_destroy_work_fn);
Ben Blum72a8cb32009-09-23 15:56:27 -07003585 l->key.type = type;
Tejun Heoe6b81712013-11-29 10:42:59 -05003586 /* don't need task_nsproxy() if we're looking at ourself */
3587 l->key.ns = get_pid_ns(task_active_pid_ns(current));
Ben Blum72a8cb32009-09-23 15:56:27 -07003588 l->owner = cgrp;
3589 list_add(&l->links, &cgrp->pidlists);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 return l;
3591}
3592
3593/*
Ben Blum102a7752009-09-23 15:56:26 -07003594 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3595 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003596static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3597 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003598{
3599 pid_t *array;
3600 int length;
3601 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003602 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003603 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003604 struct cgroup_pidlist *l;
3605
Tejun Heo4bac00d2013-11-29 10:42:59 -05003606 lockdep_assert_held(&cgrp->pidlist_mutex);
3607
Ben Blum102a7752009-09-23 15:56:26 -07003608 /*
3609 * If cgroup gets more users after we read count, we won't have
3610 * enough space - tough. This race is indistinguishable to the
3611 * caller from the case that the additional cgroup users didn't
3612 * show up until sometime later on.
3613 */
3614 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003615 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003616 if (!array)
3617 return -ENOMEM;
3618 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003619 css_task_iter_start(&cgrp->dummy_css, &it);
3620 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003621 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003622 break;
Ben Blum102a7752009-09-23 15:56:26 -07003623 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003624 if (type == CGROUP_FILE_PROCS)
3625 pid = task_tgid_vnr(tsk);
3626 else
3627 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003628 if (pid > 0) /* make sure to only use valid results */
3629 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003630 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003631 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003632 length = n;
3633 /* now sort & (if procs) strip out duplicates */
Tejun Heoafb2bc12013-11-29 10:42:59 -05003634 if (cgroup_sane_behavior(cgrp))
3635 sort(array, length, sizeof(pid_t), fried_cmppid, NULL);
3636 else
3637 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003638 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003639 length = pidlist_uniq(array, length);
Tejun Heoe6b81712013-11-29 10:42:59 -05003640
Tejun Heoe6b81712013-11-29 10:42:59 -05003641 l = cgroup_pidlist_find_create(cgrp, type);
Ben Blum72a8cb32009-09-23 15:56:27 -07003642 if (!l) {
Tejun Heoe6b81712013-11-29 10:42:59 -05003643 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003644 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003646 }
Tejun Heoe6b81712013-11-29 10:42:59 -05003647
3648 /* store array, freeing old if necessary */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003649 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003650 l->list = array;
3651 l->length = length;
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003653 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003654}
3655
Balbir Singh846c7bb2007-10-18 23:39:44 -07003656/**
Li Zefana043e3b2008-02-23 15:24:09 -08003657 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003658 * @stats: cgroupstats to fill information into
3659 * @dentry: A dentry entry belonging to the cgroup for which stats have
3660 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003661 *
3662 * Build and fill cgroupstats so that taskstats can export it to user
3663 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003664 */
3665int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3666{
3667 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003668 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003669 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003670 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003671
Balbir Singh846c7bb2007-10-18 23:39:44 -07003672 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003673 * Validate dentry by checking the superblock operations,
3674 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003675 */
Li Zefan33d283b2008-11-19 15:36:48 -08003676 if (dentry->d_sb->s_op != &cgroup_ops ||
3677 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003678 goto err;
3679
3680 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003681 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682
Tejun Heo72ec7022013-08-08 20:11:26 -04003683 css_task_iter_start(&cgrp->dummy_css, &it);
3684 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003685 switch (tsk->state) {
3686 case TASK_RUNNING:
3687 stats->nr_running++;
3688 break;
3689 case TASK_INTERRUPTIBLE:
3690 stats->nr_sleeping++;
3691 break;
3692 case TASK_UNINTERRUPTIBLE:
3693 stats->nr_uninterruptible++;
3694 break;
3695 case TASK_STOPPED:
3696 stats->nr_stopped++;
3697 break;
3698 default:
3699 if (delayacct_is_task_waiting_on_io(tsk))
3700 stats->nr_io_wait++;
3701 break;
3702 }
3703 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003704 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003705
Balbir Singh846c7bb2007-10-18 23:39:44 -07003706err:
3707 return ret;
3708}
3709
Paul Menage8f3ff202009-09-23 15:56:25 -07003710
Paul Menagecc31edc2008-10-18 20:28:04 -07003711/*
Ben Blum102a7752009-09-23 15:56:26 -07003712 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003713 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003714 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003715 */
3716
Ben Blum102a7752009-09-23 15:56:26 -07003717static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003718{
3719 /*
3720 * Initially we receive a position value that corresponds to
3721 * one more than the last pid shown (or 0 on the first call or
3722 * after a seek to the start). Use a binary-search to find the
3723 * next pid to display, if any
3724 */
Tejun Heo5d224442013-12-05 12:28:04 -05003725 struct cgroup_open_file *of = s->private;
Tejun Heo7da11272013-12-05 12:28:04 -05003726 struct cgroup *cgrp = seq_css(s)->cgroup;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003727 struct cgroup_pidlist *l;
Tejun Heo7da11272013-12-05 12:28:04 -05003728 enum cgroup_filetype type = seq_cft(s)->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003729 int index = 0, pid = *pos;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003730 int *iter, ret;
Paul Menagecc31edc2008-10-18 20:28:04 -07003731
Tejun Heo4bac00d2013-11-29 10:42:59 -05003732 mutex_lock(&cgrp->pidlist_mutex);
3733
3734 /*
Tejun Heo5d224442013-12-05 12:28:04 -05003735 * !NULL @of->priv indicates that this isn't the first start()
Tejun Heo4bac00d2013-11-29 10:42:59 -05003736 * after open. If the matching pidlist is around, we can use that.
Tejun Heo5d224442013-12-05 12:28:04 -05003737 * Look for it. Note that @of->priv can't be used directly. It
Tejun Heo4bac00d2013-11-29 10:42:59 -05003738 * could already have been destroyed.
3739 */
Tejun Heo5d224442013-12-05 12:28:04 -05003740 if (of->priv)
3741 of->priv = cgroup_pidlist_find(cgrp, type);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003742
3743 /*
3744 * Either this is the first start() after open or the matching
3745 * pidlist has been destroyed inbetween. Create a new one.
3746 */
Tejun Heo5d224442013-12-05 12:28:04 -05003747 if (!of->priv) {
3748 ret = pidlist_array_load(cgrp, type,
3749 (struct cgroup_pidlist **)&of->priv);
Tejun Heo4bac00d2013-11-29 10:42:59 -05003750 if (ret)
3751 return ERR_PTR(ret);
3752 }
Tejun Heo5d224442013-12-05 12:28:04 -05003753 l = of->priv;
Tejun Heo4bac00d2013-11-29 10:42:59 -05003754
Paul Menagecc31edc2008-10-18 20:28:04 -07003755 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003756 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003757
Paul Menagecc31edc2008-10-18 20:28:04 -07003758 while (index < end) {
3759 int mid = (index + end) / 2;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003760 if (cgroup_pid_fry(cgrp, l->list[mid]) == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003761 index = mid;
3762 break;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003763 } else if (cgroup_pid_fry(cgrp, l->list[mid]) <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003764 index = mid + 1;
3765 else
3766 end = mid;
3767 }
3768 }
3769 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003770 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 return NULL;
3772 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003773 iter = l->list + index;
Tejun Heoafb2bc12013-11-29 10:42:59 -05003774 *pos = cgroup_pid_fry(cgrp, *iter);
Paul Menagecc31edc2008-10-18 20:28:04 -07003775 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003776}
3777
Ben Blum102a7752009-09-23 15:56:26 -07003778static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003779{
Tejun Heo5d224442013-12-05 12:28:04 -05003780 struct cgroup_open_file *of = s->private;
3781 struct cgroup_pidlist *l = of->priv;
Tejun Heo62236852013-11-29 10:42:58 -05003782
Tejun Heo5d224442013-12-05 12:28:04 -05003783 if (l)
3784 mod_delayed_work(cgroup_pidlist_destroy_wq, &l->destroy_dwork,
Tejun Heo04502362013-11-29 10:42:59 -05003785 CGROUP_PIDLIST_DESTROY_DELAY);
Tejun Heo7da11272013-12-05 12:28:04 -05003786 mutex_unlock(&seq_css(s)->cgroup->pidlist_mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003787}
3788
Ben Blum102a7752009-09-23 15:56:26 -07003789static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003790{
Tejun Heo5d224442013-12-05 12:28:04 -05003791 struct cgroup_open_file *of = s->private;
3792 struct cgroup_pidlist *l = of->priv;
Ben Blum102a7752009-09-23 15:56:26 -07003793 pid_t *p = v;
3794 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003795 /*
3796 * Advance to the next pid in the array. If this goes off the
3797 * end, we're done
3798 */
3799 p++;
3800 if (p >= end) {
3801 return NULL;
3802 } else {
Tejun Heo7da11272013-12-05 12:28:04 -05003803 *pos = cgroup_pid_fry(seq_css(s)->cgroup, *p);
Paul Menagecc31edc2008-10-18 20:28:04 -07003804 return p;
3805 }
3806}
3807
Ben Blum102a7752009-09-23 15:56:26 -07003808static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003809{
3810 return seq_printf(s, "%d\n", *(int *)v);
3811}
3812
Ben Blum102a7752009-09-23 15:56:26 -07003813/*
3814 * seq_operations functions for iterating on pidlists through seq_file -
3815 * independent of whether it's tasks or procs
3816 */
3817static const struct seq_operations cgroup_pidlist_seq_operations = {
3818 .start = cgroup_pidlist_start,
3819 .stop = cgroup_pidlist_stop,
3820 .next = cgroup_pidlist_next,
3821 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003822};
3823
Tejun Heo182446d2013-08-08 20:11:24 -04003824static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3825 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003826{
Tejun Heo182446d2013-08-08 20:11:24 -04003827 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003828}
3829
Tejun Heo182446d2013-08-08 20:11:24 -04003830static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3831 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003832{
Tejun Heo182446d2013-08-08 20:11:24 -04003833 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003834 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003835 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003836 else
Tejun Heo182446d2013-08-08 20:11:24 -04003837 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003838 return 0;
3839}
3840
Paul Menagebbcb81d2007-10-18 23:39:32 -07003841/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003842 * When dput() is called asynchronously, if umount has been done and
3843 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3844 * there's a small window that vfs will see the root dentry with non-zero
3845 * refcnt and trigger BUG().
3846 *
3847 * That's why we hold a reference before dput() and drop it right after.
3848 */
3849static void cgroup_dput(struct cgroup *cgrp)
3850{
3851 struct super_block *sb = cgrp->root->sb;
3852
3853 atomic_inc(&sb->s_active);
3854 dput(cgrp->dentry);
3855 deactivate_super(sb);
3856}
3857
Tejun Heo182446d2013-08-08 20:11:24 -04003858static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3859 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003860{
Tejun Heo182446d2013-08-08 20:11:24 -04003861 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003862}
3863
Tejun Heo182446d2013-08-08 20:11:24 -04003864static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3865 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003866{
3867 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003868 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003869 else
Tejun Heo182446d2013-08-08 20:11:24 -04003870 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003871 return 0;
3872}
3873
Tejun Heod5c56ce2013-06-03 19:14:34 -07003874static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003875 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003876 .name = "cgroup.procs",
Tejun Heo6612f052013-12-05 12:28:04 -05003877 .seq_start = cgroup_pidlist_start,
3878 .seq_next = cgroup_pidlist_next,
3879 .seq_stop = cgroup_pidlist_stop,
3880 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003881 .private = CGROUP_FILE_PROCS,
Ben Blum74a11662011-05-26 16:25:20 -07003882 .write_u64 = cgroup_procs_write,
Ben Blum74a11662011-05-26 16:25:20 -07003883 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003884 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003885 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003886 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003887 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003888 .read_u64 = cgroup_clone_children_read,
3889 .write_u64 = cgroup_clone_children_write,
3890 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003891 {
Tejun Heo873fe092013-04-14 20:15:26 -07003892 .name = "cgroup.sane_behavior",
3893 .flags = CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003894 .seq_show = cgroup_sane_behavior_show,
Tejun Heo873fe092013-04-14 20:15:26 -07003895 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003896
3897 /*
3898 * Historical crazy stuff. These don't have "cgroup." prefix and
3899 * don't exist if sane_behavior. If you're depending on these, be
3900 * prepared to be burned.
3901 */
3902 {
3903 .name = "tasks",
3904 .flags = CFTYPE_INSANE, /* use "procs" instead */
Tejun Heo6612f052013-12-05 12:28:04 -05003905 .seq_start = cgroup_pidlist_start,
3906 .seq_next = cgroup_pidlist_next,
3907 .seq_stop = cgroup_pidlist_stop,
3908 .seq_show = cgroup_pidlist_show,
Tejun Heo5d224442013-12-05 12:28:04 -05003909 .private = CGROUP_FILE_TASKS,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003910 .write_u64 = cgroup_tasks_write,
Tejun Heod5c56ce2013-06-03 19:14:34 -07003911 .mode = S_IRUGO | S_IWUSR,
3912 },
3913 {
3914 .name = "notify_on_release",
3915 .flags = CFTYPE_INSANE,
3916 .read_u64 = cgroup_read_notify_on_release,
3917 .write_u64 = cgroup_write_notify_on_release,
3918 },
Tejun Heo873fe092013-04-14 20:15:26 -07003919 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003920 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003921 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo2da8ca82013-12-05 12:28:04 -05003922 .seq_show = cgroup_release_agent_show,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003923 .write_string = cgroup_release_agent_write,
3924 .max_write_len = PATH_MAX,
3925 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003926 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003927};
3928
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003929/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003930 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003931 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003932 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003933 *
3934 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003935 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003936static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003937{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003938 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003939 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003940
Tejun Heo8e3f6542012-04-01 12:09:55 -07003941 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003942 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003943 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003944
3945 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003946 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003947
Tejun Heobee55092013-06-28 16:24:11 -07003948 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003949 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003950 if (ret < 0)
3951 goto err;
3952 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003953 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003954 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003955err:
3956 cgroup_clear_dir(cgrp, subsys_mask);
3957 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003958}
3959
Tejun Heo0c21ead2013-08-13 20:22:51 -04003960/*
3961 * css destruction is four-stage process.
3962 *
3963 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3964 * Implemented in kill_css().
3965 *
3966 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3967 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3968 * by invoking offline_css(). After offlining, the base ref is put.
3969 * Implemented in css_killed_work_fn().
3970 *
3971 * 3. When the percpu_ref reaches zero, the only possible remaining
3972 * accessors are inside RCU read sections. css_release() schedules the
3973 * RCU callback.
3974 *
3975 * 4. After the grace period, the css can be freed. Implemented in
3976 * css_free_work_fn().
3977 *
3978 * It is actually hairier because both step 2 and 4 require process context
3979 * and thus involve punting to css->destroy_work adding two additional
3980 * steps to the already complex sequence.
3981 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003982static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003983{
3984 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003985 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003986 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003987
Tejun Heo0ae78e02013-08-13 11:01:54 -04003988 if (css->parent)
3989 css_put(css->parent);
3990
Tejun Heo0c21ead2013-08-13 20:22:51 -04003991 css->ss->css_free(css);
3992 cgroup_dput(cgrp);
3993}
3994
3995static void css_free_rcu_fn(struct rcu_head *rcu_head)
3996{
3997 struct cgroup_subsys_state *css =
3998 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
3999
4000 /*
4001 * css holds an extra ref to @cgrp->dentry which is put on the last
4002 * css_put(). dput() requires process context which we don't have.
4003 */
4004 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004005 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004006}
4007
Tejun Heod3daf282013-06-13 19:39:16 -07004008static void css_release(struct percpu_ref *ref)
4009{
4010 struct cgroup_subsys_state *css =
4011 container_of(ref, struct cgroup_subsys_state, refcnt);
4012
Tejun Heo0c21ead2013-08-13 20:22:51 -04004013 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004014}
4015
Tejun Heo623f9262013-08-13 11:01:55 -04004016static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4017 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004018{
Paul Menagebd89aab2007-10-18 23:40:44 -07004019 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004020 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004021 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004022
Tejun Heo0ae78e02013-08-13 11:01:54 -04004023 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004024 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004025 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004026 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004027
Tejun Heoca8bdca2013-08-26 18:40:56 -04004028 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004029}
4030
Li Zefan2a4ac632013-07-31 16:16:40 +08004031/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004032static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004033{
Tejun Heo623f9262013-08-13 11:01:55 -04004034 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004035 int ret = 0;
4036
Tejun Heoa31f2d32012-11-19 08:13:37 -08004037 lockdep_assert_held(&cgroup_mutex);
4038
Tejun Heo92fb9742012-11-19 08:13:38 -08004039 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004040 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004041 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004042 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004043 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004044 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4045 }
Tejun Heob1929db2012-11-19 08:13:38 -08004046 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004047}
4048
Li Zefan2a4ac632013-07-31 16:16:40 +08004049/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004050static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004051{
Tejun Heo623f9262013-08-13 11:01:55 -04004052 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004053
4054 lockdep_assert_held(&cgroup_mutex);
4055
4056 if (!(css->flags & CSS_ONLINE))
4057 return;
4058
Li Zefand7eeac12013-03-12 15:35:59 -07004059 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004060 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004061
Tejun Heoeb954192013-08-08 20:11:23 -04004062 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004063 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004064 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004065}
4066
Paul Menageddbcc7e2007-10-18 23:39:30 -07004067/*
Li Zefana043e3b2008-02-23 15:24:09 -08004068 * cgroup_create - create a cgroup
4069 * @parent: cgroup that will be parent of the new cgroup
4070 * @dentry: dentry of the new cgroup
4071 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004072 *
Li Zefana043e3b2008-02-23 15:24:09 -08004073 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004074 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004075static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004076 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004077{
Tejun Heoae7f1642013-08-13 20:22:50 -04004078 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004079 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004080 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004081 struct cgroupfs_root *root = parent->root;
4082 int err = 0;
4083 struct cgroup_subsys *ss;
4084 struct super_block *sb = root->sb;
4085
Tejun Heo0a950f62012-11-19 09:02:12 -08004086 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004087 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4088 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004089 return -ENOMEM;
4090
Li Zefan65dff752013-03-01 15:01:56 +08004091 name = cgroup_alloc_name(dentry);
4092 if (!name)
4093 goto err_free_cgrp;
4094 rcu_assign_pointer(cgrp->name, name);
4095
Li Zefan4e96ee82013-07-31 09:50:50 +08004096 /*
4097 * Temporarily set the pointer to NULL, so idr_find() won't return
4098 * a half-baked cgroup.
4099 */
4100 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004101 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004102 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004103
Tejun Heo976c06b2012-11-05 09:16:59 -08004104 /*
4105 * Only live parents can have children. Note that the liveliness
4106 * check isn't strictly necessary because cgroup_mkdir() and
4107 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4108 * anyway so that locking is contained inside cgroup proper and we
4109 * don't get nasty surprises if we ever grow another caller.
4110 */
4111 if (!cgroup_lock_live_group(parent)) {
4112 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004113 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004114 }
4115
Paul Menageddbcc7e2007-10-18 23:39:30 -07004116 /* Grab a reference on the superblock so the hierarchy doesn't
4117 * get deleted on unmount if there are child cgroups. This
4118 * can be done outside cgroup_mutex, since the sb can't
4119 * disappear while someone has an open control file on the
4120 * fs */
4121 atomic_inc(&sb->s_active);
4122
Paul Menagecc31edc2008-10-18 20:28:04 -07004123 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004124
Li Zefanfe1c06c2013-01-24 14:30:22 +08004125 dentry->d_fsdata = cgrp;
4126 cgrp->dentry = dentry;
4127
Paul Menagebd89aab2007-10-18 23:40:44 -07004128 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004129 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004130 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004131
Li Zefanb6abdb02008-03-04 14:28:19 -08004132 if (notify_on_release(parent))
4133 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4134
Tejun Heo2260e7f2012-11-19 08:13:38 -08004135 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4136 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004137
Tejun Heo5549c492013-06-24 15:21:48 -07004138 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004139 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004140
Tejun Heoca8bdca2013-08-26 18:40:56 -04004141 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142 if (IS_ERR(css)) {
4143 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004144 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004146 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004147
4148 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004149 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004150 goto err_free_all;
4151
Tejun Heo623f9262013-08-13 11:01:55 -04004152 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004153 }
4154
Tejun Heo4e139af2012-11-19 08:13:36 -08004155 /*
4156 * Create directory. cgroup_create_file() returns with the new
4157 * directory locked on success so that it can be populated without
4158 * dropping cgroup_mutex.
4159 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004160 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004161 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004162 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004163 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004164
Tejun Heo00356bd2013-06-18 11:14:22 -07004165 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004166
Tejun Heo4e139af2012-11-19 08:13:36 -08004167 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004168 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4169 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004170
Li Zefan415cf072013-04-08 14:35:02 +08004171 /* hold a ref to the parent's dentry */
4172 dget(parent->dentry);
4173
Tejun Heob1929db2012-11-19 08:13:38 -08004174 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004175 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004176 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004177
4178 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004179 if (err)
4180 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004181
Tejun Heo266ccd52013-12-06 15:07:32 -05004182 /* each css holds a ref to the cgroup's dentry and parent css */
4183 dget(dentry);
4184 css_get(css->parent);
4185
4186 /* mark it consumed for error path */
4187 css_ar[ss->subsys_id] = NULL;
4188
Glauber Costa1f869e82012-11-30 17:31:23 +04004189 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4190 parent->parent) {
4191 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",
4192 current->comm, current->pid, ss->name);
4193 if (!strcmp(ss->name, "memory"))
4194 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4195 ss->warned_broken_hierarchy = true;
4196 }
Tejun Heoa8638032012-11-09 09:12:29 -08004197 }
4198
Li Zefan4e96ee82013-07-31 09:50:50 +08004199 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4200
Tejun Heo2bb566c2013-08-08 20:11:23 -04004201 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004202 if (err)
4203 goto err_destroy;
4204
4205 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004206 if (err)
4207 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208
4209 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004210 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211
4212 return 0;
4213
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004214err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004215 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004216 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004217
4218 if (css) {
4219 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004220 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004221 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224 /* Release the reference count that we took on the superblock */
4225 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004226err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004227 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004228err_free_name:
4229 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004230err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004231 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004233
4234err_destroy:
Tejun Heo266ccd52013-12-06 15:07:32 -05004235 for_each_root_subsys(root, ss) {
4236 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
4237
4238 if (css) {
4239 percpu_ref_cancel_init(&css->refcnt);
4240 ss->css_free(css);
4241 }
4242 }
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004243 cgroup_destroy_locked(cgrp);
4244 mutex_unlock(&cgroup_mutex);
4245 mutex_unlock(&dentry->d_inode->i_mutex);
4246 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247}
4248
Al Viro18bb1db2011-07-26 01:41:39 -04004249static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250{
4251 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4252
4253 /* the vfs holds inode->i_mutex already */
4254 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4255}
4256
Tejun Heo223dbc32013-08-13 20:22:50 -04004257/*
4258 * This is called when the refcnt of a css is confirmed to be killed.
4259 * css_tryget() is now guaranteed to fail.
4260 */
4261static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004262{
Tejun Heo223dbc32013-08-13 20:22:50 -04004263 struct cgroup_subsys_state *css =
4264 container_of(work, struct cgroup_subsys_state, destroy_work);
4265 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004266
Tejun Heof20104d2013-08-13 20:22:50 -04004267 mutex_lock(&cgroup_mutex);
4268
4269 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004270 * css_tryget() is guaranteed to fail now. Tell subsystems to
4271 * initate destruction.
4272 */
4273 offline_css(css);
4274
4275 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004276 * If @cgrp is marked dead, it's waiting for refs of all css's to
4277 * be disabled before proceeding to the second phase of cgroup
4278 * destruction. If we are the last one, kick it off.
4279 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004280 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004281 cgroup_destroy_css_killed(cgrp);
4282
4283 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004284
4285 /*
4286 * Put the css refs from kill_css(). Each css holds an extra
4287 * reference to the cgroup's dentry and cgroup removal proceeds
4288 * regardless of css refs. On the last put of each css, whenever
4289 * that may be, the extra dentry ref is put so that dentry
4290 * destruction happens only after all css's are released.
4291 */
4292 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004293}
4294
Tejun Heo223dbc32013-08-13 20:22:50 -04004295/* css kill confirmation processing requires process context, bounce */
4296static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004297{
4298 struct cgroup_subsys_state *css =
4299 container_of(ref, struct cgroup_subsys_state, refcnt);
4300
Tejun Heo223dbc32013-08-13 20:22:50 -04004301 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004302 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004303}
4304
4305/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004306 * kill_css - destroy a css
4307 * @css: css to destroy
4308 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004309 * This function initiates destruction of @css by removing cgroup interface
4310 * files and putting its base reference. ->css_offline() will be invoked
4311 * asynchronously once css_tryget() is guaranteed to fail and when the
4312 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004313 */
4314static void kill_css(struct cgroup_subsys_state *css)
4315{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004316 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4317
Tejun Heoedae0c32013-08-13 20:22:51 -04004318 /*
4319 * Killing would put the base ref, but we need to keep it alive
4320 * until after ->css_offline().
4321 */
4322 css_get(css);
4323
4324 /*
4325 * cgroup core guarantees that, by the time ->css_offline() is
4326 * invoked, no new css reference will be given out via
4327 * css_tryget(). We can't simply call percpu_ref_kill() and
4328 * proceed to offlining css's because percpu_ref_kill() doesn't
4329 * guarantee that the ref is seen as killed on all CPUs on return.
4330 *
4331 * Use percpu_ref_kill_and_confirm() to get notifications as each
4332 * css is confirmed to be seen as killed on all CPUs.
4333 */
4334 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004335}
4336
4337/**
4338 * cgroup_destroy_locked - the first stage of cgroup destruction
4339 * @cgrp: cgroup to be destroyed
4340 *
4341 * css's make use of percpu refcnts whose killing latency shouldn't be
4342 * exposed to userland and are RCU protected. Also, cgroup core needs to
4343 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4344 * invoked. To satisfy all the requirements, destruction is implemented in
4345 * the following two steps.
4346 *
4347 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4348 * userland visible parts and start killing the percpu refcnts of
4349 * css's. Set up so that the next stage will be kicked off once all
4350 * the percpu refcnts are confirmed to be killed.
4351 *
4352 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4353 * rest of destruction. Once all cgroup references are gone, the
4354 * cgroup is RCU-freed.
4355 *
4356 * This function implements s1. After this step, @cgrp is gone as far as
4357 * the userland is concerned and a new cgroup with the same name may be
4358 * created. As cgroup doesn't care about the names internally, this
4359 * doesn't cause any problem.
4360 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004361static int cgroup_destroy_locked(struct cgroup *cgrp)
4362 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004363{
Tejun Heo42809dd2012-11-19 08:13:37 -08004364 struct dentry *d = cgrp->dentry;
Tejun Heoed9577932012-11-05 09:16:58 -08004365 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004366 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004367 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368
Tejun Heo42809dd2012-11-19 08:13:37 -08004369 lockdep_assert_held(&d->d_inode->i_mutex);
4370 lockdep_assert_held(&cgroup_mutex);
4371
Tejun Heoddd69142013-06-12 21:04:54 -07004372 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004373 * css_set_lock synchronizes access to ->cset_links and prevents
4374 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004375 */
4376 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004377 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004378 read_unlock(&css_set_lock);
4379 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004381
Tejun Heo1a90dd52012-11-05 09:16:59 -08004382 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004383 * Make sure there's no live children. We can't test ->children
4384 * emptiness as dead children linger on it while being destroyed;
4385 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4386 */
4387 empty = true;
4388 rcu_read_lock();
4389 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4390 empty = cgroup_is_dead(child);
4391 if (!empty)
4392 break;
4393 }
4394 rcu_read_unlock();
4395 if (!empty)
4396 return -EBUSY;
4397
4398 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004399 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4400 * will be invoked to perform the rest of destruction once the
4401 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004402 */
Tejun Heo266ccd52013-12-06 15:07:32 -05004403 for_each_root_subsys(cgrp->root, ss) {
4404 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
4405
4406 if (css)
4407 kill_css(css);
4408 }
Tejun Heo455050d2013-06-13 19:27:41 -07004409
4410 /*
4411 * Mark @cgrp dead. This prevents further task migration and child
4412 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004413 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004414 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004415 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004416 */
Tejun Heo54766d42013-06-12 21:04:53 -07004417 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004418
Tejun Heo455050d2013-06-13 19:27:41 -07004419 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4420 raw_spin_lock(&release_list_lock);
4421 if (!list_empty(&cgrp->release_list))
4422 list_del_init(&cgrp->release_list);
4423 raw_spin_unlock(&release_list_lock);
4424
4425 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004426 * If @cgrp has css's attached, the second stage of cgroup
4427 * destruction is kicked off from css_killed_work_fn() after the
4428 * refs of all attached css's are killed. If @cgrp doesn't have
4429 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004430 */
Tejun Heof20104d2013-08-13 20:22:50 -04004431 if (!cgrp->nr_css)
4432 cgroup_destroy_css_killed(cgrp);
4433
4434 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004435 * Clear the base files and remove @cgrp directory. The removal
4436 * puts the base ref but we aren't quite done with @cgrp yet, so
4437 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004438 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004439 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004440 dget(d);
4441 cgroup_d_remove_dir(d);
4442
Tejun Heoea15f8c2013-06-13 19:27:42 -07004443 return 0;
4444};
4445
Tejun Heod3daf282013-06-13 19:39:16 -07004446/**
Tejun Heof20104d2013-08-13 20:22:50 -04004447 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004448 * @work: cgroup->destroy_free_work
4449 *
4450 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004451 * destroyed after all css's are offlined and performs the rest of
4452 * destruction. This is the second step of destruction described in the
4453 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004454 */
Tejun Heof20104d2013-08-13 20:22:50 -04004455static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004456{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004457 struct cgroup *parent = cgrp->parent;
4458 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004459
Tejun Heof20104d2013-08-13 20:22:50 -04004460 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461
Paul Menage999cd8a2009-01-07 18:08:36 -08004462 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004463 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004464
Li Zefan4e96ee82013-07-31 09:50:50 +08004465 /*
4466 * We should remove the cgroup object from idr before its grace
4467 * period starts, so we won't be looking up a cgroup while the
4468 * cgroup is being freed.
4469 */
4470 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4471 cgrp->id = -1;
4472
Paul Menageddbcc7e2007-10-18 23:39:30 -07004473 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474
Paul Menagebd89aab2007-10-18 23:40:44 -07004475 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004476 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477}
4478
Tejun Heo42809dd2012-11-19 08:13:37 -08004479static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4480{
4481 int ret;
4482
4483 mutex_lock(&cgroup_mutex);
4484 ret = cgroup_destroy_locked(dentry->d_fsdata);
4485 mutex_unlock(&cgroup_mutex);
4486
4487 return ret;
4488}
4489
Tejun Heo8e3f6542012-04-01 12:09:55 -07004490static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4491{
4492 INIT_LIST_HEAD(&ss->cftsets);
4493
4494 /*
4495 * base_cftset is embedded in subsys itself, no need to worry about
4496 * deregistration.
4497 */
4498 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004499 struct cftype *cft;
4500
4501 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4502 cft->ss = ss;
4503
Tejun Heo8e3f6542012-04-01 12:09:55 -07004504 ss->base_cftset.cfts = ss->base_cftypes;
4505 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4506 }
4507}
4508
Li Zefan06a11922008-04-29 01:00:07 -07004509static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004510{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004511 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004512
4513 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004514
Tejun Heo648bb562012-11-19 08:13:36 -08004515 mutex_lock(&cgroup_mutex);
4516
Tejun Heo8e3f6542012-04-01 12:09:55 -07004517 /* init base cftset */
4518 cgroup_init_cftsets(ss);
4519
Paul Menageddbcc7e2007-10-18 23:39:30 -07004520 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004521 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4522 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004523 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004524 /* We don't handle early failures gracefully */
4525 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004526 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004527
Li Zefane8d55fd2008-04-29 01:00:13 -07004528 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004529 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004530 * newly registered, all tasks and hence the
4531 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004532 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004533
4534 need_forkexit_callback |= ss->fork || ss->exit;
4535
Li Zefane8d55fd2008-04-29 01:00:13 -07004536 /* At system boot, before all subsystems have been
4537 * registered, no tasks have been forked, so we don't
4538 * need to invoke fork callbacks here. */
4539 BUG_ON(!list_empty(&init_task.tasks));
4540
Tejun Heoae7f1642013-08-13 20:22:50 -04004541 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004542
Tejun Heo648bb562012-11-19 08:13:36 -08004543 mutex_unlock(&cgroup_mutex);
4544
Ben Blume6a11052010-03-10 15:22:09 -08004545 /* this function shouldn't be used with modular subsystems, since they
4546 * need to register a subsys_id, among other things */
4547 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004548}
4549
4550/**
Ben Blume6a11052010-03-10 15:22:09 -08004551 * cgroup_load_subsys: load and register a modular subsystem at runtime
4552 * @ss: the subsystem to load
4553 *
4554 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004555 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004556 * up for use. If the subsystem is built-in anyway, work is delegated to the
4557 * simpler cgroup_init_subsys.
4558 */
4559int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4560{
Ben Blume6a11052010-03-10 15:22:09 -08004561 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004562 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004563 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004564 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004565 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004566
4567 /* check name and function validity */
4568 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004569 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004570 return -EINVAL;
4571
4572 /*
4573 * we don't support callbacks in modular subsystems. this check is
4574 * before the ss->module check for consistency; a subsystem that could
4575 * be a module should still have no callbacks even if the user isn't
4576 * compiling it as one.
4577 */
4578 if (ss->fork || ss->exit)
4579 return -EINVAL;
4580
4581 /*
4582 * an optionally modular subsystem is built-in: we want to do nothing,
4583 * since cgroup_init_subsys will have already taken care of it.
4584 */
4585 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004586 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004587 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004588 return 0;
4589 }
4590
Tejun Heo8e3f6542012-04-01 12:09:55 -07004591 /* init base cftset */
4592 cgroup_init_cftsets(ss);
4593
Ben Blume6a11052010-03-10 15:22:09 -08004594 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004595 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004596
4597 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004598 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004599 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004600 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004601 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004602 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004603 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004604 /* failure case - need to deassign the cgroup_subsys[] slot. */
4605 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004606 mutex_unlock(&cgroup_mutex);
4607 return PTR_ERR(css);
4608 }
4609
Tejun Heo9871bf92013-06-24 15:21:47 -07004610 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4611 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004612
4613 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004614 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004615
4616 /*
4617 * Now we need to entangle the css into the existing css_sets. unlike
4618 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4619 * will need a new pointer to it; done by iterating the css_set_table.
4620 * furthermore, modifying the existing css_sets will corrupt the hash
4621 * table state, so each changed css_set will need its hash recomputed.
4622 * this is all done under the css_set_lock.
4623 */
4624 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004625 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004626 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004627 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004628 continue;
4629 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004630 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004631 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004632 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004633 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004634 key = css_set_hash(cset->subsys);
4635 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004636 }
4637 write_unlock(&css_set_lock);
4638
Tejun Heoae7f1642013-08-13 20:22:50 -04004639 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004640 if (ret)
4641 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004642
Ben Blume6a11052010-03-10 15:22:09 -08004643 /* success! */
4644 mutex_unlock(&cgroup_mutex);
4645 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004646
4647err_unload:
4648 mutex_unlock(&cgroup_mutex);
4649 /* @ss can't be mounted here as try_module_get() would fail */
4650 cgroup_unload_subsys(ss);
4651 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004652}
4653EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4654
4655/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004656 * cgroup_unload_subsys: unload a modular subsystem
4657 * @ss: the subsystem to unload
4658 *
4659 * This function should be called in a modular subsystem's exitcall. When this
4660 * function is invoked, the refcount on the subsystem's module will be 0, so
4661 * the subsystem will not be attached to any hierarchy.
4662 */
4663void cgroup_unload_subsys(struct cgroup_subsys *ss)
4664{
Tejun Heo69d02062013-06-12 21:04:50 -07004665 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004666
4667 BUG_ON(ss->module == NULL);
4668
4669 /*
4670 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004671 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004672 * doesn't start being used while we're killing it off.
4673 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004674 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004675
4676 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004677
Tejun Heoca8bdca2013-08-26 18:40:56 -04004678 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004679
Ben Blumcf5d5942010-03-10 15:22:09 -08004680 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004681 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004682
Tejun Heo9871bf92013-06-24 15:21:47 -07004683 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004684 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004685
4686 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004687 * disentangle the css from all css_sets attached to the dummy
4688 * top. as in loading, we need to pay our respects to the hashtable
4689 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004690 */
4691 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004692 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004693 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004694 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004695
Tejun Heo5abb8852013-06-12 21:04:49 -07004696 hash_del(&cset->hlist);
4697 cset->subsys[ss->subsys_id] = NULL;
4698 key = css_set_hash(cset->subsys);
4699 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004700 }
4701 write_unlock(&css_set_lock);
4702
4703 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004704 * remove subsystem's css from the cgroup_dummy_top and free it -
4705 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004706 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004707 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004708 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004709 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004710
4711 mutex_unlock(&cgroup_mutex);
4712}
4713EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4714
4715/**
Li Zefana043e3b2008-02-23 15:24:09 -08004716 * cgroup_init_early - cgroup initialization at system boot
4717 *
4718 * Initialize cgroups at system boot, and initialize any
4719 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004720 */
4721int __init cgroup_init_early(void)
4722{
Tejun Heo30159ec2013-06-25 11:53:37 -07004723 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004724 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004725
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004726 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004727 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004728 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004729 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004730 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004731 init_cgroup_root(&cgroup_dummy_root);
4732 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004733 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004734
Tejun Heo69d02062013-06-12 21:04:50 -07004735 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004736 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4737 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004738 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004739
Tejun Heo30159ec2013-06-25 11:53:37 -07004740 /* at bootup time, we don't worry about modular subsystems */
4741 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742 BUG_ON(!ss->name);
4743 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004744 BUG_ON(!ss->css_alloc);
4745 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004746 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004747 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748 ss->name, ss->subsys_id);
4749 BUG();
4750 }
4751
4752 if (ss->early_init)
4753 cgroup_init_subsys(ss);
4754 }
4755 return 0;
4756}
4757
4758/**
Li Zefana043e3b2008-02-23 15:24:09 -08004759 * cgroup_init - cgroup initialization
4760 *
4761 * Register cgroup filesystem and /proc file, and initialize
4762 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004763 */
4764int __init cgroup_init(void)
4765{
Tejun Heo30159ec2013-06-25 11:53:37 -07004766 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004767 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004768 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004769
4770 err = bdi_init(&cgroup_backing_dev_info);
4771 if (err)
4772 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004773
Tejun Heo30159ec2013-06-25 11:53:37 -07004774 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004775 if (!ss->early_init)
4776 cgroup_init_subsys(ss);
4777 }
4778
Tejun Heofa3ca072013-04-14 11:36:56 -07004779 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004780 mutex_lock(&cgroup_mutex);
4781 mutex_lock(&cgroup_root_mutex);
4782
Tejun Heo82fe9b02013-06-25 11:53:37 -07004783 /* Add init_css_set to the hash table */
4784 key = css_set_hash(init_css_set.subsys);
4785 hash_add(css_set_table, &init_css_set.hlist, key);
4786
Tejun Heofc76df72013-06-25 11:53:37 -07004787 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004788
Li Zefan4e96ee82013-07-31 09:50:50 +08004789 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4790 0, 1, GFP_KERNEL);
4791 BUG_ON(err < 0);
4792
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004793 mutex_unlock(&cgroup_root_mutex);
4794 mutex_unlock(&cgroup_mutex);
4795
Greg KH676db4a2010-08-05 13:53:35 -07004796 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4797 if (!cgroup_kobj) {
4798 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004799 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004800 }
4801
4802 err = register_filesystem(&cgroup_fs_type);
4803 if (err < 0) {
4804 kobject_put(cgroup_kobj);
4805 goto out;
4806 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004807
Li Zefan46ae2202008-04-29 01:00:08 -07004808 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004809
Paul Menageddbcc7e2007-10-18 23:39:30 -07004810out:
Paul Menagea4243162007-10-18 23:39:35 -07004811 if (err)
4812 bdi_destroy(&cgroup_backing_dev_info);
4813
Paul Menageddbcc7e2007-10-18 23:39:30 -07004814 return err;
4815}
Paul Menageb4f48b62007-10-18 23:39:33 -07004816
Tejun Heoe5fca242013-11-22 17:14:39 -05004817static int __init cgroup_wq_init(void)
4818{
4819 /*
4820 * There isn't much point in executing destruction path in
4821 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4822 * Use 1 for @max_active.
4823 *
4824 * We would prefer to do this in cgroup_init() above, but that
4825 * is called before init_workqueues(): so leave this until after.
4826 */
4827 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4828 BUG_ON(!cgroup_destroy_wq);
Tejun Heob1a21362013-11-29 10:42:58 -05004829
4830 /*
4831 * Used to destroy pidlists and separate to serve as flush domain.
4832 * Cap @max_active to 1 too.
4833 */
4834 cgroup_pidlist_destroy_wq = alloc_workqueue("cgroup_pidlist_destroy",
4835 0, 1);
4836 BUG_ON(!cgroup_pidlist_destroy_wq);
4837
Tejun Heoe5fca242013-11-22 17:14:39 -05004838 return 0;
4839}
4840core_initcall(cgroup_wq_init);
4841
Paul Menagea4243162007-10-18 23:39:35 -07004842/*
4843 * proc_cgroup_show()
4844 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4845 * - Used for /proc/<pid>/cgroup.
4846 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4847 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004848 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004849 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4850 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4851 * cgroup to top_cgroup.
4852 */
4853
4854/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004855int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004856{
4857 struct pid *pid;
4858 struct task_struct *tsk;
4859 char *buf;
4860 int retval;
4861 struct cgroupfs_root *root;
4862
4863 retval = -ENOMEM;
4864 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4865 if (!buf)
4866 goto out;
4867
4868 retval = -ESRCH;
4869 pid = m->private;
4870 tsk = get_pid_task(pid, PIDTYPE_PID);
4871 if (!tsk)
4872 goto out_free;
4873
4874 retval = 0;
4875
4876 mutex_lock(&cgroup_mutex);
4877
Li Zefane5f6a862009-01-07 18:07:41 -08004878 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004879 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004880 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004881 int count = 0;
4882
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004883 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004884 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004885 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004886 if (strlen(root->name))
4887 seq_printf(m, "%sname=%s", count ? "," : "",
4888 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004889 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004890 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004891 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004892 if (retval < 0)
4893 goto out_unlock;
4894 seq_puts(m, buf);
4895 seq_putc(m, '\n');
4896 }
4897
4898out_unlock:
4899 mutex_unlock(&cgroup_mutex);
4900 put_task_struct(tsk);
4901out_free:
4902 kfree(buf);
4903out:
4904 return retval;
4905}
4906
Paul Menagea4243162007-10-18 23:39:35 -07004907/* Display information about each subsystem and each hierarchy */
4908static int proc_cgroupstats_show(struct seq_file *m, void *v)
4909{
Tejun Heo30159ec2013-06-25 11:53:37 -07004910 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004911 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004912
Paul Menage8bab8dd2008-04-04 14:29:57 -07004913 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004914 /*
4915 * ideally we don't want subsystems moving around while we do this.
4916 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4917 * subsys/hierarchy state.
4918 */
Paul Menagea4243162007-10-18 23:39:35 -07004919 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004920
4921 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004922 seq_printf(m, "%s\t%d\t%d\t%d\n",
4923 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004924 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004925
Paul Menagea4243162007-10-18 23:39:35 -07004926 mutex_unlock(&cgroup_mutex);
4927 return 0;
4928}
4929
4930static int cgroupstats_open(struct inode *inode, struct file *file)
4931{
Al Viro9dce07f2008-03-29 03:07:28 +00004932 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004933}
4934
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004935static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004936 .open = cgroupstats_open,
4937 .read = seq_read,
4938 .llseek = seq_lseek,
4939 .release = single_release,
4940};
4941
Paul Menageb4f48b62007-10-18 23:39:33 -07004942/**
4943 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004944 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004945 *
4946 * Description: A task inherits its parent's cgroup at fork().
4947 *
4948 * A pointer to the shared css_set was automatically copied in
4949 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004950 * it was not made under the protection of RCU or cgroup_mutex, so
4951 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4952 * have already changed current->cgroups, allowing the previously
4953 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004954 *
4955 * At the point that cgroup_fork() is called, 'current' is the parent
4956 * task, and the passed argument 'child' points to the child task.
4957 */
4958void cgroup_fork(struct task_struct *child)
4959{
Tejun Heo9bb71302012-10-18 17:52:07 -07004960 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004961 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004962 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004963 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004964 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004965}
4966
4967/**
Li Zefana043e3b2008-02-23 15:24:09 -08004968 * cgroup_post_fork - called on a new task after adding it to the task list
4969 * @child: the task in question
4970 *
Tejun Heo5edee612012-10-16 15:03:14 -07004971 * Adds the task to the list running through its css_set if necessary and
4972 * call the subsystem fork() callbacks. Has to be after the task is
4973 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004974 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004975 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004976 */
Paul Menage817929e2007-10-18 23:39:36 -07004977void cgroup_post_fork(struct task_struct *child)
4978{
Tejun Heo30159ec2013-06-25 11:53:37 -07004979 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004980 int i;
4981
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004982 /*
4983 * use_task_css_set_links is set to 1 before we walk the tasklist
4984 * under the tasklist_lock and we read it here after we added the child
4985 * to the tasklist under the tasklist_lock as well. If the child wasn't
4986 * yet in the tasklist when we walked through it from
4987 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4988 * should be visible now due to the paired locking and barriers implied
4989 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4990 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4991 * lock on fork.
4992 */
Paul Menage817929e2007-10-18 23:39:36 -07004993 if (use_task_css_set_links) {
4994 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004995 task_lock(child);
4996 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004997 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004998 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004999 write_unlock(&css_set_lock);
5000 }
Tejun Heo5edee612012-10-16 15:03:14 -07005001
5002 /*
5003 * Call ss->fork(). This must happen after @child is linked on
5004 * css_set; otherwise, @child might change state between ->fork()
5005 * and addition to css_set.
5006 */
5007 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005008 /*
5009 * fork/exit callbacks are supported only for builtin
5010 * subsystems, and the builtin section of the subsys
5011 * array is immutable, so we don't need to lock the
5012 * subsys array here. On the other hand, modular section
5013 * of the array can be freed at module unload, so we
5014 * can't touch that.
5015 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005016 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005017 if (ss->fork)
5018 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005019 }
Paul Menage817929e2007-10-18 23:39:36 -07005020}
Tejun Heo5edee612012-10-16 15:03:14 -07005021
Paul Menage817929e2007-10-18 23:39:36 -07005022/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005023 * cgroup_exit - detach cgroup from exiting task
5024 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005025 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005026 *
5027 * Description: Detach cgroup from @tsk and release it.
5028 *
5029 * Note that cgroups marked notify_on_release force every task in
5030 * them to take the global cgroup_mutex mutex when exiting.
5031 * This could impact scaling on very large systems. Be reluctant to
5032 * use notify_on_release cgroups where very high task exit scaling
5033 * is required on large systems.
5034 *
5035 * the_top_cgroup_hack:
5036 *
5037 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5038 *
5039 * We call cgroup_exit() while the task is still competent to
5040 * handle notify_on_release(), then leave the task attached to the
5041 * root cgroup in each hierarchy for the remainder of its exit.
5042 *
5043 * To do this properly, we would increment the reference count on
5044 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5045 * code we would add a second cgroup function call, to drop that
5046 * reference. This would just create an unnecessary hot spot on
5047 * the top_cgroup reference count, to no avail.
5048 *
5049 * Normally, holding a reference to a cgroup without bumping its
5050 * count is unsafe. The cgroup could go away, or someone could
5051 * attach us to a different cgroup, decrementing the count on
5052 * the first cgroup that we never incremented. But in this case,
5053 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005054 * which wards off any cgroup_attach_task() attempts, or task is a failed
5055 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005056 */
5057void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5058{
Tejun Heo30159ec2013-06-25 11:53:37 -07005059 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005060 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005061 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005062
5063 /*
5064 * Unlink from the css_set task list if necessary.
5065 * Optimistically check cg_list before taking
5066 * css_set_lock
5067 */
5068 if (!list_empty(&tsk->cg_list)) {
5069 write_lock(&css_set_lock);
5070 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005071 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005072 write_unlock(&css_set_lock);
5073 }
5074
Paul Menageb4f48b62007-10-18 23:39:33 -07005075 /* Reassign the task to the init_css_set. */
5076 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005077 cset = task_css_set(tsk);
5078 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005079
5080 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005081 /*
5082 * fork/exit callbacks are supported only for builtin
5083 * subsystems, see cgroup_post_fork() for details.
5084 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005085 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005086 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005087 struct cgroup_subsys_state *old_css = cset->subsys[i];
5088 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005089
Tejun Heoeb954192013-08-08 20:11:23 -04005090 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005091 }
5092 }
5093 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005094 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005095
Tejun Heo5abb8852013-06-12 21:04:49 -07005096 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005097}
Paul Menage697f4162007-10-18 23:39:34 -07005098
Paul Menagebd89aab2007-10-18 23:40:44 -07005099static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005100{
Li Zefanf50daa72013-03-01 15:06:07 +08005101 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005102 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005103 /*
5104 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005105 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005106 * it now
5107 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005108 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005109
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005110 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005111 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005112 list_empty(&cgrp->release_list)) {
5113 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114 need_schedule_work = 1;
5115 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005116 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005117 if (need_schedule_work)
5118 schedule_work(&release_agent_work);
5119 }
5120}
5121
Paul Menage81a6a5c2007-10-18 23:39:38 -07005122/*
5123 * Notify userspace when a cgroup is released, by running the
5124 * configured release agent with the name of the cgroup (path
5125 * relative to the root of cgroup file system) as the argument.
5126 *
5127 * Most likely, this user command will try to rmdir this cgroup.
5128 *
5129 * This races with the possibility that some other task will be
5130 * attached to this cgroup before it is removed, or that some other
5131 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5132 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5133 * unused, and this cgroup will be reprieved from its death sentence,
5134 * to continue to serve a useful existence. Next time it's released,
5135 * we will get notified again, if it still has 'notify_on_release' set.
5136 *
5137 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5138 * means only wait until the task is successfully execve()'d. The
5139 * separate release agent task is forked by call_usermodehelper(),
5140 * then control in this thread returns here, without waiting for the
5141 * release agent task. We don't bother to wait because the caller of
5142 * this routine has no use for the exit status of the release agent
5143 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005144 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005145static void cgroup_release_agent(struct work_struct *work)
5146{
5147 BUG_ON(work != &release_agent_work);
5148 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005149 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005150 while (!list_empty(&release_list)) {
5151 char *argv[3], *envp[3];
5152 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005153 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005154 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005155 struct cgroup,
5156 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005157 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005158 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005159 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005160 if (!pathbuf)
5161 goto continue_free;
5162 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5163 goto continue_free;
5164 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5165 if (!agentbuf)
5166 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005167
5168 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005169 argv[i++] = agentbuf;
5170 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005171 argv[i] = NULL;
5172
5173 i = 0;
5174 /* minimal command environment */
5175 envp[i++] = "HOME=/";
5176 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5177 envp[i] = NULL;
5178
5179 /* Drop the lock while we invoke the usermode helper,
5180 * since the exec could involve hitting disk and hence
5181 * be a slow process */
5182 mutex_unlock(&cgroup_mutex);
5183 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005184 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005185 continue_free:
5186 kfree(pathbuf);
5187 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005188 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005189 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005190 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005191 mutex_unlock(&cgroup_mutex);
5192}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005193
5194static int __init cgroup_disable(char *str)
5195{
Tejun Heo30159ec2013-06-25 11:53:37 -07005196 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005197 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005198 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005199
5200 while ((token = strsep(&str, ",")) != NULL) {
5201 if (!*token)
5202 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005203
Tejun Heo30159ec2013-06-25 11:53:37 -07005204 /*
5205 * cgroup_disable, being at boot time, can't know about
5206 * module subsystems, so we don't worry about them.
5207 */
5208 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005209 if (!strcmp(token, ss->name)) {
5210 ss->disabled = 1;
5211 printk(KERN_INFO "Disabling %s control group"
5212 " subsystem\n", ss->name);
5213 break;
5214 }
5215 }
5216 }
5217 return 1;
5218}
5219__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005220
Tejun Heob77d7b62013-08-13 11:01:54 -04005221/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005222 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5223 * @dentry: directory dentry of interest
5224 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005225 *
Tejun Heo87fb54f2013-12-06 15:11:55 -05005226 * Must be called under cgroup_mutex or RCU read lock. The caller is
5227 * responsible for pinning the returned css if it needs to be accessed
5228 * outside the critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005229 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005230struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5231 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005232{
5233 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005234
Tejun Heo87fb54f2013-12-06 15:11:55 -05005235 cgroup_assert_mutex_or_rcu_locked();
Tejun Heob77d7b62013-08-13 11:01:54 -04005236
Tejun Heo35cf0832013-08-26 18:40:56 -04005237 /* is @dentry a cgroup dir? */
5238 if (!dentry->d_inode ||
5239 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005240 return ERR_PTR(-EBADF);
5241
Tejun Heo35cf0832013-08-26 18:40:56 -04005242 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005243 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005244}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005245
Li Zefan1cb650b2013-08-19 10:05:24 +08005246/**
5247 * css_from_id - lookup css by id
5248 * @id: the cgroup id
5249 * @ss: cgroup subsys to be looked into
5250 *
5251 * Returns the css if there's valid one with @id, otherwise returns NULL.
5252 * Should be called under rcu_read_lock().
5253 */
5254struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5255{
5256 struct cgroup *cgrp;
5257
Tejun Heo87fb54f2013-12-06 15:11:55 -05005258 cgroup_assert_mutex_or_rcu_locked();
Li Zefan1cb650b2013-08-19 10:05:24 +08005259
5260 cgrp = idr_find(&ss->root->cgroup_idr, id);
5261 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005262 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005263 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005264}
5265
Paul Menagefe693432009-09-23 15:56:20 -07005266#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005267static struct cgroup_subsys_state *
5268debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005269{
5270 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5271
5272 if (!css)
5273 return ERR_PTR(-ENOMEM);
5274
5275 return css;
5276}
5277
Tejun Heoeb954192013-08-08 20:11:23 -04005278static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005279{
Tejun Heoeb954192013-08-08 20:11:23 -04005280 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005281}
5282
Tejun Heo182446d2013-08-08 20:11:24 -04005283static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5284 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005285{
Tejun Heo182446d2013-08-08 20:11:24 -04005286 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005287}
5288
Tejun Heo182446d2013-08-08 20:11:24 -04005289static u64 current_css_set_read(struct cgroup_subsys_state *css,
5290 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005291{
5292 return (u64)(unsigned long)current->cgroups;
5293}
5294
Tejun Heo182446d2013-08-08 20:11:24 -04005295static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005296 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005297{
5298 u64 count;
5299
5300 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005301 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005302 rcu_read_unlock();
5303 return count;
5304}
5305
Tejun Heo2da8ca82013-12-05 12:28:04 -05005306static int current_css_set_cg_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005307{
Tejun Heo69d02062013-06-12 21:04:50 -07005308 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005309 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005310
5311 read_lock(&css_set_lock);
5312 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005313 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005314 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005315 struct cgroup *c = link->cgrp;
5316 const char *name;
5317
5318 if (c->dentry)
5319 name = c->dentry->d_name.name;
5320 else
5321 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005322 seq_printf(seq, "Root %d group %s\n",
5323 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005324 }
5325 rcu_read_unlock();
5326 read_unlock(&css_set_lock);
5327 return 0;
5328}
5329
5330#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo2da8ca82013-12-05 12:28:04 -05005331static int cgroup_css_links_read(struct seq_file *seq, void *v)
Paul Menage7717f7b2009-09-23 15:56:22 -07005332{
Tejun Heo2da8ca82013-12-05 12:28:04 -05005333 struct cgroup_subsys_state *css = seq_css(seq);
Tejun Heo69d02062013-06-12 21:04:50 -07005334 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005335
5336 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005337 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005338 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005339 struct task_struct *task;
5340 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005341 seq_printf(seq, "css_set %p\n", cset);
5342 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005343 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5344 seq_puts(seq, " ...\n");
5345 break;
5346 } else {
5347 seq_printf(seq, " task %d\n",
5348 task_pid_vnr(task));
5349 }
5350 }
5351 }
5352 read_unlock(&css_set_lock);
5353 return 0;
5354}
5355
Tejun Heo182446d2013-08-08 20:11:24 -04005356static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005357{
Tejun Heo182446d2013-08-08 20:11:24 -04005358 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005359}
5360
5361static struct cftype debug_files[] = {
5362 {
Paul Menagefe693432009-09-23 15:56:20 -07005363 .name = "taskcount",
5364 .read_u64 = debug_taskcount_read,
5365 },
5366
5367 {
5368 .name = "current_css_set",
5369 .read_u64 = current_css_set_read,
5370 },
5371
5372 {
5373 .name = "current_css_set_refcount",
5374 .read_u64 = current_css_set_refcount_read,
5375 },
5376
5377 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005378 .name = "current_css_set_cg_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005379 .seq_show = current_css_set_cg_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005380 },
5381
5382 {
5383 .name = "cgroup_css_links",
Tejun Heo2da8ca82013-12-05 12:28:04 -05005384 .seq_show = cgroup_css_links_read,
Paul Menage7717f7b2009-09-23 15:56:22 -07005385 },
5386
5387 {
Paul Menagefe693432009-09-23 15:56:20 -07005388 .name = "releasable",
5389 .read_u64 = releasable_read,
5390 },
Paul Menagefe693432009-09-23 15:56:20 -07005391
Tejun Heo4baf6e32012-04-01 12:09:55 -07005392 { } /* terminate */
5393};
Paul Menagefe693432009-09-23 15:56:20 -07005394
5395struct cgroup_subsys debug_subsys = {
5396 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005397 .css_alloc = debug_css_alloc,
5398 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005399 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005400 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005401};
5402#endif /* CONFIG_CGROUP_DEBUG */