blob: 4ed86773fff77a89e261e581ed62973389552e46 [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);
Al Viro00cd8dd2012-06-10 17:13:09 -0400805static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400807static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
808 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700809static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700810static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700811
812static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200813 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700814 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700815};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700817static int alloc_css_id(struct cgroup_subsys *ss,
818 struct cgroup *parent, struct cgroup *child);
819
Al Viroa5e7ed32011-07-26 01:55:55 -0400820static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821{
822 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823
824 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400825 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100827 inode->i_uid = current_fsuid();
828 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700829 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
830 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
831 }
832 return inode;
833}
834
Li Zefan65dff752013-03-01 15:01:56 +0800835static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
836{
837 struct cgroup_name *name;
838
839 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
840 if (!name)
841 return NULL;
842 strcpy(name->name, dentry->d_name.name);
843 return name;
844}
845
Li Zefanbe445622013-01-24 14:31:42 +0800846static void cgroup_free_fn(struct work_struct *work)
847{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700848 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800849 struct cgroup_subsys *ss;
850
851 mutex_lock(&cgroup_mutex);
852 /*
853 * Release the subsystem state objects.
854 */
Tejun Heo5549c492013-06-24 15:21:48 -0700855 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800856 ss->css_free(cgrp);
857
858 cgrp->root->number_of_cgroups--;
859 mutex_unlock(&cgroup_mutex);
860
861 /*
Li Zefan415cf072013-04-08 14:35:02 +0800862 * We get a ref to the parent's dentry, and put the ref when
863 * this cgroup is being freed, so it's guaranteed that the
864 * parent won't be destroyed before its children.
865 */
866 dput(cgrp->parent->dentry);
867
Li Zefancc20e012013-04-26 11:58:02 -0700868 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
869
Li Zefan415cf072013-04-08 14:35:02 +0800870 /*
Li Zefanbe445622013-01-24 14:31:42 +0800871 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700872 * created the cgroup. This will free cgrp->root, if we are
873 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800874 */
875 deactivate_super(cgrp->root->sb);
876
877 /*
878 * if we're getting rid of the cgroup, refcount should ensure
879 * that there are no pidlists left.
880 */
881 BUG_ON(!list_empty(&cgrp->pidlists));
882
883 simple_xattrs_free(&cgrp->xattrs);
884
Li Zefan65dff752013-03-01 15:01:56 +0800885 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800886 kfree(cgrp);
887}
888
889static void cgroup_free_rcu(struct rcu_head *head)
890{
891 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
892
Tejun Heoea15f8c2013-06-13 19:27:42 -0700893 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
894 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800895}
896
Paul Menageddbcc7e2007-10-18 23:39:30 -0700897static void cgroup_diput(struct dentry *dentry, struct inode *inode)
898{
899 /* is dentry a directory ? if so, kfree() associated cgroup */
900 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700901 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800902
Tejun Heo54766d42013-06-12 21:04:53 -0700903 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800904 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700905 } else {
906 struct cfent *cfe = __d_cfe(dentry);
907 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
908
909 WARN_ONCE(!list_empty(&cfe->node) &&
910 cgrp != &cgrp->root->top_cgroup,
911 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700912 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700913 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914 }
915 iput(inode);
916}
917
Al Viroc72a04e2011-01-14 05:31:45 +0000918static int cgroup_delete(const struct dentry *d)
919{
920 return 1;
921}
922
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923static void remove_dir(struct dentry *d)
924{
925 struct dentry *parent = dget(d->d_parent);
926
927 d_delete(d);
928 simple_rmdir(parent->d_inode, d);
929 dput(parent);
930}
931
Li Zefan2739d3c2013-01-21 18:18:33 +0800932static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700934 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
937 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100938
Li Zefan2739d3c2013-01-21 18:18:33 +0800939 /*
940 * If we're doing cleanup due to failure of cgroup_create(),
941 * the corresponding @cfe may not exist.
942 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943 list_for_each_entry(cfe, &cgrp->files, node) {
944 struct dentry *d = cfe->dentry;
945
946 if (cft && cfe->type != cft)
947 continue;
948
949 dget(d);
950 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700951 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952 list_del_init(&cfe->node);
953 dput(d);
954
Li Zefan2739d3c2013-01-21 18:18:33 +0800955 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957}
958
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959/**
960 * cgroup_clear_directory - selective removal of base and subsystem files
961 * @dir: directory containing the files
962 * @base_files: true if the base files should be removed
963 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 */
965static void cgroup_clear_directory(struct dentry *dir, bool base_files,
966 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967{
968 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700970
Tejun Heo5549c492013-06-24 15:21:48 -0700971 for_each_root_subsys(cgrp->root, ss) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cftype_set *set;
973 if (!test_bit(ss->subsys_id, &subsys_mask))
974 continue;
975 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800976 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 }
978 if (base_files) {
979 while (!list_empty(&cgrp->files))
980 cgroup_rm_file(cgrp, NULL);
981 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982}
983
984/*
985 * NOTE : the dentry must have been dget()'ed
986 */
987static void cgroup_d_remove_dir(struct dentry *dentry)
988{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400992 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100994 parent = dentry->d_parent;
995 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800996 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100998 spin_unlock(&dentry->d_lock);
999 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 remove_dir(dentry);
1001}
1002
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001003/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001004 * Call with cgroup_mutex held. Drops reference counts on modules, including
1005 * any duplicate ones that parse_cgroupfs_options took. If this function
1006 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001007 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001009 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010{
Paul Menagebd89aab2007-10-18 23:40:44 -07001011 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001012 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 int i;
1014
Ben Blumaae8aab2010-03-10 15:22:07 -08001015 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001016 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001017
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001019 for_each_subsys(ss, i) {
Li Zefan8d53d552008-02-23 15:24:11 -08001020 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001021
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001022 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001024
Tejun Heo9871bf92013-06-24 15:21:47 -07001025 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 /* Subsystem isn't free */
1027 return -EBUSY;
1028 }
1029 }
1030
1031 /* Currently we don't handle adding/removing subsystems when
1032 * any child cgroups exist. This is theoretically supportable
1033 * but involves complex error handling, so it's being left until
1034 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001035 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 return -EBUSY;
1037
1038 /* Process each subsystem */
Tejun Heo30159ec2013-06-25 11:53:37 -07001039 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001041
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001042 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001045 BUG_ON(!cgroup_dummy_top->subsys[i]);
1046 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Tejun Heo9871bf92013-06-24 15:21:47 -07001048 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001049 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001050 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001051 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001053 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001057 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001059 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001061
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001063 ss->bind(cgroup_dummy_top);
1064 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001066 cgroup_subsys[i]->root = &cgroup_dummy_root;
1067 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001068
Ben Blumcf5d5942010-03-10 15:22:09 -08001069 /* subsystem is now free - drop reference on module */
1070 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001071 root->subsys_mask &= ~bit;
1072 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001075 /*
1076 * a refcount was taken, but we already had one, so
1077 * drop the extra reference.
1078 */
1079 module_put(ss->module);
1080#ifdef CONFIG_MODULE_UNLOAD
1081 BUG_ON(ss->module && !module_refcount(ss->module));
1082#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 } else {
1084 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001085 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 }
1087 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088
Tejun Heo1672d042013-06-25 18:04:54 -07001089 /*
1090 * Mark @root has finished binding subsystems. @root->subsys_mask
1091 * now matches the bound subsystems.
1092 */
1093 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1094
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 return 0;
1096}
1097
Al Viro34c80b12011-12-08 21:32:45 -05001098static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099{
Al Viro34c80b12011-12-08 21:32:45 -05001100 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101 struct cgroup_subsys *ss;
1102
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001103 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001104 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001106 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1107 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001108 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001110 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001111 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001112 if (strlen(root->release_agent_path))
1113 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001114 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001115 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001116 if (strlen(root->name))
1117 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001118 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119 return 0;
1120}
1121
1122struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001123 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001125 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001126 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001127 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001128 /* User explicitly requested empty subsystem */
1129 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001130
1131 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001132
Paul Menageddbcc7e2007-10-18 23:39:30 -07001133};
1134
Ben Blumaae8aab2010-03-10 15:22:07 -08001135/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001136 * Convert a hierarchy specifier into a bitmask of subsystems and
1137 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1138 * array. This function takes refcounts on subsystems to be used, unless it
1139 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001140 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001141static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001143 char *token, *o = data;
1144 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001145 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001146 bool module_pin_failed = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07001147 struct cgroup_subsys *ss;
1148 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149
Ben Blumaae8aab2010-03-10 15:22:07 -08001150 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1151
Li Zefanf9ab5b52009-06-17 16:26:33 -07001152#ifdef CONFIG_CPUSETS
1153 mask = ~(1UL << cpuset_subsys_id);
1154#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155
Paul Menagec6d57f32009-09-23 15:56:19 -07001156 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157
1158 while ((token = strsep(&o, ",")) != NULL) {
1159 if (!*token)
1160 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001162 /* Explicitly have no subsystems */
1163 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strcmp(token, "all")) {
1167 /* Mutually exclusive option 'all' + subsystem name */
1168 if (one_ss)
1169 return -EINVAL;
1170 all_ss = true;
1171 continue;
1172 }
Tejun Heo873fe092013-04-14 20:15:26 -07001173 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1174 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1175 continue;
1176 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001178 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001182 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001185 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001186 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001187 continue;
1188 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001189 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001190 /* Specifying two release agents is forbidden */
1191 if (opts->release_agent)
1192 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001194 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001195 if (!opts->release_agent)
1196 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 continue;
1198 }
1199 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 const char *name = token + 5;
1201 /* Can't specify an empty name */
1202 if (!strlen(name))
1203 return -EINVAL;
1204 /* Must match [\w.-]+ */
1205 for (i = 0; i < strlen(name); i++) {
1206 char c = name[i];
1207 if (isalnum(c))
1208 continue;
1209 if ((c == '.') || (c == '-') || (c == '_'))
1210 continue;
1211 return -EINVAL;
1212 }
1213 /* Specifying two names is forbidden */
1214 if (opts->name)
1215 return -EINVAL;
1216 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001217 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001218 GFP_KERNEL);
1219 if (!opts->name)
1220 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221
1222 continue;
1223 }
1224
Tejun Heo30159ec2013-06-25 11:53:37 -07001225 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 if (strcmp(token, ss->name))
1227 continue;
1228 if (ss->disabled)
1229 continue;
1230
1231 /* Mutually exclusive option 'all' + subsystem name */
1232 if (all_ss)
1233 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001234 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 one_ss = true;
1236
1237 break;
1238 }
1239 if (i == CGROUP_SUBSYS_COUNT)
1240 return -ENOENT;
1241 }
1242
1243 /*
1244 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001245 * otherwise if 'none', 'name=' and a subsystem name options
1246 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001247 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001248 if (all_ss || (!one_ss && !opts->none && !opts->name))
1249 for_each_subsys(ss, i)
1250 if (!ss->disabled)
1251 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001253 /* Consistency checks */
1254
Tejun Heo873fe092013-04-14 20:15:26 -07001255 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1256 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1257
1258 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1259 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1260 return -EINVAL;
1261 }
1262
1263 if (opts->cpuset_clone_children) {
1264 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1265 return -EINVAL;
1266 }
1267 }
1268
Li Zefanf9ab5b52009-06-17 16:26:33 -07001269 /*
1270 * Option noprefix was introduced just for backward compatibility
1271 * with the old cpuset, so we allow noprefix only if mounting just
1272 * the cpuset subsystem.
1273 */
Tejun Heo93438622013-04-14 20:15:25 -07001274 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001275 return -EINVAL;
1276
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277
1278 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001279 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001280 return -EINVAL;
1281
1282 /*
1283 * We either have to specify by name or by subsystems. (So all
1284 * empty hierarchies must have a name).
1285 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001286 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287 return -EINVAL;
1288
Ben Blumcf5d5942010-03-10 15:22:09 -08001289 /*
1290 * Grab references on all the modules we'll need, so the subsystems
1291 * don't dance around before rebind_subsystems attaches them. This may
1292 * take duplicate reference counts on a subsystem that's already used,
1293 * but rebind_subsystems handles this case.
1294 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001295 for_each_subsys(ss, i) {
1296 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001297 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001298 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001299 module_pin_failed = true;
1300 break;
1301 }
1302 }
1303 if (module_pin_failed) {
1304 /*
1305 * oops, one of the modules was going away. this means that we
1306 * raced with a module_delete call, and to the user this is
1307 * essentially a "subsystem doesn't exist" case.
1308 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001309 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 /* drop refcounts only on the ones we took */
1311 unsigned long bit = 1UL << i;
1312
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001313 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001315 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001316 }
1317 return -ENOENT;
1318 }
1319
Paul Menageddbcc7e2007-10-18 23:39:30 -07001320 return 0;
1321}
1322
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001323static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001324{
Tejun Heo30159ec2013-06-25 11:53:37 -07001325 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001326 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001327
Tejun Heoeb178d02013-06-25 18:05:21 -07001328 mutex_lock(&cgroup_mutex);
1329 for_each_subsys(ss, i)
1330 if (subsys_mask & (1UL << i))
1331 module_put(cgroup_subsys[i]->module);
1332 mutex_unlock(&cgroup_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001333}
1334
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1336{
1337 int ret = 0;
1338 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001339 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001341 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
Tejun Heo873fe092013-04-14 20:15:26 -07001343 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1344 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1345 return -EINVAL;
1346 }
1347
Paul Menagebd89aab2007-10-18 23:40:44 -07001348 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001350 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351
1352 /* See what subsystems are wanted */
1353 ret = parse_cgroupfs_options(data, &opts);
1354 if (ret)
1355 goto out_unlock;
1356
Tejun Heoa8a648c2013-06-24 15:21:47 -07001357 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001358 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1359 task_tgid_nr(current), current->comm);
1360
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001361 added_mask = opts.subsys_mask & ~root->subsys_mask;
1362 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001363
Ben Blumcf5d5942010-03-10 15:22:09 -08001364 /* Don't allow flags or name to change at remount */
1365 if (opts.flags != root->flags ||
1366 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001368 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001369 goto out_unlock;
1370 }
1371
Gao feng7083d032012-12-03 09:28:18 +08001372 /*
1373 * Clear out the files of subsystems that should be removed, do
1374 * this before rebind_subsystems, since rebind_subsystems may
1375 * change this hierarchy's subsys_list.
1376 */
1377 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1378
Tejun Heoa8a648c2013-06-24 15:21:47 -07001379 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001380 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001381 /* rebind_subsystems failed, re-populate the removed files */
1382 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001383 drop_parsed_module_refcounts(opts.subsys_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);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398 return ret;
1399}
1400
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001401static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402 .statfs = simple_statfs,
1403 .drop_inode = generic_delete_inode,
1404 .show_options = cgroup_show_options,
1405 .remount_fs = cgroup_remount,
1406};
1407
Paul Menagecc31edc2008-10-18 20:28:04 -07001408static void init_cgroup_housekeeping(struct cgroup *cgrp)
1409{
1410 INIT_LIST_HEAD(&cgrp->sibling);
1411 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001412 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001413 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001415 INIT_LIST_HEAD(&cgrp->pidlists);
1416 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001417 INIT_LIST_HEAD(&cgrp->event_list);
1418 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001419 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001420}
Paul Menagec6d57f32009-09-23 15:56:19 -07001421
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422static void init_cgroup_root(struct cgroupfs_root *root)
1423{
Paul Menagebd89aab2007-10-18 23:40:44 -07001424 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001425
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426 INIT_LIST_HEAD(&root->subsys_list);
1427 INIT_LIST_HEAD(&root->root_list);
1428 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001429 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001430 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001431 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432}
1433
Tejun Heofc76df72013-06-25 11:53:37 -07001434static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001435{
Tejun Heo1a574232013-04-14 11:36:58 -07001436 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001437
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001438 lockdep_assert_held(&cgroup_mutex);
1439 lockdep_assert_held(&cgroup_root_mutex);
1440
Tejun Heofc76df72013-06-25 11:53:37 -07001441 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1442 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001443 if (id < 0)
1444 return id;
1445
1446 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001447 return 0;
1448}
1449
1450static void cgroup_exit_root_id(struct cgroupfs_root *root)
1451{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001452 lockdep_assert_held(&cgroup_mutex);
1453 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001454
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001455 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001456 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001457 root->hierarchy_id = 0;
1458 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001459}
1460
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461static int cgroup_test_super(struct super_block *sb, void *data)
1462{
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001464 struct cgroupfs_root *root = sb->s_fs_info;
1465
Paul Menagec6d57f32009-09-23 15:56:19 -07001466 /* If we asked for a name then it must match */
1467 if (opts->name && strcmp(opts->name, root->name))
1468 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001470 /*
1471 * If we asked for subsystems (or explicitly for no
1472 * subsystems) then they must match
1473 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001474 if ((opts->subsys_mask || opts->none)
1475 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476 return 0;
1477
1478 return 1;
1479}
1480
Paul Menagec6d57f32009-09-23 15:56:19 -07001481static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1482{
1483 struct cgroupfs_root *root;
1484
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001485 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001486 return NULL;
1487
1488 root = kzalloc(sizeof(*root), GFP_KERNEL);
1489 if (!root)
1490 return ERR_PTR(-ENOMEM);
1491
1492 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493
Tejun Heo1672d042013-06-25 18:04:54 -07001494 /*
1495 * We need to set @root->subsys_mask now so that @root can be
1496 * matched by cgroup_test_super() before it finishes
1497 * initialization; otherwise, competing mounts with the same
1498 * options may try to bind the same subsystems instead of waiting
1499 * for the first one leading to unexpected mount errors.
1500 * SUBSYS_BOUND will be set once actual binding is complete.
1501 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001502 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001503 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001504 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001505 if (opts->release_agent)
1506 strcpy(root->release_agent_path, opts->release_agent);
1507 if (opts->name)
1508 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001509 if (opts->cpuset_clone_children)
1510 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001511 return root;
1512}
1513
Tejun Heofa3ca072013-04-14 11:36:56 -07001514static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001515{
Tejun Heofa3ca072013-04-14 11:36:56 -07001516 if (root) {
1517 /* hierarhcy ID shoulid already have been released */
1518 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001519
Tejun Heofa3ca072013-04-14 11:36:56 -07001520 ida_destroy(&root->cgroup_ida);
1521 kfree(root);
1522 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001523}
1524
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525static int cgroup_set_super(struct super_block *sb, void *data)
1526{
1527 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001528 struct cgroup_sb_opts *opts = data;
1529
1530 /* If we don't have a new root, we can't set up a new sb */
1531 if (!opts->new_root)
1532 return -EINVAL;
1533
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001534 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535
1536 ret = set_anon_super(sb, NULL);
1537 if (ret)
1538 return ret;
1539
Paul Menagec6d57f32009-09-23 15:56:19 -07001540 sb->s_fs_info = opts->new_root;
1541 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542
1543 sb->s_blocksize = PAGE_CACHE_SIZE;
1544 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1545 sb->s_magic = CGROUP_SUPER_MAGIC;
1546 sb->s_op = &cgroup_ops;
1547
1548 return 0;
1549}
1550
1551static int cgroup_get_rootdir(struct super_block *sb)
1552{
Al Viro0df6a632010-12-21 13:29:29 -05001553 static const struct dentry_operations cgroup_dops = {
1554 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001555 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001556 };
1557
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 struct inode *inode =
1559 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560
1561 if (!inode)
1562 return -ENOMEM;
1563
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 inode->i_fop = &simple_dir_operations;
1565 inode->i_op = &cgroup_dir_inode_operations;
1566 /* directories start off with i_nlink == 2 (for "." entry) */
1567 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001568 sb->s_root = d_make_root(inode);
1569 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001571 /* for everything else we want ->d_op set */
1572 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 return 0;
1574}
1575
Al Virof7e83572010-07-26 13:23:11 +04001576static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001578 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579{
1580 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 int ret = 0;
1583 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001585 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
1587 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001588 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001590 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 if (ret)
1592 goto out_err;
1593
1594 /*
1595 * Allocate a new cgroup root. We may not need it if we're
1596 * reusing an existing hierarchy.
1597 */
1598 new_root = cgroup_root_from_opts(&opts);
1599 if (IS_ERR(new_root)) {
1600 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001601 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001602 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001603 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001606 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001608 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001609 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001610 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001611 }
1612
Paul Menagec6d57f32009-09-23 15:56:19 -07001613 root = sb->s_fs_info;
1614 BUG_ON(!root);
1615 if (root == opts.new_root) {
1616 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001617 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001618 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001619 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001620 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001621 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001622 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623
1624 BUG_ON(sb->s_root != NULL);
1625
1626 ret = cgroup_get_rootdir(sb);
1627 if (ret)
1628 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001629 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Paul Menage817929e2007-10-18 23:39:36 -07001631 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001632 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001633 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001634
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 /* Check for name clashes with existing mounts */
1636 ret = -EBUSY;
1637 if (strlen(root->name))
1638 for_each_active_root(existing_root)
1639 if (!strcmp(existing_root->name, root->name))
1640 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001641
Paul Menage817929e2007-10-18 23:39:36 -07001642 /*
1643 * We're accessing css_set_count without locking
1644 * css_set_lock here, but that's OK - it can only be
1645 * increased by someone holding cgroup_lock, and
1646 * that's us. The worst that can happen is that we
1647 * have some link structures left over
1648 */
Tejun Heo69d02062013-06-12 21:04:50 -07001649 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001650 if (ret)
1651 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001652
Tejun Heofc76df72013-06-25 11:53:37 -07001653 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1654 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001655 if (ret)
1656 goto unlock_drop;
1657
Tejun Heoa8a648c2013-06-24 15:21:47 -07001658 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001660 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001661 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001663 /*
1664 * There must be no failure case after here, since rebinding
1665 * takes care of subsystems' refcounts, which are explicitly
1666 * dropped in the failure exit path.
1667 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668
1669 /* EBUSY should be the only error here */
1670 BUG_ON(ret);
1671
Tejun Heo9871bf92013-06-24 15:21:47 -07001672 list_add(&root->root_list, &cgroup_roots);
1673 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674
Li Zefanc12f65d2009-01-07 18:07:42 -08001675 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 root->top_cgroup.dentry = sb->s_root;
1677
Paul Menage817929e2007-10-18 23:39:36 -07001678 /* Link the top cgroup in this hierarchy into all
1679 * the css_set objects */
1680 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001681 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001682 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001683 write_unlock(&css_set_lock);
1684
Tejun Heo69d02062013-06-12 21:04:50 -07001685 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001686
Li Zefanc12f65d2009-01-07 18:07:42 -08001687 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001688 BUG_ON(root->number_of_cgroups != 1);
1689
eparis@redhat2ce97382011-06-02 21:20:51 +10001690 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001691 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001692 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001693 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001695 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 } else {
1697 /*
1698 * We re-used an existing hierarchy - the new root (if
1699 * any) is not needed
1700 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001701 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001702
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001703 if (root->flags != opts.flags) {
1704 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1705 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1706 ret = -EINVAL;
1707 goto drop_new_super;
1708 } else {
1709 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1710 }
Tejun Heo873fe092013-04-14 20:15:26 -07001711 }
1712
Ben Blumcf5d5942010-03-10 15:22:09 -08001713 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001714 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715 }
1716
Paul Menagec6d57f32009-09-23 15:56:19 -07001717 kfree(opts.release_agent);
1718 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001719 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001721 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001722 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001723 mutex_unlock(&cgroup_root_mutex);
1724 mutex_unlock(&cgroup_mutex);
1725 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001727 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001728 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001729 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001730 out_err:
1731 kfree(opts.release_agent);
1732 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001733 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001734}
1735
1736static void cgroup_kill_sb(struct super_block *sb) {
1737 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001738 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001739 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740 int ret;
1741
1742 BUG_ON(!root);
1743
1744 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001745 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746
1747 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001748 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
1750 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001751 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1752 ret = rebind_subsystems(root, 0, root->subsys_mask);
1753 /* Shouldn't be able to fail ... */
1754 BUG_ON(ret);
1755 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756
Paul Menage817929e2007-10-18 23:39:36 -07001757 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001758 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001759 * root cgroup
1760 */
1761 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001762
Tejun Heo69d02062013-06-12 21:04:50 -07001763 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1764 list_del(&link->cset_link);
1765 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001766 kfree(link);
1767 }
1768 write_unlock(&css_set_lock);
1769
Paul Menage839ec542009-01-29 14:25:22 -08001770 if (!list_empty(&root->root_list)) {
1771 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001772 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001773 }
Li Zefane5f6a862009-01-07 18:07:41 -08001774
Tejun Heofa3ca072013-04-14 11:36:56 -07001775 cgroup_exit_root_id(root);
1776
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001777 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 mutex_unlock(&cgroup_mutex);
1779
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001780 simple_xattrs_free(&cgrp->xattrs);
1781
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001783 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784}
1785
1786static struct file_system_type cgroup_fs_type = {
1787 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001788 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 .kill_sb = cgroup_kill_sb,
1790};
1791
Greg KH676db4a2010-08-05 13:53:35 -07001792static struct kobject *cgroup_kobj;
1793
Li Zefana043e3b2008-02-23 15:24:09 -08001794/**
1795 * cgroup_path - generate the path of a cgroup
1796 * @cgrp: the cgroup in question
1797 * @buf: the buffer to write the path into
1798 * @buflen: the length of the buffer
1799 *
Li Zefan65dff752013-03-01 15:01:56 +08001800 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1801 *
1802 * We can't generate cgroup path using dentry->d_name, as accessing
1803 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1804 * inode's i_mutex, while on the other hand cgroup_path() can be called
1805 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001807int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808{
Li Zefan65dff752013-03-01 15:01:56 +08001809 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001811
Tejun Heoda1f2962013-04-14 10:32:19 -07001812 if (!cgrp->parent) {
1813 if (strlcpy(buf, "/", buflen) >= buflen)
1814 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 return 0;
1816 }
1817
Tao Ma316eb662012-11-08 21:36:38 +08001818 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001819 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001820
Li Zefan65dff752013-03-01 15:01:56 +08001821 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001822 do {
Li Zefan65dff752013-03-01 15:01:56 +08001823 const char *name = cgroup_name(cgrp);
1824 int len;
1825
1826 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001828 goto out;
1829 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001830
Paul Menageddbcc7e2007-10-18 23:39:30 -07001831 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001832 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001833 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001834
1835 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001836 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001837 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001839out:
1840 rcu_read_unlock();
1841 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001842}
Ben Blum67523c42010-03-10 15:22:11 -08001843EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001844
Tejun Heo857a2be2013-04-14 20:50:08 -07001845/**
1846 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1847 * @task: target task
1848 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1849 * @buf: the buffer to write the path into
1850 * @buflen: the length of the buffer
1851 *
1852 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1853 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1854 * be used inside locks used by cgroup controller callbacks.
1855 */
1856int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1857 char *buf, size_t buflen)
1858{
1859 struct cgroupfs_root *root;
1860 struct cgroup *cgrp = NULL;
1861 int ret = -ENOENT;
1862
1863 mutex_lock(&cgroup_mutex);
1864
1865 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1866 if (root) {
1867 cgrp = task_cgroup_from_root(task, root);
1868 ret = cgroup_path(cgrp, buf, buflen);
1869 }
1870
1871 mutex_unlock(&cgroup_mutex);
1872
1873 return ret;
1874}
1875EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1876
Ben Blum74a11662011-05-26 16:25:20 -07001877/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001878 * Control Group taskset
1879 */
Tejun Heo134d3372011-12-12 18:12:21 -08001880struct task_and_cgroup {
1881 struct task_struct *task;
1882 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001883 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001884};
1885
Tejun Heo2f7ee562011-12-12 18:12:21 -08001886struct cgroup_taskset {
1887 struct task_and_cgroup single;
1888 struct flex_array *tc_array;
1889 int tc_array_len;
1890 int idx;
1891 struct cgroup *cur_cgrp;
1892};
1893
1894/**
1895 * cgroup_taskset_first - reset taskset and return the first task
1896 * @tset: taskset of interest
1897 *
1898 * @tset iteration is initialized and the first task is returned.
1899 */
1900struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1901{
1902 if (tset->tc_array) {
1903 tset->idx = 0;
1904 return cgroup_taskset_next(tset);
1905 } else {
1906 tset->cur_cgrp = tset->single.cgrp;
1907 return tset->single.task;
1908 }
1909}
1910EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1911
1912/**
1913 * cgroup_taskset_next - iterate to the next task in taskset
1914 * @tset: taskset of interest
1915 *
1916 * Return the next task in @tset. Iteration must have been initialized
1917 * with cgroup_taskset_first().
1918 */
1919struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1920{
1921 struct task_and_cgroup *tc;
1922
1923 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1924 return NULL;
1925
1926 tc = flex_array_get(tset->tc_array, tset->idx++);
1927 tset->cur_cgrp = tc->cgrp;
1928 return tc->task;
1929}
1930EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1931
1932/**
1933 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1934 * @tset: taskset of interest
1935 *
1936 * Return the cgroup for the current (last returned) task of @tset. This
1937 * function must be preceded by either cgroup_taskset_first() or
1938 * cgroup_taskset_next().
1939 */
1940struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1941{
1942 return tset->cur_cgrp;
1943}
1944EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1945
1946/**
1947 * cgroup_taskset_size - return the number of tasks in taskset
1948 * @tset: taskset of interest
1949 */
1950int cgroup_taskset_size(struct cgroup_taskset *tset)
1951{
1952 return tset->tc_array ? tset->tc_array_len : 1;
1953}
1954EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1955
1956
Ben Blum74a11662011-05-26 16:25:20 -07001957/*
1958 * cgroup_task_migrate - move a task from one cgroup to another.
1959 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001960 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001961 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001962static void cgroup_task_migrate(struct cgroup *old_cgrp,
1963 struct task_struct *tsk,
1964 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001965{
Tejun Heo5abb8852013-06-12 21:04:49 -07001966 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001967
1968 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001969 * We are synchronized through threadgroup_lock() against PF_EXITING
1970 * setting such that we can't race against cgroup_exit() changing the
1971 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001973 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001974 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001975
Ben Blum74a11662011-05-26 16:25:20 -07001976 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001977 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001978 task_unlock(tsk);
1979
1980 /* Update the css_set linked lists if we're using them */
1981 write_lock(&css_set_lock);
1982 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001983 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001984 write_unlock(&css_set_lock);
1985
1986 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001987 * We just gained a reference on old_cset by taking it from the
1988 * task. As trading it for new_cset is protected by cgroup_mutex,
1989 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001990 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001991 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1992 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001993}
1994
Li Zefana043e3b2008-02-23 15:24:09 -08001995/**
Li Zefan081aa452013-03-13 09:17:09 +08001996 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001997 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001998 * @tsk: the task or the leader of the threadgroup to be attached
1999 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07002000 *
Tejun Heo257058a2011-12-12 18:12:21 -08002001 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08002002 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002003 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002004static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2005 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002006{
2007 int retval, i, group_size;
2008 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002009 struct cgroupfs_root *root = cgrp->root;
2010 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002011 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002012 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002013 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002014 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002015
2016 /*
2017 * step 0: in order to do expensive, possibly blocking operations for
2018 * every thread, we cannot iterate the thread group list, since it needs
2019 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002020 * group - group_rwsem prevents new threads from appearing, and if
2021 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002022 */
Li Zefan081aa452013-03-13 09:17:09 +08002023 if (threadgroup)
2024 group_size = get_nr_threads(tsk);
2025 else
2026 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002027 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002028 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002029 if (!group)
2030 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002031 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002032 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002033 if (retval)
2034 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002035
Ben Blum74a11662011-05-26 16:25:20 -07002036 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002037 /*
2038 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2039 * already PF_EXITING could be freed from underneath us unless we
2040 * take an rcu_read_lock.
2041 */
2042 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002043 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002044 struct task_and_cgroup ent;
2045
Tejun Heocd3d0952011-12-12 18:12:21 -08002046 /* @tsk either already exited or can't exit until the end */
2047 if (tsk->flags & PF_EXITING)
2048 continue;
2049
Ben Blum74a11662011-05-26 16:25:20 -07002050 /* as per above, nr_threads may decrease, but not increase. */
2051 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002052 ent.task = tsk;
2053 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002054 /* nothing to do if this task is already in the cgroup */
2055 if (ent.cgrp == cgrp)
2056 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002057 /*
2058 * saying GFP_ATOMIC has no effect here because we did prealloc
2059 * earlier, but it's good form to communicate our expectations.
2060 */
Tejun Heo134d3372011-12-12 18:12:21 -08002061 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002062 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002063 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002064
2065 if (!threadgroup)
2066 break;
Ben Blum74a11662011-05-26 16:25:20 -07002067 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002068 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002069 /* remember the number of threads in the array for later. */
2070 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002071 tset.tc_array = group;
2072 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002073
Tejun Heo134d3372011-12-12 18:12:21 -08002074 /* methods shouldn't be called if no task is actually migrating */
2075 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002076 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002077 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002078
Ben Blum74a11662011-05-26 16:25:20 -07002079 /*
2080 * step 1: check that we can legitimately attach to the cgroup.
2081 */
Tejun Heo5549c492013-06-24 15:21:48 -07002082 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002083 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002084 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002085 if (retval) {
2086 failed_ss = ss;
2087 goto out_cancel_attach;
2088 }
2089 }
Ben Blum74a11662011-05-26 16:25:20 -07002090 }
2091
2092 /*
2093 * step 2: make sure css_sets exist for all threads to be migrated.
2094 * we use find_css_set, which allocates a new one if necessary.
2095 */
Ben Blum74a11662011-05-26 16:25:20 -07002096 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002097 struct css_set *old_cset;
2098
Tejun Heo134d3372011-12-12 18:12:21 -08002099 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002100 old_cset = task_css_set(tc->task);
2101 tc->cg = find_css_set(old_cset, cgrp);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002102 if (!tc->cg) {
2103 retval = -ENOMEM;
2104 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002105 }
2106 }
2107
2108 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002109 * step 3: now that we're guaranteed success wrt the css_sets,
2110 * proceed to move all tasks to the new cgroup. There are no
2111 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002112 */
Ben Blum74a11662011-05-26 16:25:20 -07002113 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002114 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002115 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002116 }
2117 /* nothing is sensitive to fork() after this point. */
2118
2119 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002120 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002121 */
Tejun Heo5549c492013-06-24 15:21:48 -07002122 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002123 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002124 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002125 }
2126
2127 /*
2128 * step 5: success! and cleanup
2129 */
Ben Blum74a11662011-05-26 16:25:20 -07002130 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002131out_put_css_set_refs:
2132 if (retval) {
2133 for (i = 0; i < group_size; i++) {
2134 tc = flex_array_get(group, i);
2135 if (!tc->cg)
2136 break;
2137 put_css_set(tc->cg);
2138 }
Ben Blum74a11662011-05-26 16:25:20 -07002139 }
2140out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002141 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002142 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002143 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002144 break;
Ben Blum74a11662011-05-26 16:25:20 -07002145 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002146 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002147 }
2148 }
Ben Blum74a11662011-05-26 16:25:20 -07002149out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002150 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002151 return retval;
2152}
2153
2154/*
2155 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002156 * function to attach either it or all tasks in its threadgroup. Will lock
2157 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002158 */
2159static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002162 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002163 int ret;
2164
Ben Blum74a11662011-05-26 16:25:20 -07002165 if (!cgroup_lock_live_group(cgrp))
2166 return -ENODEV;
2167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168retry_find_task:
2169 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002171 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002172 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174 ret= -ESRCH;
2175 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 }
Ben Blum74a11662011-05-26 16:25:20 -07002177 /*
2178 * even if we're attaching all tasks in the thread group, we
2179 * only need to check permissions on one of them.
2180 */
David Howellsc69e8d92008-11-14 10:39:19 +11002181 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002182 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2183 !uid_eq(cred->euid, tcred->uid) &&
2184 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002185 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 ret = -EACCES;
2187 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189 } else
2190 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002191
2192 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002194
2195 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002196 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002197 * trapped in a cpuset, or RT worker may be born in a cgroup
2198 * with no rt_runtime allocated. Just say no.
2199 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002200 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002201 ret = -EINVAL;
2202 rcu_read_unlock();
2203 goto out_unlock_cgroup;
2204 }
2205
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 get_task_struct(tsk);
2207 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002208
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002209 threadgroup_lock(tsk);
2210 if (threadgroup) {
2211 if (!thread_group_leader(tsk)) {
2212 /*
2213 * a race with de_thread from another thread's exec()
2214 * may strip us of our leadership, if this happens,
2215 * there is no choice but to throw this task away and
2216 * try again; this is
2217 * "double-double-toil-and-trouble-check locking".
2218 */
2219 threadgroup_unlock(tsk);
2220 put_task_struct(tsk);
2221 goto retry_find_task;
2222 }
Li Zefan081aa452013-03-13 09:17:09 +08002223 }
2224
2225 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2226
Tejun Heocd3d0952011-12-12 18:12:21 -08002227 threadgroup_unlock(tsk);
2228
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002230out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002231 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002232 return ret;
2233}
2234
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002235/**
2236 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2237 * @from: attach to all cgroups of a given task
2238 * @tsk: the task to be attached
2239 */
2240int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2241{
2242 struct cgroupfs_root *root;
2243 int retval = 0;
2244
Tejun Heo47cfcd02013-04-07 09:29:51 -07002245 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002246 for_each_active_root(root) {
2247 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2248
2249 retval = cgroup_attach_task(from_cg, tsk, false);
2250 if (retval)
2251 break;
2252 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002254
2255 return retval;
2256}
2257EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2258
Paul Menageaf351022008-07-25 01:47:01 -07002259static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2260{
Ben Blum74a11662011-05-26 16:25:20 -07002261 return attach_task_by_pid(cgrp, pid, false);
2262}
2263
2264static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2265{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002266 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002267}
2268
Paul Menagee788e062008-07-25 01:46:59 -07002269static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2270 const char *buffer)
2271{
2272 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002273 if (strlen(buffer) >= PATH_MAX)
2274 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002275 if (!cgroup_lock_live_group(cgrp))
2276 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002277 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002278 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002279 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002280 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002281 return 0;
2282}
2283
2284static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2285 struct seq_file *seq)
2286{
2287 if (!cgroup_lock_live_group(cgrp))
2288 return -ENODEV;
2289 seq_puts(seq, cgrp->root->release_agent_path);
2290 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002291 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002292 return 0;
2293}
2294
Tejun Heo873fe092013-04-14 20:15:26 -07002295static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2296 struct seq_file *seq)
2297{
2298 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002299 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002300}
2301
Paul Menage84eea842008-07-25 01:47:00 -07002302/* A buffer size big enough for numbers or short strings */
2303#define CGROUP_LOCAL_BUFFER_SIZE 64
2304
Paul Menagee73d2c62008-04-29 01:00:06 -07002305static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002306 struct file *file,
2307 const char __user *userbuf,
2308 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002309{
Paul Menage84eea842008-07-25 01:47:00 -07002310 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002311 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002312 char *end;
2313
2314 if (!nbytes)
2315 return -EINVAL;
2316 if (nbytes >= sizeof(buffer))
2317 return -E2BIG;
2318 if (copy_from_user(buffer, userbuf, nbytes))
2319 return -EFAULT;
2320
2321 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002322 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002323 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002324 if (*end)
2325 return -EINVAL;
2326 retval = cft->write_u64(cgrp, cft, val);
2327 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002328 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (*end)
2330 return -EINVAL;
2331 retval = cft->write_s64(cgrp, cft, val);
2332 }
Paul Menage355e0c42007-10-18 23:39:33 -07002333 if (!retval)
2334 retval = nbytes;
2335 return retval;
2336}
2337
Paul Menagedb3b1492008-07-25 01:46:58 -07002338static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2339 struct file *file,
2340 const char __user *userbuf,
2341 size_t nbytes, loff_t *unused_ppos)
2342{
Paul Menage84eea842008-07-25 01:47:00 -07002343 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 int retval = 0;
2345 size_t max_bytes = cft->max_write_len;
2346 char *buffer = local_buffer;
2347
2348 if (!max_bytes)
2349 max_bytes = sizeof(local_buffer) - 1;
2350 if (nbytes >= max_bytes)
2351 return -E2BIG;
2352 /* Allocate a dynamic buffer if we need one */
2353 if (nbytes >= sizeof(local_buffer)) {
2354 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2355 if (buffer == NULL)
2356 return -ENOMEM;
2357 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002358 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2359 retval = -EFAULT;
2360 goto out;
2361 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002362
2363 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002364 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002365 if (!retval)
2366 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002367out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002368 if (buffer != local_buffer)
2369 kfree(buffer);
2370 return retval;
2371}
2372
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2374 size_t nbytes, loff_t *ppos)
2375{
2376 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002377 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378
Tejun Heo54766d42013-06-12 21:04:53 -07002379 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002381 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002383 if (cft->write_u64 || cft->write_s64)
2384 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002385 if (cft->write_string)
2386 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002387 if (cft->trigger) {
2388 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2389 return ret ? ret : nbytes;
2390 }
Paul Menage355e0c42007-10-18 23:39:33 -07002391 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392}
2393
Paul Menagef4c753b2008-04-29 00:59:56 -07002394static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2395 struct file *file,
2396 char __user *buf, size_t nbytes,
2397 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398{
Paul Menage84eea842008-07-25 01:47:00 -07002399 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002400 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2402
2403 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2404}
2405
Paul Menagee73d2c62008-04-29 01:00:06 -07002406static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2407 struct file *file,
2408 char __user *buf, size_t nbytes,
2409 loff_t *ppos)
2410{
Paul Menage84eea842008-07-25 01:47:00 -07002411 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002412 s64 val = cft->read_s64(cgrp, cft);
2413 int len = sprintf(tmp, "%lld\n", (long long) val);
2414
2415 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2416}
2417
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2419 size_t nbytes, loff_t *ppos)
2420{
2421 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002422 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423
Tejun Heo54766d42013-06-12 21:04:53 -07002424 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425 return -ENODEV;
2426
2427 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002428 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002429 if (cft->read_u64)
2430 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002431 if (cft->read_s64)
2432 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002433 return -EINVAL;
2434}
2435
Paul Menage91796562008-04-29 01:00:01 -07002436/*
2437 * seqfile ops/methods for returning structured data. Currently just
2438 * supports string->u64 maps, but can be extended in future.
2439 */
2440
2441struct cgroup_seqfile_state {
2442 struct cftype *cft;
2443 struct cgroup *cgroup;
2444};
2445
2446static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2447{
2448 struct seq_file *sf = cb->state;
2449 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2450}
2451
2452static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2453{
2454 struct cgroup_seqfile_state *state = m->private;
2455 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002456 if (cft->read_map) {
2457 struct cgroup_map_cb cb = {
2458 .fill = cgroup_map_add,
2459 .state = m,
2460 };
2461 return cft->read_map(state->cgroup, cft, &cb);
2462 }
2463 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002464}
2465
Adrian Bunk96930a62008-07-25 19:46:21 -07002466static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002467{
2468 struct seq_file *seq = file->private_data;
2469 kfree(seq->private);
2470 return single_release(inode, file);
2471}
2472
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002473static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002474 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002475 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002476 .llseek = seq_lseek,
2477 .release = cgroup_seqfile_release,
2478};
2479
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480static int cgroup_file_open(struct inode *inode, struct file *file)
2481{
2482 int err;
2483 struct cftype *cft;
2484
2485 err = generic_file_open(inode, file);
2486 if (err)
2487 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002489
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002490 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002491 struct cgroup_seqfile_state *state;
2492
2493 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002494 if (!state)
2495 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002496
Paul Menage91796562008-04-29 01:00:01 -07002497 state->cft = cft;
2498 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2499 file->f_op = &cgroup_seqfile_operations;
2500 err = single_open(file, cgroup_seqfile_show, state);
2501 if (err < 0)
2502 kfree(state);
2503 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504 err = cft->open(inode, file);
2505 else
2506 err = 0;
2507
2508 return err;
2509}
2510
2511static int cgroup_file_release(struct inode *inode, struct file *file)
2512{
2513 struct cftype *cft = __d_cft(file->f_dentry);
2514 if (cft->release)
2515 return cft->release(inode, file);
2516 return 0;
2517}
2518
2519/*
2520 * cgroup_rename - Only allow simple rename of directories in place.
2521 */
2522static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2523 struct inode *new_dir, struct dentry *new_dentry)
2524{
Li Zefan65dff752013-03-01 15:01:56 +08002525 int ret;
2526 struct cgroup_name *name, *old_name;
2527 struct cgroup *cgrp;
2528
2529 /*
2530 * It's convinient to use parent dir's i_mutex to protected
2531 * cgrp->name.
2532 */
2533 lockdep_assert_held(&old_dir->i_mutex);
2534
Paul Menageddbcc7e2007-10-18 23:39:30 -07002535 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2536 return -ENOTDIR;
2537 if (new_dentry->d_inode)
2538 return -EEXIST;
2539 if (old_dir != new_dir)
2540 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002541
2542 cgrp = __d_cgrp(old_dentry);
2543
Tejun Heo6db8e852013-06-14 11:18:22 -07002544 /*
2545 * This isn't a proper migration and its usefulness is very
2546 * limited. Disallow if sane_behavior.
2547 */
2548 if (cgroup_sane_behavior(cgrp))
2549 return -EPERM;
2550
Li Zefan65dff752013-03-01 15:01:56 +08002551 name = cgroup_alloc_name(new_dentry);
2552 if (!name)
2553 return -ENOMEM;
2554
2555 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2556 if (ret) {
2557 kfree(name);
2558 return ret;
2559 }
2560
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002561 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002562 rcu_assign_pointer(cgrp->name, name);
2563
2564 kfree_rcu(old_name, rcu_head);
2565 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002566}
2567
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002568static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2569{
2570 if (S_ISDIR(dentry->d_inode->i_mode))
2571 return &__d_cgrp(dentry)->xattrs;
2572 else
Li Zefan712317a2013-04-18 23:09:52 -07002573 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002574}
2575
2576static inline int xattr_enabled(struct dentry *dentry)
2577{
2578 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002579 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002580}
2581
2582static bool is_valid_xattr(const char *name)
2583{
2584 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2585 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2586 return true;
2587 return false;
2588}
2589
2590static int cgroup_setxattr(struct dentry *dentry, const char *name,
2591 const void *val, size_t size, int flags)
2592{
2593 if (!xattr_enabled(dentry))
2594 return -EOPNOTSUPP;
2595 if (!is_valid_xattr(name))
2596 return -EINVAL;
2597 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2598}
2599
2600static int cgroup_removexattr(struct dentry *dentry, const char *name)
2601{
2602 if (!xattr_enabled(dentry))
2603 return -EOPNOTSUPP;
2604 if (!is_valid_xattr(name))
2605 return -EINVAL;
2606 return simple_xattr_remove(__d_xattrs(dentry), name);
2607}
2608
2609static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2610 void *buf, size_t size)
2611{
2612 if (!xattr_enabled(dentry))
2613 return -EOPNOTSUPP;
2614 if (!is_valid_xattr(name))
2615 return -EINVAL;
2616 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2617}
2618
2619static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2620{
2621 if (!xattr_enabled(dentry))
2622 return -EOPNOTSUPP;
2623 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2624}
2625
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002626static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002627 .read = cgroup_file_read,
2628 .write = cgroup_file_write,
2629 .llseek = generic_file_llseek,
2630 .open = cgroup_file_open,
2631 .release = cgroup_file_release,
2632};
2633
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002634static const struct inode_operations cgroup_file_inode_operations = {
2635 .setxattr = cgroup_setxattr,
2636 .getxattr = cgroup_getxattr,
2637 .listxattr = cgroup_listxattr,
2638 .removexattr = cgroup_removexattr,
2639};
2640
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002641static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002642 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002643 .mkdir = cgroup_mkdir,
2644 .rmdir = cgroup_rmdir,
2645 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002646 .setxattr = cgroup_setxattr,
2647 .getxattr = cgroup_getxattr,
2648 .listxattr = cgroup_listxattr,
2649 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002650};
2651
Al Viro00cd8dd2012-06-10 17:13:09 -04002652static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002653{
2654 if (dentry->d_name.len > NAME_MAX)
2655 return ERR_PTR(-ENAMETOOLONG);
2656 d_add(dentry, NULL);
2657 return NULL;
2658}
2659
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002660/*
2661 * Check if a file is a control file
2662 */
2663static inline struct cftype *__file_cft(struct file *file)
2664{
Al Viro496ad9a2013-01-23 17:07:38 -05002665 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002666 return ERR_PTR(-EINVAL);
2667 return __d_cft(file->f_dentry);
2668}
2669
Al Viroa5e7ed32011-07-26 01:55:55 -04002670static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002671 struct super_block *sb)
2672{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 struct inode *inode;
2674
2675 if (!dentry)
2676 return -ENOENT;
2677 if (dentry->d_inode)
2678 return -EEXIST;
2679
2680 inode = cgroup_new_inode(mode, sb);
2681 if (!inode)
2682 return -ENOMEM;
2683
2684 if (S_ISDIR(mode)) {
2685 inode->i_op = &cgroup_dir_inode_operations;
2686 inode->i_fop = &simple_dir_operations;
2687
2688 /* start off with i_nlink == 2 (for "." entry) */
2689 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002690 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691
Tejun Heob8a2df62012-11-19 08:13:37 -08002692 /*
2693 * Control reaches here with cgroup_mutex held.
2694 * @inode->i_mutex should nest outside cgroup_mutex but we
2695 * want to populate it immediately without releasing
2696 * cgroup_mutex. As @inode isn't visible to anyone else
2697 * yet, trylock will always succeed without affecting
2698 * lockdep checks.
2699 */
2700 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 } else if (S_ISREG(mode)) {
2702 inode->i_size = 0;
2703 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002704 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 d_instantiate(dentry, inode);
2707 dget(dentry); /* Extra count - pin the dentry in core */
2708 return 0;
2709}
2710
Li Zefan099fca32009-04-02 16:57:29 -07002711/**
2712 * cgroup_file_mode - deduce file mode of a control file
2713 * @cft: the control file in question
2714 *
2715 * returns cft->mode if ->mode is not 0
2716 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2717 * returns S_IRUGO if it has only a read handler
2718 * returns S_IWUSR if it has only a write hander
2719 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002720static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002721{
Al Viroa5e7ed32011-07-26 01:55:55 -04002722 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002723
2724 if (cft->mode)
2725 return cft->mode;
2726
2727 if (cft->read || cft->read_u64 || cft->read_s64 ||
2728 cft->read_map || cft->read_seq_string)
2729 mode |= S_IRUGO;
2730
2731 if (cft->write || cft->write_u64 || cft->write_s64 ||
2732 cft->write_string || cft->trigger)
2733 mode |= S_IWUSR;
2734
2735 return mode;
2736}
2737
Tejun Heodb0416b2012-04-01 12:09:55 -07002738static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002739 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740{
Paul Menagebd89aab2007-10-18 23:40:44 -07002741 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002742 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002746 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002748
Tejun Heo93438622013-04-14 20:15:25 -07002749 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 strcpy(name, subsys->name);
2751 strcat(name, ".");
2752 }
2753 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002754
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002756
2757 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2758 if (!cfe)
2759 return -ENOMEM;
2760
Paul Menageddbcc7e2007-10-18 23:39:30 -07002761 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002762 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002764 goto out;
2765 }
2766
Li Zefand6cbf352013-05-14 19:44:20 +08002767 cfe->type = (void *)cft;
2768 cfe->dentry = dentry;
2769 dentry->d_fsdata = cfe;
2770 simple_xattrs_init(&cfe->xattrs);
2771
Tejun Heo05ef1d72012-04-01 12:09:56 -07002772 mode = cgroup_file_mode(cft);
2773 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2774 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775 list_add_tail(&cfe->node, &parent->files);
2776 cfe = NULL;
2777 }
2778 dput(dentry);
2779out:
2780 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 return error;
2782}
2783
Tejun Heo79578622012-04-01 12:09:56 -07002784static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002785 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002787 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 int err, ret = 0;
2789
2790 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002791 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002792 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2793 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002794 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2795 continue;
2796 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2797 continue;
2798
Li Zefan2739d3c2013-01-21 18:18:33 +08002799 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002800 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002801 if (err)
2802 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2803 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002804 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002805 } else {
2806 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002807 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002808 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002809 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002810}
2811
Tejun Heo8e3f6542012-04-01 12:09:55 -07002812static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002813 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002814{
2815 /*
2816 * Thanks to the entanglement with vfs inode locking, we can't walk
2817 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002818 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2819 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002820 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002821 mutex_lock(&cgroup_mutex);
2822}
2823
2824static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002825 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002826 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002827{
2828 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002829 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002830 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002831 struct dentry *prev = NULL;
2832 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002833 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834
2835 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002836 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002837 !atomic_inc_not_zero(&sb->s_active)) {
2838 mutex_unlock(&cgroup_mutex);
2839 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840 }
2841
Li Zefane8c82d22013-06-18 18:48:37 +08002842 /*
2843 * All cgroups which are created after we drop cgroup_mutex will
2844 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002845 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002846 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002847 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002848
Tejun Heo8e3f6542012-04-01 12:09:55 -07002849 mutex_unlock(&cgroup_mutex);
2850
Li Zefane8c82d22013-06-18 18:48:37 +08002851 /* @root always needs to be updated */
2852 inode = root->dentry->d_inode;
2853 mutex_lock(&inode->i_mutex);
2854 mutex_lock(&cgroup_mutex);
2855 cgroup_addrm_files(root, ss, cfts, is_add);
2856 mutex_unlock(&cgroup_mutex);
2857 mutex_unlock(&inode->i_mutex);
2858
2859 /* add/rm files for all cgroups created before */
2860 rcu_read_lock();
2861 cgroup_for_each_descendant_pre(cgrp, root) {
2862 if (cgroup_is_dead(cgrp))
2863 continue;
2864
2865 inode = cgrp->dentry->d_inode;
2866 dget(cgrp->dentry);
2867 rcu_read_unlock();
2868
2869 dput(prev);
2870 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871
2872 mutex_lock(&inode->i_mutex);
2873 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002874 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002875 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002876 mutex_unlock(&cgroup_mutex);
2877 mutex_unlock(&inode->i_mutex);
2878
Li Zefane8c82d22013-06-18 18:48:37 +08002879 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002880 }
Li Zefane8c82d22013-06-18 18:48:37 +08002881 rcu_read_unlock();
2882 dput(prev);
2883 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002884}
2885
2886/**
2887 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2888 * @ss: target cgroup subsystem
2889 * @cfts: zero-length name terminated array of cftypes
2890 *
2891 * Register @cfts to @ss. Files described by @cfts are created for all
2892 * existing cgroups to which @ss is attached and all future cgroups will
2893 * have them too. This function can be called anytime whether @ss is
2894 * attached or not.
2895 *
2896 * Returns 0 on successful registration, -errno on failure. Note that this
2897 * function currently returns 0 as long as @cfts registration is successful
2898 * even if some file creation attempts on existing cgroups fail.
2899 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002900int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901{
2902 struct cftype_set *set;
2903
2904 set = kzalloc(sizeof(*set), GFP_KERNEL);
2905 if (!set)
2906 return -ENOMEM;
2907
2908 cgroup_cfts_prepare();
2909 set->cfts = cfts;
2910 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002911 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002912
2913 return 0;
2914}
2915EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2916
Li Zefana043e3b2008-02-23 15:24:09 -08002917/**
Tejun Heo79578622012-04-01 12:09:56 -07002918 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2919 * @ss: target cgroup subsystem
2920 * @cfts: zero-length name terminated array of cftypes
2921 *
2922 * Unregister @cfts from @ss. Files described by @cfts are removed from
2923 * all existing cgroups to which @ss is attached and all future cgroups
2924 * won't have them either. This function can be called anytime whether @ss
2925 * is attached or not.
2926 *
2927 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2928 * registered with @ss.
2929 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002930int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002931{
2932 struct cftype_set *set;
2933
2934 cgroup_cfts_prepare();
2935
2936 list_for_each_entry(set, &ss->cftsets, node) {
2937 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002938 list_del(&set->node);
2939 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002940 cgroup_cfts_commit(ss, cfts, false);
2941 return 0;
2942 }
2943 }
2944
2945 cgroup_cfts_commit(ss, NULL, false);
2946 return -ENOENT;
2947}
2948
2949/**
Li Zefana043e3b2008-02-23 15:24:09 -08002950 * cgroup_task_count - count the number of tasks in a cgroup.
2951 * @cgrp: the cgroup in question
2952 *
2953 * Return the number of tasks in the cgroup.
2954 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002955int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002956{
2957 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002958 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002959
Paul Menage817929e2007-10-18 23:39:36 -07002960 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002961 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2962 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002963 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002964 return count;
2965}
2966
2967/*
Paul Menage817929e2007-10-18 23:39:36 -07002968 * Advance a list_head iterator. The iterator should be positioned at
2969 * the start of a css_set
2970 */
Tejun Heo69d02062013-06-12 21:04:50 -07002971static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002972{
Tejun Heo69d02062013-06-12 21:04:50 -07002973 struct list_head *l = it->cset_link;
2974 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002975 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002976
2977 /* Advance to the next non-empty css_set */
2978 do {
2979 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002980 if (l == &cgrp->cset_links) {
2981 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002982 return;
2983 }
Tejun Heo69d02062013-06-12 21:04:50 -07002984 link = list_entry(l, struct cgrp_cset_link, cset_link);
2985 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002986 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002987 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002988 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002989}
2990
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991/*
2992 * To reduce the fork() overhead for systems that are not actually
2993 * using their cgroups capability, we don't maintain the lists running
2994 * through each css_set to its tasks until we see the list actually
2995 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002996 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002997static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002998{
2999 struct task_struct *p, *g;
3000 write_lock(&css_set_lock);
3001 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003002 /*
3003 * We need tasklist_lock because RCU is not safe against
3004 * while_each_thread(). Besides, a forking task that has passed
3005 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3006 * is not guaranteed to have its child immediately visible in the
3007 * tasklist if we walk through it with RCU.
3008 */
3009 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010 do_each_thread(g, p) {
3011 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003012 /*
3013 * We should check if the process is exiting, otherwise
3014 * it will race with cgroup_exit() in that the list
3015 * entry won't be deleted though the process has exited.
3016 */
3017 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003018 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 task_unlock(p);
3020 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003021 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003022 write_unlock(&css_set_lock);
3023}
3024
Tejun Heo574bd9f2012-11-09 09:12:29 -08003025/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003026 * cgroup_next_sibling - find the next sibling of a given cgroup
3027 * @pos: the current cgroup
3028 *
3029 * This function returns the next sibling of @pos and should be called
3030 * under RCU read lock. The only requirement is that @pos is accessible.
3031 * The next sibling is guaranteed to be returned regardless of @pos's
3032 * state.
3033 */
3034struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3035{
3036 struct cgroup *next;
3037
3038 WARN_ON_ONCE(!rcu_read_lock_held());
3039
3040 /*
3041 * @pos could already have been removed. Once a cgroup is removed,
3042 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003043 * changes. As CGRP_DEAD assertion is serialized and happens
3044 * before the cgroup is taken off the ->sibling list, if we see it
3045 * unasserted, it's guaranteed that the next sibling hasn't
3046 * finished its grace period even if it's already removed, and thus
3047 * safe to dereference from this RCU critical section. If
3048 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3049 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003050 */
Tejun Heo54766d42013-06-12 21:04:53 -07003051 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003052 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3053 if (&next->sibling != &pos->parent->children)
3054 return next;
3055 return NULL;
3056 }
3057
3058 /*
3059 * Can't dereference the next pointer. Each cgroup is given a
3060 * monotonically increasing unique serial number and always
3061 * appended to the sibling list, so the next one can be found by
3062 * walking the parent's children until we see a cgroup with higher
3063 * serial number than @pos's.
3064 *
3065 * While this path can be slow, it's taken only when either the
3066 * current cgroup is removed or iteration and removal race.
3067 */
3068 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3069 if (next->serial_nr > pos->serial_nr)
3070 return next;
3071 return NULL;
3072}
3073EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3074
3075/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003076 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3077 * @pos: the current position (%NULL to initiate traversal)
3078 * @cgroup: cgroup whose descendants to walk
3079 *
3080 * To be used by cgroup_for_each_descendant_pre(). Find the next
3081 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003082 *
3083 * While this function requires RCU read locking, it doesn't require the
3084 * whole traversal to be contained in a single RCU critical section. This
3085 * function will return the correct next descendant as long as both @pos
3086 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003087 */
3088struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3089 struct cgroup *cgroup)
3090{
3091 struct cgroup *next;
3092
3093 WARN_ON_ONCE(!rcu_read_lock_held());
3094
3095 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003096 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003098
3099 /* visit the first child if exists */
3100 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3101 if (next)
3102 return next;
3103
3104 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003105 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003106 next = cgroup_next_sibling(pos);
3107 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003110 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003111
3112 return NULL;
3113}
3114EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3115
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003116/**
3117 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3118 * @pos: cgroup of interest
3119 *
3120 * Return the rightmost descendant of @pos. If there's no descendant,
3121 * @pos is returned. This can be used during pre-order traversal to skip
3122 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003123 *
3124 * While this function requires RCU read locking, it doesn't require the
3125 * whole traversal to be contained in a single RCU critical section. This
3126 * function will return the correct rightmost descendant as long as @pos is
3127 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003128 */
3129struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3130{
3131 struct cgroup *last, *tmp;
3132
3133 WARN_ON_ONCE(!rcu_read_lock_held());
3134
3135 do {
3136 last = pos;
3137 /* ->prev isn't RCU safe, walk ->next till the end */
3138 pos = NULL;
3139 list_for_each_entry_rcu(tmp, &last->children, sibling)
3140 pos = tmp;
3141 } while (pos);
3142
3143 return last;
3144}
3145EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3146
Tejun Heo574bd9f2012-11-09 09:12:29 -08003147static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3148{
3149 struct cgroup *last;
3150
3151 do {
3152 last = pos;
3153 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3154 sibling);
3155 } while (pos);
3156
3157 return last;
3158}
3159
3160/**
3161 * cgroup_next_descendant_post - find the next descendant for post-order walk
3162 * @pos: the current position (%NULL to initiate traversal)
3163 * @cgroup: cgroup whose descendants to walk
3164 *
3165 * To be used by cgroup_for_each_descendant_post(). Find the next
3166 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003167 *
3168 * While this function requires RCU read locking, it doesn't require the
3169 * whole traversal to be contained in a single RCU critical section. This
3170 * function will return the correct next descendant as long as both @pos
3171 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 */
3173struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3174 struct cgroup *cgroup)
3175{
3176 struct cgroup *next;
3177
3178 WARN_ON_ONCE(!rcu_read_lock_held());
3179
3180 /* if first iteration, visit the leftmost descendant */
3181 if (!pos) {
3182 next = cgroup_leftmost_descendant(cgroup);
3183 return next != cgroup ? next : NULL;
3184 }
3185
3186 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003187 next = cgroup_next_sibling(pos);
3188 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189 return cgroup_leftmost_descendant(next);
3190
3191 /* no sibling left, visit parent */
3192 next = pos->parent;
3193 return next != cgroup ? next : NULL;
3194}
3195EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3196
Paul Menagebd89aab2007-10-18 23:40:44 -07003197void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003198 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003199{
3200 /*
3201 * The first time anyone tries to iterate across a cgroup,
3202 * we need to enable the list linking each css_set to its
3203 * tasks, and fix up all existing tasks.
3204 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003205 if (!use_task_css_set_links)
3206 cgroup_enable_task_cg_lists();
3207
Paul Menage817929e2007-10-18 23:39:36 -07003208 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003209 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003210 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003211}
3212
Paul Menagebd89aab2007-10-18 23:40:44 -07003213struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003214 struct cgroup_iter *it)
3215{
3216 struct task_struct *res;
3217 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003218 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003219
3220 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003221 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003222 return NULL;
3223 res = list_entry(l, struct task_struct, cg_list);
3224 /* Advance iterator to find next entry */
3225 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003226 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3227 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003228 /* We reached the end of this task list - move on to
3229 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003230 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003231 } else {
3232 it->task = l;
3233 }
3234 return res;
3235}
3236
Paul Menagebd89aab2007-10-18 23:40:44 -07003237void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003238 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003239{
3240 read_unlock(&css_set_lock);
3241}
3242
Cliff Wickman31a7df02008-02-07 00:14:42 -08003243static inline int started_after_time(struct task_struct *t1,
3244 struct timespec *time,
3245 struct task_struct *t2)
3246{
3247 int start_diff = timespec_compare(&t1->start_time, time);
3248 if (start_diff > 0) {
3249 return 1;
3250 } else if (start_diff < 0) {
3251 return 0;
3252 } else {
3253 /*
3254 * Arbitrarily, if two processes started at the same
3255 * time, we'll say that the lower pointer value
3256 * started first. Note that t2 may have exited by now
3257 * so this may not be a valid pointer any longer, but
3258 * that's fine - it still serves to distinguish
3259 * between two tasks started (effectively) simultaneously.
3260 */
3261 return t1 > t2;
3262 }
3263}
3264
3265/*
3266 * This function is a callback from heap_insert() and is used to order
3267 * the heap.
3268 * In this case we order the heap in descending task start time.
3269 */
3270static inline int started_after(void *p1, void *p2)
3271{
3272 struct task_struct *t1 = p1;
3273 struct task_struct *t2 = p2;
3274 return started_after_time(t1, &t2->start_time, t2);
3275}
3276
3277/**
3278 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3279 * @scan: struct cgroup_scanner containing arguments for the scan
3280 *
3281 * Arguments include pointers to callback functions test_task() and
3282 * process_task().
3283 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3284 * and if it returns true, call process_task() for it also.
3285 * The test_task pointer may be NULL, meaning always true (select all tasks).
3286 * Effectively duplicates cgroup_iter_{start,next,end}()
3287 * but does not lock css_set_lock for the call to process_task().
3288 * The struct cgroup_scanner may be embedded in any structure of the caller's
3289 * creation.
3290 * It is guaranteed that process_task() will act on every task that
3291 * is a member of the cgroup for the duration of this call. This
3292 * function may or may not call process_task() for tasks that exit
3293 * or move to a different cgroup during the call, or are forked or
3294 * move into the cgroup during the call.
3295 *
3296 * Note that test_task() may be called with locks held, and may in some
3297 * situations be called multiple times for the same task, so it should
3298 * be cheap.
3299 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3300 * pre-allocated and will be used for heap operations (and its "gt" member will
3301 * be overwritten), else a temporary heap will be used (allocation of which
3302 * may cause this function to fail).
3303 */
3304int cgroup_scan_tasks(struct cgroup_scanner *scan)
3305{
3306 int retval, i;
3307 struct cgroup_iter it;
3308 struct task_struct *p, *dropped;
3309 /* Never dereference latest_task, since it's not refcounted */
3310 struct task_struct *latest_task = NULL;
3311 struct ptr_heap tmp_heap;
3312 struct ptr_heap *heap;
3313 struct timespec latest_time = { 0, 0 };
3314
3315 if (scan->heap) {
3316 /* The caller supplied our heap and pre-allocated its memory */
3317 heap = scan->heap;
3318 heap->gt = &started_after;
3319 } else {
3320 /* We need to allocate our own heap memory */
3321 heap = &tmp_heap;
3322 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3323 if (retval)
3324 /* cannot allocate the heap */
3325 return retval;
3326 }
3327
3328 again:
3329 /*
3330 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3331 * to determine which are of interest, and using the scanner's
3332 * "process_task" callback to process any of them that need an update.
3333 * Since we don't want to hold any locks during the task updates,
3334 * gather tasks to be processed in a heap structure.
3335 * The heap is sorted by descending task start time.
3336 * If the statically-sized heap fills up, we overflow tasks that
3337 * started later, and in future iterations only consider tasks that
3338 * started after the latest task in the previous pass. This
3339 * guarantees forward progress and that we don't miss any tasks.
3340 */
3341 heap->size = 0;
3342 cgroup_iter_start(scan->cg, &it);
3343 while ((p = cgroup_iter_next(scan->cg, &it))) {
3344 /*
3345 * Only affect tasks that qualify per the caller's callback,
3346 * if he provided one
3347 */
3348 if (scan->test_task && !scan->test_task(p, scan))
3349 continue;
3350 /*
3351 * Only process tasks that started after the last task
3352 * we processed
3353 */
3354 if (!started_after_time(p, &latest_time, latest_task))
3355 continue;
3356 dropped = heap_insert(heap, p);
3357 if (dropped == NULL) {
3358 /*
3359 * The new task was inserted; the heap wasn't
3360 * previously full
3361 */
3362 get_task_struct(p);
3363 } else if (dropped != p) {
3364 /*
3365 * The new task was inserted, and pushed out a
3366 * different task
3367 */
3368 get_task_struct(p);
3369 put_task_struct(dropped);
3370 }
3371 /*
3372 * Else the new task was newer than anything already in
3373 * the heap and wasn't inserted
3374 */
3375 }
3376 cgroup_iter_end(scan->cg, &it);
3377
3378 if (heap->size) {
3379 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003380 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003381 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003382 latest_time = q->start_time;
3383 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003384 }
3385 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003386 scan->process_task(q, scan);
3387 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003388 }
3389 /*
3390 * If we had to process any tasks at all, scan again
3391 * in case some of them were in the middle of forking
3392 * children that didn't get processed.
3393 * Not the most efficient way to do it, but it avoids
3394 * having to take callback_mutex in the fork path
3395 */
3396 goto again;
3397 }
3398 if (heap == &tmp_heap)
3399 heap_free(&tmp_heap);
3400 return 0;
3401}
3402
Tejun Heo8cc99342013-04-07 09:29:50 -07003403static void cgroup_transfer_one_task(struct task_struct *task,
3404 struct cgroup_scanner *scan)
3405{
3406 struct cgroup *new_cgroup = scan->data;
3407
Tejun Heo47cfcd02013-04-07 09:29:51 -07003408 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003409 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003410 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003411}
3412
3413/**
3414 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3415 * @to: cgroup to which the tasks will be moved
3416 * @from: cgroup in which the tasks currently reside
3417 */
3418int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3419{
3420 struct cgroup_scanner scan;
3421
3422 scan.cg = from;
3423 scan.test_task = NULL; /* select all tasks in cgroup */
3424 scan.process_task = cgroup_transfer_one_task;
3425 scan.heap = NULL;
3426 scan.data = to;
3427
3428 return cgroup_scan_tasks(&scan);
3429}
3430
Paul Menage817929e2007-10-18 23:39:36 -07003431/*
Ben Blum102a7752009-09-23 15:56:26 -07003432 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003433 *
3434 * Reading this file can return large amounts of data if a cgroup has
3435 * *lots* of attached tasks. So it may need several calls to read(),
3436 * but we cannot guarantee that the information we produce is correct
3437 * unless we produce it entirely atomically.
3438 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003439 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003440
Li Zefan24528252012-01-20 11:58:43 +08003441/* which pidlist file are we talking about? */
3442enum cgroup_filetype {
3443 CGROUP_FILE_PROCS,
3444 CGROUP_FILE_TASKS,
3445};
3446
3447/*
3448 * A pidlist is a list of pids that virtually represents the contents of one
3449 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3450 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3451 * to the cgroup.
3452 */
3453struct cgroup_pidlist {
3454 /*
3455 * used to find which pidlist is wanted. doesn't change as long as
3456 * this particular list stays in the list.
3457 */
3458 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3459 /* array of xids */
3460 pid_t *list;
3461 /* how many elements the above list has */
3462 int length;
3463 /* how many files are using the current array */
3464 int use_count;
3465 /* each of these stored in a list by its cgroup */
3466 struct list_head links;
3467 /* pointer to the cgroup we belong to, for list removal purposes */
3468 struct cgroup *owner;
3469 /* protects the other fields */
3470 struct rw_semaphore mutex;
3471};
3472
Paul Menagebbcb81d2007-10-18 23:39:32 -07003473/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003474 * The following two functions "fix" the issue where there are more pids
3475 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3476 * TODO: replace with a kernel-wide solution to this problem
3477 */
3478#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3479static void *pidlist_allocate(int count)
3480{
3481 if (PIDLIST_TOO_LARGE(count))
3482 return vmalloc(count * sizeof(pid_t));
3483 else
3484 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3485}
3486static void pidlist_free(void *p)
3487{
3488 if (is_vmalloc_addr(p))
3489 vfree(p);
3490 else
3491 kfree(p);
3492}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003493
3494/*
Ben Blum102a7752009-09-23 15:56:26 -07003495 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003496 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003497 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003498static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003499{
Ben Blum102a7752009-09-23 15:56:26 -07003500 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003501
3502 /*
3503 * we presume the 0th element is unique, so i starts at 1. trivial
3504 * edge cases first; no work needs to be done for either
3505 */
3506 if (length == 0 || length == 1)
3507 return length;
3508 /* src and dest walk down the list; dest counts unique elements */
3509 for (src = 1; src < length; src++) {
3510 /* find next unique element */
3511 while (list[src] == list[src-1]) {
3512 src++;
3513 if (src == length)
3514 goto after;
3515 }
3516 /* dest always points to where the next unique element goes */
3517 list[dest] = list[src];
3518 dest++;
3519 }
3520after:
Ben Blum102a7752009-09-23 15:56:26 -07003521 return dest;
3522}
3523
3524static int cmppid(const void *a, const void *b)
3525{
3526 return *(pid_t *)a - *(pid_t *)b;
3527}
3528
3529/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 * find the appropriate pidlist for our purpose (given procs vs tasks)
3531 * returns with the lock on that pidlist already held, and takes care
3532 * of the use count, or returns NULL with no locks held if we're out of
3533 * memory.
3534 */
3535static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3536 enum cgroup_filetype type)
3537{
3538 struct cgroup_pidlist *l;
3539 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003540 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003541
Ben Blum72a8cb32009-09-23 15:56:27 -07003542 /*
3543 * We can't drop the pidlist_mutex before taking the l->mutex in case
3544 * the last ref-holder is trying to remove l from the list at the same
3545 * time. Holding the pidlist_mutex precludes somebody taking whichever
3546 * list we find out from under us - compare release_pid_array().
3547 */
3548 mutex_lock(&cgrp->pidlist_mutex);
3549 list_for_each_entry(l, &cgrp->pidlists, links) {
3550 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003551 /* make sure l doesn't vanish out from under us */
3552 down_write(&l->mutex);
3553 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003554 return l;
3555 }
3556 }
3557 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003558 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003559 if (!l) {
3560 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003561 return l;
3562 }
3563 init_rwsem(&l->mutex);
3564 down_write(&l->mutex);
3565 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003566 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003567 l->owner = cgrp;
3568 list_add(&l->links, &cgrp->pidlists);
3569 mutex_unlock(&cgrp->pidlist_mutex);
3570 return l;
3571}
3572
3573/*
Ben Blum102a7752009-09-23 15:56:26 -07003574 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3575 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003576static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3577 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003578{
3579 pid_t *array;
3580 int length;
3581 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003582 struct cgroup_iter it;
3583 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003584 struct cgroup_pidlist *l;
3585
3586 /*
3587 * If cgroup gets more users after we read count, we won't have
3588 * enough space - tough. This race is indistinguishable to the
3589 * caller from the case that the additional cgroup users didn't
3590 * show up until sometime later on.
3591 */
3592 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003593 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003594 if (!array)
3595 return -ENOMEM;
3596 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003597 cgroup_iter_start(cgrp, &it);
3598 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003599 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003600 break;
Ben Blum102a7752009-09-23 15:56:26 -07003601 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 if (type == CGROUP_FILE_PROCS)
3603 pid = task_tgid_vnr(tsk);
3604 else
3605 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003606 if (pid > 0) /* make sure to only use valid results */
3607 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003608 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003609 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003610 length = n;
3611 /* now sort & (if procs) strip out duplicates */
3612 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003613 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003614 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003615 l = cgroup_pidlist_find(cgrp, type);
3616 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003617 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003618 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003619 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003620 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003621 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003622 l->list = array;
3623 l->length = length;
3624 l->use_count++;
3625 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003627 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003628}
3629
Balbir Singh846c7bb2007-10-18 23:39:44 -07003630/**
Li Zefana043e3b2008-02-23 15:24:09 -08003631 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003632 * @stats: cgroupstats to fill information into
3633 * @dentry: A dentry entry belonging to the cgroup for which stats have
3634 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003635 *
3636 * Build and fill cgroupstats so that taskstats can export it to user
3637 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003638 */
3639int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3640{
3641 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003642 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003643 struct cgroup_iter it;
3644 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003645
Balbir Singh846c7bb2007-10-18 23:39:44 -07003646 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003647 * Validate dentry by checking the superblock operations,
3648 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003649 */
Li Zefan33d283b2008-11-19 15:36:48 -08003650 if (dentry->d_sb->s_op != &cgroup_ops ||
3651 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003652 goto err;
3653
3654 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003655 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003656
Paul Menagebd89aab2007-10-18 23:40:44 -07003657 cgroup_iter_start(cgrp, &it);
3658 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003659 switch (tsk->state) {
3660 case TASK_RUNNING:
3661 stats->nr_running++;
3662 break;
3663 case TASK_INTERRUPTIBLE:
3664 stats->nr_sleeping++;
3665 break;
3666 case TASK_UNINTERRUPTIBLE:
3667 stats->nr_uninterruptible++;
3668 break;
3669 case TASK_STOPPED:
3670 stats->nr_stopped++;
3671 break;
3672 default:
3673 if (delayacct_is_task_waiting_on_io(tsk))
3674 stats->nr_io_wait++;
3675 break;
3676 }
3677 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003678 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003679
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680err:
3681 return ret;
3682}
3683
Paul Menage8f3ff202009-09-23 15:56:25 -07003684
Paul Menagecc31edc2008-10-18 20:28:04 -07003685/*
Ben Blum102a7752009-09-23 15:56:26 -07003686 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003687 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003688 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 */
3690
Ben Blum102a7752009-09-23 15:56:26 -07003691static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003692{
3693 /*
3694 * Initially we receive a position value that corresponds to
3695 * one more than the last pid shown (or 0 on the first call or
3696 * after a seek to the start). Use a binary-search to find the
3697 * next pid to display, if any
3698 */
Ben Blum102a7752009-09-23 15:56:26 -07003699 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003700 int index = 0, pid = *pos;
3701 int *iter;
3702
Ben Blum102a7752009-09-23 15:56:26 -07003703 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003705 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003706
Paul Menagecc31edc2008-10-18 20:28:04 -07003707 while (index < end) {
3708 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003709 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 index = mid;
3711 break;
Ben Blum102a7752009-09-23 15:56:26 -07003712 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003713 index = mid + 1;
3714 else
3715 end = mid;
3716 }
3717 }
3718 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003719 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003720 return NULL;
3721 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003722 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003723 *pos = *iter;
3724 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003725}
3726
Ben Blum102a7752009-09-23 15:56:26 -07003727static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003728{
Ben Blum102a7752009-09-23 15:56:26 -07003729 struct cgroup_pidlist *l = s->private;
3730 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003731}
3732
Ben Blum102a7752009-09-23 15:56:26 -07003733static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003734{
Ben Blum102a7752009-09-23 15:56:26 -07003735 struct cgroup_pidlist *l = s->private;
3736 pid_t *p = v;
3737 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 /*
3739 * Advance to the next pid in the array. If this goes off the
3740 * end, we're done
3741 */
3742 p++;
3743 if (p >= end) {
3744 return NULL;
3745 } else {
3746 *pos = *p;
3747 return p;
3748 }
3749}
3750
Ben Blum102a7752009-09-23 15:56:26 -07003751static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003752{
3753 return seq_printf(s, "%d\n", *(int *)v);
3754}
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756/*
3757 * seq_operations functions for iterating on pidlists through seq_file -
3758 * independent of whether it's tasks or procs
3759 */
3760static const struct seq_operations cgroup_pidlist_seq_operations = {
3761 .start = cgroup_pidlist_start,
3762 .stop = cgroup_pidlist_stop,
3763 .next = cgroup_pidlist_next,
3764 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003765};
3766
Ben Blum102a7752009-09-23 15:56:26 -07003767static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003768{
Ben Blum72a8cb32009-09-23 15:56:27 -07003769 /*
3770 * the case where we're the last user of this particular pidlist will
3771 * have us remove it from the cgroup's list, which entails taking the
3772 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3773 * pidlist_mutex, we have to take pidlist_mutex first.
3774 */
3775 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003776 down_write(&l->mutex);
3777 BUG_ON(!l->use_count);
3778 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003779 /* we're the last user if refcount is 0; remove and free */
3780 list_del(&l->links);
3781 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003782 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003783 put_pid_ns(l->key.ns);
3784 up_write(&l->mutex);
3785 kfree(l);
3786 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003788 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003789 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003790}
3791
Ben Blum102a7752009-09-23 15:56:26 -07003792static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003793{
Ben Blum102a7752009-09-23 15:56:26 -07003794 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003795 if (!(file->f_mode & FMODE_READ))
3796 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003797 /*
3798 * the seq_file will only be initialized if the file was opened for
3799 * reading; hence we check if it's not null only in that case.
3800 */
3801 l = ((struct seq_file *)file->private_data)->private;
3802 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003803 return seq_release(inode, file);
3804}
3805
Ben Blum102a7752009-09-23 15:56:26 -07003806static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003807 .read = seq_read,
3808 .llseek = seq_lseek,
3809 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003810 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003811};
3812
3813/*
Ben Blum102a7752009-09-23 15:56:26 -07003814 * The following functions handle opens on a file that displays a pidlist
3815 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3816 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003817 */
Ben Blum102a7752009-09-23 15:56:26 -07003818/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003819static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003820{
3821 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003822 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003823 int retval;
3824
3825 /* Nothing to do for write-only files */
3826 if (!(file->f_mode & FMODE_READ))
3827 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003828
Ben Blum102a7752009-09-23 15:56:26 -07003829 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003830 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003831 if (retval)
3832 return retval;
3833 /* configure file information */
3834 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003835
Ben Blum102a7752009-09-23 15:56:26 -07003836 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003837 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003838 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003839 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003840 }
Ben Blum102a7752009-09-23 15:56:26 -07003841 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003842 return 0;
3843}
Ben Blum102a7752009-09-23 15:56:26 -07003844static int cgroup_tasks_open(struct inode *unused, struct file *file)
3845{
Ben Blum72a8cb32009-09-23 15:56:27 -07003846 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003847}
3848static int cgroup_procs_open(struct inode *unused, struct file *file)
3849{
Ben Blum72a8cb32009-09-23 15:56:27 -07003850 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003851}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003852
Paul Menagebd89aab2007-10-18 23:40:44 -07003853static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003854 struct cftype *cft)
3855{
Paul Menagebd89aab2007-10-18 23:40:44 -07003856 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003857}
3858
Paul Menage6379c102008-07-25 01:47:01 -07003859static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3860 struct cftype *cft,
3861 u64 val)
3862{
3863 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3864 if (val)
3865 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3866 else
3867 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3868 return 0;
3869}
3870
Paul Menagebbcb81d2007-10-18 23:39:32 -07003871/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003872 * When dput() is called asynchronously, if umount has been done and
3873 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3874 * there's a small window that vfs will see the root dentry with non-zero
3875 * refcnt and trigger BUG().
3876 *
3877 * That's why we hold a reference before dput() and drop it right after.
3878 */
3879static void cgroup_dput(struct cgroup *cgrp)
3880{
3881 struct super_block *sb = cgrp->root->sb;
3882
3883 atomic_inc(&sb->s_active);
3884 dput(cgrp->dentry);
3885 deactivate_super(sb);
3886}
3887
3888/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 * Unregister event and free resources.
3890 *
3891 * Gets called from workqueue.
3892 */
3893static void cgroup_event_remove(struct work_struct *work)
3894{
3895 struct cgroup_event *event = container_of(work, struct cgroup_event,
3896 remove);
3897 struct cgroup *cgrp = event->cgrp;
3898
Li Zefan810cbee2013-02-18 18:56:14 +08003899 remove_wait_queue(event->wqh, &event->wait);
3900
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003901 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3902
Li Zefan810cbee2013-02-18 18:56:14 +08003903 /* Notify userspace the event is going away. */
3904 eventfd_signal(event->eventfd, 1);
3905
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003906 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003907 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003908 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003909}
3910
3911/*
3912 * Gets called on POLLHUP on eventfd when user closes it.
3913 *
3914 * Called with wqh->lock held and interrupts disabled.
3915 */
3916static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3917 int sync, void *key)
3918{
3919 struct cgroup_event *event = container_of(wait,
3920 struct cgroup_event, wait);
3921 struct cgroup *cgrp = event->cgrp;
3922 unsigned long flags = (unsigned long)key;
3923
3924 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003925 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003926 * If the event has been detached at cgroup removal, we
3927 * can simply return knowing the other side will cleanup
3928 * for us.
3929 *
3930 * We can't race against event freeing since the other
3931 * side will require wqh->lock via remove_wait_queue(),
3932 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003933 */
Li Zefan810cbee2013-02-18 18:56:14 +08003934 spin_lock(&cgrp->event_list_lock);
3935 if (!list_empty(&event->list)) {
3936 list_del_init(&event->list);
3937 /*
3938 * We are in atomic context, but cgroup_event_remove()
3939 * may sleep, so we have to call it in workqueue.
3940 */
3941 schedule_work(&event->remove);
3942 }
3943 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003944 }
3945
3946 return 0;
3947}
3948
3949static void cgroup_event_ptable_queue_proc(struct file *file,
3950 wait_queue_head_t *wqh, poll_table *pt)
3951{
3952 struct cgroup_event *event = container_of(pt,
3953 struct cgroup_event, pt);
3954
3955 event->wqh = wqh;
3956 add_wait_queue(wqh, &event->wait);
3957}
3958
3959/*
3960 * Parse input and register new cgroup event handler.
3961 *
3962 * Input must be in format '<event_fd> <control_fd> <args>'.
3963 * Interpretation of args is defined by control file implementation.
3964 */
3965static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3966 const char *buffer)
3967{
3968 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003969 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003970 unsigned int efd, cfd;
3971 struct file *efile = NULL;
3972 struct file *cfile = NULL;
3973 char *endp;
3974 int ret;
3975
3976 efd = simple_strtoul(buffer, &endp, 10);
3977 if (*endp != ' ')
3978 return -EINVAL;
3979 buffer = endp + 1;
3980
3981 cfd = simple_strtoul(buffer, &endp, 10);
3982 if ((*endp != ' ') && (*endp != '\0'))
3983 return -EINVAL;
3984 buffer = endp + 1;
3985
3986 event = kzalloc(sizeof(*event), GFP_KERNEL);
3987 if (!event)
3988 return -ENOMEM;
3989 event->cgrp = cgrp;
3990 INIT_LIST_HEAD(&event->list);
3991 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3992 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3993 INIT_WORK(&event->remove, cgroup_event_remove);
3994
3995 efile = eventfd_fget(efd);
3996 if (IS_ERR(efile)) {
3997 ret = PTR_ERR(efile);
3998 goto fail;
3999 }
4000
4001 event->eventfd = eventfd_ctx_fileget(efile);
4002 if (IS_ERR(event->eventfd)) {
4003 ret = PTR_ERR(event->eventfd);
4004 goto fail;
4005 }
4006
4007 cfile = fget(cfd);
4008 if (!cfile) {
4009 ret = -EBADF;
4010 goto fail;
4011 }
4012
4013 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004014 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004015 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004016 if (ret < 0)
4017 goto fail;
4018
4019 event->cft = __file_cft(cfile);
4020 if (IS_ERR(event->cft)) {
4021 ret = PTR_ERR(event->cft);
4022 goto fail;
4023 }
4024
Li Zefanf1690072013-02-18 14:13:35 +08004025 /*
4026 * The file to be monitored must be in the same cgroup as
4027 * cgroup.event_control is.
4028 */
4029 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4030 if (cgrp_cfile != cgrp) {
4031 ret = -EINVAL;
4032 goto fail;
4033 }
4034
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004035 if (!event->cft->register_event || !event->cft->unregister_event) {
4036 ret = -EINVAL;
4037 goto fail;
4038 }
4039
4040 ret = event->cft->register_event(cgrp, event->cft,
4041 event->eventfd, buffer);
4042 if (ret)
4043 goto fail;
4044
Li Zefan7ef70e42013-04-26 11:58:03 -07004045 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004046
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004047 /*
4048 * Events should be removed after rmdir of cgroup directory, but before
4049 * destroying subsystem state objects. Let's take reference to cgroup
4050 * directory dentry to do that.
4051 */
4052 dget(cgrp->dentry);
4053
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004054 spin_lock(&cgrp->event_list_lock);
4055 list_add(&event->list, &cgrp->event_list);
4056 spin_unlock(&cgrp->event_list_lock);
4057
4058 fput(cfile);
4059 fput(efile);
4060
4061 return 0;
4062
4063fail:
4064 if (cfile)
4065 fput(cfile);
4066
4067 if (event && event->eventfd && !IS_ERR(event->eventfd))
4068 eventfd_ctx_put(event->eventfd);
4069
4070 if (!IS_ERR_OR_NULL(efile))
4071 fput(efile);
4072
4073 kfree(event);
4074
4075 return ret;
4076}
4077
Daniel Lezcano97978e62010-10-27 15:33:35 -07004078static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4079 struct cftype *cft)
4080{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004081 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004082}
4083
4084static int cgroup_clone_children_write(struct cgroup *cgrp,
4085 struct cftype *cft,
4086 u64 val)
4087{
4088 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004089 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004090 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004091 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004092 return 0;
4093}
4094
Tejun Heod5c56ce2013-06-03 19:14:34 -07004095static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004096 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004097 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004098 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004099 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004100 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004101 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004102 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004103 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004104 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004105 .write_string = cgroup_write_event_control,
4106 .mode = S_IWUGO,
4107 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004108 {
4109 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004110 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004111 .read_u64 = cgroup_clone_children_read,
4112 .write_u64 = cgroup_clone_children_write,
4113 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004114 {
Tejun Heo873fe092013-04-14 20:15:26 -07004115 .name = "cgroup.sane_behavior",
4116 .flags = CFTYPE_ONLY_ON_ROOT,
4117 .read_seq_string = cgroup_sane_behavior_show,
4118 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004119
4120 /*
4121 * Historical crazy stuff. These don't have "cgroup." prefix and
4122 * don't exist if sane_behavior. If you're depending on these, be
4123 * prepared to be burned.
4124 */
4125 {
4126 .name = "tasks",
4127 .flags = CFTYPE_INSANE, /* use "procs" instead */
4128 .open = cgroup_tasks_open,
4129 .write_u64 = cgroup_tasks_write,
4130 .release = cgroup_pidlist_release,
4131 .mode = S_IRUGO | S_IWUSR,
4132 },
4133 {
4134 .name = "notify_on_release",
4135 .flags = CFTYPE_INSANE,
4136 .read_u64 = cgroup_read_notify_on_release,
4137 .write_u64 = cgroup_write_notify_on_release,
4138 },
Tejun Heo873fe092013-04-14 20:15:26 -07004139 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004140 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004141 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004142 .read_seq_string = cgroup_release_agent_show,
4143 .write_string = cgroup_release_agent_write,
4144 .max_write_len = PATH_MAX,
4145 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004146 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004147};
4148
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004149/**
4150 * cgroup_populate_dir - selectively creation of files in a directory
4151 * @cgrp: target cgroup
4152 * @base_files: true if the base files should be added
4153 * @subsys_mask: mask of the subsystem ids whose files should be added
4154 */
4155static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4156 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157{
4158 int err;
4159 struct cgroup_subsys *ss;
4160
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004161 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004162 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004163 if (err < 0)
4164 return err;
4165 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004166
Tejun Heo8e3f6542012-04-01 12:09:55 -07004167 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004168 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004169 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004170 if (!test_bit(ss->subsys_id, &subsys_mask))
4171 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004172
Tejun Heodb0416b2012-04-01 12:09:55 -07004173 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004174 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004175 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004176
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004177 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004178 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004179 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004180 struct css_id *id = rcu_dereference_protected(css->id, true);
4181
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004182 /*
4183 * Update id->css pointer and make this css visible from
4184 * CSS ID functions. This pointer will be dereferened
4185 * from RCU-read-side without locks.
4186 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004187 if (id)
4188 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004189 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190
4191 return 0;
4192}
4193
Tejun Heo48ddbe12012-04-01 12:09:56 -07004194static void css_dput_fn(struct work_struct *work)
4195{
4196 struct cgroup_subsys_state *css =
4197 container_of(work, struct cgroup_subsys_state, dput_work);
4198
Li Zefan1c8158e2013-06-18 18:41:10 +08004199 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004200}
4201
Tejun Heod3daf282013-06-13 19:39:16 -07004202static void css_release(struct percpu_ref *ref)
4203{
4204 struct cgroup_subsys_state *css =
4205 container_of(ref, struct cgroup_subsys_state, refcnt);
4206
4207 schedule_work(&css->dput_work);
4208}
4209
Paul Menageddbcc7e2007-10-18 23:39:30 -07004210static void init_cgroup_css(struct cgroup_subsys_state *css,
4211 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004212 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004213{
Paul Menagebd89aab2007-10-18 23:40:44 -07004214 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004215 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004216 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004217 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004218 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004219 BUG_ON(cgrp->subsys[ss->subsys_id]);
4220 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004221
4222 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004223 * css holds an extra ref to @cgrp->dentry which is put on the last
4224 * css_put(). dput() requires process context, which css_put() may
4225 * be called without. @css->dput_work will be used to invoke
4226 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004227 */
4228 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229}
4230
Tejun Heob1929db2012-11-19 08:13:38 -08004231/* invoke ->post_create() on a new CSS and mark it online if successful */
4232static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004233{
Tejun Heob1929db2012-11-19 08:13:38 -08004234 int ret = 0;
4235
Tejun Heoa31f2d32012-11-19 08:13:37 -08004236 lockdep_assert_held(&cgroup_mutex);
4237
Tejun Heo92fb9742012-11-19 08:13:38 -08004238 if (ss->css_online)
4239 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004240 if (!ret)
4241 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4242 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004243}
4244
4245/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4246static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4247 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4248{
4249 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4250
4251 lockdep_assert_held(&cgroup_mutex);
4252
4253 if (!(css->flags & CSS_ONLINE))
4254 return;
4255
Li Zefand7eeac12013-03-12 15:35:59 -07004256 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004257 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004258
4259 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4260}
4261
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262/*
Li Zefana043e3b2008-02-23 15:24:09 -08004263 * cgroup_create - create a cgroup
4264 * @parent: cgroup that will be parent of the new cgroup
4265 * @dentry: dentry of the new cgroup
4266 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267 *
Li Zefana043e3b2008-02-23 15:24:09 -08004268 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004269 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004271 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272{
Paul Menagebd89aab2007-10-18 23:40:44 -07004273 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004274 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275 struct cgroupfs_root *root = parent->root;
4276 int err = 0;
4277 struct cgroup_subsys *ss;
4278 struct super_block *sb = root->sb;
4279
Tejun Heo0a950f62012-11-19 09:02:12 -08004280 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004281 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4282 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283 return -ENOMEM;
4284
Li Zefan65dff752013-03-01 15:01:56 +08004285 name = cgroup_alloc_name(dentry);
4286 if (!name)
4287 goto err_free_cgrp;
4288 rcu_assign_pointer(cgrp->name, name);
4289
Tejun Heo0a950f62012-11-19 09:02:12 -08004290 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4291 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004292 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004293
Tejun Heo976c06b2012-11-05 09:16:59 -08004294 /*
4295 * Only live parents can have children. Note that the liveliness
4296 * check isn't strictly necessary because cgroup_mkdir() and
4297 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4298 * anyway so that locking is contained inside cgroup proper and we
4299 * don't get nasty surprises if we ever grow another caller.
4300 */
4301 if (!cgroup_lock_live_group(parent)) {
4302 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004303 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004304 }
4305
Paul Menageddbcc7e2007-10-18 23:39:30 -07004306 /* Grab a reference on the superblock so the hierarchy doesn't
4307 * get deleted on unmount if there are child cgroups. This
4308 * can be done outside cgroup_mutex, since the sb can't
4309 * disappear while someone has an open control file on the
4310 * fs */
4311 atomic_inc(&sb->s_active);
4312
Paul Menagecc31edc2008-10-18 20:28:04 -07004313 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314
Li Zefanfe1c06c2013-01-24 14:30:22 +08004315 dentry->d_fsdata = cgrp;
4316 cgrp->dentry = dentry;
4317
Paul Menagebd89aab2007-10-18 23:40:44 -07004318 cgrp->parent = parent;
4319 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004320
Li Zefanb6abdb02008-03-04 14:28:19 -08004321 if (notify_on_release(parent))
4322 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4323
Tejun Heo2260e7f2012-11-19 08:13:38 -08004324 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4325 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004326
Tejun Heo5549c492013-06-24 15:21:48 -07004327 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004328 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004329
Tejun Heo92fb9742012-11-19 08:13:38 -08004330 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331 if (IS_ERR(css)) {
4332 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004333 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 }
Tejun Heod3daf282013-06-13 19:39:16 -07004335
4336 err = percpu_ref_init(&css->refcnt, css_release);
4337 if (err)
4338 goto err_free_all;
4339
Paul Menagebd89aab2007-10-18 23:40:44 -07004340 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004341
Li Zefan4528fd02010-02-02 13:44:10 -08004342 if (ss->use_id) {
4343 err = alloc_css_id(ss, parent, cgrp);
4344 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004345 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004346 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004347 }
4348
Tejun Heo4e139af2012-11-19 08:13:36 -08004349 /*
4350 * Create directory. cgroup_create_file() returns with the new
4351 * directory locked on success so that it can be populated without
4352 * dropping cgroup_mutex.
4353 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004354 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004355 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004356 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004357 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358
Tejun Heo00356bd2013-06-18 11:14:22 -07004359 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004360
Tejun Heo4e139af2012-11-19 08:13:36 -08004361 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004362 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4363 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004364
Tejun Heob1929db2012-11-19 08:13:38 -08004365 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004366 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004367 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004368
Li Zefan415cf072013-04-08 14:35:02 +08004369 /* hold a ref to the parent's dentry */
4370 dget(parent->dentry);
4371
Tejun Heob1929db2012-11-19 08:13:38 -08004372 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004373 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004374 err = online_css(ss, cgrp);
4375 if (err)
4376 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004377
4378 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4379 parent->parent) {
4380 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",
4381 current->comm, current->pid, ss->name);
4382 if (!strcmp(ss->name, "memory"))
4383 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4384 ss->warned_broken_hierarchy = true;
4385 }
Tejun Heoa8638032012-11-09 09:12:29 -08004386 }
4387
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004388 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004389 if (err)
4390 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004391
4392 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004393 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004394
4395 return 0;
4396
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004397err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004398 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004399 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4400
4401 if (css) {
4402 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004403 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004404 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004405 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 /* Release the reference count that we took on the superblock */
4408 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004409err_free_id:
4410 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004411err_free_name:
4412 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004413err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004414 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004415 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004416
4417err_destroy:
4418 cgroup_destroy_locked(cgrp);
4419 mutex_unlock(&cgroup_mutex);
4420 mutex_unlock(&dentry->d_inode->i_mutex);
4421 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422}
4423
Al Viro18bb1db2011-07-26 01:41:39 -04004424static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004425{
4426 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4427
4428 /* the vfs holds inode->i_mutex already */
4429 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4430}
4431
Tejun Heod3daf282013-06-13 19:39:16 -07004432static void cgroup_css_killed(struct cgroup *cgrp)
4433{
4434 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4435 return;
4436
4437 /* percpu ref's of all css's are killed, kick off the next step */
4438 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4439 schedule_work(&cgrp->destroy_work);
4440}
4441
4442static void css_ref_killed_fn(struct percpu_ref *ref)
4443{
4444 struct cgroup_subsys_state *css =
4445 container_of(ref, struct cgroup_subsys_state, refcnt);
4446
4447 cgroup_css_killed(css->cgroup);
4448}
4449
4450/**
4451 * cgroup_destroy_locked - the first stage of cgroup destruction
4452 * @cgrp: cgroup to be destroyed
4453 *
4454 * css's make use of percpu refcnts whose killing latency shouldn't be
4455 * exposed to userland and are RCU protected. Also, cgroup core needs to
4456 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4457 * invoked. To satisfy all the requirements, destruction is implemented in
4458 * the following two steps.
4459 *
4460 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4461 * userland visible parts and start killing the percpu refcnts of
4462 * css's. Set up so that the next stage will be kicked off once all
4463 * the percpu refcnts are confirmed to be killed.
4464 *
4465 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4466 * rest of destruction. Once all cgroup references are gone, the
4467 * cgroup is RCU-freed.
4468 *
4469 * This function implements s1. After this step, @cgrp is gone as far as
4470 * the userland is concerned and a new cgroup with the same name may be
4471 * created. As cgroup doesn't care about the names internally, this
4472 * doesn't cause any problem.
4473 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004474static int cgroup_destroy_locked(struct cgroup *cgrp)
4475 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004476{
Tejun Heo42809dd2012-11-19 08:13:37 -08004477 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004478 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004479 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004480 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004481
Tejun Heo42809dd2012-11-19 08:13:37 -08004482 lockdep_assert_held(&d->d_inode->i_mutex);
4483 lockdep_assert_held(&cgroup_mutex);
4484
Tejun Heoddd69142013-06-12 21:04:54 -07004485 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004486 * css_set_lock synchronizes access to ->cset_links and prevents
4487 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004488 */
4489 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004490 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004491 read_unlock(&css_set_lock);
4492 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004493 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004494
Tejun Heo1a90dd52012-11-05 09:16:59 -08004495 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004496 * Block new css_tryget() by killing css refcnts. cgroup core
4497 * guarantees that, by the time ->css_offline() is invoked, no new
4498 * css reference will be given out via css_tryget(). We can't
4499 * simply call percpu_ref_kill() and proceed to offlining css's
4500 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4501 * as killed on all CPUs on return.
4502 *
4503 * Use percpu_ref_kill_and_confirm() to get notifications as each
4504 * css is confirmed to be seen as killed on all CPUs. The
4505 * notification callback keeps track of the number of css's to be
4506 * killed and schedules cgroup_offline_fn() to perform the rest of
4507 * destruction once the percpu refs of all css's are confirmed to
4508 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004509 */
Tejun Heod3daf282013-06-13 19:39:16 -07004510 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004511 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004512 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4513
Tejun Heod3daf282013-06-13 19:39:16 -07004514 /*
4515 * Killing would put the base ref, but we need to keep it
4516 * alive until after ->css_offline.
4517 */
4518 percpu_ref_get(&css->refcnt);
4519
4520 atomic_inc(&cgrp->css_kill_cnt);
4521 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004522 }
Tejun Heod3daf282013-06-13 19:39:16 -07004523 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004524
4525 /*
4526 * Mark @cgrp dead. This prevents further task migration and child
4527 * creation by disabling cgroup_lock_live_group(). Note that
4528 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4529 * resume iteration after dropping RCU read lock. See
4530 * cgroup_next_sibling() for details.
4531 */
Tejun Heo54766d42013-06-12 21:04:53 -07004532 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004533
Tejun Heo455050d2013-06-13 19:27:41 -07004534 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4535 raw_spin_lock(&release_list_lock);
4536 if (!list_empty(&cgrp->release_list))
4537 list_del_init(&cgrp->release_list);
4538 raw_spin_unlock(&release_list_lock);
4539
4540 /*
4541 * Remove @cgrp directory. The removal puts the base ref but we
4542 * aren't quite done with @cgrp yet, so hold onto it.
4543 */
4544 dget(d);
4545 cgroup_d_remove_dir(d);
4546
4547 /*
4548 * Unregister events and notify userspace.
4549 * Notify userspace about cgroup removing only after rmdir of cgroup
4550 * directory to avoid race between userspace and kernelspace.
4551 */
4552 spin_lock(&cgrp->event_list_lock);
4553 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4554 list_del_init(&event->list);
4555 schedule_work(&event->remove);
4556 }
4557 spin_unlock(&cgrp->event_list_lock);
4558
Tejun Heoea15f8c2013-06-13 19:27:42 -07004559 return 0;
4560};
4561
Tejun Heod3daf282013-06-13 19:39:16 -07004562/**
4563 * cgroup_offline_fn - the second step of cgroup destruction
4564 * @work: cgroup->destroy_free_work
4565 *
4566 * This function is invoked from a work item for a cgroup which is being
4567 * destroyed after the percpu refcnts of all css's are guaranteed to be
4568 * seen as killed on all CPUs, and performs the rest of destruction. This
4569 * is the second step of destruction described in the comment above
4570 * cgroup_destroy_locked().
4571 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004572static void cgroup_offline_fn(struct work_struct *work)
4573{
4574 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4575 struct cgroup *parent = cgrp->parent;
4576 struct dentry *d = cgrp->dentry;
4577 struct cgroup_subsys *ss;
4578
4579 mutex_lock(&cgroup_mutex);
4580
Tejun Heod3daf282013-06-13 19:39:16 -07004581 /*
4582 * css_tryget() is guaranteed to fail now. Tell subsystems to
4583 * initate destruction.
4584 */
Tejun Heo5549c492013-06-24 15:21:48 -07004585 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004586 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004587
4588 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004589 * Put the css refs from cgroup_destroy_locked(). Each css holds
4590 * an extra reference to the cgroup's dentry and cgroup removal
4591 * proceeds regardless of css refs. On the last put of each css,
4592 * whenever that may be, the extra dentry ref is put so that dentry
4593 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004594 */
Tejun Heo5549c492013-06-24 15:21:48 -07004595 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004596 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004597
Paul Menage999cd8a2009-01-07 18:08:36 -08004598 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004599 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004600
Paul Menageddbcc7e2007-10-18 23:39:30 -07004601 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602
Paul Menagebd89aab2007-10-18 23:40:44 -07004603 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004604 check_for_release(parent);
4605
Tejun Heoea15f8c2013-06-13 19:27:42 -07004606 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004607}
4608
Tejun Heo42809dd2012-11-19 08:13:37 -08004609static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4610{
4611 int ret;
4612
4613 mutex_lock(&cgroup_mutex);
4614 ret = cgroup_destroy_locked(dentry->d_fsdata);
4615 mutex_unlock(&cgroup_mutex);
4616
4617 return ret;
4618}
4619
Tejun Heo8e3f6542012-04-01 12:09:55 -07004620static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4621{
4622 INIT_LIST_HEAD(&ss->cftsets);
4623
4624 /*
4625 * base_cftset is embedded in subsys itself, no need to worry about
4626 * deregistration.
4627 */
4628 if (ss->base_cftypes) {
4629 ss->base_cftset.cfts = ss->base_cftypes;
4630 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4631 }
4632}
4633
Li Zefan06a11922008-04-29 01:00:07 -07004634static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004637
4638 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639
Tejun Heo648bb562012-11-19 08:13:36 -08004640 mutex_lock(&cgroup_mutex);
4641
Tejun Heo8e3f6542012-04-01 12:09:55 -07004642 /* init base cftset */
4643 cgroup_init_cftsets(ss);
4644
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004646 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4647 ss->root = &cgroup_dummy_root;
4648 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649 /* We don't handle early failures gracefully */
4650 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004651 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652
Li Zefane8d55fd2008-04-29 01:00:13 -07004653 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004654 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004655 * newly registered, all tasks and hence the
4656 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004657 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004658
4659 need_forkexit_callback |= ss->fork || ss->exit;
4660
Li Zefane8d55fd2008-04-29 01:00:13 -07004661 /* At system boot, before all subsystems have been
4662 * registered, no tasks have been forked, so we don't
4663 * need to invoke fork callbacks here. */
4664 BUG_ON(!list_empty(&init_task.tasks));
4665
Tejun Heo9871bf92013-06-24 15:21:47 -07004666 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004667
Tejun Heo648bb562012-11-19 08:13:36 -08004668 mutex_unlock(&cgroup_mutex);
4669
Ben Blume6a11052010-03-10 15:22:09 -08004670 /* this function shouldn't be used with modular subsystems, since they
4671 * need to register a subsys_id, among other things */
4672 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673}
4674
4675/**
Ben Blume6a11052010-03-10 15:22:09 -08004676 * cgroup_load_subsys: load and register a modular subsystem at runtime
4677 * @ss: the subsystem to load
4678 *
4679 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004680 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004681 * up for use. If the subsystem is built-in anyway, work is delegated to the
4682 * simpler cgroup_init_subsys.
4683 */
4684int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4685{
Ben Blume6a11052010-03-10 15:22:09 -08004686 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004687 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004688 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004689 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004690 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004691
4692 /* check name and function validity */
4693 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004694 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004695 return -EINVAL;
4696
4697 /*
4698 * we don't support callbacks in modular subsystems. this check is
4699 * before the ss->module check for consistency; a subsystem that could
4700 * be a module should still have no callbacks even if the user isn't
4701 * compiling it as one.
4702 */
4703 if (ss->fork || ss->exit)
4704 return -EINVAL;
4705
4706 /*
4707 * an optionally modular subsystem is built-in: we want to do nothing,
4708 * since cgroup_init_subsys will have already taken care of it.
4709 */
4710 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004711 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004712 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004713 return 0;
4714 }
4715
Tejun Heo8e3f6542012-04-01 12:09:55 -07004716 /* init base cftset */
4717 cgroup_init_cftsets(ss);
4718
Ben Blume6a11052010-03-10 15:22:09 -08004719 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004720 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004721
4722 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004723 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004724 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004725 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004726 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004727 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004728 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004729 /* failure case - need to deassign the cgroup_subsys[] slot. */
4730 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004731 mutex_unlock(&cgroup_mutex);
4732 return PTR_ERR(css);
4733 }
4734
Tejun Heo9871bf92013-06-24 15:21:47 -07004735 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4736 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004737
4738 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004739 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004740 /* init_idr must be after init_cgroup_css because it sets css->id. */
4741 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004742 ret = cgroup_init_idr(ss, css);
4743 if (ret)
4744 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004745 }
4746
4747 /*
4748 * Now we need to entangle the css into the existing css_sets. unlike
4749 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4750 * will need a new pointer to it; done by iterating the css_set_table.
4751 * furthermore, modifying the existing css_sets will corrupt the hash
4752 * table state, so each changed css_set will need its hash recomputed.
4753 * this is all done under the css_set_lock.
4754 */
4755 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004756 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004757 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004758 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004759 continue;
4760 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004761 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004762 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004763 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004764 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004765 key = css_set_hash(cset->subsys);
4766 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004767 }
4768 write_unlock(&css_set_lock);
4769
Tejun Heo9871bf92013-06-24 15:21:47 -07004770 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004771 if (ret)
4772 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004773
Ben Blume6a11052010-03-10 15:22:09 -08004774 /* success! */
4775 mutex_unlock(&cgroup_mutex);
4776 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004777
4778err_unload:
4779 mutex_unlock(&cgroup_mutex);
4780 /* @ss can't be mounted here as try_module_get() would fail */
4781 cgroup_unload_subsys(ss);
4782 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004783}
4784EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4785
4786/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004787 * cgroup_unload_subsys: unload a modular subsystem
4788 * @ss: the subsystem to unload
4789 *
4790 * This function should be called in a modular subsystem's exitcall. When this
4791 * function is invoked, the refcount on the subsystem's module will be 0, so
4792 * the subsystem will not be attached to any hierarchy.
4793 */
4794void cgroup_unload_subsys(struct cgroup_subsys *ss)
4795{
Tejun Heo69d02062013-06-12 21:04:50 -07004796 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004797
4798 BUG_ON(ss->module == NULL);
4799
4800 /*
4801 * we shouldn't be called if the subsystem is in use, and the use of
4802 * try_module_get in parse_cgroupfs_options should ensure that it
4803 * doesn't start being used while we're killing it off.
4804 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004805 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004806
4807 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004808
Tejun Heo9871bf92013-06-24 15:21:47 -07004809 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004810
Tejun Heoc897ff62013-02-27 17:03:49 -08004811 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004812 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004813
Ben Blumcf5d5942010-03-10 15:22:09 -08004814 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004815 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004816
Tejun Heo9871bf92013-06-24 15:21:47 -07004817 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004818 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004819
4820 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004821 * disentangle the css from all css_sets attached to the dummy
4822 * top. as in loading, we need to pay our respects to the hashtable
4823 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004824 */
4825 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004826 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004827 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004828 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004829
Tejun Heo5abb8852013-06-12 21:04:49 -07004830 hash_del(&cset->hlist);
4831 cset->subsys[ss->subsys_id] = NULL;
4832 key = css_set_hash(cset->subsys);
4833 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004834 }
4835 write_unlock(&css_set_lock);
4836
4837 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004838 * remove subsystem's css from the cgroup_dummy_top and free it -
4839 * need to free before marking as null because ss->css_free needs
4840 * the cgrp->subsys pointer to find their state. note that this
4841 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004842 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 ss->css_free(cgroup_dummy_top);
4844 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004845
4846 mutex_unlock(&cgroup_mutex);
4847}
4848EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4849
4850/**
Li Zefana043e3b2008-02-23 15:24:09 -08004851 * cgroup_init_early - cgroup initialization at system boot
4852 *
4853 * Initialize cgroups at system boot, and initialize any
4854 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004855 */
4856int __init cgroup_init_early(void)
4857{
Tejun Heo30159ec2013-06-25 11:53:37 -07004858 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004859 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004860
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004861 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004862 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004863 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004864 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004865 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004866 init_cgroup_root(&cgroup_dummy_root);
4867 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004868 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004869
Tejun Heo69d02062013-06-12 21:04:50 -07004870 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004871 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4872 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004873 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004874
Tejun Heo30159ec2013-06-25 11:53:37 -07004875 /* at bootup time, we don't worry about modular subsystems */
4876 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004877 BUG_ON(!ss->name);
4878 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004879 BUG_ON(!ss->css_alloc);
4880 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004881 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004882 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004883 ss->name, ss->subsys_id);
4884 BUG();
4885 }
4886
4887 if (ss->early_init)
4888 cgroup_init_subsys(ss);
4889 }
4890 return 0;
4891}
4892
4893/**
Li Zefana043e3b2008-02-23 15:24:09 -08004894 * cgroup_init - cgroup initialization
4895 *
4896 * Register cgroup filesystem and /proc file, and initialize
4897 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004898 */
4899int __init cgroup_init(void)
4900{
Tejun Heo30159ec2013-06-25 11:53:37 -07004901 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004902 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004903 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004904
4905 err = bdi_init(&cgroup_backing_dev_info);
4906 if (err)
4907 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004908
Tejun Heo30159ec2013-06-25 11:53:37 -07004909 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004910 if (!ss->early_init)
4911 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004912 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004913 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914 }
4915
Tejun Heofa3ca072013-04-14 11:36:56 -07004916 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004917 mutex_lock(&cgroup_mutex);
4918 mutex_lock(&cgroup_root_mutex);
4919
Tejun Heo82fe9b02013-06-25 11:53:37 -07004920 /* Add init_css_set to the hash table */
4921 key = css_set_hash(init_css_set.subsys);
4922 hash_add(css_set_table, &init_css_set.hlist, key);
4923
Tejun Heofc76df72013-06-25 11:53:37 -07004924 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004925
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004926 mutex_unlock(&cgroup_root_mutex);
4927 mutex_unlock(&cgroup_mutex);
4928
Greg KH676db4a2010-08-05 13:53:35 -07004929 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4930 if (!cgroup_kobj) {
4931 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004932 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004933 }
4934
4935 err = register_filesystem(&cgroup_fs_type);
4936 if (err < 0) {
4937 kobject_put(cgroup_kobj);
4938 goto out;
4939 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004940
Li Zefan46ae2202008-04-29 01:00:08 -07004941 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004942
Paul Menageddbcc7e2007-10-18 23:39:30 -07004943out:
Paul Menagea4243162007-10-18 23:39:35 -07004944 if (err)
4945 bdi_destroy(&cgroup_backing_dev_info);
4946
Paul Menageddbcc7e2007-10-18 23:39:30 -07004947 return err;
4948}
Paul Menageb4f48b62007-10-18 23:39:33 -07004949
Paul Menagea4243162007-10-18 23:39:35 -07004950/*
4951 * proc_cgroup_show()
4952 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4953 * - Used for /proc/<pid>/cgroup.
4954 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4955 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004956 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004957 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4958 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4959 * cgroup to top_cgroup.
4960 */
4961
4962/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004963int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004964{
4965 struct pid *pid;
4966 struct task_struct *tsk;
4967 char *buf;
4968 int retval;
4969 struct cgroupfs_root *root;
4970
4971 retval = -ENOMEM;
4972 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4973 if (!buf)
4974 goto out;
4975
4976 retval = -ESRCH;
4977 pid = m->private;
4978 tsk = get_pid_task(pid, PIDTYPE_PID);
4979 if (!tsk)
4980 goto out_free;
4981
4982 retval = 0;
4983
4984 mutex_lock(&cgroup_mutex);
4985
Li Zefane5f6a862009-01-07 18:07:41 -08004986 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004987 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004988 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004989 int count = 0;
4990
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004991 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004992 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004993 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004994 if (strlen(root->name))
4995 seq_printf(m, "%sname=%s", count ? "," : "",
4996 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004997 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004998 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004999 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005000 if (retval < 0)
5001 goto out_unlock;
5002 seq_puts(m, buf);
5003 seq_putc(m, '\n');
5004 }
5005
5006out_unlock:
5007 mutex_unlock(&cgroup_mutex);
5008 put_task_struct(tsk);
5009out_free:
5010 kfree(buf);
5011out:
5012 return retval;
5013}
5014
Paul Menagea4243162007-10-18 23:39:35 -07005015/* Display information about each subsystem and each hierarchy */
5016static int proc_cgroupstats_show(struct seq_file *m, void *v)
5017{
Tejun Heo30159ec2013-06-25 11:53:37 -07005018 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005019 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005020
Paul Menage8bab8dd2008-04-04 14:29:57 -07005021 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005022 /*
5023 * ideally we don't want subsystems moving around while we do this.
5024 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5025 * subsys/hierarchy state.
5026 */
Paul Menagea4243162007-10-18 23:39:35 -07005027 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005028
5029 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005030 seq_printf(m, "%s\t%d\t%d\t%d\n",
5031 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005032 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005033
Paul Menagea4243162007-10-18 23:39:35 -07005034 mutex_unlock(&cgroup_mutex);
5035 return 0;
5036}
5037
5038static int cgroupstats_open(struct inode *inode, struct file *file)
5039{
Al Viro9dce07f2008-03-29 03:07:28 +00005040 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005041}
5042
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005043static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005044 .open = cgroupstats_open,
5045 .read = seq_read,
5046 .llseek = seq_lseek,
5047 .release = single_release,
5048};
5049
Paul Menageb4f48b62007-10-18 23:39:33 -07005050/**
5051 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005052 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005053 *
5054 * Description: A task inherits its parent's cgroup at fork().
5055 *
5056 * A pointer to the shared css_set was automatically copied in
5057 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005058 * it was not made under the protection of RCU or cgroup_mutex, so
5059 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5060 * have already changed current->cgroups, allowing the previously
5061 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005062 *
5063 * At the point that cgroup_fork() is called, 'current' is the parent
5064 * task, and the passed argument 'child' points to the child task.
5065 */
5066void cgroup_fork(struct task_struct *child)
5067{
Tejun Heo9bb71302012-10-18 17:52:07 -07005068 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005069 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005070 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005071 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005072 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005073}
5074
5075/**
Li Zefana043e3b2008-02-23 15:24:09 -08005076 * cgroup_post_fork - called on a new task after adding it to the task list
5077 * @child: the task in question
5078 *
Tejun Heo5edee612012-10-16 15:03:14 -07005079 * Adds the task to the list running through its css_set if necessary and
5080 * call the subsystem fork() callbacks. Has to be after the task is
5081 * visible on the task list in case we race with the first call to
5082 * cgroup_iter_start() - to guarantee that the new task ends up on its
5083 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005084 */
Paul Menage817929e2007-10-18 23:39:36 -07005085void cgroup_post_fork(struct task_struct *child)
5086{
Tejun Heo30159ec2013-06-25 11:53:37 -07005087 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005088 int i;
5089
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005090 /*
5091 * use_task_css_set_links is set to 1 before we walk the tasklist
5092 * under the tasklist_lock and we read it here after we added the child
5093 * to the tasklist under the tasklist_lock as well. If the child wasn't
5094 * yet in the tasklist when we walked through it from
5095 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5096 * should be visible now due to the paired locking and barriers implied
5097 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5098 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5099 * lock on fork.
5100 */
Paul Menage817929e2007-10-18 23:39:36 -07005101 if (use_task_css_set_links) {
5102 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005103 task_lock(child);
5104 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005105 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005106 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005107 write_unlock(&css_set_lock);
5108 }
Tejun Heo5edee612012-10-16 15:03:14 -07005109
5110 /*
5111 * Call ss->fork(). This must happen after @child is linked on
5112 * css_set; otherwise, @child might change state between ->fork()
5113 * and addition to css_set.
5114 */
5115 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005116 /*
5117 * fork/exit callbacks are supported only for builtin
5118 * subsystems, and the builtin section of the subsys
5119 * array is immutable, so we don't need to lock the
5120 * subsys array here. On the other hand, modular section
5121 * of the array can be freed at module unload, so we
5122 * can't touch that.
5123 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005124 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005125 if (ss->fork)
5126 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005127 }
Paul Menage817929e2007-10-18 23:39:36 -07005128}
Tejun Heo5edee612012-10-16 15:03:14 -07005129
Paul Menage817929e2007-10-18 23:39:36 -07005130/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005131 * cgroup_exit - detach cgroup from exiting task
5132 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005133 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005134 *
5135 * Description: Detach cgroup from @tsk and release it.
5136 *
5137 * Note that cgroups marked notify_on_release force every task in
5138 * them to take the global cgroup_mutex mutex when exiting.
5139 * This could impact scaling on very large systems. Be reluctant to
5140 * use notify_on_release cgroups where very high task exit scaling
5141 * is required on large systems.
5142 *
5143 * the_top_cgroup_hack:
5144 *
5145 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5146 *
5147 * We call cgroup_exit() while the task is still competent to
5148 * handle notify_on_release(), then leave the task attached to the
5149 * root cgroup in each hierarchy for the remainder of its exit.
5150 *
5151 * To do this properly, we would increment the reference count on
5152 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5153 * code we would add a second cgroup function call, to drop that
5154 * reference. This would just create an unnecessary hot spot on
5155 * the top_cgroup reference count, to no avail.
5156 *
5157 * Normally, holding a reference to a cgroup without bumping its
5158 * count is unsafe. The cgroup could go away, or someone could
5159 * attach us to a different cgroup, decrementing the count on
5160 * the first cgroup that we never incremented. But in this case,
5161 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005162 * which wards off any cgroup_attach_task() attempts, or task is a failed
5163 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005164 */
5165void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5166{
Tejun Heo30159ec2013-06-25 11:53:37 -07005167 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005168 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005169 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005170
5171 /*
5172 * Unlink from the css_set task list if necessary.
5173 * Optimistically check cg_list before taking
5174 * css_set_lock
5175 */
5176 if (!list_empty(&tsk->cg_list)) {
5177 write_lock(&css_set_lock);
5178 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005179 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005180 write_unlock(&css_set_lock);
5181 }
5182
Paul Menageb4f48b62007-10-18 23:39:33 -07005183 /* Reassign the task to the init_css_set. */
5184 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005185 cset = task_css_set(tsk);
5186 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005187
5188 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005189 /*
5190 * fork/exit callbacks are supported only for builtin
5191 * subsystems, see cgroup_post_fork() for details.
5192 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005193 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005194 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005195 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005196 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005197
Li Zefan761b3ef2012-01-31 13:47:36 +08005198 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005199 }
5200 }
5201 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005202 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005203
Tejun Heo5abb8852013-06-12 21:04:49 -07005204 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005205}
Paul Menage697f4162007-10-18 23:39:34 -07005206
Paul Menagebd89aab2007-10-18 23:40:44 -07005207static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005208{
Li Zefanf50daa72013-03-01 15:06:07 +08005209 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005210 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005211 /*
5212 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005213 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005214 * it now
5215 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005216 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005217
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005218 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005219 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005220 list_empty(&cgrp->release_list)) {
5221 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005222 need_schedule_work = 1;
5223 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005224 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005225 if (need_schedule_work)
5226 schedule_work(&release_agent_work);
5227 }
5228}
5229
Paul Menage81a6a5c2007-10-18 23:39:38 -07005230/*
5231 * Notify userspace when a cgroup is released, by running the
5232 * configured release agent with the name of the cgroup (path
5233 * relative to the root of cgroup file system) as the argument.
5234 *
5235 * Most likely, this user command will try to rmdir this cgroup.
5236 *
5237 * This races with the possibility that some other task will be
5238 * attached to this cgroup before it is removed, or that some other
5239 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5240 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5241 * unused, and this cgroup will be reprieved from its death sentence,
5242 * to continue to serve a useful existence. Next time it's released,
5243 * we will get notified again, if it still has 'notify_on_release' set.
5244 *
5245 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5246 * means only wait until the task is successfully execve()'d. The
5247 * separate release agent task is forked by call_usermodehelper(),
5248 * then control in this thread returns here, without waiting for the
5249 * release agent task. We don't bother to wait because the caller of
5250 * this routine has no use for the exit status of the release agent
5251 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005252 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005253static void cgroup_release_agent(struct work_struct *work)
5254{
5255 BUG_ON(work != &release_agent_work);
5256 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005257 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005258 while (!list_empty(&release_list)) {
5259 char *argv[3], *envp[3];
5260 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005261 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005262 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005263 struct cgroup,
5264 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005265 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005266 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005267 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005268 if (!pathbuf)
5269 goto continue_free;
5270 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5271 goto continue_free;
5272 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5273 if (!agentbuf)
5274 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005275
5276 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005277 argv[i++] = agentbuf;
5278 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005279 argv[i] = NULL;
5280
5281 i = 0;
5282 /* minimal command environment */
5283 envp[i++] = "HOME=/";
5284 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5285 envp[i] = NULL;
5286
5287 /* Drop the lock while we invoke the usermode helper,
5288 * since the exec could involve hitting disk and hence
5289 * be a slow process */
5290 mutex_unlock(&cgroup_mutex);
5291 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005292 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005293 continue_free:
5294 kfree(pathbuf);
5295 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005296 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005297 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005298 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005299 mutex_unlock(&cgroup_mutex);
5300}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005301
5302static int __init cgroup_disable(char *str)
5303{
Tejun Heo30159ec2013-06-25 11:53:37 -07005304 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005305 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005306 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005307
5308 while ((token = strsep(&str, ",")) != NULL) {
5309 if (!*token)
5310 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005311
Tejun Heo30159ec2013-06-25 11:53:37 -07005312 /*
5313 * cgroup_disable, being at boot time, can't know about
5314 * module subsystems, so we don't worry about them.
5315 */
5316 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005317 if (!strcmp(token, ss->name)) {
5318 ss->disabled = 1;
5319 printk(KERN_INFO "Disabling %s control group"
5320 " subsystem\n", ss->name);
5321 break;
5322 }
5323 }
5324 }
5325 return 1;
5326}
5327__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328
5329/*
5330 * Functons for CSS ID.
5331 */
5332
Tejun Heo54766d42013-06-12 21:04:53 -07005333/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334unsigned short css_id(struct cgroup_subsys_state *css)
5335{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005336 struct css_id *cssid;
5337
5338 /*
5339 * This css_id() can return correct value when somone has refcnt
5340 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5341 * it's unchanged until freed.
5342 */
Tejun Heod3daf282013-06-13 19:39:16 -07005343 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344
5345 if (cssid)
5346 return cssid->id;
5347 return 0;
5348}
Ben Blum67523c42010-03-10 15:22:11 -08005349EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005350
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005351/**
5352 * css_is_ancestor - test "root" css is an ancestor of "child"
5353 * @child: the css to be tested.
5354 * @root: the css supporsed to be an ancestor of the child.
5355 *
5356 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005357 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005358 * But, considering usual usage, the csses should be valid objects after test.
5359 * Assuming that the caller will do some action to the child if this returns
5360 * returns true, the caller must take "child";s reference count.
5361 * If "child" is valid object and this returns true, "root" is valid, too.
5362 */
5363
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005364bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005365 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005366{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005367 struct css_id *child_id;
5368 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005369
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005370 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005371 if (!child_id)
5372 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005373 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005374 if (!root_id)
5375 return false;
5376 if (child_id->depth < root_id->depth)
5377 return false;
5378 if (child_id->stack[root_id->depth] != root_id->id)
5379 return false;
5380 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005381}
5382
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005383void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5384{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005385 struct css_id *id = rcu_dereference_protected(css->id, true);
5386
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005387 /* When this is called before css_id initialization, id can be NULL */
5388 if (!id)
5389 return;
5390
5391 BUG_ON(!ss->use_id);
5392
5393 rcu_assign_pointer(id->css, NULL);
5394 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005395 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005396 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005397 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005398 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399}
Ben Blum67523c42010-03-10 15:22:11 -08005400EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005401
5402/*
5403 * This is called by init or create(). Then, calls to this function are
5404 * always serialized (By cgroup_mutex() at create()).
5405 */
5406
5407static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5408{
5409 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005410 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005411
5412 BUG_ON(!ss->use_id);
5413
5414 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5415 newid = kzalloc(size, GFP_KERNEL);
5416 if (!newid)
5417 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005418
5419 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005420 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005422 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005423 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005424 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005425
5426 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005427 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005428 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429
Tejun Heod228d9e2013-02-27 17:04:54 -08005430 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005431 newid->depth = depth;
5432 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005433err_out:
5434 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005435 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005436
5437}
5438
Ben Blume6a11052010-03-10 15:22:09 -08005439static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5440 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005441{
5442 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005443
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005444 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005445 idr_init(&ss->idr);
5446
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005447 newid = get_new_cssid(ss, 0);
5448 if (IS_ERR(newid))
5449 return PTR_ERR(newid);
5450
5451 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005452 RCU_INIT_POINTER(newid->css, rootcss);
5453 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005454 return 0;
5455}
5456
5457static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5458 struct cgroup *child)
5459{
5460 int subsys_id, i, depth = 0;
5461 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005462 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005463
5464 subsys_id = ss->subsys_id;
5465 parent_css = parent->subsys[subsys_id];
5466 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005467 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005468 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005469
5470 child_id = get_new_cssid(ss, depth);
5471 if (IS_ERR(child_id))
5472 return PTR_ERR(child_id);
5473
5474 for (i = 0; i < depth; i++)
5475 child_id->stack[i] = parent_id->stack[i];
5476 child_id->stack[depth] = child_id->id;
5477 /*
5478 * child_id->css pointer will be set after this cgroup is available
5479 * see cgroup_populate_dir()
5480 */
5481 rcu_assign_pointer(child_css->id, child_id);
5482
5483 return 0;
5484}
5485
5486/**
5487 * css_lookup - lookup css by id
5488 * @ss: cgroup subsys to be looked into.
5489 * @id: the id
5490 *
5491 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5492 * NULL if not. Should be called under rcu_read_lock()
5493 */
5494struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5495{
5496 struct css_id *cssid = NULL;
5497
5498 BUG_ON(!ss->use_id);
5499 cssid = idr_find(&ss->idr, id);
5500
5501 if (unlikely(!cssid))
5502 return NULL;
5503
5504 return rcu_dereference(cssid->css);
5505}
Ben Blum67523c42010-03-10 15:22:11 -08005506EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005507
Stephane Eraniane5d13672011-02-14 11:20:01 +02005508/*
5509 * get corresponding css from file open on cgroupfs directory
5510 */
5511struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5512{
5513 struct cgroup *cgrp;
5514 struct inode *inode;
5515 struct cgroup_subsys_state *css;
5516
Al Viro496ad9a2013-01-23 17:07:38 -05005517 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005518 /* check in cgroup filesystem dir */
5519 if (inode->i_op != &cgroup_dir_inode_operations)
5520 return ERR_PTR(-EBADF);
5521
5522 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5523 return ERR_PTR(-EINVAL);
5524
5525 /* get cgroup */
5526 cgrp = __d_cgrp(f->f_dentry);
5527 css = cgrp->subsys[id];
5528 return css ? css : ERR_PTR(-ENOENT);
5529}
5530
Paul Menagefe693432009-09-23 15:56:20 -07005531#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005532static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005533{
5534 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5535
5536 if (!css)
5537 return ERR_PTR(-ENOMEM);
5538
5539 return css;
5540}
5541
Li Zefan03c78cb2013-06-14 11:17:19 +08005542static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005543{
Li Zefan03c78cb2013-06-14 11:17:19 +08005544 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005545}
5546
Li Zefan03c78cb2013-06-14 11:17:19 +08005547static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005548{
Li Zefan03c78cb2013-06-14 11:17:19 +08005549 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005550}
5551
Li Zefan03c78cb2013-06-14 11:17:19 +08005552static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005553{
5554 return (u64)(unsigned long)current->cgroups;
5555}
5556
Li Zefan03c78cb2013-06-14 11:17:19 +08005557static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5558 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005559{
5560 u64 count;
5561
5562 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005563 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005564 rcu_read_unlock();
5565 return count;
5566}
5567
Li Zefan03c78cb2013-06-14 11:17:19 +08005568static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005569 struct cftype *cft,
5570 struct seq_file *seq)
5571{
Tejun Heo69d02062013-06-12 21:04:50 -07005572 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005573 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005574
5575 read_lock(&css_set_lock);
5576 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005577 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005578 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005579 struct cgroup *c = link->cgrp;
5580 const char *name;
5581
5582 if (c->dentry)
5583 name = c->dentry->d_name.name;
5584 else
5585 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005586 seq_printf(seq, "Root %d group %s\n",
5587 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005588 }
5589 rcu_read_unlock();
5590 read_unlock(&css_set_lock);
5591 return 0;
5592}
5593
5594#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005595static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005596 struct cftype *cft,
5597 struct seq_file *seq)
5598{
Tejun Heo69d02062013-06-12 21:04:50 -07005599 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005600
5601 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005602 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005603 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005604 struct task_struct *task;
5605 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005606 seq_printf(seq, "css_set %p\n", cset);
5607 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005608 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5609 seq_puts(seq, " ...\n");
5610 break;
5611 } else {
5612 seq_printf(seq, " task %d\n",
5613 task_pid_vnr(task));
5614 }
5615 }
5616 }
5617 read_unlock(&css_set_lock);
5618 return 0;
5619}
5620
Paul Menagefe693432009-09-23 15:56:20 -07005621static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5622{
5623 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5624}
5625
5626static struct cftype debug_files[] = {
5627 {
Paul Menagefe693432009-09-23 15:56:20 -07005628 .name = "taskcount",
5629 .read_u64 = debug_taskcount_read,
5630 },
5631
5632 {
5633 .name = "current_css_set",
5634 .read_u64 = current_css_set_read,
5635 },
5636
5637 {
5638 .name = "current_css_set_refcount",
5639 .read_u64 = current_css_set_refcount_read,
5640 },
5641
5642 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005643 .name = "current_css_set_cg_links",
5644 .read_seq_string = current_css_set_cg_links_read,
5645 },
5646
5647 {
5648 .name = "cgroup_css_links",
5649 .read_seq_string = cgroup_css_links_read,
5650 },
5651
5652 {
Paul Menagefe693432009-09-23 15:56:20 -07005653 .name = "releasable",
5654 .read_u64 = releasable_read,
5655 },
Paul Menagefe693432009-09-23 15:56:20 -07005656
Tejun Heo4baf6e32012-04-01 12:09:55 -07005657 { } /* terminate */
5658};
Paul Menagefe693432009-09-23 15:56:20 -07005659
5660struct cgroup_subsys debug_subsys = {
5661 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005662 .css_alloc = debug_css_alloc,
5663 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005664 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005665 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005666};
5667#endif /* CONFIG_CGROUP_DEBUG */