blob: 4c62513fe19fc8c4b938aeb8b3acb5158da2f976 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Al Viro4e10f3c2013-08-30 12:29:49 -040063#include <linux/file.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heoe25e2cb2011-12-12 18:12:21 -080067/*
68 * cgroup_mutex is the master lock. Any modification to cgroup or its
69 * hierarchy must be performed while holding it.
70 *
71 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
72 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
73 * release_agent_path and so on. Modifying requires both cgroup_mutex and
74 * cgroup_root_mutex. Readers can acquire either of the two. This is to
75 * break the following locking order cycle.
76 *
77 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
78 * B. namespace_sem -> cgroup_mutex
79 *
80 * B happens only through cgroup_show_options() and using cgroup_root_mutex
81 * breaks it.
82 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040085EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070086#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070087static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070088#endif
89
Tejun Heoe25e2cb2011-12-12 18:12:21 -080090static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070091
Ben Blumaae8aab2010-03-10 15:22:07 -080092/*
93 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020094 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080095 * registered after that. The mutable section of this array is protected by
96 * cgroup_mutex.
97 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020098#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020099#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700100static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700101#include <linux/cgroup_subsys.h>
102};
103
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700105 * The dummy hierarchy, reserved for the subsystems that are otherwise
106 * unattached - it never has more than a single cgroup, and all tasks are
107 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700108 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700109static struct cgroupfs_root cgroup_dummy_root;
110
111/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
112static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700113
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700114/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700115 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
116 */
117struct cfent {
118 struct list_head node;
119 struct dentry *dentry;
120 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400121 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700122
123 /* file xattrs */
124 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700125};
126
127/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300128 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800129 */
130struct cgroup_event {
131 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400132 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800133 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400134 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800135 /*
136 * Control file which the event associated.
137 */
138 struct cftype *cft;
139 /*
140 * eventfd to signal userspace about the event.
141 */
142 struct eventfd_ctx *eventfd;
143 /*
144 * Each of these stored in a list by the cgroup.
145 */
146 struct list_head list;
147 /*
148 * All fields below needed to unregister event when
149 * userspace closes eventfd.
150 */
151 poll_table pt;
152 wait_queue_head_t *wqh;
153 wait_queue_t wait;
154 struct work_struct remove;
155};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700156
Paul Menageddbcc7e2007-10-18 23:39:30 -0700157/* The list of hierarchy roots */
158
Tejun Heo9871bf92013-06-24 15:21:47 -0700159static LIST_HEAD(cgroup_roots);
160static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700161
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700162/*
163 * Hierarchy ID allocation and mapping. It follows the same exclusion
164 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
165 * writes, either for reads.
166 */
Tejun Heo1a574232013-04-14 11:36:58 -0700167static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700168
Li Zefan65dff752013-03-01 15:01:56 +0800169static struct cgroup_name root_cgroup_name = { .name = "/" };
170
Li Zefan794611a2013-06-18 18:53:53 +0800171/*
172 * Assign a monotonically increasing serial number to cgroups. It
173 * guarantees cgroups with bigger numbers are newer than those with smaller
174 * numbers. Also, as cgroups are always appended to the parent's
175 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700176 * the ascending serial number order on the list. Protected by
177 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800178 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700179static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800180
Paul Menageddbcc7e2007-10-18 23:39:30 -0700181/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800182 * check for fork/exit handlers to call. This avoids us having to do
183 * extra work in the fork/exit path if none of the subsystems need to
184 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700185 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700186static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187
Tejun Heo628f7cd2013-06-28 16:24:11 -0700188static struct cftype cgroup_base_files[];
189
Tejun Heof20104d2013-08-13 20:22:50 -0400190static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800191static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400192static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
193 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800194
Tejun Heo95109b62013-08-08 20:11:27 -0400195/**
196 * cgroup_css - obtain a cgroup's css for the specified subsystem
197 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400198 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400199 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400200 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
201 * function must be called either under cgroup_mutex or rcu_read_lock() and
202 * the caller is responsible for pinning the returned css if it wants to
203 * keep accessing it outside the said locks. This function may return
204 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400205 */
206static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400207 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400208{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400209 if (ss)
210 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
211 lockdep_is_held(&cgroup_mutex));
212 else
213 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400214}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700217static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700218{
Tejun Heo54766d42013-06-12 21:04:53 -0700219 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700220}
221
Li Zefan78574cf2013-04-08 19:00:38 -0700222/**
223 * cgroup_is_descendant - test ancestry
224 * @cgrp: the cgroup to be tested
225 * @ancestor: possible ancestor of @cgrp
226 *
227 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
228 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
229 * and @ancestor are accessible.
230 */
231bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
232{
233 while (cgrp) {
234 if (cgrp == ancestor)
235 return true;
236 cgrp = cgrp->parent;
237 }
238 return false;
239}
240EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700241
Adrian Bunke9685a02008-02-07 00:13:46 -0800242static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700243{
244 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700245 (1 << CGRP_RELEASABLE) |
246 (1 << CGRP_NOTIFY_ON_RELEASE);
247 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700248}
249
Adrian Bunke9685a02008-02-07 00:13:46 -0800250static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700251{
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700253}
254
Tejun Heo30159ec2013-06-25 11:53:37 -0700255/**
256 * for_each_subsys - iterate all loaded cgroup subsystems
257 * @ss: the iteration cursor
258 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
259 *
260 * Should be called under cgroup_mutex.
261 */
262#define for_each_subsys(ss, i) \
263 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
264 if (({ lockdep_assert_held(&cgroup_mutex); \
265 !((ss) = cgroup_subsys[i]); })) { } \
266 else
267
268/**
269 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
270 * @ss: the iteration cursor
271 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
272 *
273 * Bulit-in subsystems are always present and iteration itself doesn't
274 * require any synchronization.
275 */
276#define for_each_builtin_subsys(ss, i) \
277 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
278 (((ss) = cgroup_subsys[i]) || true); (i)++)
279
Tejun Heo5549c492013-06-24 15:21:48 -0700280/* iterate each subsystem attached to a hierarchy */
281#define for_each_root_subsys(root, ss) \
282 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700283
Tejun Heo5549c492013-06-24 15:21:48 -0700284/* iterate across the active hierarchies */
285#define for_each_active_root(root) \
286 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700287
Tejun Heof6ea9372012-04-01 12:09:55 -0700288static inline struct cgroup *__d_cgrp(struct dentry *dentry)
289{
290 return dentry->d_fsdata;
291}
292
Tejun Heo05ef1d72012-04-01 12:09:56 -0700293static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700294{
295 return dentry->d_fsdata;
296}
297
Tejun Heo05ef1d72012-04-01 12:09:56 -0700298static inline struct cftype *__d_cft(struct dentry *dentry)
299{
300 return __d_cfe(dentry)->type;
301}
302
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700303/**
304 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
305 * @cgrp: the cgroup to be checked for liveness
306 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700307 * On success, returns true; the mutex should be later unlocked. On
308 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700309 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700310static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700311{
312 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700313 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700314 mutex_unlock(&cgroup_mutex);
315 return false;
316 }
317 return true;
318}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700319
Paul Menage81a6a5c2007-10-18 23:39:38 -0700320/* the list of cgroups eligible for automatic release. Protected by
321 * release_list_lock */
322static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200323static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700324static void cgroup_release_agent(struct work_struct *work);
325static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700326static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327
Tejun Heo69d02062013-06-12 21:04:50 -0700328/*
329 * A cgroup can be associated with multiple css_sets as different tasks may
330 * belong to different cgroups on different hierarchies. In the other
331 * direction, a css_set is naturally associated with multiple cgroups.
332 * This M:N relationship is represented by the following link structure
333 * which exists for each association and allows traversing the associations
334 * from both sides.
335 */
336struct cgrp_cset_link {
337 /* the cgroup and css_set this link associates */
338 struct cgroup *cgrp;
339 struct css_set *cset;
340
341 /* list of cgrp_cset_links anchored at cgrp->cset_links */
342 struct list_head cset_link;
343
344 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
345 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700346};
347
348/* The default css_set - used by init and its children prior to any
349 * hierarchies being mounted. It contains a pointer to the root state
350 * for each subsystem. Also used to anchor the list of css_sets. Not
351 * reference-counted, to improve performance when child cgroups
352 * haven't been created.
353 */
354
355static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700356static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700357
Tejun Heo0942eee2013-08-08 20:11:26 -0400358/*
359 * css_set_lock protects the list of css_set objects, and the chain of
360 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400361 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400362 */
Paul Menage817929e2007-10-18 23:39:36 -0700363static DEFINE_RWLOCK(css_set_lock);
364static int css_set_count;
365
Paul Menage7717f7b2009-09-23 15:56:22 -0700366/*
367 * hash table for cgroup groups. This improves the performance to find
368 * an existing css_set. This hash doesn't (currently) take into
369 * account cgroups in empty hierarchies.
370 */
Li Zefan472b1052008-04-29 01:00:11 -0700371#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800372static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700373
Li Zefan0ac801f2013-01-10 11:49:27 +0800374static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700375{
Li Zefan0ac801f2013-01-10 11:49:27 +0800376 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700377 struct cgroup_subsys *ss;
378 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700379
Tejun Heo30159ec2013-06-25 11:53:37 -0700380 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800381 key += (unsigned long)css[i];
382 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700385}
386
Tejun Heo0942eee2013-08-08 20:11:26 -0400387/*
388 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400389 * until after the first call to css_task_iter_start(). This reduces the
390 * fork()/exit() overhead for people who have cgroups compiled into their
391 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400392 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700393static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700394
Tejun Heo5abb8852013-06-12 21:04:49 -0700395static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700396{
Tejun Heo69d02062013-06-12 21:04:50 -0700397 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700398
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700399 /*
400 * Ensure that the refcount doesn't hit zero while any readers
401 * can see it. Similar to atomic_dec_and_lock(), but for an
402 * rwlock
403 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700404 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 return;
406 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700407 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700408 write_unlock(&css_set_lock);
409 return;
410 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700411
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700412 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700413 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414 css_set_count--;
415
Tejun Heo69d02062013-06-12 21:04:50 -0700416 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700417 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700418
Tejun Heo69d02062013-06-12 21:04:50 -0700419 list_del(&link->cset_link);
420 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800421
Tejun Heoddd69142013-06-12 21:04:54 -0700422 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700423 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700424 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700425 set_bit(CGRP_RELEASABLE, &cgrp->flags);
426 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700427 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700428
429 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700431
432 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700433 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700434}
435
436/*
437 * refcounted get/put for css_set objects
438 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700439static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700440{
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
Tejun Heo5abb8852013-06-12 21:04:49 -0700444static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700445{
Tejun Heo5abb8852013-06-12 21:04:49 -0700446 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700447}
448
Tejun Heo5abb8852013-06-12 21:04:49 -0700449static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700450{
Tejun Heo5abb8852013-06-12 21:04:49 -0700451 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452}
453
Tejun Heob326f9d2013-06-24 15:21:48 -0700454/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700455 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700456 * @cset: candidate css_set being tested
457 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700458 * @new_cgrp: cgroup that's being entered by the task
459 * @template: desired set of css pointers in css_set (pre-calculated)
460 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800461 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700462 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
463 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700464static bool compare_css_sets(struct css_set *cset,
465 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 struct cgroup *new_cgrp,
467 struct cgroup_subsys_state *template[])
468{
469 struct list_head *l1, *l2;
470
Tejun Heo5abb8852013-06-12 21:04:49 -0700471 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 /* Not all subsystems matched */
473 return false;
474 }
475
476 /*
477 * Compare cgroup pointers in order to distinguish between
478 * different cgroups in heirarchies with no subsystems. We
479 * could get by with just this check alone (and skip the
480 * memcmp above) but on most setups the memcmp check will
481 * avoid the need for this more expensive check on almost all
482 * candidates.
483 */
484
Tejun Heo69d02062013-06-12 21:04:50 -0700485 l1 = &cset->cgrp_links;
486 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700488 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700489 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700490
491 l1 = l1->next;
492 l2 = l2->next;
493 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700494 if (l1 == &cset->cgrp_links) {
495 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 break;
497 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700498 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700499 }
500 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700501 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
502 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
503 cgrp1 = link1->cgrp;
504 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700506 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507
508 /*
509 * If this hierarchy is the hierarchy of the cgroup
510 * that's changing, then we need to check that this
511 * css_set points to the new cgroup; if it's any other
512 * hierarchy, then this css_set should point to the
513 * same cgroup as the old css_set.
514 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700515 if (cgrp1->root == new_cgrp->root) {
516 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700517 return false;
518 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700519 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700520 return false;
521 }
522 }
523 return true;
524}
525
Tejun Heob326f9d2013-06-24 15:21:48 -0700526/**
527 * find_existing_css_set - init css array and find the matching css_set
528 * @old_cset: the css_set that we're using before the cgroup transition
529 * @cgrp: the cgroup that we're moving into
530 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700531 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700532static struct css_set *find_existing_css_set(struct css_set *old_cset,
533 struct cgroup *cgrp,
534 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700535{
Paul Menagebd89aab2007-10-18 23:40:44 -0700536 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700537 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700538 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800539 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700540 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700541
Ben Blumaae8aab2010-03-10 15:22:07 -0800542 /*
543 * Build the set of subsystem state objects that we want to see in the
544 * new css_set. while subsystems can change globally, the entries here
545 * won't change, so no need for locking.
546 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700547 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400548 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700549 /* Subsystem is in this hierarchy. So we want
550 * the subsystem state from the new
551 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400552 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700553 } else {
554 /* Subsystem is not in this hierarchy, so we
555 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700556 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700557 }
558 }
559
Li Zefan0ac801f2013-01-10 11:49:27 +0800560 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700561 hash_for_each_possible(css_set_table, cset, hlist, key) {
562 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700563 continue;
564
565 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700566 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700567 }
Paul Menage817929e2007-10-18 23:39:36 -0700568
569 /* No existing cgroup group matched */
570 return NULL;
571}
572
Tejun Heo69d02062013-06-12 21:04:50 -0700573static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700574{
Tejun Heo69d02062013-06-12 21:04:50 -0700575 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700576
Tejun Heo69d02062013-06-12 21:04:50 -0700577 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
578 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700579 kfree(link);
580 }
581}
582
Tejun Heo69d02062013-06-12 21:04:50 -0700583/**
584 * allocate_cgrp_cset_links - allocate cgrp_cset_links
585 * @count: the number of links to allocate
586 * @tmp_links: list_head the allocated links are put on
587 *
588 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
589 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700590 */
Tejun Heo69d02062013-06-12 21:04:50 -0700591static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700592{
Tejun Heo69d02062013-06-12 21:04:50 -0700593 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700594 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700595
596 INIT_LIST_HEAD(tmp_links);
597
Li Zefan36553432008-07-29 22:33:19 -0700598 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700599 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700600 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700601 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700602 return -ENOMEM;
603 }
Tejun Heo69d02062013-06-12 21:04:50 -0700604 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700605 }
606 return 0;
607}
608
Li Zefanc12f65d2009-01-07 18:07:42 -0800609/**
610 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700611 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700612 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800613 * @cgrp: the destination cgroup
614 */
Tejun Heo69d02062013-06-12 21:04:50 -0700615static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
616 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800617{
Tejun Heo69d02062013-06-12 21:04:50 -0700618 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800619
Tejun Heo69d02062013-06-12 21:04:50 -0700620 BUG_ON(list_empty(tmp_links));
621 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
622 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700623 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700624 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700625 /*
626 * Always add links to the tail of the list so that the list
627 * is sorted by order of hierarchy creation
628 */
Tejun Heo69d02062013-06-12 21:04:50 -0700629 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800630}
631
Tejun Heob326f9d2013-06-24 15:21:48 -0700632/**
633 * find_css_set - return a new css_set with one cgroup updated
634 * @old_cset: the baseline css_set
635 * @cgrp: the cgroup to be updated
636 *
637 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
638 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700639 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700640static struct css_set *find_css_set(struct css_set *old_cset,
641 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700642{
Tejun Heob326f9d2013-06-24 15:21:48 -0700643 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700645 struct list_head tmp_links;
646 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800647 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700648
Tejun Heob326f9d2013-06-24 15:21:48 -0700649 lockdep_assert_held(&cgroup_mutex);
650
Paul Menage817929e2007-10-18 23:39:36 -0700651 /* First see if we already have a cgroup group that matches
652 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700653 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 cset = find_existing_css_set(old_cset, cgrp, template);
655 if (cset)
656 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700657 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700658
Tejun Heo5abb8852013-06-12 21:04:49 -0700659 if (cset)
660 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700661
Tejun Heof4f4be22013-06-12 21:04:51 -0700662 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700664 return NULL;
665
Tejun Heo69d02062013-06-12 21:04:50 -0700666 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700667 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700669 return NULL;
670 }
671
Tejun Heo5abb8852013-06-12 21:04:49 -0700672 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700673 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700674 INIT_LIST_HEAD(&cset->tasks);
675 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700676
677 /* Copy the set of subsystem state objects generated in
678 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700679 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700680
681 write_lock(&css_set_lock);
682 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700683 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700684 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700685
Paul Menage7717f7b2009-09-23 15:56:22 -0700686 if (c->root == cgrp->root)
687 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700688 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700689 }
Paul Menage817929e2007-10-18 23:39:36 -0700690
Tejun Heo69d02062013-06-12 21:04:50 -0700691 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700692
Paul Menage817929e2007-10-18 23:39:36 -0700693 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700694
695 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700696 key = css_set_hash(cset->subsys);
697 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700698
Paul Menage817929e2007-10-18 23:39:36 -0700699 write_unlock(&css_set_lock);
700
Tejun Heo5abb8852013-06-12 21:04:49 -0700701 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700702}
703
Paul Menageddbcc7e2007-10-18 23:39:30 -0700704/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700705 * Return the cgroup for "task" from the given hierarchy. Must be
706 * called with cgroup_mutex held.
707 */
708static struct cgroup *task_cgroup_from_root(struct task_struct *task,
709 struct cgroupfs_root *root)
710{
Tejun Heo5abb8852013-06-12 21:04:49 -0700711 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700712 struct cgroup *res = NULL;
713
714 BUG_ON(!mutex_is_locked(&cgroup_mutex));
715 read_lock(&css_set_lock);
716 /*
717 * No need to lock the task - since we hold cgroup_mutex the
718 * task can't change groups, so the only thing that can happen
719 * is that it exits and its css is set back to init_css_set.
720 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700721 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700722 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700723 res = &root->top_cgroup;
724 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700725 struct cgrp_cset_link *link;
726
727 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700728 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700729
Paul Menage7717f7b2009-09-23 15:56:22 -0700730 if (c->root == root) {
731 res = c;
732 break;
733 }
734 }
735 }
736 read_unlock(&css_set_lock);
737 BUG_ON(!res);
738 return res;
739}
740
741/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700742 * There is one global cgroup mutex. We also require taking
743 * task_lock() when dereferencing a task's cgroup subsys pointers.
744 * See "The task_lock() exception", at the end of this comment.
745 *
746 * A task must hold cgroup_mutex to modify cgroups.
747 *
748 * Any task can increment and decrement the count field without lock.
749 * So in general, code holding cgroup_mutex can't rely on the count
750 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800751 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700752 * means that no tasks are currently attached, therefore there is no
753 * way a task attached to that cgroup can fork (the other way to
754 * increment the count). So code holding cgroup_mutex can safely
755 * assume that if the count is zero, it will stay zero. Similarly, if
756 * a task holds cgroup_mutex on a cgroup with zero count, it
757 * knows that the cgroup won't be removed, as cgroup_rmdir()
758 * needs that mutex.
759 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
761 * (usually) take cgroup_mutex. These are the two most performance
762 * critical pieces of code here. The exception occurs on cgroup_exit(),
763 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
764 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800765 * to the release agent with the name of the cgroup (path relative to
766 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767 *
768 * A cgroup can only be deleted if both its 'count' of using tasks
769 * is zero, and its list of 'children' cgroups is empty. Since all
770 * tasks in the system use _some_ cgroup, and since there is always at
771 * least one task in the system (init, pid == 1), therefore, top_cgroup
772 * always has either children cgroups and/or using tasks. So we don't
773 * need a special hack to ensure that top_cgroup cannot be deleted.
774 *
775 * The task_lock() exception
776 *
777 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800778 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800779 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700780 * several performance critical places that need to reference
781 * task->cgroup without the expense of grabbing a system global
782 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800783 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
785 * the task_struct routinely used for such matters.
786 *
787 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800788 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 */
790
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791/*
792 * A couple of forward declarations required, due to cyclic reference loop:
793 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
794 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
795 * -> cgroup_mkdir.
796 */
797
Al Viro18bb1db2011-07-26 01:41:39 -0400798static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700800static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700801static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700802static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700803
804static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200805 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700806 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700807};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808
Al Viroa5e7ed32011-07-26 01:55:55 -0400809static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700810{
811 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812
813 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400814 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100816 inode->i_uid = current_fsuid();
817 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
819 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
820 }
821 return inode;
822}
823
Li Zefan65dff752013-03-01 15:01:56 +0800824static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
825{
826 struct cgroup_name *name;
827
828 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
829 if (!name)
830 return NULL;
831 strcpy(name->name, dentry->d_name.name);
832 return name;
833}
834
Li Zefanbe445622013-01-24 14:31:42 +0800835static void cgroup_free_fn(struct work_struct *work)
836{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700837 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800838
839 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800840 cgrp->root->number_of_cgroups--;
841 mutex_unlock(&cgroup_mutex);
842
843 /*
Li Zefan415cf072013-04-08 14:35:02 +0800844 * We get a ref to the parent's dentry, and put the ref when
845 * this cgroup is being freed, so it's guaranteed that the
846 * parent won't be destroyed before its children.
847 */
848 dput(cgrp->parent->dentry);
849
850 /*
Li Zefanbe445622013-01-24 14:31:42 +0800851 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700852 * created the cgroup. This will free cgrp->root, if we are
853 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800854 */
855 deactivate_super(cgrp->root->sb);
856
857 /*
858 * if we're getting rid of the cgroup, refcount should ensure
859 * that there are no pidlists left.
860 */
861 BUG_ON(!list_empty(&cgrp->pidlists));
862
863 simple_xattrs_free(&cgrp->xattrs);
864
Li Zefan65dff752013-03-01 15:01:56 +0800865 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800866 kfree(cgrp);
867}
868
869static void cgroup_free_rcu(struct rcu_head *head)
870{
871 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
872
Tejun Heoea15f8c2013-06-13 19:27:42 -0700873 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
874 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800875}
876
Paul Menageddbcc7e2007-10-18 23:39:30 -0700877static void cgroup_diput(struct dentry *dentry, struct inode *inode)
878{
879 /* is dentry a directory ? if so, kfree() associated cgroup */
880 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700881 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800882
Tejun Heo54766d42013-06-12 21:04:53 -0700883 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800884 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700885 } else {
886 struct cfent *cfe = __d_cfe(dentry);
887 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
888
889 WARN_ONCE(!list_empty(&cfe->node) &&
890 cgrp != &cgrp->root->top_cgroup,
891 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700892 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700893 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894 }
895 iput(inode);
896}
897
898static void remove_dir(struct dentry *d)
899{
900 struct dentry *parent = dget(d->d_parent);
901
902 d_delete(d);
903 simple_rmdir(parent->d_inode, d);
904 dput(parent);
905}
906
Li Zefan2739d3c2013-01-21 18:18:33 +0800907static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700909 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700910
Tejun Heo05ef1d72012-04-01 12:09:56 -0700911 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
912 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100913
Li Zefan2739d3c2013-01-21 18:18:33 +0800914 /*
915 * If we're doing cleanup due to failure of cgroup_create(),
916 * the corresponding @cfe may not exist.
917 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918 list_for_each_entry(cfe, &cgrp->files, node) {
919 struct dentry *d = cfe->dentry;
920
921 if (cft && cfe->type != cft)
922 continue;
923
924 dget(d);
925 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700926 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 list_del_init(&cfe->node);
928 dput(d);
929
Li Zefan2739d3c2013-01-21 18:18:33 +0800930 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932}
933
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400934/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700935 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700936 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400937 * @subsys_mask: mask of the subsystem ids whose files should be removed
938 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700939static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400941 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700942 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943
Tejun Heob420ba72013-07-12 12:34:02 -0700944 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400945 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700946
947 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400948 continue;
949 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400950 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952}
953
954/*
955 * NOTE : the dentry must have been dget()'ed
956 */
957static void cgroup_d_remove_dir(struct dentry *dentry)
958{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100959 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100961 parent = dentry->d_parent;
962 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800963 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700964 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100965 spin_unlock(&dentry->d_lock);
966 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700967 remove_dir(dentry);
968}
969
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700970/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800971 * Call with cgroup_mutex held. Drops reference counts on modules, including
972 * any duplicate ones that parse_cgroupfs_options took. If this function
973 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800974 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700976 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977{
Paul Menagebd89aab2007-10-18 23:40:44 -0700978 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700979 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700980 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700981 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982
Ben Blumaae8aab2010-03-10 15:22:07 -0800983 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800984 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800985
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700987 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700988 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700989 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700990
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700991 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700992 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700993 ret = -EBUSY;
994 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700996
997 /* pin the module */
998 if (!try_module_get(ss->module)) {
999 ret = -ENOENT;
1000 goto out_put;
1001 }
1002 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 }
1004
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001005 /* subsys could be missing if unloaded between parsing and here */
1006 if (added_mask != pinned) {
1007 ret = -ENOENT;
1008 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010
Tejun Heo31261212013-06-28 17:07:30 -07001011 ret = cgroup_populate_dir(cgrp, added_mask);
1012 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001013 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001014
1015 /*
1016 * Nothing can fail from this point on. Remove files for the
1017 * removed subsystems and rebind each subsystem.
1018 */
1019 cgroup_clear_dir(cgrp, removed_mask);
1020
Tejun Heo30159ec2013-06-25 11:53:37 -07001021 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001023
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001024 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001026 BUG_ON(cgroup_css(cgrp, ss));
1027 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1028 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001029
Tejun Heo73e80ed2013-08-13 11:01:55 -04001030 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001031 cgroup_css(cgroup_dummy_top, ss));
1032 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001033
Li Zefan33a68ac2009-01-07 18:07:42 -08001034 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001035 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001037 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001038
Ben Blumcf5d5942010-03-10 15:22:09 -08001039 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001040 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001041 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001043 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1044 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001045
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001047 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001048
Tejun Heoca8bdca2013-08-26 18:40:56 -04001049 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001050 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1051
Tejun Heo9871bf92013-06-24 15:21:47 -07001052 cgroup_subsys[i]->root = &cgroup_dummy_root;
1053 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /* subsystem is now free - drop reference on module */
1056 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001057 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 }
1059 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060
Tejun Heo1672d042013-06-25 18:04:54 -07001061 /*
1062 * Mark @root has finished binding subsystems. @root->subsys_mask
1063 * now matches the bound subsystems.
1064 */
1065 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1066
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001068
1069out_put:
1070 for_each_subsys(ss, i)
1071 if (pinned & (1 << i))
1072 module_put(ss->module);
1073 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074}
1075
Al Viro34c80b12011-12-08 21:32:45 -05001076static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077{
Al Viro34c80b12011-12-08 21:32:45 -05001078 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 struct cgroup_subsys *ss;
1080
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001081 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001082 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001084 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1085 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001086 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001088 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001089 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001090 if (strlen(root->release_agent_path))
1091 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001092 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001093 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001094 if (strlen(root->name))
1095 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001096 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097 return 0;
1098}
1099
1100struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001101 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001104 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001105 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001106 /* User explicitly requested empty subsystem */
1107 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001108
1109 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001110
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111};
1112
Ben Blumaae8aab2010-03-10 15:22:07 -08001113/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001114 * Convert a hierarchy specifier into a bitmask of subsystems and
1115 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1116 * array. This function takes refcounts on subsystems to be used, unless it
1117 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001118 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001119static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001121 char *token, *o = data;
1122 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001123 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001124 struct cgroup_subsys *ss;
1125 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001126
Ben Blumaae8aab2010-03-10 15:22:07 -08001127 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1128
Li Zefanf9ab5b52009-06-17 16:26:33 -07001129#ifdef CONFIG_CPUSETS
1130 mask = ~(1UL << cpuset_subsys_id);
1131#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132
Paul Menagec6d57f32009-09-23 15:56:19 -07001133 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134
1135 while ((token = strsep(&o, ",")) != NULL) {
1136 if (!*token)
1137 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001138 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001139 /* Explicitly have no subsystems */
1140 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 continue;
1142 }
1143 if (!strcmp(token, "all")) {
1144 /* Mutually exclusive option 'all' + subsystem name */
1145 if (one_ss)
1146 return -EINVAL;
1147 all_ss = true;
1148 continue;
1149 }
Tejun Heo873fe092013-04-14 20:15:26 -07001150 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1151 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1152 continue;
1153 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001155 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001156 continue;
1157 }
1158 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001159 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 continue;
1161 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001162 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001163 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001164 continue;
1165 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001166 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001167 /* Specifying two release agents is forbidden */
1168 if (opts->release_agent)
1169 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001170 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001171 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001172 if (!opts->release_agent)
1173 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 continue;
1175 }
1176 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001177 const char *name = token + 5;
1178 /* Can't specify an empty name */
1179 if (!strlen(name))
1180 return -EINVAL;
1181 /* Must match [\w.-]+ */
1182 for (i = 0; i < strlen(name); i++) {
1183 char c = name[i];
1184 if (isalnum(c))
1185 continue;
1186 if ((c == '.') || (c == '-') || (c == '_'))
1187 continue;
1188 return -EINVAL;
1189 }
1190 /* Specifying two names is forbidden */
1191 if (opts->name)
1192 return -EINVAL;
1193 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001194 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001195 GFP_KERNEL);
1196 if (!opts->name)
1197 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198
1199 continue;
1200 }
1201
Tejun Heo30159ec2013-06-25 11:53:37 -07001202 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001203 if (strcmp(token, ss->name))
1204 continue;
1205 if (ss->disabled)
1206 continue;
1207
1208 /* Mutually exclusive option 'all' + subsystem name */
1209 if (all_ss)
1210 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001211 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212 one_ss = true;
1213
1214 break;
1215 }
1216 if (i == CGROUP_SUBSYS_COUNT)
1217 return -ENOENT;
1218 }
1219
1220 /*
1221 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001222 * otherwise if 'none', 'name=' and a subsystem name options
1223 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001224 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001225 if (all_ss || (!one_ss && !opts->none && !opts->name))
1226 for_each_subsys(ss, i)
1227 if (!ss->disabled)
1228 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001229
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001230 /* Consistency checks */
1231
Tejun Heo873fe092013-04-14 20:15:26 -07001232 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1233 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1234
1235 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1236 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1237 return -EINVAL;
1238 }
1239
1240 if (opts->cpuset_clone_children) {
1241 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1242 return -EINVAL;
1243 }
1244 }
1245
Li Zefanf9ab5b52009-06-17 16:26:33 -07001246 /*
1247 * Option noprefix was introduced just for backward compatibility
1248 * with the old cpuset, so we allow noprefix only if mounting just
1249 * the cpuset subsystem.
1250 */
Tejun Heo93438622013-04-14 20:15:25 -07001251 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001252 return -EINVAL;
1253
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001254
1255 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001256 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001257 return -EINVAL;
1258
1259 /*
1260 * We either have to specify by name or by subsystems. (So all
1261 * empty hierarchies must have a name).
1262 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001263 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001264 return -EINVAL;
1265
1266 return 0;
1267}
1268
1269static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1270{
1271 int ret = 0;
1272 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001273 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001274 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001275 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276
Tejun Heo873fe092013-04-14 20:15:26 -07001277 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1278 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1279 return -EINVAL;
1280 }
1281
Paul Menagebd89aab2007-10-18 23:40:44 -07001282 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001284 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285
1286 /* See what subsystems are wanted */
1287 ret = parse_cgroupfs_options(data, &opts);
1288 if (ret)
1289 goto out_unlock;
1290
Tejun Heoa8a648c2013-06-24 15:21:47 -07001291 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001292 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1293 task_tgid_nr(current), current->comm);
1294
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001295 added_mask = opts.subsys_mask & ~root->subsys_mask;
1296 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001297
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001299 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001300 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001301 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1302 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1303 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001305 goto out_unlock;
1306 }
1307
Tejun Heof172e672013-06-28 17:07:30 -07001308 /* remounting is not allowed for populated hierarchies */
1309 if (root->number_of_cgroups > 1) {
1310 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001311 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001312 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001315 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317
Paul Menage81a6a5c2007-10-18 23:39:38 -07001318 if (opts.release_agent)
1319 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001321 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001322 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001323 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001325 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 return ret;
1327}
1328
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001329static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330 .statfs = simple_statfs,
1331 .drop_inode = generic_delete_inode,
1332 .show_options = cgroup_show_options,
1333 .remount_fs = cgroup_remount,
1334};
1335
Paul Menagecc31edc2008-10-18 20:28:04 -07001336static void init_cgroup_housekeeping(struct cgroup *cgrp)
1337{
1338 INIT_LIST_HEAD(&cgrp->sibling);
1339 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001340 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001341 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001342 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001343 INIT_LIST_HEAD(&cgrp->pidlists);
1344 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001345 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001346 INIT_LIST_HEAD(&cgrp->event_list);
1347 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001348 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001349}
Paul Menagec6d57f32009-09-23 15:56:19 -07001350
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351static void init_cgroup_root(struct cgroupfs_root *root)
1352{
Paul Menagebd89aab2007-10-18 23:40:44 -07001353 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001354
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355 INIT_LIST_HEAD(&root->subsys_list);
1356 INIT_LIST_HEAD(&root->root_list);
1357 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001358 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001359 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001360 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001361 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362}
1363
Tejun Heofc76df72013-06-25 11:53:37 -07001364static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001365{
Tejun Heo1a574232013-04-14 11:36:58 -07001366 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001367
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001368 lockdep_assert_held(&cgroup_mutex);
1369 lockdep_assert_held(&cgroup_root_mutex);
1370
Tejun Heofc76df72013-06-25 11:53:37 -07001371 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1372 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001373 if (id < 0)
1374 return id;
1375
1376 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001377 return 0;
1378}
1379
1380static void cgroup_exit_root_id(struct cgroupfs_root *root)
1381{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001382 lockdep_assert_held(&cgroup_mutex);
1383 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001384
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001385 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001386 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001387 root->hierarchy_id = 0;
1388 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001389}
1390
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391static int cgroup_test_super(struct super_block *sb, void *data)
1392{
Paul Menagec6d57f32009-09-23 15:56:19 -07001393 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 struct cgroupfs_root *root = sb->s_fs_info;
1395
Paul Menagec6d57f32009-09-23 15:56:19 -07001396 /* If we asked for a name then it must match */
1397 if (opts->name && strcmp(opts->name, root->name))
1398 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001400 /*
1401 * If we asked for subsystems (or explicitly for no
1402 * subsystems) then they must match
1403 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001404 if ((opts->subsys_mask || opts->none)
1405 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406 return 0;
1407
1408 return 1;
1409}
1410
Paul Menagec6d57f32009-09-23 15:56:19 -07001411static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1412{
1413 struct cgroupfs_root *root;
1414
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001415 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001416 return NULL;
1417
1418 root = kzalloc(sizeof(*root), GFP_KERNEL);
1419 if (!root)
1420 return ERR_PTR(-ENOMEM);
1421
1422 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001423
Tejun Heo1672d042013-06-25 18:04:54 -07001424 /*
1425 * We need to set @root->subsys_mask now so that @root can be
1426 * matched by cgroup_test_super() before it finishes
1427 * initialization; otherwise, competing mounts with the same
1428 * options may try to bind the same subsystems instead of waiting
1429 * for the first one leading to unexpected mount errors.
1430 * SUBSYS_BOUND will be set once actual binding is complete.
1431 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001432 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 root->flags = opts->flags;
1434 if (opts->release_agent)
1435 strcpy(root->release_agent_path, opts->release_agent);
1436 if (opts->name)
1437 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001438 if (opts->cpuset_clone_children)
1439 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001440 return root;
1441}
1442
Tejun Heofa3ca072013-04-14 11:36:56 -07001443static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001444{
Tejun Heofa3ca072013-04-14 11:36:56 -07001445 if (root) {
1446 /* hierarhcy ID shoulid already have been released */
1447 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001448
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001449 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001450 kfree(root);
1451 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001452}
1453
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454static int cgroup_set_super(struct super_block *sb, void *data)
1455{
1456 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 struct cgroup_sb_opts *opts = data;
1458
1459 /* If we don't have a new root, we can't set up a new sb */
1460 if (!opts->new_root)
1461 return -EINVAL;
1462
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001463 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464
1465 ret = set_anon_super(sb, NULL);
1466 if (ret)
1467 return ret;
1468
Paul Menagec6d57f32009-09-23 15:56:19 -07001469 sb->s_fs_info = opts->new_root;
1470 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471
1472 sb->s_blocksize = PAGE_CACHE_SIZE;
1473 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1474 sb->s_magic = CGROUP_SUPER_MAGIC;
1475 sb->s_op = &cgroup_ops;
1476
1477 return 0;
1478}
1479
1480static int cgroup_get_rootdir(struct super_block *sb)
1481{
Al Viro0df6a632010-12-21 13:29:29 -05001482 static const struct dentry_operations cgroup_dops = {
1483 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001484 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001485 };
1486
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487 struct inode *inode =
1488 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001489
1490 if (!inode)
1491 return -ENOMEM;
1492
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493 inode->i_fop = &simple_dir_operations;
1494 inode->i_op = &cgroup_dir_inode_operations;
1495 /* directories start off with i_nlink == 2 (for "." entry) */
1496 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001497 sb->s_root = d_make_root(inode);
1498 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001499 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001500 /* for everything else we want ->d_op set */
1501 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502 return 0;
1503}
1504
Al Virof7e83572010-07-26 13:23:11 +04001505static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001507 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508{
1509 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511 int ret = 0;
1512 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001514 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001515 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001516 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517
1518 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001519 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001521 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 if (ret)
1523 goto out_err;
1524
1525 /*
1526 * Allocate a new cgroup root. We may not need it if we're
1527 * reusing an existing hierarchy.
1528 */
1529 new_root = cgroup_root_from_opts(&opts);
1530 if (IS_ERR(new_root)) {
1531 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001532 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001533 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535
Paul Menagec6d57f32009-09-23 15:56:19 -07001536 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001537 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001539 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001540 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001541 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 }
1543
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 root = sb->s_fs_info;
1545 BUG_ON(!root);
1546 if (root == opts.new_root) {
1547 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001548 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001549 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001550 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001551 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552
1553 BUG_ON(sb->s_root != NULL);
1554
1555 ret = cgroup_get_rootdir(sb);
1556 if (ret)
1557 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001558 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
Paul Menage817929e2007-10-18 23:39:36 -07001560 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001562 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001564 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1565 0, 1, GFP_KERNEL);
1566 if (root_cgrp->id < 0)
1567 goto unlock_drop;
1568
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001569 /* Check for name clashes with existing mounts */
1570 ret = -EBUSY;
1571 if (strlen(root->name))
1572 for_each_active_root(existing_root)
1573 if (!strcmp(existing_root->name, root->name))
1574 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575
Paul Menage817929e2007-10-18 23:39:36 -07001576 /*
1577 * We're accessing css_set_count without locking
1578 * css_set_lock here, but that's OK - it can only be
1579 * increased by someone holding cgroup_lock, and
1580 * that's us. The worst that can happen is that we
1581 * have some link structures left over
1582 */
Tejun Heo69d02062013-06-12 21:04:50 -07001583 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001584 if (ret)
1585 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001586
Tejun Heofc76df72013-06-25 11:53:37 -07001587 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1588 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001589 if (ret)
1590 goto unlock_drop;
1591
Tejun Heo31261212013-06-28 17:07:30 -07001592 sb->s_root->d_fsdata = root_cgrp;
1593 root_cgrp->dentry = sb->s_root;
1594
1595 /*
1596 * We're inside get_sb() and will call lookup_one_len() to
1597 * create the root files, which doesn't work if SELinux is
1598 * in use. The following cred dancing somehow works around
1599 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1600 * populating new cgroupfs mount") for more details.
1601 */
1602 cred = override_creds(&init_cred);
1603
Tejun Heo2bb566c2013-08-08 20:11:23 -04001604 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001605 if (ret)
1606 goto rm_base_files;
1607
Tejun Heoa8a648c2013-06-24 15:21:47 -07001608 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001609 if (ret)
1610 goto rm_base_files;
1611
1612 revert_creds(cred);
1613
Ben Blumcf5d5942010-03-10 15:22:09 -08001614 /*
1615 * There must be no failure case after here, since rebinding
1616 * takes care of subsystems' refcounts, which are explicitly
1617 * dropped in the failure exit path.
1618 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
Tejun Heo9871bf92013-06-24 15:21:47 -07001620 list_add(&root->root_list, &cgroup_roots);
1621 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001622
Paul Menage817929e2007-10-18 23:39:36 -07001623 /* Link the top cgroup in this hierarchy into all
1624 * the css_set objects */
1625 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001626 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001627 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001628 write_unlock(&css_set_lock);
1629
Tejun Heo69d02062013-06-12 21:04:50 -07001630 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001631
Li Zefanc12f65d2009-01-07 18:07:42 -08001632 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 BUG_ON(root->number_of_cgroups != 1);
1634
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001637 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001638 } else {
1639 /*
1640 * We re-used an existing hierarchy - the new root (if
1641 * any) is not needed
1642 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001643 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001644
Tejun Heoc7ba8282013-06-29 14:06:10 -07001645 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001646 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1647 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1648 ret = -EINVAL;
1649 goto drop_new_super;
1650 } else {
1651 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1652 }
Tejun Heo873fe092013-04-14 20:15:26 -07001653 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654 }
1655
Paul Menagec6d57f32009-09-23 15:56:19 -07001656 kfree(opts.release_agent);
1657 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001658 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659
Tejun Heo31261212013-06-28 17:07:30 -07001660 rm_base_files:
1661 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001662 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001663 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001664 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001665 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001666 mutex_unlock(&cgroup_root_mutex);
1667 mutex_unlock(&cgroup_mutex);
1668 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001670 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001671 out_err:
1672 kfree(opts.release_agent);
1673 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001674 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675}
1676
1677static void cgroup_kill_sb(struct super_block *sb) {
1678 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001679 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001680 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 int ret;
1682
1683 BUG_ON(!root);
1684
1685 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001686 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001687
Tejun Heo31261212013-06-28 17:07:30 -07001688 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001690 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691
1692 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001693 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1694 ret = rebind_subsystems(root, 0, root->subsys_mask);
1695 /* Shouldn't be able to fail ... */
1696 BUG_ON(ret);
1697 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698
Paul Menage817929e2007-10-18 23:39:36 -07001699 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001700 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001701 * root cgroup
1702 */
1703 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001704
Tejun Heo69d02062013-06-12 21:04:50 -07001705 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1706 list_del(&link->cset_link);
1707 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001708 kfree(link);
1709 }
1710 write_unlock(&css_set_lock);
1711
Paul Menage839ec542009-01-29 14:25:22 -08001712 if (!list_empty(&root->root_list)) {
1713 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001714 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001715 }
Li Zefane5f6a862009-01-07 18:07:41 -08001716
Tejun Heofa3ca072013-04-14 11:36:56 -07001717 cgroup_exit_root_id(root);
1718
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001719 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001721 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001723 simple_xattrs_free(&cgrp->xattrs);
1724
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001726 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727}
1728
1729static struct file_system_type cgroup_fs_type = {
1730 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001731 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 .kill_sb = cgroup_kill_sb,
1733};
1734
Greg KH676db4a2010-08-05 13:53:35 -07001735static struct kobject *cgroup_kobj;
1736
Li Zefana043e3b2008-02-23 15:24:09 -08001737/**
1738 * cgroup_path - generate the path of a cgroup
1739 * @cgrp: the cgroup in question
1740 * @buf: the buffer to write the path into
1741 * @buflen: the length of the buffer
1742 *
Li Zefan65dff752013-03-01 15:01:56 +08001743 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1744 *
1745 * We can't generate cgroup path using dentry->d_name, as accessing
1746 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1747 * inode's i_mutex, while on the other hand cgroup_path() can be called
1748 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001750int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751{
Li Zefan65dff752013-03-01 15:01:56 +08001752 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001754
Tejun Heoda1f2962013-04-14 10:32:19 -07001755 if (!cgrp->parent) {
1756 if (strlcpy(buf, "/", buflen) >= buflen)
1757 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 return 0;
1759 }
1760
Tao Ma316eb662012-11-08 21:36:38 +08001761 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001762 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001763
Li Zefan65dff752013-03-01 15:01:56 +08001764 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001765 do {
Li Zefan65dff752013-03-01 15:01:56 +08001766 const char *name = cgroup_name(cgrp);
1767 int len;
1768
1769 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001771 goto out;
1772 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001773
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001775 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001777
1778 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001779 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001780 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001782out:
1783 rcu_read_unlock();
1784 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785}
Ben Blum67523c42010-03-10 15:22:11 -08001786EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787
Tejun Heo857a2be2013-04-14 20:50:08 -07001788/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001789 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001790 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001791 * @buf: the buffer to write the path into
1792 * @buflen: the length of the buffer
1793 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001794 * Determine @task's cgroup on the first (the one with the lowest non-zero
1795 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1796 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1797 * cgroup controller callbacks.
1798 *
1799 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001800 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001801int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001802{
1803 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001804 struct cgroup *cgrp;
1805 int hierarchy_id = 1, ret = 0;
1806
1807 if (buflen < 2)
1808 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001809
1810 mutex_lock(&cgroup_mutex);
1811
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1813
Tejun Heo857a2be2013-04-14 20:50:08 -07001814 if (root) {
1815 cgrp = task_cgroup_from_root(task, root);
1816 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001817 } else {
1818 /* if no hierarchy exists, everyone is in "/" */
1819 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001820 }
1821
1822 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001823 return ret;
1824}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001826
Ben Blum74a11662011-05-26 16:25:20 -07001827/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001828 * Control Group taskset
1829 */
Tejun Heo134d3372011-12-12 18:12:21 -08001830struct task_and_cgroup {
1831 struct task_struct *task;
1832 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001833 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001834};
1835
Tejun Heo2f7ee562011-12-12 18:12:21 -08001836struct cgroup_taskset {
1837 struct task_and_cgroup single;
1838 struct flex_array *tc_array;
1839 int tc_array_len;
1840 int idx;
1841 struct cgroup *cur_cgrp;
1842};
1843
1844/**
1845 * cgroup_taskset_first - reset taskset and return the first task
1846 * @tset: taskset of interest
1847 *
1848 * @tset iteration is initialized and the first task is returned.
1849 */
1850struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1851{
1852 if (tset->tc_array) {
1853 tset->idx = 0;
1854 return cgroup_taskset_next(tset);
1855 } else {
1856 tset->cur_cgrp = tset->single.cgrp;
1857 return tset->single.task;
1858 }
1859}
1860EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1861
1862/**
1863 * cgroup_taskset_next - iterate to the next task in taskset
1864 * @tset: taskset of interest
1865 *
1866 * Return the next task in @tset. Iteration must have been initialized
1867 * with cgroup_taskset_first().
1868 */
1869struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1870{
1871 struct task_and_cgroup *tc;
1872
1873 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1874 return NULL;
1875
1876 tc = flex_array_get(tset->tc_array, tset->idx++);
1877 tset->cur_cgrp = tc->cgrp;
1878 return tc->task;
1879}
1880EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1881
1882/**
Tejun Heod99c8722013-08-08 20:11:27 -04001883 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001884 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001885 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886 *
Tejun Heod99c8722013-08-08 20:11:27 -04001887 * Return the css for the current (last returned) task of @tset for
1888 * subsystem specified by @subsys_id. This function must be preceded by
1889 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001890 */
Tejun Heod99c8722013-08-08 20:11:27 -04001891struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1892 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001893{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001894 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001895}
Tejun Heod99c8722013-08-08 20:11:27 -04001896EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001897
1898/**
1899 * cgroup_taskset_size - return the number of tasks in taskset
1900 * @tset: taskset of interest
1901 */
1902int cgroup_taskset_size(struct cgroup_taskset *tset)
1903{
1904 return tset->tc_array ? tset->tc_array_len : 1;
1905}
1906EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1907
1908
Ben Blum74a11662011-05-26 16:25:20 -07001909/*
1910 * cgroup_task_migrate - move a task from one cgroup to another.
1911 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001912 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001913 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001914static void cgroup_task_migrate(struct cgroup *old_cgrp,
1915 struct task_struct *tsk,
1916 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001917{
Tejun Heo5abb8852013-06-12 21:04:49 -07001918 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001919
1920 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001921 * We are synchronized through threadgroup_lock() against PF_EXITING
1922 * setting such that we can't race against cgroup_exit() changing the
1923 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001924 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001925 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001926 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001927
Ben Blum74a11662011-05-26 16:25:20 -07001928 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001929 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001930 task_unlock(tsk);
1931
1932 /* Update the css_set linked lists if we're using them */
1933 write_lock(&css_set_lock);
1934 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001935 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001936 write_unlock(&css_set_lock);
1937
1938 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001939 * We just gained a reference on old_cset by taking it from the
1940 * task. As trading it for new_cset is protected by cgroup_mutex,
1941 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001942 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001943 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1944 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001945}
1946
Li Zefana043e3b2008-02-23 15:24:09 -08001947/**
Li Zefan081aa452013-03-13 09:17:09 +08001948 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001949 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001950 * @tsk: the task or the leader of the threadgroup to be attached
1951 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001952 *
Tejun Heo257058a2011-12-12 18:12:21 -08001953 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001954 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001955 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001956static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1957 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001958{
1959 int retval, i, group_size;
1960 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001961 struct cgroupfs_root *root = cgrp->root;
1962 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001963 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001964 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001965 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001966 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001967
1968 /*
1969 * step 0: in order to do expensive, possibly blocking operations for
1970 * every thread, we cannot iterate the thread group list, since it needs
1971 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001972 * group - group_rwsem prevents new threads from appearing, and if
1973 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001974 */
Li Zefan081aa452013-03-13 09:17:09 +08001975 if (threadgroup)
1976 group_size = get_nr_threads(tsk);
1977 else
1978 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001979 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001980 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001981 if (!group)
1982 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001983 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001984 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001985 if (retval)
1986 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001987
Ben Blum74a11662011-05-26 16:25:20 -07001988 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001989 /*
1990 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1991 * already PF_EXITING could be freed from underneath us unless we
1992 * take an rcu_read_lock.
1993 */
1994 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001995 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001996 struct task_and_cgroup ent;
1997
Tejun Heocd3d0952011-12-12 18:12:21 -08001998 /* @tsk either already exited or can't exit until the end */
1999 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002000 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002001
Ben Blum74a11662011-05-26 16:25:20 -07002002 /* as per above, nr_threads may decrease, but not increase. */
2003 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002004 ent.task = tsk;
2005 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002006 /* nothing to do if this task is already in the cgroup */
2007 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002008 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002009 /*
2010 * saying GFP_ATOMIC has no effect here because we did prealloc
2011 * earlier, but it's good form to communicate our expectations.
2012 */
Tejun Heo134d3372011-12-12 18:12:21 -08002013 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002014 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002015 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002016 next:
Li Zefan081aa452013-03-13 09:17:09 +08002017 if (!threadgroup)
2018 break;
Ben Blum74a11662011-05-26 16:25:20 -07002019 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002020 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002021 /* remember the number of threads in the array for later. */
2022 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002023 tset.tc_array = group;
2024 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002025
Tejun Heo134d3372011-12-12 18:12:21 -08002026 /* methods shouldn't be called if no task is actually migrating */
2027 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002028 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002029 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002030
Ben Blum74a11662011-05-26 16:25:20 -07002031 /*
2032 * step 1: check that we can legitimately attach to the cgroup.
2033 */
Tejun Heo5549c492013-06-24 15:21:48 -07002034 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002035 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002036
Ben Blum74a11662011-05-26 16:25:20 -07002037 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002038 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002039 if (retval) {
2040 failed_ss = ss;
2041 goto out_cancel_attach;
2042 }
2043 }
Ben Blum74a11662011-05-26 16:25:20 -07002044 }
2045
2046 /*
2047 * step 2: make sure css_sets exist for all threads to be migrated.
2048 * we use find_css_set, which allocates a new one if necessary.
2049 */
Ben Blum74a11662011-05-26 16:25:20 -07002050 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002051 struct css_set *old_cset;
2052
Tejun Heo134d3372011-12-12 18:12:21 -08002053 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002054 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002055 tc->cset = find_css_set(old_cset, cgrp);
2056 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002057 retval = -ENOMEM;
2058 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002059 }
2060 }
2061
2062 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002063 * step 3: now that we're guaranteed success wrt the css_sets,
2064 * proceed to move all tasks to the new cgroup. There are no
2065 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002066 */
Ben Blum74a11662011-05-26 16:25:20 -07002067 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002068 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002069 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002070 }
2071 /* nothing is sensitive to fork() after this point. */
2072
2073 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002074 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002075 */
Tejun Heo5549c492013-06-24 15:21:48 -07002076 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002077 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002078
Ben Blum74a11662011-05-26 16:25:20 -07002079 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002080 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002081 }
2082
2083 /*
2084 * step 5: success! and cleanup
2085 */
Ben Blum74a11662011-05-26 16:25:20 -07002086 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002087out_put_css_set_refs:
2088 if (retval) {
2089 for (i = 0; i < group_size; i++) {
2090 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002091 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002092 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002093 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002094 }
Ben Blum74a11662011-05-26 16:25:20 -07002095 }
2096out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002097 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002098 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002099 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002100
Tejun Heo494c1672011-12-12 18:12:22 -08002101 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002102 break;
Ben Blum74a11662011-05-26 16:25:20 -07002103 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002104 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002105 }
2106 }
Ben Blum74a11662011-05-26 16:25:20 -07002107out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002108 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002109 return retval;
2110}
2111
2112/*
2113 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002114 * function to attach either it or all tasks in its threadgroup. Will lock
2115 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002116 */
2117static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002118{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002120 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002121 int ret;
2122
Ben Blum74a11662011-05-26 16:25:20 -07002123 if (!cgroup_lock_live_group(cgrp))
2124 return -ENODEV;
2125
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002126retry_find_task:
2127 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002129 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002130 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002131 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002132 ret= -ESRCH;
2133 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002134 }
Ben Blum74a11662011-05-26 16:25:20 -07002135 /*
2136 * even if we're attaching all tasks in the thread group, we
2137 * only need to check permissions on one of them.
2138 */
David Howellsc69e8d92008-11-14 10:39:19 +11002139 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002140 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2141 !uid_eq(cred->euid, tcred->uid) &&
2142 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002143 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002144 ret = -EACCES;
2145 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002146 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147 } else
2148 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002149
2150 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002151 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002152
2153 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002154 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002155 * trapped in a cpuset, or RT worker may be born in a cgroup
2156 * with no rt_runtime allocated. Just say no.
2157 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002158 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002159 ret = -EINVAL;
2160 rcu_read_unlock();
2161 goto out_unlock_cgroup;
2162 }
2163
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002164 get_task_struct(tsk);
2165 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002166
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167 threadgroup_lock(tsk);
2168 if (threadgroup) {
2169 if (!thread_group_leader(tsk)) {
2170 /*
2171 * a race with de_thread from another thread's exec()
2172 * may strip us of our leadership, if this happens,
2173 * there is no choice but to throw this task away and
2174 * try again; this is
2175 * "double-double-toil-and-trouble-check locking".
2176 */
2177 threadgroup_unlock(tsk);
2178 put_task_struct(tsk);
2179 goto retry_find_task;
2180 }
Li Zefan081aa452013-03-13 09:17:09 +08002181 }
2182
2183 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2184
Tejun Heocd3d0952011-12-12 18:12:21 -08002185 threadgroup_unlock(tsk);
2186
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002189 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002190 return ret;
2191}
2192
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002193/**
2194 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2195 * @from: attach to all cgroups of a given task
2196 * @tsk: the task to be attached
2197 */
2198int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2199{
2200 struct cgroupfs_root *root;
2201 int retval = 0;
2202
Tejun Heo47cfcd02013-04-07 09:29:51 -07002203 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002204 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002205 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002206
Li Zefan6f4b7e62013-07-31 16:18:36 +08002207 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002208 if (retval)
2209 break;
2210 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002211 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002212
2213 return retval;
2214}
2215EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2216
Tejun Heo182446d2013-08-08 20:11:24 -04002217static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2218 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002219{
Tejun Heo182446d2013-08-08 20:11:24 -04002220 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002221}
2222
Tejun Heo182446d2013-08-08 20:11:24 -04002223static int cgroup_procs_write(struct cgroup_subsys_state *css,
2224 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002225{
Tejun Heo182446d2013-08-08 20:11:24 -04002226 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002227}
2228
Tejun Heo182446d2013-08-08 20:11:24 -04002229static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2230 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002231{
Tejun Heo182446d2013-08-08 20:11:24 -04002232 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002233 if (strlen(buffer) >= PATH_MAX)
2234 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002235 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002236 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002237 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002238 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002239 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002241 return 0;
2242}
2243
Tejun Heo182446d2013-08-08 20:11:24 -04002244static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2245 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002246{
Tejun Heo182446d2013-08-08 20:11:24 -04002247 struct cgroup *cgrp = css->cgroup;
2248
Paul Menagee788e062008-07-25 01:46:59 -07002249 if (!cgroup_lock_live_group(cgrp))
2250 return -ENODEV;
2251 seq_puts(seq, cgrp->root->release_agent_path);
2252 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002254 return 0;
2255}
2256
Tejun Heo182446d2013-08-08 20:11:24 -04002257static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2258 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002259{
Tejun Heo182446d2013-08-08 20:11:24 -04002260 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002261 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002262}
2263
Paul Menage84eea842008-07-25 01:47:00 -07002264/* A buffer size big enough for numbers or short strings */
2265#define CGROUP_LOCAL_BUFFER_SIZE 64
2266
Tejun Heo182446d2013-08-08 20:11:24 -04002267static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2268 struct cftype *cft, struct file *file,
2269 const char __user *userbuf, size_t nbytes,
2270 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002271{
Paul Menage84eea842008-07-25 01:47:00 -07002272 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002273 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002274 char *end;
2275
2276 if (!nbytes)
2277 return -EINVAL;
2278 if (nbytes >= sizeof(buffer))
2279 return -E2BIG;
2280 if (copy_from_user(buffer, userbuf, nbytes))
2281 return -EFAULT;
2282
2283 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002284 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002285 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002286 if (*end)
2287 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002288 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002289 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002290 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002291 if (*end)
2292 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002293 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002294 }
Paul Menage355e0c42007-10-18 23:39:33 -07002295 if (!retval)
2296 retval = nbytes;
2297 return retval;
2298}
2299
Tejun Heo182446d2013-08-08 20:11:24 -04002300static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2301 struct cftype *cft, struct file *file,
2302 const char __user *userbuf, size_t nbytes,
2303 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002304{
Paul Menage84eea842008-07-25 01:47:00 -07002305 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002306 int retval = 0;
2307 size_t max_bytes = cft->max_write_len;
2308 char *buffer = local_buffer;
2309
2310 if (!max_bytes)
2311 max_bytes = sizeof(local_buffer) - 1;
2312 if (nbytes >= max_bytes)
2313 return -E2BIG;
2314 /* Allocate a dynamic buffer if we need one */
2315 if (nbytes >= sizeof(local_buffer)) {
2316 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2317 if (buffer == NULL)
2318 return -ENOMEM;
2319 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002320 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2321 retval = -EFAULT;
2322 goto out;
2323 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002324
2325 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002326 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002327 if (!retval)
2328 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002329out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002330 if (buffer != local_buffer)
2331 kfree(buffer);
2332 return retval;
2333}
2334
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002336 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002337{
Tejun Heo182446d2013-08-08 20:11:24 -04002338 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002340 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002341
Paul Menage355e0c42007-10-18 23:39:33 -07002342 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002343 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002344 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002345 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002346 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002347 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002348 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002349 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002350 return ret ? ret : nbytes;
2351 }
Paul Menage355e0c42007-10-18 23:39:33 -07002352 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353}
2354
Tejun Heo182446d2013-08-08 20:11:24 -04002355static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2356 struct cftype *cft, struct file *file,
2357 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358{
Paul Menage84eea842008-07-25 01:47:00 -07002359 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002360 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2362
2363 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2364}
2365
Tejun Heo182446d2013-08-08 20:11:24 -04002366static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2367 struct cftype *cft, struct file *file,
2368 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002369{
Paul Menage84eea842008-07-25 01:47:00 -07002370 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002371 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002372 int len = sprintf(tmp, "%lld\n", (long long) val);
2373
2374 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2375}
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002378 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379{
Tejun Heo182446d2013-08-08 20:11:24 -04002380 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002381 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002382 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383
2384 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002385 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002386 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002387 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002388 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002389 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390 return -EINVAL;
2391}
2392
Paul Menage91796562008-04-29 01:00:01 -07002393/*
2394 * seqfile ops/methods for returning structured data. Currently just
2395 * supports string->u64 maps, but can be extended in future.
2396 */
2397
Paul Menage91796562008-04-29 01:00:01 -07002398static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2399{
2400 struct seq_file *sf = cb->state;
2401 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2402}
2403
2404static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2405{
Li Zefane0798ce2013-07-31 17:36:25 +08002406 struct cfent *cfe = m->private;
2407 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002408 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002409
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002410 if (cft->read_map) {
2411 struct cgroup_map_cb cb = {
2412 .fill = cgroup_map_add,
2413 .state = m,
2414 };
Tejun Heo182446d2013-08-08 20:11:24 -04002415 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002416 }
Tejun Heo182446d2013-08-08 20:11:24 -04002417 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002418}
2419
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002420static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002421 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002422 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002423 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002424 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002425};
2426
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427static int cgroup_file_open(struct inode *inode, struct file *file)
2428{
Tejun Heof7d58812013-08-08 20:11:23 -04002429 struct cfent *cfe = __d_cfe(file->f_dentry);
2430 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002431 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2432 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434
2435 err = generic_file_open(inode, file);
2436 if (err)
2437 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002438
2439 /*
2440 * If the file belongs to a subsystem, pin the css. Will be
2441 * unpinned either on open failure or release. This ensures that
2442 * @css stays alive for all file operations.
2443 */
Tejun Heo105347b2013-08-13 11:01:55 -04002444 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002445 css = cgroup_css(cgrp, cft->ss);
2446 if (cft->ss && !css_tryget(css))
2447 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002448 rcu_read_unlock();
2449
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002450 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002451 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002452
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002453 /*
2454 * @cfe->css is used by read/write/close to determine the
2455 * associated css. @file->private_data would be a better place but
2456 * that's already used by seqfile. Multiple accessors may use it
2457 * simultaneously which is okay as the association never changes.
2458 */
2459 WARN_ON_ONCE(cfe->css && cfe->css != css);
2460 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002462 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002463 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002464 err = single_open(file, cgroup_seqfile_show, cfe);
2465 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002466 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002467 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002468
Tejun Heo67f4c362013-08-08 20:11:24 -04002469 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002470 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002471 return err;
2472}
2473
2474static int cgroup_file_release(struct inode *inode, struct file *file)
2475{
Tejun Heof7d58812013-08-08 20:11:23 -04002476 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002478 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002479 int ret = 0;
2480
Paul Menageddbcc7e2007-10-18 23:39:30 -07002481 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002482 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002483 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002484 css_put(css);
2485 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486}
2487
2488/*
2489 * cgroup_rename - Only allow simple rename of directories in place.
2490 */
2491static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2492 struct inode *new_dir, struct dentry *new_dentry)
2493{
Li Zefan65dff752013-03-01 15:01:56 +08002494 int ret;
2495 struct cgroup_name *name, *old_name;
2496 struct cgroup *cgrp;
2497
2498 /*
2499 * It's convinient to use parent dir's i_mutex to protected
2500 * cgrp->name.
2501 */
2502 lockdep_assert_held(&old_dir->i_mutex);
2503
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2505 return -ENOTDIR;
2506 if (new_dentry->d_inode)
2507 return -EEXIST;
2508 if (old_dir != new_dir)
2509 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002510
2511 cgrp = __d_cgrp(old_dentry);
2512
Tejun Heo6db8e852013-06-14 11:18:22 -07002513 /*
2514 * This isn't a proper migration and its usefulness is very
2515 * limited. Disallow if sane_behavior.
2516 */
2517 if (cgroup_sane_behavior(cgrp))
2518 return -EPERM;
2519
Li Zefan65dff752013-03-01 15:01:56 +08002520 name = cgroup_alloc_name(new_dentry);
2521 if (!name)
2522 return -ENOMEM;
2523
2524 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2525 if (ret) {
2526 kfree(name);
2527 return ret;
2528 }
2529
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002530 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002531 rcu_assign_pointer(cgrp->name, name);
2532
2533 kfree_rcu(old_name, rcu_head);
2534 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002535}
2536
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002537static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2538{
2539 if (S_ISDIR(dentry->d_inode->i_mode))
2540 return &__d_cgrp(dentry)->xattrs;
2541 else
Li Zefan712317a2013-04-18 23:09:52 -07002542 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002543}
2544
2545static inline int xattr_enabled(struct dentry *dentry)
2546{
2547 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002548 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002549}
2550
2551static bool is_valid_xattr(const char *name)
2552{
2553 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2554 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2555 return true;
2556 return false;
2557}
2558
2559static int cgroup_setxattr(struct dentry *dentry, const char *name,
2560 const void *val, size_t size, int flags)
2561{
2562 if (!xattr_enabled(dentry))
2563 return -EOPNOTSUPP;
2564 if (!is_valid_xattr(name))
2565 return -EINVAL;
2566 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2567}
2568
2569static int cgroup_removexattr(struct dentry *dentry, const char *name)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 if (!is_valid_xattr(name))
2574 return -EINVAL;
2575 return simple_xattr_remove(__d_xattrs(dentry), name);
2576}
2577
2578static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2579 void *buf, size_t size)
2580{
2581 if (!xattr_enabled(dentry))
2582 return -EOPNOTSUPP;
2583 if (!is_valid_xattr(name))
2584 return -EINVAL;
2585 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2586}
2587
2588static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2589{
2590 if (!xattr_enabled(dentry))
2591 return -EOPNOTSUPP;
2592 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2593}
2594
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002595static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 .read = cgroup_file_read,
2597 .write = cgroup_file_write,
2598 .llseek = generic_file_llseek,
2599 .open = cgroup_file_open,
2600 .release = cgroup_file_release,
2601};
2602
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002603static const struct inode_operations cgroup_file_inode_operations = {
2604 .setxattr = cgroup_setxattr,
2605 .getxattr = cgroup_getxattr,
2606 .listxattr = cgroup_listxattr,
2607 .removexattr = cgroup_removexattr,
2608};
2609
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002610static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002611 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002612 .mkdir = cgroup_mkdir,
2613 .rmdir = cgroup_rmdir,
2614 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615 .setxattr = cgroup_setxattr,
2616 .getxattr = cgroup_getxattr,
2617 .listxattr = cgroup_listxattr,
2618 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619};
2620
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002621/*
2622 * Check if a file is a control file
2623 */
2624static inline struct cftype *__file_cft(struct file *file)
2625{
Al Viro496ad9a2013-01-23 17:07:38 -05002626 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002627 return ERR_PTR(-EINVAL);
2628 return __d_cft(file->f_dentry);
2629}
2630
Al Viroa5e7ed32011-07-26 01:55:55 -04002631static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002632 struct super_block *sb)
2633{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634 struct inode *inode;
2635
2636 if (!dentry)
2637 return -ENOENT;
2638 if (dentry->d_inode)
2639 return -EEXIST;
2640
2641 inode = cgroup_new_inode(mode, sb);
2642 if (!inode)
2643 return -ENOMEM;
2644
2645 if (S_ISDIR(mode)) {
2646 inode->i_op = &cgroup_dir_inode_operations;
2647 inode->i_fop = &simple_dir_operations;
2648
2649 /* start off with i_nlink == 2 (for "." entry) */
2650 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002651 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652
Tejun Heob8a2df62012-11-19 08:13:37 -08002653 /*
2654 * Control reaches here with cgroup_mutex held.
2655 * @inode->i_mutex should nest outside cgroup_mutex but we
2656 * want to populate it immediately without releasing
2657 * cgroup_mutex. As @inode isn't visible to anyone else
2658 * yet, trylock will always succeed without affecting
2659 * lockdep checks.
2660 */
2661 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002662 } else if (S_ISREG(mode)) {
2663 inode->i_size = 0;
2664 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002665 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002666 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667 d_instantiate(dentry, inode);
2668 dget(dentry); /* Extra count - pin the dentry in core */
2669 return 0;
2670}
2671
Li Zefan099fca32009-04-02 16:57:29 -07002672/**
2673 * cgroup_file_mode - deduce file mode of a control file
2674 * @cft: the control file in question
2675 *
2676 * returns cft->mode if ->mode is not 0
2677 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2678 * returns S_IRUGO if it has only a read handler
2679 * returns S_IWUSR if it has only a write hander
2680 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002681static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002682{
Al Viroa5e7ed32011-07-26 01:55:55 -04002683 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002684
2685 if (cft->mode)
2686 return cft->mode;
2687
2688 if (cft->read || cft->read_u64 || cft->read_s64 ||
2689 cft->read_map || cft->read_seq_string)
2690 mode |= S_IRUGO;
2691
2692 if (cft->write || cft->write_u64 || cft->write_s64 ||
2693 cft->write_string || cft->trigger)
2694 mode |= S_IWUSR;
2695
2696 return mode;
2697}
2698
Tejun Heo2bb566c2013-08-08 20:11:23 -04002699static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002700{
Paul Menagebd89aab2007-10-18 23:40:44 -07002701 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002704 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002706 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002708
Tejun Heo9fa4db32013-08-26 18:40:56 -04002709 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2710 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002711 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 strcat(name, ".");
2713 }
2714 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002717
2718 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2719 if (!cfe)
2720 return -ENOMEM;
2721
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725 goto out;
2726 }
2727
Li Zefand6cbf352013-05-14 19:44:20 +08002728 cfe->type = (void *)cft;
2729 cfe->dentry = dentry;
2730 dentry->d_fsdata = cfe;
2731 simple_xattrs_init(&cfe->xattrs);
2732
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733 mode = cgroup_file_mode(cft);
2734 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2735 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002736 list_add_tail(&cfe->node, &parent->files);
2737 cfe = NULL;
2738 }
2739 dput(dentry);
2740out:
2741 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 return error;
2743}
2744
Tejun Heob1f28d32013-06-28 16:24:10 -07002745/**
2746 * cgroup_addrm_files - add or remove files to a cgroup directory
2747 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002748 * @cfts: array of cftypes to be added
2749 * @is_add: whether to add or remove
2750 *
2751 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002752 * For removals, this function never fails. If addition fails, this
2753 * function doesn't remove files already added. The caller is responsible
2754 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002755 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002756static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2757 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002759 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002760 int ret;
2761
2762 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2763 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002764
2765 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002766 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002767 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2768 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002769 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2770 continue;
2771 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2772 continue;
2773
Li Zefan2739d3c2013-01-21 18:18:33 +08002774 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002775 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002776 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002777 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002778 cft->name, ret);
2779 return ret;
2780 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002781 } else {
2782 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002783 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002785 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786}
2787
Tejun Heo8e3f6542012-04-01 12:09:55 -07002788static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002789 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002790{
2791 /*
2792 * Thanks to the entanglement with vfs inode locking, we can't walk
2793 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002794 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2795 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 mutex_lock(&cgroup_mutex);
2798}
2799
Tejun Heo2bb566c2013-08-08 20:11:23 -04002800static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002801 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002802{
2803 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002804 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002805 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002806 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002807 struct dentry *prev = NULL;
2808 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002809 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002810 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002811 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002812
2813 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002814 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002815 !atomic_inc_not_zero(&sb->s_active)) {
2816 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002817 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002818 }
2819
Li Zefane8c82d22013-06-18 18:48:37 +08002820 /*
2821 * All cgroups which are created after we drop cgroup_mutex will
2822 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002823 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002824 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002825 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002826
Tejun Heo8e3f6542012-04-01 12:09:55 -07002827 mutex_unlock(&cgroup_mutex);
2828
Li Zefane8c82d22013-06-18 18:48:37 +08002829 /* add/rm files for all cgroups created before */
2830 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002831 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002832 struct cgroup *cgrp = css->cgroup;
2833
Li Zefane8c82d22013-06-18 18:48:37 +08002834 if (cgroup_is_dead(cgrp))
2835 continue;
2836
2837 inode = cgrp->dentry->d_inode;
2838 dget(cgrp->dentry);
2839 rcu_read_unlock();
2840
2841 dput(prev);
2842 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002843
2844 mutex_lock(&inode->i_mutex);
2845 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002846 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002847 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848 mutex_unlock(&cgroup_mutex);
2849 mutex_unlock(&inode->i_mutex);
2850
Li Zefane8c82d22013-06-18 18:48:37 +08002851 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002852 if (ret)
2853 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854 }
Li Zefane8c82d22013-06-18 18:48:37 +08002855 rcu_read_unlock();
2856 dput(prev);
2857 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002858 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859}
2860
2861/**
2862 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2863 * @ss: target cgroup subsystem
2864 * @cfts: zero-length name terminated array of cftypes
2865 *
2866 * Register @cfts to @ss. Files described by @cfts are created for all
2867 * existing cgroups to which @ss is attached and all future cgroups will
2868 * have them too. This function can be called anytime whether @ss is
2869 * attached or not.
2870 *
2871 * Returns 0 on successful registration, -errno on failure. Note that this
2872 * function currently returns 0 as long as @cfts registration is successful
2873 * even if some file creation attempts on existing cgroups fail.
2874 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002875int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002876{
2877 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002878 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002879 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002880
2881 set = kzalloc(sizeof(*set), GFP_KERNEL);
2882 if (!set)
2883 return -ENOMEM;
2884
Tejun Heo2bb566c2013-08-08 20:11:23 -04002885 for (cft = cfts; cft->name[0] != '\0'; cft++)
2886 cft->ss = ss;
2887
Tejun Heo8e3f6542012-04-01 12:09:55 -07002888 cgroup_cfts_prepare();
2889 set->cfts = cfts;
2890 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002891 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002892 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002893 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002894 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002895}
2896EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2897
Li Zefana043e3b2008-02-23 15:24:09 -08002898/**
Tejun Heo79578622012-04-01 12:09:56 -07002899 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002900 * @cfts: zero-length name terminated array of cftypes
2901 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002902 * Unregister @cfts. Files described by @cfts are removed from all
2903 * existing cgroups and all future cgroups won't have them either. This
2904 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002905 *
2906 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002907 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002908 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002909int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002910{
2911 struct cftype_set *set;
2912
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913 if (!cfts || !cfts[0].ss)
2914 return -ENOENT;
2915
Tejun Heo79578622012-04-01 12:09:56 -07002916 cgroup_cfts_prepare();
2917
Tejun Heo2bb566c2013-08-08 20:11:23 -04002918 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002919 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002920 list_del(&set->node);
2921 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002922 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002923 return 0;
2924 }
2925 }
2926
Tejun Heo2bb566c2013-08-08 20:11:23 -04002927 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002928 return -ENOENT;
2929}
2930
2931/**
Li Zefana043e3b2008-02-23 15:24:09 -08002932 * cgroup_task_count - count the number of tasks in a cgroup.
2933 * @cgrp: the cgroup in question
2934 *
2935 * Return the number of tasks in the cgroup.
2936 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002937int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002938{
2939 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002940 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002941
Paul Menage817929e2007-10-18 23:39:36 -07002942 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002943 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2944 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002945 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002946 return count;
2947}
2948
2949/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002950 * To reduce the fork() overhead for systems that are not actually using
2951 * their cgroups capability, we don't maintain the lists running through
2952 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002953 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002954 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002955static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002956{
2957 struct task_struct *p, *g;
2958 write_lock(&css_set_lock);
2959 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002960 /*
2961 * We need tasklist_lock because RCU is not safe against
2962 * while_each_thread(). Besides, a forking task that has passed
2963 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2964 * is not guaranteed to have its child immediately visible in the
2965 * tasklist if we walk through it with RCU.
2966 */
2967 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002968 do_each_thread(g, p) {
2969 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002970 /*
2971 * We should check if the process is exiting, otherwise
2972 * it will race with cgroup_exit() in that the list
2973 * entry won't be deleted though the process has exited.
2974 */
2975 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002976 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002977 task_unlock(p);
2978 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002979 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002980 write_unlock(&css_set_lock);
2981}
2982
Tejun Heo574bd9f2012-11-09 09:12:29 -08002983/**
Tejun Heo492eb212013-08-08 20:11:25 -04002984 * css_next_child - find the next child of a given css
2985 * @pos_css: the current position (%NULL to initiate traversal)
2986 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002987 *
Tejun Heo492eb212013-08-08 20:11:25 -04002988 * This function returns the next child of @parent_css and should be called
2989 * under RCU read lock. The only requirement is that @parent_css and
2990 * @pos_css are accessible. The next sibling is guaranteed to be returned
2991 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002992 */
Tejun Heo492eb212013-08-08 20:11:25 -04002993struct cgroup_subsys_state *
2994css_next_child(struct cgroup_subsys_state *pos_css,
2995 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002996{
Tejun Heo492eb212013-08-08 20:11:25 -04002997 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2998 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002999 struct cgroup *next;
3000
3001 WARN_ON_ONCE(!rcu_read_lock_held());
3002
3003 /*
3004 * @pos could already have been removed. Once a cgroup is removed,
3005 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003006 * changes. As CGRP_DEAD assertion is serialized and happens
3007 * before the cgroup is taken off the ->sibling list, if we see it
3008 * unasserted, it's guaranteed that the next sibling hasn't
3009 * finished its grace period even if it's already removed, and thus
3010 * safe to dereference from this RCU critical section. If
3011 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3012 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003013 *
3014 * If @pos is dead, its next pointer can't be dereferenced;
3015 * however, as each cgroup is given a monotonically increasing
3016 * unique serial number and always appended to the sibling list,
3017 * the next one can be found by walking the parent's children until
3018 * we see a cgroup with higher serial number than @pos's. While
3019 * this path can be slower, it's taken only when either the current
3020 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003021 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003022 if (!pos) {
3023 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3024 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003025 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003026 } else {
3027 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3028 if (next->serial_nr > pos->serial_nr)
3029 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003030 }
3031
Tejun Heo492eb212013-08-08 20:11:25 -04003032 if (&next->sibling == &cgrp->children)
3033 return NULL;
3034
Tejun Heoca8bdca2013-08-26 18:40:56 -04003035 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003036}
Tejun Heo492eb212013-08-08 20:11:25 -04003037EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003038
3039/**
Tejun Heo492eb212013-08-08 20:11:25 -04003040 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003041 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003042 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003043 *
Tejun Heo492eb212013-08-08 20:11:25 -04003044 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003045 * to visit for pre-order traversal of @root's descendants. @root is
3046 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003047 *
3048 * While this function requires RCU read locking, it doesn't require the
3049 * whole traversal to be contained in a single RCU critical section. This
3050 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003051 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003052 */
Tejun Heo492eb212013-08-08 20:11:25 -04003053struct cgroup_subsys_state *
3054css_next_descendant_pre(struct cgroup_subsys_state *pos,
3055 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003056{
Tejun Heo492eb212013-08-08 20:11:25 -04003057 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003058
3059 WARN_ON_ONCE(!rcu_read_lock_held());
3060
Tejun Heobd8815a2013-08-08 20:11:27 -04003061 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003062 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003063 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064
3065 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003066 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067 if (next)
3068 return next;
3069
3070 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003071 while (pos != root) {
3072 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003073 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003075 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003076 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077
3078 return NULL;
3079}
Tejun Heo492eb212013-08-08 20:11:25 -04003080EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003081
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003082/**
Tejun Heo492eb212013-08-08 20:11:25 -04003083 * css_rightmost_descendant - return the rightmost descendant of a css
3084 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003085 *
Tejun Heo492eb212013-08-08 20:11:25 -04003086 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3087 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003088 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003089 *
3090 * While this function requires RCU read locking, it doesn't require the
3091 * whole traversal to be contained in a single RCU critical section. This
3092 * function will return the correct rightmost descendant as long as @pos is
3093 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003094 */
Tejun Heo492eb212013-08-08 20:11:25 -04003095struct cgroup_subsys_state *
3096css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003097{
Tejun Heo492eb212013-08-08 20:11:25 -04003098 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003099
3100 WARN_ON_ONCE(!rcu_read_lock_held());
3101
3102 do {
3103 last = pos;
3104 /* ->prev isn't RCU safe, walk ->next till the end */
3105 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003106 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003107 pos = tmp;
3108 } while (pos);
3109
3110 return last;
3111}
Tejun Heo492eb212013-08-08 20:11:25 -04003112EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003113
Tejun Heo492eb212013-08-08 20:11:25 -04003114static struct cgroup_subsys_state *
3115css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003116{
Tejun Heo492eb212013-08-08 20:11:25 -04003117 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118
3119 do {
3120 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003121 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003122 } while (pos);
3123
3124 return last;
3125}
3126
3127/**
Tejun Heo492eb212013-08-08 20:11:25 -04003128 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003129 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003130 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003131 *
Tejun Heo492eb212013-08-08 20:11:25 -04003132 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003133 * to visit for post-order traversal of @root's descendants. @root is
3134 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003135 *
3136 * While this function requires RCU read locking, it doesn't require the
3137 * whole traversal to be contained in a single RCU critical section. This
3138 * function will return the correct next descendant as long as both @pos
3139 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003140 */
Tejun Heo492eb212013-08-08 20:11:25 -04003141struct cgroup_subsys_state *
3142css_next_descendant_post(struct cgroup_subsys_state *pos,
3143 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003144{
Tejun Heo492eb212013-08-08 20:11:25 -04003145 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003146
3147 WARN_ON_ONCE(!rcu_read_lock_held());
3148
Tejun Heo58b79a92013-09-06 15:31:08 -04003149 /* if first iteration, visit leftmost descendant which may be @root */
3150 if (!pos)
3151 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003152
Tejun Heobd8815a2013-08-08 20:11:27 -04003153 /* if we visited @root, we're done */
3154 if (pos == root)
3155 return NULL;
3156
Tejun Heo574bd9f2012-11-09 09:12:29 -08003157 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003158 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003159 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003160 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003161
3162 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003163 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003164}
Tejun Heo492eb212013-08-08 20:11:25 -04003165EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003166
Tejun Heo0942eee2013-08-08 20:11:26 -04003167/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003168 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003169 * @it: the iterator to advance
3170 *
3171 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003172 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003173static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003174{
3175 struct list_head *l = it->cset_link;
3176 struct cgrp_cset_link *link;
3177 struct css_set *cset;
3178
3179 /* Advance to the next non-empty css_set */
3180 do {
3181 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003182 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003183 it->cset_link = NULL;
3184 return;
3185 }
3186 link = list_entry(l, struct cgrp_cset_link, cset_link);
3187 cset = link->cset;
3188 } while (list_empty(&cset->tasks));
3189 it->cset_link = l;
3190 it->task = cset->tasks.next;
3191}
3192
Tejun Heo0942eee2013-08-08 20:11:26 -04003193/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003194 * css_task_iter_start - initiate task iteration
3195 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003196 * @it: the task iterator to use
3197 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003198 * Initiate iteration through the tasks of @css. The caller can call
3199 * css_task_iter_next() to walk through the tasks until the function
3200 * returns NULL. On completion of iteration, css_task_iter_end() must be
3201 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003202 *
3203 * Note that this function acquires a lock which is released when the
3204 * iteration finishes. The caller can't sleep while iteration is in
3205 * progress.
3206 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003207void css_task_iter_start(struct cgroup_subsys_state *css,
3208 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003209 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003210{
3211 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003212 * The first time anyone tries to iterate across a css, we need to
3213 * enable the list linking each css_set to its tasks, and fix up
3214 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003215 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003216 if (!use_task_css_set_links)
3217 cgroup_enable_task_cg_lists();
3218
Paul Menage817929e2007-10-18 23:39:36 -07003219 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003220
Tejun Heo72ec7022013-08-08 20:11:26 -04003221 it->origin_css = css;
3222 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003223
Tejun Heo72ec7022013-08-08 20:11:26 -04003224 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003225}
3226
Tejun Heo0942eee2013-08-08 20:11:26 -04003227/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003228 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003229 * @it: the task iterator being iterated
3230 *
3231 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003232 * initialized via css_task_iter_start(). Returns NULL when the iteration
3233 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003234 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003235struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003236{
3237 struct task_struct *res;
3238 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003239 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003240
3241 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003242 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003243 return NULL;
3244 res = list_entry(l, struct task_struct, cg_list);
3245 /* Advance iterator to find next entry */
3246 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003247 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3248 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003249 /*
3250 * We reached the end of this task list - move on to the
3251 * next cgrp_cset_link.
3252 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003253 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003254 } else {
3255 it->task = l;
3256 }
3257 return res;
3258}
3259
Tejun Heo0942eee2013-08-08 20:11:26 -04003260/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003261 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003262 * @it: the task iterator to finish
3263 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003265 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003266void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003267 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003268{
3269 read_unlock(&css_set_lock);
3270}
3271
Cliff Wickman31a7df02008-02-07 00:14:42 -08003272static inline int started_after_time(struct task_struct *t1,
3273 struct timespec *time,
3274 struct task_struct *t2)
3275{
3276 int start_diff = timespec_compare(&t1->start_time, time);
3277 if (start_diff > 0) {
3278 return 1;
3279 } else if (start_diff < 0) {
3280 return 0;
3281 } else {
3282 /*
3283 * Arbitrarily, if two processes started at the same
3284 * time, we'll say that the lower pointer value
3285 * started first. Note that t2 may have exited by now
3286 * so this may not be a valid pointer any longer, but
3287 * that's fine - it still serves to distinguish
3288 * between two tasks started (effectively) simultaneously.
3289 */
3290 return t1 > t2;
3291 }
3292}
3293
3294/*
3295 * This function is a callback from heap_insert() and is used to order
3296 * the heap.
3297 * In this case we order the heap in descending task start time.
3298 */
3299static inline int started_after(void *p1, void *p2)
3300{
3301 struct task_struct *t1 = p1;
3302 struct task_struct *t2 = p2;
3303 return started_after_time(t1, &t2->start_time, t2);
3304}
3305
3306/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003307 * css_scan_tasks - iterate though all the tasks in a css
3308 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003309 * @test: optional test callback
3310 * @process: process callback
3311 * @data: data passed to @test and @process
3312 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003313 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003314 * Iterate through all the tasks in @css, calling @test for each, and if it
3315 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003316 *
Tejun Heoe5358372013-08-08 20:11:26 -04003317 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003318 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003319 * lock css_set_lock for the call to @process.
3320 *
3321 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003322 * of @css for the duration of this call. This function may or may not
3323 * call @process for tasks that exit or move to a different css during the
3324 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003325 *
3326 * Note that @test may be called with locks held, and may in some
3327 * situations be called multiple times for the same task, so it should be
3328 * cheap.
3329 *
3330 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3331 * heap operations (and its "gt" member will be overwritten), else a
3332 * temporary heap will be used (allocation of which may cause this function
3333 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003334 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003335int css_scan_tasks(struct cgroup_subsys_state *css,
3336 bool (*test)(struct task_struct *, void *),
3337 void (*process)(struct task_struct *, void *),
3338 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003339{
3340 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003341 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003342 struct task_struct *p, *dropped;
3343 /* Never dereference latest_task, since it's not refcounted */
3344 struct task_struct *latest_task = NULL;
3345 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003346 struct timespec latest_time = { 0, 0 };
3347
Tejun Heoe5358372013-08-08 20:11:26 -04003348 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003349 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350 heap->gt = &started_after;
3351 } else {
3352 /* We need to allocate our own heap memory */
3353 heap = &tmp_heap;
3354 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3355 if (retval)
3356 /* cannot allocate the heap */
3357 return retval;
3358 }
3359
3360 again:
3361 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003362 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003363 * which are of interest, and invoking @process callback on the
3364 * ones which need an update. Since we don't want to hold any
3365 * locks during the task updates, gather tasks to be processed in a
3366 * heap structure. The heap is sorted by descending task start
3367 * time. If the statically-sized heap fills up, we overflow tasks
3368 * that started later, and in future iterations only consider tasks
3369 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003370 * guarantees forward progress and that we don't miss any tasks.
3371 */
3372 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 css_task_iter_start(css, &it);
3374 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003375 /*
3376 * Only affect tasks that qualify per the caller's callback,
3377 * if he provided one
3378 */
Tejun Heoe5358372013-08-08 20:11:26 -04003379 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003380 continue;
3381 /*
3382 * Only process tasks that started after the last task
3383 * we processed
3384 */
3385 if (!started_after_time(p, &latest_time, latest_task))
3386 continue;
3387 dropped = heap_insert(heap, p);
3388 if (dropped == NULL) {
3389 /*
3390 * The new task was inserted; the heap wasn't
3391 * previously full
3392 */
3393 get_task_struct(p);
3394 } else if (dropped != p) {
3395 /*
3396 * The new task was inserted, and pushed out a
3397 * different task
3398 */
3399 get_task_struct(p);
3400 put_task_struct(dropped);
3401 }
3402 /*
3403 * Else the new task was newer than anything already in
3404 * the heap and wasn't inserted
3405 */
3406 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003407 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003408
3409 if (heap->size) {
3410 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003411 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003412 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003413 latest_time = q->start_time;
3414 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003415 }
3416 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003417 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003418 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 }
3420 /*
3421 * If we had to process any tasks at all, scan again
3422 * in case some of them were in the middle of forking
3423 * children that didn't get processed.
3424 * Not the most efficient way to do it, but it avoids
3425 * having to take callback_mutex in the fork path
3426 */
3427 goto again;
3428 }
3429 if (heap == &tmp_heap)
3430 heap_free(&tmp_heap);
3431 return 0;
3432}
3433
Tejun Heoe5358372013-08-08 20:11:26 -04003434static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003435{
Tejun Heoe5358372013-08-08 20:11:26 -04003436 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003437
Tejun Heo47cfcd02013-04-07 09:29:51 -07003438 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003439 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003440 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003441}
3442
3443/**
3444 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3445 * @to: cgroup to which the tasks will be moved
3446 * @from: cgroup in which the tasks currently reside
3447 */
3448int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3449{
Tejun Heo72ec7022013-08-08 20:11:26 -04003450 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3451 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003452}
3453
Paul Menage817929e2007-10-18 23:39:36 -07003454/*
Ben Blum102a7752009-09-23 15:56:26 -07003455 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003456 *
3457 * Reading this file can return large amounts of data if a cgroup has
3458 * *lots* of attached tasks. So it may need several calls to read(),
3459 * but we cannot guarantee that the information we produce is correct
3460 * unless we produce it entirely atomically.
3461 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003462 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003463
Li Zefan24528252012-01-20 11:58:43 +08003464/* which pidlist file are we talking about? */
3465enum cgroup_filetype {
3466 CGROUP_FILE_PROCS,
3467 CGROUP_FILE_TASKS,
3468};
3469
3470/*
3471 * A pidlist is a list of pids that virtually represents the contents of one
3472 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3473 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3474 * to the cgroup.
3475 */
3476struct cgroup_pidlist {
3477 /*
3478 * used to find which pidlist is wanted. doesn't change as long as
3479 * this particular list stays in the list.
3480 */
3481 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3482 /* array of xids */
3483 pid_t *list;
3484 /* how many elements the above list has */
3485 int length;
3486 /* how many files are using the current array */
3487 int use_count;
3488 /* each of these stored in a list by its cgroup */
3489 struct list_head links;
3490 /* pointer to the cgroup we belong to, for list removal purposes */
3491 struct cgroup *owner;
3492 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003493 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003494};
3495
Paul Menagebbcb81d2007-10-18 23:39:32 -07003496/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003497 * The following two functions "fix" the issue where there are more pids
3498 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3499 * TODO: replace with a kernel-wide solution to this problem
3500 */
3501#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3502static void *pidlist_allocate(int count)
3503{
3504 if (PIDLIST_TOO_LARGE(count))
3505 return vmalloc(count * sizeof(pid_t));
3506 else
3507 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3508}
3509static void pidlist_free(void *p)
3510{
3511 if (is_vmalloc_addr(p))
3512 vfree(p);
3513 else
3514 kfree(p);
3515}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003516
3517/*
Ben Blum102a7752009-09-23 15:56:26 -07003518 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003519 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003520 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003521static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003522{
Ben Blum102a7752009-09-23 15:56:26 -07003523 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003524
3525 /*
3526 * we presume the 0th element is unique, so i starts at 1. trivial
3527 * edge cases first; no work needs to be done for either
3528 */
3529 if (length == 0 || length == 1)
3530 return length;
3531 /* src and dest walk down the list; dest counts unique elements */
3532 for (src = 1; src < length; src++) {
3533 /* find next unique element */
3534 while (list[src] == list[src-1]) {
3535 src++;
3536 if (src == length)
3537 goto after;
3538 }
3539 /* dest always points to where the next unique element goes */
3540 list[dest] = list[src];
3541 dest++;
3542 }
3543after:
Ben Blum102a7752009-09-23 15:56:26 -07003544 return dest;
3545}
3546
3547static int cmppid(const void *a, const void *b)
3548{
3549 return *(pid_t *)a - *(pid_t *)b;
3550}
3551
3552/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003553 * find the appropriate pidlist for our purpose (given procs vs tasks)
3554 * returns with the lock on that pidlist already held, and takes care
3555 * of the use count, or returns NULL with no locks held if we're out of
3556 * memory.
3557 */
3558static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3559 enum cgroup_filetype type)
3560{
3561 struct cgroup_pidlist *l;
3562 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003563 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003564
Ben Blum72a8cb32009-09-23 15:56:27 -07003565 /*
Li Zefanb3958902013-08-01 09:52:15 +08003566 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003567 * the last ref-holder is trying to remove l from the list at the same
3568 * time. Holding the pidlist_mutex precludes somebody taking whichever
3569 * list we find out from under us - compare release_pid_array().
3570 */
3571 mutex_lock(&cgrp->pidlist_mutex);
3572 list_for_each_entry(l, &cgrp->pidlists, links) {
3573 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003574 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003575 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003576 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003577 return l;
3578 }
3579 }
3580 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003581 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 if (!l) {
3583 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 return l;
3585 }
Li Zefanb3958902013-08-01 09:52:15 +08003586 init_rwsem(&l->rwsem);
3587 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003588 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003589 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 l->owner = cgrp;
3591 list_add(&l->links, &cgrp->pidlists);
3592 mutex_unlock(&cgrp->pidlist_mutex);
3593 return l;
3594}
3595
3596/*
Ben Blum102a7752009-09-23 15:56:26 -07003597 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3598 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003599static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3600 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003601{
3602 pid_t *array;
3603 int length;
3604 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003605 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003606 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003607 struct cgroup_pidlist *l;
3608
3609 /*
3610 * If cgroup gets more users after we read count, we won't have
3611 * enough space - tough. This race is indistinguishable to the
3612 * caller from the case that the additional cgroup users didn't
3613 * show up until sometime later on.
3614 */
3615 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003616 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003617 if (!array)
3618 return -ENOMEM;
3619 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003620 css_task_iter_start(&cgrp->dummy_css, &it);
3621 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003622 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003623 break;
Ben Blum102a7752009-09-23 15:56:26 -07003624 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003625 if (type == CGROUP_FILE_PROCS)
3626 pid = task_tgid_vnr(tsk);
3627 else
3628 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003629 if (pid > 0) /* make sure to only use valid results */
3630 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003631 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003632 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003633 length = n;
3634 /* now sort & (if procs) strip out duplicates */
3635 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003637 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003638 l = cgroup_pidlist_find(cgrp, type);
3639 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003640 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003641 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003642 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003644 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003645 l->list = array;
3646 l->length = length;
3647 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003648 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003650 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003651}
3652
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653/**
Li Zefana043e3b2008-02-23 15:24:09 -08003654 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003655 * @stats: cgroupstats to fill information into
3656 * @dentry: A dentry entry belonging to the cgroup for which stats have
3657 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003658 *
3659 * Build and fill cgroupstats so that taskstats can export it to user
3660 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661 */
3662int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3663{
3664 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003665 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003666 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003667 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003668
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003670 * Validate dentry by checking the superblock operations,
3671 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003672 */
Li Zefan33d283b2008-11-19 15:36:48 -08003673 if (dentry->d_sb->s_op != &cgroup_ops ||
3674 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003675 goto err;
3676
3677 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003678 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003679
Tejun Heo72ec7022013-08-08 20:11:26 -04003680 css_task_iter_start(&cgrp->dummy_css, &it);
3681 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682 switch (tsk->state) {
3683 case TASK_RUNNING:
3684 stats->nr_running++;
3685 break;
3686 case TASK_INTERRUPTIBLE:
3687 stats->nr_sleeping++;
3688 break;
3689 case TASK_UNINTERRUPTIBLE:
3690 stats->nr_uninterruptible++;
3691 break;
3692 case TASK_STOPPED:
3693 stats->nr_stopped++;
3694 break;
3695 default:
3696 if (delayacct_is_task_waiting_on_io(tsk))
3697 stats->nr_io_wait++;
3698 break;
3699 }
3700 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003701 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003702
Balbir Singh846c7bb2007-10-18 23:39:44 -07003703err:
3704 return ret;
3705}
3706
Paul Menage8f3ff202009-09-23 15:56:25 -07003707
Paul Menagecc31edc2008-10-18 20:28:04 -07003708/*
Ben Blum102a7752009-09-23 15:56:26 -07003709 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003711 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003712 */
3713
Ben Blum102a7752009-09-23 15:56:26 -07003714static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003715{
3716 /*
3717 * Initially we receive a position value that corresponds to
3718 * one more than the last pid shown (or 0 on the first call or
3719 * after a seek to the start). Use a binary-search to find the
3720 * next pid to display, if any
3721 */
Ben Blum102a7752009-09-23 15:56:26 -07003722 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003723 int index = 0, pid = *pos;
3724 int *iter;
3725
Li Zefanb3958902013-08-01 09:52:15 +08003726 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003728 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003729
Paul Menagecc31edc2008-10-18 20:28:04 -07003730 while (index < end) {
3731 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003732 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003733 index = mid;
3734 break;
Ben Blum102a7752009-09-23 15:56:26 -07003735 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003736 index = mid + 1;
3737 else
3738 end = mid;
3739 }
3740 }
3741 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003742 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003743 return NULL;
3744 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003745 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003746 *pos = *iter;
3747 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003748}
3749
Ben Blum102a7752009-09-23 15:56:26 -07003750static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003751{
Ben Blum102a7752009-09-23 15:56:26 -07003752 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003753 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003754}
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003757{
Ben Blum102a7752009-09-23 15:56:26 -07003758 struct cgroup_pidlist *l = s->private;
3759 pid_t *p = v;
3760 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003761 /*
3762 * Advance to the next pid in the array. If this goes off the
3763 * end, we're done
3764 */
3765 p++;
3766 if (p >= end) {
3767 return NULL;
3768 } else {
3769 *pos = *p;
3770 return p;
3771 }
3772}
3773
Ben Blum102a7752009-09-23 15:56:26 -07003774static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003775{
3776 return seq_printf(s, "%d\n", *(int *)v);
3777}
3778
Ben Blum102a7752009-09-23 15:56:26 -07003779/*
3780 * seq_operations functions for iterating on pidlists through seq_file -
3781 * independent of whether it's tasks or procs
3782 */
3783static const struct seq_operations cgroup_pidlist_seq_operations = {
3784 .start = cgroup_pidlist_start,
3785 .stop = cgroup_pidlist_stop,
3786 .next = cgroup_pidlist_next,
3787 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003788};
3789
Ben Blum102a7752009-09-23 15:56:26 -07003790static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003791{
Ben Blum72a8cb32009-09-23 15:56:27 -07003792 /*
3793 * the case where we're the last user of this particular pidlist will
3794 * have us remove it from the cgroup's list, which entails taking the
3795 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3796 * pidlist_mutex, we have to take pidlist_mutex first.
3797 */
3798 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003799 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003800 BUG_ON(!l->use_count);
3801 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003802 /* we're the last user if refcount is 0; remove and free */
3803 list_del(&l->links);
3804 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003805 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003806 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003807 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003808 kfree(l);
3809 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003810 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003811 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003812 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003813}
3814
Ben Blum102a7752009-09-23 15:56:26 -07003815static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816{
Ben Blum102a7752009-09-23 15:56:26 -07003817 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003818 if (!(file->f_mode & FMODE_READ))
3819 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003820 /*
3821 * the seq_file will only be initialized if the file was opened for
3822 * reading; hence we check if it's not null only in that case.
3823 */
3824 l = ((struct seq_file *)file->private_data)->private;
3825 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 return seq_release(inode, file);
3827}
3828
Ben Blum102a7752009-09-23 15:56:26 -07003829static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003830 .read = seq_read,
3831 .llseek = seq_lseek,
3832 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003833 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003834};
3835
3836/*
Ben Blum102a7752009-09-23 15:56:26 -07003837 * The following functions handle opens on a file that displays a pidlist
3838 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3839 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003840 */
Ben Blum102a7752009-09-23 15:56:26 -07003841/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003842static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003843{
3844 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003845 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003846 int retval;
3847
3848 /* Nothing to do for write-only files */
3849 if (!(file->f_mode & FMODE_READ))
3850 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003851
Ben Blum102a7752009-09-23 15:56:26 -07003852 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003853 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003854 if (retval)
3855 return retval;
3856 /* configure file information */
3857 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003858
Ben Blum102a7752009-09-23 15:56:26 -07003859 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003860 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003861 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003862 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003863 }
Ben Blum102a7752009-09-23 15:56:26 -07003864 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003865 return 0;
3866}
Ben Blum102a7752009-09-23 15:56:26 -07003867static int cgroup_tasks_open(struct inode *unused, struct file *file)
3868{
Ben Blum72a8cb32009-09-23 15:56:27 -07003869 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003870}
3871static int cgroup_procs_open(struct inode *unused, struct file *file)
3872{
Ben Blum72a8cb32009-09-23 15:56:27 -07003873 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003874}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003875
Tejun Heo182446d2013-08-08 20:11:24 -04003876static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3877 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003878{
Tejun Heo182446d2013-08-08 20:11:24 -04003879 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003880}
3881
Tejun Heo182446d2013-08-08 20:11:24 -04003882static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3883 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003884{
Tejun Heo182446d2013-08-08 20:11:24 -04003885 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003886 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003887 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003888 else
Tejun Heo182446d2013-08-08 20:11:24 -04003889 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003890 return 0;
3891}
3892
Paul Menagebbcb81d2007-10-18 23:39:32 -07003893/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003894 * When dput() is called asynchronously, if umount has been done and
3895 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3896 * there's a small window that vfs will see the root dentry with non-zero
3897 * refcnt and trigger BUG().
3898 *
3899 * That's why we hold a reference before dput() and drop it right after.
3900 */
3901static void cgroup_dput(struct cgroup *cgrp)
3902{
3903 struct super_block *sb = cgrp->root->sb;
3904
3905 atomic_inc(&sb->s_active);
3906 dput(cgrp->dentry);
3907 deactivate_super(sb);
3908}
3909
3910/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003911 * Unregister event and free resources.
3912 *
3913 * Gets called from workqueue.
3914 */
3915static void cgroup_event_remove(struct work_struct *work)
3916{
3917 struct cgroup_event *event = container_of(work, struct cgroup_event,
3918 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003919 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003920
Li Zefan810cbee2013-02-18 18:56:14 +08003921 remove_wait_queue(event->wqh, &event->wait);
3922
Tejun Heo81eeaf02013-08-08 20:11:26 -04003923 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003924
Li Zefan810cbee2013-02-18 18:56:14 +08003925 /* Notify userspace the event is going away. */
3926 eventfd_signal(event->eventfd, 1);
3927
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003928 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003929 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003930 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003931}
3932
3933/*
3934 * Gets called on POLLHUP on eventfd when user closes it.
3935 *
3936 * Called with wqh->lock held and interrupts disabled.
3937 */
3938static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3939 int sync, void *key)
3940{
3941 struct cgroup_event *event = container_of(wait,
3942 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003943 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003944 unsigned long flags = (unsigned long)key;
3945
3946 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003947 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003948 * If the event has been detached at cgroup removal, we
3949 * can simply return knowing the other side will cleanup
3950 * for us.
3951 *
3952 * We can't race against event freeing since the other
3953 * side will require wqh->lock via remove_wait_queue(),
3954 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 */
Li Zefan810cbee2013-02-18 18:56:14 +08003956 spin_lock(&cgrp->event_list_lock);
3957 if (!list_empty(&event->list)) {
3958 list_del_init(&event->list);
3959 /*
3960 * We are in atomic context, but cgroup_event_remove()
3961 * may sleep, so we have to call it in workqueue.
3962 */
3963 schedule_work(&event->remove);
3964 }
3965 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003966 }
3967
3968 return 0;
3969}
3970
3971static void cgroup_event_ptable_queue_proc(struct file *file,
3972 wait_queue_head_t *wqh, poll_table *pt)
3973{
3974 struct cgroup_event *event = container_of(pt,
3975 struct cgroup_event, pt);
3976
3977 event->wqh = wqh;
3978 add_wait_queue(wqh, &event->wait);
3979}
3980
3981/*
3982 * Parse input and register new cgroup event handler.
3983 *
3984 * Input must be in format '<event_fd> <control_fd> <args>'.
3985 * Interpretation of args is defined by control file implementation.
3986 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04003987static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04003988 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003989{
Tejun Heo6e6eab02013-08-15 11:43:15 -04003990 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08003991 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04003992 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003993 unsigned int efd, cfd;
Al Viro4e10f3c2013-08-30 12:29:49 -04003994 struct fd efile;
3995 struct fd cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003996 char *endp;
3997 int ret;
3998
3999 efd = simple_strtoul(buffer, &endp, 10);
4000 if (*endp != ' ')
4001 return -EINVAL;
4002 buffer = endp + 1;
4003
4004 cfd = simple_strtoul(buffer, &endp, 10);
4005 if ((*endp != ' ') && (*endp != '\0'))
4006 return -EINVAL;
4007 buffer = endp + 1;
4008
4009 event = kzalloc(sizeof(*event), GFP_KERNEL);
4010 if (!event)
4011 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004012
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004013 INIT_LIST_HEAD(&event->list);
4014 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4015 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4016 INIT_WORK(&event->remove, cgroup_event_remove);
4017
Al Viro4e10f3c2013-08-30 12:29:49 -04004018 efile = fdget(efd);
4019 if (!efile.file) {
4020 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004021 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004022 }
4023
Al Viro4e10f3c2013-08-30 12:29:49 -04004024 event->eventfd = eventfd_ctx_fileget(efile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004025 if (IS_ERR(event->eventfd)) {
4026 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004027 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004028 }
4029
Al Viro4e10f3c2013-08-30 12:29:49 -04004030 cfile = fdget(cfd);
4031 if (!cfile.file) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004032 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004033 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004034 }
4035
4036 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004037 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro4e10f3c2013-08-30 12:29:49 -04004038 ret = inode_permission(file_inode(cfile.file), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004039 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004040 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004041
Al Viro4e10f3c2013-08-30 12:29:49 -04004042 event->cft = __file_cft(cfile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004043 if (IS_ERR(event->cft)) {
4044 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004045 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004046 }
4047
Tejun Heo6e6eab02013-08-15 11:43:15 -04004048 if (!event->cft->ss) {
4049 ret = -EBADF;
4050 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004051 }
4052
Li Zefanf1690072013-02-18 14:13:35 +08004053 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004054 * Determine the css of @cfile, verify it belongs to the same
4055 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004056 * Remaining events are automatically removed on cgroup destruction
4057 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004058 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004059 rcu_read_lock();
4060
4061 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004062 event->css = cgroup_css(cgrp, event->cft->ss);
Al Viro4e10f3c2013-08-30 12:29:49 -04004063 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004064 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004065 ret = 0;
4066
4067 rcu_read_unlock();
4068 if (ret)
4069 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004070
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004071 if (!event->cft->register_event || !event->cft->unregister_event) {
4072 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004073 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004074 }
4075
Tejun Heo6e6eab02013-08-15 11:43:15 -04004076 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004077 event->eventfd, buffer);
4078 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004079 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004080
Al Viro4e10f3c2013-08-30 12:29:49 -04004081 efile.file->f_op->poll(efile.file, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004082
4083 spin_lock(&cgrp->event_list_lock);
4084 list_add(&event->list, &cgrp->event_list);
4085 spin_unlock(&cgrp->event_list_lock);
4086
Al Viro4e10f3c2013-08-30 12:29:49 -04004087 fdput(cfile);
4088 fdput(efile);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004089
4090 return 0;
4091
Tejun Heo7941cb02013-08-26 18:40:56 -04004092out_put_css:
4093 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004094out_put_cfile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004095 fdput(cfile);
Li Zefan876ede82013-08-01 09:51:47 +08004096out_put_eventfd:
4097 eventfd_ctx_put(event->eventfd);
4098out_put_efile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004099 fdput(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004100out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004101 kfree(event);
4102
4103 return ret;
4104}
4105
Tejun Heo182446d2013-08-08 20:11:24 -04004106static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4107 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004108{
Tejun Heo182446d2013-08-08 20:11:24 -04004109 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004110}
4111
Tejun Heo182446d2013-08-08 20:11:24 -04004112static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4113 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004114{
4115 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004116 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004117 else
Tejun Heo182446d2013-08-08 20:11:24 -04004118 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004119 return 0;
4120}
4121
Tejun Heod5c56ce2013-06-03 19:14:34 -07004122static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004123 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004124 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004125 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004126 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004127 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004128 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004129 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004130 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004131 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004132 .write_string = cgroup_write_event_control,
4133 .mode = S_IWUGO,
4134 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004135 {
4136 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004137 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004138 .read_u64 = cgroup_clone_children_read,
4139 .write_u64 = cgroup_clone_children_write,
4140 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004141 {
Tejun Heo873fe092013-04-14 20:15:26 -07004142 .name = "cgroup.sane_behavior",
4143 .flags = CFTYPE_ONLY_ON_ROOT,
4144 .read_seq_string = cgroup_sane_behavior_show,
4145 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004146
4147 /*
4148 * Historical crazy stuff. These don't have "cgroup." prefix and
4149 * don't exist if sane_behavior. If you're depending on these, be
4150 * prepared to be burned.
4151 */
4152 {
4153 .name = "tasks",
4154 .flags = CFTYPE_INSANE, /* use "procs" instead */
4155 .open = cgroup_tasks_open,
4156 .write_u64 = cgroup_tasks_write,
4157 .release = cgroup_pidlist_release,
4158 .mode = S_IRUGO | S_IWUSR,
4159 },
4160 {
4161 .name = "notify_on_release",
4162 .flags = CFTYPE_INSANE,
4163 .read_u64 = cgroup_read_notify_on_release,
4164 .write_u64 = cgroup_write_notify_on_release,
4165 },
Tejun Heo873fe092013-04-14 20:15:26 -07004166 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004167 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004168 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004169 .read_seq_string = cgroup_release_agent_show,
4170 .write_string = cgroup_release_agent_write,
4171 .max_write_len = PATH_MAX,
4172 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004173 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004174};
4175
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004176/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004177 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004178 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004179 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004180 *
4181 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004182 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004183static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004186 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004187
Tejun Heo8e3f6542012-04-01 12:09:55 -07004188 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004189 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004190 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004191
4192 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004193 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004194
Tejun Heobee55092013-06-28 16:24:11 -07004195 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004196 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004197 if (ret < 0)
4198 goto err;
4199 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004200 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004201 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004202err:
4203 cgroup_clear_dir(cgrp, subsys_mask);
4204 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004205}
4206
Tejun Heo0c21ead2013-08-13 20:22:51 -04004207/*
4208 * css destruction is four-stage process.
4209 *
4210 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4211 * Implemented in kill_css().
4212 *
4213 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4214 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4215 * by invoking offline_css(). After offlining, the base ref is put.
4216 * Implemented in css_killed_work_fn().
4217 *
4218 * 3. When the percpu_ref reaches zero, the only possible remaining
4219 * accessors are inside RCU read sections. css_release() schedules the
4220 * RCU callback.
4221 *
4222 * 4. After the grace period, the css can be freed. Implemented in
4223 * css_free_work_fn().
4224 *
4225 * It is actually hairier because both step 2 and 4 require process context
4226 * and thus involve punting to css->destroy_work adding two additional
4227 * steps to the already complex sequence.
4228 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004229static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004230{
4231 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004232 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004233 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004234
Tejun Heo0ae78e02013-08-13 11:01:54 -04004235 if (css->parent)
4236 css_put(css->parent);
4237
Tejun Heo0c21ead2013-08-13 20:22:51 -04004238 css->ss->css_free(css);
4239 cgroup_dput(cgrp);
4240}
4241
4242static void css_free_rcu_fn(struct rcu_head *rcu_head)
4243{
4244 struct cgroup_subsys_state *css =
4245 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4246
4247 /*
4248 * css holds an extra ref to @cgrp->dentry which is put on the last
4249 * css_put(). dput() requires process context which we don't have.
4250 */
4251 INIT_WORK(&css->destroy_work, css_free_work_fn);
4252 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004253}
4254
Tejun Heod3daf282013-06-13 19:39:16 -07004255static void css_release(struct percpu_ref *ref)
4256{
4257 struct cgroup_subsys_state *css =
4258 container_of(ref, struct cgroup_subsys_state, refcnt);
4259
Tejun Heo0c21ead2013-08-13 20:22:51 -04004260 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004261}
4262
Tejun Heo623f9262013-08-13 11:01:55 -04004263static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4264 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004265{
Paul Menagebd89aab2007-10-18 23:40:44 -07004266 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004267 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004269
Tejun Heo0ae78e02013-08-13 11:01:54 -04004270 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004271 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004272 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004274
Tejun Heoca8bdca2013-08-26 18:40:56 -04004275 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276}
4277
Li Zefan2a4ac632013-07-31 16:16:40 +08004278/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004279static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004280{
Tejun Heo623f9262013-08-13 11:01:55 -04004281 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004282 int ret = 0;
4283
Tejun Heoa31f2d32012-11-19 08:13:37 -08004284 lockdep_assert_held(&cgroup_mutex);
4285
Tejun Heo92fb9742012-11-19 08:13:38 -08004286 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004287 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004288 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004289 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004290 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004291 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4292 }
Tejun Heob1929db2012-11-19 08:13:38 -08004293 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004294}
4295
Li Zefan2a4ac632013-07-31 16:16:40 +08004296/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004297static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004298{
Tejun Heo623f9262013-08-13 11:01:55 -04004299 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004300
4301 lockdep_assert_held(&cgroup_mutex);
4302
4303 if (!(css->flags & CSS_ONLINE))
4304 return;
4305
Li Zefand7eeac12013-03-12 15:35:59 -07004306 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004307 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004308
Tejun Heoeb954192013-08-08 20:11:23 -04004309 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004310 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004311 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004312}
4313
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314/*
Li Zefana043e3b2008-02-23 15:24:09 -08004315 * cgroup_create - create a cgroup
4316 * @parent: cgroup that will be parent of the new cgroup
4317 * @dentry: dentry of the new cgroup
4318 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004319 *
Li Zefana043e3b2008-02-23 15:24:09 -08004320 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004323 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324{
Tejun Heoae7f1642013-08-13 20:22:50 -04004325 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004326 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004327 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328 struct cgroupfs_root *root = parent->root;
4329 int err = 0;
4330 struct cgroup_subsys *ss;
4331 struct super_block *sb = root->sb;
4332
Tejun Heo0a950f62012-11-19 09:02:12 -08004333 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004334 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4335 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 return -ENOMEM;
4337
Li Zefan65dff752013-03-01 15:01:56 +08004338 name = cgroup_alloc_name(dentry);
4339 if (!name)
4340 goto err_free_cgrp;
4341 rcu_assign_pointer(cgrp->name, name);
4342
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004343 /*
4344 * Temporarily set the pointer to NULL, so idr_find() won't return
4345 * a half-baked cgroup.
4346 */
4347 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004348 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004349 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004350
Tejun Heo976c06b2012-11-05 09:16:59 -08004351 /*
4352 * Only live parents can have children. Note that the liveliness
4353 * check isn't strictly necessary because cgroup_mkdir() and
4354 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4355 * anyway so that locking is contained inside cgroup proper and we
4356 * don't get nasty surprises if we ever grow another caller.
4357 */
4358 if (!cgroup_lock_live_group(parent)) {
4359 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004360 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004361 }
4362
Paul Menageddbcc7e2007-10-18 23:39:30 -07004363 /* Grab a reference on the superblock so the hierarchy doesn't
4364 * get deleted on unmount if there are child cgroups. This
4365 * can be done outside cgroup_mutex, since the sb can't
4366 * disappear while someone has an open control file on the
4367 * fs */
4368 atomic_inc(&sb->s_active);
4369
Paul Menagecc31edc2008-10-18 20:28:04 -07004370 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371
Li Zefanfe1c06c2013-01-24 14:30:22 +08004372 dentry->d_fsdata = cgrp;
4373 cgrp->dentry = dentry;
4374
Paul Menagebd89aab2007-10-18 23:40:44 -07004375 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004376 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004377 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378
Li Zefanb6abdb02008-03-04 14:28:19 -08004379 if (notify_on_release(parent))
4380 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4381
Tejun Heo2260e7f2012-11-19 08:13:38 -08004382 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4383 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004384
Tejun Heo5549c492013-06-24 15:21:48 -07004385 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004386 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004387
Tejun Heoca8bdca2013-08-26 18:40:56 -04004388 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389 if (IS_ERR(css)) {
4390 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004391 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004393 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004394
4395 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004396 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004397 goto err_free_all;
4398
Tejun Heo623f9262013-08-13 11:01:55 -04004399 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 }
4401
Tejun Heo4e139af2012-11-19 08:13:36 -08004402 /*
4403 * Create directory. cgroup_create_file() returns with the new
4404 * directory locked on success so that it can be populated without
4405 * dropping cgroup_mutex.
4406 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004407 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004408 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004409 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004410 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411
Tejun Heo00356bd2013-06-18 11:14:22 -07004412 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004413
Tejun Heo4e139af2012-11-19 08:13:36 -08004414 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004415 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4416 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004417
Tejun Heo0ae78e02013-08-13 11:01:54 -04004418 /* each css holds a ref to the cgroup's dentry and the parent css */
4419 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004420 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004421
Tejun Heoed9577932012-11-05 09:16:58 -08004422 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004423 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004424 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004425
Li Zefan415cf072013-04-08 14:35:02 +08004426 /* hold a ref to the parent's dentry */
4427 dget(parent->dentry);
4428
Tejun Heob1929db2012-11-19 08:13:38 -08004429 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004430 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004431 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004432
4433 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004434 if (err)
4435 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004436
4437 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4438 parent->parent) {
4439 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",
4440 current->comm, current->pid, ss->name);
4441 if (!strcmp(ss->name, "memory"))
4442 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4443 ss->warned_broken_hierarchy = true;
4444 }
Tejun Heoa8638032012-11-09 09:12:29 -08004445 }
4446
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004447 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4448
Tejun Heo2bb566c2013-08-08 20:11:23 -04004449 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004450 if (err)
4451 goto err_destroy;
4452
4453 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004454 if (err)
4455 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004456
4457 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004458 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004459
4460 return 0;
4461
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004462err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004463 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004464 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004465
4466 if (css) {
4467 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004468 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004469 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004470 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004471 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472 /* Release the reference count that we took on the superblock */
4473 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004474err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004475 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004476err_free_name:
4477 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004478err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004479 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004480 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004481
4482err_destroy:
4483 cgroup_destroy_locked(cgrp);
4484 mutex_unlock(&cgroup_mutex);
4485 mutex_unlock(&dentry->d_inode->i_mutex);
4486 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487}
4488
Al Viro18bb1db2011-07-26 01:41:39 -04004489static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004490{
4491 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4492
4493 /* the vfs holds inode->i_mutex already */
4494 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4495}
4496
Tejun Heo223dbc32013-08-13 20:22:50 -04004497/*
4498 * This is called when the refcnt of a css is confirmed to be killed.
4499 * css_tryget() is now guaranteed to fail.
4500 */
4501static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004502{
Tejun Heo223dbc32013-08-13 20:22:50 -04004503 struct cgroup_subsys_state *css =
4504 container_of(work, struct cgroup_subsys_state, destroy_work);
4505 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004506
Tejun Heof20104d2013-08-13 20:22:50 -04004507 mutex_lock(&cgroup_mutex);
4508
4509 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004510 * css_tryget() is guaranteed to fail now. Tell subsystems to
4511 * initate destruction.
4512 */
4513 offline_css(css);
4514
4515 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004516 * If @cgrp is marked dead, it's waiting for refs of all css's to
4517 * be disabled before proceeding to the second phase of cgroup
4518 * destruction. If we are the last one, kick it off.
4519 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004520 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004521 cgroup_destroy_css_killed(cgrp);
4522
4523 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004524
4525 /*
4526 * Put the css refs from kill_css(). Each css holds an extra
4527 * reference to the cgroup's dentry and cgroup removal proceeds
4528 * regardless of css refs. On the last put of each css, whenever
4529 * that may be, the extra dentry ref is put so that dentry
4530 * destruction happens only after all css's are released.
4531 */
4532 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004533}
4534
Tejun Heo223dbc32013-08-13 20:22:50 -04004535/* css kill confirmation processing requires process context, bounce */
4536static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004537{
4538 struct cgroup_subsys_state *css =
4539 container_of(ref, struct cgroup_subsys_state, refcnt);
4540
Tejun Heo223dbc32013-08-13 20:22:50 -04004541 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4542 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004543}
4544
4545/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004546 * kill_css - destroy a css
4547 * @css: css to destroy
4548 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004549 * This function initiates destruction of @css by removing cgroup interface
4550 * files and putting its base reference. ->css_offline() will be invoked
4551 * asynchronously once css_tryget() is guaranteed to fail and when the
4552 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004553 */
4554static void kill_css(struct cgroup_subsys_state *css)
4555{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004556 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4557
Tejun Heoedae0c32013-08-13 20:22:51 -04004558 /*
4559 * Killing would put the base ref, but we need to keep it alive
4560 * until after ->css_offline().
4561 */
4562 css_get(css);
4563
4564 /*
4565 * cgroup core guarantees that, by the time ->css_offline() is
4566 * invoked, no new css reference will be given out via
4567 * css_tryget(). We can't simply call percpu_ref_kill() and
4568 * proceed to offlining css's because percpu_ref_kill() doesn't
4569 * guarantee that the ref is seen as killed on all CPUs on return.
4570 *
4571 * Use percpu_ref_kill_and_confirm() to get notifications as each
4572 * css is confirmed to be seen as killed on all CPUs.
4573 */
4574 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004575}
4576
4577/**
4578 * cgroup_destroy_locked - the first stage of cgroup destruction
4579 * @cgrp: cgroup to be destroyed
4580 *
4581 * css's make use of percpu refcnts whose killing latency shouldn't be
4582 * exposed to userland and are RCU protected. Also, cgroup core needs to
4583 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4584 * invoked. To satisfy all the requirements, destruction is implemented in
4585 * the following two steps.
4586 *
4587 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4588 * userland visible parts and start killing the percpu refcnts of
4589 * css's. Set up so that the next stage will be kicked off once all
4590 * the percpu refcnts are confirmed to be killed.
4591 *
4592 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4593 * rest of destruction. Once all cgroup references are gone, the
4594 * cgroup is RCU-freed.
4595 *
4596 * This function implements s1. After this step, @cgrp is gone as far as
4597 * the userland is concerned and a new cgroup with the same name may be
4598 * created. As cgroup doesn't care about the names internally, this
4599 * doesn't cause any problem.
4600 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004601static int cgroup_destroy_locked(struct cgroup *cgrp)
4602 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603{
Tejun Heo42809dd2012-11-19 08:13:37 -08004604 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004605 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004606 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004607 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004608 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609
Tejun Heo42809dd2012-11-19 08:13:37 -08004610 lockdep_assert_held(&d->d_inode->i_mutex);
4611 lockdep_assert_held(&cgroup_mutex);
4612
Tejun Heoddd69142013-06-12 21:04:54 -07004613 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004614 * css_set_lock synchronizes access to ->cset_links and prevents
4615 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004616 */
4617 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004618 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004619 read_unlock(&css_set_lock);
4620 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004622
Tejun Heo1a90dd52012-11-05 09:16:59 -08004623 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004624 * Make sure there's no live children. We can't test ->children
4625 * emptiness as dead children linger on it while being destroyed;
4626 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4627 */
4628 empty = true;
4629 rcu_read_lock();
4630 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4631 empty = cgroup_is_dead(child);
4632 if (!empty)
4633 break;
4634 }
4635 rcu_read_unlock();
4636 if (!empty)
4637 return -EBUSY;
4638
4639 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004640 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4641 * will be invoked to perform the rest of destruction once the
4642 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004643 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004644 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004645 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004646
4647 /*
4648 * Mark @cgrp dead. This prevents further task migration and child
4649 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004650 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004651 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004652 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004653 */
Tejun Heo54766d42013-06-12 21:04:53 -07004654 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004655
Tejun Heo455050d2013-06-13 19:27:41 -07004656 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4657 raw_spin_lock(&release_list_lock);
4658 if (!list_empty(&cgrp->release_list))
4659 list_del_init(&cgrp->release_list);
4660 raw_spin_unlock(&release_list_lock);
4661
4662 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004663 * If @cgrp has css's attached, the second stage of cgroup
4664 * destruction is kicked off from css_killed_work_fn() after the
4665 * refs of all attached css's are killed. If @cgrp doesn't have
4666 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004667 */
Tejun Heof20104d2013-08-13 20:22:50 -04004668 if (!cgrp->nr_css)
4669 cgroup_destroy_css_killed(cgrp);
4670
4671 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004672 * Clear the base files and remove @cgrp directory. The removal
4673 * puts the base ref but we aren't quite done with @cgrp yet, so
4674 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004675 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004676 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004677 dget(d);
4678 cgroup_d_remove_dir(d);
4679
4680 /*
4681 * Unregister events and notify userspace.
4682 * Notify userspace about cgroup removing only after rmdir of cgroup
4683 * directory to avoid race between userspace and kernelspace.
4684 */
4685 spin_lock(&cgrp->event_list_lock);
4686 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4687 list_del_init(&event->list);
4688 schedule_work(&event->remove);
4689 }
4690 spin_unlock(&cgrp->event_list_lock);
4691
Tejun Heoea15f8c2013-06-13 19:27:42 -07004692 return 0;
4693};
4694
Tejun Heod3daf282013-06-13 19:39:16 -07004695/**
Tejun Heof20104d2013-08-13 20:22:50 -04004696 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004697 * @work: cgroup->destroy_free_work
4698 *
4699 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004700 * destroyed after all css's are offlined and performs the rest of
4701 * destruction. This is the second step of destruction described in the
4702 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004703 */
Tejun Heof20104d2013-08-13 20:22:50 -04004704static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004705{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004706 struct cgroup *parent = cgrp->parent;
4707 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004708
Tejun Heof20104d2013-08-13 20:22:50 -04004709 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004710
Paul Menage999cd8a2009-01-07 18:08:36 -08004711 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004712 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004713
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004714 /*
4715 * We should remove the cgroup object from idr before its grace
4716 * period starts, so we won't be looking up a cgroup while the
4717 * cgroup is being freed.
4718 */
4719 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4720 cgrp->id = -1;
4721
Paul Menageddbcc7e2007-10-18 23:39:30 -07004722 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004723
Paul Menagebd89aab2007-10-18 23:40:44 -07004724 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004725 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004726}
4727
Tejun Heo42809dd2012-11-19 08:13:37 -08004728static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4729{
4730 int ret;
4731
4732 mutex_lock(&cgroup_mutex);
4733 ret = cgroup_destroy_locked(dentry->d_fsdata);
4734 mutex_unlock(&cgroup_mutex);
4735
4736 return ret;
4737}
4738
Tejun Heo8e3f6542012-04-01 12:09:55 -07004739static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4740{
4741 INIT_LIST_HEAD(&ss->cftsets);
4742
4743 /*
4744 * base_cftset is embedded in subsys itself, no need to worry about
4745 * deregistration.
4746 */
4747 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004748 struct cftype *cft;
4749
4750 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4751 cft->ss = ss;
4752
Tejun Heo8e3f6542012-04-01 12:09:55 -07004753 ss->base_cftset.cfts = ss->base_cftypes;
4754 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4755 }
4756}
4757
Li Zefan06a11922008-04-29 01:00:07 -07004758static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004759{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004760 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004761
4762 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004763
Tejun Heo648bb562012-11-19 08:13:36 -08004764 mutex_lock(&cgroup_mutex);
4765
Tejun Heo8e3f6542012-04-01 12:09:55 -07004766 /* init base cftset */
4767 cgroup_init_cftsets(ss);
4768
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004770 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4771 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004772 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004773 /* We don't handle early failures gracefully */
4774 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004775 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776
Li Zefane8d55fd2008-04-29 01:00:13 -07004777 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004778 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004779 * newly registered, all tasks and hence the
4780 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004781 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004782
4783 need_forkexit_callback |= ss->fork || ss->exit;
4784
Li Zefane8d55fd2008-04-29 01:00:13 -07004785 /* At system boot, before all subsystems have been
4786 * registered, no tasks have been forked, so we don't
4787 * need to invoke fork callbacks here. */
4788 BUG_ON(!list_empty(&init_task.tasks));
4789
Tejun Heoae7f1642013-08-13 20:22:50 -04004790 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004791
Tejun Heo648bb562012-11-19 08:13:36 -08004792 mutex_unlock(&cgroup_mutex);
4793
Ben Blume6a11052010-03-10 15:22:09 -08004794 /* this function shouldn't be used with modular subsystems, since they
4795 * need to register a subsys_id, among other things */
4796 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004797}
4798
4799/**
Ben Blume6a11052010-03-10 15:22:09 -08004800 * cgroup_load_subsys: load and register a modular subsystem at runtime
4801 * @ss: the subsystem to load
4802 *
4803 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004804 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004805 * up for use. If the subsystem is built-in anyway, work is delegated to the
4806 * simpler cgroup_init_subsys.
4807 */
4808int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4809{
Ben Blume6a11052010-03-10 15:22:09 -08004810 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004811 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004812 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004813 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004814 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004815
4816 /* check name and function validity */
4817 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004818 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004819 return -EINVAL;
4820
4821 /*
4822 * we don't support callbacks in modular subsystems. this check is
4823 * before the ss->module check for consistency; a subsystem that could
4824 * be a module should still have no callbacks even if the user isn't
4825 * compiling it as one.
4826 */
4827 if (ss->fork || ss->exit)
4828 return -EINVAL;
4829
4830 /*
4831 * an optionally modular subsystem is built-in: we want to do nothing,
4832 * since cgroup_init_subsys will have already taken care of it.
4833 */
4834 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004835 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004836 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004837 return 0;
4838 }
4839
Tejun Heo8e3f6542012-04-01 12:09:55 -07004840 /* init base cftset */
4841 cgroup_init_cftsets(ss);
4842
Ben Blume6a11052010-03-10 15:22:09 -08004843 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004844 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004845
4846 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004847 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004848 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004849 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004850 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004851 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004852 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004853 /* failure case - need to deassign the cgroup_subsys[] slot. */
4854 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004855 mutex_unlock(&cgroup_mutex);
4856 return PTR_ERR(css);
4857 }
4858
Tejun Heo9871bf92013-06-24 15:21:47 -07004859 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4860 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004861
4862 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004863 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004864
4865 /*
4866 * Now we need to entangle the css into the existing css_sets. unlike
4867 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4868 * will need a new pointer to it; done by iterating the css_set_table.
4869 * furthermore, modifying the existing css_sets will corrupt the hash
4870 * table state, so each changed css_set will need its hash recomputed.
4871 * this is all done under the css_set_lock.
4872 */
4873 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004874 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004875 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004876 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004877 continue;
4878 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004879 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004880 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004881 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004882 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004883 key = css_set_hash(cset->subsys);
4884 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004885 }
4886 write_unlock(&css_set_lock);
4887
Tejun Heoae7f1642013-08-13 20:22:50 -04004888 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004889 if (ret)
4890 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004891
Ben Blume6a11052010-03-10 15:22:09 -08004892 /* success! */
4893 mutex_unlock(&cgroup_mutex);
4894 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004895
4896err_unload:
4897 mutex_unlock(&cgroup_mutex);
4898 /* @ss can't be mounted here as try_module_get() would fail */
4899 cgroup_unload_subsys(ss);
4900 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004901}
4902EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4903
4904/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004905 * cgroup_unload_subsys: unload a modular subsystem
4906 * @ss: the subsystem to unload
4907 *
4908 * This function should be called in a modular subsystem's exitcall. When this
4909 * function is invoked, the refcount on the subsystem's module will be 0, so
4910 * the subsystem will not be attached to any hierarchy.
4911 */
4912void cgroup_unload_subsys(struct cgroup_subsys *ss)
4913{
Tejun Heo69d02062013-06-12 21:04:50 -07004914 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004915
4916 BUG_ON(ss->module == NULL);
4917
4918 /*
4919 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004920 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004921 * doesn't start being used while we're killing it off.
4922 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004923 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004924
4925 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004926
Tejun Heoca8bdca2013-08-26 18:40:56 -04004927 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004928
Ben Blumcf5d5942010-03-10 15:22:09 -08004929 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004930 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004931
Tejun Heo9871bf92013-06-24 15:21:47 -07004932 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004933 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004934
4935 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004936 * disentangle the css from all css_sets attached to the dummy
4937 * top. as in loading, we need to pay our respects to the hashtable
4938 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004939 */
4940 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004941 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004942 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004943 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004944
Tejun Heo5abb8852013-06-12 21:04:49 -07004945 hash_del(&cset->hlist);
4946 cset->subsys[ss->subsys_id] = NULL;
4947 key = css_set_hash(cset->subsys);
4948 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004949 }
4950 write_unlock(&css_set_lock);
4951
4952 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004953 * remove subsystem's css from the cgroup_dummy_top and free it -
4954 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004955 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004956 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004957 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004958 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004959
4960 mutex_unlock(&cgroup_mutex);
4961}
4962EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4963
4964/**
Li Zefana043e3b2008-02-23 15:24:09 -08004965 * cgroup_init_early - cgroup initialization at system boot
4966 *
4967 * Initialize cgroups at system boot, and initialize any
4968 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004969 */
4970int __init cgroup_init_early(void)
4971{
Tejun Heo30159ec2013-06-25 11:53:37 -07004972 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004973 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004974
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004975 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004976 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004977 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004978 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004979 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004980 init_cgroup_root(&cgroup_dummy_root);
4981 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004982 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004983
Tejun Heo69d02062013-06-12 21:04:50 -07004984 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004985 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4986 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004987 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004988
Tejun Heo30159ec2013-06-25 11:53:37 -07004989 /* at bootup time, we don't worry about modular subsystems */
4990 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004991 BUG_ON(!ss->name);
4992 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004993 BUG_ON(!ss->css_alloc);
4994 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004995 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004996 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004997 ss->name, ss->subsys_id);
4998 BUG();
4999 }
5000
5001 if (ss->early_init)
5002 cgroup_init_subsys(ss);
5003 }
5004 return 0;
5005}
5006
5007/**
Li Zefana043e3b2008-02-23 15:24:09 -08005008 * cgroup_init - cgroup initialization
5009 *
5010 * Register cgroup filesystem and /proc file, and initialize
5011 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005012 */
5013int __init cgroup_init(void)
5014{
Tejun Heo30159ec2013-06-25 11:53:37 -07005015 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005016 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005017 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005018
5019 err = bdi_init(&cgroup_backing_dev_info);
5020 if (err)
5021 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005022
Tejun Heo30159ec2013-06-25 11:53:37 -07005023 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005024 if (!ss->early_init)
5025 cgroup_init_subsys(ss);
5026 }
5027
Tejun Heofa3ca072013-04-14 11:36:56 -07005028 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005029 mutex_lock(&cgroup_mutex);
5030 mutex_lock(&cgroup_root_mutex);
5031
Tejun Heo82fe9b02013-06-25 11:53:37 -07005032 /* Add init_css_set to the hash table */
5033 key = css_set_hash(init_css_set.subsys);
5034 hash_add(css_set_table, &init_css_set.hlist, key);
5035
Tejun Heofc76df72013-06-25 11:53:37 -07005036 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005037
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005038 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5039 0, 1, GFP_KERNEL);
5040 BUG_ON(err < 0);
5041
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005042 mutex_unlock(&cgroup_root_mutex);
5043 mutex_unlock(&cgroup_mutex);
5044
Greg KH676db4a2010-08-05 13:53:35 -07005045 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5046 if (!cgroup_kobj) {
5047 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005048 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005049 }
5050
5051 err = register_filesystem(&cgroup_fs_type);
5052 if (err < 0) {
5053 kobject_put(cgroup_kobj);
5054 goto out;
5055 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005056
Li Zefan46ae2202008-04-29 01:00:08 -07005057 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005058
Paul Menageddbcc7e2007-10-18 23:39:30 -07005059out:
Paul Menagea4243162007-10-18 23:39:35 -07005060 if (err)
5061 bdi_destroy(&cgroup_backing_dev_info);
5062
Paul Menageddbcc7e2007-10-18 23:39:30 -07005063 return err;
5064}
Paul Menageb4f48b62007-10-18 23:39:33 -07005065
Paul Menagea4243162007-10-18 23:39:35 -07005066/*
5067 * proc_cgroup_show()
5068 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5069 * - Used for /proc/<pid>/cgroup.
5070 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5071 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005072 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005073 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5074 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5075 * cgroup to top_cgroup.
5076 */
5077
5078/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005079int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005080{
5081 struct pid *pid;
5082 struct task_struct *tsk;
5083 char *buf;
5084 int retval;
5085 struct cgroupfs_root *root;
5086
5087 retval = -ENOMEM;
5088 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5089 if (!buf)
5090 goto out;
5091
5092 retval = -ESRCH;
5093 pid = m->private;
5094 tsk = get_pid_task(pid, PIDTYPE_PID);
5095 if (!tsk)
5096 goto out_free;
5097
5098 retval = 0;
5099
5100 mutex_lock(&cgroup_mutex);
5101
Li Zefane5f6a862009-01-07 18:07:41 -08005102 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005103 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005104 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005105 int count = 0;
5106
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005107 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005108 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005109 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005110 if (strlen(root->name))
5111 seq_printf(m, "%sname=%s", count ? "," : "",
5112 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005113 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005114 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005115 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005116 if (retval < 0)
5117 goto out_unlock;
5118 seq_puts(m, buf);
5119 seq_putc(m, '\n');
5120 }
5121
5122out_unlock:
5123 mutex_unlock(&cgroup_mutex);
5124 put_task_struct(tsk);
5125out_free:
5126 kfree(buf);
5127out:
5128 return retval;
5129}
5130
Paul Menagea4243162007-10-18 23:39:35 -07005131/* Display information about each subsystem and each hierarchy */
5132static int proc_cgroupstats_show(struct seq_file *m, void *v)
5133{
Tejun Heo30159ec2013-06-25 11:53:37 -07005134 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005135 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005136
Paul Menage8bab8dd2008-04-04 14:29:57 -07005137 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005138 /*
5139 * ideally we don't want subsystems moving around while we do this.
5140 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5141 * subsys/hierarchy state.
5142 */
Paul Menagea4243162007-10-18 23:39:35 -07005143 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005144
5145 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005146 seq_printf(m, "%s\t%d\t%d\t%d\n",
5147 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005148 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005149
Paul Menagea4243162007-10-18 23:39:35 -07005150 mutex_unlock(&cgroup_mutex);
5151 return 0;
5152}
5153
5154static int cgroupstats_open(struct inode *inode, struct file *file)
5155{
Al Viro9dce07f2008-03-29 03:07:28 +00005156 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005157}
5158
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005159static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005160 .open = cgroupstats_open,
5161 .read = seq_read,
5162 .llseek = seq_lseek,
5163 .release = single_release,
5164};
5165
Paul Menageb4f48b62007-10-18 23:39:33 -07005166/**
5167 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005168 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005169 *
5170 * Description: A task inherits its parent's cgroup at fork().
5171 *
5172 * A pointer to the shared css_set was automatically copied in
5173 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005174 * it was not made under the protection of RCU or cgroup_mutex, so
5175 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5176 * have already changed current->cgroups, allowing the previously
5177 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005178 *
5179 * At the point that cgroup_fork() is called, 'current' is the parent
5180 * task, and the passed argument 'child' points to the child task.
5181 */
5182void cgroup_fork(struct task_struct *child)
5183{
Tejun Heo9bb71302012-10-18 17:52:07 -07005184 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005185 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005186 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005187 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005188 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005189}
5190
5191/**
Li Zefana043e3b2008-02-23 15:24:09 -08005192 * cgroup_post_fork - called on a new task after adding it to the task list
5193 * @child: the task in question
5194 *
Tejun Heo5edee612012-10-16 15:03:14 -07005195 * Adds the task to the list running through its css_set if necessary and
5196 * call the subsystem fork() callbacks. Has to be after the task is
5197 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005198 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005199 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005200 */
Paul Menage817929e2007-10-18 23:39:36 -07005201void cgroup_post_fork(struct task_struct *child)
5202{
Tejun Heo30159ec2013-06-25 11:53:37 -07005203 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005204 int i;
5205
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005206 /*
5207 * use_task_css_set_links is set to 1 before we walk the tasklist
5208 * under the tasklist_lock and we read it here after we added the child
5209 * to the tasklist under the tasklist_lock as well. If the child wasn't
5210 * yet in the tasklist when we walked through it from
5211 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5212 * should be visible now due to the paired locking and barriers implied
5213 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5214 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5215 * lock on fork.
5216 */
Paul Menage817929e2007-10-18 23:39:36 -07005217 if (use_task_css_set_links) {
5218 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005219 task_lock(child);
5220 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005221 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005222 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005223 write_unlock(&css_set_lock);
5224 }
Tejun Heo5edee612012-10-16 15:03:14 -07005225
5226 /*
5227 * Call ss->fork(). This must happen after @child is linked on
5228 * css_set; otherwise, @child might change state between ->fork()
5229 * and addition to css_set.
5230 */
5231 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005232 /*
5233 * fork/exit callbacks are supported only for builtin
5234 * subsystems, and the builtin section of the subsys
5235 * array is immutable, so we don't need to lock the
5236 * subsys array here. On the other hand, modular section
5237 * of the array can be freed at module unload, so we
5238 * can't touch that.
5239 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005240 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005241 if (ss->fork)
5242 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005243 }
Paul Menage817929e2007-10-18 23:39:36 -07005244}
Tejun Heo5edee612012-10-16 15:03:14 -07005245
Paul Menage817929e2007-10-18 23:39:36 -07005246/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005247 * cgroup_exit - detach cgroup from exiting task
5248 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005249 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005250 *
5251 * Description: Detach cgroup from @tsk and release it.
5252 *
5253 * Note that cgroups marked notify_on_release force every task in
5254 * them to take the global cgroup_mutex mutex when exiting.
5255 * This could impact scaling on very large systems. Be reluctant to
5256 * use notify_on_release cgroups where very high task exit scaling
5257 * is required on large systems.
5258 *
5259 * the_top_cgroup_hack:
5260 *
5261 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5262 *
5263 * We call cgroup_exit() while the task is still competent to
5264 * handle notify_on_release(), then leave the task attached to the
5265 * root cgroup in each hierarchy for the remainder of its exit.
5266 *
5267 * To do this properly, we would increment the reference count on
5268 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5269 * code we would add a second cgroup function call, to drop that
5270 * reference. This would just create an unnecessary hot spot on
5271 * the top_cgroup reference count, to no avail.
5272 *
5273 * Normally, holding a reference to a cgroup without bumping its
5274 * count is unsafe. The cgroup could go away, or someone could
5275 * attach us to a different cgroup, decrementing the count on
5276 * the first cgroup that we never incremented. But in this case,
5277 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005278 * which wards off any cgroup_attach_task() attempts, or task is a failed
5279 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005280 */
5281void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5282{
Tejun Heo30159ec2013-06-25 11:53:37 -07005283 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005284 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005285 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005286
5287 /*
5288 * Unlink from the css_set task list if necessary.
5289 * Optimistically check cg_list before taking
5290 * css_set_lock
5291 */
5292 if (!list_empty(&tsk->cg_list)) {
5293 write_lock(&css_set_lock);
5294 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005295 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005296 write_unlock(&css_set_lock);
5297 }
5298
Paul Menageb4f48b62007-10-18 23:39:33 -07005299 /* Reassign the task to the init_css_set. */
5300 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005301 cset = task_css_set(tsk);
5302 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005303
5304 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005305 /*
5306 * fork/exit callbacks are supported only for builtin
5307 * subsystems, see cgroup_post_fork() for details.
5308 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005309 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005310 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005311 struct cgroup_subsys_state *old_css = cset->subsys[i];
5312 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005313
Tejun Heoeb954192013-08-08 20:11:23 -04005314 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005315 }
5316 }
5317 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005318 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005319
Tejun Heo5abb8852013-06-12 21:04:49 -07005320 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005321}
Paul Menage697f4162007-10-18 23:39:34 -07005322
Paul Menagebd89aab2007-10-18 23:40:44 -07005323static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005324{
Li Zefanf50daa72013-03-01 15:06:07 +08005325 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005326 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005327 /*
5328 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005329 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005330 * it now
5331 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005332 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005333
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005334 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005335 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005336 list_empty(&cgrp->release_list)) {
5337 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005338 need_schedule_work = 1;
5339 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005340 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005341 if (need_schedule_work)
5342 schedule_work(&release_agent_work);
5343 }
5344}
5345
Paul Menage81a6a5c2007-10-18 23:39:38 -07005346/*
5347 * Notify userspace when a cgroup is released, by running the
5348 * configured release agent with the name of the cgroup (path
5349 * relative to the root of cgroup file system) as the argument.
5350 *
5351 * Most likely, this user command will try to rmdir this cgroup.
5352 *
5353 * This races with the possibility that some other task will be
5354 * attached to this cgroup before it is removed, or that some other
5355 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5356 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5357 * unused, and this cgroup will be reprieved from its death sentence,
5358 * to continue to serve a useful existence. Next time it's released,
5359 * we will get notified again, if it still has 'notify_on_release' set.
5360 *
5361 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5362 * means only wait until the task is successfully execve()'d. The
5363 * separate release agent task is forked by call_usermodehelper(),
5364 * then control in this thread returns here, without waiting for the
5365 * release agent task. We don't bother to wait because the caller of
5366 * this routine has no use for the exit status of the release agent
5367 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005368 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005369static void cgroup_release_agent(struct work_struct *work)
5370{
5371 BUG_ON(work != &release_agent_work);
5372 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005373 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005374 while (!list_empty(&release_list)) {
5375 char *argv[3], *envp[3];
5376 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005377 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005378 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005379 struct cgroup,
5380 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005381 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005382 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005383 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005384 if (!pathbuf)
5385 goto continue_free;
5386 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5387 goto continue_free;
5388 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5389 if (!agentbuf)
5390 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005391
5392 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005393 argv[i++] = agentbuf;
5394 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005395 argv[i] = NULL;
5396
5397 i = 0;
5398 /* minimal command environment */
5399 envp[i++] = "HOME=/";
5400 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5401 envp[i] = NULL;
5402
5403 /* Drop the lock while we invoke the usermode helper,
5404 * since the exec could involve hitting disk and hence
5405 * be a slow process */
5406 mutex_unlock(&cgroup_mutex);
5407 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005408 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005409 continue_free:
5410 kfree(pathbuf);
5411 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005412 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005413 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005414 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005415 mutex_unlock(&cgroup_mutex);
5416}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005417
5418static int __init cgroup_disable(char *str)
5419{
Tejun Heo30159ec2013-06-25 11:53:37 -07005420 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005421 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005422 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005423
5424 while ((token = strsep(&str, ",")) != NULL) {
5425 if (!*token)
5426 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005427
Tejun Heo30159ec2013-06-25 11:53:37 -07005428 /*
5429 * cgroup_disable, being at boot time, can't know about
5430 * module subsystems, so we don't worry about them.
5431 */
5432 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005433 if (!strcmp(token, ss->name)) {
5434 ss->disabled = 1;
5435 printk(KERN_INFO "Disabling %s control group"
5436 " subsystem\n", ss->name);
5437 break;
5438 }
5439 }
5440 }
5441 return 1;
5442}
5443__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005444
Tejun Heob77d7b62013-08-13 11:01:54 -04005445/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005446 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5447 * @dentry: directory dentry of interest
5448 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005449 *
5450 * Must be called under RCU read lock. The caller is responsible for
5451 * pinning the returned css if it needs to be accessed outside the RCU
5452 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005453 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005454struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5455 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005456{
5457 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005458
Tejun Heob77d7b62013-08-13 11:01:54 -04005459 WARN_ON_ONCE(!rcu_read_lock_held());
5460
Tejun Heo35cf0832013-08-26 18:40:56 -04005461 /* is @dentry a cgroup dir? */
5462 if (!dentry->d_inode ||
5463 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005464 return ERR_PTR(-EBADF);
5465
Tejun Heo35cf0832013-08-26 18:40:56 -04005466 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005467 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005468}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005469
Li Zefan1cb650b2013-08-19 10:05:24 +08005470/**
5471 * css_from_id - lookup css by id
5472 * @id: the cgroup id
5473 * @ss: cgroup subsys to be looked into
5474 *
5475 * Returns the css if there's valid one with @id, otherwise returns NULL.
5476 * Should be called under rcu_read_lock().
5477 */
5478struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5479{
5480 struct cgroup *cgrp;
5481
5482 rcu_lockdep_assert(rcu_read_lock_held() ||
5483 lockdep_is_held(&cgroup_mutex),
5484 "css_from_id() needs proper protection");
5485
5486 cgrp = idr_find(&ss->root->cgroup_idr, id);
5487 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005488 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005489 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005490}
5491
Paul Menagefe693432009-09-23 15:56:20 -07005492#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005493static struct cgroup_subsys_state *
5494debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005495{
5496 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5497
5498 if (!css)
5499 return ERR_PTR(-ENOMEM);
5500
5501 return css;
5502}
5503
Tejun Heoeb954192013-08-08 20:11:23 -04005504static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005505{
Tejun Heoeb954192013-08-08 20:11:23 -04005506 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005507}
5508
Tejun Heo182446d2013-08-08 20:11:24 -04005509static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5510 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005511{
Tejun Heo182446d2013-08-08 20:11:24 -04005512 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005513}
5514
Tejun Heo182446d2013-08-08 20:11:24 -04005515static u64 current_css_set_read(struct cgroup_subsys_state *css,
5516 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005517{
5518 return (u64)(unsigned long)current->cgroups;
5519}
5520
Tejun Heo182446d2013-08-08 20:11:24 -04005521static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005522 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005523{
5524 u64 count;
5525
5526 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005527 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005528 rcu_read_unlock();
5529 return count;
5530}
5531
Tejun Heo182446d2013-08-08 20:11:24 -04005532static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005533 struct cftype *cft,
5534 struct seq_file *seq)
5535{
Tejun Heo69d02062013-06-12 21:04:50 -07005536 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005537 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005538
5539 read_lock(&css_set_lock);
5540 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005541 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005542 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005543 struct cgroup *c = link->cgrp;
5544 const char *name;
5545
5546 if (c->dentry)
5547 name = c->dentry->d_name.name;
5548 else
5549 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005550 seq_printf(seq, "Root %d group %s\n",
5551 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005552 }
5553 rcu_read_unlock();
5554 read_unlock(&css_set_lock);
5555 return 0;
5556}
5557
5558#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005559static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5560 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005561{
Tejun Heo69d02062013-06-12 21:04:50 -07005562 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005563
5564 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005565 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005566 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005567 struct task_struct *task;
5568 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005569 seq_printf(seq, "css_set %p\n", cset);
5570 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005571 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5572 seq_puts(seq, " ...\n");
5573 break;
5574 } else {
5575 seq_printf(seq, " task %d\n",
5576 task_pid_vnr(task));
5577 }
5578 }
5579 }
5580 read_unlock(&css_set_lock);
5581 return 0;
5582}
5583
Tejun Heo182446d2013-08-08 20:11:24 -04005584static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005585{
Tejun Heo182446d2013-08-08 20:11:24 -04005586 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005587}
5588
5589static struct cftype debug_files[] = {
5590 {
Paul Menagefe693432009-09-23 15:56:20 -07005591 .name = "taskcount",
5592 .read_u64 = debug_taskcount_read,
5593 },
5594
5595 {
5596 .name = "current_css_set",
5597 .read_u64 = current_css_set_read,
5598 },
5599
5600 {
5601 .name = "current_css_set_refcount",
5602 .read_u64 = current_css_set_refcount_read,
5603 },
5604
5605 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005606 .name = "current_css_set_cg_links",
5607 .read_seq_string = current_css_set_cg_links_read,
5608 },
5609
5610 {
5611 .name = "cgroup_css_links",
5612 .read_seq_string = cgroup_css_links_read,
5613 },
5614
5615 {
Paul Menagefe693432009-09-23 15:56:20 -07005616 .name = "releasable",
5617 .read_u64 = releasable_read,
5618 },
Paul Menagefe693432009-09-23 15:56:20 -07005619
Tejun Heo4baf6e32012-04-01 12:09:55 -07005620 { } /* terminate */
5621};
Paul Menagefe693432009-09-23 15:56:20 -07005622
5623struct cgroup_subsys debug_subsys = {
5624 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005625 .css_alloc = debug_css_alloc,
5626 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005627 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005628 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005629};
5630#endif /* CONFIG_CGROUP_DEBUG */