blob: c4bc8dac3b1d2b9ed0cb8943609d9b3287934121 [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);
Tejun Heo8af01f52013-08-08 20:11:22 -040084EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070085#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);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400222static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
223 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Ben Blume6a11052010-03-10 15:22:09 -0800367static int cgroup_init_idr(struct cgroup_subsys *ss,
368 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700369
Paul Menage817929e2007-10-18 23:39:36 -0700370/* css_set_lock protects the list of css_set objects, and the
371 * chain of tasks off each css_set. Nests outside task->alloc_lock
372 * due to cgroup_iter_start() */
373static DEFINE_RWLOCK(css_set_lock);
374static int css_set_count;
375
Paul Menage7717f7b2009-09-23 15:56:22 -0700376/*
377 * hash table for cgroup groups. This improves the performance to find
378 * an existing css_set. This hash doesn't (currently) take into
379 * account cgroups in empty hierarchies.
380 */
Li Zefan472b1052008-04-29 01:00:11 -0700381#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700385{
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700387 struct cgroup_subsys *ss;
388 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Tejun Heo30159ec2013-06-25 11:53:37 -0700390 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 key += (unsigned long)css[i];
392 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700393
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Tejun Heo5abb8852013-06-12 21:04:49 -0700403static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
Tejun Heo69d02062013-06-12 21:04:50 -0700405 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700413 return;
414 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422 css_set_count--;
423
Tejun Heo69d02062013-06-12 21:04:50 -0700424 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700425 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700426
Tejun Heo69d02062013-06-12 21:04:50 -0700427 list_del(&link->cset_link);
428 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800429
Tejun Heoddd69142013-06-12 21:04:54 -0700430 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700431 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700433 set_bit(CGRP_RELEASABLE, &cgrp->flags);
434 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
444/*
445 * refcounted get/put for css_set objects
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700448{
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700453{
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458{
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460}
461
Tejun Heob326f9d2013-06-24 15:21:48 -0700462/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 * @cset: candidate css_set being tested
465 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * @new_cgrp: cgroup that's being entered by the task
467 * @template: desired set of css pointers in css_set (pre-calculated)
468 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800469 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
471 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472static bool compare_css_sets(struct css_set *cset,
473 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 struct cgroup *new_cgrp,
475 struct cgroup_subsys_state *template[])
476{
477 struct list_head *l1, *l2;
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 /* Not all subsystems matched */
481 return false;
482 }
483
484 /*
485 * Compare cgroup pointers in order to distinguish between
486 * different cgroups in heirarchies with no subsystems. We
487 * could get by with just this check alone (and skip the
488 * memcmp above) but on most setups the memcmp check will
489 * avoid the need for this more expensive check on almost all
490 * candidates.
491 */
492
Tejun Heo69d02062013-06-12 21:04:50 -0700493 l1 = &cset->cgrp_links;
494 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700496 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 l1 = l1->next;
500 l2 = l2->next;
501 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700502 if (l1 == &cset->cgrp_links) {
503 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 break;
505 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700506 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 }
508 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700509 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
510 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 cgrp1 = link1->cgrp;
512 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 /*
517 * If this hierarchy is the hierarchy of the cgroup
518 * that's changing, then we need to check that this
519 * css_set points to the new cgroup; if it's any other
520 * hierarchy, then this css_set should point to the
521 * same cgroup as the old css_set.
522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 if (cgrp1->root == new_cgrp->root) {
524 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 return false;
526 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 return false;
529 }
530 }
531 return true;
532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
535 * find_existing_css_set - init css array and find the matching css_set
536 * @old_cset: the css_set that we're using before the cgroup transition
537 * @cgrp: the cgroup that we're moving into
538 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540static struct css_set *find_existing_css_set(struct css_set *old_cset,
541 struct cgroup *cgrp,
542 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700543{
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800547 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700548 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Ben Blumaae8aab2010-03-10 15:22:07 -0800550 /*
551 * Build the set of subsystem state objects that we want to see in the
552 * new css_set. while subsystems can change globally, the entries here
553 * won't change, so no need for locking.
554 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700555 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400556 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700557 /* Subsystem is in this hierarchy. So we want
558 * the subsystem state from the new
559 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700560 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700561 } else {
562 /* Subsystem is not in this hierarchy, so we
563 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700564 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 }
566 }
567
Li Zefan0ac801f2013-01-10 11:49:27 +0800568 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 hash_for_each_possible(css_set_table, cset, hlist, key) {
570 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700571 continue;
572
573 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700574 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700575 }
Paul Menage817929e2007-10-18 23:39:36 -0700576
577 /* No existing cgroup group matched */
578 return NULL;
579}
580
Tejun Heo69d02062013-06-12 21:04:50 -0700581static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700582{
Tejun Heo69d02062013-06-12 21:04:50 -0700583 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700584
Tejun Heo69d02062013-06-12 21:04:50 -0700585 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
586 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700587 kfree(link);
588 }
589}
590
Tejun Heo69d02062013-06-12 21:04:50 -0700591/**
592 * allocate_cgrp_cset_links - allocate cgrp_cset_links
593 * @count: the number of links to allocate
594 * @tmp_links: list_head the allocated links are put on
595 *
596 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
597 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700600{
Tejun Heo69d02062013-06-12 21:04:50 -0700601 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700602 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700603
604 INIT_LIST_HEAD(tmp_links);
605
Li Zefan36553432008-07-29 22:33:19 -0700606 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700607 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700608 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700609 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700610 return -ENOMEM;
611 }
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700613 }
614 return 0;
615}
616
Li Zefanc12f65d2009-01-07 18:07:42 -0800617/**
618 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700619 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800621 * @cgrp: the destination cgroup
622 */
Tejun Heo69d02062013-06-12 21:04:50 -0700623static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
624 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800625{
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800627
Tejun Heo69d02062013-06-12 21:04:50 -0700628 BUG_ON(list_empty(tmp_links));
629 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
630 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700632 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 /*
634 * Always add links to the tail of the list so that the list
635 * is sorted by order of hierarchy creation
636 */
Tejun Heo69d02062013-06-12 21:04:50 -0700637 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800638}
639
Tejun Heob326f9d2013-06-24 15:21:48 -0700640/**
641 * find_css_set - return a new css_set with one cgroup updated
642 * @old_cset: the baseline css_set
643 * @cgrp: the cgroup to be updated
644 *
645 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
646 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700647 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700648static struct css_set *find_css_set(struct css_set *old_cset,
649 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700650{
Tejun Heob326f9d2013-06-24 15:21:48 -0700651 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700653 struct list_head tmp_links;
654 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800655 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700656
Tejun Heob326f9d2013-06-24 15:21:48 -0700657 lockdep_assert_held(&cgroup_mutex);
658
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* First see if we already have a cgroup group that matches
660 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700661 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 cset = find_existing_css_set(old_cset, cgrp, template);
663 if (cset)
664 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 if (cset)
668 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700669
Tejun Heof4f4be22013-06-12 21:04:51 -0700670 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700672 return NULL;
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700675 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700676 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700677 return NULL;
678 }
679
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700681 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 INIT_LIST_HEAD(&cset->tasks);
683 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 /* Copy the set of subsystem state objects generated in
686 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 write_lock(&css_set_lock);
690 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700691 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700693
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 if (c->root == cgrp->root)
695 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700696 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 }
Paul Menage817929e2007-10-18 23:39:36 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700700
Paul Menage817929e2007-10-18 23:39:36 -0700701 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700702
703 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 key = css_set_hash(cset->subsys);
705 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700706
Paul Menage817929e2007-10-18 23:39:36 -0700707 write_unlock(&css_set_lock);
708
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700710}
711
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 * Return the cgroup for "task" from the given hierarchy. Must be
714 * called with cgroup_mutex held.
715 */
716static struct cgroup *task_cgroup_from_root(struct task_struct *task,
717 struct cgroupfs_root *root)
718{
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 struct cgroup *res = NULL;
721
722 BUG_ON(!mutex_is_locked(&cgroup_mutex));
723 read_lock(&css_set_lock);
724 /*
725 * No need to lock the task - since we hold cgroup_mutex the
726 * task can't change groups, so the only thing that can happen
727 * is that it exits and its css is set back to init_css_set.
728 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700729 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700730 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700731 res = &root->top_cgroup;
732 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700733 struct cgrp_cset_link *link;
734
735 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700737
Paul Menage7717f7b2009-09-23 15:56:22 -0700738 if (c->root == root) {
739 res = c;
740 break;
741 }
742 }
743 }
744 read_unlock(&css_set_lock);
745 BUG_ON(!res);
746 return res;
747}
748
749/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 * There is one global cgroup mutex. We also require taking
751 * task_lock() when dereferencing a task's cgroup subsys pointers.
752 * See "The task_lock() exception", at the end of this comment.
753 *
754 * A task must hold cgroup_mutex to modify cgroups.
755 *
756 * Any task can increment and decrement the count field without lock.
757 * So in general, code holding cgroup_mutex can't rely on the count
758 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800759 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * means that no tasks are currently attached, therefore there is no
761 * way a task attached to that cgroup can fork (the other way to
762 * increment the count). So code holding cgroup_mutex can safely
763 * assume that if the count is zero, it will stay zero. Similarly, if
764 * a task holds cgroup_mutex on a cgroup with zero count, it
765 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * needs that mutex.
767 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
769 * (usually) take cgroup_mutex. These are the two most performance
770 * critical pieces of code here. The exception occurs on cgroup_exit(),
771 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
772 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800773 * to the release agent with the name of the cgroup (path relative to
774 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 *
776 * A cgroup can only be deleted if both its 'count' of using tasks
777 * is zero, and its list of 'children' cgroups is empty. Since all
778 * tasks in the system use _some_ cgroup, and since there is always at
779 * least one task in the system (init, pid == 1), therefore, top_cgroup
780 * always has either children cgroups and/or using tasks. So we don't
781 * need a special hack to ensure that top_cgroup cannot be deleted.
782 *
783 * The task_lock() exception
784 *
785 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800786 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800787 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 * several performance critical places that need to reference
789 * task->cgroup without the expense of grabbing a system global
790 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800791 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
793 * the task_struct routinely used for such matters.
794 *
795 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800796 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 */
798
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799/*
800 * A couple of forward declarations required, due to cyclic reference loop:
801 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
802 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
803 * -> cgroup_mkdir.
804 */
805
Al Viro18bb1db2011-07-26 01:41:39 -0400806static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400807static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700809static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700810static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700811static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700812
813static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200814 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700815 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700816};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700818static int alloc_css_id(struct cgroup_subsys *ss,
819 struct cgroup *parent, struct cgroup *child);
820
Al Viroa5e7ed32011-07-26 01:55:55 -0400821static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822{
823 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824
825 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400826 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100828 inode->i_uid = current_fsuid();
829 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
831 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
832 }
833 return inode;
834}
835
Li Zefan65dff752013-03-01 15:01:56 +0800836static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
837{
838 struct cgroup_name *name;
839
840 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
841 if (!name)
842 return NULL;
843 strcpy(name->name, dentry->d_name.name);
844 return name;
845}
846
Li Zefanbe445622013-01-24 14:31:42 +0800847static void cgroup_free_fn(struct work_struct *work)
848{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700849 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800850 struct cgroup_subsys *ss;
851
852 mutex_lock(&cgroup_mutex);
853 /*
854 * Release the subsystem state objects.
855 */
Tejun Heoeb954192013-08-08 20:11:23 -0400856 for_each_root_subsys(cgrp->root, ss) {
857 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
858
859 ss->css_free(css);
860 }
Li Zefanbe445622013-01-24 14:31:42 +0800861
862 cgrp->root->number_of_cgroups--;
863 mutex_unlock(&cgroup_mutex);
864
865 /*
Li Zefan415cf072013-04-08 14:35:02 +0800866 * We get a ref to the parent's dentry, and put the ref when
867 * this cgroup is being freed, so it's guaranteed that the
868 * parent won't be destroyed before its children.
869 */
870 dput(cgrp->parent->dentry);
871
872 /*
Li Zefanbe445622013-01-24 14:31:42 +0800873 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700874 * created the cgroup. This will free cgrp->root, if we are
875 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800876 */
877 deactivate_super(cgrp->root->sb);
878
879 /*
880 * if we're getting rid of the cgroup, refcount should ensure
881 * that there are no pidlists left.
882 */
883 BUG_ON(!list_empty(&cgrp->pidlists));
884
885 simple_xattrs_free(&cgrp->xattrs);
886
Li Zefan65dff752013-03-01 15:01:56 +0800887 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800888 kfree(cgrp);
889}
890
891static void cgroup_free_rcu(struct rcu_head *head)
892{
893 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
894
Tejun Heoea15f8c2013-06-13 19:27:42 -0700895 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
896 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800897}
898
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899static void cgroup_diput(struct dentry *dentry, struct inode *inode)
900{
901 /* is dentry a directory ? if so, kfree() associated cgroup */
902 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700903 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800904
Tejun Heo54766d42013-06-12 21:04:53 -0700905 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800906 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700907 } else {
908 struct cfent *cfe = __d_cfe(dentry);
909 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
910
911 WARN_ONCE(!list_empty(&cfe->node) &&
912 cgrp != &cgrp->root->top_cgroup,
913 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700914 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700915 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916 }
917 iput(inode);
918}
919
Al Viroc72a04e2011-01-14 05:31:45 +0000920static int cgroup_delete(const struct dentry *d)
921{
922 return 1;
923}
924
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925static void remove_dir(struct dentry *d)
926{
927 struct dentry *parent = dget(d->d_parent);
928
929 d_delete(d);
930 simple_rmdir(parent->d_inode, d);
931 dput(parent);
932}
933
Li Zefan2739d3c2013-01-21 18:18:33 +0800934static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
939 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100940
Li Zefan2739d3c2013-01-21 18:18:33 +0800941 /*
942 * If we're doing cleanup due to failure of cgroup_create(),
943 * the corresponding @cfe may not exist.
944 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700945 list_for_each_entry(cfe, &cgrp->files, node) {
946 struct dentry *d = cfe->dentry;
947
948 if (cft && cfe->type != cft)
949 continue;
950
951 dget(d);
952 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700953 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700954 list_del_init(&cfe->node);
955 dput(d);
956
Li Zefan2739d3c2013-01-21 18:18:33 +0800957 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959}
960
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700962 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700963 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400964 * @subsys_mask: mask of the subsystem ids whose files should be removed
965 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700966static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400968 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700969 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700970
Tejun Heob420ba72013-07-12 12:34:02 -0700971 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700973
974 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975 continue;
976 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400977 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979}
980
981/*
982 * NOTE : the dentry must have been dget()'ed
983 */
984static void cgroup_d_remove_dir(struct dentry *dentry)
985{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100986 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100988 parent = dentry->d_parent;
989 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800990 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100992 spin_unlock(&dentry->d_lock);
993 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 remove_dir(dentry);
995}
996
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700997/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800998 * Call with cgroup_mutex held. Drops reference counts on modules, including
999 * any duplicate ones that parse_cgroupfs_options took. If this function
1000 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001001 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001003 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004{
Paul Menagebd89aab2007-10-18 23:40:44 -07001005 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001006 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001007 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001008 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009
Ben Blumaae8aab2010-03-10 15:22:07 -08001010 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001011 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001012
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001014 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001015 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001017
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001018 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001019 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001020 ret = -EBUSY;
1021 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001023
1024 /* pin the module */
1025 if (!try_module_get(ss->module)) {
1026 ret = -ENOENT;
1027 goto out_put;
1028 }
1029 pinned |= 1 << i;
1030 }
1031
1032 /* subsys could be missing if unloaded between parsing and here */
1033 if (added_mask != pinned) {
1034 ret = -ENOENT;
1035 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 }
1037
Tejun Heo31261212013-06-28 17:07:30 -07001038 ret = cgroup_populate_dir(cgrp, added_mask);
1039 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001040 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001041
1042 /*
1043 * Nothing can fail from this point on. Remove files for the
1044 * removed subsystems and rebind each subsystem.
1045 */
1046 cgroup_clear_dir(cgrp, removed_mask);
1047
Tejun Heo30159ec2013-06-25 11:53:37 -07001048 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001050
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001051 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001053 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 BUG_ON(!cgroup_dummy_top->subsys[i]);
1055 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056
Tejun Heo9871bf92013-06-24 15:21:47 -07001057 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001059 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001060 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001062 ss->bind(cgrp->subsys[i]);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001063
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001066 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001068 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001069 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001070
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001072 ss->bind(cgroup_dummy_top->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001073 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001075 cgroup_subsys[i]->root = &cgroup_dummy_root;
1076 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001077
Ben Blumcf5d5942010-03-10 15:22:09 -08001078 /* subsystem is now free - drop reference on module */
1079 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001080 root->subsys_mask &= ~bit;
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;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001091
1092out_put:
1093 for_each_subsys(ss, i)
1094 if (pinned & (1 << i))
1095 module_put(ss->module);
1096 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097}
1098
Al Viro34c80b12011-12-08 21:32:45 -05001099static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100{
Al Viro34c80b12011-12-08 21:32:45 -05001101 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 struct cgroup_subsys *ss;
1103
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001104 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001105 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001107 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1108 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001109 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001111 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001112 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001113 if (strlen(root->release_agent_path))
1114 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001115 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001116 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001117 if (strlen(root->name))
1118 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001119 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120 return 0;
1121}
1122
1123struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001124 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001126 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001127 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001128 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001129 /* User explicitly requested empty subsystem */
1130 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001131
1132 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001133
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134};
1135
Ben Blumaae8aab2010-03-10 15:22:07 -08001136/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001137 * Convert a hierarchy specifier into a bitmask of subsystems and
1138 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1139 * array. This function takes refcounts on subsystems to be used, unless it
1140 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001141 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001142static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 char *token, *o = data;
1145 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001146 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001147 struct cgroup_subsys *ss;
1148 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149
Ben Blumaae8aab2010-03-10 15:22:07 -08001150 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1151
Li Zefanf9ab5b52009-06-17 16:26:33 -07001152#ifdef CONFIG_CPUSETS
1153 mask = ~(1UL << cpuset_subsys_id);
1154#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155
Paul Menagec6d57f32009-09-23 15:56:19 -07001156 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157
1158 while ((token = strsep(&o, ",")) != NULL) {
1159 if (!*token)
1160 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001162 /* Explicitly have no subsystems */
1163 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strcmp(token, "all")) {
1167 /* Mutually exclusive option 'all' + subsystem name */
1168 if (one_ss)
1169 return -EINVAL;
1170 all_ss = true;
1171 continue;
1172 }
Tejun Heo873fe092013-04-14 20:15:26 -07001173 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1174 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1175 continue;
1176 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001178 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001182 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001185 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001186 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001187 continue;
1188 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001189 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001190 /* Specifying two release agents is forbidden */
1191 if (opts->release_agent)
1192 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001194 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001195 if (!opts->release_agent)
1196 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 continue;
1198 }
1199 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 const char *name = token + 5;
1201 /* Can't specify an empty name */
1202 if (!strlen(name))
1203 return -EINVAL;
1204 /* Must match [\w.-]+ */
1205 for (i = 0; i < strlen(name); i++) {
1206 char c = name[i];
1207 if (isalnum(c))
1208 continue;
1209 if ((c == '.') || (c == '-') || (c == '_'))
1210 continue;
1211 return -EINVAL;
1212 }
1213 /* Specifying two names is forbidden */
1214 if (opts->name)
1215 return -EINVAL;
1216 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001217 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001218 GFP_KERNEL);
1219 if (!opts->name)
1220 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221
1222 continue;
1223 }
1224
Tejun Heo30159ec2013-06-25 11:53:37 -07001225 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 if (strcmp(token, ss->name))
1227 continue;
1228 if (ss->disabled)
1229 continue;
1230
1231 /* Mutually exclusive option 'all' + subsystem name */
1232 if (all_ss)
1233 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001234 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 one_ss = true;
1236
1237 break;
1238 }
1239 if (i == CGROUP_SUBSYS_COUNT)
1240 return -ENOENT;
1241 }
1242
1243 /*
1244 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001245 * otherwise if 'none', 'name=' and a subsystem name options
1246 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001247 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001248 if (all_ss || (!one_ss && !opts->none && !opts->name))
1249 for_each_subsys(ss, i)
1250 if (!ss->disabled)
1251 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001253 /* Consistency checks */
1254
Tejun Heo873fe092013-04-14 20:15:26 -07001255 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1256 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1257
1258 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1259 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1260 return -EINVAL;
1261 }
1262
1263 if (opts->cpuset_clone_children) {
1264 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1265 return -EINVAL;
1266 }
1267 }
1268
Li Zefanf9ab5b52009-06-17 16:26:33 -07001269 /*
1270 * Option noprefix was introduced just for backward compatibility
1271 * with the old cpuset, so we allow noprefix only if mounting just
1272 * the cpuset subsystem.
1273 */
Tejun Heo93438622013-04-14 20:15:25 -07001274 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001275 return -EINVAL;
1276
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277
1278 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001279 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001280 return -EINVAL;
1281
1282 /*
1283 * We either have to specify by name or by subsystems. (So all
1284 * empty hierarchies must have a name).
1285 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001286 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287 return -EINVAL;
1288
1289 return 0;
1290}
1291
1292static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1293{
1294 int ret = 0;
1295 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001296 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001297 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001298 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299
Tejun Heo873fe092013-04-14 20:15:26 -07001300 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1301 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1302 return -EINVAL;
1303 }
1304
Paul Menagebd89aab2007-10-18 23:40:44 -07001305 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001307 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308
1309 /* See what subsystems are wanted */
1310 ret = parse_cgroupfs_options(data, &opts);
1311 if (ret)
1312 goto out_unlock;
1313
Tejun Heoa8a648c2013-06-24 15:21:47 -07001314 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001315 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1316 task_tgid_nr(current), current->comm);
1317
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001318 added_mask = opts.subsys_mask & ~root->subsys_mask;
1319 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001320
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001322 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001323 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001324 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1325 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1326 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001328 goto out_unlock;
1329 }
1330
Tejun Heof172e672013-06-28 17:07:30 -07001331 /* remounting is not allowed for populated hierarchies */
1332 if (root->number_of_cgroups > 1) {
1333 ret = -EBUSY;
1334 goto out_unlock;
1335 }
1336
Tejun Heoa8a648c2013-06-24 15:21:47 -07001337 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001338 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001339 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340
Paul Menage81a6a5c2007-10-18 23:39:38 -07001341 if (opts.release_agent)
1342 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001344 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001345 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001346 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001348 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349 return ret;
1350}
1351
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001352static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 .statfs = simple_statfs,
1354 .drop_inode = generic_delete_inode,
1355 .show_options = cgroup_show_options,
1356 .remount_fs = cgroup_remount,
1357};
1358
Paul Menagecc31edc2008-10-18 20:28:04 -07001359static void init_cgroup_housekeeping(struct cgroup *cgrp)
1360{
1361 INIT_LIST_HEAD(&cgrp->sibling);
1362 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001363 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001364 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001365 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001366 INIT_LIST_HEAD(&cgrp->pidlists);
1367 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001368 INIT_LIST_HEAD(&cgrp->event_list);
1369 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001370 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001371}
Paul Menagec6d57f32009-09-23 15:56:19 -07001372
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373static void init_cgroup_root(struct cgroupfs_root *root)
1374{
Paul Menagebd89aab2007-10-18 23:40:44 -07001375 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001376
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377 INIT_LIST_HEAD(&root->subsys_list);
1378 INIT_LIST_HEAD(&root->root_list);
1379 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001380 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001381 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001382 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001383 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001384}
1385
Tejun Heofc76df72013-06-25 11:53:37 -07001386static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001387{
Tejun Heo1a574232013-04-14 11:36:58 -07001388 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001389
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001390 lockdep_assert_held(&cgroup_mutex);
1391 lockdep_assert_held(&cgroup_root_mutex);
1392
Tejun Heofc76df72013-06-25 11:53:37 -07001393 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1394 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001395 if (id < 0)
1396 return id;
1397
1398 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001399 return 0;
1400}
1401
1402static void cgroup_exit_root_id(struct cgroupfs_root *root)
1403{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001404 lockdep_assert_held(&cgroup_mutex);
1405 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001406
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001407 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001408 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001409 root->hierarchy_id = 0;
1410 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001411}
1412
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413static int cgroup_test_super(struct super_block *sb, void *data)
1414{
Paul Menagec6d57f32009-09-23 15:56:19 -07001415 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416 struct cgroupfs_root *root = sb->s_fs_info;
1417
Paul Menagec6d57f32009-09-23 15:56:19 -07001418 /* If we asked for a name then it must match */
1419 if (opts->name && strcmp(opts->name, root->name))
1420 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001422 /*
1423 * If we asked for subsystems (or explicitly for no
1424 * subsystems) then they must match
1425 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001426 if ((opts->subsys_mask || opts->none)
1427 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428 return 0;
1429
1430 return 1;
1431}
1432
Paul Menagec6d57f32009-09-23 15:56:19 -07001433static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1434{
1435 struct cgroupfs_root *root;
1436
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001437 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001438 return NULL;
1439
1440 root = kzalloc(sizeof(*root), GFP_KERNEL);
1441 if (!root)
1442 return ERR_PTR(-ENOMEM);
1443
1444 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001445
Tejun Heo1672d042013-06-25 18:04:54 -07001446 /*
1447 * We need to set @root->subsys_mask now so that @root can be
1448 * matched by cgroup_test_super() before it finishes
1449 * initialization; otherwise, competing mounts with the same
1450 * options may try to bind the same subsystems instead of waiting
1451 * for the first one leading to unexpected mount errors.
1452 * SUBSYS_BOUND will be set once actual binding is complete.
1453 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001454 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001455 root->flags = opts->flags;
1456 if (opts->release_agent)
1457 strcpy(root->release_agent_path, opts->release_agent);
1458 if (opts->name)
1459 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001460 if (opts->cpuset_clone_children)
1461 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001462 return root;
1463}
1464
Tejun Heofa3ca072013-04-14 11:36:56 -07001465static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001466{
Tejun Heofa3ca072013-04-14 11:36:56 -07001467 if (root) {
1468 /* hierarhcy ID shoulid already have been released */
1469 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001470
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001471 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001472 kfree(root);
1473 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001474}
1475
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476static int cgroup_set_super(struct super_block *sb, void *data)
1477{
1478 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 struct cgroup_sb_opts *opts = data;
1480
1481 /* If we don't have a new root, we can't set up a new sb */
1482 if (!opts->new_root)
1483 return -EINVAL;
1484
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001485 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001486
1487 ret = set_anon_super(sb, NULL);
1488 if (ret)
1489 return ret;
1490
Paul Menagec6d57f32009-09-23 15:56:19 -07001491 sb->s_fs_info = opts->new_root;
1492 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001493
1494 sb->s_blocksize = PAGE_CACHE_SIZE;
1495 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1496 sb->s_magic = CGROUP_SUPER_MAGIC;
1497 sb->s_op = &cgroup_ops;
1498
1499 return 0;
1500}
1501
1502static int cgroup_get_rootdir(struct super_block *sb)
1503{
Al Viro0df6a632010-12-21 13:29:29 -05001504 static const struct dentry_operations cgroup_dops = {
1505 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001506 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001507 };
1508
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509 struct inode *inode =
1510 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511
1512 if (!inode)
1513 return -ENOMEM;
1514
Paul Menageddbcc7e2007-10-18 23:39:30 -07001515 inode->i_fop = &simple_dir_operations;
1516 inode->i_op = &cgroup_dir_inode_operations;
1517 /* directories start off with i_nlink == 2 (for "." entry) */
1518 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001519 sb->s_root = d_make_root(inode);
1520 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001522 /* for everything else we want ->d_op set */
1523 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524 return 0;
1525}
1526
Al Virof7e83572010-07-26 13:23:11 +04001527static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001529 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530{
1531 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001532 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 int ret = 0;
1534 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001535 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001536 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001537 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001538 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539
1540 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001541 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001543 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001544 if (ret)
1545 goto out_err;
1546
1547 /*
1548 * Allocate a new cgroup root. We may not need it if we're
1549 * reusing an existing hierarchy.
1550 */
1551 new_root = cgroup_root_from_opts(&opts);
1552 if (IS_ERR(new_root)) {
1553 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001554 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001555 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001556 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557
Paul Menagec6d57f32009-09-23 15:56:19 -07001558 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001559 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001561 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001562 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001563 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 }
1565
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 root = sb->s_fs_info;
1567 BUG_ON(!root);
1568 if (root == opts.new_root) {
1569 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001570 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001572 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001573 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574
1575 BUG_ON(sb->s_root != NULL);
1576
1577 ret = cgroup_get_rootdir(sb);
1578 if (ret)
1579 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001580 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001581
Paul Menage817929e2007-10-18 23:39:36 -07001582 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001584 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001585
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001586 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1587 0, 1, GFP_KERNEL);
1588 if (root_cgrp->id < 0)
1589 goto unlock_drop;
1590
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001591 /* Check for name clashes with existing mounts */
1592 ret = -EBUSY;
1593 if (strlen(root->name))
1594 for_each_active_root(existing_root)
1595 if (!strcmp(existing_root->name, root->name))
1596 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001597
Paul Menage817929e2007-10-18 23:39:36 -07001598 /*
1599 * We're accessing css_set_count without locking
1600 * css_set_lock here, but that's OK - it can only be
1601 * increased by someone holding cgroup_lock, and
1602 * that's us. The worst that can happen is that we
1603 * have some link structures left over
1604 */
Tejun Heo69d02062013-06-12 21:04:50 -07001605 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001606 if (ret)
1607 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001608
Tejun Heofc76df72013-06-25 11:53:37 -07001609 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1610 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001611 if (ret)
1612 goto unlock_drop;
1613
Tejun Heo31261212013-06-28 17:07:30 -07001614 sb->s_root->d_fsdata = root_cgrp;
1615 root_cgrp->dentry = sb->s_root;
1616
1617 /*
1618 * We're inside get_sb() and will call lookup_one_len() to
1619 * create the root files, which doesn't work if SELinux is
1620 * in use. The following cred dancing somehow works around
1621 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1622 * populating new cgroupfs mount") for more details.
1623 */
1624 cred = override_creds(&init_cred);
1625
Tejun Heo2bb566c2013-08-08 20:11:23 -04001626 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001627 if (ret)
1628 goto rm_base_files;
1629
Tejun Heoa8a648c2013-06-24 15:21:47 -07001630 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001631 if (ret)
1632 goto rm_base_files;
1633
1634 revert_creds(cred);
1635
Ben Blumcf5d5942010-03-10 15:22:09 -08001636 /*
1637 * There must be no failure case after here, since rebinding
1638 * takes care of subsystems' refcounts, which are explicitly
1639 * dropped in the failure exit path.
1640 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001641
Tejun Heo9871bf92013-06-24 15:21:47 -07001642 list_add(&root->root_list, &cgroup_roots);
1643 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001644
Paul Menage817929e2007-10-18 23:39:36 -07001645 /* Link the top cgroup in this hierarchy into all
1646 * the css_set objects */
1647 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001648 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001649 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001650 write_unlock(&css_set_lock);
1651
Tejun Heo69d02062013-06-12 21:04:50 -07001652 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001653
Li Zefanc12f65d2009-01-07 18:07:42 -08001654 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 BUG_ON(root->number_of_cgroups != 1);
1656
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001657 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001659 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001660 } else {
1661 /*
1662 * We re-used an existing hierarchy - the new root (if
1663 * any) is not needed
1664 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001665 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001666
Tejun Heoc7ba8282013-06-29 14:06:10 -07001667 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001668 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1669 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1670 ret = -EINVAL;
1671 goto drop_new_super;
1672 } else {
1673 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1674 }
Tejun Heo873fe092013-04-14 20:15:26 -07001675 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 }
1677
Paul Menagec6d57f32009-09-23 15:56:19 -07001678 kfree(opts.release_agent);
1679 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001680 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681
Tejun Heo31261212013-06-28 17:07:30 -07001682 rm_base_files:
1683 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001684 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001685 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001686 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001687 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001688 mutex_unlock(&cgroup_root_mutex);
1689 mutex_unlock(&cgroup_mutex);
1690 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001691 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001692 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001693 out_err:
1694 kfree(opts.release_agent);
1695 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001696 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001697}
1698
1699static void cgroup_kill_sb(struct super_block *sb) {
1700 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001701 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001702 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001703 int ret;
1704
1705 BUG_ON(!root);
1706
1707 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001708 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709
Tejun Heo31261212013-06-28 17:07:30 -07001710 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001712 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001713
1714 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001715 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1716 ret = rebind_subsystems(root, 0, root->subsys_mask);
1717 /* Shouldn't be able to fail ... */
1718 BUG_ON(ret);
1719 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720
Paul Menage817929e2007-10-18 23:39:36 -07001721 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001722 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001723 * root cgroup
1724 */
1725 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001726
Tejun Heo69d02062013-06-12 21:04:50 -07001727 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1728 list_del(&link->cset_link);
1729 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001730 kfree(link);
1731 }
1732 write_unlock(&css_set_lock);
1733
Paul Menage839ec542009-01-29 14:25:22 -08001734 if (!list_empty(&root->root_list)) {
1735 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001736 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001737 }
Li Zefane5f6a862009-01-07 18:07:41 -08001738
Tejun Heofa3ca072013-04-14 11:36:56 -07001739 cgroup_exit_root_id(root);
1740
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001741 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001742 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001743 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001745 simple_xattrs_free(&cgrp->xattrs);
1746
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001748 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749}
1750
1751static struct file_system_type cgroup_fs_type = {
1752 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001753 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 .kill_sb = cgroup_kill_sb,
1755};
1756
Greg KH676db4a2010-08-05 13:53:35 -07001757static struct kobject *cgroup_kobj;
1758
Li Zefana043e3b2008-02-23 15:24:09 -08001759/**
1760 * cgroup_path - generate the path of a cgroup
1761 * @cgrp: the cgroup in question
1762 * @buf: the buffer to write the path into
1763 * @buflen: the length of the buffer
1764 *
Li Zefan65dff752013-03-01 15:01:56 +08001765 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1766 *
1767 * We can't generate cgroup path using dentry->d_name, as accessing
1768 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1769 * inode's i_mutex, while on the other hand cgroup_path() can be called
1770 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001772int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773{
Li Zefan65dff752013-03-01 15:01:56 +08001774 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001775 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001776
Tejun Heoda1f2962013-04-14 10:32:19 -07001777 if (!cgrp->parent) {
1778 if (strlcpy(buf, "/", buflen) >= buflen)
1779 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 return 0;
1781 }
1782
Tao Ma316eb662012-11-08 21:36:38 +08001783 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001784 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001785
Li Zefan65dff752013-03-01 15:01:56 +08001786 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001787 do {
Li Zefan65dff752013-03-01 15:01:56 +08001788 const char *name = cgroup_name(cgrp);
1789 int len;
1790
1791 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001793 goto out;
1794 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001795
Paul Menageddbcc7e2007-10-18 23:39:30 -07001796 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001797 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001799
1800 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001801 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001802 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001804out:
1805 rcu_read_unlock();
1806 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001807}
Ben Blum67523c42010-03-10 15:22:11 -08001808EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809
Tejun Heo857a2be2013-04-14 20:50:08 -07001810/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001811 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001812 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001813 * @buf: the buffer to write the path into
1814 * @buflen: the length of the buffer
1815 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001816 * Determine @task's cgroup on the first (the one with the lowest non-zero
1817 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1818 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1819 * cgroup controller callbacks.
1820 *
1821 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001822 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001823int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001824{
1825 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001826 struct cgroup *cgrp;
1827 int hierarchy_id = 1, ret = 0;
1828
1829 if (buflen < 2)
1830 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001831
1832 mutex_lock(&cgroup_mutex);
1833
Tejun Heo913ffdb2013-07-11 16:34:48 -07001834 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1835
Tejun Heo857a2be2013-04-14 20:50:08 -07001836 if (root) {
1837 cgrp = task_cgroup_from_root(task, root);
1838 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001839 } else {
1840 /* if no hierarchy exists, everyone is in "/" */
1841 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001842 }
1843
1844 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001845 return ret;
1846}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001847EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001848
Ben Blum74a11662011-05-26 16:25:20 -07001849/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001850 * Control Group taskset
1851 */
Tejun Heo134d3372011-12-12 18:12:21 -08001852struct task_and_cgroup {
1853 struct task_struct *task;
1854 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001855 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001856};
1857
Tejun Heo2f7ee562011-12-12 18:12:21 -08001858struct cgroup_taskset {
1859 struct task_and_cgroup single;
1860 struct flex_array *tc_array;
1861 int tc_array_len;
1862 int idx;
1863 struct cgroup *cur_cgrp;
1864};
1865
1866/**
1867 * cgroup_taskset_first - reset taskset and return the first task
1868 * @tset: taskset of interest
1869 *
1870 * @tset iteration is initialized and the first task is returned.
1871 */
1872struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1873{
1874 if (tset->tc_array) {
1875 tset->idx = 0;
1876 return cgroup_taskset_next(tset);
1877 } else {
1878 tset->cur_cgrp = tset->single.cgrp;
1879 return tset->single.task;
1880 }
1881}
1882EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1883
1884/**
1885 * cgroup_taskset_next - iterate to the next task in taskset
1886 * @tset: taskset of interest
1887 *
1888 * Return the next task in @tset. Iteration must have been initialized
1889 * with cgroup_taskset_first().
1890 */
1891struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1892{
1893 struct task_and_cgroup *tc;
1894
1895 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1896 return NULL;
1897
1898 tc = flex_array_get(tset->tc_array, tset->idx++);
1899 tset->cur_cgrp = tc->cgrp;
1900 return tc->task;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1903
1904/**
1905 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1906 * @tset: taskset of interest
1907 *
1908 * Return the cgroup for the current (last returned) task of @tset. This
1909 * function must be preceded by either cgroup_taskset_first() or
1910 * cgroup_taskset_next().
1911 */
1912struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1913{
1914 return tset->cur_cgrp;
1915}
1916EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1917
1918/**
1919 * cgroup_taskset_size - return the number of tasks in taskset
1920 * @tset: taskset of interest
1921 */
1922int cgroup_taskset_size(struct cgroup_taskset *tset)
1923{
1924 return tset->tc_array ? tset->tc_array_len : 1;
1925}
1926EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1927
1928
Ben Blum74a11662011-05-26 16:25:20 -07001929/*
1930 * cgroup_task_migrate - move a task from one cgroup to another.
1931 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001932 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001933 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001934static void cgroup_task_migrate(struct cgroup *old_cgrp,
1935 struct task_struct *tsk,
1936 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001937{
Tejun Heo5abb8852013-06-12 21:04:49 -07001938 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001939
1940 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001941 * We are synchronized through threadgroup_lock() against PF_EXITING
1942 * setting such that we can't race against cgroup_exit() changing the
1943 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001944 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001945 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001946 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001947
Ben Blum74a11662011-05-26 16:25:20 -07001948 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001949 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001950 task_unlock(tsk);
1951
1952 /* Update the css_set linked lists if we're using them */
1953 write_lock(&css_set_lock);
1954 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001955 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001956 write_unlock(&css_set_lock);
1957
1958 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 * We just gained a reference on old_cset by taking it from the
1960 * task. As trading it for new_cset is protected by cgroup_mutex,
1961 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001963 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1964 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001965}
1966
Li Zefana043e3b2008-02-23 15:24:09 -08001967/**
Li Zefan081aa452013-03-13 09:17:09 +08001968 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001969 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001970 * @tsk: the task or the leader of the threadgroup to be attached
1971 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001972 *
Tejun Heo257058a2011-12-12 18:12:21 -08001973 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001974 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001975 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001976static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1977 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001978{
1979 int retval, i, group_size;
1980 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001981 struct cgroupfs_root *root = cgrp->root;
1982 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001983 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001984 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001985 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001986 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001987
1988 /*
1989 * step 0: in order to do expensive, possibly blocking operations for
1990 * every thread, we cannot iterate the thread group list, since it needs
1991 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001992 * group - group_rwsem prevents new threads from appearing, and if
1993 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001994 */
Li Zefan081aa452013-03-13 09:17:09 +08001995 if (threadgroup)
1996 group_size = get_nr_threads(tsk);
1997 else
1998 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001999 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002000 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002001 if (!group)
2002 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002003 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002004 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002005 if (retval)
2006 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002007
Ben Blum74a11662011-05-26 16:25:20 -07002008 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002009 /*
2010 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2011 * already PF_EXITING could be freed from underneath us unless we
2012 * take an rcu_read_lock.
2013 */
2014 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002015 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002016 struct task_and_cgroup ent;
2017
Tejun Heocd3d0952011-12-12 18:12:21 -08002018 /* @tsk either already exited or can't exit until the end */
2019 if (tsk->flags & PF_EXITING)
2020 continue;
2021
Ben Blum74a11662011-05-26 16:25:20 -07002022 /* as per above, nr_threads may decrease, but not increase. */
2023 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002024 ent.task = tsk;
2025 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002026 /* nothing to do if this task is already in the cgroup */
2027 if (ent.cgrp == cgrp)
2028 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002029 /*
2030 * saying GFP_ATOMIC has no effect here because we did prealloc
2031 * earlier, but it's good form to communicate our expectations.
2032 */
Tejun Heo134d3372011-12-12 18:12:21 -08002033 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002034 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002035 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002036
2037 if (!threadgroup)
2038 break;
Ben Blum74a11662011-05-26 16:25:20 -07002039 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002040 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002041 /* remember the number of threads in the array for later. */
2042 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002043 tset.tc_array = group;
2044 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002045
Tejun Heo134d3372011-12-12 18:12:21 -08002046 /* methods shouldn't be called if no task is actually migrating */
2047 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002048 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002049 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002050
Ben Blum74a11662011-05-26 16:25:20 -07002051 /*
2052 * step 1: check that we can legitimately attach to the cgroup.
2053 */
Tejun Heo5549c492013-06-24 15:21:48 -07002054 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002055 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2056
Ben Blum74a11662011-05-26 16:25:20 -07002057 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002058 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002059 if (retval) {
2060 failed_ss = ss;
2061 goto out_cancel_attach;
2062 }
2063 }
Ben Blum74a11662011-05-26 16:25:20 -07002064 }
2065
2066 /*
2067 * step 2: make sure css_sets exist for all threads to be migrated.
2068 * we use find_css_set, which allocates a new one if necessary.
2069 */
Ben Blum74a11662011-05-26 16:25:20 -07002070 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002071 struct css_set *old_cset;
2072
Tejun Heo134d3372011-12-12 18:12:21 -08002073 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002074 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002075 tc->cset = find_css_set(old_cset, cgrp);
2076 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002077 retval = -ENOMEM;
2078 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002079 }
2080 }
2081
2082 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002083 * step 3: now that we're guaranteed success wrt the css_sets,
2084 * proceed to move all tasks to the new cgroup. There are no
2085 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002086 */
Ben Blum74a11662011-05-26 16:25:20 -07002087 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002088 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002089 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002090 }
2091 /* nothing is sensitive to fork() after this point. */
2092
2093 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002094 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002095 */
Tejun Heo5549c492013-06-24 15:21:48 -07002096 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002097 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2098
Ben Blum74a11662011-05-26 16:25:20 -07002099 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002100 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002101 }
2102
2103 /*
2104 * step 5: success! and cleanup
2105 */
Ben Blum74a11662011-05-26 16:25:20 -07002106 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002107out_put_css_set_refs:
2108 if (retval) {
2109 for (i = 0; i < group_size; i++) {
2110 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002111 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002112 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002113 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002114 }
Ben Blum74a11662011-05-26 16:25:20 -07002115 }
2116out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002117 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002118 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002119 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2120
Tejun Heo494c1672011-12-12 18:12:22 -08002121 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002122 break;
Ben Blum74a11662011-05-26 16:25:20 -07002123 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002124 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002125 }
2126 }
Ben Blum74a11662011-05-26 16:25:20 -07002127out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002128 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002129 return retval;
2130}
2131
2132/*
2133 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002134 * function to attach either it or all tasks in its threadgroup. Will lock
2135 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002136 */
2137static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002138{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002140 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141 int ret;
2142
Ben Blum74a11662011-05-26 16:25:20 -07002143 if (!cgroup_lock_live_group(cgrp))
2144 return -ENODEV;
2145
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002146retry_find_task:
2147 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002148 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002149 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002150 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002151 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002152 ret= -ESRCH;
2153 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002154 }
Ben Blum74a11662011-05-26 16:25:20 -07002155 /*
2156 * even if we're attaching all tasks in the thread group, we
2157 * only need to check permissions on one of them.
2158 */
David Howellsc69e8d92008-11-14 10:39:19 +11002159 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002160 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2161 !uid_eq(cred->euid, tcred->uid) &&
2162 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002163 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002164 ret = -EACCES;
2165 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002166 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167 } else
2168 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002169
2170 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002171 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002172
2173 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002174 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002175 * trapped in a cpuset, or RT worker may be born in a cgroup
2176 * with no rt_runtime allocated. Just say no.
2177 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002178 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002179 ret = -EINVAL;
2180 rcu_read_unlock();
2181 goto out_unlock_cgroup;
2182 }
2183
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002184 get_task_struct(tsk);
2185 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002186
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187 threadgroup_lock(tsk);
2188 if (threadgroup) {
2189 if (!thread_group_leader(tsk)) {
2190 /*
2191 * a race with de_thread from another thread's exec()
2192 * may strip us of our leadership, if this happens,
2193 * there is no choice but to throw this task away and
2194 * try again; this is
2195 * "double-double-toil-and-trouble-check locking".
2196 */
2197 threadgroup_unlock(tsk);
2198 put_task_struct(tsk);
2199 goto retry_find_task;
2200 }
Li Zefan081aa452013-03-13 09:17:09 +08002201 }
2202
2203 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2204
Tejun Heocd3d0952011-12-12 18:12:21 -08002205 threadgroup_unlock(tsk);
2206
Paul Menagebbcb81d2007-10-18 23:39:32 -07002207 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002208out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002209 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002210 return ret;
2211}
2212
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002213/**
2214 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2215 * @from: attach to all cgroups of a given task
2216 * @tsk: the task to be attached
2217 */
2218int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2219{
2220 struct cgroupfs_root *root;
2221 int retval = 0;
2222
Tejun Heo47cfcd02013-04-07 09:29:51 -07002223 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002224 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002225 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002226
Li Zefan6f4b7e62013-07-31 16:18:36 +08002227 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002228 if (retval)
2229 break;
2230 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002231 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002232
2233 return retval;
2234}
2235EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2236
Paul Menageaf351022008-07-25 01:47:01 -07002237static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2238{
Ben Blum74a11662011-05-26 16:25:20 -07002239 return attach_task_by_pid(cgrp, pid, false);
2240}
2241
2242static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2243{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002244 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002245}
2246
Paul Menagee788e062008-07-25 01:46:59 -07002247static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2248 const char *buffer)
2249{
2250 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002251 if (strlen(buffer) >= PATH_MAX)
2252 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002253 if (!cgroup_lock_live_group(cgrp))
2254 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002255 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002256 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002257 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002258 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002259 return 0;
2260}
2261
2262static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2263 struct seq_file *seq)
2264{
2265 if (!cgroup_lock_live_group(cgrp))
2266 return -ENODEV;
2267 seq_puts(seq, cgrp->root->release_agent_path);
2268 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002269 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002270 return 0;
2271}
2272
Tejun Heo873fe092013-04-14 20:15:26 -07002273static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2274 struct seq_file *seq)
2275{
2276 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002277 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002278}
2279
Paul Menage84eea842008-07-25 01:47:00 -07002280/* A buffer size big enough for numbers or short strings */
2281#define CGROUP_LOCAL_BUFFER_SIZE 64
2282
Paul Menagee73d2c62008-04-29 01:00:06 -07002283static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002284 struct file *file,
2285 const char __user *userbuf,
2286 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002287{
Paul Menage84eea842008-07-25 01:47:00 -07002288 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002289 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002290 char *end;
2291
2292 if (!nbytes)
2293 return -EINVAL;
2294 if (nbytes >= sizeof(buffer))
2295 return -E2BIG;
2296 if (copy_from_user(buffer, userbuf, nbytes))
2297 return -EFAULT;
2298
2299 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002300 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002301 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002302 if (*end)
2303 return -EINVAL;
2304 retval = cft->write_u64(cgrp, cft, val);
2305 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002306 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002307 if (*end)
2308 return -EINVAL;
2309 retval = cft->write_s64(cgrp, cft, val);
2310 }
Paul Menage355e0c42007-10-18 23:39:33 -07002311 if (!retval)
2312 retval = nbytes;
2313 return retval;
2314}
2315
Paul Menagedb3b1492008-07-25 01:46:58 -07002316static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2317 struct file *file,
2318 const char __user *userbuf,
2319 size_t nbytes, loff_t *unused_ppos)
2320{
Paul Menage84eea842008-07-25 01:47:00 -07002321 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002322 int retval = 0;
2323 size_t max_bytes = cft->max_write_len;
2324 char *buffer = local_buffer;
2325
2326 if (!max_bytes)
2327 max_bytes = sizeof(local_buffer) - 1;
2328 if (nbytes >= max_bytes)
2329 return -E2BIG;
2330 /* Allocate a dynamic buffer if we need one */
2331 if (nbytes >= sizeof(local_buffer)) {
2332 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2333 if (buffer == NULL)
2334 return -ENOMEM;
2335 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002336 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2337 retval = -EFAULT;
2338 goto out;
2339 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002340
2341 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002342 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002343 if (!retval)
2344 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002345out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002346 if (buffer != local_buffer)
2347 kfree(buffer);
2348 return retval;
2349}
2350
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2352 size_t nbytes, loff_t *ppos)
2353{
2354 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002355 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002356
Tejun Heo54766d42013-06-12 21:04:53 -07002357 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002358 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002359 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002360 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002361 if (cft->write_u64 || cft->write_s64)
2362 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002363 if (cft->write_string)
2364 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002365 if (cft->trigger) {
2366 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2367 return ret ? ret : nbytes;
2368 }
Paul Menage355e0c42007-10-18 23:39:33 -07002369 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370}
2371
Paul Menagef4c753b2008-04-29 00:59:56 -07002372static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2373 struct file *file,
2374 char __user *buf, size_t nbytes,
2375 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376{
Paul Menage84eea842008-07-25 01:47:00 -07002377 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002378 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2380
2381 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2382}
2383
Paul Menagee73d2c62008-04-29 01:00:06 -07002384static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2385 struct file *file,
2386 char __user *buf, size_t nbytes,
2387 loff_t *ppos)
2388{
Paul Menage84eea842008-07-25 01:47:00 -07002389 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002390 s64 val = cft->read_s64(cgrp, cft);
2391 int len = sprintf(tmp, "%lld\n", (long long) val);
2392
2393 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2394}
2395
Paul Menageddbcc7e2007-10-18 23:39:30 -07002396static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2397 size_t nbytes, loff_t *ppos)
2398{
2399 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002400 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401
Tejun Heo54766d42013-06-12 21:04:53 -07002402 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403 return -ENODEV;
2404
2405 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002406 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002407 if (cft->read_u64)
2408 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002409 if (cft->read_s64)
2410 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411 return -EINVAL;
2412}
2413
Paul Menage91796562008-04-29 01:00:01 -07002414/*
2415 * seqfile ops/methods for returning structured data. Currently just
2416 * supports string->u64 maps, but can be extended in future.
2417 */
2418
Paul Menage91796562008-04-29 01:00:01 -07002419static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2420{
2421 struct seq_file *sf = cb->state;
2422 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2423}
2424
2425static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2426{
Li Zefane0798ce2013-07-31 17:36:25 +08002427 struct cfent *cfe = m->private;
2428 struct cftype *cft = cfe->type;
2429 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2430
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002431 if (cft->read_map) {
2432 struct cgroup_map_cb cb = {
2433 .fill = cgroup_map_add,
2434 .state = m,
2435 };
Li Zefane0798ce2013-07-31 17:36:25 +08002436 return cft->read_map(cgrp, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002437 }
Li Zefane0798ce2013-07-31 17:36:25 +08002438 return cft->read_seq_string(cgrp, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002439}
2440
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002441static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002442 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002443 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002444 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002445 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002446};
2447
Paul Menageddbcc7e2007-10-18 23:39:30 -07002448static int cgroup_file_open(struct inode *inode, struct file *file)
2449{
2450 int err;
Li Zefane0798ce2013-07-31 17:36:25 +08002451 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002452 struct cftype *cft;
2453
2454 err = generic_file_open(inode, file);
2455 if (err)
2456 return err;
Li Zefane0798ce2013-07-31 17:36:25 +08002457 cfe = __d_cfe(file->f_dentry);
2458 cft = cfe->type;
Li Zefan75139b82009-01-07 18:07:33 -08002459
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002460 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002461 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002462 err = single_open(file, cgroup_seqfile_show, cfe);
2463 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002465 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002466
2467 return err;
2468}
2469
2470static int cgroup_file_release(struct inode *inode, struct file *file)
2471{
2472 struct cftype *cft = __d_cft(file->f_dentry);
2473 if (cft->release)
2474 return cft->release(inode, file);
2475 return 0;
2476}
2477
2478/*
2479 * cgroup_rename - Only allow simple rename of directories in place.
2480 */
2481static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2482 struct inode *new_dir, struct dentry *new_dentry)
2483{
Li Zefan65dff752013-03-01 15:01:56 +08002484 int ret;
2485 struct cgroup_name *name, *old_name;
2486 struct cgroup *cgrp;
2487
2488 /*
2489 * It's convinient to use parent dir's i_mutex to protected
2490 * cgrp->name.
2491 */
2492 lockdep_assert_held(&old_dir->i_mutex);
2493
Paul Menageddbcc7e2007-10-18 23:39:30 -07002494 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2495 return -ENOTDIR;
2496 if (new_dentry->d_inode)
2497 return -EEXIST;
2498 if (old_dir != new_dir)
2499 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002500
2501 cgrp = __d_cgrp(old_dentry);
2502
Tejun Heo6db8e852013-06-14 11:18:22 -07002503 /*
2504 * This isn't a proper migration and its usefulness is very
2505 * limited. Disallow if sane_behavior.
2506 */
2507 if (cgroup_sane_behavior(cgrp))
2508 return -EPERM;
2509
Li Zefan65dff752013-03-01 15:01:56 +08002510 name = cgroup_alloc_name(new_dentry);
2511 if (!name)
2512 return -ENOMEM;
2513
2514 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2515 if (ret) {
2516 kfree(name);
2517 return ret;
2518 }
2519
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002520 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002521 rcu_assign_pointer(cgrp->name, name);
2522
2523 kfree_rcu(old_name, rcu_head);
2524 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002525}
2526
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002527static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2528{
2529 if (S_ISDIR(dentry->d_inode->i_mode))
2530 return &__d_cgrp(dentry)->xattrs;
2531 else
Li Zefan712317a2013-04-18 23:09:52 -07002532 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002533}
2534
2535static inline int xattr_enabled(struct dentry *dentry)
2536{
2537 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002538 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002539}
2540
2541static bool is_valid_xattr(const char *name)
2542{
2543 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2544 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2545 return true;
2546 return false;
2547}
2548
2549static int cgroup_setxattr(struct dentry *dentry, const char *name,
2550 const void *val, size_t size, int flags)
2551{
2552 if (!xattr_enabled(dentry))
2553 return -EOPNOTSUPP;
2554 if (!is_valid_xattr(name))
2555 return -EINVAL;
2556 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2557}
2558
2559static int cgroup_removexattr(struct dentry *dentry, const char *name)
2560{
2561 if (!xattr_enabled(dentry))
2562 return -EOPNOTSUPP;
2563 if (!is_valid_xattr(name))
2564 return -EINVAL;
2565 return simple_xattr_remove(__d_xattrs(dentry), name);
2566}
2567
2568static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2569 void *buf, size_t size)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 if (!is_valid_xattr(name))
2574 return -EINVAL;
2575 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2576}
2577
2578static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2579{
2580 if (!xattr_enabled(dentry))
2581 return -EOPNOTSUPP;
2582 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2583}
2584
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002585static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002586 .read = cgroup_file_read,
2587 .write = cgroup_file_write,
2588 .llseek = generic_file_llseek,
2589 .open = cgroup_file_open,
2590 .release = cgroup_file_release,
2591};
2592
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002593static const struct inode_operations cgroup_file_inode_operations = {
2594 .setxattr = cgroup_setxattr,
2595 .getxattr = cgroup_getxattr,
2596 .listxattr = cgroup_listxattr,
2597 .removexattr = cgroup_removexattr,
2598};
2599
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002600static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002601 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002602 .mkdir = cgroup_mkdir,
2603 .rmdir = cgroup_rmdir,
2604 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002605 .setxattr = cgroup_setxattr,
2606 .getxattr = cgroup_getxattr,
2607 .listxattr = cgroup_listxattr,
2608 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002609};
2610
Al Viro00cd8dd2012-06-10 17:13:09 -04002611static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002612{
2613 if (dentry->d_name.len > NAME_MAX)
2614 return ERR_PTR(-ENAMETOOLONG);
2615 d_add(dentry, NULL);
2616 return NULL;
2617}
2618
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002619/*
2620 * Check if a file is a control file
2621 */
2622static inline struct cftype *__file_cft(struct file *file)
2623{
Al Viro496ad9a2013-01-23 17:07:38 -05002624 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002625 return ERR_PTR(-EINVAL);
2626 return __d_cft(file->f_dentry);
2627}
2628
Al Viroa5e7ed32011-07-26 01:55:55 -04002629static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002630 struct super_block *sb)
2631{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 struct inode *inode;
2633
2634 if (!dentry)
2635 return -ENOENT;
2636 if (dentry->d_inode)
2637 return -EEXIST;
2638
2639 inode = cgroup_new_inode(mode, sb);
2640 if (!inode)
2641 return -ENOMEM;
2642
2643 if (S_ISDIR(mode)) {
2644 inode->i_op = &cgroup_dir_inode_operations;
2645 inode->i_fop = &simple_dir_operations;
2646
2647 /* start off with i_nlink == 2 (for "." entry) */
2648 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002649 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002650
Tejun Heob8a2df62012-11-19 08:13:37 -08002651 /*
2652 * Control reaches here with cgroup_mutex held.
2653 * @inode->i_mutex should nest outside cgroup_mutex but we
2654 * want to populate it immediately without releasing
2655 * cgroup_mutex. As @inode isn't visible to anyone else
2656 * yet, trylock will always succeed without affecting
2657 * lockdep checks.
2658 */
2659 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660 } else if (S_ISREG(mode)) {
2661 inode->i_size = 0;
2662 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002663 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002664 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002665 d_instantiate(dentry, inode);
2666 dget(dentry); /* Extra count - pin the dentry in core */
2667 return 0;
2668}
2669
Li Zefan099fca32009-04-02 16:57:29 -07002670/**
2671 * cgroup_file_mode - deduce file mode of a control file
2672 * @cft: the control file in question
2673 *
2674 * returns cft->mode if ->mode is not 0
2675 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2676 * returns S_IRUGO if it has only a read handler
2677 * returns S_IWUSR if it has only a write hander
2678 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002679static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002680{
Al Viroa5e7ed32011-07-26 01:55:55 -04002681 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002682
2683 if (cft->mode)
2684 return cft->mode;
2685
2686 if (cft->read || cft->read_u64 || cft->read_s64 ||
2687 cft->read_map || cft->read_seq_string)
2688 mode |= S_IRUGO;
2689
2690 if (cft->write || cft->write_u64 || cft->write_s64 ||
2691 cft->write_string || cft->trigger)
2692 mode |= S_IWUSR;
2693
2694 return mode;
2695}
2696
Tejun Heo2bb566c2013-08-08 20:11:23 -04002697static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002698{
Paul Menagebd89aab2007-10-18 23:40:44 -07002699 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002700 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002701 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002704 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002706
Tejun Heo2bb566c2013-08-08 20:11:23 -04002707 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2708 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 strcat(name, ".");
2710 }
2711 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002712
Paul Menageddbcc7e2007-10-18 23:39:30 -07002713 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002714
2715 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2716 if (!cfe)
2717 return -ENOMEM;
2718
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002722 goto out;
2723 }
2724
Li Zefand6cbf352013-05-14 19:44:20 +08002725 cfe->type = (void *)cft;
2726 cfe->dentry = dentry;
2727 dentry->d_fsdata = cfe;
2728 simple_xattrs_init(&cfe->xattrs);
2729
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730 mode = cgroup_file_mode(cft);
2731 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2732 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733 list_add_tail(&cfe->node, &parent->files);
2734 cfe = NULL;
2735 }
2736 dput(dentry);
2737out:
2738 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 return error;
2740}
2741
Tejun Heob1f28d32013-06-28 16:24:10 -07002742/**
2743 * cgroup_addrm_files - add or remove files to a cgroup directory
2744 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002745 * @cfts: array of cftypes to be added
2746 * @is_add: whether to add or remove
2747 *
2748 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002749 * For removals, this function never fails. If addition fails, this
2750 * function doesn't remove files already added. The caller is responsible
2751 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002752 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002753static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2754 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002756 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002757 int ret;
2758
2759 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2760 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002761
2762 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002763 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002764 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2765 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002766 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2767 continue;
2768 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2769 continue;
2770
Li Zefan2739d3c2013-01-21 18:18:33 +08002771 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002772 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002773 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002774 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002775 cft->name, ret);
2776 return ret;
2777 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002778 } else {
2779 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002782 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783}
2784
Tejun Heo8e3f6542012-04-01 12:09:55 -07002785static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002786 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002787{
2788 /*
2789 * Thanks to the entanglement with vfs inode locking, we can't walk
2790 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002791 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2792 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002794 mutex_lock(&cgroup_mutex);
2795}
2796
Tejun Heo2bb566c2013-08-08 20:11:23 -04002797static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002798 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799{
2800 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002801 struct cgroup_subsys *ss = cfts[0].ss;
Li Zefane8c82d22013-06-18 18:48:37 +08002802 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002803 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002804 struct dentry *prev = NULL;
2805 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002806 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002807 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808
2809 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002810 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002811 !atomic_inc_not_zero(&sb->s_active)) {
2812 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002813 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002814 }
2815
Li Zefane8c82d22013-06-18 18:48:37 +08002816 /*
2817 * All cgroups which are created after we drop cgroup_mutex will
2818 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002819 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002820 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002821 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002822
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823 mutex_unlock(&cgroup_mutex);
2824
Li Zefane8c82d22013-06-18 18:48:37 +08002825 /* @root always needs to be updated */
2826 inode = root->dentry->d_inode;
2827 mutex_lock(&inode->i_mutex);
2828 mutex_lock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002829 ret = cgroup_addrm_files(root, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002830 mutex_unlock(&cgroup_mutex);
2831 mutex_unlock(&inode->i_mutex);
2832
Tejun Heo9ccece82013-06-28 16:24:11 -07002833 if (ret)
2834 goto out_deact;
2835
Li Zefane8c82d22013-06-18 18:48:37 +08002836 /* add/rm files for all cgroups created before */
2837 rcu_read_lock();
2838 cgroup_for_each_descendant_pre(cgrp, root) {
2839 if (cgroup_is_dead(cgrp))
2840 continue;
2841
2842 inode = cgrp->dentry->d_inode;
2843 dget(cgrp->dentry);
2844 rcu_read_unlock();
2845
2846 dput(prev);
2847 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002848
2849 mutex_lock(&inode->i_mutex);
2850 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002851 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002852 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853 mutex_unlock(&cgroup_mutex);
2854 mutex_unlock(&inode->i_mutex);
2855
Li Zefane8c82d22013-06-18 18:48:37 +08002856 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002857 if (ret)
2858 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859 }
Li Zefane8c82d22013-06-18 18:48:37 +08002860 rcu_read_unlock();
2861 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002862out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002863 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002864 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865}
2866
2867/**
2868 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2869 * @ss: target cgroup subsystem
2870 * @cfts: zero-length name terminated array of cftypes
2871 *
2872 * Register @cfts to @ss. Files described by @cfts are created for all
2873 * existing cgroups to which @ss is attached and all future cgroups will
2874 * have them too. This function can be called anytime whether @ss is
2875 * attached or not.
2876 *
2877 * Returns 0 on successful registration, -errno on failure. Note that this
2878 * function currently returns 0 as long as @cfts registration is successful
2879 * even if some file creation attempts on existing cgroups fail.
2880 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002881int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002882{
2883 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002884 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002885 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002886
2887 set = kzalloc(sizeof(*set), GFP_KERNEL);
2888 if (!set)
2889 return -ENOMEM;
2890
Tejun Heo2bb566c2013-08-08 20:11:23 -04002891 for (cft = cfts; cft->name[0] != '\0'; cft++)
2892 cft->ss = ss;
2893
Tejun Heo8e3f6542012-04-01 12:09:55 -07002894 cgroup_cfts_prepare();
2895 set->cfts = cfts;
2896 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002897 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002898 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002899 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002900 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901}
2902EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2903
Li Zefana043e3b2008-02-23 15:24:09 -08002904/**
Tejun Heo79578622012-04-01 12:09:56 -07002905 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002906 * @cfts: zero-length name terminated array of cftypes
2907 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002908 * Unregister @cfts. Files described by @cfts are removed from all
2909 * existing cgroups and all future cgroups won't have them either. This
2910 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002911 *
2912 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002913 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002914 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002915int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002916{
2917 struct cftype_set *set;
2918
Tejun Heo2bb566c2013-08-08 20:11:23 -04002919 if (!cfts || !cfts[0].ss)
2920 return -ENOENT;
2921
Tejun Heo79578622012-04-01 12:09:56 -07002922 cgroup_cfts_prepare();
2923
Tejun Heo2bb566c2013-08-08 20:11:23 -04002924 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002925 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002926 list_del(&set->node);
2927 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002928 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002929 return 0;
2930 }
2931 }
2932
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002934 return -ENOENT;
2935}
2936
2937/**
Li Zefana043e3b2008-02-23 15:24:09 -08002938 * cgroup_task_count - count the number of tasks in a cgroup.
2939 * @cgrp: the cgroup in question
2940 *
2941 * Return the number of tasks in the cgroup.
2942 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002943int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002944{
2945 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002946 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002947
Paul Menage817929e2007-10-18 23:39:36 -07002948 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002949 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2950 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002951 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002952 return count;
2953}
2954
2955/*
Paul Menage817929e2007-10-18 23:39:36 -07002956 * Advance a list_head iterator. The iterator should be positioned at
2957 * the start of a css_set
2958 */
Tejun Heo69d02062013-06-12 21:04:50 -07002959static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002960{
Tejun Heo69d02062013-06-12 21:04:50 -07002961 struct list_head *l = it->cset_link;
2962 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002963 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002964
2965 /* Advance to the next non-empty css_set */
2966 do {
2967 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002968 if (l == &cgrp->cset_links) {
2969 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002970 return;
2971 }
Tejun Heo69d02062013-06-12 21:04:50 -07002972 link = list_entry(l, struct cgrp_cset_link, cset_link);
2973 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002974 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002975 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002976 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002977}
2978
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979/*
2980 * To reduce the fork() overhead for systems that are not actually
2981 * using their cgroups capability, we don't maintain the lists running
2982 * through each css_set to its tasks until we see the list actually
2983 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002984 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002985static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002986{
2987 struct task_struct *p, *g;
2988 write_lock(&css_set_lock);
2989 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002990 /*
2991 * We need tasklist_lock because RCU is not safe against
2992 * while_each_thread(). Besides, a forking task that has passed
2993 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2994 * is not guaranteed to have its child immediately visible in the
2995 * tasklist if we walk through it with RCU.
2996 */
2997 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002998 do_each_thread(g, p) {
2999 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003000 /*
3001 * We should check if the process is exiting, otherwise
3002 * it will race with cgroup_exit() in that the list
3003 * entry won't be deleted though the process has exited.
3004 */
3005 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003006 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003007 task_unlock(p);
3008 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003009 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010 write_unlock(&css_set_lock);
3011}
3012
Tejun Heo574bd9f2012-11-09 09:12:29 -08003013/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003014 * cgroup_next_sibling - find the next sibling of a given cgroup
3015 * @pos: the current cgroup
3016 *
3017 * This function returns the next sibling of @pos and should be called
3018 * under RCU read lock. The only requirement is that @pos is accessible.
3019 * The next sibling is guaranteed to be returned regardless of @pos's
3020 * state.
3021 */
3022struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3023{
3024 struct cgroup *next;
3025
3026 WARN_ON_ONCE(!rcu_read_lock_held());
3027
3028 /*
3029 * @pos could already have been removed. Once a cgroup is removed,
3030 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003031 * changes. As CGRP_DEAD assertion is serialized and happens
3032 * before the cgroup is taken off the ->sibling list, if we see it
3033 * unasserted, it's guaranteed that the next sibling hasn't
3034 * finished its grace period even if it's already removed, and thus
3035 * safe to dereference from this RCU critical section. If
3036 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3037 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003038 */
Tejun Heo54766d42013-06-12 21:04:53 -07003039 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003040 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3041 if (&next->sibling != &pos->parent->children)
3042 return next;
3043 return NULL;
3044 }
3045
3046 /*
3047 * Can't dereference the next pointer. Each cgroup is given a
3048 * monotonically increasing unique serial number and always
3049 * appended to the sibling list, so the next one can be found by
3050 * walking the parent's children until we see a cgroup with higher
3051 * serial number than @pos's.
3052 *
3053 * While this path can be slow, it's taken only when either the
3054 * current cgroup is removed or iteration and removal race.
3055 */
3056 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3057 if (next->serial_nr > pos->serial_nr)
3058 return next;
3059 return NULL;
3060}
3061EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3062
3063/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003064 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3065 * @pos: the current position (%NULL to initiate traversal)
3066 * @cgroup: cgroup whose descendants to walk
3067 *
3068 * To be used by cgroup_for_each_descendant_pre(). Find the next
3069 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003070 *
3071 * While this function requires RCU read locking, it doesn't require the
3072 * whole traversal to be contained in a single RCU critical section. This
3073 * function will return the correct next descendant as long as both @pos
3074 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003075 */
3076struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3077 struct cgroup *cgroup)
3078{
3079 struct cgroup *next;
3080
3081 WARN_ON_ONCE(!rcu_read_lock_held());
3082
3083 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003084 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086
3087 /* visit the first child if exists */
3088 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3089 if (next)
3090 return next;
3091
3092 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003093 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003094 next = cgroup_next_sibling(pos);
3095 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003096 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003098 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099
3100 return NULL;
3101}
3102EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3103
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003104/**
3105 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3106 * @pos: cgroup of interest
3107 *
3108 * Return the rightmost descendant of @pos. If there's no descendant,
3109 * @pos is returned. This can be used during pre-order traversal to skip
3110 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003111 *
3112 * While this function requires RCU read locking, it doesn't require the
3113 * whole traversal to be contained in a single RCU critical section. This
3114 * function will return the correct rightmost descendant as long as @pos is
3115 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003116 */
3117struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3118{
3119 struct cgroup *last, *tmp;
3120
3121 WARN_ON_ONCE(!rcu_read_lock_held());
3122
3123 do {
3124 last = pos;
3125 /* ->prev isn't RCU safe, walk ->next till the end */
3126 pos = NULL;
3127 list_for_each_entry_rcu(tmp, &last->children, sibling)
3128 pos = tmp;
3129 } while (pos);
3130
3131 return last;
3132}
3133EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3134
Tejun Heo574bd9f2012-11-09 09:12:29 -08003135static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3136{
3137 struct cgroup *last;
3138
3139 do {
3140 last = pos;
3141 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3142 sibling);
3143 } while (pos);
3144
3145 return last;
3146}
3147
3148/**
3149 * cgroup_next_descendant_post - find the next descendant for post-order walk
3150 * @pos: the current position (%NULL to initiate traversal)
3151 * @cgroup: cgroup whose descendants to walk
3152 *
3153 * To be used by cgroup_for_each_descendant_post(). Find the next
3154 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003155 *
3156 * While this function requires RCU read locking, it doesn't require the
3157 * whole traversal to be contained in a single RCU critical section. This
3158 * function will return the correct next descendant as long as both @pos
3159 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003160 */
3161struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3162 struct cgroup *cgroup)
3163{
3164 struct cgroup *next;
3165
3166 WARN_ON_ONCE(!rcu_read_lock_held());
3167
3168 /* if first iteration, visit the leftmost descendant */
3169 if (!pos) {
3170 next = cgroup_leftmost_descendant(cgroup);
3171 return next != cgroup ? next : NULL;
3172 }
3173
3174 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003175 next = cgroup_next_sibling(pos);
3176 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003177 return cgroup_leftmost_descendant(next);
3178
3179 /* no sibling left, visit parent */
3180 next = pos->parent;
3181 return next != cgroup ? next : NULL;
3182}
3183EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3184
Paul Menagebd89aab2007-10-18 23:40:44 -07003185void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003186 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003187{
3188 /*
3189 * The first time anyone tries to iterate across a cgroup,
3190 * we need to enable the list linking each css_set to its
3191 * tasks, and fix up all existing tasks.
3192 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003193 if (!use_task_css_set_links)
3194 cgroup_enable_task_cg_lists();
3195
Paul Menage817929e2007-10-18 23:39:36 -07003196 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003197 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003198 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003199}
3200
Paul Menagebd89aab2007-10-18 23:40:44 -07003201struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003202 struct cgroup_iter *it)
3203{
3204 struct task_struct *res;
3205 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003206 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003207
3208 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003209 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003210 return NULL;
3211 res = list_entry(l, struct task_struct, cg_list);
3212 /* Advance iterator to find next entry */
3213 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003214 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3215 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003216 /* We reached the end of this task list - move on to
3217 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003218 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003219 } else {
3220 it->task = l;
3221 }
3222 return res;
3223}
3224
Paul Menagebd89aab2007-10-18 23:40:44 -07003225void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003226 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003227{
3228 read_unlock(&css_set_lock);
3229}
3230
Cliff Wickman31a7df02008-02-07 00:14:42 -08003231static inline int started_after_time(struct task_struct *t1,
3232 struct timespec *time,
3233 struct task_struct *t2)
3234{
3235 int start_diff = timespec_compare(&t1->start_time, time);
3236 if (start_diff > 0) {
3237 return 1;
3238 } else if (start_diff < 0) {
3239 return 0;
3240 } else {
3241 /*
3242 * Arbitrarily, if two processes started at the same
3243 * time, we'll say that the lower pointer value
3244 * started first. Note that t2 may have exited by now
3245 * so this may not be a valid pointer any longer, but
3246 * that's fine - it still serves to distinguish
3247 * between two tasks started (effectively) simultaneously.
3248 */
3249 return t1 > t2;
3250 }
3251}
3252
3253/*
3254 * This function is a callback from heap_insert() and is used to order
3255 * the heap.
3256 * In this case we order the heap in descending task start time.
3257 */
3258static inline int started_after(void *p1, void *p2)
3259{
3260 struct task_struct *t1 = p1;
3261 struct task_struct *t2 = p2;
3262 return started_after_time(t1, &t2->start_time, t2);
3263}
3264
3265/**
3266 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3267 * @scan: struct cgroup_scanner containing arguments for the scan
3268 *
3269 * Arguments include pointers to callback functions test_task() and
3270 * process_task().
3271 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3272 * and if it returns true, call process_task() for it also.
3273 * The test_task pointer may be NULL, meaning always true (select all tasks).
3274 * Effectively duplicates cgroup_iter_{start,next,end}()
3275 * but does not lock css_set_lock for the call to process_task().
3276 * The struct cgroup_scanner may be embedded in any structure of the caller's
3277 * creation.
3278 * It is guaranteed that process_task() will act on every task that
3279 * is a member of the cgroup for the duration of this call. This
3280 * function may or may not call process_task() for tasks that exit
3281 * or move to a different cgroup during the call, or are forked or
3282 * move into the cgroup during the call.
3283 *
3284 * Note that test_task() may be called with locks held, and may in some
3285 * situations be called multiple times for the same task, so it should
3286 * be cheap.
3287 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3288 * pre-allocated and will be used for heap operations (and its "gt" member will
3289 * be overwritten), else a temporary heap will be used (allocation of which
3290 * may cause this function to fail).
3291 */
3292int cgroup_scan_tasks(struct cgroup_scanner *scan)
3293{
3294 int retval, i;
3295 struct cgroup_iter it;
3296 struct task_struct *p, *dropped;
3297 /* Never dereference latest_task, since it's not refcounted */
3298 struct task_struct *latest_task = NULL;
3299 struct ptr_heap tmp_heap;
3300 struct ptr_heap *heap;
3301 struct timespec latest_time = { 0, 0 };
3302
3303 if (scan->heap) {
3304 /* The caller supplied our heap and pre-allocated its memory */
3305 heap = scan->heap;
3306 heap->gt = &started_after;
3307 } else {
3308 /* We need to allocate our own heap memory */
3309 heap = &tmp_heap;
3310 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3311 if (retval)
3312 /* cannot allocate the heap */
3313 return retval;
3314 }
3315
3316 again:
3317 /*
3318 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3319 * to determine which are of interest, and using the scanner's
3320 * "process_task" callback to process any of them that need an update.
3321 * Since we don't want to hold any locks during the task updates,
3322 * gather tasks to be processed in a heap structure.
3323 * The heap is sorted by descending task start time.
3324 * If the statically-sized heap fills up, we overflow tasks that
3325 * started later, and in future iterations only consider tasks that
3326 * started after the latest task in the previous pass. This
3327 * guarantees forward progress and that we don't miss any tasks.
3328 */
3329 heap->size = 0;
Li Zefan6f4b7e62013-07-31 16:18:36 +08003330 cgroup_iter_start(scan->cgrp, &it);
3331 while ((p = cgroup_iter_next(scan->cgrp, &it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003332 /*
3333 * Only affect tasks that qualify per the caller's callback,
3334 * if he provided one
3335 */
3336 if (scan->test_task && !scan->test_task(p, scan))
3337 continue;
3338 /*
3339 * Only process tasks that started after the last task
3340 * we processed
3341 */
3342 if (!started_after_time(p, &latest_time, latest_task))
3343 continue;
3344 dropped = heap_insert(heap, p);
3345 if (dropped == NULL) {
3346 /*
3347 * The new task was inserted; the heap wasn't
3348 * previously full
3349 */
3350 get_task_struct(p);
3351 } else if (dropped != p) {
3352 /*
3353 * The new task was inserted, and pushed out a
3354 * different task
3355 */
3356 get_task_struct(p);
3357 put_task_struct(dropped);
3358 }
3359 /*
3360 * Else the new task was newer than anything already in
3361 * the heap and wasn't inserted
3362 */
3363 }
Li Zefan6f4b7e62013-07-31 16:18:36 +08003364 cgroup_iter_end(scan->cgrp, &it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003365
3366 if (heap->size) {
3367 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003368 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003369 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003370 latest_time = q->start_time;
3371 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003372 }
3373 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003374 scan->process_task(q, scan);
3375 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003376 }
3377 /*
3378 * If we had to process any tasks at all, scan again
3379 * in case some of them were in the middle of forking
3380 * children that didn't get processed.
3381 * Not the most efficient way to do it, but it avoids
3382 * having to take callback_mutex in the fork path
3383 */
3384 goto again;
3385 }
3386 if (heap == &tmp_heap)
3387 heap_free(&tmp_heap);
3388 return 0;
3389}
3390
Tejun Heo8cc99342013-04-07 09:29:50 -07003391static void cgroup_transfer_one_task(struct task_struct *task,
3392 struct cgroup_scanner *scan)
3393{
3394 struct cgroup *new_cgroup = scan->data;
3395
Tejun Heo47cfcd02013-04-07 09:29:51 -07003396 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003397 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003398 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003399}
3400
3401/**
3402 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3403 * @to: cgroup to which the tasks will be moved
3404 * @from: cgroup in which the tasks currently reside
3405 */
3406int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3407{
3408 struct cgroup_scanner scan;
3409
Li Zefan6f4b7e62013-07-31 16:18:36 +08003410 scan.cgrp = from;
Tejun Heo8cc99342013-04-07 09:29:50 -07003411 scan.test_task = NULL; /* select all tasks in cgroup */
3412 scan.process_task = cgroup_transfer_one_task;
3413 scan.heap = NULL;
3414 scan.data = to;
3415
3416 return cgroup_scan_tasks(&scan);
3417}
3418
Paul Menage817929e2007-10-18 23:39:36 -07003419/*
Ben Blum102a7752009-09-23 15:56:26 -07003420 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003421 *
3422 * Reading this file can return large amounts of data if a cgroup has
3423 * *lots* of attached tasks. So it may need several calls to read(),
3424 * but we cannot guarantee that the information we produce is correct
3425 * unless we produce it entirely atomically.
3426 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003427 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003428
Li Zefan24528252012-01-20 11:58:43 +08003429/* which pidlist file are we talking about? */
3430enum cgroup_filetype {
3431 CGROUP_FILE_PROCS,
3432 CGROUP_FILE_TASKS,
3433};
3434
3435/*
3436 * A pidlist is a list of pids that virtually represents the contents of one
3437 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3438 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3439 * to the cgroup.
3440 */
3441struct cgroup_pidlist {
3442 /*
3443 * used to find which pidlist is wanted. doesn't change as long as
3444 * this particular list stays in the list.
3445 */
3446 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3447 /* array of xids */
3448 pid_t *list;
3449 /* how many elements the above list has */
3450 int length;
3451 /* how many files are using the current array */
3452 int use_count;
3453 /* each of these stored in a list by its cgroup */
3454 struct list_head links;
3455 /* pointer to the cgroup we belong to, for list removal purposes */
3456 struct cgroup *owner;
3457 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003458 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003459};
3460
Paul Menagebbcb81d2007-10-18 23:39:32 -07003461/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003462 * The following two functions "fix" the issue where there are more pids
3463 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3464 * TODO: replace with a kernel-wide solution to this problem
3465 */
3466#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3467static void *pidlist_allocate(int count)
3468{
3469 if (PIDLIST_TOO_LARGE(count))
3470 return vmalloc(count * sizeof(pid_t));
3471 else
3472 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3473}
3474static void pidlist_free(void *p)
3475{
3476 if (is_vmalloc_addr(p))
3477 vfree(p);
3478 else
3479 kfree(p);
3480}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003481
3482/*
Ben Blum102a7752009-09-23 15:56:26 -07003483 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003484 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003485 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003486static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003487{
Ben Blum102a7752009-09-23 15:56:26 -07003488 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003489
3490 /*
3491 * we presume the 0th element is unique, so i starts at 1. trivial
3492 * edge cases first; no work needs to be done for either
3493 */
3494 if (length == 0 || length == 1)
3495 return length;
3496 /* src and dest walk down the list; dest counts unique elements */
3497 for (src = 1; src < length; src++) {
3498 /* find next unique element */
3499 while (list[src] == list[src-1]) {
3500 src++;
3501 if (src == length)
3502 goto after;
3503 }
3504 /* dest always points to where the next unique element goes */
3505 list[dest] = list[src];
3506 dest++;
3507 }
3508after:
Ben Blum102a7752009-09-23 15:56:26 -07003509 return dest;
3510}
3511
3512static int cmppid(const void *a, const void *b)
3513{
3514 return *(pid_t *)a - *(pid_t *)b;
3515}
3516
3517/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003518 * find the appropriate pidlist for our purpose (given procs vs tasks)
3519 * returns with the lock on that pidlist already held, and takes care
3520 * of the use count, or returns NULL with no locks held if we're out of
3521 * memory.
3522 */
3523static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3524 enum cgroup_filetype type)
3525{
3526 struct cgroup_pidlist *l;
3527 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003528 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003529
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 /*
Li Zefanb3958902013-08-01 09:52:15 +08003531 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003532 * the last ref-holder is trying to remove l from the list at the same
3533 * time. Holding the pidlist_mutex precludes somebody taking whichever
3534 * list we find out from under us - compare release_pid_array().
3535 */
3536 mutex_lock(&cgrp->pidlist_mutex);
3537 list_for_each_entry(l, &cgrp->pidlists, links) {
3538 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003539 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003540 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003541 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003542 return l;
3543 }
3544 }
3545 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003546 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003547 if (!l) {
3548 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003549 return l;
3550 }
Li Zefanb3958902013-08-01 09:52:15 +08003551 init_rwsem(&l->rwsem);
3552 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003553 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003554 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003555 l->owner = cgrp;
3556 list_add(&l->links, &cgrp->pidlists);
3557 mutex_unlock(&cgrp->pidlist_mutex);
3558 return l;
3559}
3560
3561/*
Ben Blum102a7752009-09-23 15:56:26 -07003562 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3563 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003564static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3565 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003566{
3567 pid_t *array;
3568 int length;
3569 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003570 struct cgroup_iter it;
3571 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003572 struct cgroup_pidlist *l;
3573
3574 /*
3575 * If cgroup gets more users after we read count, we won't have
3576 * enough space - tough. This race is indistinguishable to the
3577 * caller from the case that the additional cgroup users didn't
3578 * show up until sometime later on.
3579 */
3580 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003581 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003582 if (!array)
3583 return -ENOMEM;
3584 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003585 cgroup_iter_start(cgrp, &it);
3586 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003587 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003588 break;
Ben Blum102a7752009-09-23 15:56:26 -07003589 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003590 if (type == CGROUP_FILE_PROCS)
3591 pid = task_tgid_vnr(tsk);
3592 else
3593 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003594 if (pid > 0) /* make sure to only use valid results */
3595 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003596 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003597 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003598 length = n;
3599 /* now sort & (if procs) strip out duplicates */
3600 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003601 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003602 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003603 l = cgroup_pidlist_find(cgrp, type);
3604 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003605 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003606 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003607 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003608 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003609 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003610 l->list = array;
3611 l->length = length;
3612 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003613 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003614 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003615 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003616}
3617
Balbir Singh846c7bb2007-10-18 23:39:44 -07003618/**
Li Zefana043e3b2008-02-23 15:24:09 -08003619 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003620 * @stats: cgroupstats to fill information into
3621 * @dentry: A dentry entry belonging to the cgroup for which stats have
3622 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003623 *
3624 * Build and fill cgroupstats so that taskstats can export it to user
3625 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003626 */
3627int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3628{
3629 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003630 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003631 struct cgroup_iter it;
3632 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003633
Balbir Singh846c7bb2007-10-18 23:39:44 -07003634 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003635 * Validate dentry by checking the superblock operations,
3636 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003637 */
Li Zefan33d283b2008-11-19 15:36:48 -08003638 if (dentry->d_sb->s_op != &cgroup_ops ||
3639 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003640 goto err;
3641
3642 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003643 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003644
Paul Menagebd89aab2007-10-18 23:40:44 -07003645 cgroup_iter_start(cgrp, &it);
3646 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003647 switch (tsk->state) {
3648 case TASK_RUNNING:
3649 stats->nr_running++;
3650 break;
3651 case TASK_INTERRUPTIBLE:
3652 stats->nr_sleeping++;
3653 break;
3654 case TASK_UNINTERRUPTIBLE:
3655 stats->nr_uninterruptible++;
3656 break;
3657 case TASK_STOPPED:
3658 stats->nr_stopped++;
3659 break;
3660 default:
3661 if (delayacct_is_task_waiting_on_io(tsk))
3662 stats->nr_io_wait++;
3663 break;
3664 }
3665 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003666 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003667
Balbir Singh846c7bb2007-10-18 23:39:44 -07003668err:
3669 return ret;
3670}
3671
Paul Menage8f3ff202009-09-23 15:56:25 -07003672
Paul Menagecc31edc2008-10-18 20:28:04 -07003673/*
Ben Blum102a7752009-09-23 15:56:26 -07003674 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003675 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003676 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003677 */
3678
Ben Blum102a7752009-09-23 15:56:26 -07003679static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003680{
3681 /*
3682 * Initially we receive a position value that corresponds to
3683 * one more than the last pid shown (or 0 on the first call or
3684 * after a seek to the start). Use a binary-search to find the
3685 * next pid to display, if any
3686 */
Ben Blum102a7752009-09-23 15:56:26 -07003687 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 int index = 0, pid = *pos;
3689 int *iter;
3690
Li Zefanb3958902013-08-01 09:52:15 +08003691 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003693 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003694
Paul Menagecc31edc2008-10-18 20:28:04 -07003695 while (index < end) {
3696 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003697 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003698 index = mid;
3699 break;
Ben Blum102a7752009-09-23 15:56:26 -07003700 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003701 index = mid + 1;
3702 else
3703 end = mid;
3704 }
3705 }
3706 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003707 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003708 return NULL;
3709 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003710 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003711 *pos = *iter;
3712 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003713}
3714
Ben Blum102a7752009-09-23 15:56:26 -07003715static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003716{
Ben Blum102a7752009-09-23 15:56:26 -07003717 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003718 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003719}
3720
Ben Blum102a7752009-09-23 15:56:26 -07003721static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003722{
Ben Blum102a7752009-09-23 15:56:26 -07003723 struct cgroup_pidlist *l = s->private;
3724 pid_t *p = v;
3725 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003726 /*
3727 * Advance to the next pid in the array. If this goes off the
3728 * end, we're done
3729 */
3730 p++;
3731 if (p >= end) {
3732 return NULL;
3733 } else {
3734 *pos = *p;
3735 return p;
3736 }
3737}
3738
Ben Blum102a7752009-09-23 15:56:26 -07003739static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003740{
3741 return seq_printf(s, "%d\n", *(int *)v);
3742}
3743
Ben Blum102a7752009-09-23 15:56:26 -07003744/*
3745 * seq_operations functions for iterating on pidlists through seq_file -
3746 * independent of whether it's tasks or procs
3747 */
3748static const struct seq_operations cgroup_pidlist_seq_operations = {
3749 .start = cgroup_pidlist_start,
3750 .stop = cgroup_pidlist_stop,
3751 .next = cgroup_pidlist_next,
3752 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003753};
3754
Ben Blum102a7752009-09-23 15:56:26 -07003755static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003756{
Ben Blum72a8cb32009-09-23 15:56:27 -07003757 /*
3758 * the case where we're the last user of this particular pidlist will
3759 * have us remove it from the cgroup's list, which entails taking the
3760 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3761 * pidlist_mutex, we have to take pidlist_mutex first.
3762 */
3763 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003764 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003765 BUG_ON(!l->use_count);
3766 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003767 /* we're the last user if refcount is 0; remove and free */
3768 list_del(&l->links);
3769 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003770 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003771 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003772 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003773 kfree(l);
3774 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003775 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003776 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003777 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003778}
3779
Ben Blum102a7752009-09-23 15:56:26 -07003780static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003781{
Ben Blum102a7752009-09-23 15:56:26 -07003782 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003783 if (!(file->f_mode & FMODE_READ))
3784 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003785 /*
3786 * the seq_file will only be initialized if the file was opened for
3787 * reading; hence we check if it's not null only in that case.
3788 */
3789 l = ((struct seq_file *)file->private_data)->private;
3790 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003791 return seq_release(inode, file);
3792}
3793
Ben Blum102a7752009-09-23 15:56:26 -07003794static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003795 .read = seq_read,
3796 .llseek = seq_lseek,
3797 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003798 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003799};
3800
3801/*
Ben Blum102a7752009-09-23 15:56:26 -07003802 * The following functions handle opens on a file that displays a pidlist
3803 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3804 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003805 */
Ben Blum102a7752009-09-23 15:56:26 -07003806/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003807static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003808{
3809 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003810 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003811 int retval;
3812
3813 /* Nothing to do for write-only files */
3814 if (!(file->f_mode & FMODE_READ))
3815 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816
Ben Blum102a7752009-09-23 15:56:26 -07003817 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003818 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003819 if (retval)
3820 return retval;
3821 /* configure file information */
3822 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003823
Ben Blum102a7752009-09-23 15:56:26 -07003824 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003825 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003826 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003827 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003828 }
Ben Blum102a7752009-09-23 15:56:26 -07003829 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003830 return 0;
3831}
Ben Blum102a7752009-09-23 15:56:26 -07003832static int cgroup_tasks_open(struct inode *unused, struct file *file)
3833{
Ben Blum72a8cb32009-09-23 15:56:27 -07003834 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003835}
3836static int cgroup_procs_open(struct inode *unused, struct file *file)
3837{
Ben Blum72a8cb32009-09-23 15:56:27 -07003838 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003839}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003840
Paul Menagebd89aab2007-10-18 23:40:44 -07003841static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003842 struct cftype *cft)
3843{
Paul Menagebd89aab2007-10-18 23:40:44 -07003844 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003845}
3846
Paul Menage6379c102008-07-25 01:47:01 -07003847static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3848 struct cftype *cft,
3849 u64 val)
3850{
3851 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3852 if (val)
3853 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3854 else
3855 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3856 return 0;
3857}
3858
Paul Menagebbcb81d2007-10-18 23:39:32 -07003859/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003860 * When dput() is called asynchronously, if umount has been done and
3861 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3862 * there's a small window that vfs will see the root dentry with non-zero
3863 * refcnt and trigger BUG().
3864 *
3865 * That's why we hold a reference before dput() and drop it right after.
3866 */
3867static void cgroup_dput(struct cgroup *cgrp)
3868{
3869 struct super_block *sb = cgrp->root->sb;
3870
3871 atomic_inc(&sb->s_active);
3872 dput(cgrp->dentry);
3873 deactivate_super(sb);
3874}
3875
3876/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003877 * Unregister event and free resources.
3878 *
3879 * Gets called from workqueue.
3880 */
3881static void cgroup_event_remove(struct work_struct *work)
3882{
3883 struct cgroup_event *event = container_of(work, struct cgroup_event,
3884 remove);
3885 struct cgroup *cgrp = event->cgrp;
3886
Li Zefan810cbee2013-02-18 18:56:14 +08003887 remove_wait_queue(event->wqh, &event->wait);
3888
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003889 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3890
Li Zefan810cbee2013-02-18 18:56:14 +08003891 /* Notify userspace the event is going away. */
3892 eventfd_signal(event->eventfd, 1);
3893
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003894 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003895 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003896 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003897}
3898
3899/*
3900 * Gets called on POLLHUP on eventfd when user closes it.
3901 *
3902 * Called with wqh->lock held and interrupts disabled.
3903 */
3904static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3905 int sync, void *key)
3906{
3907 struct cgroup_event *event = container_of(wait,
3908 struct cgroup_event, wait);
3909 struct cgroup *cgrp = event->cgrp;
3910 unsigned long flags = (unsigned long)key;
3911
3912 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003913 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003914 * If the event has been detached at cgroup removal, we
3915 * can simply return knowing the other side will cleanup
3916 * for us.
3917 *
3918 * We can't race against event freeing since the other
3919 * side will require wqh->lock via remove_wait_queue(),
3920 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003921 */
Li Zefan810cbee2013-02-18 18:56:14 +08003922 spin_lock(&cgrp->event_list_lock);
3923 if (!list_empty(&event->list)) {
3924 list_del_init(&event->list);
3925 /*
3926 * We are in atomic context, but cgroup_event_remove()
3927 * may sleep, so we have to call it in workqueue.
3928 */
3929 schedule_work(&event->remove);
3930 }
3931 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003932 }
3933
3934 return 0;
3935}
3936
3937static void cgroup_event_ptable_queue_proc(struct file *file,
3938 wait_queue_head_t *wqh, poll_table *pt)
3939{
3940 struct cgroup_event *event = container_of(pt,
3941 struct cgroup_event, pt);
3942
3943 event->wqh = wqh;
3944 add_wait_queue(wqh, &event->wait);
3945}
3946
3947/*
3948 * Parse input and register new cgroup event handler.
3949 *
3950 * Input must be in format '<event_fd> <control_fd> <args>'.
3951 * Interpretation of args is defined by control file implementation.
3952 */
3953static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3954 const char *buffer)
3955{
Li Zefan876ede82013-08-01 09:51:47 +08003956 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08003957 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003958 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08003959 struct file *efile;
3960 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003961 char *endp;
3962 int ret;
3963
3964 efd = simple_strtoul(buffer, &endp, 10);
3965 if (*endp != ' ')
3966 return -EINVAL;
3967 buffer = endp + 1;
3968
3969 cfd = simple_strtoul(buffer, &endp, 10);
3970 if ((*endp != ' ') && (*endp != '\0'))
3971 return -EINVAL;
3972 buffer = endp + 1;
3973
3974 event = kzalloc(sizeof(*event), GFP_KERNEL);
3975 if (!event)
3976 return -ENOMEM;
3977 event->cgrp = cgrp;
3978 INIT_LIST_HEAD(&event->list);
3979 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3980 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3981 INIT_WORK(&event->remove, cgroup_event_remove);
3982
3983 efile = eventfd_fget(efd);
3984 if (IS_ERR(efile)) {
3985 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08003986 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003987 }
3988
3989 event->eventfd = eventfd_ctx_fileget(efile);
3990 if (IS_ERR(event->eventfd)) {
3991 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08003992 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003993 }
3994
3995 cfile = fget(cfd);
3996 if (!cfile) {
3997 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08003998 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 }
4000
4001 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004002 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004003 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004004 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004005 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004006
4007 event->cft = __file_cft(cfile);
4008 if (IS_ERR(event->cft)) {
4009 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004010 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004011 }
4012
Li Zefanf1690072013-02-18 14:13:35 +08004013 /*
4014 * The file to be monitored must be in the same cgroup as
4015 * cgroup.event_control is.
4016 */
4017 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4018 if (cgrp_cfile != cgrp) {
4019 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004020 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004021 }
4022
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004023 if (!event->cft->register_event || !event->cft->unregister_event) {
4024 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004025 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004026 }
4027
4028 ret = event->cft->register_event(cgrp, event->cft,
4029 event->eventfd, buffer);
4030 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004031 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004032
Li Zefan7ef70e42013-04-26 11:58:03 -07004033 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004034
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004035 /*
4036 * Events should be removed after rmdir of cgroup directory, but before
4037 * destroying subsystem state objects. Let's take reference to cgroup
4038 * directory dentry to do that.
4039 */
4040 dget(cgrp->dentry);
4041
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004042 spin_lock(&cgrp->event_list_lock);
4043 list_add(&event->list, &cgrp->event_list);
4044 spin_unlock(&cgrp->event_list_lock);
4045
4046 fput(cfile);
4047 fput(efile);
4048
4049 return 0;
4050
Li Zefan876ede82013-08-01 09:51:47 +08004051out_put_cfile:
4052 fput(cfile);
4053out_put_eventfd:
4054 eventfd_ctx_put(event->eventfd);
4055out_put_efile:
4056 fput(efile);
4057out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004058 kfree(event);
4059
4060 return ret;
4061}
4062
Daniel Lezcano97978e62010-10-27 15:33:35 -07004063static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4064 struct cftype *cft)
4065{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004066 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004067}
4068
4069static int cgroup_clone_children_write(struct cgroup *cgrp,
4070 struct cftype *cft,
4071 u64 val)
4072{
4073 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004074 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004075 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004076 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004077 return 0;
4078}
4079
Tejun Heod5c56ce2013-06-03 19:14:34 -07004080static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004081 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004082 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004083 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004084 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004085 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004086 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004087 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004088 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004089 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004090 .write_string = cgroup_write_event_control,
4091 .mode = S_IWUGO,
4092 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004093 {
4094 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004095 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004096 .read_u64 = cgroup_clone_children_read,
4097 .write_u64 = cgroup_clone_children_write,
4098 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004099 {
Tejun Heo873fe092013-04-14 20:15:26 -07004100 .name = "cgroup.sane_behavior",
4101 .flags = CFTYPE_ONLY_ON_ROOT,
4102 .read_seq_string = cgroup_sane_behavior_show,
4103 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004104
4105 /*
4106 * Historical crazy stuff. These don't have "cgroup." prefix and
4107 * don't exist if sane_behavior. If you're depending on these, be
4108 * prepared to be burned.
4109 */
4110 {
4111 .name = "tasks",
4112 .flags = CFTYPE_INSANE, /* use "procs" instead */
4113 .open = cgroup_tasks_open,
4114 .write_u64 = cgroup_tasks_write,
4115 .release = cgroup_pidlist_release,
4116 .mode = S_IRUGO | S_IWUSR,
4117 },
4118 {
4119 .name = "notify_on_release",
4120 .flags = CFTYPE_INSANE,
4121 .read_u64 = cgroup_read_notify_on_release,
4122 .write_u64 = cgroup_write_notify_on_release,
4123 },
Tejun Heo873fe092013-04-14 20:15:26 -07004124 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004125 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004126 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004127 .read_seq_string = cgroup_release_agent_show,
4128 .write_string = cgroup_release_agent_write,
4129 .max_write_len = PATH_MAX,
4130 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004131 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004132};
4133
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004134/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004135 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004136 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004137 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004138 *
4139 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004140 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004141static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004142{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004144 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004145
Tejun Heo8e3f6542012-04-01 12:09:55 -07004146 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004147 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004148 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004149
4150 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004151 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004152
Tejun Heobee55092013-06-28 16:24:11 -07004153 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004154 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004155 if (ret < 0)
4156 goto err;
4157 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004158 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004159
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004160 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004161 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004162 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004163 struct css_id *id = rcu_dereference_protected(css->id, true);
4164
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004165 /*
4166 * Update id->css pointer and make this css visible from
4167 * CSS ID functions. This pointer will be dereferened
4168 * from RCU-read-side without locks.
4169 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004170 if (id)
4171 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004172 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173
4174 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004175err:
4176 cgroup_clear_dir(cgrp, subsys_mask);
4177 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004178}
4179
Tejun Heo48ddbe12012-04-01 12:09:56 -07004180static void css_dput_fn(struct work_struct *work)
4181{
4182 struct cgroup_subsys_state *css =
4183 container_of(work, struct cgroup_subsys_state, dput_work);
4184
Li Zefan1c8158e2013-06-18 18:41:10 +08004185 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004186}
4187
Tejun Heod3daf282013-06-13 19:39:16 -07004188static void css_release(struct percpu_ref *ref)
4189{
4190 struct cgroup_subsys_state *css =
4191 container_of(ref, struct cgroup_subsys_state, refcnt);
4192
4193 schedule_work(&css->dput_work);
4194}
4195
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196static void init_cgroup_css(struct cgroup_subsys_state *css,
4197 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004198 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199{
Paul Menagebd89aab2007-10-18 23:40:44 -07004200 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004201 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004202 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004203 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004204 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004205 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004206 BUG_ON(cgrp->subsys[ss->subsys_id]);
4207 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004208
4209 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004210 * css holds an extra ref to @cgrp->dentry which is put on the last
4211 * css_put(). dput() requires process context, which css_put() may
4212 * be called without. @css->dput_work will be used to invoke
4213 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004214 */
4215 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004216}
4217
Li Zefan2a4ac632013-07-31 16:16:40 +08004218/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004219static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004220{
Tejun Heoeb954192013-08-08 20:11:23 -04004221 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heob1929db2012-11-19 08:13:38 -08004222 int ret = 0;
4223
Tejun Heoa31f2d32012-11-19 08:13:37 -08004224 lockdep_assert_held(&cgroup_mutex);
4225
Tejun Heo92fb9742012-11-19 08:13:38 -08004226 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004227 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004228 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004229 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004230 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004231}
4232
Li Zefan2a4ac632013-07-31 16:16:40 +08004233/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004234static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004235{
4236 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4237
4238 lockdep_assert_held(&cgroup_mutex);
4239
4240 if (!(css->flags & CSS_ONLINE))
4241 return;
4242
Li Zefand7eeac12013-03-12 15:35:59 -07004243 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004244 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004245
Tejun Heoeb954192013-08-08 20:11:23 -04004246 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004247}
4248
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249/*
Li Zefana043e3b2008-02-23 15:24:09 -08004250 * cgroup_create - create a cgroup
4251 * @parent: cgroup that will be parent of the new cgroup
4252 * @dentry: dentry of the new cgroup
4253 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004254 *
Li Zefana043e3b2008-02-23 15:24:09 -08004255 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004258 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259{
Paul Menagebd89aab2007-10-18 23:40:44 -07004260 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004261 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262 struct cgroupfs_root *root = parent->root;
4263 int err = 0;
4264 struct cgroup_subsys *ss;
4265 struct super_block *sb = root->sb;
4266
Tejun Heo0a950f62012-11-19 09:02:12 -08004267 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004268 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4269 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004270 return -ENOMEM;
4271
Li Zefan65dff752013-03-01 15:01:56 +08004272 name = cgroup_alloc_name(dentry);
4273 if (!name)
4274 goto err_free_cgrp;
4275 rcu_assign_pointer(cgrp->name, name);
4276
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004277 /*
4278 * Temporarily set the pointer to NULL, so idr_find() won't return
4279 * a half-baked cgroup.
4280 */
4281 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004282 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004283 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004284
Tejun Heo976c06b2012-11-05 09:16:59 -08004285 /*
4286 * Only live parents can have children. Note that the liveliness
4287 * check isn't strictly necessary because cgroup_mkdir() and
4288 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4289 * anyway so that locking is contained inside cgroup proper and we
4290 * don't get nasty surprises if we ever grow another caller.
4291 */
4292 if (!cgroup_lock_live_group(parent)) {
4293 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004294 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004295 }
4296
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297 /* Grab a reference on the superblock so the hierarchy doesn't
4298 * get deleted on unmount if there are child cgroups. This
4299 * can be done outside cgroup_mutex, since the sb can't
4300 * disappear while someone has an open control file on the
4301 * fs */
4302 atomic_inc(&sb->s_active);
4303
Paul Menagecc31edc2008-10-18 20:28:04 -07004304 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305
Li Zefanfe1c06c2013-01-24 14:30:22 +08004306 dentry->d_fsdata = cgrp;
4307 cgrp->dentry = dentry;
4308
Paul Menagebd89aab2007-10-18 23:40:44 -07004309 cgrp->parent = parent;
4310 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311
Li Zefanb6abdb02008-03-04 14:28:19 -08004312 if (notify_on_release(parent))
4313 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4314
Tejun Heo2260e7f2012-11-19 08:13:38 -08004315 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4316 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004317
Tejun Heo5549c492013-06-24 15:21:48 -07004318 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004319 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004320
Tejun Heoeb954192013-08-08 20:11:23 -04004321 css = ss->css_alloc(parent->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322 if (IS_ERR(css)) {
4323 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004324 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325 }
Tejun Heod3daf282013-06-13 19:39:16 -07004326
4327 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004328 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004329 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004330 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004331 }
Tejun Heod3daf282013-06-13 19:39:16 -07004332
Paul Menagebd89aab2007-10-18 23:40:44 -07004333 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004334
Li Zefan4528fd02010-02-02 13:44:10 -08004335 if (ss->use_id) {
4336 err = alloc_css_id(ss, parent, cgrp);
4337 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004338 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004339 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 }
4341
Tejun Heo4e139af2012-11-19 08:13:36 -08004342 /*
4343 * Create directory. cgroup_create_file() returns with the new
4344 * directory locked on success so that it can be populated without
4345 * dropping cgroup_mutex.
4346 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004347 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004349 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004350 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351
Tejun Heo00356bd2013-06-18 11:14:22 -07004352 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004353
Tejun Heo4e139af2012-11-19 08:13:36 -08004354 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004355 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4356 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004357
Tejun Heob1929db2012-11-19 08:13:38 -08004358 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004359 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004360 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004361
Li Zefan415cf072013-04-08 14:35:02 +08004362 /* hold a ref to the parent's dentry */
4363 dget(parent->dentry);
4364
Tejun Heob1929db2012-11-19 08:13:38 -08004365 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004366 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004367 err = online_css(ss, cgrp);
4368 if (err)
4369 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004370
4371 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4372 parent->parent) {
4373 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",
4374 current->comm, current->pid, ss->name);
4375 if (!strcmp(ss->name, "memory"))
4376 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4377 ss->warned_broken_hierarchy = true;
4378 }
Tejun Heoa8638032012-11-09 09:12:29 -08004379 }
4380
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004381 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4382
Tejun Heo2bb566c2013-08-08 20:11:23 -04004383 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004384 if (err)
4385 goto err_destroy;
4386
4387 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004388 if (err)
4389 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390
4391 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004392 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393
4394 return 0;
4395
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004396err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004397 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004398 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4399
4400 if (css) {
4401 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004402 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004403 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004405 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004406 /* Release the reference count that we took on the superblock */
4407 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004408err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004409 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004410err_free_name:
4411 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004412err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004413 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004414 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004415
4416err_destroy:
4417 cgroup_destroy_locked(cgrp);
4418 mutex_unlock(&cgroup_mutex);
4419 mutex_unlock(&dentry->d_inode->i_mutex);
4420 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004421}
4422
Al Viro18bb1db2011-07-26 01:41:39 -04004423static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004424{
4425 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4426
4427 /* the vfs holds inode->i_mutex already */
4428 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4429}
4430
Tejun Heod3daf282013-06-13 19:39:16 -07004431static void cgroup_css_killed(struct cgroup *cgrp)
4432{
4433 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4434 return;
4435
4436 /* percpu ref's of all css's are killed, kick off the next step */
4437 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4438 schedule_work(&cgrp->destroy_work);
4439}
4440
4441static void css_ref_killed_fn(struct percpu_ref *ref)
4442{
4443 struct cgroup_subsys_state *css =
4444 container_of(ref, struct cgroup_subsys_state, refcnt);
4445
4446 cgroup_css_killed(css->cgroup);
4447}
4448
4449/**
4450 * cgroup_destroy_locked - the first stage of cgroup destruction
4451 * @cgrp: cgroup to be destroyed
4452 *
4453 * css's make use of percpu refcnts whose killing latency shouldn't be
4454 * exposed to userland and are RCU protected. Also, cgroup core needs to
4455 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4456 * invoked. To satisfy all the requirements, destruction is implemented in
4457 * the following two steps.
4458 *
4459 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4460 * userland visible parts and start killing the percpu refcnts of
4461 * css's. Set up so that the next stage will be kicked off once all
4462 * the percpu refcnts are confirmed to be killed.
4463 *
4464 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4465 * rest of destruction. Once all cgroup references are gone, the
4466 * cgroup is RCU-freed.
4467 *
4468 * This function implements s1. After this step, @cgrp is gone as far as
4469 * the userland is concerned and a new cgroup with the same name may be
4470 * created. As cgroup doesn't care about the names internally, this
4471 * doesn't cause any problem.
4472 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004473static int cgroup_destroy_locked(struct cgroup *cgrp)
4474 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004475{
Tejun Heo42809dd2012-11-19 08:13:37 -08004476 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004477 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004478 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004479 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004480
Tejun Heo42809dd2012-11-19 08:13:37 -08004481 lockdep_assert_held(&d->d_inode->i_mutex);
4482 lockdep_assert_held(&cgroup_mutex);
4483
Tejun Heoddd69142013-06-12 21:04:54 -07004484 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004485 * css_set_lock synchronizes access to ->cset_links and prevents
4486 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004487 */
4488 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004489 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004490 read_unlock(&css_set_lock);
4491 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004493
Tejun Heo1a90dd52012-11-05 09:16:59 -08004494 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004495 * Block new css_tryget() by killing css refcnts. cgroup core
4496 * guarantees that, by the time ->css_offline() is invoked, no new
4497 * css reference will be given out via css_tryget(). We can't
4498 * simply call percpu_ref_kill() and proceed to offlining css's
4499 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4500 * as killed on all CPUs on return.
4501 *
4502 * Use percpu_ref_kill_and_confirm() to get notifications as each
4503 * css is confirmed to be seen as killed on all CPUs. The
4504 * notification callback keeps track of the number of css's to be
4505 * killed and schedules cgroup_offline_fn() to perform the rest of
4506 * destruction once the percpu refs of all css's are confirmed to
4507 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004508 */
Tejun Heod3daf282013-06-13 19:39:16 -07004509 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004510 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004511 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4512
Tejun Heod3daf282013-06-13 19:39:16 -07004513 /*
4514 * Killing would put the base ref, but we need to keep it
4515 * alive until after ->css_offline.
4516 */
4517 percpu_ref_get(&css->refcnt);
4518
4519 atomic_inc(&cgrp->css_kill_cnt);
4520 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004521 }
Tejun Heod3daf282013-06-13 19:39:16 -07004522 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004523
4524 /*
4525 * Mark @cgrp dead. This prevents further task migration and child
4526 * creation by disabling cgroup_lock_live_group(). Note that
4527 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4528 * resume iteration after dropping RCU read lock. See
4529 * cgroup_next_sibling() for details.
4530 */
Tejun Heo54766d42013-06-12 21:04:53 -07004531 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004532
Tejun Heo455050d2013-06-13 19:27:41 -07004533 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4534 raw_spin_lock(&release_list_lock);
4535 if (!list_empty(&cgrp->release_list))
4536 list_del_init(&cgrp->release_list);
4537 raw_spin_unlock(&release_list_lock);
4538
4539 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004540 * Clear and remove @cgrp directory. The removal puts the base ref
4541 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004542 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004543 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004544 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004545 dget(d);
4546 cgroup_d_remove_dir(d);
4547
4548 /*
4549 * Unregister events and notify userspace.
4550 * Notify userspace about cgroup removing only after rmdir of cgroup
4551 * directory to avoid race between userspace and kernelspace.
4552 */
4553 spin_lock(&cgrp->event_list_lock);
4554 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4555 list_del_init(&event->list);
4556 schedule_work(&event->remove);
4557 }
4558 spin_unlock(&cgrp->event_list_lock);
4559
Tejun Heoea15f8c2013-06-13 19:27:42 -07004560 return 0;
4561};
4562
Tejun Heod3daf282013-06-13 19:39:16 -07004563/**
4564 * cgroup_offline_fn - the second step of cgroup destruction
4565 * @work: cgroup->destroy_free_work
4566 *
4567 * This function is invoked from a work item for a cgroup which is being
4568 * destroyed after the percpu refcnts of all css's are guaranteed to be
4569 * seen as killed on all CPUs, and performs the rest of destruction. This
4570 * is the second step of destruction described in the comment above
4571 * cgroup_destroy_locked().
4572 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004573static void cgroup_offline_fn(struct work_struct *work)
4574{
4575 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4576 struct cgroup *parent = cgrp->parent;
4577 struct dentry *d = cgrp->dentry;
4578 struct cgroup_subsys *ss;
4579
4580 mutex_lock(&cgroup_mutex);
4581
Tejun Heod3daf282013-06-13 19:39:16 -07004582 /*
4583 * css_tryget() is guaranteed to fail now. Tell subsystems to
4584 * initate destruction.
4585 */
Tejun Heo5549c492013-06-24 15:21:48 -07004586 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004587 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004588
4589 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004590 * Put the css refs from cgroup_destroy_locked(). Each css holds
4591 * an extra reference to the cgroup's dentry and cgroup removal
4592 * proceeds regardless of css refs. On the last put of each css,
4593 * whenever that may be, the extra dentry ref is put so that dentry
4594 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004595 */
Tejun Heo5549c492013-06-24 15:21:48 -07004596 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004597 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004598
Paul Menage999cd8a2009-01-07 18:08:36 -08004599 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004600 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004601
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004602 /*
4603 * We should remove the cgroup object from idr before its grace
4604 * period starts, so we won't be looking up a cgroup while the
4605 * cgroup is being freed.
4606 */
4607 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4608 cgrp->id = -1;
4609
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004611
Paul Menagebd89aab2007-10-18 23:40:44 -07004612 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004613 check_for_release(parent);
4614
Tejun Heoea15f8c2013-06-13 19:27:42 -07004615 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616}
4617
Tejun Heo42809dd2012-11-19 08:13:37 -08004618static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4619{
4620 int ret;
4621
4622 mutex_lock(&cgroup_mutex);
4623 ret = cgroup_destroy_locked(dentry->d_fsdata);
4624 mutex_unlock(&cgroup_mutex);
4625
4626 return ret;
4627}
4628
Tejun Heo8e3f6542012-04-01 12:09:55 -07004629static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4630{
4631 INIT_LIST_HEAD(&ss->cftsets);
4632
4633 /*
4634 * base_cftset is embedded in subsys itself, no need to worry about
4635 * deregistration.
4636 */
4637 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004638 struct cftype *cft;
4639
4640 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4641 cft->ss = ss;
4642
Tejun Heo8e3f6542012-04-01 12:09:55 -07004643 ss->base_cftset.cfts = ss->base_cftypes;
4644 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4645 }
4646}
4647
Li Zefan06a11922008-04-29 01:00:07 -07004648static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004649{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004651
4652 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004653
Tejun Heo648bb562012-11-19 08:13:36 -08004654 mutex_lock(&cgroup_mutex);
4655
Tejun Heo8e3f6542012-04-01 12:09:55 -07004656 /* init base cftset */
4657 cgroup_init_cftsets(ss);
4658
Paul Menageddbcc7e2007-10-18 23:39:30 -07004659 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004660 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4661 ss->root = &cgroup_dummy_root;
Tejun Heoeb954192013-08-08 20:11:23 -04004662 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004663 /* We don't handle early failures gracefully */
4664 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004665 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666
Li Zefane8d55fd2008-04-29 01:00:13 -07004667 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004668 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004669 * newly registered, all tasks and hence the
4670 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004671 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004672
4673 need_forkexit_callback |= ss->fork || ss->exit;
4674
Li Zefane8d55fd2008-04-29 01:00:13 -07004675 /* At system boot, before all subsystems have been
4676 * registered, no tasks have been forked, so we don't
4677 * need to invoke fork callbacks here. */
4678 BUG_ON(!list_empty(&init_task.tasks));
4679
Tejun Heo9871bf92013-06-24 15:21:47 -07004680 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004681
Tejun Heo648bb562012-11-19 08:13:36 -08004682 mutex_unlock(&cgroup_mutex);
4683
Ben Blume6a11052010-03-10 15:22:09 -08004684 /* this function shouldn't be used with modular subsystems, since they
4685 * need to register a subsys_id, among other things */
4686 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687}
4688
4689/**
Ben Blume6a11052010-03-10 15:22:09 -08004690 * cgroup_load_subsys: load and register a modular subsystem at runtime
4691 * @ss: the subsystem to load
4692 *
4693 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004694 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004695 * up for use. If the subsystem is built-in anyway, work is delegated to the
4696 * simpler cgroup_init_subsys.
4697 */
4698int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4699{
Ben Blume6a11052010-03-10 15:22:09 -08004700 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004701 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004702 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004703 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004704 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004705
4706 /* check name and function validity */
4707 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004708 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004709 return -EINVAL;
4710
4711 /*
4712 * we don't support callbacks in modular subsystems. this check is
4713 * before the ss->module check for consistency; a subsystem that could
4714 * be a module should still have no callbacks even if the user isn't
4715 * compiling it as one.
4716 */
4717 if (ss->fork || ss->exit)
4718 return -EINVAL;
4719
4720 /*
4721 * an optionally modular subsystem is built-in: we want to do nothing,
4722 * since cgroup_init_subsys will have already taken care of it.
4723 */
4724 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004725 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004726 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004727 return 0;
4728 }
4729
Tejun Heo8e3f6542012-04-01 12:09:55 -07004730 /* init base cftset */
4731 cgroup_init_cftsets(ss);
4732
Ben Blume6a11052010-03-10 15:22:09 -08004733 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004734 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004735
4736 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004737 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004738 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004739 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004740 */
Tejun Heoeb954192013-08-08 20:11:23 -04004741 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Ben Blume6a11052010-03-10 15:22:09 -08004742 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004743 /* failure case - need to deassign the cgroup_subsys[] slot. */
4744 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004745 mutex_unlock(&cgroup_mutex);
4746 return PTR_ERR(css);
4747 }
4748
Tejun Heo9871bf92013-06-24 15:21:47 -07004749 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4750 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004751
4752 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004753 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004754 /* init_idr must be after init_cgroup_css because it sets css->id. */
4755 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004756 ret = cgroup_init_idr(ss, css);
4757 if (ret)
4758 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004759 }
4760
4761 /*
4762 * Now we need to entangle the css into the existing css_sets. unlike
4763 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4764 * will need a new pointer to it; done by iterating the css_set_table.
4765 * furthermore, modifying the existing css_sets will corrupt the hash
4766 * table state, so each changed css_set will need its hash recomputed.
4767 * this is all done under the css_set_lock.
4768 */
4769 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004770 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004771 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004772 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004773 continue;
4774 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004775 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004776 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004777 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004778 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004779 key = css_set_hash(cset->subsys);
4780 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004781 }
4782 write_unlock(&css_set_lock);
4783
Tejun Heo9871bf92013-06-24 15:21:47 -07004784 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004785 if (ret)
4786 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004787
Ben Blume6a11052010-03-10 15:22:09 -08004788 /* success! */
4789 mutex_unlock(&cgroup_mutex);
4790 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004791
4792err_unload:
4793 mutex_unlock(&cgroup_mutex);
4794 /* @ss can't be mounted here as try_module_get() would fail */
4795 cgroup_unload_subsys(ss);
4796 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004797}
4798EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4799
4800/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004801 * cgroup_unload_subsys: unload a modular subsystem
4802 * @ss: the subsystem to unload
4803 *
4804 * This function should be called in a modular subsystem's exitcall. When this
4805 * function is invoked, the refcount on the subsystem's module will be 0, so
4806 * the subsystem will not be attached to any hierarchy.
4807 */
4808void cgroup_unload_subsys(struct cgroup_subsys *ss)
4809{
Tejun Heo69d02062013-06-12 21:04:50 -07004810 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004811
4812 BUG_ON(ss->module == NULL);
4813
4814 /*
4815 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004816 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004817 * doesn't start being used while we're killing it off.
4818 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004819 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004820
4821 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004822
Tejun Heo9871bf92013-06-24 15:21:47 -07004823 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004824
Tejun Heoc897ff62013-02-27 17:03:49 -08004825 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004826 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004827
Ben Blumcf5d5942010-03-10 15:22:09 -08004828 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004829 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004830
Tejun Heo9871bf92013-06-24 15:21:47 -07004831 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004832 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004833
4834 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004835 * disentangle the css from all css_sets attached to the dummy
4836 * top. as in loading, we need to pay our respects to the hashtable
4837 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004838 */
4839 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004840 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004841 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004842 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004843
Tejun Heo5abb8852013-06-12 21:04:49 -07004844 hash_del(&cset->hlist);
4845 cset->subsys[ss->subsys_id] = NULL;
4846 key = css_set_hash(cset->subsys);
4847 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004848 }
4849 write_unlock(&css_set_lock);
4850
4851 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004852 * remove subsystem's css from the cgroup_dummy_top and free it -
4853 * need to free before marking as null because ss->css_free needs
4854 * the cgrp->subsys pointer to find their state. note that this
4855 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004856 */
Tejun Heoeb954192013-08-08 20:11:23 -04004857 ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
Tejun Heo9871bf92013-06-24 15:21:47 -07004858 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004859
4860 mutex_unlock(&cgroup_mutex);
4861}
4862EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4863
4864/**
Li Zefana043e3b2008-02-23 15:24:09 -08004865 * cgroup_init_early - cgroup initialization at system boot
4866 *
4867 * Initialize cgroups at system boot, and initialize any
4868 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004869 */
4870int __init cgroup_init_early(void)
4871{
Tejun Heo30159ec2013-06-25 11:53:37 -07004872 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004873 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004874
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004875 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004876 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004877 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004878 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004879 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004880 init_cgroup_root(&cgroup_dummy_root);
4881 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004882 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004883
Tejun Heo69d02062013-06-12 21:04:50 -07004884 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004885 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4886 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004887 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004888
Tejun Heo30159ec2013-06-25 11:53:37 -07004889 /* at bootup time, we don't worry about modular subsystems */
4890 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004891 BUG_ON(!ss->name);
4892 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004893 BUG_ON(!ss->css_alloc);
4894 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004895 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004896 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004897 ss->name, ss->subsys_id);
4898 BUG();
4899 }
4900
4901 if (ss->early_init)
4902 cgroup_init_subsys(ss);
4903 }
4904 return 0;
4905}
4906
4907/**
Li Zefana043e3b2008-02-23 15:24:09 -08004908 * cgroup_init - cgroup initialization
4909 *
4910 * Register cgroup filesystem and /proc file, and initialize
4911 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004912 */
4913int __init cgroup_init(void)
4914{
Tejun Heo30159ec2013-06-25 11:53:37 -07004915 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004916 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004917 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004918
4919 err = bdi_init(&cgroup_backing_dev_info);
4920 if (err)
4921 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004922
Tejun Heo30159ec2013-06-25 11:53:37 -07004923 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004924 if (!ss->early_init)
4925 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004926 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004927 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004928 }
4929
Tejun Heofa3ca072013-04-14 11:36:56 -07004930 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004931 mutex_lock(&cgroup_mutex);
4932 mutex_lock(&cgroup_root_mutex);
4933
Tejun Heo82fe9b02013-06-25 11:53:37 -07004934 /* Add init_css_set to the hash table */
4935 key = css_set_hash(init_css_set.subsys);
4936 hash_add(css_set_table, &init_css_set.hlist, key);
4937
Tejun Heofc76df72013-06-25 11:53:37 -07004938 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004939
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004940 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4941 0, 1, GFP_KERNEL);
4942 BUG_ON(err < 0);
4943
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004944 mutex_unlock(&cgroup_root_mutex);
4945 mutex_unlock(&cgroup_mutex);
4946
Greg KH676db4a2010-08-05 13:53:35 -07004947 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4948 if (!cgroup_kobj) {
4949 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004951 }
4952
4953 err = register_filesystem(&cgroup_fs_type);
4954 if (err < 0) {
4955 kobject_put(cgroup_kobj);
4956 goto out;
4957 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004958
Li Zefan46ae2202008-04-29 01:00:08 -07004959 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004960
Paul Menageddbcc7e2007-10-18 23:39:30 -07004961out:
Paul Menagea4243162007-10-18 23:39:35 -07004962 if (err)
4963 bdi_destroy(&cgroup_backing_dev_info);
4964
Paul Menageddbcc7e2007-10-18 23:39:30 -07004965 return err;
4966}
Paul Menageb4f48b62007-10-18 23:39:33 -07004967
Paul Menagea4243162007-10-18 23:39:35 -07004968/*
4969 * proc_cgroup_show()
4970 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4971 * - Used for /proc/<pid>/cgroup.
4972 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4973 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004974 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004975 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4976 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4977 * cgroup to top_cgroup.
4978 */
4979
4980/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004981int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004982{
4983 struct pid *pid;
4984 struct task_struct *tsk;
4985 char *buf;
4986 int retval;
4987 struct cgroupfs_root *root;
4988
4989 retval = -ENOMEM;
4990 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4991 if (!buf)
4992 goto out;
4993
4994 retval = -ESRCH;
4995 pid = m->private;
4996 tsk = get_pid_task(pid, PIDTYPE_PID);
4997 if (!tsk)
4998 goto out_free;
4999
5000 retval = 0;
5001
5002 mutex_lock(&cgroup_mutex);
5003
Li Zefane5f6a862009-01-07 18:07:41 -08005004 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005005 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005006 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005007 int count = 0;
5008
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005009 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005010 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005011 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005012 if (strlen(root->name))
5013 seq_printf(m, "%sname=%s", count ? "," : "",
5014 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005015 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005016 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005017 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005018 if (retval < 0)
5019 goto out_unlock;
5020 seq_puts(m, buf);
5021 seq_putc(m, '\n');
5022 }
5023
5024out_unlock:
5025 mutex_unlock(&cgroup_mutex);
5026 put_task_struct(tsk);
5027out_free:
5028 kfree(buf);
5029out:
5030 return retval;
5031}
5032
Paul Menagea4243162007-10-18 23:39:35 -07005033/* Display information about each subsystem and each hierarchy */
5034static int proc_cgroupstats_show(struct seq_file *m, void *v)
5035{
Tejun Heo30159ec2013-06-25 11:53:37 -07005036 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005037 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005038
Paul Menage8bab8dd2008-04-04 14:29:57 -07005039 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005040 /*
5041 * ideally we don't want subsystems moving around while we do this.
5042 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5043 * subsys/hierarchy state.
5044 */
Paul Menagea4243162007-10-18 23:39:35 -07005045 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005046
5047 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005048 seq_printf(m, "%s\t%d\t%d\t%d\n",
5049 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005050 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005051
Paul Menagea4243162007-10-18 23:39:35 -07005052 mutex_unlock(&cgroup_mutex);
5053 return 0;
5054}
5055
5056static int cgroupstats_open(struct inode *inode, struct file *file)
5057{
Al Viro9dce07f2008-03-29 03:07:28 +00005058 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005059}
5060
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005061static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005062 .open = cgroupstats_open,
5063 .read = seq_read,
5064 .llseek = seq_lseek,
5065 .release = single_release,
5066};
5067
Paul Menageb4f48b62007-10-18 23:39:33 -07005068/**
5069 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005070 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005071 *
5072 * Description: A task inherits its parent's cgroup at fork().
5073 *
5074 * A pointer to the shared css_set was automatically copied in
5075 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005076 * it was not made under the protection of RCU or cgroup_mutex, so
5077 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5078 * have already changed current->cgroups, allowing the previously
5079 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005080 *
5081 * At the point that cgroup_fork() is called, 'current' is the parent
5082 * task, and the passed argument 'child' points to the child task.
5083 */
5084void cgroup_fork(struct task_struct *child)
5085{
Tejun Heo9bb71302012-10-18 17:52:07 -07005086 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005087 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005088 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005089 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005090 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005091}
5092
5093/**
Li Zefana043e3b2008-02-23 15:24:09 -08005094 * cgroup_post_fork - called on a new task after adding it to the task list
5095 * @child: the task in question
5096 *
Tejun Heo5edee612012-10-16 15:03:14 -07005097 * Adds the task to the list running through its css_set if necessary and
5098 * call the subsystem fork() callbacks. Has to be after the task is
5099 * visible on the task list in case we race with the first call to
5100 * cgroup_iter_start() - to guarantee that the new task ends up on its
5101 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005102 */
Paul Menage817929e2007-10-18 23:39:36 -07005103void cgroup_post_fork(struct task_struct *child)
5104{
Tejun Heo30159ec2013-06-25 11:53:37 -07005105 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005106 int i;
5107
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005108 /*
5109 * use_task_css_set_links is set to 1 before we walk the tasklist
5110 * under the tasklist_lock and we read it here after we added the child
5111 * to the tasklist under the tasklist_lock as well. If the child wasn't
5112 * yet in the tasklist when we walked through it from
5113 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5114 * should be visible now due to the paired locking and barriers implied
5115 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5116 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5117 * lock on fork.
5118 */
Paul Menage817929e2007-10-18 23:39:36 -07005119 if (use_task_css_set_links) {
5120 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005121 task_lock(child);
5122 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005123 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005124 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005125 write_unlock(&css_set_lock);
5126 }
Tejun Heo5edee612012-10-16 15:03:14 -07005127
5128 /*
5129 * Call ss->fork(). This must happen after @child is linked on
5130 * css_set; otherwise, @child might change state between ->fork()
5131 * and addition to css_set.
5132 */
5133 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005134 /*
5135 * fork/exit callbacks are supported only for builtin
5136 * subsystems, and the builtin section of the subsys
5137 * array is immutable, so we don't need to lock the
5138 * subsys array here. On the other hand, modular section
5139 * of the array can be freed at module unload, so we
5140 * can't touch that.
5141 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005142 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005143 if (ss->fork)
5144 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005145 }
Paul Menage817929e2007-10-18 23:39:36 -07005146}
Tejun Heo5edee612012-10-16 15:03:14 -07005147
Paul Menage817929e2007-10-18 23:39:36 -07005148/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005149 * cgroup_exit - detach cgroup from exiting task
5150 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005151 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005152 *
5153 * Description: Detach cgroup from @tsk and release it.
5154 *
5155 * Note that cgroups marked notify_on_release force every task in
5156 * them to take the global cgroup_mutex mutex when exiting.
5157 * This could impact scaling on very large systems. Be reluctant to
5158 * use notify_on_release cgroups where very high task exit scaling
5159 * is required on large systems.
5160 *
5161 * the_top_cgroup_hack:
5162 *
5163 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5164 *
5165 * We call cgroup_exit() while the task is still competent to
5166 * handle notify_on_release(), then leave the task attached to the
5167 * root cgroup in each hierarchy for the remainder of its exit.
5168 *
5169 * To do this properly, we would increment the reference count on
5170 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5171 * code we would add a second cgroup function call, to drop that
5172 * reference. This would just create an unnecessary hot spot on
5173 * the top_cgroup reference count, to no avail.
5174 *
5175 * Normally, holding a reference to a cgroup without bumping its
5176 * count is unsafe. The cgroup could go away, or someone could
5177 * attach us to a different cgroup, decrementing the count on
5178 * the first cgroup that we never incremented. But in this case,
5179 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005180 * which wards off any cgroup_attach_task() attempts, or task is a failed
5181 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005182 */
5183void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5184{
Tejun Heo30159ec2013-06-25 11:53:37 -07005185 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005186 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005187 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005188
5189 /*
5190 * Unlink from the css_set task list if necessary.
5191 * Optimistically check cg_list before taking
5192 * css_set_lock
5193 */
5194 if (!list_empty(&tsk->cg_list)) {
5195 write_lock(&css_set_lock);
5196 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005197 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005198 write_unlock(&css_set_lock);
5199 }
5200
Paul Menageb4f48b62007-10-18 23:39:33 -07005201 /* Reassign the task to the init_css_set. */
5202 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005203 cset = task_css_set(tsk);
5204 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005205
5206 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005207 /*
5208 * fork/exit callbacks are supported only for builtin
5209 * subsystems, see cgroup_post_fork() for details.
5210 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005211 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005212 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005213 struct cgroup_subsys_state *old_css = cset->subsys[i];
5214 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005215
Tejun Heoeb954192013-08-08 20:11:23 -04005216 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005217 }
5218 }
5219 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005220 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005221
Tejun Heo5abb8852013-06-12 21:04:49 -07005222 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005223}
Paul Menage697f4162007-10-18 23:39:34 -07005224
Paul Menagebd89aab2007-10-18 23:40:44 -07005225static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005226{
Li Zefanf50daa72013-03-01 15:06:07 +08005227 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005228 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005229 /*
5230 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005231 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005232 * it now
5233 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005234 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005235
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005236 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005237 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005238 list_empty(&cgrp->release_list)) {
5239 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005240 need_schedule_work = 1;
5241 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005242 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005243 if (need_schedule_work)
5244 schedule_work(&release_agent_work);
5245 }
5246}
5247
Paul Menage81a6a5c2007-10-18 23:39:38 -07005248/*
5249 * Notify userspace when a cgroup is released, by running the
5250 * configured release agent with the name of the cgroup (path
5251 * relative to the root of cgroup file system) as the argument.
5252 *
5253 * Most likely, this user command will try to rmdir this cgroup.
5254 *
5255 * This races with the possibility that some other task will be
5256 * attached to this cgroup before it is removed, or that some other
5257 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5258 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5259 * unused, and this cgroup will be reprieved from its death sentence,
5260 * to continue to serve a useful existence. Next time it's released,
5261 * we will get notified again, if it still has 'notify_on_release' set.
5262 *
5263 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5264 * means only wait until the task is successfully execve()'d. The
5265 * separate release agent task is forked by call_usermodehelper(),
5266 * then control in this thread returns here, without waiting for the
5267 * release agent task. We don't bother to wait because the caller of
5268 * this routine has no use for the exit status of the release agent
5269 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005270 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005271static void cgroup_release_agent(struct work_struct *work)
5272{
5273 BUG_ON(work != &release_agent_work);
5274 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005275 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005276 while (!list_empty(&release_list)) {
5277 char *argv[3], *envp[3];
5278 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005279 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005280 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005281 struct cgroup,
5282 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005283 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005284 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005285 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005286 if (!pathbuf)
5287 goto continue_free;
5288 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5289 goto continue_free;
5290 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5291 if (!agentbuf)
5292 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005293
5294 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005295 argv[i++] = agentbuf;
5296 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005297 argv[i] = NULL;
5298
5299 i = 0;
5300 /* minimal command environment */
5301 envp[i++] = "HOME=/";
5302 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5303 envp[i] = NULL;
5304
5305 /* Drop the lock while we invoke the usermode helper,
5306 * since the exec could involve hitting disk and hence
5307 * be a slow process */
5308 mutex_unlock(&cgroup_mutex);
5309 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005310 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005311 continue_free:
5312 kfree(pathbuf);
5313 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005314 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005315 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005316 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005317 mutex_unlock(&cgroup_mutex);
5318}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005319
5320static int __init cgroup_disable(char *str)
5321{
Tejun Heo30159ec2013-06-25 11:53:37 -07005322 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005323 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005324 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005325
5326 while ((token = strsep(&str, ",")) != NULL) {
5327 if (!*token)
5328 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005329
Tejun Heo30159ec2013-06-25 11:53:37 -07005330 /*
5331 * cgroup_disable, being at boot time, can't know about
5332 * module subsystems, so we don't worry about them.
5333 */
5334 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005335 if (!strcmp(token, ss->name)) {
5336 ss->disabled = 1;
5337 printk(KERN_INFO "Disabling %s control group"
5338 " subsystem\n", ss->name);
5339 break;
5340 }
5341 }
5342 }
5343 return 1;
5344}
5345__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005346
5347/*
5348 * Functons for CSS ID.
5349 */
5350
Tejun Heo54766d42013-06-12 21:04:53 -07005351/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352unsigned short css_id(struct cgroup_subsys_state *css)
5353{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005354 struct css_id *cssid;
5355
5356 /*
5357 * This css_id() can return correct value when somone has refcnt
5358 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5359 * it's unchanged until freed.
5360 */
Tejun Heod3daf282013-06-13 19:39:16 -07005361 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005362
5363 if (cssid)
5364 return cssid->id;
5365 return 0;
5366}
Ben Blum67523c42010-03-10 15:22:11 -08005367EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005368
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005369/**
5370 * css_is_ancestor - test "root" css is an ancestor of "child"
5371 * @child: the css to be tested.
5372 * @root: the css supporsed to be an ancestor of the child.
5373 *
5374 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005375 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005376 * But, considering usual usage, the csses should be valid objects after test.
5377 * Assuming that the caller will do some action to the child if this returns
5378 * returns true, the caller must take "child";s reference count.
5379 * If "child" is valid object and this returns true, "root" is valid, too.
5380 */
5381
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005382bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005383 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005384{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005385 struct css_id *child_id;
5386 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005387
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005388 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005389 if (!child_id)
5390 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005391 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005392 if (!root_id)
5393 return false;
5394 if (child_id->depth < root_id->depth)
5395 return false;
5396 if (child_id->stack[root_id->depth] != root_id->id)
5397 return false;
5398 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005399}
5400
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005401void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5402{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005403 struct css_id *id = rcu_dereference_protected(css->id, true);
5404
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005405 /* When this is called before css_id initialization, id can be NULL */
5406 if (!id)
5407 return;
5408
5409 BUG_ON(!ss->use_id);
5410
5411 rcu_assign_pointer(id->css, NULL);
5412 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005413 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005414 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005415 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005416 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417}
Ben Blum67523c42010-03-10 15:22:11 -08005418EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005419
5420/*
5421 * This is called by init or create(). Then, calls to this function are
5422 * always serialized (By cgroup_mutex() at create()).
5423 */
5424
5425static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5426{
5427 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005428 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429
5430 BUG_ON(!ss->use_id);
5431
5432 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5433 newid = kzalloc(size, GFP_KERNEL);
5434 if (!newid)
5435 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005436
5437 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005438 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005439 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005440 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005441 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005442 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005443
5444 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005445 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005447
Tejun Heod228d9e2013-02-27 17:04:54 -08005448 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005449 newid->depth = depth;
5450 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451err_out:
5452 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005453 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005454
5455}
5456
Ben Blume6a11052010-03-10 15:22:09 -08005457static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5458 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005459{
5460 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005461
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005462 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005463 idr_init(&ss->idr);
5464
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005465 newid = get_new_cssid(ss, 0);
5466 if (IS_ERR(newid))
5467 return PTR_ERR(newid);
5468
5469 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005470 RCU_INIT_POINTER(newid->css, rootcss);
5471 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005472 return 0;
5473}
5474
5475static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5476 struct cgroup *child)
5477{
5478 int subsys_id, i, depth = 0;
5479 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005480 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005481
5482 subsys_id = ss->subsys_id;
5483 parent_css = parent->subsys[subsys_id];
5484 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005485 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005486 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005487
5488 child_id = get_new_cssid(ss, depth);
5489 if (IS_ERR(child_id))
5490 return PTR_ERR(child_id);
5491
5492 for (i = 0; i < depth; i++)
5493 child_id->stack[i] = parent_id->stack[i];
5494 child_id->stack[depth] = child_id->id;
5495 /*
5496 * child_id->css pointer will be set after this cgroup is available
5497 * see cgroup_populate_dir()
5498 */
5499 rcu_assign_pointer(child_css->id, child_id);
5500
5501 return 0;
5502}
5503
5504/**
5505 * css_lookup - lookup css by id
5506 * @ss: cgroup subsys to be looked into.
5507 * @id: the id
5508 *
5509 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5510 * NULL if not. Should be called under rcu_read_lock()
5511 */
5512struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5513{
5514 struct css_id *cssid = NULL;
5515
5516 BUG_ON(!ss->use_id);
5517 cssid = idr_find(&ss->idr, id);
5518
5519 if (unlikely(!cssid))
5520 return NULL;
5521
5522 return rcu_dereference(cssid->css);
5523}
Ben Blum67523c42010-03-10 15:22:11 -08005524EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005525
Stephane Eraniane5d13672011-02-14 11:20:01 +02005526/*
5527 * get corresponding css from file open on cgroupfs directory
5528 */
5529struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5530{
5531 struct cgroup *cgrp;
5532 struct inode *inode;
5533 struct cgroup_subsys_state *css;
5534
Al Viro496ad9a2013-01-23 17:07:38 -05005535 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005536 /* check in cgroup filesystem dir */
5537 if (inode->i_op != &cgroup_dir_inode_operations)
5538 return ERR_PTR(-EBADF);
5539
5540 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5541 return ERR_PTR(-EINVAL);
5542
5543 /* get cgroup */
5544 cgrp = __d_cgrp(f->f_dentry);
5545 css = cgrp->subsys[id];
5546 return css ? css : ERR_PTR(-ENOENT);
5547}
5548
Paul Menagefe693432009-09-23 15:56:20 -07005549#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005550static struct cgroup_subsys_state *
5551debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005552{
5553 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5554
5555 if (!css)
5556 return ERR_PTR(-ENOMEM);
5557
5558 return css;
5559}
5560
Tejun Heoeb954192013-08-08 20:11:23 -04005561static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005562{
Tejun Heoeb954192013-08-08 20:11:23 -04005563 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005564}
5565
Li Zefan03c78cb2013-06-14 11:17:19 +08005566static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005567{
Li Zefan03c78cb2013-06-14 11:17:19 +08005568 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005569}
5570
Li Zefan03c78cb2013-06-14 11:17:19 +08005571static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005572{
5573 return (u64)(unsigned long)current->cgroups;
5574}
5575
Li Zefan03c78cb2013-06-14 11:17:19 +08005576static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5577 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005578{
5579 u64 count;
5580
5581 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005582 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005583 rcu_read_unlock();
5584 return count;
5585}
5586
Li Zefan03c78cb2013-06-14 11:17:19 +08005587static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005588 struct cftype *cft,
5589 struct seq_file *seq)
5590{
Tejun Heo69d02062013-06-12 21:04:50 -07005591 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005592 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005593
5594 read_lock(&css_set_lock);
5595 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005596 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005597 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005598 struct cgroup *c = link->cgrp;
5599 const char *name;
5600
5601 if (c->dentry)
5602 name = c->dentry->d_name.name;
5603 else
5604 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005605 seq_printf(seq, "Root %d group %s\n",
5606 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005607 }
5608 rcu_read_unlock();
5609 read_unlock(&css_set_lock);
5610 return 0;
5611}
5612
5613#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005614static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005615 struct cftype *cft,
5616 struct seq_file *seq)
5617{
Tejun Heo69d02062013-06-12 21:04:50 -07005618 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005619
5620 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005621 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005622 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005623 struct task_struct *task;
5624 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005625 seq_printf(seq, "css_set %p\n", cset);
5626 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005627 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5628 seq_puts(seq, " ...\n");
5629 break;
5630 } else {
5631 seq_printf(seq, " task %d\n",
5632 task_pid_vnr(task));
5633 }
5634 }
5635 }
5636 read_unlock(&css_set_lock);
5637 return 0;
5638}
5639
Paul Menagefe693432009-09-23 15:56:20 -07005640static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5641{
5642 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5643}
5644
5645static struct cftype debug_files[] = {
5646 {
Paul Menagefe693432009-09-23 15:56:20 -07005647 .name = "taskcount",
5648 .read_u64 = debug_taskcount_read,
5649 },
5650
5651 {
5652 .name = "current_css_set",
5653 .read_u64 = current_css_set_read,
5654 },
5655
5656 {
5657 .name = "current_css_set_refcount",
5658 .read_u64 = current_css_set_refcount_read,
5659 },
5660
5661 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005662 .name = "current_css_set_cg_links",
5663 .read_seq_string = current_css_set_cg_links_read,
5664 },
5665
5666 {
5667 .name = "cgroup_css_links",
5668 .read_seq_string = cgroup_css_links_read,
5669 },
5670
5671 {
Paul Menagefe693432009-09-23 15:56:20 -07005672 .name = "releasable",
5673 .read_u64 = releasable_read,
5674 },
Paul Menagefe693432009-09-23 15:56:20 -07005675
Tejun Heo4baf6e32012-04-01 12:09:55 -07005676 { } /* terminate */
5677};
Paul Menagefe693432009-09-23 15:56:20 -07005678
5679struct cgroup_subsys debug_subsys = {
5680 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005681 .css_alloc = debug_css_alloc,
5682 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005683 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005684 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005685};
5686#endif /* CONFIG_CGROUP_DEBUG */