blob: 789ec4683db3b73f83e455891bdb319124215f97 [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 Heoea15f8c2013-06-13 19:27:42 -0700218static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800220static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
221 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700224static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225{
Tejun Heo54766d42013-06-12 21:04:53 -0700226 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227}
228
Li Zefan78574cf2013-04-08 19:00:38 -0700229/**
230 * cgroup_is_descendant - test ancestry
231 * @cgrp: the cgroup to be tested
232 * @ancestor: possible ancestor of @cgrp
233 *
234 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
235 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
236 * and @ancestor are accessible.
237 */
238bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
239{
240 while (cgrp) {
241 if (cgrp == ancestor)
242 return true;
243 cgrp = cgrp->parent;
244 }
245 return false;
246}
247EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248
Adrian Bunke9685a02008-02-07 00:13:46 -0800249static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700250{
251 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 (1 << CGRP_RELEASABLE) |
253 (1 << CGRP_NOTIFY_ON_RELEASE);
254 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255}
256
Adrian Bunke9685a02008-02-07 00:13:46 -0800257static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700258{
Paul Menagebd89aab2007-10-18 23:40:44 -0700259 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260}
261
Tejun Heo30159ec2013-06-25 11:53:37 -0700262/**
263 * for_each_subsys - iterate all loaded cgroup subsystems
264 * @ss: the iteration cursor
265 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
266 *
267 * Should be called under cgroup_mutex.
268 */
269#define for_each_subsys(ss, i) \
270 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
271 if (({ lockdep_assert_held(&cgroup_mutex); \
272 !((ss) = cgroup_subsys[i]); })) { } \
273 else
274
275/**
276 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
277 * @ss: the iteration cursor
278 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
279 *
280 * Bulit-in subsystems are always present and iteration itself doesn't
281 * require any synchronization.
282 */
283#define for_each_builtin_subsys(ss, i) \
284 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
285 (((ss) = cgroup_subsys[i]) || true); (i)++)
286
Tejun Heo5549c492013-06-24 15:21:48 -0700287/* iterate each subsystem attached to a hierarchy */
288#define for_each_root_subsys(root, ss) \
289 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700290
Tejun Heo5549c492013-06-24 15:21:48 -0700291/* iterate across the active hierarchies */
292#define for_each_active_root(root) \
293 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700294
Tejun Heof6ea9372012-04-01 12:09:55 -0700295static inline struct cgroup *__d_cgrp(struct dentry *dentry)
296{
297 return dentry->d_fsdata;
298}
299
Tejun Heo05ef1d72012-04-01 12:09:56 -0700300static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700301{
302 return dentry->d_fsdata;
303}
304
Tejun Heo05ef1d72012-04-01 12:09:56 -0700305static inline struct cftype *__d_cft(struct dentry *dentry)
306{
307 return __d_cfe(dentry)->type;
308}
309
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310/**
311 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
312 * @cgrp: the cgroup to be checked for liveness
313 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700314 * On success, returns true; the mutex should be later unlocked. On
315 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700316 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700317static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318{
319 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700320 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700321 mutex_unlock(&cgroup_mutex);
322 return false;
323 }
324 return true;
325}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700326
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327/* the list of cgroups eligible for automatic release. Protected by
328 * release_list_lock */
329static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200330static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331static void cgroup_release_agent(struct work_struct *work);
332static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700333static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334
Tejun Heo69d02062013-06-12 21:04:50 -0700335/*
336 * A cgroup can be associated with multiple css_sets as different tasks may
337 * belong to different cgroups on different hierarchies. In the other
338 * direction, a css_set is naturally associated with multiple cgroups.
339 * This M:N relationship is represented by the following link structure
340 * which exists for each association and allows traversing the associations
341 * from both sides.
342 */
343struct cgrp_cset_link {
344 /* the cgroup and css_set this link associates */
345 struct cgroup *cgrp;
346 struct css_set *cset;
347
348 /* list of cgrp_cset_links anchored at cgrp->cset_links */
349 struct list_head cset_link;
350
351 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
352 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700353};
354
355/* The default css_set - used by init and its children prior to any
356 * hierarchies being mounted. It contains a pointer to the root state
357 * for each subsystem. Also used to anchor the list of css_sets. Not
358 * reference-counted, to improve performance when child cgroups
359 * haven't been created.
360 */
361
362static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700363static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700364
Ben Blume6a11052010-03-10 15:22:09 -0800365static int cgroup_init_idr(struct cgroup_subsys *ss,
366 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700367
Paul Menage817929e2007-10-18 23:39:36 -0700368/* css_set_lock protects the list of css_set objects, and the
369 * chain of tasks off each css_set. Nests outside task->alloc_lock
370 * due to cgroup_iter_start() */
371static DEFINE_RWLOCK(css_set_lock);
372static int css_set_count;
373
Paul Menage7717f7b2009-09-23 15:56:22 -0700374/*
375 * hash table for cgroup groups. This improves the performance to find
376 * an existing css_set. This hash doesn't (currently) take into
377 * account cgroups in empty hierarchies.
378 */
Li Zefan472b1052008-04-29 01:00:11 -0700379#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800380static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700381
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700383{
Li Zefan0ac801f2013-01-10 11:49:27 +0800384 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700385 struct cgroup_subsys *ss;
386 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700387
Tejun Heo30159ec2013-06-25 11:53:37 -0700388 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800389 key += (unsigned long)css[i];
390 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700391
Li Zefan0ac801f2013-01-10 11:49:27 +0800392 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700399static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700400
Tejun Heo5abb8852013-06-12 21:04:49 -0700401static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700402{
Tejun Heo69d02062013-06-12 21:04:50 -0700403 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700404
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 /*
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
408 * rwlock
409 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700410 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700411 return;
412 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700413 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700414 write_unlock(&css_set_lock);
415 return;
416 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 css_set_count--;
421
Tejun Heo69d02062013-06-12 21:04:50 -0700422 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700423 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700424
Tejun Heo69d02062013-06-12 21:04:50 -0700425 list_del(&link->cset_link);
426 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800427
Tejun Heoddd69142013-06-12 21:04:54 -0700428 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700429 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700431 set_bit(CGRP_RELEASABLE, &cgrp->flags);
432 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700434
435 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700437
438 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700440}
441
442/*
443 * refcounted get/put for css_set objects
444 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700445static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700446{
Tejun Heo5abb8852013-06-12 21:04:49 -0700447 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700448}
449
Tejun Heo5abb8852013-06-12 21:04:49 -0700450static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700451{
Tejun Heo5abb8852013-06-12 21:04:49 -0700452 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700453}
454
Tejun Heo5abb8852013-06-12 21:04:49 -0700455static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456{
Tejun Heo5abb8852013-06-12 21:04:49 -0700457 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458}
459
Tejun Heob326f9d2013-06-24 15:21:48 -0700460/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700461 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700462 * @cset: candidate css_set being tested
463 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 * @new_cgrp: cgroup that's being entered by the task
465 * @template: desired set of css pointers in css_set (pre-calculated)
466 *
467 * Returns true if "cg" matches "old_cg" except for the hierarchy
468 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
469 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700470static bool compare_css_sets(struct css_set *cset,
471 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 struct cgroup *new_cgrp,
473 struct cgroup_subsys_state *template[])
474{
475 struct list_head *l1, *l2;
476
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 /* Not all subsystems matched */
479 return false;
480 }
481
482 /*
483 * Compare cgroup pointers in order to distinguish between
484 * different cgroups in heirarchies with no subsystems. We
485 * could get by with just this check alone (and skip the
486 * memcmp above) but on most setups the memcmp check will
487 * avoid the need for this more expensive check on almost all
488 * candidates.
489 */
490
Tejun Heo69d02062013-06-12 21:04:50 -0700491 l1 = &cset->cgrp_links;
492 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700494 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700496
497 l1 = l1->next;
498 l2 = l2->next;
499 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700500 if (l1 == &cset->cgrp_links) {
501 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700502 break;
503 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700504 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 }
506 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700507 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
508 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
509 cgrp1 = link1->cgrp;
510 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700511 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700512 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700513
514 /*
515 * If this hierarchy is the hierarchy of the cgroup
516 * that's changing, then we need to check that this
517 * css_set points to the new cgroup; if it's any other
518 * hierarchy, then this css_set should point to the
519 * same cgroup as the old css_set.
520 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700521 if (cgrp1->root == new_cgrp->root) {
522 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700523 return false;
524 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700525 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700526 return false;
527 }
528 }
529 return true;
530}
531
Tejun Heob326f9d2013-06-24 15:21:48 -0700532/**
533 * find_existing_css_set - init css array and find the matching css_set
534 * @old_cset: the css_set that we're using before the cgroup transition
535 * @cgrp: the cgroup that we're moving into
536 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700537 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700538static struct css_set *find_existing_css_set(struct css_set *old_cset,
539 struct cgroup *cgrp,
540 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700541{
Paul Menagebd89aab2007-10-18 23:40:44 -0700542 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700543 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800545 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700546 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700547
Ben Blumaae8aab2010-03-10 15:22:07 -0800548 /*
549 * Build the set of subsystem state objects that we want to see in the
550 * new css_set. while subsystems can change globally, the entries here
551 * won't change, so no need for locking.
552 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700553 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400554 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700555 /* Subsystem is in this hierarchy. So we want
556 * the subsystem state from the new
557 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700558 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700559 } else {
560 /* Subsystem is not in this hierarchy, so we
561 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700562 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700563 }
564 }
565
Li Zefan0ac801f2013-01-10 11:49:27 +0800566 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700567 hash_for_each_possible(css_set_table, cset, hlist, key) {
568 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700569 continue;
570
571 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700572 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700573 }
Paul Menage817929e2007-10-18 23:39:36 -0700574
575 /* No existing cgroup group matched */
576 return NULL;
577}
578
Tejun Heo69d02062013-06-12 21:04:50 -0700579static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700580{
Tejun Heo69d02062013-06-12 21:04:50 -0700581 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700582
Tejun Heo69d02062013-06-12 21:04:50 -0700583 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
584 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700585 kfree(link);
586 }
587}
588
Tejun Heo69d02062013-06-12 21:04:50 -0700589/**
590 * allocate_cgrp_cset_links - allocate cgrp_cset_links
591 * @count: the number of links to allocate
592 * @tmp_links: list_head the allocated links are put on
593 *
594 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
595 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700596 */
Tejun Heo69d02062013-06-12 21:04:50 -0700597static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700598{
Tejun Heo69d02062013-06-12 21:04:50 -0700599 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700600 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700601
602 INIT_LIST_HEAD(tmp_links);
603
Li Zefan36553432008-07-29 22:33:19 -0700604 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700605 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700606 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700607 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700608 return -ENOMEM;
609 }
Tejun Heo69d02062013-06-12 21:04:50 -0700610 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700611 }
612 return 0;
613}
614
Li Zefanc12f65d2009-01-07 18:07:42 -0800615/**
616 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700617 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700618 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800619 * @cgrp: the destination cgroup
620 */
Tejun Heo69d02062013-06-12 21:04:50 -0700621static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
622 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800623{
Tejun Heo69d02062013-06-12 21:04:50 -0700624 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800625
Tejun Heo69d02062013-06-12 21:04:50 -0700626 BUG_ON(list_empty(tmp_links));
627 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
628 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700629 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700630 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 /*
632 * Always add links to the tail of the list so that the list
633 * is sorted by order of hierarchy creation
634 */
Tejun Heo69d02062013-06-12 21:04:50 -0700635 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800636}
637
Tejun Heob326f9d2013-06-24 15:21:48 -0700638/**
639 * find_css_set - return a new css_set with one cgroup updated
640 * @old_cset: the baseline css_set
641 * @cgrp: the cgroup to be updated
642 *
643 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
644 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700645 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700646static struct css_set *find_css_set(struct css_set *old_cset,
647 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700648{
Tejun Heob326f9d2013-06-24 15:21:48 -0700649 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700651 struct list_head tmp_links;
652 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800653 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700654
Tejun Heob326f9d2013-06-24 15:21:48 -0700655 lockdep_assert_held(&cgroup_mutex);
656
Paul Menage817929e2007-10-18 23:39:36 -0700657 /* First see if we already have a cgroup group that matches
658 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700659 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 cset = find_existing_css_set(old_cset, cgrp, template);
661 if (cset)
662 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700663 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700664
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 if (cset)
666 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700667
Tejun Heof4f4be22013-06-12 21:04:51 -0700668 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700669 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700670 return NULL;
671
Tejun Heo69d02062013-06-12 21:04:50 -0700672 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700673 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700674 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700675 return NULL;
676 }
677
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700679 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 INIT_LIST_HEAD(&cset->tasks);
681 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700682
683 /* Copy the set of subsystem state objects generated in
684 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700686
687 write_lock(&css_set_lock);
688 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700689 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700690 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700691
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 if (c->root == cgrp->root)
693 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700694 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 }
Paul Menage817929e2007-10-18 23:39:36 -0700696
Tejun Heo69d02062013-06-12 21:04:50 -0700697 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700698
Paul Menage817929e2007-10-18 23:39:36 -0700699 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700700
701 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700702 key = css_set_hash(cset->subsys);
703 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700704
Paul Menage817929e2007-10-18 23:39:36 -0700705 write_unlock(&css_set_lock);
706
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700708}
709
Paul Menageddbcc7e2007-10-18 23:39:30 -0700710/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 * Return the cgroup for "task" from the given hierarchy. Must be
712 * called with cgroup_mutex held.
713 */
714static struct cgroup *task_cgroup_from_root(struct task_struct *task,
715 struct cgroupfs_root *root)
716{
Tejun Heo5abb8852013-06-12 21:04:49 -0700717 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 struct cgroup *res = NULL;
719
720 BUG_ON(!mutex_is_locked(&cgroup_mutex));
721 read_lock(&css_set_lock);
722 /*
723 * No need to lock the task - since we hold cgroup_mutex the
724 * task can't change groups, so the only thing that can happen
725 * is that it exits and its css is set back to init_css_set.
726 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700727 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700728 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 res = &root->top_cgroup;
730 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700731 struct cgrp_cset_link *link;
732
733 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700734 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700735
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 if (c->root == root) {
737 res = c;
738 break;
739 }
740 }
741 }
742 read_unlock(&css_set_lock);
743 BUG_ON(!res);
744 return res;
745}
746
747/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 * There is one global cgroup mutex. We also require taking
749 * task_lock() when dereferencing a task's cgroup subsys pointers.
750 * See "The task_lock() exception", at the end of this comment.
751 *
752 * A task must hold cgroup_mutex to modify cgroups.
753 *
754 * Any task can increment and decrement the count field without lock.
755 * So in general, code holding cgroup_mutex can't rely on the count
756 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800757 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 * means that no tasks are currently attached, therefore there is no
759 * way a task attached to that cgroup can fork (the other way to
760 * increment the count). So code holding cgroup_mutex can safely
761 * assume that if the count is zero, it will stay zero. Similarly, if
762 * a task holds cgroup_mutex on a cgroup with zero count, it
763 * knows that the cgroup won't be removed, as cgroup_rmdir()
764 * needs that mutex.
765 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
767 * (usually) take cgroup_mutex. These are the two most performance
768 * critical pieces of code here. The exception occurs on cgroup_exit(),
769 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
770 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800771 * to the release agent with the name of the cgroup (path relative to
772 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 *
774 * A cgroup can only be deleted if both its 'count' of using tasks
775 * is zero, and its list of 'children' cgroups is empty. Since all
776 * tasks in the system use _some_ cgroup, and since there is always at
777 * least one task in the system (init, pid == 1), therefore, top_cgroup
778 * always has either children cgroups and/or using tasks. So we don't
779 * need a special hack to ensure that top_cgroup cannot be deleted.
780 *
781 * The task_lock() exception
782 *
783 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800784 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800785 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 * several performance critical places that need to reference
787 * task->cgroup without the expense of grabbing a system global
788 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800789 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
791 * the task_struct routinely used for such matters.
792 *
793 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800794 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795 */
796
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797/*
798 * A couple of forward declarations required, due to cyclic reference loop:
799 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
800 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
801 * -> cgroup_mkdir.
802 */
803
Al Viro18bb1db2011-07-26 01:41:39 -0400804static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400806static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
807 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700808static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700809static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700810
811static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200812 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700813 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700814};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700816static int alloc_css_id(struct cgroup_subsys *ss,
817 struct cgroup *parent, struct cgroup *child);
818
Al Viroa5e7ed32011-07-26 01:55:55 -0400819static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700820{
821 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822
823 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400824 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100826 inode->i_uid = current_fsuid();
827 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
829 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
830 }
831 return inode;
832}
833
Li Zefan65dff752013-03-01 15:01:56 +0800834static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
835{
836 struct cgroup_name *name;
837
838 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
839 if (!name)
840 return NULL;
841 strcpy(name->name, dentry->d_name.name);
842 return name;
843}
844
Li Zefanbe445622013-01-24 14:31:42 +0800845static void cgroup_free_fn(struct work_struct *work)
846{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700847 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800848 struct cgroup_subsys *ss;
849
850 mutex_lock(&cgroup_mutex);
851 /*
852 * Release the subsystem state objects.
853 */
Tejun Heo5549c492013-06-24 15:21:48 -0700854 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800855 ss->css_free(cgrp);
856
857 cgrp->root->number_of_cgroups--;
858 mutex_unlock(&cgroup_mutex);
859
860 /*
Li Zefan415cf072013-04-08 14:35:02 +0800861 * We get a ref to the parent's dentry, and put the ref when
862 * this cgroup is being freed, so it's guaranteed that the
863 * parent won't be destroyed before its children.
864 */
865 dput(cgrp->parent->dentry);
866
Li Zefancc20e012013-04-26 11:58:02 -0700867 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
868
Li Zefan415cf072013-04-08 14:35:02 +0800869 /*
Li Zefanbe445622013-01-24 14:31:42 +0800870 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700871 * created the cgroup. This will free cgrp->root, if we are
872 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800873 */
874 deactivate_super(cgrp->root->sb);
875
876 /*
877 * if we're getting rid of the cgroup, refcount should ensure
878 * that there are no pidlists left.
879 */
880 BUG_ON(!list_empty(&cgrp->pidlists));
881
882 simple_xattrs_free(&cgrp->xattrs);
883
Li Zefan65dff752013-03-01 15:01:56 +0800884 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800885 kfree(cgrp);
886}
887
888static void cgroup_free_rcu(struct rcu_head *head)
889{
890 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
891
Tejun Heoea15f8c2013-06-13 19:27:42 -0700892 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
893 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800894}
895
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896static void cgroup_diput(struct dentry *dentry, struct inode *inode)
897{
898 /* is dentry a directory ? if so, kfree() associated cgroup */
899 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700900 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800901
Tejun Heo54766d42013-06-12 21:04:53 -0700902 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800903 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700904 } else {
905 struct cfent *cfe = __d_cfe(dentry);
906 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
907
908 WARN_ONCE(!list_empty(&cfe->node) &&
909 cgrp != &cgrp->root->top_cgroup,
910 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700911 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700912 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913 }
914 iput(inode);
915}
916
Al Viroc72a04e2011-01-14 05:31:45 +0000917static int cgroup_delete(const struct dentry *d)
918{
919 return 1;
920}
921
Paul Menageddbcc7e2007-10-18 23:39:30 -0700922static void remove_dir(struct dentry *d)
923{
924 struct dentry *parent = dget(d->d_parent);
925
926 d_delete(d);
927 simple_rmdir(parent->d_inode, d);
928 dput(parent);
929}
930
Li Zefan2739d3c2013-01-21 18:18:33 +0800931static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700932{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700933 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
936 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100937
Li Zefan2739d3c2013-01-21 18:18:33 +0800938 /*
939 * If we're doing cleanup due to failure of cgroup_create(),
940 * the corresponding @cfe may not exist.
941 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 list_for_each_entry(cfe, &cgrp->files, node) {
943 struct dentry *d = cfe->dentry;
944
945 if (cft && cfe->type != cft)
946 continue;
947
948 dget(d);
949 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700950 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951 list_del_init(&cfe->node);
952 dput(d);
953
Li Zefan2739d3c2013-01-21 18:18:33 +0800954 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700956}
957
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400958/**
959 * cgroup_clear_directory - selective removal of base and subsystem files
960 * @dir: directory containing the files
961 * @base_files: true if the base files should be removed
962 * @subsys_mask: mask of the subsystem ids whose files should be removed
963 */
964static void cgroup_clear_directory(struct dentry *dir, bool base_files,
965 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700966{
967 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400968 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969
Tejun Heo5549c492013-06-24 15:21:48 -0700970 for_each_root_subsys(cgrp->root, ss) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400971 struct cftype_set *set;
972 if (!test_bit(ss->subsys_id, &subsys_mask))
973 continue;
974 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800975 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 }
977 if (base_files) {
978 while (!list_empty(&cgrp->files))
979 cgroup_rm_file(cgrp, NULL);
980 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981}
982
983/*
984 * NOTE : the dentry must have been dget()'ed
985 */
986static void cgroup_d_remove_dir(struct dentry *dentry)
987{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100988 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400989 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100990
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400991 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100993 parent = dentry->d_parent;
994 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800995 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100997 spin_unlock(&dentry->d_lock);
998 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700999 remove_dir(dentry);
1000}
1001
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001002/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001003 * Call with cgroup_mutex held. Drops reference counts on modules, including
1004 * any duplicate ones that parse_cgroupfs_options took. If this function
1005 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001006 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001008 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009{
Paul Menagebd89aab2007-10-18 23:40:44 -07001010 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001011 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 int i;
1013
Ben Blumaae8aab2010-03-10 15:22:07 -08001014 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001015 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001016
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001018 for_each_subsys(ss, i) {
Li Zefan8d53d552008-02-23 15:24:11 -08001019 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001020
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001021 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001023
Tejun Heo9871bf92013-06-24 15:21:47 -07001024 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 /* Subsystem isn't free */
1026 return -EBUSY;
1027 }
1028 }
1029
1030 /* Currently we don't handle adding/removing subsystems when
1031 * any child cgroups exist. This is theoretically supportable
1032 * but involves complex error handling, so it's being left until
1033 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001034 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 return -EBUSY;
1036
1037 /* Process each subsystem */
Tejun Heo30159ec2013-06-25 11:53:37 -07001038 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001040
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001041 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001042 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001043 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001044 BUG_ON(!cgroup_dummy_top->subsys[i]);
1045 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001046
Tejun Heo9871bf92013-06-24 15:21:47 -07001047 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001048 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001049 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001050 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001052 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001053
Ben Blumcf5d5942010-03-10 15:22:09 -08001054 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001055 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001056 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001058 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001059 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001060
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001062 ss->bind(cgroup_dummy_top);
1063 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001064 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001065 cgroup_subsys[i]->root = &cgroup_dummy_root;
1066 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001067
Ben Blumcf5d5942010-03-10 15:22:09 -08001068 /* subsystem is now free - drop reference on module */
1069 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001070 root->subsys_mask &= ~bit;
1071 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001072 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001074 /*
1075 * a refcount was taken, but we already had one, so
1076 * drop the extra reference.
1077 */
1078 module_put(ss->module);
1079#ifdef CONFIG_MODULE_UNLOAD
1080 BUG_ON(ss->module && !module_refcount(ss->module));
1081#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 } else {
1083 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001084 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 }
1086 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087
Tejun Heo1672d042013-06-25 18:04:54 -07001088 /*
1089 * Mark @root has finished binding subsystems. @root->subsys_mask
1090 * now matches the bound subsystems.
1091 */
1092 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1093
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 return 0;
1095}
1096
Al Viro34c80b12011-12-08 21:32:45 -05001097static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098{
Al Viro34c80b12011-12-08 21:32:45 -05001099 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 struct cgroup_subsys *ss;
1101
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001102 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001103 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001105 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1106 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001107 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001109 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001110 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001111 if (strlen(root->release_agent_path))
1112 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001113 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001114 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001115 if (strlen(root->name))
1116 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001117 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 return 0;
1119}
1120
1121struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001122 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001124 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001125 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001126 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001127 /* User explicitly requested empty subsystem */
1128 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001129
1130 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001131
Paul Menageddbcc7e2007-10-18 23:39:30 -07001132};
1133
Ben Blumaae8aab2010-03-10 15:22:07 -08001134/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001135 * Convert a hierarchy specifier into a bitmask of subsystems and
1136 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1137 * array. This function takes refcounts on subsystems to be used, unless it
1138 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001139 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001140static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001141{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001142 char *token, *o = data;
1143 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001144 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001145 bool module_pin_failed = false;
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
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 /*
1289 * Grab references on all the modules we'll need, so the subsystems
1290 * don't dance around before rebind_subsystems attaches them. This may
1291 * take duplicate reference counts on a subsystem that's already used,
1292 * but rebind_subsystems handles this case.
1293 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001294 for_each_subsys(ss, i) {
1295 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001296 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001297 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 module_pin_failed = true;
1299 break;
1300 }
1301 }
1302 if (module_pin_failed) {
1303 /*
1304 * oops, one of the modules was going away. this means that we
1305 * raced with a module_delete call, and to the user this is
1306 * essentially a "subsystem doesn't exist" case.
1307 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001308 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 /* drop refcounts only on the ones we took */
1310 unsigned long bit = 1UL << i;
1311
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001312 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001313 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001314 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001315 }
1316 return -ENOENT;
1317 }
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319 return 0;
1320}
1321
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001322static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001323{
Tejun Heo30159ec2013-06-25 11:53:37 -07001324 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001326
Tejun Heoeb178d02013-06-25 18:05:21 -07001327 mutex_lock(&cgroup_mutex);
1328 for_each_subsys(ss, i)
1329 if (subsys_mask & (1UL << i))
1330 module_put(cgroup_subsys[i]->module);
1331 mutex_unlock(&cgroup_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001332}
1333
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1335{
1336 int ret = 0;
1337 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001338 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001339 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001340 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001341
Tejun Heo873fe092013-04-14 20:15:26 -07001342 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1343 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1344 return -EINVAL;
1345 }
1346
Paul Menagebd89aab2007-10-18 23:40:44 -07001347 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001348 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001349 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001350
1351 /* See what subsystems are wanted */
1352 ret = parse_cgroupfs_options(data, &opts);
1353 if (ret)
1354 goto out_unlock;
1355
Tejun Heoa8a648c2013-06-24 15:21:47 -07001356 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001357 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1358 task_tgid_nr(current), current->comm);
1359
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001360 added_mask = opts.subsys_mask & ~root->subsys_mask;
1361 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001362
Ben Blumcf5d5942010-03-10 15:22:09 -08001363 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001364 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001365 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001366 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1367 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1368 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001369 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001370 goto out_unlock;
1371 }
1372
Gao feng7083d032012-12-03 09:28:18 +08001373 /*
1374 * Clear out the files of subsystems that should be removed, do
1375 * this before rebind_subsystems, since rebind_subsystems may
1376 * change this hierarchy's subsys_list.
1377 */
1378 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1379
Tejun Heoa8a648c2013-06-24 15:21:47 -07001380 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001381 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001382 /* rebind_subsystems failed, re-populate the removed files */
1383 cgroup_populate_dir(cgrp, false, removed_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001384 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001385 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001387 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001388 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389
Paul Menage81a6a5c2007-10-18 23:39:38 -07001390 if (opts.release_agent)
1391 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001393 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001394 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001395 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001397 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoe2bd4162013-06-27 19:37:23 -07001398 if (ret)
1399 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001400 return ret;
1401}
1402
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001403static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404 .statfs = simple_statfs,
1405 .drop_inode = generic_delete_inode,
1406 .show_options = cgroup_show_options,
1407 .remount_fs = cgroup_remount,
1408};
1409
Paul Menagecc31edc2008-10-18 20:28:04 -07001410static void init_cgroup_housekeeping(struct cgroup *cgrp)
1411{
1412 INIT_LIST_HEAD(&cgrp->sibling);
1413 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001414 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001415 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001417 INIT_LIST_HEAD(&cgrp->pidlists);
1418 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001419 INIT_LIST_HEAD(&cgrp->event_list);
1420 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001421 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001422}
Paul Menagec6d57f32009-09-23 15:56:19 -07001423
Paul Menageddbcc7e2007-10-18 23:39:30 -07001424static void init_cgroup_root(struct cgroupfs_root *root)
1425{
Paul Menagebd89aab2007-10-18 23:40:44 -07001426 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001427
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428 INIT_LIST_HEAD(&root->subsys_list);
1429 INIT_LIST_HEAD(&root->root_list);
1430 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001431 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001432 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001433 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434}
1435
Tejun Heofc76df72013-06-25 11:53:37 -07001436static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001437{
Tejun Heo1a574232013-04-14 11:36:58 -07001438 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001439
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001440 lockdep_assert_held(&cgroup_mutex);
1441 lockdep_assert_held(&cgroup_root_mutex);
1442
Tejun Heofc76df72013-06-25 11:53:37 -07001443 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1444 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001445 if (id < 0)
1446 return id;
1447
1448 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001449 return 0;
1450}
1451
1452static void cgroup_exit_root_id(struct cgroupfs_root *root)
1453{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001454 lockdep_assert_held(&cgroup_mutex);
1455 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001456
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001457 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001458 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001459 root->hierarchy_id = 0;
1460 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001461}
1462
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463static int cgroup_test_super(struct super_block *sb, void *data)
1464{
Paul Menagec6d57f32009-09-23 15:56:19 -07001465 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466 struct cgroupfs_root *root = sb->s_fs_info;
1467
Paul Menagec6d57f32009-09-23 15:56:19 -07001468 /* If we asked for a name then it must match */
1469 if (opts->name && strcmp(opts->name, root->name))
1470 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001471
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001472 /*
1473 * If we asked for subsystems (or explicitly for no
1474 * subsystems) then they must match
1475 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001476 if ((opts->subsys_mask || opts->none)
1477 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001478 return 0;
1479
1480 return 1;
1481}
1482
Paul Menagec6d57f32009-09-23 15:56:19 -07001483static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1484{
1485 struct cgroupfs_root *root;
1486
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001487 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 return NULL;
1489
1490 root = kzalloc(sizeof(*root), GFP_KERNEL);
1491 if (!root)
1492 return ERR_PTR(-ENOMEM);
1493
1494 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001495
Tejun Heo1672d042013-06-25 18:04:54 -07001496 /*
1497 * We need to set @root->subsys_mask now so that @root can be
1498 * matched by cgroup_test_super() before it finishes
1499 * initialization; otherwise, competing mounts with the same
1500 * options may try to bind the same subsystems instead of waiting
1501 * for the first one leading to unexpected mount errors.
1502 * SUBSYS_BOUND will be set once actual binding is complete.
1503 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001504 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001506 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 if (opts->release_agent)
1508 strcpy(root->release_agent_path, opts->release_agent);
1509 if (opts->name)
1510 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001511 if (opts->cpuset_clone_children)
1512 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 return root;
1514}
1515
Tejun Heofa3ca072013-04-14 11:36:56 -07001516static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001517{
Tejun Heofa3ca072013-04-14 11:36:56 -07001518 if (root) {
1519 /* hierarhcy ID shoulid already have been released */
1520 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001521
Tejun Heofa3ca072013-04-14 11:36:56 -07001522 ida_destroy(&root->cgroup_ida);
1523 kfree(root);
1524 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001525}
1526
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527static int cgroup_set_super(struct super_block *sb, void *data)
1528{
1529 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001530 struct cgroup_sb_opts *opts = data;
1531
1532 /* If we don't have a new root, we can't set up a new sb */
1533 if (!opts->new_root)
1534 return -EINVAL;
1535
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001536 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
1538 ret = set_anon_super(sb, NULL);
1539 if (ret)
1540 return ret;
1541
Paul Menagec6d57f32009-09-23 15:56:19 -07001542 sb->s_fs_info = opts->new_root;
1543 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
1545 sb->s_blocksize = PAGE_CACHE_SIZE;
1546 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1547 sb->s_magic = CGROUP_SUPER_MAGIC;
1548 sb->s_op = &cgroup_ops;
1549
1550 return 0;
1551}
1552
1553static int cgroup_get_rootdir(struct super_block *sb)
1554{
Al Viro0df6a632010-12-21 13:29:29 -05001555 static const struct dentry_operations cgroup_dops = {
1556 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001557 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001558 };
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 struct inode *inode =
1561 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562
1563 if (!inode)
1564 return -ENOMEM;
1565
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 inode->i_fop = &simple_dir_operations;
1567 inode->i_op = &cgroup_dir_inode_operations;
1568 /* directories start off with i_nlink == 2 (for "." entry) */
1569 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001570 sb->s_root = d_make_root(inode);
1571 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001573 /* for everything else we want ->d_op set */
1574 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 return 0;
1576}
1577
Al Virof7e83572010-07-26 13:23:11 +04001578static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001580 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581{
1582 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 int ret = 0;
1585 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001587 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588
1589 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001590 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001591 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001592 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 if (ret)
1594 goto out_err;
1595
1596 /*
1597 * Allocate a new cgroup root. We may not need it if we're
1598 * reusing an existing hierarchy.
1599 */
1600 new_root = cgroup_root_from_opts(&opts);
1601 if (IS_ERR(new_root)) {
1602 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001603 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001604 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001606
Paul Menagec6d57f32009-09-23 15:56:19 -07001607 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001608 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001610 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001611 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001612 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001613 }
1614
Paul Menagec6d57f32009-09-23 15:56:19 -07001615 root = sb->s_fs_info;
1616 BUG_ON(!root);
1617 if (root == opts.new_root) {
1618 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001619 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001620 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001621 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001622 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001623 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001624 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
1626 BUG_ON(sb->s_root != NULL);
1627
1628 ret = cgroup_get_rootdir(sb);
1629 if (ret)
1630 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001631 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632
Paul Menage817929e2007-10-18 23:39:36 -07001633 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001637 /* Check for name clashes with existing mounts */
1638 ret = -EBUSY;
1639 if (strlen(root->name))
1640 for_each_active_root(existing_root)
1641 if (!strcmp(existing_root->name, root->name))
1642 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001643
Paul Menage817929e2007-10-18 23:39:36 -07001644 /*
1645 * We're accessing css_set_count without locking
1646 * css_set_lock here, but that's OK - it can only be
1647 * increased by someone holding cgroup_lock, and
1648 * that's us. The worst that can happen is that we
1649 * have some link structures left over
1650 */
Tejun Heo69d02062013-06-12 21:04:50 -07001651 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001652 if (ret)
1653 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001654
Tejun Heofc76df72013-06-25 11:53:37 -07001655 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1656 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001657 if (ret)
1658 goto unlock_drop;
1659
Tejun Heoa8a648c2013-06-24 15:21:47 -07001660 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001662 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001663 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001665 /*
1666 * There must be no failure case after here, since rebinding
1667 * takes care of subsystems' refcounts, which are explicitly
1668 * dropped in the failure exit path.
1669 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670
1671 /* EBUSY should be the only error here */
1672 BUG_ON(ret);
1673
Tejun Heo9871bf92013-06-24 15:21:47 -07001674 list_add(&root->root_list, &cgroup_roots);
1675 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676
Li Zefanc12f65d2009-01-07 18:07:42 -08001677 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 root->top_cgroup.dentry = sb->s_root;
1679
Paul Menage817929e2007-10-18 23:39:36 -07001680 /* Link the top cgroup in this hierarchy into all
1681 * the css_set objects */
1682 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001683 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001684 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001685 write_unlock(&css_set_lock);
1686
Tejun Heo69d02062013-06-12 21:04:50 -07001687 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001688
Li Zefanc12f65d2009-01-07 18:07:42 -08001689 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 BUG_ON(root->number_of_cgroups != 1);
1691
eparis@redhat2ce97382011-06-02 21:20:51 +10001692 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001693 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001694 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001695 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001697 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001698 } else {
1699 /*
1700 * We re-used an existing hierarchy - the new root (if
1701 * any) is not needed
1702 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001703 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001704
Tejun Heoc7ba8282013-06-29 14:06:10 -07001705 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001706 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1707 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1708 ret = -EINVAL;
1709 goto drop_new_super;
1710 } else {
1711 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1712 }
Tejun Heo873fe092013-04-14 20:15:26 -07001713 }
1714
Ben Blumcf5d5942010-03-10 15:22:09 -08001715 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001716 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717 }
1718
Paul Menagec6d57f32009-09-23 15:56:19 -07001719 kfree(opts.release_agent);
1720 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001721 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001723 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001724 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001725 mutex_unlock(&cgroup_root_mutex);
1726 mutex_unlock(&cgroup_mutex);
1727 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001729 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001730 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001731 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001732 out_err:
1733 kfree(opts.release_agent);
1734 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001735 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736}
1737
1738static void cgroup_kill_sb(struct super_block *sb) {
1739 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001740 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001741 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742 int ret;
1743
1744 BUG_ON(!root);
1745
1746 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001747 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748
1749 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001750 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751
1752 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001753 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1754 ret = rebind_subsystems(root, 0, root->subsys_mask);
1755 /* Shouldn't be able to fail ... */
1756 BUG_ON(ret);
1757 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001758
Paul Menage817929e2007-10-18 23:39:36 -07001759 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001760 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001761 * root cgroup
1762 */
1763 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001764
Tejun Heo69d02062013-06-12 21:04:50 -07001765 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1766 list_del(&link->cset_link);
1767 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001768 kfree(link);
1769 }
1770 write_unlock(&css_set_lock);
1771
Paul Menage839ec542009-01-29 14:25:22 -08001772 if (!list_empty(&root->root_list)) {
1773 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001774 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001775 }
Li Zefane5f6a862009-01-07 18:07:41 -08001776
Tejun Heofa3ca072013-04-14 11:36:56 -07001777 cgroup_exit_root_id(root);
1778
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001779 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 mutex_unlock(&cgroup_mutex);
1781
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001782 simple_xattrs_free(&cgrp->xattrs);
1783
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001785 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786}
1787
1788static struct file_system_type cgroup_fs_type = {
1789 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001790 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 .kill_sb = cgroup_kill_sb,
1792};
1793
Greg KH676db4a2010-08-05 13:53:35 -07001794static struct kobject *cgroup_kobj;
1795
Li Zefana043e3b2008-02-23 15:24:09 -08001796/**
1797 * cgroup_path - generate the path of a cgroup
1798 * @cgrp: the cgroup in question
1799 * @buf: the buffer to write the path into
1800 * @buflen: the length of the buffer
1801 *
Li Zefan65dff752013-03-01 15:01:56 +08001802 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1803 *
1804 * We can't generate cgroup path using dentry->d_name, as accessing
1805 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1806 * inode's i_mutex, while on the other hand cgroup_path() can be called
1807 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001809int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810{
Li Zefan65dff752013-03-01 15:01:56 +08001811 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001813
Tejun Heoda1f2962013-04-14 10:32:19 -07001814 if (!cgrp->parent) {
1815 if (strlcpy(buf, "/", buflen) >= buflen)
1816 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 return 0;
1818 }
1819
Tao Ma316eb662012-11-08 21:36:38 +08001820 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001821 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001822
Li Zefan65dff752013-03-01 15:01:56 +08001823 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001824 do {
Li Zefan65dff752013-03-01 15:01:56 +08001825 const char *name = cgroup_name(cgrp);
1826 int len;
1827
1828 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001830 goto out;
1831 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001832
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001834 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001836
1837 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001838 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001839 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001840 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001841out:
1842 rcu_read_unlock();
1843 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001844}
Ben Blum67523c42010-03-10 15:22:11 -08001845EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001846
Tejun Heo857a2be2013-04-14 20:50:08 -07001847/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001848 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001849 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001850 * @buf: the buffer to write the path into
1851 * @buflen: the length of the buffer
1852 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001853 * Determine @task's cgroup on the first (the one with the lowest non-zero
1854 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1855 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1856 * cgroup controller callbacks.
1857 *
1858 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001859 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001860int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001861{
1862 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001863 struct cgroup *cgrp;
1864 int hierarchy_id = 1, ret = 0;
1865
1866 if (buflen < 2)
1867 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001868
1869 mutex_lock(&cgroup_mutex);
1870
Tejun Heo913ffdb2013-07-11 16:34:48 -07001871 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1872
Tejun Heo857a2be2013-04-14 20:50:08 -07001873 if (root) {
1874 cgrp = task_cgroup_from_root(task, root);
1875 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001876 } else {
1877 /* if no hierarchy exists, everyone is in "/" */
1878 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001879 }
1880
1881 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001882 return ret;
1883}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001884EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001885
Ben Blum74a11662011-05-26 16:25:20 -07001886/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001887 * Control Group taskset
1888 */
Tejun Heo134d3372011-12-12 18:12:21 -08001889struct task_and_cgroup {
1890 struct task_struct *task;
1891 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001892 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001893};
1894
Tejun Heo2f7ee562011-12-12 18:12:21 -08001895struct cgroup_taskset {
1896 struct task_and_cgroup single;
1897 struct flex_array *tc_array;
1898 int tc_array_len;
1899 int idx;
1900 struct cgroup *cur_cgrp;
1901};
1902
1903/**
1904 * cgroup_taskset_first - reset taskset and return the first task
1905 * @tset: taskset of interest
1906 *
1907 * @tset iteration is initialized and the first task is returned.
1908 */
1909struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1910{
1911 if (tset->tc_array) {
1912 tset->idx = 0;
1913 return cgroup_taskset_next(tset);
1914 } else {
1915 tset->cur_cgrp = tset->single.cgrp;
1916 return tset->single.task;
1917 }
1918}
1919EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1920
1921/**
1922 * cgroup_taskset_next - iterate to the next task in taskset
1923 * @tset: taskset of interest
1924 *
1925 * Return the next task in @tset. Iteration must have been initialized
1926 * with cgroup_taskset_first().
1927 */
1928struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1929{
1930 struct task_and_cgroup *tc;
1931
1932 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1933 return NULL;
1934
1935 tc = flex_array_get(tset->tc_array, tset->idx++);
1936 tset->cur_cgrp = tc->cgrp;
1937 return tc->task;
1938}
1939EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1940
1941/**
1942 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1943 * @tset: taskset of interest
1944 *
1945 * Return the cgroup for the current (last returned) task of @tset. This
1946 * function must be preceded by either cgroup_taskset_first() or
1947 * cgroup_taskset_next().
1948 */
1949struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1950{
1951 return tset->cur_cgrp;
1952}
1953EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1954
1955/**
1956 * cgroup_taskset_size - return the number of tasks in taskset
1957 * @tset: taskset of interest
1958 */
1959int cgroup_taskset_size(struct cgroup_taskset *tset)
1960{
1961 return tset->tc_array ? tset->tc_array_len : 1;
1962}
1963EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1964
1965
Ben Blum74a11662011-05-26 16:25:20 -07001966/*
1967 * cgroup_task_migrate - move a task from one cgroup to another.
1968 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001969 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001970 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001971static void cgroup_task_migrate(struct cgroup *old_cgrp,
1972 struct task_struct *tsk,
1973 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001974{
Tejun Heo5abb8852013-06-12 21:04:49 -07001975 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001976
1977 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001978 * We are synchronized through threadgroup_lock() against PF_EXITING
1979 * setting such that we can't race against cgroup_exit() changing the
1980 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001981 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001982 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001983 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001984
Ben Blum74a11662011-05-26 16:25:20 -07001985 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001986 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001987 task_unlock(tsk);
1988
1989 /* Update the css_set linked lists if we're using them */
1990 write_lock(&css_set_lock);
1991 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001992 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001993 write_unlock(&css_set_lock);
1994
1995 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001996 * We just gained a reference on old_cset by taking it from the
1997 * task. As trading it for new_cset is protected by cgroup_mutex,
1998 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001999 */
Tejun Heo5abb8852013-06-12 21:04:49 -07002000 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
2001 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07002002}
2003
Li Zefana043e3b2008-02-23 15:24:09 -08002004/**
Li Zefan081aa452013-03-13 09:17:09 +08002005 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07002006 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08002007 * @tsk: the task or the leader of the threadgroup to be attached
2008 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07002009 *
Tejun Heo257058a2011-12-12 18:12:21 -08002010 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08002011 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002012 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002013static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2014 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002015{
2016 int retval, i, group_size;
2017 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002018 struct cgroupfs_root *root = cgrp->root;
2019 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002020 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002021 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002022 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002023 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002024
2025 /*
2026 * step 0: in order to do expensive, possibly blocking operations for
2027 * every thread, we cannot iterate the thread group list, since it needs
2028 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002029 * group - group_rwsem prevents new threads from appearing, and if
2030 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002031 */
Li Zefan081aa452013-03-13 09:17:09 +08002032 if (threadgroup)
2033 group_size = get_nr_threads(tsk);
2034 else
2035 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002036 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002037 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002038 if (!group)
2039 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002040 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002041 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002042 if (retval)
2043 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002044
Ben Blum74a11662011-05-26 16:25:20 -07002045 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002046 /*
2047 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2048 * already PF_EXITING could be freed from underneath us unless we
2049 * take an rcu_read_lock.
2050 */
2051 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002052 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002053 struct task_and_cgroup ent;
2054
Tejun Heocd3d0952011-12-12 18:12:21 -08002055 /* @tsk either already exited or can't exit until the end */
2056 if (tsk->flags & PF_EXITING)
2057 continue;
2058
Ben Blum74a11662011-05-26 16:25:20 -07002059 /* as per above, nr_threads may decrease, but not increase. */
2060 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002061 ent.task = tsk;
2062 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002063 /* nothing to do if this task is already in the cgroup */
2064 if (ent.cgrp == cgrp)
2065 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002066 /*
2067 * saying GFP_ATOMIC has no effect here because we did prealloc
2068 * earlier, but it's good form to communicate our expectations.
2069 */
Tejun Heo134d3372011-12-12 18:12:21 -08002070 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002071 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002072 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002073
2074 if (!threadgroup)
2075 break;
Ben Blum74a11662011-05-26 16:25:20 -07002076 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002077 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002078 /* remember the number of threads in the array for later. */
2079 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002080 tset.tc_array = group;
2081 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002082
Tejun Heo134d3372011-12-12 18:12:21 -08002083 /* methods shouldn't be called if no task is actually migrating */
2084 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002085 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002086 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002087
Ben Blum74a11662011-05-26 16:25:20 -07002088 /*
2089 * step 1: check that we can legitimately attach to the cgroup.
2090 */
Tejun Heo5549c492013-06-24 15:21:48 -07002091 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002092 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002093 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002094 if (retval) {
2095 failed_ss = ss;
2096 goto out_cancel_attach;
2097 }
2098 }
Ben Blum74a11662011-05-26 16:25:20 -07002099 }
2100
2101 /*
2102 * step 2: make sure css_sets exist for all threads to be migrated.
2103 * we use find_css_set, which allocates a new one if necessary.
2104 */
Ben Blum74a11662011-05-26 16:25:20 -07002105 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002106 struct css_set *old_cset;
2107
Tejun Heo134d3372011-12-12 18:12:21 -08002108 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002109 old_cset = task_css_set(tc->task);
2110 tc->cg = find_css_set(old_cset, cgrp);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002111 if (!tc->cg) {
2112 retval = -ENOMEM;
2113 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002114 }
2115 }
2116
2117 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002118 * step 3: now that we're guaranteed success wrt the css_sets,
2119 * proceed to move all tasks to the new cgroup. There are no
2120 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002121 */
Ben Blum74a11662011-05-26 16:25:20 -07002122 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002123 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002124 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002125 }
2126 /* nothing is sensitive to fork() after this point. */
2127
2128 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002129 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002130 */
Tejun Heo5549c492013-06-24 15:21:48 -07002131 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002132 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002133 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002134 }
2135
2136 /*
2137 * step 5: success! and cleanup
2138 */
Ben Blum74a11662011-05-26 16:25:20 -07002139 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002140out_put_css_set_refs:
2141 if (retval) {
2142 for (i = 0; i < group_size; i++) {
2143 tc = flex_array_get(group, i);
2144 if (!tc->cg)
2145 break;
2146 put_css_set(tc->cg);
2147 }
Ben Blum74a11662011-05-26 16:25:20 -07002148 }
2149out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002150 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002151 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002152 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002153 break;
Ben Blum74a11662011-05-26 16:25:20 -07002154 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002155 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002156 }
2157 }
Ben Blum74a11662011-05-26 16:25:20 -07002158out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002159 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002160 return retval;
2161}
2162
2163/*
2164 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002165 * function to attach either it or all tasks in its threadgroup. Will lock
2166 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002167 */
2168static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002171 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002172 int ret;
2173
Ben Blum74a11662011-05-26 16:25:20 -07002174 if (!cgroup_lock_live_group(cgrp))
2175 return -ENODEV;
2176
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002177retry_find_task:
2178 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002179 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002180 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002181 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002182 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002183 ret= -ESRCH;
2184 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002185 }
Ben Blum74a11662011-05-26 16:25:20 -07002186 /*
2187 * even if we're attaching all tasks in the thread group, we
2188 * only need to check permissions on one of them.
2189 */
David Howellsc69e8d92008-11-14 10:39:19 +11002190 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002191 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2192 !uid_eq(cred->euid, tcred->uid) &&
2193 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002194 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002195 ret = -EACCES;
2196 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002197 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002198 } else
2199 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002200
2201 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002202 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002203
2204 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002205 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002206 * trapped in a cpuset, or RT worker may be born in a cgroup
2207 * with no rt_runtime allocated. Just say no.
2208 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002209 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002210 ret = -EINVAL;
2211 rcu_read_unlock();
2212 goto out_unlock_cgroup;
2213 }
2214
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002215 get_task_struct(tsk);
2216 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002217
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002218 threadgroup_lock(tsk);
2219 if (threadgroup) {
2220 if (!thread_group_leader(tsk)) {
2221 /*
2222 * a race with de_thread from another thread's exec()
2223 * may strip us of our leadership, if this happens,
2224 * there is no choice but to throw this task away and
2225 * try again; this is
2226 * "double-double-toil-and-trouble-check locking".
2227 */
2228 threadgroup_unlock(tsk);
2229 put_task_struct(tsk);
2230 goto retry_find_task;
2231 }
Li Zefan081aa452013-03-13 09:17:09 +08002232 }
2233
2234 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2235
Tejun Heocd3d0952011-12-12 18:12:21 -08002236 threadgroup_unlock(tsk);
2237
Paul Menagebbcb81d2007-10-18 23:39:32 -07002238 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002239out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002240 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002241 return ret;
2242}
2243
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002244/**
2245 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2246 * @from: attach to all cgroups of a given task
2247 * @tsk: the task to be attached
2248 */
2249int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2250{
2251 struct cgroupfs_root *root;
2252 int retval = 0;
2253
Tejun Heo47cfcd02013-04-07 09:29:51 -07002254 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002255 for_each_active_root(root) {
2256 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2257
2258 retval = cgroup_attach_task(from_cg, tsk, false);
2259 if (retval)
2260 break;
2261 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002262 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002263
2264 return retval;
2265}
2266EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2267
Paul Menageaf351022008-07-25 01:47:01 -07002268static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2269{
Ben Blum74a11662011-05-26 16:25:20 -07002270 return attach_task_by_pid(cgrp, pid, false);
2271}
2272
2273static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2274{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002275 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002276}
2277
Paul Menagee788e062008-07-25 01:46:59 -07002278static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2279 const char *buffer)
2280{
2281 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002282 if (strlen(buffer) >= PATH_MAX)
2283 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002284 if (!cgroup_lock_live_group(cgrp))
2285 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002286 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002287 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002288 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002289 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002290 return 0;
2291}
2292
2293static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2294 struct seq_file *seq)
2295{
2296 if (!cgroup_lock_live_group(cgrp))
2297 return -ENODEV;
2298 seq_puts(seq, cgrp->root->release_agent_path);
2299 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002300 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002301 return 0;
2302}
2303
Tejun Heo873fe092013-04-14 20:15:26 -07002304static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2305 struct seq_file *seq)
2306{
2307 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002308 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002309}
2310
Paul Menage84eea842008-07-25 01:47:00 -07002311/* A buffer size big enough for numbers or short strings */
2312#define CGROUP_LOCAL_BUFFER_SIZE 64
2313
Paul Menagee73d2c62008-04-29 01:00:06 -07002314static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002315 struct file *file,
2316 const char __user *userbuf,
2317 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002318{
Paul Menage84eea842008-07-25 01:47:00 -07002319 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002320 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002321 char *end;
2322
2323 if (!nbytes)
2324 return -EINVAL;
2325 if (nbytes >= sizeof(buffer))
2326 return -E2BIG;
2327 if (copy_from_user(buffer, userbuf, nbytes))
2328 return -EFAULT;
2329
2330 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002331 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002332 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 if (*end)
2334 return -EINVAL;
2335 retval = cft->write_u64(cgrp, cft, val);
2336 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002337 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002338 if (*end)
2339 return -EINVAL;
2340 retval = cft->write_s64(cgrp, cft, val);
2341 }
Paul Menage355e0c42007-10-18 23:39:33 -07002342 if (!retval)
2343 retval = nbytes;
2344 return retval;
2345}
2346
Paul Menagedb3b1492008-07-25 01:46:58 -07002347static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2348 struct file *file,
2349 const char __user *userbuf,
2350 size_t nbytes, loff_t *unused_ppos)
2351{
Paul Menage84eea842008-07-25 01:47:00 -07002352 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002353 int retval = 0;
2354 size_t max_bytes = cft->max_write_len;
2355 char *buffer = local_buffer;
2356
2357 if (!max_bytes)
2358 max_bytes = sizeof(local_buffer) - 1;
2359 if (nbytes >= max_bytes)
2360 return -E2BIG;
2361 /* Allocate a dynamic buffer if we need one */
2362 if (nbytes >= sizeof(local_buffer)) {
2363 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2364 if (buffer == NULL)
2365 return -ENOMEM;
2366 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002367 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2368 retval = -EFAULT;
2369 goto out;
2370 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002371
2372 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002373 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002374 if (!retval)
2375 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002376out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002377 if (buffer != local_buffer)
2378 kfree(buffer);
2379 return retval;
2380}
2381
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2383 size_t nbytes, loff_t *ppos)
2384{
2385 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002386 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387
Tejun Heo54766d42013-06-12 21:04:53 -07002388 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002390 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002391 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002392 if (cft->write_u64 || cft->write_s64)
2393 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002394 if (cft->write_string)
2395 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002396 if (cft->trigger) {
2397 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2398 return ret ? ret : nbytes;
2399 }
Paul Menage355e0c42007-10-18 23:39:33 -07002400 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401}
2402
Paul Menagef4c753b2008-04-29 00:59:56 -07002403static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2404 struct file *file,
2405 char __user *buf, size_t nbytes,
2406 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002407{
Paul Menage84eea842008-07-25 01:47:00 -07002408 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002409 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002410 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2411
2412 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2413}
2414
Paul Menagee73d2c62008-04-29 01:00:06 -07002415static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2416 struct file *file,
2417 char __user *buf, size_t nbytes,
2418 loff_t *ppos)
2419{
Paul Menage84eea842008-07-25 01:47:00 -07002420 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002421 s64 val = cft->read_s64(cgrp, cft);
2422 int len = sprintf(tmp, "%lld\n", (long long) val);
2423
2424 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2425}
2426
Paul Menageddbcc7e2007-10-18 23:39:30 -07002427static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2428 size_t nbytes, loff_t *ppos)
2429{
2430 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002431 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432
Tejun Heo54766d42013-06-12 21:04:53 -07002433 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434 return -ENODEV;
2435
2436 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002437 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002438 if (cft->read_u64)
2439 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002440 if (cft->read_s64)
2441 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442 return -EINVAL;
2443}
2444
Paul Menage91796562008-04-29 01:00:01 -07002445/*
2446 * seqfile ops/methods for returning structured data. Currently just
2447 * supports string->u64 maps, but can be extended in future.
2448 */
2449
2450struct cgroup_seqfile_state {
2451 struct cftype *cft;
2452 struct cgroup *cgroup;
2453};
2454
2455static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2456{
2457 struct seq_file *sf = cb->state;
2458 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2459}
2460
2461static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2462{
2463 struct cgroup_seqfile_state *state = m->private;
2464 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002465 if (cft->read_map) {
2466 struct cgroup_map_cb cb = {
2467 .fill = cgroup_map_add,
2468 .state = m,
2469 };
2470 return cft->read_map(state->cgroup, cft, &cb);
2471 }
2472 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002473}
2474
Adrian Bunk96930a62008-07-25 19:46:21 -07002475static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002476{
2477 struct seq_file *seq = file->private_data;
2478 kfree(seq->private);
2479 return single_release(inode, file);
2480}
2481
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002482static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002483 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002484 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002485 .llseek = seq_lseek,
2486 .release = cgroup_seqfile_release,
2487};
2488
Paul Menageddbcc7e2007-10-18 23:39:30 -07002489static int cgroup_file_open(struct inode *inode, struct file *file)
2490{
2491 int err;
2492 struct cftype *cft;
2493
2494 err = generic_file_open(inode, file);
2495 if (err)
2496 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002498
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002499 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002500 struct cgroup_seqfile_state *state;
2501
2502 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002503 if (!state)
2504 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002505
Paul Menage91796562008-04-29 01:00:01 -07002506 state->cft = cft;
2507 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2508 file->f_op = &cgroup_seqfile_operations;
2509 err = single_open(file, cgroup_seqfile_show, state);
2510 if (err < 0)
2511 kfree(state);
2512 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002513 err = cft->open(inode, file);
2514 else
2515 err = 0;
2516
2517 return err;
2518}
2519
2520static int cgroup_file_release(struct inode *inode, struct file *file)
2521{
2522 struct cftype *cft = __d_cft(file->f_dentry);
2523 if (cft->release)
2524 return cft->release(inode, file);
2525 return 0;
2526}
2527
2528/*
2529 * cgroup_rename - Only allow simple rename of directories in place.
2530 */
2531static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2532 struct inode *new_dir, struct dentry *new_dentry)
2533{
Li Zefan65dff752013-03-01 15:01:56 +08002534 int ret;
2535 struct cgroup_name *name, *old_name;
2536 struct cgroup *cgrp;
2537
2538 /*
2539 * It's convinient to use parent dir's i_mutex to protected
2540 * cgrp->name.
2541 */
2542 lockdep_assert_held(&old_dir->i_mutex);
2543
Paul Menageddbcc7e2007-10-18 23:39:30 -07002544 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2545 return -ENOTDIR;
2546 if (new_dentry->d_inode)
2547 return -EEXIST;
2548 if (old_dir != new_dir)
2549 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002550
2551 cgrp = __d_cgrp(old_dentry);
2552
Tejun Heo6db8e852013-06-14 11:18:22 -07002553 /*
2554 * This isn't a proper migration and its usefulness is very
2555 * limited. Disallow if sane_behavior.
2556 */
2557 if (cgroup_sane_behavior(cgrp))
2558 return -EPERM;
2559
Li Zefan65dff752013-03-01 15:01:56 +08002560 name = cgroup_alloc_name(new_dentry);
2561 if (!name)
2562 return -ENOMEM;
2563
2564 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2565 if (ret) {
2566 kfree(name);
2567 return ret;
2568 }
2569
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002570 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002571 rcu_assign_pointer(cgrp->name, name);
2572
2573 kfree_rcu(old_name, rcu_head);
2574 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002575}
2576
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002577static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2578{
2579 if (S_ISDIR(dentry->d_inode->i_mode))
2580 return &__d_cgrp(dentry)->xattrs;
2581 else
Li Zefan712317a2013-04-18 23:09:52 -07002582 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002583}
2584
2585static inline int xattr_enabled(struct dentry *dentry)
2586{
2587 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002588 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002589}
2590
2591static bool is_valid_xattr(const char *name)
2592{
2593 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2594 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2595 return true;
2596 return false;
2597}
2598
2599static int cgroup_setxattr(struct dentry *dentry, const char *name,
2600 const void *val, size_t size, int flags)
2601{
2602 if (!xattr_enabled(dentry))
2603 return -EOPNOTSUPP;
2604 if (!is_valid_xattr(name))
2605 return -EINVAL;
2606 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2607}
2608
2609static int cgroup_removexattr(struct dentry *dentry, const char *name)
2610{
2611 if (!xattr_enabled(dentry))
2612 return -EOPNOTSUPP;
2613 if (!is_valid_xattr(name))
2614 return -EINVAL;
2615 return simple_xattr_remove(__d_xattrs(dentry), name);
2616}
2617
2618static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2619 void *buf, size_t size)
2620{
2621 if (!xattr_enabled(dentry))
2622 return -EOPNOTSUPP;
2623 if (!is_valid_xattr(name))
2624 return -EINVAL;
2625 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2626}
2627
2628static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2629{
2630 if (!xattr_enabled(dentry))
2631 return -EOPNOTSUPP;
2632 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2633}
2634
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002635static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002636 .read = cgroup_file_read,
2637 .write = cgroup_file_write,
2638 .llseek = generic_file_llseek,
2639 .open = cgroup_file_open,
2640 .release = cgroup_file_release,
2641};
2642
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002643static const struct inode_operations cgroup_file_inode_operations = {
2644 .setxattr = cgroup_setxattr,
2645 .getxattr = cgroup_getxattr,
2646 .listxattr = cgroup_listxattr,
2647 .removexattr = cgroup_removexattr,
2648};
2649
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002650static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002651 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 .mkdir = cgroup_mkdir,
2653 .rmdir = cgroup_rmdir,
2654 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002655 .setxattr = cgroup_setxattr,
2656 .getxattr = cgroup_getxattr,
2657 .listxattr = cgroup_listxattr,
2658 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002659};
2660
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002661/*
2662 * Check if a file is a control file
2663 */
2664static inline struct cftype *__file_cft(struct file *file)
2665{
Al Viro496ad9a2013-01-23 17:07:38 -05002666 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002667 return ERR_PTR(-EINVAL);
2668 return __d_cft(file->f_dentry);
2669}
2670
Al Viroa5e7ed32011-07-26 01:55:55 -04002671static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002672 struct super_block *sb)
2673{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 struct inode *inode;
2675
2676 if (!dentry)
2677 return -ENOENT;
2678 if (dentry->d_inode)
2679 return -EEXIST;
2680
2681 inode = cgroup_new_inode(mode, sb);
2682 if (!inode)
2683 return -ENOMEM;
2684
2685 if (S_ISDIR(mode)) {
2686 inode->i_op = &cgroup_dir_inode_operations;
2687 inode->i_fop = &simple_dir_operations;
2688
2689 /* start off with i_nlink == 2 (for "." entry) */
2690 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002691 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002692
Tejun Heob8a2df62012-11-19 08:13:37 -08002693 /*
2694 * Control reaches here with cgroup_mutex held.
2695 * @inode->i_mutex should nest outside cgroup_mutex but we
2696 * want to populate it immediately without releasing
2697 * cgroup_mutex. As @inode isn't visible to anyone else
2698 * yet, trylock will always succeed without affecting
2699 * lockdep checks.
2700 */
2701 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702 } else if (S_ISREG(mode)) {
2703 inode->i_size = 0;
2704 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002705 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 d_instantiate(dentry, inode);
2708 dget(dentry); /* Extra count - pin the dentry in core */
2709 return 0;
2710}
2711
Li Zefan099fca32009-04-02 16:57:29 -07002712/**
2713 * cgroup_file_mode - deduce file mode of a control file
2714 * @cft: the control file in question
2715 *
2716 * returns cft->mode if ->mode is not 0
2717 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2718 * returns S_IRUGO if it has only a read handler
2719 * returns S_IWUSR if it has only a write hander
2720 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002721static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002722{
Al Viroa5e7ed32011-07-26 01:55:55 -04002723 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002724
2725 if (cft->mode)
2726 return cft->mode;
2727
2728 if (cft->read || cft->read_u64 || cft->read_s64 ||
2729 cft->read_map || cft->read_seq_string)
2730 mode |= S_IRUGO;
2731
2732 if (cft->write || cft->write_u64 || cft->write_s64 ||
2733 cft->write_string || cft->trigger)
2734 mode |= S_IWUSR;
2735
2736 return mode;
2737}
2738
Tejun Heodb0416b2012-04-01 12:09:55 -07002739static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002740 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741{
Paul Menagebd89aab2007-10-18 23:40:44 -07002742 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002743 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002747 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002749
Tejun Heo93438622013-04-14 20:15:25 -07002750 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002751 strcpy(name, subsys->name);
2752 strcat(name, ".");
2753 }
2754 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002755
Paul Menageddbcc7e2007-10-18 23:39:30 -07002756 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002757
2758 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2759 if (!cfe)
2760 return -ENOMEM;
2761
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002763 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002765 goto out;
2766 }
2767
Li Zefand6cbf352013-05-14 19:44:20 +08002768 cfe->type = (void *)cft;
2769 cfe->dentry = dentry;
2770 dentry->d_fsdata = cfe;
2771 simple_xattrs_init(&cfe->xattrs);
2772
Tejun Heo05ef1d72012-04-01 12:09:56 -07002773 mode = cgroup_file_mode(cft);
2774 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2775 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002776 list_add_tail(&cfe->node, &parent->files);
2777 cfe = NULL;
2778 }
2779 dput(dentry);
2780out:
2781 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002782 return error;
2783}
2784
Tejun Heo79578622012-04-01 12:09:56 -07002785static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002786 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002788 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002789 int err, ret = 0;
2790
2791 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002792 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002793 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2794 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002795 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2796 continue;
2797 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2798 continue;
2799
Li Zefan2739d3c2013-01-21 18:18:33 +08002800 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002801 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002802 if (err)
2803 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2804 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002805 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002806 } else {
2807 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002808 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002809 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002810 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002811}
2812
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002814 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002815{
2816 /*
2817 * Thanks to the entanglement with vfs inode locking, we can't walk
2818 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002819 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2820 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002821 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002822 mutex_lock(&cgroup_mutex);
2823}
2824
2825static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002826 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002827 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002828{
2829 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002830 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002831 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002832 struct dentry *prev = NULL;
2833 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002834 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835
2836 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002837 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002838 !atomic_inc_not_zero(&sb->s_active)) {
2839 mutex_unlock(&cgroup_mutex);
2840 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002841 }
2842
Li Zefane8c82d22013-06-18 18:48:37 +08002843 /*
2844 * All cgroups which are created after we drop cgroup_mutex will
2845 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002846 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002847 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002848 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002849
Tejun Heo8e3f6542012-04-01 12:09:55 -07002850 mutex_unlock(&cgroup_mutex);
2851
Li Zefane8c82d22013-06-18 18:48:37 +08002852 /* @root always needs to be updated */
2853 inode = root->dentry->d_inode;
2854 mutex_lock(&inode->i_mutex);
2855 mutex_lock(&cgroup_mutex);
2856 cgroup_addrm_files(root, ss, cfts, is_add);
2857 mutex_unlock(&cgroup_mutex);
2858 mutex_unlock(&inode->i_mutex);
2859
2860 /* add/rm files for all cgroups created before */
2861 rcu_read_lock();
2862 cgroup_for_each_descendant_pre(cgrp, root) {
2863 if (cgroup_is_dead(cgrp))
2864 continue;
2865
2866 inode = cgrp->dentry->d_inode;
2867 dget(cgrp->dentry);
2868 rcu_read_unlock();
2869
2870 dput(prev);
2871 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002872
2873 mutex_lock(&inode->i_mutex);
2874 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002875 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002876 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002877 mutex_unlock(&cgroup_mutex);
2878 mutex_unlock(&inode->i_mutex);
2879
Li Zefane8c82d22013-06-18 18:48:37 +08002880 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881 }
Li Zefane8c82d22013-06-18 18:48:37 +08002882 rcu_read_unlock();
2883 dput(prev);
2884 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885}
2886
2887/**
2888 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2889 * @ss: target cgroup subsystem
2890 * @cfts: zero-length name terminated array of cftypes
2891 *
2892 * Register @cfts to @ss. Files described by @cfts are created for all
2893 * existing cgroups to which @ss is attached and all future cgroups will
2894 * have them too. This function can be called anytime whether @ss is
2895 * attached or not.
2896 *
2897 * Returns 0 on successful registration, -errno on failure. Note that this
2898 * function currently returns 0 as long as @cfts registration is successful
2899 * even if some file creation attempts on existing cgroups fail.
2900 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002901int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002902{
2903 struct cftype_set *set;
2904
2905 set = kzalloc(sizeof(*set), GFP_KERNEL);
2906 if (!set)
2907 return -ENOMEM;
2908
2909 cgroup_cfts_prepare();
2910 set->cfts = cfts;
2911 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002912 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002913
2914 return 0;
2915}
2916EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2917
Li Zefana043e3b2008-02-23 15:24:09 -08002918/**
Tejun Heo79578622012-04-01 12:09:56 -07002919 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2920 * @ss: target cgroup subsystem
2921 * @cfts: zero-length name terminated array of cftypes
2922 *
2923 * Unregister @cfts from @ss. Files described by @cfts are removed from
2924 * all existing cgroups to which @ss is attached and all future cgroups
2925 * won't have them either. This function can be called anytime whether @ss
2926 * is attached or not.
2927 *
2928 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2929 * registered with @ss.
2930 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002931int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002932{
2933 struct cftype_set *set;
2934
2935 cgroup_cfts_prepare();
2936
2937 list_for_each_entry(set, &ss->cftsets, node) {
2938 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002939 list_del(&set->node);
2940 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002941 cgroup_cfts_commit(ss, cfts, false);
2942 return 0;
2943 }
2944 }
2945
2946 cgroup_cfts_commit(ss, NULL, false);
2947 return -ENOENT;
2948}
2949
2950/**
Li Zefana043e3b2008-02-23 15:24:09 -08002951 * cgroup_task_count - count the number of tasks in a cgroup.
2952 * @cgrp: the cgroup in question
2953 *
2954 * Return the number of tasks in the cgroup.
2955 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002956int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002957{
2958 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002959 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002960
Paul Menage817929e2007-10-18 23:39:36 -07002961 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002962 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2963 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002964 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002965 return count;
2966}
2967
2968/*
Paul Menage817929e2007-10-18 23:39:36 -07002969 * Advance a list_head iterator. The iterator should be positioned at
2970 * the start of a css_set
2971 */
Tejun Heo69d02062013-06-12 21:04:50 -07002972static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002973{
Tejun Heo69d02062013-06-12 21:04:50 -07002974 struct list_head *l = it->cset_link;
2975 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002976 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002977
2978 /* Advance to the next non-empty css_set */
2979 do {
2980 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002981 if (l == &cgrp->cset_links) {
2982 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002983 return;
2984 }
Tejun Heo69d02062013-06-12 21:04:50 -07002985 link = list_entry(l, struct cgrp_cset_link, cset_link);
2986 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002987 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002988 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002989 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002990}
2991
Cliff Wickman31a7df02008-02-07 00:14:42 -08002992/*
2993 * To reduce the fork() overhead for systems that are not actually
2994 * using their cgroups capability, we don't maintain the lists running
2995 * through each css_set to its tasks until we see the list actually
2996 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002997 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002998static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002999{
3000 struct task_struct *p, *g;
3001 write_lock(&css_set_lock);
3002 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003003 /*
3004 * We need tasklist_lock because RCU is not safe against
3005 * while_each_thread(). Besides, a forking task that has passed
3006 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3007 * is not guaranteed to have its child immediately visible in the
3008 * tasklist if we walk through it with RCU.
3009 */
3010 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003011 do_each_thread(g, p) {
3012 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003013 /*
3014 * We should check if the process is exiting, otherwise
3015 * it will race with cgroup_exit() in that the list
3016 * entry won't be deleted though the process has exited.
3017 */
3018 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003019 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003020 task_unlock(p);
3021 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003022 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003023 write_unlock(&css_set_lock);
3024}
3025
Tejun Heo574bd9f2012-11-09 09:12:29 -08003026/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003027 * cgroup_next_sibling - find the next sibling of a given cgroup
3028 * @pos: the current cgroup
3029 *
3030 * This function returns the next sibling of @pos and should be called
3031 * under RCU read lock. The only requirement is that @pos is accessible.
3032 * The next sibling is guaranteed to be returned regardless of @pos's
3033 * state.
3034 */
3035struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3036{
3037 struct cgroup *next;
3038
3039 WARN_ON_ONCE(!rcu_read_lock_held());
3040
3041 /*
3042 * @pos could already have been removed. Once a cgroup is removed,
3043 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003044 * changes. As CGRP_DEAD assertion is serialized and happens
3045 * before the cgroup is taken off the ->sibling list, if we see it
3046 * unasserted, it's guaranteed that the next sibling hasn't
3047 * finished its grace period even if it's already removed, and thus
3048 * safe to dereference from this RCU critical section. If
3049 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3050 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003051 */
Tejun Heo54766d42013-06-12 21:04:53 -07003052 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003053 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3054 if (&next->sibling != &pos->parent->children)
3055 return next;
3056 return NULL;
3057 }
3058
3059 /*
3060 * Can't dereference the next pointer. Each cgroup is given a
3061 * monotonically increasing unique serial number and always
3062 * appended to the sibling list, so the next one can be found by
3063 * walking the parent's children until we see a cgroup with higher
3064 * serial number than @pos's.
3065 *
3066 * While this path can be slow, it's taken only when either the
3067 * current cgroup is removed or iteration and removal race.
3068 */
3069 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3070 if (next->serial_nr > pos->serial_nr)
3071 return next;
3072 return NULL;
3073}
3074EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3075
3076/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3078 * @pos: the current position (%NULL to initiate traversal)
3079 * @cgroup: cgroup whose descendants to walk
3080 *
3081 * To be used by cgroup_for_each_descendant_pre(). Find the next
3082 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003083 *
3084 * While this function requires RCU read locking, it doesn't require the
3085 * whole traversal to be contained in a single RCU critical section. This
3086 * function will return the correct next descendant as long as both @pos
3087 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088 */
3089struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3090 struct cgroup *cgroup)
3091{
3092 struct cgroup *next;
3093
3094 WARN_ON_ONCE(!rcu_read_lock_held());
3095
3096 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003097 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003098 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099
3100 /* visit the first child if exists */
3101 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3102 if (next)
3103 return next;
3104
3105 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003106 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003107 next = cgroup_next_sibling(pos);
3108 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003110 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003111 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112
3113 return NULL;
3114}
3115EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3116
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003117/**
3118 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3119 * @pos: cgroup of interest
3120 *
3121 * Return the rightmost descendant of @pos. If there's no descendant,
3122 * @pos is returned. This can be used during pre-order traversal to skip
3123 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003124 *
3125 * While this function requires RCU read locking, it doesn't require the
3126 * whole traversal to be contained in a single RCU critical section. This
3127 * function will return the correct rightmost descendant as long as @pos is
3128 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003129 */
3130struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3131{
3132 struct cgroup *last, *tmp;
3133
3134 WARN_ON_ONCE(!rcu_read_lock_held());
3135
3136 do {
3137 last = pos;
3138 /* ->prev isn't RCU safe, walk ->next till the end */
3139 pos = NULL;
3140 list_for_each_entry_rcu(tmp, &last->children, sibling)
3141 pos = tmp;
3142 } while (pos);
3143
3144 return last;
3145}
3146EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3147
Tejun Heo574bd9f2012-11-09 09:12:29 -08003148static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3149{
3150 struct cgroup *last;
3151
3152 do {
3153 last = pos;
3154 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3155 sibling);
3156 } while (pos);
3157
3158 return last;
3159}
3160
3161/**
3162 * cgroup_next_descendant_post - find the next descendant for post-order walk
3163 * @pos: the current position (%NULL to initiate traversal)
3164 * @cgroup: cgroup whose descendants to walk
3165 *
3166 * To be used by cgroup_for_each_descendant_post(). Find the next
3167 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003168 *
3169 * While this function requires RCU read locking, it doesn't require the
3170 * whole traversal to be contained in a single RCU critical section. This
3171 * function will return the correct next descendant as long as both @pos
3172 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003173 */
3174struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3175 struct cgroup *cgroup)
3176{
3177 struct cgroup *next;
3178
3179 WARN_ON_ONCE(!rcu_read_lock_held());
3180
3181 /* if first iteration, visit the leftmost descendant */
3182 if (!pos) {
3183 next = cgroup_leftmost_descendant(cgroup);
3184 return next != cgroup ? next : NULL;
3185 }
3186
3187 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003188 next = cgroup_next_sibling(pos);
3189 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190 return cgroup_leftmost_descendant(next);
3191
3192 /* no sibling left, visit parent */
3193 next = pos->parent;
3194 return next != cgroup ? next : NULL;
3195}
3196EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3197
Paul Menagebd89aab2007-10-18 23:40:44 -07003198void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003199 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003200{
3201 /*
3202 * The first time anyone tries to iterate across a cgroup,
3203 * we need to enable the list linking each css_set to its
3204 * tasks, and fix up all existing tasks.
3205 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003206 if (!use_task_css_set_links)
3207 cgroup_enable_task_cg_lists();
3208
Paul Menage817929e2007-10-18 23:39:36 -07003209 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003210 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003211 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003212}
3213
Paul Menagebd89aab2007-10-18 23:40:44 -07003214struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003215 struct cgroup_iter *it)
3216{
3217 struct task_struct *res;
3218 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003219 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003220
3221 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003222 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003223 return NULL;
3224 res = list_entry(l, struct task_struct, cg_list);
3225 /* Advance iterator to find next entry */
3226 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003227 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3228 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003229 /* We reached the end of this task list - move on to
3230 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003231 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003232 } else {
3233 it->task = l;
3234 }
3235 return res;
3236}
3237
Paul Menagebd89aab2007-10-18 23:40:44 -07003238void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003239 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003240{
3241 read_unlock(&css_set_lock);
3242}
3243
Cliff Wickman31a7df02008-02-07 00:14:42 -08003244static inline int started_after_time(struct task_struct *t1,
3245 struct timespec *time,
3246 struct task_struct *t2)
3247{
3248 int start_diff = timespec_compare(&t1->start_time, time);
3249 if (start_diff > 0) {
3250 return 1;
3251 } else if (start_diff < 0) {
3252 return 0;
3253 } else {
3254 /*
3255 * Arbitrarily, if two processes started at the same
3256 * time, we'll say that the lower pointer value
3257 * started first. Note that t2 may have exited by now
3258 * so this may not be a valid pointer any longer, but
3259 * that's fine - it still serves to distinguish
3260 * between two tasks started (effectively) simultaneously.
3261 */
3262 return t1 > t2;
3263 }
3264}
3265
3266/*
3267 * This function is a callback from heap_insert() and is used to order
3268 * the heap.
3269 * In this case we order the heap in descending task start time.
3270 */
3271static inline int started_after(void *p1, void *p2)
3272{
3273 struct task_struct *t1 = p1;
3274 struct task_struct *t2 = p2;
3275 return started_after_time(t1, &t2->start_time, t2);
3276}
3277
3278/**
3279 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3280 * @scan: struct cgroup_scanner containing arguments for the scan
3281 *
3282 * Arguments include pointers to callback functions test_task() and
3283 * process_task().
3284 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3285 * and if it returns true, call process_task() for it also.
3286 * The test_task pointer may be NULL, meaning always true (select all tasks).
3287 * Effectively duplicates cgroup_iter_{start,next,end}()
3288 * but does not lock css_set_lock for the call to process_task().
3289 * The struct cgroup_scanner may be embedded in any structure of the caller's
3290 * creation.
3291 * It is guaranteed that process_task() will act on every task that
3292 * is a member of the cgroup for the duration of this call. This
3293 * function may or may not call process_task() for tasks that exit
3294 * or move to a different cgroup during the call, or are forked or
3295 * move into the cgroup during the call.
3296 *
3297 * Note that test_task() may be called with locks held, and may in some
3298 * situations be called multiple times for the same task, so it should
3299 * be cheap.
3300 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3301 * pre-allocated and will be used for heap operations (and its "gt" member will
3302 * be overwritten), else a temporary heap will be used (allocation of which
3303 * may cause this function to fail).
3304 */
3305int cgroup_scan_tasks(struct cgroup_scanner *scan)
3306{
3307 int retval, i;
3308 struct cgroup_iter it;
3309 struct task_struct *p, *dropped;
3310 /* Never dereference latest_task, since it's not refcounted */
3311 struct task_struct *latest_task = NULL;
3312 struct ptr_heap tmp_heap;
3313 struct ptr_heap *heap;
3314 struct timespec latest_time = { 0, 0 };
3315
3316 if (scan->heap) {
3317 /* The caller supplied our heap and pre-allocated its memory */
3318 heap = scan->heap;
3319 heap->gt = &started_after;
3320 } else {
3321 /* We need to allocate our own heap memory */
3322 heap = &tmp_heap;
3323 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3324 if (retval)
3325 /* cannot allocate the heap */
3326 return retval;
3327 }
3328
3329 again:
3330 /*
3331 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3332 * to determine which are of interest, and using the scanner's
3333 * "process_task" callback to process any of them that need an update.
3334 * Since we don't want to hold any locks during the task updates,
3335 * gather tasks to be processed in a heap structure.
3336 * The heap is sorted by descending task start time.
3337 * If the statically-sized heap fills up, we overflow tasks that
3338 * started later, and in future iterations only consider tasks that
3339 * started after the latest task in the previous pass. This
3340 * guarantees forward progress and that we don't miss any tasks.
3341 */
3342 heap->size = 0;
3343 cgroup_iter_start(scan->cg, &it);
3344 while ((p = cgroup_iter_next(scan->cg, &it))) {
3345 /*
3346 * Only affect tasks that qualify per the caller's callback,
3347 * if he provided one
3348 */
3349 if (scan->test_task && !scan->test_task(p, scan))
3350 continue;
3351 /*
3352 * Only process tasks that started after the last task
3353 * we processed
3354 */
3355 if (!started_after_time(p, &latest_time, latest_task))
3356 continue;
3357 dropped = heap_insert(heap, p);
3358 if (dropped == NULL) {
3359 /*
3360 * The new task was inserted; the heap wasn't
3361 * previously full
3362 */
3363 get_task_struct(p);
3364 } else if (dropped != p) {
3365 /*
3366 * The new task was inserted, and pushed out a
3367 * different task
3368 */
3369 get_task_struct(p);
3370 put_task_struct(dropped);
3371 }
3372 /*
3373 * Else the new task was newer than anything already in
3374 * the heap and wasn't inserted
3375 */
3376 }
3377 cgroup_iter_end(scan->cg, &it);
3378
3379 if (heap->size) {
3380 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003381 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003382 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003383 latest_time = q->start_time;
3384 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003385 }
3386 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003387 scan->process_task(q, scan);
3388 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003389 }
3390 /*
3391 * If we had to process any tasks at all, scan again
3392 * in case some of them were in the middle of forking
3393 * children that didn't get processed.
3394 * Not the most efficient way to do it, but it avoids
3395 * having to take callback_mutex in the fork path
3396 */
3397 goto again;
3398 }
3399 if (heap == &tmp_heap)
3400 heap_free(&tmp_heap);
3401 return 0;
3402}
3403
Tejun Heo8cc99342013-04-07 09:29:50 -07003404static void cgroup_transfer_one_task(struct task_struct *task,
3405 struct cgroup_scanner *scan)
3406{
3407 struct cgroup *new_cgroup = scan->data;
3408
Tejun Heo47cfcd02013-04-07 09:29:51 -07003409 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003410 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003411 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003412}
3413
3414/**
3415 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3416 * @to: cgroup to which the tasks will be moved
3417 * @from: cgroup in which the tasks currently reside
3418 */
3419int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3420{
3421 struct cgroup_scanner scan;
3422
3423 scan.cg = from;
3424 scan.test_task = NULL; /* select all tasks in cgroup */
3425 scan.process_task = cgroup_transfer_one_task;
3426 scan.heap = NULL;
3427 scan.data = to;
3428
3429 return cgroup_scan_tasks(&scan);
3430}
3431
Paul Menage817929e2007-10-18 23:39:36 -07003432/*
Ben Blum102a7752009-09-23 15:56:26 -07003433 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003434 *
3435 * Reading this file can return large amounts of data if a cgroup has
3436 * *lots* of attached tasks. So it may need several calls to read(),
3437 * but we cannot guarantee that the information we produce is correct
3438 * unless we produce it entirely atomically.
3439 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003440 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003441
Li Zefan24528252012-01-20 11:58:43 +08003442/* which pidlist file are we talking about? */
3443enum cgroup_filetype {
3444 CGROUP_FILE_PROCS,
3445 CGROUP_FILE_TASKS,
3446};
3447
3448/*
3449 * A pidlist is a list of pids that virtually represents the contents of one
3450 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3451 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3452 * to the cgroup.
3453 */
3454struct cgroup_pidlist {
3455 /*
3456 * used to find which pidlist is wanted. doesn't change as long as
3457 * this particular list stays in the list.
3458 */
3459 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3460 /* array of xids */
3461 pid_t *list;
3462 /* how many elements the above list has */
3463 int length;
3464 /* how many files are using the current array */
3465 int use_count;
3466 /* each of these stored in a list by its cgroup */
3467 struct list_head links;
3468 /* pointer to the cgroup we belong to, for list removal purposes */
3469 struct cgroup *owner;
3470 /* protects the other fields */
3471 struct rw_semaphore mutex;
3472};
3473
Paul Menagebbcb81d2007-10-18 23:39:32 -07003474/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003475 * The following two functions "fix" the issue where there are more pids
3476 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3477 * TODO: replace with a kernel-wide solution to this problem
3478 */
3479#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3480static void *pidlist_allocate(int count)
3481{
3482 if (PIDLIST_TOO_LARGE(count))
3483 return vmalloc(count * sizeof(pid_t));
3484 else
3485 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3486}
3487static void pidlist_free(void *p)
3488{
3489 if (is_vmalloc_addr(p))
3490 vfree(p);
3491 else
3492 kfree(p);
3493}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003494
3495/*
Ben Blum102a7752009-09-23 15:56:26 -07003496 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003497 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003498 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003499static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003500{
Ben Blum102a7752009-09-23 15:56:26 -07003501 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003502
3503 /*
3504 * we presume the 0th element is unique, so i starts at 1. trivial
3505 * edge cases first; no work needs to be done for either
3506 */
3507 if (length == 0 || length == 1)
3508 return length;
3509 /* src and dest walk down the list; dest counts unique elements */
3510 for (src = 1; src < length; src++) {
3511 /* find next unique element */
3512 while (list[src] == list[src-1]) {
3513 src++;
3514 if (src == length)
3515 goto after;
3516 }
3517 /* dest always points to where the next unique element goes */
3518 list[dest] = list[src];
3519 dest++;
3520 }
3521after:
Ben Blum102a7752009-09-23 15:56:26 -07003522 return dest;
3523}
3524
3525static int cmppid(const void *a, const void *b)
3526{
3527 return *(pid_t *)a - *(pid_t *)b;
3528}
3529
3530/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003531 * find the appropriate pidlist for our purpose (given procs vs tasks)
3532 * returns with the lock on that pidlist already held, and takes care
3533 * of the use count, or returns NULL with no locks held if we're out of
3534 * memory.
3535 */
3536static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3537 enum cgroup_filetype type)
3538{
3539 struct cgroup_pidlist *l;
3540 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003541 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003542
Ben Blum72a8cb32009-09-23 15:56:27 -07003543 /*
3544 * We can't drop the pidlist_mutex before taking the l->mutex in case
3545 * the last ref-holder is trying to remove l from the list at the same
3546 * time. Holding the pidlist_mutex precludes somebody taking whichever
3547 * list we find out from under us - compare release_pid_array().
3548 */
3549 mutex_lock(&cgrp->pidlist_mutex);
3550 list_for_each_entry(l, &cgrp->pidlists, links) {
3551 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003552 /* make sure l doesn't vanish out from under us */
3553 down_write(&l->mutex);
3554 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003555 return l;
3556 }
3557 }
3558 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003559 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003560 if (!l) {
3561 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003562 return l;
3563 }
3564 init_rwsem(&l->mutex);
3565 down_write(&l->mutex);
3566 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003567 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003568 l->owner = cgrp;
3569 list_add(&l->links, &cgrp->pidlists);
3570 mutex_unlock(&cgrp->pidlist_mutex);
3571 return l;
3572}
3573
3574/*
Ben Blum102a7752009-09-23 15:56:26 -07003575 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3576 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003577static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3578 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003579{
3580 pid_t *array;
3581 int length;
3582 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003583 struct cgroup_iter it;
3584 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003585 struct cgroup_pidlist *l;
3586
3587 /*
3588 * If cgroup gets more users after we read count, we won't have
3589 * enough space - tough. This race is indistinguishable to the
3590 * caller from the case that the additional cgroup users didn't
3591 * show up until sometime later on.
3592 */
3593 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003594 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003595 if (!array)
3596 return -ENOMEM;
3597 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003598 cgroup_iter_start(cgrp, &it);
3599 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003600 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003601 break;
Ben Blum102a7752009-09-23 15:56:26 -07003602 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003603 if (type == CGROUP_FILE_PROCS)
3604 pid = task_tgid_vnr(tsk);
3605 else
3606 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003607 if (pid > 0) /* make sure to only use valid results */
3608 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003609 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003610 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003611 length = n;
3612 /* now sort & (if procs) strip out duplicates */
3613 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003614 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003615 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003616 l = cgroup_pidlist_find(cgrp, type);
3617 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003618 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003620 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003621 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003622 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003623 l->list = array;
3624 l->length = length;
3625 l->use_count++;
3626 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003627 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003628 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003629}
3630
Balbir Singh846c7bb2007-10-18 23:39:44 -07003631/**
Li Zefana043e3b2008-02-23 15:24:09 -08003632 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003633 * @stats: cgroupstats to fill information into
3634 * @dentry: A dentry entry belonging to the cgroup for which stats have
3635 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003636 *
3637 * Build and fill cgroupstats so that taskstats can export it to user
3638 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003639 */
3640int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3641{
3642 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003643 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003644 struct cgroup_iter it;
3645 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003646
Balbir Singh846c7bb2007-10-18 23:39:44 -07003647 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003648 * Validate dentry by checking the superblock operations,
3649 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003650 */
Li Zefan33d283b2008-11-19 15:36:48 -08003651 if (dentry->d_sb->s_op != &cgroup_ops ||
3652 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653 goto err;
3654
3655 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003656 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003657
Paul Menagebd89aab2007-10-18 23:40:44 -07003658 cgroup_iter_start(cgrp, &it);
3659 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003660 switch (tsk->state) {
3661 case TASK_RUNNING:
3662 stats->nr_running++;
3663 break;
3664 case TASK_INTERRUPTIBLE:
3665 stats->nr_sleeping++;
3666 break;
3667 case TASK_UNINTERRUPTIBLE:
3668 stats->nr_uninterruptible++;
3669 break;
3670 case TASK_STOPPED:
3671 stats->nr_stopped++;
3672 break;
3673 default:
3674 if (delayacct_is_task_waiting_on_io(tsk))
3675 stats->nr_io_wait++;
3676 break;
3677 }
3678 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003679 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680
Balbir Singh846c7bb2007-10-18 23:39:44 -07003681err:
3682 return ret;
3683}
3684
Paul Menage8f3ff202009-09-23 15:56:25 -07003685
Paul Menagecc31edc2008-10-18 20:28:04 -07003686/*
Ben Blum102a7752009-09-23 15:56:26 -07003687 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003689 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003690 */
3691
Ben Blum102a7752009-09-23 15:56:26 -07003692static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003693{
3694 /*
3695 * Initially we receive a position value that corresponds to
3696 * one more than the last pid shown (or 0 on the first call or
3697 * after a seek to the start). Use a binary-search to find the
3698 * next pid to display, if any
3699 */
Ben Blum102a7752009-09-23 15:56:26 -07003700 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003701 int index = 0, pid = *pos;
3702 int *iter;
3703
Ben Blum102a7752009-09-23 15:56:26 -07003704 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003705 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003706 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003707
Paul Menagecc31edc2008-10-18 20:28:04 -07003708 while (index < end) {
3709 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003710 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 index = mid;
3712 break;
Ben Blum102a7752009-09-23 15:56:26 -07003713 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003714 index = mid + 1;
3715 else
3716 end = mid;
3717 }
3718 }
3719 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003720 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003721 return NULL;
3722 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003723 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003724 *pos = *iter;
3725 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003726}
3727
Ben Blum102a7752009-09-23 15:56:26 -07003728static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003729{
Ben Blum102a7752009-09-23 15:56:26 -07003730 struct cgroup_pidlist *l = s->private;
3731 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003732}
3733
Ben Blum102a7752009-09-23 15:56:26 -07003734static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003735{
Ben Blum102a7752009-09-23 15:56:26 -07003736 struct cgroup_pidlist *l = s->private;
3737 pid_t *p = v;
3738 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003739 /*
3740 * Advance to the next pid in the array. If this goes off the
3741 * end, we're done
3742 */
3743 p++;
3744 if (p >= end) {
3745 return NULL;
3746 } else {
3747 *pos = *p;
3748 return p;
3749 }
3750}
3751
Ben Blum102a7752009-09-23 15:56:26 -07003752static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003753{
3754 return seq_printf(s, "%d\n", *(int *)v);
3755}
3756
Ben Blum102a7752009-09-23 15:56:26 -07003757/*
3758 * seq_operations functions for iterating on pidlists through seq_file -
3759 * independent of whether it's tasks or procs
3760 */
3761static const struct seq_operations cgroup_pidlist_seq_operations = {
3762 .start = cgroup_pidlist_start,
3763 .stop = cgroup_pidlist_stop,
3764 .next = cgroup_pidlist_next,
3765 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003766};
3767
Ben Blum102a7752009-09-23 15:56:26 -07003768static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003769{
Ben Blum72a8cb32009-09-23 15:56:27 -07003770 /*
3771 * the case where we're the last user of this particular pidlist will
3772 * have us remove it from the cgroup's list, which entails taking the
3773 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3774 * pidlist_mutex, we have to take pidlist_mutex first.
3775 */
3776 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003777 down_write(&l->mutex);
3778 BUG_ON(!l->use_count);
3779 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003780 /* we're the last user if refcount is 0; remove and free */
3781 list_del(&l->links);
3782 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003783 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003784 put_pid_ns(l->key.ns);
3785 up_write(&l->mutex);
3786 kfree(l);
3787 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003788 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003789 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003790 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003791}
3792
Ben Blum102a7752009-09-23 15:56:26 -07003793static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003794{
Ben Blum102a7752009-09-23 15:56:26 -07003795 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003796 if (!(file->f_mode & FMODE_READ))
3797 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003798 /*
3799 * the seq_file will only be initialized if the file was opened for
3800 * reading; hence we check if it's not null only in that case.
3801 */
3802 l = ((struct seq_file *)file->private_data)->private;
3803 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003804 return seq_release(inode, file);
3805}
3806
Ben Blum102a7752009-09-23 15:56:26 -07003807static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 .read = seq_read,
3809 .llseek = seq_lseek,
3810 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003811 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003812};
3813
3814/*
Ben Blum102a7752009-09-23 15:56:26 -07003815 * The following functions handle opens on a file that displays a pidlist
3816 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3817 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 */
Ben Blum102a7752009-09-23 15:56:26 -07003819/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003820static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003821{
3822 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003823 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003824 int retval;
3825
3826 /* Nothing to do for write-only files */
3827 if (!(file->f_mode & FMODE_READ))
3828 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003829
Ben Blum102a7752009-09-23 15:56:26 -07003830 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003831 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003832 if (retval)
3833 return retval;
3834 /* configure file information */
3835 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003836
Ben Blum102a7752009-09-23 15:56:26 -07003837 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003839 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003840 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003841 }
Ben Blum102a7752009-09-23 15:56:26 -07003842 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003843 return 0;
3844}
Ben Blum102a7752009-09-23 15:56:26 -07003845static int cgroup_tasks_open(struct inode *unused, struct file *file)
3846{
Ben Blum72a8cb32009-09-23 15:56:27 -07003847 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003848}
3849static int cgroup_procs_open(struct inode *unused, struct file *file)
3850{
Ben Blum72a8cb32009-09-23 15:56:27 -07003851 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003852}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003853
Paul Menagebd89aab2007-10-18 23:40:44 -07003854static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003855 struct cftype *cft)
3856{
Paul Menagebd89aab2007-10-18 23:40:44 -07003857 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003858}
3859
Paul Menage6379c102008-07-25 01:47:01 -07003860static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3861 struct cftype *cft,
3862 u64 val)
3863{
3864 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3865 if (val)
3866 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3867 else
3868 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3869 return 0;
3870}
3871
Paul Menagebbcb81d2007-10-18 23:39:32 -07003872/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003873 * When dput() is called asynchronously, if umount has been done and
3874 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3875 * there's a small window that vfs will see the root dentry with non-zero
3876 * refcnt and trigger BUG().
3877 *
3878 * That's why we hold a reference before dput() and drop it right after.
3879 */
3880static void cgroup_dput(struct cgroup *cgrp)
3881{
3882 struct super_block *sb = cgrp->root->sb;
3883
3884 atomic_inc(&sb->s_active);
3885 dput(cgrp->dentry);
3886 deactivate_super(sb);
3887}
3888
3889/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003890 * Unregister event and free resources.
3891 *
3892 * Gets called from workqueue.
3893 */
3894static void cgroup_event_remove(struct work_struct *work)
3895{
3896 struct cgroup_event *event = container_of(work, struct cgroup_event,
3897 remove);
3898 struct cgroup *cgrp = event->cgrp;
3899
Li Zefan810cbee2013-02-18 18:56:14 +08003900 remove_wait_queue(event->wqh, &event->wait);
3901
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003902 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3903
Li Zefan810cbee2013-02-18 18:56:14 +08003904 /* Notify userspace the event is going away. */
3905 eventfd_signal(event->eventfd, 1);
3906
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003907 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003908 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003909 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003910}
3911
3912/*
3913 * Gets called on POLLHUP on eventfd when user closes it.
3914 *
3915 * Called with wqh->lock held and interrupts disabled.
3916 */
3917static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3918 int sync, void *key)
3919{
3920 struct cgroup_event *event = container_of(wait,
3921 struct cgroup_event, wait);
3922 struct cgroup *cgrp = event->cgrp;
3923 unsigned long flags = (unsigned long)key;
3924
3925 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003926 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003927 * If the event has been detached at cgroup removal, we
3928 * can simply return knowing the other side will cleanup
3929 * for us.
3930 *
3931 * We can't race against event freeing since the other
3932 * side will require wqh->lock via remove_wait_queue(),
3933 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003934 */
Li Zefan810cbee2013-02-18 18:56:14 +08003935 spin_lock(&cgrp->event_list_lock);
3936 if (!list_empty(&event->list)) {
3937 list_del_init(&event->list);
3938 /*
3939 * We are in atomic context, but cgroup_event_remove()
3940 * may sleep, so we have to call it in workqueue.
3941 */
3942 schedule_work(&event->remove);
3943 }
3944 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003945 }
3946
3947 return 0;
3948}
3949
3950static void cgroup_event_ptable_queue_proc(struct file *file,
3951 wait_queue_head_t *wqh, poll_table *pt)
3952{
3953 struct cgroup_event *event = container_of(pt,
3954 struct cgroup_event, pt);
3955
3956 event->wqh = wqh;
3957 add_wait_queue(wqh, &event->wait);
3958}
3959
3960/*
3961 * Parse input and register new cgroup event handler.
3962 *
3963 * Input must be in format '<event_fd> <control_fd> <args>'.
3964 * Interpretation of args is defined by control file implementation.
3965 */
3966static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3967 const char *buffer)
3968{
3969 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003970 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003971 unsigned int efd, cfd;
3972 struct file *efile = NULL;
3973 struct file *cfile = NULL;
3974 char *endp;
3975 int ret;
3976
3977 efd = simple_strtoul(buffer, &endp, 10);
3978 if (*endp != ' ')
3979 return -EINVAL;
3980 buffer = endp + 1;
3981
3982 cfd = simple_strtoul(buffer, &endp, 10);
3983 if ((*endp != ' ') && (*endp != '\0'))
3984 return -EINVAL;
3985 buffer = endp + 1;
3986
3987 event = kzalloc(sizeof(*event), GFP_KERNEL);
3988 if (!event)
3989 return -ENOMEM;
3990 event->cgrp = cgrp;
3991 INIT_LIST_HEAD(&event->list);
3992 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3993 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3994 INIT_WORK(&event->remove, cgroup_event_remove);
3995
3996 efile = eventfd_fget(efd);
3997 if (IS_ERR(efile)) {
3998 ret = PTR_ERR(efile);
3999 goto fail;
4000 }
4001
4002 event->eventfd = eventfd_ctx_fileget(efile);
4003 if (IS_ERR(event->eventfd)) {
4004 ret = PTR_ERR(event->eventfd);
4005 goto fail;
4006 }
4007
4008 cfile = fget(cfd);
4009 if (!cfile) {
4010 ret = -EBADF;
4011 goto fail;
4012 }
4013
4014 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004015 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004016 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004017 if (ret < 0)
4018 goto fail;
4019
4020 event->cft = __file_cft(cfile);
4021 if (IS_ERR(event->cft)) {
4022 ret = PTR_ERR(event->cft);
4023 goto fail;
4024 }
4025
Li Zefanf1690072013-02-18 14:13:35 +08004026 /*
4027 * The file to be monitored must be in the same cgroup as
4028 * cgroup.event_control is.
4029 */
4030 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4031 if (cgrp_cfile != cgrp) {
4032 ret = -EINVAL;
4033 goto fail;
4034 }
4035
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004036 if (!event->cft->register_event || !event->cft->unregister_event) {
4037 ret = -EINVAL;
4038 goto fail;
4039 }
4040
4041 ret = event->cft->register_event(cgrp, event->cft,
4042 event->eventfd, buffer);
4043 if (ret)
4044 goto fail;
4045
Li Zefan7ef70e42013-04-26 11:58:03 -07004046 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004047
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004048 /*
4049 * Events should be removed after rmdir of cgroup directory, but before
4050 * destroying subsystem state objects. Let's take reference to cgroup
4051 * directory dentry to do that.
4052 */
4053 dget(cgrp->dentry);
4054
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004055 spin_lock(&cgrp->event_list_lock);
4056 list_add(&event->list, &cgrp->event_list);
4057 spin_unlock(&cgrp->event_list_lock);
4058
4059 fput(cfile);
4060 fput(efile);
4061
4062 return 0;
4063
4064fail:
4065 if (cfile)
4066 fput(cfile);
4067
4068 if (event && event->eventfd && !IS_ERR(event->eventfd))
4069 eventfd_ctx_put(event->eventfd);
4070
4071 if (!IS_ERR_OR_NULL(efile))
4072 fput(efile);
4073
4074 kfree(event);
4075
4076 return ret;
4077}
4078
Daniel Lezcano97978e62010-10-27 15:33:35 -07004079static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4080 struct cftype *cft)
4081{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004082 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004083}
4084
4085static int cgroup_clone_children_write(struct cgroup *cgrp,
4086 struct cftype *cft,
4087 u64 val)
4088{
4089 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004090 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004091 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004092 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004093 return 0;
4094}
4095
Tejun Heod5c56ce2013-06-03 19:14:34 -07004096static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004097 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004098 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004099 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004100 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004101 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004102 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004103 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004104 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004105 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004106 .write_string = cgroup_write_event_control,
4107 .mode = S_IWUGO,
4108 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004109 {
4110 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004111 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004112 .read_u64 = cgroup_clone_children_read,
4113 .write_u64 = cgroup_clone_children_write,
4114 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004115 {
Tejun Heo873fe092013-04-14 20:15:26 -07004116 .name = "cgroup.sane_behavior",
4117 .flags = CFTYPE_ONLY_ON_ROOT,
4118 .read_seq_string = cgroup_sane_behavior_show,
4119 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004120
4121 /*
4122 * Historical crazy stuff. These don't have "cgroup." prefix and
4123 * don't exist if sane_behavior. If you're depending on these, be
4124 * prepared to be burned.
4125 */
4126 {
4127 .name = "tasks",
4128 .flags = CFTYPE_INSANE, /* use "procs" instead */
4129 .open = cgroup_tasks_open,
4130 .write_u64 = cgroup_tasks_write,
4131 .release = cgroup_pidlist_release,
4132 .mode = S_IRUGO | S_IWUSR,
4133 },
4134 {
4135 .name = "notify_on_release",
4136 .flags = CFTYPE_INSANE,
4137 .read_u64 = cgroup_read_notify_on_release,
4138 .write_u64 = cgroup_write_notify_on_release,
4139 },
Tejun Heo873fe092013-04-14 20:15:26 -07004140 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004141 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004142 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004143 .read_seq_string = cgroup_release_agent_show,
4144 .write_string = cgroup_release_agent_write,
4145 .max_write_len = PATH_MAX,
4146 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004147 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004148};
4149
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004150/**
4151 * cgroup_populate_dir - selectively creation of files in a directory
4152 * @cgrp: target cgroup
4153 * @base_files: true if the base files should be added
4154 * @subsys_mask: mask of the subsystem ids whose files should be added
4155 */
4156static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4157 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158{
4159 int err;
4160 struct cgroup_subsys *ss;
4161
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004162 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004163 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004164 if (err < 0)
4165 return err;
4166 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004167
Tejun Heo8e3f6542012-04-01 12:09:55 -07004168 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004169 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004170 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004171 if (!test_bit(ss->subsys_id, &subsys_mask))
4172 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004173
Tejun Heodb0416b2012-04-01 12:09:55 -07004174 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004175 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004177
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004178 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004179 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004180 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004181 struct css_id *id = rcu_dereference_protected(css->id, true);
4182
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004183 /*
4184 * Update id->css pointer and make this css visible from
4185 * CSS ID functions. This pointer will be dereferened
4186 * from RCU-read-side without locks.
4187 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004188 if (id)
4189 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004190 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191
4192 return 0;
4193}
4194
Tejun Heo48ddbe12012-04-01 12:09:56 -07004195static void css_dput_fn(struct work_struct *work)
4196{
4197 struct cgroup_subsys_state *css =
4198 container_of(work, struct cgroup_subsys_state, dput_work);
4199
Li Zefan1c8158e2013-06-18 18:41:10 +08004200 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004201}
4202
Tejun Heod3daf282013-06-13 19:39:16 -07004203static void css_release(struct percpu_ref *ref)
4204{
4205 struct cgroup_subsys_state *css =
4206 container_of(ref, struct cgroup_subsys_state, refcnt);
4207
4208 schedule_work(&css->dput_work);
4209}
4210
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211static void init_cgroup_css(struct cgroup_subsys_state *css,
4212 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004213 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004214{
Paul Menagebd89aab2007-10-18 23:40:44 -07004215 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004217 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004218 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004219 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004220 BUG_ON(cgrp->subsys[ss->subsys_id]);
4221 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004222
4223 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004224 * css holds an extra ref to @cgrp->dentry which is put on the last
4225 * css_put(). dput() requires process context, which css_put() may
4226 * be called without. @css->dput_work will be used to invoke
4227 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004228 */
4229 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004230}
4231
Tejun Heob1929db2012-11-19 08:13:38 -08004232/* invoke ->post_create() on a new CSS and mark it online if successful */
4233static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004234{
Tejun Heob1929db2012-11-19 08:13:38 -08004235 int ret = 0;
4236
Tejun Heoa31f2d32012-11-19 08:13:37 -08004237 lockdep_assert_held(&cgroup_mutex);
4238
Tejun Heo92fb9742012-11-19 08:13:38 -08004239 if (ss->css_online)
4240 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004241 if (!ret)
4242 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4243 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004244}
4245
4246/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4247static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4248 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4249{
4250 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4251
4252 lockdep_assert_held(&cgroup_mutex);
4253
4254 if (!(css->flags & CSS_ONLINE))
4255 return;
4256
Li Zefand7eeac12013-03-12 15:35:59 -07004257 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004258 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004259
4260 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4261}
4262
Paul Menageddbcc7e2007-10-18 23:39:30 -07004263/*
Li Zefana043e3b2008-02-23 15:24:09 -08004264 * cgroup_create - create a cgroup
4265 * @parent: cgroup that will be parent of the new cgroup
4266 * @dentry: dentry of the new cgroup
4267 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004268 *
Li Zefana043e3b2008-02-23 15:24:09 -08004269 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004271static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004272 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273{
Paul Menagebd89aab2007-10-18 23:40:44 -07004274 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004275 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004276 struct cgroupfs_root *root = parent->root;
4277 int err = 0;
4278 struct cgroup_subsys *ss;
4279 struct super_block *sb = root->sb;
4280
Tejun Heo0a950f62012-11-19 09:02:12 -08004281 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004282 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4283 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284 return -ENOMEM;
4285
Li Zefan65dff752013-03-01 15:01:56 +08004286 name = cgroup_alloc_name(dentry);
4287 if (!name)
4288 goto err_free_cgrp;
4289 rcu_assign_pointer(cgrp->name, name);
4290
Tejun Heo0a950f62012-11-19 09:02:12 -08004291 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4292 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004293 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004294
Tejun Heo976c06b2012-11-05 09:16:59 -08004295 /*
4296 * Only live parents can have children. Note that the liveliness
4297 * check isn't strictly necessary because cgroup_mkdir() and
4298 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4299 * anyway so that locking is contained inside cgroup proper and we
4300 * don't get nasty surprises if we ever grow another caller.
4301 */
4302 if (!cgroup_lock_live_group(parent)) {
4303 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004304 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004305 }
4306
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307 /* Grab a reference on the superblock so the hierarchy doesn't
4308 * get deleted on unmount if there are child cgroups. This
4309 * can be done outside cgroup_mutex, since the sb can't
4310 * disappear while someone has an open control file on the
4311 * fs */
4312 atomic_inc(&sb->s_active);
4313
Paul Menagecc31edc2008-10-18 20:28:04 -07004314 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004315
Li Zefanfe1c06c2013-01-24 14:30:22 +08004316 dentry->d_fsdata = cgrp;
4317 cgrp->dentry = dentry;
4318
Paul Menagebd89aab2007-10-18 23:40:44 -07004319 cgrp->parent = parent;
4320 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321
Li Zefanb6abdb02008-03-04 14:28:19 -08004322 if (notify_on_release(parent))
4323 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4324
Tejun Heo2260e7f2012-11-19 08:13:38 -08004325 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4326 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004327
Tejun Heo5549c492013-06-24 15:21:48 -07004328 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004329 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004330
Tejun Heo92fb9742012-11-19 08:13:38 -08004331 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 if (IS_ERR(css)) {
4333 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004334 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 }
Tejun Heod3daf282013-06-13 19:39:16 -07004336
4337 err = percpu_ref_init(&css->refcnt, css_release);
4338 if (err)
4339 goto err_free_all;
4340
Paul Menagebd89aab2007-10-18 23:40:44 -07004341 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004342
Li Zefan4528fd02010-02-02 13:44:10 -08004343 if (ss->use_id) {
4344 err = alloc_css_id(ss, parent, cgrp);
4345 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004346 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004347 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 }
4349
Tejun Heo4e139af2012-11-19 08:13:36 -08004350 /*
4351 * Create directory. cgroup_create_file() returns with the new
4352 * directory locked on success so that it can be populated without
4353 * dropping cgroup_mutex.
4354 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004355 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004356 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004357 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004358 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359
Tejun Heo00356bd2013-06-18 11:14:22 -07004360 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004361
Tejun Heo4e139af2012-11-19 08:13:36 -08004362 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004363 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4364 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004365
Tejun Heob1929db2012-11-19 08:13:38 -08004366 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004367 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004368 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004369
Li Zefan415cf072013-04-08 14:35:02 +08004370 /* hold a ref to the parent's dentry */
4371 dget(parent->dentry);
4372
Tejun Heob1929db2012-11-19 08:13:38 -08004373 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004374 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004375 err = online_css(ss, cgrp);
4376 if (err)
4377 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004378
4379 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4380 parent->parent) {
4381 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",
4382 current->comm, current->pid, ss->name);
4383 if (!strcmp(ss->name, "memory"))
4384 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4385 ss->warned_broken_hierarchy = true;
4386 }
Tejun Heoa8638032012-11-09 09:12:29 -08004387 }
4388
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004389 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004390 if (err)
4391 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392
4393 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004394 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004395
4396 return 0;
4397
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004398err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004399 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004400 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4401
4402 if (css) {
4403 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004404 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004405 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004408 /* Release the reference count that we took on the superblock */
4409 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004410err_free_id:
4411 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004412err_free_name:
4413 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004414err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004415 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004417
4418err_destroy:
4419 cgroup_destroy_locked(cgrp);
4420 mutex_unlock(&cgroup_mutex);
4421 mutex_unlock(&dentry->d_inode->i_mutex);
4422 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423}
4424
Al Viro18bb1db2011-07-26 01:41:39 -04004425static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004426{
4427 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4428
4429 /* the vfs holds inode->i_mutex already */
4430 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4431}
4432
Tejun Heod3daf282013-06-13 19:39:16 -07004433static void cgroup_css_killed(struct cgroup *cgrp)
4434{
4435 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4436 return;
4437
4438 /* percpu ref's of all css's are killed, kick off the next step */
4439 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4440 schedule_work(&cgrp->destroy_work);
4441}
4442
4443static void css_ref_killed_fn(struct percpu_ref *ref)
4444{
4445 struct cgroup_subsys_state *css =
4446 container_of(ref, struct cgroup_subsys_state, refcnt);
4447
4448 cgroup_css_killed(css->cgroup);
4449}
4450
4451/**
4452 * cgroup_destroy_locked - the first stage of cgroup destruction
4453 * @cgrp: cgroup to be destroyed
4454 *
4455 * css's make use of percpu refcnts whose killing latency shouldn't be
4456 * exposed to userland and are RCU protected. Also, cgroup core needs to
4457 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4458 * invoked. To satisfy all the requirements, destruction is implemented in
4459 * the following two steps.
4460 *
4461 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4462 * userland visible parts and start killing the percpu refcnts of
4463 * css's. Set up so that the next stage will be kicked off once all
4464 * the percpu refcnts are confirmed to be killed.
4465 *
4466 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4467 * rest of destruction. Once all cgroup references are gone, the
4468 * cgroup is RCU-freed.
4469 *
4470 * This function implements s1. After this step, @cgrp is gone as far as
4471 * the userland is concerned and a new cgroup with the same name may be
4472 * created. As cgroup doesn't care about the names internally, this
4473 * doesn't cause any problem.
4474 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004475static int cgroup_destroy_locked(struct cgroup *cgrp)
4476 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477{
Tejun Heo42809dd2012-11-19 08:13:37 -08004478 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004479 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004480 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004481 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482
Tejun Heo42809dd2012-11-19 08:13:37 -08004483 lockdep_assert_held(&d->d_inode->i_mutex);
4484 lockdep_assert_held(&cgroup_mutex);
4485
Tejun Heoddd69142013-06-12 21:04:54 -07004486 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004487 * css_set_lock synchronizes access to ->cset_links and prevents
4488 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004489 */
4490 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004491 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004492 read_unlock(&css_set_lock);
4493 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004495
Tejun Heo1a90dd52012-11-05 09:16:59 -08004496 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004497 * Block new css_tryget() by killing css refcnts. cgroup core
4498 * guarantees that, by the time ->css_offline() is invoked, no new
4499 * css reference will be given out via css_tryget(). We can't
4500 * simply call percpu_ref_kill() and proceed to offlining css's
4501 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4502 * as killed on all CPUs on return.
4503 *
4504 * Use percpu_ref_kill_and_confirm() to get notifications as each
4505 * css is confirmed to be seen as killed on all CPUs. The
4506 * notification callback keeps track of the number of css's to be
4507 * killed and schedules cgroup_offline_fn() to perform the rest of
4508 * destruction once the percpu refs of all css's are confirmed to
4509 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004510 */
Tejun Heod3daf282013-06-13 19:39:16 -07004511 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004512 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004513 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4514
Tejun Heod3daf282013-06-13 19:39:16 -07004515 /*
4516 * Killing would put the base ref, but we need to keep it
4517 * alive until after ->css_offline.
4518 */
4519 percpu_ref_get(&css->refcnt);
4520
4521 atomic_inc(&cgrp->css_kill_cnt);
4522 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004523 }
Tejun Heod3daf282013-06-13 19:39:16 -07004524 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004525
4526 /*
4527 * Mark @cgrp dead. This prevents further task migration and child
4528 * creation by disabling cgroup_lock_live_group(). Note that
4529 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4530 * resume iteration after dropping RCU read lock. See
4531 * cgroup_next_sibling() for details.
4532 */
Tejun Heo54766d42013-06-12 21:04:53 -07004533 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004534
Tejun Heo455050d2013-06-13 19:27:41 -07004535 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4536 raw_spin_lock(&release_list_lock);
4537 if (!list_empty(&cgrp->release_list))
4538 list_del_init(&cgrp->release_list);
4539 raw_spin_unlock(&release_list_lock);
4540
4541 /*
4542 * Remove @cgrp directory. The removal puts the base ref but we
4543 * aren't quite done with @cgrp yet, so hold onto it.
4544 */
4545 dget(d);
4546 cgroup_d_remove_dir(d);
4547
4548 /*
4549 * Unregister events and notify userspace.
4550 * Notify userspace about cgroup removing only after rmdir of cgroup
4551 * directory to avoid race between userspace and kernelspace.
4552 */
4553 spin_lock(&cgrp->event_list_lock);
4554 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4555 list_del_init(&event->list);
4556 schedule_work(&event->remove);
4557 }
4558 spin_unlock(&cgrp->event_list_lock);
4559
Tejun Heoea15f8c2013-06-13 19:27:42 -07004560 return 0;
4561};
4562
Tejun Heod3daf282013-06-13 19:39:16 -07004563/**
4564 * cgroup_offline_fn - the second step of cgroup destruction
4565 * @work: cgroup->destroy_free_work
4566 *
4567 * This function is invoked from a work item for a cgroup which is being
4568 * destroyed after the percpu refcnts of all css's are guaranteed to be
4569 * seen as killed on all CPUs, and performs the rest of destruction. This
4570 * is the second step of destruction described in the comment above
4571 * cgroup_destroy_locked().
4572 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004573static void cgroup_offline_fn(struct work_struct *work)
4574{
4575 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4576 struct cgroup *parent = cgrp->parent;
4577 struct dentry *d = cgrp->dentry;
4578 struct cgroup_subsys *ss;
4579
4580 mutex_lock(&cgroup_mutex);
4581
Tejun Heod3daf282013-06-13 19:39:16 -07004582 /*
4583 * css_tryget() is guaranteed to fail now. Tell subsystems to
4584 * initate destruction.
4585 */
Tejun Heo5549c492013-06-24 15:21:48 -07004586 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004587 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004588
4589 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004590 * Put the css refs from cgroup_destroy_locked(). Each css holds
4591 * an extra reference to the cgroup's dentry and cgroup removal
4592 * proceeds regardless of css refs. On the last put of each css,
4593 * whenever that may be, the extra dentry ref is put so that dentry
4594 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004595 */
Tejun Heo5549c492013-06-24 15:21:48 -07004596 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004597 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004598
Paul Menage999cd8a2009-01-07 18:08:36 -08004599 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004600 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004601
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603
Paul Menagebd89aab2007-10-18 23:40:44 -07004604 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004605 check_for_release(parent);
4606
Tejun Heoea15f8c2013-06-13 19:27:42 -07004607 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004608}
4609
Tejun Heo42809dd2012-11-19 08:13:37 -08004610static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4611{
4612 int ret;
4613
4614 mutex_lock(&cgroup_mutex);
4615 ret = cgroup_destroy_locked(dentry->d_fsdata);
4616 mutex_unlock(&cgroup_mutex);
4617
4618 return ret;
4619}
4620
Tejun Heo8e3f6542012-04-01 12:09:55 -07004621static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4622{
4623 INIT_LIST_HEAD(&ss->cftsets);
4624
4625 /*
4626 * base_cftset is embedded in subsys itself, no need to worry about
4627 * deregistration.
4628 */
4629 if (ss->base_cftypes) {
4630 ss->base_cftset.cfts = ss->base_cftypes;
4631 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4632 }
4633}
4634
Li Zefan06a11922008-04-29 01:00:07 -07004635static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004638
4639 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640
Tejun Heo648bb562012-11-19 08:13:36 -08004641 mutex_lock(&cgroup_mutex);
4642
Tejun Heo8e3f6542012-04-01 12:09:55 -07004643 /* init base cftset */
4644 cgroup_init_cftsets(ss);
4645
Paul Menageddbcc7e2007-10-18 23:39:30 -07004646 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004647 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4648 ss->root = &cgroup_dummy_root;
4649 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650 /* We don't handle early failures gracefully */
4651 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004652 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653
Li Zefane8d55fd2008-04-29 01:00:13 -07004654 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004655 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004656 * newly registered, all tasks and hence the
4657 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004658 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659
4660 need_forkexit_callback |= ss->fork || ss->exit;
4661
Li Zefane8d55fd2008-04-29 01:00:13 -07004662 /* At system boot, before all subsystems have been
4663 * registered, no tasks have been forked, so we don't
4664 * need to invoke fork callbacks here. */
4665 BUG_ON(!list_empty(&init_task.tasks));
4666
Tejun Heo9871bf92013-06-24 15:21:47 -07004667 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004668
Tejun Heo648bb562012-11-19 08:13:36 -08004669 mutex_unlock(&cgroup_mutex);
4670
Ben Blume6a11052010-03-10 15:22:09 -08004671 /* this function shouldn't be used with modular subsystems, since they
4672 * need to register a subsys_id, among other things */
4673 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674}
4675
4676/**
Ben Blume6a11052010-03-10 15:22:09 -08004677 * cgroup_load_subsys: load and register a modular subsystem at runtime
4678 * @ss: the subsystem to load
4679 *
4680 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004681 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004682 * up for use. If the subsystem is built-in anyway, work is delegated to the
4683 * simpler cgroup_init_subsys.
4684 */
4685int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4686{
Ben Blume6a11052010-03-10 15:22:09 -08004687 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004688 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004689 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004690 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004691 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004692
4693 /* check name and function validity */
4694 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004695 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004696 return -EINVAL;
4697
4698 /*
4699 * we don't support callbacks in modular subsystems. this check is
4700 * before the ss->module check for consistency; a subsystem that could
4701 * be a module should still have no callbacks even if the user isn't
4702 * compiling it as one.
4703 */
4704 if (ss->fork || ss->exit)
4705 return -EINVAL;
4706
4707 /*
4708 * an optionally modular subsystem is built-in: we want to do nothing,
4709 * since cgroup_init_subsys will have already taken care of it.
4710 */
4711 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004712 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004713 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004714 return 0;
4715 }
4716
Tejun Heo8e3f6542012-04-01 12:09:55 -07004717 /* init base cftset */
4718 cgroup_init_cftsets(ss);
4719
Ben Blume6a11052010-03-10 15:22:09 -08004720 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004721 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004722
4723 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004724 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004725 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004726 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004727 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004728 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004729 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004730 /* failure case - need to deassign the cgroup_subsys[] slot. */
4731 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004732 mutex_unlock(&cgroup_mutex);
4733 return PTR_ERR(css);
4734 }
4735
Tejun Heo9871bf92013-06-24 15:21:47 -07004736 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4737 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004738
4739 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004740 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004741 /* init_idr must be after init_cgroup_css because it sets css->id. */
4742 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004743 ret = cgroup_init_idr(ss, css);
4744 if (ret)
4745 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004746 }
4747
4748 /*
4749 * Now we need to entangle the css into the existing css_sets. unlike
4750 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4751 * will need a new pointer to it; done by iterating the css_set_table.
4752 * furthermore, modifying the existing css_sets will corrupt the hash
4753 * table state, so each changed css_set will need its hash recomputed.
4754 * this is all done under the css_set_lock.
4755 */
4756 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004757 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004758 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004759 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004760 continue;
4761 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004762 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004763 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004764 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004765 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004766 key = css_set_hash(cset->subsys);
4767 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004768 }
4769 write_unlock(&css_set_lock);
4770
Tejun Heo9871bf92013-06-24 15:21:47 -07004771 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004772 if (ret)
4773 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004774
Ben Blume6a11052010-03-10 15:22:09 -08004775 /* success! */
4776 mutex_unlock(&cgroup_mutex);
4777 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004778
4779err_unload:
4780 mutex_unlock(&cgroup_mutex);
4781 /* @ss can't be mounted here as try_module_get() would fail */
4782 cgroup_unload_subsys(ss);
4783 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004784}
4785EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4786
4787/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004788 * cgroup_unload_subsys: unload a modular subsystem
4789 * @ss: the subsystem to unload
4790 *
4791 * This function should be called in a modular subsystem's exitcall. When this
4792 * function is invoked, the refcount on the subsystem's module will be 0, so
4793 * the subsystem will not be attached to any hierarchy.
4794 */
4795void cgroup_unload_subsys(struct cgroup_subsys *ss)
4796{
Tejun Heo69d02062013-06-12 21:04:50 -07004797 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004798
4799 BUG_ON(ss->module == NULL);
4800
4801 /*
4802 * we shouldn't be called if the subsystem is in use, and the use of
4803 * try_module_get in parse_cgroupfs_options should ensure that it
4804 * doesn't start being used while we're killing it off.
4805 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004806 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004807
4808 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004809
Tejun Heo9871bf92013-06-24 15:21:47 -07004810 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004811
Tejun Heoc897ff62013-02-27 17:03:49 -08004812 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004813 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004814
Ben Blumcf5d5942010-03-10 15:22:09 -08004815 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004816 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004817
Tejun Heo9871bf92013-06-24 15:21:47 -07004818 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004819 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004820
4821 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004822 * disentangle the css from all css_sets attached to the dummy
4823 * top. as in loading, we need to pay our respects to the hashtable
4824 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004825 */
4826 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004828 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004829 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004830
Tejun Heo5abb8852013-06-12 21:04:49 -07004831 hash_del(&cset->hlist);
4832 cset->subsys[ss->subsys_id] = NULL;
4833 key = css_set_hash(cset->subsys);
4834 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004835 }
4836 write_unlock(&css_set_lock);
4837
4838 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004839 * remove subsystem's css from the cgroup_dummy_top and free it -
4840 * need to free before marking as null because ss->css_free needs
4841 * the cgrp->subsys pointer to find their state. note that this
4842 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004843 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004844 ss->css_free(cgroup_dummy_top);
4845 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004846
4847 mutex_unlock(&cgroup_mutex);
4848}
4849EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4850
4851/**
Li Zefana043e3b2008-02-23 15:24:09 -08004852 * cgroup_init_early - cgroup initialization at system boot
4853 *
4854 * Initialize cgroups at system boot, and initialize any
4855 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004856 */
4857int __init cgroup_init_early(void)
4858{
Tejun Heo30159ec2013-06-25 11:53:37 -07004859 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004861
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004862 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004863 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004864 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004865 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004866 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004867 init_cgroup_root(&cgroup_dummy_root);
4868 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004869 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004870
Tejun Heo69d02062013-06-12 21:04:50 -07004871 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004872 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4873 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004874 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004875
Tejun Heo30159ec2013-06-25 11:53:37 -07004876 /* at bootup time, we don't worry about modular subsystems */
4877 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004878 BUG_ON(!ss->name);
4879 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004880 BUG_ON(!ss->css_alloc);
4881 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004882 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004883 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004884 ss->name, ss->subsys_id);
4885 BUG();
4886 }
4887
4888 if (ss->early_init)
4889 cgroup_init_subsys(ss);
4890 }
4891 return 0;
4892}
4893
4894/**
Li Zefana043e3b2008-02-23 15:24:09 -08004895 * cgroup_init - cgroup initialization
4896 *
4897 * Register cgroup filesystem and /proc file, and initialize
4898 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899 */
4900int __init cgroup_init(void)
4901{
Tejun Heo30159ec2013-06-25 11:53:37 -07004902 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004903 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004904 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004905
4906 err = bdi_init(&cgroup_backing_dev_info);
4907 if (err)
4908 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004909
Tejun Heo30159ec2013-06-25 11:53:37 -07004910 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004911 if (!ss->early_init)
4912 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004913 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004914 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004915 }
4916
Tejun Heofa3ca072013-04-14 11:36:56 -07004917 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004918 mutex_lock(&cgroup_mutex);
4919 mutex_lock(&cgroup_root_mutex);
4920
Tejun Heo82fe9b02013-06-25 11:53:37 -07004921 /* Add init_css_set to the hash table */
4922 key = css_set_hash(init_css_set.subsys);
4923 hash_add(css_set_table, &init_css_set.hlist, key);
4924
Tejun Heofc76df72013-06-25 11:53:37 -07004925 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004926
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004927 mutex_unlock(&cgroup_root_mutex);
4928 mutex_unlock(&cgroup_mutex);
4929
Greg KH676db4a2010-08-05 13:53:35 -07004930 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4931 if (!cgroup_kobj) {
4932 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004933 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004934 }
4935
4936 err = register_filesystem(&cgroup_fs_type);
4937 if (err < 0) {
4938 kobject_put(cgroup_kobj);
4939 goto out;
4940 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004941
Li Zefan46ae2202008-04-29 01:00:08 -07004942 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004943
Paul Menageddbcc7e2007-10-18 23:39:30 -07004944out:
Paul Menagea4243162007-10-18 23:39:35 -07004945 if (err)
4946 bdi_destroy(&cgroup_backing_dev_info);
4947
Paul Menageddbcc7e2007-10-18 23:39:30 -07004948 return err;
4949}
Paul Menageb4f48b62007-10-18 23:39:33 -07004950
Paul Menagea4243162007-10-18 23:39:35 -07004951/*
4952 * proc_cgroup_show()
4953 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4954 * - Used for /proc/<pid>/cgroup.
4955 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4956 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004957 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004958 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4959 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4960 * cgroup to top_cgroup.
4961 */
4962
4963/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004964int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004965{
4966 struct pid *pid;
4967 struct task_struct *tsk;
4968 char *buf;
4969 int retval;
4970 struct cgroupfs_root *root;
4971
4972 retval = -ENOMEM;
4973 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4974 if (!buf)
4975 goto out;
4976
4977 retval = -ESRCH;
4978 pid = m->private;
4979 tsk = get_pid_task(pid, PIDTYPE_PID);
4980 if (!tsk)
4981 goto out_free;
4982
4983 retval = 0;
4984
4985 mutex_lock(&cgroup_mutex);
4986
Li Zefane5f6a862009-01-07 18:07:41 -08004987 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004988 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004989 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004990 int count = 0;
4991
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004992 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004993 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004994 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004995 if (strlen(root->name))
4996 seq_printf(m, "%sname=%s", count ? "," : "",
4997 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004998 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004999 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005000 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005001 if (retval < 0)
5002 goto out_unlock;
5003 seq_puts(m, buf);
5004 seq_putc(m, '\n');
5005 }
5006
5007out_unlock:
5008 mutex_unlock(&cgroup_mutex);
5009 put_task_struct(tsk);
5010out_free:
5011 kfree(buf);
5012out:
5013 return retval;
5014}
5015
Paul Menagea4243162007-10-18 23:39:35 -07005016/* Display information about each subsystem and each hierarchy */
5017static int proc_cgroupstats_show(struct seq_file *m, void *v)
5018{
Tejun Heo30159ec2013-06-25 11:53:37 -07005019 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005020 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005021
Paul Menage8bab8dd2008-04-04 14:29:57 -07005022 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005023 /*
5024 * ideally we don't want subsystems moving around while we do this.
5025 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5026 * subsys/hierarchy state.
5027 */
Paul Menagea4243162007-10-18 23:39:35 -07005028 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005029
5030 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005031 seq_printf(m, "%s\t%d\t%d\t%d\n",
5032 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005033 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005034
Paul Menagea4243162007-10-18 23:39:35 -07005035 mutex_unlock(&cgroup_mutex);
5036 return 0;
5037}
5038
5039static int cgroupstats_open(struct inode *inode, struct file *file)
5040{
Al Viro9dce07f2008-03-29 03:07:28 +00005041 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005042}
5043
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005044static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005045 .open = cgroupstats_open,
5046 .read = seq_read,
5047 .llseek = seq_lseek,
5048 .release = single_release,
5049};
5050
Paul Menageb4f48b62007-10-18 23:39:33 -07005051/**
5052 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005053 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005054 *
5055 * Description: A task inherits its parent's cgroup at fork().
5056 *
5057 * A pointer to the shared css_set was automatically copied in
5058 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005059 * it was not made under the protection of RCU or cgroup_mutex, so
5060 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5061 * have already changed current->cgroups, allowing the previously
5062 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005063 *
5064 * At the point that cgroup_fork() is called, 'current' is the parent
5065 * task, and the passed argument 'child' points to the child task.
5066 */
5067void cgroup_fork(struct task_struct *child)
5068{
Tejun Heo9bb71302012-10-18 17:52:07 -07005069 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005070 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005071 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005072 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005073 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005074}
5075
5076/**
Li Zefana043e3b2008-02-23 15:24:09 -08005077 * cgroup_post_fork - called on a new task after adding it to the task list
5078 * @child: the task in question
5079 *
Tejun Heo5edee612012-10-16 15:03:14 -07005080 * Adds the task to the list running through its css_set if necessary and
5081 * call the subsystem fork() callbacks. Has to be after the task is
5082 * visible on the task list in case we race with the first call to
5083 * cgroup_iter_start() - to guarantee that the new task ends up on its
5084 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005085 */
Paul Menage817929e2007-10-18 23:39:36 -07005086void cgroup_post_fork(struct task_struct *child)
5087{
Tejun Heo30159ec2013-06-25 11:53:37 -07005088 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005089 int i;
5090
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005091 /*
5092 * use_task_css_set_links is set to 1 before we walk the tasklist
5093 * under the tasklist_lock and we read it here after we added the child
5094 * to the tasklist under the tasklist_lock as well. If the child wasn't
5095 * yet in the tasklist when we walked through it from
5096 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5097 * should be visible now due to the paired locking and barriers implied
5098 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5099 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5100 * lock on fork.
5101 */
Paul Menage817929e2007-10-18 23:39:36 -07005102 if (use_task_css_set_links) {
5103 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005104 task_lock(child);
5105 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005106 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005107 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005108 write_unlock(&css_set_lock);
5109 }
Tejun Heo5edee612012-10-16 15:03:14 -07005110
5111 /*
5112 * Call ss->fork(). This must happen after @child is linked on
5113 * css_set; otherwise, @child might change state between ->fork()
5114 * and addition to css_set.
5115 */
5116 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005117 /*
5118 * fork/exit callbacks are supported only for builtin
5119 * subsystems, and the builtin section of the subsys
5120 * array is immutable, so we don't need to lock the
5121 * subsys array here. On the other hand, modular section
5122 * of the array can be freed at module unload, so we
5123 * can't touch that.
5124 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005125 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005126 if (ss->fork)
5127 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005128 }
Paul Menage817929e2007-10-18 23:39:36 -07005129}
Tejun Heo5edee612012-10-16 15:03:14 -07005130
Paul Menage817929e2007-10-18 23:39:36 -07005131/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005132 * cgroup_exit - detach cgroup from exiting task
5133 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005134 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005135 *
5136 * Description: Detach cgroup from @tsk and release it.
5137 *
5138 * Note that cgroups marked notify_on_release force every task in
5139 * them to take the global cgroup_mutex mutex when exiting.
5140 * This could impact scaling on very large systems. Be reluctant to
5141 * use notify_on_release cgroups where very high task exit scaling
5142 * is required on large systems.
5143 *
5144 * the_top_cgroup_hack:
5145 *
5146 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5147 *
5148 * We call cgroup_exit() while the task is still competent to
5149 * handle notify_on_release(), then leave the task attached to the
5150 * root cgroup in each hierarchy for the remainder of its exit.
5151 *
5152 * To do this properly, we would increment the reference count on
5153 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5154 * code we would add a second cgroup function call, to drop that
5155 * reference. This would just create an unnecessary hot spot on
5156 * the top_cgroup reference count, to no avail.
5157 *
5158 * Normally, holding a reference to a cgroup without bumping its
5159 * count is unsafe. The cgroup could go away, or someone could
5160 * attach us to a different cgroup, decrementing the count on
5161 * the first cgroup that we never incremented. But in this case,
5162 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005163 * which wards off any cgroup_attach_task() attempts, or task is a failed
5164 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005165 */
5166void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5167{
Tejun Heo30159ec2013-06-25 11:53:37 -07005168 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005169 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005170 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005171
5172 /*
5173 * Unlink from the css_set task list if necessary.
5174 * Optimistically check cg_list before taking
5175 * css_set_lock
5176 */
5177 if (!list_empty(&tsk->cg_list)) {
5178 write_lock(&css_set_lock);
5179 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005180 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005181 write_unlock(&css_set_lock);
5182 }
5183
Paul Menageb4f48b62007-10-18 23:39:33 -07005184 /* Reassign the task to the init_css_set. */
5185 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005186 cset = task_css_set(tsk);
5187 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005188
5189 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005190 /*
5191 * fork/exit callbacks are supported only for builtin
5192 * subsystems, see cgroup_post_fork() for details.
5193 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005194 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005195 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005196 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005197 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005198
Li Zefan761b3ef2012-01-31 13:47:36 +08005199 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005200 }
5201 }
5202 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005203 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005204
Tejun Heo5abb8852013-06-12 21:04:49 -07005205 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005206}
Paul Menage697f4162007-10-18 23:39:34 -07005207
Paul Menagebd89aab2007-10-18 23:40:44 -07005208static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005209{
Li Zefanf50daa72013-03-01 15:06:07 +08005210 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005211 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005212 /*
5213 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005214 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005215 * it now
5216 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005217 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005218
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005219 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005220 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005221 list_empty(&cgrp->release_list)) {
5222 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005223 need_schedule_work = 1;
5224 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005225 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005226 if (need_schedule_work)
5227 schedule_work(&release_agent_work);
5228 }
5229}
5230
Paul Menage81a6a5c2007-10-18 23:39:38 -07005231/*
5232 * Notify userspace when a cgroup is released, by running the
5233 * configured release agent with the name of the cgroup (path
5234 * relative to the root of cgroup file system) as the argument.
5235 *
5236 * Most likely, this user command will try to rmdir this cgroup.
5237 *
5238 * This races with the possibility that some other task will be
5239 * attached to this cgroup before it is removed, or that some other
5240 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5241 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5242 * unused, and this cgroup will be reprieved from its death sentence,
5243 * to continue to serve a useful existence. Next time it's released,
5244 * we will get notified again, if it still has 'notify_on_release' set.
5245 *
5246 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5247 * means only wait until the task is successfully execve()'d. The
5248 * separate release agent task is forked by call_usermodehelper(),
5249 * then control in this thread returns here, without waiting for the
5250 * release agent task. We don't bother to wait because the caller of
5251 * this routine has no use for the exit status of the release agent
5252 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005253 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005254static void cgroup_release_agent(struct work_struct *work)
5255{
5256 BUG_ON(work != &release_agent_work);
5257 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005258 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005259 while (!list_empty(&release_list)) {
5260 char *argv[3], *envp[3];
5261 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005262 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005263 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005264 struct cgroup,
5265 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005266 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005267 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005268 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005269 if (!pathbuf)
5270 goto continue_free;
5271 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5272 goto continue_free;
5273 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5274 if (!agentbuf)
5275 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005276
5277 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005278 argv[i++] = agentbuf;
5279 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005280 argv[i] = NULL;
5281
5282 i = 0;
5283 /* minimal command environment */
5284 envp[i++] = "HOME=/";
5285 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5286 envp[i] = NULL;
5287
5288 /* Drop the lock while we invoke the usermode helper,
5289 * since the exec could involve hitting disk and hence
5290 * be a slow process */
5291 mutex_unlock(&cgroup_mutex);
5292 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005293 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005294 continue_free:
5295 kfree(pathbuf);
5296 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005297 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005298 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005299 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005300 mutex_unlock(&cgroup_mutex);
5301}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005302
5303static int __init cgroup_disable(char *str)
5304{
Tejun Heo30159ec2013-06-25 11:53:37 -07005305 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005306 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005307 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005308
5309 while ((token = strsep(&str, ",")) != NULL) {
5310 if (!*token)
5311 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005312
Tejun Heo30159ec2013-06-25 11:53:37 -07005313 /*
5314 * cgroup_disable, being at boot time, can't know about
5315 * module subsystems, so we don't worry about them.
5316 */
5317 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005318 if (!strcmp(token, ss->name)) {
5319 ss->disabled = 1;
5320 printk(KERN_INFO "Disabling %s control group"
5321 " subsystem\n", ss->name);
5322 break;
5323 }
5324 }
5325 }
5326 return 1;
5327}
5328__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005329
5330/*
5331 * Functons for CSS ID.
5332 */
5333
Tejun Heo54766d42013-06-12 21:04:53 -07005334/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005335unsigned short css_id(struct cgroup_subsys_state *css)
5336{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005337 struct css_id *cssid;
5338
5339 /*
5340 * This css_id() can return correct value when somone has refcnt
5341 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5342 * it's unchanged until freed.
5343 */
Tejun Heod3daf282013-06-13 19:39:16 -07005344 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005345
5346 if (cssid)
5347 return cssid->id;
5348 return 0;
5349}
Ben Blum67523c42010-03-10 15:22:11 -08005350EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005351
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005352/**
5353 * css_is_ancestor - test "root" css is an ancestor of "child"
5354 * @child: the css to be tested.
5355 * @root: the css supporsed to be an ancestor of the child.
5356 *
5357 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005358 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005359 * But, considering usual usage, the csses should be valid objects after test.
5360 * Assuming that the caller will do some action to the child if this returns
5361 * returns true, the caller must take "child";s reference count.
5362 * If "child" is valid object and this returns true, "root" is valid, too.
5363 */
5364
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005365bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005366 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005367{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005368 struct css_id *child_id;
5369 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005370
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005371 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005372 if (!child_id)
5373 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005374 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005375 if (!root_id)
5376 return false;
5377 if (child_id->depth < root_id->depth)
5378 return false;
5379 if (child_id->stack[root_id->depth] != root_id->id)
5380 return false;
5381 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005382}
5383
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005384void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5385{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005386 struct css_id *id = rcu_dereference_protected(css->id, true);
5387
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005388 /* When this is called before css_id initialization, id can be NULL */
5389 if (!id)
5390 return;
5391
5392 BUG_ON(!ss->use_id);
5393
5394 rcu_assign_pointer(id->css, NULL);
5395 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005396 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005397 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005398 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005399 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005400}
Ben Blum67523c42010-03-10 15:22:11 -08005401EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005402
5403/*
5404 * This is called by init or create(). Then, calls to this function are
5405 * always serialized (By cgroup_mutex() at create()).
5406 */
5407
5408static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5409{
5410 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005411 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005412
5413 BUG_ON(!ss->use_id);
5414
5415 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5416 newid = kzalloc(size, GFP_KERNEL);
5417 if (!newid)
5418 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005419
5420 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005421 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005422 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005423 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005424 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005425 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426
5427 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005428 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005430
Tejun Heod228d9e2013-02-27 17:04:54 -08005431 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005432 newid->depth = depth;
5433 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005434err_out:
5435 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005436 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005437
5438}
5439
Ben Blume6a11052010-03-10 15:22:09 -08005440static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5441 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005442{
5443 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005444
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005445 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446 idr_init(&ss->idr);
5447
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005448 newid = get_new_cssid(ss, 0);
5449 if (IS_ERR(newid))
5450 return PTR_ERR(newid);
5451
5452 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005453 RCU_INIT_POINTER(newid->css, rootcss);
5454 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005455 return 0;
5456}
5457
5458static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5459 struct cgroup *child)
5460{
5461 int subsys_id, i, depth = 0;
5462 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005463 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005464
5465 subsys_id = ss->subsys_id;
5466 parent_css = parent->subsys[subsys_id];
5467 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005468 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005469 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005470
5471 child_id = get_new_cssid(ss, depth);
5472 if (IS_ERR(child_id))
5473 return PTR_ERR(child_id);
5474
5475 for (i = 0; i < depth; i++)
5476 child_id->stack[i] = parent_id->stack[i];
5477 child_id->stack[depth] = child_id->id;
5478 /*
5479 * child_id->css pointer will be set after this cgroup is available
5480 * see cgroup_populate_dir()
5481 */
5482 rcu_assign_pointer(child_css->id, child_id);
5483
5484 return 0;
5485}
5486
5487/**
5488 * css_lookup - lookup css by id
5489 * @ss: cgroup subsys to be looked into.
5490 * @id: the id
5491 *
5492 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5493 * NULL if not. Should be called under rcu_read_lock()
5494 */
5495struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5496{
5497 struct css_id *cssid = NULL;
5498
5499 BUG_ON(!ss->use_id);
5500 cssid = idr_find(&ss->idr, id);
5501
5502 if (unlikely(!cssid))
5503 return NULL;
5504
5505 return rcu_dereference(cssid->css);
5506}
Ben Blum67523c42010-03-10 15:22:11 -08005507EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005508
Stephane Eraniane5d13672011-02-14 11:20:01 +02005509/*
5510 * get corresponding css from file open on cgroupfs directory
5511 */
5512struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5513{
5514 struct cgroup *cgrp;
5515 struct inode *inode;
5516 struct cgroup_subsys_state *css;
5517
Al Viro496ad9a2013-01-23 17:07:38 -05005518 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005519 /* check in cgroup filesystem dir */
5520 if (inode->i_op != &cgroup_dir_inode_operations)
5521 return ERR_PTR(-EBADF);
5522
5523 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5524 return ERR_PTR(-EINVAL);
5525
5526 /* get cgroup */
5527 cgrp = __d_cgrp(f->f_dentry);
5528 css = cgrp->subsys[id];
5529 return css ? css : ERR_PTR(-ENOENT);
5530}
5531
Paul Menagefe693432009-09-23 15:56:20 -07005532#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005533static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005534{
5535 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5536
5537 if (!css)
5538 return ERR_PTR(-ENOMEM);
5539
5540 return css;
5541}
5542
Li Zefan03c78cb2013-06-14 11:17:19 +08005543static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005544{
Li Zefan03c78cb2013-06-14 11:17:19 +08005545 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005546}
5547
Li Zefan03c78cb2013-06-14 11:17:19 +08005548static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005549{
Li Zefan03c78cb2013-06-14 11:17:19 +08005550 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005551}
5552
Li Zefan03c78cb2013-06-14 11:17:19 +08005553static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005554{
5555 return (u64)(unsigned long)current->cgroups;
5556}
5557
Li Zefan03c78cb2013-06-14 11:17:19 +08005558static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5559 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005560{
5561 u64 count;
5562
5563 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005564 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005565 rcu_read_unlock();
5566 return count;
5567}
5568
Li Zefan03c78cb2013-06-14 11:17:19 +08005569static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005570 struct cftype *cft,
5571 struct seq_file *seq)
5572{
Tejun Heo69d02062013-06-12 21:04:50 -07005573 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005574 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005575
5576 read_lock(&css_set_lock);
5577 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005578 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005579 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005580 struct cgroup *c = link->cgrp;
5581 const char *name;
5582
5583 if (c->dentry)
5584 name = c->dentry->d_name.name;
5585 else
5586 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005587 seq_printf(seq, "Root %d group %s\n",
5588 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005589 }
5590 rcu_read_unlock();
5591 read_unlock(&css_set_lock);
5592 return 0;
5593}
5594
5595#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005596static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005597 struct cftype *cft,
5598 struct seq_file *seq)
5599{
Tejun Heo69d02062013-06-12 21:04:50 -07005600 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005601
5602 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005603 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005604 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005605 struct task_struct *task;
5606 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005607 seq_printf(seq, "css_set %p\n", cset);
5608 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005609 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5610 seq_puts(seq, " ...\n");
5611 break;
5612 } else {
5613 seq_printf(seq, " task %d\n",
5614 task_pid_vnr(task));
5615 }
5616 }
5617 }
5618 read_unlock(&css_set_lock);
5619 return 0;
5620}
5621
Paul Menagefe693432009-09-23 15:56:20 -07005622static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5623{
5624 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5625}
5626
5627static struct cftype debug_files[] = {
5628 {
Paul Menagefe693432009-09-23 15:56:20 -07005629 .name = "taskcount",
5630 .read_u64 = debug_taskcount_read,
5631 },
5632
5633 {
5634 .name = "current_css_set",
5635 .read_u64 = current_css_set_read,
5636 },
5637
5638 {
5639 .name = "current_css_set_refcount",
5640 .read_u64 = current_css_set_refcount_read,
5641 },
5642
5643 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005644 .name = "current_css_set_cg_links",
5645 .read_seq_string = current_css_set_cg_links_read,
5646 },
5647
5648 {
5649 .name = "cgroup_css_links",
5650 .read_seq_string = cgroup_css_links_read,
5651 },
5652
5653 {
Paul Menagefe693432009-09-23 15:56:20 -07005654 .name = "releasable",
5655 .read_u64 = releasable_read,
5656 },
Paul Menagefe693432009-09-23 15:56:20 -07005657
Tejun Heo4baf6e32012-04-01 12:09:55 -07005658 { } /* terminate */
5659};
Paul Menagefe693432009-09-23 15:56:20 -07005660
5661struct cgroup_subsys debug_subsys = {
5662 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005663 .css_alloc = debug_css_alloc,
5664 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005665 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005666 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005667};
5668#endif /* CONFIG_CGROUP_DEBUG */