blob: 9835a097f3c0d3c8c21191e78fdf41611ece9ff4 [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>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218static struct cftype cgroup_base_files[];
219
Tejun Heoea15f8c2013-06-13 19:27:42 -0700220static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800222static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
223 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Ben Blume6a11052010-03-10 15:22:09 -0800367static int cgroup_init_idr(struct cgroup_subsys *ss,
368 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700369
Paul Menage817929e2007-10-18 23:39:36 -0700370/* css_set_lock protects the list of css_set objects, and the
371 * chain of tasks off each css_set. Nests outside task->alloc_lock
372 * due to cgroup_iter_start() */
373static DEFINE_RWLOCK(css_set_lock);
374static int css_set_count;
375
Paul Menage7717f7b2009-09-23 15:56:22 -0700376/*
377 * hash table for cgroup groups. This improves the performance to find
378 * an existing css_set. This hash doesn't (currently) take into
379 * account cgroups in empty hierarchies.
380 */
Li Zefan472b1052008-04-29 01:00:11 -0700381#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700385{
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700387 struct cgroup_subsys *ss;
388 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Tejun Heo30159ec2013-06-25 11:53:37 -0700390 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 key += (unsigned long)css[i];
392 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700393
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
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 *
469 * Returns true if "cg" matches "old_cg" except for the hierarchy
470 * 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 */
Paul Menagebd89aab2007-10-18 23:40:44 -0700560 template[i] = cgrp->subsys[i];
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);
Al Viro00cd8dd2012-06-10 17:13:09 -0400807static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700809static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700810static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700811static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700812
813static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200814 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700815 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700816};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700818static int alloc_css_id(struct cgroup_subsys *ss,
819 struct cgroup *parent, struct cgroup *child);
820
Al Viroa5e7ed32011-07-26 01:55:55 -0400821static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822{
823 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824
825 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400826 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100828 inode->i_uid = current_fsuid();
829 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
831 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
832 }
833 return inode;
834}
835
Li Zefan65dff752013-03-01 15:01:56 +0800836static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
837{
838 struct cgroup_name *name;
839
840 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
841 if (!name)
842 return NULL;
843 strcpy(name->name, dentry->d_name.name);
844 return name;
845}
846
Li Zefanbe445622013-01-24 14:31:42 +0800847static void cgroup_free_fn(struct work_struct *work)
848{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700849 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800850 struct cgroup_subsys *ss;
851
852 mutex_lock(&cgroup_mutex);
853 /*
854 * Release the subsystem state objects.
855 */
Tejun Heo5549c492013-06-24 15:21:48 -0700856 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800857 ss->css_free(cgrp);
858
859 cgrp->root->number_of_cgroups--;
860 mutex_unlock(&cgroup_mutex);
861
862 /*
Li Zefan415cf072013-04-08 14:35:02 +0800863 * We get a ref to the parent's dentry, and put the ref when
864 * this cgroup is being freed, so it's guaranteed that the
865 * parent won't be destroyed before its children.
866 */
867 dput(cgrp->parent->dentry);
868
Li Zefancc20e012013-04-26 11:58:02 -0700869 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
870
Li Zefan415cf072013-04-08 14:35:02 +0800871 /*
Li Zefanbe445622013-01-24 14:31:42 +0800872 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700873 * created the cgroup. This will free cgrp->root, if we are
874 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800875 */
876 deactivate_super(cgrp->root->sb);
877
878 /*
879 * if we're getting rid of the cgroup, refcount should ensure
880 * that there are no pidlists left.
881 */
882 BUG_ON(!list_empty(&cgrp->pidlists));
883
884 simple_xattrs_free(&cgrp->xattrs);
885
Li Zefan65dff752013-03-01 15:01:56 +0800886 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800887 kfree(cgrp);
888}
889
890static void cgroup_free_rcu(struct rcu_head *head)
891{
892 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
893
Tejun Heoea15f8c2013-06-13 19:27:42 -0700894 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
895 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800896}
897
Paul Menageddbcc7e2007-10-18 23:39:30 -0700898static void cgroup_diput(struct dentry *dentry, struct inode *inode)
899{
900 /* is dentry a directory ? if so, kfree() associated cgroup */
901 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700902 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800903
Tejun Heo54766d42013-06-12 21:04:53 -0700904 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800905 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700906 } else {
907 struct cfent *cfe = __d_cfe(dentry);
908 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
909
910 WARN_ONCE(!list_empty(&cfe->node) &&
911 cgrp != &cgrp->root->top_cgroup,
912 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700913 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915 }
916 iput(inode);
917}
918
Al Viroc72a04e2011-01-14 05:31:45 +0000919static int cgroup_delete(const struct dentry *d)
920{
921 return 1;
922}
923
Paul Menageddbcc7e2007-10-18 23:39:30 -0700924static void remove_dir(struct dentry *d)
925{
926 struct dentry *parent = dget(d->d_parent);
927
928 d_delete(d);
929 simple_rmdir(parent->d_inode, d);
930 dput(parent);
931}
932
Li Zefan2739d3c2013-01-21 18:18:33 +0800933static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
938 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940 /*
941 * If we're doing cleanup due to failure of cgroup_create(),
942 * the corresponding @cfe may not exist.
943 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_for_each_entry(cfe, &cgrp->files, node) {
945 struct dentry *d = cfe->dentry;
946
947 if (cft && cfe->type != cft)
948 continue;
949
950 dget(d);
951 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700952 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953 list_del_init(&cfe->node);
954 dput(d);
955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958}
959
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700961 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700962 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700965static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700966{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968
Tejun Heo5549c492013-06-24 15:21:48 -0700969 for_each_root_subsys(cgrp->root, ss) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400970 struct cftype_set *set;
971 if (!test_bit(ss->subsys_id, &subsys_mask))
972 continue;
973 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800974 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700976}
977
978/*
979 * NOTE : the dentry must have been dget()'ed
980 */
981static void cgroup_d_remove_dir(struct dentry *dentry)
982{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700984
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100985 parent = dentry->d_parent;
986 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800987 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 spin_unlock(&dentry->d_lock);
990 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 remove_dir(dentry);
992}
993
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700994/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800995 * Call with cgroup_mutex held. Drops reference counts on modules, including
996 * any duplicate ones that parse_cgroupfs_options took. If this function
997 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800998 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001000 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001{
Paul Menagebd89aab2007-10-18 23:40:44 -07001002 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001003 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004 int i;
1005
Ben Blumaae8aab2010-03-10 15:22:07 -08001006 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001007 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001008
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001010 for_each_subsys(ss, i) {
Li Zefan8d53d552008-02-23 15:24:11 -08001011 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001012
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001013 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001014 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001015
Tejun Heo9871bf92013-06-24 15:21:47 -07001016 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 /* Subsystem isn't free */
1018 return -EBUSY;
1019 }
1020 }
1021
1022 /* Currently we don't handle adding/removing subsystems when
1023 * any child cgroups exist. This is theoretically supportable
1024 * but involves complex error handling, so it's being left until
1025 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001026 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 return -EBUSY;
1028
1029 /* Process each subsystem */
Tejun Heo30159ec2013-06-25 11:53:37 -07001030 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001032
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001033 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001035 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001036 BUG_ON(!cgroup_dummy_top->subsys[i]);
1037 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001038
Tejun Heo9871bf92013-06-24 15:21:47 -07001039 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001040 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001041 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001042 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001044 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001045
Ben Blumcf5d5942010-03-10 15:22:09 -08001046 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001048 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001050 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001051 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001052
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 ss->bind(cgroup_dummy_top);
1055 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001056 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001057 cgroup_subsys[i]->root = &cgroup_dummy_root;
1058 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001059
Ben Blumcf5d5942010-03-10 15:22:09 -08001060 /* subsystem is now free - drop reference on module */
1061 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062 root->subsys_mask &= ~bit;
1063 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001066 /*
1067 * a refcount was taken, but we already had one, so
1068 * drop the extra reference.
1069 */
1070 module_put(ss->module);
1071#ifdef CONFIG_MODULE_UNLOAD
1072 BUG_ON(ss->module && !module_refcount(ss->module));
1073#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001074 } else {
1075 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001076 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 }
1078 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079
Tejun Heo1672d042013-06-25 18:04:54 -07001080 /*
1081 * Mark @root has finished binding subsystems. @root->subsys_mask
1082 * now matches the bound subsystems.
1083 */
1084 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1085
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 return 0;
1087}
1088
Al Viro34c80b12011-12-08 21:32:45 -05001089static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090{
Al Viro34c80b12011-12-08 21:32:45 -05001091 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092 struct cgroup_subsys *ss;
1093
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001094 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001095 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001097 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1098 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001099 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001101 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001102 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001103 if (strlen(root->release_agent_path))
1104 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001105 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001106 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001107 if (strlen(root->name))
1108 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001109 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 return 0;
1111}
1112
1113struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001114 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001116 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001117 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001118 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001119 /* User explicitly requested empty subsystem */
1120 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121
1122 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124};
1125
Ben Blumaae8aab2010-03-10 15:22:07 -08001126/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001127 * Convert a hierarchy specifier into a bitmask of subsystems and
1128 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1129 * array. This function takes refcounts on subsystems to be used, unless it
1130 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001131 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001132static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 char *token, *o = data;
1135 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001136 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001137 bool module_pin_failed = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07001138 struct cgroup_subsys *ss;
1139 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001140
Ben Blumaae8aab2010-03-10 15:22:07 -08001141 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1142
Li Zefanf9ab5b52009-06-17 16:26:33 -07001143#ifdef CONFIG_CPUSETS
1144 mask = ~(1UL << cpuset_subsys_id);
1145#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001146
Paul Menagec6d57f32009-09-23 15:56:19 -07001147 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001148
1149 while ((token = strsep(&o, ",")) != NULL) {
1150 if (!*token)
1151 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001153 /* Explicitly have no subsystems */
1154 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001155 continue;
1156 }
1157 if (!strcmp(token, "all")) {
1158 /* Mutually exclusive option 'all' + subsystem name */
1159 if (one_ss)
1160 return -EINVAL;
1161 all_ss = true;
1162 continue;
1163 }
Tejun Heo873fe092013-04-14 20:15:26 -07001164 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1165 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1166 continue;
1167 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001169 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 continue;
1171 }
1172 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001173 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 continue;
1175 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001176 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001177 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001178 continue;
1179 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001181 /* Specifying two release agents is forbidden */
1182 if (opts->release_agent)
1183 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001184 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001185 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001186 if (!opts->release_agent)
1187 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 continue;
1189 }
1190 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001191 const char *name = token + 5;
1192 /* Can't specify an empty name */
1193 if (!strlen(name))
1194 return -EINVAL;
1195 /* Must match [\w.-]+ */
1196 for (i = 0; i < strlen(name); i++) {
1197 char c = name[i];
1198 if (isalnum(c))
1199 continue;
1200 if ((c == '.') || (c == '-') || (c == '_'))
1201 continue;
1202 return -EINVAL;
1203 }
1204 /* Specifying two names is forbidden */
1205 if (opts->name)
1206 return -EINVAL;
1207 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001208 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001209 GFP_KERNEL);
1210 if (!opts->name)
1211 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001212
1213 continue;
1214 }
1215
Tejun Heo30159ec2013-06-25 11:53:37 -07001216 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001217 if (strcmp(token, ss->name))
1218 continue;
1219 if (ss->disabled)
1220 continue;
1221
1222 /* Mutually exclusive option 'all' + subsystem name */
1223 if (all_ss)
1224 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001225 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 one_ss = true;
1227
1228 break;
1229 }
1230 if (i == CGROUP_SUBSYS_COUNT)
1231 return -ENOENT;
1232 }
1233
1234 /*
1235 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001236 * otherwise if 'none', 'name=' and a subsystem name options
1237 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001239 if (all_ss || (!one_ss && !opts->none && !opts->name))
1240 for_each_subsys(ss, i)
1241 if (!ss->disabled)
1242 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001243
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001244 /* Consistency checks */
1245
Tejun Heo873fe092013-04-14 20:15:26 -07001246 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1247 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1248
1249 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1250 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1251 return -EINVAL;
1252 }
1253
1254 if (opts->cpuset_clone_children) {
1255 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1256 return -EINVAL;
1257 }
1258 }
1259
Li Zefanf9ab5b52009-06-17 16:26:33 -07001260 /*
1261 * Option noprefix was introduced just for backward compatibility
1262 * with the old cpuset, so we allow noprefix only if mounting just
1263 * the cpuset subsystem.
1264 */
Tejun Heo93438622013-04-14 20:15:25 -07001265 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001266 return -EINVAL;
1267
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001268
1269 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001270 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271 return -EINVAL;
1272
1273 /*
1274 * We either have to specify by name or by subsystems. (So all
1275 * empty hierarchies must have a name).
1276 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001277 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001278 return -EINVAL;
1279
Ben Blumcf5d5942010-03-10 15:22:09 -08001280 /*
1281 * Grab references on all the modules we'll need, so the subsystems
1282 * don't dance around before rebind_subsystems attaches them. This may
1283 * take duplicate reference counts on a subsystem that's already used,
1284 * but rebind_subsystems handles this case.
1285 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001286 for_each_subsys(ss, i) {
1287 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001289 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 module_pin_failed = true;
1291 break;
1292 }
1293 }
1294 if (module_pin_failed) {
1295 /*
1296 * oops, one of the modules was going away. this means that we
1297 * raced with a module_delete call, and to the user this is
1298 * essentially a "subsystem doesn't exist" case.
1299 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001300 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001301 /* drop refcounts only on the ones we took */
1302 unsigned long bit = 1UL << i;
1303
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001304 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001306 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 }
1308 return -ENOENT;
1309 }
1310
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 return 0;
1312}
1313
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001314static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001315{
Tejun Heo30159ec2013-06-25 11:53:37 -07001316 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001317 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001318
Tejun Heoeb178d02013-06-25 18:05:21 -07001319 mutex_lock(&cgroup_mutex);
1320 for_each_subsys(ss, i)
1321 if (subsys_mask & (1UL << i))
1322 module_put(cgroup_subsys[i]->module);
1323 mutex_unlock(&cgroup_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001324}
1325
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1327{
1328 int ret = 0;
1329 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001330 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001332 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333
Tejun Heo873fe092013-04-14 20:15:26 -07001334 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1335 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1336 return -EINVAL;
1337 }
1338
Paul Menagebd89aab2007-10-18 23:40:44 -07001339 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001341 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
1343 /* See what subsystems are wanted */
1344 ret = parse_cgroupfs_options(data, &opts);
1345 if (ret)
1346 goto out_unlock;
1347
Tejun Heoa8a648c2013-06-24 15:21:47 -07001348 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001349 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1350 task_tgid_nr(current), current->comm);
1351
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001352 added_mask = opts.subsys_mask & ~root->subsys_mask;
1353 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001354
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001356 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001357 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001358 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1359 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1360 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 goto out_unlock;
1363 }
1364
Gao feng7083d032012-12-03 09:28:18 +08001365 /*
1366 * Clear out the files of subsystems that should be removed, do
1367 * this before rebind_subsystems, since rebind_subsystems may
1368 * change this hierarchy's subsys_list.
1369 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001370 cgroup_clear_dir(cgrp, removed_mask);
Gao feng7083d032012-12-03 09:28:18 +08001371
Tejun Heoa8a648c2013-06-24 15:21:47 -07001372 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001373 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001374 /* rebind_subsystems failed, re-populate the removed files */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001375 cgroup_populate_dir(cgrp, removed_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001376 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001377 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001379 /* re-populate subsystem files */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001380 cgroup_populate_dir(cgrp, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381
Paul Menage81a6a5c2007-10-18 23:39:38 -07001382 if (opts.release_agent)
1383 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001385 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001386 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001387 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001388 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001389 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoe2bd4162013-06-27 19:37:23 -07001390 if (ret)
1391 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 return ret;
1393}
1394
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001395static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 .statfs = simple_statfs,
1397 .drop_inode = generic_delete_inode,
1398 .show_options = cgroup_show_options,
1399 .remount_fs = cgroup_remount,
1400};
1401
Paul Menagecc31edc2008-10-18 20:28:04 -07001402static void init_cgroup_housekeeping(struct cgroup *cgrp)
1403{
1404 INIT_LIST_HEAD(&cgrp->sibling);
1405 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001406 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001407 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 INIT_LIST_HEAD(&cgrp->pidlists);
1410 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001411 INIT_LIST_HEAD(&cgrp->event_list);
1412 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001413 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414}
Paul Menagec6d57f32009-09-23 15:56:19 -07001415
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416static void init_cgroup_root(struct cgroupfs_root *root)
1417{
Paul Menagebd89aab2007-10-18 23:40:44 -07001418 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001419
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420 INIT_LIST_HEAD(&root->subsys_list);
1421 INIT_LIST_HEAD(&root->root_list);
1422 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001423 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001424 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001425 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426}
1427
Tejun Heofc76df72013-06-25 11:53:37 -07001428static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429{
Tejun Heo1a574232013-04-14 11:36:58 -07001430 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001431
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001432 lockdep_assert_held(&cgroup_mutex);
1433 lockdep_assert_held(&cgroup_root_mutex);
1434
Tejun Heofc76df72013-06-25 11:53:37 -07001435 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1436 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001437 if (id < 0)
1438 return id;
1439
1440 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001441 return 0;
1442}
1443
1444static void cgroup_exit_root_id(struct cgroupfs_root *root)
1445{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001446 lockdep_assert_held(&cgroup_mutex);
1447 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001448
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001449 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001450 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001451 root->hierarchy_id = 0;
1452 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453}
1454
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455static int cgroup_test_super(struct super_block *sb, void *data)
1456{
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458 struct cgroupfs_root *root = sb->s_fs_info;
1459
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 /* If we asked for a name then it must match */
1461 if (opts->name && strcmp(opts->name, root->name))
1462 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001464 /*
1465 * If we asked for subsystems (or explicitly for no
1466 * subsystems) then they must match
1467 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001468 if ((opts->subsys_mask || opts->none)
1469 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 return 0;
1471
1472 return 1;
1473}
1474
Paul Menagec6d57f32009-09-23 15:56:19 -07001475static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1476{
1477 struct cgroupfs_root *root;
1478
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001479 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 return NULL;
1481
1482 root = kzalloc(sizeof(*root), GFP_KERNEL);
1483 if (!root)
1484 return ERR_PTR(-ENOMEM);
1485
1486 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001487
Tejun Heo1672d042013-06-25 18:04:54 -07001488 /*
1489 * We need to set @root->subsys_mask now so that @root can be
1490 * matched by cgroup_test_super() before it finishes
1491 * initialization; otherwise, competing mounts with the same
1492 * options may try to bind the same subsystems instead of waiting
1493 * for the first one leading to unexpected mount errors.
1494 * SUBSYS_BOUND will be set once actual binding is complete.
1495 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001496 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001498 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 if (opts->release_agent)
1500 strcpy(root->release_agent_path, opts->release_agent);
1501 if (opts->name)
1502 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001503 if (opts->cpuset_clone_children)
1504 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 return root;
1506}
1507
Tejun Heofa3ca072013-04-14 11:36:56 -07001508static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001509{
Tejun Heofa3ca072013-04-14 11:36:56 -07001510 if (root) {
1511 /* hierarhcy ID shoulid already have been released */
1512 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001513
Tejun Heofa3ca072013-04-14 11:36:56 -07001514 ida_destroy(&root->cgroup_ida);
1515 kfree(root);
1516 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001517}
1518
Paul Menageddbcc7e2007-10-18 23:39:30 -07001519static int cgroup_set_super(struct super_block *sb, void *data)
1520{
1521 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 struct cgroup_sb_opts *opts = data;
1523
1524 /* If we don't have a new root, we can't set up a new sb */
1525 if (!opts->new_root)
1526 return -EINVAL;
1527
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001528 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529
1530 ret = set_anon_super(sb, NULL);
1531 if (ret)
1532 return ret;
1533
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 sb->s_fs_info = opts->new_root;
1535 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536
1537 sb->s_blocksize = PAGE_CACHE_SIZE;
1538 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1539 sb->s_magic = CGROUP_SUPER_MAGIC;
1540 sb->s_op = &cgroup_ops;
1541
1542 return 0;
1543}
1544
1545static int cgroup_get_rootdir(struct super_block *sb)
1546{
Al Viro0df6a632010-12-21 13:29:29 -05001547 static const struct dentry_operations cgroup_dops = {
1548 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001549 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001550 };
1551
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 struct inode *inode =
1553 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
1555 if (!inode)
1556 return -ENOMEM;
1557
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 inode->i_fop = &simple_dir_operations;
1559 inode->i_op = &cgroup_dir_inode_operations;
1560 /* directories start off with i_nlink == 2 (for "." entry) */
1561 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001562 sb->s_root = d_make_root(inode);
1563 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001565 /* for everything else we want ->d_op set */
1566 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 return 0;
1568}
1569
Al Virof7e83572010-07-26 13:23:11 +04001570static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001572 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573{
1574 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576 int ret = 0;
1577 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001579 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580
1581 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001582 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001584 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 if (ret)
1586 goto out_err;
1587
1588 /*
1589 * Allocate a new cgroup root. We may not need it if we're
1590 * reusing an existing hierarchy.
1591 */
1592 new_root = cgroup_root_from_opts(&opts);
1593 if (IS_ERR(new_root)) {
1594 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001595 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001596 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001597 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001600 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001602 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001603 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001604 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605 }
1606
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 root = sb->s_fs_info;
1608 BUG_ON(!root);
1609 if (root == opts.new_root) {
1610 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001611 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001612 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001613 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001614 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001615 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001616 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617
1618 BUG_ON(sb->s_root != NULL);
1619
1620 ret = cgroup_get_rootdir(sb);
1621 if (ret)
1622 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001623 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001624
Paul Menage817929e2007-10-18 23:39:36 -07001625 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001627 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001629 /* Check for name clashes with existing mounts */
1630 ret = -EBUSY;
1631 if (strlen(root->name))
1632 for_each_active_root(existing_root)
1633 if (!strcmp(existing_root->name, root->name))
1634 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001635
Paul Menage817929e2007-10-18 23:39:36 -07001636 /*
1637 * We're accessing css_set_count without locking
1638 * css_set_lock here, but that's OK - it can only be
1639 * increased by someone holding cgroup_lock, and
1640 * that's us. The worst that can happen is that we
1641 * have some link structures left over
1642 */
Tejun Heo69d02062013-06-12 21:04:50 -07001643 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001644 if (ret)
1645 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001646
Tejun Heofc76df72013-06-25 11:53:37 -07001647 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1648 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001649 if (ret)
1650 goto unlock_drop;
1651
Tejun Heoa8a648c2013-06-24 15:21:47 -07001652 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001653 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001654 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001655 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001657 /*
1658 * There must be no failure case after here, since rebinding
1659 * takes care of subsystems' refcounts, which are explicitly
1660 * dropped in the failure exit path.
1661 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
1663 /* EBUSY should be the only error here */
1664 BUG_ON(ret);
1665
Tejun Heo9871bf92013-06-24 15:21:47 -07001666 list_add(&root->root_list, &cgroup_roots);
1667 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
Li Zefanc12f65d2009-01-07 18:07:42 -08001669 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 root->top_cgroup.dentry = sb->s_root;
1671
Paul Menage817929e2007-10-18 23:39:36 -07001672 /* Link the top cgroup in this hierarchy into all
1673 * the css_set objects */
1674 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001675 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001676 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001677 write_unlock(&css_set_lock);
1678
Tejun Heo69d02062013-06-12 21:04:50 -07001679 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001680
Li Zefanc12f65d2009-01-07 18:07:42 -08001681 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682 BUG_ON(root->number_of_cgroups != 1);
1683
eparis@redhat2ce97382011-06-02 21:20:51 +10001684 cred = override_creds(&init_cred);
Tejun Heo628f7cd2013-06-28 16:24:11 -07001685 cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true);
1686 cgroup_populate_dir(root_cgrp, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001687 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001688 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001690 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001691 } else {
1692 /*
1693 * We re-used an existing hierarchy - the new root (if
1694 * any) is not needed
1695 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001696 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001697
Tejun Heoc7ba8282013-06-29 14:06:10 -07001698 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001699 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1700 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1701 ret = -EINVAL;
1702 goto drop_new_super;
1703 } else {
1704 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1705 }
Tejun Heo873fe092013-04-14 20:15:26 -07001706 }
1707
Ben Blumcf5d5942010-03-10 15:22:09 -08001708 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001709 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 }
1711
Paul Menagec6d57f32009-09-23 15:56:19 -07001712 kfree(opts.release_agent);
1713 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001714 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001716 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001717 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001718 mutex_unlock(&cgroup_root_mutex);
1719 mutex_unlock(&cgroup_mutex);
1720 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001722 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001723 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001724 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001725 out_err:
1726 kfree(opts.release_agent);
1727 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001728 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729}
1730
1731static void cgroup_kill_sb(struct super_block *sb) {
1732 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001733 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001734 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735 int ret;
1736
1737 BUG_ON(!root);
1738
1739 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001740 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
1742 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001743 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744
1745 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001746 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1747 ret = rebind_subsystems(root, 0, root->subsys_mask);
1748 /* Shouldn't be able to fail ... */
1749 BUG_ON(ret);
1750 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751
Paul Menage817929e2007-10-18 23:39:36 -07001752 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001753 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001754 * root cgroup
1755 */
1756 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001757
Tejun Heo69d02062013-06-12 21:04:50 -07001758 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1759 list_del(&link->cset_link);
1760 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001761 kfree(link);
1762 }
1763 write_unlock(&css_set_lock);
1764
Paul Menage839ec542009-01-29 14:25:22 -08001765 if (!list_empty(&root->root_list)) {
1766 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001767 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001768 }
Li Zefane5f6a862009-01-07 18:07:41 -08001769
Tejun Heofa3ca072013-04-14 11:36:56 -07001770 cgroup_exit_root_id(root);
1771
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001772 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 mutex_unlock(&cgroup_mutex);
1774
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001775 simple_xattrs_free(&cgrp->xattrs);
1776
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001778 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779}
1780
1781static struct file_system_type cgroup_fs_type = {
1782 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001783 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 .kill_sb = cgroup_kill_sb,
1785};
1786
Greg KH676db4a2010-08-05 13:53:35 -07001787static struct kobject *cgroup_kobj;
1788
Li Zefana043e3b2008-02-23 15:24:09 -08001789/**
1790 * cgroup_path - generate the path of a cgroup
1791 * @cgrp: the cgroup in question
1792 * @buf: the buffer to write the path into
1793 * @buflen: the length of the buffer
1794 *
Li Zefan65dff752013-03-01 15:01:56 +08001795 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1796 *
1797 * We can't generate cgroup path using dentry->d_name, as accessing
1798 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1799 * inode's i_mutex, while on the other hand cgroup_path() can be called
1800 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001802int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803{
Li Zefan65dff752013-03-01 15:01:56 +08001804 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001806
Tejun Heoda1f2962013-04-14 10:32:19 -07001807 if (!cgrp->parent) {
1808 if (strlcpy(buf, "/", buflen) >= buflen)
1809 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 return 0;
1811 }
1812
Tao Ma316eb662012-11-08 21:36:38 +08001813 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001814 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001815
Li Zefan65dff752013-03-01 15:01:56 +08001816 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001817 do {
Li Zefan65dff752013-03-01 15:01:56 +08001818 const char *name = cgroup_name(cgrp);
1819 int len;
1820
1821 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001823 goto out;
1824 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001825
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001827 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001829
1830 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001831 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001832 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001834out:
1835 rcu_read_unlock();
1836 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001837}
Ben Blum67523c42010-03-10 15:22:11 -08001838EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839
Tejun Heo857a2be2013-04-14 20:50:08 -07001840/**
1841 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1842 * @task: target task
1843 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1844 * @buf: the buffer to write the path into
1845 * @buflen: the length of the buffer
1846 *
1847 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1848 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1849 * be used inside locks used by cgroup controller callbacks.
1850 */
1851int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1852 char *buf, size_t buflen)
1853{
1854 struct cgroupfs_root *root;
1855 struct cgroup *cgrp = NULL;
1856 int ret = -ENOENT;
1857
1858 mutex_lock(&cgroup_mutex);
1859
1860 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1861 if (root) {
1862 cgrp = task_cgroup_from_root(task, root);
1863 ret = cgroup_path(cgrp, buf, buflen);
1864 }
1865
1866 mutex_unlock(&cgroup_mutex);
1867
1868 return ret;
1869}
1870EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1871
Ben Blum74a11662011-05-26 16:25:20 -07001872/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001873 * Control Group taskset
1874 */
Tejun Heo134d3372011-12-12 18:12:21 -08001875struct task_and_cgroup {
1876 struct task_struct *task;
1877 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001878 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001879};
1880
Tejun Heo2f7ee562011-12-12 18:12:21 -08001881struct cgroup_taskset {
1882 struct task_and_cgroup single;
1883 struct flex_array *tc_array;
1884 int tc_array_len;
1885 int idx;
1886 struct cgroup *cur_cgrp;
1887};
1888
1889/**
1890 * cgroup_taskset_first - reset taskset and return the first task
1891 * @tset: taskset of interest
1892 *
1893 * @tset iteration is initialized and the first task is returned.
1894 */
1895struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1896{
1897 if (tset->tc_array) {
1898 tset->idx = 0;
1899 return cgroup_taskset_next(tset);
1900 } else {
1901 tset->cur_cgrp = tset->single.cgrp;
1902 return tset->single.task;
1903 }
1904}
1905EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1906
1907/**
1908 * cgroup_taskset_next - iterate to the next task in taskset
1909 * @tset: taskset of interest
1910 *
1911 * Return the next task in @tset. Iteration must have been initialized
1912 * with cgroup_taskset_first().
1913 */
1914struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1915{
1916 struct task_and_cgroup *tc;
1917
1918 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1919 return NULL;
1920
1921 tc = flex_array_get(tset->tc_array, tset->idx++);
1922 tset->cur_cgrp = tc->cgrp;
1923 return tc->task;
1924}
1925EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1926
1927/**
1928 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1929 * @tset: taskset of interest
1930 *
1931 * Return the cgroup for the current (last returned) task of @tset. This
1932 * function must be preceded by either cgroup_taskset_first() or
1933 * cgroup_taskset_next().
1934 */
1935struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1936{
1937 return tset->cur_cgrp;
1938}
1939EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1940
1941/**
1942 * cgroup_taskset_size - return the number of tasks in taskset
1943 * @tset: taskset of interest
1944 */
1945int cgroup_taskset_size(struct cgroup_taskset *tset)
1946{
1947 return tset->tc_array ? tset->tc_array_len : 1;
1948}
1949EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1950
1951
Ben Blum74a11662011-05-26 16:25:20 -07001952/*
1953 * cgroup_task_migrate - move a task from one cgroup to another.
1954 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001955 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001956 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001957static void cgroup_task_migrate(struct cgroup *old_cgrp,
1958 struct task_struct *tsk,
1959 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001960{
Tejun Heo5abb8852013-06-12 21:04:49 -07001961 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001962
1963 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001964 * We are synchronized through threadgroup_lock() against PF_EXITING
1965 * setting such that we can't race against cgroup_exit() changing the
1966 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001967 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001968 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001969 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001970
Ben Blum74a11662011-05-26 16:25:20 -07001971 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001972 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001973 task_unlock(tsk);
1974
1975 /* Update the css_set linked lists if we're using them */
1976 write_lock(&css_set_lock);
1977 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001978 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001979 write_unlock(&css_set_lock);
1980
1981 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001982 * We just gained a reference on old_cset by taking it from the
1983 * task. As trading it for new_cset is protected by cgroup_mutex,
1984 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001985 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001986 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1987 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001988}
1989
Li Zefana043e3b2008-02-23 15:24:09 -08001990/**
Li Zefan081aa452013-03-13 09:17:09 +08001991 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001992 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001993 * @tsk: the task or the leader of the threadgroup to be attached
1994 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001995 *
Tejun Heo257058a2011-12-12 18:12:21 -08001996 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001997 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001998 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001999static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2000 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002001{
2002 int retval, i, group_size;
2003 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002004 struct cgroupfs_root *root = cgrp->root;
2005 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002006 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002007 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002008 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002009 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002010
2011 /*
2012 * step 0: in order to do expensive, possibly blocking operations for
2013 * every thread, we cannot iterate the thread group list, since it needs
2014 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002015 * group - group_rwsem prevents new threads from appearing, and if
2016 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002017 */
Li Zefan081aa452013-03-13 09:17:09 +08002018 if (threadgroup)
2019 group_size = get_nr_threads(tsk);
2020 else
2021 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002022 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002023 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002024 if (!group)
2025 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002026 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002027 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002028 if (retval)
2029 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002030
Ben Blum74a11662011-05-26 16:25:20 -07002031 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002032 /*
2033 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2034 * already PF_EXITING could be freed from underneath us unless we
2035 * take an rcu_read_lock.
2036 */
2037 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002038 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002039 struct task_and_cgroup ent;
2040
Tejun Heocd3d0952011-12-12 18:12:21 -08002041 /* @tsk either already exited or can't exit until the end */
2042 if (tsk->flags & PF_EXITING)
2043 continue;
2044
Ben Blum74a11662011-05-26 16:25:20 -07002045 /* as per above, nr_threads may decrease, but not increase. */
2046 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002047 ent.task = tsk;
2048 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002049 /* nothing to do if this task is already in the cgroup */
2050 if (ent.cgrp == cgrp)
2051 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002052 /*
2053 * saying GFP_ATOMIC has no effect here because we did prealloc
2054 * earlier, but it's good form to communicate our expectations.
2055 */
Tejun Heo134d3372011-12-12 18:12:21 -08002056 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002057 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002058 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002059
2060 if (!threadgroup)
2061 break;
Ben Blum74a11662011-05-26 16:25:20 -07002062 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002063 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002064 /* remember the number of threads in the array for later. */
2065 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002066 tset.tc_array = group;
2067 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002068
Tejun Heo134d3372011-12-12 18:12:21 -08002069 /* methods shouldn't be called if no task is actually migrating */
2070 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002071 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002072 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002073
Ben Blum74a11662011-05-26 16:25:20 -07002074 /*
2075 * step 1: check that we can legitimately attach to the cgroup.
2076 */
Tejun Heo5549c492013-06-24 15:21:48 -07002077 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002078 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002079 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002080 if (retval) {
2081 failed_ss = ss;
2082 goto out_cancel_attach;
2083 }
2084 }
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
2086
2087 /*
2088 * step 2: make sure css_sets exist for all threads to be migrated.
2089 * we use find_css_set, which allocates a new one if necessary.
2090 */
Ben Blum74a11662011-05-26 16:25:20 -07002091 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002092 struct css_set *old_cset;
2093
Tejun Heo134d3372011-12-12 18:12:21 -08002094 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002095 old_cset = task_css_set(tc->task);
2096 tc->cg = find_css_set(old_cset, cgrp);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097 if (!tc->cg) {
2098 retval = -ENOMEM;
2099 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002100 }
2101 }
2102
2103 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002104 * step 3: now that we're guaranteed success wrt the css_sets,
2105 * proceed to move all tasks to the new cgroup. There are no
2106 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002107 */
Ben Blum74a11662011-05-26 16:25:20 -07002108 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002109 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002110 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002111 }
2112 /* nothing is sensitive to fork() after this point. */
2113
2114 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002115 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002116 */
Tejun Heo5549c492013-06-24 15:21:48 -07002117 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002118 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002119 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121
2122 /*
2123 * step 5: success! and cleanup
2124 */
Ben Blum74a11662011-05-26 16:25:20 -07002125 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002126out_put_css_set_refs:
2127 if (retval) {
2128 for (i = 0; i < group_size; i++) {
2129 tc = flex_array_get(group, i);
2130 if (!tc->cg)
2131 break;
2132 put_css_set(tc->cg);
2133 }
Ben Blum74a11662011-05-26 16:25:20 -07002134 }
2135out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002136 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002137 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002138 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002139 break;
Ben Blum74a11662011-05-26 16:25:20 -07002140 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002141 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002142 }
2143 }
Ben Blum74a11662011-05-26 16:25:20 -07002144out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002145 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002146 return retval;
2147}
2148
2149/*
2150 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002151 * function to attach either it or all tasks in its threadgroup. Will lock
2152 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002153 */
2154static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002156 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002157 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002158 int ret;
2159
Ben Blum74a11662011-05-26 16:25:20 -07002160 if (!cgroup_lock_live_group(cgrp))
2161 return -ENODEV;
2162
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002163retry_find_task:
2164 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002165 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002166 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002167 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 ret= -ESRCH;
2170 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 }
Ben Blum74a11662011-05-26 16:25:20 -07002172 /*
2173 * even if we're attaching all tasks in the thread group, we
2174 * only need to check permissions on one of them.
2175 */
David Howellsc69e8d92008-11-14 10:39:19 +11002176 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002177 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2178 !uid_eq(cred->euid, tcred->uid) &&
2179 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002180 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002181 ret = -EACCES;
2182 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002183 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002184 } else
2185 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002186
2187 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002189
2190 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002191 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002192 * trapped in a cpuset, or RT worker may be born in a cgroup
2193 * with no rt_runtime allocated. Just say no.
2194 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002195 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002196 ret = -EINVAL;
2197 rcu_read_unlock();
2198 goto out_unlock_cgroup;
2199 }
2200
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002201 get_task_struct(tsk);
2202 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002203
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002204 threadgroup_lock(tsk);
2205 if (threadgroup) {
2206 if (!thread_group_leader(tsk)) {
2207 /*
2208 * a race with de_thread from another thread's exec()
2209 * may strip us of our leadership, if this happens,
2210 * there is no choice but to throw this task away and
2211 * try again; this is
2212 * "double-double-toil-and-trouble-check locking".
2213 */
2214 threadgroup_unlock(tsk);
2215 put_task_struct(tsk);
2216 goto retry_find_task;
2217 }
Li Zefan081aa452013-03-13 09:17:09 +08002218 }
2219
2220 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2221
Tejun Heocd3d0952011-12-12 18:12:21 -08002222 threadgroup_unlock(tsk);
2223
Paul Menagebbcb81d2007-10-18 23:39:32 -07002224 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002225out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002226 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002227 return ret;
2228}
2229
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002230/**
2231 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2232 * @from: attach to all cgroups of a given task
2233 * @tsk: the task to be attached
2234 */
2235int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2236{
2237 struct cgroupfs_root *root;
2238 int retval = 0;
2239
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002241 for_each_active_root(root) {
2242 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2243
2244 retval = cgroup_attach_task(from_cg, tsk, false);
2245 if (retval)
2246 break;
2247 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002248 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002249
2250 return retval;
2251}
2252EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2253
Paul Menageaf351022008-07-25 01:47:01 -07002254static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2255{
Ben Blum74a11662011-05-26 16:25:20 -07002256 return attach_task_by_pid(cgrp, pid, false);
2257}
2258
2259static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2260{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002261 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002262}
2263
Paul Menagee788e062008-07-25 01:46:59 -07002264static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2265 const char *buffer)
2266{
2267 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002268 if (strlen(buffer) >= PATH_MAX)
2269 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002270 if (!cgroup_lock_live_group(cgrp))
2271 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002272 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002273 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002274 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002275 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002276 return 0;
2277}
2278
2279static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2280 struct seq_file *seq)
2281{
2282 if (!cgroup_lock_live_group(cgrp))
2283 return -ENODEV;
2284 seq_puts(seq, cgrp->root->release_agent_path);
2285 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002286 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002287 return 0;
2288}
2289
Tejun Heo873fe092013-04-14 20:15:26 -07002290static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2291 struct seq_file *seq)
2292{
2293 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002294 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002295}
2296
Paul Menage84eea842008-07-25 01:47:00 -07002297/* A buffer size big enough for numbers or short strings */
2298#define CGROUP_LOCAL_BUFFER_SIZE 64
2299
Paul Menagee73d2c62008-04-29 01:00:06 -07002300static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002301 struct file *file,
2302 const char __user *userbuf,
2303 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002304{
Paul Menage84eea842008-07-25 01:47:00 -07002305 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002306 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002307 char *end;
2308
2309 if (!nbytes)
2310 return -EINVAL;
2311 if (nbytes >= sizeof(buffer))
2312 return -E2BIG;
2313 if (copy_from_user(buffer, userbuf, nbytes))
2314 return -EFAULT;
2315
2316 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002317 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002318 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002319 if (*end)
2320 return -EINVAL;
2321 retval = cft->write_u64(cgrp, cft, val);
2322 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002324 if (*end)
2325 return -EINVAL;
2326 retval = cft->write_s64(cgrp, cft, val);
2327 }
Paul Menage355e0c42007-10-18 23:39:33 -07002328 if (!retval)
2329 retval = nbytes;
2330 return retval;
2331}
2332
Paul Menagedb3b1492008-07-25 01:46:58 -07002333static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2334 struct file *file,
2335 const char __user *userbuf,
2336 size_t nbytes, loff_t *unused_ppos)
2337{
Paul Menage84eea842008-07-25 01:47:00 -07002338 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002339 int retval = 0;
2340 size_t max_bytes = cft->max_write_len;
2341 char *buffer = local_buffer;
2342
2343 if (!max_bytes)
2344 max_bytes = sizeof(local_buffer) - 1;
2345 if (nbytes >= max_bytes)
2346 return -E2BIG;
2347 /* Allocate a dynamic buffer if we need one */
2348 if (nbytes >= sizeof(local_buffer)) {
2349 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2350 if (buffer == NULL)
2351 return -ENOMEM;
2352 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002353 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2354 retval = -EFAULT;
2355 goto out;
2356 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002357
2358 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002359 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002360 if (!retval)
2361 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002362out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002363 if (buffer != local_buffer)
2364 kfree(buffer);
2365 return retval;
2366}
2367
Paul Menageddbcc7e2007-10-18 23:39:30 -07002368static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2369 size_t nbytes, loff_t *ppos)
2370{
2371 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002372 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373
Tejun Heo54766d42013-06-12 21:04:53 -07002374 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002376 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002378 if (cft->write_u64 || cft->write_s64)
2379 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002380 if (cft->write_string)
2381 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002382 if (cft->trigger) {
2383 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2384 return ret ? ret : nbytes;
2385 }
Paul Menage355e0c42007-10-18 23:39:33 -07002386 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387}
2388
Paul Menagef4c753b2008-04-29 00:59:56 -07002389static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2390 struct file *file,
2391 char __user *buf, size_t nbytes,
2392 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002393{
Paul Menage84eea842008-07-25 01:47:00 -07002394 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002395 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2397
2398 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2399}
2400
Paul Menagee73d2c62008-04-29 01:00:06 -07002401static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2402 struct file *file,
2403 char __user *buf, size_t nbytes,
2404 loff_t *ppos)
2405{
Paul Menage84eea842008-07-25 01:47:00 -07002406 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002407 s64 val = cft->read_s64(cgrp, cft);
2408 int len = sprintf(tmp, "%lld\n", (long long) val);
2409
2410 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2411}
2412
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2414 size_t nbytes, loff_t *ppos)
2415{
2416 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002417 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418
Tejun Heo54766d42013-06-12 21:04:53 -07002419 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 return -ENODEV;
2421
2422 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002423 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002424 if (cft->read_u64)
2425 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002426 if (cft->read_s64)
2427 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428 return -EINVAL;
2429}
2430
Paul Menage91796562008-04-29 01:00:01 -07002431/*
2432 * seqfile ops/methods for returning structured data. Currently just
2433 * supports string->u64 maps, but can be extended in future.
2434 */
2435
2436struct cgroup_seqfile_state {
2437 struct cftype *cft;
2438 struct cgroup *cgroup;
2439};
2440
2441static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2442{
2443 struct seq_file *sf = cb->state;
2444 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2445}
2446
2447static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2448{
2449 struct cgroup_seqfile_state *state = m->private;
2450 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002451 if (cft->read_map) {
2452 struct cgroup_map_cb cb = {
2453 .fill = cgroup_map_add,
2454 .state = m,
2455 };
2456 return cft->read_map(state->cgroup, cft, &cb);
2457 }
2458 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002459}
2460
Adrian Bunk96930a62008-07-25 19:46:21 -07002461static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002462{
2463 struct seq_file *seq = file->private_data;
2464 kfree(seq->private);
2465 return single_release(inode, file);
2466}
2467
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002468static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002469 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002470 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002471 .llseek = seq_lseek,
2472 .release = cgroup_seqfile_release,
2473};
2474
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475static int cgroup_file_open(struct inode *inode, struct file *file)
2476{
2477 int err;
2478 struct cftype *cft;
2479
2480 err = generic_file_open(inode, file);
2481 if (err)
2482 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002484
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002485 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002486 struct cgroup_seqfile_state *state;
2487
2488 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002489 if (!state)
2490 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002491
Paul Menage91796562008-04-29 01:00:01 -07002492 state->cft = cft;
2493 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2494 file->f_op = &cgroup_seqfile_operations;
2495 err = single_open(file, cgroup_seqfile_show, state);
2496 if (err < 0)
2497 kfree(state);
2498 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002499 err = cft->open(inode, file);
2500 else
2501 err = 0;
2502
2503 return err;
2504}
2505
2506static int cgroup_file_release(struct inode *inode, struct file *file)
2507{
2508 struct cftype *cft = __d_cft(file->f_dentry);
2509 if (cft->release)
2510 return cft->release(inode, file);
2511 return 0;
2512}
2513
2514/*
2515 * cgroup_rename - Only allow simple rename of directories in place.
2516 */
2517static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2518 struct inode *new_dir, struct dentry *new_dentry)
2519{
Li Zefan65dff752013-03-01 15:01:56 +08002520 int ret;
2521 struct cgroup_name *name, *old_name;
2522 struct cgroup *cgrp;
2523
2524 /*
2525 * It's convinient to use parent dir's i_mutex to protected
2526 * cgrp->name.
2527 */
2528 lockdep_assert_held(&old_dir->i_mutex);
2529
Paul Menageddbcc7e2007-10-18 23:39:30 -07002530 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2531 return -ENOTDIR;
2532 if (new_dentry->d_inode)
2533 return -EEXIST;
2534 if (old_dir != new_dir)
2535 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002536
2537 cgrp = __d_cgrp(old_dentry);
2538
Tejun Heo6db8e852013-06-14 11:18:22 -07002539 /*
2540 * This isn't a proper migration and its usefulness is very
2541 * limited. Disallow if sane_behavior.
2542 */
2543 if (cgroup_sane_behavior(cgrp))
2544 return -EPERM;
2545
Li Zefan65dff752013-03-01 15:01:56 +08002546 name = cgroup_alloc_name(new_dentry);
2547 if (!name)
2548 return -ENOMEM;
2549
2550 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2551 if (ret) {
2552 kfree(name);
2553 return ret;
2554 }
2555
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002556 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002557 rcu_assign_pointer(cgrp->name, name);
2558
2559 kfree_rcu(old_name, rcu_head);
2560 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002561}
2562
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002563static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2564{
2565 if (S_ISDIR(dentry->d_inode->i_mode))
2566 return &__d_cgrp(dentry)->xattrs;
2567 else
Li Zefan712317a2013-04-18 23:09:52 -07002568 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002569}
2570
2571static inline int xattr_enabled(struct dentry *dentry)
2572{
2573 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002574 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002575}
2576
2577static bool is_valid_xattr(const char *name)
2578{
2579 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2580 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2581 return true;
2582 return false;
2583}
2584
2585static int cgroup_setxattr(struct dentry *dentry, const char *name,
2586 const void *val, size_t size, int flags)
2587{
2588 if (!xattr_enabled(dentry))
2589 return -EOPNOTSUPP;
2590 if (!is_valid_xattr(name))
2591 return -EINVAL;
2592 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2593}
2594
2595static int cgroup_removexattr(struct dentry *dentry, const char *name)
2596{
2597 if (!xattr_enabled(dentry))
2598 return -EOPNOTSUPP;
2599 if (!is_valid_xattr(name))
2600 return -EINVAL;
2601 return simple_xattr_remove(__d_xattrs(dentry), name);
2602}
2603
2604static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2605 void *buf, size_t size)
2606{
2607 if (!xattr_enabled(dentry))
2608 return -EOPNOTSUPP;
2609 if (!is_valid_xattr(name))
2610 return -EINVAL;
2611 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2612}
2613
2614static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2615{
2616 if (!xattr_enabled(dentry))
2617 return -EOPNOTSUPP;
2618 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2619}
2620
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002621static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622 .read = cgroup_file_read,
2623 .write = cgroup_file_write,
2624 .llseek = generic_file_llseek,
2625 .open = cgroup_file_open,
2626 .release = cgroup_file_release,
2627};
2628
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002629static const struct inode_operations cgroup_file_inode_operations = {
2630 .setxattr = cgroup_setxattr,
2631 .getxattr = cgroup_getxattr,
2632 .listxattr = cgroup_listxattr,
2633 .removexattr = cgroup_removexattr,
2634};
2635
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002636static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002637 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002638 .mkdir = cgroup_mkdir,
2639 .rmdir = cgroup_rmdir,
2640 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002641 .setxattr = cgroup_setxattr,
2642 .getxattr = cgroup_getxattr,
2643 .listxattr = cgroup_listxattr,
2644 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645};
2646
Al Viro00cd8dd2012-06-10 17:13:09 -04002647static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002648{
2649 if (dentry->d_name.len > NAME_MAX)
2650 return ERR_PTR(-ENAMETOOLONG);
2651 d_add(dentry, NULL);
2652 return NULL;
2653}
2654
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002655/*
2656 * Check if a file is a control file
2657 */
2658static inline struct cftype *__file_cft(struct file *file)
2659{
Al Viro496ad9a2013-01-23 17:07:38 -05002660 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002661 return ERR_PTR(-EINVAL);
2662 return __d_cft(file->f_dentry);
2663}
2664
Al Viroa5e7ed32011-07-26 01:55:55 -04002665static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002666 struct super_block *sb)
2667{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002668 struct inode *inode;
2669
2670 if (!dentry)
2671 return -ENOENT;
2672 if (dentry->d_inode)
2673 return -EEXIST;
2674
2675 inode = cgroup_new_inode(mode, sb);
2676 if (!inode)
2677 return -ENOMEM;
2678
2679 if (S_ISDIR(mode)) {
2680 inode->i_op = &cgroup_dir_inode_operations;
2681 inode->i_fop = &simple_dir_operations;
2682
2683 /* start off with i_nlink == 2 (for "." entry) */
2684 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002685 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686
Tejun Heob8a2df62012-11-19 08:13:37 -08002687 /*
2688 * Control reaches here with cgroup_mutex held.
2689 * @inode->i_mutex should nest outside cgroup_mutex but we
2690 * want to populate it immediately without releasing
2691 * cgroup_mutex. As @inode isn't visible to anyone else
2692 * yet, trylock will always succeed without affecting
2693 * lockdep checks.
2694 */
2695 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696 } else if (S_ISREG(mode)) {
2697 inode->i_size = 0;
2698 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002699 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002700 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 d_instantiate(dentry, inode);
2702 dget(dentry); /* Extra count - pin the dentry in core */
2703 return 0;
2704}
2705
Li Zefan099fca32009-04-02 16:57:29 -07002706/**
2707 * cgroup_file_mode - deduce file mode of a control file
2708 * @cft: the control file in question
2709 *
2710 * returns cft->mode if ->mode is not 0
2711 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2712 * returns S_IRUGO if it has only a read handler
2713 * returns S_IWUSR if it has only a write hander
2714 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002715static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002716{
Al Viroa5e7ed32011-07-26 01:55:55 -04002717 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002718
2719 if (cft->mode)
2720 return cft->mode;
2721
2722 if (cft->read || cft->read_u64 || cft->read_s64 ||
2723 cft->read_map || cft->read_seq_string)
2724 mode |= S_IRUGO;
2725
2726 if (cft->write || cft->write_u64 || cft->write_s64 ||
2727 cft->write_string || cft->trigger)
2728 mode |= S_IWUSR;
2729
2730 return mode;
2731}
2732
Tejun Heodb0416b2012-04-01 12:09:55 -07002733static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002734 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735{
Paul Menagebd89aab2007-10-18 23:40:44 -07002736 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002741 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002743
Tejun Heo93438622013-04-14 20:15:25 -07002744 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 strcpy(name, subsys->name);
2746 strcat(name, ".");
2747 }
2748 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002749
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002751
2752 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2753 if (!cfe)
2754 return -ENOMEM;
2755
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002757 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002759 goto out;
2760 }
2761
Li Zefand6cbf352013-05-14 19:44:20 +08002762 cfe->type = (void *)cft;
2763 cfe->dentry = dentry;
2764 dentry->d_fsdata = cfe;
2765 simple_xattrs_init(&cfe->xattrs);
2766
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767 mode = cgroup_file_mode(cft);
2768 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2769 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002770 list_add_tail(&cfe->node, &parent->files);
2771 cfe = NULL;
2772 }
2773 dput(dentry);
2774out:
2775 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002776 return error;
2777}
2778
Tejun Heob1f28d32013-06-28 16:24:10 -07002779/**
2780 * cgroup_addrm_files - add or remove files to a cgroup directory
2781 * @cgrp: the target cgroup
2782 * @subsys: the subsystem of files to be added
2783 * @cfts: array of cftypes to be added
2784 * @is_add: whether to add or remove
2785 *
2786 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2787 * All @cfts should belong to @subsys. For removals, this function never
2788 * fails. If addition fails, this function doesn't remove files already
2789 * added. The caller is responsible for cleaning up.
2790 */
Tejun Heo79578622012-04-01 12:09:56 -07002791static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002792 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002793{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002794 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002795 int ret;
2796
2797 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2798 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002799
2800 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002801 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002802 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2803 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002804 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2805 continue;
2806 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2807 continue;
2808
Li Zefan2739d3c2013-01-21 18:18:33 +08002809 if (is_add) {
Tejun Heob1f28d32013-06-28 16:24:10 -07002810 ret = cgroup_add_file(cgrp, subsys, cft);
2811 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002812 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002813 cft->name, ret);
2814 return ret;
2815 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002816 } else {
2817 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002818 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002819 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002820 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002821}
2822
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002824 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002825{
2826 /*
2827 * Thanks to the entanglement with vfs inode locking, we can't walk
2828 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002829 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2830 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002831 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002832 mutex_lock(&cgroup_mutex);
2833}
2834
Tejun Heo9ccece82013-06-28 16:24:11 -07002835static int cgroup_cfts_commit(struct cgroup_subsys *ss,
2836 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002837 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838{
2839 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002840 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002841 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002842 struct dentry *prev = NULL;
2843 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002844 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002845 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002846
2847 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002848 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002849 !atomic_inc_not_zero(&sb->s_active)) {
2850 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002851 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852 }
2853
Li Zefane8c82d22013-06-18 18:48:37 +08002854 /*
2855 * All cgroups which are created after we drop cgroup_mutex will
2856 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002857 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002858 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002859 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002860
Tejun Heo8e3f6542012-04-01 12:09:55 -07002861 mutex_unlock(&cgroup_mutex);
2862
Li Zefane8c82d22013-06-18 18:48:37 +08002863 /* @root always needs to be updated */
2864 inode = root->dentry->d_inode;
2865 mutex_lock(&inode->i_mutex);
2866 mutex_lock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002867 ret = cgroup_addrm_files(root, ss, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002868 mutex_unlock(&cgroup_mutex);
2869 mutex_unlock(&inode->i_mutex);
2870
Tejun Heo9ccece82013-06-28 16:24:11 -07002871 if (ret)
2872 goto out_deact;
2873
Li Zefane8c82d22013-06-18 18:48:37 +08002874 /* add/rm files for all cgroups created before */
2875 rcu_read_lock();
2876 cgroup_for_each_descendant_pre(cgrp, root) {
2877 if (cgroup_is_dead(cgrp))
2878 continue;
2879
2880 inode = cgrp->dentry->d_inode;
2881 dget(cgrp->dentry);
2882 rcu_read_unlock();
2883
2884 dput(prev);
2885 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002886
2887 mutex_lock(&inode->i_mutex);
2888 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002889 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo9ccece82013-06-28 16:24:11 -07002890 ret = cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002891 mutex_unlock(&cgroup_mutex);
2892 mutex_unlock(&inode->i_mutex);
2893
Li Zefane8c82d22013-06-18 18:48:37 +08002894 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002895 if (ret)
2896 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002897 }
Li Zefane8c82d22013-06-18 18:48:37 +08002898 rcu_read_unlock();
2899 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002900out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002901 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002902 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002903}
2904
2905/**
2906 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2907 * @ss: target cgroup subsystem
2908 * @cfts: zero-length name terminated array of cftypes
2909 *
2910 * Register @cfts to @ss. Files described by @cfts are created for all
2911 * existing cgroups to which @ss is attached and all future cgroups will
2912 * have them too. This function can be called anytime whether @ss is
2913 * attached or not.
2914 *
2915 * Returns 0 on successful registration, -errno on failure. Note that this
2916 * function currently returns 0 as long as @cfts registration is successful
2917 * even if some file creation attempts on existing cgroups fail.
2918 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002919int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002920{
2921 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002922 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002923
2924 set = kzalloc(sizeof(*set), GFP_KERNEL);
2925 if (!set)
2926 return -ENOMEM;
2927
2928 cgroup_cfts_prepare();
2929 set->cfts = cfts;
2930 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo9ccece82013-06-28 16:24:11 -07002931 ret = cgroup_cfts_commit(ss, cfts, true);
2932 if (ret)
2933 cgroup_rm_cftypes(ss, cfts);
2934 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002935}
2936EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2937
Li Zefana043e3b2008-02-23 15:24:09 -08002938/**
Tejun Heo79578622012-04-01 12:09:56 -07002939 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2940 * @ss: target cgroup subsystem
2941 * @cfts: zero-length name terminated array of cftypes
2942 *
2943 * Unregister @cfts from @ss. Files described by @cfts are removed from
2944 * all existing cgroups to which @ss is attached and all future cgroups
2945 * won't have them either. This function can be called anytime whether @ss
2946 * is attached or not.
2947 *
2948 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2949 * registered with @ss.
2950 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002951int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002952{
2953 struct cftype_set *set;
2954
2955 cgroup_cfts_prepare();
2956
2957 list_for_each_entry(set, &ss->cftsets, node) {
2958 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002959 list_del(&set->node);
2960 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002961 cgroup_cfts_commit(ss, cfts, false);
2962 return 0;
2963 }
2964 }
2965
2966 cgroup_cfts_commit(ss, NULL, false);
2967 return -ENOENT;
2968}
2969
2970/**
Li Zefana043e3b2008-02-23 15:24:09 -08002971 * cgroup_task_count - count the number of tasks in a cgroup.
2972 * @cgrp: the cgroup in question
2973 *
2974 * Return the number of tasks in the cgroup.
2975 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002976int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002977{
2978 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002979 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002980
Paul Menage817929e2007-10-18 23:39:36 -07002981 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002982 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2983 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002984 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002985 return count;
2986}
2987
2988/*
Paul Menage817929e2007-10-18 23:39:36 -07002989 * Advance a list_head iterator. The iterator should be positioned at
2990 * the start of a css_set
2991 */
Tejun Heo69d02062013-06-12 21:04:50 -07002992static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002993{
Tejun Heo69d02062013-06-12 21:04:50 -07002994 struct list_head *l = it->cset_link;
2995 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002996 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002997
2998 /* Advance to the next non-empty css_set */
2999 do {
3000 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003001 if (l == &cgrp->cset_links) {
3002 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07003003 return;
3004 }
Tejun Heo69d02062013-06-12 21:04:50 -07003005 link = list_entry(l, struct cgrp_cset_link, cset_link);
3006 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07003007 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07003008 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07003009 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07003010}
3011
Cliff Wickman31a7df02008-02-07 00:14:42 -08003012/*
3013 * To reduce the fork() overhead for systems that are not actually
3014 * using their cgroups capability, we don't maintain the lists running
3015 * through each css_set to its tasks until we see the list actually
3016 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003017 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003018static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019{
3020 struct task_struct *p, *g;
3021 write_lock(&css_set_lock);
3022 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003023 /*
3024 * We need tasklist_lock because RCU is not safe against
3025 * while_each_thread(). Besides, a forking task that has passed
3026 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3027 * is not guaranteed to have its child immediately visible in the
3028 * tasklist if we walk through it with RCU.
3029 */
3030 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003031 do_each_thread(g, p) {
3032 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003033 /*
3034 * We should check if the process is exiting, otherwise
3035 * it will race with cgroup_exit() in that the list
3036 * entry won't be deleted though the process has exited.
3037 */
3038 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003039 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003040 task_unlock(p);
3041 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003042 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003043 write_unlock(&css_set_lock);
3044}
3045
Tejun Heo574bd9f2012-11-09 09:12:29 -08003046/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003047 * cgroup_next_sibling - find the next sibling of a given cgroup
3048 * @pos: the current cgroup
3049 *
3050 * This function returns the next sibling of @pos and should be called
3051 * under RCU read lock. The only requirement is that @pos is accessible.
3052 * The next sibling is guaranteed to be returned regardless of @pos's
3053 * state.
3054 */
3055struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3056{
3057 struct cgroup *next;
3058
3059 WARN_ON_ONCE(!rcu_read_lock_held());
3060
3061 /*
3062 * @pos could already have been removed. Once a cgroup is removed,
3063 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003064 * changes. As CGRP_DEAD assertion is serialized and happens
3065 * before the cgroup is taken off the ->sibling list, if we see it
3066 * unasserted, it's guaranteed that the next sibling hasn't
3067 * finished its grace period even if it's already removed, and thus
3068 * safe to dereference from this RCU critical section. If
3069 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3070 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003071 */
Tejun Heo54766d42013-06-12 21:04:53 -07003072 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003073 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3074 if (&next->sibling != &pos->parent->children)
3075 return next;
3076 return NULL;
3077 }
3078
3079 /*
3080 * Can't dereference the next pointer. Each cgroup is given a
3081 * monotonically increasing unique serial number and always
3082 * appended to the sibling list, so the next one can be found by
3083 * walking the parent's children until we see a cgroup with higher
3084 * serial number than @pos's.
3085 *
3086 * While this path can be slow, it's taken only when either the
3087 * current cgroup is removed or iteration and removal race.
3088 */
3089 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3090 if (next->serial_nr > pos->serial_nr)
3091 return next;
3092 return NULL;
3093}
3094EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3095
3096/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3098 * @pos: the current position (%NULL to initiate traversal)
3099 * @cgroup: cgroup whose descendants to walk
3100 *
3101 * To be used by cgroup_for_each_descendant_pre(). Find the next
3102 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003103 *
3104 * While this function requires RCU read locking, it doesn't require the
3105 * whole traversal to be contained in a single RCU critical section. This
3106 * function will return the correct next descendant as long as both @pos
3107 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 */
3109struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3110 struct cgroup *cgroup)
3111{
3112 struct cgroup *next;
3113
3114 WARN_ON_ONCE(!rcu_read_lock_held());
3115
3116 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003117 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003119
3120 /* visit the first child if exists */
3121 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3122 if (next)
3123 return next;
3124
3125 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003126 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003127 next = cgroup_next_sibling(pos);
3128 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003129 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003130 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003131 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003132
3133 return NULL;
3134}
3135EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3136
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003137/**
3138 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3139 * @pos: cgroup of interest
3140 *
3141 * Return the rightmost descendant of @pos. If there's no descendant,
3142 * @pos is returned. This can be used during pre-order traversal to skip
3143 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003144 *
3145 * While this function requires RCU read locking, it doesn't require the
3146 * whole traversal to be contained in a single RCU critical section. This
3147 * function will return the correct rightmost descendant as long as @pos is
3148 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003149 */
3150struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3151{
3152 struct cgroup *last, *tmp;
3153
3154 WARN_ON_ONCE(!rcu_read_lock_held());
3155
3156 do {
3157 last = pos;
3158 /* ->prev isn't RCU safe, walk ->next till the end */
3159 pos = NULL;
3160 list_for_each_entry_rcu(tmp, &last->children, sibling)
3161 pos = tmp;
3162 } while (pos);
3163
3164 return last;
3165}
3166EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3167
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3169{
3170 struct cgroup *last;
3171
3172 do {
3173 last = pos;
3174 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3175 sibling);
3176 } while (pos);
3177
3178 return last;
3179}
3180
3181/**
3182 * cgroup_next_descendant_post - find the next descendant for post-order walk
3183 * @pos: the current position (%NULL to initiate traversal)
3184 * @cgroup: cgroup whose descendants to walk
3185 *
3186 * To be used by cgroup_for_each_descendant_post(). Find the next
3187 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003188 *
3189 * While this function requires RCU read locking, it doesn't require the
3190 * whole traversal to be contained in a single RCU critical section. This
3191 * function will return the correct next descendant as long as both @pos
3192 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193 */
3194struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3195 struct cgroup *cgroup)
3196{
3197 struct cgroup *next;
3198
3199 WARN_ON_ONCE(!rcu_read_lock_held());
3200
3201 /* if first iteration, visit the leftmost descendant */
3202 if (!pos) {
3203 next = cgroup_leftmost_descendant(cgroup);
3204 return next != cgroup ? next : NULL;
3205 }
3206
3207 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003208 next = cgroup_next_sibling(pos);
3209 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003210 return cgroup_leftmost_descendant(next);
3211
3212 /* no sibling left, visit parent */
3213 next = pos->parent;
3214 return next != cgroup ? next : NULL;
3215}
3216EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3217
Paul Menagebd89aab2007-10-18 23:40:44 -07003218void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003219 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003220{
3221 /*
3222 * The first time anyone tries to iterate across a cgroup,
3223 * we need to enable the list linking each css_set to its
3224 * tasks, and fix up all existing tasks.
3225 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003226 if (!use_task_css_set_links)
3227 cgroup_enable_task_cg_lists();
3228
Paul Menage817929e2007-10-18 23:39:36 -07003229 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003230 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003231 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003232}
3233
Paul Menagebd89aab2007-10-18 23:40:44 -07003234struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003235 struct cgroup_iter *it)
3236{
3237 struct task_struct *res;
3238 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003239 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003240
3241 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003242 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003243 return NULL;
3244 res = list_entry(l, struct task_struct, cg_list);
3245 /* Advance iterator to find next entry */
3246 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003247 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3248 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003249 /* We reached the end of this task list - move on to
3250 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003251 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003252 } else {
3253 it->task = l;
3254 }
3255 return res;
3256}
3257
Paul Menagebd89aab2007-10-18 23:40:44 -07003258void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003259 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003260{
3261 read_unlock(&css_set_lock);
3262}
3263
Cliff Wickman31a7df02008-02-07 00:14:42 -08003264static inline int started_after_time(struct task_struct *t1,
3265 struct timespec *time,
3266 struct task_struct *t2)
3267{
3268 int start_diff = timespec_compare(&t1->start_time, time);
3269 if (start_diff > 0) {
3270 return 1;
3271 } else if (start_diff < 0) {
3272 return 0;
3273 } else {
3274 /*
3275 * Arbitrarily, if two processes started at the same
3276 * time, we'll say that the lower pointer value
3277 * started first. Note that t2 may have exited by now
3278 * so this may not be a valid pointer any longer, but
3279 * that's fine - it still serves to distinguish
3280 * between two tasks started (effectively) simultaneously.
3281 */
3282 return t1 > t2;
3283 }
3284}
3285
3286/*
3287 * This function is a callback from heap_insert() and is used to order
3288 * the heap.
3289 * In this case we order the heap in descending task start time.
3290 */
3291static inline int started_after(void *p1, void *p2)
3292{
3293 struct task_struct *t1 = p1;
3294 struct task_struct *t2 = p2;
3295 return started_after_time(t1, &t2->start_time, t2);
3296}
3297
3298/**
3299 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3300 * @scan: struct cgroup_scanner containing arguments for the scan
3301 *
3302 * Arguments include pointers to callback functions test_task() and
3303 * process_task().
3304 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3305 * and if it returns true, call process_task() for it also.
3306 * The test_task pointer may be NULL, meaning always true (select all tasks).
3307 * Effectively duplicates cgroup_iter_{start,next,end}()
3308 * but does not lock css_set_lock for the call to process_task().
3309 * The struct cgroup_scanner may be embedded in any structure of the caller's
3310 * creation.
3311 * It is guaranteed that process_task() will act on every task that
3312 * is a member of the cgroup for the duration of this call. This
3313 * function may or may not call process_task() for tasks that exit
3314 * or move to a different cgroup during the call, or are forked or
3315 * move into the cgroup during the call.
3316 *
3317 * Note that test_task() may be called with locks held, and may in some
3318 * situations be called multiple times for the same task, so it should
3319 * be cheap.
3320 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3321 * pre-allocated and will be used for heap operations (and its "gt" member will
3322 * be overwritten), else a temporary heap will be used (allocation of which
3323 * may cause this function to fail).
3324 */
3325int cgroup_scan_tasks(struct cgroup_scanner *scan)
3326{
3327 int retval, i;
3328 struct cgroup_iter it;
3329 struct task_struct *p, *dropped;
3330 /* Never dereference latest_task, since it's not refcounted */
3331 struct task_struct *latest_task = NULL;
3332 struct ptr_heap tmp_heap;
3333 struct ptr_heap *heap;
3334 struct timespec latest_time = { 0, 0 };
3335
3336 if (scan->heap) {
3337 /* The caller supplied our heap and pre-allocated its memory */
3338 heap = scan->heap;
3339 heap->gt = &started_after;
3340 } else {
3341 /* We need to allocate our own heap memory */
3342 heap = &tmp_heap;
3343 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3344 if (retval)
3345 /* cannot allocate the heap */
3346 return retval;
3347 }
3348
3349 again:
3350 /*
3351 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3352 * to determine which are of interest, and using the scanner's
3353 * "process_task" callback to process any of them that need an update.
3354 * Since we don't want to hold any locks during the task updates,
3355 * gather tasks to be processed in a heap structure.
3356 * The heap is sorted by descending task start time.
3357 * If the statically-sized heap fills up, we overflow tasks that
3358 * started later, and in future iterations only consider tasks that
3359 * started after the latest task in the previous pass. This
3360 * guarantees forward progress and that we don't miss any tasks.
3361 */
3362 heap->size = 0;
3363 cgroup_iter_start(scan->cg, &it);
3364 while ((p = cgroup_iter_next(scan->cg, &it))) {
3365 /*
3366 * Only affect tasks that qualify per the caller's callback,
3367 * if he provided one
3368 */
3369 if (scan->test_task && !scan->test_task(p, scan))
3370 continue;
3371 /*
3372 * Only process tasks that started after the last task
3373 * we processed
3374 */
3375 if (!started_after_time(p, &latest_time, latest_task))
3376 continue;
3377 dropped = heap_insert(heap, p);
3378 if (dropped == NULL) {
3379 /*
3380 * The new task was inserted; the heap wasn't
3381 * previously full
3382 */
3383 get_task_struct(p);
3384 } else if (dropped != p) {
3385 /*
3386 * The new task was inserted, and pushed out a
3387 * different task
3388 */
3389 get_task_struct(p);
3390 put_task_struct(dropped);
3391 }
3392 /*
3393 * Else the new task was newer than anything already in
3394 * the heap and wasn't inserted
3395 */
3396 }
3397 cgroup_iter_end(scan->cg, &it);
3398
3399 if (heap->size) {
3400 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003401 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003402 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003403 latest_time = q->start_time;
3404 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003405 }
3406 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003407 scan->process_task(q, scan);
3408 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003409 }
3410 /*
3411 * If we had to process any tasks at all, scan again
3412 * in case some of them were in the middle of forking
3413 * children that didn't get processed.
3414 * Not the most efficient way to do it, but it avoids
3415 * having to take callback_mutex in the fork path
3416 */
3417 goto again;
3418 }
3419 if (heap == &tmp_heap)
3420 heap_free(&tmp_heap);
3421 return 0;
3422}
3423
Tejun Heo8cc99342013-04-07 09:29:50 -07003424static void cgroup_transfer_one_task(struct task_struct *task,
3425 struct cgroup_scanner *scan)
3426{
3427 struct cgroup *new_cgroup = scan->data;
3428
Tejun Heo47cfcd02013-04-07 09:29:51 -07003429 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003430 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003431 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003432}
3433
3434/**
3435 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3436 * @to: cgroup to which the tasks will be moved
3437 * @from: cgroup in which the tasks currently reside
3438 */
3439int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3440{
3441 struct cgroup_scanner scan;
3442
3443 scan.cg = from;
3444 scan.test_task = NULL; /* select all tasks in cgroup */
3445 scan.process_task = cgroup_transfer_one_task;
3446 scan.heap = NULL;
3447 scan.data = to;
3448
3449 return cgroup_scan_tasks(&scan);
3450}
3451
Paul Menage817929e2007-10-18 23:39:36 -07003452/*
Ben Blum102a7752009-09-23 15:56:26 -07003453 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003454 *
3455 * Reading this file can return large amounts of data if a cgroup has
3456 * *lots* of attached tasks. So it may need several calls to read(),
3457 * but we cannot guarantee that the information we produce is correct
3458 * unless we produce it entirely atomically.
3459 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003460 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003461
Li Zefan24528252012-01-20 11:58:43 +08003462/* which pidlist file are we talking about? */
3463enum cgroup_filetype {
3464 CGROUP_FILE_PROCS,
3465 CGROUP_FILE_TASKS,
3466};
3467
3468/*
3469 * A pidlist is a list of pids that virtually represents the contents of one
3470 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3471 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3472 * to the cgroup.
3473 */
3474struct cgroup_pidlist {
3475 /*
3476 * used to find which pidlist is wanted. doesn't change as long as
3477 * this particular list stays in the list.
3478 */
3479 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3480 /* array of xids */
3481 pid_t *list;
3482 /* how many elements the above list has */
3483 int length;
3484 /* how many files are using the current array */
3485 int use_count;
3486 /* each of these stored in a list by its cgroup */
3487 struct list_head links;
3488 /* pointer to the cgroup we belong to, for list removal purposes */
3489 struct cgroup *owner;
3490 /* protects the other fields */
3491 struct rw_semaphore mutex;
3492};
3493
Paul Menagebbcb81d2007-10-18 23:39:32 -07003494/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003495 * The following two functions "fix" the issue where there are more pids
3496 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3497 * TODO: replace with a kernel-wide solution to this problem
3498 */
3499#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3500static void *pidlist_allocate(int count)
3501{
3502 if (PIDLIST_TOO_LARGE(count))
3503 return vmalloc(count * sizeof(pid_t));
3504 else
3505 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3506}
3507static void pidlist_free(void *p)
3508{
3509 if (is_vmalloc_addr(p))
3510 vfree(p);
3511 else
3512 kfree(p);
3513}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003514
3515/*
Ben Blum102a7752009-09-23 15:56:26 -07003516 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003517 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003518 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003519static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003520{
Ben Blum102a7752009-09-23 15:56:26 -07003521 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003522
3523 /*
3524 * we presume the 0th element is unique, so i starts at 1. trivial
3525 * edge cases first; no work needs to be done for either
3526 */
3527 if (length == 0 || length == 1)
3528 return length;
3529 /* src and dest walk down the list; dest counts unique elements */
3530 for (src = 1; src < length; src++) {
3531 /* find next unique element */
3532 while (list[src] == list[src-1]) {
3533 src++;
3534 if (src == length)
3535 goto after;
3536 }
3537 /* dest always points to where the next unique element goes */
3538 list[dest] = list[src];
3539 dest++;
3540 }
3541after:
Ben Blum102a7752009-09-23 15:56:26 -07003542 return dest;
3543}
3544
3545static int cmppid(const void *a, const void *b)
3546{
3547 return *(pid_t *)a - *(pid_t *)b;
3548}
3549
3550/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003551 * find the appropriate pidlist for our purpose (given procs vs tasks)
3552 * returns with the lock on that pidlist already held, and takes care
3553 * of the use count, or returns NULL with no locks held if we're out of
3554 * memory.
3555 */
3556static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3557 enum cgroup_filetype type)
3558{
3559 struct cgroup_pidlist *l;
3560 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003561 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003562
Ben Blum72a8cb32009-09-23 15:56:27 -07003563 /*
3564 * We can't drop the pidlist_mutex before taking the l->mutex in case
3565 * the last ref-holder is trying to remove l from the list at the same
3566 * time. Holding the pidlist_mutex precludes somebody taking whichever
3567 * list we find out from under us - compare release_pid_array().
3568 */
3569 mutex_lock(&cgrp->pidlist_mutex);
3570 list_for_each_entry(l, &cgrp->pidlists, links) {
3571 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003572 /* make sure l doesn't vanish out from under us */
3573 down_write(&l->mutex);
3574 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003575 return l;
3576 }
3577 }
3578 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003579 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003580 if (!l) {
3581 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 return l;
3583 }
3584 init_rwsem(&l->mutex);
3585 down_write(&l->mutex);
3586 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003587 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003588 l->owner = cgrp;
3589 list_add(&l->links, &cgrp->pidlists);
3590 mutex_unlock(&cgrp->pidlist_mutex);
3591 return l;
3592}
3593
3594/*
Ben Blum102a7752009-09-23 15:56:26 -07003595 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3596 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003597static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3598 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003599{
3600 pid_t *array;
3601 int length;
3602 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003603 struct cgroup_iter it;
3604 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003605 struct cgroup_pidlist *l;
3606
3607 /*
3608 * If cgroup gets more users after we read count, we won't have
3609 * enough space - tough. This race is indistinguishable to the
3610 * caller from the case that the additional cgroup users didn't
3611 * show up until sometime later on.
3612 */
3613 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003614 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003615 if (!array)
3616 return -ENOMEM;
3617 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003618 cgroup_iter_start(cgrp, &it);
3619 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003620 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003621 break;
Ben Blum102a7752009-09-23 15:56:26 -07003622 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003623 if (type == CGROUP_FILE_PROCS)
3624 pid = task_tgid_vnr(tsk);
3625 else
3626 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003627 if (pid > 0) /* make sure to only use valid results */
3628 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003629 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003630 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003631 length = n;
3632 /* now sort & (if procs) strip out duplicates */
3633 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003635 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 l = cgroup_pidlist_find(cgrp, type);
3637 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003638 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003639 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003640 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003641 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003642 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003643 l->list = array;
3644 l->length = length;
3645 l->use_count++;
3646 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003647 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003648 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003649}
3650
Balbir Singh846c7bb2007-10-18 23:39:44 -07003651/**
Li Zefana043e3b2008-02-23 15:24:09 -08003652 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653 * @stats: cgroupstats to fill information into
3654 * @dentry: A dentry entry belonging to the cgroup for which stats have
3655 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003656 *
3657 * Build and fill cgroupstats so that taskstats can export it to user
3658 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003659 */
3660int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3661{
3662 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003663 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003664 struct cgroup_iter it;
3665 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003666
Balbir Singh846c7bb2007-10-18 23:39:44 -07003667 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003668 * Validate dentry by checking the superblock operations,
3669 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003670 */
Li Zefan33d283b2008-11-19 15:36:48 -08003671 if (dentry->d_sb->s_op != &cgroup_ops ||
3672 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003673 goto err;
3674
3675 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003676 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003677
Paul Menagebd89aab2007-10-18 23:40:44 -07003678 cgroup_iter_start(cgrp, &it);
3679 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680 switch (tsk->state) {
3681 case TASK_RUNNING:
3682 stats->nr_running++;
3683 break;
3684 case TASK_INTERRUPTIBLE:
3685 stats->nr_sleeping++;
3686 break;
3687 case TASK_UNINTERRUPTIBLE:
3688 stats->nr_uninterruptible++;
3689 break;
3690 case TASK_STOPPED:
3691 stats->nr_stopped++;
3692 break;
3693 default:
3694 if (delayacct_is_task_waiting_on_io(tsk))
3695 stats->nr_io_wait++;
3696 break;
3697 }
3698 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003699 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003700
Balbir Singh846c7bb2007-10-18 23:39:44 -07003701err:
3702 return ret;
3703}
3704
Paul Menage8f3ff202009-09-23 15:56:25 -07003705
Paul Menagecc31edc2008-10-18 20:28:04 -07003706/*
Ben Blum102a7752009-09-23 15:56:26 -07003707 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003708 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003709 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 */
3711
Ben Blum102a7752009-09-23 15:56:26 -07003712static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003713{
3714 /*
3715 * Initially we receive a position value that corresponds to
3716 * one more than the last pid shown (or 0 on the first call or
3717 * after a seek to the start). Use a binary-search to find the
3718 * next pid to display, if any
3719 */
Ben Blum102a7752009-09-23 15:56:26 -07003720 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003721 int index = 0, pid = *pos;
3722 int *iter;
3723
Ben Blum102a7752009-09-23 15:56:26 -07003724 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003726 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003727
Paul Menagecc31edc2008-10-18 20:28:04 -07003728 while (index < end) {
3729 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003730 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003731 index = mid;
3732 break;
Ben Blum102a7752009-09-23 15:56:26 -07003733 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003734 index = mid + 1;
3735 else
3736 end = mid;
3737 }
3738 }
3739 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003740 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 return NULL;
3742 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003743 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 *pos = *iter;
3745 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003746}
3747
Ben Blum102a7752009-09-23 15:56:26 -07003748static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003749{
Ben Blum102a7752009-09-23 15:56:26 -07003750 struct cgroup_pidlist *l = s->private;
3751 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003752}
3753
Ben Blum102a7752009-09-23 15:56:26 -07003754static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003755{
Ben Blum102a7752009-09-23 15:56:26 -07003756 struct cgroup_pidlist *l = s->private;
3757 pid_t *p = v;
3758 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003759 /*
3760 * Advance to the next pid in the array. If this goes off the
3761 * end, we're done
3762 */
3763 p++;
3764 if (p >= end) {
3765 return NULL;
3766 } else {
3767 *pos = *p;
3768 return p;
3769 }
3770}
3771
Ben Blum102a7752009-09-23 15:56:26 -07003772static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003773{
3774 return seq_printf(s, "%d\n", *(int *)v);
3775}
3776
Ben Blum102a7752009-09-23 15:56:26 -07003777/*
3778 * seq_operations functions for iterating on pidlists through seq_file -
3779 * independent of whether it's tasks or procs
3780 */
3781static const struct seq_operations cgroup_pidlist_seq_operations = {
3782 .start = cgroup_pidlist_start,
3783 .stop = cgroup_pidlist_stop,
3784 .next = cgroup_pidlist_next,
3785 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003786};
3787
Ben Blum102a7752009-09-23 15:56:26 -07003788static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003789{
Ben Blum72a8cb32009-09-23 15:56:27 -07003790 /*
3791 * the case where we're the last user of this particular pidlist will
3792 * have us remove it from the cgroup's list, which entails taking the
3793 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3794 * pidlist_mutex, we have to take pidlist_mutex first.
3795 */
3796 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003797 down_write(&l->mutex);
3798 BUG_ON(!l->use_count);
3799 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003800 /* we're the last user if refcount is 0; remove and free */
3801 list_del(&l->links);
3802 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003803 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003804 put_pid_ns(l->key.ns);
3805 up_write(&l->mutex);
3806 kfree(l);
3807 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003809 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003810 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003811}
3812
Ben Blum102a7752009-09-23 15:56:26 -07003813static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003814{
Ben Blum102a7752009-09-23 15:56:26 -07003815 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816 if (!(file->f_mode & FMODE_READ))
3817 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003818 /*
3819 * the seq_file will only be initialized if the file was opened for
3820 * reading; hence we check if it's not null only in that case.
3821 */
3822 l = ((struct seq_file *)file->private_data)->private;
3823 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003824 return seq_release(inode, file);
3825}
3826
Ben Blum102a7752009-09-23 15:56:26 -07003827static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003828 .read = seq_read,
3829 .llseek = seq_lseek,
3830 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003831 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003832};
3833
3834/*
Ben Blum102a7752009-09-23 15:56:26 -07003835 * The following functions handle opens on a file that displays a pidlist
3836 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3837 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 */
Ben Blum102a7752009-09-23 15:56:26 -07003839/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003840static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003841{
3842 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003843 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003844 int retval;
3845
3846 /* Nothing to do for write-only files */
3847 if (!(file->f_mode & FMODE_READ))
3848 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003849
Ben Blum102a7752009-09-23 15:56:26 -07003850 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003851 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003852 if (retval)
3853 return retval;
3854 /* configure file information */
3855 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003856
Ben Blum102a7752009-09-23 15:56:26 -07003857 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003858 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003859 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003860 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003861 }
Ben Blum102a7752009-09-23 15:56:26 -07003862 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003863 return 0;
3864}
Ben Blum102a7752009-09-23 15:56:26 -07003865static int cgroup_tasks_open(struct inode *unused, struct file *file)
3866{
Ben Blum72a8cb32009-09-23 15:56:27 -07003867 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003868}
3869static int cgroup_procs_open(struct inode *unused, struct file *file)
3870{
Ben Blum72a8cb32009-09-23 15:56:27 -07003871 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003872}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003873
Paul Menagebd89aab2007-10-18 23:40:44 -07003874static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003875 struct cftype *cft)
3876{
Paul Menagebd89aab2007-10-18 23:40:44 -07003877 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003878}
3879
Paul Menage6379c102008-07-25 01:47:01 -07003880static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3881 struct cftype *cft,
3882 u64 val)
3883{
3884 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3885 if (val)
3886 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3887 else
3888 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3889 return 0;
3890}
3891
Paul Menagebbcb81d2007-10-18 23:39:32 -07003892/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003893 * When dput() is called asynchronously, if umount has been done and
3894 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3895 * there's a small window that vfs will see the root dentry with non-zero
3896 * refcnt and trigger BUG().
3897 *
3898 * That's why we hold a reference before dput() and drop it right after.
3899 */
3900static void cgroup_dput(struct cgroup *cgrp)
3901{
3902 struct super_block *sb = cgrp->root->sb;
3903
3904 atomic_inc(&sb->s_active);
3905 dput(cgrp->dentry);
3906 deactivate_super(sb);
3907}
3908
3909/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003910 * Unregister event and free resources.
3911 *
3912 * Gets called from workqueue.
3913 */
3914static void cgroup_event_remove(struct work_struct *work)
3915{
3916 struct cgroup_event *event = container_of(work, struct cgroup_event,
3917 remove);
3918 struct cgroup *cgrp = event->cgrp;
3919
Li Zefan810cbee2013-02-18 18:56:14 +08003920 remove_wait_queue(event->wqh, &event->wait);
3921
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003922 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3923
Li Zefan810cbee2013-02-18 18:56:14 +08003924 /* Notify userspace the event is going away. */
3925 eventfd_signal(event->eventfd, 1);
3926
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003927 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003928 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003929 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003930}
3931
3932/*
3933 * Gets called on POLLHUP on eventfd when user closes it.
3934 *
3935 * Called with wqh->lock held and interrupts disabled.
3936 */
3937static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3938 int sync, void *key)
3939{
3940 struct cgroup_event *event = container_of(wait,
3941 struct cgroup_event, wait);
3942 struct cgroup *cgrp = event->cgrp;
3943 unsigned long flags = (unsigned long)key;
3944
3945 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003946 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003947 * If the event has been detached at cgroup removal, we
3948 * can simply return knowing the other side will cleanup
3949 * for us.
3950 *
3951 * We can't race against event freeing since the other
3952 * side will require wqh->lock via remove_wait_queue(),
3953 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003954 */
Li Zefan810cbee2013-02-18 18:56:14 +08003955 spin_lock(&cgrp->event_list_lock);
3956 if (!list_empty(&event->list)) {
3957 list_del_init(&event->list);
3958 /*
3959 * We are in atomic context, but cgroup_event_remove()
3960 * may sleep, so we have to call it in workqueue.
3961 */
3962 schedule_work(&event->remove);
3963 }
3964 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003965 }
3966
3967 return 0;
3968}
3969
3970static void cgroup_event_ptable_queue_proc(struct file *file,
3971 wait_queue_head_t *wqh, poll_table *pt)
3972{
3973 struct cgroup_event *event = container_of(pt,
3974 struct cgroup_event, pt);
3975
3976 event->wqh = wqh;
3977 add_wait_queue(wqh, &event->wait);
3978}
3979
3980/*
3981 * Parse input and register new cgroup event handler.
3982 *
3983 * Input must be in format '<event_fd> <control_fd> <args>'.
3984 * Interpretation of args is defined by control file implementation.
3985 */
3986static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3987 const char *buffer)
3988{
3989 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003990 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003991 unsigned int efd, cfd;
3992 struct file *efile = NULL;
3993 struct file *cfile = NULL;
3994 char *endp;
3995 int ret;
3996
3997 efd = simple_strtoul(buffer, &endp, 10);
3998 if (*endp != ' ')
3999 return -EINVAL;
4000 buffer = endp + 1;
4001
4002 cfd = simple_strtoul(buffer, &endp, 10);
4003 if ((*endp != ' ') && (*endp != '\0'))
4004 return -EINVAL;
4005 buffer = endp + 1;
4006
4007 event = kzalloc(sizeof(*event), GFP_KERNEL);
4008 if (!event)
4009 return -ENOMEM;
4010 event->cgrp = cgrp;
4011 INIT_LIST_HEAD(&event->list);
4012 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4013 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4014 INIT_WORK(&event->remove, cgroup_event_remove);
4015
4016 efile = eventfd_fget(efd);
4017 if (IS_ERR(efile)) {
4018 ret = PTR_ERR(efile);
4019 goto fail;
4020 }
4021
4022 event->eventfd = eventfd_ctx_fileget(efile);
4023 if (IS_ERR(event->eventfd)) {
4024 ret = PTR_ERR(event->eventfd);
4025 goto fail;
4026 }
4027
4028 cfile = fget(cfd);
4029 if (!cfile) {
4030 ret = -EBADF;
4031 goto fail;
4032 }
4033
4034 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004035 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004036 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037 if (ret < 0)
4038 goto fail;
4039
4040 event->cft = __file_cft(cfile);
4041 if (IS_ERR(event->cft)) {
4042 ret = PTR_ERR(event->cft);
4043 goto fail;
4044 }
4045
Li Zefanf1690072013-02-18 14:13:35 +08004046 /*
4047 * The file to be monitored must be in the same cgroup as
4048 * cgroup.event_control is.
4049 */
4050 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4051 if (cgrp_cfile != cgrp) {
4052 ret = -EINVAL;
4053 goto fail;
4054 }
4055
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004056 if (!event->cft->register_event || !event->cft->unregister_event) {
4057 ret = -EINVAL;
4058 goto fail;
4059 }
4060
4061 ret = event->cft->register_event(cgrp, event->cft,
4062 event->eventfd, buffer);
4063 if (ret)
4064 goto fail;
4065
Li Zefan7ef70e42013-04-26 11:58:03 -07004066 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004067
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004068 /*
4069 * Events should be removed after rmdir of cgroup directory, but before
4070 * destroying subsystem state objects. Let's take reference to cgroup
4071 * directory dentry to do that.
4072 */
4073 dget(cgrp->dentry);
4074
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004075 spin_lock(&cgrp->event_list_lock);
4076 list_add(&event->list, &cgrp->event_list);
4077 spin_unlock(&cgrp->event_list_lock);
4078
4079 fput(cfile);
4080 fput(efile);
4081
4082 return 0;
4083
4084fail:
4085 if (cfile)
4086 fput(cfile);
4087
4088 if (event && event->eventfd && !IS_ERR(event->eventfd))
4089 eventfd_ctx_put(event->eventfd);
4090
4091 if (!IS_ERR_OR_NULL(efile))
4092 fput(efile);
4093
4094 kfree(event);
4095
4096 return ret;
4097}
4098
Daniel Lezcano97978e62010-10-27 15:33:35 -07004099static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4100 struct cftype *cft)
4101{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004102 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004103}
4104
4105static int cgroup_clone_children_write(struct cgroup *cgrp,
4106 struct cftype *cft,
4107 u64 val)
4108{
4109 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004110 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004111 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004112 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004113 return 0;
4114}
4115
Tejun Heod5c56ce2013-06-03 19:14:34 -07004116static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004117 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004118 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004119 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004120 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004121 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004122 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004123 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004124 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004125 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004126 .write_string = cgroup_write_event_control,
4127 .mode = S_IWUGO,
4128 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004129 {
4130 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004131 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004132 .read_u64 = cgroup_clone_children_read,
4133 .write_u64 = cgroup_clone_children_write,
4134 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004135 {
Tejun Heo873fe092013-04-14 20:15:26 -07004136 .name = "cgroup.sane_behavior",
4137 .flags = CFTYPE_ONLY_ON_ROOT,
4138 .read_seq_string = cgroup_sane_behavior_show,
4139 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004140
4141 /*
4142 * Historical crazy stuff. These don't have "cgroup." prefix and
4143 * don't exist if sane_behavior. If you're depending on these, be
4144 * prepared to be burned.
4145 */
4146 {
4147 .name = "tasks",
4148 .flags = CFTYPE_INSANE, /* use "procs" instead */
4149 .open = cgroup_tasks_open,
4150 .write_u64 = cgroup_tasks_write,
4151 .release = cgroup_pidlist_release,
4152 .mode = S_IRUGO | S_IWUSR,
4153 },
4154 {
4155 .name = "notify_on_release",
4156 .flags = CFTYPE_INSANE,
4157 .read_u64 = cgroup_read_notify_on_release,
4158 .write_u64 = cgroup_write_notify_on_release,
4159 },
Tejun Heo873fe092013-04-14 20:15:26 -07004160 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004161 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004162 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004163 .read_seq_string = cgroup_release_agent_show,
4164 .write_string = cgroup_release_agent_write,
4165 .max_write_len = PATH_MAX,
4166 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004167 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004168};
4169
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004170/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004171 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004172 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004173 * @subsys_mask: mask of the subsystem ids whose files should be added
4174 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004175static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177 struct cgroup_subsys *ss;
4178
Tejun Heo8e3f6542012-04-01 12:09:55 -07004179 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004180 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004181 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004182 if (!test_bit(ss->subsys_id, &subsys_mask))
4183 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004184
Tejun Heodb0416b2012-04-01 12:09:55 -07004185 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004186 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004188
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004189 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004190 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004191 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004192 struct css_id *id = rcu_dereference_protected(css->id, true);
4193
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004194 /*
4195 * Update id->css pointer and make this css visible from
4196 * CSS ID functions. This pointer will be dereferened
4197 * from RCU-read-side without locks.
4198 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004199 if (id)
4200 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004201 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202
4203 return 0;
4204}
4205
Tejun Heo48ddbe12012-04-01 12:09:56 -07004206static void css_dput_fn(struct work_struct *work)
4207{
4208 struct cgroup_subsys_state *css =
4209 container_of(work, struct cgroup_subsys_state, dput_work);
4210
Li Zefan1c8158e2013-06-18 18:41:10 +08004211 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004212}
4213
Tejun Heod3daf282013-06-13 19:39:16 -07004214static void css_release(struct percpu_ref *ref)
4215{
4216 struct cgroup_subsys_state *css =
4217 container_of(ref, struct cgroup_subsys_state, refcnt);
4218
4219 schedule_work(&css->dput_work);
4220}
4221
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222static void init_cgroup_css(struct cgroup_subsys_state *css,
4223 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225{
Paul Menagebd89aab2007-10-18 23:40:44 -07004226 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004228 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004229 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004230 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004231 BUG_ON(cgrp->subsys[ss->subsys_id]);
4232 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004233
4234 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004235 * css holds an extra ref to @cgrp->dentry which is put on the last
4236 * css_put(). dput() requires process context, which css_put() may
4237 * be called without. @css->dput_work will be used to invoke
4238 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004239 */
4240 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241}
4242
Tejun Heob1929db2012-11-19 08:13:38 -08004243/* invoke ->post_create() on a new CSS and mark it online if successful */
4244static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004245{
Tejun Heob1929db2012-11-19 08:13:38 -08004246 int ret = 0;
4247
Tejun Heoa31f2d32012-11-19 08:13:37 -08004248 lockdep_assert_held(&cgroup_mutex);
4249
Tejun Heo92fb9742012-11-19 08:13:38 -08004250 if (ss->css_online)
4251 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004252 if (!ret)
4253 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4254 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004255}
4256
4257/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4258static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4259 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4260{
4261 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4262
4263 lockdep_assert_held(&cgroup_mutex);
4264
4265 if (!(css->flags & CSS_ONLINE))
4266 return;
4267
Li Zefand7eeac12013-03-12 15:35:59 -07004268 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004269 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004270
4271 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4272}
4273
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274/*
Li Zefana043e3b2008-02-23 15:24:09 -08004275 * cgroup_create - create a cgroup
4276 * @parent: cgroup that will be parent of the new cgroup
4277 * @dentry: dentry of the new cgroup
4278 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 *
Li Zefana043e3b2008-02-23 15:24:09 -08004280 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004283 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284{
Paul Menagebd89aab2007-10-18 23:40:44 -07004285 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004286 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287 struct cgroupfs_root *root = parent->root;
4288 int err = 0;
4289 struct cgroup_subsys *ss;
4290 struct super_block *sb = root->sb;
4291
Tejun Heo0a950f62012-11-19 09:02:12 -08004292 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004293 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4294 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004295 return -ENOMEM;
4296
Li Zefan65dff752013-03-01 15:01:56 +08004297 name = cgroup_alloc_name(dentry);
4298 if (!name)
4299 goto err_free_cgrp;
4300 rcu_assign_pointer(cgrp->name, name);
4301
Tejun Heo0a950f62012-11-19 09:02:12 -08004302 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4303 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004304 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004305
Tejun Heo976c06b2012-11-05 09:16:59 -08004306 /*
4307 * Only live parents can have children. Note that the liveliness
4308 * check isn't strictly necessary because cgroup_mkdir() and
4309 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4310 * anyway so that locking is contained inside cgroup proper and we
4311 * don't get nasty surprises if we ever grow another caller.
4312 */
4313 if (!cgroup_lock_live_group(parent)) {
4314 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004315 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004316 }
4317
Paul Menageddbcc7e2007-10-18 23:39:30 -07004318 /* Grab a reference on the superblock so the hierarchy doesn't
4319 * get deleted on unmount if there are child cgroups. This
4320 * can be done outside cgroup_mutex, since the sb can't
4321 * disappear while someone has an open control file on the
4322 * fs */
4323 atomic_inc(&sb->s_active);
4324
Paul Menagecc31edc2008-10-18 20:28:04 -07004325 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326
Li Zefanfe1c06c2013-01-24 14:30:22 +08004327 dentry->d_fsdata = cgrp;
4328 cgrp->dentry = dentry;
4329
Paul Menagebd89aab2007-10-18 23:40:44 -07004330 cgrp->parent = parent;
4331 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332
Li Zefanb6abdb02008-03-04 14:28:19 -08004333 if (notify_on_release(parent))
4334 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4335
Tejun Heo2260e7f2012-11-19 08:13:38 -08004336 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4337 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004338
Tejun Heo5549c492013-06-24 15:21:48 -07004339 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004340 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004341
Tejun Heo92fb9742012-11-19 08:13:38 -08004342 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343 if (IS_ERR(css)) {
4344 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004345 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004346 }
Tejun Heod3daf282013-06-13 19:39:16 -07004347
4348 err = percpu_ref_init(&css->refcnt, css_release);
4349 if (err)
4350 goto err_free_all;
4351
Paul Menagebd89aab2007-10-18 23:40:44 -07004352 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004353
Li Zefan4528fd02010-02-02 13:44:10 -08004354 if (ss->use_id) {
4355 err = alloc_css_id(ss, parent, cgrp);
4356 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004357 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004358 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359 }
4360
Tejun Heo4e139af2012-11-19 08:13:36 -08004361 /*
4362 * Create directory. cgroup_create_file() returns with the new
4363 * directory locked on success so that it can be populated without
4364 * dropping cgroup_mutex.
4365 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004366 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004367 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004368 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004369 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
Tejun Heo00356bd2013-06-18 11:14:22 -07004371 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004372
Tejun Heo4e139af2012-11-19 08:13:36 -08004373 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004374 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4375 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004376
Tejun Heob1929db2012-11-19 08:13:38 -08004377 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004378 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004379 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004380
Li Zefan415cf072013-04-08 14:35:02 +08004381 /* hold a ref to the parent's dentry */
4382 dget(parent->dentry);
4383
Tejun Heob1929db2012-11-19 08:13:38 -08004384 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004385 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004386 err = online_css(ss, cgrp);
4387 if (err)
4388 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004389
4390 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4391 parent->parent) {
4392 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",
4393 current->comm, current->pid, ss->name);
4394 if (!strcmp(ss->name, "memory"))
4395 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4396 ss->warned_broken_hierarchy = true;
4397 }
Tejun Heoa8638032012-11-09 09:12:29 -08004398 }
4399
Tejun Heo628f7cd2013-06-28 16:24:11 -07004400 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4401 if (err)
4402 goto err_destroy;
4403
4404 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004405 if (err)
4406 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407
4408 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004409 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004410
4411 return 0;
4412
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004413err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004414 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004415 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4416
4417 if (css) {
4418 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004419 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004420 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004421 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423 /* Release the reference count that we took on the superblock */
4424 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004425err_free_id:
4426 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004427err_free_name:
4428 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004429err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004430 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004431 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004432
4433err_destroy:
4434 cgroup_destroy_locked(cgrp);
4435 mutex_unlock(&cgroup_mutex);
4436 mutex_unlock(&dentry->d_inode->i_mutex);
4437 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004438}
4439
Al Viro18bb1db2011-07-26 01:41:39 -04004440static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004441{
4442 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4443
4444 /* the vfs holds inode->i_mutex already */
4445 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4446}
4447
Tejun Heod3daf282013-06-13 19:39:16 -07004448static void cgroup_css_killed(struct cgroup *cgrp)
4449{
4450 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4451 return;
4452
4453 /* percpu ref's of all css's are killed, kick off the next step */
4454 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4455 schedule_work(&cgrp->destroy_work);
4456}
4457
4458static void css_ref_killed_fn(struct percpu_ref *ref)
4459{
4460 struct cgroup_subsys_state *css =
4461 container_of(ref, struct cgroup_subsys_state, refcnt);
4462
4463 cgroup_css_killed(css->cgroup);
4464}
4465
4466/**
4467 * cgroup_destroy_locked - the first stage of cgroup destruction
4468 * @cgrp: cgroup to be destroyed
4469 *
4470 * css's make use of percpu refcnts whose killing latency shouldn't be
4471 * exposed to userland and are RCU protected. Also, cgroup core needs to
4472 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4473 * invoked. To satisfy all the requirements, destruction is implemented in
4474 * the following two steps.
4475 *
4476 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4477 * userland visible parts and start killing the percpu refcnts of
4478 * css's. Set up so that the next stage will be kicked off once all
4479 * the percpu refcnts are confirmed to be killed.
4480 *
4481 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4482 * rest of destruction. Once all cgroup references are gone, the
4483 * cgroup is RCU-freed.
4484 *
4485 * This function implements s1. After this step, @cgrp is gone as far as
4486 * the userland is concerned and a new cgroup with the same name may be
4487 * created. As cgroup doesn't care about the names internally, this
4488 * doesn't cause any problem.
4489 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004490static int cgroup_destroy_locked(struct cgroup *cgrp)
4491 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492{
Tejun Heo42809dd2012-11-19 08:13:37 -08004493 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004494 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004495 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004496 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004497
Tejun Heo42809dd2012-11-19 08:13:37 -08004498 lockdep_assert_held(&d->d_inode->i_mutex);
4499 lockdep_assert_held(&cgroup_mutex);
4500
Tejun Heoddd69142013-06-12 21:04:54 -07004501 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004502 * css_set_lock synchronizes access to ->cset_links and prevents
4503 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004504 */
4505 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004506 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004507 read_unlock(&css_set_lock);
4508 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004509 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004510
Tejun Heo1a90dd52012-11-05 09:16:59 -08004511 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004512 * Block new css_tryget() by killing css refcnts. cgroup core
4513 * guarantees that, by the time ->css_offline() is invoked, no new
4514 * css reference will be given out via css_tryget(). We can't
4515 * simply call percpu_ref_kill() and proceed to offlining css's
4516 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4517 * as killed on all CPUs on return.
4518 *
4519 * Use percpu_ref_kill_and_confirm() to get notifications as each
4520 * css is confirmed to be seen as killed on all CPUs. The
4521 * notification callback keeps track of the number of css's to be
4522 * killed and schedules cgroup_offline_fn() to perform the rest of
4523 * destruction once the percpu refs of all css's are confirmed to
4524 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004525 */
Tejun Heod3daf282013-06-13 19:39:16 -07004526 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004527 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004528 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4529
Tejun Heod3daf282013-06-13 19:39:16 -07004530 /*
4531 * Killing would put the base ref, but we need to keep it
4532 * alive until after ->css_offline.
4533 */
4534 percpu_ref_get(&css->refcnt);
4535
4536 atomic_inc(&cgrp->css_kill_cnt);
4537 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004538 }
Tejun Heod3daf282013-06-13 19:39:16 -07004539 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004540
4541 /*
4542 * Mark @cgrp dead. This prevents further task migration and child
4543 * creation by disabling cgroup_lock_live_group(). Note that
4544 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4545 * resume iteration after dropping RCU read lock. See
4546 * cgroup_next_sibling() for details.
4547 */
Tejun Heo54766d42013-06-12 21:04:53 -07004548 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004549
Tejun Heo455050d2013-06-13 19:27:41 -07004550 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4551 raw_spin_lock(&release_list_lock);
4552 if (!list_empty(&cgrp->release_list))
4553 list_del_init(&cgrp->release_list);
4554 raw_spin_unlock(&release_list_lock);
4555
4556 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004557 * Clear and remove @cgrp directory. The removal puts the base ref
4558 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004559 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004560 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
4561 cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004562 dget(d);
4563 cgroup_d_remove_dir(d);
4564
4565 /*
4566 * Unregister events and notify userspace.
4567 * Notify userspace about cgroup removing only after rmdir of cgroup
4568 * directory to avoid race between userspace and kernelspace.
4569 */
4570 spin_lock(&cgrp->event_list_lock);
4571 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4572 list_del_init(&event->list);
4573 schedule_work(&event->remove);
4574 }
4575 spin_unlock(&cgrp->event_list_lock);
4576
Tejun Heoea15f8c2013-06-13 19:27:42 -07004577 return 0;
4578};
4579
Tejun Heod3daf282013-06-13 19:39:16 -07004580/**
4581 * cgroup_offline_fn - the second step of cgroup destruction
4582 * @work: cgroup->destroy_free_work
4583 *
4584 * This function is invoked from a work item for a cgroup which is being
4585 * destroyed after the percpu refcnts of all css's are guaranteed to be
4586 * seen as killed on all CPUs, and performs the rest of destruction. This
4587 * is the second step of destruction described in the comment above
4588 * cgroup_destroy_locked().
4589 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004590static void cgroup_offline_fn(struct work_struct *work)
4591{
4592 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4593 struct cgroup *parent = cgrp->parent;
4594 struct dentry *d = cgrp->dentry;
4595 struct cgroup_subsys *ss;
4596
4597 mutex_lock(&cgroup_mutex);
4598
Tejun Heod3daf282013-06-13 19:39:16 -07004599 /*
4600 * css_tryget() is guaranteed to fail now. Tell subsystems to
4601 * initate destruction.
4602 */
Tejun Heo5549c492013-06-24 15:21:48 -07004603 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004604 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004605
4606 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004607 * Put the css refs from cgroup_destroy_locked(). Each css holds
4608 * an extra reference to the cgroup's dentry and cgroup removal
4609 * proceeds regardless of css refs. On the last put of each css,
4610 * whenever that may be, the extra dentry ref is put so that dentry
4611 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004612 */
Tejun Heo5549c492013-06-24 15:21:48 -07004613 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004614 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615
Paul Menage999cd8a2009-01-07 18:08:36 -08004616 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004617 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004618
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004620
Paul Menagebd89aab2007-10-18 23:40:44 -07004621 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004622 check_for_release(parent);
4623
Tejun Heoea15f8c2013-06-13 19:27:42 -07004624 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625}
4626
Tejun Heo42809dd2012-11-19 08:13:37 -08004627static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4628{
4629 int ret;
4630
4631 mutex_lock(&cgroup_mutex);
4632 ret = cgroup_destroy_locked(dentry->d_fsdata);
4633 mutex_unlock(&cgroup_mutex);
4634
4635 return ret;
4636}
4637
Tejun Heo8e3f6542012-04-01 12:09:55 -07004638static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4639{
4640 INIT_LIST_HEAD(&ss->cftsets);
4641
4642 /*
4643 * base_cftset is embedded in subsys itself, no need to worry about
4644 * deregistration.
4645 */
4646 if (ss->base_cftypes) {
4647 ss->base_cftset.cfts = ss->base_cftypes;
4648 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4649 }
4650}
4651
Li Zefan06a11922008-04-29 01:00:07 -07004652static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004654 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004655
4656 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004657
Tejun Heo648bb562012-11-19 08:13:36 -08004658 mutex_lock(&cgroup_mutex);
4659
Tejun Heo8e3f6542012-04-01 12:09:55 -07004660 /* init base cftset */
4661 cgroup_init_cftsets(ss);
4662
Paul Menageddbcc7e2007-10-18 23:39:30 -07004663 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004664 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4665 ss->root = &cgroup_dummy_root;
4666 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004667 /* We don't handle early failures gracefully */
4668 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004669 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004670
Li Zefane8d55fd2008-04-29 01:00:13 -07004671 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004672 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004673 * newly registered, all tasks and hence the
4674 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004675 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676
4677 need_forkexit_callback |= ss->fork || ss->exit;
4678
Li Zefane8d55fd2008-04-29 01:00:13 -07004679 /* At system boot, before all subsystems have been
4680 * registered, no tasks have been forked, so we don't
4681 * need to invoke fork callbacks here. */
4682 BUG_ON(!list_empty(&init_task.tasks));
4683
Tejun Heo9871bf92013-06-24 15:21:47 -07004684 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004685
Tejun Heo648bb562012-11-19 08:13:36 -08004686 mutex_unlock(&cgroup_mutex);
4687
Ben Blume6a11052010-03-10 15:22:09 -08004688 /* this function shouldn't be used with modular subsystems, since they
4689 * need to register a subsys_id, among other things */
4690 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004691}
4692
4693/**
Ben Blume6a11052010-03-10 15:22:09 -08004694 * cgroup_load_subsys: load and register a modular subsystem at runtime
4695 * @ss: the subsystem to load
4696 *
4697 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004698 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004699 * up for use. If the subsystem is built-in anyway, work is delegated to the
4700 * simpler cgroup_init_subsys.
4701 */
4702int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4703{
Ben Blume6a11052010-03-10 15:22:09 -08004704 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004705 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004706 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004707 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004708 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004709
4710 /* check name and function validity */
4711 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004712 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004713 return -EINVAL;
4714
4715 /*
4716 * we don't support callbacks in modular subsystems. this check is
4717 * before the ss->module check for consistency; a subsystem that could
4718 * be a module should still have no callbacks even if the user isn't
4719 * compiling it as one.
4720 */
4721 if (ss->fork || ss->exit)
4722 return -EINVAL;
4723
4724 /*
4725 * an optionally modular subsystem is built-in: we want to do nothing,
4726 * since cgroup_init_subsys will have already taken care of it.
4727 */
4728 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004729 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004730 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004731 return 0;
4732 }
4733
Tejun Heo8e3f6542012-04-01 12:09:55 -07004734 /* init base cftset */
4735 cgroup_init_cftsets(ss);
4736
Ben Blume6a11052010-03-10 15:22:09 -08004737 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004738 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004739
4740 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004741 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004742 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004743 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004744 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004745 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004746 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004747 /* failure case - need to deassign the cgroup_subsys[] slot. */
4748 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004749 mutex_unlock(&cgroup_mutex);
4750 return PTR_ERR(css);
4751 }
4752
Tejun Heo9871bf92013-06-24 15:21:47 -07004753 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4754 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004755
4756 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004757 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004758 /* init_idr must be after init_cgroup_css because it sets css->id. */
4759 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004760 ret = cgroup_init_idr(ss, css);
4761 if (ret)
4762 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004763 }
4764
4765 /*
4766 * Now we need to entangle the css into the existing css_sets. unlike
4767 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4768 * will need a new pointer to it; done by iterating the css_set_table.
4769 * furthermore, modifying the existing css_sets will corrupt the hash
4770 * table state, so each changed css_set will need its hash recomputed.
4771 * this is all done under the css_set_lock.
4772 */
4773 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004774 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004775 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004776 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004777 continue;
4778 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004779 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004780 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004781 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004782 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004783 key = css_set_hash(cset->subsys);
4784 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004785 }
4786 write_unlock(&css_set_lock);
4787
Tejun Heo9871bf92013-06-24 15:21:47 -07004788 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004789 if (ret)
4790 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004791
Ben Blume6a11052010-03-10 15:22:09 -08004792 /* success! */
4793 mutex_unlock(&cgroup_mutex);
4794 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004795
4796err_unload:
4797 mutex_unlock(&cgroup_mutex);
4798 /* @ss can't be mounted here as try_module_get() would fail */
4799 cgroup_unload_subsys(ss);
4800 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004801}
4802EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4803
4804/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004805 * cgroup_unload_subsys: unload a modular subsystem
4806 * @ss: the subsystem to unload
4807 *
4808 * This function should be called in a modular subsystem's exitcall. When this
4809 * function is invoked, the refcount on the subsystem's module will be 0, so
4810 * the subsystem will not be attached to any hierarchy.
4811 */
4812void cgroup_unload_subsys(struct cgroup_subsys *ss)
4813{
Tejun Heo69d02062013-06-12 21:04:50 -07004814 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004815
4816 BUG_ON(ss->module == NULL);
4817
4818 /*
4819 * we shouldn't be called if the subsystem is in use, and the use of
4820 * try_module_get in parse_cgroupfs_options should ensure that it
4821 * doesn't start being used while we're killing it off.
4822 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004823 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004824
4825 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004826
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004828
Tejun Heoc897ff62013-02-27 17:03:49 -08004829 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004830 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004831
Ben Blumcf5d5942010-03-10 15:22:09 -08004832 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004833 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004834
Tejun Heo9871bf92013-06-24 15:21:47 -07004835 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004836 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004837
4838 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004839 * disentangle the css from all css_sets attached to the dummy
4840 * top. as in loading, we need to pay our respects to the hashtable
4841 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004842 */
4843 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004844 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004845 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004846 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004847
Tejun Heo5abb8852013-06-12 21:04:49 -07004848 hash_del(&cset->hlist);
4849 cset->subsys[ss->subsys_id] = NULL;
4850 key = css_set_hash(cset->subsys);
4851 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004852 }
4853 write_unlock(&css_set_lock);
4854
4855 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004856 * remove subsystem's css from the cgroup_dummy_top and free it -
4857 * need to free before marking as null because ss->css_free needs
4858 * the cgrp->subsys pointer to find their state. note that this
4859 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004860 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004861 ss->css_free(cgroup_dummy_top);
4862 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004863
4864 mutex_unlock(&cgroup_mutex);
4865}
4866EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4867
4868/**
Li Zefana043e3b2008-02-23 15:24:09 -08004869 * cgroup_init_early - cgroup initialization at system boot
4870 *
4871 * Initialize cgroups at system boot, and initialize any
4872 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004873 */
4874int __init cgroup_init_early(void)
4875{
Tejun Heo30159ec2013-06-25 11:53:37 -07004876 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004877 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004878
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004879 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004880 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004881 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004882 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004883 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004884 init_cgroup_root(&cgroup_dummy_root);
4885 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004886 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004887
Tejun Heo69d02062013-06-12 21:04:50 -07004888 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004889 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4890 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004891 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004892
Tejun Heo30159ec2013-06-25 11:53:37 -07004893 /* at bootup time, we don't worry about modular subsystems */
4894 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004895 BUG_ON(!ss->name);
4896 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004897 BUG_ON(!ss->css_alloc);
4898 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004900 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004901 ss->name, ss->subsys_id);
4902 BUG();
4903 }
4904
4905 if (ss->early_init)
4906 cgroup_init_subsys(ss);
4907 }
4908 return 0;
4909}
4910
4911/**
Li Zefana043e3b2008-02-23 15:24:09 -08004912 * cgroup_init - cgroup initialization
4913 *
4914 * Register cgroup filesystem and /proc file, and initialize
4915 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004916 */
4917int __init cgroup_init(void)
4918{
Tejun Heo30159ec2013-06-25 11:53:37 -07004919 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004920 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004921 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004922
4923 err = bdi_init(&cgroup_backing_dev_info);
4924 if (err)
4925 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004926
Tejun Heo30159ec2013-06-25 11:53:37 -07004927 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004928 if (!ss->early_init)
4929 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004930 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004931 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004932 }
4933
Tejun Heofa3ca072013-04-14 11:36:56 -07004934 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004935 mutex_lock(&cgroup_mutex);
4936 mutex_lock(&cgroup_root_mutex);
4937
Tejun Heo82fe9b02013-06-25 11:53:37 -07004938 /* Add init_css_set to the hash table */
4939 key = css_set_hash(init_css_set.subsys);
4940 hash_add(css_set_table, &init_css_set.hlist, key);
4941
Tejun Heofc76df72013-06-25 11:53:37 -07004942 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004943
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004944 mutex_unlock(&cgroup_root_mutex);
4945 mutex_unlock(&cgroup_mutex);
4946
Greg KH676db4a2010-08-05 13:53:35 -07004947 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4948 if (!cgroup_kobj) {
4949 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004951 }
4952
4953 err = register_filesystem(&cgroup_fs_type);
4954 if (err < 0) {
4955 kobject_put(cgroup_kobj);
4956 goto out;
4957 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004958
Li Zefan46ae2202008-04-29 01:00:08 -07004959 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004960
Paul Menageddbcc7e2007-10-18 23:39:30 -07004961out:
Paul Menagea4243162007-10-18 23:39:35 -07004962 if (err)
4963 bdi_destroy(&cgroup_backing_dev_info);
4964
Paul Menageddbcc7e2007-10-18 23:39:30 -07004965 return err;
4966}
Paul Menageb4f48b62007-10-18 23:39:33 -07004967
Paul Menagea4243162007-10-18 23:39:35 -07004968/*
4969 * proc_cgroup_show()
4970 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4971 * - Used for /proc/<pid>/cgroup.
4972 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4973 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004974 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004975 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4976 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4977 * cgroup to top_cgroup.
4978 */
4979
4980/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004981int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004982{
4983 struct pid *pid;
4984 struct task_struct *tsk;
4985 char *buf;
4986 int retval;
4987 struct cgroupfs_root *root;
4988
4989 retval = -ENOMEM;
4990 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4991 if (!buf)
4992 goto out;
4993
4994 retval = -ESRCH;
4995 pid = m->private;
4996 tsk = get_pid_task(pid, PIDTYPE_PID);
4997 if (!tsk)
4998 goto out_free;
4999
5000 retval = 0;
5001
5002 mutex_lock(&cgroup_mutex);
5003
Li Zefane5f6a862009-01-07 18:07:41 -08005004 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005005 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005006 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005007 int count = 0;
5008
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005009 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005010 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005011 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005012 if (strlen(root->name))
5013 seq_printf(m, "%sname=%s", count ? "," : "",
5014 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005015 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005016 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005017 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005018 if (retval < 0)
5019 goto out_unlock;
5020 seq_puts(m, buf);
5021 seq_putc(m, '\n');
5022 }
5023
5024out_unlock:
5025 mutex_unlock(&cgroup_mutex);
5026 put_task_struct(tsk);
5027out_free:
5028 kfree(buf);
5029out:
5030 return retval;
5031}
5032
Paul Menagea4243162007-10-18 23:39:35 -07005033/* Display information about each subsystem and each hierarchy */
5034static int proc_cgroupstats_show(struct seq_file *m, void *v)
5035{
Tejun Heo30159ec2013-06-25 11:53:37 -07005036 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005037 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005038
Paul Menage8bab8dd2008-04-04 14:29:57 -07005039 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005040 /*
5041 * ideally we don't want subsystems moving around while we do this.
5042 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5043 * subsys/hierarchy state.
5044 */
Paul Menagea4243162007-10-18 23:39:35 -07005045 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005046
5047 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005048 seq_printf(m, "%s\t%d\t%d\t%d\n",
5049 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005050 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005051
Paul Menagea4243162007-10-18 23:39:35 -07005052 mutex_unlock(&cgroup_mutex);
5053 return 0;
5054}
5055
5056static int cgroupstats_open(struct inode *inode, struct file *file)
5057{
Al Viro9dce07f2008-03-29 03:07:28 +00005058 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005059}
5060
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005061static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005062 .open = cgroupstats_open,
5063 .read = seq_read,
5064 .llseek = seq_lseek,
5065 .release = single_release,
5066};
5067
Paul Menageb4f48b62007-10-18 23:39:33 -07005068/**
5069 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005070 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005071 *
5072 * Description: A task inherits its parent's cgroup at fork().
5073 *
5074 * A pointer to the shared css_set was automatically copied in
5075 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005076 * it was not made under the protection of RCU or cgroup_mutex, so
5077 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5078 * have already changed current->cgroups, allowing the previously
5079 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005080 *
5081 * At the point that cgroup_fork() is called, 'current' is the parent
5082 * task, and the passed argument 'child' points to the child task.
5083 */
5084void cgroup_fork(struct task_struct *child)
5085{
Tejun Heo9bb71302012-10-18 17:52:07 -07005086 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005087 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005088 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005089 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005090 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005091}
5092
5093/**
Li Zefana043e3b2008-02-23 15:24:09 -08005094 * cgroup_post_fork - called on a new task after adding it to the task list
5095 * @child: the task in question
5096 *
Tejun Heo5edee612012-10-16 15:03:14 -07005097 * Adds the task to the list running through its css_set if necessary and
5098 * call the subsystem fork() callbacks. Has to be after the task is
5099 * visible on the task list in case we race with the first call to
5100 * cgroup_iter_start() - to guarantee that the new task ends up on its
5101 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005102 */
Paul Menage817929e2007-10-18 23:39:36 -07005103void cgroup_post_fork(struct task_struct *child)
5104{
Tejun Heo30159ec2013-06-25 11:53:37 -07005105 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005106 int i;
5107
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005108 /*
5109 * use_task_css_set_links is set to 1 before we walk the tasklist
5110 * under the tasklist_lock and we read it here after we added the child
5111 * to the tasklist under the tasklist_lock as well. If the child wasn't
5112 * yet in the tasklist when we walked through it from
5113 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5114 * should be visible now due to the paired locking and barriers implied
5115 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5116 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5117 * lock on fork.
5118 */
Paul Menage817929e2007-10-18 23:39:36 -07005119 if (use_task_css_set_links) {
5120 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005121 task_lock(child);
5122 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005123 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005124 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005125 write_unlock(&css_set_lock);
5126 }
Tejun Heo5edee612012-10-16 15:03:14 -07005127
5128 /*
5129 * Call ss->fork(). This must happen after @child is linked on
5130 * css_set; otherwise, @child might change state between ->fork()
5131 * and addition to css_set.
5132 */
5133 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005134 /*
5135 * fork/exit callbacks are supported only for builtin
5136 * subsystems, and the builtin section of the subsys
5137 * array is immutable, so we don't need to lock the
5138 * subsys array here. On the other hand, modular section
5139 * of the array can be freed at module unload, so we
5140 * can't touch that.
5141 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005142 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005143 if (ss->fork)
5144 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005145 }
Paul Menage817929e2007-10-18 23:39:36 -07005146}
Tejun Heo5edee612012-10-16 15:03:14 -07005147
Paul Menage817929e2007-10-18 23:39:36 -07005148/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005149 * cgroup_exit - detach cgroup from exiting task
5150 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005151 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005152 *
5153 * Description: Detach cgroup from @tsk and release it.
5154 *
5155 * Note that cgroups marked notify_on_release force every task in
5156 * them to take the global cgroup_mutex mutex when exiting.
5157 * This could impact scaling on very large systems. Be reluctant to
5158 * use notify_on_release cgroups where very high task exit scaling
5159 * is required on large systems.
5160 *
5161 * the_top_cgroup_hack:
5162 *
5163 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5164 *
5165 * We call cgroup_exit() while the task is still competent to
5166 * handle notify_on_release(), then leave the task attached to the
5167 * root cgroup in each hierarchy for the remainder of its exit.
5168 *
5169 * To do this properly, we would increment the reference count on
5170 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5171 * code we would add a second cgroup function call, to drop that
5172 * reference. This would just create an unnecessary hot spot on
5173 * the top_cgroup reference count, to no avail.
5174 *
5175 * Normally, holding a reference to a cgroup without bumping its
5176 * count is unsafe. The cgroup could go away, or someone could
5177 * attach us to a different cgroup, decrementing the count on
5178 * the first cgroup that we never incremented. But in this case,
5179 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005180 * which wards off any cgroup_attach_task() attempts, or task is a failed
5181 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005182 */
5183void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5184{
Tejun Heo30159ec2013-06-25 11:53:37 -07005185 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005186 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005187 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005188
5189 /*
5190 * Unlink from the css_set task list if necessary.
5191 * Optimistically check cg_list before taking
5192 * css_set_lock
5193 */
5194 if (!list_empty(&tsk->cg_list)) {
5195 write_lock(&css_set_lock);
5196 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005197 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005198 write_unlock(&css_set_lock);
5199 }
5200
Paul Menageb4f48b62007-10-18 23:39:33 -07005201 /* Reassign the task to the init_css_set. */
5202 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005203 cset = task_css_set(tsk);
5204 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005205
5206 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005207 /*
5208 * fork/exit callbacks are supported only for builtin
5209 * subsystems, see cgroup_post_fork() for details.
5210 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005211 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005212 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005213 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005214 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005215
Li Zefan761b3ef2012-01-31 13:47:36 +08005216 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005217 }
5218 }
5219 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005220 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005221
Tejun Heo5abb8852013-06-12 21:04:49 -07005222 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005223}
Paul Menage697f4162007-10-18 23:39:34 -07005224
Paul Menagebd89aab2007-10-18 23:40:44 -07005225static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005226{
Li Zefanf50daa72013-03-01 15:06:07 +08005227 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005228 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005229 /*
5230 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005231 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005232 * it now
5233 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005234 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005235
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005236 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005237 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005238 list_empty(&cgrp->release_list)) {
5239 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005240 need_schedule_work = 1;
5241 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005242 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005243 if (need_schedule_work)
5244 schedule_work(&release_agent_work);
5245 }
5246}
5247
Paul Menage81a6a5c2007-10-18 23:39:38 -07005248/*
5249 * Notify userspace when a cgroup is released, by running the
5250 * configured release agent with the name of the cgroup (path
5251 * relative to the root of cgroup file system) as the argument.
5252 *
5253 * Most likely, this user command will try to rmdir this cgroup.
5254 *
5255 * This races with the possibility that some other task will be
5256 * attached to this cgroup before it is removed, or that some other
5257 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5258 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5259 * unused, and this cgroup will be reprieved from its death sentence,
5260 * to continue to serve a useful existence. Next time it's released,
5261 * we will get notified again, if it still has 'notify_on_release' set.
5262 *
5263 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5264 * means only wait until the task is successfully execve()'d. The
5265 * separate release agent task is forked by call_usermodehelper(),
5266 * then control in this thread returns here, without waiting for the
5267 * release agent task. We don't bother to wait because the caller of
5268 * this routine has no use for the exit status of the release agent
5269 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005270 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005271static void cgroup_release_agent(struct work_struct *work)
5272{
5273 BUG_ON(work != &release_agent_work);
5274 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005275 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005276 while (!list_empty(&release_list)) {
5277 char *argv[3], *envp[3];
5278 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005279 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005280 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005281 struct cgroup,
5282 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005283 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005284 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005285 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005286 if (!pathbuf)
5287 goto continue_free;
5288 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5289 goto continue_free;
5290 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5291 if (!agentbuf)
5292 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005293
5294 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005295 argv[i++] = agentbuf;
5296 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005297 argv[i] = NULL;
5298
5299 i = 0;
5300 /* minimal command environment */
5301 envp[i++] = "HOME=/";
5302 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5303 envp[i] = NULL;
5304
5305 /* Drop the lock while we invoke the usermode helper,
5306 * since the exec could involve hitting disk and hence
5307 * be a slow process */
5308 mutex_unlock(&cgroup_mutex);
5309 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005310 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005311 continue_free:
5312 kfree(pathbuf);
5313 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005314 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005315 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005316 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005317 mutex_unlock(&cgroup_mutex);
5318}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005319
5320static int __init cgroup_disable(char *str)
5321{
Tejun Heo30159ec2013-06-25 11:53:37 -07005322 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005323 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005324 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005325
5326 while ((token = strsep(&str, ",")) != NULL) {
5327 if (!*token)
5328 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005329
Tejun Heo30159ec2013-06-25 11:53:37 -07005330 /*
5331 * cgroup_disable, being at boot time, can't know about
5332 * module subsystems, so we don't worry about them.
5333 */
5334 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005335 if (!strcmp(token, ss->name)) {
5336 ss->disabled = 1;
5337 printk(KERN_INFO "Disabling %s control group"
5338 " subsystem\n", ss->name);
5339 break;
5340 }
5341 }
5342 }
5343 return 1;
5344}
5345__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005346
5347/*
5348 * Functons for CSS ID.
5349 */
5350
Tejun Heo54766d42013-06-12 21:04:53 -07005351/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352unsigned short css_id(struct cgroup_subsys_state *css)
5353{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005354 struct css_id *cssid;
5355
5356 /*
5357 * This css_id() can return correct value when somone has refcnt
5358 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5359 * it's unchanged until freed.
5360 */
Tejun Heod3daf282013-06-13 19:39:16 -07005361 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005362
5363 if (cssid)
5364 return cssid->id;
5365 return 0;
5366}
Ben Blum67523c42010-03-10 15:22:11 -08005367EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005368
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005369/**
5370 * css_is_ancestor - test "root" css is an ancestor of "child"
5371 * @child: the css to be tested.
5372 * @root: the css supporsed to be an ancestor of the child.
5373 *
5374 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005375 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005376 * But, considering usual usage, the csses should be valid objects after test.
5377 * Assuming that the caller will do some action to the child if this returns
5378 * returns true, the caller must take "child";s reference count.
5379 * If "child" is valid object and this returns true, "root" is valid, too.
5380 */
5381
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005382bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005383 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005384{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005385 struct css_id *child_id;
5386 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005387
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005388 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005389 if (!child_id)
5390 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005391 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005392 if (!root_id)
5393 return false;
5394 if (child_id->depth < root_id->depth)
5395 return false;
5396 if (child_id->stack[root_id->depth] != root_id->id)
5397 return false;
5398 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399}
5400
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005401void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5402{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005403 struct css_id *id = rcu_dereference_protected(css->id, true);
5404
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005405 /* When this is called before css_id initialization, id can be NULL */
5406 if (!id)
5407 return;
5408
5409 BUG_ON(!ss->use_id);
5410
5411 rcu_assign_pointer(id->css, NULL);
5412 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005413 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005414 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005415 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005416 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417}
Ben Blum67523c42010-03-10 15:22:11 -08005418EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005419
5420/*
5421 * This is called by init or create(). Then, calls to this function are
5422 * always serialized (By cgroup_mutex() at create()).
5423 */
5424
5425static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5426{
5427 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005428 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429
5430 BUG_ON(!ss->use_id);
5431
5432 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5433 newid = kzalloc(size, GFP_KERNEL);
5434 if (!newid)
5435 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005436
5437 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005438 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005439 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005440 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005441 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005442 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005443
5444 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005445 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005447
Tejun Heod228d9e2013-02-27 17:04:54 -08005448 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005449 newid->depth = depth;
5450 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451err_out:
5452 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005453 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005454
5455}
5456
Ben Blume6a11052010-03-10 15:22:09 -08005457static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5458 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005459{
5460 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005461
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005462 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005463 idr_init(&ss->idr);
5464
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005465 newid = get_new_cssid(ss, 0);
5466 if (IS_ERR(newid))
5467 return PTR_ERR(newid);
5468
5469 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005470 RCU_INIT_POINTER(newid->css, rootcss);
5471 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005472 return 0;
5473}
5474
5475static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5476 struct cgroup *child)
5477{
5478 int subsys_id, i, depth = 0;
5479 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005480 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005481
5482 subsys_id = ss->subsys_id;
5483 parent_css = parent->subsys[subsys_id];
5484 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005485 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005486 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005487
5488 child_id = get_new_cssid(ss, depth);
5489 if (IS_ERR(child_id))
5490 return PTR_ERR(child_id);
5491
5492 for (i = 0; i < depth; i++)
5493 child_id->stack[i] = parent_id->stack[i];
5494 child_id->stack[depth] = child_id->id;
5495 /*
5496 * child_id->css pointer will be set after this cgroup is available
5497 * see cgroup_populate_dir()
5498 */
5499 rcu_assign_pointer(child_css->id, child_id);
5500
5501 return 0;
5502}
5503
5504/**
5505 * css_lookup - lookup css by id
5506 * @ss: cgroup subsys to be looked into.
5507 * @id: the id
5508 *
5509 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5510 * NULL if not. Should be called under rcu_read_lock()
5511 */
5512struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5513{
5514 struct css_id *cssid = NULL;
5515
5516 BUG_ON(!ss->use_id);
5517 cssid = idr_find(&ss->idr, id);
5518
5519 if (unlikely(!cssid))
5520 return NULL;
5521
5522 return rcu_dereference(cssid->css);
5523}
Ben Blum67523c42010-03-10 15:22:11 -08005524EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005525
Stephane Eraniane5d13672011-02-14 11:20:01 +02005526/*
5527 * get corresponding css from file open on cgroupfs directory
5528 */
5529struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5530{
5531 struct cgroup *cgrp;
5532 struct inode *inode;
5533 struct cgroup_subsys_state *css;
5534
Al Viro496ad9a2013-01-23 17:07:38 -05005535 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005536 /* check in cgroup filesystem dir */
5537 if (inode->i_op != &cgroup_dir_inode_operations)
5538 return ERR_PTR(-EBADF);
5539
5540 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5541 return ERR_PTR(-EINVAL);
5542
5543 /* get cgroup */
5544 cgrp = __d_cgrp(f->f_dentry);
5545 css = cgrp->subsys[id];
5546 return css ? css : ERR_PTR(-ENOENT);
5547}
5548
Paul Menagefe693432009-09-23 15:56:20 -07005549#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005550static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005551{
5552 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5553
5554 if (!css)
5555 return ERR_PTR(-ENOMEM);
5556
5557 return css;
5558}
5559
Li Zefan03c78cb2013-06-14 11:17:19 +08005560static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005561{
Li Zefan03c78cb2013-06-14 11:17:19 +08005562 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005563}
5564
Li Zefan03c78cb2013-06-14 11:17:19 +08005565static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005566{
Li Zefan03c78cb2013-06-14 11:17:19 +08005567 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005568}
5569
Li Zefan03c78cb2013-06-14 11:17:19 +08005570static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005571{
5572 return (u64)(unsigned long)current->cgroups;
5573}
5574
Li Zefan03c78cb2013-06-14 11:17:19 +08005575static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5576 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005577{
5578 u64 count;
5579
5580 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005581 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005582 rcu_read_unlock();
5583 return count;
5584}
5585
Li Zefan03c78cb2013-06-14 11:17:19 +08005586static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005587 struct cftype *cft,
5588 struct seq_file *seq)
5589{
Tejun Heo69d02062013-06-12 21:04:50 -07005590 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005591 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005592
5593 read_lock(&css_set_lock);
5594 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005595 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005596 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005597 struct cgroup *c = link->cgrp;
5598 const char *name;
5599
5600 if (c->dentry)
5601 name = c->dentry->d_name.name;
5602 else
5603 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005604 seq_printf(seq, "Root %d group %s\n",
5605 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005606 }
5607 rcu_read_unlock();
5608 read_unlock(&css_set_lock);
5609 return 0;
5610}
5611
5612#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005613static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005614 struct cftype *cft,
5615 struct seq_file *seq)
5616{
Tejun Heo69d02062013-06-12 21:04:50 -07005617 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005618
5619 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005620 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005621 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005622 struct task_struct *task;
5623 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005624 seq_printf(seq, "css_set %p\n", cset);
5625 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005626 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5627 seq_puts(seq, " ...\n");
5628 break;
5629 } else {
5630 seq_printf(seq, " task %d\n",
5631 task_pid_vnr(task));
5632 }
5633 }
5634 }
5635 read_unlock(&css_set_lock);
5636 return 0;
5637}
5638
Paul Menagefe693432009-09-23 15:56:20 -07005639static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5640{
5641 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5642}
5643
5644static struct cftype debug_files[] = {
5645 {
Paul Menagefe693432009-09-23 15:56:20 -07005646 .name = "taskcount",
5647 .read_u64 = debug_taskcount_read,
5648 },
5649
5650 {
5651 .name = "current_css_set",
5652 .read_u64 = current_css_set_read,
5653 },
5654
5655 {
5656 .name = "current_css_set_refcount",
5657 .read_u64 = current_css_set_refcount_read,
5658 },
5659
5660 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005661 .name = "current_css_set_cg_links",
5662 .read_seq_string = current_css_set_cg_links_read,
5663 },
5664
5665 {
5666 .name = "cgroup_css_links",
5667 .read_seq_string = cgroup_css_links_read,
5668 },
5669
5670 {
Paul Menagefe693432009-09-23 15:56:20 -07005671 .name = "releasable",
5672 .read_u64 = releasable_read,
5673 },
Paul Menagefe693432009-09-23 15:56:20 -07005674
Tejun Heo4baf6e32012-04-01 12:09:55 -07005675 { } /* terminate */
5676};
Paul Menagefe693432009-09-23 15:56:20 -07005677
5678struct cgroup_subsys debug_subsys = {
5679 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005680 .css_alloc = debug_css_alloc,
5681 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005682 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005683 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005684};
5685#endif /* CONFIG_CGROUP_DEBUG */