blob: 8f70dc0c0c79ad2f761dd0b29834e787bca09fa6 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218static struct cftype cgroup_base_files[];
219
Tejun Heoea15f8c2013-06-13 19:27:42 -0700220static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800222static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
223 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Ben Blume6a11052010-03-10 15:22:09 -0800367static int cgroup_init_idr(struct cgroup_subsys *ss,
368 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700369
Paul Menage817929e2007-10-18 23:39:36 -0700370/* css_set_lock protects the list of css_set objects, and the
371 * chain of tasks off each css_set. Nests outside task->alloc_lock
372 * due to cgroup_iter_start() */
373static DEFINE_RWLOCK(css_set_lock);
374static int css_set_count;
375
Paul Menage7717f7b2009-09-23 15:56:22 -0700376/*
377 * hash table for cgroup groups. This improves the performance to find
378 * an existing css_set. This hash doesn't (currently) take into
379 * account cgroups in empty hierarchies.
380 */
Li Zefan472b1052008-04-29 01:00:11 -0700381#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700385{
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700387 struct cgroup_subsys *ss;
388 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Tejun Heo30159ec2013-06-25 11:53:37 -0700390 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 key += (unsigned long)css[i];
392 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700393
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Tejun Heo5abb8852013-06-12 21:04:49 -0700403static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
Tejun Heo69d02062013-06-12 21:04:50 -0700405 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700413 return;
414 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422 css_set_count--;
423
Tejun Heo69d02062013-06-12 21:04:50 -0700424 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700425 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700426
Tejun Heo69d02062013-06-12 21:04:50 -0700427 list_del(&link->cset_link);
428 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800429
Tejun Heoddd69142013-06-12 21:04:54 -0700430 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d8282013-06-12 21:04:55 -0700431 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700433 set_bit(CGRP_RELEASABLE, &cgrp->flags);
434 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
444/*
445 * refcounted get/put for css_set objects
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700448{
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700453{
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458{
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460}
461
Tejun Heob326f9d2013-06-24 15:21:48 -0700462/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 * @cset: candidate css_set being tested
465 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * @new_cgrp: cgroup that's being entered by the task
467 * @template: desired set of css pointers in css_set (pre-calculated)
468 *
469 * Returns true if "cg" matches "old_cg" except for the hierarchy
470 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
471 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472static bool compare_css_sets(struct css_set *cset,
473 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 struct cgroup *new_cgrp,
475 struct cgroup_subsys_state *template[])
476{
477 struct list_head *l1, *l2;
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 /* Not all subsystems matched */
481 return false;
482 }
483
484 /*
485 * Compare cgroup pointers in order to distinguish between
486 * different cgroups in heirarchies with no subsystems. We
487 * could get by with just this check alone (and skip the
488 * memcmp above) but on most setups the memcmp check will
489 * avoid the need for this more expensive check on almost all
490 * candidates.
491 */
492
Tejun Heo69d02062013-06-12 21:04:50 -0700493 l1 = &cset->cgrp_links;
494 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700496 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 l1 = l1->next;
500 l2 = l2->next;
501 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700502 if (l1 == &cset->cgrp_links) {
503 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 break;
505 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700506 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 }
508 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700509 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
510 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 cgrp1 = link1->cgrp;
512 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 /*
517 * If this hierarchy is the hierarchy of the cgroup
518 * that's changing, then we need to check that this
519 * css_set points to the new cgroup; if it's any other
520 * hierarchy, then this css_set should point to the
521 * same cgroup as the old css_set.
522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 if (cgrp1->root == new_cgrp->root) {
524 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 return false;
526 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 return false;
529 }
530 }
531 return true;
532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
535 * find_existing_css_set - init css array and find the matching css_set
536 * @old_cset: the css_set that we're using before the cgroup transition
537 * @cgrp: the cgroup that we're moving into
538 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540static struct css_set *find_existing_css_set(struct css_set *old_cset,
541 struct cgroup *cgrp,
542 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700543{
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800547 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700548 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Ben Blumaae8aab2010-03-10 15:22:07 -0800550 /*
551 * Build the set of subsystem state objects that we want to see in the
552 * new css_set. while subsystems can change globally, the entries here
553 * won't change, so no need for locking.
554 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700555 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400556 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700557 /* Subsystem is in this hierarchy. So we want
558 * the subsystem state from the new
559 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700560 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700561 } else {
562 /* Subsystem is not in this hierarchy, so we
563 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700564 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 }
566 }
567
Li Zefan0ac801f2013-01-10 11:49:27 +0800568 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 hash_for_each_possible(css_set_table, cset, hlist, key) {
570 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700571 continue;
572
573 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700574 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700575 }
Paul Menage817929e2007-10-18 23:39:36 -0700576
577 /* No existing cgroup group matched */
578 return NULL;
579}
580
Tejun Heo69d02062013-06-12 21:04:50 -0700581static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700582{
Tejun Heo69d02062013-06-12 21:04:50 -0700583 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700584
Tejun Heo69d02062013-06-12 21:04:50 -0700585 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
586 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700587 kfree(link);
588 }
589}
590
Tejun Heo69d02062013-06-12 21:04:50 -0700591/**
592 * allocate_cgrp_cset_links - allocate cgrp_cset_links
593 * @count: the number of links to allocate
594 * @tmp_links: list_head the allocated links are put on
595 *
596 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
597 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700600{
Tejun Heo69d02062013-06-12 21:04:50 -0700601 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700602 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700603
604 INIT_LIST_HEAD(tmp_links);
605
Li Zefan36553432008-07-29 22:33:19 -0700606 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700607 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700608 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700609 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700610 return -ENOMEM;
611 }
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700613 }
614 return 0;
615}
616
Li Zefanc12f65d2009-01-07 18:07:42 -0800617/**
618 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700619 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800621 * @cgrp: the destination cgroup
622 */
Tejun Heo69d02062013-06-12 21:04:50 -0700623static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
624 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800625{
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800627
Tejun Heo69d02062013-06-12 21:04:50 -0700628 BUG_ON(list_empty(tmp_links));
629 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
630 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700632 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 /*
634 * Always add links to the tail of the list so that the list
635 * is sorted by order of hierarchy creation
636 */
Tejun Heo69d02062013-06-12 21:04:50 -0700637 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800638}
639
Tejun Heob326f9d2013-06-24 15:21:48 -0700640/**
641 * find_css_set - return a new css_set with one cgroup updated
642 * @old_cset: the baseline css_set
643 * @cgrp: the cgroup to be updated
644 *
645 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
646 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700647 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700648static struct css_set *find_css_set(struct css_set *old_cset,
649 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700650{
Tejun Heob326f9d2013-06-24 15:21:48 -0700651 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700653 struct list_head tmp_links;
654 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800655 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700656
Tejun Heob326f9d2013-06-24 15:21:48 -0700657 lockdep_assert_held(&cgroup_mutex);
658
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* First see if we already have a cgroup group that matches
660 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700661 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 cset = find_existing_css_set(old_cset, cgrp, template);
663 if (cset)
664 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 if (cset)
668 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700669
Tejun Heof4f4be22013-06-12 21:04:51 -0700670 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700672 return NULL;
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700675 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700676 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700677 return NULL;
678 }
679
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700681 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 INIT_LIST_HEAD(&cset->tasks);
683 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 /* Copy the set of subsystem state objects generated in
686 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 write_lock(&css_set_lock);
690 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700691 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700693
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 if (c->root == cgrp->root)
695 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700696 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 }
Paul Menage817929e2007-10-18 23:39:36 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700700
Paul Menage817929e2007-10-18 23:39:36 -0700701 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700702
703 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 key = css_set_hash(cset->subsys);
705 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700706
Paul Menage817929e2007-10-18 23:39:36 -0700707 write_unlock(&css_set_lock);
708
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700710}
711
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 * Return the cgroup for "task" from the given hierarchy. Must be
714 * called with cgroup_mutex held.
715 */
716static struct cgroup *task_cgroup_from_root(struct task_struct *task,
717 struct cgroupfs_root *root)
718{
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 struct cgroup *res = NULL;
721
722 BUG_ON(!mutex_is_locked(&cgroup_mutex));
723 read_lock(&css_set_lock);
724 /*
725 * No need to lock the task - since we hold cgroup_mutex the
726 * task can't change groups, so the only thing that can happen
727 * is that it exits and its css is set back to init_css_set.
728 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700729 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700730 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700731 res = &root->top_cgroup;
732 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700733 struct cgrp_cset_link *link;
734
735 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700737
Paul Menage7717f7b2009-09-23 15:56:22 -0700738 if (c->root == root) {
739 res = c;
740 break;
741 }
742 }
743 }
744 read_unlock(&css_set_lock);
745 BUG_ON(!res);
746 return res;
747}
748
749/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 * There is one global cgroup mutex. We also require taking
751 * task_lock() when dereferencing a task's cgroup subsys pointers.
752 * See "The task_lock() exception", at the end of this comment.
753 *
754 * A task must hold cgroup_mutex to modify cgroups.
755 *
756 * Any task can increment and decrement the count field without lock.
757 * So in general, code holding cgroup_mutex can't rely on the count
758 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800759 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * means that no tasks are currently attached, therefore there is no
761 * way a task attached to that cgroup can fork (the other way to
762 * increment the count). So code holding cgroup_mutex can safely
763 * assume that if the count is zero, it will stay zero. Similarly, if
764 * a task holds cgroup_mutex on a cgroup with zero count, it
765 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * needs that mutex.
767 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
769 * (usually) take cgroup_mutex. These are the two most performance
770 * critical pieces of code here. The exception occurs on cgroup_exit(),
771 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
772 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800773 * to the release agent with the name of the cgroup (path relative to
774 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 *
776 * A cgroup can only be deleted if both its 'count' of using tasks
777 * is zero, and its list of 'children' cgroups is empty. Since all
778 * tasks in the system use _some_ cgroup, and since there is always at
779 * least one task in the system (init, pid == 1), therefore, top_cgroup
780 * always has either children cgroups and/or using tasks. So we don't
781 * need a special hack to ensure that top_cgroup cannot be deleted.
782 *
783 * The task_lock() exception
784 *
785 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800786 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800787 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 * several performance critical places that need to reference
789 * task->cgroup without the expense of grabbing a system global
790 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800791 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
793 * the task_struct routinely used for such matters.
794 *
795 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800796 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 */
798
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799/*
800 * A couple of forward declarations required, due to cyclic reference loop:
801 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
802 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
803 * -> cgroup_mkdir.
804 */
805
Al Viro18bb1db2011-07-26 01:41:39 -0400806static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400807static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700809static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700810static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700811static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700812
813static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200814 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700815 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700816};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700818static int alloc_css_id(struct cgroup_subsys *ss,
819 struct cgroup *parent, struct cgroup *child);
820
Al Viroa5e7ed32011-07-26 01:55:55 -0400821static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822{
823 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824
825 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400826 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100828 inode->i_uid = current_fsuid();
829 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
831 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
832 }
833 return inode;
834}
835
Li Zefan65dff752013-03-01 15:01:56 +0800836static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
837{
838 struct cgroup_name *name;
839
840 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
841 if (!name)
842 return NULL;
843 strcpy(name->name, dentry->d_name.name);
844 return name;
845}
846
Li Zefanbe445622013-01-24 14:31:42 +0800847static void cgroup_free_fn(struct work_struct *work)
848{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700849 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800850 struct cgroup_subsys *ss;
851
852 mutex_lock(&cgroup_mutex);
853 /*
854 * Release the subsystem state objects.
855 */
Tejun Heo5549c492013-06-24 15:21:48 -0700856 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800857 ss->css_free(cgrp);
858
859 cgrp->root->number_of_cgroups--;
860 mutex_unlock(&cgroup_mutex);
861
862 /*
Li Zefan415cf072013-04-08 14:35:02 +0800863 * We get a ref to the parent's dentry, and put the ref when
864 * this cgroup is being freed, so it's guaranteed that the
865 * parent won't be destroyed before its children.
866 */
867 dput(cgrp->parent->dentry);
868
Li Zefancc20e012013-04-26 11:58:02 -0700869 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
870
Li Zefan415cf072013-04-08 14:35:02 +0800871 /*
Li Zefanbe445622013-01-24 14:31:42 +0800872 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700873 * created the cgroup. This will free cgrp->root, if we are
874 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800875 */
876 deactivate_super(cgrp->root->sb);
877
878 /*
879 * if we're getting rid of the cgroup, refcount should ensure
880 * that there are no pidlists left.
881 */
882 BUG_ON(!list_empty(&cgrp->pidlists));
883
884 simple_xattrs_free(&cgrp->xattrs);
885
Li Zefan65dff752013-03-01 15:01:56 +0800886 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800887 kfree(cgrp);
888}
889
890static void cgroup_free_rcu(struct rcu_head *head)
891{
892 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
893
Tejun Heoea15f8c2013-06-13 19:27:42 -0700894 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
895 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800896}
897
Paul Menageddbcc7e2007-10-18 23:39:30 -0700898static void cgroup_diput(struct dentry *dentry, struct inode *inode)
899{
900 /* is dentry a directory ? if so, kfree() associated cgroup */
901 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700902 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800903
Tejun Heo54766d42013-06-12 21:04:53 -0700904 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800905 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700906 } else {
907 struct cfent *cfe = __d_cfe(dentry);
908 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
909
910 WARN_ONCE(!list_empty(&cfe->node) &&
911 cgrp != &cgrp->root->top_cgroup,
912 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700913 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915 }
916 iput(inode);
917}
918
Al Viroc72a04e2011-01-14 05:31:45 +0000919static int cgroup_delete(const struct dentry *d)
920{
921 return 1;
922}
923
Paul Menageddbcc7e2007-10-18 23:39:30 -0700924static void remove_dir(struct dentry *d)
925{
926 struct dentry *parent = dget(d->d_parent);
927
928 d_delete(d);
929 simple_rmdir(parent->d_inode, d);
930 dput(parent);
931}
932
Li Zefan2739d3c2013-01-21 18:18:33 +0800933static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700934{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700935 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
938 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100939
Li Zefan2739d3c2013-01-21 18:18:33 +0800940 /*
941 * If we're doing cleanup due to failure of cgroup_create(),
942 * the corresponding @cfe may not exist.
943 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700944 list_for_each_entry(cfe, &cgrp->files, node) {
945 struct dentry *d = cfe->dentry;
946
947 if (cft && cfe->type != cft)
948 continue;
949
950 dget(d);
951 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700952 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953 list_del_init(&cfe->node);
954 dput(d);
955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700957 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958}
959
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400960/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700961 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700962 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700965static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700966{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400967 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700968 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969
Tejun Heob420ba72013-07-12 12:34:02 -0700970 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400971 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700972
973 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400974 continue;
975 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800976 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700978}
979
980/*
981 * NOTE : the dentry must have been dget()'ed
982 */
983static void cgroup_d_remove_dir(struct dentry *dentry)
984{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100985 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100987 parent = dentry->d_parent;
988 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800989 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991 spin_unlock(&dentry->d_lock);
992 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993 remove_dir(dentry);
994}
995
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700996/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800997 * Call with cgroup_mutex held. Drops reference counts on modules, including
998 * any duplicate ones that parse_cgroupfs_options took. If this function
999 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001000 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001002 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003{
Paul Menagebd89aab2007-10-18 23:40:44 -07001004 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001005 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 int i;
1007
Ben Blumaae8aab2010-03-10 15:22:07 -08001008 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001009 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001010
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001012 for_each_subsys(ss, i) {
Li Zefan8d53d552008-02-23 15:24:11 -08001013 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001014
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001015 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001017
Tejun Heo9871bf92013-06-24 15:21:47 -07001018 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019 /* Subsystem isn't free */
1020 return -EBUSY;
1021 }
1022 }
1023
1024 /* Currently we don't handle adding/removing subsystems when
1025 * any child cgroups exist. This is theoretically supportable
1026 * but involves complex error handling, so it's being left until
1027 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001028 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001029 return -EBUSY;
1030
1031 /* Process each subsystem */
Tejun Heo30159ec2013-06-25 11:53:37 -07001032 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001034
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001035 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001037 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001038 BUG_ON(!cgroup_dummy_top->subsys[i]);
1039 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001040
Tejun Heo9871bf92013-06-24 15:21:47 -07001041 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001042 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001043 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001044 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001046 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Ben Blumcf5d5942010-03-10 15:22:09 -08001048 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001049 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001050 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001052 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001053 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001056 ss->bind(cgroup_dummy_top);
1057 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001059 cgroup_subsys[i]->root = &cgroup_dummy_root;
1060 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001061
Ben Blumcf5d5942010-03-10 15:22:09 -08001062 /* subsystem is now free - drop reference on module */
1063 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001064 root->subsys_mask &= ~bit;
1065 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001067 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001068 /*
1069 * a refcount was taken, but we already had one, so
1070 * drop the extra reference.
1071 */
1072 module_put(ss->module);
1073#ifdef CONFIG_MODULE_UNLOAD
1074 BUG_ON(ss->module && !module_refcount(ss->module));
1075#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001076 } else {
1077 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001078 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 }
1080 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081
Tejun Heo1672d042013-06-25 18:04:54 -07001082 /*
1083 * Mark @root has finished binding subsystems. @root->subsys_mask
1084 * now matches the bound subsystems.
1085 */
1086 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1087
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 return 0;
1089}
1090
Al Viro34c80b12011-12-08 21:32:45 -05001091static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001092{
Al Viro34c80b12011-12-08 21:32:45 -05001093 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 struct cgroup_subsys *ss;
1095
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001096 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001097 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001099 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1100 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001101 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001103 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001104 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001105 if (strlen(root->release_agent_path))
1106 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001107 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001108 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001109 if (strlen(root->name))
1110 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001111 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001112 return 0;
1113}
1114
1115struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001116 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001118 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001119 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001120 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001121 /* User explicitly requested empty subsystem */
1122 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001123
1124 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001125
Paul Menageddbcc7e2007-10-18 23:39:30 -07001126};
1127
Ben Blumaae8aab2010-03-10 15:22:07 -08001128/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001129 * Convert a hierarchy specifier into a bitmask of subsystems and
1130 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1131 * array. This function takes refcounts on subsystems to be used, unless it
1132 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001133 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001134static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001136 char *token, *o = data;
1137 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001138 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001139 bool module_pin_failed = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07001140 struct cgroup_subsys *ss;
1141 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001142
Ben Blumaae8aab2010-03-10 15:22:07 -08001143 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1144
Li Zefanf9ab5b52009-06-17 16:26:33 -07001145#ifdef CONFIG_CPUSETS
1146 mask = ~(1UL << cpuset_subsys_id);
1147#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001148
Paul Menagec6d57f32009-09-23 15:56:19 -07001149 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150
1151 while ((token = strsep(&o, ",")) != NULL) {
1152 if (!*token)
1153 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001154 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001155 /* Explicitly have no subsystems */
1156 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001157 continue;
1158 }
1159 if (!strcmp(token, "all")) {
1160 /* Mutually exclusive option 'all' + subsystem name */
1161 if (one_ss)
1162 return -EINVAL;
1163 all_ss = true;
1164 continue;
1165 }
Tejun Heo873fe092013-04-14 20:15:26 -07001166 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1167 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1168 continue;
1169 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001171 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 continue;
1173 }
1174 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001175 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001176 continue;
1177 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001178 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001179 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001180 continue;
1181 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001183 /* Specifying two release agents is forbidden */
1184 if (opts->release_agent)
1185 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001186 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001187 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001188 if (!opts->release_agent)
1189 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001190 continue;
1191 }
1192 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 const char *name = token + 5;
1194 /* Can't specify an empty name */
1195 if (!strlen(name))
1196 return -EINVAL;
1197 /* Must match [\w.-]+ */
1198 for (i = 0; i < strlen(name); i++) {
1199 char c = name[i];
1200 if (isalnum(c))
1201 continue;
1202 if ((c == '.') || (c == '-') || (c == '_'))
1203 continue;
1204 return -EINVAL;
1205 }
1206 /* Specifying two names is forbidden */
1207 if (opts->name)
1208 return -EINVAL;
1209 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001210 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001211 GFP_KERNEL);
1212 if (!opts->name)
1213 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001214
1215 continue;
1216 }
1217
Tejun Heo30159ec2013-06-25 11:53:37 -07001218 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001219 if (strcmp(token, ss->name))
1220 continue;
1221 if (ss->disabled)
1222 continue;
1223
1224 /* Mutually exclusive option 'all' + subsystem name */
1225 if (all_ss)
1226 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001227 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001228 one_ss = true;
1229
1230 break;
1231 }
1232 if (i == CGROUP_SUBSYS_COUNT)
1233 return -ENOENT;
1234 }
1235
1236 /*
1237 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001238 * otherwise if 'none', 'name=' and a subsystem name options
1239 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001240 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001241 if (all_ss || (!one_ss && !opts->none && !opts->name))
1242 for_each_subsys(ss, i)
1243 if (!ss->disabled)
1244 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001245
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001246 /* Consistency checks */
1247
Tejun Heo873fe092013-04-14 20:15:26 -07001248 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1249 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1250
1251 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1252 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1253 return -EINVAL;
1254 }
1255
1256 if (opts->cpuset_clone_children) {
1257 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1258 return -EINVAL;
1259 }
1260 }
1261
Li Zefanf9ab5b52009-06-17 16:26:33 -07001262 /*
1263 * Option noprefix was introduced just for backward compatibility
1264 * with the old cpuset, so we allow noprefix only if mounting just
1265 * the cpuset subsystem.
1266 */
Tejun Heo93438622013-04-14 20:15:25 -07001267 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001268 return -EINVAL;
1269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270
1271 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001272 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001273 return -EINVAL;
1274
1275 /*
1276 * We either have to specify by name or by subsystems. (So all
1277 * empty hierarchies must have a name).
1278 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001279 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 return -EINVAL;
1281
Ben Blumcf5d5942010-03-10 15:22:09 -08001282 /*
1283 * Grab references on all the modules we'll need, so the subsystems
1284 * don't dance around before rebind_subsystems attaches them. This may
1285 * take duplicate reference counts on a subsystem that's already used,
1286 * but rebind_subsystems handles this case.
1287 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001288 for_each_subsys(ss, i) {
1289 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001291 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 module_pin_failed = true;
1293 break;
1294 }
1295 }
1296 if (module_pin_failed) {
1297 /*
1298 * oops, one of the modules was going away. this means that we
1299 * raced with a module_delete call, and to the user this is
1300 * essentially a "subsystem doesn't exist" case.
1301 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001302 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001303 /* drop refcounts only on the ones we took */
1304 unsigned long bit = 1UL << i;
1305
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001306 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001308 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 }
1310 return -ENOENT;
1311 }
1312
Paul Menageddbcc7e2007-10-18 23:39:30 -07001313 return 0;
1314}
1315
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001316static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001317{
Tejun Heo30159ec2013-06-25 11:53:37 -07001318 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001319 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001320
Tejun Heoeb178d02013-06-25 18:05:21 -07001321 mutex_lock(&cgroup_mutex);
1322 for_each_subsys(ss, i)
1323 if (subsys_mask & (1UL << i))
1324 module_put(cgroup_subsys[i]->module);
1325 mutex_unlock(&cgroup_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001326}
1327
Paul Menageddbcc7e2007-10-18 23:39:30 -07001328static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1329{
1330 int ret = 0;
1331 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001334 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
Tejun Heo873fe092013-04-14 20:15:26 -07001336 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1337 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1338 return -EINVAL;
1339 }
1340
Paul Menagebd89aab2007-10-18 23:40:44 -07001341 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001343 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344
1345 /* See what subsystems are wanted */
1346 ret = parse_cgroupfs_options(data, &opts);
1347 if (ret)
1348 goto out_unlock;
1349
Tejun Heoa8a648c2013-06-24 15:21:47 -07001350 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001351 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1352 task_tgid_nr(current), current->comm);
1353
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001354 added_mask = opts.subsys_mask & ~root->subsys_mask;
1355 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001356
Ben Blumcf5d5942010-03-10 15:22:09 -08001357 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001358 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001360 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1361 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1362 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001363 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 goto out_unlock;
1365 }
1366
Gao feng7083d032012-12-03 09:28:18 +08001367 /*
1368 * Clear out the files of subsystems that should be removed, do
1369 * this before rebind_subsystems, since rebind_subsystems may
1370 * change this hierarchy's subsys_list.
1371 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001372 cgroup_clear_dir(cgrp, removed_mask);
Gao feng7083d032012-12-03 09:28:18 +08001373
Tejun Heoa8a648c2013-06-24 15:21:47 -07001374 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001375 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001376 /* rebind_subsystems failed, re-populate the removed files */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001377 cgroup_populate_dir(cgrp, removed_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001378 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001381 /* re-populate subsystem files */
Tejun Heo628f7cd2013-06-28 16:24:11 -07001382 cgroup_populate_dir(cgrp, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383
Paul Menage81a6a5c2007-10-18 23:39:38 -07001384 if (opts.release_agent)
1385 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001387 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001388 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001389 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001391 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoe2bd4162013-06-27 19:37:23 -07001392 if (ret)
1393 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001394 return ret;
1395}
1396
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001397static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001398 .statfs = simple_statfs,
1399 .drop_inode = generic_delete_inode,
1400 .show_options = cgroup_show_options,
1401 .remount_fs = cgroup_remount,
1402};
1403
Paul Menagecc31edc2008-10-18 20:28:04 -07001404static void init_cgroup_housekeeping(struct cgroup *cgrp)
1405{
1406 INIT_LIST_HEAD(&cgrp->sibling);
1407 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001408 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001409 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001410 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001411 INIT_LIST_HEAD(&cgrp->pidlists);
1412 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001413 INIT_LIST_HEAD(&cgrp->event_list);
1414 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001415 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001416}
Paul Menagec6d57f32009-09-23 15:56:19 -07001417
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418static void init_cgroup_root(struct cgroupfs_root *root)
1419{
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001421
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422 INIT_LIST_HEAD(&root->subsys_list);
1423 INIT_LIST_HEAD(&root->root_list);
1424 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001425 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001426 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001427 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428}
1429
Tejun Heofc76df72013-06-25 11:53:37 -07001430static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001431{
Tejun Heo1a574232013-04-14 11:36:58 -07001432 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001433
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001434 lockdep_assert_held(&cgroup_mutex);
1435 lockdep_assert_held(&cgroup_root_mutex);
1436
Tejun Heofc76df72013-06-25 11:53:37 -07001437 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1438 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001439 if (id < 0)
1440 return id;
1441
1442 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001443 return 0;
1444}
1445
1446static void cgroup_exit_root_id(struct cgroupfs_root *root)
1447{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001448 lockdep_assert_held(&cgroup_mutex);
1449 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001450
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001451 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001452 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001453 root->hierarchy_id = 0;
1454 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001455}
1456
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457static int cgroup_test_super(struct super_block *sb, void *data)
1458{
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460 struct cgroupfs_root *root = sb->s_fs_info;
1461
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 /* If we asked for a name then it must match */
1463 if (opts->name && strcmp(opts->name, root->name))
1464 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001465
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001466 /*
1467 * If we asked for subsystems (or explicitly for no
1468 * subsystems) then they must match
1469 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001470 if ((opts->subsys_mask || opts->none)
1471 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001472 return 0;
1473
1474 return 1;
1475}
1476
Paul Menagec6d57f32009-09-23 15:56:19 -07001477static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1478{
1479 struct cgroupfs_root *root;
1480
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001481 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 return NULL;
1483
1484 root = kzalloc(sizeof(*root), GFP_KERNEL);
1485 if (!root)
1486 return ERR_PTR(-ENOMEM);
1487
1488 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001489
Tejun Heo1672d042013-06-25 18:04:54 -07001490 /*
1491 * We need to set @root->subsys_mask now so that @root can be
1492 * matched by cgroup_test_super() before it finishes
1493 * initialization; otherwise, competing mounts with the same
1494 * options may try to bind the same subsystems instead of waiting
1495 * for the first one leading to unexpected mount errors.
1496 * SUBSYS_BOUND will be set once actual binding is complete.
1497 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001498 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001500 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001501 if (opts->release_agent)
1502 strcpy(root->release_agent_path, opts->release_agent);
1503 if (opts->name)
1504 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001505 if (opts->cpuset_clone_children)
1506 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001507 return root;
1508}
1509
Tejun Heofa3ca072013-04-14 11:36:56 -07001510static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001511{
Tejun Heofa3ca072013-04-14 11:36:56 -07001512 if (root) {
1513 /* hierarhcy ID shoulid already have been released */
1514 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001515
Tejun Heofa3ca072013-04-14 11:36:56 -07001516 ida_destroy(&root->cgroup_ida);
1517 kfree(root);
1518 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001519}
1520
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521static int cgroup_set_super(struct super_block *sb, void *data)
1522{
1523 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001524 struct cgroup_sb_opts *opts = data;
1525
1526 /* If we don't have a new root, we can't set up a new sb */
1527 if (!opts->new_root)
1528 return -EINVAL;
1529
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001530 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 ret = set_anon_super(sb, NULL);
1533 if (ret)
1534 return ret;
1535
Paul Menagec6d57f32009-09-23 15:56:19 -07001536 sb->s_fs_info = opts->new_root;
1537 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538
1539 sb->s_blocksize = PAGE_CACHE_SIZE;
1540 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1541 sb->s_magic = CGROUP_SUPER_MAGIC;
1542 sb->s_op = &cgroup_ops;
1543
1544 return 0;
1545}
1546
1547static int cgroup_get_rootdir(struct super_block *sb)
1548{
Al Viro0df6a632010-12-21 13:29:29 -05001549 static const struct dentry_operations cgroup_dops = {
1550 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001551 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001552 };
1553
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554 struct inode *inode =
1555 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556
1557 if (!inode)
1558 return -ENOMEM;
1559
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 inode->i_fop = &simple_dir_operations;
1561 inode->i_op = &cgroup_dir_inode_operations;
1562 /* directories start off with i_nlink == 2 (for "." entry) */
1563 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001564 sb->s_root = d_make_root(inode);
1565 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001566 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001567 /* for everything else we want ->d_op set */
1568 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 return 0;
1570}
1571
Al Virof7e83572010-07-26 13:23:11 +04001572static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001574 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575{
1576 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 int ret = 0;
1579 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001581 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
1583 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001584 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001586 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 if (ret)
1588 goto out_err;
1589
1590 /*
1591 * Allocate a new cgroup root. We may not need it if we're
1592 * reusing an existing hierarchy.
1593 */
1594 new_root = cgroup_root_from_opts(&opts);
1595 if (IS_ERR(new_root)) {
1596 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001597 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001598 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001602 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001605 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001606 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 }
1608
Paul Menagec6d57f32009-09-23 15:56:19 -07001609 root = sb->s_fs_info;
1610 BUG_ON(!root);
1611 if (root == opts.new_root) {
1612 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001613 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001614 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001615 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001616 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001617 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001618 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
1620 BUG_ON(sb->s_root != NULL);
1621
1622 ret = cgroup_get_rootdir(sb);
1623 if (ret)
1624 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001625 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001626
Paul Menage817929e2007-10-18 23:39:36 -07001627 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001629 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001630
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001631 /* Check for name clashes with existing mounts */
1632 ret = -EBUSY;
1633 if (strlen(root->name))
1634 for_each_active_root(existing_root)
1635 if (!strcmp(existing_root->name, root->name))
1636 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001637
Paul Menage817929e2007-10-18 23:39:36 -07001638 /*
1639 * We're accessing css_set_count without locking
1640 * css_set_lock here, but that's OK - it can only be
1641 * increased by someone holding cgroup_lock, and
1642 * that's us. The worst that can happen is that we
1643 * have some link structures left over
1644 */
Tejun Heo69d02062013-06-12 21:04:50 -07001645 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001646 if (ret)
1647 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001648
Tejun Heofc76df72013-06-25 11:53:37 -07001649 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1650 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001651 if (ret)
1652 goto unlock_drop;
1653
Tejun Heoa8a648c2013-06-24 15:21:47 -07001654 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001656 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001657 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001659 /*
1660 * There must be no failure case after here, since rebinding
1661 * takes care of subsystems' refcounts, which are explicitly
1662 * dropped in the failure exit path.
1663 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
1665 /* EBUSY should be the only error here */
1666 BUG_ON(ret);
1667
Tejun Heo9871bf92013-06-24 15:21:47 -07001668 list_add(&root->root_list, &cgroup_roots);
1669 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001670
Li Zefanc12f65d2009-01-07 18:07:42 -08001671 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 root->top_cgroup.dentry = sb->s_root;
1673
Paul Menage817929e2007-10-18 23:39:36 -07001674 /* Link the top cgroup in this hierarchy into all
1675 * the css_set objects */
1676 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001677 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001678 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001679 write_unlock(&css_set_lock);
1680
Tejun Heo69d02062013-06-12 21:04:50 -07001681 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001682
Li Zefanc12f65d2009-01-07 18:07:42 -08001683 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001684 BUG_ON(root->number_of_cgroups != 1);
1685
eparis@redhat2ce97382011-06-02 21:20:51 +10001686 cred = override_creds(&init_cred);
Tejun Heo628f7cd2013-06-28 16:24:11 -07001687 cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true);
1688 cgroup_populate_dir(root_cgrp, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001689 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001690 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001692 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001693 } else {
1694 /*
1695 * We re-used an existing hierarchy - the new root (if
1696 * any) is not needed
1697 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001698 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001699
Tejun Heoc7ba8282013-06-29 14:06:10 -07001700 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001701 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1702 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1703 ret = -EINVAL;
1704 goto drop_new_super;
1705 } else {
1706 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1707 }
Tejun Heo873fe092013-04-14 20:15:26 -07001708 }
1709
Ben Blumcf5d5942010-03-10 15:22:09 -08001710 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001711 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 }
1713
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 kfree(opts.release_agent);
1715 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001716 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001718 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001719 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001720 mutex_unlock(&cgroup_root_mutex);
1721 mutex_unlock(&cgroup_mutex);
1722 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001724 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001725 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001726 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001727 out_err:
1728 kfree(opts.release_agent);
1729 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001730 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731}
1732
1733static void cgroup_kill_sb(struct super_block *sb) {
1734 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001735 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001736 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737 int ret;
1738
1739 BUG_ON(!root);
1740
1741 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001742 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743
1744 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001745 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001746
1747 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001748 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1749 ret = rebind_subsystems(root, 0, root->subsys_mask);
1750 /* Shouldn't be able to fail ... */
1751 BUG_ON(ret);
1752 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753
Paul Menage817929e2007-10-18 23:39:36 -07001754 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001755 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001756 * root cgroup
1757 */
1758 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001759
Tejun Heo69d02062013-06-12 21:04:50 -07001760 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1761 list_del(&link->cset_link);
1762 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001763 kfree(link);
1764 }
1765 write_unlock(&css_set_lock);
1766
Paul Menage839ec542009-01-29 14:25:22 -08001767 if (!list_empty(&root->root_list)) {
1768 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001769 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001770 }
Li Zefane5f6a862009-01-07 18:07:41 -08001771
Tejun Heofa3ca072013-04-14 11:36:56 -07001772 cgroup_exit_root_id(root);
1773
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001774 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 mutex_unlock(&cgroup_mutex);
1776
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001777 simple_xattrs_free(&cgrp->xattrs);
1778
Paul Menageddbcc7e2007-10-18 23:39:30 -07001779 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001780 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781}
1782
1783static struct file_system_type cgroup_fs_type = {
1784 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001785 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001786 .kill_sb = cgroup_kill_sb,
1787};
1788
Greg KH676db4a2010-08-05 13:53:35 -07001789static struct kobject *cgroup_kobj;
1790
Li Zefana043e3b2008-02-23 15:24:09 -08001791/**
1792 * cgroup_path - generate the path of a cgroup
1793 * @cgrp: the cgroup in question
1794 * @buf: the buffer to write the path into
1795 * @buflen: the length of the buffer
1796 *
Li Zefan65dff752013-03-01 15:01:56 +08001797 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1798 *
1799 * We can't generate cgroup path using dentry->d_name, as accessing
1800 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1801 * inode's i_mutex, while on the other hand cgroup_path() can be called
1802 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001804int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805{
Li Zefan65dff752013-03-01 15:01:56 +08001806 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001808
Tejun Heoda1f2962013-04-14 10:32:19 -07001809 if (!cgrp->parent) {
1810 if (strlcpy(buf, "/", buflen) >= buflen)
1811 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 return 0;
1813 }
1814
Tao Ma316eb662012-11-08 21:36:38 +08001815 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001816 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001817
Li Zefan65dff752013-03-01 15:01:56 +08001818 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001819 do {
Li Zefan65dff752013-03-01 15:01:56 +08001820 const char *name = cgroup_name(cgrp);
1821 int len;
1822
1823 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001825 goto out;
1826 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001827
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001829 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001830 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001831
1832 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001833 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001834 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001835 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001836out:
1837 rcu_read_unlock();
1838 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001839}
Ben Blum67523c42010-03-10 15:22:11 -08001840EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001841
Tejun Heo857a2be2013-04-14 20:50:08 -07001842/**
1843 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1844 * @task: target task
1845 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1846 * @buf: the buffer to write the path into
1847 * @buflen: the length of the buffer
1848 *
1849 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1850 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1851 * be used inside locks used by cgroup controller callbacks.
1852 */
1853int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1854 char *buf, size_t buflen)
1855{
1856 struct cgroupfs_root *root;
1857 struct cgroup *cgrp = NULL;
1858 int ret = -ENOENT;
1859
1860 mutex_lock(&cgroup_mutex);
1861
1862 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1863 if (root) {
1864 cgrp = task_cgroup_from_root(task, root);
1865 ret = cgroup_path(cgrp, buf, buflen);
1866 }
1867
1868 mutex_unlock(&cgroup_mutex);
1869
1870 return ret;
1871}
1872EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1873
Ben Blum74a11662011-05-26 16:25:20 -07001874/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001875 * Control Group taskset
1876 */
Tejun Heo134d3372011-12-12 18:12:21 -08001877struct task_and_cgroup {
1878 struct task_struct *task;
1879 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001880 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001881};
1882
Tejun Heo2f7ee562011-12-12 18:12:21 -08001883struct cgroup_taskset {
1884 struct task_and_cgroup single;
1885 struct flex_array *tc_array;
1886 int tc_array_len;
1887 int idx;
1888 struct cgroup *cur_cgrp;
1889};
1890
1891/**
1892 * cgroup_taskset_first - reset taskset and return the first task
1893 * @tset: taskset of interest
1894 *
1895 * @tset iteration is initialized and the first task is returned.
1896 */
1897struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1898{
1899 if (tset->tc_array) {
1900 tset->idx = 0;
1901 return cgroup_taskset_next(tset);
1902 } else {
1903 tset->cur_cgrp = tset->single.cgrp;
1904 return tset->single.task;
1905 }
1906}
1907EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1908
1909/**
1910 * cgroup_taskset_next - iterate to the next task in taskset
1911 * @tset: taskset of interest
1912 *
1913 * Return the next task in @tset. Iteration must have been initialized
1914 * with cgroup_taskset_first().
1915 */
1916struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1917{
1918 struct task_and_cgroup *tc;
1919
1920 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1921 return NULL;
1922
1923 tc = flex_array_get(tset->tc_array, tset->idx++);
1924 tset->cur_cgrp = tc->cgrp;
1925 return tc->task;
1926}
1927EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1928
1929/**
1930 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1931 * @tset: taskset of interest
1932 *
1933 * Return the cgroup for the current (last returned) task of @tset. This
1934 * function must be preceded by either cgroup_taskset_first() or
1935 * cgroup_taskset_next().
1936 */
1937struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1938{
1939 return tset->cur_cgrp;
1940}
1941EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1942
1943/**
1944 * cgroup_taskset_size - return the number of tasks in taskset
1945 * @tset: taskset of interest
1946 */
1947int cgroup_taskset_size(struct cgroup_taskset *tset)
1948{
1949 return tset->tc_array ? tset->tc_array_len : 1;
1950}
1951EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1952
1953
Ben Blum74a11662011-05-26 16:25:20 -07001954/*
1955 * cgroup_task_migrate - move a task from one cgroup to another.
1956 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001957 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001958 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001959static void cgroup_task_migrate(struct cgroup *old_cgrp,
1960 struct task_struct *tsk,
1961 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001962{
Tejun Heo5abb8852013-06-12 21:04:49 -07001963 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001964
1965 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001966 * We are synchronized through threadgroup_lock() against PF_EXITING
1967 * setting such that we can't race against cgroup_exit() changing the
1968 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001969 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001970 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001971 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001972
Ben Blum74a11662011-05-26 16:25:20 -07001973 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001974 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001975 task_unlock(tsk);
1976
1977 /* Update the css_set linked lists if we're using them */
1978 write_lock(&css_set_lock);
1979 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001980 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001981 write_unlock(&css_set_lock);
1982
1983 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001984 * We just gained a reference on old_cset by taking it from the
1985 * task. As trading it for new_cset is protected by cgroup_mutex,
1986 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001987 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001988 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1989 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001990}
1991
Li Zefana043e3b2008-02-23 15:24:09 -08001992/**
Li Zefan081aa452013-03-13 09:17:09 +08001993 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001994 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001995 * @tsk: the task or the leader of the threadgroup to be attached
1996 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001997 *
Tejun Heo257058a2011-12-12 18:12:21 -08001998 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001999 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002000 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002001static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2002 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002003{
2004 int retval, i, group_size;
2005 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002006 struct cgroupfs_root *root = cgrp->root;
2007 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002008 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002009 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002010 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002011 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002012
2013 /*
2014 * step 0: in order to do expensive, possibly blocking operations for
2015 * every thread, we cannot iterate the thread group list, since it needs
2016 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002017 * group - group_rwsem prevents new threads from appearing, and if
2018 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002019 */
Li Zefan081aa452013-03-13 09:17:09 +08002020 if (threadgroup)
2021 group_size = get_nr_threads(tsk);
2022 else
2023 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002024 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002025 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002026 if (!group)
2027 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002028 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002029 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002030 if (retval)
2031 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002032
Ben Blum74a11662011-05-26 16:25:20 -07002033 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002034 /*
2035 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2036 * already PF_EXITING could be freed from underneath us unless we
2037 * take an rcu_read_lock.
2038 */
2039 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002040 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002041 struct task_and_cgroup ent;
2042
Tejun Heocd3d0952011-12-12 18:12:21 -08002043 /* @tsk either already exited or can't exit until the end */
2044 if (tsk->flags & PF_EXITING)
2045 continue;
2046
Ben Blum74a11662011-05-26 16:25:20 -07002047 /* as per above, nr_threads may decrease, but not increase. */
2048 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002049 ent.task = tsk;
2050 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002051 /* nothing to do if this task is already in the cgroup */
2052 if (ent.cgrp == cgrp)
2053 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002054 /*
2055 * saying GFP_ATOMIC has no effect here because we did prealloc
2056 * earlier, but it's good form to communicate our expectations.
2057 */
Tejun Heo134d3372011-12-12 18:12:21 -08002058 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002059 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002060 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002061
2062 if (!threadgroup)
2063 break;
Ben Blum74a11662011-05-26 16:25:20 -07002064 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002065 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002066 /* remember the number of threads in the array for later. */
2067 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002068 tset.tc_array = group;
2069 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002070
Tejun Heo134d3372011-12-12 18:12:21 -08002071 /* methods shouldn't be called if no task is actually migrating */
2072 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002073 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002074 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002075
Ben Blum74a11662011-05-26 16:25:20 -07002076 /*
2077 * step 1: check that we can legitimately attach to the cgroup.
2078 */
Tejun Heo5549c492013-06-24 15:21:48 -07002079 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002080 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002081 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002082 if (retval) {
2083 failed_ss = ss;
2084 goto out_cancel_attach;
2085 }
2086 }
Ben Blum74a11662011-05-26 16:25:20 -07002087 }
2088
2089 /*
2090 * step 2: make sure css_sets exist for all threads to be migrated.
2091 * we use find_css_set, which allocates a new one if necessary.
2092 */
Ben Blum74a11662011-05-26 16:25:20 -07002093 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002094 struct css_set *old_cset;
2095
Tejun Heo134d3372011-12-12 18:12:21 -08002096 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002097 old_cset = task_css_set(tc->task);
2098 tc->cg = find_css_set(old_cset, cgrp);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002099 if (!tc->cg) {
2100 retval = -ENOMEM;
2101 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002102 }
2103 }
2104
2105 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002106 * step 3: now that we're guaranteed success wrt the css_sets,
2107 * proceed to move all tasks to the new cgroup. There are no
2108 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002109 */
Ben Blum74a11662011-05-26 16:25:20 -07002110 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002111 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002112 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002113 }
2114 /* nothing is sensitive to fork() after this point. */
2115
2116 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002117 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002118 */
Tejun Heo5549c492013-06-24 15:21:48 -07002119 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002121 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002122 }
2123
2124 /*
2125 * step 5: success! and cleanup
2126 */
Ben Blum74a11662011-05-26 16:25:20 -07002127 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002128out_put_css_set_refs:
2129 if (retval) {
2130 for (i = 0; i < group_size; i++) {
2131 tc = flex_array_get(group, i);
2132 if (!tc->cg)
2133 break;
2134 put_css_set(tc->cg);
2135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136 }
2137out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002138 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002139 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002140 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002141 break;
Ben Blum74a11662011-05-26 16:25:20 -07002142 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002143 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002144 }
2145 }
Ben Blum74a11662011-05-26 16:25:20 -07002146out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002147 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002148 return retval;
2149}
2150
2151/*
2152 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002153 * function to attach either it or all tasks in its threadgroup. Will lock
2154 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002155 */
2156static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002158 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002159 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160 int ret;
2161
Ben Blum74a11662011-05-26 16:25:20 -07002162 if (!cgroup_lock_live_group(cgrp))
2163 return -ENODEV;
2164
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165retry_find_task:
2166 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002168 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002169 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002171 ret= -ESRCH;
2172 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 }
Ben Blum74a11662011-05-26 16:25:20 -07002174 /*
2175 * even if we're attaching all tasks in the thread group, we
2176 * only need to check permissions on one of them.
2177 */
David Howellsc69e8d92008-11-14 10:39:19 +11002178 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002179 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2180 !uid_eq(cred->euid, tcred->uid) &&
2181 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002182 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002183 ret = -EACCES;
2184 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002185 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 } else
2187 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002188
2189 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002190 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002191
2192 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002193 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002194 * trapped in a cpuset, or RT worker may be born in a cgroup
2195 * with no rt_runtime allocated. Just say no.
2196 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002197 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002198 ret = -EINVAL;
2199 rcu_read_unlock();
2200 goto out_unlock_cgroup;
2201 }
2202
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002203 get_task_struct(tsk);
2204 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002205
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 threadgroup_lock(tsk);
2207 if (threadgroup) {
2208 if (!thread_group_leader(tsk)) {
2209 /*
2210 * a race with de_thread from another thread's exec()
2211 * may strip us of our leadership, if this happens,
2212 * there is no choice but to throw this task away and
2213 * try again; this is
2214 * "double-double-toil-and-trouble-check locking".
2215 */
2216 threadgroup_unlock(tsk);
2217 put_task_struct(tsk);
2218 goto retry_find_task;
2219 }
Li Zefan081aa452013-03-13 09:17:09 +08002220 }
2221
2222 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2223
Tejun Heocd3d0952011-12-12 18:12:21 -08002224 threadgroup_unlock(tsk);
2225
Paul Menagebbcb81d2007-10-18 23:39:32 -07002226 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002227out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002228 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229 return ret;
2230}
2231
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002232/**
2233 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2234 * @from: attach to all cgroups of a given task
2235 * @tsk: the task to be attached
2236 */
2237int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2238{
2239 struct cgroupfs_root *root;
2240 int retval = 0;
2241
Tejun Heo47cfcd02013-04-07 09:29:51 -07002242 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002243 for_each_active_root(root) {
2244 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2245
2246 retval = cgroup_attach_task(from_cg, tsk, false);
2247 if (retval)
2248 break;
2249 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002250 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002251
2252 return retval;
2253}
2254EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2255
Paul Menageaf351022008-07-25 01:47:01 -07002256static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2257{
Ben Blum74a11662011-05-26 16:25:20 -07002258 return attach_task_by_pid(cgrp, pid, false);
2259}
2260
2261static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2262{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002263 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002264}
2265
Paul Menagee788e062008-07-25 01:46:59 -07002266static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2267 const char *buffer)
2268{
2269 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002270 if (strlen(buffer) >= PATH_MAX)
2271 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002272 if (!cgroup_lock_live_group(cgrp))
2273 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002274 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002275 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002276 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002277 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002278 return 0;
2279}
2280
2281static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2282 struct seq_file *seq)
2283{
2284 if (!cgroup_lock_live_group(cgrp))
2285 return -ENODEV;
2286 seq_puts(seq, cgrp->root->release_agent_path);
2287 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002288 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002289 return 0;
2290}
2291
Tejun Heo873fe092013-04-14 20:15:26 -07002292static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2293 struct seq_file *seq)
2294{
2295 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002296 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002297}
2298
Paul Menage84eea842008-07-25 01:47:00 -07002299/* A buffer size big enough for numbers or short strings */
2300#define CGROUP_LOCAL_BUFFER_SIZE 64
2301
Paul Menagee73d2c62008-04-29 01:00:06 -07002302static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002303 struct file *file,
2304 const char __user *userbuf,
2305 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002306{
Paul Menage84eea842008-07-25 01:47:00 -07002307 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002308 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002309 char *end;
2310
2311 if (!nbytes)
2312 return -EINVAL;
2313 if (nbytes >= sizeof(buffer))
2314 return -E2BIG;
2315 if (copy_from_user(buffer, userbuf, nbytes))
2316 return -EFAULT;
2317
2318 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002319 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002320 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002321 if (*end)
2322 return -EINVAL;
2323 retval = cft->write_u64(cgrp, cft, val);
2324 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002325 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002326 if (*end)
2327 return -EINVAL;
2328 retval = cft->write_s64(cgrp, cft, val);
2329 }
Paul Menage355e0c42007-10-18 23:39:33 -07002330 if (!retval)
2331 retval = nbytes;
2332 return retval;
2333}
2334
Paul Menagedb3b1492008-07-25 01:46:58 -07002335static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2336 struct file *file,
2337 const char __user *userbuf,
2338 size_t nbytes, loff_t *unused_ppos)
2339{
Paul Menage84eea842008-07-25 01:47:00 -07002340 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002341 int retval = 0;
2342 size_t max_bytes = cft->max_write_len;
2343 char *buffer = local_buffer;
2344
2345 if (!max_bytes)
2346 max_bytes = sizeof(local_buffer) - 1;
2347 if (nbytes >= max_bytes)
2348 return -E2BIG;
2349 /* Allocate a dynamic buffer if we need one */
2350 if (nbytes >= sizeof(local_buffer)) {
2351 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2352 if (buffer == NULL)
2353 return -ENOMEM;
2354 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002355 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2356 retval = -EFAULT;
2357 goto out;
2358 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002359
2360 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002361 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002362 if (!retval)
2363 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002364out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002365 if (buffer != local_buffer)
2366 kfree(buffer);
2367 return retval;
2368}
2369
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2371 size_t nbytes, loff_t *ppos)
2372{
2373 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002374 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375
Tejun Heo54766d42013-06-12 21:04:53 -07002376 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002378 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002379 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002380 if (cft->write_u64 || cft->write_s64)
2381 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002382 if (cft->write_string)
2383 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002384 if (cft->trigger) {
2385 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2386 return ret ? ret : nbytes;
2387 }
Paul Menage355e0c42007-10-18 23:39:33 -07002388 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389}
2390
Paul Menagef4c753b2008-04-29 00:59:56 -07002391static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2392 struct file *file,
2393 char __user *buf, size_t nbytes,
2394 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395{
Paul Menage84eea842008-07-25 01:47:00 -07002396 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002397 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002398 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2399
2400 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2401}
2402
Paul Menagee73d2c62008-04-29 01:00:06 -07002403static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2404 struct file *file,
2405 char __user *buf, size_t nbytes,
2406 loff_t *ppos)
2407{
Paul Menage84eea842008-07-25 01:47:00 -07002408 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002409 s64 val = cft->read_s64(cgrp, cft);
2410 int len = sprintf(tmp, "%lld\n", (long long) val);
2411
2412 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2413}
2414
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2416 size_t nbytes, loff_t *ppos)
2417{
2418 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002419 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420
Tejun Heo54766d42013-06-12 21:04:53 -07002421 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422 return -ENODEV;
2423
2424 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002425 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002426 if (cft->read_u64)
2427 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002428 if (cft->read_s64)
2429 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 return -EINVAL;
2431}
2432
Paul Menage91796562008-04-29 01:00:01 -07002433/*
2434 * seqfile ops/methods for returning structured data. Currently just
2435 * supports string->u64 maps, but can be extended in future.
2436 */
2437
2438struct cgroup_seqfile_state {
2439 struct cftype *cft;
2440 struct cgroup *cgroup;
2441};
2442
2443static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2444{
2445 struct seq_file *sf = cb->state;
2446 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2447}
2448
2449static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2450{
2451 struct cgroup_seqfile_state *state = m->private;
2452 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002453 if (cft->read_map) {
2454 struct cgroup_map_cb cb = {
2455 .fill = cgroup_map_add,
2456 .state = m,
2457 };
2458 return cft->read_map(state->cgroup, cft, &cb);
2459 }
2460 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002461}
2462
Adrian Bunk96930a62008-07-25 19:46:21 -07002463static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002464{
2465 struct seq_file *seq = file->private_data;
2466 kfree(seq->private);
2467 return single_release(inode, file);
2468}
2469
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002470static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002471 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002472 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002473 .llseek = seq_lseek,
2474 .release = cgroup_seqfile_release,
2475};
2476
Paul Menageddbcc7e2007-10-18 23:39:30 -07002477static int cgroup_file_open(struct inode *inode, struct file *file)
2478{
2479 int err;
2480 struct cftype *cft;
2481
2482 err = generic_file_open(inode, file);
2483 if (err)
2484 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002486
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002487 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002488 struct cgroup_seqfile_state *state;
2489
2490 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002491 if (!state)
2492 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002493
Paul Menage91796562008-04-29 01:00:01 -07002494 state->cft = cft;
2495 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2496 file->f_op = &cgroup_seqfile_operations;
2497 err = single_open(file, cgroup_seqfile_show, state);
2498 if (err < 0)
2499 kfree(state);
2500 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501 err = cft->open(inode, file);
2502 else
2503 err = 0;
2504
2505 return err;
2506}
2507
2508static int cgroup_file_release(struct inode *inode, struct file *file)
2509{
2510 struct cftype *cft = __d_cft(file->f_dentry);
2511 if (cft->release)
2512 return cft->release(inode, file);
2513 return 0;
2514}
2515
2516/*
2517 * cgroup_rename - Only allow simple rename of directories in place.
2518 */
2519static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2520 struct inode *new_dir, struct dentry *new_dentry)
2521{
Li Zefan65dff752013-03-01 15:01:56 +08002522 int ret;
2523 struct cgroup_name *name, *old_name;
2524 struct cgroup *cgrp;
2525
2526 /*
2527 * It's convinient to use parent dir's i_mutex to protected
2528 * cgrp->name.
2529 */
2530 lockdep_assert_held(&old_dir->i_mutex);
2531
Paul Menageddbcc7e2007-10-18 23:39:30 -07002532 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2533 return -ENOTDIR;
2534 if (new_dentry->d_inode)
2535 return -EEXIST;
2536 if (old_dir != new_dir)
2537 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002538
2539 cgrp = __d_cgrp(old_dentry);
2540
Tejun Heo6db8e852013-06-14 11:18:22 -07002541 /*
2542 * This isn't a proper migration and its usefulness is very
2543 * limited. Disallow if sane_behavior.
2544 */
2545 if (cgroup_sane_behavior(cgrp))
2546 return -EPERM;
2547
Li Zefan65dff752013-03-01 15:01:56 +08002548 name = cgroup_alloc_name(new_dentry);
2549 if (!name)
2550 return -ENOMEM;
2551
2552 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2553 if (ret) {
2554 kfree(name);
2555 return ret;
2556 }
2557
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002558 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002559 rcu_assign_pointer(cgrp->name, name);
2560
2561 kfree_rcu(old_name, rcu_head);
2562 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002563}
2564
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002565static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2566{
2567 if (S_ISDIR(dentry->d_inode->i_mode))
2568 return &__d_cgrp(dentry)->xattrs;
2569 else
Li Zefan712317a2013-04-18 23:09:52 -07002570 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002571}
2572
2573static inline int xattr_enabled(struct dentry *dentry)
2574{
2575 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002576 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002577}
2578
2579static bool is_valid_xattr(const char *name)
2580{
2581 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2582 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2583 return true;
2584 return false;
2585}
2586
2587static int cgroup_setxattr(struct dentry *dentry, const char *name,
2588 const void *val, size_t size, int flags)
2589{
2590 if (!xattr_enabled(dentry))
2591 return -EOPNOTSUPP;
2592 if (!is_valid_xattr(name))
2593 return -EINVAL;
2594 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2595}
2596
2597static int cgroup_removexattr(struct dentry *dentry, const char *name)
2598{
2599 if (!xattr_enabled(dentry))
2600 return -EOPNOTSUPP;
2601 if (!is_valid_xattr(name))
2602 return -EINVAL;
2603 return simple_xattr_remove(__d_xattrs(dentry), name);
2604}
2605
2606static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2607 void *buf, size_t size)
2608{
2609 if (!xattr_enabled(dentry))
2610 return -EOPNOTSUPP;
2611 if (!is_valid_xattr(name))
2612 return -EINVAL;
2613 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2614}
2615
2616static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2617{
2618 if (!xattr_enabled(dentry))
2619 return -EOPNOTSUPP;
2620 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2621}
2622
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002623static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624 .read = cgroup_file_read,
2625 .write = cgroup_file_write,
2626 .llseek = generic_file_llseek,
2627 .open = cgroup_file_open,
2628 .release = cgroup_file_release,
2629};
2630
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002631static const struct inode_operations cgroup_file_inode_operations = {
2632 .setxattr = cgroup_setxattr,
2633 .getxattr = cgroup_getxattr,
2634 .listxattr = cgroup_listxattr,
2635 .removexattr = cgroup_removexattr,
2636};
2637
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002638static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002639 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002640 .mkdir = cgroup_mkdir,
2641 .rmdir = cgroup_rmdir,
2642 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002643 .setxattr = cgroup_setxattr,
2644 .getxattr = cgroup_getxattr,
2645 .listxattr = cgroup_listxattr,
2646 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002647};
2648
Al Viro00cd8dd2012-06-10 17:13:09 -04002649static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002650{
2651 if (dentry->d_name.len > NAME_MAX)
2652 return ERR_PTR(-ENAMETOOLONG);
2653 d_add(dentry, NULL);
2654 return NULL;
2655}
2656
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002657/*
2658 * Check if a file is a control file
2659 */
2660static inline struct cftype *__file_cft(struct file *file)
2661{
Al Viro496ad9a2013-01-23 17:07:38 -05002662 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002663 return ERR_PTR(-EINVAL);
2664 return __d_cft(file->f_dentry);
2665}
2666
Al Viroa5e7ed32011-07-26 01:55:55 -04002667static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002668 struct super_block *sb)
2669{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 struct inode *inode;
2671
2672 if (!dentry)
2673 return -ENOENT;
2674 if (dentry->d_inode)
2675 return -EEXIST;
2676
2677 inode = cgroup_new_inode(mode, sb);
2678 if (!inode)
2679 return -ENOMEM;
2680
2681 if (S_ISDIR(mode)) {
2682 inode->i_op = &cgroup_dir_inode_operations;
2683 inode->i_fop = &simple_dir_operations;
2684
2685 /* start off with i_nlink == 2 (for "." entry) */
2686 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002687 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688
Tejun Heob8a2df62012-11-19 08:13:37 -08002689 /*
2690 * Control reaches here with cgroup_mutex held.
2691 * @inode->i_mutex should nest outside cgroup_mutex but we
2692 * want to populate it immediately without releasing
2693 * cgroup_mutex. As @inode isn't visible to anyone else
2694 * yet, trylock will always succeed without affecting
2695 * lockdep checks.
2696 */
2697 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002698 } else if (S_ISREG(mode)) {
2699 inode->i_size = 0;
2700 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002701 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703 d_instantiate(dentry, inode);
2704 dget(dentry); /* Extra count - pin the dentry in core */
2705 return 0;
2706}
2707
Li Zefan099fca32009-04-02 16:57:29 -07002708/**
2709 * cgroup_file_mode - deduce file mode of a control file
2710 * @cft: the control file in question
2711 *
2712 * returns cft->mode if ->mode is not 0
2713 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2714 * returns S_IRUGO if it has only a read handler
2715 * returns S_IWUSR if it has only a write hander
2716 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002717static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002718{
Al Viroa5e7ed32011-07-26 01:55:55 -04002719 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002720
2721 if (cft->mode)
2722 return cft->mode;
2723
2724 if (cft->read || cft->read_u64 || cft->read_s64 ||
2725 cft->read_map || cft->read_seq_string)
2726 mode |= S_IRUGO;
2727
2728 if (cft->write || cft->write_u64 || cft->write_s64 ||
2729 cft->write_string || cft->trigger)
2730 mode |= S_IWUSR;
2731
2732 return mode;
2733}
2734
Tejun Heodb0416b2012-04-01 12:09:55 -07002735static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002736 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737{
Paul Menagebd89aab2007-10-18 23:40:44 -07002738 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002739 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002743 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002745
Tejun Heo93438622013-04-14 20:15:25 -07002746 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 strcpy(name, subsys->name);
2748 strcat(name, ".");
2749 }
2750 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002751
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002753
2754 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2755 if (!cfe)
2756 return -ENOMEM;
2757
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002759 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002761 goto out;
2762 }
2763
Li Zefand6cbf352013-05-14 19:44:20 +08002764 cfe->type = (void *)cft;
2765 cfe->dentry = dentry;
2766 dentry->d_fsdata = cfe;
2767 simple_xattrs_init(&cfe->xattrs);
2768
Tejun Heo05ef1d72012-04-01 12:09:56 -07002769 mode = cgroup_file_mode(cft);
2770 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2771 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002772 list_add_tail(&cfe->node, &parent->files);
2773 cfe = NULL;
2774 }
2775 dput(dentry);
2776out:
2777 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002778 return error;
2779}
2780
Tejun Heob1f28d32013-06-28 16:24:10 -07002781/**
2782 * cgroup_addrm_files - add or remove files to a cgroup directory
2783 * @cgrp: the target cgroup
2784 * @subsys: the subsystem of files to be added
2785 * @cfts: array of cftypes to be added
2786 * @is_add: whether to add or remove
2787 *
2788 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2789 * All @cfts should belong to @subsys. For removals, this function never
2790 * fails. If addition fails, this function doesn't remove files already
2791 * added. The caller is responsible for cleaning up.
2792 */
Tejun Heo79578622012-04-01 12:09:56 -07002793static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002794 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002795{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002796 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002797 int ret;
2798
2799 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2800 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002801
2802 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002803 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002804 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2805 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002806 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2807 continue;
2808 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2809 continue;
2810
Li Zefan2739d3c2013-01-21 18:18:33 +08002811 if (is_add) {
Tejun Heob1f28d32013-06-28 16:24:10 -07002812 ret = cgroup_add_file(cgrp, subsys, cft);
2813 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002814 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002815 cft->name, ret);
2816 return ret;
2817 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002818 } else {
2819 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002820 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002821 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002822 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002823}
2824
Tejun Heo8e3f6542012-04-01 12:09:55 -07002825static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002826 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002827{
2828 /*
2829 * Thanks to the entanglement with vfs inode locking, we can't walk
2830 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002831 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2832 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002833 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834 mutex_lock(&cgroup_mutex);
2835}
2836
Tejun Heo9ccece82013-06-28 16:24:11 -07002837static int cgroup_cfts_commit(struct cgroup_subsys *ss,
2838 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002839 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840{
2841 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002842 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002843 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002844 struct dentry *prev = NULL;
2845 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002846 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002847 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848
2849 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002850 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002851 !atomic_inc_not_zero(&sb->s_active)) {
2852 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002853 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854 }
2855
Li Zefane8c82d22013-06-18 18:48:37 +08002856 /*
2857 * All cgroups which are created after we drop cgroup_mutex will
2858 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002859 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002860 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002861 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002862
Tejun Heo8e3f6542012-04-01 12:09:55 -07002863 mutex_unlock(&cgroup_mutex);
2864
Li Zefane8c82d22013-06-18 18:48:37 +08002865 /* @root always needs to be updated */
2866 inode = root->dentry->d_inode;
2867 mutex_lock(&inode->i_mutex);
2868 mutex_lock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002869 ret = cgroup_addrm_files(root, ss, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002870 mutex_unlock(&cgroup_mutex);
2871 mutex_unlock(&inode->i_mutex);
2872
Tejun Heo9ccece82013-06-28 16:24:11 -07002873 if (ret)
2874 goto out_deact;
2875
Li Zefane8c82d22013-06-18 18:48:37 +08002876 /* add/rm files for all cgroups created before */
2877 rcu_read_lock();
2878 cgroup_for_each_descendant_pre(cgrp, root) {
2879 if (cgroup_is_dead(cgrp))
2880 continue;
2881
2882 inode = cgrp->dentry->d_inode;
2883 dget(cgrp->dentry);
2884 rcu_read_unlock();
2885
2886 dput(prev);
2887 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002888
2889 mutex_lock(&inode->i_mutex);
2890 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002891 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo9ccece82013-06-28 16:24:11 -07002892 ret = cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002893 mutex_unlock(&cgroup_mutex);
2894 mutex_unlock(&inode->i_mutex);
2895
Li Zefane8c82d22013-06-18 18:48:37 +08002896 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002897 if (ret)
2898 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002899 }
Li Zefane8c82d22013-06-18 18:48:37 +08002900 rcu_read_unlock();
2901 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002902out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002903 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002904 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002905}
2906
2907/**
2908 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2909 * @ss: target cgroup subsystem
2910 * @cfts: zero-length name terminated array of cftypes
2911 *
2912 * Register @cfts to @ss. Files described by @cfts are created for all
2913 * existing cgroups to which @ss is attached and all future cgroups will
2914 * have them too. This function can be called anytime whether @ss is
2915 * attached or not.
2916 *
2917 * Returns 0 on successful registration, -errno on failure. Note that this
2918 * function currently returns 0 as long as @cfts registration is successful
2919 * even if some file creation attempts on existing cgroups fail.
2920 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002921int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002922{
2923 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002924 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002925
2926 set = kzalloc(sizeof(*set), GFP_KERNEL);
2927 if (!set)
2928 return -ENOMEM;
2929
2930 cgroup_cfts_prepare();
2931 set->cfts = cfts;
2932 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo9ccece82013-06-28 16:24:11 -07002933 ret = cgroup_cfts_commit(ss, cfts, true);
2934 if (ret)
2935 cgroup_rm_cftypes(ss, cfts);
2936 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002937}
2938EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2939
Li Zefana043e3b2008-02-23 15:24:09 -08002940/**
Tejun Heo79578622012-04-01 12:09:56 -07002941 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2942 * @ss: target cgroup subsystem
2943 * @cfts: zero-length name terminated array of cftypes
2944 *
2945 * Unregister @cfts from @ss. Files described by @cfts are removed from
2946 * all existing cgroups to which @ss is attached and all future cgroups
2947 * won't have them either. This function can be called anytime whether @ss
2948 * is attached or not.
2949 *
2950 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2951 * registered with @ss.
2952 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002953int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002954{
2955 struct cftype_set *set;
2956
2957 cgroup_cfts_prepare();
2958
2959 list_for_each_entry(set, &ss->cftsets, node) {
2960 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002961 list_del(&set->node);
2962 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002963 cgroup_cfts_commit(ss, cfts, false);
2964 return 0;
2965 }
2966 }
2967
2968 cgroup_cfts_commit(ss, NULL, false);
2969 return -ENOENT;
2970}
2971
2972/**
Li Zefana043e3b2008-02-23 15:24:09 -08002973 * cgroup_task_count - count the number of tasks in a cgroup.
2974 * @cgrp: the cgroup in question
2975 *
2976 * Return the number of tasks in the cgroup.
2977 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002978int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002979{
2980 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002981 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002982
Paul Menage817929e2007-10-18 23:39:36 -07002983 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002984 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2985 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002986 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002987 return count;
2988}
2989
2990/*
Paul Menage817929e2007-10-18 23:39:36 -07002991 * Advance a list_head iterator. The iterator should be positioned at
2992 * the start of a css_set
2993 */
Tejun Heo69d02062013-06-12 21:04:50 -07002994static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002995{
Tejun Heo69d02062013-06-12 21:04:50 -07002996 struct list_head *l = it->cset_link;
2997 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002998 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002999
3000 /* Advance to the next non-empty css_set */
3001 do {
3002 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003003 if (l == &cgrp->cset_links) {
3004 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07003005 return;
3006 }
Tejun Heo69d02062013-06-12 21:04:50 -07003007 link = list_entry(l, struct cgrp_cset_link, cset_link);
3008 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07003009 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07003010 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07003011 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07003012}
3013
Cliff Wickman31a7df02008-02-07 00:14:42 -08003014/*
3015 * To reduce the fork() overhead for systems that are not actually
3016 * using their cgroups capability, we don't maintain the lists running
3017 * through each css_set to its tasks until we see the list actually
3018 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003020static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003021{
3022 struct task_struct *p, *g;
3023 write_lock(&css_set_lock);
3024 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003025 /*
3026 * We need tasklist_lock because RCU is not safe against
3027 * while_each_thread(). Besides, a forking task that has passed
3028 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3029 * is not guaranteed to have its child immediately visible in the
3030 * tasklist if we walk through it with RCU.
3031 */
3032 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003033 do_each_thread(g, p) {
3034 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003035 /*
3036 * We should check if the process is exiting, otherwise
3037 * it will race with cgroup_exit() in that the list
3038 * entry won't be deleted though the process has exited.
3039 */
3040 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003041 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003042 task_unlock(p);
3043 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003044 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003045 write_unlock(&css_set_lock);
3046}
3047
Tejun Heo574bd9f2012-11-09 09:12:29 -08003048/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003049 * cgroup_next_sibling - find the next sibling of a given cgroup
3050 * @pos: the current cgroup
3051 *
3052 * This function returns the next sibling of @pos and should be called
3053 * under RCU read lock. The only requirement is that @pos is accessible.
3054 * The next sibling is guaranteed to be returned regardless of @pos's
3055 * state.
3056 */
3057struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3058{
3059 struct cgroup *next;
3060
3061 WARN_ON_ONCE(!rcu_read_lock_held());
3062
3063 /*
3064 * @pos could already have been removed. Once a cgroup is removed,
3065 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003066 * changes. As CGRP_DEAD assertion is serialized and happens
3067 * before the cgroup is taken off the ->sibling list, if we see it
3068 * unasserted, it's guaranteed that the next sibling hasn't
3069 * finished its grace period even if it's already removed, and thus
3070 * safe to dereference from this RCU critical section. If
3071 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3072 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003073 */
Tejun Heo54766d42013-06-12 21:04:53 -07003074 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003075 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3076 if (&next->sibling != &pos->parent->children)
3077 return next;
3078 return NULL;
3079 }
3080
3081 /*
3082 * Can't dereference the next pointer. Each cgroup is given a
3083 * monotonically increasing unique serial number and always
3084 * appended to the sibling list, so the next one can be found by
3085 * walking the parent's children until we see a cgroup with higher
3086 * serial number than @pos's.
3087 *
3088 * While this path can be slow, it's taken only when either the
3089 * current cgroup is removed or iteration and removal race.
3090 */
3091 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3092 if (next->serial_nr > pos->serial_nr)
3093 return next;
3094 return NULL;
3095}
3096EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3097
3098/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3100 * @pos: the current position (%NULL to initiate traversal)
3101 * @cgroup: cgroup whose descendants to walk
3102 *
3103 * To be used by cgroup_for_each_descendant_pre(). Find the next
3104 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003105 *
3106 * While this function requires RCU read locking, it doesn't require the
3107 * whole traversal to be contained in a single RCU critical section. This
3108 * function will return the correct next descendant as long as both @pos
3109 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003110 */
3111struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3112 struct cgroup *cgroup)
3113{
3114 struct cgroup *next;
3115
3116 WARN_ON_ONCE(!rcu_read_lock_held());
3117
3118 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003119 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003120 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003121
3122 /* visit the first child if exists */
3123 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3124 if (next)
3125 return next;
3126
3127 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003128 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003129 next = cgroup_next_sibling(pos);
3130 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003131 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003132 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003133 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003134
3135 return NULL;
3136}
3137EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3138
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003139/**
3140 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3141 * @pos: cgroup of interest
3142 *
3143 * Return the rightmost descendant of @pos. If there's no descendant,
3144 * @pos is returned. This can be used during pre-order traversal to skip
3145 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003146 *
3147 * While this function requires RCU read locking, it doesn't require the
3148 * whole traversal to be contained in a single RCU critical section. This
3149 * function will return the correct rightmost descendant as long as @pos is
3150 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003151 */
3152struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3153{
3154 struct cgroup *last, *tmp;
3155
3156 WARN_ON_ONCE(!rcu_read_lock_held());
3157
3158 do {
3159 last = pos;
3160 /* ->prev isn't RCU safe, walk ->next till the end */
3161 pos = NULL;
3162 list_for_each_entry_rcu(tmp, &last->children, sibling)
3163 pos = tmp;
3164 } while (pos);
3165
3166 return last;
3167}
3168EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3169
Tejun Heo574bd9f2012-11-09 09:12:29 -08003170static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3171{
3172 struct cgroup *last;
3173
3174 do {
3175 last = pos;
3176 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3177 sibling);
3178 } while (pos);
3179
3180 return last;
3181}
3182
3183/**
3184 * cgroup_next_descendant_post - find the next descendant for post-order walk
3185 * @pos: the current position (%NULL to initiate traversal)
3186 * @cgroup: cgroup whose descendants to walk
3187 *
3188 * To be used by cgroup_for_each_descendant_post(). Find the next
3189 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003190 *
3191 * While this function requires RCU read locking, it doesn't require the
3192 * whole traversal to be contained in a single RCU critical section. This
3193 * function will return the correct next descendant as long as both @pos
3194 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003195 */
3196struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3197 struct cgroup *cgroup)
3198{
3199 struct cgroup *next;
3200
3201 WARN_ON_ONCE(!rcu_read_lock_held());
3202
3203 /* if first iteration, visit the leftmost descendant */
3204 if (!pos) {
3205 next = cgroup_leftmost_descendant(cgroup);
3206 return next != cgroup ? next : NULL;
3207 }
3208
3209 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003210 next = cgroup_next_sibling(pos);
3211 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003212 return cgroup_leftmost_descendant(next);
3213
3214 /* no sibling left, visit parent */
3215 next = pos->parent;
3216 return next != cgroup ? next : NULL;
3217}
3218EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3219
Paul Menagebd89aab2007-10-18 23:40:44 -07003220void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003221 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003222{
3223 /*
3224 * The first time anyone tries to iterate across a cgroup,
3225 * we need to enable the list linking each css_set to its
3226 * tasks, and fix up all existing tasks.
3227 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003228 if (!use_task_css_set_links)
3229 cgroup_enable_task_cg_lists();
3230
Paul Menage817929e2007-10-18 23:39:36 -07003231 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003232 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003233 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003234}
3235
Paul Menagebd89aab2007-10-18 23:40:44 -07003236struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003237 struct cgroup_iter *it)
3238{
3239 struct task_struct *res;
3240 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003241 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003242
3243 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003244 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003245 return NULL;
3246 res = list_entry(l, struct task_struct, cg_list);
3247 /* Advance iterator to find next entry */
3248 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003249 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3250 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003251 /* We reached the end of this task list - move on to
3252 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003253 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003254 } else {
3255 it->task = l;
3256 }
3257 return res;
3258}
3259
Paul Menagebd89aab2007-10-18 23:40:44 -07003260void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003261 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003262{
3263 read_unlock(&css_set_lock);
3264}
3265
Cliff Wickman31a7df02008-02-07 00:14:42 -08003266static inline int started_after_time(struct task_struct *t1,
3267 struct timespec *time,
3268 struct task_struct *t2)
3269{
3270 int start_diff = timespec_compare(&t1->start_time, time);
3271 if (start_diff > 0) {
3272 return 1;
3273 } else if (start_diff < 0) {
3274 return 0;
3275 } else {
3276 /*
3277 * Arbitrarily, if two processes started at the same
3278 * time, we'll say that the lower pointer value
3279 * started first. Note that t2 may have exited by now
3280 * so this may not be a valid pointer any longer, but
3281 * that's fine - it still serves to distinguish
3282 * between two tasks started (effectively) simultaneously.
3283 */
3284 return t1 > t2;
3285 }
3286}
3287
3288/*
3289 * This function is a callback from heap_insert() and is used to order
3290 * the heap.
3291 * In this case we order the heap in descending task start time.
3292 */
3293static inline int started_after(void *p1, void *p2)
3294{
3295 struct task_struct *t1 = p1;
3296 struct task_struct *t2 = p2;
3297 return started_after_time(t1, &t2->start_time, t2);
3298}
3299
3300/**
3301 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3302 * @scan: struct cgroup_scanner containing arguments for the scan
3303 *
3304 * Arguments include pointers to callback functions test_task() and
3305 * process_task().
3306 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3307 * and if it returns true, call process_task() for it also.
3308 * The test_task pointer may be NULL, meaning always true (select all tasks).
3309 * Effectively duplicates cgroup_iter_{start,next,end}()
3310 * but does not lock css_set_lock for the call to process_task().
3311 * The struct cgroup_scanner may be embedded in any structure of the caller's
3312 * creation.
3313 * It is guaranteed that process_task() will act on every task that
3314 * is a member of the cgroup for the duration of this call. This
3315 * function may or may not call process_task() for tasks that exit
3316 * or move to a different cgroup during the call, or are forked or
3317 * move into the cgroup during the call.
3318 *
3319 * Note that test_task() may be called with locks held, and may in some
3320 * situations be called multiple times for the same task, so it should
3321 * be cheap.
3322 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3323 * pre-allocated and will be used for heap operations (and its "gt" member will
3324 * be overwritten), else a temporary heap will be used (allocation of which
3325 * may cause this function to fail).
3326 */
3327int cgroup_scan_tasks(struct cgroup_scanner *scan)
3328{
3329 int retval, i;
3330 struct cgroup_iter it;
3331 struct task_struct *p, *dropped;
3332 /* Never dereference latest_task, since it's not refcounted */
3333 struct task_struct *latest_task = NULL;
3334 struct ptr_heap tmp_heap;
3335 struct ptr_heap *heap;
3336 struct timespec latest_time = { 0, 0 };
3337
3338 if (scan->heap) {
3339 /* The caller supplied our heap and pre-allocated its memory */
3340 heap = scan->heap;
3341 heap->gt = &started_after;
3342 } else {
3343 /* We need to allocate our own heap memory */
3344 heap = &tmp_heap;
3345 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3346 if (retval)
3347 /* cannot allocate the heap */
3348 return retval;
3349 }
3350
3351 again:
3352 /*
3353 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3354 * to determine which are of interest, and using the scanner's
3355 * "process_task" callback to process any of them that need an update.
3356 * Since we don't want to hold any locks during the task updates,
3357 * gather tasks to be processed in a heap structure.
3358 * The heap is sorted by descending task start time.
3359 * If the statically-sized heap fills up, we overflow tasks that
3360 * started later, and in future iterations only consider tasks that
3361 * started after the latest task in the previous pass. This
3362 * guarantees forward progress and that we don't miss any tasks.
3363 */
3364 heap->size = 0;
3365 cgroup_iter_start(scan->cg, &it);
3366 while ((p = cgroup_iter_next(scan->cg, &it))) {
3367 /*
3368 * Only affect tasks that qualify per the caller's callback,
3369 * if he provided one
3370 */
3371 if (scan->test_task && !scan->test_task(p, scan))
3372 continue;
3373 /*
3374 * Only process tasks that started after the last task
3375 * we processed
3376 */
3377 if (!started_after_time(p, &latest_time, latest_task))
3378 continue;
3379 dropped = heap_insert(heap, p);
3380 if (dropped == NULL) {
3381 /*
3382 * The new task was inserted; the heap wasn't
3383 * previously full
3384 */
3385 get_task_struct(p);
3386 } else if (dropped != p) {
3387 /*
3388 * The new task was inserted, and pushed out a
3389 * different task
3390 */
3391 get_task_struct(p);
3392 put_task_struct(dropped);
3393 }
3394 /*
3395 * Else the new task was newer than anything already in
3396 * the heap and wasn't inserted
3397 */
3398 }
3399 cgroup_iter_end(scan->cg, &it);
3400
3401 if (heap->size) {
3402 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003403 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003404 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003405 latest_time = q->start_time;
3406 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003407 }
3408 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003409 scan->process_task(q, scan);
3410 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003411 }
3412 /*
3413 * If we had to process any tasks at all, scan again
3414 * in case some of them were in the middle of forking
3415 * children that didn't get processed.
3416 * Not the most efficient way to do it, but it avoids
3417 * having to take callback_mutex in the fork path
3418 */
3419 goto again;
3420 }
3421 if (heap == &tmp_heap)
3422 heap_free(&tmp_heap);
3423 return 0;
3424}
3425
Tejun Heo8cc99342013-04-07 09:29:50 -07003426static void cgroup_transfer_one_task(struct task_struct *task,
3427 struct cgroup_scanner *scan)
3428{
3429 struct cgroup *new_cgroup = scan->data;
3430
Tejun Heo47cfcd02013-04-07 09:29:51 -07003431 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003432 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003433 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003434}
3435
3436/**
3437 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3438 * @to: cgroup to which the tasks will be moved
3439 * @from: cgroup in which the tasks currently reside
3440 */
3441int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3442{
3443 struct cgroup_scanner scan;
3444
3445 scan.cg = from;
3446 scan.test_task = NULL; /* select all tasks in cgroup */
3447 scan.process_task = cgroup_transfer_one_task;
3448 scan.heap = NULL;
3449 scan.data = to;
3450
3451 return cgroup_scan_tasks(&scan);
3452}
3453
Paul Menage817929e2007-10-18 23:39:36 -07003454/*
Ben Blum102a7752009-09-23 15:56:26 -07003455 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003456 *
3457 * Reading this file can return large amounts of data if a cgroup has
3458 * *lots* of attached tasks. So it may need several calls to read(),
3459 * but we cannot guarantee that the information we produce is correct
3460 * unless we produce it entirely atomically.
3461 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003462 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003463
Li Zefan24528252012-01-20 11:58:43 +08003464/* which pidlist file are we talking about? */
3465enum cgroup_filetype {
3466 CGROUP_FILE_PROCS,
3467 CGROUP_FILE_TASKS,
3468};
3469
3470/*
3471 * A pidlist is a list of pids that virtually represents the contents of one
3472 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3473 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3474 * to the cgroup.
3475 */
3476struct cgroup_pidlist {
3477 /*
3478 * used to find which pidlist is wanted. doesn't change as long as
3479 * this particular list stays in the list.
3480 */
3481 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3482 /* array of xids */
3483 pid_t *list;
3484 /* how many elements the above list has */
3485 int length;
3486 /* how many files are using the current array */
3487 int use_count;
3488 /* each of these stored in a list by its cgroup */
3489 struct list_head links;
3490 /* pointer to the cgroup we belong to, for list removal purposes */
3491 struct cgroup *owner;
3492 /* protects the other fields */
3493 struct rw_semaphore mutex;
3494};
3495
Paul Menagebbcb81d2007-10-18 23:39:32 -07003496/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003497 * The following two functions "fix" the issue where there are more pids
3498 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3499 * TODO: replace with a kernel-wide solution to this problem
3500 */
3501#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3502static void *pidlist_allocate(int count)
3503{
3504 if (PIDLIST_TOO_LARGE(count))
3505 return vmalloc(count * sizeof(pid_t));
3506 else
3507 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3508}
3509static void pidlist_free(void *p)
3510{
3511 if (is_vmalloc_addr(p))
3512 vfree(p);
3513 else
3514 kfree(p);
3515}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003516
3517/*
Ben Blum102a7752009-09-23 15:56:26 -07003518 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003519 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003520 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003521static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003522{
Ben Blum102a7752009-09-23 15:56:26 -07003523 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003524
3525 /*
3526 * we presume the 0th element is unique, so i starts at 1. trivial
3527 * edge cases first; no work needs to be done for either
3528 */
3529 if (length == 0 || length == 1)
3530 return length;
3531 /* src and dest walk down the list; dest counts unique elements */
3532 for (src = 1; src < length; src++) {
3533 /* find next unique element */
3534 while (list[src] == list[src-1]) {
3535 src++;
3536 if (src == length)
3537 goto after;
3538 }
3539 /* dest always points to where the next unique element goes */
3540 list[dest] = list[src];
3541 dest++;
3542 }
3543after:
Ben Blum102a7752009-09-23 15:56:26 -07003544 return dest;
3545}
3546
3547static int cmppid(const void *a, const void *b)
3548{
3549 return *(pid_t *)a - *(pid_t *)b;
3550}
3551
3552/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003553 * find the appropriate pidlist for our purpose (given procs vs tasks)
3554 * returns with the lock on that pidlist already held, and takes care
3555 * of the use count, or returns NULL with no locks held if we're out of
3556 * memory.
3557 */
3558static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3559 enum cgroup_filetype type)
3560{
3561 struct cgroup_pidlist *l;
3562 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003563 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003564
Ben Blum72a8cb32009-09-23 15:56:27 -07003565 /*
3566 * We can't drop the pidlist_mutex before taking the l->mutex in case
3567 * the last ref-holder is trying to remove l from the list at the same
3568 * time. Holding the pidlist_mutex precludes somebody taking whichever
3569 * list we find out from under us - compare release_pid_array().
3570 */
3571 mutex_lock(&cgrp->pidlist_mutex);
3572 list_for_each_entry(l, &cgrp->pidlists, links) {
3573 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003574 /* make sure l doesn't vanish out from under us */
3575 down_write(&l->mutex);
3576 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003577 return l;
3578 }
3579 }
3580 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003581 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 if (!l) {
3583 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 return l;
3585 }
3586 init_rwsem(&l->mutex);
3587 down_write(&l->mutex);
3588 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003589 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 l->owner = cgrp;
3591 list_add(&l->links, &cgrp->pidlists);
3592 mutex_unlock(&cgrp->pidlist_mutex);
3593 return l;
3594}
3595
3596/*
Ben Blum102a7752009-09-23 15:56:26 -07003597 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3598 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003599static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3600 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003601{
3602 pid_t *array;
3603 int length;
3604 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003605 struct cgroup_iter it;
3606 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003607 struct cgroup_pidlist *l;
3608
3609 /*
3610 * If cgroup gets more users after we read count, we won't have
3611 * enough space - tough. This race is indistinguishable to the
3612 * caller from the case that the additional cgroup users didn't
3613 * show up until sometime later on.
3614 */
3615 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003616 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003617 if (!array)
3618 return -ENOMEM;
3619 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003620 cgroup_iter_start(cgrp, &it);
3621 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003622 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003623 break;
Ben Blum102a7752009-09-23 15:56:26 -07003624 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003625 if (type == CGROUP_FILE_PROCS)
3626 pid = task_tgid_vnr(tsk);
3627 else
3628 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003629 if (pid > 0) /* make sure to only use valid results */
3630 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003631 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003632 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003633 length = n;
3634 /* now sort & (if procs) strip out duplicates */
3635 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003637 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003638 l = cgroup_pidlist_find(cgrp, type);
3639 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003640 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003641 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003642 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003643 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003644 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003645 l->list = array;
3646 l->length = length;
3647 l->use_count++;
3648 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003650 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003651}
3652
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653/**
Li Zefana043e3b2008-02-23 15:24:09 -08003654 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003655 * @stats: cgroupstats to fill information into
3656 * @dentry: A dentry entry belonging to the cgroup for which stats have
3657 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003658 *
3659 * Build and fill cgroupstats so that taskstats can export it to user
3660 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661 */
3662int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3663{
3664 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003665 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003666 struct cgroup_iter it;
3667 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003668
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003670 * Validate dentry by checking the superblock operations,
3671 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003672 */
Li Zefan33d283b2008-11-19 15:36:48 -08003673 if (dentry->d_sb->s_op != &cgroup_ops ||
3674 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003675 goto err;
3676
3677 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003678 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003679
Paul Menagebd89aab2007-10-18 23:40:44 -07003680 cgroup_iter_start(cgrp, &it);
3681 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003682 switch (tsk->state) {
3683 case TASK_RUNNING:
3684 stats->nr_running++;
3685 break;
3686 case TASK_INTERRUPTIBLE:
3687 stats->nr_sleeping++;
3688 break;
3689 case TASK_UNINTERRUPTIBLE:
3690 stats->nr_uninterruptible++;
3691 break;
3692 case TASK_STOPPED:
3693 stats->nr_stopped++;
3694 break;
3695 default:
3696 if (delayacct_is_task_waiting_on_io(tsk))
3697 stats->nr_io_wait++;
3698 break;
3699 }
3700 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003701 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003702
Balbir Singh846c7bb2007-10-18 23:39:44 -07003703err:
3704 return ret;
3705}
3706
Paul Menage8f3ff202009-09-23 15:56:25 -07003707
Paul Menagecc31edc2008-10-18 20:28:04 -07003708/*
Ben Blum102a7752009-09-23 15:56:26 -07003709 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003710 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003711 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003712 */
3713
Ben Blum102a7752009-09-23 15:56:26 -07003714static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003715{
3716 /*
3717 * Initially we receive a position value that corresponds to
3718 * one more than the last pid shown (or 0 on the first call or
3719 * after a seek to the start). Use a binary-search to find the
3720 * next pid to display, if any
3721 */
Ben Blum102a7752009-09-23 15:56:26 -07003722 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003723 int index = 0, pid = *pos;
3724 int *iter;
3725
Ben Blum102a7752009-09-23 15:56:26 -07003726 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003728 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003729
Paul Menagecc31edc2008-10-18 20:28:04 -07003730 while (index < end) {
3731 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003732 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003733 index = mid;
3734 break;
Ben Blum102a7752009-09-23 15:56:26 -07003735 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003736 index = mid + 1;
3737 else
3738 end = mid;
3739 }
3740 }
3741 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003742 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003743 return NULL;
3744 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003745 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003746 *pos = *iter;
3747 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003748}
3749
Ben Blum102a7752009-09-23 15:56:26 -07003750static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003751{
Ben Blum102a7752009-09-23 15:56:26 -07003752 struct cgroup_pidlist *l = s->private;
3753 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003754}
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003757{
Ben Blum102a7752009-09-23 15:56:26 -07003758 struct cgroup_pidlist *l = s->private;
3759 pid_t *p = v;
3760 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003761 /*
3762 * Advance to the next pid in the array. If this goes off the
3763 * end, we're done
3764 */
3765 p++;
3766 if (p >= end) {
3767 return NULL;
3768 } else {
3769 *pos = *p;
3770 return p;
3771 }
3772}
3773
Ben Blum102a7752009-09-23 15:56:26 -07003774static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003775{
3776 return seq_printf(s, "%d\n", *(int *)v);
3777}
3778
Ben Blum102a7752009-09-23 15:56:26 -07003779/*
3780 * seq_operations functions for iterating on pidlists through seq_file -
3781 * independent of whether it's tasks or procs
3782 */
3783static const struct seq_operations cgroup_pidlist_seq_operations = {
3784 .start = cgroup_pidlist_start,
3785 .stop = cgroup_pidlist_stop,
3786 .next = cgroup_pidlist_next,
3787 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003788};
3789
Ben Blum102a7752009-09-23 15:56:26 -07003790static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003791{
Ben Blum72a8cb32009-09-23 15:56:27 -07003792 /*
3793 * the case where we're the last user of this particular pidlist will
3794 * have us remove it from the cgroup's list, which entails taking the
3795 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3796 * pidlist_mutex, we have to take pidlist_mutex first.
3797 */
3798 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003799 down_write(&l->mutex);
3800 BUG_ON(!l->use_count);
3801 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003802 /* we're the last user if refcount is 0; remove and free */
3803 list_del(&l->links);
3804 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003805 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003806 put_pid_ns(l->key.ns);
3807 up_write(&l->mutex);
3808 kfree(l);
3809 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003810 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003811 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003812 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003813}
3814
Ben Blum102a7752009-09-23 15:56:26 -07003815static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816{
Ben Blum102a7752009-09-23 15:56:26 -07003817 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003818 if (!(file->f_mode & FMODE_READ))
3819 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003820 /*
3821 * the seq_file will only be initialized if the file was opened for
3822 * reading; hence we check if it's not null only in that case.
3823 */
3824 l = ((struct seq_file *)file->private_data)->private;
3825 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 return seq_release(inode, file);
3827}
3828
Ben Blum102a7752009-09-23 15:56:26 -07003829static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003830 .read = seq_read,
3831 .llseek = seq_lseek,
3832 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003833 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003834};
3835
3836/*
Ben Blum102a7752009-09-23 15:56:26 -07003837 * The following functions handle opens on a file that displays a pidlist
3838 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3839 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003840 */
Ben Blum102a7752009-09-23 15:56:26 -07003841/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003842static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003843{
3844 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003845 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003846 int retval;
3847
3848 /* Nothing to do for write-only files */
3849 if (!(file->f_mode & FMODE_READ))
3850 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003851
Ben Blum102a7752009-09-23 15:56:26 -07003852 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003853 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003854 if (retval)
3855 return retval;
3856 /* configure file information */
3857 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003858
Ben Blum102a7752009-09-23 15:56:26 -07003859 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003860 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003861 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003862 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003863 }
Ben Blum102a7752009-09-23 15:56:26 -07003864 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003865 return 0;
3866}
Ben Blum102a7752009-09-23 15:56:26 -07003867static int cgroup_tasks_open(struct inode *unused, struct file *file)
3868{
Ben Blum72a8cb32009-09-23 15:56:27 -07003869 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003870}
3871static int cgroup_procs_open(struct inode *unused, struct file *file)
3872{
Ben Blum72a8cb32009-09-23 15:56:27 -07003873 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003874}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003875
Paul Menagebd89aab2007-10-18 23:40:44 -07003876static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003877 struct cftype *cft)
3878{
Paul Menagebd89aab2007-10-18 23:40:44 -07003879 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003880}
3881
Paul Menage6379c102008-07-25 01:47:01 -07003882static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3883 struct cftype *cft,
3884 u64 val)
3885{
3886 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3887 if (val)
3888 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3889 else
3890 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3891 return 0;
3892}
3893
Paul Menagebbcb81d2007-10-18 23:39:32 -07003894/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003895 * When dput() is called asynchronously, if umount has been done and
3896 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3897 * there's a small window that vfs will see the root dentry with non-zero
3898 * refcnt and trigger BUG().
3899 *
3900 * That's why we hold a reference before dput() and drop it right after.
3901 */
3902static void cgroup_dput(struct cgroup *cgrp)
3903{
3904 struct super_block *sb = cgrp->root->sb;
3905
3906 atomic_inc(&sb->s_active);
3907 dput(cgrp->dentry);
3908 deactivate_super(sb);
3909}
3910
3911/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003912 * Unregister event and free resources.
3913 *
3914 * Gets called from workqueue.
3915 */
3916static void cgroup_event_remove(struct work_struct *work)
3917{
3918 struct cgroup_event *event = container_of(work, struct cgroup_event,
3919 remove);
3920 struct cgroup *cgrp = event->cgrp;
3921
Li Zefan810cbee2013-02-18 18:56:14 +08003922 remove_wait_queue(event->wqh, &event->wait);
3923
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003924 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3925
Li Zefan810cbee2013-02-18 18:56:14 +08003926 /* Notify userspace the event is going away. */
3927 eventfd_signal(event->eventfd, 1);
3928
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003929 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003930 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003931 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003932}
3933
3934/*
3935 * Gets called on POLLHUP on eventfd when user closes it.
3936 *
3937 * Called with wqh->lock held and interrupts disabled.
3938 */
3939static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3940 int sync, void *key)
3941{
3942 struct cgroup_event *event = container_of(wait,
3943 struct cgroup_event, wait);
3944 struct cgroup *cgrp = event->cgrp;
3945 unsigned long flags = (unsigned long)key;
3946
3947 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003948 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003949 * If the event has been detached at cgroup removal, we
3950 * can simply return knowing the other side will cleanup
3951 * for us.
3952 *
3953 * We can't race against event freeing since the other
3954 * side will require wqh->lock via remove_wait_queue(),
3955 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003956 */
Li Zefan810cbee2013-02-18 18:56:14 +08003957 spin_lock(&cgrp->event_list_lock);
3958 if (!list_empty(&event->list)) {
3959 list_del_init(&event->list);
3960 /*
3961 * We are in atomic context, but cgroup_event_remove()
3962 * may sleep, so we have to call it in workqueue.
3963 */
3964 schedule_work(&event->remove);
3965 }
3966 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003967 }
3968
3969 return 0;
3970}
3971
3972static void cgroup_event_ptable_queue_proc(struct file *file,
3973 wait_queue_head_t *wqh, poll_table *pt)
3974{
3975 struct cgroup_event *event = container_of(pt,
3976 struct cgroup_event, pt);
3977
3978 event->wqh = wqh;
3979 add_wait_queue(wqh, &event->wait);
3980}
3981
3982/*
3983 * Parse input and register new cgroup event handler.
3984 *
3985 * Input must be in format '<event_fd> <control_fd> <args>'.
3986 * Interpretation of args is defined by control file implementation.
3987 */
3988static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3989 const char *buffer)
3990{
3991 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003992 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003993 unsigned int efd, cfd;
3994 struct file *efile = NULL;
3995 struct file *cfile = NULL;
3996 char *endp;
3997 int ret;
3998
3999 efd = simple_strtoul(buffer, &endp, 10);
4000 if (*endp != ' ')
4001 return -EINVAL;
4002 buffer = endp + 1;
4003
4004 cfd = simple_strtoul(buffer, &endp, 10);
4005 if ((*endp != ' ') && (*endp != '\0'))
4006 return -EINVAL;
4007 buffer = endp + 1;
4008
4009 event = kzalloc(sizeof(*event), GFP_KERNEL);
4010 if (!event)
4011 return -ENOMEM;
4012 event->cgrp = cgrp;
4013 INIT_LIST_HEAD(&event->list);
4014 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4015 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4016 INIT_WORK(&event->remove, cgroup_event_remove);
4017
4018 efile = eventfd_fget(efd);
4019 if (IS_ERR(efile)) {
4020 ret = PTR_ERR(efile);
4021 goto fail;
4022 }
4023
4024 event->eventfd = eventfd_ctx_fileget(efile);
4025 if (IS_ERR(event->eventfd)) {
4026 ret = PTR_ERR(event->eventfd);
4027 goto fail;
4028 }
4029
4030 cfile = fget(cfd);
4031 if (!cfile) {
4032 ret = -EBADF;
4033 goto fail;
4034 }
4035
4036 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004037 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004038 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004039 if (ret < 0)
4040 goto fail;
4041
4042 event->cft = __file_cft(cfile);
4043 if (IS_ERR(event->cft)) {
4044 ret = PTR_ERR(event->cft);
4045 goto fail;
4046 }
4047
Li Zefanf1690072013-02-18 14:13:35 +08004048 /*
4049 * The file to be monitored must be in the same cgroup as
4050 * cgroup.event_control is.
4051 */
4052 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4053 if (cgrp_cfile != cgrp) {
4054 ret = -EINVAL;
4055 goto fail;
4056 }
4057
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004058 if (!event->cft->register_event || !event->cft->unregister_event) {
4059 ret = -EINVAL;
4060 goto fail;
4061 }
4062
4063 ret = event->cft->register_event(cgrp, event->cft,
4064 event->eventfd, buffer);
4065 if (ret)
4066 goto fail;
4067
Li Zefan7ef70e42013-04-26 11:58:03 -07004068 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004069
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004070 /*
4071 * Events should be removed after rmdir of cgroup directory, but before
4072 * destroying subsystem state objects. Let's take reference to cgroup
4073 * directory dentry to do that.
4074 */
4075 dget(cgrp->dentry);
4076
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004077 spin_lock(&cgrp->event_list_lock);
4078 list_add(&event->list, &cgrp->event_list);
4079 spin_unlock(&cgrp->event_list_lock);
4080
4081 fput(cfile);
4082 fput(efile);
4083
4084 return 0;
4085
4086fail:
4087 if (cfile)
4088 fput(cfile);
4089
4090 if (event && event->eventfd && !IS_ERR(event->eventfd))
4091 eventfd_ctx_put(event->eventfd);
4092
4093 if (!IS_ERR_OR_NULL(efile))
4094 fput(efile);
4095
4096 kfree(event);
4097
4098 return ret;
4099}
4100
Daniel Lezcano97978e62010-10-27 15:33:35 -07004101static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4102 struct cftype *cft)
4103{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004104 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004105}
4106
4107static int cgroup_clone_children_write(struct cgroup *cgrp,
4108 struct cftype *cft,
4109 u64 val)
4110{
4111 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004112 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004113 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004114 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004115 return 0;
4116}
4117
Tejun Heod5c56ce2013-06-03 19:14:34 -07004118static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004119 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004120 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004121 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004122 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004123 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004124 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004125 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004126 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004127 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004128 .write_string = cgroup_write_event_control,
4129 .mode = S_IWUGO,
4130 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004131 {
4132 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004133 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004134 .read_u64 = cgroup_clone_children_read,
4135 .write_u64 = cgroup_clone_children_write,
4136 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004137 {
Tejun Heo873fe092013-04-14 20:15:26 -07004138 .name = "cgroup.sane_behavior",
4139 .flags = CFTYPE_ONLY_ON_ROOT,
4140 .read_seq_string = cgroup_sane_behavior_show,
4141 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004142
4143 /*
4144 * Historical crazy stuff. These don't have "cgroup." prefix and
4145 * don't exist if sane_behavior. If you're depending on these, be
4146 * prepared to be burned.
4147 */
4148 {
4149 .name = "tasks",
4150 .flags = CFTYPE_INSANE, /* use "procs" instead */
4151 .open = cgroup_tasks_open,
4152 .write_u64 = cgroup_tasks_write,
4153 .release = cgroup_pidlist_release,
4154 .mode = S_IRUGO | S_IWUSR,
4155 },
4156 {
4157 .name = "notify_on_release",
4158 .flags = CFTYPE_INSANE,
4159 .read_u64 = cgroup_read_notify_on_release,
4160 .write_u64 = cgroup_write_notify_on_release,
4161 },
Tejun Heo873fe092013-04-14 20:15:26 -07004162 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004163 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004164 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004165 .read_seq_string = cgroup_release_agent_show,
4166 .write_string = cgroup_release_agent_write,
4167 .max_write_len = PATH_MAX,
4168 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004169 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004170};
4171
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004172/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004173 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004174 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004175 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004176 *
4177 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004178 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004179static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004181 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004182 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004183
Tejun Heo8e3f6542012-04-01 12:09:55 -07004184 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004185 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004186 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004187
4188 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004189 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004190
Tejun Heobee55092013-06-28 16:24:11 -07004191 list_for_each_entry(set, &ss->cftsets, node) {
4192 ret = cgroup_addrm_files(cgrp, ss, set->cfts, true);
4193 if (ret < 0)
4194 goto err;
4195 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004197
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004198 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004199 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004200 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004201 struct css_id *id = rcu_dereference_protected(css->id, true);
4202
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004203 /*
4204 * Update id->css pointer and make this css visible from
4205 * CSS ID functions. This pointer will be dereferened
4206 * from RCU-read-side without locks.
4207 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004208 if (id)
4209 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004210 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004211
4212 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004213err:
4214 cgroup_clear_dir(cgrp, subsys_mask);
4215 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216}
4217
Tejun Heo48ddbe12012-04-01 12:09:56 -07004218static void css_dput_fn(struct work_struct *work)
4219{
4220 struct cgroup_subsys_state *css =
4221 container_of(work, struct cgroup_subsys_state, dput_work);
4222
Li Zefan1c8158e2013-06-18 18:41:10 +08004223 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004224}
4225
Tejun Heod3daf282013-06-13 19:39:16 -07004226static void css_release(struct percpu_ref *ref)
4227{
4228 struct cgroup_subsys_state *css =
4229 container_of(ref, struct cgroup_subsys_state, refcnt);
4230
4231 schedule_work(&css->dput_work);
4232}
4233
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234static void init_cgroup_css(struct cgroup_subsys_state *css,
4235 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004236 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237{
Paul Menagebd89aab2007-10-18 23:40:44 -07004238 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004240 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004241 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004242 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004243 BUG_ON(cgrp->subsys[ss->subsys_id]);
4244 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004245
4246 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004247 * css holds an extra ref to @cgrp->dentry which is put on the last
4248 * css_put(). dput() requires process context, which css_put() may
4249 * be called without. @css->dput_work will be used to invoke
4250 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004251 */
4252 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004253}
4254
Tejun Heob1929db2012-11-19 08:13:38 -08004255/* invoke ->post_create() on a new CSS and mark it online if successful */
4256static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004257{
Tejun Heob1929db2012-11-19 08:13:38 -08004258 int ret = 0;
4259
Tejun Heoa31f2d32012-11-19 08:13:37 -08004260 lockdep_assert_held(&cgroup_mutex);
4261
Tejun Heo92fb9742012-11-19 08:13:38 -08004262 if (ss->css_online)
4263 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004264 if (!ret)
4265 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4266 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004267}
4268
4269/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4270static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4271 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4272{
4273 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4274
4275 lockdep_assert_held(&cgroup_mutex);
4276
4277 if (!(css->flags & CSS_ONLINE))
4278 return;
4279
Li Zefand7eeac12013-03-12 15:35:59 -07004280 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004281 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004282
4283 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4284}
4285
Paul Menageddbcc7e2007-10-18 23:39:30 -07004286/*
Li Zefana043e3b2008-02-23 15:24:09 -08004287 * cgroup_create - create a cgroup
4288 * @parent: cgroup that will be parent of the new cgroup
4289 * @dentry: dentry of the new cgroup
4290 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291 *
Li Zefana043e3b2008-02-23 15:24:09 -08004292 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004293 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004295 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004296{
Paul Menagebd89aab2007-10-18 23:40:44 -07004297 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004298 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299 struct cgroupfs_root *root = parent->root;
4300 int err = 0;
4301 struct cgroup_subsys *ss;
4302 struct super_block *sb = root->sb;
4303
Tejun Heo0a950f62012-11-19 09:02:12 -08004304 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004305 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4306 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307 return -ENOMEM;
4308
Li Zefan65dff752013-03-01 15:01:56 +08004309 name = cgroup_alloc_name(dentry);
4310 if (!name)
4311 goto err_free_cgrp;
4312 rcu_assign_pointer(cgrp->name, name);
4313
Tejun Heo0a950f62012-11-19 09:02:12 -08004314 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4315 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004316 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004317
Tejun Heo976c06b2012-11-05 09:16:59 -08004318 /*
4319 * Only live parents can have children. Note that the liveliness
4320 * check isn't strictly necessary because cgroup_mkdir() and
4321 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4322 * anyway so that locking is contained inside cgroup proper and we
4323 * don't get nasty surprises if we ever grow another caller.
4324 */
4325 if (!cgroup_lock_live_group(parent)) {
4326 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004327 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004328 }
4329
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330 /* Grab a reference on the superblock so the hierarchy doesn't
4331 * get deleted on unmount if there are child cgroups. This
4332 * can be done outside cgroup_mutex, since the sb can't
4333 * disappear while someone has an open control file on the
4334 * fs */
4335 atomic_inc(&sb->s_active);
4336
Paul Menagecc31edc2008-10-18 20:28:04 -07004337 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338
Li Zefanfe1c06c2013-01-24 14:30:22 +08004339 dentry->d_fsdata = cgrp;
4340 cgrp->dentry = dentry;
4341
Paul Menagebd89aab2007-10-18 23:40:44 -07004342 cgrp->parent = parent;
4343 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344
Li Zefanb6abdb02008-03-04 14:28:19 -08004345 if (notify_on_release(parent))
4346 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4347
Tejun Heo2260e7f2012-11-19 08:13:38 -08004348 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4349 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004350
Tejun Heo5549c492013-06-24 15:21:48 -07004351 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004352 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004353
Tejun Heo92fb9742012-11-19 08:13:38 -08004354 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004355 if (IS_ERR(css)) {
4356 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004357 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358 }
Tejun Heod3daf282013-06-13 19:39:16 -07004359
4360 err = percpu_ref_init(&css->refcnt, css_release);
4361 if (err)
4362 goto err_free_all;
4363
Paul Menagebd89aab2007-10-18 23:40:44 -07004364 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004365
Li Zefan4528fd02010-02-02 13:44:10 -08004366 if (ss->use_id) {
4367 err = alloc_css_id(ss, parent, cgrp);
4368 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004369 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004370 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371 }
4372
Tejun Heo4e139af2012-11-19 08:13:36 -08004373 /*
4374 * Create directory. cgroup_create_file() returns with the new
4375 * directory locked on success so that it can be populated without
4376 * dropping cgroup_mutex.
4377 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004378 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004380 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004381 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382
Tejun Heo00356bd2013-06-18 11:14:22 -07004383 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004384
Tejun Heo4e139af2012-11-19 08:13:36 -08004385 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004386 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4387 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004388
Tejun Heob1929db2012-11-19 08:13:38 -08004389 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004390 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004391 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004392
Li Zefan415cf072013-04-08 14:35:02 +08004393 /* hold a ref to the parent's dentry */
4394 dget(parent->dentry);
4395
Tejun Heob1929db2012-11-19 08:13:38 -08004396 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004397 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004398 err = online_css(ss, cgrp);
4399 if (err)
4400 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004401
4402 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4403 parent->parent) {
4404 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",
4405 current->comm, current->pid, ss->name);
4406 if (!strcmp(ss->name, "memory"))
4407 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4408 ss->warned_broken_hierarchy = true;
4409 }
Tejun Heoa8638032012-11-09 09:12:29 -08004410 }
4411
Tejun Heo628f7cd2013-06-28 16:24:11 -07004412 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4413 if (err)
4414 goto err_destroy;
4415
4416 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004417 if (err)
4418 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419
4420 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004421 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422
4423 return 0;
4424
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004425err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004426 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004427 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4428
4429 if (css) {
4430 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004431 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004432 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004433 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004434 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004435 /* Release the reference count that we took on the superblock */
4436 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004437err_free_id:
4438 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004439err_free_name:
4440 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004441err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004442 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004443 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004444
4445err_destroy:
4446 cgroup_destroy_locked(cgrp);
4447 mutex_unlock(&cgroup_mutex);
4448 mutex_unlock(&dentry->d_inode->i_mutex);
4449 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004450}
4451
Al Viro18bb1db2011-07-26 01:41:39 -04004452static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004453{
4454 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4455
4456 /* the vfs holds inode->i_mutex already */
4457 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4458}
4459
Tejun Heod3daf282013-06-13 19:39:16 -07004460static void cgroup_css_killed(struct cgroup *cgrp)
4461{
4462 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4463 return;
4464
4465 /* percpu ref's of all css's are killed, kick off the next step */
4466 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4467 schedule_work(&cgrp->destroy_work);
4468}
4469
4470static void css_ref_killed_fn(struct percpu_ref *ref)
4471{
4472 struct cgroup_subsys_state *css =
4473 container_of(ref, struct cgroup_subsys_state, refcnt);
4474
4475 cgroup_css_killed(css->cgroup);
4476}
4477
4478/**
4479 * cgroup_destroy_locked - the first stage of cgroup destruction
4480 * @cgrp: cgroup to be destroyed
4481 *
4482 * css's make use of percpu refcnts whose killing latency shouldn't be
4483 * exposed to userland and are RCU protected. Also, cgroup core needs to
4484 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4485 * invoked. To satisfy all the requirements, destruction is implemented in
4486 * the following two steps.
4487 *
4488 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4489 * userland visible parts and start killing the percpu refcnts of
4490 * css's. Set up so that the next stage will be kicked off once all
4491 * the percpu refcnts are confirmed to be killed.
4492 *
4493 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4494 * rest of destruction. Once all cgroup references are gone, the
4495 * cgroup is RCU-freed.
4496 *
4497 * This function implements s1. After this step, @cgrp is gone as far as
4498 * the userland is concerned and a new cgroup with the same name may be
4499 * created. As cgroup doesn't care about the names internally, this
4500 * doesn't cause any problem.
4501 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004502static int cgroup_destroy_locked(struct cgroup *cgrp)
4503 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004504{
Tejun Heo42809dd2012-11-19 08:13:37 -08004505 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004506 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004507 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004508 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004509
Tejun Heo42809dd2012-11-19 08:13:37 -08004510 lockdep_assert_held(&d->d_inode->i_mutex);
4511 lockdep_assert_held(&cgroup_mutex);
4512
Tejun Heoddd69142013-06-12 21:04:54 -07004513 /*
Tejun Heo6f3d8282013-06-12 21:04:55 -07004514 * css_set_lock synchronizes access to ->cset_links and prevents
4515 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004516 */
4517 read_lock(&css_set_lock);
Tejun Heo6f3d8282013-06-12 21:04:55 -07004518 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004519 read_unlock(&css_set_lock);
4520 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004521 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004522
Tejun Heo1a90dd52012-11-05 09:16:59 -08004523 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004524 * Block new css_tryget() by killing css refcnts. cgroup core
4525 * guarantees that, by the time ->css_offline() is invoked, no new
4526 * css reference will be given out via css_tryget(). We can't
4527 * simply call percpu_ref_kill() and proceed to offlining css's
4528 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4529 * as killed on all CPUs on return.
4530 *
4531 * Use percpu_ref_kill_and_confirm() to get notifications as each
4532 * css is confirmed to be seen as killed on all CPUs. The
4533 * notification callback keeps track of the number of css's to be
4534 * killed and schedules cgroup_offline_fn() to perform the rest of
4535 * destruction once the percpu refs of all css's are confirmed to
4536 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004537 */
Tejun Heod3daf282013-06-13 19:39:16 -07004538 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004539 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004540 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4541
Tejun Heod3daf282013-06-13 19:39:16 -07004542 /*
4543 * Killing would put the base ref, but we need to keep it
4544 * alive until after ->css_offline.
4545 */
4546 percpu_ref_get(&css->refcnt);
4547
4548 atomic_inc(&cgrp->css_kill_cnt);
4549 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004550 }
Tejun Heod3daf282013-06-13 19:39:16 -07004551 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004552
4553 /*
4554 * Mark @cgrp dead. This prevents further task migration and child
4555 * creation by disabling cgroup_lock_live_group(). Note that
4556 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4557 * resume iteration after dropping RCU read lock. See
4558 * cgroup_next_sibling() for details.
4559 */
Tejun Heo54766d42013-06-12 21:04:53 -07004560 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004561
Tejun Heo455050d2013-06-13 19:27:41 -07004562 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4563 raw_spin_lock(&release_list_lock);
4564 if (!list_empty(&cgrp->release_list))
4565 list_del_init(&cgrp->release_list);
4566 raw_spin_unlock(&release_list_lock);
4567
4568 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004569 * Clear and remove @cgrp directory. The removal puts the base ref
4570 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004571 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004572 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
4573 cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004574 dget(d);
4575 cgroup_d_remove_dir(d);
4576
4577 /*
4578 * Unregister events and notify userspace.
4579 * Notify userspace about cgroup removing only after rmdir of cgroup
4580 * directory to avoid race between userspace and kernelspace.
4581 */
4582 spin_lock(&cgrp->event_list_lock);
4583 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4584 list_del_init(&event->list);
4585 schedule_work(&event->remove);
4586 }
4587 spin_unlock(&cgrp->event_list_lock);
4588
Tejun Heoea15f8c2013-06-13 19:27:42 -07004589 return 0;
4590};
4591
Tejun Heod3daf282013-06-13 19:39:16 -07004592/**
4593 * cgroup_offline_fn - the second step of cgroup destruction
4594 * @work: cgroup->destroy_free_work
4595 *
4596 * This function is invoked from a work item for a cgroup which is being
4597 * destroyed after the percpu refcnts of all css's are guaranteed to be
4598 * seen as killed on all CPUs, and performs the rest of destruction. This
4599 * is the second step of destruction described in the comment above
4600 * cgroup_destroy_locked().
4601 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004602static void cgroup_offline_fn(struct work_struct *work)
4603{
4604 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4605 struct cgroup *parent = cgrp->parent;
4606 struct dentry *d = cgrp->dentry;
4607 struct cgroup_subsys *ss;
4608
4609 mutex_lock(&cgroup_mutex);
4610
Tejun Heod3daf282013-06-13 19:39:16 -07004611 /*
4612 * css_tryget() is guaranteed to fail now. Tell subsystems to
4613 * initate destruction.
4614 */
Tejun Heo5549c492013-06-24 15:21:48 -07004615 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004616 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004617
4618 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004619 * Put the css refs from cgroup_destroy_locked(). Each css holds
4620 * an extra reference to the cgroup's dentry and cgroup removal
4621 * proceeds regardless of css refs. On the last put of each css,
4622 * whenever that may be, the extra dentry ref is put so that dentry
4623 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004624 */
Tejun Heo5549c492013-06-24 15:21:48 -07004625 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004626 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004627
Paul Menage999cd8a2009-01-07 18:08:36 -08004628 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004629 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004630
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
Paul Menagebd89aab2007-10-18 23:40:44 -07004633 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004634 check_for_release(parent);
4635
Tejun Heoea15f8c2013-06-13 19:27:42 -07004636 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637}
4638
Tejun Heo42809dd2012-11-19 08:13:37 -08004639static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4640{
4641 int ret;
4642
4643 mutex_lock(&cgroup_mutex);
4644 ret = cgroup_destroy_locked(dentry->d_fsdata);
4645 mutex_unlock(&cgroup_mutex);
4646
4647 return ret;
4648}
4649
Tejun Heo8e3f6542012-04-01 12:09:55 -07004650static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4651{
4652 INIT_LIST_HEAD(&ss->cftsets);
4653
4654 /*
4655 * base_cftset is embedded in subsys itself, no need to worry about
4656 * deregistration.
4657 */
4658 if (ss->base_cftypes) {
4659 ss->base_cftset.cfts = ss->base_cftypes;
4660 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4661 }
4662}
4663
Li Zefan06a11922008-04-29 01:00:07 -07004664static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004665{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004667
4668 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669
Tejun Heo648bb562012-11-19 08:13:36 -08004670 mutex_lock(&cgroup_mutex);
4671
Tejun Heo8e3f6542012-04-01 12:09:55 -07004672 /* init base cftset */
4673 cgroup_init_cftsets(ss);
4674
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004676 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4677 ss->root = &cgroup_dummy_root;
4678 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679 /* We don't handle early failures gracefully */
4680 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004681 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682
Li Zefane8d55fd2008-04-29 01:00:13 -07004683 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004684 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004685 * newly registered, all tasks and hence the
4686 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004687 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688
4689 need_forkexit_callback |= ss->fork || ss->exit;
4690
Li Zefane8d55fd2008-04-29 01:00:13 -07004691 /* At system boot, before all subsystems have been
4692 * registered, no tasks have been forked, so we don't
4693 * need to invoke fork callbacks here. */
4694 BUG_ON(!list_empty(&init_task.tasks));
4695
Tejun Heo9871bf92013-06-24 15:21:47 -07004696 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004697
Tejun Heo648bb562012-11-19 08:13:36 -08004698 mutex_unlock(&cgroup_mutex);
4699
Ben Blume6a11052010-03-10 15:22:09 -08004700 /* this function shouldn't be used with modular subsystems, since they
4701 * need to register a subsys_id, among other things */
4702 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004703}
4704
4705/**
Ben Blume6a11052010-03-10 15:22:09 -08004706 * cgroup_load_subsys: load and register a modular subsystem at runtime
4707 * @ss: the subsystem to load
4708 *
4709 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004710 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004711 * up for use. If the subsystem is built-in anyway, work is delegated to the
4712 * simpler cgroup_init_subsys.
4713 */
4714int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4715{
Ben Blume6a11052010-03-10 15:22:09 -08004716 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004717 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004718 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004719 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004720 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004721
4722 /* check name and function validity */
4723 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004724 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004725 return -EINVAL;
4726
4727 /*
4728 * we don't support callbacks in modular subsystems. this check is
4729 * before the ss->module check for consistency; a subsystem that could
4730 * be a module should still have no callbacks even if the user isn't
4731 * compiling it as one.
4732 */
4733 if (ss->fork || ss->exit)
4734 return -EINVAL;
4735
4736 /*
4737 * an optionally modular subsystem is built-in: we want to do nothing,
4738 * since cgroup_init_subsys will have already taken care of it.
4739 */
4740 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004741 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004742 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004743 return 0;
4744 }
4745
Tejun Heo8e3f6542012-04-01 12:09:55 -07004746 /* init base cftset */
4747 cgroup_init_cftsets(ss);
4748
Ben Blume6a11052010-03-10 15:22:09 -08004749 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004750 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004751
4752 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004753 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004754 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004755 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004756 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004757 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004758 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004759 /* failure case - need to deassign the cgroup_subsys[] slot. */
4760 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004761 mutex_unlock(&cgroup_mutex);
4762 return PTR_ERR(css);
4763 }
4764
Tejun Heo9871bf92013-06-24 15:21:47 -07004765 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4766 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004767
4768 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004769 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004770 /* init_idr must be after init_cgroup_css because it sets css->id. */
4771 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004772 ret = cgroup_init_idr(ss, css);
4773 if (ret)
4774 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004775 }
4776
4777 /*
4778 * Now we need to entangle the css into the existing css_sets. unlike
4779 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4780 * will need a new pointer to it; done by iterating the css_set_table.
4781 * furthermore, modifying the existing css_sets will corrupt the hash
4782 * table state, so each changed css_set will need its hash recomputed.
4783 * this is all done under the css_set_lock.
4784 */
4785 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004786 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004787 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004788 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004789 continue;
4790 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004791 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004792 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004793 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004794 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004795 key = css_set_hash(cset->subsys);
4796 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004797 }
4798 write_unlock(&css_set_lock);
4799
Tejun Heo9871bf92013-06-24 15:21:47 -07004800 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004801 if (ret)
4802 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004803
Ben Blume6a11052010-03-10 15:22:09 -08004804 /* success! */
4805 mutex_unlock(&cgroup_mutex);
4806 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004807
4808err_unload:
4809 mutex_unlock(&cgroup_mutex);
4810 /* @ss can't be mounted here as try_module_get() would fail */
4811 cgroup_unload_subsys(ss);
4812 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004813}
4814EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4815
4816/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004817 * cgroup_unload_subsys: unload a modular subsystem
4818 * @ss: the subsystem to unload
4819 *
4820 * This function should be called in a modular subsystem's exitcall. When this
4821 * function is invoked, the refcount on the subsystem's module will be 0, so
4822 * the subsystem will not be attached to any hierarchy.
4823 */
4824void cgroup_unload_subsys(struct cgroup_subsys *ss)
4825{
Tejun Heo69d02062013-06-12 21:04:50 -07004826 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004827
4828 BUG_ON(ss->module == NULL);
4829
4830 /*
4831 * we shouldn't be called if the subsystem is in use, and the use of
4832 * try_module_get in parse_cgroupfs_options should ensure that it
4833 * doesn't start being used while we're killing it off.
4834 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004835 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004836
4837 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004838
Tejun Heo9871bf92013-06-24 15:21:47 -07004839 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004840
Tejun Heoc897ff62013-02-27 17:03:49 -08004841 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004842 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004843
Ben Blumcf5d5942010-03-10 15:22:09 -08004844 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004845 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004846
Tejun Heo9871bf92013-06-24 15:21:47 -07004847 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004848 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004849
4850 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004851 * disentangle the css from all css_sets attached to the dummy
4852 * top. as in loading, we need to pay our respects to the hashtable
4853 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004854 */
4855 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004856 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004857 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004858 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004859
Tejun Heo5abb8852013-06-12 21:04:49 -07004860 hash_del(&cset->hlist);
4861 cset->subsys[ss->subsys_id] = NULL;
4862 key = css_set_hash(cset->subsys);
4863 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004864 }
4865 write_unlock(&css_set_lock);
4866
4867 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004868 * remove subsystem's css from the cgroup_dummy_top and free it -
4869 * need to free before marking as null because ss->css_free needs
4870 * the cgrp->subsys pointer to find their state. note that this
4871 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004872 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004873 ss->css_free(cgroup_dummy_top);
4874 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004875
4876 mutex_unlock(&cgroup_mutex);
4877}
4878EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4879
4880/**
Li Zefana043e3b2008-02-23 15:24:09 -08004881 * cgroup_init_early - cgroup initialization at system boot
4882 *
4883 * Initialize cgroups at system boot, and initialize any
4884 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004885 */
4886int __init cgroup_init_early(void)
4887{
Tejun Heo30159ec2013-06-25 11:53:37 -07004888 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004889 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004890
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004891 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004892 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004893 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004894 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004895 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004896 init_cgroup_root(&cgroup_dummy_root);
4897 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004898 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004899
Tejun Heo69d02062013-06-12 21:04:50 -07004900 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004901 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4902 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004903 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004904
Tejun Heo30159ec2013-06-25 11:53:37 -07004905 /* at bootup time, we don't worry about modular subsystems */
4906 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004907 BUG_ON(!ss->name);
4908 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004909 BUG_ON(!ss->css_alloc);
4910 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004911 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004912 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004913 ss->name, ss->subsys_id);
4914 BUG();
4915 }
4916
4917 if (ss->early_init)
4918 cgroup_init_subsys(ss);
4919 }
4920 return 0;
4921}
4922
4923/**
Li Zefana043e3b2008-02-23 15:24:09 -08004924 * cgroup_init - cgroup initialization
4925 *
4926 * Register cgroup filesystem and /proc file, and initialize
4927 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004928 */
4929int __init cgroup_init(void)
4930{
Tejun Heo30159ec2013-06-25 11:53:37 -07004931 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004932 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004933 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004934
4935 err = bdi_init(&cgroup_backing_dev_info);
4936 if (err)
4937 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004938
Tejun Heo30159ec2013-06-25 11:53:37 -07004939 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004940 if (!ss->early_init)
4941 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004942 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004943 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004944 }
4945
Tejun Heofa3ca072013-04-14 11:36:56 -07004946 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004947 mutex_lock(&cgroup_mutex);
4948 mutex_lock(&cgroup_root_mutex);
4949
Tejun Heo82fe9b02013-06-25 11:53:37 -07004950 /* Add init_css_set to the hash table */
4951 key = css_set_hash(init_css_set.subsys);
4952 hash_add(css_set_table, &init_css_set.hlist, key);
4953
Tejun Heofc76df72013-06-25 11:53:37 -07004954 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004955
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004956 mutex_unlock(&cgroup_root_mutex);
4957 mutex_unlock(&cgroup_mutex);
4958
Greg KH676db4a2010-08-05 13:53:35 -07004959 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4960 if (!cgroup_kobj) {
4961 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004962 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004963 }
4964
4965 err = register_filesystem(&cgroup_fs_type);
4966 if (err < 0) {
4967 kobject_put(cgroup_kobj);
4968 goto out;
4969 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004970
Li Zefan46ae2202008-04-29 01:00:08 -07004971 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004972
Paul Menageddbcc7e2007-10-18 23:39:30 -07004973out:
Paul Menagea4243162007-10-18 23:39:35 -07004974 if (err)
4975 bdi_destroy(&cgroup_backing_dev_info);
4976
Paul Menageddbcc7e2007-10-18 23:39:30 -07004977 return err;
4978}
Paul Menageb4f48b62007-10-18 23:39:33 -07004979
Paul Menagea4243162007-10-18 23:39:35 -07004980/*
4981 * proc_cgroup_show()
4982 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4983 * - Used for /proc/<pid>/cgroup.
4984 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4985 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004986 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004987 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4988 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4989 * cgroup to top_cgroup.
4990 */
4991
4992/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004993int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004994{
4995 struct pid *pid;
4996 struct task_struct *tsk;
4997 char *buf;
4998 int retval;
4999 struct cgroupfs_root *root;
5000
5001 retval = -ENOMEM;
5002 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5003 if (!buf)
5004 goto out;
5005
5006 retval = -ESRCH;
5007 pid = m->private;
5008 tsk = get_pid_task(pid, PIDTYPE_PID);
5009 if (!tsk)
5010 goto out_free;
5011
5012 retval = 0;
5013
5014 mutex_lock(&cgroup_mutex);
5015
Li Zefane5f6a862009-01-07 18:07:41 -08005016 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005017 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005018 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005019 int count = 0;
5020
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005021 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005022 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005023 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005024 if (strlen(root->name))
5025 seq_printf(m, "%sname=%s", count ? "," : "",
5026 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005027 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005028 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005029 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005030 if (retval < 0)
5031 goto out_unlock;
5032 seq_puts(m, buf);
5033 seq_putc(m, '\n');
5034 }
5035
5036out_unlock:
5037 mutex_unlock(&cgroup_mutex);
5038 put_task_struct(tsk);
5039out_free:
5040 kfree(buf);
5041out:
5042 return retval;
5043}
5044
Paul Menagea4243162007-10-18 23:39:35 -07005045/* Display information about each subsystem and each hierarchy */
5046static int proc_cgroupstats_show(struct seq_file *m, void *v)
5047{
Tejun Heo30159ec2013-06-25 11:53:37 -07005048 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005049 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005050
Paul Menage8bab8dd2008-04-04 14:29:57 -07005051 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005052 /*
5053 * ideally we don't want subsystems moving around while we do this.
5054 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5055 * subsys/hierarchy state.
5056 */
Paul Menagea4243162007-10-18 23:39:35 -07005057 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005058
5059 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005060 seq_printf(m, "%s\t%d\t%d\t%d\n",
5061 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005062 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005063
Paul Menagea4243162007-10-18 23:39:35 -07005064 mutex_unlock(&cgroup_mutex);
5065 return 0;
5066}
5067
5068static int cgroupstats_open(struct inode *inode, struct file *file)
5069{
Al Viro9dce07f2008-03-29 03:07:28 +00005070 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005071}
5072
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005073static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005074 .open = cgroupstats_open,
5075 .read = seq_read,
5076 .llseek = seq_lseek,
5077 .release = single_release,
5078};
5079
Paul Menageb4f48b62007-10-18 23:39:33 -07005080/**
5081 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005082 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005083 *
5084 * Description: A task inherits its parent's cgroup at fork().
5085 *
5086 * A pointer to the shared css_set was automatically copied in
5087 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005088 * it was not made under the protection of RCU or cgroup_mutex, so
5089 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5090 * have already changed current->cgroups, allowing the previously
5091 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005092 *
5093 * At the point that cgroup_fork() is called, 'current' is the parent
5094 * task, and the passed argument 'child' points to the child task.
5095 */
5096void cgroup_fork(struct task_struct *child)
5097{
Tejun Heo9bb71302012-10-18 17:52:07 -07005098 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005099 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005100 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005101 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005102 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005103}
5104
5105/**
Li Zefana043e3b2008-02-23 15:24:09 -08005106 * cgroup_post_fork - called on a new task after adding it to the task list
5107 * @child: the task in question
5108 *
Tejun Heo5edee612012-10-16 15:03:14 -07005109 * Adds the task to the list running through its css_set if necessary and
5110 * call the subsystem fork() callbacks. Has to be after the task is
5111 * visible on the task list in case we race with the first call to
5112 * cgroup_iter_start() - to guarantee that the new task ends up on its
5113 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005114 */
Paul Menage817929e2007-10-18 23:39:36 -07005115void cgroup_post_fork(struct task_struct *child)
5116{
Tejun Heo30159ec2013-06-25 11:53:37 -07005117 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005118 int i;
5119
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005120 /*
5121 * use_task_css_set_links is set to 1 before we walk the tasklist
5122 * under the tasklist_lock and we read it here after we added the child
5123 * to the tasklist under the tasklist_lock as well. If the child wasn't
5124 * yet in the tasklist when we walked through it from
5125 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5126 * should be visible now due to the paired locking and barriers implied
5127 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5128 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5129 * lock on fork.
5130 */
Paul Menage817929e2007-10-18 23:39:36 -07005131 if (use_task_css_set_links) {
5132 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005133 task_lock(child);
5134 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005135 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005136 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005137 write_unlock(&css_set_lock);
5138 }
Tejun Heo5edee612012-10-16 15:03:14 -07005139
5140 /*
5141 * Call ss->fork(). This must happen after @child is linked on
5142 * css_set; otherwise, @child might change state between ->fork()
5143 * and addition to css_set.
5144 */
5145 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005146 /*
5147 * fork/exit callbacks are supported only for builtin
5148 * subsystems, and the builtin section of the subsys
5149 * array is immutable, so we don't need to lock the
5150 * subsys array here. On the other hand, modular section
5151 * of the array can be freed at module unload, so we
5152 * can't touch that.
5153 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005154 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005155 if (ss->fork)
5156 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005157 }
Paul Menage817929e2007-10-18 23:39:36 -07005158}
Tejun Heo5edee612012-10-16 15:03:14 -07005159
Paul Menage817929e2007-10-18 23:39:36 -07005160/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005161 * cgroup_exit - detach cgroup from exiting task
5162 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005163 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005164 *
5165 * Description: Detach cgroup from @tsk and release it.
5166 *
5167 * Note that cgroups marked notify_on_release force every task in
5168 * them to take the global cgroup_mutex mutex when exiting.
5169 * This could impact scaling on very large systems. Be reluctant to
5170 * use notify_on_release cgroups where very high task exit scaling
5171 * is required on large systems.
5172 *
5173 * the_top_cgroup_hack:
5174 *
5175 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5176 *
5177 * We call cgroup_exit() while the task is still competent to
5178 * handle notify_on_release(), then leave the task attached to the
5179 * root cgroup in each hierarchy for the remainder of its exit.
5180 *
5181 * To do this properly, we would increment the reference count on
5182 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5183 * code we would add a second cgroup function call, to drop that
5184 * reference. This would just create an unnecessary hot spot on
5185 * the top_cgroup reference count, to no avail.
5186 *
5187 * Normally, holding a reference to a cgroup without bumping its
5188 * count is unsafe. The cgroup could go away, or someone could
5189 * attach us to a different cgroup, decrementing the count on
5190 * the first cgroup that we never incremented. But in this case,
5191 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005192 * which wards off any cgroup_attach_task() attempts, or task is a failed
5193 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005194 */
5195void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5196{
Tejun Heo30159ec2013-06-25 11:53:37 -07005197 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005198 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005199 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005200
5201 /*
5202 * Unlink from the css_set task list if necessary.
5203 * Optimistically check cg_list before taking
5204 * css_set_lock
5205 */
5206 if (!list_empty(&tsk->cg_list)) {
5207 write_lock(&css_set_lock);
5208 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005209 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005210 write_unlock(&css_set_lock);
5211 }
5212
Paul Menageb4f48b62007-10-18 23:39:33 -07005213 /* Reassign the task to the init_css_set. */
5214 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005215 cset = task_css_set(tsk);
5216 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005217
5218 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005219 /*
5220 * fork/exit callbacks are supported only for builtin
5221 * subsystems, see cgroup_post_fork() for details.
5222 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005223 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005224 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005225 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005226 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005227
Li Zefan761b3ef2012-01-31 13:47:36 +08005228 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005229 }
5230 }
5231 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005232 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005233
Tejun Heo5abb8852013-06-12 21:04:49 -07005234 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005235}
Paul Menage697f4162007-10-18 23:39:34 -07005236
Paul Menagebd89aab2007-10-18 23:40:44 -07005237static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005238{
Li Zefanf50daa72013-03-01 15:06:07 +08005239 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d8282013-06-12 21:04:55 -07005240 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005241 /*
5242 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005243 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005244 * it now
5245 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005246 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005247
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005248 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005249 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005250 list_empty(&cgrp->release_list)) {
5251 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005252 need_schedule_work = 1;
5253 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005254 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005255 if (need_schedule_work)
5256 schedule_work(&release_agent_work);
5257 }
5258}
5259
Paul Menage81a6a5c2007-10-18 23:39:38 -07005260/*
5261 * Notify userspace when a cgroup is released, by running the
5262 * configured release agent with the name of the cgroup (path
5263 * relative to the root of cgroup file system) as the argument.
5264 *
5265 * Most likely, this user command will try to rmdir this cgroup.
5266 *
5267 * This races with the possibility that some other task will be
5268 * attached to this cgroup before it is removed, or that some other
5269 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5270 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5271 * unused, and this cgroup will be reprieved from its death sentence,
5272 * to continue to serve a useful existence. Next time it's released,
5273 * we will get notified again, if it still has 'notify_on_release' set.
5274 *
5275 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5276 * means only wait until the task is successfully execve()'d. The
5277 * separate release agent task is forked by call_usermodehelper(),
5278 * then control in this thread returns here, without waiting for the
5279 * release agent task. We don't bother to wait because the caller of
5280 * this routine has no use for the exit status of the release agent
5281 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005282 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005283static void cgroup_release_agent(struct work_struct *work)
5284{
5285 BUG_ON(work != &release_agent_work);
5286 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005287 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005288 while (!list_empty(&release_list)) {
5289 char *argv[3], *envp[3];
5290 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005291 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005292 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005293 struct cgroup,
5294 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005295 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005296 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005297 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005298 if (!pathbuf)
5299 goto continue_free;
5300 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5301 goto continue_free;
5302 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5303 if (!agentbuf)
5304 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005305
5306 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005307 argv[i++] = agentbuf;
5308 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005309 argv[i] = NULL;
5310
5311 i = 0;
5312 /* minimal command environment */
5313 envp[i++] = "HOME=/";
5314 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5315 envp[i] = NULL;
5316
5317 /* Drop the lock while we invoke the usermode helper,
5318 * since the exec could involve hitting disk and hence
5319 * be a slow process */
5320 mutex_unlock(&cgroup_mutex);
5321 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005322 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005323 continue_free:
5324 kfree(pathbuf);
5325 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005326 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005327 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005328 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005329 mutex_unlock(&cgroup_mutex);
5330}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005331
5332static int __init cgroup_disable(char *str)
5333{
Tejun Heo30159ec2013-06-25 11:53:37 -07005334 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005335 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005336 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005337
5338 while ((token = strsep(&str, ",")) != NULL) {
5339 if (!*token)
5340 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005341
Tejun Heo30159ec2013-06-25 11:53:37 -07005342 /*
5343 * cgroup_disable, being at boot time, can't know about
5344 * module subsystems, so we don't worry about them.
5345 */
5346 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005347 if (!strcmp(token, ss->name)) {
5348 ss->disabled = 1;
5349 printk(KERN_INFO "Disabling %s control group"
5350 " subsystem\n", ss->name);
5351 break;
5352 }
5353 }
5354 }
5355 return 1;
5356}
5357__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005358
5359/*
5360 * Functons for CSS ID.
5361 */
5362
Tejun Heo54766d42013-06-12 21:04:53 -07005363/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005364unsigned short css_id(struct cgroup_subsys_state *css)
5365{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005366 struct css_id *cssid;
5367
5368 /*
5369 * This css_id() can return correct value when somone has refcnt
5370 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5371 * it's unchanged until freed.
5372 */
Tejun Heod3daf282013-06-13 19:39:16 -07005373 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005374
5375 if (cssid)
5376 return cssid->id;
5377 return 0;
5378}
Ben Blum67523c42010-03-10 15:22:11 -08005379EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005380
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005381/**
5382 * css_is_ancestor - test "root" css is an ancestor of "child"
5383 * @child: the css to be tested.
5384 * @root: the css supporsed to be an ancestor of the child.
5385 *
5386 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005387 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005388 * But, considering usual usage, the csses should be valid objects after test.
5389 * Assuming that the caller will do some action to the child if this returns
5390 * returns true, the caller must take "child";s reference count.
5391 * If "child" is valid object and this returns true, "root" is valid, too.
5392 */
5393
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005395 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005396{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005397 struct css_id *child_id;
5398 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005400 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005401 if (!child_id)
5402 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005403 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005404 if (!root_id)
5405 return false;
5406 if (child_id->depth < root_id->depth)
5407 return false;
5408 if (child_id->stack[root_id->depth] != root_id->id)
5409 return false;
5410 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005411}
5412
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5414{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005415 struct css_id *id = rcu_dereference_protected(css->id, true);
5416
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417 /* When this is called before css_id initialization, id can be NULL */
5418 if (!id)
5419 return;
5420
5421 BUG_ON(!ss->use_id);
5422
5423 rcu_assign_pointer(id->css, NULL);
5424 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005425 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005427 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005428 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429}
Ben Blum67523c42010-03-10 15:22:11 -08005430EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005431
5432/*
5433 * This is called by init or create(). Then, calls to this function are
5434 * always serialized (By cgroup_mutex() at create()).
5435 */
5436
5437static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5438{
5439 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005440 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005441
5442 BUG_ON(!ss->use_id);
5443
5444 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5445 newid = kzalloc(size, GFP_KERNEL);
5446 if (!newid)
5447 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005448
5449 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005450 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005452 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005453 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005454 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005455
5456 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005457 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005458 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005459
Tejun Heod228d9e2013-02-27 17:04:54 -08005460 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005461 newid->depth = depth;
5462 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005463err_out:
5464 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005465 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005466
5467}
5468
Ben Blume6a11052010-03-10 15:22:09 -08005469static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5470 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005471{
5472 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005473
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005474 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005475 idr_init(&ss->idr);
5476
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005477 newid = get_new_cssid(ss, 0);
5478 if (IS_ERR(newid))
5479 return PTR_ERR(newid);
5480
5481 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005482 RCU_INIT_POINTER(newid->css, rootcss);
5483 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005484 return 0;
5485}
5486
5487static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5488 struct cgroup *child)
5489{
5490 int subsys_id, i, depth = 0;
5491 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005492 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005493
5494 subsys_id = ss->subsys_id;
5495 parent_css = parent->subsys[subsys_id];
5496 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005497 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005498 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005499
5500 child_id = get_new_cssid(ss, depth);
5501 if (IS_ERR(child_id))
5502 return PTR_ERR(child_id);
5503
5504 for (i = 0; i < depth; i++)
5505 child_id->stack[i] = parent_id->stack[i];
5506 child_id->stack[depth] = child_id->id;
5507 /*
5508 * child_id->css pointer will be set after this cgroup is available
5509 * see cgroup_populate_dir()
5510 */
5511 rcu_assign_pointer(child_css->id, child_id);
5512
5513 return 0;
5514}
5515
5516/**
5517 * css_lookup - lookup css by id
5518 * @ss: cgroup subsys to be looked into.
5519 * @id: the id
5520 *
5521 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5522 * NULL if not. Should be called under rcu_read_lock()
5523 */
5524struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5525{
5526 struct css_id *cssid = NULL;
5527
5528 BUG_ON(!ss->use_id);
5529 cssid = idr_find(&ss->idr, id);
5530
5531 if (unlikely(!cssid))
5532 return NULL;
5533
5534 return rcu_dereference(cssid->css);
5535}
Ben Blum67523c42010-03-10 15:22:11 -08005536EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005537
Stephane Eraniane5d13672011-02-14 11:20:01 +02005538/*
5539 * get corresponding css from file open on cgroupfs directory
5540 */
5541struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5542{
5543 struct cgroup *cgrp;
5544 struct inode *inode;
5545 struct cgroup_subsys_state *css;
5546
Al Viro496ad9a2013-01-23 17:07:38 -05005547 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005548 /* check in cgroup filesystem dir */
5549 if (inode->i_op != &cgroup_dir_inode_operations)
5550 return ERR_PTR(-EBADF);
5551
5552 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5553 return ERR_PTR(-EINVAL);
5554
5555 /* get cgroup */
5556 cgrp = __d_cgrp(f->f_dentry);
5557 css = cgrp->subsys[id];
5558 return css ? css : ERR_PTR(-ENOENT);
5559}
5560
Paul Menagefe693432009-09-23 15:56:20 -07005561#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005562static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005563{
5564 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5565
5566 if (!css)
5567 return ERR_PTR(-ENOMEM);
5568
5569 return css;
5570}
5571
Li Zefan03c78cb2013-06-14 11:17:19 +08005572static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005573{
Li Zefan03c78cb2013-06-14 11:17:19 +08005574 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005575}
5576
Li Zefan03c78cb2013-06-14 11:17:19 +08005577static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005578{
Li Zefan03c78cb2013-06-14 11:17:19 +08005579 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005580}
5581
Li Zefan03c78cb2013-06-14 11:17:19 +08005582static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005583{
5584 return (u64)(unsigned long)current->cgroups;
5585}
5586
Li Zefan03c78cb2013-06-14 11:17:19 +08005587static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5588 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005589{
5590 u64 count;
5591
5592 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005593 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005594 rcu_read_unlock();
5595 return count;
5596}
5597
Li Zefan03c78cb2013-06-14 11:17:19 +08005598static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005599 struct cftype *cft,
5600 struct seq_file *seq)
5601{
Tejun Heo69d02062013-06-12 21:04:50 -07005602 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005603 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005604
5605 read_lock(&css_set_lock);
5606 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005607 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005608 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005609 struct cgroup *c = link->cgrp;
5610 const char *name;
5611
5612 if (c->dentry)
5613 name = c->dentry->d_name.name;
5614 else
5615 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005616 seq_printf(seq, "Root %d group %s\n",
5617 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005618 }
5619 rcu_read_unlock();
5620 read_unlock(&css_set_lock);
5621 return 0;
5622}
5623
5624#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005625static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005626 struct cftype *cft,
5627 struct seq_file *seq)
5628{
Tejun Heo69d02062013-06-12 21:04:50 -07005629 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005630
5631 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005632 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005633 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005634 struct task_struct *task;
5635 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005636 seq_printf(seq, "css_set %p\n", cset);
5637 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005638 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5639 seq_puts(seq, " ...\n");
5640 break;
5641 } else {
5642 seq_printf(seq, " task %d\n",
5643 task_pid_vnr(task));
5644 }
5645 }
5646 }
5647 read_unlock(&css_set_lock);
5648 return 0;
5649}
5650
Paul Menagefe693432009-09-23 15:56:20 -07005651static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5652{
5653 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5654}
5655
5656static struct cftype debug_files[] = {
5657 {
Paul Menagefe693432009-09-23 15:56:20 -07005658 .name = "taskcount",
5659 .read_u64 = debug_taskcount_read,
5660 },
5661
5662 {
5663 .name = "current_css_set",
5664 .read_u64 = current_css_set_read,
5665 },
5666
5667 {
5668 .name = "current_css_set_refcount",
5669 .read_u64 = current_css_set_refcount_read,
5670 },
5671
5672 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005673 .name = "current_css_set_cg_links",
5674 .read_seq_string = current_css_set_cg_links_read,
5675 },
5676
5677 {
5678 .name = "cgroup_css_links",
5679 .read_seq_string = cgroup_css_links_read,
5680 },
5681
5682 {
Paul Menagefe693432009-09-23 15:56:20 -07005683 .name = "releasable",
5684 .read_u64 = releasable_read,
5685 },
Paul Menagefe693432009-09-23 15:56:20 -07005686
Tejun Heo4baf6e32012-04-01 12:09:55 -07005687 { } /* terminate */
5688};
Paul Menagefe693432009-09-23 15:56:20 -07005689
5690struct cgroup_subsys debug_subsys = {
5691 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005692 .css_alloc = debug_css_alloc,
5693 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005694 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005695 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005696};
5697#endif /* CONFIG_CGROUP_DEBUG */