blob: a7b98ee35ef7249c886d9ca77e729ef9cf680bef [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 Heo42809dd2012-11-19 08:13:37 -0800202
Tejun Heo95109b62013-08-08 20:11:27 -0400203/**
204 * cgroup_css - obtain a cgroup's css for the specified subsystem
205 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400206 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400207 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400208 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
209 * function must be called either under cgroup_mutex or rcu_read_lock() and
210 * the caller is responsible for pinning the returned css if it wants to
211 * keep accessing it outside the said locks. This function may return
212 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400213 */
214static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400215 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400216{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400217 if (ss)
218 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
219 lockdep_is_held(&cgroup_mutex));
220 else
221 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400222}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223
Paul Menageddbcc7e2007-10-18 23:39:30 -0700224/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700225static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700226{
Tejun Heo54766d42013-06-12 21:04:53 -0700227 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700228}
229
Li Zefan78574cf2013-04-08 19:00:38 -0700230/**
231 * cgroup_is_descendant - test ancestry
232 * @cgrp: the cgroup to be tested
233 * @ancestor: possible ancestor of @cgrp
234 *
235 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
236 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
237 * and @ancestor are accessible.
238 */
239bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
240{
241 while (cgrp) {
242 if (cgrp == ancestor)
243 return true;
244 cgrp = cgrp->parent;
245 }
246 return false;
247}
248EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249
Adrian Bunke9685a02008-02-07 00:13:46 -0800250static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700251{
252 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700253 (1 << CGRP_RELEASABLE) |
254 (1 << CGRP_NOTIFY_ON_RELEASE);
255 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700256}
257
Adrian Bunke9685a02008-02-07 00:13:46 -0800258static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700259{
Paul Menagebd89aab2007-10-18 23:40:44 -0700260 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700261}
262
Tejun Heo30159ec2013-06-25 11:53:37 -0700263/**
264 * for_each_subsys - iterate all loaded cgroup subsystems
265 * @ss: the iteration cursor
266 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
267 *
268 * Should be called under cgroup_mutex.
269 */
270#define for_each_subsys(ss, i) \
271 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
272 if (({ lockdep_assert_held(&cgroup_mutex); \
273 !((ss) = cgroup_subsys[i]); })) { } \
274 else
275
276/**
277 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
278 * @ss: the iteration cursor
279 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
280 *
281 * Bulit-in subsystems are always present and iteration itself doesn't
282 * require any synchronization.
283 */
284#define for_each_builtin_subsys(ss, i) \
285 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
286 (((ss) = cgroup_subsys[i]) || true); (i)++)
287
Tejun Heo5549c492013-06-24 15:21:48 -0700288/* iterate each subsystem attached to a hierarchy */
289#define for_each_root_subsys(root, ss) \
290 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700291
Tejun Heo5549c492013-06-24 15:21:48 -0700292/* iterate across the active hierarchies */
293#define for_each_active_root(root) \
294 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700295
Tejun Heof6ea9372012-04-01 12:09:55 -0700296static inline struct cgroup *__d_cgrp(struct dentry *dentry)
297{
298 return dentry->d_fsdata;
299}
300
Tejun Heo05ef1d72012-04-01 12:09:56 -0700301static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700302{
303 return dentry->d_fsdata;
304}
305
Tejun Heo05ef1d72012-04-01 12:09:56 -0700306static inline struct cftype *__d_cft(struct dentry *dentry)
307{
308 return __d_cfe(dentry)->type;
309}
310
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700311/**
312 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
313 * @cgrp: the cgroup to be checked for liveness
314 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700315 * On success, returns true; the mutex should be later unlocked. On
316 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700317 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700318static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700319{
320 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700321 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700322 mutex_unlock(&cgroup_mutex);
323 return false;
324 }
325 return true;
326}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700327
Paul Menage81a6a5c2007-10-18 23:39:38 -0700328/* the list of cgroups eligible for automatic release. Protected by
329 * release_list_lock */
330static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200331static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700332static void cgroup_release_agent(struct work_struct *work);
333static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700334static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700335
Tejun Heo69d02062013-06-12 21:04:50 -0700336/*
337 * A cgroup can be associated with multiple css_sets as different tasks may
338 * belong to different cgroups on different hierarchies. In the other
339 * direction, a css_set is naturally associated with multiple cgroups.
340 * This M:N relationship is represented by the following link structure
341 * which exists for each association and allows traversing the associations
342 * from both sides.
343 */
344struct cgrp_cset_link {
345 /* the cgroup and css_set this link associates */
346 struct cgroup *cgrp;
347 struct css_set *cset;
348
349 /* list of cgrp_cset_links anchored at cgrp->cset_links */
350 struct list_head cset_link;
351
352 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
353 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700354};
355
356/* The default css_set - used by init and its children prior to any
357 * hierarchies being mounted. It contains a pointer to the root state
358 * for each subsystem. Also used to anchor the list of css_sets. Not
359 * reference-counted, to improve performance when child cgroups
360 * haven't been created.
361 */
362
363static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700364static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700365
Tejun Heo0942eee2013-08-08 20:11:26 -0400366/*
367 * css_set_lock protects the list of css_set objects, and the chain of
368 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400369 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400370 */
Paul Menage817929e2007-10-18 23:39:36 -0700371static DEFINE_RWLOCK(css_set_lock);
372static int css_set_count;
373
Paul Menage7717f7b2009-09-23 15:56:22 -0700374/*
375 * hash table for cgroup groups. This improves the performance to find
376 * an existing css_set. This hash doesn't (currently) take into
377 * account cgroups in empty hierarchies.
378 */
Li Zefan472b1052008-04-29 01:00:11 -0700379#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800380static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700381
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700383{
Li Zefan0ac801f2013-01-10 11:49:27 +0800384 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700385 struct cgroup_subsys *ss;
386 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700387
Tejun Heo30159ec2013-06-25 11:53:37 -0700388 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800389 key += (unsigned long)css[i];
390 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700391
Li Zefan0ac801f2013-01-10 11:49:27 +0800392 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700393}
394
Tejun Heo0942eee2013-08-08 20:11:26 -0400395/*
396 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400397 * until after the first call to css_task_iter_start(). This reduces the
398 * fork()/exit() overhead for people who have cgroups compiled into their
399 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400400 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Tejun Heo5abb8852013-06-12 21:04:49 -0700403static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
Tejun Heo69d02062013-06-12 21:04:50 -0700405 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700413 return;
414 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422 css_set_count--;
423
Tejun Heo69d02062013-06-12 21:04:50 -0700424 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700425 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700426
Tejun Heo69d02062013-06-12 21:04:50 -0700427 list_del(&link->cset_link);
428 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800429
Tejun Heoddd69142013-06-12 21:04:54 -0700430 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700431 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700433 set_bit(CGRP_RELEASABLE, &cgrp->flags);
434 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
444/*
445 * refcounted get/put for css_set objects
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700448{
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700453{
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458{
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460}
461
Tejun Heob326f9d2013-06-24 15:21:48 -0700462/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 * @cset: candidate css_set being tested
465 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * @new_cgrp: cgroup that's being entered by the task
467 * @template: desired set of css pointers in css_set (pre-calculated)
468 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800469 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
471 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472static bool compare_css_sets(struct css_set *cset,
473 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 struct cgroup *new_cgrp,
475 struct cgroup_subsys_state *template[])
476{
477 struct list_head *l1, *l2;
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 /* Not all subsystems matched */
481 return false;
482 }
483
484 /*
485 * Compare cgroup pointers in order to distinguish between
486 * different cgroups in heirarchies with no subsystems. We
487 * could get by with just this check alone (and skip the
488 * memcmp above) but on most setups the memcmp check will
489 * avoid the need for this more expensive check on almost all
490 * candidates.
491 */
492
Tejun Heo69d02062013-06-12 21:04:50 -0700493 l1 = &cset->cgrp_links;
494 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700496 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 l1 = l1->next;
500 l2 = l2->next;
501 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700502 if (l1 == &cset->cgrp_links) {
503 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 break;
505 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700506 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 }
508 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700509 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
510 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 cgrp1 = link1->cgrp;
512 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 /*
517 * If this hierarchy is the hierarchy of the cgroup
518 * that's changing, then we need to check that this
519 * css_set points to the new cgroup; if it's any other
520 * hierarchy, then this css_set should point to the
521 * same cgroup as the old css_set.
522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 if (cgrp1->root == new_cgrp->root) {
524 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 return false;
526 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 return false;
529 }
530 }
531 return true;
532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
535 * find_existing_css_set - init css array and find the matching css_set
536 * @old_cset: the css_set that we're using before the cgroup transition
537 * @cgrp: the cgroup that we're moving into
538 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540static struct css_set *find_existing_css_set(struct css_set *old_cset,
541 struct cgroup *cgrp,
542 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700543{
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800547 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700548 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Ben Blumaae8aab2010-03-10 15:22:07 -0800550 /*
551 * Build the set of subsystem state objects that we want to see in the
552 * new css_set. while subsystems can change globally, the entries here
553 * won't change, so no need for locking.
554 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700555 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400556 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700557 /* Subsystem is in this hierarchy. So we want
558 * the subsystem state from the new
559 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400560 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700561 } else {
562 /* Subsystem is not in this hierarchy, so we
563 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700564 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 }
566 }
567
Li Zefan0ac801f2013-01-10 11:49:27 +0800568 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 hash_for_each_possible(css_set_table, cset, hlist, key) {
570 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700571 continue;
572
573 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700574 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700575 }
Paul Menage817929e2007-10-18 23:39:36 -0700576
577 /* No existing cgroup group matched */
578 return NULL;
579}
580
Tejun Heo69d02062013-06-12 21:04:50 -0700581static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700582{
Tejun Heo69d02062013-06-12 21:04:50 -0700583 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700584
Tejun Heo69d02062013-06-12 21:04:50 -0700585 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
586 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700587 kfree(link);
588 }
589}
590
Tejun Heo69d02062013-06-12 21:04:50 -0700591/**
592 * allocate_cgrp_cset_links - allocate cgrp_cset_links
593 * @count: the number of links to allocate
594 * @tmp_links: list_head the allocated links are put on
595 *
596 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
597 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700600{
Tejun Heo69d02062013-06-12 21:04:50 -0700601 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700602 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700603
604 INIT_LIST_HEAD(tmp_links);
605
Li Zefan36553432008-07-29 22:33:19 -0700606 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700607 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700608 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700609 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700610 return -ENOMEM;
611 }
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700613 }
614 return 0;
615}
616
Li Zefanc12f65d2009-01-07 18:07:42 -0800617/**
618 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700619 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800621 * @cgrp: the destination cgroup
622 */
Tejun Heo69d02062013-06-12 21:04:50 -0700623static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
624 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800625{
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800627
Tejun Heo69d02062013-06-12 21:04:50 -0700628 BUG_ON(list_empty(tmp_links));
629 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
630 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700632 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 /*
634 * Always add links to the tail of the list so that the list
635 * is sorted by order of hierarchy creation
636 */
Tejun Heo69d02062013-06-12 21:04:50 -0700637 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800638}
639
Tejun Heob326f9d2013-06-24 15:21:48 -0700640/**
641 * find_css_set - return a new css_set with one cgroup updated
642 * @old_cset: the baseline css_set
643 * @cgrp: the cgroup to be updated
644 *
645 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
646 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700647 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700648static struct css_set *find_css_set(struct css_set *old_cset,
649 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700650{
Tejun Heob326f9d2013-06-24 15:21:48 -0700651 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700653 struct list_head tmp_links;
654 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800655 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700656
Tejun Heob326f9d2013-06-24 15:21:48 -0700657 lockdep_assert_held(&cgroup_mutex);
658
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* First see if we already have a cgroup group that matches
660 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700661 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 cset = find_existing_css_set(old_cset, cgrp, template);
663 if (cset)
664 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 if (cset)
668 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700669
Tejun Heof4f4be22013-06-12 21:04:51 -0700670 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700672 return NULL;
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700675 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700676 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700677 return NULL;
678 }
679
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700681 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 INIT_LIST_HEAD(&cset->tasks);
683 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 /* Copy the set of subsystem state objects generated in
686 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 write_lock(&css_set_lock);
690 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700691 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700693
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 if (c->root == cgrp->root)
695 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700696 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 }
Paul Menage817929e2007-10-18 23:39:36 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700700
Paul Menage817929e2007-10-18 23:39:36 -0700701 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700702
703 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 key = css_set_hash(cset->subsys);
705 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700706
Paul Menage817929e2007-10-18 23:39:36 -0700707 write_unlock(&css_set_lock);
708
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700710}
711
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 * Return the cgroup for "task" from the given hierarchy. Must be
714 * called with cgroup_mutex held.
715 */
716static struct cgroup *task_cgroup_from_root(struct task_struct *task,
717 struct cgroupfs_root *root)
718{
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 struct cgroup *res = NULL;
721
722 BUG_ON(!mutex_is_locked(&cgroup_mutex));
723 read_lock(&css_set_lock);
724 /*
725 * No need to lock the task - since we hold cgroup_mutex the
726 * task can't change groups, so the only thing that can happen
727 * is that it exits and its css is set back to init_css_set.
728 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700729 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700730 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700731 res = &root->top_cgroup;
732 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700733 struct cgrp_cset_link *link;
734
735 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700737
Paul Menage7717f7b2009-09-23 15:56:22 -0700738 if (c->root == root) {
739 res = c;
740 break;
741 }
742 }
743 }
744 read_unlock(&css_set_lock);
745 BUG_ON(!res);
746 return res;
747}
748
749/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 * There is one global cgroup mutex. We also require taking
751 * task_lock() when dereferencing a task's cgroup subsys pointers.
752 * See "The task_lock() exception", at the end of this comment.
753 *
754 * A task must hold cgroup_mutex to modify cgroups.
755 *
756 * Any task can increment and decrement the count field without lock.
757 * So in general, code holding cgroup_mutex can't rely on the count
758 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800759 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * means that no tasks are currently attached, therefore there is no
761 * way a task attached to that cgroup can fork (the other way to
762 * increment the count). So code holding cgroup_mutex can safely
763 * assume that if the count is zero, it will stay zero. Similarly, if
764 * a task holds cgroup_mutex on a cgroup with zero count, it
765 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * needs that mutex.
767 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
769 * (usually) take cgroup_mutex. These are the two most performance
770 * critical pieces of code here. The exception occurs on cgroup_exit(),
771 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
772 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800773 * to the release agent with the name of the cgroup (path relative to
774 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 *
776 * A cgroup can only be deleted if both its 'count' of using tasks
777 * is zero, and its list of 'children' cgroups is empty. Since all
778 * tasks in the system use _some_ cgroup, and since there is always at
779 * least one task in the system (init, pid == 1), therefore, top_cgroup
780 * always has either children cgroups and/or using tasks. So we don't
781 * need a special hack to ensure that top_cgroup cannot be deleted.
782 *
783 * The task_lock() exception
784 *
785 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800786 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800787 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 * several performance critical places that need to reference
789 * task->cgroup without the expense of grabbing a system global
790 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800791 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
793 * the task_struct routinely used for such matters.
794 *
795 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800796 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 */
798
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799/*
800 * A couple of forward declarations required, due to cyclic reference loop:
801 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
802 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
803 * -> cgroup_mkdir.
804 */
805
Al Viro18bb1db2011-07-26 01:41:39 -0400806static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700807static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700808static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700809static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700810static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700811
812static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200813 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700814 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700815};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816
Al Viroa5e7ed32011-07-26 01:55:55 -0400817static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818{
819 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820
821 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400822 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100824 inode->i_uid = current_fsuid();
825 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
827 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
828 }
829 return inode;
830}
831
Li Zefan65dff752013-03-01 15:01:56 +0800832static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
833{
834 struct cgroup_name *name;
835
836 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
837 if (!name)
838 return NULL;
839 strcpy(name->name, dentry->d_name.name);
840 return name;
841}
842
Li Zefanbe445622013-01-24 14:31:42 +0800843static void cgroup_free_fn(struct work_struct *work)
844{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700845 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800846
847 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800848 cgrp->root->number_of_cgroups--;
849 mutex_unlock(&cgroup_mutex);
850
851 /*
Li Zefan415cf072013-04-08 14:35:02 +0800852 * We get a ref to the parent's dentry, and put the ref when
853 * this cgroup is being freed, so it's guaranteed that the
854 * parent won't be destroyed before its children.
855 */
856 dput(cgrp->parent->dentry);
857
858 /*
Li Zefanbe445622013-01-24 14:31:42 +0800859 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700860 * created the cgroup. This will free cgrp->root, if we are
861 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800862 */
863 deactivate_super(cgrp->root->sb);
864
865 /*
866 * if we're getting rid of the cgroup, refcount should ensure
867 * that there are no pidlists left.
868 */
869 BUG_ON(!list_empty(&cgrp->pidlists));
870
871 simple_xattrs_free(&cgrp->xattrs);
872
Li Zefan65dff752013-03-01 15:01:56 +0800873 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800874 kfree(cgrp);
875}
876
877static void cgroup_free_rcu(struct rcu_head *head)
878{
879 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
880
Tejun Heoea15f8c2013-06-13 19:27:42 -0700881 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500882 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800883}
884
Paul Menageddbcc7e2007-10-18 23:39:30 -0700885static void cgroup_diput(struct dentry *dentry, struct inode *inode)
886{
887 /* is dentry a directory ? if so, kfree() associated cgroup */
888 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700889 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800890
Tejun Heo54766d42013-06-12 21:04:53 -0700891 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800892 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700893 } else {
894 struct cfent *cfe = __d_cfe(dentry);
895 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
896
897 WARN_ONCE(!list_empty(&cfe->node) &&
898 cgrp != &cgrp->root->top_cgroup,
899 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700900 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700901 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700902 }
903 iput(inode);
904}
905
906static void remove_dir(struct dentry *d)
907{
908 struct dentry *parent = dget(d->d_parent);
909
910 d_delete(d);
911 simple_rmdir(parent->d_inode, d);
912 dput(parent);
913}
914
Li Zefan2739d3c2013-01-21 18:18:33 +0800915static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700917 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700918
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
920 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100921
Li Zefan2739d3c2013-01-21 18:18:33 +0800922 /*
923 * If we're doing cleanup due to failure of cgroup_create(),
924 * the corresponding @cfe may not exist.
925 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700926 list_for_each_entry(cfe, &cgrp->files, node) {
927 struct dentry *d = cfe->dentry;
928
929 if (cft && cfe->type != cft)
930 continue;
931
932 dget(d);
933 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700934 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 list_del_init(&cfe->node);
936 dput(d);
937
Li Zefan2739d3c2013-01-21 18:18:33 +0800938 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940}
941
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400942/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700943 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700944 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400945 * @subsys_mask: mask of the subsystem ids whose files should be removed
946 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700947static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700948{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700950 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951
Tejun Heob420ba72013-07-12 12:34:02 -0700952 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400953 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700954
955 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400956 continue;
957 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400958 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700960}
961
962/*
963 * NOTE : the dentry must have been dget()'ed
964 */
965static void cgroup_d_remove_dir(struct dentry *dentry)
966{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100967 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700968
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100969 parent = dentry->d_parent;
970 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800971 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100973 spin_unlock(&dentry->d_lock);
974 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 remove_dir(dentry);
976}
977
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700978/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800979 * Call with cgroup_mutex held. Drops reference counts on modules, including
980 * any duplicate ones that parse_cgroupfs_options took. If this function
981 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800982 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700984 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985{
Paul Menagebd89aab2007-10-18 23:40:44 -0700986 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700987 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700988 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700989 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990
Ben Blumaae8aab2010-03-10 15:22:07 -0800991 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800992 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800993
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700995 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700996 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700998
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700999 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001000 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001001 ret = -EBUSY;
1002 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001004
1005 /* pin the module */
1006 if (!try_module_get(ss->module)) {
1007 ret = -ENOENT;
1008 goto out_put;
1009 }
1010 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 }
1012
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001013 /* subsys could be missing if unloaded between parsing and here */
1014 if (added_mask != pinned) {
1015 ret = -ENOENT;
1016 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018
Tejun Heo31261212013-06-28 17:07:30 -07001019 ret = cgroup_populate_dir(cgrp, added_mask);
1020 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001021 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001022
1023 /*
1024 * Nothing can fail from this point on. Remove files for the
1025 * removed subsystems and rebind each subsystem.
1026 */
1027 cgroup_clear_dir(cgrp, removed_mask);
1028
Tejun Heo30159ec2013-06-25 11:53:37 -07001029 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001031
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001032 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001034 BUG_ON(cgroup_css(cgrp, ss));
1035 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1036 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001037
Tejun Heo73e80ed2013-08-13 11:01:55 -04001038 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001039 cgroup_css(cgroup_dummy_top, ss));
1040 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001041
Li Zefan33a68ac2009-01-07 18:07:42 -08001042 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001043 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001045 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001046
Ben Blumcf5d5942010-03-10 15:22:09 -08001047 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001048 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001049 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001051 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1052 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001053
Paul Menageddbcc7e2007-10-18 23:39:30 -07001054 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001055 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001056
Tejun Heoca8bdca2013-08-26 18:40:56 -04001057 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001058 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1059
Tejun Heo9871bf92013-06-24 15:21:47 -07001060 cgroup_subsys[i]->root = &cgroup_dummy_root;
1061 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /* subsystem is now free - drop reference on module */
1064 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 }
1067 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068
Tejun Heo1672d042013-06-25 18:04:54 -07001069 /*
1070 * Mark @root has finished binding subsystems. @root->subsys_mask
1071 * now matches the bound subsystems.
1072 */
1073 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1074
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001076
1077out_put:
1078 for_each_subsys(ss, i)
1079 if (pinned & (1 << i))
1080 module_put(ss->module);
1081 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082}
1083
Al Viro34c80b12011-12-08 21:32:45 -05001084static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085{
Al Viro34c80b12011-12-08 21:32:45 -05001086 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 struct cgroup_subsys *ss;
1088
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001089 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001090 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001091 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001092 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1093 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001094 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001096 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001097 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001098 if (strlen(root->release_agent_path))
1099 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001100 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001101 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001102 if (strlen(root->name))
1103 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001104 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 return 0;
1106}
1107
1108struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001109 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001111 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001112 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001113 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001114 /* User explicitly requested empty subsystem */
1115 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001116
1117 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001118
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119};
1120
Ben Blumaae8aab2010-03-10 15:22:07 -08001121/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001122 * Convert a hierarchy specifier into a bitmask of subsystems and
1123 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1124 * array. This function takes refcounts on subsystems to be used, unless it
1125 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001126 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001127static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001129 char *token, *o = data;
1130 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001131 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001132 struct cgroup_subsys *ss;
1133 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001134
Ben Blumaae8aab2010-03-10 15:22:07 -08001135 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1136
Li Zefanf9ab5b52009-06-17 16:26:33 -07001137#ifdef CONFIG_CPUSETS
1138 mask = ~(1UL << cpuset_subsys_id);
1139#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140
Paul Menagec6d57f32009-09-23 15:56:19 -07001141 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142
1143 while ((token = strsep(&o, ",")) != NULL) {
1144 if (!*token)
1145 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001146 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001147 /* Explicitly have no subsystems */
1148 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001149 continue;
1150 }
1151 if (!strcmp(token, "all")) {
1152 /* Mutually exclusive option 'all' + subsystem name */
1153 if (one_ss)
1154 return -EINVAL;
1155 all_ss = true;
1156 continue;
1157 }
Tejun Heo873fe092013-04-14 20:15:26 -07001158 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1159 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1160 continue;
1161 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001162 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001163 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001167 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001170 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001171 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001172 continue;
1173 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 /* Specifying two release agents is forbidden */
1176 if (opts->release_agent)
1177 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001178 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001179 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001180 if (!opts->release_agent)
1181 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
1184 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001185 const char *name = token + 5;
1186 /* Can't specify an empty name */
1187 if (!strlen(name))
1188 return -EINVAL;
1189 /* Must match [\w.-]+ */
1190 for (i = 0; i < strlen(name); i++) {
1191 char c = name[i];
1192 if (isalnum(c))
1193 continue;
1194 if ((c == '.') || (c == '-') || (c == '_'))
1195 continue;
1196 return -EINVAL;
1197 }
1198 /* Specifying two names is forbidden */
1199 if (opts->name)
1200 return -EINVAL;
1201 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001202 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001203 GFP_KERNEL);
1204 if (!opts->name)
1205 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206
1207 continue;
1208 }
1209
Tejun Heo30159ec2013-06-25 11:53:37 -07001210 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001211 if (strcmp(token, ss->name))
1212 continue;
1213 if (ss->disabled)
1214 continue;
1215
1216 /* Mutually exclusive option 'all' + subsystem name */
1217 if (all_ss)
1218 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001219 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220 one_ss = true;
1221
1222 break;
1223 }
1224 if (i == CGROUP_SUBSYS_COUNT)
1225 return -ENOENT;
1226 }
1227
1228 /*
1229 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001230 * otherwise if 'none', 'name=' and a subsystem name options
1231 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001233 if (all_ss || (!one_ss && !opts->none && !opts->name))
1234 for_each_subsys(ss, i)
1235 if (!ss->disabled)
1236 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001237
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001238 /* Consistency checks */
1239
Tejun Heo873fe092013-04-14 20:15:26 -07001240 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1241 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1242
1243 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1244 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1245 return -EINVAL;
1246 }
1247
1248 if (opts->cpuset_clone_children) {
1249 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1250 return -EINVAL;
1251 }
1252 }
1253
Li Zefanf9ab5b52009-06-17 16:26:33 -07001254 /*
1255 * Option noprefix was introduced just for backward compatibility
1256 * with the old cpuset, so we allow noprefix only if mounting just
1257 * the cpuset subsystem.
1258 */
Tejun Heo93438622013-04-14 20:15:25 -07001259 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001260 return -EINVAL;
1261
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262
1263 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001264 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001265 return -EINVAL;
1266
1267 /*
1268 * We either have to specify by name or by subsystems. (So all
1269 * empty hierarchies must have a name).
1270 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001271 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001272 return -EINVAL;
1273
1274 return 0;
1275}
1276
1277static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1278{
1279 int ret = 0;
1280 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001281 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001283 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284
Tejun Heo873fe092013-04-14 20:15:26 -07001285 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1286 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1287 return -EINVAL;
1288 }
1289
Paul Menagebd89aab2007-10-18 23:40:44 -07001290 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001292 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293
1294 /* See what subsystems are wanted */
1295 ret = parse_cgroupfs_options(data, &opts);
1296 if (ret)
1297 goto out_unlock;
1298
Tejun Heoa8a648c2013-06-24 15:21:47 -07001299 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001300 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1301 task_tgid_nr(current), current->comm);
1302
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001303 added_mask = opts.subsys_mask & ~root->subsys_mask;
1304 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001305
Ben Blumcf5d5942010-03-10 15:22:09 -08001306 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001307 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001309 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1310 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1311 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001312 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001313 goto out_unlock;
1314 }
1315
Tejun Heof172e672013-06-28 17:07:30 -07001316 /* remounting is not allowed for populated hierarchies */
1317 if (root->number_of_cgroups > 1) {
1318 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001319 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001321
Paul Menageddbcc7e2007-10-18 23:39:30 -07001322 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001323 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325
Paul Menage81a6a5c2007-10-18 23:39:38 -07001326 if (opts.release_agent)
1327 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001329 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001330 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001331 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001332 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001333 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334 return ret;
1335}
1336
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001337static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001338 .statfs = simple_statfs,
1339 .drop_inode = generic_delete_inode,
1340 .show_options = cgroup_show_options,
1341 .remount_fs = cgroup_remount,
1342};
1343
Paul Menagecc31edc2008-10-18 20:28:04 -07001344static void init_cgroup_housekeeping(struct cgroup *cgrp)
1345{
1346 INIT_LIST_HEAD(&cgrp->sibling);
1347 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001348 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001349 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001350 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001351 INIT_LIST_HEAD(&cgrp->pidlists);
1352 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001353 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001354 INIT_LIST_HEAD(&cgrp->event_list);
1355 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001356 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001357}
Paul Menagec6d57f32009-09-23 15:56:19 -07001358
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359static void init_cgroup_root(struct cgroupfs_root *root)
1360{
Paul Menagebd89aab2007-10-18 23:40:44 -07001361 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001362
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 INIT_LIST_HEAD(&root->subsys_list);
1364 INIT_LIST_HEAD(&root->root_list);
1365 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001366 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001367 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001368 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001369 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370}
1371
Tejun Heofc76df72013-06-25 11:53:37 -07001372static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001373{
Tejun Heo1a574232013-04-14 11:36:58 -07001374 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001375
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001376 lockdep_assert_held(&cgroup_mutex);
1377 lockdep_assert_held(&cgroup_root_mutex);
1378
Tejun Heofc76df72013-06-25 11:53:37 -07001379 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1380 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001381 if (id < 0)
1382 return id;
1383
1384 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001385 return 0;
1386}
1387
1388static void cgroup_exit_root_id(struct cgroupfs_root *root)
1389{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001390 lockdep_assert_held(&cgroup_mutex);
1391 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001392
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001393 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001394 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001395 root->hierarchy_id = 0;
1396 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397}
1398
Paul Menageddbcc7e2007-10-18 23:39:30 -07001399static int cgroup_test_super(struct super_block *sb, void *data)
1400{
Paul Menagec6d57f32009-09-23 15:56:19 -07001401 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402 struct cgroupfs_root *root = sb->s_fs_info;
1403
Paul Menagec6d57f32009-09-23 15:56:19 -07001404 /* If we asked for a name then it must match */
1405 if (opts->name && strcmp(opts->name, root->name))
1406 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001407
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408 /*
1409 * If we asked for subsystems (or explicitly for no
1410 * subsystems) then they must match
1411 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001412 if ((opts->subsys_mask || opts->none)
1413 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 return 0;
1415
1416 return 1;
1417}
1418
Paul Menagec6d57f32009-09-23 15:56:19 -07001419static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1420{
1421 struct cgroupfs_root *root;
1422
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001423 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001424 return NULL;
1425
1426 root = kzalloc(sizeof(*root), GFP_KERNEL);
1427 if (!root)
1428 return ERR_PTR(-ENOMEM);
1429
1430 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001431
Tejun Heo1672d042013-06-25 18:04:54 -07001432 /*
1433 * We need to set @root->subsys_mask now so that @root can be
1434 * matched by cgroup_test_super() before it finishes
1435 * initialization; otherwise, competing mounts with the same
1436 * options may try to bind the same subsystems instead of waiting
1437 * for the first one leading to unexpected mount errors.
1438 * SUBSYS_BOUND will be set once actual binding is complete.
1439 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001440 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001441 root->flags = opts->flags;
1442 if (opts->release_agent)
1443 strcpy(root->release_agent_path, opts->release_agent);
1444 if (opts->name)
1445 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001446 if (opts->cpuset_clone_children)
1447 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001448 return root;
1449}
1450
Tejun Heofa3ca072013-04-14 11:36:56 -07001451static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001452{
Tejun Heofa3ca072013-04-14 11:36:56 -07001453 if (root) {
1454 /* hierarhcy ID shoulid already have been released */
1455 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001456
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001457 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001458 kfree(root);
1459 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001460}
1461
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462static int cgroup_set_super(struct super_block *sb, void *data)
1463{
1464 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001465 struct cgroup_sb_opts *opts = data;
1466
1467 /* If we don't have a new root, we can't set up a new sb */
1468 if (!opts->new_root)
1469 return -EINVAL;
1470
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001471 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472
1473 ret = set_anon_super(sb, NULL);
1474 if (ret)
1475 return ret;
1476
Paul Menagec6d57f32009-09-23 15:56:19 -07001477 sb->s_fs_info = opts->new_root;
1478 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001479
1480 sb->s_blocksize = PAGE_CACHE_SIZE;
1481 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1482 sb->s_magic = CGROUP_SUPER_MAGIC;
1483 sb->s_op = &cgroup_ops;
1484
1485 return 0;
1486}
1487
1488static int cgroup_get_rootdir(struct super_block *sb)
1489{
Al Viro0df6a632010-12-21 13:29:29 -05001490 static const struct dentry_operations cgroup_dops = {
1491 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001492 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001493 };
1494
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495 struct inode *inode =
1496 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001497
1498 if (!inode)
1499 return -ENOMEM;
1500
Paul Menageddbcc7e2007-10-18 23:39:30 -07001501 inode->i_fop = &simple_dir_operations;
1502 inode->i_op = &cgroup_dir_inode_operations;
1503 /* directories start off with i_nlink == 2 (for "." entry) */
1504 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001505 sb->s_root = d_make_root(inode);
1506 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001508 /* for everything else we want ->d_op set */
1509 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510 return 0;
1511}
1512
Al Virof7e83572010-07-26 13:23:11 +04001513static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001515 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516{
1517 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519 int ret = 0;
1520 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001521 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001522 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001523 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001524 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525
1526 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001527 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001529 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001530 if (ret)
1531 goto out_err;
1532
1533 /*
1534 * Allocate a new cgroup root. We may not need it if we're
1535 * reusing an existing hierarchy.
1536 */
1537 new_root = cgroup_root_from_opts(&opts);
1538 if (IS_ERR(new_root)) {
1539 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001540 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001541 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001542 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001545 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001547 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001548 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001549 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 }
1551
Paul Menagec6d57f32009-09-23 15:56:19 -07001552 root = sb->s_fs_info;
1553 BUG_ON(!root);
1554 if (root == opts.new_root) {
1555 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001556 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001558 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001559 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560
1561 BUG_ON(sb->s_root != NULL);
1562
1563 ret = cgroup_get_rootdir(sb);
1564 if (ret)
1565 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001566 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567
Paul Menage817929e2007-10-18 23:39:36 -07001568 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001570 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001572 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1573 0, 1, GFP_KERNEL);
1574 if (root_cgrp->id < 0)
1575 goto unlock_drop;
1576
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001577 /* Check for name clashes with existing mounts */
1578 ret = -EBUSY;
1579 if (strlen(root->name))
1580 for_each_active_root(existing_root)
1581 if (!strcmp(existing_root->name, root->name))
1582 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001583
Paul Menage817929e2007-10-18 23:39:36 -07001584 /*
1585 * We're accessing css_set_count without locking
1586 * css_set_lock here, but that's OK - it can only be
1587 * increased by someone holding cgroup_lock, and
1588 * that's us. The worst that can happen is that we
1589 * have some link structures left over
1590 */
Tejun Heo69d02062013-06-12 21:04:50 -07001591 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001592 if (ret)
1593 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001594
Tejun Heofc76df72013-06-25 11:53:37 -07001595 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1596 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001597 if (ret)
1598 goto unlock_drop;
1599
Tejun Heo31261212013-06-28 17:07:30 -07001600 sb->s_root->d_fsdata = root_cgrp;
1601 root_cgrp->dentry = sb->s_root;
1602
1603 /*
1604 * We're inside get_sb() and will call lookup_one_len() to
1605 * create the root files, which doesn't work if SELinux is
1606 * in use. The following cred dancing somehow works around
1607 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1608 * populating new cgroupfs mount") for more details.
1609 */
1610 cred = override_creds(&init_cred);
1611
Tejun Heo2bb566c2013-08-08 20:11:23 -04001612 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001613 if (ret)
1614 goto rm_base_files;
1615
Tejun Heoa8a648c2013-06-24 15:21:47 -07001616 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001617 if (ret)
1618 goto rm_base_files;
1619
1620 revert_creds(cred);
1621
Ben Blumcf5d5942010-03-10 15:22:09 -08001622 /*
1623 * There must be no failure case after here, since rebinding
1624 * takes care of subsystems' refcounts, which are explicitly
1625 * dropped in the failure exit path.
1626 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001627
Tejun Heo9871bf92013-06-24 15:21:47 -07001628 list_add(&root->root_list, &cgroup_roots);
1629 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Paul Menage817929e2007-10-18 23:39:36 -07001631 /* Link the top cgroup in this hierarchy into all
1632 * the css_set objects */
1633 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001634 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001635 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001636 write_unlock(&css_set_lock);
1637
Tejun Heo69d02062013-06-12 21:04:50 -07001638 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001639
Li Zefanc12f65d2009-01-07 18:07:42 -08001640 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641 BUG_ON(root->number_of_cgroups != 1);
1642
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001643 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001645 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001646 } else {
1647 /*
1648 * We re-used an existing hierarchy - the new root (if
1649 * any) is not needed
1650 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001651 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001652
Tejun Heoc7ba8282013-06-29 14:06:10 -07001653 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001654 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1655 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1656 ret = -EINVAL;
1657 goto drop_new_super;
1658 } else {
1659 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1660 }
Tejun Heo873fe092013-04-14 20:15:26 -07001661 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 }
1663
Paul Menagec6d57f32009-09-23 15:56:19 -07001664 kfree(opts.release_agent);
1665 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001666 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001667
Tejun Heo31261212013-06-28 17:07:30 -07001668 rm_base_files:
1669 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001670 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001671 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001672 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001673 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001674 mutex_unlock(&cgroup_root_mutex);
1675 mutex_unlock(&cgroup_mutex);
1676 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001678 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001679 out_err:
1680 kfree(opts.release_agent);
1681 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001682 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001683}
1684
1685static void cgroup_kill_sb(struct super_block *sb) {
1686 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001687 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001688 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 int ret;
1690
1691 BUG_ON(!root);
1692
1693 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001694 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695
Tejun Heo31261212013-06-28 17:07:30 -07001696 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001698 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
1700 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001701 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1702 ret = rebind_subsystems(root, 0, root->subsys_mask);
1703 /* Shouldn't be able to fail ... */
1704 BUG_ON(ret);
1705 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Paul Menage817929e2007-10-18 23:39:36 -07001707 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001708 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001709 * root cgroup
1710 */
1711 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001712
Tejun Heo69d02062013-06-12 21:04:50 -07001713 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1714 list_del(&link->cset_link);
1715 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001716 kfree(link);
1717 }
1718 write_unlock(&css_set_lock);
1719
Paul Menage839ec542009-01-29 14:25:22 -08001720 if (!list_empty(&root->root_list)) {
1721 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001722 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001723 }
Li Zefane5f6a862009-01-07 18:07:41 -08001724
Tejun Heofa3ca072013-04-14 11:36:56 -07001725 cgroup_exit_root_id(root);
1726
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001727 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001729 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001731 simple_xattrs_free(&cgrp->xattrs);
1732
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001734 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735}
1736
1737static struct file_system_type cgroup_fs_type = {
1738 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001739 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740 .kill_sb = cgroup_kill_sb,
1741};
1742
Greg KH676db4a2010-08-05 13:53:35 -07001743static struct kobject *cgroup_kobj;
1744
Li Zefana043e3b2008-02-23 15:24:09 -08001745/**
1746 * cgroup_path - generate the path of a cgroup
1747 * @cgrp: the cgroup in question
1748 * @buf: the buffer to write the path into
1749 * @buflen: the length of the buffer
1750 *
Li Zefan65dff752013-03-01 15:01:56 +08001751 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1752 *
1753 * We can't generate cgroup path using dentry->d_name, as accessing
1754 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1755 * inode's i_mutex, while on the other hand cgroup_path() can be called
1756 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001758int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759{
Li Zefan65dff752013-03-01 15:01:56 +08001760 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001762
Tejun Heoda1f2962013-04-14 10:32:19 -07001763 if (!cgrp->parent) {
1764 if (strlcpy(buf, "/", buflen) >= buflen)
1765 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 return 0;
1767 }
1768
Tao Ma316eb662012-11-08 21:36:38 +08001769 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001770 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001771
Li Zefan65dff752013-03-01 15:01:56 +08001772 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001773 do {
Li Zefan65dff752013-03-01 15:01:56 +08001774 const char *name = cgroup_name(cgrp);
1775 int len;
1776
1777 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001779 goto out;
1780 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001781
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001783 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001785
1786 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001787 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001788 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001790out:
1791 rcu_read_unlock();
1792 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793}
Ben Blum67523c42010-03-10 15:22:11 -08001794EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795
Tejun Heo857a2be2013-04-14 20:50:08 -07001796/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001797 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001798 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001799 * @buf: the buffer to write the path into
1800 * @buflen: the length of the buffer
1801 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001802 * Determine @task's cgroup on the first (the one with the lowest non-zero
1803 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1804 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1805 * cgroup controller callbacks.
1806 *
1807 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001808 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001809int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001810{
1811 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 struct cgroup *cgrp;
1813 int hierarchy_id = 1, ret = 0;
1814
1815 if (buflen < 2)
1816 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001817
1818 mutex_lock(&cgroup_mutex);
1819
Tejun Heo913ffdb2013-07-11 16:34:48 -07001820 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1821
Tejun Heo857a2be2013-04-14 20:50:08 -07001822 if (root) {
1823 cgrp = task_cgroup_from_root(task, root);
1824 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001825 } else {
1826 /* if no hierarchy exists, everyone is in "/" */
1827 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001828 }
1829
1830 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001831 return ret;
1832}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001833EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001834
Ben Blum74a11662011-05-26 16:25:20 -07001835/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001836 * Control Group taskset
1837 */
Tejun Heo134d3372011-12-12 18:12:21 -08001838struct task_and_cgroup {
1839 struct task_struct *task;
1840 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001841 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001842};
1843
Tejun Heo2f7ee562011-12-12 18:12:21 -08001844struct cgroup_taskset {
1845 struct task_and_cgroup single;
1846 struct flex_array *tc_array;
1847 int tc_array_len;
1848 int idx;
1849 struct cgroup *cur_cgrp;
1850};
1851
1852/**
1853 * cgroup_taskset_first - reset taskset and return the first task
1854 * @tset: taskset of interest
1855 *
1856 * @tset iteration is initialized and the first task is returned.
1857 */
1858struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1859{
1860 if (tset->tc_array) {
1861 tset->idx = 0;
1862 return cgroup_taskset_next(tset);
1863 } else {
1864 tset->cur_cgrp = tset->single.cgrp;
1865 return tset->single.task;
1866 }
1867}
1868EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1869
1870/**
1871 * cgroup_taskset_next - iterate to the next task in taskset
1872 * @tset: taskset of interest
1873 *
1874 * Return the next task in @tset. Iteration must have been initialized
1875 * with cgroup_taskset_first().
1876 */
1877struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1878{
1879 struct task_and_cgroup *tc;
1880
1881 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1882 return NULL;
1883
1884 tc = flex_array_get(tset->tc_array, tset->idx++);
1885 tset->cur_cgrp = tc->cgrp;
1886 return tc->task;
1887}
1888EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1889
1890/**
Tejun Heod99c8722013-08-08 20:11:27 -04001891 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001892 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001893 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001894 *
Tejun Heod99c8722013-08-08 20:11:27 -04001895 * Return the css for the current (last returned) task of @tset for
1896 * subsystem specified by @subsys_id. This function must be preceded by
1897 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001898 */
Tejun Heod99c8722013-08-08 20:11:27 -04001899struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1900 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001901{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001902 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001903}
Tejun Heod99c8722013-08-08 20:11:27 -04001904EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001905
1906/**
1907 * cgroup_taskset_size - return the number of tasks in taskset
1908 * @tset: taskset of interest
1909 */
1910int cgroup_taskset_size(struct cgroup_taskset *tset)
1911{
1912 return tset->tc_array ? tset->tc_array_len : 1;
1913}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1915
1916
Ben Blum74a11662011-05-26 16:25:20 -07001917/*
1918 * cgroup_task_migrate - move a task from one cgroup to another.
1919 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001920 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001921 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001922static void cgroup_task_migrate(struct cgroup *old_cgrp,
1923 struct task_struct *tsk,
1924 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001925{
Tejun Heo5abb8852013-06-12 21:04:49 -07001926 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001927
1928 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001929 * We are synchronized through threadgroup_lock() against PF_EXITING
1930 * setting such that we can't race against cgroup_exit() changing the
1931 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001932 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001933 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001934 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001935
Ben Blum74a11662011-05-26 16:25:20 -07001936 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001937 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001938 task_unlock(tsk);
1939
1940 /* Update the css_set linked lists if we're using them */
1941 write_lock(&css_set_lock);
1942 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001943 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001944 write_unlock(&css_set_lock);
1945
1946 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001947 * We just gained a reference on old_cset by taking it from the
1948 * task. As trading it for new_cset is protected by cgroup_mutex,
1949 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001950 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001951 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1952 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001953}
1954
Li Zefana043e3b2008-02-23 15:24:09 -08001955/**
Li Zefan081aa452013-03-13 09:17:09 +08001956 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001957 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001958 * @tsk: the task or the leader of the threadgroup to be attached
1959 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001960 *
Tejun Heo257058a2011-12-12 18:12:21 -08001961 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001962 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001964static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1965 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001966{
1967 int retval, i, group_size;
1968 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001969 struct cgroupfs_root *root = cgrp->root;
1970 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001971 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001972 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001973 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001974 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001975
1976 /*
1977 * step 0: in order to do expensive, possibly blocking operations for
1978 * every thread, we cannot iterate the thread group list, since it needs
1979 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001980 * group - group_rwsem prevents new threads from appearing, and if
1981 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001982 */
Li Zefan081aa452013-03-13 09:17:09 +08001983 if (threadgroup)
1984 group_size = get_nr_threads(tsk);
1985 else
1986 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001987 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001988 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001989 if (!group)
1990 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001991 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001992 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001993 if (retval)
1994 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001995
Ben Blum74a11662011-05-26 16:25:20 -07001996 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001997 /*
1998 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1999 * already PF_EXITING could be freed from underneath us unless we
2000 * take an rcu_read_lock.
2001 */
2002 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002003 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002004 struct task_and_cgroup ent;
2005
Tejun Heocd3d0952011-12-12 18:12:21 -08002006 /* @tsk either already exited or can't exit until the end */
2007 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002008 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002009
Ben Blum74a11662011-05-26 16:25:20 -07002010 /* as per above, nr_threads may decrease, but not increase. */
2011 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002012 ent.task = tsk;
2013 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002014 /* nothing to do if this task is already in the cgroup */
2015 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002016 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002017 /*
2018 * saying GFP_ATOMIC has no effect here because we did prealloc
2019 * earlier, but it's good form to communicate our expectations.
2020 */
Tejun Heo134d3372011-12-12 18:12:21 -08002021 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002022 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002023 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002024 next:
Li Zefan081aa452013-03-13 09:17:09 +08002025 if (!threadgroup)
2026 break;
Ben Blum74a11662011-05-26 16:25:20 -07002027 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002028 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002029 /* remember the number of threads in the array for later. */
2030 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002031 tset.tc_array = group;
2032 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002033
Tejun Heo134d3372011-12-12 18:12:21 -08002034 /* methods shouldn't be called if no task is actually migrating */
2035 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002036 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002037 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002038
Ben Blum74a11662011-05-26 16:25:20 -07002039 /*
2040 * step 1: check that we can legitimately attach to the cgroup.
2041 */
Tejun Heo5549c492013-06-24 15:21:48 -07002042 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002043 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002044
Ben Blum74a11662011-05-26 16:25:20 -07002045 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002046 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002047 if (retval) {
2048 failed_ss = ss;
2049 goto out_cancel_attach;
2050 }
2051 }
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053
2054 /*
2055 * step 2: make sure css_sets exist for all threads to be migrated.
2056 * we use find_css_set, which allocates a new one if necessary.
2057 */
Ben Blum74a11662011-05-26 16:25:20 -07002058 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002059 struct css_set *old_cset;
2060
Tejun Heo134d3372011-12-12 18:12:21 -08002061 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002062 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002063 tc->cset = find_css_set(old_cset, cgrp);
2064 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002065 retval = -ENOMEM;
2066 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002067 }
2068 }
2069
2070 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002071 * step 3: now that we're guaranteed success wrt the css_sets,
2072 * proceed to move all tasks to the new cgroup. There are no
2073 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002074 */
Ben Blum74a11662011-05-26 16:25:20 -07002075 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002076 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002077 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002078 }
2079 /* nothing is sensitive to fork() after this point. */
2080
2081 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002082 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002083 */
Tejun Heo5549c492013-06-24 15:21:48 -07002084 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002085 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002086
Ben Blum74a11662011-05-26 16:25:20 -07002087 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002088 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002089 }
2090
2091 /*
2092 * step 5: success! and cleanup
2093 */
Ben Blum74a11662011-05-26 16:25:20 -07002094 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002095out_put_css_set_refs:
2096 if (retval) {
2097 for (i = 0; i < group_size; i++) {
2098 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002099 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002100 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002101 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002102 }
Ben Blum74a11662011-05-26 16:25:20 -07002103 }
2104out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002105 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002106 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002107 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002108
Tejun Heo494c1672011-12-12 18:12:22 -08002109 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002110 break;
Ben Blum74a11662011-05-26 16:25:20 -07002111 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002112 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002113 }
2114 }
Ben Blum74a11662011-05-26 16:25:20 -07002115out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002116 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002117 return retval;
2118}
2119
2120/*
2121 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002122 * function to attach either it or all tasks in its threadgroup. Will lock
2123 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002124 */
2125static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002126{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002127 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002128 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 int ret;
2130
Ben Blum74a11662011-05-26 16:25:20 -07002131 if (!cgroup_lock_live_group(cgrp))
2132 return -ENODEV;
2133
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002134retry_find_task:
2135 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002137 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002138 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002140 ret= -ESRCH;
2141 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 }
Ben Blum74a11662011-05-26 16:25:20 -07002143 /*
2144 * even if we're attaching all tasks in the thread group, we
2145 * only need to check permissions on one of them.
2146 */
David Howellsc69e8d92008-11-14 10:39:19 +11002147 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002148 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2149 !uid_eq(cred->euid, tcred->uid) &&
2150 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002151 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 ret = -EACCES;
2153 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002154 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 } else
2156 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002157
2158 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002159 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002160
2161 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002162 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002163 * trapped in a cpuset, or RT worker may be born in a cgroup
2164 * with no rt_runtime allocated. Just say no.
2165 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002166 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002167 ret = -EINVAL;
2168 rcu_read_unlock();
2169 goto out_unlock_cgroup;
2170 }
2171
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 get_task_struct(tsk);
2173 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002174
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002175 threadgroup_lock(tsk);
2176 if (threadgroup) {
2177 if (!thread_group_leader(tsk)) {
2178 /*
2179 * a race with de_thread from another thread's exec()
2180 * may strip us of our leadership, if this happens,
2181 * there is no choice but to throw this task away and
2182 * try again; this is
2183 * "double-double-toil-and-trouble-check locking".
2184 */
2185 threadgroup_unlock(tsk);
2186 put_task_struct(tsk);
2187 goto retry_find_task;
2188 }
Li Zefan081aa452013-03-13 09:17:09 +08002189 }
2190
2191 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2192
Tejun Heocd3d0952011-12-12 18:12:21 -08002193 threadgroup_unlock(tsk);
2194
Paul Menagebbcb81d2007-10-18 23:39:32 -07002195 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002196out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002197 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002198 return ret;
2199}
2200
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002201/**
2202 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2203 * @from: attach to all cgroups of a given task
2204 * @tsk: the task to be attached
2205 */
2206int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2207{
2208 struct cgroupfs_root *root;
2209 int retval = 0;
2210
Tejun Heo47cfcd02013-04-07 09:29:51 -07002211 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002212 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002213 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002214
Li Zefan6f4b7e62013-07-31 16:18:36 +08002215 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002216 if (retval)
2217 break;
2218 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002219 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002220
2221 return retval;
2222}
2223EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2224
Tejun Heo182446d2013-08-08 20:11:24 -04002225static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2226 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002227{
Tejun Heo182446d2013-08-08 20:11:24 -04002228 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002229}
2230
Tejun Heo182446d2013-08-08 20:11:24 -04002231static int cgroup_procs_write(struct cgroup_subsys_state *css,
2232 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002233{
Tejun Heo182446d2013-08-08 20:11:24 -04002234 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002235}
2236
Tejun Heo182446d2013-08-08 20:11:24 -04002237static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2238 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002239{
Tejun Heo182446d2013-08-08 20:11:24 -04002240 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002241 if (strlen(buffer) >= PATH_MAX)
2242 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002243 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002244 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002245 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002246 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002247 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002248 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002249 return 0;
2250}
2251
Tejun Heo182446d2013-08-08 20:11:24 -04002252static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2253 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002254{
Tejun Heo182446d2013-08-08 20:11:24 -04002255 struct cgroup *cgrp = css->cgroup;
2256
Paul Menagee788e062008-07-25 01:46:59 -07002257 if (!cgroup_lock_live_group(cgrp))
2258 return -ENODEV;
2259 seq_puts(seq, cgrp->root->release_agent_path);
2260 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002261 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002262 return 0;
2263}
2264
Tejun Heo182446d2013-08-08 20:11:24 -04002265static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2266 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002267{
Tejun Heo182446d2013-08-08 20:11:24 -04002268 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002269 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002270}
2271
Paul Menage84eea842008-07-25 01:47:00 -07002272/* A buffer size big enough for numbers or short strings */
2273#define CGROUP_LOCAL_BUFFER_SIZE 64
2274
Tejun Heo182446d2013-08-08 20:11:24 -04002275static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2276 struct cftype *cft, struct file *file,
2277 const char __user *userbuf, size_t nbytes,
2278 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002279{
Paul Menage84eea842008-07-25 01:47:00 -07002280 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002281 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002282 char *end;
2283
2284 if (!nbytes)
2285 return -EINVAL;
2286 if (nbytes >= sizeof(buffer))
2287 return -E2BIG;
2288 if (copy_from_user(buffer, userbuf, nbytes))
2289 return -EFAULT;
2290
2291 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002292 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002293 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002294 if (*end)
2295 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002296 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002297 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002298 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002299 if (*end)
2300 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002301 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002302 }
Paul Menage355e0c42007-10-18 23:39:33 -07002303 if (!retval)
2304 retval = nbytes;
2305 return retval;
2306}
2307
Tejun Heo182446d2013-08-08 20:11:24 -04002308static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2309 struct cftype *cft, struct file *file,
2310 const char __user *userbuf, size_t nbytes,
2311 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002312{
Paul Menage84eea842008-07-25 01:47:00 -07002313 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002314 int retval = 0;
2315 size_t max_bytes = cft->max_write_len;
2316 char *buffer = local_buffer;
2317
2318 if (!max_bytes)
2319 max_bytes = sizeof(local_buffer) - 1;
2320 if (nbytes >= max_bytes)
2321 return -E2BIG;
2322 /* Allocate a dynamic buffer if we need one */
2323 if (nbytes >= sizeof(local_buffer)) {
2324 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2325 if (buffer == NULL)
2326 return -ENOMEM;
2327 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002328 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2329 retval = -EFAULT;
2330 goto out;
2331 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002332
2333 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002334 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002335 if (!retval)
2336 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002337out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002338 if (buffer != local_buffer)
2339 kfree(buffer);
2340 return retval;
2341}
2342
Paul Menageddbcc7e2007-10-18 23:39:30 -07002343static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002344 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002345{
Tejun Heo182446d2013-08-08 20:11:24 -04002346 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002348 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002349
Paul Menage355e0c42007-10-18 23:39:33 -07002350 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002351 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002352 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002353 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002354 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002355 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002356 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002357 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002358 return ret ? ret : nbytes;
2359 }
Paul Menage355e0c42007-10-18 23:39:33 -07002360 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361}
2362
Tejun Heo182446d2013-08-08 20:11:24 -04002363static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2364 struct cftype *cft, struct file *file,
2365 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002366{
Paul Menage84eea842008-07-25 01:47:00 -07002367 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002368 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2370
2371 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2372}
2373
Tejun Heo182446d2013-08-08 20:11:24 -04002374static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2375 struct cftype *cft, struct file *file,
2376 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002377{
Paul Menage84eea842008-07-25 01:47:00 -07002378 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002379 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002380 int len = sprintf(tmp, "%lld\n", (long long) val);
2381
2382 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2383}
2384
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002386 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387{
Tejun Heo182446d2013-08-08 20:11:24 -04002388 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002390 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002391
2392 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002393 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002394 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002395 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002396 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002397 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398 return -EINVAL;
2399}
2400
Paul Menage91796562008-04-29 01:00:01 -07002401/*
2402 * seqfile ops/methods for returning structured data. Currently just
2403 * supports string->u64 maps, but can be extended in future.
2404 */
2405
Paul Menage91796562008-04-29 01:00:01 -07002406static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2407{
2408 struct seq_file *sf = cb->state;
2409 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2410}
2411
2412static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2413{
Li Zefane0798ce2013-07-31 17:36:25 +08002414 struct cfent *cfe = m->private;
2415 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002416 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002417
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002418 if (cft->read_map) {
2419 struct cgroup_map_cb cb = {
2420 .fill = cgroup_map_add,
2421 .state = m,
2422 };
Tejun Heo182446d2013-08-08 20:11:24 -04002423 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002424 }
Tejun Heo182446d2013-08-08 20:11:24 -04002425 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002426}
2427
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002428static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002429 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002430 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002431 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002432 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002433};
2434
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435static int cgroup_file_open(struct inode *inode, struct file *file)
2436{
Tejun Heof7d58812013-08-08 20:11:23 -04002437 struct cfent *cfe = __d_cfe(file->f_dentry);
2438 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002439 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2440 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002441 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442
2443 err = generic_file_open(inode, file);
2444 if (err)
2445 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002446
2447 /*
2448 * If the file belongs to a subsystem, pin the css. Will be
2449 * unpinned either on open failure or release. This ensures that
2450 * @css stays alive for all file operations.
2451 */
Tejun Heo105347b2013-08-13 11:01:55 -04002452 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002453 css = cgroup_css(cgrp, cft->ss);
2454 if (cft->ss && !css_tryget(css))
2455 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002456 rcu_read_unlock();
2457
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002458 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002459 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002460
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002461 /*
2462 * @cfe->css is used by read/write/close to determine the
2463 * associated css. @file->private_data would be a better place but
2464 * that's already used by seqfile. Multiple accessors may use it
2465 * simultaneously which is okay as the association never changes.
2466 */
2467 WARN_ON_ONCE(cfe->css && cfe->css != css);
2468 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002470 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002471 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002472 err = single_open(file, cgroup_seqfile_show, cfe);
2473 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002475 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002476
Tejun Heo67f4c362013-08-08 20:11:24 -04002477 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002478 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002479 return err;
2480}
2481
2482static int cgroup_file_release(struct inode *inode, struct file *file)
2483{
Tejun Heof7d58812013-08-08 20:11:23 -04002484 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002486 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002487 int ret = 0;
2488
Paul Menageddbcc7e2007-10-18 23:39:30 -07002489 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002490 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002491 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002492 css_put(css);
2493 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002494}
2495
2496/*
2497 * cgroup_rename - Only allow simple rename of directories in place.
2498 */
2499static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2500 struct inode *new_dir, struct dentry *new_dentry)
2501{
Li Zefan65dff752013-03-01 15:01:56 +08002502 int ret;
2503 struct cgroup_name *name, *old_name;
2504 struct cgroup *cgrp;
2505
2506 /*
2507 * It's convinient to use parent dir's i_mutex to protected
2508 * cgrp->name.
2509 */
2510 lockdep_assert_held(&old_dir->i_mutex);
2511
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2513 return -ENOTDIR;
2514 if (new_dentry->d_inode)
2515 return -EEXIST;
2516 if (old_dir != new_dir)
2517 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002518
2519 cgrp = __d_cgrp(old_dentry);
2520
Tejun Heo6db8e852013-06-14 11:18:22 -07002521 /*
2522 * This isn't a proper migration and its usefulness is very
2523 * limited. Disallow if sane_behavior.
2524 */
2525 if (cgroup_sane_behavior(cgrp))
2526 return -EPERM;
2527
Li Zefan65dff752013-03-01 15:01:56 +08002528 name = cgroup_alloc_name(new_dentry);
2529 if (!name)
2530 return -ENOMEM;
2531
2532 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2533 if (ret) {
2534 kfree(name);
2535 return ret;
2536 }
2537
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002538 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002539 rcu_assign_pointer(cgrp->name, name);
2540
2541 kfree_rcu(old_name, rcu_head);
2542 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002543}
2544
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002545static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2546{
2547 if (S_ISDIR(dentry->d_inode->i_mode))
2548 return &__d_cgrp(dentry)->xattrs;
2549 else
Li Zefan712317a2013-04-18 23:09:52 -07002550 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002551}
2552
2553static inline int xattr_enabled(struct dentry *dentry)
2554{
2555 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002556 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002557}
2558
2559static bool is_valid_xattr(const char *name)
2560{
2561 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2562 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2563 return true;
2564 return false;
2565}
2566
2567static int cgroup_setxattr(struct dentry *dentry, const char *name,
2568 const void *val, size_t size, int flags)
2569{
2570 if (!xattr_enabled(dentry))
2571 return -EOPNOTSUPP;
2572 if (!is_valid_xattr(name))
2573 return -EINVAL;
2574 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2575}
2576
2577static int cgroup_removexattr(struct dentry *dentry, const char *name)
2578{
2579 if (!xattr_enabled(dentry))
2580 return -EOPNOTSUPP;
2581 if (!is_valid_xattr(name))
2582 return -EINVAL;
2583 return simple_xattr_remove(__d_xattrs(dentry), name);
2584}
2585
2586static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2587 void *buf, size_t size)
2588{
2589 if (!xattr_enabled(dentry))
2590 return -EOPNOTSUPP;
2591 if (!is_valid_xattr(name))
2592 return -EINVAL;
2593 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2594}
2595
2596static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2597{
2598 if (!xattr_enabled(dentry))
2599 return -EOPNOTSUPP;
2600 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2601}
2602
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002603static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002604 .read = cgroup_file_read,
2605 .write = cgroup_file_write,
2606 .llseek = generic_file_llseek,
2607 .open = cgroup_file_open,
2608 .release = cgroup_file_release,
2609};
2610
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002611static const struct inode_operations cgroup_file_inode_operations = {
2612 .setxattr = cgroup_setxattr,
2613 .getxattr = cgroup_getxattr,
2614 .listxattr = cgroup_listxattr,
2615 .removexattr = cgroup_removexattr,
2616};
2617
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002618static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002619 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002620 .mkdir = cgroup_mkdir,
2621 .rmdir = cgroup_rmdir,
2622 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002623 .setxattr = cgroup_setxattr,
2624 .getxattr = cgroup_getxattr,
2625 .listxattr = cgroup_listxattr,
2626 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002627};
2628
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002629/*
2630 * Check if a file is a control file
2631 */
2632static inline struct cftype *__file_cft(struct file *file)
2633{
Al Viro496ad9a2013-01-23 17:07:38 -05002634 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002635 return ERR_PTR(-EINVAL);
2636 return __d_cft(file->f_dentry);
2637}
2638
Al Viroa5e7ed32011-07-26 01:55:55 -04002639static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002640 struct super_block *sb)
2641{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002642 struct inode *inode;
2643
2644 if (!dentry)
2645 return -ENOENT;
2646 if (dentry->d_inode)
2647 return -EEXIST;
2648
2649 inode = cgroup_new_inode(mode, sb);
2650 if (!inode)
2651 return -ENOMEM;
2652
2653 if (S_ISDIR(mode)) {
2654 inode->i_op = &cgroup_dir_inode_operations;
2655 inode->i_fop = &simple_dir_operations;
2656
2657 /* start off with i_nlink == 2 (for "." entry) */
2658 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002659 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660
Tejun Heob8a2df62012-11-19 08:13:37 -08002661 /*
2662 * Control reaches here with cgroup_mutex held.
2663 * @inode->i_mutex should nest outside cgroup_mutex but we
2664 * want to populate it immediately without releasing
2665 * cgroup_mutex. As @inode isn't visible to anyone else
2666 * yet, trylock will always succeed without affecting
2667 * lockdep checks.
2668 */
2669 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 } else if (S_ISREG(mode)) {
2671 inode->i_size = 0;
2672 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002673 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675 d_instantiate(dentry, inode);
2676 dget(dentry); /* Extra count - pin the dentry in core */
2677 return 0;
2678}
2679
Li Zefan099fca32009-04-02 16:57:29 -07002680/**
2681 * cgroup_file_mode - deduce file mode of a control file
2682 * @cft: the control file in question
2683 *
2684 * returns cft->mode if ->mode is not 0
2685 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2686 * returns S_IRUGO if it has only a read handler
2687 * returns S_IWUSR if it has only a write hander
2688 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002689static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002690{
Al Viroa5e7ed32011-07-26 01:55:55 -04002691 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002692
2693 if (cft->mode)
2694 return cft->mode;
2695
2696 if (cft->read || cft->read_u64 || cft->read_s64 ||
2697 cft->read_map || cft->read_seq_string)
2698 mode |= S_IRUGO;
2699
2700 if (cft->write || cft->write_u64 || cft->write_s64 ||
2701 cft->write_string || cft->trigger)
2702 mode |= S_IWUSR;
2703
2704 return mode;
2705}
2706
Tejun Heo2bb566c2013-08-08 20:11:23 -04002707static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708{
Paul Menagebd89aab2007-10-18 23:40:44 -07002709 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002710 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002712 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002714 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002715 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002716
Tejun Heo9fa4db32013-08-26 18:40:56 -04002717 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2718 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002719 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002720 strcat(name, ".");
2721 }
2722 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725
2726 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2727 if (!cfe)
2728 return -ENOMEM;
2729
Paul Menageddbcc7e2007-10-18 23:39:30 -07002730 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733 goto out;
2734 }
2735
Li Zefand6cbf352013-05-14 19:44:20 +08002736 cfe->type = (void *)cft;
2737 cfe->dentry = dentry;
2738 dentry->d_fsdata = cfe;
2739 simple_xattrs_init(&cfe->xattrs);
2740
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 mode = cgroup_file_mode(cft);
2742 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2743 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 list_add_tail(&cfe->node, &parent->files);
2745 cfe = NULL;
2746 }
2747 dput(dentry);
2748out:
2749 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 return error;
2751}
2752
Tejun Heob1f28d32013-06-28 16:24:10 -07002753/**
2754 * cgroup_addrm_files - add or remove files to a cgroup directory
2755 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002756 * @cfts: array of cftypes to be added
2757 * @is_add: whether to add or remove
2758 *
2759 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002760 * For removals, this function never fails. If addition fails, this
2761 * function doesn't remove files already added. The caller is responsible
2762 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002763 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002764static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2765 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002767 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002768 int ret;
2769
2770 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2771 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002772
2773 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002774 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002775 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2776 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002777 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2778 continue;
2779 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2780 continue;
2781
Li Zefan2739d3c2013-01-21 18:18:33 +08002782 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002783 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002784 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002785 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002786 cft->name, ret);
2787 return ret;
2788 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002789 } else {
2790 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002791 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002792 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002793 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002794}
2795
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002797 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002798{
2799 /*
2800 * Thanks to the entanglement with vfs inode locking, we can't walk
2801 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002802 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2803 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002804 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002805 mutex_lock(&cgroup_mutex);
2806}
2807
Tejun Heo2bb566c2013-08-08 20:11:23 -04002808static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002809 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002810{
2811 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002812 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002813 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002814 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002815 struct dentry *prev = NULL;
2816 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002817 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002818 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002819 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002820
2821 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002822 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002823 !atomic_inc_not_zero(&sb->s_active)) {
2824 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002825 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002826 }
2827
Li Zefane8c82d22013-06-18 18:48:37 +08002828 /*
2829 * All cgroups which are created after we drop cgroup_mutex will
2830 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002831 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002832 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002833 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002834
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835 mutex_unlock(&cgroup_mutex);
2836
Li Zefane8c82d22013-06-18 18:48:37 +08002837 /* add/rm files for all cgroups created before */
2838 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002839 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002840 struct cgroup *cgrp = css->cgroup;
2841
Li Zefane8c82d22013-06-18 18:48:37 +08002842 if (cgroup_is_dead(cgrp))
2843 continue;
2844
2845 inode = cgrp->dentry->d_inode;
2846 dget(cgrp->dentry);
2847 rcu_read_unlock();
2848
2849 dput(prev);
2850 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002851
2852 mutex_lock(&inode->i_mutex);
2853 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002854 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002855 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002856 mutex_unlock(&cgroup_mutex);
2857 mutex_unlock(&inode->i_mutex);
2858
Li Zefane8c82d22013-06-18 18:48:37 +08002859 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002860 if (ret)
2861 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862 }
Li Zefane8c82d22013-06-18 18:48:37 +08002863 rcu_read_unlock();
2864 dput(prev);
2865 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002866 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002867}
2868
2869/**
2870 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2871 * @ss: target cgroup subsystem
2872 * @cfts: zero-length name terminated array of cftypes
2873 *
2874 * Register @cfts to @ss. Files described by @cfts are created for all
2875 * existing cgroups to which @ss is attached and all future cgroups will
2876 * have them too. This function can be called anytime whether @ss is
2877 * attached or not.
2878 *
2879 * Returns 0 on successful registration, -errno on failure. Note that this
2880 * function currently returns 0 as long as @cfts registration is successful
2881 * even if some file creation attempts on existing cgroups fail.
2882 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002883int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002884{
2885 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002886 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002887 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002888
2889 set = kzalloc(sizeof(*set), GFP_KERNEL);
2890 if (!set)
2891 return -ENOMEM;
2892
Tejun Heo2bb566c2013-08-08 20:11:23 -04002893 for (cft = cfts; cft->name[0] != '\0'; cft++)
2894 cft->ss = ss;
2895
Tejun Heo8e3f6542012-04-01 12:09:55 -07002896 cgroup_cfts_prepare();
2897 set->cfts = cfts;
2898 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002899 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002900 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002901 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002902 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002903}
2904EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2905
Li Zefana043e3b2008-02-23 15:24:09 -08002906/**
Tejun Heo79578622012-04-01 12:09:56 -07002907 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002908 * @cfts: zero-length name terminated array of cftypes
2909 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002910 * Unregister @cfts. Files described by @cfts are removed from all
2911 * existing cgroups and all future cgroups won't have them either. This
2912 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002913 *
2914 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002915 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002916 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002917int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002918{
2919 struct cftype_set *set;
2920
Tejun Heo2bb566c2013-08-08 20:11:23 -04002921 if (!cfts || !cfts[0].ss)
2922 return -ENOENT;
2923
Tejun Heo79578622012-04-01 12:09:56 -07002924 cgroup_cfts_prepare();
2925
Tejun Heo2bb566c2013-08-08 20:11:23 -04002926 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002927 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002928 list_del(&set->node);
2929 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002930 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002931 return 0;
2932 }
2933 }
2934
Tejun Heo2bb566c2013-08-08 20:11:23 -04002935 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002936 return -ENOENT;
2937}
2938
2939/**
Li Zefana043e3b2008-02-23 15:24:09 -08002940 * cgroup_task_count - count the number of tasks in a cgroup.
2941 * @cgrp: the cgroup in question
2942 *
2943 * Return the number of tasks in the cgroup.
2944 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002945int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002946{
2947 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002948 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002949
Paul Menage817929e2007-10-18 23:39:36 -07002950 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002951 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2952 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002953 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002954 return count;
2955}
2956
2957/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002958 * To reduce the fork() overhead for systems that are not actually using
2959 * their cgroups capability, we don't maintain the lists running through
2960 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002961 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002962 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002963static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964{
2965 struct task_struct *p, *g;
2966 write_lock(&css_set_lock);
2967 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002968 /*
2969 * We need tasklist_lock because RCU is not safe against
2970 * while_each_thread(). Besides, a forking task that has passed
2971 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2972 * is not guaranteed to have its child immediately visible in the
2973 * tasklist if we walk through it with RCU.
2974 */
2975 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002976 do_each_thread(g, p) {
2977 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002978 /*
2979 * We should check if the process is exiting, otherwise
2980 * it will race with cgroup_exit() in that the list
2981 * entry won't be deleted though the process has exited.
2982 */
2983 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002984 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002985 task_unlock(p);
2986 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002987 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002988 write_unlock(&css_set_lock);
2989}
2990
Tejun Heo574bd9f2012-11-09 09:12:29 -08002991/**
Tejun Heo492eb212013-08-08 20:11:25 -04002992 * css_next_child - find the next child of a given css
2993 * @pos_css: the current position (%NULL to initiate traversal)
2994 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002995 *
Tejun Heo492eb212013-08-08 20:11:25 -04002996 * This function returns the next child of @parent_css and should be called
2997 * under RCU read lock. The only requirement is that @parent_css and
2998 * @pos_css are accessible. The next sibling is guaranteed to be returned
2999 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003000 */
Tejun Heo492eb212013-08-08 20:11:25 -04003001struct cgroup_subsys_state *
3002css_next_child(struct cgroup_subsys_state *pos_css,
3003 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003004{
Tejun Heo492eb212013-08-08 20:11:25 -04003005 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3006 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003007 struct cgroup *next;
3008
3009 WARN_ON_ONCE(!rcu_read_lock_held());
3010
3011 /*
3012 * @pos could already have been removed. Once a cgroup is removed,
3013 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003014 * changes. As CGRP_DEAD assertion is serialized and happens
3015 * before the cgroup is taken off the ->sibling list, if we see it
3016 * unasserted, it's guaranteed that the next sibling hasn't
3017 * finished its grace period even if it's already removed, and thus
3018 * safe to dereference from this RCU critical section. If
3019 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3020 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003021 *
3022 * If @pos is dead, its next pointer can't be dereferenced;
3023 * however, as each cgroup is given a monotonically increasing
3024 * unique serial number and always appended to the sibling list,
3025 * the next one can be found by walking the parent's children until
3026 * we see a cgroup with higher serial number than @pos's. While
3027 * this path can be slower, it's taken only when either the current
3028 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003029 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003030 if (!pos) {
3031 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3032 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003033 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003034 } else {
3035 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3036 if (next->serial_nr > pos->serial_nr)
3037 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003038 }
3039
Tejun Heo492eb212013-08-08 20:11:25 -04003040 if (&next->sibling == &cgrp->children)
3041 return NULL;
3042
Tejun Heoca8bdca2013-08-26 18:40:56 -04003043 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003044}
Tejun Heo492eb212013-08-08 20:11:25 -04003045EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003046
3047/**
Tejun Heo492eb212013-08-08 20:11:25 -04003048 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003049 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003050 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003051 *
Tejun Heo492eb212013-08-08 20:11:25 -04003052 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003053 * to visit for pre-order traversal of @root's descendants. @root is
3054 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003055 *
3056 * While this function requires RCU read locking, it doesn't require the
3057 * whole traversal to be contained in a single RCU critical section. This
3058 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003059 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003060 */
Tejun Heo492eb212013-08-08 20:11:25 -04003061struct cgroup_subsys_state *
3062css_next_descendant_pre(struct cgroup_subsys_state *pos,
3063 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064{
Tejun Heo492eb212013-08-08 20:11:25 -04003065 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003066
3067 WARN_ON_ONCE(!rcu_read_lock_held());
3068
Tejun Heobd8815a2013-08-08 20:11:27 -04003069 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003070 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003071 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003072
3073 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003074 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075 if (next)
3076 return next;
3077
3078 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003079 while (pos != root) {
3080 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003081 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003083 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003084 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085
3086 return NULL;
3087}
Tejun Heo492eb212013-08-08 20:11:25 -04003088EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003089
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003090/**
Tejun Heo492eb212013-08-08 20:11:25 -04003091 * css_rightmost_descendant - return the rightmost descendant of a css
3092 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003093 *
Tejun Heo492eb212013-08-08 20:11:25 -04003094 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3095 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003096 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003097 *
3098 * While this function requires RCU read locking, it doesn't require the
3099 * whole traversal to be contained in a single RCU critical section. This
3100 * function will return the correct rightmost descendant as long as @pos is
3101 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003102 */
Tejun Heo492eb212013-08-08 20:11:25 -04003103struct cgroup_subsys_state *
3104css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003105{
Tejun Heo492eb212013-08-08 20:11:25 -04003106 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003107
3108 WARN_ON_ONCE(!rcu_read_lock_held());
3109
3110 do {
3111 last = pos;
3112 /* ->prev isn't RCU safe, walk ->next till the end */
3113 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003114 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003115 pos = tmp;
3116 } while (pos);
3117
3118 return last;
3119}
Tejun Heo492eb212013-08-08 20:11:25 -04003120EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003121
Tejun Heo492eb212013-08-08 20:11:25 -04003122static struct cgroup_subsys_state *
3123css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003124{
Tejun Heo492eb212013-08-08 20:11:25 -04003125 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003126
3127 do {
3128 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003129 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003130 } while (pos);
3131
3132 return last;
3133}
3134
3135/**
Tejun Heo492eb212013-08-08 20:11:25 -04003136 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003137 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003138 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003139 *
Tejun Heo492eb212013-08-08 20:11:25 -04003140 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003141 * to visit for post-order traversal of @root's descendants. @root is
3142 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003143 *
3144 * While this function requires RCU read locking, it doesn't require the
3145 * whole traversal to be contained in a single RCU critical section. This
3146 * function will return the correct next descendant as long as both @pos
3147 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003148 */
Tejun Heo492eb212013-08-08 20:11:25 -04003149struct cgroup_subsys_state *
3150css_next_descendant_post(struct cgroup_subsys_state *pos,
3151 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003152{
Tejun Heo492eb212013-08-08 20:11:25 -04003153 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003154
3155 WARN_ON_ONCE(!rcu_read_lock_held());
3156
Tejun Heo58b79a92013-09-06 15:31:08 -04003157 /* if first iteration, visit leftmost descendant which may be @root */
3158 if (!pos)
3159 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003160
Tejun Heobd8815a2013-08-08 20:11:27 -04003161 /* if we visited @root, we're done */
3162 if (pos == root)
3163 return NULL;
3164
Tejun Heo574bd9f2012-11-09 09:12:29 -08003165 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003166 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003167 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003168 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003169
3170 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003171 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172}
Tejun Heo492eb212013-08-08 20:11:25 -04003173EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174
Tejun Heo0942eee2013-08-08 20:11:26 -04003175/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003176 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003177 * @it: the iterator to advance
3178 *
3179 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003180 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003181static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003182{
3183 struct list_head *l = it->cset_link;
3184 struct cgrp_cset_link *link;
3185 struct css_set *cset;
3186
3187 /* Advance to the next non-empty css_set */
3188 do {
3189 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003190 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003191 it->cset_link = NULL;
3192 return;
3193 }
3194 link = list_entry(l, struct cgrp_cset_link, cset_link);
3195 cset = link->cset;
3196 } while (list_empty(&cset->tasks));
3197 it->cset_link = l;
3198 it->task = cset->tasks.next;
3199}
3200
Tejun Heo0942eee2013-08-08 20:11:26 -04003201/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003202 * css_task_iter_start - initiate task iteration
3203 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003204 * @it: the task iterator to use
3205 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003206 * Initiate iteration through the tasks of @css. The caller can call
3207 * css_task_iter_next() to walk through the tasks until the function
3208 * returns NULL. On completion of iteration, css_task_iter_end() must be
3209 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003210 *
3211 * Note that this function acquires a lock which is released when the
3212 * iteration finishes. The caller can't sleep while iteration is in
3213 * progress.
3214 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003215void css_task_iter_start(struct cgroup_subsys_state *css,
3216 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003217 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003218{
3219 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003220 * The first time anyone tries to iterate across a css, we need to
3221 * enable the list linking each css_set to its tasks, and fix up
3222 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003223 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003224 if (!use_task_css_set_links)
3225 cgroup_enable_task_cg_lists();
3226
Paul Menage817929e2007-10-18 23:39:36 -07003227 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003228
Tejun Heo72ec7022013-08-08 20:11:26 -04003229 it->origin_css = css;
3230 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003231
Tejun Heo72ec7022013-08-08 20:11:26 -04003232 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003233}
3234
Tejun Heo0942eee2013-08-08 20:11:26 -04003235/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003236 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003237 * @it: the task iterator being iterated
3238 *
3239 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003240 * initialized via css_task_iter_start(). Returns NULL when the iteration
3241 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003242 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003243struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003244{
3245 struct task_struct *res;
3246 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003247 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003248
3249 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003250 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003251 return NULL;
3252 res = list_entry(l, struct task_struct, cg_list);
3253 /* Advance iterator to find next entry */
3254 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003255 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3256 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003257 /*
3258 * We reached the end of this task list - move on to the
3259 * next cgrp_cset_link.
3260 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003261 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003262 } else {
3263 it->task = l;
3264 }
3265 return res;
3266}
3267
Tejun Heo0942eee2013-08-08 20:11:26 -04003268/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003269 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003270 * @it: the task iterator to finish
3271 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003272 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003273 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003274void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003275 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003276{
3277 read_unlock(&css_set_lock);
3278}
3279
Cliff Wickman31a7df02008-02-07 00:14:42 -08003280static inline int started_after_time(struct task_struct *t1,
3281 struct timespec *time,
3282 struct task_struct *t2)
3283{
3284 int start_diff = timespec_compare(&t1->start_time, time);
3285 if (start_diff > 0) {
3286 return 1;
3287 } else if (start_diff < 0) {
3288 return 0;
3289 } else {
3290 /*
3291 * Arbitrarily, if two processes started at the same
3292 * time, we'll say that the lower pointer value
3293 * started first. Note that t2 may have exited by now
3294 * so this may not be a valid pointer any longer, but
3295 * that's fine - it still serves to distinguish
3296 * between two tasks started (effectively) simultaneously.
3297 */
3298 return t1 > t2;
3299 }
3300}
3301
3302/*
3303 * This function is a callback from heap_insert() and is used to order
3304 * the heap.
3305 * In this case we order the heap in descending task start time.
3306 */
3307static inline int started_after(void *p1, void *p2)
3308{
3309 struct task_struct *t1 = p1;
3310 struct task_struct *t2 = p2;
3311 return started_after_time(t1, &t2->start_time, t2);
3312}
3313
3314/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003315 * css_scan_tasks - iterate though all the tasks in a css
3316 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003317 * @test: optional test callback
3318 * @process: process callback
3319 * @data: data passed to @test and @process
3320 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003321 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003322 * Iterate through all the tasks in @css, calling @test for each, and if it
3323 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003324 *
Tejun Heoe5358372013-08-08 20:11:26 -04003325 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003326 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003327 * lock css_set_lock for the call to @process.
3328 *
3329 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003330 * of @css for the duration of this call. This function may or may not
3331 * call @process for tasks that exit or move to a different css during the
3332 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003333 *
3334 * Note that @test may be called with locks held, and may in some
3335 * situations be called multiple times for the same task, so it should be
3336 * cheap.
3337 *
3338 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3339 * heap operations (and its "gt" member will be overwritten), else a
3340 * temporary heap will be used (allocation of which may cause this function
3341 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003342 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003343int css_scan_tasks(struct cgroup_subsys_state *css,
3344 bool (*test)(struct task_struct *, void *),
3345 void (*process)(struct task_struct *, void *),
3346 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003347{
3348 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003349 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350 struct task_struct *p, *dropped;
3351 /* Never dereference latest_task, since it's not refcounted */
3352 struct task_struct *latest_task = NULL;
3353 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003354 struct timespec latest_time = { 0, 0 };
3355
Tejun Heoe5358372013-08-08 20:11:26 -04003356 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003358 heap->gt = &started_after;
3359 } else {
3360 /* We need to allocate our own heap memory */
3361 heap = &tmp_heap;
3362 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3363 if (retval)
3364 /* cannot allocate the heap */
3365 return retval;
3366 }
3367
3368 again:
3369 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003370 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003371 * which are of interest, and invoking @process callback on the
3372 * ones which need an update. Since we don't want to hold any
3373 * locks during the task updates, gather tasks to be processed in a
3374 * heap structure. The heap is sorted by descending task start
3375 * time. If the statically-sized heap fills up, we overflow tasks
3376 * that started later, and in future iterations only consider tasks
3377 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003378 * guarantees forward progress and that we don't miss any tasks.
3379 */
3380 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003381 css_task_iter_start(css, &it);
3382 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003383 /*
3384 * Only affect tasks that qualify per the caller's callback,
3385 * if he provided one
3386 */
Tejun Heoe5358372013-08-08 20:11:26 -04003387 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003388 continue;
3389 /*
3390 * Only process tasks that started after the last task
3391 * we processed
3392 */
3393 if (!started_after_time(p, &latest_time, latest_task))
3394 continue;
3395 dropped = heap_insert(heap, p);
3396 if (dropped == NULL) {
3397 /*
3398 * The new task was inserted; the heap wasn't
3399 * previously full
3400 */
3401 get_task_struct(p);
3402 } else if (dropped != p) {
3403 /*
3404 * The new task was inserted, and pushed out a
3405 * different task
3406 */
3407 get_task_struct(p);
3408 put_task_struct(dropped);
3409 }
3410 /*
3411 * Else the new task was newer than anything already in
3412 * the heap and wasn't inserted
3413 */
3414 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003415 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003416
3417 if (heap->size) {
3418 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003419 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003420 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003421 latest_time = q->start_time;
3422 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003423 }
3424 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003425 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003426 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003427 }
3428 /*
3429 * If we had to process any tasks at all, scan again
3430 * in case some of them were in the middle of forking
3431 * children that didn't get processed.
3432 * Not the most efficient way to do it, but it avoids
3433 * having to take callback_mutex in the fork path
3434 */
3435 goto again;
3436 }
3437 if (heap == &tmp_heap)
3438 heap_free(&tmp_heap);
3439 return 0;
3440}
3441
Tejun Heoe5358372013-08-08 20:11:26 -04003442static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003443{
Tejun Heoe5358372013-08-08 20:11:26 -04003444 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003445
Tejun Heo47cfcd02013-04-07 09:29:51 -07003446 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003447 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003448 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003449}
3450
3451/**
3452 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3453 * @to: cgroup to which the tasks will be moved
3454 * @from: cgroup in which the tasks currently reside
3455 */
3456int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3457{
Tejun Heo72ec7022013-08-08 20:11:26 -04003458 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3459 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003460}
3461
Paul Menage817929e2007-10-18 23:39:36 -07003462/*
Ben Blum102a7752009-09-23 15:56:26 -07003463 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003464 *
3465 * Reading this file can return large amounts of data if a cgroup has
3466 * *lots* of attached tasks. So it may need several calls to read(),
3467 * but we cannot guarantee that the information we produce is correct
3468 * unless we produce it entirely atomically.
3469 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003470 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003471
Li Zefan24528252012-01-20 11:58:43 +08003472/* which pidlist file are we talking about? */
3473enum cgroup_filetype {
3474 CGROUP_FILE_PROCS,
3475 CGROUP_FILE_TASKS,
3476};
3477
3478/*
3479 * A pidlist is a list of pids that virtually represents the contents of one
3480 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3481 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3482 * to the cgroup.
3483 */
3484struct cgroup_pidlist {
3485 /*
3486 * used to find which pidlist is wanted. doesn't change as long as
3487 * this particular list stays in the list.
3488 */
3489 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3490 /* array of xids */
3491 pid_t *list;
3492 /* how many elements the above list has */
3493 int length;
3494 /* how many files are using the current array */
3495 int use_count;
3496 /* each of these stored in a list by its cgroup */
3497 struct list_head links;
3498 /* pointer to the cgroup we belong to, for list removal purposes */
3499 struct cgroup *owner;
3500 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003501 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003502};
3503
Paul Menagebbcb81d2007-10-18 23:39:32 -07003504/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003505 * The following two functions "fix" the issue where there are more pids
3506 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3507 * TODO: replace with a kernel-wide solution to this problem
3508 */
3509#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3510static void *pidlist_allocate(int count)
3511{
3512 if (PIDLIST_TOO_LARGE(count))
3513 return vmalloc(count * sizeof(pid_t));
3514 else
3515 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3516}
3517static void pidlist_free(void *p)
3518{
3519 if (is_vmalloc_addr(p))
3520 vfree(p);
3521 else
3522 kfree(p);
3523}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003524
3525/*
Ben Blum102a7752009-09-23 15:56:26 -07003526 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003527 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003528 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003529static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003530{
Ben Blum102a7752009-09-23 15:56:26 -07003531 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003532
3533 /*
3534 * we presume the 0th element is unique, so i starts at 1. trivial
3535 * edge cases first; no work needs to be done for either
3536 */
3537 if (length == 0 || length == 1)
3538 return length;
3539 /* src and dest walk down the list; dest counts unique elements */
3540 for (src = 1; src < length; src++) {
3541 /* find next unique element */
3542 while (list[src] == list[src-1]) {
3543 src++;
3544 if (src == length)
3545 goto after;
3546 }
3547 /* dest always points to where the next unique element goes */
3548 list[dest] = list[src];
3549 dest++;
3550 }
3551after:
Ben Blum102a7752009-09-23 15:56:26 -07003552 return dest;
3553}
3554
3555static int cmppid(const void *a, const void *b)
3556{
3557 return *(pid_t *)a - *(pid_t *)b;
3558}
3559
3560/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003561 * find the appropriate pidlist for our purpose (given procs vs tasks)
3562 * returns with the lock on that pidlist already held, and takes care
3563 * of the use count, or returns NULL with no locks held if we're out of
3564 * memory.
3565 */
3566static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3567 enum cgroup_filetype type)
3568{
3569 struct cgroup_pidlist *l;
3570 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003571 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003572
Ben Blum72a8cb32009-09-23 15:56:27 -07003573 /*
Li Zefanb3958902013-08-01 09:52:15 +08003574 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003575 * the last ref-holder is trying to remove l from the list at the same
3576 * time. Holding the pidlist_mutex precludes somebody taking whichever
3577 * list we find out from under us - compare release_pid_array().
3578 */
3579 mutex_lock(&cgrp->pidlist_mutex);
3580 list_for_each_entry(l, &cgrp->pidlists, links) {
3581 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003583 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003585 return l;
3586 }
3587 }
3588 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003589 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 if (!l) {
3591 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003592 return l;
3593 }
Li Zefanb3958902013-08-01 09:52:15 +08003594 init_rwsem(&l->rwsem);
3595 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003596 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003597 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003598 l->owner = cgrp;
3599 list_add(&l->links, &cgrp->pidlists);
3600 mutex_unlock(&cgrp->pidlist_mutex);
3601 return l;
3602}
3603
3604/*
Ben Blum102a7752009-09-23 15:56:26 -07003605 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3606 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003607static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3608 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003609{
3610 pid_t *array;
3611 int length;
3612 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003613 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003614 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003615 struct cgroup_pidlist *l;
3616
3617 /*
3618 * If cgroup gets more users after we read count, we won't have
3619 * enough space - tough. This race is indistinguishable to the
3620 * caller from the case that the additional cgroup users didn't
3621 * show up until sometime later on.
3622 */
3623 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003624 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003625 if (!array)
3626 return -ENOMEM;
3627 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003628 css_task_iter_start(&cgrp->dummy_css, &it);
3629 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003630 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003631 break;
Ben Blum102a7752009-09-23 15:56:26 -07003632 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003633 if (type == CGROUP_FILE_PROCS)
3634 pid = task_tgid_vnr(tsk);
3635 else
3636 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003637 if (pid > 0) /* make sure to only use valid results */
3638 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003639 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003640 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003641 length = n;
3642 /* now sort & (if procs) strip out duplicates */
3643 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003645 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 l = cgroup_pidlist_find(cgrp, type);
3647 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003648 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003650 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003651 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003652 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003653 l->list = array;
3654 l->length = length;
3655 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003656 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003658 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003659}
3660
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661/**
Li Zefana043e3b2008-02-23 15:24:09 -08003662 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003663 * @stats: cgroupstats to fill information into
3664 * @dentry: A dentry entry belonging to the cgroup for which stats have
3665 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003666 *
3667 * Build and fill cgroupstats so that taskstats can export it to user
3668 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669 */
3670int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3671{
3672 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003673 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003674 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003675 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003676
Balbir Singh846c7bb2007-10-18 23:39:44 -07003677 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003678 * Validate dentry by checking the superblock operations,
3679 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680 */
Li Zefan33d283b2008-11-19 15:36:48 -08003681 if (dentry->d_sb->s_op != &cgroup_ops ||
3682 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003683 goto err;
3684
3685 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003686 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687
Tejun Heo72ec7022013-08-08 20:11:26 -04003688 css_task_iter_start(&cgrp->dummy_css, &it);
3689 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690 switch (tsk->state) {
3691 case TASK_RUNNING:
3692 stats->nr_running++;
3693 break;
3694 case TASK_INTERRUPTIBLE:
3695 stats->nr_sleeping++;
3696 break;
3697 case TASK_UNINTERRUPTIBLE:
3698 stats->nr_uninterruptible++;
3699 break;
3700 case TASK_STOPPED:
3701 stats->nr_stopped++;
3702 break;
3703 default:
3704 if (delayacct_is_task_waiting_on_io(tsk))
3705 stats->nr_io_wait++;
3706 break;
3707 }
3708 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003709 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710
Balbir Singh846c7bb2007-10-18 23:39:44 -07003711err:
3712 return ret;
3713}
3714
Paul Menage8f3ff202009-09-23 15:56:25 -07003715
Paul Menagecc31edc2008-10-18 20:28:04 -07003716/*
Ben Blum102a7752009-09-23 15:56:26 -07003717 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003718 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003719 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003720 */
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003723{
3724 /*
3725 * Initially we receive a position value that corresponds to
3726 * one more than the last pid shown (or 0 on the first call or
3727 * after a seek to the start). Use a binary-search to find the
3728 * next pid to display, if any
3729 */
Ben Blum102a7752009-09-23 15:56:26 -07003730 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003731 int index = 0, pid = *pos;
3732 int *iter;
3733
Li Zefanb3958902013-08-01 09:52:15 +08003734 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003736 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003737
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 while (index < end) {
3739 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003740 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 index = mid;
3742 break;
Ben Blum102a7752009-09-23 15:56:26 -07003743 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 index = mid + 1;
3745 else
3746 end = mid;
3747 }
3748 }
3749 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003750 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003751 return NULL;
3752 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003753 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003754 *pos = *iter;
3755 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003756}
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003759{
Ben Blum102a7752009-09-23 15:56:26 -07003760 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003761 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003762}
3763
Ben Blum102a7752009-09-23 15:56:26 -07003764static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003765{
Ben Blum102a7752009-09-23 15:56:26 -07003766 struct cgroup_pidlist *l = s->private;
3767 pid_t *p = v;
3768 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 /*
3770 * Advance to the next pid in the array. If this goes off the
3771 * end, we're done
3772 */
3773 p++;
3774 if (p >= end) {
3775 return NULL;
3776 } else {
3777 *pos = *p;
3778 return p;
3779 }
3780}
3781
Ben Blum102a7752009-09-23 15:56:26 -07003782static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783{
3784 return seq_printf(s, "%d\n", *(int *)v);
3785}
3786
Ben Blum102a7752009-09-23 15:56:26 -07003787/*
3788 * seq_operations functions for iterating on pidlists through seq_file -
3789 * independent of whether it's tasks or procs
3790 */
3791static const struct seq_operations cgroup_pidlist_seq_operations = {
3792 .start = cgroup_pidlist_start,
3793 .stop = cgroup_pidlist_stop,
3794 .next = cgroup_pidlist_next,
3795 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003796};
3797
Ben Blum102a7752009-09-23 15:56:26 -07003798static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003799{
Ben Blum72a8cb32009-09-23 15:56:27 -07003800 /*
3801 * the case where we're the last user of this particular pidlist will
3802 * have us remove it from the cgroup's list, which entails taking the
3803 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3804 * pidlist_mutex, we have to take pidlist_mutex first.
3805 */
3806 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003807 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003808 BUG_ON(!l->use_count);
3809 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003810 /* we're the last user if refcount is 0; remove and free */
3811 list_del(&l->links);
3812 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003813 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003814 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003815 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003816 kfree(l);
3817 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003820 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003821}
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003824{
Ben Blum102a7752009-09-23 15:56:26 -07003825 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003826 if (!(file->f_mode & FMODE_READ))
3827 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003828 /*
3829 * the seq_file will only be initialized if the file was opened for
3830 * reading; hence we check if it's not null only in that case.
3831 */
3832 l = ((struct seq_file *)file->private_data)->private;
3833 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003834 return seq_release(inode, file);
3835}
3836
Ben Blum102a7752009-09-23 15:56:26 -07003837static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 .read = seq_read,
3839 .llseek = seq_lseek,
3840 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003841 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003842};
3843
3844/*
Ben Blum102a7752009-09-23 15:56:26 -07003845 * The following functions handle opens on a file that displays a pidlist
3846 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3847 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003848 */
Ben Blum102a7752009-09-23 15:56:26 -07003849/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003850static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003851{
3852 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003853 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003854 int retval;
3855
3856 /* Nothing to do for write-only files */
3857 if (!(file->f_mode & FMODE_READ))
3858 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003859
Ben Blum102a7752009-09-23 15:56:26 -07003860 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003861 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003862 if (retval)
3863 return retval;
3864 /* configure file information */
3865 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003866
Ben Blum102a7752009-09-23 15:56:26 -07003867 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003869 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003871 }
Ben Blum102a7752009-09-23 15:56:26 -07003872 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003873 return 0;
3874}
Ben Blum102a7752009-09-23 15:56:26 -07003875static int cgroup_tasks_open(struct inode *unused, struct file *file)
3876{
Ben Blum72a8cb32009-09-23 15:56:27 -07003877 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003878}
3879static int cgroup_procs_open(struct inode *unused, struct file *file)
3880{
Ben Blum72a8cb32009-09-23 15:56:27 -07003881 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003882}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003883
Tejun Heo182446d2013-08-08 20:11:24 -04003884static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3885 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003886{
Tejun Heo182446d2013-08-08 20:11:24 -04003887 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003888}
3889
Tejun Heo182446d2013-08-08 20:11:24 -04003890static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3891 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003892{
Tejun Heo182446d2013-08-08 20:11:24 -04003893 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003894 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003895 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003896 else
Tejun Heo182446d2013-08-08 20:11:24 -04003897 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003898 return 0;
3899}
3900
Paul Menagebbcb81d2007-10-18 23:39:32 -07003901/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003902 * When dput() is called asynchronously, if umount has been done and
3903 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3904 * there's a small window that vfs will see the root dentry with non-zero
3905 * refcnt and trigger BUG().
3906 *
3907 * That's why we hold a reference before dput() and drop it right after.
3908 */
3909static void cgroup_dput(struct cgroup *cgrp)
3910{
3911 struct super_block *sb = cgrp->root->sb;
3912
3913 atomic_inc(&sb->s_active);
3914 dput(cgrp->dentry);
3915 deactivate_super(sb);
3916}
3917
3918/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003919 * Unregister event and free resources.
3920 *
3921 * Gets called from workqueue.
3922 */
3923static void cgroup_event_remove(struct work_struct *work)
3924{
3925 struct cgroup_event *event = container_of(work, struct cgroup_event,
3926 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003927 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003928
Li Zefan810cbee2013-02-18 18:56:14 +08003929 remove_wait_queue(event->wqh, &event->wait);
3930
Tejun Heo81eeaf02013-08-08 20:11:26 -04003931 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003932
Li Zefan810cbee2013-02-18 18:56:14 +08003933 /* Notify userspace the event is going away. */
3934 eventfd_signal(event->eventfd, 1);
3935
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003936 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003937 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003938 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003939}
3940
3941/*
3942 * Gets called on POLLHUP on eventfd when user closes it.
3943 *
3944 * Called with wqh->lock held and interrupts disabled.
3945 */
3946static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3947 int sync, void *key)
3948{
3949 struct cgroup_event *event = container_of(wait,
3950 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003951 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003952 unsigned long flags = (unsigned long)key;
3953
3954 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003956 * If the event has been detached at cgroup removal, we
3957 * can simply return knowing the other side will cleanup
3958 * for us.
3959 *
3960 * We can't race against event freeing since the other
3961 * side will require wqh->lock via remove_wait_queue(),
3962 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003963 */
Li Zefan810cbee2013-02-18 18:56:14 +08003964 spin_lock(&cgrp->event_list_lock);
3965 if (!list_empty(&event->list)) {
3966 list_del_init(&event->list);
3967 /*
3968 * We are in atomic context, but cgroup_event_remove()
3969 * may sleep, so we have to call it in workqueue.
3970 */
3971 schedule_work(&event->remove);
3972 }
3973 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003974 }
3975
3976 return 0;
3977}
3978
3979static void cgroup_event_ptable_queue_proc(struct file *file,
3980 wait_queue_head_t *wqh, poll_table *pt)
3981{
3982 struct cgroup_event *event = container_of(pt,
3983 struct cgroup_event, pt);
3984
3985 event->wqh = wqh;
3986 add_wait_queue(wqh, &event->wait);
3987}
3988
3989/*
3990 * Parse input and register new cgroup event handler.
3991 *
3992 * Input must be in format '<event_fd> <control_fd> <args>'.
3993 * Interpretation of args is defined by control file implementation.
3994 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04003995static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04003996 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003997{
Tejun Heo6e6eab02013-08-15 11:43:15 -04003998 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08003999 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04004000 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004001 unsigned int efd, cfd;
Al Viro4e10f3c2013-08-30 12:29:49 -04004002 struct fd efile;
4003 struct fd cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004004 char *endp;
4005 int ret;
4006
4007 efd = simple_strtoul(buffer, &endp, 10);
4008 if (*endp != ' ')
4009 return -EINVAL;
4010 buffer = endp + 1;
4011
4012 cfd = simple_strtoul(buffer, &endp, 10);
4013 if ((*endp != ' ') && (*endp != '\0'))
4014 return -EINVAL;
4015 buffer = endp + 1;
4016
4017 event = kzalloc(sizeof(*event), GFP_KERNEL);
4018 if (!event)
4019 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004020
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004021 INIT_LIST_HEAD(&event->list);
4022 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4023 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4024 INIT_WORK(&event->remove, cgroup_event_remove);
4025
Al Viro4e10f3c2013-08-30 12:29:49 -04004026 efile = fdget(efd);
4027 if (!efile.file) {
4028 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004029 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004030 }
4031
Al Viro4e10f3c2013-08-30 12:29:49 -04004032 event->eventfd = eventfd_ctx_fileget(efile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033 if (IS_ERR(event->eventfd)) {
4034 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004035 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004036 }
4037
Al Viro4e10f3c2013-08-30 12:29:49 -04004038 cfile = fdget(cfd);
4039 if (!cfile.file) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004040 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004041 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004042 }
4043
4044 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004045 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro4e10f3c2013-08-30 12:29:49 -04004046 ret = inode_permission(file_inode(cfile.file), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004047 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004048 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004049
Al Viro4e10f3c2013-08-30 12:29:49 -04004050 event->cft = __file_cft(cfile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004051 if (IS_ERR(event->cft)) {
4052 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004053 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004054 }
4055
Tejun Heo6e6eab02013-08-15 11:43:15 -04004056 if (!event->cft->ss) {
4057 ret = -EBADF;
4058 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004059 }
4060
Li Zefanf1690072013-02-18 14:13:35 +08004061 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004062 * Determine the css of @cfile, verify it belongs to the same
4063 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004064 * Remaining events are automatically removed on cgroup destruction
4065 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004066 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004067 rcu_read_lock();
4068
4069 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004070 event->css = cgroup_css(cgrp, event->cft->ss);
Al Viro4e10f3c2013-08-30 12:29:49 -04004071 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004072 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004073 ret = 0;
4074
4075 rcu_read_unlock();
4076 if (ret)
4077 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004078
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004079 if (!event->cft->register_event || !event->cft->unregister_event) {
4080 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004081 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004082 }
4083
Tejun Heo6e6eab02013-08-15 11:43:15 -04004084 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085 event->eventfd, buffer);
4086 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004087 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004088
Al Viro4e10f3c2013-08-30 12:29:49 -04004089 efile.file->f_op->poll(efile.file, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004090
4091 spin_lock(&cgrp->event_list_lock);
4092 list_add(&event->list, &cgrp->event_list);
4093 spin_unlock(&cgrp->event_list_lock);
4094
Al Viro4e10f3c2013-08-30 12:29:49 -04004095 fdput(cfile);
4096 fdput(efile);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004097
4098 return 0;
4099
Tejun Heo7941cb02013-08-26 18:40:56 -04004100out_put_css:
4101 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004102out_put_cfile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004103 fdput(cfile);
Li Zefan876ede82013-08-01 09:51:47 +08004104out_put_eventfd:
4105 eventfd_ctx_put(event->eventfd);
4106out_put_efile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004107 fdput(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004108out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004109 kfree(event);
4110
4111 return ret;
4112}
4113
Tejun Heo182446d2013-08-08 20:11:24 -04004114static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4115 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004116{
Tejun Heo182446d2013-08-08 20:11:24 -04004117 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004118}
4119
Tejun Heo182446d2013-08-08 20:11:24 -04004120static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4121 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004122{
4123 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004124 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004125 else
Tejun Heo182446d2013-08-08 20:11:24 -04004126 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004127 return 0;
4128}
4129
Tejun Heod5c56ce2013-06-03 19:14:34 -07004130static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004131 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004132 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004133 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004134 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004135 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004136 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004137 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004138 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004139 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004140 .write_string = cgroup_write_event_control,
4141 .mode = S_IWUGO,
4142 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004143 {
4144 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004145 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004146 .read_u64 = cgroup_clone_children_read,
4147 .write_u64 = cgroup_clone_children_write,
4148 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004149 {
Tejun Heo873fe092013-04-14 20:15:26 -07004150 .name = "cgroup.sane_behavior",
4151 .flags = CFTYPE_ONLY_ON_ROOT,
4152 .read_seq_string = cgroup_sane_behavior_show,
4153 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004154
4155 /*
4156 * Historical crazy stuff. These don't have "cgroup." prefix and
4157 * don't exist if sane_behavior. If you're depending on these, be
4158 * prepared to be burned.
4159 */
4160 {
4161 .name = "tasks",
4162 .flags = CFTYPE_INSANE, /* use "procs" instead */
4163 .open = cgroup_tasks_open,
4164 .write_u64 = cgroup_tasks_write,
4165 .release = cgroup_pidlist_release,
4166 .mode = S_IRUGO | S_IWUSR,
4167 },
4168 {
4169 .name = "notify_on_release",
4170 .flags = CFTYPE_INSANE,
4171 .read_u64 = cgroup_read_notify_on_release,
4172 .write_u64 = cgroup_write_notify_on_release,
4173 },
Tejun Heo873fe092013-04-14 20:15:26 -07004174 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004175 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004176 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004177 .read_seq_string = cgroup_release_agent_show,
4178 .write_string = cgroup_release_agent_write,
4179 .max_write_len = PATH_MAX,
4180 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004181 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004182};
4183
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004184/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004185 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004186 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004187 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004188 *
4189 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004190 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004191static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004193 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004194 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004195
Tejun Heo8e3f6542012-04-01 12:09:55 -07004196 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004197 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004198 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004199
4200 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004201 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004202
Tejun Heobee55092013-06-28 16:24:11 -07004203 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004204 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004205 if (ret < 0)
4206 goto err;
4207 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004209 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004210err:
4211 cgroup_clear_dir(cgrp, subsys_mask);
4212 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213}
4214
Tejun Heo0c21ead2013-08-13 20:22:51 -04004215/*
4216 * css destruction is four-stage process.
4217 *
4218 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4219 * Implemented in kill_css().
4220 *
4221 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4222 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4223 * by invoking offline_css(). After offlining, the base ref is put.
4224 * Implemented in css_killed_work_fn().
4225 *
4226 * 3. When the percpu_ref reaches zero, the only possible remaining
4227 * accessors are inside RCU read sections. css_release() schedules the
4228 * RCU callback.
4229 *
4230 * 4. After the grace period, the css can be freed. Implemented in
4231 * css_free_work_fn().
4232 *
4233 * It is actually hairier because both step 2 and 4 require process context
4234 * and thus involve punting to css->destroy_work adding two additional
4235 * steps to the already complex sequence.
4236 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004237static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004238{
4239 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004240 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004241 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004242
Tejun Heo0ae78e02013-08-13 11:01:54 -04004243 if (css->parent)
4244 css_put(css->parent);
4245
Tejun Heo0c21ead2013-08-13 20:22:51 -04004246 css->ss->css_free(css);
4247 cgroup_dput(cgrp);
4248}
4249
4250static void css_free_rcu_fn(struct rcu_head *rcu_head)
4251{
4252 struct cgroup_subsys_state *css =
4253 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4254
4255 /*
4256 * css holds an extra ref to @cgrp->dentry which is put on the last
4257 * css_put(). dput() requires process context which we don't have.
4258 */
4259 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004260 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004261}
4262
Tejun Heod3daf282013-06-13 19:39:16 -07004263static void css_release(struct percpu_ref *ref)
4264{
4265 struct cgroup_subsys_state *css =
4266 container_of(ref, struct cgroup_subsys_state, refcnt);
4267
Tejun Heo0c21ead2013-08-13 20:22:51 -04004268 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004269}
4270
Tejun Heo623f9262013-08-13 11:01:55 -04004271static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4272 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273{
Paul Menagebd89aab2007-10-18 23:40:44 -07004274 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004275 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004277
Tejun Heo0ae78e02013-08-13 11:01:54 -04004278 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004279 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004280 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004282
Tejun Heoca8bdca2013-08-26 18:40:56 -04004283 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284}
4285
Li Zefan2a4ac632013-07-31 16:16:40 +08004286/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004287static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004288{
Tejun Heo623f9262013-08-13 11:01:55 -04004289 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004290 int ret = 0;
4291
Tejun Heoa31f2d32012-11-19 08:13:37 -08004292 lockdep_assert_held(&cgroup_mutex);
4293
Tejun Heo92fb9742012-11-19 08:13:38 -08004294 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004295 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004296 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004297 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004298 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004299 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4300 }
Tejun Heob1929db2012-11-19 08:13:38 -08004301 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004302}
4303
Li Zefan2a4ac632013-07-31 16:16:40 +08004304/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004305static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004306{
Tejun Heo623f9262013-08-13 11:01:55 -04004307 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004308
4309 lockdep_assert_held(&cgroup_mutex);
4310
4311 if (!(css->flags & CSS_ONLINE))
4312 return;
4313
Li Zefand7eeac12013-03-12 15:35:59 -07004314 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004315 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004316
Tejun Heoeb954192013-08-08 20:11:23 -04004317 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004318 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004319 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004320}
4321
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322/*
Li Zefana043e3b2008-02-23 15:24:09 -08004323 * cgroup_create - create a cgroup
4324 * @parent: cgroup that will be parent of the new cgroup
4325 * @dentry: dentry of the new cgroup
4326 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327 *
Li Zefana043e3b2008-02-23 15:24:09 -08004328 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004331 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332{
Tejun Heoae7f1642013-08-13 20:22:50 -04004333 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004334 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004335 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 struct cgroupfs_root *root = parent->root;
4337 int err = 0;
4338 struct cgroup_subsys *ss;
4339 struct super_block *sb = root->sb;
4340
Tejun Heo0a950f62012-11-19 09:02:12 -08004341 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004342 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4343 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344 return -ENOMEM;
4345
Li Zefan65dff752013-03-01 15:01:56 +08004346 name = cgroup_alloc_name(dentry);
4347 if (!name)
4348 goto err_free_cgrp;
4349 rcu_assign_pointer(cgrp->name, name);
4350
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004351 /*
4352 * Temporarily set the pointer to NULL, so idr_find() won't return
4353 * a half-baked cgroup.
4354 */
4355 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004356 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004357 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004358
Tejun Heo976c06b2012-11-05 09:16:59 -08004359 /*
4360 * Only live parents can have children. Note that the liveliness
4361 * check isn't strictly necessary because cgroup_mkdir() and
4362 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4363 * anyway so that locking is contained inside cgroup proper and we
4364 * don't get nasty surprises if we ever grow another caller.
4365 */
4366 if (!cgroup_lock_live_group(parent)) {
4367 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004368 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004369 }
4370
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371 /* Grab a reference on the superblock so the hierarchy doesn't
4372 * get deleted on unmount if there are child cgroups. This
4373 * can be done outside cgroup_mutex, since the sb can't
4374 * disappear while someone has an open control file on the
4375 * fs */
4376 atomic_inc(&sb->s_active);
4377
Paul Menagecc31edc2008-10-18 20:28:04 -07004378 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379
Li Zefanfe1c06c2013-01-24 14:30:22 +08004380 dentry->d_fsdata = cgrp;
4381 cgrp->dentry = dentry;
4382
Paul Menagebd89aab2007-10-18 23:40:44 -07004383 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004384 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004385 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386
Li Zefanb6abdb02008-03-04 14:28:19 -08004387 if (notify_on_release(parent))
4388 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4389
Tejun Heo2260e7f2012-11-19 08:13:38 -08004390 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4391 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004392
Tejun Heo5549c492013-06-24 15:21:48 -07004393 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004394 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004395
Tejun Heoca8bdca2013-08-26 18:40:56 -04004396 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004397 if (IS_ERR(css)) {
4398 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004399 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004401 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004402
4403 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004404 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004405 goto err_free_all;
4406
Tejun Heo623f9262013-08-13 11:01:55 -04004407 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004408 }
4409
Tejun Heo4e139af2012-11-19 08:13:36 -08004410 /*
4411 * Create directory. cgroup_create_file() returns with the new
4412 * directory locked on success so that it can be populated without
4413 * dropping cgroup_mutex.
4414 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004415 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004417 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004418 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419
Tejun Heo00356bd2013-06-18 11:14:22 -07004420 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004421
Tejun Heo4e139af2012-11-19 08:13:36 -08004422 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004423 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4424 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004425
Tejun Heo0ae78e02013-08-13 11:01:54 -04004426 /* each css holds a ref to the cgroup's dentry and the parent css */
4427 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004428 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004429
Tejun Heoed9577932012-11-05 09:16:58 -08004430 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004431 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004432 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004433
Li Zefan415cf072013-04-08 14:35:02 +08004434 /* hold a ref to the parent's dentry */
4435 dget(parent->dentry);
4436
Tejun Heob1929db2012-11-19 08:13:38 -08004437 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004438 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004439 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004440
4441 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004442 if (err)
4443 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004444
4445 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4446 parent->parent) {
4447 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",
4448 current->comm, current->pid, ss->name);
4449 if (!strcmp(ss->name, "memory"))
4450 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4451 ss->warned_broken_hierarchy = true;
4452 }
Tejun Heoa8638032012-11-09 09:12:29 -08004453 }
4454
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004455 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4456
Tejun Heo2bb566c2013-08-08 20:11:23 -04004457 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004458 if (err)
4459 goto err_destroy;
4460
4461 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004462 if (err)
4463 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004464
4465 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004466 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004467
4468 return 0;
4469
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004470err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004471 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004472 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004473
4474 if (css) {
4475 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004476 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004477 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004478 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004480 /* Release the reference count that we took on the superblock */
4481 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004482err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004483 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004484err_free_name:
4485 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004486err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004487 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004488 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004489
4490err_destroy:
4491 cgroup_destroy_locked(cgrp);
4492 mutex_unlock(&cgroup_mutex);
4493 mutex_unlock(&dentry->d_inode->i_mutex);
4494 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004495}
4496
Al Viro18bb1db2011-07-26 01:41:39 -04004497static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004498{
4499 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4500
4501 /* the vfs holds inode->i_mutex already */
4502 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4503}
4504
Tejun Heo223dbc32013-08-13 20:22:50 -04004505/*
4506 * This is called when the refcnt of a css is confirmed to be killed.
4507 * css_tryget() is now guaranteed to fail.
4508 */
4509static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004510{
Tejun Heo223dbc32013-08-13 20:22:50 -04004511 struct cgroup_subsys_state *css =
4512 container_of(work, struct cgroup_subsys_state, destroy_work);
4513 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004514
Tejun Heof20104d2013-08-13 20:22:50 -04004515 mutex_lock(&cgroup_mutex);
4516
4517 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004518 * css_tryget() is guaranteed to fail now. Tell subsystems to
4519 * initate destruction.
4520 */
4521 offline_css(css);
4522
4523 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004524 * If @cgrp is marked dead, it's waiting for refs of all css's to
4525 * be disabled before proceeding to the second phase of cgroup
4526 * destruction. If we are the last one, kick it off.
4527 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004528 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004529 cgroup_destroy_css_killed(cgrp);
4530
4531 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004532
4533 /*
4534 * Put the css refs from kill_css(). Each css holds an extra
4535 * reference to the cgroup's dentry and cgroup removal proceeds
4536 * regardless of css refs. On the last put of each css, whenever
4537 * that may be, the extra dentry ref is put so that dentry
4538 * destruction happens only after all css's are released.
4539 */
4540 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004541}
4542
Tejun Heo223dbc32013-08-13 20:22:50 -04004543/* css kill confirmation processing requires process context, bounce */
4544static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004545{
4546 struct cgroup_subsys_state *css =
4547 container_of(ref, struct cgroup_subsys_state, refcnt);
4548
Tejun Heo223dbc32013-08-13 20:22:50 -04004549 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004550 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004551}
4552
4553/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004554 * kill_css - destroy a css
4555 * @css: css to destroy
4556 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004557 * This function initiates destruction of @css by removing cgroup interface
4558 * files and putting its base reference. ->css_offline() will be invoked
4559 * asynchronously once css_tryget() is guaranteed to fail and when the
4560 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004561 */
4562static void kill_css(struct cgroup_subsys_state *css)
4563{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004564 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4565
Tejun Heoedae0c32013-08-13 20:22:51 -04004566 /*
4567 * Killing would put the base ref, but we need to keep it alive
4568 * until after ->css_offline().
4569 */
4570 css_get(css);
4571
4572 /*
4573 * cgroup core guarantees that, by the time ->css_offline() is
4574 * invoked, no new css reference will be given out via
4575 * css_tryget(). We can't simply call percpu_ref_kill() and
4576 * proceed to offlining css's because percpu_ref_kill() doesn't
4577 * guarantee that the ref is seen as killed on all CPUs on return.
4578 *
4579 * Use percpu_ref_kill_and_confirm() to get notifications as each
4580 * css is confirmed to be seen as killed on all CPUs.
4581 */
4582 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004583}
4584
4585/**
4586 * cgroup_destroy_locked - the first stage of cgroup destruction
4587 * @cgrp: cgroup to be destroyed
4588 *
4589 * css's make use of percpu refcnts whose killing latency shouldn't be
4590 * exposed to userland and are RCU protected. Also, cgroup core needs to
4591 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4592 * invoked. To satisfy all the requirements, destruction is implemented in
4593 * the following two steps.
4594 *
4595 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4596 * userland visible parts and start killing the percpu refcnts of
4597 * css's. Set up so that the next stage will be kicked off once all
4598 * the percpu refcnts are confirmed to be killed.
4599 *
4600 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4601 * rest of destruction. Once all cgroup references are gone, the
4602 * cgroup is RCU-freed.
4603 *
4604 * This function implements s1. After this step, @cgrp is gone as far as
4605 * the userland is concerned and a new cgroup with the same name may be
4606 * created. As cgroup doesn't care about the names internally, this
4607 * doesn't cause any problem.
4608 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004609static int cgroup_destroy_locked(struct cgroup *cgrp)
4610 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611{
Tejun Heo42809dd2012-11-19 08:13:37 -08004612 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004613 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004614 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004615 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004616 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617
Tejun Heo42809dd2012-11-19 08:13:37 -08004618 lockdep_assert_held(&d->d_inode->i_mutex);
4619 lockdep_assert_held(&cgroup_mutex);
4620
Tejun Heoddd69142013-06-12 21:04:54 -07004621 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004622 * css_set_lock synchronizes access to ->cset_links and prevents
4623 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004624 */
4625 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004626 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004627 read_unlock(&css_set_lock);
4628 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004630
Tejun Heo1a90dd52012-11-05 09:16:59 -08004631 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004632 * Make sure there's no live children. We can't test ->children
4633 * emptiness as dead children linger on it while being destroyed;
4634 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4635 */
4636 empty = true;
4637 rcu_read_lock();
4638 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4639 empty = cgroup_is_dead(child);
4640 if (!empty)
4641 break;
4642 }
4643 rcu_read_unlock();
4644 if (!empty)
4645 return -EBUSY;
4646
4647 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004648 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4649 * will be invoked to perform the rest of destruction once the
4650 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004651 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004652 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004653 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004654
4655 /*
4656 * Mark @cgrp dead. This prevents further task migration and child
4657 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004658 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004659 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004660 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004661 */
Tejun Heo54766d42013-06-12 21:04:53 -07004662 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004663
Tejun Heo455050d2013-06-13 19:27:41 -07004664 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4665 raw_spin_lock(&release_list_lock);
4666 if (!list_empty(&cgrp->release_list))
4667 list_del_init(&cgrp->release_list);
4668 raw_spin_unlock(&release_list_lock);
4669
4670 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004671 * If @cgrp has css's attached, the second stage of cgroup
4672 * destruction is kicked off from css_killed_work_fn() after the
4673 * refs of all attached css's are killed. If @cgrp doesn't have
4674 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004675 */
Tejun Heof20104d2013-08-13 20:22:50 -04004676 if (!cgrp->nr_css)
4677 cgroup_destroy_css_killed(cgrp);
4678
4679 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004680 * Clear the base files and remove @cgrp directory. The removal
4681 * puts the base ref but we aren't quite done with @cgrp yet, so
4682 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004683 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004684 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004685 dget(d);
4686 cgroup_d_remove_dir(d);
4687
4688 /*
4689 * Unregister events and notify userspace.
4690 * Notify userspace about cgroup removing only after rmdir of cgroup
4691 * directory to avoid race between userspace and kernelspace.
4692 */
4693 spin_lock(&cgrp->event_list_lock);
4694 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4695 list_del_init(&event->list);
4696 schedule_work(&event->remove);
4697 }
4698 spin_unlock(&cgrp->event_list_lock);
4699
Tejun Heoea15f8c2013-06-13 19:27:42 -07004700 return 0;
4701};
4702
Tejun Heod3daf282013-06-13 19:39:16 -07004703/**
Tejun Heof20104d2013-08-13 20:22:50 -04004704 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004705 * @work: cgroup->destroy_free_work
4706 *
4707 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004708 * destroyed after all css's are offlined and performs the rest of
4709 * destruction. This is the second step of destruction described in the
4710 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004711 */
Tejun Heof20104d2013-08-13 20:22:50 -04004712static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004713{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004714 struct cgroup *parent = cgrp->parent;
4715 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004716
Tejun Heof20104d2013-08-13 20:22:50 -04004717 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004718
Paul Menage999cd8a2009-01-07 18:08:36 -08004719 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004720 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004721
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004722 /*
4723 * We should remove the cgroup object from idr before its grace
4724 * period starts, so we won't be looking up a cgroup while the
4725 * cgroup is being freed.
4726 */
4727 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4728 cgrp->id = -1;
4729
Paul Menageddbcc7e2007-10-18 23:39:30 -07004730 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731
Paul Menagebd89aab2007-10-18 23:40:44 -07004732 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004733 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004734}
4735
Tejun Heo42809dd2012-11-19 08:13:37 -08004736static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4737{
4738 int ret;
4739
4740 mutex_lock(&cgroup_mutex);
4741 ret = cgroup_destroy_locked(dentry->d_fsdata);
4742 mutex_unlock(&cgroup_mutex);
4743
4744 return ret;
4745}
4746
Tejun Heo8e3f6542012-04-01 12:09:55 -07004747static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4748{
4749 INIT_LIST_HEAD(&ss->cftsets);
4750
4751 /*
4752 * base_cftset is embedded in subsys itself, no need to worry about
4753 * deregistration.
4754 */
4755 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004756 struct cftype *cft;
4757
4758 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4759 cft->ss = ss;
4760
Tejun Heo8e3f6542012-04-01 12:09:55 -07004761 ss->base_cftset.cfts = ss->base_cftypes;
4762 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4763 }
4764}
4765
Li Zefan06a11922008-04-29 01:00:07 -07004766static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004767{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004768 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004769
4770 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004771
Tejun Heo648bb562012-11-19 08:13:36 -08004772 mutex_lock(&cgroup_mutex);
4773
Tejun Heo8e3f6542012-04-01 12:09:55 -07004774 /* init base cftset */
4775 cgroup_init_cftsets(ss);
4776
Paul Menageddbcc7e2007-10-18 23:39:30 -07004777 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004778 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4779 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004780 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004781 /* We don't handle early failures gracefully */
4782 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004783 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004784
Li Zefane8d55fd2008-04-29 01:00:13 -07004785 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004786 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004787 * newly registered, all tasks and hence the
4788 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004789 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004790
4791 need_forkexit_callback |= ss->fork || ss->exit;
4792
Li Zefane8d55fd2008-04-29 01:00:13 -07004793 /* At system boot, before all subsystems have been
4794 * registered, no tasks have been forked, so we don't
4795 * need to invoke fork callbacks here. */
4796 BUG_ON(!list_empty(&init_task.tasks));
4797
Tejun Heoae7f1642013-08-13 20:22:50 -04004798 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004799
Tejun Heo648bb562012-11-19 08:13:36 -08004800 mutex_unlock(&cgroup_mutex);
4801
Ben Blume6a11052010-03-10 15:22:09 -08004802 /* this function shouldn't be used with modular subsystems, since they
4803 * need to register a subsys_id, among other things */
4804 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004805}
4806
4807/**
Ben Blume6a11052010-03-10 15:22:09 -08004808 * cgroup_load_subsys: load and register a modular subsystem at runtime
4809 * @ss: the subsystem to load
4810 *
4811 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004812 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004813 * up for use. If the subsystem is built-in anyway, work is delegated to the
4814 * simpler cgroup_init_subsys.
4815 */
4816int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4817{
Ben Blume6a11052010-03-10 15:22:09 -08004818 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004819 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004820 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004821 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004822 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004823
4824 /* check name and function validity */
4825 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004826 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004827 return -EINVAL;
4828
4829 /*
4830 * we don't support callbacks in modular subsystems. this check is
4831 * before the ss->module check for consistency; a subsystem that could
4832 * be a module should still have no callbacks even if the user isn't
4833 * compiling it as one.
4834 */
4835 if (ss->fork || ss->exit)
4836 return -EINVAL;
4837
4838 /*
4839 * an optionally modular subsystem is built-in: we want to do nothing,
4840 * since cgroup_init_subsys will have already taken care of it.
4841 */
4842 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004843 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004844 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004845 return 0;
4846 }
4847
Tejun Heo8e3f6542012-04-01 12:09:55 -07004848 /* init base cftset */
4849 cgroup_init_cftsets(ss);
4850
Ben Blume6a11052010-03-10 15:22:09 -08004851 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004852 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004853
4854 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004855 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004856 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004857 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004858 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004859 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004860 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004861 /* failure case - need to deassign the cgroup_subsys[] slot. */
4862 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004863 mutex_unlock(&cgroup_mutex);
4864 return PTR_ERR(css);
4865 }
4866
Tejun Heo9871bf92013-06-24 15:21:47 -07004867 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4868 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004869
4870 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004871 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004872
4873 /*
4874 * Now we need to entangle the css into the existing css_sets. unlike
4875 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4876 * will need a new pointer to it; done by iterating the css_set_table.
4877 * furthermore, modifying the existing css_sets will corrupt the hash
4878 * table state, so each changed css_set will need its hash recomputed.
4879 * this is all done under the css_set_lock.
4880 */
4881 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004882 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004883 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004884 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004885 continue;
4886 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004887 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004888 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004889 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004890 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004891 key = css_set_hash(cset->subsys);
4892 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004893 }
4894 write_unlock(&css_set_lock);
4895
Tejun Heoae7f1642013-08-13 20:22:50 -04004896 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004897 if (ret)
4898 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004899
Ben Blume6a11052010-03-10 15:22:09 -08004900 /* success! */
4901 mutex_unlock(&cgroup_mutex);
4902 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004903
4904err_unload:
4905 mutex_unlock(&cgroup_mutex);
4906 /* @ss can't be mounted here as try_module_get() would fail */
4907 cgroup_unload_subsys(ss);
4908 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004909}
4910EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4911
4912/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004913 * cgroup_unload_subsys: unload a modular subsystem
4914 * @ss: the subsystem to unload
4915 *
4916 * This function should be called in a modular subsystem's exitcall. When this
4917 * function is invoked, the refcount on the subsystem's module will be 0, so
4918 * the subsystem will not be attached to any hierarchy.
4919 */
4920void cgroup_unload_subsys(struct cgroup_subsys *ss)
4921{
Tejun Heo69d02062013-06-12 21:04:50 -07004922 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004923
4924 BUG_ON(ss->module == NULL);
4925
4926 /*
4927 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004928 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004929 * doesn't start being used while we're killing it off.
4930 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004931 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004932
4933 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004934
Tejun Heoca8bdca2013-08-26 18:40:56 -04004935 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004936
Ben Blumcf5d5942010-03-10 15:22:09 -08004937 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004938 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004939
Tejun Heo9871bf92013-06-24 15:21:47 -07004940 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004941 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004942
4943 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004944 * disentangle the css from all css_sets attached to the dummy
4945 * top. as in loading, we need to pay our respects to the hashtable
4946 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004947 */
4948 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004949 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004950 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004951 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004952
Tejun Heo5abb8852013-06-12 21:04:49 -07004953 hash_del(&cset->hlist);
4954 cset->subsys[ss->subsys_id] = NULL;
4955 key = css_set_hash(cset->subsys);
4956 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004957 }
4958 write_unlock(&css_set_lock);
4959
4960 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004961 * remove subsystem's css from the cgroup_dummy_top and free it -
4962 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004963 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004964 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004965 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004966 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004967
4968 mutex_unlock(&cgroup_mutex);
4969}
4970EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4971
4972/**
Li Zefana043e3b2008-02-23 15:24:09 -08004973 * cgroup_init_early - cgroup initialization at system boot
4974 *
4975 * Initialize cgroups at system boot, and initialize any
4976 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004977 */
4978int __init cgroup_init_early(void)
4979{
Tejun Heo30159ec2013-06-25 11:53:37 -07004980 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004981 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004982
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004983 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004984 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004985 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004986 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004987 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004988 init_cgroup_root(&cgroup_dummy_root);
4989 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004990 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004991
Tejun Heo69d02062013-06-12 21:04:50 -07004992 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004993 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4994 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004995 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004996
Tejun Heo30159ec2013-06-25 11:53:37 -07004997 /* at bootup time, we don't worry about modular subsystems */
4998 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004999 BUG_ON(!ss->name);
5000 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005001 BUG_ON(!ss->css_alloc);
5002 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005003 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005004 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005005 ss->name, ss->subsys_id);
5006 BUG();
5007 }
5008
5009 if (ss->early_init)
5010 cgroup_init_subsys(ss);
5011 }
5012 return 0;
5013}
5014
5015/**
Li Zefana043e3b2008-02-23 15:24:09 -08005016 * cgroup_init - cgroup initialization
5017 *
5018 * Register cgroup filesystem and /proc file, and initialize
5019 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005020 */
5021int __init cgroup_init(void)
5022{
Tejun Heo30159ec2013-06-25 11:53:37 -07005023 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005024 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005025 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005026
5027 err = bdi_init(&cgroup_backing_dev_info);
5028 if (err)
5029 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005030
Tejun Heo30159ec2013-06-25 11:53:37 -07005031 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005032 if (!ss->early_init)
5033 cgroup_init_subsys(ss);
5034 }
5035
Tejun Heofa3ca072013-04-14 11:36:56 -07005036 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005037 mutex_lock(&cgroup_mutex);
5038 mutex_lock(&cgroup_root_mutex);
5039
Tejun Heo82fe9b02013-06-25 11:53:37 -07005040 /* Add init_css_set to the hash table */
5041 key = css_set_hash(init_css_set.subsys);
5042 hash_add(css_set_table, &init_css_set.hlist, key);
5043
Tejun Heofc76df72013-06-25 11:53:37 -07005044 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005045
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005046 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5047 0, 1, GFP_KERNEL);
5048 BUG_ON(err < 0);
5049
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005050 mutex_unlock(&cgroup_root_mutex);
5051 mutex_unlock(&cgroup_mutex);
5052
Greg KH676db4a2010-08-05 13:53:35 -07005053 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5054 if (!cgroup_kobj) {
5055 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005056 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005057 }
5058
5059 err = register_filesystem(&cgroup_fs_type);
5060 if (err < 0) {
5061 kobject_put(cgroup_kobj);
5062 goto out;
5063 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005064
Li Zefan46ae2202008-04-29 01:00:08 -07005065 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005066
Paul Menageddbcc7e2007-10-18 23:39:30 -07005067out:
Paul Menagea4243162007-10-18 23:39:35 -07005068 if (err)
5069 bdi_destroy(&cgroup_backing_dev_info);
5070
Paul Menageddbcc7e2007-10-18 23:39:30 -07005071 return err;
5072}
Paul Menageb4f48b62007-10-18 23:39:33 -07005073
Tejun Heoe5fca242013-11-22 17:14:39 -05005074static int __init cgroup_wq_init(void)
5075{
5076 /*
5077 * There isn't much point in executing destruction path in
5078 * parallel. Good chunk is serialized with cgroup_mutex anyway.
5079 * Use 1 for @max_active.
5080 *
5081 * We would prefer to do this in cgroup_init() above, but that
5082 * is called before init_workqueues(): so leave this until after.
5083 */
5084 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
5085 BUG_ON(!cgroup_destroy_wq);
5086 return 0;
5087}
5088core_initcall(cgroup_wq_init);
5089
Paul Menagea4243162007-10-18 23:39:35 -07005090/*
5091 * proc_cgroup_show()
5092 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5093 * - Used for /proc/<pid>/cgroup.
5094 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5095 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005096 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005097 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5098 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5099 * cgroup to top_cgroup.
5100 */
5101
5102/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005103int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005104{
5105 struct pid *pid;
5106 struct task_struct *tsk;
5107 char *buf;
5108 int retval;
5109 struct cgroupfs_root *root;
5110
5111 retval = -ENOMEM;
5112 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5113 if (!buf)
5114 goto out;
5115
5116 retval = -ESRCH;
5117 pid = m->private;
5118 tsk = get_pid_task(pid, PIDTYPE_PID);
5119 if (!tsk)
5120 goto out_free;
5121
5122 retval = 0;
5123
5124 mutex_lock(&cgroup_mutex);
5125
Li Zefane5f6a862009-01-07 18:07:41 -08005126 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005127 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005128 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005129 int count = 0;
5130
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005131 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005132 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005133 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005134 if (strlen(root->name))
5135 seq_printf(m, "%sname=%s", count ? "," : "",
5136 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005137 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005138 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005139 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005140 if (retval < 0)
5141 goto out_unlock;
5142 seq_puts(m, buf);
5143 seq_putc(m, '\n');
5144 }
5145
5146out_unlock:
5147 mutex_unlock(&cgroup_mutex);
5148 put_task_struct(tsk);
5149out_free:
5150 kfree(buf);
5151out:
5152 return retval;
5153}
5154
Paul Menagea4243162007-10-18 23:39:35 -07005155/* Display information about each subsystem and each hierarchy */
5156static int proc_cgroupstats_show(struct seq_file *m, void *v)
5157{
Tejun Heo30159ec2013-06-25 11:53:37 -07005158 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005159 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005160
Paul Menage8bab8dd2008-04-04 14:29:57 -07005161 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005162 /*
5163 * ideally we don't want subsystems moving around while we do this.
5164 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5165 * subsys/hierarchy state.
5166 */
Paul Menagea4243162007-10-18 23:39:35 -07005167 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005168
5169 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005170 seq_printf(m, "%s\t%d\t%d\t%d\n",
5171 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005172 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005173
Paul Menagea4243162007-10-18 23:39:35 -07005174 mutex_unlock(&cgroup_mutex);
5175 return 0;
5176}
5177
5178static int cgroupstats_open(struct inode *inode, struct file *file)
5179{
Al Viro9dce07f2008-03-29 03:07:28 +00005180 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005181}
5182
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005183static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005184 .open = cgroupstats_open,
5185 .read = seq_read,
5186 .llseek = seq_lseek,
5187 .release = single_release,
5188};
5189
Paul Menageb4f48b62007-10-18 23:39:33 -07005190/**
5191 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005192 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005193 *
5194 * Description: A task inherits its parent's cgroup at fork().
5195 *
5196 * A pointer to the shared css_set was automatically copied in
5197 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005198 * it was not made under the protection of RCU or cgroup_mutex, so
5199 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5200 * have already changed current->cgroups, allowing the previously
5201 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005202 *
5203 * At the point that cgroup_fork() is called, 'current' is the parent
5204 * task, and the passed argument 'child' points to the child task.
5205 */
5206void cgroup_fork(struct task_struct *child)
5207{
Tejun Heo9bb71302012-10-18 17:52:07 -07005208 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005209 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005210 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005211 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005212 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005213}
5214
5215/**
Li Zefana043e3b2008-02-23 15:24:09 -08005216 * cgroup_post_fork - called on a new task after adding it to the task list
5217 * @child: the task in question
5218 *
Tejun Heo5edee612012-10-16 15:03:14 -07005219 * Adds the task to the list running through its css_set if necessary and
5220 * call the subsystem fork() callbacks. Has to be after the task is
5221 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005222 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005223 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005224 */
Paul Menage817929e2007-10-18 23:39:36 -07005225void cgroup_post_fork(struct task_struct *child)
5226{
Tejun Heo30159ec2013-06-25 11:53:37 -07005227 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005228 int i;
5229
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005230 /*
5231 * use_task_css_set_links is set to 1 before we walk the tasklist
5232 * under the tasklist_lock and we read it here after we added the child
5233 * to the tasklist under the tasklist_lock as well. If the child wasn't
5234 * yet in the tasklist when we walked through it from
5235 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5236 * should be visible now due to the paired locking and barriers implied
5237 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5238 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5239 * lock on fork.
5240 */
Paul Menage817929e2007-10-18 23:39:36 -07005241 if (use_task_css_set_links) {
5242 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005243 task_lock(child);
5244 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005245 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005246 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005247 write_unlock(&css_set_lock);
5248 }
Tejun Heo5edee612012-10-16 15:03:14 -07005249
5250 /*
5251 * Call ss->fork(). This must happen after @child is linked on
5252 * css_set; otherwise, @child might change state between ->fork()
5253 * and addition to css_set.
5254 */
5255 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005256 /*
5257 * fork/exit callbacks are supported only for builtin
5258 * subsystems, and the builtin section of the subsys
5259 * array is immutable, so we don't need to lock the
5260 * subsys array here. On the other hand, modular section
5261 * of the array can be freed at module unload, so we
5262 * can't touch that.
5263 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005264 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005265 if (ss->fork)
5266 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005267 }
Paul Menage817929e2007-10-18 23:39:36 -07005268}
Tejun Heo5edee612012-10-16 15:03:14 -07005269
Paul Menage817929e2007-10-18 23:39:36 -07005270/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005271 * cgroup_exit - detach cgroup from exiting task
5272 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005273 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005274 *
5275 * Description: Detach cgroup from @tsk and release it.
5276 *
5277 * Note that cgroups marked notify_on_release force every task in
5278 * them to take the global cgroup_mutex mutex when exiting.
5279 * This could impact scaling on very large systems. Be reluctant to
5280 * use notify_on_release cgroups where very high task exit scaling
5281 * is required on large systems.
5282 *
5283 * the_top_cgroup_hack:
5284 *
5285 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5286 *
5287 * We call cgroup_exit() while the task is still competent to
5288 * handle notify_on_release(), then leave the task attached to the
5289 * root cgroup in each hierarchy for the remainder of its exit.
5290 *
5291 * To do this properly, we would increment the reference count on
5292 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5293 * code we would add a second cgroup function call, to drop that
5294 * reference. This would just create an unnecessary hot spot on
5295 * the top_cgroup reference count, to no avail.
5296 *
5297 * Normally, holding a reference to a cgroup without bumping its
5298 * count is unsafe. The cgroup could go away, or someone could
5299 * attach us to a different cgroup, decrementing the count on
5300 * the first cgroup that we never incremented. But in this case,
5301 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005302 * which wards off any cgroup_attach_task() attempts, or task is a failed
5303 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005304 */
5305void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5306{
Tejun Heo30159ec2013-06-25 11:53:37 -07005307 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005308 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005309 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005310
5311 /*
5312 * Unlink from the css_set task list if necessary.
5313 * Optimistically check cg_list before taking
5314 * css_set_lock
5315 */
5316 if (!list_empty(&tsk->cg_list)) {
5317 write_lock(&css_set_lock);
5318 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005319 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005320 write_unlock(&css_set_lock);
5321 }
5322
Paul Menageb4f48b62007-10-18 23:39:33 -07005323 /* Reassign the task to the init_css_set. */
5324 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005325 cset = task_css_set(tsk);
5326 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005327
5328 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005329 /*
5330 * fork/exit callbacks are supported only for builtin
5331 * subsystems, see cgroup_post_fork() for details.
5332 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005333 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005334 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005335 struct cgroup_subsys_state *old_css = cset->subsys[i];
5336 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005337
Tejun Heoeb954192013-08-08 20:11:23 -04005338 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005339 }
5340 }
5341 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005342 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005343
Tejun Heo5abb8852013-06-12 21:04:49 -07005344 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005345}
Paul Menage697f4162007-10-18 23:39:34 -07005346
Paul Menagebd89aab2007-10-18 23:40:44 -07005347static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005348{
Li Zefanf50daa72013-03-01 15:06:07 +08005349 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005350 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005351 /*
5352 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005353 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005354 * it now
5355 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005356 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005357
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005358 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005359 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005360 list_empty(&cgrp->release_list)) {
5361 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005362 need_schedule_work = 1;
5363 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005364 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005365 if (need_schedule_work)
5366 schedule_work(&release_agent_work);
5367 }
5368}
5369
Paul Menage81a6a5c2007-10-18 23:39:38 -07005370/*
5371 * Notify userspace when a cgroup is released, by running the
5372 * configured release agent with the name of the cgroup (path
5373 * relative to the root of cgroup file system) as the argument.
5374 *
5375 * Most likely, this user command will try to rmdir this cgroup.
5376 *
5377 * This races with the possibility that some other task will be
5378 * attached to this cgroup before it is removed, or that some other
5379 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5380 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5381 * unused, and this cgroup will be reprieved from its death sentence,
5382 * to continue to serve a useful existence. Next time it's released,
5383 * we will get notified again, if it still has 'notify_on_release' set.
5384 *
5385 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5386 * means only wait until the task is successfully execve()'d. The
5387 * separate release agent task is forked by call_usermodehelper(),
5388 * then control in this thread returns here, without waiting for the
5389 * release agent task. We don't bother to wait because the caller of
5390 * this routine has no use for the exit status of the release agent
5391 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005392 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005393static void cgroup_release_agent(struct work_struct *work)
5394{
5395 BUG_ON(work != &release_agent_work);
5396 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005397 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005398 while (!list_empty(&release_list)) {
5399 char *argv[3], *envp[3];
5400 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005401 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005402 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005403 struct cgroup,
5404 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005405 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005406 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005408 if (!pathbuf)
5409 goto continue_free;
5410 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5411 goto continue_free;
5412 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5413 if (!agentbuf)
5414 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005415
5416 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005417 argv[i++] = agentbuf;
5418 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005419 argv[i] = NULL;
5420
5421 i = 0;
5422 /* minimal command environment */
5423 envp[i++] = "HOME=/";
5424 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5425 envp[i] = NULL;
5426
5427 /* Drop the lock while we invoke the usermode helper,
5428 * since the exec could involve hitting disk and hence
5429 * be a slow process */
5430 mutex_unlock(&cgroup_mutex);
5431 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005432 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005433 continue_free:
5434 kfree(pathbuf);
5435 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005436 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005437 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005438 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005439 mutex_unlock(&cgroup_mutex);
5440}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005441
5442static int __init cgroup_disable(char *str)
5443{
Tejun Heo30159ec2013-06-25 11:53:37 -07005444 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005445 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005446 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005447
5448 while ((token = strsep(&str, ",")) != NULL) {
5449 if (!*token)
5450 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005451
Tejun Heo30159ec2013-06-25 11:53:37 -07005452 /*
5453 * cgroup_disable, being at boot time, can't know about
5454 * module subsystems, so we don't worry about them.
5455 */
5456 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005457 if (!strcmp(token, ss->name)) {
5458 ss->disabled = 1;
5459 printk(KERN_INFO "Disabling %s control group"
5460 " subsystem\n", ss->name);
5461 break;
5462 }
5463 }
5464 }
5465 return 1;
5466}
5467__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005468
Tejun Heob77d7b62013-08-13 11:01:54 -04005469/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005470 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5471 * @dentry: directory dentry of interest
5472 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005473 *
5474 * Must be called under RCU read lock. The caller is responsible for
5475 * pinning the returned css if it needs to be accessed outside the RCU
5476 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005477 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005478struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5479 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005480{
5481 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005482
Tejun Heob77d7b62013-08-13 11:01:54 -04005483 WARN_ON_ONCE(!rcu_read_lock_held());
5484
Tejun Heo35cf0832013-08-26 18:40:56 -04005485 /* is @dentry a cgroup dir? */
5486 if (!dentry->d_inode ||
5487 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005488 return ERR_PTR(-EBADF);
5489
Tejun Heo35cf0832013-08-26 18:40:56 -04005490 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005491 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005492}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005493
Li Zefan1cb650b2013-08-19 10:05:24 +08005494/**
5495 * css_from_id - lookup css by id
5496 * @id: the cgroup id
5497 * @ss: cgroup subsys to be looked into
5498 *
5499 * Returns the css if there's valid one with @id, otherwise returns NULL.
5500 * Should be called under rcu_read_lock().
5501 */
5502struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5503{
5504 struct cgroup *cgrp;
5505
5506 rcu_lockdep_assert(rcu_read_lock_held() ||
5507 lockdep_is_held(&cgroup_mutex),
5508 "css_from_id() needs proper protection");
5509
5510 cgrp = idr_find(&ss->root->cgroup_idr, id);
5511 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005512 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005513 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005514}
5515
Paul Menagefe693432009-09-23 15:56:20 -07005516#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005517static struct cgroup_subsys_state *
5518debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005519{
5520 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5521
5522 if (!css)
5523 return ERR_PTR(-ENOMEM);
5524
5525 return css;
5526}
5527
Tejun Heoeb954192013-08-08 20:11:23 -04005528static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005529{
Tejun Heoeb954192013-08-08 20:11:23 -04005530 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005531}
5532
Tejun Heo182446d2013-08-08 20:11:24 -04005533static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5534 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005535{
Tejun Heo182446d2013-08-08 20:11:24 -04005536 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005537}
5538
Tejun Heo182446d2013-08-08 20:11:24 -04005539static u64 current_css_set_read(struct cgroup_subsys_state *css,
5540 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005541{
5542 return (u64)(unsigned long)current->cgroups;
5543}
5544
Tejun Heo182446d2013-08-08 20:11:24 -04005545static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005546 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005547{
5548 u64 count;
5549
5550 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005551 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005552 rcu_read_unlock();
5553 return count;
5554}
5555
Tejun Heo182446d2013-08-08 20:11:24 -04005556static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005557 struct cftype *cft,
5558 struct seq_file *seq)
5559{
Tejun Heo69d02062013-06-12 21:04:50 -07005560 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005561 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005562
5563 read_lock(&css_set_lock);
5564 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005565 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005566 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005567 struct cgroup *c = link->cgrp;
5568 const char *name;
5569
5570 if (c->dentry)
5571 name = c->dentry->d_name.name;
5572 else
5573 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005574 seq_printf(seq, "Root %d group %s\n",
5575 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005576 }
5577 rcu_read_unlock();
5578 read_unlock(&css_set_lock);
5579 return 0;
5580}
5581
5582#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005583static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5584 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005585{
Tejun Heo69d02062013-06-12 21:04:50 -07005586 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005587
5588 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005589 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005590 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005591 struct task_struct *task;
5592 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005593 seq_printf(seq, "css_set %p\n", cset);
5594 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005595 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5596 seq_puts(seq, " ...\n");
5597 break;
5598 } else {
5599 seq_printf(seq, " task %d\n",
5600 task_pid_vnr(task));
5601 }
5602 }
5603 }
5604 read_unlock(&css_set_lock);
5605 return 0;
5606}
5607
Tejun Heo182446d2013-08-08 20:11:24 -04005608static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005609{
Tejun Heo182446d2013-08-08 20:11:24 -04005610 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005611}
5612
5613static struct cftype debug_files[] = {
5614 {
Paul Menagefe693432009-09-23 15:56:20 -07005615 .name = "taskcount",
5616 .read_u64 = debug_taskcount_read,
5617 },
5618
5619 {
5620 .name = "current_css_set",
5621 .read_u64 = current_css_set_read,
5622 },
5623
5624 {
5625 .name = "current_css_set_refcount",
5626 .read_u64 = current_css_set_refcount_read,
5627 },
5628
5629 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005630 .name = "current_css_set_cg_links",
5631 .read_seq_string = current_css_set_cg_links_read,
5632 },
5633
5634 {
5635 .name = "cgroup_css_links",
5636 .read_seq_string = cgroup_css_links_read,
5637 },
5638
5639 {
Paul Menagefe693432009-09-23 15:56:20 -07005640 .name = "releasable",
5641 .read_u64 = releasable_read,
5642 },
Paul Menagefe693432009-09-23 15:56:20 -07005643
Tejun Heo4baf6e32012-04-01 12:09:55 -07005644 { } /* terminate */
5645};
Paul Menagefe693432009-09-23 15:56:20 -07005646
5647struct cgroup_subsys debug_subsys = {
5648 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005649 .css_alloc = debug_css_alloc,
5650 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005651 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005652 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005653};
5654#endif /* CONFIG_CGROUP_DEBUG */