blob: 3f65933335253d8be3e4dfd2b59839018f8ee3ad [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 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800469 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
471 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472static bool compare_css_sets(struct css_set *cset,
473 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 struct cgroup *new_cgrp,
475 struct cgroup_subsys_state *template[])
476{
477 struct list_head *l1, *l2;
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 /* Not all subsystems matched */
481 return false;
482 }
483
484 /*
485 * Compare cgroup pointers in order to distinguish between
486 * different cgroups in heirarchies with no subsystems. We
487 * could get by with just this check alone (and skip the
488 * memcmp above) but on most setups the memcmp check will
489 * avoid the need for this more expensive check on almost all
490 * candidates.
491 */
492
Tejun Heo69d02062013-06-12 21:04:50 -0700493 l1 = &cset->cgrp_links;
494 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700496 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 l1 = l1->next;
500 l2 = l2->next;
501 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700502 if (l1 == &cset->cgrp_links) {
503 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 break;
505 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700506 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 }
508 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700509 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
510 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 cgrp1 = link1->cgrp;
512 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 /*
517 * If this hierarchy is the hierarchy of the cgroup
518 * that's changing, then we need to check that this
519 * css_set points to the new cgroup; if it's any other
520 * hierarchy, then this css_set should point to the
521 * same cgroup as the old css_set.
522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 if (cgrp1->root == new_cgrp->root) {
524 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 return false;
526 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 return false;
529 }
530 }
531 return true;
532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
535 * find_existing_css_set - init css array and find the matching css_set
536 * @old_cset: the css_set that we're using before the cgroup transition
537 * @cgrp: the cgroup that we're moving into
538 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540static struct css_set *find_existing_css_set(struct css_set *old_cset,
541 struct cgroup *cgrp,
542 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700543{
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800547 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700548 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Ben Blumaae8aab2010-03-10 15:22:07 -0800550 /*
551 * Build the set of subsystem state objects that we want to see in the
552 * new css_set. while subsystems can change globally, the entries here
553 * won't change, so no need for locking.
554 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700555 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400556 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700557 /* Subsystem is in this hierarchy. So we want
558 * the subsystem state from the new
559 * cgroup */
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
869 /*
Li Zefanbe445622013-01-24 14:31:42 +0800870 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700871 * created the cgroup. This will free cgrp->root, if we are
872 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800873 */
874 deactivate_super(cgrp->root->sb);
875
876 /*
877 * if we're getting rid of the cgroup, refcount should ensure
878 * that there are no pidlists left.
879 */
880 BUG_ON(!list_empty(&cgrp->pidlists));
881
882 simple_xattrs_free(&cgrp->xattrs);
883
Li Zefan65dff752013-03-01 15:01:56 +0800884 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800885 kfree(cgrp);
886}
887
888static void cgroup_free_rcu(struct rcu_head *head)
889{
890 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
891
Tejun Heoea15f8c2013-06-13 19:27:42 -0700892 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
893 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800894}
895
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896static void cgroup_diput(struct dentry *dentry, struct inode *inode)
897{
898 /* is dentry a directory ? if so, kfree() associated cgroup */
899 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700900 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800901
Tejun Heo54766d42013-06-12 21:04:53 -0700902 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800903 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904 } else {
905 struct cfent *cfe = __d_cfe(dentry);
906 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
907
908 WARN_ONCE(!list_empty(&cfe->node) &&
909 cgrp != &cgrp->root->top_cgroup,
910 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700911 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700912 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913 }
914 iput(inode);
915}
916
Al Viroc72a04e2011-01-14 05:31:45 +0000917static int cgroup_delete(const struct dentry *d)
918{
919 return 1;
920}
921
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922static void remove_dir(struct dentry *d)
923{
924 struct dentry *parent = dget(d->d_parent);
925
926 d_delete(d);
927 simple_rmdir(parent->d_inode, d);
928 dput(parent);
929}
930
Li Zefan2739d3c2013-01-21 18:18:33 +0800931static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700932{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700933 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
936 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100937
Li Zefan2739d3c2013-01-21 18:18:33 +0800938 /*
939 * If we're doing cleanup due to failure of cgroup_create(),
940 * the corresponding @cfe may not exist.
941 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 list_for_each_entry(cfe, &cgrp->files, node) {
943 struct dentry *d = cfe->dentry;
944
945 if (cft && cfe->type != cft)
946 continue;
947
948 dget(d);
949 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700950 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951 list_del_init(&cfe->node);
952 dput(d);
953
Li Zefan2739d3c2013-01-21 18:18:33 +0800954 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956}
957
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400958/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700959 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700960 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961 * @subsys_mask: mask of the subsystem ids whose files should be removed
962 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700963static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700964{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400965 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700966 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967
Tejun Heob420ba72013-07-12 12:34:02 -0700968 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700970
971 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 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;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001004 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001005 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006
Ben Blumaae8aab2010-03-10 15:22:07 -08001007 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001008 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001009
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001011 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001012 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001014
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001015 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001016 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001017 ret = -EBUSY;
1018 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001020
1021 /* pin the module */
1022 if (!try_module_get(ss->module)) {
1023 ret = -ENOENT;
1024 goto out_put;
1025 }
1026 pinned |= 1 << i;
1027 }
1028
1029 /* subsys could be missing if unloaded between parsing and here */
1030 if (added_mask != pinned) {
1031 ret = -ENOENT;
1032 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 }
1034
Tejun Heo31261212013-06-28 17:07:30 -07001035 ret = cgroup_populate_dir(cgrp, added_mask);
1036 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001037 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001038
1039 /*
1040 * Nothing can fail from this point on. Remove files for the
1041 * removed subsystems and rebind each subsystem.
1042 */
1043 cgroup_clear_dir(cgrp, removed_mask);
1044
Tejun Heo30159ec2013-06-25 11:53:37 -07001045 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001046 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001047
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001048 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001050 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001051 BUG_ON(!cgroup_dummy_top->subsys[i]);
1052 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001053
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001055 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001056 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001057 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001059 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001060
Ben Blumcf5d5942010-03-10 15:22:09 -08001061 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001063 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001065 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001066 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001067
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001069 ss->bind(cgroup_dummy_top);
1070 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001071 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001072 cgroup_subsys[i]->root = &cgroup_dummy_root;
1073 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001074
Ben Blumcf5d5942010-03-10 15:22:09 -08001075 /* subsystem is now free - drop reference on module */
1076 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001077 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 }
1079 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080
Tejun Heo1672d042013-06-25 18:04:54 -07001081 /*
1082 * Mark @root has finished binding subsystems. @root->subsys_mask
1083 * now matches the bound subsystems.
1084 */
1085 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1086
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001088
1089out_put:
1090 for_each_subsys(ss, i)
1091 if (pinned & (1 << i))
1092 module_put(ss->module);
1093 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094}
1095
Al Viro34c80b12011-12-08 21:32:45 -05001096static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097{
Al Viro34c80b12011-12-08 21:32:45 -05001098 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 struct cgroup_subsys *ss;
1100
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001101 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001102 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001104 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1105 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001106 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001108 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001109 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001110 if (strlen(root->release_agent_path))
1111 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001112 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001113 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001114 if (strlen(root->name))
1115 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001116 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117 return 0;
1118}
1119
1120struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001121 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001122 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001123 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001124 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001125 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001126 /* User explicitly requested empty subsystem */
1127 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001128
1129 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001130
Paul Menageddbcc7e2007-10-18 23:39:30 -07001131};
1132
Ben Blumaae8aab2010-03-10 15:22:07 -08001133/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001134 * Convert a hierarchy specifier into a bitmask of subsystems and
1135 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1136 * array. This function takes refcounts on subsystems to be used, unless it
1137 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001138 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001139static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001140{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 char *token, *o = data;
1142 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001143 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001144 struct cgroup_subsys *ss;
1145 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001146
Ben Blumaae8aab2010-03-10 15:22:07 -08001147 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1148
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149#ifdef CONFIG_CPUSETS
1150 mask = ~(1UL << cpuset_subsys_id);
1151#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152
Paul Menagec6d57f32009-09-23 15:56:19 -07001153 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154
1155 while ((token = strsep(&o, ",")) != NULL) {
1156 if (!*token)
1157 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001158 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001159 /* Explicitly have no subsystems */
1160 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 continue;
1162 }
1163 if (!strcmp(token, "all")) {
1164 /* Mutually exclusive option 'all' + subsystem name */
1165 if (one_ss)
1166 return -EINVAL;
1167 all_ss = true;
1168 continue;
1169 }
Tejun Heo873fe092013-04-14 20:15:26 -07001170 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1171 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1172 continue;
1173 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001175 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 continue;
1177 }
1178 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001179 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 continue;
1181 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001182 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001183 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001184 continue;
1185 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001186 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001187 /* Specifying two release agents is forbidden */
1188 if (opts->release_agent)
1189 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001190 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001191 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001192 if (!opts->release_agent)
1193 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194 continue;
1195 }
1196 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001197 const char *name = token + 5;
1198 /* Can't specify an empty name */
1199 if (!strlen(name))
1200 return -EINVAL;
1201 /* Must match [\w.-]+ */
1202 for (i = 0; i < strlen(name); i++) {
1203 char c = name[i];
1204 if (isalnum(c))
1205 continue;
1206 if ((c == '.') || (c == '-') || (c == '_'))
1207 continue;
1208 return -EINVAL;
1209 }
1210 /* Specifying two names is forbidden */
1211 if (opts->name)
1212 return -EINVAL;
1213 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001214 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001215 GFP_KERNEL);
1216 if (!opts->name)
1217 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218
1219 continue;
1220 }
1221
Tejun Heo30159ec2013-06-25 11:53:37 -07001222 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001223 if (strcmp(token, ss->name))
1224 continue;
1225 if (ss->disabled)
1226 continue;
1227
1228 /* Mutually exclusive option 'all' + subsystem name */
1229 if (all_ss)
1230 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001231 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 one_ss = true;
1233
1234 break;
1235 }
1236 if (i == CGROUP_SUBSYS_COUNT)
1237 return -ENOENT;
1238 }
1239
1240 /*
1241 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001242 * otherwise if 'none', 'name=' and a subsystem name options
1243 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001244 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001245 if (all_ss || (!one_ss && !opts->none && !opts->name))
1246 for_each_subsys(ss, i)
1247 if (!ss->disabled)
1248 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001249
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001250 /* Consistency checks */
1251
Tejun Heo873fe092013-04-14 20:15:26 -07001252 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1253 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1254
1255 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1256 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1257 return -EINVAL;
1258 }
1259
1260 if (opts->cpuset_clone_children) {
1261 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1262 return -EINVAL;
1263 }
1264 }
1265
Li Zefanf9ab5b52009-06-17 16:26:33 -07001266 /*
1267 * Option noprefix was introduced just for backward compatibility
1268 * with the old cpuset, so we allow noprefix only if mounting just
1269 * the cpuset subsystem.
1270 */
Tejun Heo93438622013-04-14 20:15:25 -07001271 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001272 return -EINVAL;
1273
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001274
1275 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001276 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277 return -EINVAL;
1278
1279 /*
1280 * We either have to specify by name or by subsystems. (So all
1281 * empty hierarchies must have a name).
1282 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001283 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001284 return -EINVAL;
1285
1286 return 0;
1287}
1288
1289static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1290{
1291 int ret = 0;
1292 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001293 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001294 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001295 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296
Tejun Heo873fe092013-04-14 20:15:26 -07001297 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1298 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1299 return -EINVAL;
1300 }
1301
Paul Menagebd89aab2007-10-18 23:40:44 -07001302 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001304 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305
1306 /* See what subsystems are wanted */
1307 ret = parse_cgroupfs_options(data, &opts);
1308 if (ret)
1309 goto out_unlock;
1310
Tejun Heoa8a648c2013-06-24 15:21:47 -07001311 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001312 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1313 task_tgid_nr(current), current->comm);
1314
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001315 added_mask = opts.subsys_mask & ~root->subsys_mask;
1316 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001317
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001319 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001321 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1322 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1323 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001325 goto out_unlock;
1326 }
1327
Tejun Heof172e672013-06-28 17:07:30 -07001328 /* remounting is not allowed for populated hierarchies */
1329 if (root->number_of_cgroups > 1) {
1330 ret = -EBUSY;
1331 goto out_unlock;
1332 }
1333
Tejun Heoa8a648c2013-06-24 15:21:47 -07001334 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001335 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001336 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337
Paul Menage81a6a5c2007-10-18 23:39:38 -07001338 if (opts.release_agent)
1339 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001341 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001342 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001343 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001345 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 return ret;
1347}
1348
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001349static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350 .statfs = simple_statfs,
1351 .drop_inode = generic_delete_inode,
1352 .show_options = cgroup_show_options,
1353 .remount_fs = cgroup_remount,
1354};
1355
Paul Menagecc31edc2008-10-18 20:28:04 -07001356static void init_cgroup_housekeeping(struct cgroup *cgrp)
1357{
1358 INIT_LIST_HEAD(&cgrp->sibling);
1359 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001360 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001361 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001362 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001363 INIT_LIST_HEAD(&cgrp->pidlists);
1364 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001365 INIT_LIST_HEAD(&cgrp->event_list);
1366 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001367 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001368}
Paul Menagec6d57f32009-09-23 15:56:19 -07001369
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370static void init_cgroup_root(struct cgroupfs_root *root)
1371{
Paul Menagebd89aab2007-10-18 23:40:44 -07001372 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001373
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374 INIT_LIST_HEAD(&root->subsys_list);
1375 INIT_LIST_HEAD(&root->root_list);
1376 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001377 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001378 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001379 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001380 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381}
1382
Tejun Heofc76df72013-06-25 11:53:37 -07001383static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001384{
Tejun Heo1a574232013-04-14 11:36:58 -07001385 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001386
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001387 lockdep_assert_held(&cgroup_mutex);
1388 lockdep_assert_held(&cgroup_root_mutex);
1389
Tejun Heofc76df72013-06-25 11:53:37 -07001390 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1391 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001392 if (id < 0)
1393 return id;
1394
1395 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001396 return 0;
1397}
1398
1399static void cgroup_exit_root_id(struct cgroupfs_root *root)
1400{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001401 lockdep_assert_held(&cgroup_mutex);
1402 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001403
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001404 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001405 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001406 root->hierarchy_id = 0;
1407 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408}
1409
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410static int cgroup_test_super(struct super_block *sb, void *data)
1411{
Paul Menagec6d57f32009-09-23 15:56:19 -07001412 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413 struct cgroupfs_root *root = sb->s_fs_info;
1414
Paul Menagec6d57f32009-09-23 15:56:19 -07001415 /* If we asked for a name then it must match */
1416 if (opts->name && strcmp(opts->name, root->name))
1417 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001419 /*
1420 * If we asked for subsystems (or explicitly for no
1421 * subsystems) then they must match
1422 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001423 if ((opts->subsys_mask || opts->none)
1424 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001425 return 0;
1426
1427 return 1;
1428}
1429
Paul Menagec6d57f32009-09-23 15:56:19 -07001430static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1431{
1432 struct cgroupfs_root *root;
1433
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001434 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 return NULL;
1436
1437 root = kzalloc(sizeof(*root), GFP_KERNEL);
1438 if (!root)
1439 return ERR_PTR(-ENOMEM);
1440
1441 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001442
Tejun Heo1672d042013-06-25 18:04:54 -07001443 /*
1444 * We need to set @root->subsys_mask now so that @root can be
1445 * matched by cgroup_test_super() before it finishes
1446 * initialization; otherwise, competing mounts with the same
1447 * options may try to bind the same subsystems instead of waiting
1448 * for the first one leading to unexpected mount errors.
1449 * SUBSYS_BOUND will be set once actual binding is complete.
1450 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001451 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001452 root->flags = opts->flags;
1453 if (opts->release_agent)
1454 strcpy(root->release_agent_path, opts->release_agent);
1455 if (opts->name)
1456 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001457 if (opts->cpuset_clone_children)
1458 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 return root;
1460}
1461
Tejun Heofa3ca072013-04-14 11:36:56 -07001462static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463{
Tejun Heofa3ca072013-04-14 11:36:56 -07001464 if (root) {
1465 /* hierarhcy ID shoulid already have been released */
1466 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001468 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001469 kfree(root);
1470 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001471}
1472
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473static int cgroup_set_super(struct super_block *sb, void *data)
1474{
1475 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 struct cgroup_sb_opts *opts = data;
1477
1478 /* If we don't have a new root, we can't set up a new sb */
1479 if (!opts->new_root)
1480 return -EINVAL;
1481
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001482 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001483
1484 ret = set_anon_super(sb, NULL);
1485 if (ret)
1486 return ret;
1487
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 sb->s_fs_info = opts->new_root;
1489 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001490
1491 sb->s_blocksize = PAGE_CACHE_SIZE;
1492 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1493 sb->s_magic = CGROUP_SUPER_MAGIC;
1494 sb->s_op = &cgroup_ops;
1495
1496 return 0;
1497}
1498
1499static int cgroup_get_rootdir(struct super_block *sb)
1500{
Al Viro0df6a632010-12-21 13:29:29 -05001501 static const struct dentry_operations cgroup_dops = {
1502 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001503 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001504 };
1505
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506 struct inode *inode =
1507 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508
1509 if (!inode)
1510 return -ENOMEM;
1511
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512 inode->i_fop = &simple_dir_operations;
1513 inode->i_op = &cgroup_dir_inode_operations;
1514 /* directories start off with i_nlink == 2 (for "." entry) */
1515 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001516 sb->s_root = d_make_root(inode);
1517 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001518 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001519 /* for everything else we want ->d_op set */
1520 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521 return 0;
1522}
1523
Al Virof7e83572010-07-26 13:23:11 +04001524static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001526 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527{
1528 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001529 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530 int ret = 0;
1531 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001532 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001533 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001534 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001535 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001536
1537 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001538 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001540 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001541 if (ret)
1542 goto out_err;
1543
1544 /*
1545 * Allocate a new cgroup root. We may not need it if we're
1546 * reusing an existing hierarchy.
1547 */
1548 new_root = cgroup_root_from_opts(&opts);
1549 if (IS_ERR(new_root)) {
1550 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001551 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001552 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001553 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001556 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001558 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001559 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001560 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 }
1562
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 root = sb->s_fs_info;
1564 BUG_ON(!root);
1565 if (root == opts.new_root) {
1566 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001567 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001568 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001569 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001570 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571
1572 BUG_ON(sb->s_root != NULL);
1573
1574 ret = cgroup_get_rootdir(sb);
1575 if (ret)
1576 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001577 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578
Paul Menage817929e2007-10-18 23:39:36 -07001579 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001581 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001583 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1584 0, 1, GFP_KERNEL);
1585 if (root_cgrp->id < 0)
1586 goto unlock_drop;
1587
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001588 /* Check for name clashes with existing mounts */
1589 ret = -EBUSY;
1590 if (strlen(root->name))
1591 for_each_active_root(existing_root)
1592 if (!strcmp(existing_root->name, root->name))
1593 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001594
Paul Menage817929e2007-10-18 23:39:36 -07001595 /*
1596 * We're accessing css_set_count without locking
1597 * css_set_lock here, but that's OK - it can only be
1598 * increased by someone holding cgroup_lock, and
1599 * that's us. The worst that can happen is that we
1600 * have some link structures left over
1601 */
Tejun Heo69d02062013-06-12 21:04:50 -07001602 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001603 if (ret)
1604 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001605
Tejun Heofc76df72013-06-25 11:53:37 -07001606 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1607 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001608 if (ret)
1609 goto unlock_drop;
1610
Tejun Heo31261212013-06-28 17:07:30 -07001611 sb->s_root->d_fsdata = root_cgrp;
1612 root_cgrp->dentry = sb->s_root;
1613
1614 /*
1615 * We're inside get_sb() and will call lookup_one_len() to
1616 * create the root files, which doesn't work if SELinux is
1617 * in use. The following cred dancing somehow works around
1618 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1619 * populating new cgroupfs mount") for more details.
1620 */
1621 cred = override_creds(&init_cred);
1622
1623 ret = cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true);
1624 if (ret)
1625 goto rm_base_files;
1626
Tejun Heoa8a648c2013-06-24 15:21:47 -07001627 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001628 if (ret)
1629 goto rm_base_files;
1630
1631 revert_creds(cred);
1632
Ben Blumcf5d5942010-03-10 15:22:09 -08001633 /*
1634 * There must be no failure case after here, since rebinding
1635 * takes care of subsystems' refcounts, which are explicitly
1636 * dropped in the failure exit path.
1637 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
Tejun Heo9871bf92013-06-24 15:21:47 -07001639 list_add(&root->root_list, &cgroup_roots);
1640 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641
Paul Menage817929e2007-10-18 23:39:36 -07001642 /* Link the top cgroup in this hierarchy into all
1643 * the css_set objects */
1644 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001645 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001646 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001647 write_unlock(&css_set_lock);
1648
Tejun Heo69d02062013-06-12 21:04:50 -07001649 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001650
Li Zefanc12f65d2009-01-07 18:07:42 -08001651 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 BUG_ON(root->number_of_cgroups != 1);
1653
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001654 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001656 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001657 } else {
1658 /*
1659 * We re-used an existing hierarchy - the new root (if
1660 * any) is not needed
1661 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001662 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001663
Tejun Heoc7ba8282013-06-29 14:06:10 -07001664 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001665 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1666 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1667 ret = -EINVAL;
1668 goto drop_new_super;
1669 } else {
1670 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1671 }
Tejun Heo873fe092013-04-14 20:15:26 -07001672 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 }
1674
Paul Menagec6d57f32009-09-23 15:56:19 -07001675 kfree(opts.release_agent);
1676 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001677 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678
Tejun Heo31261212013-06-28 17:07:30 -07001679 rm_base_files:
1680 free_cgrp_cset_links(&tmp_links);
1681 cgroup_addrm_files(&root->top_cgroup, NULL, cgroup_base_files, false);
1682 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001683 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001684 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001685 mutex_unlock(&cgroup_root_mutex);
1686 mutex_unlock(&cgroup_mutex);
1687 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001689 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001690 out_err:
1691 kfree(opts.release_agent);
1692 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001693 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694}
1695
1696static void cgroup_kill_sb(struct super_block *sb) {
1697 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001698 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001699 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 int ret;
1701
1702 BUG_ON(!root);
1703
1704 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001705 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Tejun Heo31261212013-06-28 17:07:30 -07001707 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001709 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710
1711 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001712 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1713 ret = rebind_subsystems(root, 0, root->subsys_mask);
1714 /* Shouldn't be able to fail ... */
1715 BUG_ON(ret);
1716 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717
Paul Menage817929e2007-10-18 23:39:36 -07001718 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001719 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001720 * root cgroup
1721 */
1722 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001723
Tejun Heo69d02062013-06-12 21:04:50 -07001724 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1725 list_del(&link->cset_link);
1726 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001727 kfree(link);
1728 }
1729 write_unlock(&css_set_lock);
1730
Paul Menage839ec542009-01-29 14:25:22 -08001731 if (!list_empty(&root->root_list)) {
1732 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001733 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001734 }
Li Zefane5f6a862009-01-07 18:07:41 -08001735
Tejun Heofa3ca072013-04-14 11:36:56 -07001736 cgroup_exit_root_id(root);
1737
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001738 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001740 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001742 simple_xattrs_free(&cgrp->xattrs);
1743
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001745 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746}
1747
1748static struct file_system_type cgroup_fs_type = {
1749 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001750 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751 .kill_sb = cgroup_kill_sb,
1752};
1753
Greg KH676db4a2010-08-05 13:53:35 -07001754static struct kobject *cgroup_kobj;
1755
Li Zefana043e3b2008-02-23 15:24:09 -08001756/**
1757 * cgroup_path - generate the path of a cgroup
1758 * @cgrp: the cgroup in question
1759 * @buf: the buffer to write the path into
1760 * @buflen: the length of the buffer
1761 *
Li Zefan65dff752013-03-01 15:01:56 +08001762 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1763 *
1764 * We can't generate cgroup path using dentry->d_name, as accessing
1765 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1766 * inode's i_mutex, while on the other hand cgroup_path() can be called
1767 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001769int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001770{
Li Zefan65dff752013-03-01 15:01:56 +08001771 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001773
Tejun Heoda1f2962013-04-14 10:32:19 -07001774 if (!cgrp->parent) {
1775 if (strlcpy(buf, "/", buflen) >= buflen)
1776 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001777 return 0;
1778 }
1779
Tao Ma316eb662012-11-08 21:36:38 +08001780 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001781 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001782
Li Zefan65dff752013-03-01 15:01:56 +08001783 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001784 do {
Li Zefan65dff752013-03-01 15:01:56 +08001785 const char *name = cgroup_name(cgrp);
1786 int len;
1787
1788 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001790 goto out;
1791 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001792
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001794 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001796
1797 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001798 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001799 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001801out:
1802 rcu_read_unlock();
1803 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804}
Ben Blum67523c42010-03-10 15:22:11 -08001805EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806
Tejun Heo857a2be2013-04-14 20:50:08 -07001807/**
1808 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1809 * @task: target task
1810 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1811 * @buf: the buffer to write the path into
1812 * @buflen: the length of the buffer
1813 *
1814 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1815 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1816 * be used inside locks used by cgroup controller callbacks.
1817 */
1818int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1819 char *buf, size_t buflen)
1820{
1821 struct cgroupfs_root *root;
1822 struct cgroup *cgrp = NULL;
1823 int ret = -ENOENT;
1824
1825 mutex_lock(&cgroup_mutex);
1826
1827 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1828 if (root) {
1829 cgrp = task_cgroup_from_root(task, root);
1830 ret = cgroup_path(cgrp, buf, buflen);
1831 }
1832
1833 mutex_unlock(&cgroup_mutex);
1834
1835 return ret;
1836}
1837EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1838
Ben Blum74a11662011-05-26 16:25:20 -07001839/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001840 * Control Group taskset
1841 */
Tejun Heo134d3372011-12-12 18:12:21 -08001842struct task_and_cgroup {
1843 struct task_struct *task;
1844 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001845 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001846};
1847
Tejun Heo2f7ee562011-12-12 18:12:21 -08001848struct cgroup_taskset {
1849 struct task_and_cgroup single;
1850 struct flex_array *tc_array;
1851 int tc_array_len;
1852 int idx;
1853 struct cgroup *cur_cgrp;
1854};
1855
1856/**
1857 * cgroup_taskset_first - reset taskset and return the first task
1858 * @tset: taskset of interest
1859 *
1860 * @tset iteration is initialized and the first task is returned.
1861 */
1862struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1863{
1864 if (tset->tc_array) {
1865 tset->idx = 0;
1866 return cgroup_taskset_next(tset);
1867 } else {
1868 tset->cur_cgrp = tset->single.cgrp;
1869 return tset->single.task;
1870 }
1871}
1872EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1873
1874/**
1875 * cgroup_taskset_next - iterate to the next task in taskset
1876 * @tset: taskset of interest
1877 *
1878 * Return the next task in @tset. Iteration must have been initialized
1879 * with cgroup_taskset_first().
1880 */
1881struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1882{
1883 struct task_and_cgroup *tc;
1884
1885 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1886 return NULL;
1887
1888 tc = flex_array_get(tset->tc_array, tset->idx++);
1889 tset->cur_cgrp = tc->cgrp;
1890 return tc->task;
1891}
1892EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1893
1894/**
1895 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1896 * @tset: taskset of interest
1897 *
1898 * Return the cgroup for the current (last returned) task of @tset. This
1899 * function must be preceded by either cgroup_taskset_first() or
1900 * cgroup_taskset_next().
1901 */
1902struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1903{
1904 return tset->cur_cgrp;
1905}
1906EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1907
1908/**
1909 * cgroup_taskset_size - return the number of tasks in taskset
1910 * @tset: taskset of interest
1911 */
1912int cgroup_taskset_size(struct cgroup_taskset *tset)
1913{
1914 return tset->tc_array ? tset->tc_array_len : 1;
1915}
1916EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1917
1918
Ben Blum74a11662011-05-26 16:25:20 -07001919/*
1920 * cgroup_task_migrate - move a task from one cgroup to another.
1921 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001922 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001923 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001924static void cgroup_task_migrate(struct cgroup *old_cgrp,
1925 struct task_struct *tsk,
1926 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001927{
Tejun Heo5abb8852013-06-12 21:04:49 -07001928 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001929
1930 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001931 * We are synchronized through threadgroup_lock() against PF_EXITING
1932 * setting such that we can't race against cgroup_exit() changing the
1933 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001934 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001935 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001936 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001937
Ben Blum74a11662011-05-26 16:25:20 -07001938 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001939 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001940 task_unlock(tsk);
1941
1942 /* Update the css_set linked lists if we're using them */
1943 write_lock(&css_set_lock);
1944 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001945 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001946 write_unlock(&css_set_lock);
1947
1948 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001949 * We just gained a reference on old_cset by taking it from the
1950 * task. As trading it for new_cset is protected by cgroup_mutex,
1951 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001952 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001953 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1954 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001955}
1956
Li Zefana043e3b2008-02-23 15:24:09 -08001957/**
Li Zefan081aa452013-03-13 09:17:09 +08001958 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001959 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001960 * @tsk: the task or the leader of the threadgroup to be attached
1961 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001962 *
Tejun Heo257058a2011-12-12 18:12:21 -08001963 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001964 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001965 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001966static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1967 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001968{
1969 int retval, i, group_size;
1970 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001971 struct cgroupfs_root *root = cgrp->root;
1972 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001973 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001974 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001975 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001976 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001977
1978 /*
1979 * step 0: in order to do expensive, possibly blocking operations for
1980 * every thread, we cannot iterate the thread group list, since it needs
1981 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001982 * group - group_rwsem prevents new threads from appearing, and if
1983 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001984 */
Li Zefan081aa452013-03-13 09:17:09 +08001985 if (threadgroup)
1986 group_size = get_nr_threads(tsk);
1987 else
1988 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001989 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001990 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001991 if (!group)
1992 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001993 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001994 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001995 if (retval)
1996 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001997
Ben Blum74a11662011-05-26 16:25:20 -07001998 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001999 /*
2000 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2001 * already PF_EXITING could be freed from underneath us unless we
2002 * take an rcu_read_lock.
2003 */
2004 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002005 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002006 struct task_and_cgroup ent;
2007
Tejun Heocd3d0952011-12-12 18:12:21 -08002008 /* @tsk either already exited or can't exit until the end */
2009 if (tsk->flags & PF_EXITING)
2010 continue;
2011
Ben Blum74a11662011-05-26 16:25:20 -07002012 /* as per above, nr_threads may decrease, but not increase. */
2013 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002014 ent.task = tsk;
2015 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002016 /* nothing to do if this task is already in the cgroup */
2017 if (ent.cgrp == cgrp)
2018 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002019 /*
2020 * saying GFP_ATOMIC has no effect here because we did prealloc
2021 * earlier, but it's good form to communicate our expectations.
2022 */
Tejun Heo134d3372011-12-12 18:12:21 -08002023 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002024 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002025 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002026
2027 if (!threadgroup)
2028 break;
Ben Blum74a11662011-05-26 16:25:20 -07002029 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002030 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002031 /* remember the number of threads in the array for later. */
2032 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002033 tset.tc_array = group;
2034 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002035
Tejun Heo134d3372011-12-12 18:12:21 -08002036 /* methods shouldn't be called if no task is actually migrating */
2037 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002038 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002039 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002040
Ben Blum74a11662011-05-26 16:25:20 -07002041 /*
2042 * step 1: check that we can legitimately attach to the cgroup.
2043 */
Tejun Heo5549c492013-06-24 15:21:48 -07002044 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002045 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002046 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002047 if (retval) {
2048 failed_ss = ss;
2049 goto out_cancel_attach;
2050 }
2051 }
Ben Blum74a11662011-05-26 16:25:20 -07002052 }
2053
2054 /*
2055 * step 2: make sure css_sets exist for all threads to be migrated.
2056 * we use find_css_set, which allocates a new one if necessary.
2057 */
Ben Blum74a11662011-05-26 16:25:20 -07002058 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002059 struct css_set *old_cset;
2060
Tejun Heo134d3372011-12-12 18:12:21 -08002061 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002062 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002063 tc->cset = find_css_set(old_cset, cgrp);
2064 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002065 retval = -ENOMEM;
2066 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002067 }
2068 }
2069
2070 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002071 * step 3: now that we're guaranteed success wrt the css_sets,
2072 * proceed to move all tasks to the new cgroup. There are no
2073 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002074 */
Ben Blum74a11662011-05-26 16:25:20 -07002075 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002076 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002077 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002078 }
2079 /* nothing is sensitive to fork() after this point. */
2080
2081 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002082 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002083 */
Tejun Heo5549c492013-06-24 15:21:48 -07002084 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002085 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002086 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002087 }
2088
2089 /*
2090 * step 5: success! and cleanup
2091 */
Ben Blum74a11662011-05-26 16:25:20 -07002092 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002093out_put_css_set_refs:
2094 if (retval) {
2095 for (i = 0; i < group_size; i++) {
2096 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002097 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002098 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002099 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002100 }
Ben Blum74a11662011-05-26 16:25:20 -07002101 }
2102out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002103 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002104 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002105 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002106 break;
Ben Blum74a11662011-05-26 16:25:20 -07002107 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002108 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002109 }
2110 }
Ben Blum74a11662011-05-26 16:25:20 -07002111out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002112 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002113 return retval;
2114}
2115
2116/*
2117 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002118 * function to attach either it or all tasks in its threadgroup. Will lock
2119 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002120 */
2121static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002123 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002124 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002125 int ret;
2126
Ben Blum74a11662011-05-26 16:25:20 -07002127 if (!cgroup_lock_live_group(cgrp))
2128 return -ENODEV;
2129
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002130retry_find_task:
2131 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002133 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002134 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002136 ret= -ESRCH;
2137 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002138 }
Ben Blum74a11662011-05-26 16:25:20 -07002139 /*
2140 * even if we're attaching all tasks in the thread group, we
2141 * only need to check permissions on one of them.
2142 */
David Howellsc69e8d92008-11-14 10:39:19 +11002143 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002144 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2145 !uid_eq(cred->euid, tcred->uid) &&
2146 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002147 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 ret = -EACCES;
2149 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002150 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002151 } else
2152 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002153
2154 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002156
2157 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002158 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002159 * trapped in a cpuset, or RT worker may be born in a cgroup
2160 * with no rt_runtime allocated. Just say no.
2161 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002162 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002163 ret = -EINVAL;
2164 rcu_read_unlock();
2165 goto out_unlock_cgroup;
2166 }
2167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 get_task_struct(tsk);
2169 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002170
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002171 threadgroup_lock(tsk);
2172 if (threadgroup) {
2173 if (!thread_group_leader(tsk)) {
2174 /*
2175 * a race with de_thread from another thread's exec()
2176 * may strip us of our leadership, if this happens,
2177 * there is no choice but to throw this task away and
2178 * try again; this is
2179 * "double-double-toil-and-trouble-check locking".
2180 */
2181 threadgroup_unlock(tsk);
2182 put_task_struct(tsk);
2183 goto retry_find_task;
2184 }
Li Zefan081aa452013-03-13 09:17:09 +08002185 }
2186
2187 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2188
Tejun Heocd3d0952011-12-12 18:12:21 -08002189 threadgroup_unlock(tsk);
2190
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002192out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002193 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002194 return ret;
2195}
2196
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002197/**
2198 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2199 * @from: attach to all cgroups of a given task
2200 * @tsk: the task to be attached
2201 */
2202int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2203{
2204 struct cgroupfs_root *root;
2205 int retval = 0;
2206
Tejun Heo47cfcd02013-04-07 09:29:51 -07002207 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002208 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002209 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002210
Li Zefan6f4b7e62013-07-31 16:18:36 +08002211 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002212 if (retval)
2213 break;
2214 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002215 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002216
2217 return retval;
2218}
2219EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2220
Paul Menageaf351022008-07-25 01:47:01 -07002221static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2222{
Ben Blum74a11662011-05-26 16:25:20 -07002223 return attach_task_by_pid(cgrp, pid, false);
2224}
2225
2226static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2227{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002228 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002229}
2230
Paul Menagee788e062008-07-25 01:46:59 -07002231static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2232 const char *buffer)
2233{
2234 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002235 if (strlen(buffer) >= PATH_MAX)
2236 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002237 if (!cgroup_lock_live_group(cgrp))
2238 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002239 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002240 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002241 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002242 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002243 return 0;
2244}
2245
2246static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2247 struct seq_file *seq)
2248{
2249 if (!cgroup_lock_live_group(cgrp))
2250 return -ENODEV;
2251 seq_puts(seq, cgrp->root->release_agent_path);
2252 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002254 return 0;
2255}
2256
Tejun Heo873fe092013-04-14 20:15:26 -07002257static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2258 struct seq_file *seq)
2259{
2260 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002261 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002262}
2263
Paul Menage84eea842008-07-25 01:47:00 -07002264/* A buffer size big enough for numbers or short strings */
2265#define CGROUP_LOCAL_BUFFER_SIZE 64
2266
Paul Menagee73d2c62008-04-29 01:00:06 -07002267static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002268 struct file *file,
2269 const char __user *userbuf,
2270 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002271{
Paul Menage84eea842008-07-25 01:47:00 -07002272 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002273 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002274 char *end;
2275
2276 if (!nbytes)
2277 return -EINVAL;
2278 if (nbytes >= sizeof(buffer))
2279 return -E2BIG;
2280 if (copy_from_user(buffer, userbuf, nbytes))
2281 return -EFAULT;
2282
2283 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002284 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002285 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002286 if (*end)
2287 return -EINVAL;
2288 retval = cft->write_u64(cgrp, cft, val);
2289 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002290 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002291 if (*end)
2292 return -EINVAL;
2293 retval = cft->write_s64(cgrp, cft, val);
2294 }
Paul Menage355e0c42007-10-18 23:39:33 -07002295 if (!retval)
2296 retval = nbytes;
2297 return retval;
2298}
2299
Paul Menagedb3b1492008-07-25 01:46:58 -07002300static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2301 struct file *file,
2302 const char __user *userbuf,
2303 size_t nbytes, loff_t *unused_ppos)
2304{
Paul Menage84eea842008-07-25 01:47:00 -07002305 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002306 int retval = 0;
2307 size_t max_bytes = cft->max_write_len;
2308 char *buffer = local_buffer;
2309
2310 if (!max_bytes)
2311 max_bytes = sizeof(local_buffer) - 1;
2312 if (nbytes >= max_bytes)
2313 return -E2BIG;
2314 /* Allocate a dynamic buffer if we need one */
2315 if (nbytes >= sizeof(local_buffer)) {
2316 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2317 if (buffer == NULL)
2318 return -ENOMEM;
2319 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002320 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2321 retval = -EFAULT;
2322 goto out;
2323 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002324
2325 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002326 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002327 if (!retval)
2328 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002329out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002330 if (buffer != local_buffer)
2331 kfree(buffer);
2332 return retval;
2333}
2334
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2336 size_t nbytes, loff_t *ppos)
2337{
2338 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002339 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002340
Tejun Heo54766d42013-06-12 21:04:53 -07002341 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002343 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002344 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002345 if (cft->write_u64 || cft->write_s64)
2346 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002347 if (cft->write_string)
2348 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002349 if (cft->trigger) {
2350 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2351 return ret ? ret : nbytes;
2352 }
Paul Menage355e0c42007-10-18 23:39:33 -07002353 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354}
2355
Paul Menagef4c753b2008-04-29 00:59:56 -07002356static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2357 struct file *file,
2358 char __user *buf, size_t nbytes,
2359 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360{
Paul Menage84eea842008-07-25 01:47:00 -07002361 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002362 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002363 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2364
2365 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2366}
2367
Paul Menagee73d2c62008-04-29 01:00:06 -07002368static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2369 struct file *file,
2370 char __user *buf, size_t nbytes,
2371 loff_t *ppos)
2372{
Paul Menage84eea842008-07-25 01:47:00 -07002373 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002374 s64 val = cft->read_s64(cgrp, cft);
2375 int len = sprintf(tmp, "%lld\n", (long long) val);
2376
2377 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2378}
2379
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2381 size_t nbytes, loff_t *ppos)
2382{
2383 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002384 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385
Tejun Heo54766d42013-06-12 21:04:53 -07002386 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387 return -ENODEV;
2388
2389 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002390 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002391 if (cft->read_u64)
2392 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002393 if (cft->read_s64)
2394 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395 return -EINVAL;
2396}
2397
Paul Menage91796562008-04-29 01:00:01 -07002398/*
2399 * seqfile ops/methods for returning structured data. Currently just
2400 * supports string->u64 maps, but can be extended in future.
2401 */
2402
Paul Menage91796562008-04-29 01:00:01 -07002403static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2404{
2405 struct seq_file *sf = cb->state;
2406 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2407}
2408
2409static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2410{
Li Zefane0798ce2013-07-31 17:36:25 +08002411 struct cfent *cfe = m->private;
2412 struct cftype *cft = cfe->type;
2413 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2414
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002415 if (cft->read_map) {
2416 struct cgroup_map_cb cb = {
2417 .fill = cgroup_map_add,
2418 .state = m,
2419 };
Li Zefane0798ce2013-07-31 17:36:25 +08002420 return cft->read_map(cgrp, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002421 }
Li Zefane0798ce2013-07-31 17:36:25 +08002422 return cft->read_seq_string(cgrp, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002423}
2424
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002425static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002426 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002427 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002428 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002429 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002430};
2431
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432static int cgroup_file_open(struct inode *inode, struct file *file)
2433{
2434 int err;
Li Zefane0798ce2013-07-31 17:36:25 +08002435 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002436 struct cftype *cft;
2437
2438 err = generic_file_open(inode, file);
2439 if (err)
2440 return err;
Li Zefane0798ce2013-07-31 17:36:25 +08002441 cfe = __d_cfe(file->f_dentry);
2442 cft = cfe->type;
Li Zefan75139b82009-01-07 18:07:33 -08002443
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002444 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002445 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002446 err = single_open(file, cgroup_seqfile_show, cfe);
2447 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002448 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002449 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002450
2451 return err;
2452}
2453
2454static int cgroup_file_release(struct inode *inode, struct file *file)
2455{
2456 struct cftype *cft = __d_cft(file->f_dentry);
2457 if (cft->release)
2458 return cft->release(inode, file);
2459 return 0;
2460}
2461
2462/*
2463 * cgroup_rename - Only allow simple rename of directories in place.
2464 */
2465static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2466 struct inode *new_dir, struct dentry *new_dentry)
2467{
Li Zefan65dff752013-03-01 15:01:56 +08002468 int ret;
2469 struct cgroup_name *name, *old_name;
2470 struct cgroup *cgrp;
2471
2472 /*
2473 * It's convinient to use parent dir's i_mutex to protected
2474 * cgrp->name.
2475 */
2476 lockdep_assert_held(&old_dir->i_mutex);
2477
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2479 return -ENOTDIR;
2480 if (new_dentry->d_inode)
2481 return -EEXIST;
2482 if (old_dir != new_dir)
2483 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002484
2485 cgrp = __d_cgrp(old_dentry);
2486
Tejun Heo6db8e852013-06-14 11:18:22 -07002487 /*
2488 * This isn't a proper migration and its usefulness is very
2489 * limited. Disallow if sane_behavior.
2490 */
2491 if (cgroup_sane_behavior(cgrp))
2492 return -EPERM;
2493
Li Zefan65dff752013-03-01 15:01:56 +08002494 name = cgroup_alloc_name(new_dentry);
2495 if (!name)
2496 return -ENOMEM;
2497
2498 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2499 if (ret) {
2500 kfree(name);
2501 return ret;
2502 }
2503
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002504 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002505 rcu_assign_pointer(cgrp->name, name);
2506
2507 kfree_rcu(old_name, rcu_head);
2508 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509}
2510
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002511static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2512{
2513 if (S_ISDIR(dentry->d_inode->i_mode))
2514 return &__d_cgrp(dentry)->xattrs;
2515 else
Li Zefan712317a2013-04-18 23:09:52 -07002516 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002517}
2518
2519static inline int xattr_enabled(struct dentry *dentry)
2520{
2521 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002522 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002523}
2524
2525static bool is_valid_xattr(const char *name)
2526{
2527 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2528 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2529 return true;
2530 return false;
2531}
2532
2533static int cgroup_setxattr(struct dentry *dentry, const char *name,
2534 const void *val, size_t size, int flags)
2535{
2536 if (!xattr_enabled(dentry))
2537 return -EOPNOTSUPP;
2538 if (!is_valid_xattr(name))
2539 return -EINVAL;
2540 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2541}
2542
2543static int cgroup_removexattr(struct dentry *dentry, const char *name)
2544{
2545 if (!xattr_enabled(dentry))
2546 return -EOPNOTSUPP;
2547 if (!is_valid_xattr(name))
2548 return -EINVAL;
2549 return simple_xattr_remove(__d_xattrs(dentry), name);
2550}
2551
2552static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2553 void *buf, size_t size)
2554{
2555 if (!xattr_enabled(dentry))
2556 return -EOPNOTSUPP;
2557 if (!is_valid_xattr(name))
2558 return -EINVAL;
2559 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2560}
2561
2562static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2563{
2564 if (!xattr_enabled(dentry))
2565 return -EOPNOTSUPP;
2566 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2567}
2568
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002569static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570 .read = cgroup_file_read,
2571 .write = cgroup_file_write,
2572 .llseek = generic_file_llseek,
2573 .open = cgroup_file_open,
2574 .release = cgroup_file_release,
2575};
2576
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002577static const struct inode_operations cgroup_file_inode_operations = {
2578 .setxattr = cgroup_setxattr,
2579 .getxattr = cgroup_getxattr,
2580 .listxattr = cgroup_listxattr,
2581 .removexattr = cgroup_removexattr,
2582};
2583
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002584static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002585 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586 .mkdir = cgroup_mkdir,
2587 .rmdir = cgroup_rmdir,
2588 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002589 .setxattr = cgroup_setxattr,
2590 .getxattr = cgroup_getxattr,
2591 .listxattr = cgroup_listxattr,
2592 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002593};
2594
Al Viro00cd8dd2012-06-10 17:13:09 -04002595static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002596{
2597 if (dentry->d_name.len > NAME_MAX)
2598 return ERR_PTR(-ENAMETOOLONG);
2599 d_add(dentry, NULL);
2600 return NULL;
2601}
2602
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002603/*
2604 * Check if a file is a control file
2605 */
2606static inline struct cftype *__file_cft(struct file *file)
2607{
Al Viro496ad9a2013-01-23 17:07:38 -05002608 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002609 return ERR_PTR(-EINVAL);
2610 return __d_cft(file->f_dentry);
2611}
2612
Al Viroa5e7ed32011-07-26 01:55:55 -04002613static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002614 struct super_block *sb)
2615{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 struct inode *inode;
2617
2618 if (!dentry)
2619 return -ENOENT;
2620 if (dentry->d_inode)
2621 return -EEXIST;
2622
2623 inode = cgroup_new_inode(mode, sb);
2624 if (!inode)
2625 return -ENOMEM;
2626
2627 if (S_ISDIR(mode)) {
2628 inode->i_op = &cgroup_dir_inode_operations;
2629 inode->i_fop = &simple_dir_operations;
2630
2631 /* start off with i_nlink == 2 (for "." entry) */
2632 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002633 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002634
Tejun Heob8a2df62012-11-19 08:13:37 -08002635 /*
2636 * Control reaches here with cgroup_mutex held.
2637 * @inode->i_mutex should nest outside cgroup_mutex but we
2638 * want to populate it immediately without releasing
2639 * cgroup_mutex. As @inode isn't visible to anyone else
2640 * yet, trylock will always succeed without affecting
2641 * lockdep checks.
2642 */
2643 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002644 } else if (S_ISREG(mode)) {
2645 inode->i_size = 0;
2646 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002647 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 d_instantiate(dentry, inode);
2650 dget(dentry); /* Extra count - pin the dentry in core */
2651 return 0;
2652}
2653
Li Zefan099fca32009-04-02 16:57:29 -07002654/**
2655 * cgroup_file_mode - deduce file mode of a control file
2656 * @cft: the control file in question
2657 *
2658 * returns cft->mode if ->mode is not 0
2659 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2660 * returns S_IRUGO if it has only a read handler
2661 * returns S_IWUSR if it has only a write hander
2662 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002663static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002664{
Al Viroa5e7ed32011-07-26 01:55:55 -04002665 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002666
2667 if (cft->mode)
2668 return cft->mode;
2669
2670 if (cft->read || cft->read_u64 || cft->read_s64 ||
2671 cft->read_map || cft->read_seq_string)
2672 mode |= S_IRUGO;
2673
2674 if (cft->write || cft->write_u64 || cft->write_s64 ||
2675 cft->write_string || cft->trigger)
2676 mode |= S_IWUSR;
2677
2678 return mode;
2679}
2680
Tejun Heodb0416b2012-04-01 12:09:55 -07002681static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002682 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683{
Paul Menagebd89aab2007-10-18 23:40:44 -07002684 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002685 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002687 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002689 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002691
Tejun Heo93438622013-04-14 20:15:25 -07002692 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693 strcpy(name, subsys->name);
2694 strcat(name, ".");
2695 }
2696 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002697
Paul Menageddbcc7e2007-10-18 23:39:30 -07002698 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002699
2700 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2701 if (!cfe)
2702 return -ENOMEM;
2703
Paul Menageddbcc7e2007-10-18 23:39:30 -07002704 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002705 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002707 goto out;
2708 }
2709
Li Zefand6cbf352013-05-14 19:44:20 +08002710 cfe->type = (void *)cft;
2711 cfe->dentry = dentry;
2712 dentry->d_fsdata = cfe;
2713 simple_xattrs_init(&cfe->xattrs);
2714
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715 mode = cgroup_file_mode(cft);
2716 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2717 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002718 list_add_tail(&cfe->node, &parent->files);
2719 cfe = NULL;
2720 }
2721 dput(dentry);
2722out:
2723 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 return error;
2725}
2726
Tejun Heob1f28d32013-06-28 16:24:10 -07002727/**
2728 * cgroup_addrm_files - add or remove files to a cgroup directory
2729 * @cgrp: the target cgroup
2730 * @subsys: the subsystem of files to be added
2731 * @cfts: array of cftypes to be added
2732 * @is_add: whether to add or remove
2733 *
2734 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2735 * All @cfts should belong to @subsys. For removals, this function never
2736 * fails. If addition fails, this function doesn't remove files already
2737 * added. The caller is responsible for cleaning up.
2738 */
Tejun Heo79578622012-04-01 12:09:56 -07002739static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002740 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002742 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002743 int ret;
2744
2745 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2746 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002747
2748 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002749 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002750 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2751 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002752 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2753 continue;
2754 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2755 continue;
2756
Li Zefan2739d3c2013-01-21 18:18:33 +08002757 if (is_add) {
Tejun Heob1f28d32013-06-28 16:24:10 -07002758 ret = cgroup_add_file(cgrp, subsys, cft);
2759 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002760 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002761 cft->name, ret);
2762 return ret;
2763 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002764 } else {
2765 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002766 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002767 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002768 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769}
2770
Tejun Heo8e3f6542012-04-01 12:09:55 -07002771static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002772 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002773{
2774 /*
2775 * Thanks to the entanglement with vfs inode locking, we can't walk
2776 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002777 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2778 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002779 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002780 mutex_lock(&cgroup_mutex);
2781}
2782
Tejun Heo9ccece82013-06-28 16:24:11 -07002783static int cgroup_cfts_commit(struct cgroup_subsys *ss,
2784 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002785 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002786{
2787 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002788 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002789 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002790 struct dentry *prev = NULL;
2791 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002792 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002793 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002794
2795 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002796 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002797 !atomic_inc_not_zero(&sb->s_active)) {
2798 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002799 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002800 }
2801
Li Zefane8c82d22013-06-18 18:48:37 +08002802 /*
2803 * All cgroups which are created after we drop cgroup_mutex will
2804 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002805 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002806 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002807 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002808
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809 mutex_unlock(&cgroup_mutex);
2810
Li Zefane8c82d22013-06-18 18:48:37 +08002811 /* @root always needs to be updated */
2812 inode = root->dentry->d_inode;
2813 mutex_lock(&inode->i_mutex);
2814 mutex_lock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002815 ret = cgroup_addrm_files(root, ss, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002816 mutex_unlock(&cgroup_mutex);
2817 mutex_unlock(&inode->i_mutex);
2818
Tejun Heo9ccece82013-06-28 16:24:11 -07002819 if (ret)
2820 goto out_deact;
2821
Li Zefane8c82d22013-06-18 18:48:37 +08002822 /* add/rm files for all cgroups created before */
2823 rcu_read_lock();
2824 cgroup_for_each_descendant_pre(cgrp, root) {
2825 if (cgroup_is_dead(cgrp))
2826 continue;
2827
2828 inode = cgrp->dentry->d_inode;
2829 dget(cgrp->dentry);
2830 rcu_read_unlock();
2831
2832 dput(prev);
2833 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834
2835 mutex_lock(&inode->i_mutex);
2836 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002837 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo9ccece82013-06-28 16:24:11 -07002838 ret = cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002839 mutex_unlock(&cgroup_mutex);
2840 mutex_unlock(&inode->i_mutex);
2841
Li Zefane8c82d22013-06-18 18:48:37 +08002842 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002843 if (ret)
2844 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002845 }
Li Zefane8c82d22013-06-18 18:48:37 +08002846 rcu_read_unlock();
2847 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002848out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002849 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002850 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002851}
2852
2853/**
2854 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2855 * @ss: target cgroup subsystem
2856 * @cfts: zero-length name terminated array of cftypes
2857 *
2858 * Register @cfts to @ss. Files described by @cfts are created for all
2859 * existing cgroups to which @ss is attached and all future cgroups will
2860 * have them too. This function can be called anytime whether @ss is
2861 * attached or not.
2862 *
2863 * Returns 0 on successful registration, -errno on failure. Note that this
2864 * function currently returns 0 as long as @cfts registration is successful
2865 * even if some file creation attempts on existing cgroups fail.
2866 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002867int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002868{
2869 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002870 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871
2872 set = kzalloc(sizeof(*set), GFP_KERNEL);
2873 if (!set)
2874 return -ENOMEM;
2875
2876 cgroup_cfts_prepare();
2877 set->cfts = cfts;
2878 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo9ccece82013-06-28 16:24:11 -07002879 ret = cgroup_cfts_commit(ss, cfts, true);
2880 if (ret)
2881 cgroup_rm_cftypes(ss, cfts);
2882 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002883}
2884EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2885
Li Zefana043e3b2008-02-23 15:24:09 -08002886/**
Tejun Heo79578622012-04-01 12:09:56 -07002887 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2888 * @ss: target cgroup subsystem
2889 * @cfts: zero-length name terminated array of cftypes
2890 *
2891 * Unregister @cfts from @ss. Files described by @cfts are removed from
2892 * all existing cgroups to which @ss is attached and all future cgroups
2893 * won't have them either. This function can be called anytime whether @ss
2894 * is attached or not.
2895 *
2896 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2897 * registered with @ss.
2898 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002899int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002900{
2901 struct cftype_set *set;
2902
2903 cgroup_cfts_prepare();
2904
2905 list_for_each_entry(set, &ss->cftsets, node) {
2906 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002907 list_del(&set->node);
2908 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002909 cgroup_cfts_commit(ss, cfts, false);
2910 return 0;
2911 }
2912 }
2913
2914 cgroup_cfts_commit(ss, NULL, false);
2915 return -ENOENT;
2916}
2917
2918/**
Li Zefana043e3b2008-02-23 15:24:09 -08002919 * cgroup_task_count - count the number of tasks in a cgroup.
2920 * @cgrp: the cgroup in question
2921 *
2922 * Return the number of tasks in the cgroup.
2923 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002924int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002925{
2926 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002927 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002928
Paul Menage817929e2007-10-18 23:39:36 -07002929 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002930 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2931 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002932 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002933 return count;
2934}
2935
2936/*
Paul Menage817929e2007-10-18 23:39:36 -07002937 * Advance a list_head iterator. The iterator should be positioned at
2938 * the start of a css_set
2939 */
Tejun Heo69d02062013-06-12 21:04:50 -07002940static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002941{
Tejun Heo69d02062013-06-12 21:04:50 -07002942 struct list_head *l = it->cset_link;
2943 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002944 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002945
2946 /* Advance to the next non-empty css_set */
2947 do {
2948 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002949 if (l == &cgrp->cset_links) {
2950 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002951 return;
2952 }
Tejun Heo69d02062013-06-12 21:04:50 -07002953 link = list_entry(l, struct cgrp_cset_link, cset_link);
2954 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002955 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002956 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002957 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002958}
2959
Cliff Wickman31a7df02008-02-07 00:14:42 -08002960/*
2961 * To reduce the fork() overhead for systems that are not actually
2962 * using their cgroups capability, we don't maintain the lists running
2963 * through each css_set to its tasks until we see the list actually
2964 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002966static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967{
2968 struct task_struct *p, *g;
2969 write_lock(&css_set_lock);
2970 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002971 /*
2972 * We need tasklist_lock because RCU is not safe against
2973 * while_each_thread(). Besides, a forking task that has passed
2974 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2975 * is not guaranteed to have its child immediately visible in the
2976 * tasklist if we walk through it with RCU.
2977 */
2978 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979 do_each_thread(g, p) {
2980 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002981 /*
2982 * We should check if the process is exiting, otherwise
2983 * it will race with cgroup_exit() in that the list
2984 * entry won't be deleted though the process has exited.
2985 */
2986 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002987 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002988 task_unlock(p);
2989 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002990 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991 write_unlock(&css_set_lock);
2992}
2993
Tejun Heo574bd9f2012-11-09 09:12:29 -08002994/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002995 * cgroup_next_sibling - find the next sibling of a given cgroup
2996 * @pos: the current cgroup
2997 *
2998 * This function returns the next sibling of @pos and should be called
2999 * under RCU read lock. The only requirement is that @pos is accessible.
3000 * The next sibling is guaranteed to be returned regardless of @pos's
3001 * state.
3002 */
3003struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3004{
3005 struct cgroup *next;
3006
3007 WARN_ON_ONCE(!rcu_read_lock_held());
3008
3009 /*
3010 * @pos could already have been removed. Once a cgroup is removed,
3011 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003012 * changes. As CGRP_DEAD assertion is serialized and happens
3013 * before the cgroup is taken off the ->sibling list, if we see it
3014 * unasserted, it's guaranteed that the next sibling hasn't
3015 * finished its grace period even if it's already removed, and thus
3016 * safe to dereference from this RCU critical section. If
3017 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3018 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003019 */
Tejun Heo54766d42013-06-12 21:04:53 -07003020 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003021 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3022 if (&next->sibling != &pos->parent->children)
3023 return next;
3024 return NULL;
3025 }
3026
3027 /*
3028 * Can't dereference the next pointer. Each cgroup is given a
3029 * monotonically increasing unique serial number and always
3030 * appended to the sibling list, so the next one can be found by
3031 * walking the parent's children until we see a cgroup with higher
3032 * serial number than @pos's.
3033 *
3034 * While this path can be slow, it's taken only when either the
3035 * current cgroup is removed or iteration and removal race.
3036 */
3037 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3038 if (next->serial_nr > pos->serial_nr)
3039 return next;
3040 return NULL;
3041}
3042EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3043
3044/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003045 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3046 * @pos: the current position (%NULL to initiate traversal)
3047 * @cgroup: cgroup whose descendants to walk
3048 *
3049 * To be used by cgroup_for_each_descendant_pre(). Find the next
3050 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003051 *
3052 * While this function requires RCU read locking, it doesn't require the
3053 * whole traversal to be contained in a single RCU critical section. This
3054 * function will return the correct next descendant as long as both @pos
3055 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003056 */
3057struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3058 struct cgroup *cgroup)
3059{
3060 struct cgroup *next;
3061
3062 WARN_ON_ONCE(!rcu_read_lock_held());
3063
3064 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003065 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003066 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067
3068 /* visit the first child if exists */
3069 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3070 if (next)
3071 return next;
3072
3073 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003074 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003075 next = cgroup_next_sibling(pos);
3076 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003079 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003080
3081 return NULL;
3082}
3083EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3084
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003085/**
3086 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3087 * @pos: cgroup of interest
3088 *
3089 * Return the rightmost descendant of @pos. If there's no descendant,
3090 * @pos is returned. This can be used during pre-order traversal to skip
3091 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003092 *
3093 * While this function requires RCU read locking, it doesn't require the
3094 * whole traversal to be contained in a single RCU critical section. This
3095 * function will return the correct rightmost descendant as long as @pos is
3096 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003097 */
3098struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3099{
3100 struct cgroup *last, *tmp;
3101
3102 WARN_ON_ONCE(!rcu_read_lock_held());
3103
3104 do {
3105 last = pos;
3106 /* ->prev isn't RCU safe, walk ->next till the end */
3107 pos = NULL;
3108 list_for_each_entry_rcu(tmp, &last->children, sibling)
3109 pos = tmp;
3110 } while (pos);
3111
3112 return last;
3113}
3114EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3115
Tejun Heo574bd9f2012-11-09 09:12:29 -08003116static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3117{
3118 struct cgroup *last;
3119
3120 do {
3121 last = pos;
3122 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3123 sibling);
3124 } while (pos);
3125
3126 return last;
3127}
3128
3129/**
3130 * cgroup_next_descendant_post - find the next descendant for post-order walk
3131 * @pos: the current position (%NULL to initiate traversal)
3132 * @cgroup: cgroup whose descendants to walk
3133 *
3134 * To be used by cgroup_for_each_descendant_post(). Find the next
3135 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003136 *
3137 * While this function requires RCU read locking, it doesn't require the
3138 * whole traversal to be contained in a single RCU critical section. This
3139 * function will return the correct next descendant as long as both @pos
3140 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003141 */
3142struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3143 struct cgroup *cgroup)
3144{
3145 struct cgroup *next;
3146
3147 WARN_ON_ONCE(!rcu_read_lock_held());
3148
3149 /* if first iteration, visit the leftmost descendant */
3150 if (!pos) {
3151 next = cgroup_leftmost_descendant(cgroup);
3152 return next != cgroup ? next : NULL;
3153 }
3154
3155 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003156 next = cgroup_next_sibling(pos);
3157 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158 return cgroup_leftmost_descendant(next);
3159
3160 /* no sibling left, visit parent */
3161 next = pos->parent;
3162 return next != cgroup ? next : NULL;
3163}
3164EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3165
Paul Menagebd89aab2007-10-18 23:40:44 -07003166void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003167 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003168{
3169 /*
3170 * The first time anyone tries to iterate across a cgroup,
3171 * we need to enable the list linking each css_set to its
3172 * tasks, and fix up all existing tasks.
3173 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003174 if (!use_task_css_set_links)
3175 cgroup_enable_task_cg_lists();
3176
Paul Menage817929e2007-10-18 23:39:36 -07003177 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003178 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003179 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003180}
3181
Paul Menagebd89aab2007-10-18 23:40:44 -07003182struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003183 struct cgroup_iter *it)
3184{
3185 struct task_struct *res;
3186 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003187 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003188
3189 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003190 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003191 return NULL;
3192 res = list_entry(l, struct task_struct, cg_list);
3193 /* Advance iterator to find next entry */
3194 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003195 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3196 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003197 /* We reached the end of this task list - move on to
3198 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003199 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003200 } else {
3201 it->task = l;
3202 }
3203 return res;
3204}
3205
Paul Menagebd89aab2007-10-18 23:40:44 -07003206void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003207 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003208{
3209 read_unlock(&css_set_lock);
3210}
3211
Cliff Wickman31a7df02008-02-07 00:14:42 -08003212static inline int started_after_time(struct task_struct *t1,
3213 struct timespec *time,
3214 struct task_struct *t2)
3215{
3216 int start_diff = timespec_compare(&t1->start_time, time);
3217 if (start_diff > 0) {
3218 return 1;
3219 } else if (start_diff < 0) {
3220 return 0;
3221 } else {
3222 /*
3223 * Arbitrarily, if two processes started at the same
3224 * time, we'll say that the lower pointer value
3225 * started first. Note that t2 may have exited by now
3226 * so this may not be a valid pointer any longer, but
3227 * that's fine - it still serves to distinguish
3228 * between two tasks started (effectively) simultaneously.
3229 */
3230 return t1 > t2;
3231 }
3232}
3233
3234/*
3235 * This function is a callback from heap_insert() and is used to order
3236 * the heap.
3237 * In this case we order the heap in descending task start time.
3238 */
3239static inline int started_after(void *p1, void *p2)
3240{
3241 struct task_struct *t1 = p1;
3242 struct task_struct *t2 = p2;
3243 return started_after_time(t1, &t2->start_time, t2);
3244}
3245
3246/**
3247 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3248 * @scan: struct cgroup_scanner containing arguments for the scan
3249 *
3250 * Arguments include pointers to callback functions test_task() and
3251 * process_task().
3252 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3253 * and if it returns true, call process_task() for it also.
3254 * The test_task pointer may be NULL, meaning always true (select all tasks).
3255 * Effectively duplicates cgroup_iter_{start,next,end}()
3256 * but does not lock css_set_lock for the call to process_task().
3257 * The struct cgroup_scanner may be embedded in any structure of the caller's
3258 * creation.
3259 * It is guaranteed that process_task() will act on every task that
3260 * is a member of the cgroup for the duration of this call. This
3261 * function may or may not call process_task() for tasks that exit
3262 * or move to a different cgroup during the call, or are forked or
3263 * move into the cgroup during the call.
3264 *
3265 * Note that test_task() may be called with locks held, and may in some
3266 * situations be called multiple times for the same task, so it should
3267 * be cheap.
3268 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3269 * pre-allocated and will be used for heap operations (and its "gt" member will
3270 * be overwritten), else a temporary heap will be used (allocation of which
3271 * may cause this function to fail).
3272 */
3273int cgroup_scan_tasks(struct cgroup_scanner *scan)
3274{
3275 int retval, i;
3276 struct cgroup_iter it;
3277 struct task_struct *p, *dropped;
3278 /* Never dereference latest_task, since it's not refcounted */
3279 struct task_struct *latest_task = NULL;
3280 struct ptr_heap tmp_heap;
3281 struct ptr_heap *heap;
3282 struct timespec latest_time = { 0, 0 };
3283
3284 if (scan->heap) {
3285 /* The caller supplied our heap and pre-allocated its memory */
3286 heap = scan->heap;
3287 heap->gt = &started_after;
3288 } else {
3289 /* We need to allocate our own heap memory */
3290 heap = &tmp_heap;
3291 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3292 if (retval)
3293 /* cannot allocate the heap */
3294 return retval;
3295 }
3296
3297 again:
3298 /*
3299 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3300 * to determine which are of interest, and using the scanner's
3301 * "process_task" callback to process any of them that need an update.
3302 * Since we don't want to hold any locks during the task updates,
3303 * gather tasks to be processed in a heap structure.
3304 * The heap is sorted by descending task start time.
3305 * If the statically-sized heap fills up, we overflow tasks that
3306 * started later, and in future iterations only consider tasks that
3307 * started after the latest task in the previous pass. This
3308 * guarantees forward progress and that we don't miss any tasks.
3309 */
3310 heap->size = 0;
Li Zefan6f4b7e62013-07-31 16:18:36 +08003311 cgroup_iter_start(scan->cgrp, &it);
3312 while ((p = cgroup_iter_next(scan->cgrp, &it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003313 /*
3314 * Only affect tasks that qualify per the caller's callback,
3315 * if he provided one
3316 */
3317 if (scan->test_task && !scan->test_task(p, scan))
3318 continue;
3319 /*
3320 * Only process tasks that started after the last task
3321 * we processed
3322 */
3323 if (!started_after_time(p, &latest_time, latest_task))
3324 continue;
3325 dropped = heap_insert(heap, p);
3326 if (dropped == NULL) {
3327 /*
3328 * The new task was inserted; the heap wasn't
3329 * previously full
3330 */
3331 get_task_struct(p);
3332 } else if (dropped != p) {
3333 /*
3334 * The new task was inserted, and pushed out a
3335 * different task
3336 */
3337 get_task_struct(p);
3338 put_task_struct(dropped);
3339 }
3340 /*
3341 * Else the new task was newer than anything already in
3342 * the heap and wasn't inserted
3343 */
3344 }
Li Zefan6f4b7e62013-07-31 16:18:36 +08003345 cgroup_iter_end(scan->cgrp, &it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003346
3347 if (heap->size) {
3348 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003349 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003351 latest_time = q->start_time;
3352 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003353 }
3354 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003355 scan->process_task(q, scan);
3356 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 }
3358 /*
3359 * If we had to process any tasks at all, scan again
3360 * in case some of them were in the middle of forking
3361 * children that didn't get processed.
3362 * Not the most efficient way to do it, but it avoids
3363 * having to take callback_mutex in the fork path
3364 */
3365 goto again;
3366 }
3367 if (heap == &tmp_heap)
3368 heap_free(&tmp_heap);
3369 return 0;
3370}
3371
Tejun Heo8cc99342013-04-07 09:29:50 -07003372static void cgroup_transfer_one_task(struct task_struct *task,
3373 struct cgroup_scanner *scan)
3374{
3375 struct cgroup *new_cgroup = scan->data;
3376
Tejun Heo47cfcd02013-04-07 09:29:51 -07003377 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003378 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003379 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003380}
3381
3382/**
3383 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3384 * @to: cgroup to which the tasks will be moved
3385 * @from: cgroup in which the tasks currently reside
3386 */
3387int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3388{
3389 struct cgroup_scanner scan;
3390
Li Zefan6f4b7e62013-07-31 16:18:36 +08003391 scan.cgrp = from;
Tejun Heo8cc99342013-04-07 09:29:50 -07003392 scan.test_task = NULL; /* select all tasks in cgroup */
3393 scan.process_task = cgroup_transfer_one_task;
3394 scan.heap = NULL;
3395 scan.data = to;
3396
3397 return cgroup_scan_tasks(&scan);
3398}
3399
Paul Menage817929e2007-10-18 23:39:36 -07003400/*
Ben Blum102a7752009-09-23 15:56:26 -07003401 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003402 *
3403 * Reading this file can return large amounts of data if a cgroup has
3404 * *lots* of attached tasks. So it may need several calls to read(),
3405 * but we cannot guarantee that the information we produce is correct
3406 * unless we produce it entirely atomically.
3407 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003408 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003409
Li Zefan24528252012-01-20 11:58:43 +08003410/* which pidlist file are we talking about? */
3411enum cgroup_filetype {
3412 CGROUP_FILE_PROCS,
3413 CGROUP_FILE_TASKS,
3414};
3415
3416/*
3417 * A pidlist is a list of pids that virtually represents the contents of one
3418 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3419 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3420 * to the cgroup.
3421 */
3422struct cgroup_pidlist {
3423 /*
3424 * used to find which pidlist is wanted. doesn't change as long as
3425 * this particular list stays in the list.
3426 */
3427 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3428 /* array of xids */
3429 pid_t *list;
3430 /* how many elements the above list has */
3431 int length;
3432 /* how many files are using the current array */
3433 int use_count;
3434 /* each of these stored in a list by its cgroup */
3435 struct list_head links;
3436 /* pointer to the cgroup we belong to, for list removal purposes */
3437 struct cgroup *owner;
3438 /* protects the other fields */
3439 struct rw_semaphore mutex;
3440};
3441
Paul Menagebbcb81d2007-10-18 23:39:32 -07003442/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003443 * The following two functions "fix" the issue where there are more pids
3444 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3445 * TODO: replace with a kernel-wide solution to this problem
3446 */
3447#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3448static void *pidlist_allocate(int count)
3449{
3450 if (PIDLIST_TOO_LARGE(count))
3451 return vmalloc(count * sizeof(pid_t));
3452 else
3453 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3454}
3455static void pidlist_free(void *p)
3456{
3457 if (is_vmalloc_addr(p))
3458 vfree(p);
3459 else
3460 kfree(p);
3461}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003462
3463/*
Ben Blum102a7752009-09-23 15:56:26 -07003464 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003465 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003466 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003467static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003468{
Ben Blum102a7752009-09-23 15:56:26 -07003469 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003470
3471 /*
3472 * we presume the 0th element is unique, so i starts at 1. trivial
3473 * edge cases first; no work needs to be done for either
3474 */
3475 if (length == 0 || length == 1)
3476 return length;
3477 /* src and dest walk down the list; dest counts unique elements */
3478 for (src = 1; src < length; src++) {
3479 /* find next unique element */
3480 while (list[src] == list[src-1]) {
3481 src++;
3482 if (src == length)
3483 goto after;
3484 }
3485 /* dest always points to where the next unique element goes */
3486 list[dest] = list[src];
3487 dest++;
3488 }
3489after:
Ben Blum102a7752009-09-23 15:56:26 -07003490 return dest;
3491}
3492
3493static int cmppid(const void *a, const void *b)
3494{
3495 return *(pid_t *)a - *(pid_t *)b;
3496}
3497
3498/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003499 * find the appropriate pidlist for our purpose (given procs vs tasks)
3500 * returns with the lock on that pidlist already held, and takes care
3501 * of the use count, or returns NULL with no locks held if we're out of
3502 * memory.
3503 */
3504static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3505 enum cgroup_filetype type)
3506{
3507 struct cgroup_pidlist *l;
3508 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003509 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003510
Ben Blum72a8cb32009-09-23 15:56:27 -07003511 /*
3512 * We can't drop the pidlist_mutex before taking the l->mutex in case
3513 * the last ref-holder is trying to remove l from the list at the same
3514 * time. Holding the pidlist_mutex precludes somebody taking whichever
3515 * list we find out from under us - compare release_pid_array().
3516 */
3517 mutex_lock(&cgrp->pidlist_mutex);
3518 list_for_each_entry(l, &cgrp->pidlists, links) {
3519 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 /* make sure l doesn't vanish out from under us */
3521 down_write(&l->mutex);
3522 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003523 return l;
3524 }
3525 }
3526 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003527 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003528 if (!l) {
3529 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 return l;
3531 }
3532 init_rwsem(&l->mutex);
3533 down_write(&l->mutex);
3534 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003535 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003536 l->owner = cgrp;
3537 list_add(&l->links, &cgrp->pidlists);
3538 mutex_unlock(&cgrp->pidlist_mutex);
3539 return l;
3540}
3541
3542/*
Ben Blum102a7752009-09-23 15:56:26 -07003543 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3544 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003545static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3546 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003547{
3548 pid_t *array;
3549 int length;
3550 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003551 struct cgroup_iter it;
3552 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003553 struct cgroup_pidlist *l;
3554
3555 /*
3556 * If cgroup gets more users after we read count, we won't have
3557 * enough space - tough. This race is indistinguishable to the
3558 * caller from the case that the additional cgroup users didn't
3559 * show up until sometime later on.
3560 */
3561 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003562 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003563 if (!array)
3564 return -ENOMEM;
3565 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003566 cgroup_iter_start(cgrp, &it);
3567 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003568 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003569 break;
Ben Blum102a7752009-09-23 15:56:26 -07003570 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003571 if (type == CGROUP_FILE_PROCS)
3572 pid = task_tgid_vnr(tsk);
3573 else
3574 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003575 if (pid > 0) /* make sure to only use valid results */
3576 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003577 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003578 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003579 length = n;
3580 /* now sort & (if procs) strip out duplicates */
3581 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003583 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 l = cgroup_pidlist_find(cgrp, type);
3585 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003586 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003587 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003588 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003589 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003590 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003591 l->list = array;
3592 l->length = length;
3593 l->use_count++;
3594 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003596 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003597}
3598
Balbir Singh846c7bb2007-10-18 23:39:44 -07003599/**
Li Zefana043e3b2008-02-23 15:24:09 -08003600 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003601 * @stats: cgroupstats to fill information into
3602 * @dentry: A dentry entry belonging to the cgroup for which stats have
3603 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003604 *
3605 * Build and fill cgroupstats so that taskstats can export it to user
3606 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003607 */
3608int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3609{
3610 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003611 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003612 struct cgroup_iter it;
3613 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003614
Balbir Singh846c7bb2007-10-18 23:39:44 -07003615 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003616 * Validate dentry by checking the superblock operations,
3617 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003618 */
Li Zefan33d283b2008-11-19 15:36:48 -08003619 if (dentry->d_sb->s_op != &cgroup_ops ||
3620 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003621 goto err;
3622
3623 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003624 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625
Paul Menagebd89aab2007-10-18 23:40:44 -07003626 cgroup_iter_start(cgrp, &it);
3627 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003628 switch (tsk->state) {
3629 case TASK_RUNNING:
3630 stats->nr_running++;
3631 break;
3632 case TASK_INTERRUPTIBLE:
3633 stats->nr_sleeping++;
3634 break;
3635 case TASK_UNINTERRUPTIBLE:
3636 stats->nr_uninterruptible++;
3637 break;
3638 case TASK_STOPPED:
3639 stats->nr_stopped++;
3640 break;
3641 default:
3642 if (delayacct_is_task_waiting_on_io(tsk))
3643 stats->nr_io_wait++;
3644 break;
3645 }
3646 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003647 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003648
Balbir Singh846c7bb2007-10-18 23:39:44 -07003649err:
3650 return ret;
3651}
3652
Paul Menage8f3ff202009-09-23 15:56:25 -07003653
Paul Menagecc31edc2008-10-18 20:28:04 -07003654/*
Ben Blum102a7752009-09-23 15:56:26 -07003655 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003656 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003657 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 */
3659
Ben Blum102a7752009-09-23 15:56:26 -07003660static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003661{
3662 /*
3663 * Initially we receive a position value that corresponds to
3664 * one more than the last pid shown (or 0 on the first call or
3665 * after a seek to the start). Use a binary-search to find the
3666 * next pid to display, if any
3667 */
Ben Blum102a7752009-09-23 15:56:26 -07003668 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003669 int index = 0, pid = *pos;
3670 int *iter;
3671
Ben Blum102a7752009-09-23 15:56:26 -07003672 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003674 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003675
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 while (index < end) {
3677 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003678 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003679 index = mid;
3680 break;
Ben Blum102a7752009-09-23 15:56:26 -07003681 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 index = mid + 1;
3683 else
3684 end = mid;
3685 }
3686 }
3687 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003688 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 return NULL;
3690 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003691 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 *pos = *iter;
3693 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694}
3695
Ben Blum102a7752009-09-23 15:56:26 -07003696static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003697{
Ben Blum102a7752009-09-23 15:56:26 -07003698 struct cgroup_pidlist *l = s->private;
3699 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003700}
3701
Ben Blum102a7752009-09-23 15:56:26 -07003702static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003703{
Ben Blum102a7752009-09-23 15:56:26 -07003704 struct cgroup_pidlist *l = s->private;
3705 pid_t *p = v;
3706 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003707 /*
3708 * Advance to the next pid in the array. If this goes off the
3709 * end, we're done
3710 */
3711 p++;
3712 if (p >= end) {
3713 return NULL;
3714 } else {
3715 *pos = *p;
3716 return p;
3717 }
3718}
3719
Ben Blum102a7752009-09-23 15:56:26 -07003720static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003721{
3722 return seq_printf(s, "%d\n", *(int *)v);
3723}
3724
Ben Blum102a7752009-09-23 15:56:26 -07003725/*
3726 * seq_operations functions for iterating on pidlists through seq_file -
3727 * independent of whether it's tasks or procs
3728 */
3729static const struct seq_operations cgroup_pidlist_seq_operations = {
3730 .start = cgroup_pidlist_start,
3731 .stop = cgroup_pidlist_stop,
3732 .next = cgroup_pidlist_next,
3733 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003734};
3735
Ben Blum102a7752009-09-23 15:56:26 -07003736static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003737{
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 /*
3739 * the case where we're the last user of this particular pidlist will
3740 * have us remove it from the cgroup's list, which entails taking the
3741 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3742 * pidlist_mutex, we have to take pidlist_mutex first.
3743 */
3744 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003745 down_write(&l->mutex);
3746 BUG_ON(!l->use_count);
3747 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 /* we're the last user if refcount is 0; remove and free */
3749 list_del(&l->links);
3750 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003751 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003752 put_pid_ns(l->key.ns);
3753 up_write(&l->mutex);
3754 kfree(l);
3755 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003756 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003757 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003758 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003759}
3760
Ben Blum102a7752009-09-23 15:56:26 -07003761static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003762{
Ben Blum102a7752009-09-23 15:56:26 -07003763 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003764 if (!(file->f_mode & FMODE_READ))
3765 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003766 /*
3767 * the seq_file will only be initialized if the file was opened for
3768 * reading; hence we check if it's not null only in that case.
3769 */
3770 l = ((struct seq_file *)file->private_data)->private;
3771 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003772 return seq_release(inode, file);
3773}
3774
Ben Blum102a7752009-09-23 15:56:26 -07003775static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 .read = seq_read,
3777 .llseek = seq_lseek,
3778 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003779 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003780};
3781
3782/*
Ben Blum102a7752009-09-23 15:56:26 -07003783 * The following functions handle opens on a file that displays a pidlist
3784 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3785 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003786 */
Ben Blum102a7752009-09-23 15:56:26 -07003787/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003788static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003789{
3790 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003791 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003792 int retval;
3793
3794 /* Nothing to do for write-only files */
3795 if (!(file->f_mode & FMODE_READ))
3796 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003797
Ben Blum102a7752009-09-23 15:56:26 -07003798 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003799 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003800 if (retval)
3801 return retval;
3802 /* configure file information */
3803 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003804
Ben Blum102a7752009-09-23 15:56:26 -07003805 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003806 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003807 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003809 }
Ben Blum102a7752009-09-23 15:56:26 -07003810 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003811 return 0;
3812}
Ben Blum102a7752009-09-23 15:56:26 -07003813static int cgroup_tasks_open(struct inode *unused, struct file *file)
3814{
Ben Blum72a8cb32009-09-23 15:56:27 -07003815 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003816}
3817static int cgroup_procs_open(struct inode *unused, struct file *file)
3818{
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003820}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003821
Paul Menagebd89aab2007-10-18 23:40:44 -07003822static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003823 struct cftype *cft)
3824{
Paul Menagebd89aab2007-10-18 23:40:44 -07003825 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003826}
3827
Paul Menage6379c102008-07-25 01:47:01 -07003828static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3829 struct cftype *cft,
3830 u64 val)
3831{
3832 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3833 if (val)
3834 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3835 else
3836 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3837 return 0;
3838}
3839
Paul Menagebbcb81d2007-10-18 23:39:32 -07003840/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003841 * When dput() is called asynchronously, if umount has been done and
3842 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3843 * there's a small window that vfs will see the root dentry with non-zero
3844 * refcnt and trigger BUG().
3845 *
3846 * That's why we hold a reference before dput() and drop it right after.
3847 */
3848static void cgroup_dput(struct cgroup *cgrp)
3849{
3850 struct super_block *sb = cgrp->root->sb;
3851
3852 atomic_inc(&sb->s_active);
3853 dput(cgrp->dentry);
3854 deactivate_super(sb);
3855}
3856
3857/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003858 * Unregister event and free resources.
3859 *
3860 * Gets called from workqueue.
3861 */
3862static void cgroup_event_remove(struct work_struct *work)
3863{
3864 struct cgroup_event *event = container_of(work, struct cgroup_event,
3865 remove);
3866 struct cgroup *cgrp = event->cgrp;
3867
Li Zefan810cbee2013-02-18 18:56:14 +08003868 remove_wait_queue(event->wqh, &event->wait);
3869
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003870 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3871
Li Zefan810cbee2013-02-18 18:56:14 +08003872 /* Notify userspace the event is going away. */
3873 eventfd_signal(event->eventfd, 1);
3874
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003875 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003876 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003877 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003878}
3879
3880/*
3881 * Gets called on POLLHUP on eventfd when user closes it.
3882 *
3883 * Called with wqh->lock held and interrupts disabled.
3884 */
3885static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3886 int sync, void *key)
3887{
3888 struct cgroup_event *event = container_of(wait,
3889 struct cgroup_event, wait);
3890 struct cgroup *cgrp = event->cgrp;
3891 unsigned long flags = (unsigned long)key;
3892
3893 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003894 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003895 * If the event has been detached at cgroup removal, we
3896 * can simply return knowing the other side will cleanup
3897 * for us.
3898 *
3899 * We can't race against event freeing since the other
3900 * side will require wqh->lock via remove_wait_queue(),
3901 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003902 */
Li Zefan810cbee2013-02-18 18:56:14 +08003903 spin_lock(&cgrp->event_list_lock);
3904 if (!list_empty(&event->list)) {
3905 list_del_init(&event->list);
3906 /*
3907 * We are in atomic context, but cgroup_event_remove()
3908 * may sleep, so we have to call it in workqueue.
3909 */
3910 schedule_work(&event->remove);
3911 }
3912 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003913 }
3914
3915 return 0;
3916}
3917
3918static void cgroup_event_ptable_queue_proc(struct file *file,
3919 wait_queue_head_t *wqh, poll_table *pt)
3920{
3921 struct cgroup_event *event = container_of(pt,
3922 struct cgroup_event, pt);
3923
3924 event->wqh = wqh;
3925 add_wait_queue(wqh, &event->wait);
3926}
3927
3928/*
3929 * Parse input and register new cgroup event handler.
3930 *
3931 * Input must be in format '<event_fd> <control_fd> <args>'.
3932 * Interpretation of args is defined by control file implementation.
3933 */
3934static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3935 const char *buffer)
3936{
3937 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003938 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003939 unsigned int efd, cfd;
3940 struct file *efile = NULL;
3941 struct file *cfile = NULL;
3942 char *endp;
3943 int ret;
3944
3945 efd = simple_strtoul(buffer, &endp, 10);
3946 if (*endp != ' ')
3947 return -EINVAL;
3948 buffer = endp + 1;
3949
3950 cfd = simple_strtoul(buffer, &endp, 10);
3951 if ((*endp != ' ') && (*endp != '\0'))
3952 return -EINVAL;
3953 buffer = endp + 1;
3954
3955 event = kzalloc(sizeof(*event), GFP_KERNEL);
3956 if (!event)
3957 return -ENOMEM;
3958 event->cgrp = cgrp;
3959 INIT_LIST_HEAD(&event->list);
3960 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3961 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3962 INIT_WORK(&event->remove, cgroup_event_remove);
3963
3964 efile = eventfd_fget(efd);
3965 if (IS_ERR(efile)) {
3966 ret = PTR_ERR(efile);
3967 goto fail;
3968 }
3969
3970 event->eventfd = eventfd_ctx_fileget(efile);
3971 if (IS_ERR(event->eventfd)) {
3972 ret = PTR_ERR(event->eventfd);
3973 goto fail;
3974 }
3975
3976 cfile = fget(cfd);
3977 if (!cfile) {
3978 ret = -EBADF;
3979 goto fail;
3980 }
3981
3982 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003983 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003984 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 if (ret < 0)
3986 goto fail;
3987
3988 event->cft = __file_cft(cfile);
3989 if (IS_ERR(event->cft)) {
3990 ret = PTR_ERR(event->cft);
3991 goto fail;
3992 }
3993
Li Zefanf1690072013-02-18 14:13:35 +08003994 /*
3995 * The file to be monitored must be in the same cgroup as
3996 * cgroup.event_control is.
3997 */
3998 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3999 if (cgrp_cfile != cgrp) {
4000 ret = -EINVAL;
4001 goto fail;
4002 }
4003
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004004 if (!event->cft->register_event || !event->cft->unregister_event) {
4005 ret = -EINVAL;
4006 goto fail;
4007 }
4008
4009 ret = event->cft->register_event(cgrp, event->cft,
4010 event->eventfd, buffer);
4011 if (ret)
4012 goto fail;
4013
Li Zefan7ef70e42013-04-26 11:58:03 -07004014 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004015
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004016 /*
4017 * Events should be removed after rmdir of cgroup directory, but before
4018 * destroying subsystem state objects. Let's take reference to cgroup
4019 * directory dentry to do that.
4020 */
4021 dget(cgrp->dentry);
4022
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004023 spin_lock(&cgrp->event_list_lock);
4024 list_add(&event->list, &cgrp->event_list);
4025 spin_unlock(&cgrp->event_list_lock);
4026
4027 fput(cfile);
4028 fput(efile);
4029
4030 return 0;
4031
4032fail:
4033 if (cfile)
4034 fput(cfile);
4035
4036 if (event && event->eventfd && !IS_ERR(event->eventfd))
4037 eventfd_ctx_put(event->eventfd);
4038
4039 if (!IS_ERR_OR_NULL(efile))
4040 fput(efile);
4041
4042 kfree(event);
4043
4044 return ret;
4045}
4046
Daniel Lezcano97978e62010-10-27 15:33:35 -07004047static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4048 struct cftype *cft)
4049{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004050 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004051}
4052
4053static int cgroup_clone_children_write(struct cgroup *cgrp,
4054 struct cftype *cft,
4055 u64 val)
4056{
4057 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004058 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004059 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004060 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004061 return 0;
4062}
4063
Tejun Heod5c56ce2013-06-03 19:14:34 -07004064static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004065 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004066 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004067 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004068 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004069 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004070 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004071 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004072 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004073 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004074 .write_string = cgroup_write_event_control,
4075 .mode = S_IWUGO,
4076 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004077 {
4078 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004079 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004080 .read_u64 = cgroup_clone_children_read,
4081 .write_u64 = cgroup_clone_children_write,
4082 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004083 {
Tejun Heo873fe092013-04-14 20:15:26 -07004084 .name = "cgroup.sane_behavior",
4085 .flags = CFTYPE_ONLY_ON_ROOT,
4086 .read_seq_string = cgroup_sane_behavior_show,
4087 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004088
4089 /*
4090 * Historical crazy stuff. These don't have "cgroup." prefix and
4091 * don't exist if sane_behavior. If you're depending on these, be
4092 * prepared to be burned.
4093 */
4094 {
4095 .name = "tasks",
4096 .flags = CFTYPE_INSANE, /* use "procs" instead */
4097 .open = cgroup_tasks_open,
4098 .write_u64 = cgroup_tasks_write,
4099 .release = cgroup_pidlist_release,
4100 .mode = S_IRUGO | S_IWUSR,
4101 },
4102 {
4103 .name = "notify_on_release",
4104 .flags = CFTYPE_INSANE,
4105 .read_u64 = cgroup_read_notify_on_release,
4106 .write_u64 = cgroup_write_notify_on_release,
4107 },
Tejun Heo873fe092013-04-14 20:15:26 -07004108 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004109 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004110 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004111 .read_seq_string = cgroup_release_agent_show,
4112 .write_string = cgroup_release_agent_write,
4113 .max_write_len = PATH_MAX,
4114 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004115 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004116};
4117
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004118/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004119 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004120 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004121 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004122 *
4123 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004124 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004125static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004126{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004127 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004128 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004129
Tejun Heo8e3f6542012-04-01 12:09:55 -07004130 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004131 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004132 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004133
4134 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004135 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004136
Tejun Heobee55092013-06-28 16:24:11 -07004137 list_for_each_entry(set, &ss->cftsets, node) {
4138 ret = cgroup_addrm_files(cgrp, ss, set->cfts, true);
4139 if (ret < 0)
4140 goto err;
4141 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004143
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004144 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004145 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004146 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004147 struct css_id *id = rcu_dereference_protected(css->id, true);
4148
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004149 /*
4150 * Update id->css pointer and make this css visible from
4151 * CSS ID functions. This pointer will be dereferened
4152 * from RCU-read-side without locks.
4153 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004154 if (id)
4155 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004156 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157
4158 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004159err:
4160 cgroup_clear_dir(cgrp, subsys_mask);
4161 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004162}
4163
Tejun Heo48ddbe12012-04-01 12:09:56 -07004164static void css_dput_fn(struct work_struct *work)
4165{
4166 struct cgroup_subsys_state *css =
4167 container_of(work, struct cgroup_subsys_state, dput_work);
4168
Li Zefan1c8158e2013-06-18 18:41:10 +08004169 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004170}
4171
Tejun Heod3daf282013-06-13 19:39:16 -07004172static void css_release(struct percpu_ref *ref)
4173{
4174 struct cgroup_subsys_state *css =
4175 container_of(ref, struct cgroup_subsys_state, refcnt);
4176
4177 schedule_work(&css->dput_work);
4178}
4179
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180static void init_cgroup_css(struct cgroup_subsys_state *css,
4181 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004182 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183{
Paul Menagebd89aab2007-10-18 23:40:44 -07004184 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004185 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004186 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004187 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004188 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004189 BUG_ON(cgrp->subsys[ss->subsys_id]);
4190 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004191
4192 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004193 * css holds an extra ref to @cgrp->dentry which is put on the last
4194 * css_put(). dput() requires process context, which css_put() may
4195 * be called without. @css->dput_work will be used to invoke
4196 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004197 */
4198 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199}
4200
Li Zefan2a4ac632013-07-31 16:16:40 +08004201/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004202static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004203{
Tejun Heob1929db2012-11-19 08:13:38 -08004204 int ret = 0;
4205
Tejun Heoa31f2d32012-11-19 08:13:37 -08004206 lockdep_assert_held(&cgroup_mutex);
4207
Tejun Heo92fb9742012-11-19 08:13:38 -08004208 if (ss->css_online)
4209 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004210 if (!ret)
4211 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4212 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004213}
4214
Li Zefan2a4ac632013-07-31 16:16:40 +08004215/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004216static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004217{
4218 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4219
4220 lockdep_assert_held(&cgroup_mutex);
4221
4222 if (!(css->flags & CSS_ONLINE))
4223 return;
4224
Li Zefand7eeac12013-03-12 15:35:59 -07004225 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004226 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004227
4228 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4229}
4230
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231/*
Li Zefana043e3b2008-02-23 15:24:09 -08004232 * cgroup_create - create a cgroup
4233 * @parent: cgroup that will be parent of the new cgroup
4234 * @dentry: dentry of the new cgroup
4235 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 *
Li Zefana043e3b2008-02-23 15:24:09 -08004237 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004240 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241{
Paul Menagebd89aab2007-10-18 23:40:44 -07004242 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004243 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 struct cgroupfs_root *root = parent->root;
4245 int err = 0;
4246 struct cgroup_subsys *ss;
4247 struct super_block *sb = root->sb;
4248
Tejun Heo0a950f62012-11-19 09:02:12 -08004249 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004250 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4251 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252 return -ENOMEM;
4253
Li Zefan65dff752013-03-01 15:01:56 +08004254 name = cgroup_alloc_name(dentry);
4255 if (!name)
4256 goto err_free_cgrp;
4257 rcu_assign_pointer(cgrp->name, name);
4258
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004259 /*
4260 * Temporarily set the pointer to NULL, so idr_find() won't return
4261 * a half-baked cgroup.
4262 */
4263 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004264 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004265 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004266
Tejun Heo976c06b2012-11-05 09:16:59 -08004267 /*
4268 * Only live parents can have children. Note that the liveliness
4269 * check isn't strictly necessary because cgroup_mkdir() and
4270 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4271 * anyway so that locking is contained inside cgroup proper and we
4272 * don't get nasty surprises if we ever grow another caller.
4273 */
4274 if (!cgroup_lock_live_group(parent)) {
4275 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004276 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004277 }
4278
Paul Menageddbcc7e2007-10-18 23:39:30 -07004279 /* Grab a reference on the superblock so the hierarchy doesn't
4280 * get deleted on unmount if there are child cgroups. This
4281 * can be done outside cgroup_mutex, since the sb can't
4282 * disappear while someone has an open control file on the
4283 * fs */
4284 atomic_inc(&sb->s_active);
4285
Paul Menagecc31edc2008-10-18 20:28:04 -07004286 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287
Li Zefanfe1c06c2013-01-24 14:30:22 +08004288 dentry->d_fsdata = cgrp;
4289 cgrp->dentry = dentry;
4290
Paul Menagebd89aab2007-10-18 23:40:44 -07004291 cgrp->parent = parent;
4292 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004293
Li Zefanb6abdb02008-03-04 14:28:19 -08004294 if (notify_on_release(parent))
4295 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4296
Tejun Heo2260e7f2012-11-19 08:13:38 -08004297 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4298 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004299
Tejun Heo5549c492013-06-24 15:21:48 -07004300 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004301 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004302
Tejun Heo92fb9742012-11-19 08:13:38 -08004303 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004304 if (IS_ERR(css)) {
4305 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004306 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307 }
Tejun Heod3daf282013-06-13 19:39:16 -07004308
4309 err = percpu_ref_init(&css->refcnt, css_release);
4310 if (err)
4311 goto err_free_all;
4312
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004314
Li Zefan4528fd02010-02-02 13:44:10 -08004315 if (ss->use_id) {
4316 err = alloc_css_id(ss, parent, cgrp);
4317 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004318 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004319 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320 }
4321
Tejun Heo4e139af2012-11-19 08:13:36 -08004322 /*
4323 * Create directory. cgroup_create_file() returns with the new
4324 * directory locked on success so that it can be populated without
4325 * dropping cgroup_mutex.
4326 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004327 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004328 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004329 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004330 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331
Tejun Heo00356bd2013-06-18 11:14:22 -07004332 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004333
Tejun Heo4e139af2012-11-19 08:13:36 -08004334 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004335 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4336 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004337
Tejun Heob1929db2012-11-19 08:13:38 -08004338 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004339 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004340 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004341
Li Zefan415cf072013-04-08 14:35:02 +08004342 /* hold a ref to the parent's dentry */
4343 dget(parent->dentry);
4344
Tejun Heob1929db2012-11-19 08:13:38 -08004345 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004346 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004347 err = online_css(ss, cgrp);
4348 if (err)
4349 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004350
4351 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4352 parent->parent) {
4353 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",
4354 current->comm, current->pid, ss->name);
4355 if (!strcmp(ss->name, "memory"))
4356 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4357 ss->warned_broken_hierarchy = true;
4358 }
Tejun Heoa8638032012-11-09 09:12:29 -08004359 }
4360
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004361 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4362
Tejun Heo628f7cd2013-06-28 16:24:11 -07004363 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4364 if (err)
4365 goto err_destroy;
4366
4367 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004368 if (err)
4369 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
4371 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004372 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
4374 return 0;
4375
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004376err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004377 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004378 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4379
4380 if (css) {
4381 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004382 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004383 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 /* Release the reference count that we took on the superblock */
4387 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004388err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004389 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004390err_free_name:
4391 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004392err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004393 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004394 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004395
4396err_destroy:
4397 cgroup_destroy_locked(cgrp);
4398 mutex_unlock(&cgroup_mutex);
4399 mutex_unlock(&dentry->d_inode->i_mutex);
4400 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004401}
4402
Al Viro18bb1db2011-07-26 01:41:39 -04004403static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404{
4405 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4406
4407 /* the vfs holds inode->i_mutex already */
4408 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4409}
4410
Tejun Heod3daf282013-06-13 19:39:16 -07004411static void cgroup_css_killed(struct cgroup *cgrp)
4412{
4413 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4414 return;
4415
4416 /* percpu ref's of all css's are killed, kick off the next step */
4417 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4418 schedule_work(&cgrp->destroy_work);
4419}
4420
4421static void css_ref_killed_fn(struct percpu_ref *ref)
4422{
4423 struct cgroup_subsys_state *css =
4424 container_of(ref, struct cgroup_subsys_state, refcnt);
4425
4426 cgroup_css_killed(css->cgroup);
4427}
4428
4429/**
4430 * cgroup_destroy_locked - the first stage of cgroup destruction
4431 * @cgrp: cgroup to be destroyed
4432 *
4433 * css's make use of percpu refcnts whose killing latency shouldn't be
4434 * exposed to userland and are RCU protected. Also, cgroup core needs to
4435 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4436 * invoked. To satisfy all the requirements, destruction is implemented in
4437 * the following two steps.
4438 *
4439 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4440 * userland visible parts and start killing the percpu refcnts of
4441 * css's. Set up so that the next stage will be kicked off once all
4442 * the percpu refcnts are confirmed to be killed.
4443 *
4444 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4445 * rest of destruction. Once all cgroup references are gone, the
4446 * cgroup is RCU-freed.
4447 *
4448 * This function implements s1. After this step, @cgrp is gone as far as
4449 * the userland is concerned and a new cgroup with the same name may be
4450 * created. As cgroup doesn't care about the names internally, this
4451 * doesn't cause any problem.
4452 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004453static int cgroup_destroy_locked(struct cgroup *cgrp)
4454 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004455{
Tejun Heo42809dd2012-11-19 08:13:37 -08004456 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004457 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004458 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004459 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460
Tejun Heo42809dd2012-11-19 08:13:37 -08004461 lockdep_assert_held(&d->d_inode->i_mutex);
4462 lockdep_assert_held(&cgroup_mutex);
4463
Tejun Heoddd69142013-06-12 21:04:54 -07004464 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004465 * css_set_lock synchronizes access to ->cset_links and prevents
4466 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004467 */
4468 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004469 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004470 read_unlock(&css_set_lock);
4471 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004473
Tejun Heo1a90dd52012-11-05 09:16:59 -08004474 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004475 * Block new css_tryget() by killing css refcnts. cgroup core
4476 * guarantees that, by the time ->css_offline() is invoked, no new
4477 * css reference will be given out via css_tryget(). We can't
4478 * simply call percpu_ref_kill() and proceed to offlining css's
4479 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4480 * as killed on all CPUs on return.
4481 *
4482 * Use percpu_ref_kill_and_confirm() to get notifications as each
4483 * css is confirmed to be seen as killed on all CPUs. The
4484 * notification callback keeps track of the number of css's to be
4485 * killed and schedules cgroup_offline_fn() to perform the rest of
4486 * destruction once the percpu refs of all css's are confirmed to
4487 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004488 */
Tejun Heod3daf282013-06-13 19:39:16 -07004489 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004490 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004491 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4492
Tejun Heod3daf282013-06-13 19:39:16 -07004493 /*
4494 * Killing would put the base ref, but we need to keep it
4495 * alive until after ->css_offline.
4496 */
4497 percpu_ref_get(&css->refcnt);
4498
4499 atomic_inc(&cgrp->css_kill_cnt);
4500 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004501 }
Tejun Heod3daf282013-06-13 19:39:16 -07004502 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004503
4504 /*
4505 * Mark @cgrp dead. This prevents further task migration and child
4506 * creation by disabling cgroup_lock_live_group(). Note that
4507 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4508 * resume iteration after dropping RCU read lock. See
4509 * cgroup_next_sibling() for details.
4510 */
Tejun Heo54766d42013-06-12 21:04:53 -07004511 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004512
Tejun Heo455050d2013-06-13 19:27:41 -07004513 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4514 raw_spin_lock(&release_list_lock);
4515 if (!list_empty(&cgrp->release_list))
4516 list_del_init(&cgrp->release_list);
4517 raw_spin_unlock(&release_list_lock);
4518
4519 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004520 * Clear and remove @cgrp directory. The removal puts the base ref
4521 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004522 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004523 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
4524 cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004525 dget(d);
4526 cgroup_d_remove_dir(d);
4527
4528 /*
4529 * Unregister events and notify userspace.
4530 * Notify userspace about cgroup removing only after rmdir of cgroup
4531 * directory to avoid race between userspace and kernelspace.
4532 */
4533 spin_lock(&cgrp->event_list_lock);
4534 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4535 list_del_init(&event->list);
4536 schedule_work(&event->remove);
4537 }
4538 spin_unlock(&cgrp->event_list_lock);
4539
Tejun Heoea15f8c2013-06-13 19:27:42 -07004540 return 0;
4541};
4542
Tejun Heod3daf282013-06-13 19:39:16 -07004543/**
4544 * cgroup_offline_fn - the second step of cgroup destruction
4545 * @work: cgroup->destroy_free_work
4546 *
4547 * This function is invoked from a work item for a cgroup which is being
4548 * destroyed after the percpu refcnts of all css's are guaranteed to be
4549 * seen as killed on all CPUs, and performs the rest of destruction. This
4550 * is the second step of destruction described in the comment above
4551 * cgroup_destroy_locked().
4552 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004553static void cgroup_offline_fn(struct work_struct *work)
4554{
4555 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4556 struct cgroup *parent = cgrp->parent;
4557 struct dentry *d = cgrp->dentry;
4558 struct cgroup_subsys *ss;
4559
4560 mutex_lock(&cgroup_mutex);
4561
Tejun Heod3daf282013-06-13 19:39:16 -07004562 /*
4563 * css_tryget() is guaranteed to fail now. Tell subsystems to
4564 * initate destruction.
4565 */
Tejun Heo5549c492013-06-24 15:21:48 -07004566 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004567 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004568
4569 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004570 * Put the css refs from cgroup_destroy_locked(). Each css holds
4571 * an extra reference to the cgroup's dentry and cgroup removal
4572 * proceeds regardless of css refs. On the last put of each css,
4573 * whenever that may be, the extra dentry ref is put so that dentry
4574 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004575 */
Tejun Heo5549c492013-06-24 15:21:48 -07004576 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004577 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578
Paul Menage999cd8a2009-01-07 18:08:36 -08004579 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004580 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004581
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004582 /*
4583 * We should remove the cgroup object from idr before its grace
4584 * period starts, so we won't be looking up a cgroup while the
4585 * cgroup is being freed.
4586 */
4587 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4588 cgrp->id = -1;
4589
Paul Menageddbcc7e2007-10-18 23:39:30 -07004590 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004591
Paul Menagebd89aab2007-10-18 23:40:44 -07004592 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004593 check_for_release(parent);
4594
Tejun Heoea15f8c2013-06-13 19:27:42 -07004595 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004596}
4597
Tejun Heo42809dd2012-11-19 08:13:37 -08004598static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4599{
4600 int ret;
4601
4602 mutex_lock(&cgroup_mutex);
4603 ret = cgroup_destroy_locked(dentry->d_fsdata);
4604 mutex_unlock(&cgroup_mutex);
4605
4606 return ret;
4607}
4608
Tejun Heo8e3f6542012-04-01 12:09:55 -07004609static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4610{
4611 INIT_LIST_HEAD(&ss->cftsets);
4612
4613 /*
4614 * base_cftset is embedded in subsys itself, no need to worry about
4615 * deregistration.
4616 */
4617 if (ss->base_cftypes) {
4618 ss->base_cftset.cfts = ss->base_cftypes;
4619 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4620 }
4621}
4622
Li Zefan06a11922008-04-29 01:00:07 -07004623static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004626
4627 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628
Tejun Heo648bb562012-11-19 08:13:36 -08004629 mutex_lock(&cgroup_mutex);
4630
Tejun Heo8e3f6542012-04-01 12:09:55 -07004631 /* init base cftset */
4632 cgroup_init_cftsets(ss);
4633
Paul Menageddbcc7e2007-10-18 23:39:30 -07004634 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004635 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4636 ss->root = &cgroup_dummy_root;
4637 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004638 /* We don't handle early failures gracefully */
4639 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004640 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004641
Li Zefane8d55fd2008-04-29 01:00:13 -07004642 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004643 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004644 * newly registered, all tasks and hence the
4645 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004646 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647
4648 need_forkexit_callback |= ss->fork || ss->exit;
4649
Li Zefane8d55fd2008-04-29 01:00:13 -07004650 /* At system boot, before all subsystems have been
4651 * registered, no tasks have been forked, so we don't
4652 * need to invoke fork callbacks here. */
4653 BUG_ON(!list_empty(&init_task.tasks));
4654
Tejun Heo9871bf92013-06-24 15:21:47 -07004655 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004656
Tejun Heo648bb562012-11-19 08:13:36 -08004657 mutex_unlock(&cgroup_mutex);
4658
Ben Blume6a11052010-03-10 15:22:09 -08004659 /* this function shouldn't be used with modular subsystems, since they
4660 * need to register a subsys_id, among other things */
4661 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004662}
4663
4664/**
Ben Blume6a11052010-03-10 15:22:09 -08004665 * cgroup_load_subsys: load and register a modular subsystem at runtime
4666 * @ss: the subsystem to load
4667 *
4668 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004669 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004670 * up for use. If the subsystem is built-in anyway, work is delegated to the
4671 * simpler cgroup_init_subsys.
4672 */
4673int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4674{
Ben Blume6a11052010-03-10 15:22:09 -08004675 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004676 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004677 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004678 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004679 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004680
4681 /* check name and function validity */
4682 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004683 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004684 return -EINVAL;
4685
4686 /*
4687 * we don't support callbacks in modular subsystems. this check is
4688 * before the ss->module check for consistency; a subsystem that could
4689 * be a module should still have no callbacks even if the user isn't
4690 * compiling it as one.
4691 */
4692 if (ss->fork || ss->exit)
4693 return -EINVAL;
4694
4695 /*
4696 * an optionally modular subsystem is built-in: we want to do nothing,
4697 * since cgroup_init_subsys will have already taken care of it.
4698 */
4699 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004700 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004701 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004702 return 0;
4703 }
4704
Tejun Heo8e3f6542012-04-01 12:09:55 -07004705 /* init base cftset */
4706 cgroup_init_cftsets(ss);
4707
Ben Blume6a11052010-03-10 15:22:09 -08004708 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004709 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004710
4711 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004712 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004713 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004714 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004715 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004716 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004717 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004718 /* failure case - need to deassign the cgroup_subsys[] slot. */
4719 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004720 mutex_unlock(&cgroup_mutex);
4721 return PTR_ERR(css);
4722 }
4723
Tejun Heo9871bf92013-06-24 15:21:47 -07004724 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4725 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004726
4727 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004728 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004729 /* init_idr must be after init_cgroup_css because it sets css->id. */
4730 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004731 ret = cgroup_init_idr(ss, css);
4732 if (ret)
4733 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004734 }
4735
4736 /*
4737 * Now we need to entangle the css into the existing css_sets. unlike
4738 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4739 * will need a new pointer to it; done by iterating the css_set_table.
4740 * furthermore, modifying the existing css_sets will corrupt the hash
4741 * table state, so each changed css_set will need its hash recomputed.
4742 * this is all done under the css_set_lock.
4743 */
4744 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004745 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004746 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004747 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004748 continue;
4749 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004750 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004751 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004752 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004753 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004754 key = css_set_hash(cset->subsys);
4755 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004756 }
4757 write_unlock(&css_set_lock);
4758
Tejun Heo9871bf92013-06-24 15:21:47 -07004759 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004760 if (ret)
4761 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004762
Ben Blume6a11052010-03-10 15:22:09 -08004763 /* success! */
4764 mutex_unlock(&cgroup_mutex);
4765 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004766
4767err_unload:
4768 mutex_unlock(&cgroup_mutex);
4769 /* @ss can't be mounted here as try_module_get() would fail */
4770 cgroup_unload_subsys(ss);
4771 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004772}
4773EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4774
4775/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004776 * cgroup_unload_subsys: unload a modular subsystem
4777 * @ss: the subsystem to unload
4778 *
4779 * This function should be called in a modular subsystem's exitcall. When this
4780 * function is invoked, the refcount on the subsystem's module will be 0, so
4781 * the subsystem will not be attached to any hierarchy.
4782 */
4783void cgroup_unload_subsys(struct cgroup_subsys *ss)
4784{
Tejun Heo69d02062013-06-12 21:04:50 -07004785 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004786
4787 BUG_ON(ss->module == NULL);
4788
4789 /*
4790 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004791 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004792 * doesn't start being used while we're killing it off.
4793 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004794 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004795
4796 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004797
Tejun Heo9871bf92013-06-24 15:21:47 -07004798 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004799
Tejun Heoc897ff62013-02-27 17:03:49 -08004800 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004801 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004802
Ben Blumcf5d5942010-03-10 15:22:09 -08004803 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004804 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004805
Tejun Heo9871bf92013-06-24 15:21:47 -07004806 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004807 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004808
4809 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004810 * disentangle the css from all css_sets attached to the dummy
4811 * top. as in loading, we need to pay our respects to the hashtable
4812 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004813 */
4814 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004815 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004816 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004817 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004818
Tejun Heo5abb8852013-06-12 21:04:49 -07004819 hash_del(&cset->hlist);
4820 cset->subsys[ss->subsys_id] = NULL;
4821 key = css_set_hash(cset->subsys);
4822 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004823 }
4824 write_unlock(&css_set_lock);
4825
4826 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 * remove subsystem's css from the cgroup_dummy_top and free it -
4828 * need to free before marking as null because ss->css_free needs
4829 * the cgrp->subsys pointer to find their state. note that this
4830 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004831 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004832 ss->css_free(cgroup_dummy_top);
4833 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004834
4835 mutex_unlock(&cgroup_mutex);
4836}
4837EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4838
4839/**
Li Zefana043e3b2008-02-23 15:24:09 -08004840 * cgroup_init_early - cgroup initialization at system boot
4841 *
4842 * Initialize cgroups at system boot, and initialize any
4843 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004844 */
4845int __init cgroup_init_early(void)
4846{
Tejun Heo30159ec2013-06-25 11:53:37 -07004847 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004848 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004849
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004850 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004851 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004852 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004853 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004854 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004855 init_cgroup_root(&cgroup_dummy_root);
4856 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004857 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004858
Tejun Heo69d02062013-06-12 21:04:50 -07004859 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004860 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4861 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004862 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004863
Tejun Heo30159ec2013-06-25 11:53:37 -07004864 /* at bootup time, we don't worry about modular subsystems */
4865 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004866 BUG_ON(!ss->name);
4867 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004868 BUG_ON(!ss->css_alloc);
4869 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004870 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004871 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004872 ss->name, ss->subsys_id);
4873 BUG();
4874 }
4875
4876 if (ss->early_init)
4877 cgroup_init_subsys(ss);
4878 }
4879 return 0;
4880}
4881
4882/**
Li Zefana043e3b2008-02-23 15:24:09 -08004883 * cgroup_init - cgroup initialization
4884 *
4885 * Register cgroup filesystem and /proc file, and initialize
4886 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004887 */
4888int __init cgroup_init(void)
4889{
Tejun Heo30159ec2013-06-25 11:53:37 -07004890 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004891 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004892 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004893
4894 err = bdi_init(&cgroup_backing_dev_info);
4895 if (err)
4896 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004897
Tejun Heo30159ec2013-06-25 11:53:37 -07004898 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899 if (!ss->early_init)
4900 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004901 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004902 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004903 }
4904
Tejun Heofa3ca072013-04-14 11:36:56 -07004905 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004906 mutex_lock(&cgroup_mutex);
4907 mutex_lock(&cgroup_root_mutex);
4908
Tejun Heo82fe9b02013-06-25 11:53:37 -07004909 /* Add init_css_set to the hash table */
4910 key = css_set_hash(init_css_set.subsys);
4911 hash_add(css_set_table, &init_css_set.hlist, key);
4912
Tejun Heofc76df72013-06-25 11:53:37 -07004913 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004914
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004915 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4916 0, 1, GFP_KERNEL);
4917 BUG_ON(err < 0);
4918
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004919 mutex_unlock(&cgroup_root_mutex);
4920 mutex_unlock(&cgroup_mutex);
4921
Greg KH676db4a2010-08-05 13:53:35 -07004922 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4923 if (!cgroup_kobj) {
4924 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004925 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004926 }
4927
4928 err = register_filesystem(&cgroup_fs_type);
4929 if (err < 0) {
4930 kobject_put(cgroup_kobj);
4931 goto out;
4932 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004933
Li Zefan46ae2202008-04-29 01:00:08 -07004934 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004935
Paul Menageddbcc7e2007-10-18 23:39:30 -07004936out:
Paul Menagea4243162007-10-18 23:39:35 -07004937 if (err)
4938 bdi_destroy(&cgroup_backing_dev_info);
4939
Paul Menageddbcc7e2007-10-18 23:39:30 -07004940 return err;
4941}
Paul Menageb4f48b62007-10-18 23:39:33 -07004942
Paul Menagea4243162007-10-18 23:39:35 -07004943/*
4944 * proc_cgroup_show()
4945 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4946 * - Used for /proc/<pid>/cgroup.
4947 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4948 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004949 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004950 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4951 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4952 * cgroup to top_cgroup.
4953 */
4954
4955/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004956int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004957{
4958 struct pid *pid;
4959 struct task_struct *tsk;
4960 char *buf;
4961 int retval;
4962 struct cgroupfs_root *root;
4963
4964 retval = -ENOMEM;
4965 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4966 if (!buf)
4967 goto out;
4968
4969 retval = -ESRCH;
4970 pid = m->private;
4971 tsk = get_pid_task(pid, PIDTYPE_PID);
4972 if (!tsk)
4973 goto out_free;
4974
4975 retval = 0;
4976
4977 mutex_lock(&cgroup_mutex);
4978
Li Zefane5f6a862009-01-07 18:07:41 -08004979 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004980 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004981 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004982 int count = 0;
4983
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004984 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004985 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004986 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004987 if (strlen(root->name))
4988 seq_printf(m, "%sname=%s", count ? "," : "",
4989 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004990 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004991 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004992 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004993 if (retval < 0)
4994 goto out_unlock;
4995 seq_puts(m, buf);
4996 seq_putc(m, '\n');
4997 }
4998
4999out_unlock:
5000 mutex_unlock(&cgroup_mutex);
5001 put_task_struct(tsk);
5002out_free:
5003 kfree(buf);
5004out:
5005 return retval;
5006}
5007
Paul Menagea4243162007-10-18 23:39:35 -07005008/* Display information about each subsystem and each hierarchy */
5009static int proc_cgroupstats_show(struct seq_file *m, void *v)
5010{
Tejun Heo30159ec2013-06-25 11:53:37 -07005011 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005012 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005013
Paul Menage8bab8dd2008-04-04 14:29:57 -07005014 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005015 /*
5016 * ideally we don't want subsystems moving around while we do this.
5017 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5018 * subsys/hierarchy state.
5019 */
Paul Menagea4243162007-10-18 23:39:35 -07005020 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005021
5022 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005023 seq_printf(m, "%s\t%d\t%d\t%d\n",
5024 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005025 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005026
Paul Menagea4243162007-10-18 23:39:35 -07005027 mutex_unlock(&cgroup_mutex);
5028 return 0;
5029}
5030
5031static int cgroupstats_open(struct inode *inode, struct file *file)
5032{
Al Viro9dce07f2008-03-29 03:07:28 +00005033 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005034}
5035
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005036static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005037 .open = cgroupstats_open,
5038 .read = seq_read,
5039 .llseek = seq_lseek,
5040 .release = single_release,
5041};
5042
Paul Menageb4f48b62007-10-18 23:39:33 -07005043/**
5044 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005045 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005046 *
5047 * Description: A task inherits its parent's cgroup at fork().
5048 *
5049 * A pointer to the shared css_set was automatically copied in
5050 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005051 * it was not made under the protection of RCU or cgroup_mutex, so
5052 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5053 * have already changed current->cgroups, allowing the previously
5054 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005055 *
5056 * At the point that cgroup_fork() is called, 'current' is the parent
5057 * task, and the passed argument 'child' points to the child task.
5058 */
5059void cgroup_fork(struct task_struct *child)
5060{
Tejun Heo9bb71302012-10-18 17:52:07 -07005061 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005062 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005063 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005064 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005065 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005066}
5067
5068/**
Li Zefana043e3b2008-02-23 15:24:09 -08005069 * cgroup_post_fork - called on a new task after adding it to the task list
5070 * @child: the task in question
5071 *
Tejun Heo5edee612012-10-16 15:03:14 -07005072 * Adds the task to the list running through its css_set if necessary and
5073 * call the subsystem fork() callbacks. Has to be after the task is
5074 * visible on the task list in case we race with the first call to
5075 * cgroup_iter_start() - to guarantee that the new task ends up on its
5076 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005077 */
Paul Menage817929e2007-10-18 23:39:36 -07005078void cgroup_post_fork(struct task_struct *child)
5079{
Tejun Heo30159ec2013-06-25 11:53:37 -07005080 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005081 int i;
5082
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005083 /*
5084 * use_task_css_set_links is set to 1 before we walk the tasklist
5085 * under the tasklist_lock and we read it here after we added the child
5086 * to the tasklist under the tasklist_lock as well. If the child wasn't
5087 * yet in the tasklist when we walked through it from
5088 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5089 * should be visible now due to the paired locking and barriers implied
5090 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5091 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5092 * lock on fork.
5093 */
Paul Menage817929e2007-10-18 23:39:36 -07005094 if (use_task_css_set_links) {
5095 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005096 task_lock(child);
5097 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005098 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005099 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005100 write_unlock(&css_set_lock);
5101 }
Tejun Heo5edee612012-10-16 15:03:14 -07005102
5103 /*
5104 * Call ss->fork(). This must happen after @child is linked on
5105 * css_set; otherwise, @child might change state between ->fork()
5106 * and addition to css_set.
5107 */
5108 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005109 /*
5110 * fork/exit callbacks are supported only for builtin
5111 * subsystems, and the builtin section of the subsys
5112 * array is immutable, so we don't need to lock the
5113 * subsys array here. On the other hand, modular section
5114 * of the array can be freed at module unload, so we
5115 * can't touch that.
5116 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005117 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005118 if (ss->fork)
5119 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005120 }
Paul Menage817929e2007-10-18 23:39:36 -07005121}
Tejun Heo5edee612012-10-16 15:03:14 -07005122
Paul Menage817929e2007-10-18 23:39:36 -07005123/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005124 * cgroup_exit - detach cgroup from exiting task
5125 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005126 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005127 *
5128 * Description: Detach cgroup from @tsk and release it.
5129 *
5130 * Note that cgroups marked notify_on_release force every task in
5131 * them to take the global cgroup_mutex mutex when exiting.
5132 * This could impact scaling on very large systems. Be reluctant to
5133 * use notify_on_release cgroups where very high task exit scaling
5134 * is required on large systems.
5135 *
5136 * the_top_cgroup_hack:
5137 *
5138 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5139 *
5140 * We call cgroup_exit() while the task is still competent to
5141 * handle notify_on_release(), then leave the task attached to the
5142 * root cgroup in each hierarchy for the remainder of its exit.
5143 *
5144 * To do this properly, we would increment the reference count on
5145 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5146 * code we would add a second cgroup function call, to drop that
5147 * reference. This would just create an unnecessary hot spot on
5148 * the top_cgroup reference count, to no avail.
5149 *
5150 * Normally, holding a reference to a cgroup without bumping its
5151 * count is unsafe. The cgroup could go away, or someone could
5152 * attach us to a different cgroup, decrementing the count on
5153 * the first cgroup that we never incremented. But in this case,
5154 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005155 * which wards off any cgroup_attach_task() attempts, or task is a failed
5156 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005157 */
5158void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5159{
Tejun Heo30159ec2013-06-25 11:53:37 -07005160 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005161 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005162 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005163
5164 /*
5165 * Unlink from the css_set task list if necessary.
5166 * Optimistically check cg_list before taking
5167 * css_set_lock
5168 */
5169 if (!list_empty(&tsk->cg_list)) {
5170 write_lock(&css_set_lock);
5171 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005172 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005173 write_unlock(&css_set_lock);
5174 }
5175
Paul Menageb4f48b62007-10-18 23:39:33 -07005176 /* Reassign the task to the init_css_set. */
5177 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005178 cset = task_css_set(tsk);
5179 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005180
5181 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005182 /*
5183 * fork/exit callbacks are supported only for builtin
5184 * subsystems, see cgroup_post_fork() for details.
5185 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005186 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005187 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005188 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005189 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005190
Li Zefan761b3ef2012-01-31 13:47:36 +08005191 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005192 }
5193 }
5194 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005195 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005196
Tejun Heo5abb8852013-06-12 21:04:49 -07005197 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005198}
Paul Menage697f4162007-10-18 23:39:34 -07005199
Paul Menagebd89aab2007-10-18 23:40:44 -07005200static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005201{
Li Zefanf50daa72013-03-01 15:06:07 +08005202 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005203 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005204 /*
5205 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005206 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005207 * it now
5208 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005209 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005210
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005211 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005212 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005213 list_empty(&cgrp->release_list)) {
5214 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005215 need_schedule_work = 1;
5216 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005217 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005218 if (need_schedule_work)
5219 schedule_work(&release_agent_work);
5220 }
5221}
5222
Paul Menage81a6a5c2007-10-18 23:39:38 -07005223/*
5224 * Notify userspace when a cgroup is released, by running the
5225 * configured release agent with the name of the cgroup (path
5226 * relative to the root of cgroup file system) as the argument.
5227 *
5228 * Most likely, this user command will try to rmdir this cgroup.
5229 *
5230 * This races with the possibility that some other task will be
5231 * attached to this cgroup before it is removed, or that some other
5232 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5233 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5234 * unused, and this cgroup will be reprieved from its death sentence,
5235 * to continue to serve a useful existence. Next time it's released,
5236 * we will get notified again, if it still has 'notify_on_release' set.
5237 *
5238 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5239 * means only wait until the task is successfully execve()'d. The
5240 * separate release agent task is forked by call_usermodehelper(),
5241 * then control in this thread returns here, without waiting for the
5242 * release agent task. We don't bother to wait because the caller of
5243 * this routine has no use for the exit status of the release agent
5244 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005245 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005246static void cgroup_release_agent(struct work_struct *work)
5247{
5248 BUG_ON(work != &release_agent_work);
5249 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005250 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005251 while (!list_empty(&release_list)) {
5252 char *argv[3], *envp[3];
5253 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005254 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005255 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005256 struct cgroup,
5257 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005258 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005259 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005260 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005261 if (!pathbuf)
5262 goto continue_free;
5263 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5264 goto continue_free;
5265 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5266 if (!agentbuf)
5267 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005268
5269 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005270 argv[i++] = agentbuf;
5271 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005272 argv[i] = NULL;
5273
5274 i = 0;
5275 /* minimal command environment */
5276 envp[i++] = "HOME=/";
5277 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5278 envp[i] = NULL;
5279
5280 /* Drop the lock while we invoke the usermode helper,
5281 * since the exec could involve hitting disk and hence
5282 * be a slow process */
5283 mutex_unlock(&cgroup_mutex);
5284 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005285 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005286 continue_free:
5287 kfree(pathbuf);
5288 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005289 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005290 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005291 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005292 mutex_unlock(&cgroup_mutex);
5293}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005294
5295static int __init cgroup_disable(char *str)
5296{
Tejun Heo30159ec2013-06-25 11:53:37 -07005297 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005298 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005299 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005300
5301 while ((token = strsep(&str, ",")) != NULL) {
5302 if (!*token)
5303 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005304
Tejun Heo30159ec2013-06-25 11:53:37 -07005305 /*
5306 * cgroup_disable, being at boot time, can't know about
5307 * module subsystems, so we don't worry about them.
5308 */
5309 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005310 if (!strcmp(token, ss->name)) {
5311 ss->disabled = 1;
5312 printk(KERN_INFO "Disabling %s control group"
5313 " subsystem\n", ss->name);
5314 break;
5315 }
5316 }
5317 }
5318 return 1;
5319}
5320__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005321
5322/*
5323 * Functons for CSS ID.
5324 */
5325
Tejun Heo54766d42013-06-12 21:04:53 -07005326/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005327unsigned short css_id(struct cgroup_subsys_state *css)
5328{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005329 struct css_id *cssid;
5330
5331 /*
5332 * This css_id() can return correct value when somone has refcnt
5333 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5334 * it's unchanged until freed.
5335 */
Tejun Heod3daf282013-06-13 19:39:16 -07005336 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005337
5338 if (cssid)
5339 return cssid->id;
5340 return 0;
5341}
Ben Blum67523c42010-03-10 15:22:11 -08005342EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005343
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005344/**
5345 * css_is_ancestor - test "root" css is an ancestor of "child"
5346 * @child: the css to be tested.
5347 * @root: the css supporsed to be an ancestor of the child.
5348 *
5349 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005350 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005351 * But, considering usual usage, the csses should be valid objects after test.
5352 * Assuming that the caller will do some action to the child if this returns
5353 * returns true, the caller must take "child";s reference count.
5354 * If "child" is valid object and this returns true, "root" is valid, too.
5355 */
5356
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005357bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005358 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005359{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005360 struct css_id *child_id;
5361 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005362
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005363 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005364 if (!child_id)
5365 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005366 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005367 if (!root_id)
5368 return false;
5369 if (child_id->depth < root_id->depth)
5370 return false;
5371 if (child_id->stack[root_id->depth] != root_id->id)
5372 return false;
5373 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005374}
5375
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005376void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5377{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005378 struct css_id *id = rcu_dereference_protected(css->id, true);
5379
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005380 /* When this is called before css_id initialization, id can be NULL */
5381 if (!id)
5382 return;
5383
5384 BUG_ON(!ss->use_id);
5385
5386 rcu_assign_pointer(id->css, NULL);
5387 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005388 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005389 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005390 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005391 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005392}
Ben Blum67523c42010-03-10 15:22:11 -08005393EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394
5395/*
5396 * This is called by init or create(). Then, calls to this function are
5397 * always serialized (By cgroup_mutex() at create()).
5398 */
5399
5400static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5401{
5402 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005403 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005404
5405 BUG_ON(!ss->use_id);
5406
5407 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5408 newid = kzalloc(size, GFP_KERNEL);
5409 if (!newid)
5410 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005411
5412 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005413 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005414 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005415 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005416 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005417 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418
5419 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005420 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005422
Tejun Heod228d9e2013-02-27 17:04:54 -08005423 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005424 newid->depth = depth;
5425 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426err_out:
5427 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005428 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429
5430}
5431
Ben Blume6a11052010-03-10 15:22:09 -08005432static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5433 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005434{
5435 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005436
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005437 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005438 idr_init(&ss->idr);
5439
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005440 newid = get_new_cssid(ss, 0);
5441 if (IS_ERR(newid))
5442 return PTR_ERR(newid);
5443
5444 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005445 RCU_INIT_POINTER(newid->css, rootcss);
5446 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005447 return 0;
5448}
5449
5450static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5451 struct cgroup *child)
5452{
5453 int subsys_id, i, depth = 0;
5454 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005455 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005456
5457 subsys_id = ss->subsys_id;
5458 parent_css = parent->subsys[subsys_id];
5459 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005460 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005461 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005462
5463 child_id = get_new_cssid(ss, depth);
5464 if (IS_ERR(child_id))
5465 return PTR_ERR(child_id);
5466
5467 for (i = 0; i < depth; i++)
5468 child_id->stack[i] = parent_id->stack[i];
5469 child_id->stack[depth] = child_id->id;
5470 /*
5471 * child_id->css pointer will be set after this cgroup is available
5472 * see cgroup_populate_dir()
5473 */
5474 rcu_assign_pointer(child_css->id, child_id);
5475
5476 return 0;
5477}
5478
5479/**
5480 * css_lookup - lookup css by id
5481 * @ss: cgroup subsys to be looked into.
5482 * @id: the id
5483 *
5484 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5485 * NULL if not. Should be called under rcu_read_lock()
5486 */
5487struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5488{
5489 struct css_id *cssid = NULL;
5490
5491 BUG_ON(!ss->use_id);
5492 cssid = idr_find(&ss->idr, id);
5493
5494 if (unlikely(!cssid))
5495 return NULL;
5496
5497 return rcu_dereference(cssid->css);
5498}
Ben Blum67523c42010-03-10 15:22:11 -08005499EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005500
Stephane Eraniane5d13672011-02-14 11:20:01 +02005501/*
5502 * get corresponding css from file open on cgroupfs directory
5503 */
5504struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5505{
5506 struct cgroup *cgrp;
5507 struct inode *inode;
5508 struct cgroup_subsys_state *css;
5509
Al Viro496ad9a2013-01-23 17:07:38 -05005510 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005511 /* check in cgroup filesystem dir */
5512 if (inode->i_op != &cgroup_dir_inode_operations)
5513 return ERR_PTR(-EBADF);
5514
5515 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5516 return ERR_PTR(-EINVAL);
5517
5518 /* get cgroup */
5519 cgrp = __d_cgrp(f->f_dentry);
5520 css = cgrp->subsys[id];
5521 return css ? css : ERR_PTR(-ENOENT);
5522}
5523
Paul Menagefe693432009-09-23 15:56:20 -07005524#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005525static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005526{
5527 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5528
5529 if (!css)
5530 return ERR_PTR(-ENOMEM);
5531
5532 return css;
5533}
5534
Li Zefan03c78cb2013-06-14 11:17:19 +08005535static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005536{
Li Zefan03c78cb2013-06-14 11:17:19 +08005537 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005538}
5539
Li Zefan03c78cb2013-06-14 11:17:19 +08005540static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005541{
Li Zefan03c78cb2013-06-14 11:17:19 +08005542 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005543}
5544
Li Zefan03c78cb2013-06-14 11:17:19 +08005545static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005546{
5547 return (u64)(unsigned long)current->cgroups;
5548}
5549
Li Zefan03c78cb2013-06-14 11:17:19 +08005550static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5551 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005552{
5553 u64 count;
5554
5555 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005556 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005557 rcu_read_unlock();
5558 return count;
5559}
5560
Li Zefan03c78cb2013-06-14 11:17:19 +08005561static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005562 struct cftype *cft,
5563 struct seq_file *seq)
5564{
Tejun Heo69d02062013-06-12 21:04:50 -07005565 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005566 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005567
5568 read_lock(&css_set_lock);
5569 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005570 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005571 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005572 struct cgroup *c = link->cgrp;
5573 const char *name;
5574
5575 if (c->dentry)
5576 name = c->dentry->d_name.name;
5577 else
5578 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005579 seq_printf(seq, "Root %d group %s\n",
5580 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005581 }
5582 rcu_read_unlock();
5583 read_unlock(&css_set_lock);
5584 return 0;
5585}
5586
5587#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005588static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005589 struct cftype *cft,
5590 struct seq_file *seq)
5591{
Tejun Heo69d02062013-06-12 21:04:50 -07005592 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005593
5594 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005595 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005596 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005597 struct task_struct *task;
5598 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005599 seq_printf(seq, "css_set %p\n", cset);
5600 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005601 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5602 seq_puts(seq, " ...\n");
5603 break;
5604 } else {
5605 seq_printf(seq, " task %d\n",
5606 task_pid_vnr(task));
5607 }
5608 }
5609 }
5610 read_unlock(&css_set_lock);
5611 return 0;
5612}
5613
Paul Menagefe693432009-09-23 15:56:20 -07005614static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5615{
5616 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5617}
5618
5619static struct cftype debug_files[] = {
5620 {
Paul Menagefe693432009-09-23 15:56:20 -07005621 .name = "taskcount",
5622 .read_u64 = debug_taskcount_read,
5623 },
5624
5625 {
5626 .name = "current_css_set",
5627 .read_u64 = current_css_set_read,
5628 },
5629
5630 {
5631 .name = "current_css_set_refcount",
5632 .read_u64 = current_css_set_refcount_read,
5633 },
5634
5635 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005636 .name = "current_css_set_cg_links",
5637 .read_seq_string = current_css_set_cg_links_read,
5638 },
5639
5640 {
5641 .name = "cgroup_css_links",
5642 .read_seq_string = cgroup_css_links_read,
5643 },
5644
5645 {
Paul Menagefe693432009-09-23 15:56:20 -07005646 .name = "releasable",
5647 .read_u64 = releasable_read,
5648 },
Paul Menagefe693432009-09-23 15:56:20 -07005649
Tejun Heo4baf6e32012-04-01 12:09:55 -07005650 { } /* terminate */
5651};
Paul Menagefe693432009-09-23 15:56:20 -07005652
5653struct cgroup_subsys debug_subsys = {
5654 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005655 .css_alloc = debug_css_alloc,
5656 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005657 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005658 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005659};
5660#endif /* CONFIG_CGROUP_DEBUG */