blob: bcb1755f410a6b79538af66c016b55dbb06efb4f [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/*
Tejun Heoe5fca242013-11-22 17:14:39 -050093 * cgroup destruction makes heavy use of work items and there can be a lot
94 * of concurrent destructions. Use a separate workqueue so that cgroup
95 * destruction work items don't end up filling up max_active of system_wq
96 * which may lead to deadlock.
97 */
98static struct workqueue_struct *cgroup_destroy_wq;
99
100/*
Ben Blumaae8aab2010-03-10 15:22:07 -0800101 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +0200102 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800103 * registered after that. The mutable section of this array is protected by
104 * cgroup_mutex.
105 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200106#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200107#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109#include <linux/cgroup_subsys.h>
110};
111
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700113 * The dummy hierarchy, reserved for the subsystems that are otherwise
114 * unattached - it never has more than a single cgroup, and all tasks are
115 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700116 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700117static struct cgroupfs_root cgroup_dummy_root;
118
119/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
120static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700121
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700122/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
124 */
125struct cfent {
126 struct list_head node;
127 struct dentry *dentry;
128 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400129 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700130
131 /* file xattrs */
132 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700133};
134
135/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300136 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800137 */
138struct cgroup_event {
139 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400140 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800141 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400142 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800143 /*
144 * Control file which the event associated.
145 */
146 struct cftype *cft;
147 /*
148 * eventfd to signal userspace about the event.
149 */
150 struct eventfd_ctx *eventfd;
151 /*
152 * Each of these stored in a list by the cgroup.
153 */
154 struct list_head list;
155 /*
156 * All fields below needed to unregister event when
157 * userspace closes eventfd.
158 */
159 poll_table pt;
160 wait_queue_head_t *wqh;
161 wait_queue_t wait;
162 struct work_struct remove;
163};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700164
Paul Menageddbcc7e2007-10-18 23:39:30 -0700165/* The list of hierarchy roots */
166
Tejun Heo9871bf92013-06-24 15:21:47 -0700167static LIST_HEAD(cgroup_roots);
168static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700169
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700170/*
171 * Hierarchy ID allocation and mapping. It follows the same exclusion
172 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
173 * writes, either for reads.
174 */
Tejun Heo1a574232013-04-14 11:36:58 -0700175static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700176
Li Zefan65dff752013-03-01 15:01:56 +0800177static struct cgroup_name root_cgroup_name = { .name = "/" };
178
Li Zefan794611a2013-06-18 18:53:53 +0800179/*
180 * Assign a monotonically increasing serial number to cgroups. It
181 * guarantees cgroups with bigger numbers are newer than those with smaller
182 * numbers. Also, as cgroups are always appended to the parent's
183 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700184 * the ascending serial number order on the list. Protected by
185 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800186 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700187static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800190 * check for fork/exit handlers to call. This avoids us having to do
191 * extra work in the fork/exit path if none of the subsystems need to
192 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700194static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700195
Tejun Heo628f7cd2013-06-28 16:24:11 -0700196static struct cftype cgroup_base_files[];
197
Tejun Heof20104d2013-08-13 20:22:50 -0400198static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800199static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400200static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
201 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500202static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heo42809dd2012-11-19 08:13:37 -0800203
Tejun Heo95109b62013-08-08 20:11:27 -0400204/**
205 * cgroup_css - obtain a cgroup's css for the specified subsystem
206 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400207 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400208 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400209 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
210 * function must be called either under cgroup_mutex or rcu_read_lock() and
211 * the caller is responsible for pinning the returned css if it wants to
212 * keep accessing it outside the said locks. This function may return
213 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400214 */
215static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400216 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400217{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400218 if (ss)
219 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
220 lockdep_is_held(&cgroup_mutex));
221 else
222 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400223}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Tejun Heo0942eee2013-08-08 20:11:26 -0400367/*
368 * css_set_lock protects the list of css_set objects, and the chain of
369 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400370 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400371 */
Paul Menage817929e2007-10-18 23:39:36 -0700372static DEFINE_RWLOCK(css_set_lock);
373static int css_set_count;
374
Paul Menage7717f7b2009-09-23 15:56:22 -0700375/*
376 * hash table for cgroup groups. This improves the performance to find
377 * an existing css_set. This hash doesn't (currently) take into
378 * account cgroups in empty hierarchies.
379 */
Li Zefan472b1052008-04-29 01:00:11 -0700380#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800381static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700382
Li Zefan0ac801f2013-01-10 11:49:27 +0800383static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700384{
Li Zefan0ac801f2013-01-10 11:49:27 +0800385 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700386 struct cgroup_subsys *ss;
387 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700388
Tejun Heo30159ec2013-06-25 11:53:37 -0700389 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800390 key += (unsigned long)css[i];
391 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700392
Li Zefan0ac801f2013-01-10 11:49:27 +0800393 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700394}
395
Tejun Heo0942eee2013-08-08 20:11:26 -0400396/*
397 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400398 * until after the first call to css_task_iter_start(). This reduces the
399 * fork()/exit() overhead for people who have cgroups compiled into their
400 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400401 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700402static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700403
Tejun Heo5abb8852013-06-12 21:04:49 -0700404static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700405{
Tejun Heo69d02062013-06-12 21:04:50 -0700406 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700407
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700408 /*
409 * Ensure that the refcount doesn't hit zero while any readers
410 * can see it. Similar to atomic_dec_and_lock(), but for an
411 * rwlock
412 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700413 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700414 return;
415 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700416 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700417 write_unlock(&css_set_lock);
418 return;
419 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700420
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700421 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700423 css_set_count--;
424
Tejun Heo69d02062013-06-12 21:04:50 -0700425 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700426 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700427
Tejun Heo69d02062013-06-12 21:04:50 -0700428 list_del(&link->cset_link);
429 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800430
Tejun Heoddd69142013-06-12 21:04:54 -0700431 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700432 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700434 set_bit(CGRP_RELEASABLE, &cgrp->flags);
435 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700437
438 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700439 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700440
441 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700442 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700443}
444
445/*
446 * refcounted get/put for css_set objects
447 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700448static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700449{
Tejun Heo5abb8852013-06-12 21:04:49 -0700450 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700451}
452
Tejun Heo5abb8852013-06-12 21:04:49 -0700453static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700454{
Tejun Heo5abb8852013-06-12 21:04:49 -0700455 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700456}
457
Tejun Heo5abb8852013-06-12 21:04:49 -0700458static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700459{
Tejun Heo5abb8852013-06-12 21:04:49 -0700460 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700461}
462
Tejun Heob326f9d2013-06-24 15:21:48 -0700463/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700465 * @cset: candidate css_set being tested
466 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 * @new_cgrp: cgroup that's being entered by the task
468 * @template: desired set of css pointers in css_set (pre-calculated)
469 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800470 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700471 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
472 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700473static bool compare_css_sets(struct css_set *cset,
474 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700475 struct cgroup *new_cgrp,
476 struct cgroup_subsys_state *template[])
477{
478 struct list_head *l1, *l2;
479
Tejun Heo5abb8852013-06-12 21:04:49 -0700480 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 /* Not all subsystems matched */
482 return false;
483 }
484
485 /*
486 * Compare cgroup pointers in order to distinguish between
487 * different cgroups in heirarchies with no subsystems. We
488 * could get by with just this check alone (and skip the
489 * memcmp above) but on most setups the memcmp check will
490 * avoid the need for this more expensive check on almost all
491 * candidates.
492 */
493
Tejun Heo69d02062013-06-12 21:04:50 -0700494 l1 = &cset->cgrp_links;
495 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700497 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700498 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700499
500 l1 = l1->next;
501 l2 = l2->next;
502 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700503 if (l1 == &cset->cgrp_links) {
504 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 break;
506 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700507 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700508 }
509 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700510 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
511 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
512 cgrp1 = link1->cgrp;
513 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700514 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700515 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700516
517 /*
518 * If this hierarchy is the hierarchy of the cgroup
519 * that's changing, then we need to check that this
520 * css_set points to the new cgroup; if it's any other
521 * hierarchy, then this css_set should point to the
522 * same cgroup as the old css_set.
523 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700524 if (cgrp1->root == new_cgrp->root) {
525 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700526 return false;
527 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700528 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700529 return false;
530 }
531 }
532 return true;
533}
534
Tejun Heob326f9d2013-06-24 15:21:48 -0700535/**
536 * find_existing_css_set - init css array and find the matching css_set
537 * @old_cset: the css_set that we're using before the cgroup transition
538 * @cgrp: the cgroup that we're moving into
539 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700540 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700541static struct css_set *find_existing_css_set(struct css_set *old_cset,
542 struct cgroup *cgrp,
543 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700544{
Paul Menagebd89aab2007-10-18 23:40:44 -0700545 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700546 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700547 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800548 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700549 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700550
Ben Blumaae8aab2010-03-10 15:22:07 -0800551 /*
552 * Build the set of subsystem state objects that we want to see in the
553 * new css_set. while subsystems can change globally, the entries here
554 * won't change, so no need for locking.
555 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700556 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400557 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700558 /* Subsystem is in this hierarchy. So we want
559 * the subsystem state from the new
560 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400561 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700562 } else {
563 /* Subsystem is not in this hierarchy, so we
564 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700565 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700566 }
567 }
568
Li Zefan0ac801f2013-01-10 11:49:27 +0800569 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700570 hash_for_each_possible(css_set_table, cset, hlist, key) {
571 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700572 continue;
573
574 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700575 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700576 }
Paul Menage817929e2007-10-18 23:39:36 -0700577
578 /* No existing cgroup group matched */
579 return NULL;
580}
581
Tejun Heo69d02062013-06-12 21:04:50 -0700582static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700583{
Tejun Heo69d02062013-06-12 21:04:50 -0700584 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700585
Tejun Heo69d02062013-06-12 21:04:50 -0700586 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
587 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700588 kfree(link);
589 }
590}
591
Tejun Heo69d02062013-06-12 21:04:50 -0700592/**
593 * allocate_cgrp_cset_links - allocate cgrp_cset_links
594 * @count: the number of links to allocate
595 * @tmp_links: list_head the allocated links are put on
596 *
597 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
598 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700599 */
Tejun Heo69d02062013-06-12 21:04:50 -0700600static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700601{
Tejun Heo69d02062013-06-12 21:04:50 -0700602 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700603 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700604
605 INIT_LIST_HEAD(tmp_links);
606
Li Zefan36553432008-07-29 22:33:19 -0700607 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700608 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700609 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700610 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700611 return -ENOMEM;
612 }
Tejun Heo69d02062013-06-12 21:04:50 -0700613 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700614 }
615 return 0;
616}
617
Li Zefanc12f65d2009-01-07 18:07:42 -0800618/**
619 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700620 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700621 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800622 * @cgrp: the destination cgroup
623 */
Tejun Heo69d02062013-06-12 21:04:50 -0700624static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
625 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800626{
Tejun Heo69d02062013-06-12 21:04:50 -0700627 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800628
Tejun Heo69d02062013-06-12 21:04:50 -0700629 BUG_ON(list_empty(tmp_links));
630 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
631 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700632 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700633 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700634 /*
635 * Always add links to the tail of the list so that the list
636 * is sorted by order of hierarchy creation
637 */
Tejun Heo69d02062013-06-12 21:04:50 -0700638 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800639}
640
Tejun Heob326f9d2013-06-24 15:21:48 -0700641/**
642 * find_css_set - return a new css_set with one cgroup updated
643 * @old_cset: the baseline css_set
644 * @cgrp: the cgroup to be updated
645 *
646 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
647 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700648 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700649static struct css_set *find_css_set(struct css_set *old_cset,
650 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700651{
Tejun Heob326f9d2013-06-24 15:21:48 -0700652 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700654 struct list_head tmp_links;
655 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800656 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700657
Tejun Heob326f9d2013-06-24 15:21:48 -0700658 lockdep_assert_held(&cgroup_mutex);
659
Paul Menage817929e2007-10-18 23:39:36 -0700660 /* First see if we already have a cgroup group that matches
661 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700662 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700663 cset = find_existing_css_set(old_cset, cgrp, template);
664 if (cset)
665 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700666 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700667
Tejun Heo5abb8852013-06-12 21:04:49 -0700668 if (cset)
669 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700670
Tejun Heof4f4be22013-06-12 21:04:51 -0700671 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700672 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700673 return NULL;
674
Tejun Heo69d02062013-06-12 21:04:50 -0700675 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700676 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700678 return NULL;
679 }
680
Tejun Heo5abb8852013-06-12 21:04:49 -0700681 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700682 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700683 INIT_LIST_HEAD(&cset->tasks);
684 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700685
686 /* Copy the set of subsystem state objects generated in
687 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700689
690 write_lock(&css_set_lock);
691 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700692 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700693 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700694
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 if (c->root == cgrp->root)
696 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700697 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700698 }
Paul Menage817929e2007-10-18 23:39:36 -0700699
Tejun Heo69d02062013-06-12 21:04:50 -0700700 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700701
Paul Menage817929e2007-10-18 23:39:36 -0700702 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700703
704 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700705 key = css_set_hash(cset->subsys);
706 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700707
Paul Menage817929e2007-10-18 23:39:36 -0700708 write_unlock(&css_set_lock);
709
Tejun Heo5abb8852013-06-12 21:04:49 -0700710 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700711}
712
Paul Menageddbcc7e2007-10-18 23:39:30 -0700713/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 * Return the cgroup for "task" from the given hierarchy. Must be
715 * called with cgroup_mutex held.
716 */
717static struct cgroup *task_cgroup_from_root(struct task_struct *task,
718 struct cgroupfs_root *root)
719{
Tejun Heo5abb8852013-06-12 21:04:49 -0700720 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 struct cgroup *res = NULL;
722
723 BUG_ON(!mutex_is_locked(&cgroup_mutex));
724 read_lock(&css_set_lock);
725 /*
726 * No need to lock the task - since we hold cgroup_mutex the
727 * task can't change groups, so the only thing that can happen
728 * is that it exits and its css is set back to init_css_set.
729 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700730 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700731 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700732 res = &root->top_cgroup;
733 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700734 struct cgrp_cset_link *link;
735
736 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700737 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700738
Paul Menage7717f7b2009-09-23 15:56:22 -0700739 if (c->root == root) {
740 res = c;
741 break;
742 }
743 }
744 }
745 read_unlock(&css_set_lock);
746 BUG_ON(!res);
747 return res;
748}
749
750/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700751 * There is one global cgroup mutex. We also require taking
752 * task_lock() when dereferencing a task's cgroup subsys pointers.
753 * See "The task_lock() exception", at the end of this comment.
754 *
755 * A task must hold cgroup_mutex to modify cgroups.
756 *
757 * Any task can increment and decrement the count field without lock.
758 * So in general, code holding cgroup_mutex can't rely on the count
759 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800760 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700761 * means that no tasks are currently attached, therefore there is no
762 * way a task attached to that cgroup can fork (the other way to
763 * increment the count). So code holding cgroup_mutex can safely
764 * assume that if the count is zero, it will stay zero. Similarly, if
765 * a task holds cgroup_mutex on a cgroup with zero count, it
766 * knows that the cgroup won't be removed, as cgroup_rmdir()
767 * needs that mutex.
768 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700769 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
770 * (usually) take cgroup_mutex. These are the two most performance
771 * critical pieces of code here. The exception occurs on cgroup_exit(),
772 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
773 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800774 * to the release agent with the name of the cgroup (path relative to
775 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 *
777 * A cgroup can only be deleted if both its 'count' of using tasks
778 * is zero, and its list of 'children' cgroups is empty. Since all
779 * tasks in the system use _some_ cgroup, and since there is always at
780 * least one task in the system (init, pid == 1), therefore, top_cgroup
781 * always has either children cgroups and/or using tasks. So we don't
782 * need a special hack to ensure that top_cgroup cannot be deleted.
783 *
784 * The task_lock() exception
785 *
786 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800787 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800788 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789 * several performance critical places that need to reference
790 * task->cgroup without the expense of grabbing a system global
791 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800792 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700793 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
794 * the task_struct routinely used for such matters.
795 *
796 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800797 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700798 */
799
Paul Menageddbcc7e2007-10-18 23:39:30 -0700800/*
801 * A couple of forward declarations required, due to cyclic reference loop:
802 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
803 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
804 * -> cgroup_mkdir.
805 */
806
Al Viro18bb1db2011-07-26 01:41:39 -0400807static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700809static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700810static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700811static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700812
813static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200814 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700815 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700816};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
Al Viroa5e7ed32011-07-26 01:55:55 -0400818static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819{
820 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821
822 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400823 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100825 inode->i_uid = current_fsuid();
826 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
828 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
829 }
830 return inode;
831}
832
Li Zefan65dff752013-03-01 15:01:56 +0800833static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
834{
835 struct cgroup_name *name;
836
837 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
838 if (!name)
839 return NULL;
840 strcpy(name->name, dentry->d_name.name);
841 return name;
842}
843
Li Zefanbe445622013-01-24 14:31:42 +0800844static void cgroup_free_fn(struct work_struct *work)
845{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700846 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800847
848 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800849 cgrp->root->number_of_cgroups--;
850 mutex_unlock(&cgroup_mutex);
851
852 /*
Li Zefan415cf072013-04-08 14:35:02 +0800853 * We get a ref to the parent's dentry, and put the ref when
854 * this cgroup is being freed, so it's guaranteed that the
855 * parent won't be destroyed before its children.
856 */
857 dput(cgrp->parent->dentry);
858
859 /*
Li Zefanbe445622013-01-24 14:31:42 +0800860 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700861 * created the cgroup. This will free cgrp->root, if we are
862 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800863 */
864 deactivate_super(cgrp->root->sb);
865
866 /*
867 * if we're getting rid of the cgroup, refcount should ensure
868 * that there are no pidlists left.
869 */
870 BUG_ON(!list_empty(&cgrp->pidlists));
871
872 simple_xattrs_free(&cgrp->xattrs);
873
Li Zefan65dff752013-03-01 15:01:56 +0800874 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800875 kfree(cgrp);
876}
877
878static void cgroup_free_rcu(struct rcu_head *head)
879{
880 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
881
Tejun Heoea15f8c2013-06-13 19:27:42 -0700882 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500883 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800884}
885
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886static void cgroup_diput(struct dentry *dentry, struct inode *inode)
887{
888 /* is dentry a directory ? if so, kfree() associated cgroup */
889 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700890 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800891
Tejun Heo54766d42013-06-12 21:04:53 -0700892 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800893 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700894 } else {
895 struct cfent *cfe = __d_cfe(dentry);
896 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
897
898 WARN_ONCE(!list_empty(&cfe->node) &&
899 cgrp != &cgrp->root->top_cgroup,
900 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700901 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700902 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903 }
904 iput(inode);
905}
906
907static void remove_dir(struct dentry *d)
908{
909 struct dentry *parent = dget(d->d_parent);
910
911 d_delete(d);
912 simple_rmdir(parent->d_inode, d);
913 dput(parent);
914}
915
Li Zefan2739d3c2013-01-21 18:18:33 +0800916static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700917{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700918 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700919
Tejun Heo05ef1d72012-04-01 12:09:56 -0700920 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
921 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100922
Li Zefan2739d3c2013-01-21 18:18:33 +0800923 /*
924 * If we're doing cleanup due to failure of cgroup_create(),
925 * the corresponding @cfe may not exist.
926 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700927 list_for_each_entry(cfe, &cgrp->files, node) {
928 struct dentry *d = cfe->dentry;
929
930 if (cft && cfe->type != cft)
931 continue;
932
933 dget(d);
934 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700935 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 list_del_init(&cfe->node);
937 dput(d);
938
Li Zefan2739d3c2013-01-21 18:18:33 +0800939 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700941}
942
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400943/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700944 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700945 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400946 * @subsys_mask: mask of the subsystem ids whose files should be removed
947 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700948static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400950 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700951 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952
Tejun Heob420ba72013-07-12 12:34:02 -0700953 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400954 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700955
956 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400957 continue;
958 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400959 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700961}
962
963/*
964 * NOTE : the dentry must have been dget()'ed
965 */
966static void cgroup_d_remove_dir(struct dentry *dentry)
967{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100968 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700969
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100970 parent = dentry->d_parent;
971 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800972 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100974 spin_unlock(&dentry->d_lock);
975 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976 remove_dir(dentry);
977}
978
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700979/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800980 * Call with cgroup_mutex held. Drops reference counts on modules, including
981 * any duplicate ones that parse_cgroupfs_options took. If this function
982 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800983 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700985 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986{
Paul Menagebd89aab2007-10-18 23:40:44 -0700987 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700988 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700989 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700990 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991
Ben Blumaae8aab2010-03-10 15:22:07 -0800992 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800993 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800994
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700996 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700997 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700999
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001000 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001001 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001002 ret = -EBUSY;
1003 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001005
1006 /* pin the module */
1007 if (!try_module_get(ss->module)) {
1008 ret = -ENOENT;
1009 goto out_put;
1010 }
1011 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 }
1013
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001014 /* subsys could be missing if unloaded between parsing and here */
1015 if (added_mask != pinned) {
1016 ret = -ENOENT;
1017 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019
Tejun Heo31261212013-06-28 17:07:30 -07001020 ret = cgroup_populate_dir(cgrp, added_mask);
1021 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001022 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001023
1024 /*
1025 * Nothing can fail from this point on. Remove files for the
1026 * removed subsystems and rebind each subsystem.
1027 */
1028 cgroup_clear_dir(cgrp, removed_mask);
1029
Tejun Heo30159ec2013-06-25 11:53:37 -07001030 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001032
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001033 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001035 BUG_ON(cgroup_css(cgrp, ss));
1036 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1037 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001038
Tejun Heo73e80ed2013-08-13 11:01:55 -04001039 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001040 cgroup_css(cgroup_dummy_top, ss));
1041 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001042
Li Zefan33a68ac2009-01-07 18:07:42 -08001043 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001044 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001046 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Ben Blumcf5d5942010-03-10 15:22:09 -08001048 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001049 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001050 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001052 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1053 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001056 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001057
Tejun Heoca8bdca2013-08-26 18:40:56 -04001058 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001059 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1060
Tejun Heo9871bf92013-06-24 15:21:47 -07001061 cgroup_subsys[i]->root = &cgroup_dummy_root;
1062 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001063
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /* subsystem is now free - drop reference on module */
1065 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001066 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 }
1068 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069
Tejun Heo1672d042013-06-25 18:04:54 -07001070 /*
1071 * Mark @root has finished binding subsystems. @root->subsys_mask
1072 * now matches the bound subsystems.
1073 */
1074 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1075
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001077
1078out_put:
1079 for_each_subsys(ss, i)
1080 if (pinned & (1 << i))
1081 module_put(ss->module);
1082 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083}
1084
Al Viro34c80b12011-12-08 21:32:45 -05001085static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086{
Al Viro34c80b12011-12-08 21:32:45 -05001087 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 struct cgroup_subsys *ss;
1089
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001090 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001091 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001093 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1094 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001095 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001097 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001098 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001099 if (strlen(root->release_agent_path))
1100 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001101 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001102 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001103 if (strlen(root->name))
1104 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001105 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 return 0;
1107}
1108
1109struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001110 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001111 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001112 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001113 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001114 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001115 /* User explicitly requested empty subsystem */
1116 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001117
1118 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001119
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120};
1121
Ben Blumaae8aab2010-03-10 15:22:07 -08001122/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001123 * Convert a hierarchy specifier into a bitmask of subsystems and
1124 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1125 * array. This function takes refcounts on subsystems to be used, unless it
1126 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001127 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001128static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001130 char *token, *o = data;
1131 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001132 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001133 struct cgroup_subsys *ss;
1134 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001135
Ben Blumaae8aab2010-03-10 15:22:07 -08001136 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1137
Li Zefanf9ab5b52009-06-17 16:26:33 -07001138#ifdef CONFIG_CPUSETS
1139 mask = ~(1UL << cpuset_subsys_id);
1140#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141
Paul Menagec6d57f32009-09-23 15:56:19 -07001142 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143
1144 while ((token = strsep(&o, ",")) != NULL) {
1145 if (!*token)
1146 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001147 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001148 /* Explicitly have no subsystems */
1149 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 continue;
1151 }
1152 if (!strcmp(token, "all")) {
1153 /* Mutually exclusive option 'all' + subsystem name */
1154 if (one_ss)
1155 return -EINVAL;
1156 all_ss = true;
1157 continue;
1158 }
Tejun Heo873fe092013-04-14 20:15:26 -07001159 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1160 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1161 continue;
1162 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001164 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 continue;
1166 }
1167 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001168 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 continue;
1170 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001171 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001172 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001173 continue;
1174 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001175 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001176 /* Specifying two release agents is forbidden */
1177 if (opts->release_agent)
1178 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001179 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001180 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001181 if (!opts->release_agent)
1182 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
1185 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001186 const char *name = token + 5;
1187 /* Can't specify an empty name */
1188 if (!strlen(name))
1189 return -EINVAL;
1190 /* Must match [\w.-]+ */
1191 for (i = 0; i < strlen(name); i++) {
1192 char c = name[i];
1193 if (isalnum(c))
1194 continue;
1195 if ((c == '.') || (c == '-') || (c == '_'))
1196 continue;
1197 return -EINVAL;
1198 }
1199 /* Specifying two names is forbidden */
1200 if (opts->name)
1201 return -EINVAL;
1202 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001203 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001204 GFP_KERNEL);
1205 if (!opts->name)
1206 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001207
1208 continue;
1209 }
1210
Tejun Heo30159ec2013-06-25 11:53:37 -07001211 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212 if (strcmp(token, ss->name))
1213 continue;
1214 if (ss->disabled)
1215 continue;
1216
1217 /* Mutually exclusive option 'all' + subsystem name */
1218 if (all_ss)
1219 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001220 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221 one_ss = true;
1222
1223 break;
1224 }
1225 if (i == CGROUP_SUBSYS_COUNT)
1226 return -ENOENT;
1227 }
1228
1229 /*
1230 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 * otherwise if 'none', 'name=' and a subsystem name options
1232 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001233 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001234 if (all_ss || (!one_ss && !opts->none && !opts->name))
1235 for_each_subsys(ss, i)
1236 if (!ss->disabled)
1237 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001238
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001239 /* Consistency checks */
1240
Tejun Heo873fe092013-04-14 20:15:26 -07001241 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1242 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1243
1244 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1245 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1246 return -EINVAL;
1247 }
1248
1249 if (opts->cpuset_clone_children) {
1250 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1251 return -EINVAL;
1252 }
1253 }
1254
Li Zefanf9ab5b52009-06-17 16:26:33 -07001255 /*
1256 * Option noprefix was introduced just for backward compatibility
1257 * with the old cpuset, so we allow noprefix only if mounting just
1258 * the cpuset subsystem.
1259 */
Tejun Heo93438622013-04-14 20:15:25 -07001260 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001261 return -EINVAL;
1262
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001263
1264 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001265 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001266 return -EINVAL;
1267
1268 /*
1269 * We either have to specify by name or by subsystems. (So all
1270 * empty hierarchies must have a name).
1271 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001272 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001273 return -EINVAL;
1274
1275 return 0;
1276}
1277
1278static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1279{
1280 int ret = 0;
1281 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001282 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001283 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001284 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001285
Tejun Heo873fe092013-04-14 20:15:26 -07001286 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1287 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1288 return -EINVAL;
1289 }
1290
Paul Menagebd89aab2007-10-18 23:40:44 -07001291 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001293 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001294
1295 /* See what subsystems are wanted */
1296 ret = parse_cgroupfs_options(data, &opts);
1297 if (ret)
1298 goto out_unlock;
1299
Tejun Heoa8a648c2013-06-24 15:21:47 -07001300 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001301 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1302 task_tgid_nr(current), current->comm);
1303
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001304 added_mask = opts.subsys_mask & ~root->subsys_mask;
1305 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001306
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001308 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001310 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1311 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1312 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001314 goto out_unlock;
1315 }
1316
Tejun Heof172e672013-06-28 17:07:30 -07001317 /* remounting is not allowed for populated hierarchies */
1318 if (root->number_of_cgroups > 1) {
1319 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001320 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001324 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
Paul Menage81a6a5c2007-10-18 23:39:38 -07001327 if (opts.release_agent)
1328 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001330 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001331 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001332 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335 return ret;
1336}
1337
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001338static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339 .statfs = simple_statfs,
1340 .drop_inode = generic_delete_inode,
1341 .show_options = cgroup_show_options,
1342 .remount_fs = cgroup_remount,
1343};
1344
Paul Menagecc31edc2008-10-18 20:28:04 -07001345static void init_cgroup_housekeeping(struct cgroup *cgrp)
1346{
1347 INIT_LIST_HEAD(&cgrp->sibling);
1348 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001349 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001350 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001351 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001352 INIT_LIST_HEAD(&cgrp->pidlists);
1353 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001354 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001355 INIT_LIST_HEAD(&cgrp->event_list);
1356 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001357 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001358}
Paul Menagec6d57f32009-09-23 15:56:19 -07001359
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360static void init_cgroup_root(struct cgroupfs_root *root)
1361{
Paul Menagebd89aab2007-10-18 23:40:44 -07001362 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001363
Paul Menageddbcc7e2007-10-18 23:39:30 -07001364 INIT_LIST_HEAD(&root->subsys_list);
1365 INIT_LIST_HEAD(&root->root_list);
1366 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001368 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001369 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001370 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371}
1372
Tejun Heofc76df72013-06-25 11:53:37 -07001373static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001374{
Tejun Heo1a574232013-04-14 11:36:58 -07001375 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001376
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001377 lockdep_assert_held(&cgroup_mutex);
1378 lockdep_assert_held(&cgroup_root_mutex);
1379
Tejun Heofc76df72013-06-25 11:53:37 -07001380 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1381 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001382 if (id < 0)
1383 return id;
1384
1385 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001386 return 0;
1387}
1388
1389static void cgroup_exit_root_id(struct cgroupfs_root *root)
1390{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001391 lockdep_assert_held(&cgroup_mutex);
1392 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001393
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001394 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001395 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001396 root->hierarchy_id = 0;
1397 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001398}
1399
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400static int cgroup_test_super(struct super_block *sb, void *data)
1401{
Paul Menagec6d57f32009-09-23 15:56:19 -07001402 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403 struct cgroupfs_root *root = sb->s_fs_info;
1404
Paul Menagec6d57f32009-09-23 15:56:19 -07001405 /* If we asked for a name then it must match */
1406 if (opts->name && strcmp(opts->name, root->name))
1407 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001408
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001409 /*
1410 * If we asked for subsystems (or explicitly for no
1411 * subsystems) then they must match
1412 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001413 if ((opts->subsys_mask || opts->none)
1414 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001415 return 0;
1416
1417 return 1;
1418}
1419
Paul Menagec6d57f32009-09-23 15:56:19 -07001420static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1421{
1422 struct cgroupfs_root *root;
1423
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001424 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001425 return NULL;
1426
1427 root = kzalloc(sizeof(*root), GFP_KERNEL);
1428 if (!root)
1429 return ERR_PTR(-ENOMEM);
1430
1431 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001432
Tejun Heo1672d042013-06-25 18:04:54 -07001433 /*
1434 * We need to set @root->subsys_mask now so that @root can be
1435 * matched by cgroup_test_super() before it finishes
1436 * initialization; otherwise, competing mounts with the same
1437 * options may try to bind the same subsystems instead of waiting
1438 * for the first one leading to unexpected mount errors.
1439 * SUBSYS_BOUND will be set once actual binding is complete.
1440 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001441 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001442 root->flags = opts->flags;
1443 if (opts->release_agent)
1444 strcpy(root->release_agent_path, opts->release_agent);
1445 if (opts->name)
1446 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001447 if (opts->cpuset_clone_children)
1448 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001449 return root;
1450}
1451
Tejun Heofa3ca072013-04-14 11:36:56 -07001452static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453{
Tejun Heofa3ca072013-04-14 11:36:56 -07001454 if (root) {
1455 /* hierarhcy ID shoulid already have been released */
1456 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001457
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001458 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001459 kfree(root);
1460 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001461}
1462
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463static int cgroup_set_super(struct super_block *sb, void *data)
1464{
1465 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 struct cgroup_sb_opts *opts = data;
1467
1468 /* If we don't have a new root, we can't set up a new sb */
1469 if (!opts->new_root)
1470 return -EINVAL;
1471
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001472 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473
1474 ret = set_anon_super(sb, NULL);
1475 if (ret)
1476 return ret;
1477
Paul Menagec6d57f32009-09-23 15:56:19 -07001478 sb->s_fs_info = opts->new_root;
1479 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480
1481 sb->s_blocksize = PAGE_CACHE_SIZE;
1482 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1483 sb->s_magic = CGROUP_SUPER_MAGIC;
1484 sb->s_op = &cgroup_ops;
1485
1486 return 0;
1487}
1488
1489static int cgroup_get_rootdir(struct super_block *sb)
1490{
Al Viro0df6a632010-12-21 13:29:29 -05001491 static const struct dentry_operations cgroup_dops = {
1492 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001493 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001494 };
1495
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496 struct inode *inode =
1497 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498
1499 if (!inode)
1500 return -ENOMEM;
1501
Paul Menageddbcc7e2007-10-18 23:39:30 -07001502 inode->i_fop = &simple_dir_operations;
1503 inode->i_op = &cgroup_dir_inode_operations;
1504 /* directories start off with i_nlink == 2 (for "." entry) */
1505 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001506 sb->s_root = d_make_root(inode);
1507 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001509 /* for everything else we want ->d_op set */
1510 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511 return 0;
1512}
1513
Al Virof7e83572010-07-26 13:23:11 +04001514static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001516 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517{
1518 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 int ret = 0;
1521 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001523 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001524 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001525 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001528 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001530 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 if (ret)
1532 goto out_err;
1533
1534 /*
1535 * Allocate a new cgroup root. We may not need it if we're
1536 * reusing an existing hierarchy.
1537 */
1538 new_root = cgroup_root_from_opts(&opts);
1539 if (IS_ERR(new_root)) {
1540 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001541 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001542 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
Paul Menagec6d57f32009-09-23 15:56:19 -07001545 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001546 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001548 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001549 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001550 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 }
1552
Paul Menagec6d57f32009-09-23 15:56:19 -07001553 root = sb->s_fs_info;
1554 BUG_ON(!root);
1555 if (root == opts.new_root) {
1556 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001557 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001558 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001559 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001560 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
1562 BUG_ON(sb->s_root != NULL);
1563
1564 ret = cgroup_get_rootdir(sb);
1565 if (ret)
1566 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001567 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
Paul Menage817929e2007-10-18 23:39:36 -07001569 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001571 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001573 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1574 0, 1, GFP_KERNEL);
1575 if (root_cgrp->id < 0)
1576 goto unlock_drop;
1577
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001578 /* Check for name clashes with existing mounts */
1579 ret = -EBUSY;
1580 if (strlen(root->name))
1581 for_each_active_root(existing_root)
1582 if (!strcmp(existing_root->name, root->name))
1583 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001584
Paul Menage817929e2007-10-18 23:39:36 -07001585 /*
1586 * We're accessing css_set_count without locking
1587 * css_set_lock here, but that's OK - it can only be
1588 * increased by someone holding cgroup_lock, and
1589 * that's us. The worst that can happen is that we
1590 * have some link structures left over
1591 */
Tejun Heo69d02062013-06-12 21:04:50 -07001592 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001593 if (ret)
1594 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001595
Tejun Heofc76df72013-06-25 11:53:37 -07001596 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1597 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001598 if (ret)
1599 goto unlock_drop;
1600
Tejun Heo31261212013-06-28 17:07:30 -07001601 sb->s_root->d_fsdata = root_cgrp;
1602 root_cgrp->dentry = sb->s_root;
1603
1604 /*
1605 * We're inside get_sb() and will call lookup_one_len() to
1606 * create the root files, which doesn't work if SELinux is
1607 * in use. The following cred dancing somehow works around
1608 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1609 * populating new cgroupfs mount") for more details.
1610 */
1611 cred = override_creds(&init_cred);
1612
Tejun Heo2bb566c2013-08-08 20:11:23 -04001613 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001614 if (ret)
1615 goto rm_base_files;
1616
Tejun Heoa8a648c2013-06-24 15:21:47 -07001617 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001618 if (ret)
1619 goto rm_base_files;
1620
1621 revert_creds(cred);
1622
Ben Blumcf5d5942010-03-10 15:22:09 -08001623 /*
1624 * There must be no failure case after here, since rebinding
1625 * takes care of subsystems' refcounts, which are explicitly
1626 * dropped in the failure exit path.
1627 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628
Tejun Heo9871bf92013-06-24 15:21:47 -07001629 list_add(&root->root_list, &cgroup_roots);
1630 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001631
Paul Menage817929e2007-10-18 23:39:36 -07001632 /* Link the top cgroup in this hierarchy into all
1633 * the css_set objects */
1634 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001635 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001636 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001637 write_unlock(&css_set_lock);
1638
Tejun Heo69d02062013-06-12 21:04:50 -07001639 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001640
Li Zefanc12f65d2009-01-07 18:07:42 -08001641 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642 BUG_ON(root->number_of_cgroups != 1);
1643
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001644 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001646 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001647 } else {
1648 /*
1649 * We re-used an existing hierarchy - the new root (if
1650 * any) is not needed
1651 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001652 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001653
Tejun Heoc7ba8282013-06-29 14:06:10 -07001654 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001655 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1656 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1657 ret = -EINVAL;
1658 goto drop_new_super;
1659 } else {
1660 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1661 }
Tejun Heo873fe092013-04-14 20:15:26 -07001662 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 }
1664
Paul Menagec6d57f32009-09-23 15:56:19 -07001665 kfree(opts.release_agent);
1666 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001667 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
Tejun Heo31261212013-06-28 17:07:30 -07001669 rm_base_files:
1670 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001671 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001672 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001673 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001674 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001675 mutex_unlock(&cgroup_root_mutex);
1676 mutex_unlock(&cgroup_mutex);
1677 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001679 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001680 out_err:
1681 kfree(opts.release_agent);
1682 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001683 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684}
1685
1686static void cgroup_kill_sb(struct super_block *sb) {
1687 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001688 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001689 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 int ret;
1691
1692 BUG_ON(!root);
1693
1694 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Tejun Heo31261212013-06-28 17:07:30 -07001697 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001699 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700
1701 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001702 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1703 ret = rebind_subsystems(root, 0, root->subsys_mask);
1704 /* Shouldn't be able to fail ... */
1705 BUG_ON(ret);
1706 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707
Paul Menage817929e2007-10-18 23:39:36 -07001708 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001709 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001710 * root cgroup
1711 */
1712 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001713
Tejun Heo69d02062013-06-12 21:04:50 -07001714 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1715 list_del(&link->cset_link);
1716 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001717 kfree(link);
1718 }
1719 write_unlock(&css_set_lock);
1720
Paul Menage839ec542009-01-29 14:25:22 -08001721 if (!list_empty(&root->root_list)) {
1722 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001723 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001724 }
Li Zefane5f6a862009-01-07 18:07:41 -08001725
Tejun Heofa3ca072013-04-14 11:36:56 -07001726 cgroup_exit_root_id(root);
1727
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001728 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001730 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001732 simple_xattrs_free(&cgrp->xattrs);
1733
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001735 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736}
1737
1738static struct file_system_type cgroup_fs_type = {
1739 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001740 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741 .kill_sb = cgroup_kill_sb,
1742};
1743
Greg KH676db4a2010-08-05 13:53:35 -07001744static struct kobject *cgroup_kobj;
1745
Li Zefana043e3b2008-02-23 15:24:09 -08001746/**
1747 * cgroup_path - generate the path of a cgroup
1748 * @cgrp: the cgroup in question
1749 * @buf: the buffer to write the path into
1750 * @buflen: the length of the buffer
1751 *
Li Zefan65dff752013-03-01 15:01:56 +08001752 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1753 *
1754 * We can't generate cgroup path using dentry->d_name, as accessing
1755 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1756 * inode's i_mutex, while on the other hand cgroup_path() can be called
1757 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001759int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760{
Li Zefan65dff752013-03-01 15:01:56 +08001761 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001763
Tejun Heoda1f2962013-04-14 10:32:19 -07001764 if (!cgrp->parent) {
1765 if (strlcpy(buf, "/", buflen) >= buflen)
1766 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 return 0;
1768 }
1769
Tao Ma316eb662012-11-08 21:36:38 +08001770 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001771 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001772
Li Zefan65dff752013-03-01 15:01:56 +08001773 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001774 do {
Li Zefan65dff752013-03-01 15:01:56 +08001775 const char *name = cgroup_name(cgrp);
1776 int len;
1777
1778 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001780 goto out;
1781 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001782
Paul Menageddbcc7e2007-10-18 23:39:30 -07001783 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001784 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001786
1787 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001788 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001789 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001791out:
1792 rcu_read_unlock();
1793 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794}
Ben Blum67523c42010-03-10 15:22:11 -08001795EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796
Tejun Heo857a2be2013-04-14 20:50:08 -07001797/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001798 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001800 * @buf: the buffer to write the path into
1801 * @buflen: the length of the buffer
1802 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001803 * Determine @task's cgroup on the first (the one with the lowest non-zero
1804 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1805 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1806 * cgroup controller callbacks.
1807 *
1808 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001809 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001810int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001811{
1812 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001813 struct cgroup *cgrp;
1814 int hierarchy_id = 1, ret = 0;
1815
1816 if (buflen < 2)
1817 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001818
1819 mutex_lock(&cgroup_mutex);
1820
Tejun Heo913ffdb2013-07-11 16:34:48 -07001821 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1822
Tejun Heo857a2be2013-04-14 20:50:08 -07001823 if (root) {
1824 cgrp = task_cgroup_from_root(task, root);
1825 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001826 } else {
1827 /* if no hierarchy exists, everyone is in "/" */
1828 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001829 }
1830
1831 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 return ret;
1833}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001834EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001835
Ben Blum74a11662011-05-26 16:25:20 -07001836/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001837 * Control Group taskset
1838 */
Tejun Heo134d3372011-12-12 18:12:21 -08001839struct task_and_cgroup {
1840 struct task_struct *task;
1841 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001842 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001843};
1844
Tejun Heo2f7ee562011-12-12 18:12:21 -08001845struct cgroup_taskset {
1846 struct task_and_cgroup single;
1847 struct flex_array *tc_array;
1848 int tc_array_len;
1849 int idx;
1850 struct cgroup *cur_cgrp;
1851};
1852
1853/**
1854 * cgroup_taskset_first - reset taskset and return the first task
1855 * @tset: taskset of interest
1856 *
1857 * @tset iteration is initialized and the first task is returned.
1858 */
1859struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1860{
1861 if (tset->tc_array) {
1862 tset->idx = 0;
1863 return cgroup_taskset_next(tset);
1864 } else {
1865 tset->cur_cgrp = tset->single.cgrp;
1866 return tset->single.task;
1867 }
1868}
1869EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1870
1871/**
1872 * cgroup_taskset_next - iterate to the next task in taskset
1873 * @tset: taskset of interest
1874 *
1875 * Return the next task in @tset. Iteration must have been initialized
1876 * with cgroup_taskset_first().
1877 */
1878struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1879{
1880 struct task_and_cgroup *tc;
1881
1882 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1883 return NULL;
1884
1885 tc = flex_array_get(tset->tc_array, tset->idx++);
1886 tset->cur_cgrp = tc->cgrp;
1887 return tc->task;
1888}
1889EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1890
1891/**
Tejun Heod99c8722013-08-08 20:11:27 -04001892 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001893 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001894 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001895 *
Tejun Heod99c8722013-08-08 20:11:27 -04001896 * Return the css for the current (last returned) task of @tset for
1897 * subsystem specified by @subsys_id. This function must be preceded by
1898 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001899 */
Tejun Heod99c8722013-08-08 20:11:27 -04001900struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1901 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001902{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001903 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001904}
Tejun Heod99c8722013-08-08 20:11:27 -04001905EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001906
1907/**
1908 * cgroup_taskset_size - return the number of tasks in taskset
1909 * @tset: taskset of interest
1910 */
1911int cgroup_taskset_size(struct cgroup_taskset *tset)
1912{
1913 return tset->tc_array ? tset->tc_array_len : 1;
1914}
1915EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1916
1917
Ben Blum74a11662011-05-26 16:25:20 -07001918/*
1919 * cgroup_task_migrate - move a task from one cgroup to another.
1920 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001921 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001922 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001923static void cgroup_task_migrate(struct cgroup *old_cgrp,
1924 struct task_struct *tsk,
1925 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001926{
Tejun Heo5abb8852013-06-12 21:04:49 -07001927 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001928
1929 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001930 * We are synchronized through threadgroup_lock() against PF_EXITING
1931 * setting such that we can't race against cgroup_exit() changing the
1932 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001933 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001934 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001935 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001936
Ben Blum74a11662011-05-26 16:25:20 -07001937 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001938 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001939 task_unlock(tsk);
1940
1941 /* Update the css_set linked lists if we're using them */
1942 write_lock(&css_set_lock);
1943 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001944 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001945 write_unlock(&css_set_lock);
1946
1947 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001948 * We just gained a reference on old_cset by taking it from the
1949 * task. As trading it for new_cset is protected by cgroup_mutex,
1950 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001951 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001952 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1953 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001954}
1955
Li Zefana043e3b2008-02-23 15:24:09 -08001956/**
Li Zefan081aa452013-03-13 09:17:09 +08001957 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001958 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001959 * @tsk: the task or the leader of the threadgroup to be attached
1960 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001961 *
Tejun Heo257058a2011-12-12 18:12:21 -08001962 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001963 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001964 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001965static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1966 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001967{
1968 int retval, i, group_size;
1969 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001970 struct cgroupfs_root *root = cgrp->root;
1971 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001972 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001973 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001974 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001975 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001976
1977 /*
1978 * step 0: in order to do expensive, possibly blocking operations for
1979 * every thread, we cannot iterate the thread group list, since it needs
1980 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001981 * group - group_rwsem prevents new threads from appearing, and if
1982 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001983 */
Li Zefan081aa452013-03-13 09:17:09 +08001984 if (threadgroup)
1985 group_size = get_nr_threads(tsk);
1986 else
1987 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001988 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001989 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001990 if (!group)
1991 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001992 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001993 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001994 if (retval)
1995 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001996
Ben Blum74a11662011-05-26 16:25:20 -07001997 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001998 /*
1999 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2000 * already PF_EXITING could be freed from underneath us unless we
2001 * take an rcu_read_lock.
2002 */
2003 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002004 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002005 struct task_and_cgroup ent;
2006
Tejun Heocd3d0952011-12-12 18:12:21 -08002007 /* @tsk either already exited or can't exit until the end */
2008 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002009 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002010
Ben Blum74a11662011-05-26 16:25:20 -07002011 /* as per above, nr_threads may decrease, but not increase. */
2012 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002013 ent.task = tsk;
2014 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002015 /* nothing to do if this task is already in the cgroup */
2016 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002017 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002018 /*
2019 * saying GFP_ATOMIC has no effect here because we did prealloc
2020 * earlier, but it's good form to communicate our expectations.
2021 */
Tejun Heo134d3372011-12-12 18:12:21 -08002022 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002023 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002024 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002025 next:
Li Zefan081aa452013-03-13 09:17:09 +08002026 if (!threadgroup)
2027 break;
Ben Blum74a11662011-05-26 16:25:20 -07002028 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002029 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002030 /* remember the number of threads in the array for later. */
2031 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002032 tset.tc_array = group;
2033 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002034
Tejun Heo134d3372011-12-12 18:12:21 -08002035 /* methods shouldn't be called if no task is actually migrating */
2036 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002037 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002038 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002039
Ben Blum74a11662011-05-26 16:25:20 -07002040 /*
2041 * step 1: check that we can legitimately attach to the cgroup.
2042 */
Tejun Heo5549c492013-06-24 15:21:48 -07002043 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002044 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002045
Ben Blum74a11662011-05-26 16:25:20 -07002046 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002047 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002048 if (retval) {
2049 failed_ss = ss;
2050 goto out_cancel_attach;
2051 }
2052 }
Ben Blum74a11662011-05-26 16:25:20 -07002053 }
2054
2055 /*
2056 * step 2: make sure css_sets exist for all threads to be migrated.
2057 * we use find_css_set, which allocates a new one if necessary.
2058 */
Ben Blum74a11662011-05-26 16:25:20 -07002059 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002060 struct css_set *old_cset;
2061
Tejun Heo134d3372011-12-12 18:12:21 -08002062 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002063 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002064 tc->cset = find_css_set(old_cset, cgrp);
2065 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002066 retval = -ENOMEM;
2067 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002068 }
2069 }
2070
2071 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002072 * step 3: now that we're guaranteed success wrt the css_sets,
2073 * proceed to move all tasks to the new cgroup. There are no
2074 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002075 */
Ben Blum74a11662011-05-26 16:25:20 -07002076 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002077 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002078 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002079 }
2080 /* nothing is sensitive to fork() after this point. */
2081
2082 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002083 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002084 */
Tejun Heo5549c492013-06-24 15:21:48 -07002085 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002086 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002087
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002089 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002090 }
2091
2092 /*
2093 * step 5: success! and cleanup
2094 */
Ben Blum74a11662011-05-26 16:25:20 -07002095 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002096out_put_css_set_refs:
2097 if (retval) {
2098 for (i = 0; i < group_size; i++) {
2099 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002100 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002101 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002102 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002103 }
Ben Blum74a11662011-05-26 16:25:20 -07002104 }
2105out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002106 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002107 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002108 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002109
Tejun Heo494c1672011-12-12 18:12:22 -08002110 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002111 break;
Ben Blum74a11662011-05-26 16:25:20 -07002112 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002113 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002114 }
2115 }
Ben Blum74a11662011-05-26 16:25:20 -07002116out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002117 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002118 return retval;
2119}
2120
2121/*
2122 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002123 * function to attach either it or all tasks in its threadgroup. Will lock
2124 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002125 */
2126static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002127{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002128 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002129 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 int ret;
2131
Ben Blum74a11662011-05-26 16:25:20 -07002132 if (!cgroup_lock_live_group(cgrp))
2133 return -ENODEV;
2134
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002135retry_find_task:
2136 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002137 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002138 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002139 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141 ret= -ESRCH;
2142 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143 }
Ben Blum74a11662011-05-26 16:25:20 -07002144 /*
2145 * even if we're attaching all tasks in the thread group, we
2146 * only need to check permissions on one of them.
2147 */
David Howellsc69e8d92008-11-14 10:39:19 +11002148 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002149 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2150 !uid_eq(cred->euid, tcred->uid) &&
2151 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002152 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 ret = -EACCES;
2154 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002156 } else
2157 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002158
2159 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002160 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002161
2162 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002163 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002164 * trapped in a cpuset, or RT worker may be born in a cgroup
2165 * with no rt_runtime allocated. Just say no.
2166 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002167 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002168 ret = -EINVAL;
2169 rcu_read_unlock();
2170 goto out_unlock_cgroup;
2171 }
2172
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 get_task_struct(tsk);
2174 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002175
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176 threadgroup_lock(tsk);
2177 if (threadgroup) {
2178 if (!thread_group_leader(tsk)) {
2179 /*
2180 * a race with de_thread from another thread's exec()
2181 * may strip us of our leadership, if this happens,
2182 * there is no choice but to throw this task away and
2183 * try again; this is
2184 * "double-double-toil-and-trouble-check locking".
2185 */
2186 threadgroup_unlock(tsk);
2187 put_task_struct(tsk);
2188 goto retry_find_task;
2189 }
Li Zefan081aa452013-03-13 09:17:09 +08002190 }
2191
2192 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2193
Tejun Heocd3d0952011-12-12 18:12:21 -08002194 threadgroup_unlock(tsk);
2195
Paul Menagebbcb81d2007-10-18 23:39:32 -07002196 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002197out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002198 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002199 return ret;
2200}
2201
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002202/**
2203 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2204 * @from: attach to all cgroups of a given task
2205 * @tsk: the task to be attached
2206 */
2207int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2208{
2209 struct cgroupfs_root *root;
2210 int retval = 0;
2211
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002214 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215
Li Zefan6f4b7e62013-07-31 16:18:36 +08002216 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002217 if (retval)
2218 break;
2219 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002220 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002221
2222 return retval;
2223}
2224EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2225
Tejun Heo182446d2013-08-08 20:11:24 -04002226static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2227 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002228{
Tejun Heo182446d2013-08-08 20:11:24 -04002229 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002230}
2231
Tejun Heo182446d2013-08-08 20:11:24 -04002232static int cgroup_procs_write(struct cgroup_subsys_state *css,
2233 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002234{
Tejun Heo182446d2013-08-08 20:11:24 -04002235 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002236}
2237
Tejun Heo182446d2013-08-08 20:11:24 -04002238static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2239 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002240{
Tejun Heo182446d2013-08-08 20:11:24 -04002241 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002242 if (strlen(buffer) >= PATH_MAX)
2243 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002244 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002245 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002246 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002247 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002248 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002249 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002250 return 0;
2251}
2252
Tejun Heo182446d2013-08-08 20:11:24 -04002253static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2254 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002255{
Tejun Heo182446d2013-08-08 20:11:24 -04002256 struct cgroup *cgrp = css->cgroup;
2257
Paul Menagee788e062008-07-25 01:46:59 -07002258 if (!cgroup_lock_live_group(cgrp))
2259 return -ENODEV;
2260 seq_puts(seq, cgrp->root->release_agent_path);
2261 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002262 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002263 return 0;
2264}
2265
Tejun Heo182446d2013-08-08 20:11:24 -04002266static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2267 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002268{
Tejun Heo182446d2013-08-08 20:11:24 -04002269 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002270 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002271}
2272
Paul Menage84eea842008-07-25 01:47:00 -07002273/* A buffer size big enough for numbers or short strings */
2274#define CGROUP_LOCAL_BUFFER_SIZE 64
2275
Tejun Heo182446d2013-08-08 20:11:24 -04002276static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2277 struct cftype *cft, struct file *file,
2278 const char __user *userbuf, size_t nbytes,
2279 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002280{
Paul Menage84eea842008-07-25 01:47:00 -07002281 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002282 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002283 char *end;
2284
2285 if (!nbytes)
2286 return -EINVAL;
2287 if (nbytes >= sizeof(buffer))
2288 return -E2BIG;
2289 if (copy_from_user(buffer, userbuf, nbytes))
2290 return -EFAULT;
2291
2292 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002293 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002294 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002295 if (*end)
2296 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002297 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002298 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002299 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002300 if (*end)
2301 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002302 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002303 }
Paul Menage355e0c42007-10-18 23:39:33 -07002304 if (!retval)
2305 retval = nbytes;
2306 return retval;
2307}
2308
Tejun Heo182446d2013-08-08 20:11:24 -04002309static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2310 struct cftype *cft, struct file *file,
2311 const char __user *userbuf, size_t nbytes,
2312 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002313{
Paul Menage84eea842008-07-25 01:47:00 -07002314 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002315 int retval = 0;
2316 size_t max_bytes = cft->max_write_len;
2317 char *buffer = local_buffer;
2318
2319 if (!max_bytes)
2320 max_bytes = sizeof(local_buffer) - 1;
2321 if (nbytes >= max_bytes)
2322 return -E2BIG;
2323 /* Allocate a dynamic buffer if we need one */
2324 if (nbytes >= sizeof(local_buffer)) {
2325 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2326 if (buffer == NULL)
2327 return -ENOMEM;
2328 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002329 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2330 retval = -EFAULT;
2331 goto out;
2332 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002333
2334 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002335 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002336 if (!retval)
2337 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002338out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002339 if (buffer != local_buffer)
2340 kfree(buffer);
2341 return retval;
2342}
2343
Paul Menageddbcc7e2007-10-18 23:39:30 -07002344static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002345 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002346{
Tejun Heo182446d2013-08-08 20:11:24 -04002347 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002348 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002349 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002350
Paul Menage355e0c42007-10-18 23:39:33 -07002351 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002352 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002353 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002354 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002355 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002356 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002357 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002358 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002359 return ret ? ret : nbytes;
2360 }
Paul Menage355e0c42007-10-18 23:39:33 -07002361 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002362}
2363
Tejun Heo182446d2013-08-08 20:11:24 -04002364static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2365 struct cftype *cft, struct file *file,
2366 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367{
Paul Menage84eea842008-07-25 01:47:00 -07002368 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002369 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2371
2372 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2373}
2374
Tejun Heo182446d2013-08-08 20:11:24 -04002375static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2376 struct cftype *cft, struct file *file,
2377 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002378{
Paul Menage84eea842008-07-25 01:47:00 -07002379 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002380 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002381 int len = sprintf(tmp, "%lld\n", (long long) val);
2382
2383 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2384}
2385
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002387 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388{
Tejun Heo182446d2013-08-08 20:11:24 -04002389 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002391 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392
2393 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002394 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002395 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002396 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002397 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002398 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399 return -EINVAL;
2400}
2401
Paul Menage91796562008-04-29 01:00:01 -07002402/*
2403 * seqfile ops/methods for returning structured data. Currently just
2404 * supports string->u64 maps, but can be extended in future.
2405 */
2406
Paul Menage91796562008-04-29 01:00:01 -07002407static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2408{
2409 struct seq_file *sf = cb->state;
2410 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2411}
2412
2413static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2414{
Li Zefane0798ce2013-07-31 17:36:25 +08002415 struct cfent *cfe = m->private;
2416 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002417 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002418
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002419 if (cft->read_map) {
2420 struct cgroup_map_cb cb = {
2421 .fill = cgroup_map_add,
2422 .state = m,
2423 };
Tejun Heo182446d2013-08-08 20:11:24 -04002424 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002425 }
Tejun Heo182446d2013-08-08 20:11:24 -04002426 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002427}
2428
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002429static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002430 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002431 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002432 .llseek = seq_lseek,
Tejun Heoe605b362013-11-27 18:16:21 -05002433 .release = cgroup_file_release,
Paul Menage91796562008-04-29 01:00:01 -07002434};
2435
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436static int cgroup_file_open(struct inode *inode, struct file *file)
2437{
Tejun Heof7d58812013-08-08 20:11:23 -04002438 struct cfent *cfe = __d_cfe(file->f_dentry);
2439 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002440 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2441 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002443
2444 err = generic_file_open(inode, file);
2445 if (err)
2446 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002447
2448 /*
2449 * If the file belongs to a subsystem, pin the css. Will be
2450 * unpinned either on open failure or release. This ensures that
2451 * @css stays alive for all file operations.
2452 */
Tejun Heo105347b2013-08-13 11:01:55 -04002453 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002454 css = cgroup_css(cgrp, cft->ss);
2455 if (cft->ss && !css_tryget(css))
2456 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002457 rcu_read_unlock();
2458
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002459 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002460 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002461
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002462 /*
2463 * @cfe->css is used by read/write/close to determine the
2464 * associated css. @file->private_data would be a better place but
2465 * that's already used by seqfile. Multiple accessors may use it
2466 * simultaneously which is okay as the association never changes.
2467 */
2468 WARN_ON_ONCE(cfe->css && cfe->css != css);
2469 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002470
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002471 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002472 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002473 err = single_open(file, cgroup_seqfile_show, cfe);
2474 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002476 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477
Tejun Heo67f4c362013-08-08 20:11:24 -04002478 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002479 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 return err;
2481}
2482
2483static int cgroup_file_release(struct inode *inode, struct file *file)
2484{
Tejun Heof7d58812013-08-08 20:11:23 -04002485 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002487 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002488 int ret = 0;
2489
Paul Menageddbcc7e2007-10-18 23:39:30 -07002490 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002491 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002492 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002493 css_put(css);
Tejun Heoe605b362013-11-27 18:16:21 -05002494 if (file->f_op == &cgroup_seqfile_operations)
2495 single_release(inode, file);
Tejun Heof7d58812013-08-08 20:11:23 -04002496 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497}
2498
2499/*
2500 * cgroup_rename - Only allow simple rename of directories in place.
2501 */
2502static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2503 struct inode *new_dir, struct dentry *new_dentry)
2504{
Li Zefan65dff752013-03-01 15:01:56 +08002505 int ret;
2506 struct cgroup_name *name, *old_name;
2507 struct cgroup *cgrp;
2508
2509 /*
2510 * It's convinient to use parent dir's i_mutex to protected
2511 * cgrp->name.
2512 */
2513 lockdep_assert_held(&old_dir->i_mutex);
2514
Paul Menageddbcc7e2007-10-18 23:39:30 -07002515 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2516 return -ENOTDIR;
2517 if (new_dentry->d_inode)
2518 return -EEXIST;
2519 if (old_dir != new_dir)
2520 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002521
2522 cgrp = __d_cgrp(old_dentry);
2523
Tejun Heo6db8e852013-06-14 11:18:22 -07002524 /*
2525 * This isn't a proper migration and its usefulness is very
2526 * limited. Disallow if sane_behavior.
2527 */
2528 if (cgroup_sane_behavior(cgrp))
2529 return -EPERM;
2530
Li Zefan65dff752013-03-01 15:01:56 +08002531 name = cgroup_alloc_name(new_dentry);
2532 if (!name)
2533 return -ENOMEM;
2534
2535 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2536 if (ret) {
2537 kfree(name);
2538 return ret;
2539 }
2540
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002541 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002542 rcu_assign_pointer(cgrp->name, name);
2543
2544 kfree_rcu(old_name, rcu_head);
2545 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546}
2547
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002548static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2549{
2550 if (S_ISDIR(dentry->d_inode->i_mode))
2551 return &__d_cgrp(dentry)->xattrs;
2552 else
Li Zefan712317a2013-04-18 23:09:52 -07002553 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002554}
2555
2556static inline int xattr_enabled(struct dentry *dentry)
2557{
2558 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002559 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002560}
2561
2562static bool is_valid_xattr(const char *name)
2563{
2564 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2565 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2566 return true;
2567 return false;
2568}
2569
2570static int cgroup_setxattr(struct dentry *dentry, const char *name,
2571 const void *val, size_t size, int flags)
2572{
2573 if (!xattr_enabled(dentry))
2574 return -EOPNOTSUPP;
2575 if (!is_valid_xattr(name))
2576 return -EINVAL;
2577 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2578}
2579
2580static int cgroup_removexattr(struct dentry *dentry, const char *name)
2581{
2582 if (!xattr_enabled(dentry))
2583 return -EOPNOTSUPP;
2584 if (!is_valid_xattr(name))
2585 return -EINVAL;
2586 return simple_xattr_remove(__d_xattrs(dentry), name);
2587}
2588
2589static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2590 void *buf, size_t size)
2591{
2592 if (!xattr_enabled(dentry))
2593 return -EOPNOTSUPP;
2594 if (!is_valid_xattr(name))
2595 return -EINVAL;
2596 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2597}
2598
2599static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2600{
2601 if (!xattr_enabled(dentry))
2602 return -EOPNOTSUPP;
2603 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2604}
2605
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002606static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002607 .read = cgroup_file_read,
2608 .write = cgroup_file_write,
2609 .llseek = generic_file_llseek,
2610 .open = cgroup_file_open,
2611 .release = cgroup_file_release,
2612};
2613
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002614static const struct inode_operations cgroup_file_inode_operations = {
2615 .setxattr = cgroup_setxattr,
2616 .getxattr = cgroup_getxattr,
2617 .listxattr = cgroup_listxattr,
2618 .removexattr = cgroup_removexattr,
2619};
2620
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002621static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002622 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002623 .mkdir = cgroup_mkdir,
2624 .rmdir = cgroup_rmdir,
2625 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002626 .setxattr = cgroup_setxattr,
2627 .getxattr = cgroup_getxattr,
2628 .listxattr = cgroup_listxattr,
2629 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002630};
2631
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002632/*
2633 * Check if a file is a control file
2634 */
2635static inline struct cftype *__file_cft(struct file *file)
2636{
Al Viro496ad9a2013-01-23 17:07:38 -05002637 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002638 return ERR_PTR(-EINVAL);
2639 return __d_cft(file->f_dentry);
2640}
2641
Al Viroa5e7ed32011-07-26 01:55:55 -04002642static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002643 struct super_block *sb)
2644{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 struct inode *inode;
2646
2647 if (!dentry)
2648 return -ENOENT;
2649 if (dentry->d_inode)
2650 return -EEXIST;
2651
2652 inode = cgroup_new_inode(mode, sb);
2653 if (!inode)
2654 return -ENOMEM;
2655
2656 if (S_ISDIR(mode)) {
2657 inode->i_op = &cgroup_dir_inode_operations;
2658 inode->i_fop = &simple_dir_operations;
2659
2660 /* start off with i_nlink == 2 (for "." entry) */
2661 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002662 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002663
Tejun Heob8a2df62012-11-19 08:13:37 -08002664 /*
2665 * Control reaches here with cgroup_mutex held.
2666 * @inode->i_mutex should nest outside cgroup_mutex but we
2667 * want to populate it immediately without releasing
2668 * cgroup_mutex. As @inode isn't visible to anyone else
2669 * yet, trylock will always succeed without affecting
2670 * lockdep checks.
2671 */
2672 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 } else if (S_ISREG(mode)) {
2674 inode->i_size = 0;
2675 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002676 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 d_instantiate(dentry, inode);
2679 dget(dentry); /* Extra count - pin the dentry in core */
2680 return 0;
2681}
2682
Li Zefan099fca32009-04-02 16:57:29 -07002683/**
2684 * cgroup_file_mode - deduce file mode of a control file
2685 * @cft: the control file in question
2686 *
2687 * returns cft->mode if ->mode is not 0
2688 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2689 * returns S_IRUGO if it has only a read handler
2690 * returns S_IWUSR if it has only a write hander
2691 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002692static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002693{
Al Viroa5e7ed32011-07-26 01:55:55 -04002694 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002695
2696 if (cft->mode)
2697 return cft->mode;
2698
2699 if (cft->read || cft->read_u64 || cft->read_s64 ||
2700 cft->read_map || cft->read_seq_string)
2701 mode |= S_IRUGO;
2702
2703 if (cft->write || cft->write_u64 || cft->write_s64 ||
2704 cft->write_string || cft->trigger)
2705 mode |= S_IWUSR;
2706
2707 return mode;
2708}
2709
Tejun Heo2bb566c2013-08-08 20:11:23 -04002710static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711{
Paul Menagebd89aab2007-10-18 23:40:44 -07002712 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002713 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002717 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002718 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002719
Tejun Heo9fa4db32013-08-26 18:40:56 -04002720 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2721 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002722 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 strcat(name, ".");
2724 }
2725 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002726
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728
2729 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2730 if (!cfe)
2731 return -ENOMEM;
2732
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002734 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002736 goto out;
2737 }
2738
Li Zefand6cbf352013-05-14 19:44:20 +08002739 cfe->type = (void *)cft;
2740 cfe->dentry = dentry;
2741 dentry->d_fsdata = cfe;
2742 simple_xattrs_init(&cfe->xattrs);
2743
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 mode = cgroup_file_mode(cft);
2745 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2746 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 list_add_tail(&cfe->node, &parent->files);
2748 cfe = NULL;
2749 }
2750 dput(dentry);
2751out:
2752 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753 return error;
2754}
2755
Tejun Heob1f28d32013-06-28 16:24:10 -07002756/**
2757 * cgroup_addrm_files - add or remove files to a cgroup directory
2758 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002759 * @cfts: array of cftypes to be added
2760 * @is_add: whether to add or remove
2761 *
2762 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002763 * For removals, this function never fails. If addition fails, this
2764 * function doesn't remove files already added. The caller is responsible
2765 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002766 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002767static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2768 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002770 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002771 int ret;
2772
2773 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2774 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002775
2776 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002777 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002778 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2779 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002780 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2781 continue;
2782 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2783 continue;
2784
Li Zefan2739d3c2013-01-21 18:18:33 +08002785 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002786 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002787 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002788 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002789 cft->name, ret);
2790 return ret;
2791 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002792 } else {
2793 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002794 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002795 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002796 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002797}
2798
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002800 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002801{
2802 /*
2803 * Thanks to the entanglement with vfs inode locking, we can't walk
2804 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002805 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2806 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002807 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808 mutex_lock(&cgroup_mutex);
2809}
2810
Tejun Heo2bb566c2013-08-08 20:11:23 -04002811static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002812 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813{
2814 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002815 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002816 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002817 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002818 struct dentry *prev = NULL;
2819 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002820 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002821 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002822 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823
2824 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002825 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002826 !atomic_inc_not_zero(&sb->s_active)) {
2827 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002828 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829 }
2830
Li Zefane8c82d22013-06-18 18:48:37 +08002831 /*
2832 * All cgroups which are created after we drop cgroup_mutex will
2833 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002834 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002835 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002836 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002837
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838 mutex_unlock(&cgroup_mutex);
2839
Li Zefane8c82d22013-06-18 18:48:37 +08002840 /* add/rm files for all cgroups created before */
2841 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002842 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002843 struct cgroup *cgrp = css->cgroup;
2844
Li Zefane8c82d22013-06-18 18:48:37 +08002845 if (cgroup_is_dead(cgrp))
2846 continue;
2847
2848 inode = cgrp->dentry->d_inode;
2849 dget(cgrp->dentry);
2850 rcu_read_unlock();
2851
2852 dput(prev);
2853 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854
2855 mutex_lock(&inode->i_mutex);
2856 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002857 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002858 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859 mutex_unlock(&cgroup_mutex);
2860 mutex_unlock(&inode->i_mutex);
2861
Li Zefane8c82d22013-06-18 18:48:37 +08002862 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002863 if (ret)
2864 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865 }
Li Zefane8c82d22013-06-18 18:48:37 +08002866 rcu_read_unlock();
2867 dput(prev);
2868 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002869 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002870}
2871
2872/**
2873 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2874 * @ss: target cgroup subsystem
2875 * @cfts: zero-length name terminated array of cftypes
2876 *
2877 * Register @cfts to @ss. Files described by @cfts are created for all
2878 * existing cgroups to which @ss is attached and all future cgroups will
2879 * have them too. This function can be called anytime whether @ss is
2880 * attached or not.
2881 *
2882 * Returns 0 on successful registration, -errno on failure. Note that this
2883 * function currently returns 0 as long as @cfts registration is successful
2884 * even if some file creation attempts on existing cgroups fail.
2885 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002886int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002887{
2888 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002889 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002890 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002891
2892 set = kzalloc(sizeof(*set), GFP_KERNEL);
2893 if (!set)
2894 return -ENOMEM;
2895
Tejun Heo2bb566c2013-08-08 20:11:23 -04002896 for (cft = cfts; cft->name[0] != '\0'; cft++)
2897 cft->ss = ss;
2898
Tejun Heo8e3f6542012-04-01 12:09:55 -07002899 cgroup_cfts_prepare();
2900 set->cfts = cfts;
2901 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002902 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002903 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002904 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002905 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002906}
2907EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2908
Li Zefana043e3b2008-02-23 15:24:09 -08002909/**
Tejun Heo79578622012-04-01 12:09:56 -07002910 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002911 * @cfts: zero-length name terminated array of cftypes
2912 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913 * Unregister @cfts. Files described by @cfts are removed from all
2914 * existing cgroups and all future cgroups won't have them either. This
2915 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002916 *
2917 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002918 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002919 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002920int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002921{
2922 struct cftype_set *set;
2923
Tejun Heo2bb566c2013-08-08 20:11:23 -04002924 if (!cfts || !cfts[0].ss)
2925 return -ENOENT;
2926
Tejun Heo79578622012-04-01 12:09:56 -07002927 cgroup_cfts_prepare();
2928
Tejun Heo2bb566c2013-08-08 20:11:23 -04002929 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002930 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002931 list_del(&set->node);
2932 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002934 return 0;
2935 }
2936 }
2937
Tejun Heo2bb566c2013-08-08 20:11:23 -04002938 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002939 return -ENOENT;
2940}
2941
2942/**
Li Zefana043e3b2008-02-23 15:24:09 -08002943 * cgroup_task_count - count the number of tasks in a cgroup.
2944 * @cgrp: the cgroup in question
2945 *
2946 * Return the number of tasks in the cgroup.
2947 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002948int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002949{
2950 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002951 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002952
Paul Menage817929e2007-10-18 23:39:36 -07002953 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002954 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2955 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002956 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002957 return count;
2958}
2959
2960/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002961 * To reduce the fork() overhead for systems that are not actually using
2962 * their cgroups capability, we don't maintain the lists running through
2963 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002964 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002966static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967{
2968 struct task_struct *p, *g;
2969 write_lock(&css_set_lock);
2970 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002971 /*
2972 * We need tasklist_lock because RCU is not safe against
2973 * while_each_thread(). Besides, a forking task that has passed
2974 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2975 * is not guaranteed to have its child immediately visible in the
2976 * tasklist if we walk through it with RCU.
2977 */
2978 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979 do_each_thread(g, p) {
2980 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002981 /*
2982 * We should check if the process is exiting, otherwise
2983 * it will race with cgroup_exit() in that the list
2984 * entry won't be deleted though the process has exited.
2985 */
2986 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002987 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002988 task_unlock(p);
2989 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002990 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991 write_unlock(&css_set_lock);
2992}
2993
Tejun Heo574bd9f2012-11-09 09:12:29 -08002994/**
Tejun Heo492eb212013-08-08 20:11:25 -04002995 * css_next_child - find the next child of a given css
2996 * @pos_css: the current position (%NULL to initiate traversal)
2997 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002998 *
Tejun Heo492eb212013-08-08 20:11:25 -04002999 * This function returns the next child of @parent_css and should be called
3000 * under RCU read lock. The only requirement is that @parent_css and
3001 * @pos_css are accessible. The next sibling is guaranteed to be returned
3002 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003003 */
Tejun Heo492eb212013-08-08 20:11:25 -04003004struct cgroup_subsys_state *
3005css_next_child(struct cgroup_subsys_state *pos_css,
3006 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003007{
Tejun Heo492eb212013-08-08 20:11:25 -04003008 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3009 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003010 struct cgroup *next;
3011
3012 WARN_ON_ONCE(!rcu_read_lock_held());
3013
3014 /*
3015 * @pos could already have been removed. Once a cgroup is removed,
3016 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003017 * changes. As CGRP_DEAD assertion is serialized and happens
3018 * before the cgroup is taken off the ->sibling list, if we see it
3019 * unasserted, it's guaranteed that the next sibling hasn't
3020 * finished its grace period even if it's already removed, and thus
3021 * safe to dereference from this RCU critical section. If
3022 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3023 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003024 *
3025 * If @pos is dead, its next pointer can't be dereferenced;
3026 * however, as each cgroup is given a monotonically increasing
3027 * unique serial number and always appended to the sibling list,
3028 * the next one can be found by walking the parent's children until
3029 * we see a cgroup with higher serial number than @pos's. While
3030 * this path can be slower, it's taken only when either the current
3031 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003032 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003033 if (!pos) {
3034 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3035 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003036 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003037 } else {
3038 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3039 if (next->serial_nr > pos->serial_nr)
3040 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003041 }
3042
Tejun Heo492eb212013-08-08 20:11:25 -04003043 if (&next->sibling == &cgrp->children)
3044 return NULL;
3045
Tejun Heoca8bdca2013-08-26 18:40:56 -04003046 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003047}
Tejun Heo492eb212013-08-08 20:11:25 -04003048EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003049
3050/**
Tejun Heo492eb212013-08-08 20:11:25 -04003051 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003052 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003053 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003054 *
Tejun Heo492eb212013-08-08 20:11:25 -04003055 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003056 * to visit for pre-order traversal of @root's descendants. @root is
3057 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003058 *
3059 * While this function requires RCU read locking, it doesn't require the
3060 * whole traversal to be contained in a single RCU critical section. This
3061 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003062 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063 */
Tejun Heo492eb212013-08-08 20:11:25 -04003064struct cgroup_subsys_state *
3065css_next_descendant_pre(struct cgroup_subsys_state *pos,
3066 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067{
Tejun Heo492eb212013-08-08 20:11:25 -04003068 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003069
3070 WARN_ON_ONCE(!rcu_read_lock_held());
3071
Tejun Heobd8815a2013-08-08 20:11:27 -04003072 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003073 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003074 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075
3076 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003077 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078 if (next)
3079 return next;
3080
3081 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003082 while (pos != root) {
3083 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003084 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003086 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003087 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088
3089 return NULL;
3090}
Tejun Heo492eb212013-08-08 20:11:25 -04003091EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003092
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003093/**
Tejun Heo492eb212013-08-08 20:11:25 -04003094 * css_rightmost_descendant - return the rightmost descendant of a css
3095 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003096 *
Tejun Heo492eb212013-08-08 20:11:25 -04003097 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3098 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003099 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003100 *
3101 * While this function requires RCU read locking, it doesn't require the
3102 * whole traversal to be contained in a single RCU critical section. This
3103 * function will return the correct rightmost descendant as long as @pos is
3104 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003105 */
Tejun Heo492eb212013-08-08 20:11:25 -04003106struct cgroup_subsys_state *
3107css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003108{
Tejun Heo492eb212013-08-08 20:11:25 -04003109 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003110
3111 WARN_ON_ONCE(!rcu_read_lock_held());
3112
3113 do {
3114 last = pos;
3115 /* ->prev isn't RCU safe, walk ->next till the end */
3116 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003117 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003118 pos = tmp;
3119 } while (pos);
3120
3121 return last;
3122}
Tejun Heo492eb212013-08-08 20:11:25 -04003123EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003124
Tejun Heo492eb212013-08-08 20:11:25 -04003125static struct cgroup_subsys_state *
3126css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003127{
Tejun Heo492eb212013-08-08 20:11:25 -04003128 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003129
3130 do {
3131 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003132 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003133 } while (pos);
3134
3135 return last;
3136}
3137
3138/**
Tejun Heo492eb212013-08-08 20:11:25 -04003139 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003140 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003141 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003142 *
Tejun Heo492eb212013-08-08 20:11:25 -04003143 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003144 * to visit for post-order traversal of @root's descendants. @root is
3145 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003146 *
3147 * While this function requires RCU read locking, it doesn't require the
3148 * whole traversal to be contained in a single RCU critical section. This
3149 * function will return the correct next descendant as long as both @pos
3150 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003151 */
Tejun Heo492eb212013-08-08 20:11:25 -04003152struct cgroup_subsys_state *
3153css_next_descendant_post(struct cgroup_subsys_state *pos,
3154 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003155{
Tejun Heo492eb212013-08-08 20:11:25 -04003156 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003157
3158 WARN_ON_ONCE(!rcu_read_lock_held());
3159
Tejun Heo58b79a92013-09-06 15:31:08 -04003160 /* if first iteration, visit leftmost descendant which may be @root */
3161 if (!pos)
3162 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003163
Tejun Heobd8815a2013-08-08 20:11:27 -04003164 /* if we visited @root, we're done */
3165 if (pos == root)
3166 return NULL;
3167
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003169 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003170 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003171 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172
3173 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003174 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003175}
Tejun Heo492eb212013-08-08 20:11:25 -04003176EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003177
Tejun Heo0942eee2013-08-08 20:11:26 -04003178/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003179 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003180 * @it: the iterator to advance
3181 *
3182 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003183 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003184static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003185{
3186 struct list_head *l = it->cset_link;
3187 struct cgrp_cset_link *link;
3188 struct css_set *cset;
3189
3190 /* Advance to the next non-empty css_set */
3191 do {
3192 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003193 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003194 it->cset_link = NULL;
3195 return;
3196 }
3197 link = list_entry(l, struct cgrp_cset_link, cset_link);
3198 cset = link->cset;
3199 } while (list_empty(&cset->tasks));
3200 it->cset_link = l;
3201 it->task = cset->tasks.next;
3202}
3203
Tejun Heo0942eee2013-08-08 20:11:26 -04003204/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003205 * css_task_iter_start - initiate task iteration
3206 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003207 * @it: the task iterator to use
3208 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003209 * Initiate iteration through the tasks of @css. The caller can call
3210 * css_task_iter_next() to walk through the tasks until the function
3211 * returns NULL. On completion of iteration, css_task_iter_end() must be
3212 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003213 *
3214 * Note that this function acquires a lock which is released when the
3215 * iteration finishes. The caller can't sleep while iteration is in
3216 * progress.
3217 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003218void css_task_iter_start(struct cgroup_subsys_state *css,
3219 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003220 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003221{
3222 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003223 * The first time anyone tries to iterate across a css, we need to
3224 * enable the list linking each css_set to its tasks, and fix up
3225 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003226 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003227 if (!use_task_css_set_links)
3228 cgroup_enable_task_cg_lists();
3229
Paul Menage817929e2007-10-18 23:39:36 -07003230 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003231
Tejun Heo72ec7022013-08-08 20:11:26 -04003232 it->origin_css = css;
3233 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003234
Tejun Heo72ec7022013-08-08 20:11:26 -04003235 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003236}
3237
Tejun Heo0942eee2013-08-08 20:11:26 -04003238/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003239 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003240 * @it: the task iterator being iterated
3241 *
3242 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003243 * initialized via css_task_iter_start(). Returns NULL when the iteration
3244 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003245 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003246struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003247{
3248 struct task_struct *res;
3249 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003250 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003251
3252 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003253 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003254 return NULL;
3255 res = list_entry(l, struct task_struct, cg_list);
3256 /* Advance iterator to find next entry */
3257 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003258 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3259 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003260 /*
3261 * We reached the end of this task list - move on to the
3262 * next cgrp_cset_link.
3263 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003265 } else {
3266 it->task = l;
3267 }
3268 return res;
3269}
3270
Tejun Heo0942eee2013-08-08 20:11:26 -04003271/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003272 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003273 * @it: the task iterator to finish
3274 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003275 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003276 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003277void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003278 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003279{
3280 read_unlock(&css_set_lock);
3281}
3282
Cliff Wickman31a7df02008-02-07 00:14:42 -08003283static inline int started_after_time(struct task_struct *t1,
3284 struct timespec *time,
3285 struct task_struct *t2)
3286{
3287 int start_diff = timespec_compare(&t1->start_time, time);
3288 if (start_diff > 0) {
3289 return 1;
3290 } else if (start_diff < 0) {
3291 return 0;
3292 } else {
3293 /*
3294 * Arbitrarily, if two processes started at the same
3295 * time, we'll say that the lower pointer value
3296 * started first. Note that t2 may have exited by now
3297 * so this may not be a valid pointer any longer, but
3298 * that's fine - it still serves to distinguish
3299 * between two tasks started (effectively) simultaneously.
3300 */
3301 return t1 > t2;
3302 }
3303}
3304
3305/*
3306 * This function is a callback from heap_insert() and is used to order
3307 * the heap.
3308 * In this case we order the heap in descending task start time.
3309 */
3310static inline int started_after(void *p1, void *p2)
3311{
3312 struct task_struct *t1 = p1;
3313 struct task_struct *t2 = p2;
3314 return started_after_time(t1, &t2->start_time, t2);
3315}
3316
3317/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003318 * css_scan_tasks - iterate though all the tasks in a css
3319 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003320 * @test: optional test callback
3321 * @process: process callback
3322 * @data: data passed to @test and @process
3323 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003324 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003325 * Iterate through all the tasks in @css, calling @test for each, and if it
3326 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003327 *
Tejun Heoe5358372013-08-08 20:11:26 -04003328 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003329 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003330 * lock css_set_lock for the call to @process.
3331 *
3332 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003333 * of @css for the duration of this call. This function may or may not
3334 * call @process for tasks that exit or move to a different css during the
3335 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003336 *
3337 * Note that @test may be called with locks held, and may in some
3338 * situations be called multiple times for the same task, so it should be
3339 * cheap.
3340 *
3341 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3342 * heap operations (and its "gt" member will be overwritten), else a
3343 * temporary heap will be used (allocation of which may cause this function
3344 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003345 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003346int css_scan_tasks(struct cgroup_subsys_state *css,
3347 bool (*test)(struct task_struct *, void *),
3348 void (*process)(struct task_struct *, void *),
3349 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350{
3351 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003352 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003353 struct task_struct *p, *dropped;
3354 /* Never dereference latest_task, since it's not refcounted */
3355 struct task_struct *latest_task = NULL;
3356 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 struct timespec latest_time = { 0, 0 };
3358
Tejun Heoe5358372013-08-08 20:11:26 -04003359 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003360 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003361 heap->gt = &started_after;
3362 } else {
3363 /* We need to allocate our own heap memory */
3364 heap = &tmp_heap;
3365 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3366 if (retval)
3367 /* cannot allocate the heap */
3368 return retval;
3369 }
3370
3371 again:
3372 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003374 * which are of interest, and invoking @process callback on the
3375 * ones which need an update. Since we don't want to hold any
3376 * locks during the task updates, gather tasks to be processed in a
3377 * heap structure. The heap is sorted by descending task start
3378 * time. If the statically-sized heap fills up, we overflow tasks
3379 * that started later, and in future iterations only consider tasks
3380 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003381 * guarantees forward progress and that we don't miss any tasks.
3382 */
3383 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003384 css_task_iter_start(css, &it);
3385 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003386 /*
3387 * Only affect tasks that qualify per the caller's callback,
3388 * if he provided one
3389 */
Tejun Heoe5358372013-08-08 20:11:26 -04003390 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003391 continue;
3392 /*
3393 * Only process tasks that started after the last task
3394 * we processed
3395 */
3396 if (!started_after_time(p, &latest_time, latest_task))
3397 continue;
3398 dropped = heap_insert(heap, p);
3399 if (dropped == NULL) {
3400 /*
3401 * The new task was inserted; the heap wasn't
3402 * previously full
3403 */
3404 get_task_struct(p);
3405 } else if (dropped != p) {
3406 /*
3407 * The new task was inserted, and pushed out a
3408 * different task
3409 */
3410 get_task_struct(p);
3411 put_task_struct(dropped);
3412 }
3413 /*
3414 * Else the new task was newer than anything already in
3415 * the heap and wasn't inserted
3416 */
3417 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003418 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419
3420 if (heap->size) {
3421 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003422 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003423 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003424 latest_time = q->start_time;
3425 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003426 }
3427 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003428 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003429 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003430 }
3431 /*
3432 * If we had to process any tasks at all, scan again
3433 * in case some of them were in the middle of forking
3434 * children that didn't get processed.
3435 * Not the most efficient way to do it, but it avoids
3436 * having to take callback_mutex in the fork path
3437 */
3438 goto again;
3439 }
3440 if (heap == &tmp_heap)
3441 heap_free(&tmp_heap);
3442 return 0;
3443}
3444
Tejun Heoe5358372013-08-08 20:11:26 -04003445static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003446{
Tejun Heoe5358372013-08-08 20:11:26 -04003447 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003448
Tejun Heo47cfcd02013-04-07 09:29:51 -07003449 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003450 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003451 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003452}
3453
3454/**
3455 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3456 * @to: cgroup to which the tasks will be moved
3457 * @from: cgroup in which the tasks currently reside
3458 */
3459int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3460{
Tejun Heo72ec7022013-08-08 20:11:26 -04003461 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3462 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003463}
3464
Paul Menage817929e2007-10-18 23:39:36 -07003465/*
Ben Blum102a7752009-09-23 15:56:26 -07003466 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003467 *
3468 * Reading this file can return large amounts of data if a cgroup has
3469 * *lots* of attached tasks. So it may need several calls to read(),
3470 * but we cannot guarantee that the information we produce is correct
3471 * unless we produce it entirely atomically.
3472 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003473 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003474
Li Zefan24528252012-01-20 11:58:43 +08003475/* which pidlist file are we talking about? */
3476enum cgroup_filetype {
3477 CGROUP_FILE_PROCS,
3478 CGROUP_FILE_TASKS,
3479};
3480
3481/*
3482 * A pidlist is a list of pids that virtually represents the contents of one
3483 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3484 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3485 * to the cgroup.
3486 */
3487struct cgroup_pidlist {
3488 /*
3489 * used to find which pidlist is wanted. doesn't change as long as
3490 * this particular list stays in the list.
3491 */
3492 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3493 /* array of xids */
3494 pid_t *list;
3495 /* how many elements the above list has */
3496 int length;
3497 /* how many files are using the current array */
3498 int use_count;
3499 /* each of these stored in a list by its cgroup */
3500 struct list_head links;
3501 /* pointer to the cgroup we belong to, for list removal purposes */
3502 struct cgroup *owner;
3503 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003504 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003505};
3506
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003508 * The following two functions "fix" the issue where there are more pids
3509 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3510 * TODO: replace with a kernel-wide solution to this problem
3511 */
3512#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3513static void *pidlist_allocate(int count)
3514{
3515 if (PIDLIST_TOO_LARGE(count))
3516 return vmalloc(count * sizeof(pid_t));
3517 else
3518 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3519}
3520static void pidlist_free(void *p)
3521{
3522 if (is_vmalloc_addr(p))
3523 vfree(p);
3524 else
3525 kfree(p);
3526}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003527
3528/*
Ben Blum102a7752009-09-23 15:56:26 -07003529 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003530 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003531 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003532static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003533{
Ben Blum102a7752009-09-23 15:56:26 -07003534 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003535
3536 /*
3537 * we presume the 0th element is unique, so i starts at 1. trivial
3538 * edge cases first; no work needs to be done for either
3539 */
3540 if (length == 0 || length == 1)
3541 return length;
3542 /* src and dest walk down the list; dest counts unique elements */
3543 for (src = 1; src < length; src++) {
3544 /* find next unique element */
3545 while (list[src] == list[src-1]) {
3546 src++;
3547 if (src == length)
3548 goto after;
3549 }
3550 /* dest always points to where the next unique element goes */
3551 list[dest] = list[src];
3552 dest++;
3553 }
3554after:
Ben Blum102a7752009-09-23 15:56:26 -07003555 return dest;
3556}
3557
3558static int cmppid(const void *a, const void *b)
3559{
3560 return *(pid_t *)a - *(pid_t *)b;
3561}
3562
3563/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003564 * find the appropriate pidlist for our purpose (given procs vs tasks)
3565 * returns with the lock on that pidlist already held, and takes care
3566 * of the use count, or returns NULL with no locks held if we're out of
3567 * memory.
3568 */
3569static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3570 enum cgroup_filetype type)
3571{
3572 struct cgroup_pidlist *l;
3573 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003574 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003575
Ben Blum72a8cb32009-09-23 15:56:27 -07003576 /*
Li Zefanb3958902013-08-01 09:52:15 +08003577 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003578 * the last ref-holder is trying to remove l from the list at the same
3579 * time. Holding the pidlist_mutex precludes somebody taking whichever
3580 * list we find out from under us - compare release_pid_array().
3581 */
3582 mutex_lock(&cgrp->pidlist_mutex);
3583 list_for_each_entry(l, &cgrp->pidlists, links) {
3584 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003585 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003586 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003587 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003588 return l;
3589 }
3590 }
3591 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003592 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003593 if (!l) {
3594 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 return l;
3596 }
Li Zefanb3958902013-08-01 09:52:15 +08003597 init_rwsem(&l->rwsem);
3598 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003599 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003600 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003601 l->owner = cgrp;
3602 list_add(&l->links, &cgrp->pidlists);
3603 mutex_unlock(&cgrp->pidlist_mutex);
3604 return l;
3605}
3606
3607/*
Ben Blum102a7752009-09-23 15:56:26 -07003608 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3609 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003610static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3611 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003612{
3613 pid_t *array;
3614 int length;
3615 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003616 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003617 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003618 struct cgroup_pidlist *l;
3619
3620 /*
3621 * If cgroup gets more users after we read count, we won't have
3622 * enough space - tough. This race is indistinguishable to the
3623 * caller from the case that the additional cgroup users didn't
3624 * show up until sometime later on.
3625 */
3626 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003627 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003628 if (!array)
3629 return -ENOMEM;
3630 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003631 css_task_iter_start(&cgrp->dummy_css, &it);
3632 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003633 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003634 break;
Ben Blum102a7752009-09-23 15:56:26 -07003635 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (type == CGROUP_FILE_PROCS)
3637 pid = task_tgid_vnr(tsk);
3638 else
3639 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003640 if (pid > 0) /* make sure to only use valid results */
3641 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003642 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003643 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003644 length = n;
3645 /* now sort & (if procs) strip out duplicates */
3646 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003648 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 l = cgroup_pidlist_find(cgrp, type);
3650 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003651 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003652 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003653 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003654 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003655 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003656 l->list = array;
3657 l->length = length;
3658 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003659 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003660 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003661 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003662}
3663
Balbir Singh846c7bb2007-10-18 23:39:44 -07003664/**
Li Zefana043e3b2008-02-23 15:24:09 -08003665 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003666 * @stats: cgroupstats to fill information into
3667 * @dentry: A dentry entry belonging to the cgroup for which stats have
3668 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003669 *
3670 * Build and fill cgroupstats so that taskstats can export it to user
3671 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003672 */
3673int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3674{
3675 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003676 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003677 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003678 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003679
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003681 * Validate dentry by checking the superblock operations,
3682 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003683 */
Li Zefan33d283b2008-11-19 15:36:48 -08003684 if (dentry->d_sb->s_op != &cgroup_ops ||
3685 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003686 goto err;
3687
3688 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003689 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690
Tejun Heo72ec7022013-08-08 20:11:26 -04003691 css_task_iter_start(&cgrp->dummy_css, &it);
3692 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003693 switch (tsk->state) {
3694 case TASK_RUNNING:
3695 stats->nr_running++;
3696 break;
3697 case TASK_INTERRUPTIBLE:
3698 stats->nr_sleeping++;
3699 break;
3700 case TASK_UNINTERRUPTIBLE:
3701 stats->nr_uninterruptible++;
3702 break;
3703 case TASK_STOPPED:
3704 stats->nr_stopped++;
3705 break;
3706 default:
3707 if (delayacct_is_task_waiting_on_io(tsk))
3708 stats->nr_io_wait++;
3709 break;
3710 }
3711 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003712 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003713
Balbir Singh846c7bb2007-10-18 23:39:44 -07003714err:
3715 return ret;
3716}
3717
Paul Menage8f3ff202009-09-23 15:56:25 -07003718
Paul Menagecc31edc2008-10-18 20:28:04 -07003719/*
Ben Blum102a7752009-09-23 15:56:26 -07003720 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003721 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003722 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003723 */
3724
Ben Blum102a7752009-09-23 15:56:26 -07003725static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003726{
3727 /*
3728 * Initially we receive a position value that corresponds to
3729 * one more than the last pid shown (or 0 on the first call or
3730 * after a seek to the start). Use a binary-search to find the
3731 * next pid to display, if any
3732 */
Ben Blum102a7752009-09-23 15:56:26 -07003733 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003734 int index = 0, pid = *pos;
3735 int *iter;
3736
Li Zefanb3958902013-08-01 09:52:15 +08003737 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003739 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003740
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 while (index < end) {
3742 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003743 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 index = mid;
3745 break;
Ben Blum102a7752009-09-23 15:56:26 -07003746 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003747 index = mid + 1;
3748 else
3749 end = mid;
3750 }
3751 }
3752 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003753 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003754 return NULL;
3755 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003756 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003757 *pos = *iter;
3758 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003759}
3760
Ben Blum102a7752009-09-23 15:56:26 -07003761static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003762{
Ben Blum102a7752009-09-23 15:56:26 -07003763 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003764 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003765}
3766
Ben Blum102a7752009-09-23 15:56:26 -07003767static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003768{
Ben Blum102a7752009-09-23 15:56:26 -07003769 struct cgroup_pidlist *l = s->private;
3770 pid_t *p = v;
3771 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003772 /*
3773 * Advance to the next pid in the array. If this goes off the
3774 * end, we're done
3775 */
3776 p++;
3777 if (p >= end) {
3778 return NULL;
3779 } else {
3780 *pos = *p;
3781 return p;
3782 }
3783}
3784
Ben Blum102a7752009-09-23 15:56:26 -07003785static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003786{
3787 return seq_printf(s, "%d\n", *(int *)v);
3788}
3789
Ben Blum102a7752009-09-23 15:56:26 -07003790/*
3791 * seq_operations functions for iterating on pidlists through seq_file -
3792 * independent of whether it's tasks or procs
3793 */
3794static const struct seq_operations cgroup_pidlist_seq_operations = {
3795 .start = cgroup_pidlist_start,
3796 .stop = cgroup_pidlist_stop,
3797 .next = cgroup_pidlist_next,
3798 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003799};
3800
Ben Blum102a7752009-09-23 15:56:26 -07003801static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003802{
Ben Blum72a8cb32009-09-23 15:56:27 -07003803 /*
3804 * the case where we're the last user of this particular pidlist will
3805 * have us remove it from the cgroup's list, which entails taking the
3806 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3807 * pidlist_mutex, we have to take pidlist_mutex first.
3808 */
3809 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003810 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003811 BUG_ON(!l->use_count);
3812 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003813 /* we're the last user if refcount is 0; remove and free */
3814 list_del(&l->links);
3815 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003816 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003817 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003818 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 kfree(l);
3820 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003821 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003822 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003823 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003824}
3825
Ben Blum102a7752009-09-23 15:56:26 -07003826static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003827{
Ben Blum102a7752009-09-23 15:56:26 -07003828 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003829 if (!(file->f_mode & FMODE_READ))
3830 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003831 /*
3832 * the seq_file will only be initialized if the file was opened for
3833 * reading; hence we check if it's not null only in that case.
3834 */
3835 l = ((struct seq_file *)file->private_data)->private;
3836 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003837 return seq_release(inode, file);
3838}
3839
Ben Blum102a7752009-09-23 15:56:26 -07003840static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003841 .read = seq_read,
3842 .llseek = seq_lseek,
3843 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003844 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003845};
3846
3847/*
Ben Blum102a7752009-09-23 15:56:26 -07003848 * The following functions handle opens on a file that displays a pidlist
3849 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3850 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003851 */
Ben Blum102a7752009-09-23 15:56:26 -07003852/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003853static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003854{
3855 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003856 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003857 int retval;
3858
3859 /* Nothing to do for write-only files */
3860 if (!(file->f_mode & FMODE_READ))
3861 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003862
Ben Blum102a7752009-09-23 15:56:26 -07003863 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003864 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003865 if (retval)
3866 return retval;
3867 /* configure file information */
3868 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003869
Ben Blum102a7752009-09-23 15:56:26 -07003870 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003871 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003872 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003873 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003874 }
Ben Blum102a7752009-09-23 15:56:26 -07003875 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003876 return 0;
3877}
Ben Blum102a7752009-09-23 15:56:26 -07003878static int cgroup_tasks_open(struct inode *unused, struct file *file)
3879{
Ben Blum72a8cb32009-09-23 15:56:27 -07003880 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003881}
3882static int cgroup_procs_open(struct inode *unused, struct file *file)
3883{
Ben Blum72a8cb32009-09-23 15:56:27 -07003884 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003885}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003886
Tejun Heo182446d2013-08-08 20:11:24 -04003887static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3888 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003889{
Tejun Heo182446d2013-08-08 20:11:24 -04003890 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003891}
3892
Tejun Heo182446d2013-08-08 20:11:24 -04003893static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3894 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003895{
Tejun Heo182446d2013-08-08 20:11:24 -04003896 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003897 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003898 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003899 else
Tejun Heo182446d2013-08-08 20:11:24 -04003900 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003901 return 0;
3902}
3903
Paul Menagebbcb81d2007-10-18 23:39:32 -07003904/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003905 * When dput() is called asynchronously, if umount has been done and
3906 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3907 * there's a small window that vfs will see the root dentry with non-zero
3908 * refcnt and trigger BUG().
3909 *
3910 * That's why we hold a reference before dput() and drop it right after.
3911 */
3912static void cgroup_dput(struct cgroup *cgrp)
3913{
3914 struct super_block *sb = cgrp->root->sb;
3915
3916 atomic_inc(&sb->s_active);
3917 dput(cgrp->dentry);
3918 deactivate_super(sb);
3919}
3920
3921/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003922 * Unregister event and free resources.
3923 *
3924 * Gets called from workqueue.
3925 */
3926static void cgroup_event_remove(struct work_struct *work)
3927{
3928 struct cgroup_event *event = container_of(work, struct cgroup_event,
3929 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003930 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003931
Li Zefan810cbee2013-02-18 18:56:14 +08003932 remove_wait_queue(event->wqh, &event->wait);
3933
Tejun Heo81eeaf02013-08-08 20:11:26 -04003934 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003935
Li Zefan810cbee2013-02-18 18:56:14 +08003936 /* Notify userspace the event is going away. */
3937 eventfd_signal(event->eventfd, 1);
3938
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003939 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003940 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003941 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003942}
3943
3944/*
3945 * Gets called on POLLHUP on eventfd when user closes it.
3946 *
3947 * Called with wqh->lock held and interrupts disabled.
3948 */
3949static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3950 int sync, void *key)
3951{
3952 struct cgroup_event *event = container_of(wait,
3953 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003954 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 unsigned long flags = (unsigned long)key;
3956
3957 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003958 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003959 * If the event has been detached at cgroup removal, we
3960 * can simply return knowing the other side will cleanup
3961 * for us.
3962 *
3963 * We can't race against event freeing since the other
3964 * side will require wqh->lock via remove_wait_queue(),
3965 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003966 */
Li Zefan810cbee2013-02-18 18:56:14 +08003967 spin_lock(&cgrp->event_list_lock);
3968 if (!list_empty(&event->list)) {
3969 list_del_init(&event->list);
3970 /*
3971 * We are in atomic context, but cgroup_event_remove()
3972 * may sleep, so we have to call it in workqueue.
3973 */
3974 schedule_work(&event->remove);
3975 }
3976 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003977 }
3978
3979 return 0;
3980}
3981
3982static void cgroup_event_ptable_queue_proc(struct file *file,
3983 wait_queue_head_t *wqh, poll_table *pt)
3984{
3985 struct cgroup_event *event = container_of(pt,
3986 struct cgroup_event, pt);
3987
3988 event->wqh = wqh;
3989 add_wait_queue(wqh, &event->wait);
3990}
3991
3992/*
3993 * Parse input and register new cgroup event handler.
3994 *
3995 * Input must be in format '<event_fd> <control_fd> <args>'.
3996 * Interpretation of args is defined by control file implementation.
3997 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04003998static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04003999 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004000{
Tejun Heo6e6eab02013-08-15 11:43:15 -04004001 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004002 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04004003 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004004 unsigned int efd, cfd;
Al Viro4e10f3c2013-08-30 12:29:49 -04004005 struct fd efile;
4006 struct fd cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004007 char *endp;
4008 int ret;
4009
4010 efd = simple_strtoul(buffer, &endp, 10);
4011 if (*endp != ' ')
4012 return -EINVAL;
4013 buffer = endp + 1;
4014
4015 cfd = simple_strtoul(buffer, &endp, 10);
4016 if ((*endp != ' ') && (*endp != '\0'))
4017 return -EINVAL;
4018 buffer = endp + 1;
4019
4020 event = kzalloc(sizeof(*event), GFP_KERNEL);
4021 if (!event)
4022 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004023
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004024 INIT_LIST_HEAD(&event->list);
4025 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4026 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4027 INIT_WORK(&event->remove, cgroup_event_remove);
4028
Al Viro4e10f3c2013-08-30 12:29:49 -04004029 efile = fdget(efd);
4030 if (!efile.file) {
4031 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004032 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033 }
4034
Al Viro4e10f3c2013-08-30 12:29:49 -04004035 event->eventfd = eventfd_ctx_fileget(efile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004036 if (IS_ERR(event->eventfd)) {
4037 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004038 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004039 }
4040
Al Viro4e10f3c2013-08-30 12:29:49 -04004041 cfile = fdget(cfd);
4042 if (!cfile.file) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004043 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004044 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004045 }
4046
4047 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004048 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro4e10f3c2013-08-30 12:29:49 -04004049 ret = inode_permission(file_inode(cfile.file), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004050 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004051 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004052
Al Viro4e10f3c2013-08-30 12:29:49 -04004053 event->cft = __file_cft(cfile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004054 if (IS_ERR(event->cft)) {
4055 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004056 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004057 }
4058
Tejun Heo6e6eab02013-08-15 11:43:15 -04004059 if (!event->cft->ss) {
4060 ret = -EBADF;
4061 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004062 }
4063
Li Zefanf1690072013-02-18 14:13:35 +08004064 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004065 * Determine the css of @cfile, verify it belongs to the same
4066 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004067 * Remaining events are automatically removed on cgroup destruction
4068 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004069 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004070 rcu_read_lock();
4071
4072 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004073 event->css = cgroup_css(cgrp, event->cft->ss);
Al Viro4e10f3c2013-08-30 12:29:49 -04004074 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004075 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004076 ret = 0;
4077
4078 rcu_read_unlock();
4079 if (ret)
4080 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004081
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004082 if (!event->cft->register_event || !event->cft->unregister_event) {
4083 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004084 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085 }
4086
Tejun Heo6e6eab02013-08-15 11:43:15 -04004087 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004088 event->eventfd, buffer);
4089 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004090 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004091
Al Viro4e10f3c2013-08-30 12:29:49 -04004092 efile.file->f_op->poll(efile.file, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004093
4094 spin_lock(&cgrp->event_list_lock);
4095 list_add(&event->list, &cgrp->event_list);
4096 spin_unlock(&cgrp->event_list_lock);
4097
Al Viro4e10f3c2013-08-30 12:29:49 -04004098 fdput(cfile);
4099 fdput(efile);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004100
4101 return 0;
4102
Tejun Heo7941cb02013-08-26 18:40:56 -04004103out_put_css:
4104 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004105out_put_cfile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004106 fdput(cfile);
Li Zefan876ede82013-08-01 09:51:47 +08004107out_put_eventfd:
4108 eventfd_ctx_put(event->eventfd);
4109out_put_efile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004110 fdput(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004111out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004112 kfree(event);
4113
4114 return ret;
4115}
4116
Tejun Heo182446d2013-08-08 20:11:24 -04004117static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4118 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004119{
Tejun Heo182446d2013-08-08 20:11:24 -04004120 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004121}
4122
Tejun Heo182446d2013-08-08 20:11:24 -04004123static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4124 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004125{
4126 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004127 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004128 else
Tejun Heo182446d2013-08-08 20:11:24 -04004129 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004130 return 0;
4131}
4132
Tejun Heod5c56ce2013-06-03 19:14:34 -07004133static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004134 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004135 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004136 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004137 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004138 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004139 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004140 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004141 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004142 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004143 .write_string = cgroup_write_event_control,
4144 .mode = S_IWUGO,
4145 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004146 {
4147 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004148 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004149 .read_u64 = cgroup_clone_children_read,
4150 .write_u64 = cgroup_clone_children_write,
4151 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004152 {
Tejun Heo873fe092013-04-14 20:15:26 -07004153 .name = "cgroup.sane_behavior",
4154 .flags = CFTYPE_ONLY_ON_ROOT,
4155 .read_seq_string = cgroup_sane_behavior_show,
4156 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004157
4158 /*
4159 * Historical crazy stuff. These don't have "cgroup." prefix and
4160 * don't exist if sane_behavior. If you're depending on these, be
4161 * prepared to be burned.
4162 */
4163 {
4164 .name = "tasks",
4165 .flags = CFTYPE_INSANE, /* use "procs" instead */
4166 .open = cgroup_tasks_open,
4167 .write_u64 = cgroup_tasks_write,
4168 .release = cgroup_pidlist_release,
4169 .mode = S_IRUGO | S_IWUSR,
4170 },
4171 {
4172 .name = "notify_on_release",
4173 .flags = CFTYPE_INSANE,
4174 .read_u64 = cgroup_read_notify_on_release,
4175 .write_u64 = cgroup_write_notify_on_release,
4176 },
Tejun Heo873fe092013-04-14 20:15:26 -07004177 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004178 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004179 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004180 .read_seq_string = cgroup_release_agent_show,
4181 .write_string = cgroup_release_agent_write,
4182 .max_write_len = PATH_MAX,
4183 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004184 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004185};
4186
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004187/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004188 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004189 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004190 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004191 *
4192 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004193 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004194static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004195{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004197 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004198
Tejun Heo8e3f6542012-04-01 12:09:55 -07004199 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004200 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004201 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004202
4203 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004204 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004205
Tejun Heobee55092013-06-28 16:24:11 -07004206 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004207 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004208 if (ret < 0)
4209 goto err;
4210 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004212 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004213err:
4214 cgroup_clear_dir(cgrp, subsys_mask);
4215 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216}
4217
Tejun Heo0c21ead2013-08-13 20:22:51 -04004218/*
4219 * css destruction is four-stage process.
4220 *
4221 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4222 * Implemented in kill_css().
4223 *
4224 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4225 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4226 * by invoking offline_css(). After offlining, the base ref is put.
4227 * Implemented in css_killed_work_fn().
4228 *
4229 * 3. When the percpu_ref reaches zero, the only possible remaining
4230 * accessors are inside RCU read sections. css_release() schedules the
4231 * RCU callback.
4232 *
4233 * 4. After the grace period, the css can be freed. Implemented in
4234 * css_free_work_fn().
4235 *
4236 * It is actually hairier because both step 2 and 4 require process context
4237 * and thus involve punting to css->destroy_work adding two additional
4238 * steps to the already complex sequence.
4239 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004240static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004241{
4242 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004243 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004244 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004245
Tejun Heo0ae78e02013-08-13 11:01:54 -04004246 if (css->parent)
4247 css_put(css->parent);
4248
Tejun Heo0c21ead2013-08-13 20:22:51 -04004249 css->ss->css_free(css);
4250 cgroup_dput(cgrp);
4251}
4252
4253static void css_free_rcu_fn(struct rcu_head *rcu_head)
4254{
4255 struct cgroup_subsys_state *css =
4256 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4257
4258 /*
4259 * css holds an extra ref to @cgrp->dentry which is put on the last
4260 * css_put(). dput() requires process context which we don't have.
4261 */
4262 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004263 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004264}
4265
Tejun Heod3daf282013-06-13 19:39:16 -07004266static void css_release(struct percpu_ref *ref)
4267{
4268 struct cgroup_subsys_state *css =
4269 container_of(ref, struct cgroup_subsys_state, refcnt);
4270
Tejun Heo0c21ead2013-08-13 20:22:51 -04004271 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004272}
4273
Tejun Heo623f9262013-08-13 11:01:55 -04004274static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4275 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276{
Paul Menagebd89aab2007-10-18 23:40:44 -07004277 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004278 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004280
Tejun Heo0ae78e02013-08-13 11:01:54 -04004281 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004282 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004283 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004285
Tejun Heoca8bdca2013-08-26 18:40:56 -04004286 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287}
4288
Li Zefan2a4ac632013-07-31 16:16:40 +08004289/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004290static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004291{
Tejun Heo623f9262013-08-13 11:01:55 -04004292 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004293 int ret = 0;
4294
Tejun Heoa31f2d32012-11-19 08:13:37 -08004295 lockdep_assert_held(&cgroup_mutex);
4296
Tejun Heo92fb9742012-11-19 08:13:38 -08004297 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004298 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004299 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004300 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004301 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004302 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4303 }
Tejun Heob1929db2012-11-19 08:13:38 -08004304 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004305}
4306
Li Zefan2a4ac632013-07-31 16:16:40 +08004307/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004308static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004309{
Tejun Heo623f9262013-08-13 11:01:55 -04004310 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004311
4312 lockdep_assert_held(&cgroup_mutex);
4313
4314 if (!(css->flags & CSS_ONLINE))
4315 return;
4316
Li Zefand7eeac12013-03-12 15:35:59 -07004317 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004318 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004319
Tejun Heoeb954192013-08-08 20:11:23 -04004320 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004321 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004322 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004323}
4324
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325/*
Li Zefana043e3b2008-02-23 15:24:09 -08004326 * cgroup_create - create a cgroup
4327 * @parent: cgroup that will be parent of the new cgroup
4328 * @dentry: dentry of the new cgroup
4329 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330 *
Li Zefana043e3b2008-02-23 15:24:09 -08004331 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004334 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335{
Tejun Heoae7f1642013-08-13 20:22:50 -04004336 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004337 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004338 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339 struct cgroupfs_root *root = parent->root;
4340 int err = 0;
4341 struct cgroup_subsys *ss;
4342 struct super_block *sb = root->sb;
4343
Tejun Heo0a950f62012-11-19 09:02:12 -08004344 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004345 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4346 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 return -ENOMEM;
4348
Li Zefan65dff752013-03-01 15:01:56 +08004349 name = cgroup_alloc_name(dentry);
4350 if (!name)
4351 goto err_free_cgrp;
4352 rcu_assign_pointer(cgrp->name, name);
4353
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004354 /*
4355 * Temporarily set the pointer to NULL, so idr_find() won't return
4356 * a half-baked cgroup.
4357 */
4358 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004359 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004360 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004361
Tejun Heo976c06b2012-11-05 09:16:59 -08004362 /*
4363 * Only live parents can have children. Note that the liveliness
4364 * check isn't strictly necessary because cgroup_mkdir() and
4365 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4366 * anyway so that locking is contained inside cgroup proper and we
4367 * don't get nasty surprises if we ever grow another caller.
4368 */
4369 if (!cgroup_lock_live_group(parent)) {
4370 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004371 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004372 }
4373
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 /* Grab a reference on the superblock so the hierarchy doesn't
4375 * get deleted on unmount if there are child cgroups. This
4376 * can be done outside cgroup_mutex, since the sb can't
4377 * disappear while someone has an open control file on the
4378 * fs */
4379 atomic_inc(&sb->s_active);
4380
Paul Menagecc31edc2008-10-18 20:28:04 -07004381 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382
Li Zefanfe1c06c2013-01-24 14:30:22 +08004383 dentry->d_fsdata = cgrp;
4384 cgrp->dentry = dentry;
4385
Paul Menagebd89aab2007-10-18 23:40:44 -07004386 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004387 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004388 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389
Li Zefanb6abdb02008-03-04 14:28:19 -08004390 if (notify_on_release(parent))
4391 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4392
Tejun Heo2260e7f2012-11-19 08:13:38 -08004393 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4394 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004395
Tejun Heo5549c492013-06-24 15:21:48 -07004396 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004397 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004398
Tejun Heoca8bdca2013-08-26 18:40:56 -04004399 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 if (IS_ERR(css)) {
4401 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004402 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004404 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004405
4406 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004407 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004408 goto err_free_all;
4409
Tejun Heo623f9262013-08-13 11:01:55 -04004410 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411 }
4412
Tejun Heo4e139af2012-11-19 08:13:36 -08004413 /*
4414 * Create directory. cgroup_create_file() returns with the new
4415 * directory locked on success so that it can be populated without
4416 * dropping cgroup_mutex.
4417 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004418 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004420 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004421 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422
Tejun Heo00356bd2013-06-18 11:14:22 -07004423 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004424
Tejun Heo4e139af2012-11-19 08:13:36 -08004425 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004426 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4427 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004428
Li Zefan415cf072013-04-08 14:35:02 +08004429 /* hold a ref to the parent's dentry */
4430 dget(parent->dentry);
4431
Tejun Heob1929db2012-11-19 08:13:38 -08004432 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004433 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004434 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004435
4436 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004437 if (err)
4438 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004439
Tejun Heo266ccd52013-12-06 15:07:32 -05004440 /* each css holds a ref to the cgroup's dentry and parent css */
4441 dget(dentry);
4442 css_get(css->parent);
4443
4444 /* mark it consumed for error path */
4445 css_ar[ss->subsys_id] = NULL;
4446
Glauber Costa1f869e82012-11-30 17:31:23 +04004447 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4448 parent->parent) {
4449 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",
4450 current->comm, current->pid, ss->name);
4451 if (!strcmp(ss->name, "memory"))
4452 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4453 ss->warned_broken_hierarchy = true;
4454 }
Tejun Heoa8638032012-11-09 09:12:29 -08004455 }
4456
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004457 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4458
Tejun Heo2bb566c2013-08-08 20:11:23 -04004459 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004460 if (err)
4461 goto err_destroy;
4462
4463 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004464 if (err)
4465 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004466
4467 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004468 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004469
4470 return 0;
4471
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004472err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004473 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004474 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004475
4476 if (css) {
4477 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004478 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004479 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004480 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004481 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482 /* Release the reference count that we took on the superblock */
4483 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004484err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004485 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004486err_free_name:
4487 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004488err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004489 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004490 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004491
4492err_destroy:
Tejun Heo266ccd52013-12-06 15:07:32 -05004493 for_each_root_subsys(root, ss) {
4494 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
4495
4496 if (css) {
4497 percpu_ref_cancel_init(&css->refcnt);
4498 ss->css_free(css);
4499 }
4500 }
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004501 cgroup_destroy_locked(cgrp);
4502 mutex_unlock(&cgroup_mutex);
4503 mutex_unlock(&dentry->d_inode->i_mutex);
4504 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004505}
4506
Al Viro18bb1db2011-07-26 01:41:39 -04004507static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004508{
4509 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4510
4511 /* the vfs holds inode->i_mutex already */
4512 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4513}
4514
Tejun Heo223dbc32013-08-13 20:22:50 -04004515/*
4516 * This is called when the refcnt of a css is confirmed to be killed.
4517 * css_tryget() is now guaranteed to fail.
4518 */
4519static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004520{
Tejun Heo223dbc32013-08-13 20:22:50 -04004521 struct cgroup_subsys_state *css =
4522 container_of(work, struct cgroup_subsys_state, destroy_work);
4523 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004524
Tejun Heof20104d2013-08-13 20:22:50 -04004525 mutex_lock(&cgroup_mutex);
4526
4527 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004528 * css_tryget() is guaranteed to fail now. Tell subsystems to
4529 * initate destruction.
4530 */
4531 offline_css(css);
4532
4533 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004534 * If @cgrp is marked dead, it's waiting for refs of all css's to
4535 * be disabled before proceeding to the second phase of cgroup
4536 * destruction. If we are the last one, kick it off.
4537 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004538 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004539 cgroup_destroy_css_killed(cgrp);
4540
4541 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004542
4543 /*
4544 * Put the css refs from kill_css(). Each css holds an extra
4545 * reference to the cgroup's dentry and cgroup removal proceeds
4546 * regardless of css refs. On the last put of each css, whenever
4547 * that may be, the extra dentry ref is put so that dentry
4548 * destruction happens only after all css's are released.
4549 */
4550 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004551}
4552
Tejun Heo223dbc32013-08-13 20:22:50 -04004553/* css kill confirmation processing requires process context, bounce */
4554static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004555{
4556 struct cgroup_subsys_state *css =
4557 container_of(ref, struct cgroup_subsys_state, refcnt);
4558
Tejun Heo223dbc32013-08-13 20:22:50 -04004559 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004560 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004561}
4562
4563/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004564 * kill_css - destroy a css
4565 * @css: css to destroy
4566 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004567 * This function initiates destruction of @css by removing cgroup interface
4568 * files and putting its base reference. ->css_offline() will be invoked
4569 * asynchronously once css_tryget() is guaranteed to fail and when the
4570 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004571 */
4572static void kill_css(struct cgroup_subsys_state *css)
4573{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004574 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4575
Tejun Heoedae0c32013-08-13 20:22:51 -04004576 /*
4577 * Killing would put the base ref, but we need to keep it alive
4578 * until after ->css_offline().
4579 */
4580 css_get(css);
4581
4582 /*
4583 * cgroup core guarantees that, by the time ->css_offline() is
4584 * invoked, no new css reference will be given out via
4585 * css_tryget(). We can't simply call percpu_ref_kill() and
4586 * proceed to offlining css's because percpu_ref_kill() doesn't
4587 * guarantee that the ref is seen as killed on all CPUs on return.
4588 *
4589 * Use percpu_ref_kill_and_confirm() to get notifications as each
4590 * css is confirmed to be seen as killed on all CPUs.
4591 */
4592 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004593}
4594
4595/**
4596 * cgroup_destroy_locked - the first stage of cgroup destruction
4597 * @cgrp: cgroup to be destroyed
4598 *
4599 * css's make use of percpu refcnts whose killing latency shouldn't be
4600 * exposed to userland and are RCU protected. Also, cgroup core needs to
4601 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4602 * invoked. To satisfy all the requirements, destruction is implemented in
4603 * the following two steps.
4604 *
4605 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4606 * userland visible parts and start killing the percpu refcnts of
4607 * css's. Set up so that the next stage will be kicked off once all
4608 * the percpu refcnts are confirmed to be killed.
4609 *
4610 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4611 * rest of destruction. Once all cgroup references are gone, the
4612 * cgroup is RCU-freed.
4613 *
4614 * This function implements s1. After this step, @cgrp is gone as far as
4615 * the userland is concerned and a new cgroup with the same name may be
4616 * created. As cgroup doesn't care about the names internally, this
4617 * doesn't cause any problem.
4618 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004619static int cgroup_destroy_locked(struct cgroup *cgrp)
4620 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621{
Tejun Heo42809dd2012-11-19 08:13:37 -08004622 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004623 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004624 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004625 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004626 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627
Tejun Heo42809dd2012-11-19 08:13:37 -08004628 lockdep_assert_held(&d->d_inode->i_mutex);
4629 lockdep_assert_held(&cgroup_mutex);
4630
Tejun Heoddd69142013-06-12 21:04:54 -07004631 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004632 * css_set_lock synchronizes access to ->cset_links and prevents
4633 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004634 */
4635 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004636 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004637 read_unlock(&css_set_lock);
4638 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004640
Tejun Heo1a90dd52012-11-05 09:16:59 -08004641 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004642 * Make sure there's no live children. We can't test ->children
4643 * emptiness as dead children linger on it while being destroyed;
4644 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4645 */
4646 empty = true;
4647 rcu_read_lock();
4648 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4649 empty = cgroup_is_dead(child);
4650 if (!empty)
4651 break;
4652 }
4653 rcu_read_unlock();
4654 if (!empty)
4655 return -EBUSY;
4656
4657 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004658 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4659 * will be invoked to perform the rest of destruction once the
4660 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004661 */
Tejun Heo266ccd52013-12-06 15:07:32 -05004662 for_each_root_subsys(cgrp->root, ss) {
4663 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
4664
4665 if (css)
4666 kill_css(css);
4667 }
Tejun Heo455050d2013-06-13 19:27:41 -07004668
4669 /*
4670 * Mark @cgrp dead. This prevents further task migration and child
4671 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004672 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004673 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004674 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004675 */
Tejun Heo54766d42013-06-12 21:04:53 -07004676 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004677
Tejun Heo455050d2013-06-13 19:27:41 -07004678 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4679 raw_spin_lock(&release_list_lock);
4680 if (!list_empty(&cgrp->release_list))
4681 list_del_init(&cgrp->release_list);
4682 raw_spin_unlock(&release_list_lock);
4683
4684 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004685 * If @cgrp has css's attached, the second stage of cgroup
4686 * destruction is kicked off from css_killed_work_fn() after the
4687 * refs of all attached css's are killed. If @cgrp doesn't have
4688 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004689 */
Tejun Heof20104d2013-08-13 20:22:50 -04004690 if (!cgrp->nr_css)
4691 cgroup_destroy_css_killed(cgrp);
4692
4693 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004694 * Clear the base files and remove @cgrp directory. The removal
4695 * puts the base ref but we aren't quite done with @cgrp yet, so
4696 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004697 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004698 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004699 dget(d);
4700 cgroup_d_remove_dir(d);
4701
4702 /*
4703 * Unregister events and notify userspace.
4704 * Notify userspace about cgroup removing only after rmdir of cgroup
4705 * directory to avoid race between userspace and kernelspace.
4706 */
4707 spin_lock(&cgrp->event_list_lock);
4708 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4709 list_del_init(&event->list);
4710 schedule_work(&event->remove);
4711 }
4712 spin_unlock(&cgrp->event_list_lock);
4713
Tejun Heoea15f8c2013-06-13 19:27:42 -07004714 return 0;
4715};
4716
Tejun Heod3daf282013-06-13 19:39:16 -07004717/**
Tejun Heof20104d2013-08-13 20:22:50 -04004718 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004719 * @work: cgroup->destroy_free_work
4720 *
4721 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004722 * destroyed after all css's are offlined and performs the rest of
4723 * destruction. This is the second step of destruction described in the
4724 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004725 */
Tejun Heof20104d2013-08-13 20:22:50 -04004726static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004727{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004728 struct cgroup *parent = cgrp->parent;
4729 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004730
Tejun Heof20104d2013-08-13 20:22:50 -04004731 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732
Paul Menage999cd8a2009-01-07 18:08:36 -08004733 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004734 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004735
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004736 /*
4737 * We should remove the cgroup object from idr before its grace
4738 * period starts, so we won't be looking up a cgroup while the
4739 * cgroup is being freed.
4740 */
4741 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4742 cgrp->id = -1;
4743
Paul Menageddbcc7e2007-10-18 23:39:30 -07004744 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004745
Paul Menagebd89aab2007-10-18 23:40:44 -07004746 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004747 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748}
4749
Tejun Heo42809dd2012-11-19 08:13:37 -08004750static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4751{
4752 int ret;
4753
4754 mutex_lock(&cgroup_mutex);
4755 ret = cgroup_destroy_locked(dentry->d_fsdata);
4756 mutex_unlock(&cgroup_mutex);
4757
4758 return ret;
4759}
4760
Tejun Heo8e3f6542012-04-01 12:09:55 -07004761static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4762{
4763 INIT_LIST_HEAD(&ss->cftsets);
4764
4765 /*
4766 * base_cftset is embedded in subsys itself, no need to worry about
4767 * deregistration.
4768 */
4769 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004770 struct cftype *cft;
4771
4772 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4773 cft->ss = ss;
4774
Tejun Heo8e3f6542012-04-01 12:09:55 -07004775 ss->base_cftset.cfts = ss->base_cftypes;
4776 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4777 }
4778}
4779
Li Zefan06a11922008-04-29 01:00:07 -07004780static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004781{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004782 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004783
4784 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004785
Tejun Heo648bb562012-11-19 08:13:36 -08004786 mutex_lock(&cgroup_mutex);
4787
Tejun Heo8e3f6542012-04-01 12:09:55 -07004788 /* init base cftset */
4789 cgroup_init_cftsets(ss);
4790
Paul Menageddbcc7e2007-10-18 23:39:30 -07004791 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004792 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4793 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004794 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004795 /* We don't handle early failures gracefully */
4796 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004797 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004798
Li Zefane8d55fd2008-04-29 01:00:13 -07004799 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004800 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004801 * newly registered, all tasks and hence the
4802 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004803 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004804
4805 need_forkexit_callback |= ss->fork || ss->exit;
4806
Li Zefane8d55fd2008-04-29 01:00:13 -07004807 /* At system boot, before all subsystems have been
4808 * registered, no tasks have been forked, so we don't
4809 * need to invoke fork callbacks here. */
4810 BUG_ON(!list_empty(&init_task.tasks));
4811
Tejun Heoae7f1642013-08-13 20:22:50 -04004812 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004813
Tejun Heo648bb562012-11-19 08:13:36 -08004814 mutex_unlock(&cgroup_mutex);
4815
Ben Blume6a11052010-03-10 15:22:09 -08004816 /* this function shouldn't be used with modular subsystems, since they
4817 * need to register a subsys_id, among other things */
4818 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004819}
4820
4821/**
Ben Blume6a11052010-03-10 15:22:09 -08004822 * cgroup_load_subsys: load and register a modular subsystem at runtime
4823 * @ss: the subsystem to load
4824 *
4825 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004826 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004827 * up for use. If the subsystem is built-in anyway, work is delegated to the
4828 * simpler cgroup_init_subsys.
4829 */
4830int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4831{
Ben Blume6a11052010-03-10 15:22:09 -08004832 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004833 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004834 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004835 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004836 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004837
4838 /* check name and function validity */
4839 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004840 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004841 return -EINVAL;
4842
4843 /*
4844 * we don't support callbacks in modular subsystems. this check is
4845 * before the ss->module check for consistency; a subsystem that could
4846 * be a module should still have no callbacks even if the user isn't
4847 * compiling it as one.
4848 */
4849 if (ss->fork || ss->exit)
4850 return -EINVAL;
4851
4852 /*
4853 * an optionally modular subsystem is built-in: we want to do nothing,
4854 * since cgroup_init_subsys will have already taken care of it.
4855 */
4856 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004857 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004858 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004859 return 0;
4860 }
4861
Tejun Heo8e3f6542012-04-01 12:09:55 -07004862 /* init base cftset */
4863 cgroup_init_cftsets(ss);
4864
Ben Blume6a11052010-03-10 15:22:09 -08004865 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004866 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004867
4868 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004869 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004870 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004871 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004872 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004873 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004874 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004875 /* failure case - need to deassign the cgroup_subsys[] slot. */
4876 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004877 mutex_unlock(&cgroup_mutex);
4878 return PTR_ERR(css);
4879 }
4880
Tejun Heo9871bf92013-06-24 15:21:47 -07004881 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4882 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004883
4884 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004885 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004886
4887 /*
4888 * Now we need to entangle the css into the existing css_sets. unlike
4889 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4890 * will need a new pointer to it; done by iterating the css_set_table.
4891 * furthermore, modifying the existing css_sets will corrupt the hash
4892 * table state, so each changed css_set will need its hash recomputed.
4893 * this is all done under the css_set_lock.
4894 */
4895 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004896 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004897 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004898 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004899 continue;
4900 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004901 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004902 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004903 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004904 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004905 key = css_set_hash(cset->subsys);
4906 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004907 }
4908 write_unlock(&css_set_lock);
4909
Tejun Heoae7f1642013-08-13 20:22:50 -04004910 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004911 if (ret)
4912 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004913
Ben Blume6a11052010-03-10 15:22:09 -08004914 /* success! */
4915 mutex_unlock(&cgroup_mutex);
4916 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004917
4918err_unload:
4919 mutex_unlock(&cgroup_mutex);
4920 /* @ss can't be mounted here as try_module_get() would fail */
4921 cgroup_unload_subsys(ss);
4922 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004923}
4924EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4925
4926/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004927 * cgroup_unload_subsys: unload a modular subsystem
4928 * @ss: the subsystem to unload
4929 *
4930 * This function should be called in a modular subsystem's exitcall. When this
4931 * function is invoked, the refcount on the subsystem's module will be 0, so
4932 * the subsystem will not be attached to any hierarchy.
4933 */
4934void cgroup_unload_subsys(struct cgroup_subsys *ss)
4935{
Tejun Heo69d02062013-06-12 21:04:50 -07004936 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004937
4938 BUG_ON(ss->module == NULL);
4939
4940 /*
4941 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004942 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004943 * doesn't start being used while we're killing it off.
4944 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004945 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004946
4947 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004948
Tejun Heoca8bdca2013-08-26 18:40:56 -04004949 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004950
Ben Blumcf5d5942010-03-10 15:22:09 -08004951 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004952 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004953
Tejun Heo9871bf92013-06-24 15:21:47 -07004954 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004955 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004956
4957 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004958 * disentangle the css from all css_sets attached to the dummy
4959 * top. as in loading, we need to pay our respects to the hashtable
4960 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004961 */
4962 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004963 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004964 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004965 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004966
Tejun Heo5abb8852013-06-12 21:04:49 -07004967 hash_del(&cset->hlist);
4968 cset->subsys[ss->subsys_id] = NULL;
4969 key = css_set_hash(cset->subsys);
4970 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004971 }
4972 write_unlock(&css_set_lock);
4973
4974 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004975 * remove subsystem's css from the cgroup_dummy_top and free it -
4976 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004977 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004978 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004979 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004980 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004981
4982 mutex_unlock(&cgroup_mutex);
4983}
4984EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4985
4986/**
Li Zefana043e3b2008-02-23 15:24:09 -08004987 * cgroup_init_early - cgroup initialization at system boot
4988 *
4989 * Initialize cgroups at system boot, and initialize any
4990 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004991 */
4992int __init cgroup_init_early(void)
4993{
Tejun Heo30159ec2013-06-25 11:53:37 -07004994 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004995 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004996
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004997 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004998 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004999 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005000 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005001 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005002 init_cgroup_root(&cgroup_dummy_root);
5003 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005004 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005005
Tejun Heo69d02062013-06-12 21:04:50 -07005006 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005007 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5008 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005009 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005010
Tejun Heo30159ec2013-06-25 11:53:37 -07005011 /* at bootup time, we don't worry about modular subsystems */
5012 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005013 BUG_ON(!ss->name);
5014 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005015 BUG_ON(!ss->css_alloc);
5016 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005017 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005018 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005019 ss->name, ss->subsys_id);
5020 BUG();
5021 }
5022
5023 if (ss->early_init)
5024 cgroup_init_subsys(ss);
5025 }
5026 return 0;
5027}
5028
5029/**
Li Zefana043e3b2008-02-23 15:24:09 -08005030 * cgroup_init - cgroup initialization
5031 *
5032 * Register cgroup filesystem and /proc file, and initialize
5033 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005034 */
5035int __init cgroup_init(void)
5036{
Tejun Heo30159ec2013-06-25 11:53:37 -07005037 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005038 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005039 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005040
5041 err = bdi_init(&cgroup_backing_dev_info);
5042 if (err)
5043 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005044
Tejun Heo30159ec2013-06-25 11:53:37 -07005045 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005046 if (!ss->early_init)
5047 cgroup_init_subsys(ss);
5048 }
5049
Tejun Heofa3ca072013-04-14 11:36:56 -07005050 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005051 mutex_lock(&cgroup_mutex);
5052 mutex_lock(&cgroup_root_mutex);
5053
Tejun Heo82fe9b02013-06-25 11:53:37 -07005054 /* Add init_css_set to the hash table */
5055 key = css_set_hash(init_css_set.subsys);
5056 hash_add(css_set_table, &init_css_set.hlist, key);
5057
Tejun Heofc76df72013-06-25 11:53:37 -07005058 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005059
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005060 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5061 0, 1, GFP_KERNEL);
5062 BUG_ON(err < 0);
5063
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005064 mutex_unlock(&cgroup_root_mutex);
5065 mutex_unlock(&cgroup_mutex);
5066
Greg KH676db4a2010-08-05 13:53:35 -07005067 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5068 if (!cgroup_kobj) {
5069 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005070 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005071 }
5072
5073 err = register_filesystem(&cgroup_fs_type);
5074 if (err < 0) {
5075 kobject_put(cgroup_kobj);
5076 goto out;
5077 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005078
Li Zefan46ae2202008-04-29 01:00:08 -07005079 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005080
Paul Menageddbcc7e2007-10-18 23:39:30 -07005081out:
Paul Menagea4243162007-10-18 23:39:35 -07005082 if (err)
5083 bdi_destroy(&cgroup_backing_dev_info);
5084
Paul Menageddbcc7e2007-10-18 23:39:30 -07005085 return err;
5086}
Paul Menageb4f48b62007-10-18 23:39:33 -07005087
Tejun Heoe5fca242013-11-22 17:14:39 -05005088static int __init cgroup_wq_init(void)
5089{
5090 /*
5091 * There isn't much point in executing destruction path in
5092 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5093 * Use 1 for @max_active.
5094 *
5095 * We would prefer to do this in cgroup_init() above, but that
5096 * is called before init_workqueues(): so leave this until after.
5097 */
5098 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
5099 BUG_ON(!cgroup_destroy_wq);
5100 return 0;
5101}
5102core_initcall(cgroup_wq_init);
5103
Paul Menagea4243162007-10-18 23:39:35 -07005104/*
5105 * proc_cgroup_show()
5106 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5107 * - Used for /proc/<pid>/cgroup.
5108 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5109 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005110 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005111 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5112 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5113 * cgroup to top_cgroup.
5114 */
5115
5116/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005117int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005118{
5119 struct pid *pid;
5120 struct task_struct *tsk;
5121 char *buf;
5122 int retval;
5123 struct cgroupfs_root *root;
5124
5125 retval = -ENOMEM;
5126 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5127 if (!buf)
5128 goto out;
5129
5130 retval = -ESRCH;
5131 pid = m->private;
5132 tsk = get_pid_task(pid, PIDTYPE_PID);
5133 if (!tsk)
5134 goto out_free;
5135
5136 retval = 0;
5137
5138 mutex_lock(&cgroup_mutex);
5139
Li Zefane5f6a862009-01-07 18:07:41 -08005140 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005141 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005142 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005143 int count = 0;
5144
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005145 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005146 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005147 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005148 if (strlen(root->name))
5149 seq_printf(m, "%sname=%s", count ? "," : "",
5150 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005151 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005152 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005153 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005154 if (retval < 0)
5155 goto out_unlock;
5156 seq_puts(m, buf);
5157 seq_putc(m, '\n');
5158 }
5159
5160out_unlock:
5161 mutex_unlock(&cgroup_mutex);
5162 put_task_struct(tsk);
5163out_free:
5164 kfree(buf);
5165out:
5166 return retval;
5167}
5168
Paul Menagea4243162007-10-18 23:39:35 -07005169/* Display information about each subsystem and each hierarchy */
5170static int proc_cgroupstats_show(struct seq_file *m, void *v)
5171{
Tejun Heo30159ec2013-06-25 11:53:37 -07005172 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005173 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005174
Paul Menage8bab8dd2008-04-04 14:29:57 -07005175 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005176 /*
5177 * ideally we don't want subsystems moving around while we do this.
5178 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5179 * subsys/hierarchy state.
5180 */
Paul Menagea4243162007-10-18 23:39:35 -07005181 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005182
5183 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005184 seq_printf(m, "%s\t%d\t%d\t%d\n",
5185 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005186 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005187
Paul Menagea4243162007-10-18 23:39:35 -07005188 mutex_unlock(&cgroup_mutex);
5189 return 0;
5190}
5191
5192static int cgroupstats_open(struct inode *inode, struct file *file)
5193{
Al Viro9dce07f2008-03-29 03:07:28 +00005194 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005195}
5196
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005197static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005198 .open = cgroupstats_open,
5199 .read = seq_read,
5200 .llseek = seq_lseek,
5201 .release = single_release,
5202};
5203
Paul Menageb4f48b62007-10-18 23:39:33 -07005204/**
5205 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005206 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005207 *
5208 * Description: A task inherits its parent's cgroup at fork().
5209 *
5210 * A pointer to the shared css_set was automatically copied in
5211 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005212 * it was not made under the protection of RCU or cgroup_mutex, so
5213 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5214 * have already changed current->cgroups, allowing the previously
5215 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005216 *
5217 * At the point that cgroup_fork() is called, 'current' is the parent
5218 * task, and the passed argument 'child' points to the child task.
5219 */
5220void cgroup_fork(struct task_struct *child)
5221{
Tejun Heo9bb71302012-10-18 17:52:07 -07005222 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005223 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005224 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005225 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005226 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005227}
5228
5229/**
Li Zefana043e3b2008-02-23 15:24:09 -08005230 * cgroup_post_fork - called on a new task after adding it to the task list
5231 * @child: the task in question
5232 *
Tejun Heo5edee612012-10-16 15:03:14 -07005233 * Adds the task to the list running through its css_set if necessary and
5234 * call the subsystem fork() callbacks. Has to be after the task is
5235 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005236 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005237 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005238 */
Paul Menage817929e2007-10-18 23:39:36 -07005239void cgroup_post_fork(struct task_struct *child)
5240{
Tejun Heo30159ec2013-06-25 11:53:37 -07005241 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005242 int i;
5243
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005244 /*
5245 * use_task_css_set_links is set to 1 before we walk the tasklist
5246 * under the tasklist_lock and we read it here after we added the child
5247 * to the tasklist under the tasklist_lock as well. If the child wasn't
5248 * yet in the tasklist when we walked through it from
5249 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5250 * should be visible now due to the paired locking and barriers implied
5251 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5252 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5253 * lock on fork.
5254 */
Paul Menage817929e2007-10-18 23:39:36 -07005255 if (use_task_css_set_links) {
5256 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005257 task_lock(child);
5258 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005259 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005260 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005261 write_unlock(&css_set_lock);
5262 }
Tejun Heo5edee612012-10-16 15:03:14 -07005263
5264 /*
5265 * Call ss->fork(). This must happen after @child is linked on
5266 * css_set; otherwise, @child might change state between ->fork()
5267 * and addition to css_set.
5268 */
5269 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005270 /*
5271 * fork/exit callbacks are supported only for builtin
5272 * subsystems, and the builtin section of the subsys
5273 * array is immutable, so we don't need to lock the
5274 * subsys array here. On the other hand, modular section
5275 * of the array can be freed at module unload, so we
5276 * can't touch that.
5277 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005278 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005279 if (ss->fork)
5280 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005281 }
Paul Menage817929e2007-10-18 23:39:36 -07005282}
Tejun Heo5edee612012-10-16 15:03:14 -07005283
Paul Menage817929e2007-10-18 23:39:36 -07005284/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005285 * cgroup_exit - detach cgroup from exiting task
5286 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005287 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005288 *
5289 * Description: Detach cgroup from @tsk and release it.
5290 *
5291 * Note that cgroups marked notify_on_release force every task in
5292 * them to take the global cgroup_mutex mutex when exiting.
5293 * This could impact scaling on very large systems. Be reluctant to
5294 * use notify_on_release cgroups where very high task exit scaling
5295 * is required on large systems.
5296 *
5297 * the_top_cgroup_hack:
5298 *
5299 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5300 *
5301 * We call cgroup_exit() while the task is still competent to
5302 * handle notify_on_release(), then leave the task attached to the
5303 * root cgroup in each hierarchy for the remainder of its exit.
5304 *
5305 * To do this properly, we would increment the reference count on
5306 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5307 * code we would add a second cgroup function call, to drop that
5308 * reference. This would just create an unnecessary hot spot on
5309 * the top_cgroup reference count, to no avail.
5310 *
5311 * Normally, holding a reference to a cgroup without bumping its
5312 * count is unsafe. The cgroup could go away, or someone could
5313 * attach us to a different cgroup, decrementing the count on
5314 * the first cgroup that we never incremented. But in this case,
5315 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005316 * which wards off any cgroup_attach_task() attempts, or task is a failed
5317 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005318 */
5319void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5320{
Tejun Heo30159ec2013-06-25 11:53:37 -07005321 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005322 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005323 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005324
5325 /*
5326 * Unlink from the css_set task list if necessary.
5327 * Optimistically check cg_list before taking
5328 * css_set_lock
5329 */
5330 if (!list_empty(&tsk->cg_list)) {
5331 write_lock(&css_set_lock);
5332 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005333 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005334 write_unlock(&css_set_lock);
5335 }
5336
Paul Menageb4f48b62007-10-18 23:39:33 -07005337 /* Reassign the task to the init_css_set. */
5338 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005339 cset = task_css_set(tsk);
5340 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005341
5342 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005343 /*
5344 * fork/exit callbacks are supported only for builtin
5345 * subsystems, see cgroup_post_fork() for details.
5346 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005347 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005348 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005349 struct cgroup_subsys_state *old_css = cset->subsys[i];
5350 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005351
Tejun Heoeb954192013-08-08 20:11:23 -04005352 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005353 }
5354 }
5355 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005356 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005357
Tejun Heo5abb8852013-06-12 21:04:49 -07005358 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005359}
Paul Menage697f4162007-10-18 23:39:34 -07005360
Paul Menagebd89aab2007-10-18 23:40:44 -07005361static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005362{
Li Zefanf50daa72013-03-01 15:06:07 +08005363 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005364 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005365 /*
5366 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005367 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005368 * it now
5369 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005370 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005371
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005372 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005373 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005374 list_empty(&cgrp->release_list)) {
5375 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005376 need_schedule_work = 1;
5377 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005378 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005379 if (need_schedule_work)
5380 schedule_work(&release_agent_work);
5381 }
5382}
5383
Paul Menage81a6a5c2007-10-18 23:39:38 -07005384/*
5385 * Notify userspace when a cgroup is released, by running the
5386 * configured release agent with the name of the cgroup (path
5387 * relative to the root of cgroup file system) as the argument.
5388 *
5389 * Most likely, this user command will try to rmdir this cgroup.
5390 *
5391 * This races with the possibility that some other task will be
5392 * attached to this cgroup before it is removed, or that some other
5393 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5394 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5395 * unused, and this cgroup will be reprieved from its death sentence,
5396 * to continue to serve a useful existence. Next time it's released,
5397 * we will get notified again, if it still has 'notify_on_release' set.
5398 *
5399 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5400 * means only wait until the task is successfully execve()'d. The
5401 * separate release agent task is forked by call_usermodehelper(),
5402 * then control in this thread returns here, without waiting for the
5403 * release agent task. We don't bother to wait because the caller of
5404 * this routine has no use for the exit status of the release agent
5405 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005406 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407static void cgroup_release_agent(struct work_struct *work)
5408{
5409 BUG_ON(work != &release_agent_work);
5410 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005411 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005412 while (!list_empty(&release_list)) {
5413 char *argv[3], *envp[3];
5414 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005415 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005416 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005417 struct cgroup,
5418 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005419 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005420 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005421 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005422 if (!pathbuf)
5423 goto continue_free;
5424 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5425 goto continue_free;
5426 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5427 if (!agentbuf)
5428 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005429
5430 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005431 argv[i++] = agentbuf;
5432 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005433 argv[i] = NULL;
5434
5435 i = 0;
5436 /* minimal command environment */
5437 envp[i++] = "HOME=/";
5438 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5439 envp[i] = NULL;
5440
5441 /* Drop the lock while we invoke the usermode helper,
5442 * since the exec could involve hitting disk and hence
5443 * be a slow process */
5444 mutex_unlock(&cgroup_mutex);
5445 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005446 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005447 continue_free:
5448 kfree(pathbuf);
5449 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005450 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005451 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005452 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005453 mutex_unlock(&cgroup_mutex);
5454}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005455
5456static int __init cgroup_disable(char *str)
5457{
Tejun Heo30159ec2013-06-25 11:53:37 -07005458 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005459 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005460 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005461
5462 while ((token = strsep(&str, ",")) != NULL) {
5463 if (!*token)
5464 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005465
Tejun Heo30159ec2013-06-25 11:53:37 -07005466 /*
5467 * cgroup_disable, being at boot time, can't know about
5468 * module subsystems, so we don't worry about them.
5469 */
5470 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005471 if (!strcmp(token, ss->name)) {
5472 ss->disabled = 1;
5473 printk(KERN_INFO "Disabling %s control group"
5474 " subsystem\n", ss->name);
5475 break;
5476 }
5477 }
5478 }
5479 return 1;
5480}
5481__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005482
Tejun Heob77d7b62013-08-13 11:01:54 -04005483/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005484 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5485 * @dentry: directory dentry of interest
5486 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005487 *
5488 * Must be called under RCU read lock. The caller is responsible for
5489 * pinning the returned css if it needs to be accessed outside the RCU
5490 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005491 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005492struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5493 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005494{
5495 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005496
Tejun Heob77d7b62013-08-13 11:01:54 -04005497 WARN_ON_ONCE(!rcu_read_lock_held());
5498
Tejun Heo35cf0832013-08-26 18:40:56 -04005499 /* is @dentry a cgroup dir? */
5500 if (!dentry->d_inode ||
5501 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005502 return ERR_PTR(-EBADF);
5503
Tejun Heo35cf0832013-08-26 18:40:56 -04005504 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005505 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005506}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005507
Li Zefan1cb650b2013-08-19 10:05:24 +08005508/**
5509 * css_from_id - lookup css by id
5510 * @id: the cgroup id
5511 * @ss: cgroup subsys to be looked into
5512 *
5513 * Returns the css if there's valid one with @id, otherwise returns NULL.
5514 * Should be called under rcu_read_lock().
5515 */
5516struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5517{
5518 struct cgroup *cgrp;
5519
5520 rcu_lockdep_assert(rcu_read_lock_held() ||
5521 lockdep_is_held(&cgroup_mutex),
5522 "css_from_id() needs proper protection");
5523
5524 cgrp = idr_find(&ss->root->cgroup_idr, id);
5525 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005526 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005527 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005528}
5529
Paul Menagefe693432009-09-23 15:56:20 -07005530#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005531static struct cgroup_subsys_state *
5532debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005533{
5534 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5535
5536 if (!css)
5537 return ERR_PTR(-ENOMEM);
5538
5539 return css;
5540}
5541
Tejun Heoeb954192013-08-08 20:11:23 -04005542static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005543{
Tejun Heoeb954192013-08-08 20:11:23 -04005544 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005545}
5546
Tejun Heo182446d2013-08-08 20:11:24 -04005547static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5548 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005549{
Tejun Heo182446d2013-08-08 20:11:24 -04005550 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005551}
5552
Tejun Heo182446d2013-08-08 20:11:24 -04005553static u64 current_css_set_read(struct cgroup_subsys_state *css,
5554 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005555{
5556 return (u64)(unsigned long)current->cgroups;
5557}
5558
Tejun Heo182446d2013-08-08 20:11:24 -04005559static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005560 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005561{
5562 u64 count;
5563
5564 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005565 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005566 rcu_read_unlock();
5567 return count;
5568}
5569
Tejun Heo182446d2013-08-08 20:11:24 -04005570static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005571 struct cftype *cft,
5572 struct seq_file *seq)
5573{
Tejun Heo69d02062013-06-12 21:04:50 -07005574 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005575 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005576
5577 read_lock(&css_set_lock);
5578 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005579 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005580 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005581 struct cgroup *c = link->cgrp;
5582 const char *name;
5583
5584 if (c->dentry)
5585 name = c->dentry->d_name.name;
5586 else
5587 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005588 seq_printf(seq, "Root %d group %s\n",
5589 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005590 }
5591 rcu_read_unlock();
5592 read_unlock(&css_set_lock);
5593 return 0;
5594}
5595
5596#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005597static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5598 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005599{
Tejun Heo69d02062013-06-12 21:04:50 -07005600 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005601
5602 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005603 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005604 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005605 struct task_struct *task;
5606 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005607 seq_printf(seq, "css_set %p\n", cset);
5608 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005609 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5610 seq_puts(seq, " ...\n");
5611 break;
5612 } else {
5613 seq_printf(seq, " task %d\n",
5614 task_pid_vnr(task));
5615 }
5616 }
5617 }
5618 read_unlock(&css_set_lock);
5619 return 0;
5620}
5621
Tejun Heo182446d2013-08-08 20:11:24 -04005622static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005623{
Tejun Heo182446d2013-08-08 20:11:24 -04005624 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005625}
5626
5627static struct cftype debug_files[] = {
5628 {
Paul Menagefe693432009-09-23 15:56:20 -07005629 .name = "taskcount",
5630 .read_u64 = debug_taskcount_read,
5631 },
5632
5633 {
5634 .name = "current_css_set",
5635 .read_u64 = current_css_set_read,
5636 },
5637
5638 {
5639 .name = "current_css_set_refcount",
5640 .read_u64 = current_css_set_refcount_read,
5641 },
5642
5643 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005644 .name = "current_css_set_cg_links",
5645 .read_seq_string = current_css_set_cg_links_read,
5646 },
5647
5648 {
5649 .name = "cgroup_css_links",
5650 .read_seq_string = cgroup_css_links_read,
5651 },
5652
5653 {
Paul Menagefe693432009-09-23 15:56:20 -07005654 .name = "releasable",
5655 .read_u64 = releasable_read,
5656 },
Paul Menagefe693432009-09-23 15:56:20 -07005657
Tejun Heo4baf6e32012-04-01 12:09:55 -07005658 { } /* terminate */
5659};
Paul Menagefe693432009-09-23 15:56:20 -07005660
5661struct cgroup_subsys debug_subsys = {
5662 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005663 .css_alloc = debug_css_alloc,
5664 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005665 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005666 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005667};
5668#endif /* CONFIG_CGROUP_DEBUG */