blob: a5629f1df13a06f0a878b3461d1b5f78041d39bc [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
Al Viroc72a04e2011-01-14 05:31:45 +0000898static int cgroup_delete(const struct dentry *d)
899{
900 return 1;
901}
902
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903static void remove_dir(struct dentry *d)
904{
905 struct dentry *parent = dget(d->d_parent);
906
907 d_delete(d);
908 simple_rmdir(parent->d_inode, d);
909 dput(parent);
910}
911
Li Zefan2739d3c2013-01-21 18:18:33 +0800912static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
917 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100918
Li Zefan2739d3c2013-01-21 18:18:33 +0800919 /*
920 * If we're doing cleanup due to failure of cgroup_create(),
921 * the corresponding @cfe may not exist.
922 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700923 list_for_each_entry(cfe, &cgrp->files, node) {
924 struct dentry *d = cfe->dentry;
925
926 if (cft && cfe->type != cft)
927 continue;
928
929 dget(d);
930 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700931 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 list_del_init(&cfe->node);
933 dput(d);
934
Li Zefan2739d3c2013-01-21 18:18:33 +0800935 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937}
938
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400939/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700940 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700941 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400942 * @subsys_mask: mask of the subsystem ids whose files should be removed
943 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700944static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700945{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400946 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700947 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948
Tejun Heob420ba72013-07-12 12:34:02 -0700949 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400950 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700951
952 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400953 continue;
954 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400955 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400956 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957}
958
959/*
960 * NOTE : the dentry must have been dget()'ed
961 */
962static void cgroup_d_remove_dir(struct dentry *dentry)
963{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100964 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100966 parent = dentry->d_parent;
967 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800968 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100970 spin_unlock(&dentry->d_lock);
971 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 remove_dir(dentry);
973}
974
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700975/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800976 * Call with cgroup_mutex held. Drops reference counts on modules, including
977 * any duplicate ones that parse_cgroupfs_options took. If this function
978 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800979 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700981 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982{
Paul Menagebd89aab2007-10-18 23:40:44 -0700983 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700984 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700985 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700986 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987
Ben Blumaae8aab2010-03-10 15:22:07 -0800988 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800989 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800990
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700992 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700993 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700995
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700996 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700997 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700998 ret = -EBUSY;
999 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001001
1002 /* pin the module */
1003 if (!try_module_get(ss->module)) {
1004 ret = -ENOENT;
1005 goto out_put;
1006 }
1007 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 }
1009
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001010 /* subsys could be missing if unloaded between parsing and here */
1011 if (added_mask != pinned) {
1012 ret = -ENOENT;
1013 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015
Tejun Heo31261212013-06-28 17:07:30 -07001016 ret = cgroup_populate_dir(cgrp, added_mask);
1017 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001018 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001019
1020 /*
1021 * Nothing can fail from this point on. Remove files for the
1022 * removed subsystems and rebind each subsystem.
1023 */
1024 cgroup_clear_dir(cgrp, removed_mask);
1025
Tejun Heo30159ec2013-06-25 11:53:37 -07001026 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001028
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001029 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001031 BUG_ON(cgroup_css(cgrp, ss));
1032 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1033 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001034
Tejun Heo73e80ed2013-08-13 11:01:55 -04001035 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001036 cgroup_css(cgroup_dummy_top, ss));
1037 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001038
Li Zefan33a68ac2009-01-07 18:07:42 -08001039 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001040 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001042 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001043
Ben Blumcf5d5942010-03-10 15:22:09 -08001044 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001045 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001046 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001048 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1049 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001050
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001052 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001053
Tejun Heoca8bdca2013-08-26 18:40:56 -04001054 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001055 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1056
Tejun Heo9871bf92013-06-24 15:21:47 -07001057 cgroup_subsys[i]->root = &cgroup_dummy_root;
1058 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001059
Ben Blumcf5d5942010-03-10 15:22:09 -08001060 /* subsystem is now free - drop reference on module */
1061 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 }
1064 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065
Tejun Heo1672d042013-06-25 18:04:54 -07001066 /*
1067 * Mark @root has finished binding subsystems. @root->subsys_mask
1068 * now matches the bound subsystems.
1069 */
1070 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1071
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001073
1074out_put:
1075 for_each_subsys(ss, i)
1076 if (pinned & (1 << i))
1077 module_put(ss->module);
1078 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079}
1080
Al Viro34c80b12011-12-08 21:32:45 -05001081static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082{
Al Viro34c80b12011-12-08 21:32:45 -05001083 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 struct cgroup_subsys *ss;
1085
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001086 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001087 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001089 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1090 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001091 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001093 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001094 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001095 if (strlen(root->release_agent_path))
1096 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001097 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001098 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001099 if (strlen(root->name))
1100 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001101 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 return 0;
1103}
1104
1105struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001106 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001108 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001109 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001110 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001111 /* User explicitly requested empty subsystem */
1112 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001113
1114 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001115
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116};
1117
Ben Blumaae8aab2010-03-10 15:22:07 -08001118/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001119 * Convert a hierarchy specifier into a bitmask of subsystems and
1120 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1121 * array. This function takes refcounts on subsystems to be used, unless it
1122 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001123 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001124static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001126 char *token, *o = data;
1127 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001128 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001129 struct cgroup_subsys *ss;
1130 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001131
Ben Blumaae8aab2010-03-10 15:22:07 -08001132 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1133
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134#ifdef CONFIG_CPUSETS
1135 mask = ~(1UL << cpuset_subsys_id);
1136#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137
Paul Menagec6d57f32009-09-23 15:56:19 -07001138 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139
1140 while ((token = strsep(&o, ",")) != NULL) {
1141 if (!*token)
1142 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001144 /* Explicitly have no subsystems */
1145 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 continue;
1147 }
1148 if (!strcmp(token, "all")) {
1149 /* Mutually exclusive option 'all' + subsystem name */
1150 if (one_ss)
1151 return -EINVAL;
1152 all_ss = true;
1153 continue;
1154 }
Tejun Heo873fe092013-04-14 20:15:26 -07001155 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1156 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1157 continue;
1158 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001160 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 continue;
1162 }
1163 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001164 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 continue;
1166 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001167 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001168 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001169 continue;
1170 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001172 /* Specifying two release agents is forbidden */
1173 if (opts->release_agent)
1174 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001175 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001176 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001177 if (!opts->release_agent)
1178 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001182 const char *name = token + 5;
1183 /* Can't specify an empty name */
1184 if (!strlen(name))
1185 return -EINVAL;
1186 /* Must match [\w.-]+ */
1187 for (i = 0; i < strlen(name); i++) {
1188 char c = name[i];
1189 if (isalnum(c))
1190 continue;
1191 if ((c == '.') || (c == '-') || (c == '_'))
1192 continue;
1193 return -EINVAL;
1194 }
1195 /* Specifying two names is forbidden */
1196 if (opts->name)
1197 return -EINVAL;
1198 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001199 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 GFP_KERNEL);
1201 if (!opts->name)
1202 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001203
1204 continue;
1205 }
1206
Tejun Heo30159ec2013-06-25 11:53:37 -07001207 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001208 if (strcmp(token, ss->name))
1209 continue;
1210 if (ss->disabled)
1211 continue;
1212
1213 /* Mutually exclusive option 'all' + subsystem name */
1214 if (all_ss)
1215 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001216 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001217 one_ss = true;
1218
1219 break;
1220 }
1221 if (i == CGROUP_SUBSYS_COUNT)
1222 return -ENOENT;
1223 }
1224
1225 /*
1226 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001227 * otherwise if 'none', 'name=' and a subsystem name options
1228 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001229 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001230 if (all_ss || (!one_ss && !opts->none && !opts->name))
1231 for_each_subsys(ss, i)
1232 if (!ss->disabled)
1233 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001234
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001235 /* Consistency checks */
1236
Tejun Heo873fe092013-04-14 20:15:26 -07001237 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1238 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1239
1240 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1241 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1242 return -EINVAL;
1243 }
1244
1245 if (opts->cpuset_clone_children) {
1246 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1247 return -EINVAL;
1248 }
1249 }
1250
Li Zefanf9ab5b52009-06-17 16:26:33 -07001251 /*
1252 * Option noprefix was introduced just for backward compatibility
1253 * with the old cpuset, so we allow noprefix only if mounting just
1254 * the cpuset subsystem.
1255 */
Tejun Heo93438622013-04-14 20:15:25 -07001256 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001257 return -EINVAL;
1258
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259
1260 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001261 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262 return -EINVAL;
1263
1264 /*
1265 * We either have to specify by name or by subsystems. (So all
1266 * empty hierarchies must have a name).
1267 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 return -EINVAL;
1270
1271 return 0;
1272}
1273
1274static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1275{
1276 int ret = 0;
1277 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001278 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001279 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001280 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281
Tejun Heo873fe092013-04-14 20:15:26 -07001282 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1283 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1284 return -EINVAL;
1285 }
1286
Paul Menagebd89aab2007-10-18 23:40:44 -07001287 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001288 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001289 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290
1291 /* See what subsystems are wanted */
1292 ret = parse_cgroupfs_options(data, &opts);
1293 if (ret)
1294 goto out_unlock;
1295
Tejun Heoa8a648c2013-06-24 15:21:47 -07001296 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001297 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1298 task_tgid_nr(current), current->comm);
1299
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001300 added_mask = opts.subsys_mask & ~root->subsys_mask;
1301 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001302
Ben Blumcf5d5942010-03-10 15:22:09 -08001303 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001304 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001306 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1307 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1308 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001309 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001310 goto out_unlock;
1311 }
1312
Tejun Heof172e672013-06-28 17:07:30 -07001313 /* remounting is not allowed for populated hierarchies */
1314 if (root->number_of_cgroups > 1) {
1315 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001316 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001317 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001320 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322
Paul Menage81a6a5c2007-10-18 23:39:38 -07001323 if (opts.release_agent)
1324 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001326 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001327 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001328 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001330 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 return ret;
1332}
1333
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001334static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335 .statfs = simple_statfs,
1336 .drop_inode = generic_delete_inode,
1337 .show_options = cgroup_show_options,
1338 .remount_fs = cgroup_remount,
1339};
1340
Paul Menagecc31edc2008-10-18 20:28:04 -07001341static void init_cgroup_housekeeping(struct cgroup *cgrp)
1342{
1343 INIT_LIST_HEAD(&cgrp->sibling);
1344 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001345 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001346 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001347 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001348 INIT_LIST_HEAD(&cgrp->pidlists);
1349 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001350 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001351 INIT_LIST_HEAD(&cgrp->event_list);
1352 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001353 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001354}
Paul Menagec6d57f32009-09-23 15:56:19 -07001355
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356static void init_cgroup_root(struct cgroupfs_root *root)
1357{
Paul Menagebd89aab2007-10-18 23:40:44 -07001358 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001359
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 INIT_LIST_HEAD(&root->subsys_list);
1361 INIT_LIST_HEAD(&root->root_list);
1362 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001363 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001364 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001365 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001366 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367}
1368
Tejun Heofc76df72013-06-25 11:53:37 -07001369static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001370{
Tejun Heo1a574232013-04-14 11:36:58 -07001371 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001372
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001373 lockdep_assert_held(&cgroup_mutex);
1374 lockdep_assert_held(&cgroup_root_mutex);
1375
Tejun Heofc76df72013-06-25 11:53:37 -07001376 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1377 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001378 if (id < 0)
1379 return id;
1380
1381 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001382 return 0;
1383}
1384
1385static void cgroup_exit_root_id(struct cgroupfs_root *root)
1386{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001387 lockdep_assert_held(&cgroup_mutex);
1388 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001389
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001390 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001391 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001392 root->hierarchy_id = 0;
1393 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394}
1395
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396static int cgroup_test_super(struct super_block *sb, void *data)
1397{
Paul Menagec6d57f32009-09-23 15:56:19 -07001398 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399 struct cgroupfs_root *root = sb->s_fs_info;
1400
Paul Menagec6d57f32009-09-23 15:56:19 -07001401 /* If we asked for a name then it must match */
1402 if (opts->name && strcmp(opts->name, root->name))
1403 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001405 /*
1406 * If we asked for subsystems (or explicitly for no
1407 * subsystems) then they must match
1408 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001409 if ((opts->subsys_mask || opts->none)
1410 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411 return 0;
1412
1413 return 1;
1414}
1415
Paul Menagec6d57f32009-09-23 15:56:19 -07001416static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1417{
1418 struct cgroupfs_root *root;
1419
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001420 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001421 return NULL;
1422
1423 root = kzalloc(sizeof(*root), GFP_KERNEL);
1424 if (!root)
1425 return ERR_PTR(-ENOMEM);
1426
1427 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428
Tejun Heo1672d042013-06-25 18:04:54 -07001429 /*
1430 * We need to set @root->subsys_mask now so that @root can be
1431 * matched by cgroup_test_super() before it finishes
1432 * initialization; otherwise, competing mounts with the same
1433 * options may try to bind the same subsystems instead of waiting
1434 * for the first one leading to unexpected mount errors.
1435 * SUBSYS_BOUND will be set once actual binding is complete.
1436 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001437 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001438 root->flags = opts->flags;
1439 if (opts->release_agent)
1440 strcpy(root->release_agent_path, opts->release_agent);
1441 if (opts->name)
1442 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001443 if (opts->cpuset_clone_children)
1444 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001445 return root;
1446}
1447
Tejun Heofa3ca072013-04-14 11:36:56 -07001448static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001449{
Tejun Heofa3ca072013-04-14 11:36:56 -07001450 if (root) {
1451 /* hierarhcy ID shoulid already have been released */
1452 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001454 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001455 kfree(root);
1456 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001457}
1458
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459static int cgroup_set_super(struct super_block *sb, void *data)
1460{
1461 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 struct cgroup_sb_opts *opts = data;
1463
1464 /* If we don't have a new root, we can't set up a new sb */
1465 if (!opts->new_root)
1466 return -EINVAL;
1467
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001468 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469
1470 ret = set_anon_super(sb, NULL);
1471 if (ret)
1472 return ret;
1473
Paul Menagec6d57f32009-09-23 15:56:19 -07001474 sb->s_fs_info = opts->new_root;
1475 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476
1477 sb->s_blocksize = PAGE_CACHE_SIZE;
1478 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1479 sb->s_magic = CGROUP_SUPER_MAGIC;
1480 sb->s_op = &cgroup_ops;
1481
1482 return 0;
1483}
1484
1485static int cgroup_get_rootdir(struct super_block *sb)
1486{
Al Viro0df6a632010-12-21 13:29:29 -05001487 static const struct dentry_operations cgroup_dops = {
1488 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001489 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001490 };
1491
Paul Menageddbcc7e2007-10-18 23:39:30 -07001492 struct inode *inode =
1493 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494
1495 if (!inode)
1496 return -ENOMEM;
1497
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498 inode->i_fop = &simple_dir_operations;
1499 inode->i_op = &cgroup_dir_inode_operations;
1500 /* directories start off with i_nlink == 2 (for "." entry) */
1501 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001502 sb->s_root = d_make_root(inode);
1503 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001505 /* for everything else we want ->d_op set */
1506 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 return 0;
1508}
1509
Al Virof7e83572010-07-26 13:23:11 +04001510static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001512 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513{
1514 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516 int ret = 0;
1517 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001519 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001520 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001521 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522
1523 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001524 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001526 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001527 if (ret)
1528 goto out_err;
1529
1530 /*
1531 * Allocate a new cgroup root. We may not need it if we're
1532 * reusing an existing hierarchy.
1533 */
1534 new_root = cgroup_root_from_opts(&opts);
1535 if (IS_ERR(new_root)) {
1536 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001537 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001538 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001539 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540
Paul Menagec6d57f32009-09-23 15:56:19 -07001541 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001542 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001545 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001546 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 }
1548
Paul Menagec6d57f32009-09-23 15:56:19 -07001549 root = sb->s_fs_info;
1550 BUG_ON(!root);
1551 if (root == opts.new_root) {
1552 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001553 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001554 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001555 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001556 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557
1558 BUG_ON(sb->s_root != NULL);
1559
1560 ret = cgroup_get_rootdir(sb);
1561 if (ret)
1562 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001563 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564
Paul Menage817929e2007-10-18 23:39:36 -07001565 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001567 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001569 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1570 0, 1, GFP_KERNEL);
1571 if (root_cgrp->id < 0)
1572 goto unlock_drop;
1573
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001574 /* Check for name clashes with existing mounts */
1575 ret = -EBUSY;
1576 if (strlen(root->name))
1577 for_each_active_root(existing_root)
1578 if (!strcmp(existing_root->name, root->name))
1579 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001580
Paul Menage817929e2007-10-18 23:39:36 -07001581 /*
1582 * We're accessing css_set_count without locking
1583 * css_set_lock here, but that's OK - it can only be
1584 * increased by someone holding cgroup_lock, and
1585 * that's us. The worst that can happen is that we
1586 * have some link structures left over
1587 */
Tejun Heo69d02062013-06-12 21:04:50 -07001588 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001589 if (ret)
1590 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001591
Tejun Heofc76df72013-06-25 11:53:37 -07001592 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1593 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001594 if (ret)
1595 goto unlock_drop;
1596
Tejun Heo31261212013-06-28 17:07:30 -07001597 sb->s_root->d_fsdata = root_cgrp;
1598 root_cgrp->dentry = sb->s_root;
1599
1600 /*
1601 * We're inside get_sb() and will call lookup_one_len() to
1602 * create the root files, which doesn't work if SELinux is
1603 * in use. The following cred dancing somehow works around
1604 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1605 * populating new cgroupfs mount") for more details.
1606 */
1607 cred = override_creds(&init_cred);
1608
Tejun Heo2bb566c2013-08-08 20:11:23 -04001609 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001610 if (ret)
1611 goto rm_base_files;
1612
Tejun Heoa8a648c2013-06-24 15:21:47 -07001613 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001614 if (ret)
1615 goto rm_base_files;
1616
1617 revert_creds(cred);
1618
Ben Blumcf5d5942010-03-10 15:22:09 -08001619 /*
1620 * There must be no failure case after here, since rebinding
1621 * takes care of subsystems' refcounts, which are explicitly
1622 * dropped in the failure exit path.
1623 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
Tejun Heo9871bf92013-06-24 15:21:47 -07001625 list_add(&root->root_list, &cgroup_roots);
1626 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627
Paul Menage817929e2007-10-18 23:39:36 -07001628 /* Link the top cgroup in this hierarchy into all
1629 * the css_set objects */
1630 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001631 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001632 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001633 write_unlock(&css_set_lock);
1634
Tejun Heo69d02062013-06-12 21:04:50 -07001635 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001636
Li Zefanc12f65d2009-01-07 18:07:42 -08001637 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638 BUG_ON(root->number_of_cgroups != 1);
1639
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001640 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001642 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001643 } else {
1644 /*
1645 * We re-used an existing hierarchy - the new root (if
1646 * any) is not needed
1647 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001648 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001649
Tejun Heoc7ba8282013-06-29 14:06:10 -07001650 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001651 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1652 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1653 ret = -EINVAL;
1654 goto drop_new_super;
1655 } else {
1656 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1657 }
Tejun Heo873fe092013-04-14 20:15:26 -07001658 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 }
1660
Paul Menagec6d57f32009-09-23 15:56:19 -07001661 kfree(opts.release_agent);
1662 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001663 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
Tejun Heo31261212013-06-28 17:07:30 -07001665 rm_base_files:
1666 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001667 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001668 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001669 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001670 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001671 mutex_unlock(&cgroup_root_mutex);
1672 mutex_unlock(&cgroup_mutex);
1673 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001675 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001676 out_err:
1677 kfree(opts.release_agent);
1678 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001679 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680}
1681
1682static void cgroup_kill_sb(struct super_block *sb) {
1683 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001684 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001685 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686 int ret;
1687
1688 BUG_ON(!root);
1689
1690 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001691 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692
Tejun Heo31261212013-06-28 17:07:30 -07001693 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001695 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
1697 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001698 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1699 ret = rebind_subsystems(root, 0, root->subsys_mask);
1700 /* Shouldn't be able to fail ... */
1701 BUG_ON(ret);
1702 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703
Paul Menage817929e2007-10-18 23:39:36 -07001704 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001705 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001706 * root cgroup
1707 */
1708 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001709
Tejun Heo69d02062013-06-12 21:04:50 -07001710 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1711 list_del(&link->cset_link);
1712 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001713 kfree(link);
1714 }
1715 write_unlock(&css_set_lock);
1716
Paul Menage839ec542009-01-29 14:25:22 -08001717 if (!list_empty(&root->root_list)) {
1718 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001719 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001720 }
Li Zefane5f6a862009-01-07 18:07:41 -08001721
Tejun Heofa3ca072013-04-14 11:36:56 -07001722 cgroup_exit_root_id(root);
1723
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001724 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001726 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001728 simple_xattrs_free(&cgrp->xattrs);
1729
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001731 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732}
1733
1734static struct file_system_type cgroup_fs_type = {
1735 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001736 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 .kill_sb = cgroup_kill_sb,
1738};
1739
Greg KH676db4a2010-08-05 13:53:35 -07001740static struct kobject *cgroup_kobj;
1741
Li Zefana043e3b2008-02-23 15:24:09 -08001742/**
1743 * cgroup_path - generate the path of a cgroup
1744 * @cgrp: the cgroup in question
1745 * @buf: the buffer to write the path into
1746 * @buflen: the length of the buffer
1747 *
Li Zefan65dff752013-03-01 15:01:56 +08001748 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1749 *
1750 * We can't generate cgroup path using dentry->d_name, as accessing
1751 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1752 * inode's i_mutex, while on the other hand cgroup_path() can be called
1753 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001755int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756{
Li Zefan65dff752013-03-01 15:01:56 +08001757 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001759
Tejun Heoda1f2962013-04-14 10:32:19 -07001760 if (!cgrp->parent) {
1761 if (strlcpy(buf, "/", buflen) >= buflen)
1762 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763 return 0;
1764 }
1765
Tao Ma316eb662012-11-08 21:36:38 +08001766 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001767 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001768
Li Zefan65dff752013-03-01 15:01:56 +08001769 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001770 do {
Li Zefan65dff752013-03-01 15:01:56 +08001771 const char *name = cgroup_name(cgrp);
1772 int len;
1773
1774 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001776 goto out;
1777 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001778
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001780 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001782
1783 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001784 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001785 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001787out:
1788 rcu_read_unlock();
1789 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790}
Ben Blum67523c42010-03-10 15:22:11 -08001791EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792
Tejun Heo857a2be2013-04-14 20:50:08 -07001793/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001794 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001795 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001796 * @buf: the buffer to write the path into
1797 * @buflen: the length of the buffer
1798 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001799 * Determine @task's cgroup on the first (the one with the lowest non-zero
1800 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1801 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1802 * cgroup controller callbacks.
1803 *
1804 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001805 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001806int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001807{
1808 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001809 struct cgroup *cgrp;
1810 int hierarchy_id = 1, ret = 0;
1811
1812 if (buflen < 2)
1813 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001814
1815 mutex_lock(&cgroup_mutex);
1816
Tejun Heo913ffdb2013-07-11 16:34:48 -07001817 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1818
Tejun Heo857a2be2013-04-14 20:50:08 -07001819 if (root) {
1820 cgrp = task_cgroup_from_root(task, root);
1821 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001822 } else {
1823 /* if no hierarchy exists, everyone is in "/" */
1824 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001825 }
1826
1827 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001828 return ret;
1829}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001830EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001831
Ben Blum74a11662011-05-26 16:25:20 -07001832/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001833 * Control Group taskset
1834 */
Tejun Heo134d3372011-12-12 18:12:21 -08001835struct task_and_cgroup {
1836 struct task_struct *task;
1837 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001838 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001839};
1840
Tejun Heo2f7ee562011-12-12 18:12:21 -08001841struct cgroup_taskset {
1842 struct task_and_cgroup single;
1843 struct flex_array *tc_array;
1844 int tc_array_len;
1845 int idx;
1846 struct cgroup *cur_cgrp;
1847};
1848
1849/**
1850 * cgroup_taskset_first - reset taskset and return the first task
1851 * @tset: taskset of interest
1852 *
1853 * @tset iteration is initialized and the first task is returned.
1854 */
1855struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1856{
1857 if (tset->tc_array) {
1858 tset->idx = 0;
1859 return cgroup_taskset_next(tset);
1860 } else {
1861 tset->cur_cgrp = tset->single.cgrp;
1862 return tset->single.task;
1863 }
1864}
1865EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1866
1867/**
1868 * cgroup_taskset_next - iterate to the next task in taskset
1869 * @tset: taskset of interest
1870 *
1871 * Return the next task in @tset. Iteration must have been initialized
1872 * with cgroup_taskset_first().
1873 */
1874struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1875{
1876 struct task_and_cgroup *tc;
1877
1878 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1879 return NULL;
1880
1881 tc = flex_array_get(tset->tc_array, tset->idx++);
1882 tset->cur_cgrp = tc->cgrp;
1883 return tc->task;
1884}
1885EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1886
1887/**
Tejun Heod99c8722013-08-08 20:11:27 -04001888 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001889 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001890 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001891 *
Tejun Heod99c8722013-08-08 20:11:27 -04001892 * Return the css for the current (last returned) task of @tset for
1893 * subsystem specified by @subsys_id. This function must be preceded by
1894 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001895 */
Tejun Heod99c8722013-08-08 20:11:27 -04001896struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1897 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001898{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001899 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001900}
Tejun Heod99c8722013-08-08 20:11:27 -04001901EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001902
1903/**
1904 * cgroup_taskset_size - return the number of tasks in taskset
1905 * @tset: taskset of interest
1906 */
1907int cgroup_taskset_size(struct cgroup_taskset *tset)
1908{
1909 return tset->tc_array ? tset->tc_array_len : 1;
1910}
1911EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1912
1913
Ben Blum74a11662011-05-26 16:25:20 -07001914/*
1915 * cgroup_task_migrate - move a task from one cgroup to another.
1916 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001917 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001918 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001919static void cgroup_task_migrate(struct cgroup *old_cgrp,
1920 struct task_struct *tsk,
1921 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001922{
Tejun Heo5abb8852013-06-12 21:04:49 -07001923 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001924
1925 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001926 * We are synchronized through threadgroup_lock() against PF_EXITING
1927 * setting such that we can't race against cgroup_exit() changing the
1928 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001929 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001930 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001931 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001932
Ben Blum74a11662011-05-26 16:25:20 -07001933 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001934 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001935 task_unlock(tsk);
1936
1937 /* Update the css_set linked lists if we're using them */
1938 write_lock(&css_set_lock);
1939 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001940 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001941 write_unlock(&css_set_lock);
1942
1943 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001944 * We just gained a reference on old_cset by taking it from the
1945 * task. As trading it for new_cset is protected by cgroup_mutex,
1946 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001947 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001948 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1949 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001950}
1951
Li Zefana043e3b2008-02-23 15:24:09 -08001952/**
Li Zefan081aa452013-03-13 09:17:09 +08001953 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001954 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001955 * @tsk: the task or the leader of the threadgroup to be attached
1956 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001957 *
Tejun Heo257058a2011-12-12 18:12:21 -08001958 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001959 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001960 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001961static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1962 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001963{
1964 int retval, i, group_size;
1965 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001966 struct cgroupfs_root *root = cgrp->root;
1967 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001968 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001969 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001970 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001971 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001972
1973 /*
1974 * step 0: in order to do expensive, possibly blocking operations for
1975 * every thread, we cannot iterate the thread group list, since it needs
1976 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001977 * group - group_rwsem prevents new threads from appearing, and if
1978 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001979 */
Li Zefan081aa452013-03-13 09:17:09 +08001980 if (threadgroup)
1981 group_size = get_nr_threads(tsk);
1982 else
1983 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001984 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001985 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001986 if (!group)
1987 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001988 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001989 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001990 if (retval)
1991 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001992
Ben Blum74a11662011-05-26 16:25:20 -07001993 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001994 /*
1995 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1996 * already PF_EXITING could be freed from underneath us unless we
1997 * take an rcu_read_lock.
1998 */
1999 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002000 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002001 struct task_and_cgroup ent;
2002
Tejun Heocd3d0952011-12-12 18:12:21 -08002003 /* @tsk either already exited or can't exit until the end */
2004 if (tsk->flags & PF_EXITING)
2005 continue;
2006
Ben Blum74a11662011-05-26 16:25:20 -07002007 /* as per above, nr_threads may decrease, but not increase. */
2008 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002009 ent.task = tsk;
2010 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002011 /* nothing to do if this task is already in the cgroup */
2012 if (ent.cgrp == cgrp)
2013 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002014 /*
2015 * saying GFP_ATOMIC has no effect here because we did prealloc
2016 * earlier, but it's good form to communicate our expectations.
2017 */
Tejun Heo134d3372011-12-12 18:12:21 -08002018 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002019 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002020 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002021
2022 if (!threadgroup)
2023 break;
Ben Blum74a11662011-05-26 16:25:20 -07002024 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002025 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002026 /* remember the number of threads in the array for later. */
2027 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002028 tset.tc_array = group;
2029 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002030
Tejun Heo134d3372011-12-12 18:12:21 -08002031 /* methods shouldn't be called if no task is actually migrating */
2032 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002033 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002034 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002035
Ben Blum74a11662011-05-26 16:25:20 -07002036 /*
2037 * step 1: check that we can legitimately attach to the cgroup.
2038 */
Tejun Heo5549c492013-06-24 15:21:48 -07002039 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002040 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002041
Ben Blum74a11662011-05-26 16:25:20 -07002042 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002043 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002044 if (retval) {
2045 failed_ss = ss;
2046 goto out_cancel_attach;
2047 }
2048 }
Ben Blum74a11662011-05-26 16:25:20 -07002049 }
2050
2051 /*
2052 * step 2: make sure css_sets exist for all threads to be migrated.
2053 * we use find_css_set, which allocates a new one if necessary.
2054 */
Ben Blum74a11662011-05-26 16:25:20 -07002055 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002056 struct css_set *old_cset;
2057
Tejun Heo134d3372011-12-12 18:12:21 -08002058 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002059 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002060 tc->cset = find_css_set(old_cset, cgrp);
2061 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002062 retval = -ENOMEM;
2063 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002064 }
2065 }
2066
2067 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002068 * step 3: now that we're guaranteed success wrt the css_sets,
2069 * proceed to move all tasks to the new cgroup. There are no
2070 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002071 */
Ben Blum74a11662011-05-26 16:25:20 -07002072 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002073 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002074 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002075 }
2076 /* nothing is sensitive to fork() after this point. */
2077
2078 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002079 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002080 */
Tejun Heo5549c492013-06-24 15:21:48 -07002081 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002082 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002083
Ben Blum74a11662011-05-26 16:25:20 -07002084 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002085 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087
2088 /*
2089 * step 5: success! and cleanup
2090 */
Ben Blum74a11662011-05-26 16:25:20 -07002091 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002092out_put_css_set_refs:
2093 if (retval) {
2094 for (i = 0; i < group_size; i++) {
2095 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002096 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002098 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002099 }
Ben Blum74a11662011-05-26 16:25:20 -07002100 }
2101out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002102 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002103 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002104 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002105
Tejun Heo494c1672011-12-12 18:12:22 -08002106 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002107 break;
Ben Blum74a11662011-05-26 16:25:20 -07002108 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002109 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002110 }
2111 }
Ben Blum74a11662011-05-26 16:25:20 -07002112out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002113 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002114 return retval;
2115}
2116
2117/*
2118 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002119 * function to attach either it or all tasks in its threadgroup. Will lock
2120 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002121 */
2122static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002123{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002124 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002125 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002126 int ret;
2127
Ben Blum74a11662011-05-26 16:25:20 -07002128 if (!cgroup_lock_live_group(cgrp))
2129 return -ENODEV;
2130
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002131retry_find_task:
2132 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002133 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002134 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002135 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002137 ret= -ESRCH;
2138 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 }
Ben Blum74a11662011-05-26 16:25:20 -07002140 /*
2141 * even if we're attaching all tasks in the thread group, we
2142 * only need to check permissions on one of them.
2143 */
David Howellsc69e8d92008-11-14 10:39:19 +11002144 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002145 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2146 !uid_eq(cred->euid, tcred->uid) &&
2147 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002148 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002149 ret = -EACCES;
2150 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002151 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 } else
2153 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002154
2155 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002156 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002157
2158 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002159 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002160 * trapped in a cpuset, or RT worker may be born in a cgroup
2161 * with no rt_runtime allocated. Just say no.
2162 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002163 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002164 ret = -EINVAL;
2165 rcu_read_unlock();
2166 goto out_unlock_cgroup;
2167 }
2168
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 get_task_struct(tsk);
2170 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002171
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 threadgroup_lock(tsk);
2173 if (threadgroup) {
2174 if (!thread_group_leader(tsk)) {
2175 /*
2176 * a race with de_thread from another thread's exec()
2177 * may strip us of our leadership, if this happens,
2178 * there is no choice but to throw this task away and
2179 * try again; this is
2180 * "double-double-toil-and-trouble-check locking".
2181 */
2182 threadgroup_unlock(tsk);
2183 put_task_struct(tsk);
2184 goto retry_find_task;
2185 }
Li Zefan081aa452013-03-13 09:17:09 +08002186 }
2187
2188 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2189
Tejun Heocd3d0952011-12-12 18:12:21 -08002190 threadgroup_unlock(tsk);
2191
Paul Menagebbcb81d2007-10-18 23:39:32 -07002192 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002194 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 return ret;
2196}
2197
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002198/**
2199 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2200 * @from: attach to all cgroups of a given task
2201 * @tsk: the task to be attached
2202 */
2203int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2204{
2205 struct cgroupfs_root *root;
2206 int retval = 0;
2207
Tejun Heo47cfcd02013-04-07 09:29:51 -07002208 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002209 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002210 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002211
Li Zefan6f4b7e62013-07-31 16:18:36 +08002212 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213 if (retval)
2214 break;
2215 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002216 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002217
2218 return retval;
2219}
2220EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2221
Tejun Heo182446d2013-08-08 20:11:24 -04002222static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2223 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002224{
Tejun Heo182446d2013-08-08 20:11:24 -04002225 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002226}
2227
Tejun Heo182446d2013-08-08 20:11:24 -04002228static int cgroup_procs_write(struct cgroup_subsys_state *css,
2229 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002230{
Tejun Heo182446d2013-08-08 20:11:24 -04002231 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002232}
2233
Tejun Heo182446d2013-08-08 20:11:24 -04002234static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2235 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002236{
Tejun Heo182446d2013-08-08 20:11:24 -04002237 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002238 if (strlen(buffer) >= PATH_MAX)
2239 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002240 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002241 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002242 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002243 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002244 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002245 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002246 return 0;
2247}
2248
Tejun Heo182446d2013-08-08 20:11:24 -04002249static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2250 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002251{
Tejun Heo182446d2013-08-08 20:11:24 -04002252 struct cgroup *cgrp = css->cgroup;
2253
Paul Menagee788e062008-07-25 01:46:59 -07002254 if (!cgroup_lock_live_group(cgrp))
2255 return -ENODEV;
2256 seq_puts(seq, cgrp->root->release_agent_path);
2257 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002258 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002259 return 0;
2260}
2261
Tejun Heo182446d2013-08-08 20:11:24 -04002262static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2263 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002264{
Tejun Heo182446d2013-08-08 20:11:24 -04002265 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002266 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002267}
2268
Paul Menage84eea842008-07-25 01:47:00 -07002269/* A buffer size big enough for numbers or short strings */
2270#define CGROUP_LOCAL_BUFFER_SIZE 64
2271
Tejun Heo182446d2013-08-08 20:11:24 -04002272static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2273 struct cftype *cft, struct file *file,
2274 const char __user *userbuf, size_t nbytes,
2275 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002276{
Paul Menage84eea842008-07-25 01:47:00 -07002277 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002278 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002279 char *end;
2280
2281 if (!nbytes)
2282 return -EINVAL;
2283 if (nbytes >= sizeof(buffer))
2284 return -E2BIG;
2285 if (copy_from_user(buffer, userbuf, nbytes))
2286 return -EFAULT;
2287
2288 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002289 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002290 u64 val = simple_strtoull(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_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002294 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002295 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002296 if (*end)
2297 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002298 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002299 }
Paul Menage355e0c42007-10-18 23:39:33 -07002300 if (!retval)
2301 retval = nbytes;
2302 return retval;
2303}
2304
Tejun Heo182446d2013-08-08 20:11:24 -04002305static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2306 struct cftype *cft, struct file *file,
2307 const char __user *userbuf, size_t nbytes,
2308 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002309{
Paul Menage84eea842008-07-25 01:47:00 -07002310 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002311 int retval = 0;
2312 size_t max_bytes = cft->max_write_len;
2313 char *buffer = local_buffer;
2314
2315 if (!max_bytes)
2316 max_bytes = sizeof(local_buffer) - 1;
2317 if (nbytes >= max_bytes)
2318 return -E2BIG;
2319 /* Allocate a dynamic buffer if we need one */
2320 if (nbytes >= sizeof(local_buffer)) {
2321 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2322 if (buffer == NULL)
2323 return -ENOMEM;
2324 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002325 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2326 retval = -EFAULT;
2327 goto out;
2328 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002329
2330 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002331 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002332 if (!retval)
2333 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002334out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002335 if (buffer != local_buffer)
2336 kfree(buffer);
2337 return retval;
2338}
2339
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002341 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342{
Tejun Heo182446d2013-08-08 20:11:24 -04002343 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002345 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346
Paul Menage355e0c42007-10-18 23:39:33 -07002347 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002348 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002349 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002350 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002351 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002352 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002353 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002354 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002355 return ret ? ret : nbytes;
2356 }
Paul Menage355e0c42007-10-18 23:39:33 -07002357 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358}
2359
Tejun Heo182446d2013-08-08 20:11:24 -04002360static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2361 struct cftype *cft, struct file *file,
2362 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363{
Paul Menage84eea842008-07-25 01:47:00 -07002364 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002365 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002366 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2367
2368 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2369}
2370
Tejun Heo182446d2013-08-08 20:11:24 -04002371static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2372 struct cftype *cft, struct file *file,
2373 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002374{
Paul Menage84eea842008-07-25 01:47:00 -07002375 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002376 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002377 int len = sprintf(tmp, "%lld\n", (long long) val);
2378
2379 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2380}
2381
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002383 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384{
Tejun Heo182446d2013-08-08 20:11:24 -04002385 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002387 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388
2389 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002390 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002391 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002392 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002393 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002394 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395 return -EINVAL;
2396}
2397
Paul Menage91796562008-04-29 01:00:01 -07002398/*
2399 * seqfile ops/methods for returning structured data. Currently just
2400 * supports string->u64 maps, but can be extended in future.
2401 */
2402
Paul Menage91796562008-04-29 01:00:01 -07002403static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2404{
2405 struct seq_file *sf = cb->state;
2406 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2407}
2408
2409static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2410{
Li Zefane0798ce2013-07-31 17:36:25 +08002411 struct cfent *cfe = m->private;
2412 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002413 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002414
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002415 if (cft->read_map) {
2416 struct cgroup_map_cb cb = {
2417 .fill = cgroup_map_add,
2418 .state = m,
2419 };
Tejun Heo182446d2013-08-08 20:11:24 -04002420 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002421 }
Tejun Heo182446d2013-08-08 20:11:24 -04002422 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002423}
2424
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002425static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002426 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002427 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002428 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002429 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002430};
2431
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432static int cgroup_file_open(struct inode *inode, struct file *file)
2433{
Tejun Heof7d58812013-08-08 20:11:23 -04002434 struct cfent *cfe = __d_cfe(file->f_dentry);
2435 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002436 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2437 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002438 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002439
2440 err = generic_file_open(inode, file);
2441 if (err)
2442 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002443
2444 /*
2445 * If the file belongs to a subsystem, pin the css. Will be
2446 * unpinned either on open failure or release. This ensures that
2447 * @css stays alive for all file operations.
2448 */
Tejun Heo105347b2013-08-13 11:01:55 -04002449 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002450 css = cgroup_css(cgrp, cft->ss);
2451 if (cft->ss && !css_tryget(css))
2452 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002453 rcu_read_unlock();
2454
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002455 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002456 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002457
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002458 /*
2459 * @cfe->css is used by read/write/close to determine the
2460 * associated css. @file->private_data would be a better place but
2461 * that's already used by seqfile. Multiple accessors may use it
2462 * simultaneously which is okay as the association never changes.
2463 */
2464 WARN_ON_ONCE(cfe->css && cfe->css != css);
2465 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002466
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002467 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002468 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002469 err = single_open(file, cgroup_seqfile_show, cfe);
2470 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002471 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002472 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473
Tejun Heo67f4c362013-08-08 20:11:24 -04002474 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002475 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002476 return err;
2477}
2478
2479static int cgroup_file_release(struct inode *inode, struct file *file)
2480{
Tejun Heof7d58812013-08-08 20:11:23 -04002481 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002482 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002483 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002484 int ret = 0;
2485
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002487 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002488 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002489 css_put(css);
2490 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002491}
2492
2493/*
2494 * cgroup_rename - Only allow simple rename of directories in place.
2495 */
2496static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2497 struct inode *new_dir, struct dentry *new_dentry)
2498{
Li Zefan65dff752013-03-01 15:01:56 +08002499 int ret;
2500 struct cgroup_name *name, *old_name;
2501 struct cgroup *cgrp;
2502
2503 /*
2504 * It's convinient to use parent dir's i_mutex to protected
2505 * cgrp->name.
2506 */
2507 lockdep_assert_held(&old_dir->i_mutex);
2508
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2510 return -ENOTDIR;
2511 if (new_dentry->d_inode)
2512 return -EEXIST;
2513 if (old_dir != new_dir)
2514 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002515
2516 cgrp = __d_cgrp(old_dentry);
2517
Tejun Heo6db8e852013-06-14 11:18:22 -07002518 /*
2519 * This isn't a proper migration and its usefulness is very
2520 * limited. Disallow if sane_behavior.
2521 */
2522 if (cgroup_sane_behavior(cgrp))
2523 return -EPERM;
2524
Li Zefan65dff752013-03-01 15:01:56 +08002525 name = cgroup_alloc_name(new_dentry);
2526 if (!name)
2527 return -ENOMEM;
2528
2529 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2530 if (ret) {
2531 kfree(name);
2532 return ret;
2533 }
2534
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002535 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002536 rcu_assign_pointer(cgrp->name, name);
2537
2538 kfree_rcu(old_name, rcu_head);
2539 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002540}
2541
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002542static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2543{
2544 if (S_ISDIR(dentry->d_inode->i_mode))
2545 return &__d_cgrp(dentry)->xattrs;
2546 else
Li Zefan712317a2013-04-18 23:09:52 -07002547 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002548}
2549
2550static inline int xattr_enabled(struct dentry *dentry)
2551{
2552 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002553 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002554}
2555
2556static bool is_valid_xattr(const char *name)
2557{
2558 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2559 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2560 return true;
2561 return false;
2562}
2563
2564static int cgroup_setxattr(struct dentry *dentry, const char *name,
2565 const void *val, size_t size, int flags)
2566{
2567 if (!xattr_enabled(dentry))
2568 return -EOPNOTSUPP;
2569 if (!is_valid_xattr(name))
2570 return -EINVAL;
2571 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2572}
2573
2574static int cgroup_removexattr(struct dentry *dentry, const char *name)
2575{
2576 if (!xattr_enabled(dentry))
2577 return -EOPNOTSUPP;
2578 if (!is_valid_xattr(name))
2579 return -EINVAL;
2580 return simple_xattr_remove(__d_xattrs(dentry), name);
2581}
2582
2583static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2584 void *buf, size_t size)
2585{
2586 if (!xattr_enabled(dentry))
2587 return -EOPNOTSUPP;
2588 if (!is_valid_xattr(name))
2589 return -EINVAL;
2590 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2591}
2592
2593static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2594{
2595 if (!xattr_enabled(dentry))
2596 return -EOPNOTSUPP;
2597 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2598}
2599
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002600static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002601 .read = cgroup_file_read,
2602 .write = cgroup_file_write,
2603 .llseek = generic_file_llseek,
2604 .open = cgroup_file_open,
2605 .release = cgroup_file_release,
2606};
2607
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002608static const struct inode_operations cgroup_file_inode_operations = {
2609 .setxattr = cgroup_setxattr,
2610 .getxattr = cgroup_getxattr,
2611 .listxattr = cgroup_listxattr,
2612 .removexattr = cgroup_removexattr,
2613};
2614
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002615static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002616 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002617 .mkdir = cgroup_mkdir,
2618 .rmdir = cgroup_rmdir,
2619 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002620 .setxattr = cgroup_setxattr,
2621 .getxattr = cgroup_getxattr,
2622 .listxattr = cgroup_listxattr,
2623 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624};
2625
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002626/*
2627 * Check if a file is a control file
2628 */
2629static inline struct cftype *__file_cft(struct file *file)
2630{
Al Viro496ad9a2013-01-23 17:07:38 -05002631 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002632 return ERR_PTR(-EINVAL);
2633 return __d_cft(file->f_dentry);
2634}
2635
Al Viroa5e7ed32011-07-26 01:55:55 -04002636static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002637 struct super_block *sb)
2638{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639 struct inode *inode;
2640
2641 if (!dentry)
2642 return -ENOENT;
2643 if (dentry->d_inode)
2644 return -EEXIST;
2645
2646 inode = cgroup_new_inode(mode, sb);
2647 if (!inode)
2648 return -ENOMEM;
2649
2650 if (S_ISDIR(mode)) {
2651 inode->i_op = &cgroup_dir_inode_operations;
2652 inode->i_fop = &simple_dir_operations;
2653
2654 /* start off with i_nlink == 2 (for "." entry) */
2655 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002656 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002657
Tejun Heob8a2df62012-11-19 08:13:37 -08002658 /*
2659 * Control reaches here with cgroup_mutex held.
2660 * @inode->i_mutex should nest outside cgroup_mutex but we
2661 * want to populate it immediately without releasing
2662 * cgroup_mutex. As @inode isn't visible to anyone else
2663 * yet, trylock will always succeed without affecting
2664 * lockdep checks.
2665 */
2666 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667 } else if (S_ISREG(mode)) {
2668 inode->i_size = 0;
2669 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002670 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672 d_instantiate(dentry, inode);
2673 dget(dentry); /* Extra count - pin the dentry in core */
2674 return 0;
2675}
2676
Li Zefan099fca32009-04-02 16:57:29 -07002677/**
2678 * cgroup_file_mode - deduce file mode of a control file
2679 * @cft: the control file in question
2680 *
2681 * returns cft->mode if ->mode is not 0
2682 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2683 * returns S_IRUGO if it has only a read handler
2684 * returns S_IWUSR if it has only a write hander
2685 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002686static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002687{
Al Viroa5e7ed32011-07-26 01:55:55 -04002688 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002689
2690 if (cft->mode)
2691 return cft->mode;
2692
2693 if (cft->read || cft->read_u64 || cft->read_s64 ||
2694 cft->read_map || cft->read_seq_string)
2695 mode |= S_IRUGO;
2696
2697 if (cft->write || cft->write_u64 || cft->write_s64 ||
2698 cft->write_string || cft->trigger)
2699 mode |= S_IWUSR;
2700
2701 return mode;
2702}
2703
Tejun Heo2bb566c2013-08-08 20:11:23 -04002704static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705{
Paul Menagebd89aab2007-10-18 23:40:44 -07002706 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002707 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002709 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002711 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002713
Tejun Heo9fa4db32013-08-26 18:40:56 -04002714 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2715 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002716 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 strcat(name, ".");
2718 }
2719 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002722
2723 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2724 if (!cfe)
2725 return -ENOMEM;
2726
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730 goto out;
2731 }
2732
Li Zefand6cbf352013-05-14 19:44:20 +08002733 cfe->type = (void *)cft;
2734 cfe->dentry = dentry;
2735 dentry->d_fsdata = cfe;
2736 simple_xattrs_init(&cfe->xattrs);
2737
Tejun Heo05ef1d72012-04-01 12:09:56 -07002738 mode = cgroup_file_mode(cft);
2739 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2740 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 list_add_tail(&cfe->node, &parent->files);
2742 cfe = NULL;
2743 }
2744 dput(dentry);
2745out:
2746 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 return error;
2748}
2749
Tejun Heob1f28d32013-06-28 16:24:10 -07002750/**
2751 * cgroup_addrm_files - add or remove files to a cgroup directory
2752 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002753 * @cfts: array of cftypes to be added
2754 * @is_add: whether to add or remove
2755 *
2756 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002757 * For removals, this function never fails. If addition fails, this
2758 * function doesn't remove files already added. The caller is responsible
2759 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002760 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002761static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2762 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002764 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002765 int ret;
2766
2767 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2768 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002769
2770 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002771 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002772 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2773 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002774 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2775 continue;
2776 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2777 continue;
2778
Li Zefan2739d3c2013-01-21 18:18:33 +08002779 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002780 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002781 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002782 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002783 cft->name, ret);
2784 return ret;
2785 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002786 } else {
2787 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002790 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002791}
2792
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002794 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002795{
2796 /*
2797 * Thanks to the entanglement with vfs inode locking, we can't walk
2798 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002799 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2800 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002801 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002802 mutex_lock(&cgroup_mutex);
2803}
2804
Tejun Heo2bb566c2013-08-08 20:11:23 -04002805static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002806 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807{
2808 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002809 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002810 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002811 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002812 struct dentry *prev = NULL;
2813 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002814 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002815 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002816 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002817
2818 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002819 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002820 !atomic_inc_not_zero(&sb->s_active)) {
2821 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002822 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823 }
2824
Li Zefane8c82d22013-06-18 18:48:37 +08002825 /*
2826 * All cgroups which are created after we drop cgroup_mutex will
2827 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002828 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002829 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002830 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002831
Tejun Heo8e3f6542012-04-01 12:09:55 -07002832 mutex_unlock(&cgroup_mutex);
2833
Li Zefane8c82d22013-06-18 18:48:37 +08002834 /* add/rm files for all cgroups created before */
2835 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002836 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002837 struct cgroup *cgrp = css->cgroup;
2838
Li Zefane8c82d22013-06-18 18:48:37 +08002839 if (cgroup_is_dead(cgrp))
2840 continue;
2841
2842 inode = cgrp->dentry->d_inode;
2843 dget(cgrp->dentry);
2844 rcu_read_unlock();
2845
2846 dput(prev);
2847 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848
2849 mutex_lock(&inode->i_mutex);
2850 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002851 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002852 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853 mutex_unlock(&cgroup_mutex);
2854 mutex_unlock(&inode->i_mutex);
2855
Li Zefane8c82d22013-06-18 18:48:37 +08002856 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002857 if (ret)
2858 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859 }
Li Zefane8c82d22013-06-18 18:48:37 +08002860 rcu_read_unlock();
2861 dput(prev);
2862 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002863 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002864}
2865
2866/**
2867 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2868 * @ss: target cgroup subsystem
2869 * @cfts: zero-length name terminated array of cftypes
2870 *
2871 * Register @cfts to @ss. Files described by @cfts are created for all
2872 * existing cgroups to which @ss is attached and all future cgroups will
2873 * have them too. This function can be called anytime whether @ss is
2874 * attached or not.
2875 *
2876 * Returns 0 on successful registration, -errno on failure. Note that this
2877 * function currently returns 0 as long as @cfts registration is successful
2878 * even if some file creation attempts on existing cgroups fail.
2879 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002880int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881{
2882 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002883 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002884 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885
2886 set = kzalloc(sizeof(*set), GFP_KERNEL);
2887 if (!set)
2888 return -ENOMEM;
2889
Tejun Heo2bb566c2013-08-08 20:11:23 -04002890 for (cft = cfts; cft->name[0] != '\0'; cft++)
2891 cft->ss = ss;
2892
Tejun Heo8e3f6542012-04-01 12:09:55 -07002893 cgroup_cfts_prepare();
2894 set->cfts = cfts;
2895 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002896 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002897 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002898 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002899 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002900}
2901EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2902
Li Zefana043e3b2008-02-23 15:24:09 -08002903/**
Tejun Heo79578622012-04-01 12:09:56 -07002904 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002905 * @cfts: zero-length name terminated array of cftypes
2906 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002907 * Unregister @cfts. Files described by @cfts are removed from all
2908 * existing cgroups and all future cgroups won't have them either. This
2909 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002910 *
2911 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002912 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002913 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002914int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002915{
2916 struct cftype_set *set;
2917
Tejun Heo2bb566c2013-08-08 20:11:23 -04002918 if (!cfts || !cfts[0].ss)
2919 return -ENOENT;
2920
Tejun Heo79578622012-04-01 12:09:56 -07002921 cgroup_cfts_prepare();
2922
Tejun Heo2bb566c2013-08-08 20:11:23 -04002923 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002924 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002925 list_del(&set->node);
2926 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002927 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002928 return 0;
2929 }
2930 }
2931
Tejun Heo2bb566c2013-08-08 20:11:23 -04002932 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002933 return -ENOENT;
2934}
2935
2936/**
Li Zefana043e3b2008-02-23 15:24:09 -08002937 * cgroup_task_count - count the number of tasks in a cgroup.
2938 * @cgrp: the cgroup in question
2939 *
2940 * Return the number of tasks in the cgroup.
2941 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002942int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002943{
2944 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002945 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002946
Paul Menage817929e2007-10-18 23:39:36 -07002947 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002948 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2949 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002950 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002951 return count;
2952}
2953
2954/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002955 * To reduce the fork() overhead for systems that are not actually using
2956 * their cgroups capability, we don't maintain the lists running through
2957 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002958 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002959 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002960static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002961{
2962 struct task_struct *p, *g;
2963 write_lock(&css_set_lock);
2964 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002965 /*
2966 * We need tasklist_lock because RCU is not safe against
2967 * while_each_thread(). Besides, a forking task that has passed
2968 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2969 * is not guaranteed to have its child immediately visible in the
2970 * tasklist if we walk through it with RCU.
2971 */
2972 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002973 do_each_thread(g, p) {
2974 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002975 /*
2976 * We should check if the process is exiting, otherwise
2977 * it will race with cgroup_exit() in that the list
2978 * entry won't be deleted though the process has exited.
2979 */
2980 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002981 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002982 task_unlock(p);
2983 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002984 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002985 write_unlock(&css_set_lock);
2986}
2987
Tejun Heo574bd9f2012-11-09 09:12:29 -08002988/**
Tejun Heo492eb212013-08-08 20:11:25 -04002989 * css_next_child - find the next child of a given css
2990 * @pos_css: the current position (%NULL to initiate traversal)
2991 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002992 *
Tejun Heo492eb212013-08-08 20:11:25 -04002993 * This function returns the next child of @parent_css and should be called
2994 * under RCU read lock. The only requirement is that @parent_css and
2995 * @pos_css are accessible. The next sibling is guaranteed to be returned
2996 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002997 */
Tejun Heo492eb212013-08-08 20:11:25 -04002998struct cgroup_subsys_state *
2999css_next_child(struct cgroup_subsys_state *pos_css,
3000 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003001{
Tejun Heo492eb212013-08-08 20:11:25 -04003002 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3003 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003004 struct cgroup *next;
3005
3006 WARN_ON_ONCE(!rcu_read_lock_held());
3007
3008 /*
3009 * @pos could already have been removed. Once a cgroup is removed,
3010 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003011 * changes. As CGRP_DEAD assertion is serialized and happens
3012 * before the cgroup is taken off the ->sibling list, if we see it
3013 * unasserted, it's guaranteed that the next sibling hasn't
3014 * finished its grace period even if it's already removed, and thus
3015 * safe to dereference from this RCU critical section. If
3016 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3017 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003018 *
3019 * If @pos is dead, its next pointer can't be dereferenced;
3020 * however, as each cgroup is given a monotonically increasing
3021 * unique serial number and always appended to the sibling list,
3022 * the next one can be found by walking the parent's children until
3023 * we see a cgroup with higher serial number than @pos's. While
3024 * this path can be slower, it's taken only when either the current
3025 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003026 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003027 if (!pos) {
3028 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3029 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003030 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003031 } else {
3032 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3033 if (next->serial_nr > pos->serial_nr)
3034 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003035 }
3036
Tejun Heo492eb212013-08-08 20:11:25 -04003037 if (&next->sibling == &cgrp->children)
3038 return NULL;
3039
Tejun Heoca8bdca2013-08-26 18:40:56 -04003040 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003041}
Tejun Heo492eb212013-08-08 20:11:25 -04003042EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003043
3044/**
Tejun Heo492eb212013-08-08 20:11:25 -04003045 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003047 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003048 *
Tejun Heo492eb212013-08-08 20:11:25 -04003049 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003050 * to visit for pre-order traversal of @root's descendants. @root is
3051 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003052 *
3053 * While this function requires RCU read locking, it doesn't require the
3054 * whole traversal to be contained in a single RCU critical section. This
3055 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003056 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057 */
Tejun Heo492eb212013-08-08 20:11:25 -04003058struct cgroup_subsys_state *
3059css_next_descendant_pre(struct cgroup_subsys_state *pos,
3060 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003061{
Tejun Heo492eb212013-08-08 20:11:25 -04003062 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063
3064 WARN_ON_ONCE(!rcu_read_lock_held());
3065
Tejun Heobd8815a2013-08-08 20:11:27 -04003066 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003067 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003068 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003069
3070 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003071 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003072 if (next)
3073 return next;
3074
3075 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003076 while (pos != root) {
3077 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003078 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003079 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003080 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003081 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082
3083 return NULL;
3084}
Tejun Heo492eb212013-08-08 20:11:25 -04003085EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003087/**
Tejun Heo492eb212013-08-08 20:11:25 -04003088 * css_rightmost_descendant - return the rightmost descendant of a css
3089 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003090 *
Tejun Heo492eb212013-08-08 20:11:25 -04003091 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3092 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003093 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003094 *
3095 * While this function requires RCU read locking, it doesn't require the
3096 * whole traversal to be contained in a single RCU critical section. This
3097 * function will return the correct rightmost descendant as long as @pos is
3098 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003099 */
Tejun Heo492eb212013-08-08 20:11:25 -04003100struct cgroup_subsys_state *
3101css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003102{
Tejun Heo492eb212013-08-08 20:11:25 -04003103 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003104
3105 WARN_ON_ONCE(!rcu_read_lock_held());
3106
3107 do {
3108 last = pos;
3109 /* ->prev isn't RCU safe, walk ->next till the end */
3110 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003111 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003112 pos = tmp;
3113 } while (pos);
3114
3115 return last;
3116}
Tejun Heo492eb212013-08-08 20:11:25 -04003117EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003118
Tejun Heo492eb212013-08-08 20:11:25 -04003119static struct cgroup_subsys_state *
3120css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003121{
Tejun Heo492eb212013-08-08 20:11:25 -04003122 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123
3124 do {
3125 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003126 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003127 } while (pos);
3128
3129 return last;
3130}
3131
3132/**
Tejun Heo492eb212013-08-08 20:11:25 -04003133 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003134 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003135 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003136 *
Tejun Heo492eb212013-08-08 20:11:25 -04003137 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003138 * to visit for post-order traversal of @root's descendants. @root is
3139 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003140 *
3141 * While this function requires RCU read locking, it doesn't require the
3142 * whole traversal to be contained in a single RCU critical section. This
3143 * function will return the correct next descendant as long as both @pos
3144 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003145 */
Tejun Heo492eb212013-08-08 20:11:25 -04003146struct cgroup_subsys_state *
3147css_next_descendant_post(struct cgroup_subsys_state *pos,
3148 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003149{
Tejun Heo492eb212013-08-08 20:11:25 -04003150 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003151
3152 WARN_ON_ONCE(!rcu_read_lock_held());
3153
3154 /* if first iteration, visit the leftmost descendant */
3155 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003156 next = css_leftmost_descendant(root);
3157 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158 }
3159
Tejun Heobd8815a2013-08-08 20:11:27 -04003160 /* if we visited @root, we're done */
3161 if (pos == root)
3162 return NULL;
3163
Tejun Heo574bd9f2012-11-09 09:12:29 -08003164 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003165 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003166 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003167 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168
3169 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003170 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003171}
Tejun Heo492eb212013-08-08 20:11:25 -04003172EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003173
Tejun Heo0942eee2013-08-08 20:11:26 -04003174/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003175 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003176 * @it: the iterator to advance
3177 *
3178 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003179 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003180static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003181{
3182 struct list_head *l = it->cset_link;
3183 struct cgrp_cset_link *link;
3184 struct css_set *cset;
3185
3186 /* Advance to the next non-empty css_set */
3187 do {
3188 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003189 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003190 it->cset_link = NULL;
3191 return;
3192 }
3193 link = list_entry(l, struct cgrp_cset_link, cset_link);
3194 cset = link->cset;
3195 } while (list_empty(&cset->tasks));
3196 it->cset_link = l;
3197 it->task = cset->tasks.next;
3198}
3199
Tejun Heo0942eee2013-08-08 20:11:26 -04003200/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003201 * css_task_iter_start - initiate task iteration
3202 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003203 * @it: the task iterator to use
3204 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003205 * Initiate iteration through the tasks of @css. The caller can call
3206 * css_task_iter_next() to walk through the tasks until the function
3207 * returns NULL. On completion of iteration, css_task_iter_end() must be
3208 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003209 *
3210 * Note that this function acquires a lock which is released when the
3211 * iteration finishes. The caller can't sleep while iteration is in
3212 * progress.
3213 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003214void css_task_iter_start(struct cgroup_subsys_state *css,
3215 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003216 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003217{
3218 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003219 * The first time anyone tries to iterate across a css, we need to
3220 * enable the list linking each css_set to its tasks, and fix up
3221 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003222 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003223 if (!use_task_css_set_links)
3224 cgroup_enable_task_cg_lists();
3225
Paul Menage817929e2007-10-18 23:39:36 -07003226 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003227
Tejun Heo72ec7022013-08-08 20:11:26 -04003228 it->origin_css = css;
3229 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003230
Tejun Heo72ec7022013-08-08 20:11:26 -04003231 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003232}
3233
Tejun Heo0942eee2013-08-08 20:11:26 -04003234/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003235 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003236 * @it: the task iterator being iterated
3237 *
3238 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003239 * initialized via css_task_iter_start(). Returns NULL when the iteration
3240 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003241 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003242struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003243{
3244 struct task_struct *res;
3245 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003246 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003247
3248 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003249 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003250 return NULL;
3251 res = list_entry(l, struct task_struct, cg_list);
3252 /* Advance iterator to find next entry */
3253 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003254 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3255 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003256 /*
3257 * We reached the end of this task list - move on to the
3258 * next cgrp_cset_link.
3259 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003260 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003261 } else {
3262 it->task = l;
3263 }
3264 return res;
3265}
3266
Tejun Heo0942eee2013-08-08 20:11:26 -04003267/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003268 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003269 * @it: the task iterator to finish
3270 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003271 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003272 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003273void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003274 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003275{
3276 read_unlock(&css_set_lock);
3277}
3278
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279static inline int started_after_time(struct task_struct *t1,
3280 struct timespec *time,
3281 struct task_struct *t2)
3282{
3283 int start_diff = timespec_compare(&t1->start_time, time);
3284 if (start_diff > 0) {
3285 return 1;
3286 } else if (start_diff < 0) {
3287 return 0;
3288 } else {
3289 /*
3290 * Arbitrarily, if two processes started at the same
3291 * time, we'll say that the lower pointer value
3292 * started first. Note that t2 may have exited by now
3293 * so this may not be a valid pointer any longer, but
3294 * that's fine - it still serves to distinguish
3295 * between two tasks started (effectively) simultaneously.
3296 */
3297 return t1 > t2;
3298 }
3299}
3300
3301/*
3302 * This function is a callback from heap_insert() and is used to order
3303 * the heap.
3304 * In this case we order the heap in descending task start time.
3305 */
3306static inline int started_after(void *p1, void *p2)
3307{
3308 struct task_struct *t1 = p1;
3309 struct task_struct *t2 = p2;
3310 return started_after_time(t1, &t2->start_time, t2);
3311}
3312
3313/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003314 * css_scan_tasks - iterate though all the tasks in a css
3315 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003316 * @test: optional test callback
3317 * @process: process callback
3318 * @data: data passed to @test and @process
3319 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003320 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003321 * Iterate through all the tasks in @css, calling @test for each, and if it
3322 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003323 *
Tejun Heoe5358372013-08-08 20:11:26 -04003324 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003325 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003326 * lock css_set_lock for the call to @process.
3327 *
3328 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003329 * of @css for the duration of this call. This function may or may not
3330 * call @process for tasks that exit or move to a different css during the
3331 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003332 *
3333 * Note that @test may be called with locks held, and may in some
3334 * situations be called multiple times for the same task, so it should be
3335 * cheap.
3336 *
3337 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3338 * heap operations (and its "gt" member will be overwritten), else a
3339 * temporary heap will be used (allocation of which may cause this function
3340 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003342int css_scan_tasks(struct cgroup_subsys_state *css,
3343 bool (*test)(struct task_struct *, void *),
3344 void (*process)(struct task_struct *, void *),
3345 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003346{
3347 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003348 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003349 struct task_struct *p, *dropped;
3350 /* Never dereference latest_task, since it's not refcounted */
3351 struct task_struct *latest_task = NULL;
3352 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003353 struct timespec latest_time = { 0, 0 };
3354
Tejun Heoe5358372013-08-08 20:11:26 -04003355 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003356 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 heap->gt = &started_after;
3358 } else {
3359 /* We need to allocate our own heap memory */
3360 heap = &tmp_heap;
3361 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3362 if (retval)
3363 /* cannot allocate the heap */
3364 return retval;
3365 }
3366
3367 again:
3368 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003369 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003370 * which are of interest, and invoking @process callback on the
3371 * ones which need an update. Since we don't want to hold any
3372 * locks during the task updates, gather tasks to be processed in a
3373 * heap structure. The heap is sorted by descending task start
3374 * time. If the statically-sized heap fills up, we overflow tasks
3375 * that started later, and in future iterations only consider tasks
3376 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003377 * guarantees forward progress and that we don't miss any tasks.
3378 */
3379 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003380 css_task_iter_start(css, &it);
3381 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003382 /*
3383 * Only affect tasks that qualify per the caller's callback,
3384 * if he provided one
3385 */
Tejun Heoe5358372013-08-08 20:11:26 -04003386 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003387 continue;
3388 /*
3389 * Only process tasks that started after the last task
3390 * we processed
3391 */
3392 if (!started_after_time(p, &latest_time, latest_task))
3393 continue;
3394 dropped = heap_insert(heap, p);
3395 if (dropped == NULL) {
3396 /*
3397 * The new task was inserted; the heap wasn't
3398 * previously full
3399 */
3400 get_task_struct(p);
3401 } else if (dropped != p) {
3402 /*
3403 * The new task was inserted, and pushed out a
3404 * different task
3405 */
3406 get_task_struct(p);
3407 put_task_struct(dropped);
3408 }
3409 /*
3410 * Else the new task was newer than anything already in
3411 * the heap and wasn't inserted
3412 */
3413 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003414 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003415
3416 if (heap->size) {
3417 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003418 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003420 latest_time = q->start_time;
3421 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003422 }
3423 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003424 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003425 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003426 }
3427 /*
3428 * If we had to process any tasks at all, scan again
3429 * in case some of them were in the middle of forking
3430 * children that didn't get processed.
3431 * Not the most efficient way to do it, but it avoids
3432 * having to take callback_mutex in the fork path
3433 */
3434 goto again;
3435 }
3436 if (heap == &tmp_heap)
3437 heap_free(&tmp_heap);
3438 return 0;
3439}
3440
Tejun Heoe5358372013-08-08 20:11:26 -04003441static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003442{
Tejun Heoe5358372013-08-08 20:11:26 -04003443 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003444
Tejun Heo47cfcd02013-04-07 09:29:51 -07003445 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003446 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003447 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003448}
3449
3450/**
3451 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3452 * @to: cgroup to which the tasks will be moved
3453 * @from: cgroup in which the tasks currently reside
3454 */
3455int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3456{
Tejun Heo72ec7022013-08-08 20:11:26 -04003457 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3458 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003459}
3460
Paul Menage817929e2007-10-18 23:39:36 -07003461/*
Ben Blum102a7752009-09-23 15:56:26 -07003462 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003463 *
3464 * Reading this file can return large amounts of data if a cgroup has
3465 * *lots* of attached tasks. So it may need several calls to read(),
3466 * but we cannot guarantee that the information we produce is correct
3467 * unless we produce it entirely atomically.
3468 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003469 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003470
Li Zefan24528252012-01-20 11:58:43 +08003471/* which pidlist file are we talking about? */
3472enum cgroup_filetype {
3473 CGROUP_FILE_PROCS,
3474 CGROUP_FILE_TASKS,
3475};
3476
3477/*
3478 * A pidlist is a list of pids that virtually represents the contents of one
3479 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3480 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3481 * to the cgroup.
3482 */
3483struct cgroup_pidlist {
3484 /*
3485 * used to find which pidlist is wanted. doesn't change as long as
3486 * this particular list stays in the list.
3487 */
3488 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3489 /* array of xids */
3490 pid_t *list;
3491 /* how many elements the above list has */
3492 int length;
3493 /* how many files are using the current array */
3494 int use_count;
3495 /* each of these stored in a list by its cgroup */
3496 struct list_head links;
3497 /* pointer to the cgroup we belong to, for list removal purposes */
3498 struct cgroup *owner;
3499 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003500 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003501};
3502
Paul Menagebbcb81d2007-10-18 23:39:32 -07003503/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003504 * The following two functions "fix" the issue where there are more pids
3505 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3506 * TODO: replace with a kernel-wide solution to this problem
3507 */
3508#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3509static void *pidlist_allocate(int count)
3510{
3511 if (PIDLIST_TOO_LARGE(count))
3512 return vmalloc(count * sizeof(pid_t));
3513 else
3514 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3515}
3516static void pidlist_free(void *p)
3517{
3518 if (is_vmalloc_addr(p))
3519 vfree(p);
3520 else
3521 kfree(p);
3522}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003523
3524/*
Ben Blum102a7752009-09-23 15:56:26 -07003525 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003526 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003527 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003528static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003529{
Ben Blum102a7752009-09-23 15:56:26 -07003530 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003531
3532 /*
3533 * we presume the 0th element is unique, so i starts at 1. trivial
3534 * edge cases first; no work needs to be done for either
3535 */
3536 if (length == 0 || length == 1)
3537 return length;
3538 /* src and dest walk down the list; dest counts unique elements */
3539 for (src = 1; src < length; src++) {
3540 /* find next unique element */
3541 while (list[src] == list[src-1]) {
3542 src++;
3543 if (src == length)
3544 goto after;
3545 }
3546 /* dest always points to where the next unique element goes */
3547 list[dest] = list[src];
3548 dest++;
3549 }
3550after:
Ben Blum102a7752009-09-23 15:56:26 -07003551 return dest;
3552}
3553
3554static int cmppid(const void *a, const void *b)
3555{
3556 return *(pid_t *)a - *(pid_t *)b;
3557}
3558
3559/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003560 * find the appropriate pidlist for our purpose (given procs vs tasks)
3561 * returns with the lock on that pidlist already held, and takes care
3562 * of the use count, or returns NULL with no locks held if we're out of
3563 * memory.
3564 */
3565static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3566 enum cgroup_filetype type)
3567{
3568 struct cgroup_pidlist *l;
3569 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003570 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003571
Ben Blum72a8cb32009-09-23 15:56:27 -07003572 /*
Li Zefanb3958902013-08-01 09:52:15 +08003573 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003574 * the last ref-holder is trying to remove l from the list at the same
3575 * time. Holding the pidlist_mutex precludes somebody taking whichever
3576 * list we find out from under us - compare release_pid_array().
3577 */
3578 mutex_lock(&cgrp->pidlist_mutex);
3579 list_for_each_entry(l, &cgrp->pidlists, links) {
3580 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003581 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003582 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003583 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 return l;
3585 }
3586 }
3587 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003588 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003589 if (!l) {
3590 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 return l;
3592 }
Li Zefanb3958902013-08-01 09:52:15 +08003593 init_rwsem(&l->rwsem);
3594 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003596 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 l->owner = cgrp;
3598 list_add(&l->links, &cgrp->pidlists);
3599 mutex_unlock(&cgrp->pidlist_mutex);
3600 return l;
3601}
3602
3603/*
Ben Blum102a7752009-09-23 15:56:26 -07003604 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3605 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003606static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3607 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003608{
3609 pid_t *array;
3610 int length;
3611 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003612 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003613 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003614 struct cgroup_pidlist *l;
3615
3616 /*
3617 * If cgroup gets more users after we read count, we won't have
3618 * enough space - tough. This race is indistinguishable to the
3619 * caller from the case that the additional cgroup users didn't
3620 * show up until sometime later on.
3621 */
3622 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003623 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003624 if (!array)
3625 return -ENOMEM;
3626 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003627 css_task_iter_start(&cgrp->dummy_css, &it);
3628 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003629 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003630 break;
Ben Blum102a7752009-09-23 15:56:26 -07003631 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003632 if (type == CGROUP_FILE_PROCS)
3633 pid = task_tgid_vnr(tsk);
3634 else
3635 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003636 if (pid > 0) /* make sure to only use valid results */
3637 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003638 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003639 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003640 length = n;
3641 /* now sort & (if procs) strip out duplicates */
3642 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003644 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003645 l = cgroup_pidlist_find(cgrp, type);
3646 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003647 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003648 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003649 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003650 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003651 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003652 l->list = array;
3653 l->length = length;
3654 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003655 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003656 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003657 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003658}
3659
Balbir Singh846c7bb2007-10-18 23:39:44 -07003660/**
Li Zefana043e3b2008-02-23 15:24:09 -08003661 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003662 * @stats: cgroupstats to fill information into
3663 * @dentry: A dentry entry belonging to the cgroup for which stats have
3664 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003665 *
3666 * Build and fill cgroupstats so that taskstats can export it to user
3667 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003668 */
3669int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3670{
3671 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003672 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003673 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003674 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003675
Balbir Singh846c7bb2007-10-18 23:39:44 -07003676 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003677 * Validate dentry by checking the superblock operations,
3678 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003679 */
Li Zefan33d283b2008-11-19 15:36:48 -08003680 if (dentry->d_sb->s_op != &cgroup_ops ||
3681 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682 goto err;
3683
3684 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003685 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003686
Tejun Heo72ec7022013-08-08 20:11:26 -04003687 css_task_iter_start(&cgrp->dummy_css, &it);
3688 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003689 switch (tsk->state) {
3690 case TASK_RUNNING:
3691 stats->nr_running++;
3692 break;
3693 case TASK_INTERRUPTIBLE:
3694 stats->nr_sleeping++;
3695 break;
3696 case TASK_UNINTERRUPTIBLE:
3697 stats->nr_uninterruptible++;
3698 break;
3699 case TASK_STOPPED:
3700 stats->nr_stopped++;
3701 break;
3702 default:
3703 if (delayacct_is_task_waiting_on_io(tsk))
3704 stats->nr_io_wait++;
3705 break;
3706 }
3707 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003708 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003709
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710err:
3711 return ret;
3712}
3713
Paul Menage8f3ff202009-09-23 15:56:25 -07003714
Paul Menagecc31edc2008-10-18 20:28:04 -07003715/*
Ben Blum102a7752009-09-23 15:56:26 -07003716 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003717 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003718 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003719 */
3720
Ben Blum102a7752009-09-23 15:56:26 -07003721static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003722{
3723 /*
3724 * Initially we receive a position value that corresponds to
3725 * one more than the last pid shown (or 0 on the first call or
3726 * after a seek to the start). Use a binary-search to find the
3727 * next pid to display, if any
3728 */
Ben Blum102a7752009-09-23 15:56:26 -07003729 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003730 int index = 0, pid = *pos;
3731 int *iter;
3732
Li Zefanb3958902013-08-01 09:52:15 +08003733 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003734 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003735 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003736
Paul Menagecc31edc2008-10-18 20:28:04 -07003737 while (index < end) {
3738 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003739 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003740 index = mid;
3741 break;
Ben Blum102a7752009-09-23 15:56:26 -07003742 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003743 index = mid + 1;
3744 else
3745 end = mid;
3746 }
3747 }
3748 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003749 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003750 return NULL;
3751 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003752 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 *pos = *iter;
3754 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003755}
3756
Ben Blum102a7752009-09-23 15:56:26 -07003757static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003758{
Ben Blum102a7752009-09-23 15:56:26 -07003759 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003760 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003761}
3762
Ben Blum102a7752009-09-23 15:56:26 -07003763static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003764{
Ben Blum102a7752009-09-23 15:56:26 -07003765 struct cgroup_pidlist *l = s->private;
3766 pid_t *p = v;
3767 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003768 /*
3769 * Advance to the next pid in the array. If this goes off the
3770 * end, we're done
3771 */
3772 p++;
3773 if (p >= end) {
3774 return NULL;
3775 } else {
3776 *pos = *p;
3777 return p;
3778 }
3779}
3780
Ben Blum102a7752009-09-23 15:56:26 -07003781static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003782{
3783 return seq_printf(s, "%d\n", *(int *)v);
3784}
3785
Ben Blum102a7752009-09-23 15:56:26 -07003786/*
3787 * seq_operations functions for iterating on pidlists through seq_file -
3788 * independent of whether it's tasks or procs
3789 */
3790static const struct seq_operations cgroup_pidlist_seq_operations = {
3791 .start = cgroup_pidlist_start,
3792 .stop = cgroup_pidlist_stop,
3793 .next = cgroup_pidlist_next,
3794 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003795};
3796
Ben Blum102a7752009-09-23 15:56:26 -07003797static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003798{
Ben Blum72a8cb32009-09-23 15:56:27 -07003799 /*
3800 * the case where we're the last user of this particular pidlist will
3801 * have us remove it from the cgroup's list, which entails taking the
3802 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3803 * pidlist_mutex, we have to take pidlist_mutex first.
3804 */
3805 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003806 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003807 BUG_ON(!l->use_count);
3808 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003809 /* we're the last user if refcount is 0; remove and free */
3810 list_del(&l->links);
3811 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003812 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003813 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003814 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003815 kfree(l);
3816 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003817 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003818 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003819 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003820}
3821
Ben Blum102a7752009-09-23 15:56:26 -07003822static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003823{
Ben Blum102a7752009-09-23 15:56:26 -07003824 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003825 if (!(file->f_mode & FMODE_READ))
3826 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003827 /*
3828 * the seq_file will only be initialized if the file was opened for
3829 * reading; hence we check if it's not null only in that case.
3830 */
3831 l = ((struct seq_file *)file->private_data)->private;
3832 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003833 return seq_release(inode, file);
3834}
3835
Ben Blum102a7752009-09-23 15:56:26 -07003836static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003837 .read = seq_read,
3838 .llseek = seq_lseek,
3839 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003840 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003841};
3842
3843/*
Ben Blum102a7752009-09-23 15:56:26 -07003844 * The following functions handle opens on a file that displays a pidlist
3845 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3846 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003847 */
Ben Blum102a7752009-09-23 15:56:26 -07003848/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003849static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003850{
3851 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003852 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003853 int retval;
3854
3855 /* Nothing to do for write-only files */
3856 if (!(file->f_mode & FMODE_READ))
3857 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003858
Ben Blum102a7752009-09-23 15:56:26 -07003859 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003860 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003861 if (retval)
3862 return retval;
3863 /* configure file information */
3864 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003865
Ben Blum102a7752009-09-23 15:56:26 -07003866 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003867 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003868 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003869 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003870 }
Ben Blum102a7752009-09-23 15:56:26 -07003871 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003872 return 0;
3873}
Ben Blum102a7752009-09-23 15:56:26 -07003874static int cgroup_tasks_open(struct inode *unused, struct file *file)
3875{
Ben Blum72a8cb32009-09-23 15:56:27 -07003876 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003877}
3878static int cgroup_procs_open(struct inode *unused, struct file *file)
3879{
Ben Blum72a8cb32009-09-23 15:56:27 -07003880 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003881}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003882
Tejun Heo182446d2013-08-08 20:11:24 -04003883static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3884 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003885{
Tejun Heo182446d2013-08-08 20:11:24 -04003886 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003887}
3888
Tejun Heo182446d2013-08-08 20:11:24 -04003889static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3890 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003891{
Tejun Heo182446d2013-08-08 20:11:24 -04003892 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003893 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003894 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003895 else
Tejun Heo182446d2013-08-08 20:11:24 -04003896 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003897 return 0;
3898}
3899
Paul Menagebbcb81d2007-10-18 23:39:32 -07003900/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003901 * When dput() is called asynchronously, if umount has been done and
3902 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3903 * there's a small window that vfs will see the root dentry with non-zero
3904 * refcnt and trigger BUG().
3905 *
3906 * That's why we hold a reference before dput() and drop it right after.
3907 */
3908static void cgroup_dput(struct cgroup *cgrp)
3909{
3910 struct super_block *sb = cgrp->root->sb;
3911
3912 atomic_inc(&sb->s_active);
3913 dput(cgrp->dentry);
3914 deactivate_super(sb);
3915}
3916
3917/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003918 * Unregister event and free resources.
3919 *
3920 * Gets called from workqueue.
3921 */
3922static void cgroup_event_remove(struct work_struct *work)
3923{
3924 struct cgroup_event *event = container_of(work, struct cgroup_event,
3925 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003926 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003927
Li Zefan810cbee2013-02-18 18:56:14 +08003928 remove_wait_queue(event->wqh, &event->wait);
3929
Tejun Heo81eeaf02013-08-08 20:11:26 -04003930 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003931
Li Zefan810cbee2013-02-18 18:56:14 +08003932 /* Notify userspace the event is going away. */
3933 eventfd_signal(event->eventfd, 1);
3934
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003935 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003936 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003937 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003938}
3939
3940/*
3941 * Gets called on POLLHUP on eventfd when user closes it.
3942 *
3943 * Called with wqh->lock held and interrupts disabled.
3944 */
3945static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3946 int sync, void *key)
3947{
3948 struct cgroup_event *event = container_of(wait,
3949 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003950 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003951 unsigned long flags = (unsigned long)key;
3952
3953 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003954 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003955 * If the event has been detached at cgroup removal, we
3956 * can simply return knowing the other side will cleanup
3957 * for us.
3958 *
3959 * We can't race against event freeing since the other
3960 * side will require wqh->lock via remove_wait_queue(),
3961 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003962 */
Li Zefan810cbee2013-02-18 18:56:14 +08003963 spin_lock(&cgrp->event_list_lock);
3964 if (!list_empty(&event->list)) {
3965 list_del_init(&event->list);
3966 /*
3967 * We are in atomic context, but cgroup_event_remove()
3968 * may sleep, so we have to call it in workqueue.
3969 */
3970 schedule_work(&event->remove);
3971 }
3972 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003973 }
3974
3975 return 0;
3976}
3977
3978static void cgroup_event_ptable_queue_proc(struct file *file,
3979 wait_queue_head_t *wqh, poll_table *pt)
3980{
3981 struct cgroup_event *event = container_of(pt,
3982 struct cgroup_event, pt);
3983
3984 event->wqh = wqh;
3985 add_wait_queue(wqh, &event->wait);
3986}
3987
3988/*
3989 * Parse input and register new cgroup event handler.
3990 *
3991 * Input must be in format '<event_fd> <control_fd> <args>'.
3992 * Interpretation of args is defined by control file implementation.
3993 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04003994static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04003995 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003996{
Tejun Heo6e6eab02013-08-15 11:43:15 -04003997 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08003998 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04003999 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004000 unsigned int efd, cfd;
Al Viro4e10f3c2013-08-30 12:29:49 -04004001 struct fd efile;
4002 struct fd cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004003 char *endp;
4004 int ret;
4005
4006 efd = simple_strtoul(buffer, &endp, 10);
4007 if (*endp != ' ')
4008 return -EINVAL;
4009 buffer = endp + 1;
4010
4011 cfd = simple_strtoul(buffer, &endp, 10);
4012 if ((*endp != ' ') && (*endp != '\0'))
4013 return -EINVAL;
4014 buffer = endp + 1;
4015
4016 event = kzalloc(sizeof(*event), GFP_KERNEL);
4017 if (!event)
4018 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004019
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004020 INIT_LIST_HEAD(&event->list);
4021 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4022 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4023 INIT_WORK(&event->remove, cgroup_event_remove);
4024
Al Viro4e10f3c2013-08-30 12:29:49 -04004025 efile = fdget(efd);
4026 if (!efile.file) {
4027 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004028 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004029 }
4030
Al Viro4e10f3c2013-08-30 12:29:49 -04004031 event->eventfd = eventfd_ctx_fileget(efile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004032 if (IS_ERR(event->eventfd)) {
4033 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004034 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004035 }
4036
Al Viro4e10f3c2013-08-30 12:29:49 -04004037 cfile = fdget(cfd);
4038 if (!cfile.file) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004039 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004040 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004041 }
4042
4043 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004044 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro4e10f3c2013-08-30 12:29:49 -04004045 ret = inode_permission(file_inode(cfile.file), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004046 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004047 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004048
Al Viro4e10f3c2013-08-30 12:29:49 -04004049 event->cft = __file_cft(cfile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004050 if (IS_ERR(event->cft)) {
4051 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004052 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004053 }
4054
Tejun Heo6e6eab02013-08-15 11:43:15 -04004055 if (!event->cft->ss) {
4056 ret = -EBADF;
4057 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004058 }
4059
Li Zefanf1690072013-02-18 14:13:35 +08004060 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004061 * Determine the css of @cfile, verify it belongs to the same
4062 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004063 * Remaining events are automatically removed on cgroup destruction
4064 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004065 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004066 rcu_read_lock();
4067
4068 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004069 event->css = cgroup_css(cgrp, event->cft->ss);
Al Viro4e10f3c2013-08-30 12:29:49 -04004070 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004071 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004072 ret = 0;
4073
4074 rcu_read_unlock();
4075 if (ret)
4076 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004077
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004078 if (!event->cft->register_event || !event->cft->unregister_event) {
4079 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004080 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004081 }
4082
Tejun Heo6e6eab02013-08-15 11:43:15 -04004083 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004084 event->eventfd, buffer);
4085 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004086 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004087
Al Viro4e10f3c2013-08-30 12:29:49 -04004088 efile.file->f_op->poll(efile.file, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004089
4090 spin_lock(&cgrp->event_list_lock);
4091 list_add(&event->list, &cgrp->event_list);
4092 spin_unlock(&cgrp->event_list_lock);
4093
Al Viro4e10f3c2013-08-30 12:29:49 -04004094 fdput(cfile);
4095 fdput(efile);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004096
4097 return 0;
4098
Tejun Heo7941cb02013-08-26 18:40:56 -04004099out_put_css:
4100 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004101out_put_cfile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004102 fdput(cfile);
Li Zefan876ede82013-08-01 09:51:47 +08004103out_put_eventfd:
4104 eventfd_ctx_put(event->eventfd);
4105out_put_efile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004106 fdput(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004107out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004108 kfree(event);
4109
4110 return ret;
4111}
4112
Tejun Heo182446d2013-08-08 20:11:24 -04004113static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4114 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004115{
Tejun Heo182446d2013-08-08 20:11:24 -04004116 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004117}
4118
Tejun Heo182446d2013-08-08 20:11:24 -04004119static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4120 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004121{
4122 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004123 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004124 else
Tejun Heo182446d2013-08-08 20:11:24 -04004125 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004126 return 0;
4127}
4128
Tejun Heod5c56ce2013-06-03 19:14:34 -07004129static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004130 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004131 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004132 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004133 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004134 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004135 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004136 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004137 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004138 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004139 .write_string = cgroup_write_event_control,
4140 .mode = S_IWUGO,
4141 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004142 {
4143 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004144 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004145 .read_u64 = cgroup_clone_children_read,
4146 .write_u64 = cgroup_clone_children_write,
4147 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004148 {
Tejun Heo873fe092013-04-14 20:15:26 -07004149 .name = "cgroup.sane_behavior",
4150 .flags = CFTYPE_ONLY_ON_ROOT,
4151 .read_seq_string = cgroup_sane_behavior_show,
4152 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004153
4154 /*
4155 * Historical crazy stuff. These don't have "cgroup." prefix and
4156 * don't exist if sane_behavior. If you're depending on these, be
4157 * prepared to be burned.
4158 */
4159 {
4160 .name = "tasks",
4161 .flags = CFTYPE_INSANE, /* use "procs" instead */
4162 .open = cgroup_tasks_open,
4163 .write_u64 = cgroup_tasks_write,
4164 .release = cgroup_pidlist_release,
4165 .mode = S_IRUGO | S_IWUSR,
4166 },
4167 {
4168 .name = "notify_on_release",
4169 .flags = CFTYPE_INSANE,
4170 .read_u64 = cgroup_read_notify_on_release,
4171 .write_u64 = cgroup_write_notify_on_release,
4172 },
Tejun Heo873fe092013-04-14 20:15:26 -07004173 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004174 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004175 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004176 .read_seq_string = cgroup_release_agent_show,
4177 .write_string = cgroup_release_agent_write,
4178 .max_write_len = PATH_MAX,
4179 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004180 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004181};
4182
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004183/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004184 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004185 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004186 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004187 *
4188 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004189 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004190static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004193 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004194
Tejun Heo8e3f6542012-04-01 12:09:55 -07004195 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004196 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004197 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004198
4199 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004200 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004201
Tejun Heobee55092013-06-28 16:24:11 -07004202 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004203 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004204 if (ret < 0)
4205 goto err;
4206 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004207 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004209err:
4210 cgroup_clear_dir(cgrp, subsys_mask);
4211 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212}
4213
Tejun Heo0c21ead2013-08-13 20:22:51 -04004214/*
4215 * css destruction is four-stage process.
4216 *
4217 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4218 * Implemented in kill_css().
4219 *
4220 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4221 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4222 * by invoking offline_css(). After offlining, the base ref is put.
4223 * Implemented in css_killed_work_fn().
4224 *
4225 * 3. When the percpu_ref reaches zero, the only possible remaining
4226 * accessors are inside RCU read sections. css_release() schedules the
4227 * RCU callback.
4228 *
4229 * 4. After the grace period, the css can be freed. Implemented in
4230 * css_free_work_fn().
4231 *
4232 * It is actually hairier because both step 2 and 4 require process context
4233 * and thus involve punting to css->destroy_work adding two additional
4234 * steps to the already complex sequence.
4235 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004236static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004237{
4238 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004239 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004240 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004241
Tejun Heo0ae78e02013-08-13 11:01:54 -04004242 if (css->parent)
4243 css_put(css->parent);
4244
Tejun Heo0c21ead2013-08-13 20:22:51 -04004245 css->ss->css_free(css);
4246 cgroup_dput(cgrp);
4247}
4248
4249static void css_free_rcu_fn(struct rcu_head *rcu_head)
4250{
4251 struct cgroup_subsys_state *css =
4252 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4253
4254 /*
4255 * css holds an extra ref to @cgrp->dentry which is put on the last
4256 * css_put(). dput() requires process context which we don't have.
4257 */
4258 INIT_WORK(&css->destroy_work, css_free_work_fn);
4259 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004260}
4261
Tejun Heod3daf282013-06-13 19:39:16 -07004262static void css_release(struct percpu_ref *ref)
4263{
4264 struct cgroup_subsys_state *css =
4265 container_of(ref, struct cgroup_subsys_state, refcnt);
4266
Tejun Heo0c21ead2013-08-13 20:22:51 -04004267 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004268}
4269
Tejun Heo623f9262013-08-13 11:01:55 -04004270static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4271 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272{
Paul Menagebd89aab2007-10-18 23:40:44 -07004273 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004274 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004276
Tejun Heo0ae78e02013-08-13 11:01:54 -04004277 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004278 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004279 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004281
Tejun Heoca8bdca2013-08-26 18:40:56 -04004282 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283}
4284
Li Zefan2a4ac632013-07-31 16:16:40 +08004285/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004286static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004287{
Tejun Heo623f9262013-08-13 11:01:55 -04004288 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004289 int ret = 0;
4290
Tejun Heoa31f2d32012-11-19 08:13:37 -08004291 lockdep_assert_held(&cgroup_mutex);
4292
Tejun Heo92fb9742012-11-19 08:13:38 -08004293 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004294 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004295 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004296 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004297 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004298 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4299 }
Tejun Heob1929db2012-11-19 08:13:38 -08004300 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004301}
4302
Li Zefan2a4ac632013-07-31 16:16:40 +08004303/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004304static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004305{
Tejun Heo623f9262013-08-13 11:01:55 -04004306 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004307
4308 lockdep_assert_held(&cgroup_mutex);
4309
4310 if (!(css->flags & CSS_ONLINE))
4311 return;
4312
Li Zefand7eeac12013-03-12 15:35:59 -07004313 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004314 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004315
Tejun Heoeb954192013-08-08 20:11:23 -04004316 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004317 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004318 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004319}
4320
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321/*
Li Zefana043e3b2008-02-23 15:24:09 -08004322 * cgroup_create - create a cgroup
4323 * @parent: cgroup that will be parent of the new cgroup
4324 * @dentry: dentry of the new cgroup
4325 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326 *
Li Zefana043e3b2008-02-23 15:24:09 -08004327 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004330 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331{
Tejun Heoae7f1642013-08-13 20:22:50 -04004332 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004333 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004334 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 struct cgroupfs_root *root = parent->root;
4336 int err = 0;
4337 struct cgroup_subsys *ss;
4338 struct super_block *sb = root->sb;
4339
Tejun Heo0a950f62012-11-19 09:02:12 -08004340 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004341 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4342 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343 return -ENOMEM;
4344
Li Zefan65dff752013-03-01 15:01:56 +08004345 name = cgroup_alloc_name(dentry);
4346 if (!name)
4347 goto err_free_cgrp;
4348 rcu_assign_pointer(cgrp->name, name);
4349
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004350 /*
4351 * Temporarily set the pointer to NULL, so idr_find() won't return
4352 * a half-baked cgroup.
4353 */
4354 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004355 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004356 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004357
Tejun Heo976c06b2012-11-05 09:16:59 -08004358 /*
4359 * Only live parents can have children. Note that the liveliness
4360 * check isn't strictly necessary because cgroup_mkdir() and
4361 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4362 * anyway so that locking is contained inside cgroup proper and we
4363 * don't get nasty surprises if we ever grow another caller.
4364 */
4365 if (!cgroup_lock_live_group(parent)) {
4366 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004367 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004368 }
4369
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370 /* Grab a reference on the superblock so the hierarchy doesn't
4371 * get deleted on unmount if there are child cgroups. This
4372 * can be done outside cgroup_mutex, since the sb can't
4373 * disappear while someone has an open control file on the
4374 * fs */
4375 atomic_inc(&sb->s_active);
4376
Paul Menagecc31edc2008-10-18 20:28:04 -07004377 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378
Li Zefanfe1c06c2013-01-24 14:30:22 +08004379 dentry->d_fsdata = cgrp;
4380 cgrp->dentry = dentry;
4381
Paul Menagebd89aab2007-10-18 23:40:44 -07004382 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004383 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004384 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385
Li Zefanb6abdb02008-03-04 14:28:19 -08004386 if (notify_on_release(parent))
4387 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4388
Tejun Heo2260e7f2012-11-19 08:13:38 -08004389 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4390 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004391
Tejun Heo5549c492013-06-24 15:21:48 -07004392 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004393 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004394
Tejun Heoca8bdca2013-08-26 18:40:56 -04004395 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396 if (IS_ERR(css)) {
4397 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004398 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004400 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004401
4402 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004403 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004404 goto err_free_all;
4405
Tejun Heo623f9262013-08-13 11:01:55 -04004406 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 }
4408
Tejun Heo4e139af2012-11-19 08:13:36 -08004409 /*
4410 * Create directory. cgroup_create_file() returns with the new
4411 * directory locked on success so that it can be populated without
4412 * dropping cgroup_mutex.
4413 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004414 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004416 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004417 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004418
Tejun Heo00356bd2013-06-18 11:14:22 -07004419 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004420
Tejun Heo4e139af2012-11-19 08:13:36 -08004421 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004422 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4423 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004424
Tejun Heo0ae78e02013-08-13 11:01:54 -04004425 /* each css holds a ref to the cgroup's dentry and the parent css */
4426 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004427 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004428
Tejun Heoed9577932012-11-05 09:16:58 -08004429 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004430 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004431 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004432
Li Zefan415cf072013-04-08 14:35:02 +08004433 /* hold a ref to the parent's dentry */
4434 dget(parent->dentry);
4435
Tejun Heob1929db2012-11-19 08:13:38 -08004436 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004437 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004438 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004439
4440 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004441 if (err)
4442 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004443
4444 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4445 parent->parent) {
4446 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",
4447 current->comm, current->pid, ss->name);
4448 if (!strcmp(ss->name, "memory"))
4449 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4450 ss->warned_broken_hierarchy = true;
4451 }
Tejun Heoa8638032012-11-09 09:12:29 -08004452 }
4453
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004454 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4455
Tejun Heo2bb566c2013-08-08 20:11:23 -04004456 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004457 if (err)
4458 goto err_destroy;
4459
4460 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004461 if (err)
4462 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004463
4464 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004465 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004466
4467 return 0;
4468
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004469err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004470 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004471 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004472
4473 if (css) {
4474 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004475 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004476 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004478 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479 /* Release the reference count that we took on the superblock */
4480 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004481err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004482 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004483err_free_name:
4484 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004485err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004486 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004488
4489err_destroy:
4490 cgroup_destroy_locked(cgrp);
4491 mutex_unlock(&cgroup_mutex);
4492 mutex_unlock(&dentry->d_inode->i_mutex);
4493 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494}
4495
Al Viro18bb1db2011-07-26 01:41:39 -04004496static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497{
4498 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4499
4500 /* the vfs holds inode->i_mutex already */
4501 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4502}
4503
Tejun Heo223dbc32013-08-13 20:22:50 -04004504/*
4505 * This is called when the refcnt of a css is confirmed to be killed.
4506 * css_tryget() is now guaranteed to fail.
4507 */
4508static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004509{
Tejun Heo223dbc32013-08-13 20:22:50 -04004510 struct cgroup_subsys_state *css =
4511 container_of(work, struct cgroup_subsys_state, destroy_work);
4512 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004513
Tejun Heof20104d2013-08-13 20:22:50 -04004514 mutex_lock(&cgroup_mutex);
4515
4516 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004517 * css_tryget() is guaranteed to fail now. Tell subsystems to
4518 * initate destruction.
4519 */
4520 offline_css(css);
4521
4522 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004523 * If @cgrp is marked dead, it's waiting for refs of all css's to
4524 * be disabled before proceeding to the second phase of cgroup
4525 * destruction. If we are the last one, kick it off.
4526 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004527 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004528 cgroup_destroy_css_killed(cgrp);
4529
4530 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004531
4532 /*
4533 * Put the css refs from kill_css(). Each css holds an extra
4534 * reference to the cgroup's dentry and cgroup removal proceeds
4535 * regardless of css refs. On the last put of each css, whenever
4536 * that may be, the extra dentry ref is put so that dentry
4537 * destruction happens only after all css's are released.
4538 */
4539 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004540}
4541
Tejun Heo223dbc32013-08-13 20:22:50 -04004542/* css kill confirmation processing requires process context, bounce */
4543static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004544{
4545 struct cgroup_subsys_state *css =
4546 container_of(ref, struct cgroup_subsys_state, refcnt);
4547
Tejun Heo223dbc32013-08-13 20:22:50 -04004548 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4549 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004550}
4551
4552/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004553 * kill_css - destroy a css
4554 * @css: css to destroy
4555 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004556 * This function initiates destruction of @css by removing cgroup interface
4557 * files and putting its base reference. ->css_offline() will be invoked
4558 * asynchronously once css_tryget() is guaranteed to fail and when the
4559 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004560 */
4561static void kill_css(struct cgroup_subsys_state *css)
4562{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004563 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4564
Tejun Heoedae0c32013-08-13 20:22:51 -04004565 /*
4566 * Killing would put the base ref, but we need to keep it alive
4567 * until after ->css_offline().
4568 */
4569 css_get(css);
4570
4571 /*
4572 * cgroup core guarantees that, by the time ->css_offline() is
4573 * invoked, no new css reference will be given out via
4574 * css_tryget(). We can't simply call percpu_ref_kill() and
4575 * proceed to offlining css's because percpu_ref_kill() doesn't
4576 * guarantee that the ref is seen as killed on all CPUs on return.
4577 *
4578 * Use percpu_ref_kill_and_confirm() to get notifications as each
4579 * css is confirmed to be seen as killed on all CPUs.
4580 */
4581 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004582}
4583
4584/**
4585 * cgroup_destroy_locked - the first stage of cgroup destruction
4586 * @cgrp: cgroup to be destroyed
4587 *
4588 * css's make use of percpu refcnts whose killing latency shouldn't be
4589 * exposed to userland and are RCU protected. Also, cgroup core needs to
4590 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4591 * invoked. To satisfy all the requirements, destruction is implemented in
4592 * the following two steps.
4593 *
4594 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4595 * userland visible parts and start killing the percpu refcnts of
4596 * css's. Set up so that the next stage will be kicked off once all
4597 * the percpu refcnts are confirmed to be killed.
4598 *
4599 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4600 * rest of destruction. Once all cgroup references are gone, the
4601 * cgroup is RCU-freed.
4602 *
4603 * This function implements s1. After this step, @cgrp is gone as far as
4604 * the userland is concerned and a new cgroup with the same name may be
4605 * created. As cgroup doesn't care about the names internally, this
4606 * doesn't cause any problem.
4607 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004608static int cgroup_destroy_locked(struct cgroup *cgrp)
4609 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610{
Tejun Heo42809dd2012-11-19 08:13:37 -08004611 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004612 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004613 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004614 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004615 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616
Tejun Heo42809dd2012-11-19 08:13:37 -08004617 lockdep_assert_held(&d->d_inode->i_mutex);
4618 lockdep_assert_held(&cgroup_mutex);
4619
Tejun Heoddd69142013-06-12 21:04:54 -07004620 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004621 * css_set_lock synchronizes access to ->cset_links and prevents
4622 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004623 */
4624 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004625 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004626 read_unlock(&css_set_lock);
4627 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004629
Tejun Heo1a90dd52012-11-05 09:16:59 -08004630 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004631 * Make sure there's no live children. We can't test ->children
4632 * emptiness as dead children linger on it while being destroyed;
4633 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4634 */
4635 empty = true;
4636 rcu_read_lock();
4637 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4638 empty = cgroup_is_dead(child);
4639 if (!empty)
4640 break;
4641 }
4642 rcu_read_unlock();
4643 if (!empty)
4644 return -EBUSY;
4645
4646 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004647 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4648 * will be invoked to perform the rest of destruction once the
4649 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004650 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004651 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004652 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004653
4654 /*
4655 * Mark @cgrp dead. This prevents further task migration and child
4656 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004657 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004658 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004659 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004660 */
Tejun Heo54766d42013-06-12 21:04:53 -07004661 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004662
Tejun Heo455050d2013-06-13 19:27:41 -07004663 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4664 raw_spin_lock(&release_list_lock);
4665 if (!list_empty(&cgrp->release_list))
4666 list_del_init(&cgrp->release_list);
4667 raw_spin_unlock(&release_list_lock);
4668
4669 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004670 * If @cgrp has css's attached, the second stage of cgroup
4671 * destruction is kicked off from css_killed_work_fn() after the
4672 * refs of all attached css's are killed. If @cgrp doesn't have
4673 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004674 */
Tejun Heof20104d2013-08-13 20:22:50 -04004675 if (!cgrp->nr_css)
4676 cgroup_destroy_css_killed(cgrp);
4677
4678 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004679 * Clear the base files and remove @cgrp directory. The removal
4680 * puts the base ref but we aren't quite done with @cgrp yet, so
4681 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004682 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004683 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004684 dget(d);
4685 cgroup_d_remove_dir(d);
4686
4687 /*
4688 * Unregister events and notify userspace.
4689 * Notify userspace about cgroup removing only after rmdir of cgroup
4690 * directory to avoid race between userspace and kernelspace.
4691 */
4692 spin_lock(&cgrp->event_list_lock);
4693 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4694 list_del_init(&event->list);
4695 schedule_work(&event->remove);
4696 }
4697 spin_unlock(&cgrp->event_list_lock);
4698
Tejun Heoea15f8c2013-06-13 19:27:42 -07004699 return 0;
4700};
4701
Tejun Heod3daf282013-06-13 19:39:16 -07004702/**
Tejun Heof20104d2013-08-13 20:22:50 -04004703 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004704 * @work: cgroup->destroy_free_work
4705 *
4706 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004707 * destroyed after all css's are offlined and performs the rest of
4708 * destruction. This is the second step of destruction described in the
4709 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004710 */
Tejun Heof20104d2013-08-13 20:22:50 -04004711static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004712{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004713 struct cgroup *parent = cgrp->parent;
4714 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004715
Tejun Heof20104d2013-08-13 20:22:50 -04004716 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004717
Paul Menage999cd8a2009-01-07 18:08:36 -08004718 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004719 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004720
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004721 /*
4722 * We should remove the cgroup object from idr before its grace
4723 * period starts, so we won't be looking up a cgroup while the
4724 * cgroup is being freed.
4725 */
4726 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4727 cgrp->id = -1;
4728
Paul Menageddbcc7e2007-10-18 23:39:30 -07004729 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730
Paul Menagebd89aab2007-10-18 23:40:44 -07004731 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004732 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004733}
4734
Tejun Heo42809dd2012-11-19 08:13:37 -08004735static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4736{
4737 int ret;
4738
4739 mutex_lock(&cgroup_mutex);
4740 ret = cgroup_destroy_locked(dentry->d_fsdata);
4741 mutex_unlock(&cgroup_mutex);
4742
4743 return ret;
4744}
4745
Tejun Heo8e3f6542012-04-01 12:09:55 -07004746static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4747{
4748 INIT_LIST_HEAD(&ss->cftsets);
4749
4750 /*
4751 * base_cftset is embedded in subsys itself, no need to worry about
4752 * deregistration.
4753 */
4754 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004755 struct cftype *cft;
4756
4757 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4758 cft->ss = ss;
4759
Tejun Heo8e3f6542012-04-01 12:09:55 -07004760 ss->base_cftset.cfts = ss->base_cftypes;
4761 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4762 }
4763}
4764
Li Zefan06a11922008-04-29 01:00:07 -07004765static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004766{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004767 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004768
4769 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004770
Tejun Heo648bb562012-11-19 08:13:36 -08004771 mutex_lock(&cgroup_mutex);
4772
Tejun Heo8e3f6542012-04-01 12:09:55 -07004773 /* init base cftset */
4774 cgroup_init_cftsets(ss);
4775
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004777 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4778 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004779 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004780 /* We don't handle early failures gracefully */
4781 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004782 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004783
Li Zefane8d55fd2008-04-29 01:00:13 -07004784 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004785 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004786 * newly registered, all tasks and hence the
4787 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004788 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789
4790 need_forkexit_callback |= ss->fork || ss->exit;
4791
Li Zefane8d55fd2008-04-29 01:00:13 -07004792 /* At system boot, before all subsystems have been
4793 * registered, no tasks have been forked, so we don't
4794 * need to invoke fork callbacks here. */
4795 BUG_ON(!list_empty(&init_task.tasks));
4796
Tejun Heoae7f1642013-08-13 20:22:50 -04004797 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004798
Tejun Heo648bb562012-11-19 08:13:36 -08004799 mutex_unlock(&cgroup_mutex);
4800
Ben Blume6a11052010-03-10 15:22:09 -08004801 /* this function shouldn't be used with modular subsystems, since they
4802 * need to register a subsys_id, among other things */
4803 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004804}
4805
4806/**
Ben Blume6a11052010-03-10 15:22:09 -08004807 * cgroup_load_subsys: load and register a modular subsystem at runtime
4808 * @ss: the subsystem to load
4809 *
4810 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004811 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004812 * up for use. If the subsystem is built-in anyway, work is delegated to the
4813 * simpler cgroup_init_subsys.
4814 */
4815int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4816{
Ben Blume6a11052010-03-10 15:22:09 -08004817 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004818 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004819 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004820 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004821 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004822
4823 /* check name and function validity */
4824 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004825 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004826 return -EINVAL;
4827
4828 /*
4829 * we don't support callbacks in modular subsystems. this check is
4830 * before the ss->module check for consistency; a subsystem that could
4831 * be a module should still have no callbacks even if the user isn't
4832 * compiling it as one.
4833 */
4834 if (ss->fork || ss->exit)
4835 return -EINVAL;
4836
4837 /*
4838 * an optionally modular subsystem is built-in: we want to do nothing,
4839 * since cgroup_init_subsys will have already taken care of it.
4840 */
4841 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004842 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004844 return 0;
4845 }
4846
Tejun Heo8e3f6542012-04-01 12:09:55 -07004847 /* init base cftset */
4848 cgroup_init_cftsets(ss);
4849
Ben Blume6a11052010-03-10 15:22:09 -08004850 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004851 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004852
4853 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004854 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004855 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004856 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004857 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004858 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004859 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004860 /* failure case - need to deassign the cgroup_subsys[] slot. */
4861 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004862 mutex_unlock(&cgroup_mutex);
4863 return PTR_ERR(css);
4864 }
4865
Tejun Heo9871bf92013-06-24 15:21:47 -07004866 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4867 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004868
4869 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004870 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004871
4872 /*
4873 * Now we need to entangle the css into the existing css_sets. unlike
4874 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4875 * will need a new pointer to it; done by iterating the css_set_table.
4876 * furthermore, modifying the existing css_sets will corrupt the hash
4877 * table state, so each changed css_set will need its hash recomputed.
4878 * this is all done under the css_set_lock.
4879 */
4880 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004881 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004882 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004883 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004884 continue;
4885 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004886 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004887 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004888 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004889 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004890 key = css_set_hash(cset->subsys);
4891 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004892 }
4893 write_unlock(&css_set_lock);
4894
Tejun Heoae7f1642013-08-13 20:22:50 -04004895 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004896 if (ret)
4897 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004898
Ben Blume6a11052010-03-10 15:22:09 -08004899 /* success! */
4900 mutex_unlock(&cgroup_mutex);
4901 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004902
4903err_unload:
4904 mutex_unlock(&cgroup_mutex);
4905 /* @ss can't be mounted here as try_module_get() would fail */
4906 cgroup_unload_subsys(ss);
4907 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004908}
4909EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4910
4911/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004912 * cgroup_unload_subsys: unload a modular subsystem
4913 * @ss: the subsystem to unload
4914 *
4915 * This function should be called in a modular subsystem's exitcall. When this
4916 * function is invoked, the refcount on the subsystem's module will be 0, so
4917 * the subsystem will not be attached to any hierarchy.
4918 */
4919void cgroup_unload_subsys(struct cgroup_subsys *ss)
4920{
Tejun Heo69d02062013-06-12 21:04:50 -07004921 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004922
4923 BUG_ON(ss->module == NULL);
4924
4925 /*
4926 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004927 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004928 * doesn't start being used while we're killing it off.
4929 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004930 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004931
4932 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004933
Tejun Heoca8bdca2013-08-26 18:40:56 -04004934 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004935
Ben Blumcf5d5942010-03-10 15:22:09 -08004936 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004937 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004938
Tejun Heo9871bf92013-06-24 15:21:47 -07004939 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004940 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004941
4942 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004943 * disentangle the css from all css_sets attached to the dummy
4944 * top. as in loading, we need to pay our respects to the hashtable
4945 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004946 */
4947 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004948 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004949 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004950 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004951
Tejun Heo5abb8852013-06-12 21:04:49 -07004952 hash_del(&cset->hlist);
4953 cset->subsys[ss->subsys_id] = NULL;
4954 key = css_set_hash(cset->subsys);
4955 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004956 }
4957 write_unlock(&css_set_lock);
4958
4959 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004960 * remove subsystem's css from the cgroup_dummy_top and free it -
4961 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004962 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004963 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004964 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004965 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004966
4967 mutex_unlock(&cgroup_mutex);
4968}
4969EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4970
4971/**
Li Zefana043e3b2008-02-23 15:24:09 -08004972 * cgroup_init_early - cgroup initialization at system boot
4973 *
4974 * Initialize cgroups at system boot, and initialize any
4975 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004976 */
4977int __init cgroup_init_early(void)
4978{
Tejun Heo30159ec2013-06-25 11:53:37 -07004979 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004980 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004981
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004982 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004983 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004984 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004985 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004986 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004987 init_cgroup_root(&cgroup_dummy_root);
4988 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004989 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004990
Tejun Heo69d02062013-06-12 21:04:50 -07004991 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004992 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4993 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004994 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004995
Tejun Heo30159ec2013-06-25 11:53:37 -07004996 /* at bootup time, we don't worry about modular subsystems */
4997 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004998 BUG_ON(!ss->name);
4999 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005000 BUG_ON(!ss->css_alloc);
5001 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005002 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005003 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005004 ss->name, ss->subsys_id);
5005 BUG();
5006 }
5007
5008 if (ss->early_init)
5009 cgroup_init_subsys(ss);
5010 }
5011 return 0;
5012}
5013
5014/**
Li Zefana043e3b2008-02-23 15:24:09 -08005015 * cgroup_init - cgroup initialization
5016 *
5017 * Register cgroup filesystem and /proc file, and initialize
5018 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005019 */
5020int __init cgroup_init(void)
5021{
Tejun Heo30159ec2013-06-25 11:53:37 -07005022 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005023 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005024 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005025
5026 err = bdi_init(&cgroup_backing_dev_info);
5027 if (err)
5028 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005029
Tejun Heo30159ec2013-06-25 11:53:37 -07005030 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005031 if (!ss->early_init)
5032 cgroup_init_subsys(ss);
5033 }
5034
Tejun Heofa3ca072013-04-14 11:36:56 -07005035 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005036 mutex_lock(&cgroup_mutex);
5037 mutex_lock(&cgroup_root_mutex);
5038
Tejun Heo82fe9b02013-06-25 11:53:37 -07005039 /* Add init_css_set to the hash table */
5040 key = css_set_hash(init_css_set.subsys);
5041 hash_add(css_set_table, &init_css_set.hlist, key);
5042
Tejun Heofc76df72013-06-25 11:53:37 -07005043 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005044
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005045 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5046 0, 1, GFP_KERNEL);
5047 BUG_ON(err < 0);
5048
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005049 mutex_unlock(&cgroup_root_mutex);
5050 mutex_unlock(&cgroup_mutex);
5051
Greg KH676db4a2010-08-05 13:53:35 -07005052 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5053 if (!cgroup_kobj) {
5054 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005055 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005056 }
5057
5058 err = register_filesystem(&cgroup_fs_type);
5059 if (err < 0) {
5060 kobject_put(cgroup_kobj);
5061 goto out;
5062 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005063
Li Zefan46ae2202008-04-29 01:00:08 -07005064 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005065
Paul Menageddbcc7e2007-10-18 23:39:30 -07005066out:
Paul Menagea4243162007-10-18 23:39:35 -07005067 if (err)
5068 bdi_destroy(&cgroup_backing_dev_info);
5069
Paul Menageddbcc7e2007-10-18 23:39:30 -07005070 return err;
5071}
Paul Menageb4f48b62007-10-18 23:39:33 -07005072
Paul Menagea4243162007-10-18 23:39:35 -07005073/*
5074 * proc_cgroup_show()
5075 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5076 * - Used for /proc/<pid>/cgroup.
5077 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5078 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005079 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005080 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5081 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5082 * cgroup to top_cgroup.
5083 */
5084
5085/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005086int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005087{
5088 struct pid *pid;
5089 struct task_struct *tsk;
5090 char *buf;
5091 int retval;
5092 struct cgroupfs_root *root;
5093
5094 retval = -ENOMEM;
5095 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5096 if (!buf)
5097 goto out;
5098
5099 retval = -ESRCH;
5100 pid = m->private;
5101 tsk = get_pid_task(pid, PIDTYPE_PID);
5102 if (!tsk)
5103 goto out_free;
5104
5105 retval = 0;
5106
5107 mutex_lock(&cgroup_mutex);
5108
Li Zefane5f6a862009-01-07 18:07:41 -08005109 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005110 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005111 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005112 int count = 0;
5113
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005114 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005115 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005116 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005117 if (strlen(root->name))
5118 seq_printf(m, "%sname=%s", count ? "," : "",
5119 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005120 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005121 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005122 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005123 if (retval < 0)
5124 goto out_unlock;
5125 seq_puts(m, buf);
5126 seq_putc(m, '\n');
5127 }
5128
5129out_unlock:
5130 mutex_unlock(&cgroup_mutex);
5131 put_task_struct(tsk);
5132out_free:
5133 kfree(buf);
5134out:
5135 return retval;
5136}
5137
Paul Menagea4243162007-10-18 23:39:35 -07005138/* Display information about each subsystem and each hierarchy */
5139static int proc_cgroupstats_show(struct seq_file *m, void *v)
5140{
Tejun Heo30159ec2013-06-25 11:53:37 -07005141 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005142 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005143
Paul Menage8bab8dd2008-04-04 14:29:57 -07005144 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005145 /*
5146 * ideally we don't want subsystems moving around while we do this.
5147 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5148 * subsys/hierarchy state.
5149 */
Paul Menagea4243162007-10-18 23:39:35 -07005150 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005151
5152 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005153 seq_printf(m, "%s\t%d\t%d\t%d\n",
5154 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005155 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005156
Paul Menagea4243162007-10-18 23:39:35 -07005157 mutex_unlock(&cgroup_mutex);
5158 return 0;
5159}
5160
5161static int cgroupstats_open(struct inode *inode, struct file *file)
5162{
Al Viro9dce07f2008-03-29 03:07:28 +00005163 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005164}
5165
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005166static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005167 .open = cgroupstats_open,
5168 .read = seq_read,
5169 .llseek = seq_lseek,
5170 .release = single_release,
5171};
5172
Paul Menageb4f48b62007-10-18 23:39:33 -07005173/**
5174 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005175 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005176 *
5177 * Description: A task inherits its parent's cgroup at fork().
5178 *
5179 * A pointer to the shared css_set was automatically copied in
5180 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005181 * it was not made under the protection of RCU or cgroup_mutex, so
5182 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5183 * have already changed current->cgroups, allowing the previously
5184 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005185 *
5186 * At the point that cgroup_fork() is called, 'current' is the parent
5187 * task, and the passed argument 'child' points to the child task.
5188 */
5189void cgroup_fork(struct task_struct *child)
5190{
Tejun Heo9bb71302012-10-18 17:52:07 -07005191 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005192 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005193 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005194 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005195 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005196}
5197
5198/**
Li Zefana043e3b2008-02-23 15:24:09 -08005199 * cgroup_post_fork - called on a new task after adding it to the task list
5200 * @child: the task in question
5201 *
Tejun Heo5edee612012-10-16 15:03:14 -07005202 * Adds the task to the list running through its css_set if necessary and
5203 * call the subsystem fork() callbacks. Has to be after the task is
5204 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005205 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005206 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005207 */
Paul Menage817929e2007-10-18 23:39:36 -07005208void cgroup_post_fork(struct task_struct *child)
5209{
Tejun Heo30159ec2013-06-25 11:53:37 -07005210 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005211 int i;
5212
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005213 /*
5214 * use_task_css_set_links is set to 1 before we walk the tasklist
5215 * under the tasklist_lock and we read it here after we added the child
5216 * to the tasklist under the tasklist_lock as well. If the child wasn't
5217 * yet in the tasklist when we walked through it from
5218 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5219 * should be visible now due to the paired locking and barriers implied
5220 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5221 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5222 * lock on fork.
5223 */
Paul Menage817929e2007-10-18 23:39:36 -07005224 if (use_task_css_set_links) {
5225 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005226 task_lock(child);
5227 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005228 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005229 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005230 write_unlock(&css_set_lock);
5231 }
Tejun Heo5edee612012-10-16 15:03:14 -07005232
5233 /*
5234 * Call ss->fork(). This must happen after @child is linked on
5235 * css_set; otherwise, @child might change state between ->fork()
5236 * and addition to css_set.
5237 */
5238 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005239 /*
5240 * fork/exit callbacks are supported only for builtin
5241 * subsystems, and the builtin section of the subsys
5242 * array is immutable, so we don't need to lock the
5243 * subsys array here. On the other hand, modular section
5244 * of the array can be freed at module unload, so we
5245 * can't touch that.
5246 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005247 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005248 if (ss->fork)
5249 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005250 }
Paul Menage817929e2007-10-18 23:39:36 -07005251}
Tejun Heo5edee612012-10-16 15:03:14 -07005252
Paul Menage817929e2007-10-18 23:39:36 -07005253/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005254 * cgroup_exit - detach cgroup from exiting task
5255 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005256 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005257 *
5258 * Description: Detach cgroup from @tsk and release it.
5259 *
5260 * Note that cgroups marked notify_on_release force every task in
5261 * them to take the global cgroup_mutex mutex when exiting.
5262 * This could impact scaling on very large systems. Be reluctant to
5263 * use notify_on_release cgroups where very high task exit scaling
5264 * is required on large systems.
5265 *
5266 * the_top_cgroup_hack:
5267 *
5268 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5269 *
5270 * We call cgroup_exit() while the task is still competent to
5271 * handle notify_on_release(), then leave the task attached to the
5272 * root cgroup in each hierarchy for the remainder of its exit.
5273 *
5274 * To do this properly, we would increment the reference count on
5275 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5276 * code we would add a second cgroup function call, to drop that
5277 * reference. This would just create an unnecessary hot spot on
5278 * the top_cgroup reference count, to no avail.
5279 *
5280 * Normally, holding a reference to a cgroup without bumping its
5281 * count is unsafe. The cgroup could go away, or someone could
5282 * attach us to a different cgroup, decrementing the count on
5283 * the first cgroup that we never incremented. But in this case,
5284 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005285 * which wards off any cgroup_attach_task() attempts, or task is a failed
5286 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005287 */
5288void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5289{
Tejun Heo30159ec2013-06-25 11:53:37 -07005290 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005291 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005292 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005293
5294 /*
5295 * Unlink from the css_set task list if necessary.
5296 * Optimistically check cg_list before taking
5297 * css_set_lock
5298 */
5299 if (!list_empty(&tsk->cg_list)) {
5300 write_lock(&css_set_lock);
5301 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005302 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005303 write_unlock(&css_set_lock);
5304 }
5305
Paul Menageb4f48b62007-10-18 23:39:33 -07005306 /* Reassign the task to the init_css_set. */
5307 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005308 cset = task_css_set(tsk);
5309 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005310
5311 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005312 /*
5313 * fork/exit callbacks are supported only for builtin
5314 * subsystems, see cgroup_post_fork() for details.
5315 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005316 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005317 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005318 struct cgroup_subsys_state *old_css = cset->subsys[i];
5319 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005320
Tejun Heoeb954192013-08-08 20:11:23 -04005321 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005322 }
5323 }
5324 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005325 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005326
Tejun Heo5abb8852013-06-12 21:04:49 -07005327 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005328}
Paul Menage697f4162007-10-18 23:39:34 -07005329
Paul Menagebd89aab2007-10-18 23:40:44 -07005330static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005331{
Li Zefanf50daa72013-03-01 15:06:07 +08005332 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005333 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005334 /*
5335 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005336 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005337 * it now
5338 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005339 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005340
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005341 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005342 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005343 list_empty(&cgrp->release_list)) {
5344 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005345 need_schedule_work = 1;
5346 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005347 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005348 if (need_schedule_work)
5349 schedule_work(&release_agent_work);
5350 }
5351}
5352
Paul Menage81a6a5c2007-10-18 23:39:38 -07005353/*
5354 * Notify userspace when a cgroup is released, by running the
5355 * configured release agent with the name of the cgroup (path
5356 * relative to the root of cgroup file system) as the argument.
5357 *
5358 * Most likely, this user command will try to rmdir this cgroup.
5359 *
5360 * This races with the possibility that some other task will be
5361 * attached to this cgroup before it is removed, or that some other
5362 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5363 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5364 * unused, and this cgroup will be reprieved from its death sentence,
5365 * to continue to serve a useful existence. Next time it's released,
5366 * we will get notified again, if it still has 'notify_on_release' set.
5367 *
5368 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5369 * means only wait until the task is successfully execve()'d. The
5370 * separate release agent task is forked by call_usermodehelper(),
5371 * then control in this thread returns here, without waiting for the
5372 * release agent task. We don't bother to wait because the caller of
5373 * this routine has no use for the exit status of the release agent
5374 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005375 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005376static void cgroup_release_agent(struct work_struct *work)
5377{
5378 BUG_ON(work != &release_agent_work);
5379 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005380 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005381 while (!list_empty(&release_list)) {
5382 char *argv[3], *envp[3];
5383 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005384 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005385 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005386 struct cgroup,
5387 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005388 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005389 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005390 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005391 if (!pathbuf)
5392 goto continue_free;
5393 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5394 goto continue_free;
5395 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5396 if (!agentbuf)
5397 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005398
5399 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005400 argv[i++] = agentbuf;
5401 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005402 argv[i] = NULL;
5403
5404 i = 0;
5405 /* minimal command environment */
5406 envp[i++] = "HOME=/";
5407 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5408 envp[i] = NULL;
5409
5410 /* Drop the lock while we invoke the usermode helper,
5411 * since the exec could involve hitting disk and hence
5412 * be a slow process */
5413 mutex_unlock(&cgroup_mutex);
5414 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005415 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005416 continue_free:
5417 kfree(pathbuf);
5418 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005419 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005420 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005421 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005422 mutex_unlock(&cgroup_mutex);
5423}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005424
5425static int __init cgroup_disable(char *str)
5426{
Tejun Heo30159ec2013-06-25 11:53:37 -07005427 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005428 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005429 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005430
5431 while ((token = strsep(&str, ",")) != NULL) {
5432 if (!*token)
5433 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005434
Tejun Heo30159ec2013-06-25 11:53:37 -07005435 /*
5436 * cgroup_disable, being at boot time, can't know about
5437 * module subsystems, so we don't worry about them.
5438 */
5439 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005440 if (!strcmp(token, ss->name)) {
5441 ss->disabled = 1;
5442 printk(KERN_INFO "Disabling %s control group"
5443 " subsystem\n", ss->name);
5444 break;
5445 }
5446 }
5447 }
5448 return 1;
5449}
5450__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451
Tejun Heob77d7b62013-08-13 11:01:54 -04005452/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005453 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5454 * @dentry: directory dentry of interest
5455 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005456 *
5457 * Must be called under RCU read lock. The caller is responsible for
5458 * pinning the returned css if it needs to be accessed outside the RCU
5459 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005460 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005461struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5462 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005463{
5464 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005465
Tejun Heob77d7b62013-08-13 11:01:54 -04005466 WARN_ON_ONCE(!rcu_read_lock_held());
5467
Tejun Heo35cf0832013-08-26 18:40:56 -04005468 /* is @dentry a cgroup dir? */
5469 if (!dentry->d_inode ||
5470 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005471 return ERR_PTR(-EBADF);
5472
Tejun Heo35cf0832013-08-26 18:40:56 -04005473 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005474 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005475}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005476
Li Zefan1cb650b2013-08-19 10:05:24 +08005477/**
5478 * css_from_id - lookup css by id
5479 * @id: the cgroup id
5480 * @ss: cgroup subsys to be looked into
5481 *
5482 * Returns the css if there's valid one with @id, otherwise returns NULL.
5483 * Should be called under rcu_read_lock().
5484 */
5485struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5486{
5487 struct cgroup *cgrp;
5488
5489 rcu_lockdep_assert(rcu_read_lock_held() ||
5490 lockdep_is_held(&cgroup_mutex),
5491 "css_from_id() needs proper protection");
5492
5493 cgrp = idr_find(&ss->root->cgroup_idr, id);
5494 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005495 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005496 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005497}
5498
Paul Menagefe693432009-09-23 15:56:20 -07005499#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005500static struct cgroup_subsys_state *
5501debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005502{
5503 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5504
5505 if (!css)
5506 return ERR_PTR(-ENOMEM);
5507
5508 return css;
5509}
5510
Tejun Heoeb954192013-08-08 20:11:23 -04005511static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005512{
Tejun Heoeb954192013-08-08 20:11:23 -04005513 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005514}
5515
Tejun Heo182446d2013-08-08 20:11:24 -04005516static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5517 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005518{
Tejun Heo182446d2013-08-08 20:11:24 -04005519 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005520}
5521
Tejun Heo182446d2013-08-08 20:11:24 -04005522static u64 current_css_set_read(struct cgroup_subsys_state *css,
5523 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005524{
5525 return (u64)(unsigned long)current->cgroups;
5526}
5527
Tejun Heo182446d2013-08-08 20:11:24 -04005528static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005529 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005530{
5531 u64 count;
5532
5533 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005534 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005535 rcu_read_unlock();
5536 return count;
5537}
5538
Tejun Heo182446d2013-08-08 20:11:24 -04005539static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005540 struct cftype *cft,
5541 struct seq_file *seq)
5542{
Tejun Heo69d02062013-06-12 21:04:50 -07005543 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005544 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005545
5546 read_lock(&css_set_lock);
5547 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005548 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005549 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005550 struct cgroup *c = link->cgrp;
5551 const char *name;
5552
5553 if (c->dentry)
5554 name = c->dentry->d_name.name;
5555 else
5556 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005557 seq_printf(seq, "Root %d group %s\n",
5558 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005559 }
5560 rcu_read_unlock();
5561 read_unlock(&css_set_lock);
5562 return 0;
5563}
5564
5565#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005566static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5567 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005568{
Tejun Heo69d02062013-06-12 21:04:50 -07005569 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005570
5571 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005572 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005573 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005574 struct task_struct *task;
5575 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005576 seq_printf(seq, "css_set %p\n", cset);
5577 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005578 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5579 seq_puts(seq, " ...\n");
5580 break;
5581 } else {
5582 seq_printf(seq, " task %d\n",
5583 task_pid_vnr(task));
5584 }
5585 }
5586 }
5587 read_unlock(&css_set_lock);
5588 return 0;
5589}
5590
Tejun Heo182446d2013-08-08 20:11:24 -04005591static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005592{
Tejun Heo182446d2013-08-08 20:11:24 -04005593 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005594}
5595
5596static struct cftype debug_files[] = {
5597 {
Paul Menagefe693432009-09-23 15:56:20 -07005598 .name = "taskcount",
5599 .read_u64 = debug_taskcount_read,
5600 },
5601
5602 {
5603 .name = "current_css_set",
5604 .read_u64 = current_css_set_read,
5605 },
5606
5607 {
5608 .name = "current_css_set_refcount",
5609 .read_u64 = current_css_set_refcount_read,
5610 },
5611
5612 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005613 .name = "current_css_set_cg_links",
5614 .read_seq_string = current_css_set_cg_links_read,
5615 },
5616
5617 {
5618 .name = "cgroup_css_links",
5619 .read_seq_string = cgroup_css_links_read,
5620 },
5621
5622 {
Paul Menagefe693432009-09-23 15:56:20 -07005623 .name = "releasable",
5624 .read_u64 = releasable_read,
5625 },
Paul Menagefe693432009-09-23 15:56:20 -07005626
Tejun Heo4baf6e32012-04-01 12:09:55 -07005627 { } /* terminate */
5628};
Paul Menagefe693432009-09-23 15:56:20 -07005629
5630struct cgroup_subsys debug_subsys = {
5631 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005632 .css_alloc = debug_css_alloc,
5633 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005634 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005635 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005636};
5637#endif /* CONFIG_CGROUP_DEBUG */