blob: 9577bebe2546f4639d2f7db7cb424be481c2208f [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
Li Zefancc20e012013-04-26 11:58:02 -0700869 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
870
Li Zefan415cf072013-04-08 14:35:02 +0800871 /*
Li Zefanbe445622013-01-24 14:31:42 +0800872 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700873 * created the cgroup. This will free cgrp->root, if we are
874 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800875 */
876 deactivate_super(cgrp->root->sb);
877
878 /*
879 * if we're getting rid of the cgroup, refcount should ensure
880 * that there are no pidlists left.
881 */
882 BUG_ON(!list_empty(&cgrp->pidlists));
883
884 simple_xattrs_free(&cgrp->xattrs);
885
Li Zefan65dff752013-03-01 15:01:56 +0800886 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800887 kfree(cgrp);
888}
889
890static void cgroup_free_rcu(struct rcu_head *head)
891{
892 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
893
Tejun Heoea15f8c2013-06-13 19:27:42 -0700894 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
895 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800896}
897
Paul Menageddbcc7e2007-10-18 23:39:30 -0700898static void cgroup_diput(struct dentry *dentry, struct inode *inode)
899{
900 /* is dentry a directory ? if so, kfree() associated cgroup */
901 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700902 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800903
Tejun Heo54766d42013-06-12 21:04:53 -0700904 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800905 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700906 } else {
907 struct cfent *cfe = __d_cfe(dentry);
908 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
909
910 WARN_ONCE(!list_empty(&cfe->node) &&
911 cgrp != &cgrp->root->top_cgroup,
912 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700913 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915 }
916 iput(inode);
917}
918
Al Viroc72a04e2011-01-14 05:31:45 +0000919static int cgroup_delete(const struct dentry *d)
920{
921 return 1;
922}
923
Paul Menageddbcc7e2007-10-18 23:39:30 -0700924static void remove_dir(struct dentry *d)
925{
926 struct dentry *parent = dget(d->d_parent);
927
928 d_delete(d);
929 simple_rmdir(parent->d_inode, d);
930 dput(parent);
931}
932
Li Zefan2739d3c2013-01-21 18:18:33 +0800933static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
938 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940 /*
941 * If we're doing cleanup due to failure of cgroup_create(),
942 * the corresponding @cfe may not exist.
943 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_for_each_entry(cfe, &cgrp->files, node) {
945 struct dentry *d = cfe->dentry;
946
947 if (cft && cfe->type != cft)
948 continue;
949
950 dget(d);
951 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700952 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953 list_del_init(&cfe->node);
954 dput(d);
955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958}
959
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700961 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700962 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700965static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700966{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700968 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969
Tejun Heob420ba72013-07-12 12:34:02 -0700970 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400971 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700972
973 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400974 continue;
975 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800976 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978}
979
980/*
981 * NOTE : the dentry must have been dget()'ed
982 */
983static void cgroup_d_remove_dir(struct dentry *dentry)
984{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100985 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 parent = dentry->d_parent;
988 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800989 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991 spin_unlock(&dentry->d_lock);
992 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 remove_dir(dentry);
994}
995
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700996/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800997 * Call with cgroup_mutex held. Drops reference counts on modules, including
998 * any duplicate ones that parse_cgroupfs_options took. If this function
999 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001000 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001002 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003{
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001005 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001006 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001007 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008
Ben Blumaae8aab2010-03-10 15:22:07 -08001009 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001010 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001011
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001013 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001014 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001015 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001016
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001017 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001018 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001019 ret = -EBUSY;
1020 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001022
1023 /* pin the module */
1024 if (!try_module_get(ss->module)) {
1025 ret = -ENOENT;
1026 goto out_put;
1027 }
1028 pinned |= 1 << i;
1029 }
1030
1031 /* subsys could be missing if unloaded between parsing and here */
1032 if (added_mask != pinned) {
1033 ret = -ENOENT;
1034 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 }
1036
Tejun Heo31261212013-06-28 17:07:30 -07001037 ret = cgroup_populate_dir(cgrp, added_mask);
1038 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001039 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001040
1041 /*
1042 * Nothing can fail from this point on. Remove files for the
1043 * removed subsystems and rebind each subsystem.
1044 */
1045 cgroup_clear_dir(cgrp, removed_mask);
1046
Tejun Heo30159ec2013-06-25 11:53:37 -07001047 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001049
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001050 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001052 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001053 BUG_ON(!cgroup_dummy_top->subsys[i]);
1054 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001055
Tejun Heo9871bf92013-06-24 15:21:47 -07001056 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001058 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001059 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001060 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001061 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001062
Ben Blumcf5d5942010-03-10 15:22:09 -08001063 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001064 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001065 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001067 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001068 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001069
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001071 ss->bind(cgroup_dummy_top);
1072 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001074 cgroup_subsys[i]->root = &cgroup_dummy_root;
1075 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001076
Ben Blumcf5d5942010-03-10 15:22:09 -08001077 /* subsystem is now free - drop reference on module */
1078 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001079 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080 }
1081 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082
Tejun Heo1672d042013-06-25 18:04:54 -07001083 /*
1084 * Mark @root has finished binding subsystems. @root->subsys_mask
1085 * now matches the bound subsystems.
1086 */
1087 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1088
Paul Menageddbcc7e2007-10-18 23:39:30 -07001089 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001090
1091out_put:
1092 for_each_subsys(ss, i)
1093 if (pinned & (1 << i))
1094 module_put(ss->module);
1095 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096}
1097
Al Viro34c80b12011-12-08 21:32:45 -05001098static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099{
Al Viro34c80b12011-12-08 21:32:45 -05001100 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 struct cgroup_subsys *ss;
1102
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001103 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001104 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001106 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1107 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001108 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001110 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001111 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001112 if (strlen(root->release_agent_path))
1113 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001114 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001115 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001116 if (strlen(root->name))
1117 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001118 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119 return 0;
1120}
1121
1122struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001123 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001125 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001126 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001127 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001128 /* User explicitly requested empty subsystem */
1129 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001130
1131 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001132
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133};
1134
Ben Blumaae8aab2010-03-10 15:22:07 -08001135/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001136 * Convert a hierarchy specifier into a bitmask of subsystems and
1137 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1138 * array. This function takes refcounts on subsystems to be used, unless it
1139 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001141static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 char *token, *o = data;
1144 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001145 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001146 struct cgroup_subsys *ss;
1147 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001148
Ben Blumaae8aab2010-03-10 15:22:07 -08001149 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1150
Li Zefanf9ab5b52009-06-17 16:26:33 -07001151#ifdef CONFIG_CPUSETS
1152 mask = ~(1UL << cpuset_subsys_id);
1153#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001154
Paul Menagec6d57f32009-09-23 15:56:19 -07001155 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001156
1157 while ((token = strsep(&o, ",")) != NULL) {
1158 if (!*token)
1159 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001160 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001161 /* Explicitly have no subsystems */
1162 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
1165 if (!strcmp(token, "all")) {
1166 /* Mutually exclusive option 'all' + subsystem name */
1167 if (one_ss)
1168 return -EINVAL;
1169 all_ss = true;
1170 continue;
1171 }
Tejun Heo873fe092013-04-14 20:15:26 -07001172 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1173 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1174 continue;
1175 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001177 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 continue;
1179 }
1180 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001181 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001184 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001185 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001186 continue;
1187 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001189 /* Specifying two release agents is forbidden */
1190 if (opts->release_agent)
1191 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001192 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001193 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001194 if (!opts->release_agent)
1195 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196 continue;
1197 }
1198 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001199 const char *name = token + 5;
1200 /* Can't specify an empty name */
1201 if (!strlen(name))
1202 return -EINVAL;
1203 /* Must match [\w.-]+ */
1204 for (i = 0; i < strlen(name); i++) {
1205 char c = name[i];
1206 if (isalnum(c))
1207 continue;
1208 if ((c == '.') || (c == '-') || (c == '_'))
1209 continue;
1210 return -EINVAL;
1211 }
1212 /* Specifying two names is forbidden */
1213 if (opts->name)
1214 return -EINVAL;
1215 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001216 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001217 GFP_KERNEL);
1218 if (!opts->name)
1219 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220
1221 continue;
1222 }
1223
Tejun Heo30159ec2013-06-25 11:53:37 -07001224 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001225 if (strcmp(token, ss->name))
1226 continue;
1227 if (ss->disabled)
1228 continue;
1229
1230 /* Mutually exclusive option 'all' + subsystem name */
1231 if (all_ss)
1232 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001233 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 one_ss = true;
1235
1236 break;
1237 }
1238 if (i == CGROUP_SUBSYS_COUNT)
1239 return -ENOENT;
1240 }
1241
1242 /*
1243 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001244 * otherwise if 'none', 'name=' and a subsystem name options
1245 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001246 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001247 if (all_ss || (!one_ss && !opts->none && !opts->name))
1248 for_each_subsys(ss, i)
1249 if (!ss->disabled)
1250 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001251
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001252 /* Consistency checks */
1253
Tejun Heo873fe092013-04-14 20:15:26 -07001254 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1255 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1256
1257 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1258 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1259 return -EINVAL;
1260 }
1261
1262 if (opts->cpuset_clone_children) {
1263 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1264 return -EINVAL;
1265 }
1266 }
1267
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 /*
1269 * Option noprefix was introduced just for backward compatibility
1270 * with the old cpuset, so we allow noprefix only if mounting just
1271 * the cpuset subsystem.
1272 */
Tejun Heo93438622013-04-14 20:15:25 -07001273 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001274 return -EINVAL;
1275
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001276
1277 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001278 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001279 return -EINVAL;
1280
1281 /*
1282 * We either have to specify by name or by subsystems. (So all
1283 * empty hierarchies must have a name).
1284 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001285 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001286 return -EINVAL;
1287
1288 return 0;
1289}
1290
1291static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1292{
1293 int ret = 0;
1294 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001295 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001297 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001298
Tejun Heo873fe092013-04-14 20:15:26 -07001299 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1300 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1301 return -EINVAL;
1302 }
1303
Paul Menagebd89aab2007-10-18 23:40:44 -07001304 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001306 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001307
1308 /* See what subsystems are wanted */
1309 ret = parse_cgroupfs_options(data, &opts);
1310 if (ret)
1311 goto out_unlock;
1312
Tejun Heoa8a648c2013-06-24 15:21:47 -07001313 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001314 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1315 task_tgid_nr(current), current->comm);
1316
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317 added_mask = opts.subsys_mask & ~root->subsys_mask;
1318 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001319
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001321 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001322 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001323 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1324 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1325 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001327 goto out_unlock;
1328 }
1329
Tejun Heof172e672013-06-28 17:07:30 -07001330 /* remounting is not allowed for populated hierarchies */
1331 if (root->number_of_cgroups > 1) {
1332 ret = -EBUSY;
1333 goto out_unlock;
1334 }
1335
Tejun Heoa8a648c2013-06-24 15:21:47 -07001336 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001337 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001338 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339
Paul Menage81a6a5c2007-10-18 23:39:38 -07001340 if (opts.release_agent)
1341 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001343 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001344 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001345 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001347 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348 return ret;
1349}
1350
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001351static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001352 .statfs = simple_statfs,
1353 .drop_inode = generic_delete_inode,
1354 .show_options = cgroup_show_options,
1355 .remount_fs = cgroup_remount,
1356};
1357
Paul Menagecc31edc2008-10-18 20:28:04 -07001358static void init_cgroup_housekeeping(struct cgroup *cgrp)
1359{
1360 INIT_LIST_HEAD(&cgrp->sibling);
1361 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001362 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001363 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001364 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001365 INIT_LIST_HEAD(&cgrp->pidlists);
1366 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001367 INIT_LIST_HEAD(&cgrp->event_list);
1368 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001369 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001370}
Paul Menagec6d57f32009-09-23 15:56:19 -07001371
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372static void init_cgroup_root(struct cgroupfs_root *root)
1373{
Paul Menagebd89aab2007-10-18 23:40:44 -07001374 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001375
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376 INIT_LIST_HEAD(&root->subsys_list);
1377 INIT_LIST_HEAD(&root->root_list);
1378 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001379 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001380 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001381 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382}
1383
Tejun Heofc76df72013-06-25 11:53:37 -07001384static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001385{
Tejun Heo1a574232013-04-14 11:36:58 -07001386 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001387
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001388 lockdep_assert_held(&cgroup_mutex);
1389 lockdep_assert_held(&cgroup_root_mutex);
1390
Tejun Heofc76df72013-06-25 11:53:37 -07001391 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1392 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001393 if (id < 0)
1394 return id;
1395
1396 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001397 return 0;
1398}
1399
1400static void cgroup_exit_root_id(struct cgroupfs_root *root)
1401{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001402 lockdep_assert_held(&cgroup_mutex);
1403 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001404
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001405 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001406 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001407 root->hierarchy_id = 0;
1408 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001409}
1410
Paul Menageddbcc7e2007-10-18 23:39:30 -07001411static int cgroup_test_super(struct super_block *sb, void *data)
1412{
Paul Menagec6d57f32009-09-23 15:56:19 -07001413 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414 struct cgroupfs_root *root = sb->s_fs_info;
1415
Paul Menagec6d57f32009-09-23 15:56:19 -07001416 /* If we asked for a name then it must match */
1417 if (opts->name && strcmp(opts->name, root->name))
1418 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001419
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001420 /*
1421 * If we asked for subsystems (or explicitly for no
1422 * subsystems) then they must match
1423 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001424 if ((opts->subsys_mask || opts->none)
1425 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426 return 0;
1427
1428 return 1;
1429}
1430
Paul Menagec6d57f32009-09-23 15:56:19 -07001431static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1432{
1433 struct cgroupfs_root *root;
1434
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001435 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001436 return NULL;
1437
1438 root = kzalloc(sizeof(*root), GFP_KERNEL);
1439 if (!root)
1440 return ERR_PTR(-ENOMEM);
1441
1442 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001443
Tejun Heo1672d042013-06-25 18:04:54 -07001444 /*
1445 * We need to set @root->subsys_mask now so that @root can be
1446 * matched by cgroup_test_super() before it finishes
1447 * initialization; otherwise, competing mounts with the same
1448 * options may try to bind the same subsystems instead of waiting
1449 * for the first one leading to unexpected mount errors.
1450 * SUBSYS_BOUND will be set once actual binding is complete.
1451 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001452 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001453 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001454 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001455 if (opts->release_agent)
1456 strcpy(root->release_agent_path, opts->release_agent);
1457 if (opts->name)
1458 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001459 if (opts->cpuset_clone_children)
1460 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001461 return root;
1462}
1463
Tejun Heofa3ca072013-04-14 11:36:56 -07001464static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001465{
Tejun Heofa3ca072013-04-14 11:36:56 -07001466 if (root) {
1467 /* hierarhcy ID shoulid already have been released */
1468 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001469
Tejun Heofa3ca072013-04-14 11:36:56 -07001470 ida_destroy(&root->cgroup_ida);
1471 kfree(root);
1472 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001473}
1474
Paul Menageddbcc7e2007-10-18 23:39:30 -07001475static int cgroup_set_super(struct super_block *sb, void *data)
1476{
1477 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001478 struct cgroup_sb_opts *opts = data;
1479
1480 /* If we don't have a new root, we can't set up a new sb */
1481 if (!opts->new_root)
1482 return -EINVAL;
1483
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001484 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485
1486 ret = set_anon_super(sb, NULL);
1487 if (ret)
1488 return ret;
1489
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 sb->s_fs_info = opts->new_root;
1491 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001492
1493 sb->s_blocksize = PAGE_CACHE_SIZE;
1494 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1495 sb->s_magic = CGROUP_SUPER_MAGIC;
1496 sb->s_op = &cgroup_ops;
1497
1498 return 0;
1499}
1500
1501static int cgroup_get_rootdir(struct super_block *sb)
1502{
Al Viro0df6a632010-12-21 13:29:29 -05001503 static const struct dentry_operations cgroup_dops = {
1504 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001505 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001506 };
1507
Paul Menageddbcc7e2007-10-18 23:39:30 -07001508 struct inode *inode =
1509 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510
1511 if (!inode)
1512 return -ENOMEM;
1513
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514 inode->i_fop = &simple_dir_operations;
1515 inode->i_op = &cgroup_dir_inode_operations;
1516 /* directories start off with i_nlink == 2 (for "." entry) */
1517 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001518 sb->s_root = d_make_root(inode);
1519 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001521 /* for everything else we want ->d_op set */
1522 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001523 return 0;
1524}
1525
Al Virof7e83572010-07-26 13:23:11 +04001526static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001528 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529{
1530 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001532 int ret = 0;
1533 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001534 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001535 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001536 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001537 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
1539 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001540 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001542 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001543 if (ret)
1544 goto out_err;
1545
1546 /*
1547 * Allocate a new cgroup root. We may not need it if we're
1548 * reusing an existing hierarchy.
1549 */
1550 new_root = cgroup_root_from_opts(&opts);
1551 if (IS_ERR(new_root)) {
1552 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001553 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001554 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001558 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001560 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001561 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001562 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563 }
1564
Paul Menagec6d57f32009-09-23 15:56:19 -07001565 root = sb->s_fs_info;
1566 BUG_ON(!root);
1567 if (root == opts.new_root) {
1568 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001569 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001571 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001572 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573
1574 BUG_ON(sb->s_root != NULL);
1575
1576 ret = cgroup_get_rootdir(sb);
1577 if (ret)
1578 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001579 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580
Paul Menage817929e2007-10-18 23:39:36 -07001581 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001583 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001585 /* Check for name clashes with existing mounts */
1586 ret = -EBUSY;
1587 if (strlen(root->name))
1588 for_each_active_root(existing_root)
1589 if (!strcmp(existing_root->name, root->name))
1590 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001591
Paul Menage817929e2007-10-18 23:39:36 -07001592 /*
1593 * We're accessing css_set_count without locking
1594 * css_set_lock here, but that's OK - it can only be
1595 * increased by someone holding cgroup_lock, and
1596 * that's us. The worst that can happen is that we
1597 * have some link structures left over
1598 */
Tejun Heo69d02062013-06-12 21:04:50 -07001599 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001600 if (ret)
1601 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001602
Tejun Heofc76df72013-06-25 11:53:37 -07001603 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1604 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001605 if (ret)
1606 goto unlock_drop;
1607
Tejun Heo31261212013-06-28 17:07:30 -07001608 sb->s_root->d_fsdata = root_cgrp;
1609 root_cgrp->dentry = sb->s_root;
1610
1611 /*
1612 * We're inside get_sb() and will call lookup_one_len() to
1613 * create the root files, which doesn't work if SELinux is
1614 * in use. The following cred dancing somehow works around
1615 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1616 * populating new cgroupfs mount") for more details.
1617 */
1618 cred = override_creds(&init_cred);
1619
1620 ret = cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true);
1621 if (ret)
1622 goto rm_base_files;
1623
Tejun Heoa8a648c2013-06-24 15:21:47 -07001624 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001625 if (ret)
1626 goto rm_base_files;
1627
1628 revert_creds(cred);
1629
Ben Blumcf5d5942010-03-10 15:22:09 -08001630 /*
1631 * There must be no failure case after here, since rebinding
1632 * takes care of subsystems' refcounts, which are explicitly
1633 * dropped in the failure exit path.
1634 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001635
Tejun Heo9871bf92013-06-24 15:21:47 -07001636 list_add(&root->root_list, &cgroup_roots);
1637 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001638
Paul Menage817929e2007-10-18 23:39:36 -07001639 /* Link the top cgroup in this hierarchy into all
1640 * the css_set objects */
1641 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001642 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001643 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001644 write_unlock(&css_set_lock);
1645
Tejun Heo69d02062013-06-12 21:04:50 -07001646 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001647
Li Zefanc12f65d2009-01-07 18:07:42 -08001648 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649 BUG_ON(root->number_of_cgroups != 1);
1650
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001651 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001653 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001654 } else {
1655 /*
1656 * We re-used an existing hierarchy - the new root (if
1657 * any) is not needed
1658 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001659 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001660
Tejun Heoc7ba8282013-06-29 14:06:10 -07001661 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001662 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1663 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1664 ret = -EINVAL;
1665 goto drop_new_super;
1666 } else {
1667 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1668 }
Tejun Heo873fe092013-04-14 20:15:26 -07001669 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670 }
1671
Paul Menagec6d57f32009-09-23 15:56:19 -07001672 kfree(opts.release_agent);
1673 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001674 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675
Tejun Heo31261212013-06-28 17:07:30 -07001676 rm_base_files:
1677 free_cgrp_cset_links(&tmp_links);
1678 cgroup_addrm_files(&root->top_cgroup, NULL, cgroup_base_files, false);
1679 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001680 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001681 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001682 mutex_unlock(&cgroup_root_mutex);
1683 mutex_unlock(&cgroup_mutex);
1684 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001685 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001686 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001687 out_err:
1688 kfree(opts.release_agent);
1689 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001690 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691}
1692
1693static void cgroup_kill_sb(struct super_block *sb) {
1694 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001695 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001696 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697 int ret;
1698
1699 BUG_ON(!root);
1700
1701 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001702 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703
Tejun Heo31261212013-06-28 17:07:30 -07001704 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001705 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707
1708 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001709 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1710 ret = rebind_subsystems(root, 0, root->subsys_mask);
1711 /* Shouldn't be able to fail ... */
1712 BUG_ON(ret);
1713 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
Paul Menage817929e2007-10-18 23:39:36 -07001715 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001716 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001717 * root cgroup
1718 */
1719 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001720
Tejun Heo69d02062013-06-12 21:04:50 -07001721 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1722 list_del(&link->cset_link);
1723 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001724 kfree(link);
1725 }
1726 write_unlock(&css_set_lock);
1727
Paul Menage839ec542009-01-29 14:25:22 -08001728 if (!list_empty(&root->root_list)) {
1729 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001730 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001731 }
Li Zefane5f6a862009-01-07 18:07:41 -08001732
Tejun Heofa3ca072013-04-14 11:36:56 -07001733 cgroup_exit_root_id(root);
1734
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001735 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001737 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001739 simple_xattrs_free(&cgrp->xattrs);
1740
Paul Menageddbcc7e2007-10-18 23:39:30 -07001741 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001742 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743}
1744
1745static struct file_system_type cgroup_fs_type = {
1746 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001747 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748 .kill_sb = cgroup_kill_sb,
1749};
1750
Greg KH676db4a2010-08-05 13:53:35 -07001751static struct kobject *cgroup_kobj;
1752
Li Zefana043e3b2008-02-23 15:24:09 -08001753/**
1754 * cgroup_path - generate the path of a cgroup
1755 * @cgrp: the cgroup in question
1756 * @buf: the buffer to write the path into
1757 * @buflen: the length of the buffer
1758 *
Li Zefan65dff752013-03-01 15:01:56 +08001759 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1760 *
1761 * We can't generate cgroup path using dentry->d_name, as accessing
1762 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1763 * inode's i_mutex, while on the other hand cgroup_path() can be called
1764 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001766int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767{
Li Zefan65dff752013-03-01 15:01:56 +08001768 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001770
Tejun Heoda1f2962013-04-14 10:32:19 -07001771 if (!cgrp->parent) {
1772 if (strlcpy(buf, "/", buflen) >= buflen)
1773 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 return 0;
1775 }
1776
Tao Ma316eb662012-11-08 21:36:38 +08001777 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001778 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001779
Li Zefan65dff752013-03-01 15:01:56 +08001780 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001781 do {
Li Zefan65dff752013-03-01 15:01:56 +08001782 const char *name = cgroup_name(cgrp);
1783 int len;
1784
1785 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001787 goto out;
1788 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001789
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001791 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001793
1794 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001795 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001796 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001798out:
1799 rcu_read_unlock();
1800 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801}
Ben Blum67523c42010-03-10 15:22:11 -08001802EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803
Tejun Heo857a2be2013-04-14 20:50:08 -07001804/**
1805 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1806 * @task: target task
1807 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1808 * @buf: the buffer to write the path into
1809 * @buflen: the length of the buffer
1810 *
1811 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1812 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1813 * be used inside locks used by cgroup controller callbacks.
1814 */
1815int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1816 char *buf, size_t buflen)
1817{
1818 struct cgroupfs_root *root;
1819 struct cgroup *cgrp = NULL;
1820 int ret = -ENOENT;
1821
1822 mutex_lock(&cgroup_mutex);
1823
1824 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1825 if (root) {
1826 cgrp = task_cgroup_from_root(task, root);
1827 ret = cgroup_path(cgrp, buf, buflen);
1828 }
1829
1830 mutex_unlock(&cgroup_mutex);
1831
1832 return ret;
1833}
1834EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1835
Ben Blum74a11662011-05-26 16:25:20 -07001836/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001837 * Control Group taskset
1838 */
Tejun Heo134d3372011-12-12 18:12:21 -08001839struct task_and_cgroup {
1840 struct task_struct *task;
1841 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001842 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001843};
1844
Tejun Heo2f7ee562011-12-12 18:12:21 -08001845struct cgroup_taskset {
1846 struct task_and_cgroup single;
1847 struct flex_array *tc_array;
1848 int tc_array_len;
1849 int idx;
1850 struct cgroup *cur_cgrp;
1851};
1852
1853/**
1854 * cgroup_taskset_first - reset taskset and return the first task
1855 * @tset: taskset of interest
1856 *
1857 * @tset iteration is initialized and the first task is returned.
1858 */
1859struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1860{
1861 if (tset->tc_array) {
1862 tset->idx = 0;
1863 return cgroup_taskset_next(tset);
1864 } else {
1865 tset->cur_cgrp = tset->single.cgrp;
1866 return tset->single.task;
1867 }
1868}
1869EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1870
1871/**
1872 * cgroup_taskset_next - iterate to the next task in taskset
1873 * @tset: taskset of interest
1874 *
1875 * Return the next task in @tset. Iteration must have been initialized
1876 * with cgroup_taskset_first().
1877 */
1878struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1879{
1880 struct task_and_cgroup *tc;
1881
1882 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1883 return NULL;
1884
1885 tc = flex_array_get(tset->tc_array, tset->idx++);
1886 tset->cur_cgrp = tc->cgrp;
1887 return tc->task;
1888}
1889EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1890
1891/**
1892 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1893 * @tset: taskset of interest
1894 *
1895 * Return the cgroup for the current (last returned) task of @tset. This
1896 * function must be preceded by either cgroup_taskset_first() or
1897 * cgroup_taskset_next().
1898 */
1899struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1900{
1901 return tset->cur_cgrp;
1902}
1903EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1904
1905/**
1906 * cgroup_taskset_size - return the number of tasks in taskset
1907 * @tset: taskset of interest
1908 */
1909int cgroup_taskset_size(struct cgroup_taskset *tset)
1910{
1911 return tset->tc_array ? tset->tc_array_len : 1;
1912}
1913EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1914
1915
Ben Blum74a11662011-05-26 16:25:20 -07001916/*
1917 * cgroup_task_migrate - move a task from one cgroup to another.
1918 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001919 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001920 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001921static void cgroup_task_migrate(struct cgroup *old_cgrp,
1922 struct task_struct *tsk,
1923 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001924{
Tejun Heo5abb8852013-06-12 21:04:49 -07001925 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001926
1927 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001928 * We are synchronized through threadgroup_lock() against PF_EXITING
1929 * setting such that we can't race against cgroup_exit() changing the
1930 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001931 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001932 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001933 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001934
Ben Blum74a11662011-05-26 16:25:20 -07001935 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001936 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001937 task_unlock(tsk);
1938
1939 /* Update the css_set linked lists if we're using them */
1940 write_lock(&css_set_lock);
1941 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001942 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001943 write_unlock(&css_set_lock);
1944
1945 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001946 * We just gained a reference on old_cset by taking it from the
1947 * task. As trading it for new_cset is protected by cgroup_mutex,
1948 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001949 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1951 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001952}
1953
Li Zefana043e3b2008-02-23 15:24:09 -08001954/**
Li Zefan081aa452013-03-13 09:17:09 +08001955 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001956 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001957 * @tsk: the task or the leader of the threadgroup to be attached
1958 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001959 *
Tejun Heo257058a2011-12-12 18:12:21 -08001960 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001961 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001963static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1964 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001965{
1966 int retval, i, group_size;
1967 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001968 struct cgroupfs_root *root = cgrp->root;
1969 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001970 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001971 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001972 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001973 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001974
1975 /*
1976 * step 0: in order to do expensive, possibly blocking operations for
1977 * every thread, we cannot iterate the thread group list, since it needs
1978 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001979 * group - group_rwsem prevents new threads from appearing, and if
1980 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001981 */
Li Zefan081aa452013-03-13 09:17:09 +08001982 if (threadgroup)
1983 group_size = get_nr_threads(tsk);
1984 else
1985 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001986 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001987 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001988 if (!group)
1989 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001990 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001991 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001992 if (retval)
1993 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001994
Ben Blum74a11662011-05-26 16:25:20 -07001995 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001996 /*
1997 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1998 * already PF_EXITING could be freed from underneath us unless we
1999 * take an rcu_read_lock.
2000 */
2001 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002002 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002003 struct task_and_cgroup ent;
2004
Tejun Heocd3d0952011-12-12 18:12:21 -08002005 /* @tsk either already exited or can't exit until the end */
2006 if (tsk->flags & PF_EXITING)
2007 continue;
2008
Ben Blum74a11662011-05-26 16:25:20 -07002009 /* as per above, nr_threads may decrease, but not increase. */
2010 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002011 ent.task = tsk;
2012 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002013 /* nothing to do if this task is already in the cgroup */
2014 if (ent.cgrp == cgrp)
2015 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002016 /*
2017 * saying GFP_ATOMIC has no effect here because we did prealloc
2018 * earlier, but it's good form to communicate our expectations.
2019 */
Tejun Heo134d3372011-12-12 18:12:21 -08002020 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002021 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002022 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002023
2024 if (!threadgroup)
2025 break;
Ben Blum74a11662011-05-26 16:25:20 -07002026 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002027 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002028 /* remember the number of threads in the array for later. */
2029 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002030 tset.tc_array = group;
2031 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002032
Tejun Heo134d3372011-12-12 18:12:21 -08002033 /* methods shouldn't be called if no task is actually migrating */
2034 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002035 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002036 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002037
Ben Blum74a11662011-05-26 16:25:20 -07002038 /*
2039 * step 1: check that we can legitimately attach to the cgroup.
2040 */
Tejun Heo5549c492013-06-24 15:21:48 -07002041 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002042 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002043 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002044 if (retval) {
2045 failed_ss = ss;
2046 goto out_cancel_attach;
2047 }
2048 }
Ben Blum74a11662011-05-26 16:25:20 -07002049 }
2050
2051 /*
2052 * step 2: make sure css_sets exist for all threads to be migrated.
2053 * we use find_css_set, which allocates a new one if necessary.
2054 */
Ben Blum74a11662011-05-26 16:25:20 -07002055 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002056 struct css_set *old_cset;
2057
Tejun Heo134d3372011-12-12 18:12:21 -08002058 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002059 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002060 tc->cset = find_css_set(old_cset, cgrp);
2061 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002062 retval = -ENOMEM;
2063 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002064 }
2065 }
2066
2067 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002068 * step 3: now that we're guaranteed success wrt the css_sets,
2069 * proceed to move all tasks to the new cgroup. There are no
2070 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002071 */
Ben Blum74a11662011-05-26 16:25:20 -07002072 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002073 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002074 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002075 }
2076 /* nothing is sensitive to fork() after this point. */
2077
2078 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002079 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002080 */
Tejun Heo5549c492013-06-24 15:21:48 -07002081 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002082 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002083 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085
2086 /*
2087 * step 5: success! and cleanup
2088 */
Ben Blum74a11662011-05-26 16:25:20 -07002089 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002090out_put_css_set_refs:
2091 if (retval) {
2092 for (i = 0; i < group_size; i++) {
2093 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002094 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002095 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002096 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097 }
Ben Blum74a11662011-05-26 16:25:20 -07002098 }
2099out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002101 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002102 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002103 break;
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002105 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107 }
Ben Blum74a11662011-05-26 16:25:20 -07002108out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002109 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002110 return retval;
2111}
2112
2113/*
2114 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002115 * function to attach either it or all tasks in its threadgroup. Will lock
2116 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002117 */
2118static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002119{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002121 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002122 int ret;
2123
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (!cgroup_lock_live_group(cgrp))
2125 return -ENODEV;
2126
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002127retry_find_task:
2128 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002130 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002131 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002133 ret= -ESRCH;
2134 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136 /*
2137 * even if we're attaching all tasks in the thread group, we
2138 * only need to check permissions on one of them.
2139 */
David Howellsc69e8d92008-11-14 10:39:19 +11002140 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002141 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2142 !uid_eq(cred->euid, tcred->uid) &&
2143 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002144 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002145 ret = -EACCES;
2146 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002147 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002148 } else
2149 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002150
2151 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002153
2154 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002155 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002156 * trapped in a cpuset, or RT worker may be born in a cgroup
2157 * with no rt_runtime allocated. Just say no.
2158 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002159 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002160 ret = -EINVAL;
2161 rcu_read_unlock();
2162 goto out_unlock_cgroup;
2163 }
2164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 get_task_struct(tsk);
2166 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 threadgroup_lock(tsk);
2169 if (threadgroup) {
2170 if (!thread_group_leader(tsk)) {
2171 /*
2172 * a race with de_thread from another thread's exec()
2173 * may strip us of our leadership, if this happens,
2174 * there is no choice but to throw this task away and
2175 * try again; this is
2176 * "double-double-toil-and-trouble-check locking".
2177 */
2178 threadgroup_unlock(tsk);
2179 put_task_struct(tsk);
2180 goto retry_find_task;
2181 }
Li Zefan081aa452013-03-13 09:17:09 +08002182 }
2183
2184 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2185
Tejun Heocd3d0952011-12-12 18:12:21 -08002186 threadgroup_unlock(tsk);
2187
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002190 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002191 return ret;
2192}
2193
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002194/**
2195 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2196 * @from: attach to all cgroups of a given task
2197 * @tsk: the task to be attached
2198 */
2199int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2200{
2201 struct cgroupfs_root *root;
2202 int retval = 0;
2203
Tejun Heo47cfcd02013-04-07 09:29:51 -07002204 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002205 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002206 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002207
Li Zefan6f4b7e62013-07-31 16:18:36 +08002208 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002209 if (retval)
2210 break;
2211 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213
2214 return retval;
2215}
2216EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2217
Paul Menageaf351022008-07-25 01:47:01 -07002218static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2219{
Ben Blum74a11662011-05-26 16:25:20 -07002220 return attach_task_by_pid(cgrp, pid, false);
2221}
2222
2223static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2224{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002225 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002226}
2227
Paul Menagee788e062008-07-25 01:46:59 -07002228static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2229 const char *buffer)
2230{
2231 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002232 if (strlen(buffer) >= PATH_MAX)
2233 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002234 if (!cgroup_lock_live_group(cgrp))
2235 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002236 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002237 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002238 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002239 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002240 return 0;
2241}
2242
2243static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2244 struct seq_file *seq)
2245{
2246 if (!cgroup_lock_live_group(cgrp))
2247 return -ENODEV;
2248 seq_puts(seq, cgrp->root->release_agent_path);
2249 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002250 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002251 return 0;
2252}
2253
Tejun Heo873fe092013-04-14 20:15:26 -07002254static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2255 struct seq_file *seq)
2256{
2257 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002258 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002259}
2260
Paul Menage84eea842008-07-25 01:47:00 -07002261/* A buffer size big enough for numbers or short strings */
2262#define CGROUP_LOCAL_BUFFER_SIZE 64
2263
Paul Menagee73d2c62008-04-29 01:00:06 -07002264static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002265 struct file *file,
2266 const char __user *userbuf,
2267 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002268{
Paul Menage84eea842008-07-25 01:47:00 -07002269 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002270 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002271 char *end;
2272
2273 if (!nbytes)
2274 return -EINVAL;
2275 if (nbytes >= sizeof(buffer))
2276 return -E2BIG;
2277 if (copy_from_user(buffer, userbuf, nbytes))
2278 return -EFAULT;
2279
2280 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002281 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002282 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002283 if (*end)
2284 return -EINVAL;
2285 retval = cft->write_u64(cgrp, cft, val);
2286 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002287 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002288 if (*end)
2289 return -EINVAL;
2290 retval = cft->write_s64(cgrp, cft, val);
2291 }
Paul Menage355e0c42007-10-18 23:39:33 -07002292 if (!retval)
2293 retval = nbytes;
2294 return retval;
2295}
2296
Paul Menagedb3b1492008-07-25 01:46:58 -07002297static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2298 struct file *file,
2299 const char __user *userbuf,
2300 size_t nbytes, loff_t *unused_ppos)
2301{
Paul Menage84eea842008-07-25 01:47:00 -07002302 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002303 int retval = 0;
2304 size_t max_bytes = cft->max_write_len;
2305 char *buffer = local_buffer;
2306
2307 if (!max_bytes)
2308 max_bytes = sizeof(local_buffer) - 1;
2309 if (nbytes >= max_bytes)
2310 return -E2BIG;
2311 /* Allocate a dynamic buffer if we need one */
2312 if (nbytes >= sizeof(local_buffer)) {
2313 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2314 if (buffer == NULL)
2315 return -ENOMEM;
2316 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002317 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2318 retval = -EFAULT;
2319 goto out;
2320 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002321
2322 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002324 if (!retval)
2325 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002326out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002327 if (buffer != local_buffer)
2328 kfree(buffer);
2329 return retval;
2330}
2331
Paul Menageddbcc7e2007-10-18 23:39:30 -07002332static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2333 size_t nbytes, loff_t *ppos)
2334{
2335 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002336 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002337
Tejun Heo54766d42013-06-12 21:04:53 -07002338 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002339 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002340 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002341 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002342 if (cft->write_u64 || cft->write_s64)
2343 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 if (cft->write_string)
2345 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002346 if (cft->trigger) {
2347 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2348 return ret ? ret : nbytes;
2349 }
Paul Menage355e0c42007-10-18 23:39:33 -07002350 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351}
2352
Paul Menagef4c753b2008-04-29 00:59:56 -07002353static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2354 struct file *file,
2355 char __user *buf, size_t nbytes,
2356 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357{
Paul Menage84eea842008-07-25 01:47:00 -07002358 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002359 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002360 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2361
2362 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2363}
2364
Paul Menagee73d2c62008-04-29 01:00:06 -07002365static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2366 struct file *file,
2367 char __user *buf, size_t nbytes,
2368 loff_t *ppos)
2369{
Paul Menage84eea842008-07-25 01:47:00 -07002370 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002371 s64 val = cft->read_s64(cgrp, cft);
2372 int len = sprintf(tmp, "%lld\n", (long long) val);
2373
2374 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2375}
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2378 size_t nbytes, loff_t *ppos)
2379{
2380 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002381 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382
Tejun Heo54766d42013-06-12 21:04:53 -07002383 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002384 return -ENODEV;
2385
2386 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002387 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002388 if (cft->read_u64)
2389 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002390 if (cft->read_s64)
2391 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392 return -EINVAL;
2393}
2394
Paul Menage91796562008-04-29 01:00:01 -07002395/*
2396 * seqfile ops/methods for returning structured data. Currently just
2397 * supports string->u64 maps, but can be extended in future.
2398 */
2399
Paul Menage91796562008-04-29 01:00:01 -07002400static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2401{
2402 struct seq_file *sf = cb->state;
2403 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2404}
2405
2406static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2407{
Li Zefane0798ce2013-07-31 17:36:25 +08002408 struct cfent *cfe = m->private;
2409 struct cftype *cft = cfe->type;
2410 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2411
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002412 if (cft->read_map) {
2413 struct cgroup_map_cb cb = {
2414 .fill = cgroup_map_add,
2415 .state = m,
2416 };
Li Zefane0798ce2013-07-31 17:36:25 +08002417 return cft->read_map(cgrp, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002418 }
Li Zefane0798ce2013-07-31 17:36:25 +08002419 return cft->read_seq_string(cgrp, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002420}
2421
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002422static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002423 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002424 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002425 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002426 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002427};
2428
Paul Menageddbcc7e2007-10-18 23:39:30 -07002429static int cgroup_file_open(struct inode *inode, struct file *file)
2430{
2431 int err;
Li Zefane0798ce2013-07-31 17:36:25 +08002432 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 struct cftype *cft;
2434
2435 err = generic_file_open(inode, file);
2436 if (err)
2437 return err;
Li Zefane0798ce2013-07-31 17:36:25 +08002438 cfe = __d_cfe(file->f_dentry);
2439 cft = cfe->type;
Li Zefan75139b82009-01-07 18:07:33 -08002440
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002441 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002442 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002443 err = single_open(file, cgroup_seqfile_show, cfe);
2444 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002445 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002446 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002447
2448 return err;
2449}
2450
2451static int cgroup_file_release(struct inode *inode, struct file *file)
2452{
2453 struct cftype *cft = __d_cft(file->f_dentry);
2454 if (cft->release)
2455 return cft->release(inode, file);
2456 return 0;
2457}
2458
2459/*
2460 * cgroup_rename - Only allow simple rename of directories in place.
2461 */
2462static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2463 struct inode *new_dir, struct dentry *new_dentry)
2464{
Li Zefan65dff752013-03-01 15:01:56 +08002465 int ret;
2466 struct cgroup_name *name, *old_name;
2467 struct cgroup *cgrp;
2468
2469 /*
2470 * It's convinient to use parent dir's i_mutex to protected
2471 * cgrp->name.
2472 */
2473 lockdep_assert_held(&old_dir->i_mutex);
2474
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2476 return -ENOTDIR;
2477 if (new_dentry->d_inode)
2478 return -EEXIST;
2479 if (old_dir != new_dir)
2480 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002481
2482 cgrp = __d_cgrp(old_dentry);
2483
Tejun Heo6db8e852013-06-14 11:18:22 -07002484 /*
2485 * This isn't a proper migration and its usefulness is very
2486 * limited. Disallow if sane_behavior.
2487 */
2488 if (cgroup_sane_behavior(cgrp))
2489 return -EPERM;
2490
Li Zefan65dff752013-03-01 15:01:56 +08002491 name = cgroup_alloc_name(new_dentry);
2492 if (!name)
2493 return -ENOMEM;
2494
2495 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2496 if (ret) {
2497 kfree(name);
2498 return ret;
2499 }
2500
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002501 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002502 rcu_assign_pointer(cgrp->name, name);
2503
2504 kfree_rcu(old_name, rcu_head);
2505 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002506}
2507
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002508static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2509{
2510 if (S_ISDIR(dentry->d_inode->i_mode))
2511 return &__d_cgrp(dentry)->xattrs;
2512 else
Li Zefan712317a2013-04-18 23:09:52 -07002513 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002514}
2515
2516static inline int xattr_enabled(struct dentry *dentry)
2517{
2518 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002519 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002520}
2521
2522static bool is_valid_xattr(const char *name)
2523{
2524 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2525 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2526 return true;
2527 return false;
2528}
2529
2530static int cgroup_setxattr(struct dentry *dentry, const char *name,
2531 const void *val, size_t size, int flags)
2532{
2533 if (!xattr_enabled(dentry))
2534 return -EOPNOTSUPP;
2535 if (!is_valid_xattr(name))
2536 return -EINVAL;
2537 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2538}
2539
2540static int cgroup_removexattr(struct dentry *dentry, const char *name)
2541{
2542 if (!xattr_enabled(dentry))
2543 return -EOPNOTSUPP;
2544 if (!is_valid_xattr(name))
2545 return -EINVAL;
2546 return simple_xattr_remove(__d_xattrs(dentry), name);
2547}
2548
2549static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2550 void *buf, size_t size)
2551{
2552 if (!xattr_enabled(dentry))
2553 return -EOPNOTSUPP;
2554 if (!is_valid_xattr(name))
2555 return -EINVAL;
2556 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2557}
2558
2559static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2560{
2561 if (!xattr_enabled(dentry))
2562 return -EOPNOTSUPP;
2563 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2564}
2565
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002566static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002567 .read = cgroup_file_read,
2568 .write = cgroup_file_write,
2569 .llseek = generic_file_llseek,
2570 .open = cgroup_file_open,
2571 .release = cgroup_file_release,
2572};
2573
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002574static const struct inode_operations cgroup_file_inode_operations = {
2575 .setxattr = cgroup_setxattr,
2576 .getxattr = cgroup_getxattr,
2577 .listxattr = cgroup_listxattr,
2578 .removexattr = cgroup_removexattr,
2579};
2580
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002581static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002582 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002583 .mkdir = cgroup_mkdir,
2584 .rmdir = cgroup_rmdir,
2585 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002586 .setxattr = cgroup_setxattr,
2587 .getxattr = cgroup_getxattr,
2588 .listxattr = cgroup_listxattr,
2589 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002590};
2591
Al Viro00cd8dd2012-06-10 17:13:09 -04002592static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002593{
2594 if (dentry->d_name.len > NAME_MAX)
2595 return ERR_PTR(-ENAMETOOLONG);
2596 d_add(dentry, NULL);
2597 return NULL;
2598}
2599
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002600/*
2601 * Check if a file is a control file
2602 */
2603static inline struct cftype *__file_cft(struct file *file)
2604{
Al Viro496ad9a2013-01-23 17:07:38 -05002605 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002606 return ERR_PTR(-EINVAL);
2607 return __d_cft(file->f_dentry);
2608}
2609
Al Viroa5e7ed32011-07-26 01:55:55 -04002610static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002611 struct super_block *sb)
2612{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002613 struct inode *inode;
2614
2615 if (!dentry)
2616 return -ENOENT;
2617 if (dentry->d_inode)
2618 return -EEXIST;
2619
2620 inode = cgroup_new_inode(mode, sb);
2621 if (!inode)
2622 return -ENOMEM;
2623
2624 if (S_ISDIR(mode)) {
2625 inode->i_op = &cgroup_dir_inode_operations;
2626 inode->i_fop = &simple_dir_operations;
2627
2628 /* start off with i_nlink == 2 (for "." entry) */
2629 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002630 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631
Tejun Heob8a2df62012-11-19 08:13:37 -08002632 /*
2633 * Control reaches here with cgroup_mutex held.
2634 * @inode->i_mutex should nest outside cgroup_mutex but we
2635 * want to populate it immediately without releasing
2636 * cgroup_mutex. As @inode isn't visible to anyone else
2637 * yet, trylock will always succeed without affecting
2638 * lockdep checks.
2639 */
2640 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002641 } else if (S_ISREG(mode)) {
2642 inode->i_size = 0;
2643 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002644 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002645 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002646 d_instantiate(dentry, inode);
2647 dget(dentry); /* Extra count - pin the dentry in core */
2648 return 0;
2649}
2650
Li Zefan099fca32009-04-02 16:57:29 -07002651/**
2652 * cgroup_file_mode - deduce file mode of a control file
2653 * @cft: the control file in question
2654 *
2655 * returns cft->mode if ->mode is not 0
2656 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2657 * returns S_IRUGO if it has only a read handler
2658 * returns S_IWUSR if it has only a write hander
2659 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002660static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002661{
Al Viroa5e7ed32011-07-26 01:55:55 -04002662 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002663
2664 if (cft->mode)
2665 return cft->mode;
2666
2667 if (cft->read || cft->read_u64 || cft->read_s64 ||
2668 cft->read_map || cft->read_seq_string)
2669 mode |= S_IRUGO;
2670
2671 if (cft->write || cft->write_u64 || cft->write_s64 ||
2672 cft->write_string || cft->trigger)
2673 mode |= S_IWUSR;
2674
2675 return mode;
2676}
2677
Tejun Heodb0416b2012-04-01 12:09:55 -07002678static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002679 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680{
Paul Menagebd89aab2007-10-18 23:40:44 -07002681 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002682 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002683 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002684 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002686 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002688
Tejun Heo93438622013-04-14 20:15:25 -07002689 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 strcpy(name, subsys->name);
2691 strcat(name, ".");
2692 }
2693 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002694
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002696
2697 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2698 if (!cfe)
2699 return -ENOMEM;
2700
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002704 goto out;
2705 }
2706
Li Zefand6cbf352013-05-14 19:44:20 +08002707 cfe->type = (void *)cft;
2708 cfe->dentry = dentry;
2709 dentry->d_fsdata = cfe;
2710 simple_xattrs_init(&cfe->xattrs);
2711
Tejun Heo05ef1d72012-04-01 12:09:56 -07002712 mode = cgroup_file_mode(cft);
2713 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2714 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002715 list_add_tail(&cfe->node, &parent->files);
2716 cfe = NULL;
2717 }
2718 dput(dentry);
2719out:
2720 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 return error;
2722}
2723
Tejun Heob1f28d32013-06-28 16:24:10 -07002724/**
2725 * cgroup_addrm_files - add or remove files to a cgroup directory
2726 * @cgrp: the target cgroup
2727 * @subsys: the subsystem of files to be added
2728 * @cfts: array of cftypes to be added
2729 * @is_add: whether to add or remove
2730 *
2731 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2732 * All @cfts should belong to @subsys. For removals, this function never
2733 * fails. If addition fails, this function doesn't remove files already
2734 * added. The caller is responsible for cleaning up.
2735 */
Tejun Heo79578622012-04-01 12:09:56 -07002736static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002737 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002738{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002739 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002740 int ret;
2741
2742 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2743 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002744
2745 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002746 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002747 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2748 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002749 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2750 continue;
2751 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2752 continue;
2753
Li Zefan2739d3c2013-01-21 18:18:33 +08002754 if (is_add) {
Tejun Heob1f28d32013-06-28 16:24:10 -07002755 ret = cgroup_add_file(cgrp, subsys, cft);
2756 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002757 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002758 cft->name, ret);
2759 return ret;
2760 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002761 } else {
2762 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002763 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002765 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766}
2767
Tejun Heo8e3f6542012-04-01 12:09:55 -07002768static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002769 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002770{
2771 /*
2772 * Thanks to the entanglement with vfs inode locking, we can't walk
2773 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002774 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2775 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002776 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002777 mutex_lock(&cgroup_mutex);
2778}
2779
Tejun Heo9ccece82013-06-28 16:24:11 -07002780static int cgroup_cfts_commit(struct cgroup_subsys *ss,
2781 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002782 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002783{
2784 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002785 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002786 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002787 struct dentry *prev = NULL;
2788 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002789 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002790 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791
2792 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002793 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002794 !atomic_inc_not_zero(&sb->s_active)) {
2795 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002796 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 }
2798
Li Zefane8c82d22013-06-18 18:48:37 +08002799 /*
2800 * All cgroups which are created after we drop cgroup_mutex will
2801 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002802 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002803 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002804 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002805
Tejun Heo8e3f6542012-04-01 12:09:55 -07002806 mutex_unlock(&cgroup_mutex);
2807
Li Zefane8c82d22013-06-18 18:48:37 +08002808 /* @root always needs to be updated */
2809 inode = root->dentry->d_inode;
2810 mutex_lock(&inode->i_mutex);
2811 mutex_lock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002812 ret = cgroup_addrm_files(root, ss, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002813 mutex_unlock(&cgroup_mutex);
2814 mutex_unlock(&inode->i_mutex);
2815
Tejun Heo9ccece82013-06-28 16:24:11 -07002816 if (ret)
2817 goto out_deact;
2818
Li Zefane8c82d22013-06-18 18:48:37 +08002819 /* add/rm files for all cgroups created before */
2820 rcu_read_lock();
2821 cgroup_for_each_descendant_pre(cgrp, root) {
2822 if (cgroup_is_dead(cgrp))
2823 continue;
2824
2825 inode = cgrp->dentry->d_inode;
2826 dget(cgrp->dentry);
2827 rcu_read_unlock();
2828
2829 dput(prev);
2830 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002831
2832 mutex_lock(&inode->i_mutex);
2833 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002834 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo9ccece82013-06-28 16:24:11 -07002835 ret = cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002836 mutex_unlock(&cgroup_mutex);
2837 mutex_unlock(&inode->i_mutex);
2838
Li Zefane8c82d22013-06-18 18:48:37 +08002839 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002840 if (ret)
2841 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002842 }
Li Zefane8c82d22013-06-18 18:48:37 +08002843 rcu_read_unlock();
2844 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002845out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002846 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002847 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848}
2849
2850/**
2851 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2852 * @ss: target cgroup subsystem
2853 * @cfts: zero-length name terminated array of cftypes
2854 *
2855 * Register @cfts to @ss. Files described by @cfts are created for all
2856 * existing cgroups to which @ss is attached and all future cgroups will
2857 * have them too. This function can be called anytime whether @ss is
2858 * attached or not.
2859 *
2860 * Returns 0 on successful registration, -errno on failure. Note that this
2861 * function currently returns 0 as long as @cfts registration is successful
2862 * even if some file creation attempts on existing cgroups fail.
2863 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002864int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865{
2866 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002867 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002868
2869 set = kzalloc(sizeof(*set), GFP_KERNEL);
2870 if (!set)
2871 return -ENOMEM;
2872
2873 cgroup_cfts_prepare();
2874 set->cfts = cfts;
2875 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo9ccece82013-06-28 16:24:11 -07002876 ret = cgroup_cfts_commit(ss, cfts, true);
2877 if (ret)
2878 cgroup_rm_cftypes(ss, cfts);
2879 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002880}
2881EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2882
Li Zefana043e3b2008-02-23 15:24:09 -08002883/**
Tejun Heo79578622012-04-01 12:09:56 -07002884 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2885 * @ss: target cgroup subsystem
2886 * @cfts: zero-length name terminated array of cftypes
2887 *
2888 * Unregister @cfts from @ss. Files described by @cfts are removed from
2889 * all existing cgroups to which @ss is attached and all future cgroups
2890 * won't have them either. This function can be called anytime whether @ss
2891 * is attached or not.
2892 *
2893 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2894 * registered with @ss.
2895 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002896int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002897{
2898 struct cftype_set *set;
2899
2900 cgroup_cfts_prepare();
2901
2902 list_for_each_entry(set, &ss->cftsets, node) {
2903 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002904 list_del(&set->node);
2905 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002906 cgroup_cfts_commit(ss, cfts, false);
2907 return 0;
2908 }
2909 }
2910
2911 cgroup_cfts_commit(ss, NULL, false);
2912 return -ENOENT;
2913}
2914
2915/**
Li Zefana043e3b2008-02-23 15:24:09 -08002916 * cgroup_task_count - count the number of tasks in a cgroup.
2917 * @cgrp: the cgroup in question
2918 *
2919 * Return the number of tasks in the cgroup.
2920 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002921int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002922{
2923 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002924 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002925
Paul Menage817929e2007-10-18 23:39:36 -07002926 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002927 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2928 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002929 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002930 return count;
2931}
2932
2933/*
Paul Menage817929e2007-10-18 23:39:36 -07002934 * Advance a list_head iterator. The iterator should be positioned at
2935 * the start of a css_set
2936 */
Tejun Heo69d02062013-06-12 21:04:50 -07002937static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002938{
Tejun Heo69d02062013-06-12 21:04:50 -07002939 struct list_head *l = it->cset_link;
2940 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002941 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002942
2943 /* Advance to the next non-empty css_set */
2944 do {
2945 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002946 if (l == &cgrp->cset_links) {
2947 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002948 return;
2949 }
Tejun Heo69d02062013-06-12 21:04:50 -07002950 link = list_entry(l, struct cgrp_cset_link, cset_link);
2951 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002952 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002953 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002954 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002955}
2956
Cliff Wickman31a7df02008-02-07 00:14:42 -08002957/*
2958 * To reduce the fork() overhead for systems that are not actually
2959 * using their cgroups capability, we don't maintain the lists running
2960 * through each css_set to its tasks until we see the list actually
2961 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002962 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002963static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002964{
2965 struct task_struct *p, *g;
2966 write_lock(&css_set_lock);
2967 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002968 /*
2969 * We need tasklist_lock because RCU is not safe against
2970 * while_each_thread(). Besides, a forking task that has passed
2971 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2972 * is not guaranteed to have its child immediately visible in the
2973 * tasklist if we walk through it with RCU.
2974 */
2975 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002976 do_each_thread(g, p) {
2977 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002978 /*
2979 * We should check if the process is exiting, otherwise
2980 * it will race with cgroup_exit() in that the list
2981 * entry won't be deleted though the process has exited.
2982 */
2983 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002984 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002985 task_unlock(p);
2986 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002987 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002988 write_unlock(&css_set_lock);
2989}
2990
Tejun Heo574bd9f2012-11-09 09:12:29 -08002991/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002992 * cgroup_next_sibling - find the next sibling of a given cgroup
2993 * @pos: the current cgroup
2994 *
2995 * This function returns the next sibling of @pos and should be called
2996 * under RCU read lock. The only requirement is that @pos is accessible.
2997 * The next sibling is guaranteed to be returned regardless of @pos's
2998 * state.
2999 */
3000struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3001{
3002 struct cgroup *next;
3003
3004 WARN_ON_ONCE(!rcu_read_lock_held());
3005
3006 /*
3007 * @pos could already have been removed. Once a cgroup is removed,
3008 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003009 * changes. As CGRP_DEAD assertion is serialized and happens
3010 * before the cgroup is taken off the ->sibling list, if we see it
3011 * unasserted, it's guaranteed that the next sibling hasn't
3012 * finished its grace period even if it's already removed, and thus
3013 * safe to dereference from this RCU critical section. If
3014 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3015 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003016 */
Tejun Heo54766d42013-06-12 21:04:53 -07003017 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003018 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3019 if (&next->sibling != &pos->parent->children)
3020 return next;
3021 return NULL;
3022 }
3023
3024 /*
3025 * Can't dereference the next pointer. Each cgroup is given a
3026 * monotonically increasing unique serial number and always
3027 * appended to the sibling list, so the next one can be found by
3028 * walking the parent's children until we see a cgroup with higher
3029 * serial number than @pos's.
3030 *
3031 * While this path can be slow, it's taken only when either the
3032 * current cgroup is removed or iteration and removal race.
3033 */
3034 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3035 if (next->serial_nr > pos->serial_nr)
3036 return next;
3037 return NULL;
3038}
3039EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3040
3041/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003042 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3043 * @pos: the current position (%NULL to initiate traversal)
3044 * @cgroup: cgroup whose descendants to walk
3045 *
3046 * To be used by cgroup_for_each_descendant_pre(). Find the next
3047 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003048 *
3049 * While this function requires RCU read locking, it doesn't require the
3050 * whole traversal to be contained in a single RCU critical section. This
3051 * function will return the correct next descendant as long as both @pos
3052 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003053 */
3054struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3055 struct cgroup *cgroup)
3056{
3057 struct cgroup *next;
3058
3059 WARN_ON_ONCE(!rcu_read_lock_held());
3060
3061 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003062 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064
3065 /* visit the first child if exists */
3066 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3067 if (next)
3068 return next;
3069
3070 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003071 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003072 next = cgroup_next_sibling(pos);
3073 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003076 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077
3078 return NULL;
3079}
3080EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3081
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003082/**
3083 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3084 * @pos: cgroup of interest
3085 *
3086 * Return the rightmost descendant of @pos. If there's no descendant,
3087 * @pos is returned. This can be used during pre-order traversal to skip
3088 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003089 *
3090 * While this function requires RCU read locking, it doesn't require the
3091 * whole traversal to be contained in a single RCU critical section. This
3092 * function will return the correct rightmost descendant as long as @pos is
3093 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003094 */
3095struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3096{
3097 struct cgroup *last, *tmp;
3098
3099 WARN_ON_ONCE(!rcu_read_lock_held());
3100
3101 do {
3102 last = pos;
3103 /* ->prev isn't RCU safe, walk ->next till the end */
3104 pos = NULL;
3105 list_for_each_entry_rcu(tmp, &last->children, sibling)
3106 pos = tmp;
3107 } while (pos);
3108
3109 return last;
3110}
3111EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3112
Tejun Heo574bd9f2012-11-09 09:12:29 -08003113static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3114{
3115 struct cgroup *last;
3116
3117 do {
3118 last = pos;
3119 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3120 sibling);
3121 } while (pos);
3122
3123 return last;
3124}
3125
3126/**
3127 * cgroup_next_descendant_post - find the next descendant for post-order walk
3128 * @pos: the current position (%NULL to initiate traversal)
3129 * @cgroup: cgroup whose descendants to walk
3130 *
3131 * To be used by cgroup_for_each_descendant_post(). Find the next
3132 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003133 *
3134 * While this function requires RCU read locking, it doesn't require the
3135 * whole traversal to be contained in a single RCU critical section. This
3136 * function will return the correct next descendant as long as both @pos
3137 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003138 */
3139struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3140 struct cgroup *cgroup)
3141{
3142 struct cgroup *next;
3143
3144 WARN_ON_ONCE(!rcu_read_lock_held());
3145
3146 /* if first iteration, visit the leftmost descendant */
3147 if (!pos) {
3148 next = cgroup_leftmost_descendant(cgroup);
3149 return next != cgroup ? next : NULL;
3150 }
3151
3152 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003153 next = cgroup_next_sibling(pos);
3154 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003155 return cgroup_leftmost_descendant(next);
3156
3157 /* no sibling left, visit parent */
3158 next = pos->parent;
3159 return next != cgroup ? next : NULL;
3160}
3161EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3162
Paul Menagebd89aab2007-10-18 23:40:44 -07003163void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003164 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003165{
3166 /*
3167 * The first time anyone tries to iterate across a cgroup,
3168 * we need to enable the list linking each css_set to its
3169 * tasks, and fix up all existing tasks.
3170 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003171 if (!use_task_css_set_links)
3172 cgroup_enable_task_cg_lists();
3173
Paul Menage817929e2007-10-18 23:39:36 -07003174 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003175 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003176 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003177}
3178
Paul Menagebd89aab2007-10-18 23:40:44 -07003179struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003180 struct cgroup_iter *it)
3181{
3182 struct task_struct *res;
3183 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003184 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003185
3186 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003187 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003188 return NULL;
3189 res = list_entry(l, struct task_struct, cg_list);
3190 /* Advance iterator to find next entry */
3191 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003192 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3193 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003194 /* We reached the end of this task list - move on to
3195 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003196 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003197 } else {
3198 it->task = l;
3199 }
3200 return res;
3201}
3202
Paul Menagebd89aab2007-10-18 23:40:44 -07003203void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003204 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003205{
3206 read_unlock(&css_set_lock);
3207}
3208
Cliff Wickman31a7df02008-02-07 00:14:42 -08003209static inline int started_after_time(struct task_struct *t1,
3210 struct timespec *time,
3211 struct task_struct *t2)
3212{
3213 int start_diff = timespec_compare(&t1->start_time, time);
3214 if (start_diff > 0) {
3215 return 1;
3216 } else if (start_diff < 0) {
3217 return 0;
3218 } else {
3219 /*
3220 * Arbitrarily, if two processes started at the same
3221 * time, we'll say that the lower pointer value
3222 * started first. Note that t2 may have exited by now
3223 * so this may not be a valid pointer any longer, but
3224 * that's fine - it still serves to distinguish
3225 * between two tasks started (effectively) simultaneously.
3226 */
3227 return t1 > t2;
3228 }
3229}
3230
3231/*
3232 * This function is a callback from heap_insert() and is used to order
3233 * the heap.
3234 * In this case we order the heap in descending task start time.
3235 */
3236static inline int started_after(void *p1, void *p2)
3237{
3238 struct task_struct *t1 = p1;
3239 struct task_struct *t2 = p2;
3240 return started_after_time(t1, &t2->start_time, t2);
3241}
3242
3243/**
3244 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3245 * @scan: struct cgroup_scanner containing arguments for the scan
3246 *
3247 * Arguments include pointers to callback functions test_task() and
3248 * process_task().
3249 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3250 * and if it returns true, call process_task() for it also.
3251 * The test_task pointer may be NULL, meaning always true (select all tasks).
3252 * Effectively duplicates cgroup_iter_{start,next,end}()
3253 * but does not lock css_set_lock for the call to process_task().
3254 * The struct cgroup_scanner may be embedded in any structure of the caller's
3255 * creation.
3256 * It is guaranteed that process_task() will act on every task that
3257 * is a member of the cgroup for the duration of this call. This
3258 * function may or may not call process_task() for tasks that exit
3259 * or move to a different cgroup during the call, or are forked or
3260 * move into the cgroup during the call.
3261 *
3262 * Note that test_task() may be called with locks held, and may in some
3263 * situations be called multiple times for the same task, so it should
3264 * be cheap.
3265 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3266 * pre-allocated and will be used for heap operations (and its "gt" member will
3267 * be overwritten), else a temporary heap will be used (allocation of which
3268 * may cause this function to fail).
3269 */
3270int cgroup_scan_tasks(struct cgroup_scanner *scan)
3271{
3272 int retval, i;
3273 struct cgroup_iter it;
3274 struct task_struct *p, *dropped;
3275 /* Never dereference latest_task, since it's not refcounted */
3276 struct task_struct *latest_task = NULL;
3277 struct ptr_heap tmp_heap;
3278 struct ptr_heap *heap;
3279 struct timespec latest_time = { 0, 0 };
3280
3281 if (scan->heap) {
3282 /* The caller supplied our heap and pre-allocated its memory */
3283 heap = scan->heap;
3284 heap->gt = &started_after;
3285 } else {
3286 /* We need to allocate our own heap memory */
3287 heap = &tmp_heap;
3288 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3289 if (retval)
3290 /* cannot allocate the heap */
3291 return retval;
3292 }
3293
3294 again:
3295 /*
3296 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3297 * to determine which are of interest, and using the scanner's
3298 * "process_task" callback to process any of them that need an update.
3299 * Since we don't want to hold any locks during the task updates,
3300 * gather tasks to be processed in a heap structure.
3301 * The heap is sorted by descending task start time.
3302 * If the statically-sized heap fills up, we overflow tasks that
3303 * started later, and in future iterations only consider tasks that
3304 * started after the latest task in the previous pass. This
3305 * guarantees forward progress and that we don't miss any tasks.
3306 */
3307 heap->size = 0;
Li Zefan6f4b7e62013-07-31 16:18:36 +08003308 cgroup_iter_start(scan->cgrp, &it);
3309 while ((p = cgroup_iter_next(scan->cgrp, &it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003310 /*
3311 * Only affect tasks that qualify per the caller's callback,
3312 * if he provided one
3313 */
3314 if (scan->test_task && !scan->test_task(p, scan))
3315 continue;
3316 /*
3317 * Only process tasks that started after the last task
3318 * we processed
3319 */
3320 if (!started_after_time(p, &latest_time, latest_task))
3321 continue;
3322 dropped = heap_insert(heap, p);
3323 if (dropped == NULL) {
3324 /*
3325 * The new task was inserted; the heap wasn't
3326 * previously full
3327 */
3328 get_task_struct(p);
3329 } else if (dropped != p) {
3330 /*
3331 * The new task was inserted, and pushed out a
3332 * different task
3333 */
3334 get_task_struct(p);
3335 put_task_struct(dropped);
3336 }
3337 /*
3338 * Else the new task was newer than anything already in
3339 * the heap and wasn't inserted
3340 */
3341 }
Li Zefan6f4b7e62013-07-31 16:18:36 +08003342 cgroup_iter_end(scan->cgrp, &it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003343
3344 if (heap->size) {
3345 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003346 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003347 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003348 latest_time = q->start_time;
3349 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350 }
3351 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003352 scan->process_task(q, scan);
3353 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003354 }
3355 /*
3356 * If we had to process any tasks at all, scan again
3357 * in case some of them were in the middle of forking
3358 * children that didn't get processed.
3359 * Not the most efficient way to do it, but it avoids
3360 * having to take callback_mutex in the fork path
3361 */
3362 goto again;
3363 }
3364 if (heap == &tmp_heap)
3365 heap_free(&tmp_heap);
3366 return 0;
3367}
3368
Tejun Heo8cc99342013-04-07 09:29:50 -07003369static void cgroup_transfer_one_task(struct task_struct *task,
3370 struct cgroup_scanner *scan)
3371{
3372 struct cgroup *new_cgroup = scan->data;
3373
Tejun Heo47cfcd02013-04-07 09:29:51 -07003374 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003375 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003376 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003377}
3378
3379/**
3380 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3381 * @to: cgroup to which the tasks will be moved
3382 * @from: cgroup in which the tasks currently reside
3383 */
3384int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3385{
3386 struct cgroup_scanner scan;
3387
Li Zefan6f4b7e62013-07-31 16:18:36 +08003388 scan.cgrp = from;
Tejun Heo8cc99342013-04-07 09:29:50 -07003389 scan.test_task = NULL; /* select all tasks in cgroup */
3390 scan.process_task = cgroup_transfer_one_task;
3391 scan.heap = NULL;
3392 scan.data = to;
3393
3394 return cgroup_scan_tasks(&scan);
3395}
3396
Paul Menage817929e2007-10-18 23:39:36 -07003397/*
Ben Blum102a7752009-09-23 15:56:26 -07003398 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003399 *
3400 * Reading this file can return large amounts of data if a cgroup has
3401 * *lots* of attached tasks. So it may need several calls to read(),
3402 * but we cannot guarantee that the information we produce is correct
3403 * unless we produce it entirely atomically.
3404 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003405 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003406
Li Zefan24528252012-01-20 11:58:43 +08003407/* which pidlist file are we talking about? */
3408enum cgroup_filetype {
3409 CGROUP_FILE_PROCS,
3410 CGROUP_FILE_TASKS,
3411};
3412
3413/*
3414 * A pidlist is a list of pids that virtually represents the contents of one
3415 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3416 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3417 * to the cgroup.
3418 */
3419struct cgroup_pidlist {
3420 /*
3421 * used to find which pidlist is wanted. doesn't change as long as
3422 * this particular list stays in the list.
3423 */
3424 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3425 /* array of xids */
3426 pid_t *list;
3427 /* how many elements the above list has */
3428 int length;
3429 /* how many files are using the current array */
3430 int use_count;
3431 /* each of these stored in a list by its cgroup */
3432 struct list_head links;
3433 /* pointer to the cgroup we belong to, for list removal purposes */
3434 struct cgroup *owner;
3435 /* protects the other fields */
3436 struct rw_semaphore mutex;
3437};
3438
Paul Menagebbcb81d2007-10-18 23:39:32 -07003439/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003440 * The following two functions "fix" the issue where there are more pids
3441 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3442 * TODO: replace with a kernel-wide solution to this problem
3443 */
3444#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3445static void *pidlist_allocate(int count)
3446{
3447 if (PIDLIST_TOO_LARGE(count))
3448 return vmalloc(count * sizeof(pid_t));
3449 else
3450 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3451}
3452static void pidlist_free(void *p)
3453{
3454 if (is_vmalloc_addr(p))
3455 vfree(p);
3456 else
3457 kfree(p);
3458}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003459
3460/*
Ben Blum102a7752009-09-23 15:56:26 -07003461 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003462 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003463 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003464static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003465{
Ben Blum102a7752009-09-23 15:56:26 -07003466 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003467
3468 /*
3469 * we presume the 0th element is unique, so i starts at 1. trivial
3470 * edge cases first; no work needs to be done for either
3471 */
3472 if (length == 0 || length == 1)
3473 return length;
3474 /* src and dest walk down the list; dest counts unique elements */
3475 for (src = 1; src < length; src++) {
3476 /* find next unique element */
3477 while (list[src] == list[src-1]) {
3478 src++;
3479 if (src == length)
3480 goto after;
3481 }
3482 /* dest always points to where the next unique element goes */
3483 list[dest] = list[src];
3484 dest++;
3485 }
3486after:
Ben Blum102a7752009-09-23 15:56:26 -07003487 return dest;
3488}
3489
3490static int cmppid(const void *a, const void *b)
3491{
3492 return *(pid_t *)a - *(pid_t *)b;
3493}
3494
3495/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003496 * find the appropriate pidlist for our purpose (given procs vs tasks)
3497 * returns with the lock on that pidlist already held, and takes care
3498 * of the use count, or returns NULL with no locks held if we're out of
3499 * memory.
3500 */
3501static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3502 enum cgroup_filetype type)
3503{
3504 struct cgroup_pidlist *l;
3505 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003506 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003507
Ben Blum72a8cb32009-09-23 15:56:27 -07003508 /*
3509 * We can't drop the pidlist_mutex before taking the l->mutex in case
3510 * the last ref-holder is trying to remove l from the list at the same
3511 * time. Holding the pidlist_mutex precludes somebody taking whichever
3512 * list we find out from under us - compare release_pid_array().
3513 */
3514 mutex_lock(&cgrp->pidlist_mutex);
3515 list_for_each_entry(l, &cgrp->pidlists, links) {
3516 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003517 /* make sure l doesn't vanish out from under us */
3518 down_write(&l->mutex);
3519 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 return l;
3521 }
3522 }
3523 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003524 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003525 if (!l) {
3526 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 return l;
3528 }
3529 init_rwsem(&l->mutex);
3530 down_write(&l->mutex);
3531 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003532 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003533 l->owner = cgrp;
3534 list_add(&l->links, &cgrp->pidlists);
3535 mutex_unlock(&cgrp->pidlist_mutex);
3536 return l;
3537}
3538
3539/*
Ben Blum102a7752009-09-23 15:56:26 -07003540 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3541 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003542static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3543 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003544{
3545 pid_t *array;
3546 int length;
3547 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003548 struct cgroup_iter it;
3549 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003550 struct cgroup_pidlist *l;
3551
3552 /*
3553 * If cgroup gets more users after we read count, we won't have
3554 * enough space - tough. This race is indistinguishable to the
3555 * caller from the case that the additional cgroup users didn't
3556 * show up until sometime later on.
3557 */
3558 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003559 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003560 if (!array)
3561 return -ENOMEM;
3562 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003563 cgroup_iter_start(cgrp, &it);
3564 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003565 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003566 break;
Ben Blum102a7752009-09-23 15:56:26 -07003567 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003568 if (type == CGROUP_FILE_PROCS)
3569 pid = task_tgid_vnr(tsk);
3570 else
3571 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003572 if (pid > 0) /* make sure to only use valid results */
3573 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003574 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003575 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003576 length = n;
3577 /* now sort & (if procs) strip out duplicates */
3578 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003579 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003580 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003581 l = cgroup_pidlist_find(cgrp, type);
3582 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003583 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003585 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003586 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003587 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003588 l->list = array;
3589 l->length = length;
3590 l->use_count++;
3591 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003592 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003593 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003594}
3595
Balbir Singh846c7bb2007-10-18 23:39:44 -07003596/**
Li Zefana043e3b2008-02-23 15:24:09 -08003597 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003598 * @stats: cgroupstats to fill information into
3599 * @dentry: A dentry entry belonging to the cgroup for which stats have
3600 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003601 *
3602 * Build and fill cgroupstats so that taskstats can export it to user
3603 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003604 */
3605int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3606{
3607 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003608 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003609 struct cgroup_iter it;
3610 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003611
Balbir Singh846c7bb2007-10-18 23:39:44 -07003612 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003613 * Validate dentry by checking the superblock operations,
3614 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003615 */
Li Zefan33d283b2008-11-19 15:36:48 -08003616 if (dentry->d_sb->s_op != &cgroup_ops ||
3617 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003618 goto err;
3619
3620 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003621 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003622
Paul Menagebd89aab2007-10-18 23:40:44 -07003623 cgroup_iter_start(cgrp, &it);
3624 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625 switch (tsk->state) {
3626 case TASK_RUNNING:
3627 stats->nr_running++;
3628 break;
3629 case TASK_INTERRUPTIBLE:
3630 stats->nr_sleeping++;
3631 break;
3632 case TASK_UNINTERRUPTIBLE:
3633 stats->nr_uninterruptible++;
3634 break;
3635 case TASK_STOPPED:
3636 stats->nr_stopped++;
3637 break;
3638 default:
3639 if (delayacct_is_task_waiting_on_io(tsk))
3640 stats->nr_io_wait++;
3641 break;
3642 }
3643 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003644 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003645
Balbir Singh846c7bb2007-10-18 23:39:44 -07003646err:
3647 return ret;
3648}
3649
Paul Menage8f3ff202009-09-23 15:56:25 -07003650
Paul Menagecc31edc2008-10-18 20:28:04 -07003651/*
Ben Blum102a7752009-09-23 15:56:26 -07003652 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003653 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003654 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003655 */
3656
Ben Blum102a7752009-09-23 15:56:26 -07003657static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003658{
3659 /*
3660 * Initially we receive a position value that corresponds to
3661 * one more than the last pid shown (or 0 on the first call or
3662 * after a seek to the start). Use a binary-search to find the
3663 * next pid to display, if any
3664 */
Ben Blum102a7752009-09-23 15:56:26 -07003665 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003666 int index = 0, pid = *pos;
3667 int *iter;
3668
Ben Blum102a7752009-09-23 15:56:26 -07003669 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003670 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003671 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003672
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 while (index < end) {
3674 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003675 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 index = mid;
3677 break;
Ben Blum102a7752009-09-23 15:56:26 -07003678 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003679 index = mid + 1;
3680 else
3681 end = mid;
3682 }
3683 }
3684 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003685 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 return NULL;
3687 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003688 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 *pos = *iter;
3690 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003691}
3692
Ben Blum102a7752009-09-23 15:56:26 -07003693static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003694{
Ben Blum102a7752009-09-23 15:56:26 -07003695 struct cgroup_pidlist *l = s->private;
3696 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003697}
3698
Ben Blum102a7752009-09-23 15:56:26 -07003699static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003700{
Ben Blum102a7752009-09-23 15:56:26 -07003701 struct cgroup_pidlist *l = s->private;
3702 pid_t *p = v;
3703 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 /*
3705 * Advance to the next pid in the array. If this goes off the
3706 * end, we're done
3707 */
3708 p++;
3709 if (p >= end) {
3710 return NULL;
3711 } else {
3712 *pos = *p;
3713 return p;
3714 }
3715}
3716
Ben Blum102a7752009-09-23 15:56:26 -07003717static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003718{
3719 return seq_printf(s, "%d\n", *(int *)v);
3720}
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722/*
3723 * seq_operations functions for iterating on pidlists through seq_file -
3724 * independent of whether it's tasks or procs
3725 */
3726static const struct seq_operations cgroup_pidlist_seq_operations = {
3727 .start = cgroup_pidlist_start,
3728 .stop = cgroup_pidlist_stop,
3729 .next = cgroup_pidlist_next,
3730 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003731};
3732
Ben Blum102a7752009-09-23 15:56:26 -07003733static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003734{
Ben Blum72a8cb32009-09-23 15:56:27 -07003735 /*
3736 * the case where we're the last user of this particular pidlist will
3737 * have us remove it from the cgroup's list, which entails taking the
3738 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3739 * pidlist_mutex, we have to take pidlist_mutex first.
3740 */
3741 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003742 down_write(&l->mutex);
3743 BUG_ON(!l->use_count);
3744 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003745 /* we're the last user if refcount is 0; remove and free */
3746 list_del(&l->links);
3747 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003748 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003749 put_pid_ns(l->key.ns);
3750 up_write(&l->mutex);
3751 kfree(l);
3752 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003754 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003755 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003756}
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003759{
Ben Blum102a7752009-09-23 15:56:26 -07003760 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003761 if (!(file->f_mode & FMODE_READ))
3762 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003763 /*
3764 * the seq_file will only be initialized if the file was opened for
3765 * reading; hence we check if it's not null only in that case.
3766 */
3767 l = ((struct seq_file *)file->private_data)->private;
3768 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 return seq_release(inode, file);
3770}
3771
Ben Blum102a7752009-09-23 15:56:26 -07003772static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003773 .read = seq_read,
3774 .llseek = seq_lseek,
3775 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003776 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003777};
3778
3779/*
Ben Blum102a7752009-09-23 15:56:26 -07003780 * The following functions handle opens on a file that displays a pidlist
3781 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3782 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003783 */
Ben Blum102a7752009-09-23 15:56:26 -07003784/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003785static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003786{
3787 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003788 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003789 int retval;
3790
3791 /* Nothing to do for write-only files */
3792 if (!(file->f_mode & FMODE_READ))
3793 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003794
Ben Blum102a7752009-09-23 15:56:26 -07003795 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003796 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003797 if (retval)
3798 return retval;
3799 /* configure file information */
3800 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003801
Ben Blum102a7752009-09-23 15:56:26 -07003802 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003803 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003804 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003805 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003806 }
Ben Blum102a7752009-09-23 15:56:26 -07003807 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003808 return 0;
3809}
Ben Blum102a7752009-09-23 15:56:26 -07003810static int cgroup_tasks_open(struct inode *unused, struct file *file)
3811{
Ben Blum72a8cb32009-09-23 15:56:27 -07003812 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003813}
3814static int cgroup_procs_open(struct inode *unused, struct file *file)
3815{
Ben Blum72a8cb32009-09-23 15:56:27 -07003816 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003817}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003818
Paul Menagebd89aab2007-10-18 23:40:44 -07003819static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003820 struct cftype *cft)
3821{
Paul Menagebd89aab2007-10-18 23:40:44 -07003822 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003823}
3824
Paul Menage6379c102008-07-25 01:47:01 -07003825static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3826 struct cftype *cft,
3827 u64 val)
3828{
3829 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3830 if (val)
3831 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3832 else
3833 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3834 return 0;
3835}
3836
Paul Menagebbcb81d2007-10-18 23:39:32 -07003837/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003838 * When dput() is called asynchronously, if umount has been done and
3839 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3840 * there's a small window that vfs will see the root dentry with non-zero
3841 * refcnt and trigger BUG().
3842 *
3843 * That's why we hold a reference before dput() and drop it right after.
3844 */
3845static void cgroup_dput(struct cgroup *cgrp)
3846{
3847 struct super_block *sb = cgrp->root->sb;
3848
3849 atomic_inc(&sb->s_active);
3850 dput(cgrp->dentry);
3851 deactivate_super(sb);
3852}
3853
3854/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003855 * Unregister event and free resources.
3856 *
3857 * Gets called from workqueue.
3858 */
3859static void cgroup_event_remove(struct work_struct *work)
3860{
3861 struct cgroup_event *event = container_of(work, struct cgroup_event,
3862 remove);
3863 struct cgroup *cgrp = event->cgrp;
3864
Li Zefan810cbee2013-02-18 18:56:14 +08003865 remove_wait_queue(event->wqh, &event->wait);
3866
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003867 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3868
Li Zefan810cbee2013-02-18 18:56:14 +08003869 /* Notify userspace the event is going away. */
3870 eventfd_signal(event->eventfd, 1);
3871
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003872 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003873 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003874 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003875}
3876
3877/*
3878 * Gets called on POLLHUP on eventfd when user closes it.
3879 *
3880 * Called with wqh->lock held and interrupts disabled.
3881 */
3882static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3883 int sync, void *key)
3884{
3885 struct cgroup_event *event = container_of(wait,
3886 struct cgroup_event, wait);
3887 struct cgroup *cgrp = event->cgrp;
3888 unsigned long flags = (unsigned long)key;
3889
3890 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003891 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003892 * If the event has been detached at cgroup removal, we
3893 * can simply return knowing the other side will cleanup
3894 * for us.
3895 *
3896 * We can't race against event freeing since the other
3897 * side will require wqh->lock via remove_wait_queue(),
3898 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003899 */
Li Zefan810cbee2013-02-18 18:56:14 +08003900 spin_lock(&cgrp->event_list_lock);
3901 if (!list_empty(&event->list)) {
3902 list_del_init(&event->list);
3903 /*
3904 * We are in atomic context, but cgroup_event_remove()
3905 * may sleep, so we have to call it in workqueue.
3906 */
3907 schedule_work(&event->remove);
3908 }
3909 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003910 }
3911
3912 return 0;
3913}
3914
3915static void cgroup_event_ptable_queue_proc(struct file *file,
3916 wait_queue_head_t *wqh, poll_table *pt)
3917{
3918 struct cgroup_event *event = container_of(pt,
3919 struct cgroup_event, pt);
3920
3921 event->wqh = wqh;
3922 add_wait_queue(wqh, &event->wait);
3923}
3924
3925/*
3926 * Parse input and register new cgroup event handler.
3927 *
3928 * Input must be in format '<event_fd> <control_fd> <args>'.
3929 * Interpretation of args is defined by control file implementation.
3930 */
3931static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3932 const char *buffer)
3933{
3934 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003935 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003936 unsigned int efd, cfd;
3937 struct file *efile = NULL;
3938 struct file *cfile = NULL;
3939 char *endp;
3940 int ret;
3941
3942 efd = simple_strtoul(buffer, &endp, 10);
3943 if (*endp != ' ')
3944 return -EINVAL;
3945 buffer = endp + 1;
3946
3947 cfd = simple_strtoul(buffer, &endp, 10);
3948 if ((*endp != ' ') && (*endp != '\0'))
3949 return -EINVAL;
3950 buffer = endp + 1;
3951
3952 event = kzalloc(sizeof(*event), GFP_KERNEL);
3953 if (!event)
3954 return -ENOMEM;
3955 event->cgrp = cgrp;
3956 INIT_LIST_HEAD(&event->list);
3957 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3958 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3959 INIT_WORK(&event->remove, cgroup_event_remove);
3960
3961 efile = eventfd_fget(efd);
3962 if (IS_ERR(efile)) {
3963 ret = PTR_ERR(efile);
3964 goto fail;
3965 }
3966
3967 event->eventfd = eventfd_ctx_fileget(efile);
3968 if (IS_ERR(event->eventfd)) {
3969 ret = PTR_ERR(event->eventfd);
3970 goto fail;
3971 }
3972
3973 cfile = fget(cfd);
3974 if (!cfile) {
3975 ret = -EBADF;
3976 goto fail;
3977 }
3978
3979 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003980 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003981 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003982 if (ret < 0)
3983 goto fail;
3984
3985 event->cft = __file_cft(cfile);
3986 if (IS_ERR(event->cft)) {
3987 ret = PTR_ERR(event->cft);
3988 goto fail;
3989 }
3990
Li Zefanf1690072013-02-18 14:13:35 +08003991 /*
3992 * The file to be monitored must be in the same cgroup as
3993 * cgroup.event_control is.
3994 */
3995 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3996 if (cgrp_cfile != cgrp) {
3997 ret = -EINVAL;
3998 goto fail;
3999 }
4000
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004001 if (!event->cft->register_event || !event->cft->unregister_event) {
4002 ret = -EINVAL;
4003 goto fail;
4004 }
4005
4006 ret = event->cft->register_event(cgrp, event->cft,
4007 event->eventfd, buffer);
4008 if (ret)
4009 goto fail;
4010
Li Zefan7ef70e42013-04-26 11:58:03 -07004011 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004012
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004013 /*
4014 * Events should be removed after rmdir of cgroup directory, but before
4015 * destroying subsystem state objects. Let's take reference to cgroup
4016 * directory dentry to do that.
4017 */
4018 dget(cgrp->dentry);
4019
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004020 spin_lock(&cgrp->event_list_lock);
4021 list_add(&event->list, &cgrp->event_list);
4022 spin_unlock(&cgrp->event_list_lock);
4023
4024 fput(cfile);
4025 fput(efile);
4026
4027 return 0;
4028
4029fail:
4030 if (cfile)
4031 fput(cfile);
4032
4033 if (event && event->eventfd && !IS_ERR(event->eventfd))
4034 eventfd_ctx_put(event->eventfd);
4035
4036 if (!IS_ERR_OR_NULL(efile))
4037 fput(efile);
4038
4039 kfree(event);
4040
4041 return ret;
4042}
4043
Daniel Lezcano97978e62010-10-27 15:33:35 -07004044static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4045 struct cftype *cft)
4046{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004047 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004048}
4049
4050static int cgroup_clone_children_write(struct cgroup *cgrp,
4051 struct cftype *cft,
4052 u64 val)
4053{
4054 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004055 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004056 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004057 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004058 return 0;
4059}
4060
Tejun Heod5c56ce2013-06-03 19:14:34 -07004061static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004062 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004063 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004064 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004065 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004066 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004067 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004068 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004069 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004070 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004071 .write_string = cgroup_write_event_control,
4072 .mode = S_IWUGO,
4073 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004074 {
4075 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004076 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004077 .read_u64 = cgroup_clone_children_read,
4078 .write_u64 = cgroup_clone_children_write,
4079 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004080 {
Tejun Heo873fe092013-04-14 20:15:26 -07004081 .name = "cgroup.sane_behavior",
4082 .flags = CFTYPE_ONLY_ON_ROOT,
4083 .read_seq_string = cgroup_sane_behavior_show,
4084 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004085
4086 /*
4087 * Historical crazy stuff. These don't have "cgroup." prefix and
4088 * don't exist if sane_behavior. If you're depending on these, be
4089 * prepared to be burned.
4090 */
4091 {
4092 .name = "tasks",
4093 .flags = CFTYPE_INSANE, /* use "procs" instead */
4094 .open = cgroup_tasks_open,
4095 .write_u64 = cgroup_tasks_write,
4096 .release = cgroup_pidlist_release,
4097 .mode = S_IRUGO | S_IWUSR,
4098 },
4099 {
4100 .name = "notify_on_release",
4101 .flags = CFTYPE_INSANE,
4102 .read_u64 = cgroup_read_notify_on_release,
4103 .write_u64 = cgroup_write_notify_on_release,
4104 },
Tejun Heo873fe092013-04-14 20:15:26 -07004105 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004106 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004107 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004108 .read_seq_string = cgroup_release_agent_show,
4109 .write_string = cgroup_release_agent_write,
4110 .max_write_len = PATH_MAX,
4111 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004112 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004113};
4114
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004115/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004116 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004117 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004118 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004119 *
4120 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004121 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004122static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004123{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004124 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004125 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004126
Tejun Heo8e3f6542012-04-01 12:09:55 -07004127 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004128 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004129 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004130
4131 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004132 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004133
Tejun Heobee55092013-06-28 16:24:11 -07004134 list_for_each_entry(set, &ss->cftsets, node) {
4135 ret = cgroup_addrm_files(cgrp, ss, set->cfts, true);
4136 if (ret < 0)
4137 goto err;
4138 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004139 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004140
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004141 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004142 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004143 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004144 struct css_id *id = rcu_dereference_protected(css->id, true);
4145
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004146 /*
4147 * Update id->css pointer and make this css visible from
4148 * CSS ID functions. This pointer will be dereferened
4149 * from RCU-read-side without locks.
4150 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004151 if (id)
4152 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004153 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154
4155 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004156err:
4157 cgroup_clear_dir(cgrp, subsys_mask);
4158 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004159}
4160
Tejun Heo48ddbe12012-04-01 12:09:56 -07004161static void css_dput_fn(struct work_struct *work)
4162{
4163 struct cgroup_subsys_state *css =
4164 container_of(work, struct cgroup_subsys_state, dput_work);
4165
Li Zefan1c8158e2013-06-18 18:41:10 +08004166 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004167}
4168
Tejun Heod3daf282013-06-13 19:39:16 -07004169static void css_release(struct percpu_ref *ref)
4170{
4171 struct cgroup_subsys_state *css =
4172 container_of(ref, struct cgroup_subsys_state, refcnt);
4173
4174 schedule_work(&css->dput_work);
4175}
4176
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177static void init_cgroup_css(struct cgroup_subsys_state *css,
4178 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004179 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180{
Paul Menagebd89aab2007-10-18 23:40:44 -07004181 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004182 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004183 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004184 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004185 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004186 BUG_ON(cgrp->subsys[ss->subsys_id]);
4187 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004188
4189 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004190 * css holds an extra ref to @cgrp->dentry which is put on the last
4191 * css_put(). dput() requires process context, which css_put() may
4192 * be called without. @css->dput_work will be used to invoke
4193 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004194 */
4195 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196}
4197
Li Zefan2a4ac632013-07-31 16:16:40 +08004198/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004199static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004200{
Tejun Heob1929db2012-11-19 08:13:38 -08004201 int ret = 0;
4202
Tejun Heoa31f2d32012-11-19 08:13:37 -08004203 lockdep_assert_held(&cgroup_mutex);
4204
Tejun Heo92fb9742012-11-19 08:13:38 -08004205 if (ss->css_online)
4206 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004207 if (!ret)
4208 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4209 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210}
4211
Li Zefan2a4ac632013-07-31 16:16:40 +08004212/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004213static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004214{
4215 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4216
4217 lockdep_assert_held(&cgroup_mutex);
4218
4219 if (!(css->flags & CSS_ONLINE))
4220 return;
4221
Li Zefand7eeac12013-03-12 15:35:59 -07004222 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004223 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004224
4225 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4226}
4227
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228/*
Li Zefana043e3b2008-02-23 15:24:09 -08004229 * cgroup_create - create a cgroup
4230 * @parent: cgroup that will be parent of the new cgroup
4231 * @dentry: dentry of the new cgroup
4232 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004233 *
Li Zefana043e3b2008-02-23 15:24:09 -08004234 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004237 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004238{
Paul Menagebd89aab2007-10-18 23:40:44 -07004239 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004240 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241 struct cgroupfs_root *root = parent->root;
4242 int err = 0;
4243 struct cgroup_subsys *ss;
4244 struct super_block *sb = root->sb;
4245
Tejun Heo0a950f62012-11-19 09:02:12 -08004246 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4248 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 return -ENOMEM;
4250
Li Zefan65dff752013-03-01 15:01:56 +08004251 name = cgroup_alloc_name(dentry);
4252 if (!name)
4253 goto err_free_cgrp;
4254 rcu_assign_pointer(cgrp->name, name);
4255
Tejun Heo0a950f62012-11-19 09:02:12 -08004256 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4257 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004258 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004259
Tejun Heo976c06b2012-11-05 09:16:59 -08004260 /*
4261 * Only live parents can have children. Note that the liveliness
4262 * check isn't strictly necessary because cgroup_mkdir() and
4263 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4264 * anyway so that locking is contained inside cgroup proper and we
4265 * don't get nasty surprises if we ever grow another caller.
4266 */
4267 if (!cgroup_lock_live_group(parent)) {
4268 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004269 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004270 }
4271
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272 /* Grab a reference on the superblock so the hierarchy doesn't
4273 * get deleted on unmount if there are child cgroups. This
4274 * can be done outside cgroup_mutex, since the sb can't
4275 * disappear while someone has an open control file on the
4276 * fs */
4277 atomic_inc(&sb->s_active);
4278
Paul Menagecc31edc2008-10-18 20:28:04 -07004279 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280
Li Zefanfe1c06c2013-01-24 14:30:22 +08004281 dentry->d_fsdata = cgrp;
4282 cgrp->dentry = dentry;
4283
Paul Menagebd89aab2007-10-18 23:40:44 -07004284 cgrp->parent = parent;
4285 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286
Li Zefanb6abdb02008-03-04 14:28:19 -08004287 if (notify_on_release(parent))
4288 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4289
Tejun Heo2260e7f2012-11-19 08:13:38 -08004290 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4291 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004292
Tejun Heo5549c492013-06-24 15:21:48 -07004293 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004294 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004295
Tejun Heo92fb9742012-11-19 08:13:38 -08004296 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297 if (IS_ERR(css)) {
4298 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004299 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004300 }
Tejun Heod3daf282013-06-13 19:39:16 -07004301
4302 err = percpu_ref_init(&css->refcnt, css_release);
4303 if (err)
4304 goto err_free_all;
4305
Paul Menagebd89aab2007-10-18 23:40:44 -07004306 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004307
Li Zefan4528fd02010-02-02 13:44:10 -08004308 if (ss->use_id) {
4309 err = alloc_css_id(ss, parent, cgrp);
4310 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004311 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004312 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004313 }
4314
Tejun Heo4e139af2012-11-19 08:13:36 -08004315 /*
4316 * Create directory. cgroup_create_file() returns with the new
4317 * directory locked on success so that it can be populated without
4318 * dropping cgroup_mutex.
4319 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004320 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004322 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004323 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324
Tejun Heo00356bd2013-06-18 11:14:22 -07004325 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004326
Tejun Heo4e139af2012-11-19 08:13:36 -08004327 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004328 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4329 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004330
Tejun Heob1929db2012-11-19 08:13:38 -08004331 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004332 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004333 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004334
Li Zefan415cf072013-04-08 14:35:02 +08004335 /* hold a ref to the parent's dentry */
4336 dget(parent->dentry);
4337
Tejun Heob1929db2012-11-19 08:13:38 -08004338 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004339 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004340 err = online_css(ss, cgrp);
4341 if (err)
4342 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004343
4344 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4345 parent->parent) {
4346 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",
4347 current->comm, current->pid, ss->name);
4348 if (!strcmp(ss->name, "memory"))
4349 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4350 ss->warned_broken_hierarchy = true;
4351 }
Tejun Heoa8638032012-11-09 09:12:29 -08004352 }
4353
Tejun Heo628f7cd2013-06-28 16:24:11 -07004354 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4355 if (err)
4356 goto err_destroy;
4357
4358 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004359 if (err)
4360 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361
4362 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004363 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004364
4365 return 0;
4366
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004367err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004368 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004369 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4370
4371 if (css) {
4372 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004373 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004374 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377 /* Release the reference count that we took on the superblock */
4378 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004379err_free_id:
4380 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004381err_free_name:
4382 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004383err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004384 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004386
4387err_destroy:
4388 cgroup_destroy_locked(cgrp);
4389 mutex_unlock(&cgroup_mutex);
4390 mutex_unlock(&dentry->d_inode->i_mutex);
4391 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392}
4393
Al Viro18bb1db2011-07-26 01:41:39 -04004394static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395{
4396 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4397
4398 /* the vfs holds inode->i_mutex already */
4399 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4400}
4401
Tejun Heod3daf282013-06-13 19:39:16 -07004402static void cgroup_css_killed(struct cgroup *cgrp)
4403{
4404 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4405 return;
4406
4407 /* percpu ref's of all css's are killed, kick off the next step */
4408 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4409 schedule_work(&cgrp->destroy_work);
4410}
4411
4412static void css_ref_killed_fn(struct percpu_ref *ref)
4413{
4414 struct cgroup_subsys_state *css =
4415 container_of(ref, struct cgroup_subsys_state, refcnt);
4416
4417 cgroup_css_killed(css->cgroup);
4418}
4419
4420/**
4421 * cgroup_destroy_locked - the first stage of cgroup destruction
4422 * @cgrp: cgroup to be destroyed
4423 *
4424 * css's make use of percpu refcnts whose killing latency shouldn't be
4425 * exposed to userland and are RCU protected. Also, cgroup core needs to
4426 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4427 * invoked. To satisfy all the requirements, destruction is implemented in
4428 * the following two steps.
4429 *
4430 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4431 * userland visible parts and start killing the percpu refcnts of
4432 * css's. Set up so that the next stage will be kicked off once all
4433 * the percpu refcnts are confirmed to be killed.
4434 *
4435 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4436 * rest of destruction. Once all cgroup references are gone, the
4437 * cgroup is RCU-freed.
4438 *
4439 * This function implements s1. After this step, @cgrp is gone as far as
4440 * the userland is concerned and a new cgroup with the same name may be
4441 * created. As cgroup doesn't care about the names internally, this
4442 * doesn't cause any problem.
4443 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004444static int cgroup_destroy_locked(struct cgroup *cgrp)
4445 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004446{
Tejun Heo42809dd2012-11-19 08:13:37 -08004447 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004448 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004449 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004450 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004451
Tejun Heo42809dd2012-11-19 08:13:37 -08004452 lockdep_assert_held(&d->d_inode->i_mutex);
4453 lockdep_assert_held(&cgroup_mutex);
4454
Tejun Heoddd69142013-06-12 21:04:54 -07004455 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004456 * css_set_lock synchronizes access to ->cset_links and prevents
4457 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004458 */
4459 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004460 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004461 read_unlock(&css_set_lock);
4462 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004463 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004464
Tejun Heo1a90dd52012-11-05 09:16:59 -08004465 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004466 * Block new css_tryget() by killing css refcnts. cgroup core
4467 * guarantees that, by the time ->css_offline() is invoked, no new
4468 * css reference will be given out via css_tryget(). We can't
4469 * simply call percpu_ref_kill() and proceed to offlining css's
4470 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4471 * as killed on all CPUs on return.
4472 *
4473 * Use percpu_ref_kill_and_confirm() to get notifications as each
4474 * css is confirmed to be seen as killed on all CPUs. The
4475 * notification callback keeps track of the number of css's to be
4476 * killed and schedules cgroup_offline_fn() to perform the rest of
4477 * destruction once the percpu refs of all css's are confirmed to
4478 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004479 */
Tejun Heod3daf282013-06-13 19:39:16 -07004480 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004481 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004482 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4483
Tejun Heod3daf282013-06-13 19:39:16 -07004484 /*
4485 * Killing would put the base ref, but we need to keep it
4486 * alive until after ->css_offline.
4487 */
4488 percpu_ref_get(&css->refcnt);
4489
4490 atomic_inc(&cgrp->css_kill_cnt);
4491 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004492 }
Tejun Heod3daf282013-06-13 19:39:16 -07004493 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004494
4495 /*
4496 * Mark @cgrp dead. This prevents further task migration and child
4497 * creation by disabling cgroup_lock_live_group(). Note that
4498 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4499 * resume iteration after dropping RCU read lock. See
4500 * cgroup_next_sibling() for details.
4501 */
Tejun Heo54766d42013-06-12 21:04:53 -07004502 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004503
Tejun Heo455050d2013-06-13 19:27:41 -07004504 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4505 raw_spin_lock(&release_list_lock);
4506 if (!list_empty(&cgrp->release_list))
4507 list_del_init(&cgrp->release_list);
4508 raw_spin_unlock(&release_list_lock);
4509
4510 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004511 * Clear and remove @cgrp directory. The removal puts the base ref
4512 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004513 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004514 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
4515 cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004516 dget(d);
4517 cgroup_d_remove_dir(d);
4518
4519 /*
4520 * Unregister events and notify userspace.
4521 * Notify userspace about cgroup removing only after rmdir of cgroup
4522 * directory to avoid race between userspace and kernelspace.
4523 */
4524 spin_lock(&cgrp->event_list_lock);
4525 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4526 list_del_init(&event->list);
4527 schedule_work(&event->remove);
4528 }
4529 spin_unlock(&cgrp->event_list_lock);
4530
Tejun Heoea15f8c2013-06-13 19:27:42 -07004531 return 0;
4532};
4533
Tejun Heod3daf282013-06-13 19:39:16 -07004534/**
4535 * cgroup_offline_fn - the second step of cgroup destruction
4536 * @work: cgroup->destroy_free_work
4537 *
4538 * This function is invoked from a work item for a cgroup which is being
4539 * destroyed after the percpu refcnts of all css's are guaranteed to be
4540 * seen as killed on all CPUs, and performs the rest of destruction. This
4541 * is the second step of destruction described in the comment above
4542 * cgroup_destroy_locked().
4543 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004544static void cgroup_offline_fn(struct work_struct *work)
4545{
4546 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4547 struct cgroup *parent = cgrp->parent;
4548 struct dentry *d = cgrp->dentry;
4549 struct cgroup_subsys *ss;
4550
4551 mutex_lock(&cgroup_mutex);
4552
Tejun Heod3daf282013-06-13 19:39:16 -07004553 /*
4554 * css_tryget() is guaranteed to fail now. Tell subsystems to
4555 * initate destruction.
4556 */
Tejun Heo5549c492013-06-24 15:21:48 -07004557 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004558 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004559
4560 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004561 * Put the css refs from cgroup_destroy_locked(). Each css holds
4562 * an extra reference to the cgroup's dentry and cgroup removal
4563 * proceeds regardless of css refs. On the last put of each css,
4564 * whenever that may be, the extra dentry ref is put so that dentry
4565 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004566 */
Tejun Heo5549c492013-06-24 15:21:48 -07004567 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004568 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569
Paul Menage999cd8a2009-01-07 18:08:36 -08004570 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004571 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004572
Paul Menageddbcc7e2007-10-18 23:39:30 -07004573 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574
Paul Menagebd89aab2007-10-18 23:40:44 -07004575 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004576 check_for_release(parent);
4577
Tejun Heoea15f8c2013-06-13 19:27:42 -07004578 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004579}
4580
Tejun Heo42809dd2012-11-19 08:13:37 -08004581static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4582{
4583 int ret;
4584
4585 mutex_lock(&cgroup_mutex);
4586 ret = cgroup_destroy_locked(dentry->d_fsdata);
4587 mutex_unlock(&cgroup_mutex);
4588
4589 return ret;
4590}
4591
Tejun Heo8e3f6542012-04-01 12:09:55 -07004592static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4593{
4594 INIT_LIST_HEAD(&ss->cftsets);
4595
4596 /*
4597 * base_cftset is embedded in subsys itself, no need to worry about
4598 * deregistration.
4599 */
4600 if (ss->base_cftypes) {
4601 ss->base_cftset.cfts = ss->base_cftypes;
4602 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4603 }
4604}
4605
Li Zefan06a11922008-04-29 01:00:07 -07004606static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004609
4610 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611
Tejun Heo648bb562012-11-19 08:13:36 -08004612 mutex_lock(&cgroup_mutex);
4613
Tejun Heo8e3f6542012-04-01 12:09:55 -07004614 /* init base cftset */
4615 cgroup_init_cftsets(ss);
4616
Paul Menageddbcc7e2007-10-18 23:39:30 -07004617 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004618 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4619 ss->root = &cgroup_dummy_root;
4620 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004621 /* We don't handle early failures gracefully */
4622 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004623 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624
Li Zefane8d55fd2008-04-29 01:00:13 -07004625 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004626 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004627 * newly registered, all tasks and hence the
4628 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004629 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004630
4631 need_forkexit_callback |= ss->fork || ss->exit;
4632
Li Zefane8d55fd2008-04-29 01:00:13 -07004633 /* At system boot, before all subsystems have been
4634 * registered, no tasks have been forked, so we don't
4635 * need to invoke fork callbacks here. */
4636 BUG_ON(!list_empty(&init_task.tasks));
4637
Tejun Heo9871bf92013-06-24 15:21:47 -07004638 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004639
Tejun Heo648bb562012-11-19 08:13:36 -08004640 mutex_unlock(&cgroup_mutex);
4641
Ben Blume6a11052010-03-10 15:22:09 -08004642 /* this function shouldn't be used with modular subsystems, since they
4643 * need to register a subsys_id, among other things */
4644 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645}
4646
4647/**
Ben Blume6a11052010-03-10 15:22:09 -08004648 * cgroup_load_subsys: load and register a modular subsystem at runtime
4649 * @ss: the subsystem to load
4650 *
4651 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004652 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004653 * up for use. If the subsystem is built-in anyway, work is delegated to the
4654 * simpler cgroup_init_subsys.
4655 */
4656int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4657{
Ben Blume6a11052010-03-10 15:22:09 -08004658 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004659 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004660 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004661 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004662 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004663
4664 /* check name and function validity */
4665 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004666 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004667 return -EINVAL;
4668
4669 /*
4670 * we don't support callbacks in modular subsystems. this check is
4671 * before the ss->module check for consistency; a subsystem that could
4672 * be a module should still have no callbacks even if the user isn't
4673 * compiling it as one.
4674 */
4675 if (ss->fork || ss->exit)
4676 return -EINVAL;
4677
4678 /*
4679 * an optionally modular subsystem is built-in: we want to do nothing,
4680 * since cgroup_init_subsys will have already taken care of it.
4681 */
4682 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004683 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004684 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004685 return 0;
4686 }
4687
Tejun Heo8e3f6542012-04-01 12:09:55 -07004688 /* init base cftset */
4689 cgroup_init_cftsets(ss);
4690
Ben Blume6a11052010-03-10 15:22:09 -08004691 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004692 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004693
4694 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004695 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004696 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004697 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004698 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004699 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004700 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004701 /* failure case - need to deassign the cgroup_subsys[] slot. */
4702 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004703 mutex_unlock(&cgroup_mutex);
4704 return PTR_ERR(css);
4705 }
4706
Tejun Heo9871bf92013-06-24 15:21:47 -07004707 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4708 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004709
4710 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004711 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004712 /* init_idr must be after init_cgroup_css because it sets css->id. */
4713 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004714 ret = cgroup_init_idr(ss, css);
4715 if (ret)
4716 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004717 }
4718
4719 /*
4720 * Now we need to entangle the css into the existing css_sets. unlike
4721 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4722 * will need a new pointer to it; done by iterating the css_set_table.
4723 * furthermore, modifying the existing css_sets will corrupt the hash
4724 * table state, so each changed css_set will need its hash recomputed.
4725 * this is all done under the css_set_lock.
4726 */
4727 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004728 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004729 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004730 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004731 continue;
4732 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004733 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004734 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004735 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004736 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004737 key = css_set_hash(cset->subsys);
4738 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004739 }
4740 write_unlock(&css_set_lock);
4741
Tejun Heo9871bf92013-06-24 15:21:47 -07004742 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004743 if (ret)
4744 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004745
Ben Blume6a11052010-03-10 15:22:09 -08004746 /* success! */
4747 mutex_unlock(&cgroup_mutex);
4748 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004749
4750err_unload:
4751 mutex_unlock(&cgroup_mutex);
4752 /* @ss can't be mounted here as try_module_get() would fail */
4753 cgroup_unload_subsys(ss);
4754 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004755}
4756EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4757
4758/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004759 * cgroup_unload_subsys: unload a modular subsystem
4760 * @ss: the subsystem to unload
4761 *
4762 * This function should be called in a modular subsystem's exitcall. When this
4763 * function is invoked, the refcount on the subsystem's module will be 0, so
4764 * the subsystem will not be attached to any hierarchy.
4765 */
4766void cgroup_unload_subsys(struct cgroup_subsys *ss)
4767{
Tejun Heo69d02062013-06-12 21:04:50 -07004768 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004769
4770 BUG_ON(ss->module == NULL);
4771
4772 /*
4773 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004774 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004775 * doesn't start being used while we're killing it off.
4776 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004777 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004778
4779 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004780
Tejun Heo9871bf92013-06-24 15:21:47 -07004781 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004782
Tejun Heoc897ff62013-02-27 17:03:49 -08004783 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004784 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004785
Ben Blumcf5d5942010-03-10 15:22:09 -08004786 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004787 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004788
Tejun Heo9871bf92013-06-24 15:21:47 -07004789 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004790 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004791
4792 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004793 * disentangle the css from all css_sets attached to the dummy
4794 * top. as in loading, we need to pay our respects to the hashtable
4795 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004796 */
4797 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004798 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004799 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004800 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004801
Tejun Heo5abb8852013-06-12 21:04:49 -07004802 hash_del(&cset->hlist);
4803 cset->subsys[ss->subsys_id] = NULL;
4804 key = css_set_hash(cset->subsys);
4805 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004806 }
4807 write_unlock(&css_set_lock);
4808
4809 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004810 * remove subsystem's css from the cgroup_dummy_top and free it -
4811 * need to free before marking as null because ss->css_free needs
4812 * the cgrp->subsys pointer to find their state. note that this
4813 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004814 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004815 ss->css_free(cgroup_dummy_top);
4816 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004817
4818 mutex_unlock(&cgroup_mutex);
4819}
4820EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4821
4822/**
Li Zefana043e3b2008-02-23 15:24:09 -08004823 * cgroup_init_early - cgroup initialization at system boot
4824 *
4825 * Initialize cgroups at system boot, and initialize any
4826 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004827 */
4828int __init cgroup_init_early(void)
4829{
Tejun Heo30159ec2013-06-25 11:53:37 -07004830 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004831 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004832
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004833 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004834 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004835 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004836 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004837 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004838 init_cgroup_root(&cgroup_dummy_root);
4839 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004840 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004841
Tejun Heo69d02062013-06-12 21:04:50 -07004842 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4844 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004845 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004846
Tejun Heo30159ec2013-06-25 11:53:37 -07004847 /* at bootup time, we don't worry about modular subsystems */
4848 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004849 BUG_ON(!ss->name);
4850 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004851 BUG_ON(!ss->css_alloc);
4852 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004853 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004854 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004855 ss->name, ss->subsys_id);
4856 BUG();
4857 }
4858
4859 if (ss->early_init)
4860 cgroup_init_subsys(ss);
4861 }
4862 return 0;
4863}
4864
4865/**
Li Zefana043e3b2008-02-23 15:24:09 -08004866 * cgroup_init - cgroup initialization
4867 *
4868 * Register cgroup filesystem and /proc file, and initialize
4869 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004870 */
4871int __init cgroup_init(void)
4872{
Tejun Heo30159ec2013-06-25 11:53:37 -07004873 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004874 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004875 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004876
4877 err = bdi_init(&cgroup_backing_dev_info);
4878 if (err)
4879 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004880
Tejun Heo30159ec2013-06-25 11:53:37 -07004881 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004882 if (!ss->early_init)
4883 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004884 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004885 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004886 }
4887
Tejun Heofa3ca072013-04-14 11:36:56 -07004888 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004889 mutex_lock(&cgroup_mutex);
4890 mutex_lock(&cgroup_root_mutex);
4891
Tejun Heo82fe9b02013-06-25 11:53:37 -07004892 /* Add init_css_set to the hash table */
4893 key = css_set_hash(init_css_set.subsys);
4894 hash_add(css_set_table, &init_css_set.hlist, key);
4895
Tejun Heofc76df72013-06-25 11:53:37 -07004896 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004897
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004898 mutex_unlock(&cgroup_root_mutex);
4899 mutex_unlock(&cgroup_mutex);
4900
Greg KH676db4a2010-08-05 13:53:35 -07004901 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4902 if (!cgroup_kobj) {
4903 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004904 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004905 }
4906
4907 err = register_filesystem(&cgroup_fs_type);
4908 if (err < 0) {
4909 kobject_put(cgroup_kobj);
4910 goto out;
4911 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004912
Li Zefan46ae2202008-04-29 01:00:08 -07004913 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004914
Paul Menageddbcc7e2007-10-18 23:39:30 -07004915out:
Paul Menagea4243162007-10-18 23:39:35 -07004916 if (err)
4917 bdi_destroy(&cgroup_backing_dev_info);
4918
Paul Menageddbcc7e2007-10-18 23:39:30 -07004919 return err;
4920}
Paul Menageb4f48b62007-10-18 23:39:33 -07004921
Paul Menagea4243162007-10-18 23:39:35 -07004922/*
4923 * proc_cgroup_show()
4924 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4925 * - Used for /proc/<pid>/cgroup.
4926 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4927 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004928 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004929 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4930 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4931 * cgroup to top_cgroup.
4932 */
4933
4934/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004935int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004936{
4937 struct pid *pid;
4938 struct task_struct *tsk;
4939 char *buf;
4940 int retval;
4941 struct cgroupfs_root *root;
4942
4943 retval = -ENOMEM;
4944 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4945 if (!buf)
4946 goto out;
4947
4948 retval = -ESRCH;
4949 pid = m->private;
4950 tsk = get_pid_task(pid, PIDTYPE_PID);
4951 if (!tsk)
4952 goto out_free;
4953
4954 retval = 0;
4955
4956 mutex_lock(&cgroup_mutex);
4957
Li Zefane5f6a862009-01-07 18:07:41 -08004958 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004959 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004960 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004961 int count = 0;
4962
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004963 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004964 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004965 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004966 if (strlen(root->name))
4967 seq_printf(m, "%sname=%s", count ? "," : "",
4968 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004969 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004970 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004971 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004972 if (retval < 0)
4973 goto out_unlock;
4974 seq_puts(m, buf);
4975 seq_putc(m, '\n');
4976 }
4977
4978out_unlock:
4979 mutex_unlock(&cgroup_mutex);
4980 put_task_struct(tsk);
4981out_free:
4982 kfree(buf);
4983out:
4984 return retval;
4985}
4986
Paul Menagea4243162007-10-18 23:39:35 -07004987/* Display information about each subsystem and each hierarchy */
4988static int proc_cgroupstats_show(struct seq_file *m, void *v)
4989{
Tejun Heo30159ec2013-06-25 11:53:37 -07004990 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004991 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004992
Paul Menage8bab8dd2008-04-04 14:29:57 -07004993 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004994 /*
4995 * ideally we don't want subsystems moving around while we do this.
4996 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4997 * subsys/hierarchy state.
4998 */
Paul Menagea4243162007-10-18 23:39:35 -07004999 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005000
5001 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005002 seq_printf(m, "%s\t%d\t%d\t%d\n",
5003 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005004 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005005
Paul Menagea4243162007-10-18 23:39:35 -07005006 mutex_unlock(&cgroup_mutex);
5007 return 0;
5008}
5009
5010static int cgroupstats_open(struct inode *inode, struct file *file)
5011{
Al Viro9dce07f2008-03-29 03:07:28 +00005012 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005013}
5014
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005015static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005016 .open = cgroupstats_open,
5017 .read = seq_read,
5018 .llseek = seq_lseek,
5019 .release = single_release,
5020};
5021
Paul Menageb4f48b62007-10-18 23:39:33 -07005022/**
5023 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005024 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005025 *
5026 * Description: A task inherits its parent's cgroup at fork().
5027 *
5028 * A pointer to the shared css_set was automatically copied in
5029 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005030 * it was not made under the protection of RCU or cgroup_mutex, so
5031 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5032 * have already changed current->cgroups, allowing the previously
5033 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005034 *
5035 * At the point that cgroup_fork() is called, 'current' is the parent
5036 * task, and the passed argument 'child' points to the child task.
5037 */
5038void cgroup_fork(struct task_struct *child)
5039{
Tejun Heo9bb71302012-10-18 17:52:07 -07005040 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005041 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005042 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005043 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005044 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005045}
5046
5047/**
Li Zefana043e3b2008-02-23 15:24:09 -08005048 * cgroup_post_fork - called on a new task after adding it to the task list
5049 * @child: the task in question
5050 *
Tejun Heo5edee612012-10-16 15:03:14 -07005051 * Adds the task to the list running through its css_set if necessary and
5052 * call the subsystem fork() callbacks. Has to be after the task is
5053 * visible on the task list in case we race with the first call to
5054 * cgroup_iter_start() - to guarantee that the new task ends up on its
5055 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005056 */
Paul Menage817929e2007-10-18 23:39:36 -07005057void cgroup_post_fork(struct task_struct *child)
5058{
Tejun Heo30159ec2013-06-25 11:53:37 -07005059 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005060 int i;
5061
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005062 /*
5063 * use_task_css_set_links is set to 1 before we walk the tasklist
5064 * under the tasklist_lock and we read it here after we added the child
5065 * to the tasklist under the tasklist_lock as well. If the child wasn't
5066 * yet in the tasklist when we walked through it from
5067 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5068 * should be visible now due to the paired locking and barriers implied
5069 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5070 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5071 * lock on fork.
5072 */
Paul Menage817929e2007-10-18 23:39:36 -07005073 if (use_task_css_set_links) {
5074 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005075 task_lock(child);
5076 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005077 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005078 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005079 write_unlock(&css_set_lock);
5080 }
Tejun Heo5edee612012-10-16 15:03:14 -07005081
5082 /*
5083 * Call ss->fork(). This must happen after @child is linked on
5084 * css_set; otherwise, @child might change state between ->fork()
5085 * and addition to css_set.
5086 */
5087 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005088 /*
5089 * fork/exit callbacks are supported only for builtin
5090 * subsystems, and the builtin section of the subsys
5091 * array is immutable, so we don't need to lock the
5092 * subsys array here. On the other hand, modular section
5093 * of the array can be freed at module unload, so we
5094 * can't touch that.
5095 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005096 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005097 if (ss->fork)
5098 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005099 }
Paul Menage817929e2007-10-18 23:39:36 -07005100}
Tejun Heo5edee612012-10-16 15:03:14 -07005101
Paul Menage817929e2007-10-18 23:39:36 -07005102/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005103 * cgroup_exit - detach cgroup from exiting task
5104 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005105 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005106 *
5107 * Description: Detach cgroup from @tsk and release it.
5108 *
5109 * Note that cgroups marked notify_on_release force every task in
5110 * them to take the global cgroup_mutex mutex when exiting.
5111 * This could impact scaling on very large systems. Be reluctant to
5112 * use notify_on_release cgroups where very high task exit scaling
5113 * is required on large systems.
5114 *
5115 * the_top_cgroup_hack:
5116 *
5117 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5118 *
5119 * We call cgroup_exit() while the task is still competent to
5120 * handle notify_on_release(), then leave the task attached to the
5121 * root cgroup in each hierarchy for the remainder of its exit.
5122 *
5123 * To do this properly, we would increment the reference count on
5124 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5125 * code we would add a second cgroup function call, to drop that
5126 * reference. This would just create an unnecessary hot spot on
5127 * the top_cgroup reference count, to no avail.
5128 *
5129 * Normally, holding a reference to a cgroup without bumping its
5130 * count is unsafe. The cgroup could go away, or someone could
5131 * attach us to a different cgroup, decrementing the count on
5132 * the first cgroup that we never incremented. But in this case,
5133 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005134 * which wards off any cgroup_attach_task() attempts, or task is a failed
5135 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005136 */
5137void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5138{
Tejun Heo30159ec2013-06-25 11:53:37 -07005139 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005140 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005141 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005142
5143 /*
5144 * Unlink from the css_set task list if necessary.
5145 * Optimistically check cg_list before taking
5146 * css_set_lock
5147 */
5148 if (!list_empty(&tsk->cg_list)) {
5149 write_lock(&css_set_lock);
5150 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005151 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005152 write_unlock(&css_set_lock);
5153 }
5154
Paul Menageb4f48b62007-10-18 23:39:33 -07005155 /* Reassign the task to the init_css_set. */
5156 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005157 cset = task_css_set(tsk);
5158 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005159
5160 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005161 /*
5162 * fork/exit callbacks are supported only for builtin
5163 * subsystems, see cgroup_post_fork() for details.
5164 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005165 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005166 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005167 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005168 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005169
Li Zefan761b3ef2012-01-31 13:47:36 +08005170 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005171 }
5172 }
5173 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005174 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005175
Tejun Heo5abb8852013-06-12 21:04:49 -07005176 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005177}
Paul Menage697f4162007-10-18 23:39:34 -07005178
Paul Menagebd89aab2007-10-18 23:40:44 -07005179static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005180{
Li Zefanf50daa72013-03-01 15:06:07 +08005181 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005182 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005183 /*
5184 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005185 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005186 * it now
5187 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005188 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005189
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005190 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005191 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005192 list_empty(&cgrp->release_list)) {
5193 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005194 need_schedule_work = 1;
5195 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005196 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005197 if (need_schedule_work)
5198 schedule_work(&release_agent_work);
5199 }
5200}
5201
Paul Menage81a6a5c2007-10-18 23:39:38 -07005202/*
5203 * Notify userspace when a cgroup is released, by running the
5204 * configured release agent with the name of the cgroup (path
5205 * relative to the root of cgroup file system) as the argument.
5206 *
5207 * Most likely, this user command will try to rmdir this cgroup.
5208 *
5209 * This races with the possibility that some other task will be
5210 * attached to this cgroup before it is removed, or that some other
5211 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5212 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5213 * unused, and this cgroup will be reprieved from its death sentence,
5214 * to continue to serve a useful existence. Next time it's released,
5215 * we will get notified again, if it still has 'notify_on_release' set.
5216 *
5217 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5218 * means only wait until the task is successfully execve()'d. The
5219 * separate release agent task is forked by call_usermodehelper(),
5220 * then control in this thread returns here, without waiting for the
5221 * release agent task. We don't bother to wait because the caller of
5222 * this routine has no use for the exit status of the release agent
5223 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005224 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005225static void cgroup_release_agent(struct work_struct *work)
5226{
5227 BUG_ON(work != &release_agent_work);
5228 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005229 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005230 while (!list_empty(&release_list)) {
5231 char *argv[3], *envp[3];
5232 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005233 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005234 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005235 struct cgroup,
5236 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005237 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005238 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005239 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005240 if (!pathbuf)
5241 goto continue_free;
5242 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5243 goto continue_free;
5244 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5245 if (!agentbuf)
5246 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005247
5248 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005249 argv[i++] = agentbuf;
5250 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005251 argv[i] = NULL;
5252
5253 i = 0;
5254 /* minimal command environment */
5255 envp[i++] = "HOME=/";
5256 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5257 envp[i] = NULL;
5258
5259 /* Drop the lock while we invoke the usermode helper,
5260 * since the exec could involve hitting disk and hence
5261 * be a slow process */
5262 mutex_unlock(&cgroup_mutex);
5263 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005264 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005265 continue_free:
5266 kfree(pathbuf);
5267 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005268 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005269 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005270 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005271 mutex_unlock(&cgroup_mutex);
5272}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005273
5274static int __init cgroup_disable(char *str)
5275{
Tejun Heo30159ec2013-06-25 11:53:37 -07005276 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005277 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005278 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005279
5280 while ((token = strsep(&str, ",")) != NULL) {
5281 if (!*token)
5282 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005283
Tejun Heo30159ec2013-06-25 11:53:37 -07005284 /*
5285 * cgroup_disable, being at boot time, can't know about
5286 * module subsystems, so we don't worry about them.
5287 */
5288 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005289 if (!strcmp(token, ss->name)) {
5290 ss->disabled = 1;
5291 printk(KERN_INFO "Disabling %s control group"
5292 " subsystem\n", ss->name);
5293 break;
5294 }
5295 }
5296 }
5297 return 1;
5298}
5299__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005300
5301/*
5302 * Functons for CSS ID.
5303 */
5304
Tejun Heo54766d42013-06-12 21:04:53 -07005305/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005306unsigned short css_id(struct cgroup_subsys_state *css)
5307{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005308 struct css_id *cssid;
5309
5310 /*
5311 * This css_id() can return correct value when somone has refcnt
5312 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5313 * it's unchanged until freed.
5314 */
Tejun Heod3daf282013-06-13 19:39:16 -07005315 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005316
5317 if (cssid)
5318 return cssid->id;
5319 return 0;
5320}
Ben Blum67523c42010-03-10 15:22:11 -08005321EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005323/**
5324 * css_is_ancestor - test "root" css is an ancestor of "child"
5325 * @child: the css to be tested.
5326 * @root: the css supporsed to be an ancestor of the child.
5327 *
5328 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005329 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005330 * But, considering usual usage, the csses should be valid objects after test.
5331 * Assuming that the caller will do some action to the child if this returns
5332 * returns true, the caller must take "child";s reference count.
5333 * If "child" is valid object and this returns true, "root" is valid, too.
5334 */
5335
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005336bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005337 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005338{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005339 struct css_id *child_id;
5340 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005341
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005342 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005343 if (!child_id)
5344 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005345 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005346 if (!root_id)
5347 return false;
5348 if (child_id->depth < root_id->depth)
5349 return false;
5350 if (child_id->stack[root_id->depth] != root_id->id)
5351 return false;
5352 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005353}
5354
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005355void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5356{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005357 struct css_id *id = rcu_dereference_protected(css->id, true);
5358
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005359 /* When this is called before css_id initialization, id can be NULL */
5360 if (!id)
5361 return;
5362
5363 BUG_ON(!ss->use_id);
5364
5365 rcu_assign_pointer(id->css, NULL);
5366 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005367 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005368 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005369 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005370 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005371}
Ben Blum67523c42010-03-10 15:22:11 -08005372EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373
5374/*
5375 * This is called by init or create(). Then, calls to this function are
5376 * always serialized (By cgroup_mutex() at create()).
5377 */
5378
5379static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5380{
5381 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005382 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005383
5384 BUG_ON(!ss->use_id);
5385
5386 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5387 newid = kzalloc(size, GFP_KERNEL);
5388 if (!newid)
5389 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005390
5391 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005392 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005393 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005394 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005395 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005396 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005397
5398 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005399 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005400 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005401
Tejun Heod228d9e2013-02-27 17:04:54 -08005402 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005403 newid->depth = depth;
5404 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005405err_out:
5406 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005407 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408
5409}
5410
Ben Blume6a11052010-03-10 15:22:09 -08005411static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5412 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413{
5414 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005415
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005416 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417 idr_init(&ss->idr);
5418
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005419 newid = get_new_cssid(ss, 0);
5420 if (IS_ERR(newid))
5421 return PTR_ERR(newid);
5422
5423 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005424 RCU_INIT_POINTER(newid->css, rootcss);
5425 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426 return 0;
5427}
5428
5429static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5430 struct cgroup *child)
5431{
5432 int subsys_id, i, depth = 0;
5433 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005434 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005435
5436 subsys_id = ss->subsys_id;
5437 parent_css = parent->subsys[subsys_id];
5438 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005439 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005440 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005441
5442 child_id = get_new_cssid(ss, depth);
5443 if (IS_ERR(child_id))
5444 return PTR_ERR(child_id);
5445
5446 for (i = 0; i < depth; i++)
5447 child_id->stack[i] = parent_id->stack[i];
5448 child_id->stack[depth] = child_id->id;
5449 /*
5450 * child_id->css pointer will be set after this cgroup is available
5451 * see cgroup_populate_dir()
5452 */
5453 rcu_assign_pointer(child_css->id, child_id);
5454
5455 return 0;
5456}
5457
5458/**
5459 * css_lookup - lookup css by id
5460 * @ss: cgroup subsys to be looked into.
5461 * @id: the id
5462 *
5463 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5464 * NULL if not. Should be called under rcu_read_lock()
5465 */
5466struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5467{
5468 struct css_id *cssid = NULL;
5469
5470 BUG_ON(!ss->use_id);
5471 cssid = idr_find(&ss->idr, id);
5472
5473 if (unlikely(!cssid))
5474 return NULL;
5475
5476 return rcu_dereference(cssid->css);
5477}
Ben Blum67523c42010-03-10 15:22:11 -08005478EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005479
Stephane Eraniane5d13672011-02-14 11:20:01 +02005480/*
5481 * get corresponding css from file open on cgroupfs directory
5482 */
5483struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5484{
5485 struct cgroup *cgrp;
5486 struct inode *inode;
5487 struct cgroup_subsys_state *css;
5488
Al Viro496ad9a2013-01-23 17:07:38 -05005489 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005490 /* check in cgroup filesystem dir */
5491 if (inode->i_op != &cgroup_dir_inode_operations)
5492 return ERR_PTR(-EBADF);
5493
5494 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5495 return ERR_PTR(-EINVAL);
5496
5497 /* get cgroup */
5498 cgrp = __d_cgrp(f->f_dentry);
5499 css = cgrp->subsys[id];
5500 return css ? css : ERR_PTR(-ENOENT);
5501}
5502
Paul Menagefe693432009-09-23 15:56:20 -07005503#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005504static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005505{
5506 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5507
5508 if (!css)
5509 return ERR_PTR(-ENOMEM);
5510
5511 return css;
5512}
5513
Li Zefan03c78cb2013-06-14 11:17:19 +08005514static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005515{
Li Zefan03c78cb2013-06-14 11:17:19 +08005516 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005517}
5518
Li Zefan03c78cb2013-06-14 11:17:19 +08005519static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005520{
Li Zefan03c78cb2013-06-14 11:17:19 +08005521 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005522}
5523
Li Zefan03c78cb2013-06-14 11:17:19 +08005524static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005525{
5526 return (u64)(unsigned long)current->cgroups;
5527}
5528
Li Zefan03c78cb2013-06-14 11:17:19 +08005529static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5530 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005531{
5532 u64 count;
5533
5534 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005535 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005536 rcu_read_unlock();
5537 return count;
5538}
5539
Li Zefan03c78cb2013-06-14 11:17:19 +08005540static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005541 struct cftype *cft,
5542 struct seq_file *seq)
5543{
Tejun Heo69d02062013-06-12 21:04:50 -07005544 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005545 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005546
5547 read_lock(&css_set_lock);
5548 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005549 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005550 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005551 struct cgroup *c = link->cgrp;
5552 const char *name;
5553
5554 if (c->dentry)
5555 name = c->dentry->d_name.name;
5556 else
5557 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005558 seq_printf(seq, "Root %d group %s\n",
5559 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005560 }
5561 rcu_read_unlock();
5562 read_unlock(&css_set_lock);
5563 return 0;
5564}
5565
5566#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005567static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005568 struct cftype *cft,
5569 struct seq_file *seq)
5570{
Tejun Heo69d02062013-06-12 21:04:50 -07005571 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005572
5573 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005574 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005575 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005576 struct task_struct *task;
5577 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005578 seq_printf(seq, "css_set %p\n", cset);
5579 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005580 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5581 seq_puts(seq, " ...\n");
5582 break;
5583 } else {
5584 seq_printf(seq, " task %d\n",
5585 task_pid_vnr(task));
5586 }
5587 }
5588 }
5589 read_unlock(&css_set_lock);
5590 return 0;
5591}
5592
Paul Menagefe693432009-09-23 15:56:20 -07005593static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5594{
5595 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5596}
5597
5598static struct cftype debug_files[] = {
5599 {
Paul Menagefe693432009-09-23 15:56:20 -07005600 .name = "taskcount",
5601 .read_u64 = debug_taskcount_read,
5602 },
5603
5604 {
5605 .name = "current_css_set",
5606 .read_u64 = current_css_set_read,
5607 },
5608
5609 {
5610 .name = "current_css_set_refcount",
5611 .read_u64 = current_css_set_refcount_read,
5612 },
5613
5614 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005615 .name = "current_css_set_cg_links",
5616 .read_seq_string = current_css_set_cg_links_read,
5617 },
5618
5619 {
5620 .name = "cgroup_css_links",
5621 .read_seq_string = cgroup_css_links_read,
5622 },
5623
5624 {
Paul Menagefe693432009-09-23 15:56:20 -07005625 .name = "releasable",
5626 .read_u64 = releasable_read,
5627 },
Paul Menagefe693432009-09-23 15:56:20 -07005628
Tejun Heo4baf6e32012-04-01 12:09:55 -07005629 { } /* terminate */
5630};
Paul Menagefe693432009-09-23 15:56:20 -07005631
5632struct cgroup_subsys debug_subsys = {
5633 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005634 .css_alloc = debug_css_alloc,
5635 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005636 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005637 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005638};
5639#endif /* CONFIG_CGROUP_DEBUG */