blob: c108d3d1ea306af2b19b638dd1e60045213b6f9c [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218static struct cftype cgroup_base_files[];
219
Tejun Heoea15f8c2013-06-13 19:27:42 -0700220static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800222static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
223 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Ben Blume6a11052010-03-10 15:22:09 -0800367static int cgroup_init_idr(struct cgroup_subsys *ss,
368 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700369
Paul Menage817929e2007-10-18 23:39:36 -0700370/* css_set_lock protects the list of css_set objects, and the
371 * chain of tasks off each css_set. Nests outside task->alloc_lock
372 * due to cgroup_iter_start() */
373static DEFINE_RWLOCK(css_set_lock);
374static int css_set_count;
375
Paul Menage7717f7b2009-09-23 15:56:22 -0700376/*
377 * hash table for cgroup groups. This improves the performance to find
378 * an existing css_set. This hash doesn't (currently) take into
379 * account cgroups in empty hierarchies.
380 */
Li Zefan472b1052008-04-29 01:00:11 -0700381#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700385{
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700387 struct cgroup_subsys *ss;
388 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Tejun Heo30159ec2013-06-25 11:53:37 -0700390 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 key += (unsigned long)css[i];
392 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700393
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Tejun Heo5abb8852013-06-12 21:04:49 -0700403static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
Tejun Heo69d02062013-06-12 21:04:50 -0700405 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700413 return;
414 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422 css_set_count--;
423
Tejun Heo69d02062013-06-12 21:04:50 -0700424 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700425 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700426
Tejun Heo69d02062013-06-12 21:04:50 -0700427 list_del(&link->cset_link);
428 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800429
Tejun Heoddd69142013-06-12 21:04:54 -0700430 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700431 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700433 set_bit(CGRP_RELEASABLE, &cgrp->flags);
434 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
444/*
445 * refcounted get/put for css_set objects
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700448{
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700453{
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458{
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460}
461
Tejun Heob326f9d2013-06-24 15:21:48 -0700462/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 * @cset: candidate css_set being tested
465 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * @new_cgrp: cgroup that's being entered by the task
467 * @template: desired set of css pointers in css_set (pre-calculated)
468 *
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;
Tejun Heo31261212013-06-28 17:07:30 -07001006 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001007
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
Tejun Heo31261212013-06-28 17:07:30 -07001024 ret = cgroup_populate_dir(cgrp, added_mask);
1025 if (ret)
1026 return ret;
1027
1028 /*
1029 * Nothing can fail from this point on. Remove files for the
1030 * removed subsystems and rebind each subsystem.
1031 */
1032 cgroup_clear_dir(cgrp, removed_mask);
1033
Tejun Heo30159ec2013-06-25 11:53:37 -07001034 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001035 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001036
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001037 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001038 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001039 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001040 BUG_ON(!cgroup_dummy_top->subsys[i]);
1041 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001042
Tejun Heo9871bf92013-06-24 15:21:47 -07001043 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001045 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001046 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001047 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001048 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001049
Ben Blumcf5d5942010-03-10 15:22:09 -08001050 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001051 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001052 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001055 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056
Paul Menageddbcc7e2007-10-18 23:39:30 -07001057 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001058 ss->bind(cgroup_dummy_top);
1059 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001061 cgroup_subsys[i]->root = &cgroup_dummy_root;
1062 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001063
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /* subsystem is now free - drop reference on module */
1065 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001066 root->subsys_mask &= ~bit;
1067 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001069 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001070 /*
1071 * a refcount was taken, but we already had one, so
1072 * drop the extra reference.
1073 */
1074 module_put(ss->module);
1075#ifdef CONFIG_MODULE_UNLOAD
1076 BUG_ON(ss->module && !module_refcount(ss->module));
1077#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 } else {
1079 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001080 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 }
1082 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083
Tejun Heo1672d042013-06-25 18:04:54 -07001084 /*
1085 * Mark @root has finished binding subsystems. @root->subsys_mask
1086 * now matches the bound subsystems.
1087 */
1088 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1089
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 return 0;
1091}
1092
Al Viro34c80b12011-12-08 21:32:45 -05001093static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094{
Al Viro34c80b12011-12-08 21:32:45 -05001095 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096 struct cgroup_subsys *ss;
1097
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001098 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001099 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001101 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1102 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001103 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001105 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001106 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001107 if (strlen(root->release_agent_path))
1108 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001109 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001110 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001111 if (strlen(root->name))
1112 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001113 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 return 0;
1115}
1116
1117struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001118 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001120 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001121 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001122 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001123 /* User explicitly requested empty subsystem */
1124 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001125
1126 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001127
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128};
1129
Ben Blumaae8aab2010-03-10 15:22:07 -08001130/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001131 * Convert a hierarchy specifier into a bitmask of subsystems and
1132 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1133 * array. This function takes refcounts on subsystems to be used, unless it
1134 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001135 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001136static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001138 char *token, *o = data;
1139 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001140 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001141 bool module_pin_failed = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07001142 struct cgroup_subsys *ss;
1143 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001144
Ben Blumaae8aab2010-03-10 15:22:07 -08001145 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1146
Li Zefanf9ab5b52009-06-17 16:26:33 -07001147#ifdef CONFIG_CPUSETS
1148 mask = ~(1UL << cpuset_subsys_id);
1149#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001150
Paul Menagec6d57f32009-09-23 15:56:19 -07001151 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152
1153 while ((token = strsep(&o, ",")) != NULL) {
1154 if (!*token)
1155 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001156 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001157 /* Explicitly have no subsystems */
1158 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 continue;
1160 }
1161 if (!strcmp(token, "all")) {
1162 /* Mutually exclusive option 'all' + subsystem name */
1163 if (one_ss)
1164 return -EINVAL;
1165 all_ss = true;
1166 continue;
1167 }
Tejun Heo873fe092013-04-14 20:15:26 -07001168 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1169 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1170 continue;
1171 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001172 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001173 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174 continue;
1175 }
1176 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001177 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 continue;
1179 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001180 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001181 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001182 continue;
1183 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001184 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001185 /* Specifying two release agents is forbidden */
1186 if (opts->release_agent)
1187 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001188 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001189 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001190 if (!opts->release_agent)
1191 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001192 continue;
1193 }
1194 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001195 const char *name = token + 5;
1196 /* Can't specify an empty name */
1197 if (!strlen(name))
1198 return -EINVAL;
1199 /* Must match [\w.-]+ */
1200 for (i = 0; i < strlen(name); i++) {
1201 char c = name[i];
1202 if (isalnum(c))
1203 continue;
1204 if ((c == '.') || (c == '-') || (c == '_'))
1205 continue;
1206 return -EINVAL;
1207 }
1208 /* Specifying two names is forbidden */
1209 if (opts->name)
1210 return -EINVAL;
1211 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001212 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001213 GFP_KERNEL);
1214 if (!opts->name)
1215 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001216
1217 continue;
1218 }
1219
Tejun Heo30159ec2013-06-25 11:53:37 -07001220 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221 if (strcmp(token, ss->name))
1222 continue;
1223 if (ss->disabled)
1224 continue;
1225
1226 /* Mutually exclusive option 'all' + subsystem name */
1227 if (all_ss)
1228 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001229 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 one_ss = true;
1231
1232 break;
1233 }
1234 if (i == CGROUP_SUBSYS_COUNT)
1235 return -ENOENT;
1236 }
1237
1238 /*
1239 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001240 * otherwise if 'none', 'name=' and a subsystem name options
1241 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001242 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001243 if (all_ss || (!one_ss && !opts->none && !opts->name))
1244 for_each_subsys(ss, i)
1245 if (!ss->disabled)
1246 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001247
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001248 /* Consistency checks */
1249
Tejun Heo873fe092013-04-14 20:15:26 -07001250 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1251 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1252
1253 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1254 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1255 return -EINVAL;
1256 }
1257
1258 if (opts->cpuset_clone_children) {
1259 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1260 return -EINVAL;
1261 }
1262 }
1263
Li Zefanf9ab5b52009-06-17 16:26:33 -07001264 /*
1265 * Option noprefix was introduced just for backward compatibility
1266 * with the old cpuset, so we allow noprefix only if mounting just
1267 * the cpuset subsystem.
1268 */
Tejun Heo93438622013-04-14 20:15:25 -07001269 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001270 return -EINVAL;
1271
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001272
1273 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001274 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001275 return -EINVAL;
1276
1277 /*
1278 * We either have to specify by name or by subsystems. (So all
1279 * empty hierarchies must have a name).
1280 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001281 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001282 return -EINVAL;
1283
Ben Blumcf5d5942010-03-10 15:22:09 -08001284 /*
1285 * Grab references on all the modules we'll need, so the subsystems
1286 * don't dance around before rebind_subsystems attaches them. This may
1287 * take duplicate reference counts on a subsystem that's already used,
1288 * but rebind_subsystems handles this case.
1289 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001290 for_each_subsys(ss, i) {
1291 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001292 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001293 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001294 module_pin_failed = true;
1295 break;
1296 }
1297 }
1298 if (module_pin_failed) {
1299 /*
1300 * oops, one of the modules was going away. this means that we
1301 * raced with a module_delete call, and to the user this is
1302 * essentially a "subsystem doesn't exist" case.
1303 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001304 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 /* drop refcounts only on the ones we took */
1306 unsigned long bit = 1UL << i;
1307
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001308 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001309 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001310 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 }
1312 return -ENOENT;
1313 }
1314
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 return 0;
1316}
1317
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001318static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001319{
Tejun Heo30159ec2013-06-25 11:53:37 -07001320 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001322
Tejun Heoeb178d02013-06-25 18:05:21 -07001323 mutex_lock(&cgroup_mutex);
1324 for_each_subsys(ss, i)
1325 if (subsys_mask & (1UL << i))
1326 module_put(cgroup_subsys[i]->module);
1327 mutex_unlock(&cgroup_mutex);
Ben Blumcf5d5942010-03-10 15:22:09 -08001328}
1329
Paul Menageddbcc7e2007-10-18 23:39:30 -07001330static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1331{
1332 int ret = 0;
1333 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001334 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001336 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001337
Tejun Heo873fe092013-04-14 20:15:26 -07001338 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1339 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1340 return -EINVAL;
1341 }
1342
Paul Menagebd89aab2007-10-18 23:40:44 -07001343 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001345 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346
1347 /* See what subsystems are wanted */
1348 ret = parse_cgroupfs_options(data, &opts);
1349 if (ret)
1350 goto out_unlock;
1351
Tejun Heoa8a648c2013-06-24 15:21:47 -07001352 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001353 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1354 task_tgid_nr(current), current->comm);
1355
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001356 added_mask = opts.subsys_mask & ~root->subsys_mask;
1357 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001358
Ben Blumcf5d5942010-03-10 15:22:09 -08001359 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001360 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001361 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001362 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1363 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1364 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001366 goto out_unlock;
1367 }
1368
Tejun Heof172e672013-06-28 17:07:30 -07001369 /* remounting is not allowed for populated hierarchies */
1370 if (root->number_of_cgroups > 1) {
1371 ret = -EBUSY;
1372 goto out_unlock;
1373 }
1374
Tejun Heoa8a648c2013-06-24 15:21:47 -07001375 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001376 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001377 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378
Paul Menage81a6a5c2007-10-18 23:39:38 -07001379 if (opts.release_agent)
1380 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001381 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001382 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001383 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001384 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001386 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Tejun Heoe2bd4162013-06-27 19:37:23 -07001387 if (ret)
1388 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 return ret;
1390}
1391
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001392static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 .statfs = simple_statfs,
1394 .drop_inode = generic_delete_inode,
1395 .show_options = cgroup_show_options,
1396 .remount_fs = cgroup_remount,
1397};
1398
Paul Menagecc31edc2008-10-18 20:28:04 -07001399static void init_cgroup_housekeeping(struct cgroup *cgrp)
1400{
1401 INIT_LIST_HEAD(&cgrp->sibling);
1402 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001403 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001404 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001405 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001406 INIT_LIST_HEAD(&cgrp->pidlists);
1407 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001408 INIT_LIST_HEAD(&cgrp->event_list);
1409 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001410 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001411}
Paul Menagec6d57f32009-09-23 15:56:19 -07001412
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413static void init_cgroup_root(struct cgroupfs_root *root)
1414{
Paul Menagebd89aab2007-10-18 23:40:44 -07001415 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001416
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417 INIT_LIST_HEAD(&root->subsys_list);
1418 INIT_LIST_HEAD(&root->root_list);
1419 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001421 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001422 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423}
1424
Tejun Heofc76df72013-06-25 11:53:37 -07001425static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001426{
Tejun Heo1a574232013-04-14 11:36:58 -07001427 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001429 lockdep_assert_held(&cgroup_mutex);
1430 lockdep_assert_held(&cgroup_root_mutex);
1431
Tejun Heofc76df72013-06-25 11:53:37 -07001432 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1433 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001434 if (id < 0)
1435 return id;
1436
1437 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001438 return 0;
1439}
1440
1441static void cgroup_exit_root_id(struct cgroupfs_root *root)
1442{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001443 lockdep_assert_held(&cgroup_mutex);
1444 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001445
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001446 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001447 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001448 root->hierarchy_id = 0;
1449 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001450}
1451
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452static int cgroup_test_super(struct super_block *sb, void *data)
1453{
Paul Menagec6d57f32009-09-23 15:56:19 -07001454 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455 struct cgroupfs_root *root = sb->s_fs_info;
1456
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 /* If we asked for a name then it must match */
1458 if (opts->name && strcmp(opts->name, root->name))
1459 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001460
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001461 /*
1462 * If we asked for subsystems (or explicitly for no
1463 * subsystems) then they must match
1464 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001465 if ((opts->subsys_mask || opts->none)
1466 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467 return 0;
1468
1469 return 1;
1470}
1471
Paul Menagec6d57f32009-09-23 15:56:19 -07001472static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1473{
1474 struct cgroupfs_root *root;
1475
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001476 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001477 return NULL;
1478
1479 root = kzalloc(sizeof(*root), GFP_KERNEL);
1480 if (!root)
1481 return ERR_PTR(-ENOMEM);
1482
1483 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001484
Tejun Heo1672d042013-06-25 18:04:54 -07001485 /*
1486 * We need to set @root->subsys_mask now so that @root can be
1487 * matched by cgroup_test_super() before it finishes
1488 * initialization; otherwise, competing mounts with the same
1489 * options may try to bind the same subsystems instead of waiting
1490 * for the first one leading to unexpected mount errors.
1491 * SUBSYS_BOUND will be set once actual binding is complete.
1492 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001493 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001494 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001495 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 if (opts->release_agent)
1497 strcpy(root->release_agent_path, opts->release_agent);
1498 if (opts->name)
1499 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001500 if (opts->cpuset_clone_children)
1501 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001502 return root;
1503}
1504
Tejun Heofa3ca072013-04-14 11:36:56 -07001505static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001506{
Tejun Heofa3ca072013-04-14 11:36:56 -07001507 if (root) {
1508 /* hierarhcy ID shoulid already have been released */
1509 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001510
Tejun Heofa3ca072013-04-14 11:36:56 -07001511 ida_destroy(&root->cgroup_ida);
1512 kfree(root);
1513 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001514}
1515
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516static int cgroup_set_super(struct super_block *sb, void *data)
1517{
1518 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001519 struct cgroup_sb_opts *opts = data;
1520
1521 /* If we don't have a new root, we can't set up a new sb */
1522 if (!opts->new_root)
1523 return -EINVAL;
1524
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001525 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 ret = set_anon_super(sb, NULL);
1528 if (ret)
1529 return ret;
1530
Paul Menagec6d57f32009-09-23 15:56:19 -07001531 sb->s_fs_info = opts->new_root;
1532 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
1534 sb->s_blocksize = PAGE_CACHE_SIZE;
1535 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1536 sb->s_magic = CGROUP_SUPER_MAGIC;
1537 sb->s_op = &cgroup_ops;
1538
1539 return 0;
1540}
1541
1542static int cgroup_get_rootdir(struct super_block *sb)
1543{
Al Viro0df6a632010-12-21 13:29:29 -05001544 static const struct dentry_operations cgroup_dops = {
1545 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001546 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001547 };
1548
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 struct inode *inode =
1550 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551
1552 if (!inode)
1553 return -ENOMEM;
1554
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 inode->i_fop = &simple_dir_operations;
1556 inode->i_op = &cgroup_dir_inode_operations;
1557 /* directories start off with i_nlink == 2 (for "." entry) */
1558 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001559 sb->s_root = d_make_root(inode);
1560 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001562 /* for everything else we want ->d_op set */
1563 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 return 0;
1565}
1566
Al Virof7e83572010-07-26 13:23:11 +04001567static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001569 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001570{
1571 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001573 int ret = 0;
1574 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001576 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001577 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001578 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
1580 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001581 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001583 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 if (ret)
1585 goto out_err;
1586
1587 /*
1588 * Allocate a new cgroup root. We may not need it if we're
1589 * reusing an existing hierarchy.
1590 */
1591 new_root = cgroup_root_from_opts(&opts);
1592 if (IS_ERR(new_root)) {
1593 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001594 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001595 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001596 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001599 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001602 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001603 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604 }
1605
Paul Menagec6d57f32009-09-23 15:56:19 -07001606 root = sb->s_fs_info;
1607 BUG_ON(!root);
1608 if (root == opts.new_root) {
1609 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001610 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001611 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001612 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001613 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614
1615 BUG_ON(sb->s_root != NULL);
1616
1617 ret = cgroup_get_rootdir(sb);
1618 if (ret)
1619 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001620 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001621
Paul Menage817929e2007-10-18 23:39:36 -07001622 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001623 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001625
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001626 /* Check for name clashes with existing mounts */
1627 ret = -EBUSY;
1628 if (strlen(root->name))
1629 for_each_active_root(existing_root)
1630 if (!strcmp(existing_root->name, root->name))
1631 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001632
Paul Menage817929e2007-10-18 23:39:36 -07001633 /*
1634 * We're accessing css_set_count without locking
1635 * css_set_lock here, but that's OK - it can only be
1636 * increased by someone holding cgroup_lock, and
1637 * that's us. The worst that can happen is that we
1638 * have some link structures left over
1639 */
Tejun Heo69d02062013-06-12 21:04:50 -07001640 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001641 if (ret)
1642 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001643
Tejun Heofc76df72013-06-25 11:53:37 -07001644 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1645 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001646 if (ret)
1647 goto unlock_drop;
1648
Tejun Heo31261212013-06-28 17:07:30 -07001649 sb->s_root->d_fsdata = root_cgrp;
1650 root_cgrp->dentry = sb->s_root;
1651
1652 /*
1653 * We're inside get_sb() and will call lookup_one_len() to
1654 * create the root files, which doesn't work if SELinux is
1655 * in use. The following cred dancing somehow works around
1656 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1657 * populating new cgroupfs mount") for more details.
1658 */
1659 cred = override_creds(&init_cred);
1660
1661 ret = cgroup_addrm_files(root_cgrp, NULL, cgroup_base_files, true);
1662 if (ret)
1663 goto rm_base_files;
1664
Tejun Heoa8a648c2013-06-24 15:21:47 -07001665 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001666 if (ret)
1667 goto rm_base_files;
1668
1669 revert_creds(cred);
1670
Ben Blumcf5d5942010-03-10 15:22:09 -08001671 /*
1672 * There must be no failure case after here, since rebinding
1673 * takes care of subsystems' refcounts, which are explicitly
1674 * dropped in the failure exit path.
1675 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676
Tejun Heo9871bf92013-06-24 15:21:47 -07001677 list_add(&root->root_list, &cgroup_roots);
1678 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001679
Paul Menage817929e2007-10-18 23:39:36 -07001680 /* Link the top cgroup in this hierarchy into all
1681 * the css_set objects */
1682 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001683 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001684 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001685 write_unlock(&css_set_lock);
1686
Tejun Heo69d02062013-06-12 21:04:50 -07001687 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001688
Li Zefanc12f65d2009-01-07 18:07:42 -08001689 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001690 BUG_ON(root->number_of_cgroups != 1);
1691
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001692 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001693 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001694 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001695 } else {
1696 /*
1697 * We re-used an existing hierarchy - the new root (if
1698 * any) is not needed
1699 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001700 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001701
Tejun Heoc7ba8282013-06-29 14:06:10 -07001702 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001703 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1704 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1705 ret = -EINVAL;
1706 goto drop_new_super;
1707 } else {
1708 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1709 }
Tejun Heo873fe092013-04-14 20:15:26 -07001710 }
1711
Ben Blumcf5d5942010-03-10 15:22:09 -08001712 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001713 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714 }
1715
Paul Menagec6d57f32009-09-23 15:56:19 -07001716 kfree(opts.release_agent);
1717 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001718 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001719
Tejun Heo31261212013-06-28 17:07:30 -07001720 rm_base_files:
1721 free_cgrp_cset_links(&tmp_links);
1722 cgroup_addrm_files(&root->top_cgroup, NULL, cgroup_base_files, false);
1723 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001724 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001725 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001726 mutex_unlock(&cgroup_root_mutex);
1727 mutex_unlock(&cgroup_mutex);
1728 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001730 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001731 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001732 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001733 out_err:
1734 kfree(opts.release_agent);
1735 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001736 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001737}
1738
1739static void cgroup_kill_sb(struct super_block *sb) {
1740 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001741 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001742 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 int ret;
1744
1745 BUG_ON(!root);
1746
1747 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001748 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
Tejun Heo31261212013-06-28 17:07:30 -07001750 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001751 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001752 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001753
1754 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001755 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1756 ret = rebind_subsystems(root, 0, root->subsys_mask);
1757 /* Shouldn't be able to fail ... */
1758 BUG_ON(ret);
1759 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760
Paul Menage817929e2007-10-18 23:39:36 -07001761 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001762 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001763 * root cgroup
1764 */
1765 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001766
Tejun Heo69d02062013-06-12 21:04:50 -07001767 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1768 list_del(&link->cset_link);
1769 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001770 kfree(link);
1771 }
1772 write_unlock(&css_set_lock);
1773
Paul Menage839ec542009-01-29 14:25:22 -08001774 if (!list_empty(&root->root_list)) {
1775 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001776 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001777 }
Li Zefane5f6a862009-01-07 18:07:41 -08001778
Tejun Heofa3ca072013-04-14 11:36:56 -07001779 cgroup_exit_root_id(root);
1780
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001781 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001783 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001784
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001785 simple_xattrs_free(&cgrp->xattrs);
1786
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001788 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789}
1790
1791static struct file_system_type cgroup_fs_type = {
1792 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001793 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 .kill_sb = cgroup_kill_sb,
1795};
1796
Greg KH676db4a2010-08-05 13:53:35 -07001797static struct kobject *cgroup_kobj;
1798
Li Zefana043e3b2008-02-23 15:24:09 -08001799/**
1800 * cgroup_path - generate the path of a cgroup
1801 * @cgrp: the cgroup in question
1802 * @buf: the buffer to write the path into
1803 * @buflen: the length of the buffer
1804 *
Li Zefan65dff752013-03-01 15:01:56 +08001805 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1806 *
1807 * We can't generate cgroup path using dentry->d_name, as accessing
1808 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1809 * inode's i_mutex, while on the other hand cgroup_path() can be called
1810 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001812int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813{
Li Zefan65dff752013-03-01 15:01:56 +08001814 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001816
Tejun Heoda1f2962013-04-14 10:32:19 -07001817 if (!cgrp->parent) {
1818 if (strlcpy(buf, "/", buflen) >= buflen)
1819 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 return 0;
1821 }
1822
Tao Ma316eb662012-11-08 21:36:38 +08001823 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001824 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001825
Li Zefan65dff752013-03-01 15:01:56 +08001826 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001827 do {
Li Zefan65dff752013-03-01 15:01:56 +08001828 const char *name = cgroup_name(cgrp);
1829 int len;
1830
1831 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001832 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001833 goto out;
1834 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001835
Paul Menageddbcc7e2007-10-18 23:39:30 -07001836 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001837 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001838 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001839
1840 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001841 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001842 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001843 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001844out:
1845 rcu_read_unlock();
1846 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001847}
Ben Blum67523c42010-03-10 15:22:11 -08001848EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001849
Tejun Heo857a2be2013-04-14 20:50:08 -07001850/**
1851 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1852 * @task: target task
1853 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1854 * @buf: the buffer to write the path into
1855 * @buflen: the length of the buffer
1856 *
1857 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1858 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1859 * be used inside locks used by cgroup controller callbacks.
1860 */
1861int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1862 char *buf, size_t buflen)
1863{
1864 struct cgroupfs_root *root;
1865 struct cgroup *cgrp = NULL;
1866 int ret = -ENOENT;
1867
1868 mutex_lock(&cgroup_mutex);
1869
1870 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1871 if (root) {
1872 cgrp = task_cgroup_from_root(task, root);
1873 ret = cgroup_path(cgrp, buf, buflen);
1874 }
1875
1876 mutex_unlock(&cgroup_mutex);
1877
1878 return ret;
1879}
1880EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1881
Ben Blum74a11662011-05-26 16:25:20 -07001882/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001883 * Control Group taskset
1884 */
Tejun Heo134d3372011-12-12 18:12:21 -08001885struct task_and_cgroup {
1886 struct task_struct *task;
1887 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001888 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001889};
1890
Tejun Heo2f7ee562011-12-12 18:12:21 -08001891struct cgroup_taskset {
1892 struct task_and_cgroup single;
1893 struct flex_array *tc_array;
1894 int tc_array_len;
1895 int idx;
1896 struct cgroup *cur_cgrp;
1897};
1898
1899/**
1900 * cgroup_taskset_first - reset taskset and return the first task
1901 * @tset: taskset of interest
1902 *
1903 * @tset iteration is initialized and the first task is returned.
1904 */
1905struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1906{
1907 if (tset->tc_array) {
1908 tset->idx = 0;
1909 return cgroup_taskset_next(tset);
1910 } else {
1911 tset->cur_cgrp = tset->single.cgrp;
1912 return tset->single.task;
1913 }
1914}
1915EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1916
1917/**
1918 * cgroup_taskset_next - iterate to the next task in taskset
1919 * @tset: taskset of interest
1920 *
1921 * Return the next task in @tset. Iteration must have been initialized
1922 * with cgroup_taskset_first().
1923 */
1924struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1925{
1926 struct task_and_cgroup *tc;
1927
1928 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1929 return NULL;
1930
1931 tc = flex_array_get(tset->tc_array, tset->idx++);
1932 tset->cur_cgrp = tc->cgrp;
1933 return tc->task;
1934}
1935EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1936
1937/**
1938 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1939 * @tset: taskset of interest
1940 *
1941 * Return the cgroup for the current (last returned) task of @tset. This
1942 * function must be preceded by either cgroup_taskset_first() or
1943 * cgroup_taskset_next().
1944 */
1945struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1946{
1947 return tset->cur_cgrp;
1948}
1949EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1950
1951/**
1952 * cgroup_taskset_size - return the number of tasks in taskset
1953 * @tset: taskset of interest
1954 */
1955int cgroup_taskset_size(struct cgroup_taskset *tset)
1956{
1957 return tset->tc_array ? tset->tc_array_len : 1;
1958}
1959EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1960
1961
Ben Blum74a11662011-05-26 16:25:20 -07001962/*
1963 * cgroup_task_migrate - move a task from one cgroup to another.
1964 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001965 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001966 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001967static void cgroup_task_migrate(struct cgroup *old_cgrp,
1968 struct task_struct *tsk,
1969 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001970{
Tejun Heo5abb8852013-06-12 21:04:49 -07001971 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001972
1973 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001974 * We are synchronized through threadgroup_lock() against PF_EXITING
1975 * setting such that we can't race against cgroup_exit() changing the
1976 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001977 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001978 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001979 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001980
Ben Blum74a11662011-05-26 16:25:20 -07001981 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001982 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001983 task_unlock(tsk);
1984
1985 /* Update the css_set linked lists if we're using them */
1986 write_lock(&css_set_lock);
1987 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001988 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001989 write_unlock(&css_set_lock);
1990
1991 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001992 * We just gained a reference on old_cset by taking it from the
1993 * task. As trading it for new_cset is protected by cgroup_mutex,
1994 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001995 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001996 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1997 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001998}
1999
Li Zefana043e3b2008-02-23 15:24:09 -08002000/**
Li Zefan081aa452013-03-13 09:17:09 +08002001 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07002002 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08002003 * @tsk: the task or the leader of the threadgroup to be attached
2004 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07002005 *
Tejun Heo257058a2011-12-12 18:12:21 -08002006 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08002007 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07002008 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07002009static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
2010 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002011{
2012 int retval, i, group_size;
2013 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002014 struct cgroupfs_root *root = cgrp->root;
2015 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002016 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002017 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002018 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002019 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002020
2021 /*
2022 * step 0: in order to do expensive, possibly blocking operations for
2023 * every thread, we cannot iterate the thread group list, since it needs
2024 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002025 * group - group_rwsem prevents new threads from appearing, and if
2026 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002027 */
Li Zefan081aa452013-03-13 09:17:09 +08002028 if (threadgroup)
2029 group_size = get_nr_threads(tsk);
2030 else
2031 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002032 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002033 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002034 if (!group)
2035 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002036 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002037 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002038 if (retval)
2039 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002040
Ben Blum74a11662011-05-26 16:25:20 -07002041 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002042 /*
2043 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2044 * already PF_EXITING could be freed from underneath us unless we
2045 * take an rcu_read_lock.
2046 */
2047 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002048 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002049 struct task_and_cgroup ent;
2050
Tejun Heocd3d0952011-12-12 18:12:21 -08002051 /* @tsk either already exited or can't exit until the end */
2052 if (tsk->flags & PF_EXITING)
2053 continue;
2054
Ben Blum74a11662011-05-26 16:25:20 -07002055 /* as per above, nr_threads may decrease, but not increase. */
2056 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002057 ent.task = tsk;
2058 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002059 /* nothing to do if this task is already in the cgroup */
2060 if (ent.cgrp == cgrp)
2061 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002062 /*
2063 * saying GFP_ATOMIC has no effect here because we did prealloc
2064 * earlier, but it's good form to communicate our expectations.
2065 */
Tejun Heo134d3372011-12-12 18:12:21 -08002066 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002067 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002068 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002069
2070 if (!threadgroup)
2071 break;
Ben Blum74a11662011-05-26 16:25:20 -07002072 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002073 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002074 /* remember the number of threads in the array for later. */
2075 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002076 tset.tc_array = group;
2077 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002078
Tejun Heo134d3372011-12-12 18:12:21 -08002079 /* methods shouldn't be called if no task is actually migrating */
2080 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002081 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002082 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002083
Ben Blum74a11662011-05-26 16:25:20 -07002084 /*
2085 * step 1: check that we can legitimately attach to the cgroup.
2086 */
Tejun Heo5549c492013-06-24 15:21:48 -07002087 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002088 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002089 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002090 if (retval) {
2091 failed_ss = ss;
2092 goto out_cancel_attach;
2093 }
2094 }
Ben Blum74a11662011-05-26 16:25:20 -07002095 }
2096
2097 /*
2098 * step 2: make sure css_sets exist for all threads to be migrated.
2099 * we use find_css_set, which allocates a new one if necessary.
2100 */
Ben Blum74a11662011-05-26 16:25:20 -07002101 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002102 struct css_set *old_cset;
2103
Tejun Heo134d3372011-12-12 18:12:21 -08002104 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002105 old_cset = task_css_set(tc->task);
2106 tc->cg = find_css_set(old_cset, cgrp);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002107 if (!tc->cg) {
2108 retval = -ENOMEM;
2109 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002110 }
2111 }
2112
2113 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002114 * step 3: now that we're guaranteed success wrt the css_sets,
2115 * proceed to move all tasks to the new cgroup. There are no
2116 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002117 */
Ben Blum74a11662011-05-26 16:25:20 -07002118 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002119 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002120 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002121 }
2122 /* nothing is sensitive to fork() after this point. */
2123
2124 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002125 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002126 */
Tejun Heo5549c492013-06-24 15:21:48 -07002127 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002128 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002129 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002130 }
2131
2132 /*
2133 * step 5: success! and cleanup
2134 */
Ben Blum74a11662011-05-26 16:25:20 -07002135 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002136out_put_css_set_refs:
2137 if (retval) {
2138 for (i = 0; i < group_size; i++) {
2139 tc = flex_array_get(group, i);
2140 if (!tc->cg)
2141 break;
2142 put_css_set(tc->cg);
2143 }
Ben Blum74a11662011-05-26 16:25:20 -07002144 }
2145out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002146 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002147 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002148 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002149 break;
Ben Blum74a11662011-05-26 16:25:20 -07002150 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002151 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002152 }
2153 }
Ben Blum74a11662011-05-26 16:25:20 -07002154out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002155 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002156 return retval;
2157}
2158
2159/*
2160 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002161 * function to attach either it or all tasks in its threadgroup. Will lock
2162 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002163 */
2164static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002165{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002166 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002167 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 int ret;
2169
Ben Blum74a11662011-05-26 16:25:20 -07002170 if (!cgroup_lock_live_group(cgrp))
2171 return -ENODEV;
2172
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173retry_find_task:
2174 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002176 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002177 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002178 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002179 ret= -ESRCH;
2180 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002181 }
Ben Blum74a11662011-05-26 16:25:20 -07002182 /*
2183 * even if we're attaching all tasks in the thread group, we
2184 * only need to check permissions on one of them.
2185 */
David Howellsc69e8d92008-11-14 10:39:19 +11002186 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002187 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2188 !uid_eq(cred->euid, tcred->uid) &&
2189 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002190 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002191 ret = -EACCES;
2192 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002193 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002194 } else
2195 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002196
2197 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002198 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002199
2200 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002201 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002202 * trapped in a cpuset, or RT worker may be born in a cgroup
2203 * with no rt_runtime allocated. Just say no.
2204 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002205 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002206 ret = -EINVAL;
2207 rcu_read_unlock();
2208 goto out_unlock_cgroup;
2209 }
2210
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211 get_task_struct(tsk);
2212 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002213
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002214 threadgroup_lock(tsk);
2215 if (threadgroup) {
2216 if (!thread_group_leader(tsk)) {
2217 /*
2218 * a race with de_thread from another thread's exec()
2219 * may strip us of our leadership, if this happens,
2220 * there is no choice but to throw this task away and
2221 * try again; this is
2222 * "double-double-toil-and-trouble-check locking".
2223 */
2224 threadgroup_unlock(tsk);
2225 put_task_struct(tsk);
2226 goto retry_find_task;
2227 }
Li Zefan081aa452013-03-13 09:17:09 +08002228 }
2229
2230 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2231
Tejun Heocd3d0952011-12-12 18:12:21 -08002232 threadgroup_unlock(tsk);
2233
Paul Menagebbcb81d2007-10-18 23:39:32 -07002234 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002236 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002237 return ret;
2238}
2239
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002240/**
2241 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2242 * @from: attach to all cgroups of a given task
2243 * @tsk: the task to be attached
2244 */
2245int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2246{
2247 struct cgroupfs_root *root;
2248 int retval = 0;
2249
Tejun Heo47cfcd02013-04-07 09:29:51 -07002250 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002251 for_each_active_root(root) {
2252 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2253
2254 retval = cgroup_attach_task(from_cg, tsk, false);
2255 if (retval)
2256 break;
2257 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002258 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002259
2260 return retval;
2261}
2262EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2263
Paul Menageaf351022008-07-25 01:47:01 -07002264static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2265{
Ben Blum74a11662011-05-26 16:25:20 -07002266 return attach_task_by_pid(cgrp, pid, false);
2267}
2268
2269static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2270{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002271 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002272}
2273
Paul Menagee788e062008-07-25 01:46:59 -07002274static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2275 const char *buffer)
2276{
2277 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002278 if (strlen(buffer) >= PATH_MAX)
2279 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002280 if (!cgroup_lock_live_group(cgrp))
2281 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002282 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002283 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002284 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002285 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002286 return 0;
2287}
2288
2289static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2290 struct seq_file *seq)
2291{
2292 if (!cgroup_lock_live_group(cgrp))
2293 return -ENODEV;
2294 seq_puts(seq, cgrp->root->release_agent_path);
2295 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002296 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002297 return 0;
2298}
2299
Tejun Heo873fe092013-04-14 20:15:26 -07002300static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2301 struct seq_file *seq)
2302{
2303 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002304 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002305}
2306
Paul Menage84eea842008-07-25 01:47:00 -07002307/* A buffer size big enough for numbers or short strings */
2308#define CGROUP_LOCAL_BUFFER_SIZE 64
2309
Paul Menagee73d2c62008-04-29 01:00:06 -07002310static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002311 struct file *file,
2312 const char __user *userbuf,
2313 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002314{
Paul Menage84eea842008-07-25 01:47:00 -07002315 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002316 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002317 char *end;
2318
2319 if (!nbytes)
2320 return -EINVAL;
2321 if (nbytes >= sizeof(buffer))
2322 return -E2BIG;
2323 if (copy_from_user(buffer, userbuf, nbytes))
2324 return -EFAULT;
2325
2326 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002328 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002329 if (*end)
2330 return -EINVAL;
2331 retval = cft->write_u64(cgrp, cft, val);
2332 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002333 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002334 if (*end)
2335 return -EINVAL;
2336 retval = cft->write_s64(cgrp, cft, val);
2337 }
Paul Menage355e0c42007-10-18 23:39:33 -07002338 if (!retval)
2339 retval = nbytes;
2340 return retval;
2341}
2342
Paul Menagedb3b1492008-07-25 01:46:58 -07002343static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2344 struct file *file,
2345 const char __user *userbuf,
2346 size_t nbytes, loff_t *unused_ppos)
2347{
Paul Menage84eea842008-07-25 01:47:00 -07002348 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002349 int retval = 0;
2350 size_t max_bytes = cft->max_write_len;
2351 char *buffer = local_buffer;
2352
2353 if (!max_bytes)
2354 max_bytes = sizeof(local_buffer) - 1;
2355 if (nbytes >= max_bytes)
2356 return -E2BIG;
2357 /* Allocate a dynamic buffer if we need one */
2358 if (nbytes >= sizeof(local_buffer)) {
2359 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2360 if (buffer == NULL)
2361 return -ENOMEM;
2362 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002363 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2364 retval = -EFAULT;
2365 goto out;
2366 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002367
2368 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002369 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002370 if (!retval)
2371 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002372out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002373 if (buffer != local_buffer)
2374 kfree(buffer);
2375 return retval;
2376}
2377
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2379 size_t nbytes, loff_t *ppos)
2380{
2381 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002382 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383
Tejun Heo54766d42013-06-12 21:04:53 -07002384 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002386 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002387 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002388 if (cft->write_u64 || cft->write_s64)
2389 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002390 if (cft->write_string)
2391 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002392 if (cft->trigger) {
2393 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2394 return ret ? ret : nbytes;
2395 }
Paul Menage355e0c42007-10-18 23:39:33 -07002396 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397}
2398
Paul Menagef4c753b2008-04-29 00:59:56 -07002399static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2400 struct file *file,
2401 char __user *buf, size_t nbytes,
2402 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403{
Paul Menage84eea842008-07-25 01:47:00 -07002404 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002405 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2407
2408 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2409}
2410
Paul Menagee73d2c62008-04-29 01:00:06 -07002411static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2412 struct file *file,
2413 char __user *buf, size_t nbytes,
2414 loff_t *ppos)
2415{
Paul Menage84eea842008-07-25 01:47:00 -07002416 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002417 s64 val = cft->read_s64(cgrp, cft);
2418 int len = sprintf(tmp, "%lld\n", (long long) val);
2419
2420 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2421}
2422
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2424 size_t nbytes, loff_t *ppos)
2425{
2426 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002427 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428
Tejun Heo54766d42013-06-12 21:04:53 -07002429 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430 return -ENODEV;
2431
2432 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002433 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002434 if (cft->read_u64)
2435 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002436 if (cft->read_s64)
2437 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002438 return -EINVAL;
2439}
2440
Paul Menage91796562008-04-29 01:00:01 -07002441/*
2442 * seqfile ops/methods for returning structured data. Currently just
2443 * supports string->u64 maps, but can be extended in future.
2444 */
2445
2446struct cgroup_seqfile_state {
2447 struct cftype *cft;
2448 struct cgroup *cgroup;
2449};
2450
2451static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2452{
2453 struct seq_file *sf = cb->state;
2454 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2455}
2456
2457static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2458{
2459 struct cgroup_seqfile_state *state = m->private;
2460 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002461 if (cft->read_map) {
2462 struct cgroup_map_cb cb = {
2463 .fill = cgroup_map_add,
2464 .state = m,
2465 };
2466 return cft->read_map(state->cgroup, cft, &cb);
2467 }
2468 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002469}
2470
Adrian Bunk96930a62008-07-25 19:46:21 -07002471static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002472{
2473 struct seq_file *seq = file->private_data;
2474 kfree(seq->private);
2475 return single_release(inode, file);
2476}
2477
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002478static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002479 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002480 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002481 .llseek = seq_lseek,
2482 .release = cgroup_seqfile_release,
2483};
2484
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485static int cgroup_file_open(struct inode *inode, struct file *file)
2486{
2487 int err;
2488 struct cftype *cft;
2489
2490 err = generic_file_open(inode, file);
2491 if (err)
2492 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002493 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002494
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002495 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002496 struct cgroup_seqfile_state *state;
2497
2498 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002499 if (!state)
2500 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002501
Paul Menage91796562008-04-29 01:00:01 -07002502 state->cft = cft;
2503 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2504 file->f_op = &cgroup_seqfile_operations;
2505 err = single_open(file, cgroup_seqfile_show, state);
2506 if (err < 0)
2507 kfree(state);
2508 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509 err = cft->open(inode, file);
2510 else
2511 err = 0;
2512
2513 return err;
2514}
2515
2516static int cgroup_file_release(struct inode *inode, struct file *file)
2517{
2518 struct cftype *cft = __d_cft(file->f_dentry);
2519 if (cft->release)
2520 return cft->release(inode, file);
2521 return 0;
2522}
2523
2524/*
2525 * cgroup_rename - Only allow simple rename of directories in place.
2526 */
2527static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2528 struct inode *new_dir, struct dentry *new_dentry)
2529{
Li Zefan65dff752013-03-01 15:01:56 +08002530 int ret;
2531 struct cgroup_name *name, *old_name;
2532 struct cgroup *cgrp;
2533
2534 /*
2535 * It's convinient to use parent dir's i_mutex to protected
2536 * cgrp->name.
2537 */
2538 lockdep_assert_held(&old_dir->i_mutex);
2539
Paul Menageddbcc7e2007-10-18 23:39:30 -07002540 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2541 return -ENOTDIR;
2542 if (new_dentry->d_inode)
2543 return -EEXIST;
2544 if (old_dir != new_dir)
2545 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002546
2547 cgrp = __d_cgrp(old_dentry);
2548
Tejun Heo6db8e852013-06-14 11:18:22 -07002549 /*
2550 * This isn't a proper migration and its usefulness is very
2551 * limited. Disallow if sane_behavior.
2552 */
2553 if (cgroup_sane_behavior(cgrp))
2554 return -EPERM;
2555
Li Zefan65dff752013-03-01 15:01:56 +08002556 name = cgroup_alloc_name(new_dentry);
2557 if (!name)
2558 return -ENOMEM;
2559
2560 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2561 if (ret) {
2562 kfree(name);
2563 return ret;
2564 }
2565
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002566 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002567 rcu_assign_pointer(cgrp->name, name);
2568
2569 kfree_rcu(old_name, rcu_head);
2570 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002571}
2572
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002573static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2574{
2575 if (S_ISDIR(dentry->d_inode->i_mode))
2576 return &__d_cgrp(dentry)->xattrs;
2577 else
Li Zefan712317a2013-04-18 23:09:52 -07002578 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002579}
2580
2581static inline int xattr_enabled(struct dentry *dentry)
2582{
2583 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002584 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002585}
2586
2587static bool is_valid_xattr(const char *name)
2588{
2589 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2590 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2591 return true;
2592 return false;
2593}
2594
2595static int cgroup_setxattr(struct dentry *dentry, const char *name,
2596 const void *val, size_t size, int flags)
2597{
2598 if (!xattr_enabled(dentry))
2599 return -EOPNOTSUPP;
2600 if (!is_valid_xattr(name))
2601 return -EINVAL;
2602 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2603}
2604
2605static int cgroup_removexattr(struct dentry *dentry, const char *name)
2606{
2607 if (!xattr_enabled(dentry))
2608 return -EOPNOTSUPP;
2609 if (!is_valid_xattr(name))
2610 return -EINVAL;
2611 return simple_xattr_remove(__d_xattrs(dentry), name);
2612}
2613
2614static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2615 void *buf, size_t size)
2616{
2617 if (!xattr_enabled(dentry))
2618 return -EOPNOTSUPP;
2619 if (!is_valid_xattr(name))
2620 return -EINVAL;
2621 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2622}
2623
2624static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2625{
2626 if (!xattr_enabled(dentry))
2627 return -EOPNOTSUPP;
2628 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2629}
2630
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002631static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 .read = cgroup_file_read,
2633 .write = cgroup_file_write,
2634 .llseek = generic_file_llseek,
2635 .open = cgroup_file_open,
2636 .release = cgroup_file_release,
2637};
2638
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002639static const struct inode_operations cgroup_file_inode_operations = {
2640 .setxattr = cgroup_setxattr,
2641 .getxattr = cgroup_getxattr,
2642 .listxattr = cgroup_listxattr,
2643 .removexattr = cgroup_removexattr,
2644};
2645
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002646static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002647 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002648 .mkdir = cgroup_mkdir,
2649 .rmdir = cgroup_rmdir,
2650 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002651 .setxattr = cgroup_setxattr,
2652 .getxattr = cgroup_getxattr,
2653 .listxattr = cgroup_listxattr,
2654 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002655};
2656
Al Viro00cd8dd2012-06-10 17:13:09 -04002657static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002658{
2659 if (dentry->d_name.len > NAME_MAX)
2660 return ERR_PTR(-ENAMETOOLONG);
2661 d_add(dentry, NULL);
2662 return NULL;
2663}
2664
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002665/*
2666 * Check if a file is a control file
2667 */
2668static inline struct cftype *__file_cft(struct file *file)
2669{
Al Viro496ad9a2013-01-23 17:07:38 -05002670 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002671 return ERR_PTR(-EINVAL);
2672 return __d_cft(file->f_dentry);
2673}
2674
Al Viroa5e7ed32011-07-26 01:55:55 -04002675static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002676 struct super_block *sb)
2677{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 struct inode *inode;
2679
2680 if (!dentry)
2681 return -ENOENT;
2682 if (dentry->d_inode)
2683 return -EEXIST;
2684
2685 inode = cgroup_new_inode(mode, sb);
2686 if (!inode)
2687 return -ENOMEM;
2688
2689 if (S_ISDIR(mode)) {
2690 inode->i_op = &cgroup_dir_inode_operations;
2691 inode->i_fop = &simple_dir_operations;
2692
2693 /* start off with i_nlink == 2 (for "." entry) */
2694 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002695 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002696
Tejun Heob8a2df62012-11-19 08:13:37 -08002697 /*
2698 * Control reaches here with cgroup_mutex held.
2699 * @inode->i_mutex should nest outside cgroup_mutex but we
2700 * want to populate it immediately without releasing
2701 * cgroup_mutex. As @inode isn't visible to anyone else
2702 * yet, trylock will always succeed without affecting
2703 * lockdep checks.
2704 */
2705 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002706 } else if (S_ISREG(mode)) {
2707 inode->i_size = 0;
2708 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002709 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002711 d_instantiate(dentry, inode);
2712 dget(dentry); /* Extra count - pin the dentry in core */
2713 return 0;
2714}
2715
Li Zefan099fca32009-04-02 16:57:29 -07002716/**
2717 * cgroup_file_mode - deduce file mode of a control file
2718 * @cft: the control file in question
2719 *
2720 * returns cft->mode if ->mode is not 0
2721 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2722 * returns S_IRUGO if it has only a read handler
2723 * returns S_IWUSR if it has only a write hander
2724 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002725static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002726{
Al Viroa5e7ed32011-07-26 01:55:55 -04002727 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002728
2729 if (cft->mode)
2730 return cft->mode;
2731
2732 if (cft->read || cft->read_u64 || cft->read_s64 ||
2733 cft->read_map || cft->read_seq_string)
2734 mode |= S_IRUGO;
2735
2736 if (cft->write || cft->write_u64 || cft->write_s64 ||
2737 cft->write_string || cft->trigger)
2738 mode |= S_IWUSR;
2739
2740 return mode;
2741}
2742
Tejun Heodb0416b2012-04-01 12:09:55 -07002743static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002744 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745{
Paul Menagebd89aab2007-10-18 23:40:44 -07002746 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002749 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002751 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002753
Tejun Heo93438622013-04-14 20:15:25 -07002754 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755 strcpy(name, subsys->name);
2756 strcat(name, ".");
2757 }
2758 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002759
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002761
2762 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2763 if (!cfe)
2764 return -ENOMEM;
2765
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002768 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002769 goto out;
2770 }
2771
Li Zefand6cbf352013-05-14 19:44:20 +08002772 cfe->type = (void *)cft;
2773 cfe->dentry = dentry;
2774 dentry->d_fsdata = cfe;
2775 simple_xattrs_init(&cfe->xattrs);
2776
Tejun Heo05ef1d72012-04-01 12:09:56 -07002777 mode = cgroup_file_mode(cft);
2778 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2779 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002780 list_add_tail(&cfe->node, &parent->files);
2781 cfe = NULL;
2782 }
2783 dput(dentry);
2784out:
2785 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786 return error;
2787}
2788
Tejun Heob1f28d32013-06-28 16:24:10 -07002789/**
2790 * cgroup_addrm_files - add or remove files to a cgroup directory
2791 * @cgrp: the target cgroup
2792 * @subsys: the subsystem of files to be added
2793 * @cfts: array of cftypes to be added
2794 * @is_add: whether to add or remove
2795 *
2796 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
2797 * All @cfts should belong to @subsys. For removals, this function never
2798 * fails. If addition fails, this function doesn't remove files already
2799 * added. The caller is responsible for cleaning up.
2800 */
Tejun Heo79578622012-04-01 12:09:56 -07002801static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002802 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002803{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002804 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002805 int ret;
2806
2807 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2808 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002809
2810 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002811 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002812 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2813 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002814 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2815 continue;
2816 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2817 continue;
2818
Li Zefan2739d3c2013-01-21 18:18:33 +08002819 if (is_add) {
Tejun Heob1f28d32013-06-28 16:24:10 -07002820 ret = cgroup_add_file(cgrp, subsys, cft);
2821 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002822 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002823 cft->name, ret);
2824 return ret;
2825 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002826 } else {
2827 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002828 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002829 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002830 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002831}
2832
Tejun Heo8e3f6542012-04-01 12:09:55 -07002833static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002834 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002835{
2836 /*
2837 * Thanks to the entanglement with vfs inode locking, we can't walk
2838 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002839 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2840 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002841 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002842 mutex_lock(&cgroup_mutex);
2843}
2844
Tejun Heo9ccece82013-06-28 16:24:11 -07002845static int cgroup_cfts_commit(struct cgroup_subsys *ss,
2846 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002847 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848{
2849 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002850 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002851 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002852 struct dentry *prev = NULL;
2853 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002854 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002855 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002856
2857 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002858 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002859 !atomic_inc_not_zero(&sb->s_active)) {
2860 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002861 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862 }
2863
Li Zefane8c82d22013-06-18 18:48:37 +08002864 /*
2865 * All cgroups which are created after we drop cgroup_mutex will
2866 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002867 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002868 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002869 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002870
Tejun Heo8e3f6542012-04-01 12:09:55 -07002871 mutex_unlock(&cgroup_mutex);
2872
Li Zefane8c82d22013-06-18 18:48:37 +08002873 /* @root always needs to be updated */
2874 inode = root->dentry->d_inode;
2875 mutex_lock(&inode->i_mutex);
2876 mutex_lock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002877 ret = cgroup_addrm_files(root, ss, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002878 mutex_unlock(&cgroup_mutex);
2879 mutex_unlock(&inode->i_mutex);
2880
Tejun Heo9ccece82013-06-28 16:24:11 -07002881 if (ret)
2882 goto out_deact;
2883
Li Zefane8c82d22013-06-18 18:48:37 +08002884 /* add/rm files for all cgroups created before */
2885 rcu_read_lock();
2886 cgroup_for_each_descendant_pre(cgrp, root) {
2887 if (cgroup_is_dead(cgrp))
2888 continue;
2889
2890 inode = cgrp->dentry->d_inode;
2891 dget(cgrp->dentry);
2892 rcu_read_unlock();
2893
2894 dput(prev);
2895 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002896
2897 mutex_lock(&inode->i_mutex);
2898 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002899 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo9ccece82013-06-28 16:24:11 -07002900 ret = cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901 mutex_unlock(&cgroup_mutex);
2902 mutex_unlock(&inode->i_mutex);
2903
Li Zefane8c82d22013-06-18 18:48:37 +08002904 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002905 if (ret)
2906 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002907 }
Li Zefane8c82d22013-06-18 18:48:37 +08002908 rcu_read_unlock();
2909 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002910out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002911 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002912 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002913}
2914
2915/**
2916 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2917 * @ss: target cgroup subsystem
2918 * @cfts: zero-length name terminated array of cftypes
2919 *
2920 * Register @cfts to @ss. Files described by @cfts are created for all
2921 * existing cgroups to which @ss is attached and all future cgroups will
2922 * have them too. This function can be called anytime whether @ss is
2923 * attached or not.
2924 *
2925 * Returns 0 on successful registration, -errno on failure. Note that this
2926 * function currently returns 0 as long as @cfts registration is successful
2927 * even if some file creation attempts on existing cgroups fail.
2928 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002929int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930{
2931 struct cftype_set *set;
Tejun Heo9ccece82013-06-28 16:24:11 -07002932 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002933
2934 set = kzalloc(sizeof(*set), GFP_KERNEL);
2935 if (!set)
2936 return -ENOMEM;
2937
2938 cgroup_cfts_prepare();
2939 set->cfts = cfts;
2940 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo9ccece82013-06-28 16:24:11 -07002941 ret = cgroup_cfts_commit(ss, cfts, true);
2942 if (ret)
2943 cgroup_rm_cftypes(ss, cfts);
2944 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002945}
2946EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2947
Li Zefana043e3b2008-02-23 15:24:09 -08002948/**
Tejun Heo79578622012-04-01 12:09:56 -07002949 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2950 * @ss: target cgroup subsystem
2951 * @cfts: zero-length name terminated array of cftypes
2952 *
2953 * Unregister @cfts from @ss. Files described by @cfts are removed from
2954 * all existing cgroups to which @ss is attached and all future cgroups
2955 * won't have them either. This function can be called anytime whether @ss
2956 * is attached or not.
2957 *
2958 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2959 * registered with @ss.
2960 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002961int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002962{
2963 struct cftype_set *set;
2964
2965 cgroup_cfts_prepare();
2966
2967 list_for_each_entry(set, &ss->cftsets, node) {
2968 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002969 list_del(&set->node);
2970 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002971 cgroup_cfts_commit(ss, cfts, false);
2972 return 0;
2973 }
2974 }
2975
2976 cgroup_cfts_commit(ss, NULL, false);
2977 return -ENOENT;
2978}
2979
2980/**
Li Zefana043e3b2008-02-23 15:24:09 -08002981 * cgroup_task_count - count the number of tasks in a cgroup.
2982 * @cgrp: the cgroup in question
2983 *
2984 * Return the number of tasks in the cgroup.
2985 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002986int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002987{
2988 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002989 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002990
Paul Menage817929e2007-10-18 23:39:36 -07002991 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002992 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2993 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002994 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002995 return count;
2996}
2997
2998/*
Paul Menage817929e2007-10-18 23:39:36 -07002999 * Advance a list_head iterator. The iterator should be positioned at
3000 * the start of a css_set
3001 */
Tejun Heo69d02062013-06-12 21:04:50 -07003002static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003003{
Tejun Heo69d02062013-06-12 21:04:50 -07003004 struct list_head *l = it->cset_link;
3005 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07003006 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07003007
3008 /* Advance to the next non-empty css_set */
3009 do {
3010 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003011 if (l == &cgrp->cset_links) {
3012 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07003013 return;
3014 }
Tejun Heo69d02062013-06-12 21:04:50 -07003015 link = list_entry(l, struct cgrp_cset_link, cset_link);
3016 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07003017 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07003018 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07003019 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07003020}
3021
Cliff Wickman31a7df02008-02-07 00:14:42 -08003022/*
3023 * To reduce the fork() overhead for systems that are not actually
3024 * using their cgroups capability, we don't maintain the lists running
3025 * through each css_set to its tasks until we see the list actually
3026 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003027 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003028static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003029{
3030 struct task_struct *p, *g;
3031 write_lock(&css_set_lock);
3032 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003033 /*
3034 * We need tasklist_lock because RCU is not safe against
3035 * while_each_thread(). Besides, a forking task that has passed
3036 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3037 * is not guaranteed to have its child immediately visible in the
3038 * tasklist if we walk through it with RCU.
3039 */
3040 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003041 do_each_thread(g, p) {
3042 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003043 /*
3044 * We should check if the process is exiting, otherwise
3045 * it will race with cgroup_exit() in that the list
3046 * entry won't be deleted though the process has exited.
3047 */
3048 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003049 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003050 task_unlock(p);
3051 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003052 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003053 write_unlock(&css_set_lock);
3054}
3055
Tejun Heo574bd9f2012-11-09 09:12:29 -08003056/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003057 * cgroup_next_sibling - find the next sibling of a given cgroup
3058 * @pos: the current cgroup
3059 *
3060 * This function returns the next sibling of @pos and should be called
3061 * under RCU read lock. The only requirement is that @pos is accessible.
3062 * The next sibling is guaranteed to be returned regardless of @pos's
3063 * state.
3064 */
3065struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3066{
3067 struct cgroup *next;
3068
3069 WARN_ON_ONCE(!rcu_read_lock_held());
3070
3071 /*
3072 * @pos could already have been removed. Once a cgroup is removed,
3073 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003074 * changes. As CGRP_DEAD assertion is serialized and happens
3075 * before the cgroup is taken off the ->sibling list, if we see it
3076 * unasserted, it's guaranteed that the next sibling hasn't
3077 * finished its grace period even if it's already removed, and thus
3078 * safe to dereference from this RCU critical section. If
3079 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3080 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003081 */
Tejun Heo54766d42013-06-12 21:04:53 -07003082 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003083 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3084 if (&next->sibling != &pos->parent->children)
3085 return next;
3086 return NULL;
3087 }
3088
3089 /*
3090 * Can't dereference the next pointer. Each cgroup is given a
3091 * monotonically increasing unique serial number and always
3092 * appended to the sibling list, so the next one can be found by
3093 * walking the parent's children until we see a cgroup with higher
3094 * serial number than @pos's.
3095 *
3096 * While this path can be slow, it's taken only when either the
3097 * current cgroup is removed or iteration and removal race.
3098 */
3099 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3100 if (next->serial_nr > pos->serial_nr)
3101 return next;
3102 return NULL;
3103}
3104EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3105
3106/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003107 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3108 * @pos: the current position (%NULL to initiate traversal)
3109 * @cgroup: cgroup whose descendants to walk
3110 *
3111 * To be used by cgroup_for_each_descendant_pre(). Find the next
3112 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003113 *
3114 * While this function requires RCU read locking, it doesn't require the
3115 * whole traversal to be contained in a single RCU critical section. This
3116 * function will return the correct next descendant as long as both @pos
3117 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118 */
3119struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3120 struct cgroup *cgroup)
3121{
3122 struct cgroup *next;
3123
3124 WARN_ON_ONCE(!rcu_read_lock_held());
3125
3126 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003127 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003128 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003129
3130 /* visit the first child if exists */
3131 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3132 if (next)
3133 return next;
3134
3135 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003136 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003137 next = cgroup_next_sibling(pos);
3138 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003139 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003140 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003141 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003142
3143 return NULL;
3144}
3145EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3146
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003147/**
3148 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3149 * @pos: cgroup of interest
3150 *
3151 * Return the rightmost descendant of @pos. If there's no descendant,
3152 * @pos is returned. This can be used during pre-order traversal to skip
3153 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003154 *
3155 * While this function requires RCU read locking, it doesn't require the
3156 * whole traversal to be contained in a single RCU critical section. This
3157 * function will return the correct rightmost descendant as long as @pos is
3158 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003159 */
3160struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3161{
3162 struct cgroup *last, *tmp;
3163
3164 WARN_ON_ONCE(!rcu_read_lock_held());
3165
3166 do {
3167 last = pos;
3168 /* ->prev isn't RCU safe, walk ->next till the end */
3169 pos = NULL;
3170 list_for_each_entry_rcu(tmp, &last->children, sibling)
3171 pos = tmp;
3172 } while (pos);
3173
3174 return last;
3175}
3176EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3177
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3179{
3180 struct cgroup *last;
3181
3182 do {
3183 last = pos;
3184 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3185 sibling);
3186 } while (pos);
3187
3188 return last;
3189}
3190
3191/**
3192 * cgroup_next_descendant_post - find the next descendant for post-order walk
3193 * @pos: the current position (%NULL to initiate traversal)
3194 * @cgroup: cgroup whose descendants to walk
3195 *
3196 * To be used by cgroup_for_each_descendant_post(). Find the next
3197 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003198 *
3199 * While this function requires RCU read locking, it doesn't require the
3200 * whole traversal to be contained in a single RCU critical section. This
3201 * function will return the correct next descendant as long as both @pos
3202 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203 */
3204struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3205 struct cgroup *cgroup)
3206{
3207 struct cgroup *next;
3208
3209 WARN_ON_ONCE(!rcu_read_lock_held());
3210
3211 /* if first iteration, visit the leftmost descendant */
3212 if (!pos) {
3213 next = cgroup_leftmost_descendant(cgroup);
3214 return next != cgroup ? next : NULL;
3215 }
3216
3217 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003218 next = cgroup_next_sibling(pos);
3219 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220 return cgroup_leftmost_descendant(next);
3221
3222 /* no sibling left, visit parent */
3223 next = pos->parent;
3224 return next != cgroup ? next : NULL;
3225}
3226EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3227
Paul Menagebd89aab2007-10-18 23:40:44 -07003228void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003229 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003230{
3231 /*
3232 * The first time anyone tries to iterate across a cgroup,
3233 * we need to enable the list linking each css_set to its
3234 * tasks, and fix up all existing tasks.
3235 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003236 if (!use_task_css_set_links)
3237 cgroup_enable_task_cg_lists();
3238
Paul Menage817929e2007-10-18 23:39:36 -07003239 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003240 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003241 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003242}
3243
Paul Menagebd89aab2007-10-18 23:40:44 -07003244struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003245 struct cgroup_iter *it)
3246{
3247 struct task_struct *res;
3248 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003249 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003250
3251 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003252 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003253 return NULL;
3254 res = list_entry(l, struct task_struct, cg_list);
3255 /* Advance iterator to find next entry */
3256 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003257 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3258 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003259 /* We reached the end of this task list - move on to
3260 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003261 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003262 } else {
3263 it->task = l;
3264 }
3265 return res;
3266}
3267
Paul Menagebd89aab2007-10-18 23:40:44 -07003268void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003269 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003270{
3271 read_unlock(&css_set_lock);
3272}
3273
Cliff Wickman31a7df02008-02-07 00:14:42 -08003274static inline int started_after_time(struct task_struct *t1,
3275 struct timespec *time,
3276 struct task_struct *t2)
3277{
3278 int start_diff = timespec_compare(&t1->start_time, time);
3279 if (start_diff > 0) {
3280 return 1;
3281 } else if (start_diff < 0) {
3282 return 0;
3283 } else {
3284 /*
3285 * Arbitrarily, if two processes started at the same
3286 * time, we'll say that the lower pointer value
3287 * started first. Note that t2 may have exited by now
3288 * so this may not be a valid pointer any longer, but
3289 * that's fine - it still serves to distinguish
3290 * between two tasks started (effectively) simultaneously.
3291 */
3292 return t1 > t2;
3293 }
3294}
3295
3296/*
3297 * This function is a callback from heap_insert() and is used to order
3298 * the heap.
3299 * In this case we order the heap in descending task start time.
3300 */
3301static inline int started_after(void *p1, void *p2)
3302{
3303 struct task_struct *t1 = p1;
3304 struct task_struct *t2 = p2;
3305 return started_after_time(t1, &t2->start_time, t2);
3306}
3307
3308/**
3309 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3310 * @scan: struct cgroup_scanner containing arguments for the scan
3311 *
3312 * Arguments include pointers to callback functions test_task() and
3313 * process_task().
3314 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3315 * and if it returns true, call process_task() for it also.
3316 * The test_task pointer may be NULL, meaning always true (select all tasks).
3317 * Effectively duplicates cgroup_iter_{start,next,end}()
3318 * but does not lock css_set_lock for the call to process_task().
3319 * The struct cgroup_scanner may be embedded in any structure of the caller's
3320 * creation.
3321 * It is guaranteed that process_task() will act on every task that
3322 * is a member of the cgroup for the duration of this call. This
3323 * function may or may not call process_task() for tasks that exit
3324 * or move to a different cgroup during the call, or are forked or
3325 * move into the cgroup during the call.
3326 *
3327 * Note that test_task() may be called with locks held, and may in some
3328 * situations be called multiple times for the same task, so it should
3329 * be cheap.
3330 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3331 * pre-allocated and will be used for heap operations (and its "gt" member will
3332 * be overwritten), else a temporary heap will be used (allocation of which
3333 * may cause this function to fail).
3334 */
3335int cgroup_scan_tasks(struct cgroup_scanner *scan)
3336{
3337 int retval, i;
3338 struct cgroup_iter it;
3339 struct task_struct *p, *dropped;
3340 /* Never dereference latest_task, since it's not refcounted */
3341 struct task_struct *latest_task = NULL;
3342 struct ptr_heap tmp_heap;
3343 struct ptr_heap *heap;
3344 struct timespec latest_time = { 0, 0 };
3345
3346 if (scan->heap) {
3347 /* The caller supplied our heap and pre-allocated its memory */
3348 heap = scan->heap;
3349 heap->gt = &started_after;
3350 } else {
3351 /* We need to allocate our own heap memory */
3352 heap = &tmp_heap;
3353 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3354 if (retval)
3355 /* cannot allocate the heap */
3356 return retval;
3357 }
3358
3359 again:
3360 /*
3361 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3362 * to determine which are of interest, and using the scanner's
3363 * "process_task" callback to process any of them that need an update.
3364 * Since we don't want to hold any locks during the task updates,
3365 * gather tasks to be processed in a heap structure.
3366 * The heap is sorted by descending task start time.
3367 * If the statically-sized heap fills up, we overflow tasks that
3368 * started later, and in future iterations only consider tasks that
3369 * started after the latest task in the previous pass. This
3370 * guarantees forward progress and that we don't miss any tasks.
3371 */
3372 heap->size = 0;
3373 cgroup_iter_start(scan->cg, &it);
3374 while ((p = cgroup_iter_next(scan->cg, &it))) {
3375 /*
3376 * Only affect tasks that qualify per the caller's callback,
3377 * if he provided one
3378 */
3379 if (scan->test_task && !scan->test_task(p, scan))
3380 continue;
3381 /*
3382 * Only process tasks that started after the last task
3383 * we processed
3384 */
3385 if (!started_after_time(p, &latest_time, latest_task))
3386 continue;
3387 dropped = heap_insert(heap, p);
3388 if (dropped == NULL) {
3389 /*
3390 * The new task was inserted; the heap wasn't
3391 * previously full
3392 */
3393 get_task_struct(p);
3394 } else if (dropped != p) {
3395 /*
3396 * The new task was inserted, and pushed out a
3397 * different task
3398 */
3399 get_task_struct(p);
3400 put_task_struct(dropped);
3401 }
3402 /*
3403 * Else the new task was newer than anything already in
3404 * the heap and wasn't inserted
3405 */
3406 }
3407 cgroup_iter_end(scan->cg, &it);
3408
3409 if (heap->size) {
3410 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003411 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003412 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003413 latest_time = q->start_time;
3414 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003415 }
3416 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003417 scan->process_task(q, scan);
3418 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 }
3420 /*
3421 * If we had to process any tasks at all, scan again
3422 * in case some of them were in the middle of forking
3423 * children that didn't get processed.
3424 * Not the most efficient way to do it, but it avoids
3425 * having to take callback_mutex in the fork path
3426 */
3427 goto again;
3428 }
3429 if (heap == &tmp_heap)
3430 heap_free(&tmp_heap);
3431 return 0;
3432}
3433
Tejun Heo8cc99342013-04-07 09:29:50 -07003434static void cgroup_transfer_one_task(struct task_struct *task,
3435 struct cgroup_scanner *scan)
3436{
3437 struct cgroup *new_cgroup = scan->data;
3438
Tejun Heo47cfcd02013-04-07 09:29:51 -07003439 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003440 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003441 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003442}
3443
3444/**
3445 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3446 * @to: cgroup to which the tasks will be moved
3447 * @from: cgroup in which the tasks currently reside
3448 */
3449int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3450{
3451 struct cgroup_scanner scan;
3452
3453 scan.cg = from;
3454 scan.test_task = NULL; /* select all tasks in cgroup */
3455 scan.process_task = cgroup_transfer_one_task;
3456 scan.heap = NULL;
3457 scan.data = to;
3458
3459 return cgroup_scan_tasks(&scan);
3460}
3461
Paul Menage817929e2007-10-18 23:39:36 -07003462/*
Ben Blum102a7752009-09-23 15:56:26 -07003463 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003464 *
3465 * Reading this file can return large amounts of data if a cgroup has
3466 * *lots* of attached tasks. So it may need several calls to read(),
3467 * but we cannot guarantee that the information we produce is correct
3468 * unless we produce it entirely atomically.
3469 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003470 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003471
Li Zefan24528252012-01-20 11:58:43 +08003472/* which pidlist file are we talking about? */
3473enum cgroup_filetype {
3474 CGROUP_FILE_PROCS,
3475 CGROUP_FILE_TASKS,
3476};
3477
3478/*
3479 * A pidlist is a list of pids that virtually represents the contents of one
3480 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3481 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3482 * to the cgroup.
3483 */
3484struct cgroup_pidlist {
3485 /*
3486 * used to find which pidlist is wanted. doesn't change as long as
3487 * this particular list stays in the list.
3488 */
3489 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3490 /* array of xids */
3491 pid_t *list;
3492 /* how many elements the above list has */
3493 int length;
3494 /* how many files are using the current array */
3495 int use_count;
3496 /* each of these stored in a list by its cgroup */
3497 struct list_head links;
3498 /* pointer to the cgroup we belong to, for list removal purposes */
3499 struct cgroup *owner;
3500 /* protects the other fields */
3501 struct rw_semaphore mutex;
3502};
3503
Paul Menagebbcb81d2007-10-18 23:39:32 -07003504/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003505 * The following two functions "fix" the issue where there are more pids
3506 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3507 * TODO: replace with a kernel-wide solution to this problem
3508 */
3509#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3510static void *pidlist_allocate(int count)
3511{
3512 if (PIDLIST_TOO_LARGE(count))
3513 return vmalloc(count * sizeof(pid_t));
3514 else
3515 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3516}
3517static void pidlist_free(void *p)
3518{
3519 if (is_vmalloc_addr(p))
3520 vfree(p);
3521 else
3522 kfree(p);
3523}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003524
3525/*
Ben Blum102a7752009-09-23 15:56:26 -07003526 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003527 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003528 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003529static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003530{
Ben Blum102a7752009-09-23 15:56:26 -07003531 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003532
3533 /*
3534 * we presume the 0th element is unique, so i starts at 1. trivial
3535 * edge cases first; no work needs to be done for either
3536 */
3537 if (length == 0 || length == 1)
3538 return length;
3539 /* src and dest walk down the list; dest counts unique elements */
3540 for (src = 1; src < length; src++) {
3541 /* find next unique element */
3542 while (list[src] == list[src-1]) {
3543 src++;
3544 if (src == length)
3545 goto after;
3546 }
3547 /* dest always points to where the next unique element goes */
3548 list[dest] = list[src];
3549 dest++;
3550 }
3551after:
Ben Blum102a7752009-09-23 15:56:26 -07003552 return dest;
3553}
3554
3555static int cmppid(const void *a, const void *b)
3556{
3557 return *(pid_t *)a - *(pid_t *)b;
3558}
3559
3560/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003561 * find the appropriate pidlist for our purpose (given procs vs tasks)
3562 * returns with the lock on that pidlist already held, and takes care
3563 * of the use count, or returns NULL with no locks held if we're out of
3564 * memory.
3565 */
3566static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3567 enum cgroup_filetype type)
3568{
3569 struct cgroup_pidlist *l;
3570 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003571 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003572
Ben Blum72a8cb32009-09-23 15:56:27 -07003573 /*
3574 * We can't drop the pidlist_mutex before taking the l->mutex in case
3575 * the last ref-holder is trying to remove l from the list at the same
3576 * time. Holding the pidlist_mutex precludes somebody taking whichever
3577 * list we find out from under us - compare release_pid_array().
3578 */
3579 mutex_lock(&cgrp->pidlist_mutex);
3580 list_for_each_entry(l, &cgrp->pidlists, links) {
3581 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 /* make sure l doesn't vanish out from under us */
3583 down_write(&l->mutex);
3584 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003585 return l;
3586 }
3587 }
3588 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003589 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 if (!l) {
3591 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003592 return l;
3593 }
3594 init_rwsem(&l->mutex);
3595 down_write(&l->mutex);
3596 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003597 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003598 l->owner = cgrp;
3599 list_add(&l->links, &cgrp->pidlists);
3600 mutex_unlock(&cgrp->pidlist_mutex);
3601 return l;
3602}
3603
3604/*
Ben Blum102a7752009-09-23 15:56:26 -07003605 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3606 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003607static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3608 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003609{
3610 pid_t *array;
3611 int length;
3612 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003613 struct cgroup_iter it;
3614 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003615 struct cgroup_pidlist *l;
3616
3617 /*
3618 * If cgroup gets more users after we read count, we won't have
3619 * enough space - tough. This race is indistinguishable to the
3620 * caller from the case that the additional cgroup users didn't
3621 * show up until sometime later on.
3622 */
3623 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003624 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003625 if (!array)
3626 return -ENOMEM;
3627 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003628 cgroup_iter_start(cgrp, &it);
3629 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003630 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003631 break;
Ben Blum102a7752009-09-23 15:56:26 -07003632 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003633 if (type == CGROUP_FILE_PROCS)
3634 pid = task_tgid_vnr(tsk);
3635 else
3636 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003637 if (pid > 0) /* make sure to only use valid results */
3638 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003639 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003640 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003641 length = n;
3642 /* now sort & (if procs) strip out duplicates */
3643 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003645 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003646 l = cgroup_pidlist_find(cgrp, type);
3647 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003648 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003649 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003650 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003651 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003652 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003653 l->list = array;
3654 l->length = length;
3655 l->use_count++;
3656 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003657 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003658 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003659}
3660
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661/**
Li Zefana043e3b2008-02-23 15:24:09 -08003662 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003663 * @stats: cgroupstats to fill information into
3664 * @dentry: A dentry entry belonging to the cgroup for which stats have
3665 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003666 *
3667 * Build and fill cgroupstats so that taskstats can export it to user
3668 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669 */
3670int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3671{
3672 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003673 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003674 struct cgroup_iter it;
3675 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003676
Balbir Singh846c7bb2007-10-18 23:39:44 -07003677 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003678 * Validate dentry by checking the superblock operations,
3679 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003680 */
Li Zefan33d283b2008-11-19 15:36:48 -08003681 if (dentry->d_sb->s_op != &cgroup_ops ||
3682 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003683 goto err;
3684
3685 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003686 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003687
Paul Menagebd89aab2007-10-18 23:40:44 -07003688 cgroup_iter_start(cgrp, &it);
3689 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003690 switch (tsk->state) {
3691 case TASK_RUNNING:
3692 stats->nr_running++;
3693 break;
3694 case TASK_INTERRUPTIBLE:
3695 stats->nr_sleeping++;
3696 break;
3697 case TASK_UNINTERRUPTIBLE:
3698 stats->nr_uninterruptible++;
3699 break;
3700 case TASK_STOPPED:
3701 stats->nr_stopped++;
3702 break;
3703 default:
3704 if (delayacct_is_task_waiting_on_io(tsk))
3705 stats->nr_io_wait++;
3706 break;
3707 }
3708 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003709 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710
Balbir Singh846c7bb2007-10-18 23:39:44 -07003711err:
3712 return ret;
3713}
3714
Paul Menage8f3ff202009-09-23 15:56:25 -07003715
Paul Menagecc31edc2008-10-18 20:28:04 -07003716/*
Ben Blum102a7752009-09-23 15:56:26 -07003717 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003718 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003719 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003720 */
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003723{
3724 /*
3725 * Initially we receive a position value that corresponds to
3726 * one more than the last pid shown (or 0 on the first call or
3727 * after a seek to the start). Use a binary-search to find the
3728 * next pid to display, if any
3729 */
Ben Blum102a7752009-09-23 15:56:26 -07003730 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003731 int index = 0, pid = *pos;
3732 int *iter;
3733
Ben Blum102a7752009-09-23 15:56:26 -07003734 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003736 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003737
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 while (index < end) {
3739 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003740 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003741 index = mid;
3742 break;
Ben Blum102a7752009-09-23 15:56:26 -07003743 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003744 index = mid + 1;
3745 else
3746 end = mid;
3747 }
3748 }
3749 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003750 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003751 return NULL;
3752 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003753 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003754 *pos = *iter;
3755 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003756}
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003759{
Ben Blum102a7752009-09-23 15:56:26 -07003760 struct cgroup_pidlist *l = s->private;
3761 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003762}
3763
Ben Blum102a7752009-09-23 15:56:26 -07003764static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003765{
Ben Blum102a7752009-09-23 15:56:26 -07003766 struct cgroup_pidlist *l = s->private;
3767 pid_t *p = v;
3768 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003769 /*
3770 * Advance to the next pid in the array. If this goes off the
3771 * end, we're done
3772 */
3773 p++;
3774 if (p >= end) {
3775 return NULL;
3776 } else {
3777 *pos = *p;
3778 return p;
3779 }
3780}
3781
Ben Blum102a7752009-09-23 15:56:26 -07003782static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783{
3784 return seq_printf(s, "%d\n", *(int *)v);
3785}
3786
Ben Blum102a7752009-09-23 15:56:26 -07003787/*
3788 * seq_operations functions for iterating on pidlists through seq_file -
3789 * independent of whether it's tasks or procs
3790 */
3791static const struct seq_operations cgroup_pidlist_seq_operations = {
3792 .start = cgroup_pidlist_start,
3793 .stop = cgroup_pidlist_stop,
3794 .next = cgroup_pidlist_next,
3795 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003796};
3797
Ben Blum102a7752009-09-23 15:56:26 -07003798static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003799{
Ben Blum72a8cb32009-09-23 15:56:27 -07003800 /*
3801 * the case where we're the last user of this particular pidlist will
3802 * have us remove it from the cgroup's list, which entails taking the
3803 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3804 * pidlist_mutex, we have to take pidlist_mutex first.
3805 */
3806 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003807 down_write(&l->mutex);
3808 BUG_ON(!l->use_count);
3809 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003810 /* we're the last user if refcount is 0; remove and free */
3811 list_del(&l->links);
3812 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003813 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003814 put_pid_ns(l->key.ns);
3815 up_write(&l->mutex);
3816 kfree(l);
3817 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003820 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003821}
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003824{
Ben Blum102a7752009-09-23 15:56:26 -07003825 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003826 if (!(file->f_mode & FMODE_READ))
3827 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003828 /*
3829 * the seq_file will only be initialized if the file was opened for
3830 * reading; hence we check if it's not null only in that case.
3831 */
3832 l = ((struct seq_file *)file->private_data)->private;
3833 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003834 return seq_release(inode, file);
3835}
3836
Ben Blum102a7752009-09-23 15:56:26 -07003837static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 .read = seq_read,
3839 .llseek = seq_lseek,
3840 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003841 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003842};
3843
3844/*
Ben Blum102a7752009-09-23 15:56:26 -07003845 * The following functions handle opens on a file that displays a pidlist
3846 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3847 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003848 */
Ben Blum102a7752009-09-23 15:56:26 -07003849/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003850static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003851{
3852 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003853 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003854 int retval;
3855
3856 /* Nothing to do for write-only files */
3857 if (!(file->f_mode & FMODE_READ))
3858 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003859
Ben Blum102a7752009-09-23 15:56:26 -07003860 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003861 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003862 if (retval)
3863 return retval;
3864 /* configure file information */
3865 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003866
Ben Blum102a7752009-09-23 15:56:26 -07003867 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003868 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003869 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003871 }
Ben Blum102a7752009-09-23 15:56:26 -07003872 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003873 return 0;
3874}
Ben Blum102a7752009-09-23 15:56:26 -07003875static int cgroup_tasks_open(struct inode *unused, struct file *file)
3876{
Ben Blum72a8cb32009-09-23 15:56:27 -07003877 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003878}
3879static int cgroup_procs_open(struct inode *unused, struct file *file)
3880{
Ben Blum72a8cb32009-09-23 15:56:27 -07003881 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003882}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003883
Paul Menagebd89aab2007-10-18 23:40:44 -07003884static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003885 struct cftype *cft)
3886{
Paul Menagebd89aab2007-10-18 23:40:44 -07003887 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003888}
3889
Paul Menage6379c102008-07-25 01:47:01 -07003890static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3891 struct cftype *cft,
3892 u64 val)
3893{
3894 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3895 if (val)
3896 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3897 else
3898 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3899 return 0;
3900}
3901
Paul Menagebbcb81d2007-10-18 23:39:32 -07003902/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003903 * When dput() is called asynchronously, if umount has been done and
3904 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3905 * there's a small window that vfs will see the root dentry with non-zero
3906 * refcnt and trigger BUG().
3907 *
3908 * That's why we hold a reference before dput() and drop it right after.
3909 */
3910static void cgroup_dput(struct cgroup *cgrp)
3911{
3912 struct super_block *sb = cgrp->root->sb;
3913
3914 atomic_inc(&sb->s_active);
3915 dput(cgrp->dentry);
3916 deactivate_super(sb);
3917}
3918
3919/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003920 * Unregister event and free resources.
3921 *
3922 * Gets called from workqueue.
3923 */
3924static void cgroup_event_remove(struct work_struct *work)
3925{
3926 struct cgroup_event *event = container_of(work, struct cgroup_event,
3927 remove);
3928 struct cgroup *cgrp = event->cgrp;
3929
Li Zefan810cbee2013-02-18 18:56:14 +08003930 remove_wait_queue(event->wqh, &event->wait);
3931
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003932 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3933
Li Zefan810cbee2013-02-18 18:56:14 +08003934 /* Notify userspace the event is going away. */
3935 eventfd_signal(event->eventfd, 1);
3936
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003937 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003938 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003939 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003940}
3941
3942/*
3943 * Gets called on POLLHUP on eventfd when user closes it.
3944 *
3945 * Called with wqh->lock held and interrupts disabled.
3946 */
3947static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3948 int sync, void *key)
3949{
3950 struct cgroup_event *event = container_of(wait,
3951 struct cgroup_event, wait);
3952 struct cgroup *cgrp = event->cgrp;
3953 unsigned long flags = (unsigned long)key;
3954
3955 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003956 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003957 * If the event has been detached at cgroup removal, we
3958 * can simply return knowing the other side will cleanup
3959 * for us.
3960 *
3961 * We can't race against event freeing since the other
3962 * side will require wqh->lock via remove_wait_queue(),
3963 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003964 */
Li Zefan810cbee2013-02-18 18:56:14 +08003965 spin_lock(&cgrp->event_list_lock);
3966 if (!list_empty(&event->list)) {
3967 list_del_init(&event->list);
3968 /*
3969 * We are in atomic context, but cgroup_event_remove()
3970 * may sleep, so we have to call it in workqueue.
3971 */
3972 schedule_work(&event->remove);
3973 }
3974 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003975 }
3976
3977 return 0;
3978}
3979
3980static void cgroup_event_ptable_queue_proc(struct file *file,
3981 wait_queue_head_t *wqh, poll_table *pt)
3982{
3983 struct cgroup_event *event = container_of(pt,
3984 struct cgroup_event, pt);
3985
3986 event->wqh = wqh;
3987 add_wait_queue(wqh, &event->wait);
3988}
3989
3990/*
3991 * Parse input and register new cgroup event handler.
3992 *
3993 * Input must be in format '<event_fd> <control_fd> <args>'.
3994 * Interpretation of args is defined by control file implementation.
3995 */
3996static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3997 const char *buffer)
3998{
3999 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08004000 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004001 unsigned int efd, cfd;
4002 struct file *efile = NULL;
4003 struct file *cfile = NULL;
4004 char *endp;
4005 int ret;
4006
4007 efd = simple_strtoul(buffer, &endp, 10);
4008 if (*endp != ' ')
4009 return -EINVAL;
4010 buffer = endp + 1;
4011
4012 cfd = simple_strtoul(buffer, &endp, 10);
4013 if ((*endp != ' ') && (*endp != '\0'))
4014 return -EINVAL;
4015 buffer = endp + 1;
4016
4017 event = kzalloc(sizeof(*event), GFP_KERNEL);
4018 if (!event)
4019 return -ENOMEM;
4020 event->cgrp = cgrp;
4021 INIT_LIST_HEAD(&event->list);
4022 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4023 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4024 INIT_WORK(&event->remove, cgroup_event_remove);
4025
4026 efile = eventfd_fget(efd);
4027 if (IS_ERR(efile)) {
4028 ret = PTR_ERR(efile);
4029 goto fail;
4030 }
4031
4032 event->eventfd = eventfd_ctx_fileget(efile);
4033 if (IS_ERR(event->eventfd)) {
4034 ret = PTR_ERR(event->eventfd);
4035 goto fail;
4036 }
4037
4038 cfile = fget(cfd);
4039 if (!cfile) {
4040 ret = -EBADF;
4041 goto fail;
4042 }
4043
4044 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004045 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004046 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004047 if (ret < 0)
4048 goto fail;
4049
4050 event->cft = __file_cft(cfile);
4051 if (IS_ERR(event->cft)) {
4052 ret = PTR_ERR(event->cft);
4053 goto fail;
4054 }
4055
Li Zefanf1690072013-02-18 14:13:35 +08004056 /*
4057 * The file to be monitored must be in the same cgroup as
4058 * cgroup.event_control is.
4059 */
4060 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4061 if (cgrp_cfile != cgrp) {
4062 ret = -EINVAL;
4063 goto fail;
4064 }
4065
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004066 if (!event->cft->register_event || !event->cft->unregister_event) {
4067 ret = -EINVAL;
4068 goto fail;
4069 }
4070
4071 ret = event->cft->register_event(cgrp, event->cft,
4072 event->eventfd, buffer);
4073 if (ret)
4074 goto fail;
4075
Li Zefan7ef70e42013-04-26 11:58:03 -07004076 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004077
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004078 /*
4079 * Events should be removed after rmdir of cgroup directory, but before
4080 * destroying subsystem state objects. Let's take reference to cgroup
4081 * directory dentry to do that.
4082 */
4083 dget(cgrp->dentry);
4084
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085 spin_lock(&cgrp->event_list_lock);
4086 list_add(&event->list, &cgrp->event_list);
4087 spin_unlock(&cgrp->event_list_lock);
4088
4089 fput(cfile);
4090 fput(efile);
4091
4092 return 0;
4093
4094fail:
4095 if (cfile)
4096 fput(cfile);
4097
4098 if (event && event->eventfd && !IS_ERR(event->eventfd))
4099 eventfd_ctx_put(event->eventfd);
4100
4101 if (!IS_ERR_OR_NULL(efile))
4102 fput(efile);
4103
4104 kfree(event);
4105
4106 return ret;
4107}
4108
Daniel Lezcano97978e62010-10-27 15:33:35 -07004109static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4110 struct cftype *cft)
4111{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004112 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004113}
4114
4115static int cgroup_clone_children_write(struct cgroup *cgrp,
4116 struct cftype *cft,
4117 u64 val)
4118{
4119 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004120 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004121 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004122 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004123 return 0;
4124}
4125
Tejun Heod5c56ce2013-06-03 19:14:34 -07004126static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004127 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004128 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004129 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004130 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004131 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004132 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004133 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004134 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004135 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004136 .write_string = cgroup_write_event_control,
4137 .mode = S_IWUGO,
4138 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004139 {
4140 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004141 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004142 .read_u64 = cgroup_clone_children_read,
4143 .write_u64 = cgroup_clone_children_write,
4144 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004145 {
Tejun Heo873fe092013-04-14 20:15:26 -07004146 .name = "cgroup.sane_behavior",
4147 .flags = CFTYPE_ONLY_ON_ROOT,
4148 .read_seq_string = cgroup_sane_behavior_show,
4149 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004150
4151 /*
4152 * Historical crazy stuff. These don't have "cgroup." prefix and
4153 * don't exist if sane_behavior. If you're depending on these, be
4154 * prepared to be burned.
4155 */
4156 {
4157 .name = "tasks",
4158 .flags = CFTYPE_INSANE, /* use "procs" instead */
4159 .open = cgroup_tasks_open,
4160 .write_u64 = cgroup_tasks_write,
4161 .release = cgroup_pidlist_release,
4162 .mode = S_IRUGO | S_IWUSR,
4163 },
4164 {
4165 .name = "notify_on_release",
4166 .flags = CFTYPE_INSANE,
4167 .read_u64 = cgroup_read_notify_on_release,
4168 .write_u64 = cgroup_write_notify_on_release,
4169 },
Tejun Heo873fe092013-04-14 20:15:26 -07004170 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004171 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004172 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004173 .read_seq_string = cgroup_release_agent_show,
4174 .write_string = cgroup_release_agent_write,
4175 .max_write_len = PATH_MAX,
4176 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004177 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004178};
4179
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004180/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004181 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004182 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004183 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004184 *
4185 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004186 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004187static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004188{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004189 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004190 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004191
Tejun Heo8e3f6542012-04-01 12:09:55 -07004192 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004193 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004194 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004195
4196 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004197 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004198
Tejun Heobee55092013-06-28 16:24:11 -07004199 list_for_each_entry(set, &ss->cftsets, node) {
4200 ret = cgroup_addrm_files(cgrp, ss, set->cfts, true);
4201 if (ret < 0)
4202 goto err;
4203 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004204 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004205
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004206 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004207 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004208 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004209 struct css_id *id = rcu_dereference_protected(css->id, true);
4210
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004211 /*
4212 * Update id->css pointer and make this css visible from
4213 * CSS ID functions. This pointer will be dereferened
4214 * from RCU-read-side without locks.
4215 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004216 if (id)
4217 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004218 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004219
4220 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004221err:
4222 cgroup_clear_dir(cgrp, subsys_mask);
4223 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224}
4225
Tejun Heo48ddbe12012-04-01 12:09:56 -07004226static void css_dput_fn(struct work_struct *work)
4227{
4228 struct cgroup_subsys_state *css =
4229 container_of(work, struct cgroup_subsys_state, dput_work);
4230
Li Zefan1c8158e2013-06-18 18:41:10 +08004231 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004232}
4233
Tejun Heod3daf282013-06-13 19:39:16 -07004234static void css_release(struct percpu_ref *ref)
4235{
4236 struct cgroup_subsys_state *css =
4237 container_of(ref, struct cgroup_subsys_state, refcnt);
4238
4239 schedule_work(&css->dput_work);
4240}
4241
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242static void init_cgroup_css(struct cgroup_subsys_state *css,
4243 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004244 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245{
Paul Menagebd89aab2007-10-18 23:40:44 -07004246 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004248 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004249 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004250 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004251 BUG_ON(cgrp->subsys[ss->subsys_id]);
4252 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004253
4254 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004255 * css holds an extra ref to @cgrp->dentry which is put on the last
4256 * css_put(). dput() requires process context, which css_put() may
4257 * be called without. @css->dput_work will be used to invoke
4258 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004259 */
4260 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004261}
4262
Tejun Heob1929db2012-11-19 08:13:38 -08004263/* invoke ->post_create() on a new CSS and mark it online if successful */
4264static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004265{
Tejun Heob1929db2012-11-19 08:13:38 -08004266 int ret = 0;
4267
Tejun Heoa31f2d32012-11-19 08:13:37 -08004268 lockdep_assert_held(&cgroup_mutex);
4269
Tejun Heo92fb9742012-11-19 08:13:38 -08004270 if (ss->css_online)
4271 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004272 if (!ret)
4273 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4274 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004275}
4276
4277/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4278static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4279 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4280{
4281 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4282
4283 lockdep_assert_held(&cgroup_mutex);
4284
4285 if (!(css->flags & CSS_ONLINE))
4286 return;
4287
Li Zefand7eeac12013-03-12 15:35:59 -07004288 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004289 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004290
4291 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4292}
4293
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294/*
Li Zefana043e3b2008-02-23 15:24:09 -08004295 * cgroup_create - create a cgroup
4296 * @parent: cgroup that will be parent of the new cgroup
4297 * @dentry: dentry of the new cgroup
4298 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299 *
Li Zefana043e3b2008-02-23 15:24:09 -08004300 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004301 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004302static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004303 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004304{
Paul Menagebd89aab2007-10-18 23:40:44 -07004305 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004306 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004307 struct cgroupfs_root *root = parent->root;
4308 int err = 0;
4309 struct cgroup_subsys *ss;
4310 struct super_block *sb = root->sb;
4311
Tejun Heo0a950f62012-11-19 09:02:12 -08004312 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004313 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4314 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004315 return -ENOMEM;
4316
Li Zefan65dff752013-03-01 15:01:56 +08004317 name = cgroup_alloc_name(dentry);
4318 if (!name)
4319 goto err_free_cgrp;
4320 rcu_assign_pointer(cgrp->name, name);
4321
Tejun Heo0a950f62012-11-19 09:02:12 -08004322 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4323 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004324 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004325
Tejun Heo976c06b2012-11-05 09:16:59 -08004326 /*
4327 * Only live parents can have children. Note that the liveliness
4328 * check isn't strictly necessary because cgroup_mkdir() and
4329 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4330 * anyway so that locking is contained inside cgroup proper and we
4331 * don't get nasty surprises if we ever grow another caller.
4332 */
4333 if (!cgroup_lock_live_group(parent)) {
4334 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004335 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004336 }
4337
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338 /* Grab a reference on the superblock so the hierarchy doesn't
4339 * get deleted on unmount if there are child cgroups. This
4340 * can be done outside cgroup_mutex, since the sb can't
4341 * disappear while someone has an open control file on the
4342 * fs */
4343 atomic_inc(&sb->s_active);
4344
Paul Menagecc31edc2008-10-18 20:28:04 -07004345 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004346
Li Zefanfe1c06c2013-01-24 14:30:22 +08004347 dentry->d_fsdata = cgrp;
4348 cgrp->dentry = dentry;
4349
Paul Menagebd89aab2007-10-18 23:40:44 -07004350 cgrp->parent = parent;
4351 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352
Li Zefanb6abdb02008-03-04 14:28:19 -08004353 if (notify_on_release(parent))
4354 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4355
Tejun Heo2260e7f2012-11-19 08:13:38 -08004356 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4357 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004358
Tejun Heo5549c492013-06-24 15:21:48 -07004359 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004360 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004361
Tejun Heo92fb9742012-11-19 08:13:38 -08004362 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004363 if (IS_ERR(css)) {
4364 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004365 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 }
Tejun Heod3daf282013-06-13 19:39:16 -07004367
4368 err = percpu_ref_init(&css->refcnt, css_release);
4369 if (err)
4370 goto err_free_all;
4371
Paul Menagebd89aab2007-10-18 23:40:44 -07004372 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004373
Li Zefan4528fd02010-02-02 13:44:10 -08004374 if (ss->use_id) {
4375 err = alloc_css_id(ss, parent, cgrp);
4376 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004377 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004378 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 }
4380
Tejun Heo4e139af2012-11-19 08:13:36 -08004381 /*
4382 * Create directory. cgroup_create_file() returns with the new
4383 * directory locked on success so that it can be populated without
4384 * dropping cgroup_mutex.
4385 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004386 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004388 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004389 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390
Tejun Heo00356bd2013-06-18 11:14:22 -07004391 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004392
Tejun Heo4e139af2012-11-19 08:13:36 -08004393 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004394 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4395 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004396
Tejun Heob1929db2012-11-19 08:13:38 -08004397 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004398 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004399 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004400
Li Zefan415cf072013-04-08 14:35:02 +08004401 /* hold a ref to the parent's dentry */
4402 dget(parent->dentry);
4403
Tejun Heob1929db2012-11-19 08:13:38 -08004404 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004405 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004406 err = online_css(ss, cgrp);
4407 if (err)
4408 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004409
4410 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4411 parent->parent) {
4412 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",
4413 current->comm, current->pid, ss->name);
4414 if (!strcmp(ss->name, "memory"))
4415 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4416 ss->warned_broken_hierarchy = true;
4417 }
Tejun Heoa8638032012-11-09 09:12:29 -08004418 }
4419
Tejun Heo628f7cd2013-06-28 16:24:11 -07004420 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
4421 if (err)
4422 goto err_destroy;
4423
4424 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004425 if (err)
4426 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004427
4428 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004429 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004430
4431 return 0;
4432
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004433err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004434 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004435 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4436
4437 if (css) {
4438 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004439 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004440 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004441 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004442 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004443 /* Release the reference count that we took on the superblock */
4444 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004445err_free_id:
4446 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004447err_free_name:
4448 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004449err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004450 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004451 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004452
4453err_destroy:
4454 cgroup_destroy_locked(cgrp);
4455 mutex_unlock(&cgroup_mutex);
4456 mutex_unlock(&dentry->d_inode->i_mutex);
4457 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004458}
4459
Al Viro18bb1db2011-07-26 01:41:39 -04004460static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004461{
4462 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4463
4464 /* the vfs holds inode->i_mutex already */
4465 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4466}
4467
Tejun Heod3daf282013-06-13 19:39:16 -07004468static void cgroup_css_killed(struct cgroup *cgrp)
4469{
4470 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4471 return;
4472
4473 /* percpu ref's of all css's are killed, kick off the next step */
4474 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4475 schedule_work(&cgrp->destroy_work);
4476}
4477
4478static void css_ref_killed_fn(struct percpu_ref *ref)
4479{
4480 struct cgroup_subsys_state *css =
4481 container_of(ref, struct cgroup_subsys_state, refcnt);
4482
4483 cgroup_css_killed(css->cgroup);
4484}
4485
4486/**
4487 * cgroup_destroy_locked - the first stage of cgroup destruction
4488 * @cgrp: cgroup to be destroyed
4489 *
4490 * css's make use of percpu refcnts whose killing latency shouldn't be
4491 * exposed to userland and are RCU protected. Also, cgroup core needs to
4492 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4493 * invoked. To satisfy all the requirements, destruction is implemented in
4494 * the following two steps.
4495 *
4496 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4497 * userland visible parts and start killing the percpu refcnts of
4498 * css's. Set up so that the next stage will be kicked off once all
4499 * the percpu refcnts are confirmed to be killed.
4500 *
4501 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4502 * rest of destruction. Once all cgroup references are gone, the
4503 * cgroup is RCU-freed.
4504 *
4505 * This function implements s1. After this step, @cgrp is gone as far as
4506 * the userland is concerned and a new cgroup with the same name may be
4507 * created. As cgroup doesn't care about the names internally, this
4508 * doesn't cause any problem.
4509 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004510static int cgroup_destroy_locked(struct cgroup *cgrp)
4511 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004512{
Tejun Heo42809dd2012-11-19 08:13:37 -08004513 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004514 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004515 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004516 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004517
Tejun Heo42809dd2012-11-19 08:13:37 -08004518 lockdep_assert_held(&d->d_inode->i_mutex);
4519 lockdep_assert_held(&cgroup_mutex);
4520
Tejun Heoddd69142013-06-12 21:04:54 -07004521 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004522 * css_set_lock synchronizes access to ->cset_links and prevents
4523 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004524 */
4525 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004526 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004527 read_unlock(&css_set_lock);
4528 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004529 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004530
Tejun Heo1a90dd52012-11-05 09:16:59 -08004531 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004532 * Block new css_tryget() by killing css refcnts. cgroup core
4533 * guarantees that, by the time ->css_offline() is invoked, no new
4534 * css reference will be given out via css_tryget(). We can't
4535 * simply call percpu_ref_kill() and proceed to offlining css's
4536 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4537 * as killed on all CPUs on return.
4538 *
4539 * Use percpu_ref_kill_and_confirm() to get notifications as each
4540 * css is confirmed to be seen as killed on all CPUs. The
4541 * notification callback keeps track of the number of css's to be
4542 * killed and schedules cgroup_offline_fn() to perform the rest of
4543 * destruction once the percpu refs of all css's are confirmed to
4544 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004545 */
Tejun Heod3daf282013-06-13 19:39:16 -07004546 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004547 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004548 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4549
Tejun Heod3daf282013-06-13 19:39:16 -07004550 /*
4551 * Killing would put the base ref, but we need to keep it
4552 * alive until after ->css_offline.
4553 */
4554 percpu_ref_get(&css->refcnt);
4555
4556 atomic_inc(&cgrp->css_kill_cnt);
4557 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004558 }
Tejun Heod3daf282013-06-13 19:39:16 -07004559 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004560
4561 /*
4562 * Mark @cgrp dead. This prevents further task migration and child
4563 * creation by disabling cgroup_lock_live_group(). Note that
4564 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4565 * resume iteration after dropping RCU read lock. See
4566 * cgroup_next_sibling() for details.
4567 */
Tejun Heo54766d42013-06-12 21:04:53 -07004568 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004569
Tejun Heo455050d2013-06-13 19:27:41 -07004570 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4571 raw_spin_lock(&release_list_lock);
4572 if (!list_empty(&cgrp->release_list))
4573 list_del_init(&cgrp->release_list);
4574 raw_spin_unlock(&release_list_lock);
4575
4576 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004577 * Clear and remove @cgrp directory. The removal puts the base ref
4578 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004579 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004580 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
4581 cgroup_addrm_files(cgrp, NULL, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004582 dget(d);
4583 cgroup_d_remove_dir(d);
4584
4585 /*
4586 * Unregister events and notify userspace.
4587 * Notify userspace about cgroup removing only after rmdir of cgroup
4588 * directory to avoid race between userspace and kernelspace.
4589 */
4590 spin_lock(&cgrp->event_list_lock);
4591 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4592 list_del_init(&event->list);
4593 schedule_work(&event->remove);
4594 }
4595 spin_unlock(&cgrp->event_list_lock);
4596
Tejun Heoea15f8c2013-06-13 19:27:42 -07004597 return 0;
4598};
4599
Tejun Heod3daf282013-06-13 19:39:16 -07004600/**
4601 * cgroup_offline_fn - the second step of cgroup destruction
4602 * @work: cgroup->destroy_free_work
4603 *
4604 * This function is invoked from a work item for a cgroup which is being
4605 * destroyed after the percpu refcnts of all css's are guaranteed to be
4606 * seen as killed on all CPUs, and performs the rest of destruction. This
4607 * is the second step of destruction described in the comment above
4608 * cgroup_destroy_locked().
4609 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004610static void cgroup_offline_fn(struct work_struct *work)
4611{
4612 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4613 struct cgroup *parent = cgrp->parent;
4614 struct dentry *d = cgrp->dentry;
4615 struct cgroup_subsys *ss;
4616
4617 mutex_lock(&cgroup_mutex);
4618
Tejun Heod3daf282013-06-13 19:39:16 -07004619 /*
4620 * css_tryget() is guaranteed to fail now. Tell subsystems to
4621 * initate destruction.
4622 */
Tejun Heo5549c492013-06-24 15:21:48 -07004623 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004624 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004625
4626 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004627 * Put the css refs from cgroup_destroy_locked(). Each css holds
4628 * an extra reference to the cgroup's dentry and cgroup removal
4629 * proceeds regardless of css refs. On the last put of each css,
4630 * whenever that may be, the extra dentry ref is put so that dentry
4631 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004632 */
Tejun Heo5549c492013-06-24 15:21:48 -07004633 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004634 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
Paul Menage999cd8a2009-01-07 18:08:36 -08004636 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004637 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004638
Paul Menageddbcc7e2007-10-18 23:39:30 -07004639 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640
Paul Menagebd89aab2007-10-18 23:40:44 -07004641 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004642 check_for_release(parent);
4643
Tejun Heoea15f8c2013-06-13 19:27:42 -07004644 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004645}
4646
Tejun Heo42809dd2012-11-19 08:13:37 -08004647static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4648{
4649 int ret;
4650
4651 mutex_lock(&cgroup_mutex);
4652 ret = cgroup_destroy_locked(dentry->d_fsdata);
4653 mutex_unlock(&cgroup_mutex);
4654
4655 return ret;
4656}
4657
Tejun Heo8e3f6542012-04-01 12:09:55 -07004658static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4659{
4660 INIT_LIST_HEAD(&ss->cftsets);
4661
4662 /*
4663 * base_cftset is embedded in subsys itself, no need to worry about
4664 * deregistration.
4665 */
4666 if (ss->base_cftypes) {
4667 ss->base_cftset.cfts = ss->base_cftypes;
4668 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4669 }
4670}
4671
Li Zefan06a11922008-04-29 01:00:07 -07004672static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004673{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004674 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004675
4676 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677
Tejun Heo648bb562012-11-19 08:13:36 -08004678 mutex_lock(&cgroup_mutex);
4679
Tejun Heo8e3f6542012-04-01 12:09:55 -07004680 /* init base cftset */
4681 cgroup_init_cftsets(ss);
4682
Paul Menageddbcc7e2007-10-18 23:39:30 -07004683 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004684 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4685 ss->root = &cgroup_dummy_root;
4686 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687 /* We don't handle early failures gracefully */
4688 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004689 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004690
Li Zefane8d55fd2008-04-29 01:00:13 -07004691 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004692 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004693 * newly registered, all tasks and hence the
4694 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004695 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004696
4697 need_forkexit_callback |= ss->fork || ss->exit;
4698
Li Zefane8d55fd2008-04-29 01:00:13 -07004699 /* At system boot, before all subsystems have been
4700 * registered, no tasks have been forked, so we don't
4701 * need to invoke fork callbacks here. */
4702 BUG_ON(!list_empty(&init_task.tasks));
4703
Tejun Heo9871bf92013-06-24 15:21:47 -07004704 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004705
Tejun Heo648bb562012-11-19 08:13:36 -08004706 mutex_unlock(&cgroup_mutex);
4707
Ben Blume6a11052010-03-10 15:22:09 -08004708 /* this function shouldn't be used with modular subsystems, since they
4709 * need to register a subsys_id, among other things */
4710 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004711}
4712
4713/**
Ben Blume6a11052010-03-10 15:22:09 -08004714 * cgroup_load_subsys: load and register a modular subsystem at runtime
4715 * @ss: the subsystem to load
4716 *
4717 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004718 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004719 * up for use. If the subsystem is built-in anyway, work is delegated to the
4720 * simpler cgroup_init_subsys.
4721 */
4722int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4723{
Ben Blume6a11052010-03-10 15:22:09 -08004724 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004725 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004726 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004727 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004728 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004729
4730 /* check name and function validity */
4731 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004732 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004733 return -EINVAL;
4734
4735 /*
4736 * we don't support callbacks in modular subsystems. this check is
4737 * before the ss->module check for consistency; a subsystem that could
4738 * be a module should still have no callbacks even if the user isn't
4739 * compiling it as one.
4740 */
4741 if (ss->fork || ss->exit)
4742 return -EINVAL;
4743
4744 /*
4745 * an optionally modular subsystem is built-in: we want to do nothing,
4746 * since cgroup_init_subsys will have already taken care of it.
4747 */
4748 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004749 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004750 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004751 return 0;
4752 }
4753
Tejun Heo8e3f6542012-04-01 12:09:55 -07004754 /* init base cftset */
4755 cgroup_init_cftsets(ss);
4756
Ben Blume6a11052010-03-10 15:22:09 -08004757 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004758 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004759
4760 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004761 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004762 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004763 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004764 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004765 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004766 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004767 /* failure case - need to deassign the cgroup_subsys[] slot. */
4768 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004769 mutex_unlock(&cgroup_mutex);
4770 return PTR_ERR(css);
4771 }
4772
Tejun Heo9871bf92013-06-24 15:21:47 -07004773 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4774 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004775
4776 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004777 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004778 /* init_idr must be after init_cgroup_css because it sets css->id. */
4779 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004780 ret = cgroup_init_idr(ss, css);
4781 if (ret)
4782 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004783 }
4784
4785 /*
4786 * Now we need to entangle the css into the existing css_sets. unlike
4787 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4788 * will need a new pointer to it; done by iterating the css_set_table.
4789 * furthermore, modifying the existing css_sets will corrupt the hash
4790 * table state, so each changed css_set will need its hash recomputed.
4791 * this is all done under the css_set_lock.
4792 */
4793 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004794 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004795 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004796 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004797 continue;
4798 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004799 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004800 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004801 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004802 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004803 key = css_set_hash(cset->subsys);
4804 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004805 }
4806 write_unlock(&css_set_lock);
4807
Tejun Heo9871bf92013-06-24 15:21:47 -07004808 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004809 if (ret)
4810 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004811
Ben Blume6a11052010-03-10 15:22:09 -08004812 /* success! */
4813 mutex_unlock(&cgroup_mutex);
4814 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004815
4816err_unload:
4817 mutex_unlock(&cgroup_mutex);
4818 /* @ss can't be mounted here as try_module_get() would fail */
4819 cgroup_unload_subsys(ss);
4820 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004821}
4822EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4823
4824/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004825 * cgroup_unload_subsys: unload a modular subsystem
4826 * @ss: the subsystem to unload
4827 *
4828 * This function should be called in a modular subsystem's exitcall. When this
4829 * function is invoked, the refcount on the subsystem's module will be 0, so
4830 * the subsystem will not be attached to any hierarchy.
4831 */
4832void cgroup_unload_subsys(struct cgroup_subsys *ss)
4833{
Tejun Heo69d02062013-06-12 21:04:50 -07004834 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004835
4836 BUG_ON(ss->module == NULL);
4837
4838 /*
4839 * we shouldn't be called if the subsystem is in use, and the use of
4840 * try_module_get in parse_cgroupfs_options should ensure that it
4841 * doesn't start being used while we're killing it off.
4842 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004844
4845 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004846
Tejun Heo9871bf92013-06-24 15:21:47 -07004847 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004848
Tejun Heoc897ff62013-02-27 17:03:49 -08004849 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004850 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004851
Ben Blumcf5d5942010-03-10 15:22:09 -08004852 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004853 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004854
Tejun Heo9871bf92013-06-24 15:21:47 -07004855 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004856 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004857
4858 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004859 * disentangle the css from all css_sets attached to the dummy
4860 * top. as in loading, we need to pay our respects to the hashtable
4861 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004862 */
4863 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004864 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004865 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004866 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004867
Tejun Heo5abb8852013-06-12 21:04:49 -07004868 hash_del(&cset->hlist);
4869 cset->subsys[ss->subsys_id] = NULL;
4870 key = css_set_hash(cset->subsys);
4871 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004872 }
4873 write_unlock(&css_set_lock);
4874
4875 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004876 * remove subsystem's css from the cgroup_dummy_top and free it -
4877 * need to free before marking as null because ss->css_free needs
4878 * the cgrp->subsys pointer to find their state. note that this
4879 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004880 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004881 ss->css_free(cgroup_dummy_top);
4882 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004883
4884 mutex_unlock(&cgroup_mutex);
4885}
4886EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4887
4888/**
Li Zefana043e3b2008-02-23 15:24:09 -08004889 * cgroup_init_early - cgroup initialization at system boot
4890 *
4891 * Initialize cgroups at system boot, and initialize any
4892 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004893 */
4894int __init cgroup_init_early(void)
4895{
Tejun Heo30159ec2013-06-25 11:53:37 -07004896 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004897 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004898
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004899 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004900 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004901 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004902 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004903 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004904 init_cgroup_root(&cgroup_dummy_root);
4905 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004906 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004907
Tejun Heo69d02062013-06-12 21:04:50 -07004908 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004909 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4910 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004911 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004912
Tejun Heo30159ec2013-06-25 11:53:37 -07004913 /* at bootup time, we don't worry about modular subsystems */
4914 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004915 BUG_ON(!ss->name);
4916 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004917 BUG_ON(!ss->css_alloc);
4918 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004919 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004920 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921 ss->name, ss->subsys_id);
4922 BUG();
4923 }
4924
4925 if (ss->early_init)
4926 cgroup_init_subsys(ss);
4927 }
4928 return 0;
4929}
4930
4931/**
Li Zefana043e3b2008-02-23 15:24:09 -08004932 * cgroup_init - cgroup initialization
4933 *
4934 * Register cgroup filesystem and /proc file, and initialize
4935 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004936 */
4937int __init cgroup_init(void)
4938{
Tejun Heo30159ec2013-06-25 11:53:37 -07004939 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004940 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004941 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004942
4943 err = bdi_init(&cgroup_backing_dev_info);
4944 if (err)
4945 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004946
Tejun Heo30159ec2013-06-25 11:53:37 -07004947 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004948 if (!ss->early_init)
4949 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004950 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004951 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004952 }
4953
Tejun Heofa3ca072013-04-14 11:36:56 -07004954 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004955 mutex_lock(&cgroup_mutex);
4956 mutex_lock(&cgroup_root_mutex);
4957
Tejun Heo82fe9b02013-06-25 11:53:37 -07004958 /* Add init_css_set to the hash table */
4959 key = css_set_hash(init_css_set.subsys);
4960 hash_add(css_set_table, &init_css_set.hlist, key);
4961
Tejun Heofc76df72013-06-25 11:53:37 -07004962 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004963
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004964 mutex_unlock(&cgroup_root_mutex);
4965 mutex_unlock(&cgroup_mutex);
4966
Greg KH676db4a2010-08-05 13:53:35 -07004967 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4968 if (!cgroup_kobj) {
4969 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004970 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004971 }
4972
4973 err = register_filesystem(&cgroup_fs_type);
4974 if (err < 0) {
4975 kobject_put(cgroup_kobj);
4976 goto out;
4977 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004978
Li Zefan46ae2202008-04-29 01:00:08 -07004979 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004980
Paul Menageddbcc7e2007-10-18 23:39:30 -07004981out:
Paul Menagea4243162007-10-18 23:39:35 -07004982 if (err)
4983 bdi_destroy(&cgroup_backing_dev_info);
4984
Paul Menageddbcc7e2007-10-18 23:39:30 -07004985 return err;
4986}
Paul Menageb4f48b62007-10-18 23:39:33 -07004987
Paul Menagea4243162007-10-18 23:39:35 -07004988/*
4989 * proc_cgroup_show()
4990 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4991 * - Used for /proc/<pid>/cgroup.
4992 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4993 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004994 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004995 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4996 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4997 * cgroup to top_cgroup.
4998 */
4999
5000/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005001int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005002{
5003 struct pid *pid;
5004 struct task_struct *tsk;
5005 char *buf;
5006 int retval;
5007 struct cgroupfs_root *root;
5008
5009 retval = -ENOMEM;
5010 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5011 if (!buf)
5012 goto out;
5013
5014 retval = -ESRCH;
5015 pid = m->private;
5016 tsk = get_pid_task(pid, PIDTYPE_PID);
5017 if (!tsk)
5018 goto out_free;
5019
5020 retval = 0;
5021
5022 mutex_lock(&cgroup_mutex);
5023
Li Zefane5f6a862009-01-07 18:07:41 -08005024 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005025 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005026 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005027 int count = 0;
5028
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005029 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005030 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005031 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005032 if (strlen(root->name))
5033 seq_printf(m, "%sname=%s", count ? "," : "",
5034 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005035 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005036 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005037 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005038 if (retval < 0)
5039 goto out_unlock;
5040 seq_puts(m, buf);
5041 seq_putc(m, '\n');
5042 }
5043
5044out_unlock:
5045 mutex_unlock(&cgroup_mutex);
5046 put_task_struct(tsk);
5047out_free:
5048 kfree(buf);
5049out:
5050 return retval;
5051}
5052
Paul Menagea4243162007-10-18 23:39:35 -07005053/* Display information about each subsystem and each hierarchy */
5054static int proc_cgroupstats_show(struct seq_file *m, void *v)
5055{
Tejun Heo30159ec2013-06-25 11:53:37 -07005056 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005057 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005058
Paul Menage8bab8dd2008-04-04 14:29:57 -07005059 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005060 /*
5061 * ideally we don't want subsystems moving around while we do this.
5062 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5063 * subsys/hierarchy state.
5064 */
Paul Menagea4243162007-10-18 23:39:35 -07005065 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005066
5067 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005068 seq_printf(m, "%s\t%d\t%d\t%d\n",
5069 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005070 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005071
Paul Menagea4243162007-10-18 23:39:35 -07005072 mutex_unlock(&cgroup_mutex);
5073 return 0;
5074}
5075
5076static int cgroupstats_open(struct inode *inode, struct file *file)
5077{
Al Viro9dce07f2008-03-29 03:07:28 +00005078 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005079}
5080
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005081static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005082 .open = cgroupstats_open,
5083 .read = seq_read,
5084 .llseek = seq_lseek,
5085 .release = single_release,
5086};
5087
Paul Menageb4f48b62007-10-18 23:39:33 -07005088/**
5089 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005090 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005091 *
5092 * Description: A task inherits its parent's cgroup at fork().
5093 *
5094 * A pointer to the shared css_set was automatically copied in
5095 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005096 * it was not made under the protection of RCU or cgroup_mutex, so
5097 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5098 * have already changed current->cgroups, allowing the previously
5099 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005100 *
5101 * At the point that cgroup_fork() is called, 'current' is the parent
5102 * task, and the passed argument 'child' points to the child task.
5103 */
5104void cgroup_fork(struct task_struct *child)
5105{
Tejun Heo9bb71302012-10-18 17:52:07 -07005106 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005107 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005108 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005109 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005110 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005111}
5112
5113/**
Li Zefana043e3b2008-02-23 15:24:09 -08005114 * cgroup_post_fork - called on a new task after adding it to the task list
5115 * @child: the task in question
5116 *
Tejun Heo5edee612012-10-16 15:03:14 -07005117 * Adds the task to the list running through its css_set if necessary and
5118 * call the subsystem fork() callbacks. Has to be after the task is
5119 * visible on the task list in case we race with the first call to
5120 * cgroup_iter_start() - to guarantee that the new task ends up on its
5121 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005122 */
Paul Menage817929e2007-10-18 23:39:36 -07005123void cgroup_post_fork(struct task_struct *child)
5124{
Tejun Heo30159ec2013-06-25 11:53:37 -07005125 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005126 int i;
5127
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005128 /*
5129 * use_task_css_set_links is set to 1 before we walk the tasklist
5130 * under the tasklist_lock and we read it here after we added the child
5131 * to the tasklist under the tasklist_lock as well. If the child wasn't
5132 * yet in the tasklist when we walked through it from
5133 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5134 * should be visible now due to the paired locking and barriers implied
5135 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5136 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5137 * lock on fork.
5138 */
Paul Menage817929e2007-10-18 23:39:36 -07005139 if (use_task_css_set_links) {
5140 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005141 task_lock(child);
5142 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005143 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005144 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005145 write_unlock(&css_set_lock);
5146 }
Tejun Heo5edee612012-10-16 15:03:14 -07005147
5148 /*
5149 * Call ss->fork(). This must happen after @child is linked on
5150 * css_set; otherwise, @child might change state between ->fork()
5151 * and addition to css_set.
5152 */
5153 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005154 /*
5155 * fork/exit callbacks are supported only for builtin
5156 * subsystems, and the builtin section of the subsys
5157 * array is immutable, so we don't need to lock the
5158 * subsys array here. On the other hand, modular section
5159 * of the array can be freed at module unload, so we
5160 * can't touch that.
5161 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005162 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005163 if (ss->fork)
5164 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005165 }
Paul Menage817929e2007-10-18 23:39:36 -07005166}
Tejun Heo5edee612012-10-16 15:03:14 -07005167
Paul Menage817929e2007-10-18 23:39:36 -07005168/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005169 * cgroup_exit - detach cgroup from exiting task
5170 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005171 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005172 *
5173 * Description: Detach cgroup from @tsk and release it.
5174 *
5175 * Note that cgroups marked notify_on_release force every task in
5176 * them to take the global cgroup_mutex mutex when exiting.
5177 * This could impact scaling on very large systems. Be reluctant to
5178 * use notify_on_release cgroups where very high task exit scaling
5179 * is required on large systems.
5180 *
5181 * the_top_cgroup_hack:
5182 *
5183 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5184 *
5185 * We call cgroup_exit() while the task is still competent to
5186 * handle notify_on_release(), then leave the task attached to the
5187 * root cgroup in each hierarchy for the remainder of its exit.
5188 *
5189 * To do this properly, we would increment the reference count on
5190 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5191 * code we would add a second cgroup function call, to drop that
5192 * reference. This would just create an unnecessary hot spot on
5193 * the top_cgroup reference count, to no avail.
5194 *
5195 * Normally, holding a reference to a cgroup without bumping its
5196 * count is unsafe. The cgroup could go away, or someone could
5197 * attach us to a different cgroup, decrementing the count on
5198 * the first cgroup that we never incremented. But in this case,
5199 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005200 * which wards off any cgroup_attach_task() attempts, or task is a failed
5201 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005202 */
5203void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5204{
Tejun Heo30159ec2013-06-25 11:53:37 -07005205 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005206 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005207 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005208
5209 /*
5210 * Unlink from the css_set task list if necessary.
5211 * Optimistically check cg_list before taking
5212 * css_set_lock
5213 */
5214 if (!list_empty(&tsk->cg_list)) {
5215 write_lock(&css_set_lock);
5216 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005217 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005218 write_unlock(&css_set_lock);
5219 }
5220
Paul Menageb4f48b62007-10-18 23:39:33 -07005221 /* Reassign the task to the init_css_set. */
5222 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005223 cset = task_css_set(tsk);
5224 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005225
5226 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005227 /*
5228 * fork/exit callbacks are supported only for builtin
5229 * subsystems, see cgroup_post_fork() for details.
5230 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005231 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005232 if (ss->exit) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07005233 struct cgroup *old_cgrp = cset->subsys[i]->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005234 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005235
Li Zefan761b3ef2012-01-31 13:47:36 +08005236 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005237 }
5238 }
5239 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005240 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005241
Tejun Heo5abb8852013-06-12 21:04:49 -07005242 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005243}
Paul Menage697f4162007-10-18 23:39:34 -07005244
Paul Menagebd89aab2007-10-18 23:40:44 -07005245static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005246{
Li Zefanf50daa72013-03-01 15:06:07 +08005247 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005248 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005249 /*
5250 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005251 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005252 * it now
5253 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005254 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005255
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005256 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005257 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005258 list_empty(&cgrp->release_list)) {
5259 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005260 need_schedule_work = 1;
5261 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005262 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005263 if (need_schedule_work)
5264 schedule_work(&release_agent_work);
5265 }
5266}
5267
Paul Menage81a6a5c2007-10-18 23:39:38 -07005268/*
5269 * Notify userspace when a cgroup is released, by running the
5270 * configured release agent with the name of the cgroup (path
5271 * relative to the root of cgroup file system) as the argument.
5272 *
5273 * Most likely, this user command will try to rmdir this cgroup.
5274 *
5275 * This races with the possibility that some other task will be
5276 * attached to this cgroup before it is removed, or that some other
5277 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5278 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5279 * unused, and this cgroup will be reprieved from its death sentence,
5280 * to continue to serve a useful existence. Next time it's released,
5281 * we will get notified again, if it still has 'notify_on_release' set.
5282 *
5283 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5284 * means only wait until the task is successfully execve()'d. The
5285 * separate release agent task is forked by call_usermodehelper(),
5286 * then control in this thread returns here, without waiting for the
5287 * release agent task. We don't bother to wait because the caller of
5288 * this routine has no use for the exit status of the release agent
5289 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005290 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005291static void cgroup_release_agent(struct work_struct *work)
5292{
5293 BUG_ON(work != &release_agent_work);
5294 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005295 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005296 while (!list_empty(&release_list)) {
5297 char *argv[3], *envp[3];
5298 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005299 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005300 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005301 struct cgroup,
5302 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005303 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005304 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005305 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005306 if (!pathbuf)
5307 goto continue_free;
5308 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5309 goto continue_free;
5310 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5311 if (!agentbuf)
5312 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005313
5314 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005315 argv[i++] = agentbuf;
5316 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005317 argv[i] = NULL;
5318
5319 i = 0;
5320 /* minimal command environment */
5321 envp[i++] = "HOME=/";
5322 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5323 envp[i] = NULL;
5324
5325 /* Drop the lock while we invoke the usermode helper,
5326 * since the exec could involve hitting disk and hence
5327 * be a slow process */
5328 mutex_unlock(&cgroup_mutex);
5329 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005330 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005331 continue_free:
5332 kfree(pathbuf);
5333 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005334 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005335 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005336 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005337 mutex_unlock(&cgroup_mutex);
5338}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005339
5340static int __init cgroup_disable(char *str)
5341{
Tejun Heo30159ec2013-06-25 11:53:37 -07005342 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005343 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005344 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005345
5346 while ((token = strsep(&str, ",")) != NULL) {
5347 if (!*token)
5348 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005349
Tejun Heo30159ec2013-06-25 11:53:37 -07005350 /*
5351 * cgroup_disable, being at boot time, can't know about
5352 * module subsystems, so we don't worry about them.
5353 */
5354 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005355 if (!strcmp(token, ss->name)) {
5356 ss->disabled = 1;
5357 printk(KERN_INFO "Disabling %s control group"
5358 " subsystem\n", ss->name);
5359 break;
5360 }
5361 }
5362 }
5363 return 1;
5364}
5365__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005366
5367/*
5368 * Functons for CSS ID.
5369 */
5370
Tejun Heo54766d42013-06-12 21:04:53 -07005371/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005372unsigned short css_id(struct cgroup_subsys_state *css)
5373{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005374 struct css_id *cssid;
5375
5376 /*
5377 * This css_id() can return correct value when somone has refcnt
5378 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5379 * it's unchanged until freed.
5380 */
Tejun Heod3daf282013-06-13 19:39:16 -07005381 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005382
5383 if (cssid)
5384 return cssid->id;
5385 return 0;
5386}
Ben Blum67523c42010-03-10 15:22:11 -08005387EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005388
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005389/**
5390 * css_is_ancestor - test "root" css is an ancestor of "child"
5391 * @child: the css to be tested.
5392 * @root: the css supporsed to be an ancestor of the child.
5393 *
5394 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005395 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005396 * But, considering usual usage, the csses should be valid objects after test.
5397 * Assuming that the caller will do some action to the child if this returns
5398 * returns true, the caller must take "child";s reference count.
5399 * If "child" is valid object and this returns true, "root" is valid, too.
5400 */
5401
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005402bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005403 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005404{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005405 struct css_id *child_id;
5406 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005407
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005408 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005409 if (!child_id)
5410 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005411 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005412 if (!root_id)
5413 return false;
5414 if (child_id->depth < root_id->depth)
5415 return false;
5416 if (child_id->stack[root_id->depth] != root_id->id)
5417 return false;
5418 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005419}
5420
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005421void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5422{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005423 struct css_id *id = rcu_dereference_protected(css->id, true);
5424
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005425 /* When this is called before css_id initialization, id can be NULL */
5426 if (!id)
5427 return;
5428
5429 BUG_ON(!ss->use_id);
5430
5431 rcu_assign_pointer(id->css, NULL);
5432 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005433 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005434 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005435 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005436 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005437}
Ben Blum67523c42010-03-10 15:22:11 -08005438EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005439
5440/*
5441 * This is called by init or create(). Then, calls to this function are
5442 * always serialized (By cgroup_mutex() at create()).
5443 */
5444
5445static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5446{
5447 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005448 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005449
5450 BUG_ON(!ss->use_id);
5451
5452 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5453 newid = kzalloc(size, GFP_KERNEL);
5454 if (!newid)
5455 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005456
5457 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005458 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005459 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005460 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005461 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005462 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005463
5464 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005465 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005466 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005467
Tejun Heod228d9e2013-02-27 17:04:54 -08005468 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005469 newid->depth = depth;
5470 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005471err_out:
5472 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005473 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005474
5475}
5476
Ben Blume6a11052010-03-10 15:22:09 -08005477static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5478 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005479{
5480 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005481
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005482 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005483 idr_init(&ss->idr);
5484
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005485 newid = get_new_cssid(ss, 0);
5486 if (IS_ERR(newid))
5487 return PTR_ERR(newid);
5488
5489 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005490 RCU_INIT_POINTER(newid->css, rootcss);
5491 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005492 return 0;
5493}
5494
5495static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5496 struct cgroup *child)
5497{
5498 int subsys_id, i, depth = 0;
5499 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005500 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005501
5502 subsys_id = ss->subsys_id;
5503 parent_css = parent->subsys[subsys_id];
5504 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005505 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005506 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005507
5508 child_id = get_new_cssid(ss, depth);
5509 if (IS_ERR(child_id))
5510 return PTR_ERR(child_id);
5511
5512 for (i = 0; i < depth; i++)
5513 child_id->stack[i] = parent_id->stack[i];
5514 child_id->stack[depth] = child_id->id;
5515 /*
5516 * child_id->css pointer will be set after this cgroup is available
5517 * see cgroup_populate_dir()
5518 */
5519 rcu_assign_pointer(child_css->id, child_id);
5520
5521 return 0;
5522}
5523
5524/**
5525 * css_lookup - lookup css by id
5526 * @ss: cgroup subsys to be looked into.
5527 * @id: the id
5528 *
5529 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5530 * NULL if not. Should be called under rcu_read_lock()
5531 */
5532struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5533{
5534 struct css_id *cssid = NULL;
5535
5536 BUG_ON(!ss->use_id);
5537 cssid = idr_find(&ss->idr, id);
5538
5539 if (unlikely(!cssid))
5540 return NULL;
5541
5542 return rcu_dereference(cssid->css);
5543}
Ben Blum67523c42010-03-10 15:22:11 -08005544EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005545
Stephane Eraniane5d13672011-02-14 11:20:01 +02005546/*
5547 * get corresponding css from file open on cgroupfs directory
5548 */
5549struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5550{
5551 struct cgroup *cgrp;
5552 struct inode *inode;
5553 struct cgroup_subsys_state *css;
5554
Al Viro496ad9a2013-01-23 17:07:38 -05005555 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005556 /* check in cgroup filesystem dir */
5557 if (inode->i_op != &cgroup_dir_inode_operations)
5558 return ERR_PTR(-EBADF);
5559
5560 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5561 return ERR_PTR(-EINVAL);
5562
5563 /* get cgroup */
5564 cgrp = __d_cgrp(f->f_dentry);
5565 css = cgrp->subsys[id];
5566 return css ? css : ERR_PTR(-ENOENT);
5567}
5568
Paul Menagefe693432009-09-23 15:56:20 -07005569#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005570static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005571{
5572 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5573
5574 if (!css)
5575 return ERR_PTR(-ENOMEM);
5576
5577 return css;
5578}
5579
Li Zefan03c78cb2013-06-14 11:17:19 +08005580static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005581{
Li Zefan03c78cb2013-06-14 11:17:19 +08005582 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005583}
5584
Li Zefan03c78cb2013-06-14 11:17:19 +08005585static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005586{
Li Zefan03c78cb2013-06-14 11:17:19 +08005587 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005588}
5589
Li Zefan03c78cb2013-06-14 11:17:19 +08005590static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005591{
5592 return (u64)(unsigned long)current->cgroups;
5593}
5594
Li Zefan03c78cb2013-06-14 11:17:19 +08005595static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5596 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005597{
5598 u64 count;
5599
5600 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005601 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005602 rcu_read_unlock();
5603 return count;
5604}
5605
Li Zefan03c78cb2013-06-14 11:17:19 +08005606static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005607 struct cftype *cft,
5608 struct seq_file *seq)
5609{
Tejun Heo69d02062013-06-12 21:04:50 -07005610 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005611 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005612
5613 read_lock(&css_set_lock);
5614 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005615 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005616 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005617 struct cgroup *c = link->cgrp;
5618 const char *name;
5619
5620 if (c->dentry)
5621 name = c->dentry->d_name.name;
5622 else
5623 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005624 seq_printf(seq, "Root %d group %s\n",
5625 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005626 }
5627 rcu_read_unlock();
5628 read_unlock(&css_set_lock);
5629 return 0;
5630}
5631
5632#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005633static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005634 struct cftype *cft,
5635 struct seq_file *seq)
5636{
Tejun Heo69d02062013-06-12 21:04:50 -07005637 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005638
5639 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005640 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005641 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005642 struct task_struct *task;
5643 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005644 seq_printf(seq, "css_set %p\n", cset);
5645 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005646 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5647 seq_puts(seq, " ...\n");
5648 break;
5649 } else {
5650 seq_printf(seq, " task %d\n",
5651 task_pid_vnr(task));
5652 }
5653 }
5654 }
5655 read_unlock(&css_set_lock);
5656 return 0;
5657}
5658
Paul Menagefe693432009-09-23 15:56:20 -07005659static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5660{
5661 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5662}
5663
5664static struct cftype debug_files[] = {
5665 {
Paul Menagefe693432009-09-23 15:56:20 -07005666 .name = "taskcount",
5667 .read_u64 = debug_taskcount_read,
5668 },
5669
5670 {
5671 .name = "current_css_set",
5672 .read_u64 = current_css_set_read,
5673 },
5674
5675 {
5676 .name = "current_css_set_refcount",
5677 .read_u64 = current_css_set_refcount_read,
5678 },
5679
5680 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005681 .name = "current_css_set_cg_links",
5682 .read_seq_string = current_css_set_cg_links_read,
5683 },
5684
5685 {
5686 .name = "cgroup_css_links",
5687 .read_seq_string = cgroup_css_links_read,
5688 },
5689
5690 {
Paul Menagefe693432009-09-23 15:56:20 -07005691 .name = "releasable",
5692 .read_u64 = releasable_read,
5693 },
Paul Menagefe693432009-09-23 15:56:20 -07005694
Tejun Heo4baf6e32012-04-01 12:09:55 -07005695 { } /* terminate */
5696};
Paul Menagefe693432009-09-23 15:56:20 -07005697
5698struct cgroup_subsys debug_subsys = {
5699 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005700 .css_alloc = debug_css_alloc,
5701 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005702 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005703 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005704};
5705#endif /* CONFIG_CGROUP_DEBUG */