blob: cef688128fb8dd793f0b49268ac83f20a29b383e [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heoea15f8c2013-06-13 19:27:42 -0700218static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800220static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
221 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700224static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225{
Tejun Heo54766d42013-06-12 21:04:53 -0700226 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227}
228
Li Zefan78574cf2013-04-08 19:00:38 -0700229/**
230 * cgroup_is_descendant - test ancestry
231 * @cgrp: the cgroup to be tested
232 * @ancestor: possible ancestor of @cgrp
233 *
234 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
235 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
236 * and @ancestor are accessible.
237 */
238bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
239{
240 while (cgrp) {
241 if (cgrp == ancestor)
242 return true;
243 cgrp = cgrp->parent;
244 }
245 return false;
246}
247EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248
Adrian Bunke9685a02008-02-07 00:13:46 -0800249static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700250{
251 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 (1 << CGRP_RELEASABLE) |
253 (1 << CGRP_NOTIFY_ON_RELEASE);
254 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255}
256
Adrian Bunke9685a02008-02-07 00:13:46 -0800257static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700258{
Paul Menagebd89aab2007-10-18 23:40:44 -0700259 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260}
261
Tejun Heo30159ec2013-06-25 11:53:37 -0700262/**
263 * for_each_subsys - iterate all loaded cgroup subsystems
264 * @ss: the iteration cursor
265 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
266 *
267 * Should be called under cgroup_mutex.
268 */
269#define for_each_subsys(ss, i) \
270 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
271 if (({ lockdep_assert_held(&cgroup_mutex); \
272 !((ss) = cgroup_subsys[i]); })) { } \
273 else
274
275/**
276 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
277 * @ss: the iteration cursor
278 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
279 *
280 * Bulit-in subsystems are always present and iteration itself doesn't
281 * require any synchronization.
282 */
283#define for_each_builtin_subsys(ss, i) \
284 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
285 (((ss) = cgroup_subsys[i]) || true); (i)++)
286
Tejun Heo5549c492013-06-24 15:21:48 -0700287/* iterate each subsystem attached to a hierarchy */
288#define for_each_root_subsys(root, ss) \
289 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700290
Tejun Heo5549c492013-06-24 15:21:48 -0700291/* iterate across the active hierarchies */
292#define for_each_active_root(root) \
293 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700294
Tejun Heof6ea9372012-04-01 12:09:55 -0700295static inline struct cgroup *__d_cgrp(struct dentry *dentry)
296{
297 return dentry->d_fsdata;
298}
299
Tejun Heo05ef1d72012-04-01 12:09:56 -0700300static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700301{
302 return dentry->d_fsdata;
303}
304
Tejun Heo05ef1d72012-04-01 12:09:56 -0700305static inline struct cftype *__d_cft(struct dentry *dentry)
306{
307 return __d_cfe(dentry)->type;
308}
309
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700310/**
311 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
312 * @cgrp: the cgroup to be checked for liveness
313 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700314 * On success, returns true; the mutex should be later unlocked. On
315 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700316 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700317static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318{
319 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700320 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700321 mutex_unlock(&cgroup_mutex);
322 return false;
323 }
324 return true;
325}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700326
Paul Menage81a6a5c2007-10-18 23:39:38 -0700327/* the list of cgroups eligible for automatic release. Protected by
328 * release_list_lock */
329static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200330static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700331static void cgroup_release_agent(struct work_struct *work);
332static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700333static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700334
Tejun Heo69d02062013-06-12 21:04:50 -0700335/*
336 * A cgroup can be associated with multiple css_sets as different tasks may
337 * belong to different cgroups on different hierarchies. In the other
338 * direction, a css_set is naturally associated with multiple cgroups.
339 * This M:N relationship is represented by the following link structure
340 * which exists for each association and allows traversing the associations
341 * from both sides.
342 */
343struct cgrp_cset_link {
344 /* the cgroup and css_set this link associates */
345 struct cgroup *cgrp;
346 struct css_set *cset;
347
348 /* list of cgrp_cset_links anchored at cgrp->cset_links */
349 struct list_head cset_link;
350
351 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
352 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700353};
354
355/* The default css_set - used by init and its children prior to any
356 * hierarchies being mounted. It contains a pointer to the root state
357 * for each subsystem. Also used to anchor the list of css_sets. Not
358 * reference-counted, to improve performance when child cgroups
359 * haven't been created.
360 */
361
362static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700363static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700364
Ben Blume6a11052010-03-10 15:22:09 -0800365static int cgroup_init_idr(struct cgroup_subsys *ss,
366 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700367
Paul Menage817929e2007-10-18 23:39:36 -0700368/* css_set_lock protects the list of css_set objects, and the
369 * chain of tasks off each css_set. Nests outside task->alloc_lock
370 * due to cgroup_iter_start() */
371static DEFINE_RWLOCK(css_set_lock);
372static int css_set_count;
373
Paul Menage7717f7b2009-09-23 15:56:22 -0700374/*
375 * hash table for cgroup groups. This improves the performance to find
376 * an existing css_set. This hash doesn't (currently) take into
377 * account cgroups in empty hierarchies.
378 */
Li Zefan472b1052008-04-29 01:00:11 -0700379#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800380static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700381
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700383{
Li Zefan0ac801f2013-01-10 11:49:27 +0800384 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700385 struct cgroup_subsys *ss;
386 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700387
Tejun Heo30159ec2013-06-25 11:53:37 -0700388 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800389 key += (unsigned long)css[i];
390 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700391
Li Zefan0ac801f2013-01-10 11:49:27 +0800392 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700393}
394
Paul Menage817929e2007-10-18 23:39:36 -0700395/* We don't maintain the lists running through each css_set to its
396 * task until after the first call to cgroup_iter_start(). This
397 * reduces the fork()/exit() overhead for people who have cgroups
398 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700399static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700400
Tejun Heo5abb8852013-06-12 21:04:49 -0700401static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700402{
Tejun Heo69d02062013-06-12 21:04:50 -0700403 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700404
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700405 /*
406 * Ensure that the refcount doesn't hit zero while any readers
407 * can see it. Similar to atomic_dec_and_lock(), but for an
408 * rwlock
409 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700410 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700411 return;
412 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700413 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700414 write_unlock(&css_set_lock);
415 return;
416 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700417
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700418 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 css_set_count--;
421
Tejun Heo69d02062013-06-12 21:04:50 -0700422 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700423 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700424
Tejun Heo69d02062013-06-12 21:04:50 -0700425 list_del(&link->cset_link);
426 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800427
Tejun Heoddd69142013-06-12 21:04:54 -0700428 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700429 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700431 set_bit(CGRP_RELEASABLE, &cgrp->flags);
432 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700434
435 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700437
438 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700440}
441
442/*
443 * refcounted get/put for css_set objects
444 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700445static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700446{
Tejun Heo5abb8852013-06-12 21:04:49 -0700447 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700448}
449
Tejun Heo5abb8852013-06-12 21:04:49 -0700450static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700451{
Tejun Heo5abb8852013-06-12 21:04:49 -0700452 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700453}
454
Tejun Heo5abb8852013-06-12 21:04:49 -0700455static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700456{
Tejun Heo5abb8852013-06-12 21:04:49 -0700457 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458}
459
Tejun Heob326f9d2013-06-24 15:21:48 -0700460/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700461 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700462 * @cset: candidate css_set being tested
463 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700464 * @new_cgrp: cgroup that's being entered by the task
465 * @template: desired set of css pointers in css_set (pre-calculated)
466 *
467 * Returns true if "cg" matches "old_cg" except for the hierarchy
468 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
469 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700470static bool compare_css_sets(struct css_set *cset,
471 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 struct cgroup *new_cgrp,
473 struct cgroup_subsys_state *template[])
474{
475 struct list_head *l1, *l2;
476
Tejun Heo5abb8852013-06-12 21:04:49 -0700477 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 /* Not all subsystems matched */
479 return false;
480 }
481
482 /*
483 * Compare cgroup pointers in order to distinguish between
484 * different cgroups in heirarchies with no subsystems. We
485 * could get by with just this check alone (and skip the
486 * memcmp above) but on most setups the memcmp check will
487 * avoid the need for this more expensive check on almost all
488 * candidates.
489 */
490
Tejun Heo69d02062013-06-12 21:04:50 -0700491 l1 = &cset->cgrp_links;
492 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700494 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700496
497 l1 = l1->next;
498 l2 = l2->next;
499 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700500 if (l1 == &cset->cgrp_links) {
501 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700502 break;
503 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700504 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700505 }
506 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700507 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
508 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
509 cgrp1 = link1->cgrp;
510 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700511 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700512 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700513
514 /*
515 * If this hierarchy is the hierarchy of the cgroup
516 * that's changing, then we need to check that this
517 * css_set points to the new cgroup; if it's any other
518 * hierarchy, then this css_set should point to the
519 * same cgroup as the old css_set.
520 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700521 if (cgrp1->root == new_cgrp->root) {
522 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700523 return false;
524 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700525 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700526 return false;
527 }
528 }
529 return true;
530}
531
Tejun Heob326f9d2013-06-24 15:21:48 -0700532/**
533 * find_existing_css_set - init css array and find the matching css_set
534 * @old_cset: the css_set that we're using before the cgroup transition
535 * @cgrp: the cgroup that we're moving into
536 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700537 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700538static struct css_set *find_existing_css_set(struct css_set *old_cset,
539 struct cgroup *cgrp,
540 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700541{
Paul Menagebd89aab2007-10-18 23:40:44 -0700542 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700543 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800545 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700546 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700547
Ben Blumaae8aab2010-03-10 15:22:07 -0800548 /*
549 * Build the set of subsystem state objects that we want to see in the
550 * new css_set. while subsystems can change globally, the entries here
551 * won't change, so no need for locking.
552 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700553 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400554 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700555 /* Subsystem is in this hierarchy. So we want
556 * the subsystem state from the new
557 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700558 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700559 } else {
560 /* Subsystem is not in this hierarchy, so we
561 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700562 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700563 }
564 }
565
Li Zefan0ac801f2013-01-10 11:49:27 +0800566 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700567 hash_for_each_possible(css_set_table, cset, hlist, key) {
568 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700569 continue;
570
571 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700572 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700573 }
Paul Menage817929e2007-10-18 23:39:36 -0700574
575 /* No existing cgroup group matched */
576 return NULL;
577}
578
Tejun Heo69d02062013-06-12 21:04:50 -0700579static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700580{
Tejun Heo69d02062013-06-12 21:04:50 -0700581 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700582
Tejun Heo69d02062013-06-12 21:04:50 -0700583 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
584 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700585 kfree(link);
586 }
587}
588
Tejun Heo69d02062013-06-12 21:04:50 -0700589/**
590 * allocate_cgrp_cset_links - allocate cgrp_cset_links
591 * @count: the number of links to allocate
592 * @tmp_links: list_head the allocated links are put on
593 *
594 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
595 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700596 */
Tejun Heo69d02062013-06-12 21:04:50 -0700597static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700598{
Tejun Heo69d02062013-06-12 21:04:50 -0700599 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700600 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700601
602 INIT_LIST_HEAD(tmp_links);
603
Li Zefan36553432008-07-29 22:33:19 -0700604 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700605 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700606 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700607 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700608 return -ENOMEM;
609 }
Tejun Heo69d02062013-06-12 21:04:50 -0700610 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700611 }
612 return 0;
613}
614
Li Zefanc12f65d2009-01-07 18:07:42 -0800615/**
616 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700617 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700618 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800619 * @cgrp: the destination cgroup
620 */
Tejun Heo69d02062013-06-12 21:04:50 -0700621static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
622 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800623{
Tejun Heo69d02062013-06-12 21:04:50 -0700624 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800625
Tejun Heo69d02062013-06-12 21:04:50 -0700626 BUG_ON(list_empty(tmp_links));
627 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
628 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700629 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700630 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 /*
632 * Always add links to the tail of the list so that the list
633 * is sorted by order of hierarchy creation
634 */
Tejun Heo69d02062013-06-12 21:04:50 -0700635 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800636}
637
Tejun Heob326f9d2013-06-24 15:21:48 -0700638/**
639 * find_css_set - return a new css_set with one cgroup updated
640 * @old_cset: the baseline css_set
641 * @cgrp: the cgroup to be updated
642 *
643 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
644 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700645 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700646static struct css_set *find_css_set(struct css_set *old_cset,
647 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700648{
Tejun Heob326f9d2013-06-24 15:21:48 -0700649 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700651 struct list_head tmp_links;
652 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800653 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700654
Tejun Heob326f9d2013-06-24 15:21:48 -0700655 lockdep_assert_held(&cgroup_mutex);
656
Paul Menage817929e2007-10-18 23:39:36 -0700657 /* First see if we already have a cgroup group that matches
658 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700659 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 cset = find_existing_css_set(old_cset, cgrp, template);
661 if (cset)
662 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700663 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700664
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 if (cset)
666 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700667
Tejun Heof4f4be22013-06-12 21:04:51 -0700668 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700669 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700670 return NULL;
671
Tejun Heo69d02062013-06-12 21:04:50 -0700672 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700673 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700674 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700675 return NULL;
676 }
677
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700679 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 INIT_LIST_HEAD(&cset->tasks);
681 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700682
683 /* Copy the set of subsystem state objects generated in
684 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700685 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700686
687 write_lock(&css_set_lock);
688 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700689 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700690 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700691
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 if (c->root == cgrp->root)
693 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700694 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700695 }
Paul Menage817929e2007-10-18 23:39:36 -0700696
Tejun Heo69d02062013-06-12 21:04:50 -0700697 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700698
Paul Menage817929e2007-10-18 23:39:36 -0700699 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700700
701 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700702 key = css_set_hash(cset->subsys);
703 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700704
Paul Menage817929e2007-10-18 23:39:36 -0700705 write_unlock(&css_set_lock);
706
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700708}
709
Paul Menageddbcc7e2007-10-18 23:39:30 -0700710/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 * Return the cgroup for "task" from the given hierarchy. Must be
712 * called with cgroup_mutex held.
713 */
714static struct cgroup *task_cgroup_from_root(struct task_struct *task,
715 struct cgroupfs_root *root)
716{
Tejun Heo5abb8852013-06-12 21:04:49 -0700717 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 struct cgroup *res = NULL;
719
720 BUG_ON(!mutex_is_locked(&cgroup_mutex));
721 read_lock(&css_set_lock);
722 /*
723 * No need to lock the task - since we hold cgroup_mutex the
724 * task can't change groups, so the only thing that can happen
725 * is that it exits and its css is set back to init_css_set.
726 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700727 cset = task->cgroups;
728 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700729 res = &root->top_cgroup;
730 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700731 struct cgrp_cset_link *link;
732
733 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700734 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700735
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 if (c->root == root) {
737 res = c;
738 break;
739 }
740 }
741 }
742 read_unlock(&css_set_lock);
743 BUG_ON(!res);
744 return res;
745}
746
747/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700748 * There is one global cgroup mutex. We also require taking
749 * task_lock() when dereferencing a task's cgroup subsys pointers.
750 * See "The task_lock() exception", at the end of this comment.
751 *
752 * A task must hold cgroup_mutex to modify cgroups.
753 *
754 * Any task can increment and decrement the count field without lock.
755 * So in general, code holding cgroup_mutex can't rely on the count
756 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800757 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700758 * means that no tasks are currently attached, therefore there is no
759 * way a task attached to that cgroup can fork (the other way to
760 * increment the count). So code holding cgroup_mutex can safely
761 * assume that if the count is zero, it will stay zero. Similarly, if
762 * a task holds cgroup_mutex on a cgroup with zero count, it
763 * knows that the cgroup won't be removed, as cgroup_rmdir()
764 * needs that mutex.
765 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
767 * (usually) take cgroup_mutex. These are the two most performance
768 * critical pieces of code here. The exception occurs on cgroup_exit(),
769 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
770 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800771 * to the release agent with the name of the cgroup (path relative to
772 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700773 *
774 * A cgroup can only be deleted if both its 'count' of using tasks
775 * is zero, and its list of 'children' cgroups is empty. Since all
776 * tasks in the system use _some_ cgroup, and since there is always at
777 * least one task in the system (init, pid == 1), therefore, top_cgroup
778 * always has either children cgroups and/or using tasks. So we don't
779 * need a special hack to ensure that top_cgroup cannot be deleted.
780 *
781 * The task_lock() exception
782 *
783 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800784 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800785 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 * several performance critical places that need to reference
787 * task->cgroup without the expense of grabbing a system global
788 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800789 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700790 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
791 * the task_struct routinely used for such matters.
792 *
793 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800794 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795 */
796
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797/*
798 * A couple of forward declarations required, due to cyclic reference loop:
799 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
800 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
801 * -> cgroup_mkdir.
802 */
803
Al Viro18bb1db2011-07-26 01:41:39 -0400804static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400805static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400807static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
808 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700809static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700810static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700811
812static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200813 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700814 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700815};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700817static int alloc_css_id(struct cgroup_subsys *ss,
818 struct cgroup *parent, struct cgroup *child);
819
Al Viroa5e7ed32011-07-26 01:55:55 -0400820static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821{
822 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823
824 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400825 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100827 inode->i_uid = current_fsuid();
828 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700829 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
830 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
831 }
832 return inode;
833}
834
Li Zefan65dff752013-03-01 15:01:56 +0800835static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
836{
837 struct cgroup_name *name;
838
839 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
840 if (!name)
841 return NULL;
842 strcpy(name->name, dentry->d_name.name);
843 return name;
844}
845
Li Zefanbe445622013-01-24 14:31:42 +0800846static void cgroup_free_fn(struct work_struct *work)
847{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700848 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800849 struct cgroup_subsys *ss;
850
851 mutex_lock(&cgroup_mutex);
852 /*
853 * Release the subsystem state objects.
854 */
Tejun Heo5549c492013-06-24 15:21:48 -0700855 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800856 ss->css_free(cgrp);
857
858 cgrp->root->number_of_cgroups--;
859 mutex_unlock(&cgroup_mutex);
860
861 /*
Li Zefan415cf072013-04-08 14:35:02 +0800862 * We get a ref to the parent's dentry, and put the ref when
863 * this cgroup is being freed, so it's guaranteed that the
864 * parent won't be destroyed before its children.
865 */
866 dput(cgrp->parent->dentry);
867
Li Zefancc20e012013-04-26 11:58:02 -0700868 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
869
Li Zefan415cf072013-04-08 14:35:02 +0800870 /*
Li Zefanbe445622013-01-24 14:31:42 +0800871 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700872 * created the cgroup. This will free cgrp->root, if we are
873 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800874 */
875 deactivate_super(cgrp->root->sb);
876
877 /*
878 * if we're getting rid of the cgroup, refcount should ensure
879 * that there are no pidlists left.
880 */
881 BUG_ON(!list_empty(&cgrp->pidlists));
882
883 simple_xattrs_free(&cgrp->xattrs);
884
Li Zefan65dff752013-03-01 15:01:56 +0800885 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800886 kfree(cgrp);
887}
888
889static void cgroup_free_rcu(struct rcu_head *head)
890{
891 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
892
Tejun Heoea15f8c2013-06-13 19:27:42 -0700893 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
894 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800895}
896
Paul Menageddbcc7e2007-10-18 23:39:30 -0700897static void cgroup_diput(struct dentry *dentry, struct inode *inode)
898{
899 /* is dentry a directory ? if so, kfree() associated cgroup */
900 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700901 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800902
Tejun Heo54766d42013-06-12 21:04:53 -0700903 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800904 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700905 } else {
906 struct cfent *cfe = __d_cfe(dentry);
907 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
908
909 WARN_ONCE(!list_empty(&cfe->node) &&
910 cgrp != &cgrp->root->top_cgroup,
911 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700912 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700913 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914 }
915 iput(inode);
916}
917
Al Viroc72a04e2011-01-14 05:31:45 +0000918static int cgroup_delete(const struct dentry *d)
919{
920 return 1;
921}
922
Paul Menageddbcc7e2007-10-18 23:39:30 -0700923static void remove_dir(struct dentry *d)
924{
925 struct dentry *parent = dget(d->d_parent);
926
927 d_delete(d);
928 simple_rmdir(parent->d_inode, d);
929 dput(parent);
930}
931
Li Zefan2739d3c2013-01-21 18:18:33 +0800932static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700934 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
937 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100938
Li Zefan2739d3c2013-01-21 18:18:33 +0800939 /*
940 * If we're doing cleanup due to failure of cgroup_create(),
941 * the corresponding @cfe may not exist.
942 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943 list_for_each_entry(cfe, &cgrp->files, node) {
944 struct dentry *d = cfe->dentry;
945
946 if (cft && cfe->type != cft)
947 continue;
948
949 dget(d);
950 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700951 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952 list_del_init(&cfe->node);
953 dput(d);
954
Li Zefan2739d3c2013-01-21 18:18:33 +0800955 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700956 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700957}
958
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400959/**
960 * cgroup_clear_directory - selective removal of base and subsystem files
961 * @dir: directory containing the files
962 * @base_files: true if the base files should be removed
963 * @subsys_mask: mask of the subsystem ids whose files should be removed
964 */
965static void cgroup_clear_directory(struct dentry *dir, bool base_files,
966 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967{
968 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400969 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700970
Tejun Heo5549c492013-06-24 15:21:48 -0700971 for_each_root_subsys(cgrp->root, ss) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cftype_set *set;
973 if (!test_bit(ss->subsys_id, &subsys_mask))
974 continue;
975 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800976 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400977 }
978 if (base_files) {
979 while (!list_empty(&cgrp->files))
980 cgroup_rm_file(cgrp, NULL);
981 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700982}
983
984/*
985 * NOTE : the dentry must have been dget()'ed
986 */
987static void cgroup_d_remove_dir(struct dentry *dentry)
988{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100989 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100991
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400992 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100994 parent = dentry->d_parent;
995 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800996 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100998 spin_unlock(&dentry->d_lock);
999 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001000 remove_dir(dentry);
1001}
1002
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001003/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001004 * Call with cgroup_mutex held. Drops reference counts on modules, including
1005 * any duplicate ones that parse_cgroupfs_options took. If this function
1006 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001007 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001009 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010{
Paul Menagebd89aab2007-10-18 23:40:44 -07001011 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001012 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 int i;
1014
Ben Blumaae8aab2010-03-10 15:22:07 -08001015 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001016 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001017
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001019 for_each_subsys(ss, i) {
Li Zefan8d53d552008-02-23 15:24:11 -08001020 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001021
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001022 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001024
Tejun Heo9871bf92013-06-24 15:21:47 -07001025 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 /* Subsystem isn't free */
1027 return -EBUSY;
1028 }
1029 }
1030
1031 /* Currently we don't handle adding/removing subsystems when
1032 * any child cgroups exist. This is theoretically supportable
1033 * but involves complex error handling, so it's being left until
1034 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001035 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 return -EBUSY;
1037
1038 /* Process each subsystem */
Tejun Heo30159ec2013-06-25 11:53:37 -07001039 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001041
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001042 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001045 BUG_ON(!cgroup_dummy_top->subsys[i]);
1046 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Tejun Heo9871bf92013-06-24 15:21:47 -07001048 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001049 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001050 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001051 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001053 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001057 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001058 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001059 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001060 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001061
Paul Menageddbcc7e2007-10-18 23:39:30 -07001062 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001063 ss->bind(cgroup_dummy_top);
1064 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001066 cgroup_subsys[i]->root = &cgroup_dummy_root;
1067 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001068
Ben Blumcf5d5942010-03-10 15:22:09 -08001069 /* subsystem is now free - drop reference on module */
1070 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001071 root->subsys_mask &= ~bit;
1072 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 /* Subsystem state should already exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001075 /*
1076 * a refcount was taken, but we already had one, so
1077 * drop the extra reference.
1078 */
1079 module_put(ss->module);
1080#ifdef CONFIG_MODULE_UNLOAD
1081 BUG_ON(ss->module && !module_refcount(ss->module));
1082#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 } else {
1084 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001085 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001086 }
1087 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088
1089 return 0;
1090}
1091
Al Viro34c80b12011-12-08 21:32:45 -05001092static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093{
Al Viro34c80b12011-12-08 21:32:45 -05001094 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001095 struct cgroup_subsys *ss;
1096
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001097 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001098 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001100 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1101 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001102 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001103 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001104 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001105 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001106 if (strlen(root->release_agent_path))
1107 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001108 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001109 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001110 if (strlen(root->name))
1111 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001112 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001113 return 0;
1114}
1115
1116struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001117 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001119 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001120 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001122 /* User explicitly requested empty subsystem */
1123 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001124
1125 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001126
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127};
1128
Ben Blumaae8aab2010-03-10 15:22:07 -08001129/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001130 * Convert a hierarchy specifier into a bitmask of subsystems and
1131 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1132 * array. This function takes refcounts on subsystems to be used, unless it
1133 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001134 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001135static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001136{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001137 char *token, *o = data;
1138 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001139 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001140 bool module_pin_failed = false;
Tejun Heo30159ec2013-06-25 11:53:37 -07001141 struct cgroup_subsys *ss;
1142 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001143
Ben Blumaae8aab2010-03-10 15:22:07 -08001144 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1145
Li Zefanf9ab5b52009-06-17 16:26:33 -07001146#ifdef CONFIG_CPUSETS
1147 mask = ~(1UL << cpuset_subsys_id);
1148#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001149
Paul Menagec6d57f32009-09-23 15:56:19 -07001150 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151
1152 while ((token = strsep(&o, ",")) != NULL) {
1153 if (!*token)
1154 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001155 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001156 /* Explicitly have no subsystems */
1157 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001158 continue;
1159 }
1160 if (!strcmp(token, "all")) {
1161 /* Mutually exclusive option 'all' + subsystem name */
1162 if (one_ss)
1163 return -EINVAL;
1164 all_ss = true;
1165 continue;
1166 }
Tejun Heo873fe092013-04-14 20:15:26 -07001167 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1168 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1169 continue;
1170 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001171 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001172 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001173 continue;
1174 }
1175 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001176 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 continue;
1178 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001179 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001180 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001181 continue;
1182 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001184 /* Specifying two release agents is forbidden */
1185 if (opts->release_agent)
1186 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001187 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001188 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001189 if (!opts->release_agent)
1190 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001191 continue;
1192 }
1193 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001194 const char *name = token + 5;
1195 /* Can't specify an empty name */
1196 if (!strlen(name))
1197 return -EINVAL;
1198 /* Must match [\w.-]+ */
1199 for (i = 0; i < strlen(name); i++) {
1200 char c = name[i];
1201 if (isalnum(c))
1202 continue;
1203 if ((c == '.') || (c == '-') || (c == '_'))
1204 continue;
1205 return -EINVAL;
1206 }
1207 /* Specifying two names is forbidden */
1208 if (opts->name)
1209 return -EINVAL;
1210 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001211 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001212 GFP_KERNEL);
1213 if (!opts->name)
1214 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001215
1216 continue;
1217 }
1218
Tejun Heo30159ec2013-06-25 11:53:37 -07001219 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001220 if (strcmp(token, ss->name))
1221 continue;
1222 if (ss->disabled)
1223 continue;
1224
1225 /* Mutually exclusive option 'all' + subsystem name */
1226 if (all_ss)
1227 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001228 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001229 one_ss = true;
1230
1231 break;
1232 }
1233 if (i == CGROUP_SUBSYS_COUNT)
1234 return -ENOENT;
1235 }
1236
1237 /*
1238 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001239 * otherwise if 'none', 'name=' and a subsystem name options
1240 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001241 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001242 if (all_ss || (!one_ss && !opts->none && !opts->name))
1243 for_each_subsys(ss, i)
1244 if (!ss->disabled)
1245 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001246
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001247 /* Consistency checks */
1248
Tejun Heo873fe092013-04-14 20:15:26 -07001249 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1250 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1251
1252 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1253 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1254 return -EINVAL;
1255 }
1256
1257 if (opts->cpuset_clone_children) {
1258 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1259 return -EINVAL;
1260 }
1261 }
1262
Li Zefanf9ab5b52009-06-17 16:26:33 -07001263 /*
1264 * Option noprefix was introduced just for backward compatibility
1265 * with the old cpuset, so we allow noprefix only if mounting just
1266 * the cpuset subsystem.
1267 */
Tejun Heo93438622013-04-14 20:15:25 -07001268 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001269 return -EINVAL;
1270
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271
1272 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001273 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001274 return -EINVAL;
1275
1276 /*
1277 * We either have to specify by name or by subsystems. (So all
1278 * empty hierarchies must have a name).
1279 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001280 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001281 return -EINVAL;
1282
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 /*
1284 * Grab references on all the modules we'll need, so the subsystems
1285 * don't dance around before rebind_subsystems attaches them. This may
1286 * take duplicate reference counts on a subsystem that's already used,
1287 * but rebind_subsystems handles this case.
1288 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001289 for_each_subsys(ss, i) {
1290 if (!(opts->subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001291 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001292 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001293 module_pin_failed = true;
1294 break;
1295 }
1296 }
1297 if (module_pin_failed) {
1298 /*
1299 * oops, one of the modules was going away. this means that we
1300 * raced with a module_delete call, and to the user this is
1301 * essentially a "subsystem doesn't exist" case.
1302 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001303 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001304 /* drop refcounts only on the ones we took */
1305 unsigned long bit = 1UL << i;
1306
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001307 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001308 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001309 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001310 }
1311 return -ENOENT;
1312 }
1313
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 return 0;
1315}
1316
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001318{
Tejun Heo30159ec2013-06-25 11:53:37 -07001319 struct cgroup_subsys *ss;
Ben Blumcf5d5942010-03-10 15:22:09 -08001320 int i;
Ben Blumcf5d5942010-03-10 15:22:09 -08001321
Tejun Heo30159ec2013-06-25 11:53:37 -07001322 for_each_subsys(ss, i) {
1323 if (!(subsys_mask & (1UL << i)))
Ben Blumcf5d5942010-03-10 15:22:09 -08001324 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001325 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001326 }
1327}
1328
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1330{
1331 int ret = 0;
1332 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001333 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001334 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336
Tejun Heo873fe092013-04-14 20:15:26 -07001337 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1338 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1339 return -EINVAL;
1340 }
1341
Paul Menagebd89aab2007-10-18 23:40:44 -07001342 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001344 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345
1346 /* See what subsystems are wanted */
1347 ret = parse_cgroupfs_options(data, &opts);
1348 if (ret)
1349 goto out_unlock;
1350
Tejun Heoa8a648c2013-06-24 15:21:47 -07001351 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001352 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1353 task_tgid_nr(current), current->comm);
1354
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001355 added_mask = opts.subsys_mask & ~root->subsys_mask;
1356 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001357
Ben Blumcf5d5942010-03-10 15:22:09 -08001358 /* Don't allow flags or name to change at remount */
1359 if (opts.flags != root->flags ||
1360 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001362 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001363 goto out_unlock;
1364 }
1365
Gao feng7083d032012-12-03 09:28:18 +08001366 /*
1367 * Clear out the files of subsystems that should be removed, do
1368 * this before rebind_subsystems, since rebind_subsystems may
1369 * change this hierarchy's subsys_list.
1370 */
1371 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1372
Tejun Heoa8a648c2013-06-24 15:21:47 -07001373 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001374 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001375 /* rebind_subsystems failed, re-populate the removed files */
1376 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001377 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001378 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001381 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001382 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383
Paul Menage81a6a5c2007-10-18 23:39:38 -07001384 if (opts.release_agent)
1385 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001387 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001388 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001389 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001390 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001391 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392 return ret;
1393}
1394
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001395static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 .statfs = simple_statfs,
1397 .drop_inode = generic_delete_inode,
1398 .show_options = cgroup_show_options,
1399 .remount_fs = cgroup_remount,
1400};
1401
Paul Menagecc31edc2008-10-18 20:28:04 -07001402static void init_cgroup_housekeeping(struct cgroup *cgrp)
1403{
1404 INIT_LIST_HEAD(&cgrp->sibling);
1405 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001406 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001407 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001408 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001409 INIT_LIST_HEAD(&cgrp->pidlists);
1410 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001411 INIT_LIST_HEAD(&cgrp->event_list);
1412 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001413 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001414}
Paul Menagec6d57f32009-09-23 15:56:19 -07001415
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416static void init_cgroup_root(struct cgroupfs_root *root)
1417{
Paul Menagebd89aab2007-10-18 23:40:44 -07001418 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001419
Paul Menageddbcc7e2007-10-18 23:39:30 -07001420 INIT_LIST_HEAD(&root->subsys_list);
1421 INIT_LIST_HEAD(&root->root_list);
1422 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001423 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001424 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001425 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426}
1427
Tejun Heofa3ca072013-04-14 11:36:56 -07001428static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429{
Tejun Heo1a574232013-04-14 11:36:58 -07001430 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001431
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001432 lockdep_assert_held(&cgroup_mutex);
1433 lockdep_assert_held(&cgroup_root_mutex);
1434
Tejun Heo1a574232013-04-14 11:36:58 -07001435 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1436 if (id < 0)
1437 return id;
1438
1439 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001440 return 0;
1441}
1442
1443static void cgroup_exit_root_id(struct cgroupfs_root *root)
1444{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001445 lockdep_assert_held(&cgroup_mutex);
1446 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001447
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001448 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001449 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001450 root->hierarchy_id = 0;
1451 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001452}
1453
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454static int cgroup_test_super(struct super_block *sb, void *data)
1455{
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001457 struct cgroupfs_root *root = sb->s_fs_info;
1458
Paul Menagec6d57f32009-09-23 15:56:19 -07001459 /* If we asked for a name then it must match */
1460 if (opts->name && strcmp(opts->name, root->name))
1461 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001462
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463 /*
1464 * If we asked for subsystems (or explicitly for no
1465 * subsystems) then they must match
1466 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001467 if ((opts->subsys_mask || opts->none)
1468 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001469 return 0;
1470
1471 return 1;
1472}
1473
Paul Menagec6d57f32009-09-23 15:56:19 -07001474static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1475{
1476 struct cgroupfs_root *root;
1477
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001478 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001479 return NULL;
1480
1481 root = kzalloc(sizeof(*root), GFP_KERNEL);
1482 if (!root)
1483 return ERR_PTR(-ENOMEM);
1484
1485 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001486
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001487 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001488 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001489 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001490 if (opts->release_agent)
1491 strcpy(root->release_agent_path, opts->release_agent);
1492 if (opts->name)
1493 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001494 if (opts->cpuset_clone_children)
1495 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 return root;
1497}
1498
Tejun Heofa3ca072013-04-14 11:36:56 -07001499static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001500{
Tejun Heofa3ca072013-04-14 11:36:56 -07001501 if (root) {
1502 /* hierarhcy ID shoulid already have been released */
1503 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001504
Tejun Heofa3ca072013-04-14 11:36:56 -07001505 ida_destroy(&root->cgroup_ida);
1506 kfree(root);
1507 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001508}
1509
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510static int cgroup_set_super(struct super_block *sb, void *data)
1511{
1512 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 struct cgroup_sb_opts *opts = data;
1514
1515 /* If we don't have a new root, we can't set up a new sb */
1516 if (!opts->new_root)
1517 return -EINVAL;
1518
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001519 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520
1521 ret = set_anon_super(sb, NULL);
1522 if (ret)
1523 return ret;
1524
Paul Menagec6d57f32009-09-23 15:56:19 -07001525 sb->s_fs_info = opts->new_root;
1526 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527
1528 sb->s_blocksize = PAGE_CACHE_SIZE;
1529 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1530 sb->s_magic = CGROUP_SUPER_MAGIC;
1531 sb->s_op = &cgroup_ops;
1532
1533 return 0;
1534}
1535
1536static int cgroup_get_rootdir(struct super_block *sb)
1537{
Al Viro0df6a632010-12-21 13:29:29 -05001538 static const struct dentry_operations cgroup_dops = {
1539 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001540 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001541 };
1542
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 struct inode *inode =
1544 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545
1546 if (!inode)
1547 return -ENOMEM;
1548
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549 inode->i_fop = &simple_dir_operations;
1550 inode->i_op = &cgroup_dir_inode_operations;
1551 /* directories start off with i_nlink == 2 (for "." entry) */
1552 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001553 sb->s_root = d_make_root(inode);
1554 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001556 /* for everything else we want ->d_op set */
1557 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558 return 0;
1559}
1560
Al Virof7e83572010-07-26 13:23:11 +04001561static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001563 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564{
1565 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001567 int ret = 0;
1568 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001569 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001570 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571
1572 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001573 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001574 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001575 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 if (ret)
1577 goto out_err;
1578
1579 /*
1580 * Allocate a new cgroup root. We may not need it if we're
1581 * reusing an existing hierarchy.
1582 */
1583 new_root = cgroup_root_from_opts(&opts);
1584 if (IS_ERR(new_root)) {
1585 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001586 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001587 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001591 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001593 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001594 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001595 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596 }
1597
Paul Menagec6d57f32009-09-23 15:56:19 -07001598 root = sb->s_fs_info;
1599 BUG_ON(!root);
1600 if (root == opts.new_root) {
1601 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001602 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001603 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001604 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001605 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001606 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001607 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001608
1609 BUG_ON(sb->s_root != NULL);
1610
1611 ret = cgroup_get_rootdir(sb);
1612 if (ret)
1613 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001614 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001615
Paul Menage817929e2007-10-18 23:39:36 -07001616 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001617 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001618 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001619
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001620 /* Check for name clashes with existing mounts */
1621 ret = -EBUSY;
1622 if (strlen(root->name))
1623 for_each_active_root(existing_root)
1624 if (!strcmp(existing_root->name, root->name))
1625 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001626
Paul Menage817929e2007-10-18 23:39:36 -07001627 /*
1628 * We're accessing css_set_count without locking
1629 * css_set_lock here, but that's OK - it can only be
1630 * increased by someone holding cgroup_lock, and
1631 * that's us. The worst that can happen is that we
1632 * have some link structures left over
1633 */
Tejun Heo69d02062013-06-12 21:04:50 -07001634 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 if (ret)
1636 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001637
Tejun Heofa3ca072013-04-14 11:36:56 -07001638 ret = cgroup_init_root_id(root);
1639 if (ret)
1640 goto unlock_drop;
1641
Tejun Heoa8a648c2013-06-24 15:21:47 -07001642 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001644 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001645 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001647 /*
1648 * There must be no failure case after here, since rebinding
1649 * takes care of subsystems' refcounts, which are explicitly
1650 * dropped in the failure exit path.
1651 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001652
1653 /* EBUSY should be the only error here */
1654 BUG_ON(ret);
1655
Tejun Heo9871bf92013-06-24 15:21:47 -07001656 list_add(&root->root_list, &cgroup_roots);
1657 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001658
Li Zefanc12f65d2009-01-07 18:07:42 -08001659 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 root->top_cgroup.dentry = sb->s_root;
1661
Paul Menage817929e2007-10-18 23:39:36 -07001662 /* Link the top cgroup in this hierarchy into all
1663 * the css_set objects */
1664 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001665 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001666 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001667 write_unlock(&css_set_lock);
1668
Tejun Heo69d02062013-06-12 21:04:50 -07001669 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001670
Li Zefanc12f65d2009-01-07 18:07:42 -08001671 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672 BUG_ON(root->number_of_cgroups != 1);
1673
eparis@redhat2ce97382011-06-02 21:20:51 +10001674 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001675 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001676 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001677 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001679 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001680 } else {
1681 /*
1682 * We re-used an existing hierarchy - the new root (if
1683 * any) is not needed
1684 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001685 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001686
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001687 if (root->flags != opts.flags) {
1688 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1689 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1690 ret = -EINVAL;
1691 goto drop_new_super;
1692 } else {
1693 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1694 }
Tejun Heo873fe092013-04-14 20:15:26 -07001695 }
1696
Ben Blumcf5d5942010-03-10 15:22:09 -08001697 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001698 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699 }
1700
Paul Menagec6d57f32009-09-23 15:56:19 -07001701 kfree(opts.release_agent);
1702 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001703 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001705 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001706 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 mutex_unlock(&cgroup_root_mutex);
1708 mutex_unlock(&cgroup_mutex);
1709 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001711 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001712 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001713 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001714 out_err:
1715 kfree(opts.release_agent);
1716 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001717 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718}
1719
1720static void cgroup_kill_sb(struct super_block *sb) {
1721 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001722 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001723 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001724 int ret;
1725
1726 BUG_ON(!root);
1727
1728 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001729 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
1731 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001732 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
1734 /* Rebind all subsystems back to the default hierarchy */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001735 ret = rebind_subsystems(root, 0, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001736 /* Shouldn't be able to fail ... */
1737 BUG_ON(ret);
1738
Paul Menage817929e2007-10-18 23:39:36 -07001739 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001740 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001741 * root cgroup
1742 */
1743 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001744
Tejun Heo69d02062013-06-12 21:04:50 -07001745 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1746 list_del(&link->cset_link);
1747 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001748 kfree(link);
1749 }
1750 write_unlock(&css_set_lock);
1751
Paul Menage839ec542009-01-29 14:25:22 -08001752 if (!list_empty(&root->root_list)) {
1753 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001754 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001755 }
Li Zefane5f6a862009-01-07 18:07:41 -08001756
Tejun Heofa3ca072013-04-14 11:36:56 -07001757 cgroup_exit_root_id(root);
1758
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001759 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 mutex_unlock(&cgroup_mutex);
1761
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001762 simple_xattrs_free(&cgrp->xattrs);
1763
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001765 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766}
1767
1768static struct file_system_type cgroup_fs_type = {
1769 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001770 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001771 .kill_sb = cgroup_kill_sb,
1772};
1773
Greg KH676db4a2010-08-05 13:53:35 -07001774static struct kobject *cgroup_kobj;
1775
Li Zefana043e3b2008-02-23 15:24:09 -08001776/**
1777 * cgroup_path - generate the path of a cgroup
1778 * @cgrp: the cgroup in question
1779 * @buf: the buffer to write the path into
1780 * @buflen: the length of the buffer
1781 *
Li Zefan65dff752013-03-01 15:01:56 +08001782 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1783 *
1784 * We can't generate cgroup path using dentry->d_name, as accessing
1785 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1786 * inode's i_mutex, while on the other hand cgroup_path() can be called
1787 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001788 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001789int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790{
Li Zefan65dff752013-03-01 15:01:56 +08001791 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001793
Tejun Heoda1f2962013-04-14 10:32:19 -07001794 if (!cgrp->parent) {
1795 if (strlcpy(buf, "/", buflen) >= buflen)
1796 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 return 0;
1798 }
1799
Tao Ma316eb662012-11-08 21:36:38 +08001800 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001801 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001802
Li Zefan65dff752013-03-01 15:01:56 +08001803 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001804 do {
Li Zefan65dff752013-03-01 15:01:56 +08001805 const char *name = cgroup_name(cgrp);
1806 int len;
1807
1808 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001809 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001810 goto out;
1811 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001812
Paul Menageddbcc7e2007-10-18 23:39:30 -07001813 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001814 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001816
1817 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001818 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001819 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001820 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001821out:
1822 rcu_read_unlock();
1823 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001824}
Ben Blum67523c42010-03-10 15:22:11 -08001825EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826
Tejun Heo857a2be2013-04-14 20:50:08 -07001827/**
1828 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1829 * @task: target task
1830 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1831 * @buf: the buffer to write the path into
1832 * @buflen: the length of the buffer
1833 *
1834 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1835 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1836 * be used inside locks used by cgroup controller callbacks.
1837 */
1838int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1839 char *buf, size_t buflen)
1840{
1841 struct cgroupfs_root *root;
1842 struct cgroup *cgrp = NULL;
1843 int ret = -ENOENT;
1844
1845 mutex_lock(&cgroup_mutex);
1846
1847 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1848 if (root) {
1849 cgrp = task_cgroup_from_root(task, root);
1850 ret = cgroup_path(cgrp, buf, buflen);
1851 }
1852
1853 mutex_unlock(&cgroup_mutex);
1854
1855 return ret;
1856}
1857EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1858
Ben Blum74a11662011-05-26 16:25:20 -07001859/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001860 * Control Group taskset
1861 */
Tejun Heo134d3372011-12-12 18:12:21 -08001862struct task_and_cgroup {
1863 struct task_struct *task;
1864 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001865 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001866};
1867
Tejun Heo2f7ee562011-12-12 18:12:21 -08001868struct cgroup_taskset {
1869 struct task_and_cgroup single;
1870 struct flex_array *tc_array;
1871 int tc_array_len;
1872 int idx;
1873 struct cgroup *cur_cgrp;
1874};
1875
1876/**
1877 * cgroup_taskset_first - reset taskset and return the first task
1878 * @tset: taskset of interest
1879 *
1880 * @tset iteration is initialized and the first task is returned.
1881 */
1882struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1883{
1884 if (tset->tc_array) {
1885 tset->idx = 0;
1886 return cgroup_taskset_next(tset);
1887 } else {
1888 tset->cur_cgrp = tset->single.cgrp;
1889 return tset->single.task;
1890 }
1891}
1892EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1893
1894/**
1895 * cgroup_taskset_next - iterate to the next task in taskset
1896 * @tset: taskset of interest
1897 *
1898 * Return the next task in @tset. Iteration must have been initialized
1899 * with cgroup_taskset_first().
1900 */
1901struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1902{
1903 struct task_and_cgroup *tc;
1904
1905 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1906 return NULL;
1907
1908 tc = flex_array_get(tset->tc_array, tset->idx++);
1909 tset->cur_cgrp = tc->cgrp;
1910 return tc->task;
1911}
1912EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1913
1914/**
1915 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1916 * @tset: taskset of interest
1917 *
1918 * Return the cgroup for the current (last returned) task of @tset. This
1919 * function must be preceded by either cgroup_taskset_first() or
1920 * cgroup_taskset_next().
1921 */
1922struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1923{
1924 return tset->cur_cgrp;
1925}
1926EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1927
1928/**
1929 * cgroup_taskset_size - return the number of tasks in taskset
1930 * @tset: taskset of interest
1931 */
1932int cgroup_taskset_size(struct cgroup_taskset *tset)
1933{
1934 return tset->tc_array ? tset->tc_array_len : 1;
1935}
1936EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1937
1938
Ben Blum74a11662011-05-26 16:25:20 -07001939/*
1940 * cgroup_task_migrate - move a task from one cgroup to another.
1941 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001942 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001943 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001944static void cgroup_task_migrate(struct cgroup *old_cgrp,
1945 struct task_struct *tsk,
1946 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001947{
Tejun Heo5abb8852013-06-12 21:04:49 -07001948 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001949
1950 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001951 * We are synchronized through threadgroup_lock() against PF_EXITING
1952 * setting such that we can't race against cgroup_exit() changing the
1953 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001954 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001955 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001956 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001957
Ben Blum74a11662011-05-26 16:25:20 -07001958 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001960 task_unlock(tsk);
1961
1962 /* Update the css_set linked lists if we're using them */
1963 write_lock(&css_set_lock);
1964 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001965 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001966 write_unlock(&css_set_lock);
1967
1968 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001969 * We just gained a reference on old_cset by taking it from the
1970 * task. As trading it for new_cset is protected by cgroup_mutex,
1971 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001972 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001973 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1974 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001975}
1976
Li Zefana043e3b2008-02-23 15:24:09 -08001977/**
Li Zefan081aa452013-03-13 09:17:09 +08001978 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001979 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001980 * @tsk: the task or the leader of the threadgroup to be attached
1981 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001982 *
Tejun Heo257058a2011-12-12 18:12:21 -08001983 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001984 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001985 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001986static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1987 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001988{
1989 int retval, i, group_size;
1990 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001991 struct cgroupfs_root *root = cgrp->root;
1992 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001993 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001994 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001995 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001996 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001997
1998 /*
1999 * step 0: in order to do expensive, possibly blocking operations for
2000 * every thread, we cannot iterate the thread group list, since it needs
2001 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002002 * group - group_rwsem prevents new threads from appearing, and if
2003 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002004 */
Li Zefan081aa452013-03-13 09:17:09 +08002005 if (threadgroup)
2006 group_size = get_nr_threads(tsk);
2007 else
2008 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002009 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002010 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002011 if (!group)
2012 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002013 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002014 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002015 if (retval)
2016 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002017
Ben Blum74a11662011-05-26 16:25:20 -07002018 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002019 /*
2020 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2021 * already PF_EXITING could be freed from underneath us unless we
2022 * take an rcu_read_lock.
2023 */
2024 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002025 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002026 struct task_and_cgroup ent;
2027
Tejun Heocd3d0952011-12-12 18:12:21 -08002028 /* @tsk either already exited or can't exit until the end */
2029 if (tsk->flags & PF_EXITING)
2030 continue;
2031
Ben Blum74a11662011-05-26 16:25:20 -07002032 /* as per above, nr_threads may decrease, but not increase. */
2033 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002034 ent.task = tsk;
2035 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002036 /* nothing to do if this task is already in the cgroup */
2037 if (ent.cgrp == cgrp)
2038 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002039 /*
2040 * saying GFP_ATOMIC has no effect here because we did prealloc
2041 * earlier, but it's good form to communicate our expectations.
2042 */
Tejun Heo134d3372011-12-12 18:12:21 -08002043 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002044 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002045 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002046
2047 if (!threadgroup)
2048 break;
Ben Blum74a11662011-05-26 16:25:20 -07002049 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002050 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002051 /* remember the number of threads in the array for later. */
2052 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002053 tset.tc_array = group;
2054 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002055
Tejun Heo134d3372011-12-12 18:12:21 -08002056 /* methods shouldn't be called if no task is actually migrating */
2057 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002058 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002059 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002060
Ben Blum74a11662011-05-26 16:25:20 -07002061 /*
2062 * step 1: check that we can legitimately attach to the cgroup.
2063 */
Tejun Heo5549c492013-06-24 15:21:48 -07002064 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002065 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002066 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002067 if (retval) {
2068 failed_ss = ss;
2069 goto out_cancel_attach;
2070 }
2071 }
Ben Blum74a11662011-05-26 16:25:20 -07002072 }
2073
2074 /*
2075 * step 2: make sure css_sets exist for all threads to be migrated.
2076 * we use find_css_set, which allocates a new one if necessary.
2077 */
Ben Blum74a11662011-05-26 16:25:20 -07002078 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002079 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002080 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2081 if (!tc->cg) {
2082 retval = -ENOMEM;
2083 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085 }
2086
2087 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002088 * step 3: now that we're guaranteed success wrt the css_sets,
2089 * proceed to move all tasks to the new cgroup. There are no
2090 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002091 */
Ben Blum74a11662011-05-26 16:25:20 -07002092 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002093 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002094 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002095 }
2096 /* nothing is sensitive to fork() after this point. */
2097
2098 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002099 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002100 */
Tejun Heo5549c492013-06-24 15:21:48 -07002101 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002102 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002103 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002104 }
2105
2106 /*
2107 * step 5: success! and cleanup
2108 */
Ben Blum74a11662011-05-26 16:25:20 -07002109 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002110out_put_css_set_refs:
2111 if (retval) {
2112 for (i = 0; i < group_size; i++) {
2113 tc = flex_array_get(group, i);
2114 if (!tc->cg)
2115 break;
2116 put_css_set(tc->cg);
2117 }
Ben Blum74a11662011-05-26 16:25:20 -07002118 }
2119out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002121 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002122 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002123 break;
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002125 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127 }
Ben Blum74a11662011-05-26 16:25:20 -07002128out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002129 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002130 return retval;
2131}
2132
2133/*
2134 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002135 * function to attach either it or all tasks in its threadgroup. Will lock
2136 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
2138static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002141 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 int ret;
2143
Ben Blum74a11662011-05-26 16:25:20 -07002144 if (!cgroup_lock_live_group(cgrp))
2145 return -ENODEV;
2146
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147retry_find_task:
2148 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002150 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002151 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002152 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 ret= -ESRCH;
2154 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155 }
Ben Blum74a11662011-05-26 16:25:20 -07002156 /*
2157 * even if we're attaching all tasks in the thread group, we
2158 * only need to check permissions on one of them.
2159 */
David Howellsc69e8d92008-11-14 10:39:19 +11002160 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002161 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2162 !uid_eq(cred->euid, tcred->uid) &&
2163 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002164 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 ret = -EACCES;
2166 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 } else
2169 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002170
2171 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002173
2174 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002175 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002176 * trapped in a cpuset, or RT worker may be born in a cgroup
2177 * with no rt_runtime allocated. Just say no.
2178 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002179 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002180 ret = -EINVAL;
2181 rcu_read_unlock();
2182 goto out_unlock_cgroup;
2183 }
2184
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185 get_task_struct(tsk);
2186 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002187
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 threadgroup_lock(tsk);
2189 if (threadgroup) {
2190 if (!thread_group_leader(tsk)) {
2191 /*
2192 * a race with de_thread from another thread's exec()
2193 * may strip us of our leadership, if this happens,
2194 * there is no choice but to throw this task away and
2195 * try again; this is
2196 * "double-double-toil-and-trouble-check locking".
2197 */
2198 threadgroup_unlock(tsk);
2199 put_task_struct(tsk);
2200 goto retry_find_task;
2201 }
Li Zefan081aa452013-03-13 09:17:09 +08002202 }
2203
2204 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2205
Tejun Heocd3d0952011-12-12 18:12:21 -08002206 threadgroup_unlock(tsk);
2207
Paul Menagebbcb81d2007-10-18 23:39:32 -07002208 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002209out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002210 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002211 return ret;
2212}
2213
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002214/**
2215 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2216 * @from: attach to all cgroups of a given task
2217 * @tsk: the task to be attached
2218 */
2219int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2220{
2221 struct cgroupfs_root *root;
2222 int retval = 0;
2223
Tejun Heo47cfcd02013-04-07 09:29:51 -07002224 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002225 for_each_active_root(root) {
2226 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2227
2228 retval = cgroup_attach_task(from_cg, tsk, false);
2229 if (retval)
2230 break;
2231 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002232 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233
2234 return retval;
2235}
2236EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2237
Paul Menageaf351022008-07-25 01:47:01 -07002238static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2239{
Ben Blum74a11662011-05-26 16:25:20 -07002240 return attach_task_by_pid(cgrp, pid, false);
2241}
2242
2243static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2244{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002245 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002246}
2247
Paul Menagee788e062008-07-25 01:46:59 -07002248static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2249 const char *buffer)
2250{
2251 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002252 if (strlen(buffer) >= PATH_MAX)
2253 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002254 if (!cgroup_lock_live_group(cgrp))
2255 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002256 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002257 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002258 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002259 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002260 return 0;
2261}
2262
2263static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2264 struct seq_file *seq)
2265{
2266 if (!cgroup_lock_live_group(cgrp))
2267 return -ENODEV;
2268 seq_puts(seq, cgrp->root->release_agent_path);
2269 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002270 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002271 return 0;
2272}
2273
Tejun Heo873fe092013-04-14 20:15:26 -07002274static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2275 struct seq_file *seq)
2276{
2277 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002278 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002279}
2280
Paul Menage84eea842008-07-25 01:47:00 -07002281/* A buffer size big enough for numbers or short strings */
2282#define CGROUP_LOCAL_BUFFER_SIZE 64
2283
Paul Menagee73d2c62008-04-29 01:00:06 -07002284static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002285 struct file *file,
2286 const char __user *userbuf,
2287 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002288{
Paul Menage84eea842008-07-25 01:47:00 -07002289 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002290 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002291 char *end;
2292
2293 if (!nbytes)
2294 return -EINVAL;
2295 if (nbytes >= sizeof(buffer))
2296 return -E2BIG;
2297 if (copy_from_user(buffer, userbuf, nbytes))
2298 return -EFAULT;
2299
2300 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002301 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002302 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002303 if (*end)
2304 return -EINVAL;
2305 retval = cft->write_u64(cgrp, cft, val);
2306 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002307 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002308 if (*end)
2309 return -EINVAL;
2310 retval = cft->write_s64(cgrp, cft, val);
2311 }
Paul Menage355e0c42007-10-18 23:39:33 -07002312 if (!retval)
2313 retval = nbytes;
2314 return retval;
2315}
2316
Paul Menagedb3b1492008-07-25 01:46:58 -07002317static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2318 struct file *file,
2319 const char __user *userbuf,
2320 size_t nbytes, loff_t *unused_ppos)
2321{
Paul Menage84eea842008-07-25 01:47:00 -07002322 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002323 int retval = 0;
2324 size_t max_bytes = cft->max_write_len;
2325 char *buffer = local_buffer;
2326
2327 if (!max_bytes)
2328 max_bytes = sizeof(local_buffer) - 1;
2329 if (nbytes >= max_bytes)
2330 return -E2BIG;
2331 /* Allocate a dynamic buffer if we need one */
2332 if (nbytes >= sizeof(local_buffer)) {
2333 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2334 if (buffer == NULL)
2335 return -ENOMEM;
2336 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002337 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2338 retval = -EFAULT;
2339 goto out;
2340 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002341
2342 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002343 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 if (!retval)
2345 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002346out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002347 if (buffer != local_buffer)
2348 kfree(buffer);
2349 return retval;
2350}
2351
Paul Menageddbcc7e2007-10-18 23:39:30 -07002352static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2353 size_t nbytes, loff_t *ppos)
2354{
2355 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002356 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357
Tejun Heo54766d42013-06-12 21:04:53 -07002358 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002360 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002361 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002362 if (cft->write_u64 || cft->write_s64)
2363 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002364 if (cft->write_string)
2365 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002366 if (cft->trigger) {
2367 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2368 return ret ? ret : nbytes;
2369 }
Paul Menage355e0c42007-10-18 23:39:33 -07002370 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371}
2372
Paul Menagef4c753b2008-04-29 00:59:56 -07002373static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2374 struct file *file,
2375 char __user *buf, size_t nbytes,
2376 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377{
Paul Menage84eea842008-07-25 01:47:00 -07002378 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002379 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2381
2382 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2383}
2384
Paul Menagee73d2c62008-04-29 01:00:06 -07002385static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2386 struct file *file,
2387 char __user *buf, size_t nbytes,
2388 loff_t *ppos)
2389{
Paul Menage84eea842008-07-25 01:47:00 -07002390 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002391 s64 val = cft->read_s64(cgrp, cft);
2392 int len = sprintf(tmp, "%lld\n", (long long) val);
2393
2394 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2395}
2396
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2398 size_t nbytes, loff_t *ppos)
2399{
2400 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002401 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402
Tejun Heo54766d42013-06-12 21:04:53 -07002403 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404 return -ENODEV;
2405
2406 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002407 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002408 if (cft->read_u64)
2409 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002410 if (cft->read_s64)
2411 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 return -EINVAL;
2413}
2414
Paul Menage91796562008-04-29 01:00:01 -07002415/*
2416 * seqfile ops/methods for returning structured data. Currently just
2417 * supports string->u64 maps, but can be extended in future.
2418 */
2419
2420struct cgroup_seqfile_state {
2421 struct cftype *cft;
2422 struct cgroup *cgroup;
2423};
2424
2425static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2426{
2427 struct seq_file *sf = cb->state;
2428 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2429}
2430
2431static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2432{
2433 struct cgroup_seqfile_state *state = m->private;
2434 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002435 if (cft->read_map) {
2436 struct cgroup_map_cb cb = {
2437 .fill = cgroup_map_add,
2438 .state = m,
2439 };
2440 return cft->read_map(state->cgroup, cft, &cb);
2441 }
2442 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002443}
2444
Adrian Bunk96930a62008-07-25 19:46:21 -07002445static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002446{
2447 struct seq_file *seq = file->private_data;
2448 kfree(seq->private);
2449 return single_release(inode, file);
2450}
2451
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002452static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002453 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002454 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002455 .llseek = seq_lseek,
2456 .release = cgroup_seqfile_release,
2457};
2458
Paul Menageddbcc7e2007-10-18 23:39:30 -07002459static int cgroup_file_open(struct inode *inode, struct file *file)
2460{
2461 int err;
2462 struct cftype *cft;
2463
2464 err = generic_file_open(inode, file);
2465 if (err)
2466 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002467 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002468
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002469 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002470 struct cgroup_seqfile_state *state;
2471
2472 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002473 if (!state)
2474 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002475
Paul Menage91796562008-04-29 01:00:01 -07002476 state->cft = cft;
2477 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2478 file->f_op = &cgroup_seqfile_operations;
2479 err = single_open(file, cgroup_seqfile_show, state);
2480 if (err < 0)
2481 kfree(state);
2482 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 err = cft->open(inode, file);
2484 else
2485 err = 0;
2486
2487 return err;
2488}
2489
2490static int cgroup_file_release(struct inode *inode, struct file *file)
2491{
2492 struct cftype *cft = __d_cft(file->f_dentry);
2493 if (cft->release)
2494 return cft->release(inode, file);
2495 return 0;
2496}
2497
2498/*
2499 * cgroup_rename - Only allow simple rename of directories in place.
2500 */
2501static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2502 struct inode *new_dir, struct dentry *new_dentry)
2503{
Li Zefan65dff752013-03-01 15:01:56 +08002504 int ret;
2505 struct cgroup_name *name, *old_name;
2506 struct cgroup *cgrp;
2507
2508 /*
2509 * It's convinient to use parent dir's i_mutex to protected
2510 * cgrp->name.
2511 */
2512 lockdep_assert_held(&old_dir->i_mutex);
2513
Paul Menageddbcc7e2007-10-18 23:39:30 -07002514 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2515 return -ENOTDIR;
2516 if (new_dentry->d_inode)
2517 return -EEXIST;
2518 if (old_dir != new_dir)
2519 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002520
2521 cgrp = __d_cgrp(old_dentry);
2522
Tejun Heo6db8e852013-06-14 11:18:22 -07002523 /*
2524 * This isn't a proper migration and its usefulness is very
2525 * limited. Disallow if sane_behavior.
2526 */
2527 if (cgroup_sane_behavior(cgrp))
2528 return -EPERM;
2529
Li Zefan65dff752013-03-01 15:01:56 +08002530 name = cgroup_alloc_name(new_dentry);
2531 if (!name)
2532 return -ENOMEM;
2533
2534 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2535 if (ret) {
2536 kfree(name);
2537 return ret;
2538 }
2539
2540 old_name = cgrp->name;
2541 rcu_assign_pointer(cgrp->name, name);
2542
2543 kfree_rcu(old_name, rcu_head);
2544 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545}
2546
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002547static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2548{
2549 if (S_ISDIR(dentry->d_inode->i_mode))
2550 return &__d_cgrp(dentry)->xattrs;
2551 else
Li Zefan712317a2013-04-18 23:09:52 -07002552 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002553}
2554
2555static inline int xattr_enabled(struct dentry *dentry)
2556{
2557 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002558 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002559}
2560
2561static bool is_valid_xattr(const char *name)
2562{
2563 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2564 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2565 return true;
2566 return false;
2567}
2568
2569static int cgroup_setxattr(struct dentry *dentry, const char *name,
2570 const void *val, size_t size, int flags)
2571{
2572 if (!xattr_enabled(dentry))
2573 return -EOPNOTSUPP;
2574 if (!is_valid_xattr(name))
2575 return -EINVAL;
2576 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2577}
2578
2579static int cgroup_removexattr(struct dentry *dentry, const char *name)
2580{
2581 if (!xattr_enabled(dentry))
2582 return -EOPNOTSUPP;
2583 if (!is_valid_xattr(name))
2584 return -EINVAL;
2585 return simple_xattr_remove(__d_xattrs(dentry), name);
2586}
2587
2588static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2589 void *buf, size_t size)
2590{
2591 if (!xattr_enabled(dentry))
2592 return -EOPNOTSUPP;
2593 if (!is_valid_xattr(name))
2594 return -EINVAL;
2595 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2596}
2597
2598static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2599{
2600 if (!xattr_enabled(dentry))
2601 return -EOPNOTSUPP;
2602 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2603}
2604
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002605static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002606 .read = cgroup_file_read,
2607 .write = cgroup_file_write,
2608 .llseek = generic_file_llseek,
2609 .open = cgroup_file_open,
2610 .release = cgroup_file_release,
2611};
2612
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002613static const struct inode_operations cgroup_file_inode_operations = {
2614 .setxattr = cgroup_setxattr,
2615 .getxattr = cgroup_getxattr,
2616 .listxattr = cgroup_listxattr,
2617 .removexattr = cgroup_removexattr,
2618};
2619
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002620static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002621 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002622 .mkdir = cgroup_mkdir,
2623 .rmdir = cgroup_rmdir,
2624 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002625 .setxattr = cgroup_setxattr,
2626 .getxattr = cgroup_getxattr,
2627 .listxattr = cgroup_listxattr,
2628 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002629};
2630
Al Viro00cd8dd2012-06-10 17:13:09 -04002631static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002632{
2633 if (dentry->d_name.len > NAME_MAX)
2634 return ERR_PTR(-ENAMETOOLONG);
2635 d_add(dentry, NULL);
2636 return NULL;
2637}
2638
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002639/*
2640 * Check if a file is a control file
2641 */
2642static inline struct cftype *__file_cft(struct file *file)
2643{
Al Viro496ad9a2013-01-23 17:07:38 -05002644 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002645 return ERR_PTR(-EINVAL);
2646 return __d_cft(file->f_dentry);
2647}
2648
Al Viroa5e7ed32011-07-26 01:55:55 -04002649static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002650 struct super_block *sb)
2651{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002652 struct inode *inode;
2653
2654 if (!dentry)
2655 return -ENOENT;
2656 if (dentry->d_inode)
2657 return -EEXIST;
2658
2659 inode = cgroup_new_inode(mode, sb);
2660 if (!inode)
2661 return -ENOMEM;
2662
2663 if (S_ISDIR(mode)) {
2664 inode->i_op = &cgroup_dir_inode_operations;
2665 inode->i_fop = &simple_dir_operations;
2666
2667 /* start off with i_nlink == 2 (for "." entry) */
2668 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002669 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670
Tejun Heob8a2df62012-11-19 08:13:37 -08002671 /*
2672 * Control reaches here with cgroup_mutex held.
2673 * @inode->i_mutex should nest outside cgroup_mutex but we
2674 * want to populate it immediately without releasing
2675 * cgroup_mutex. As @inode isn't visible to anyone else
2676 * yet, trylock will always succeed without affecting
2677 * lockdep checks.
2678 */
2679 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680 } else if (S_ISREG(mode)) {
2681 inode->i_size = 0;
2682 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002683 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002685 d_instantiate(dentry, inode);
2686 dget(dentry); /* Extra count - pin the dentry in core */
2687 return 0;
2688}
2689
Li Zefan099fca32009-04-02 16:57:29 -07002690/**
2691 * cgroup_file_mode - deduce file mode of a control file
2692 * @cft: the control file in question
2693 *
2694 * returns cft->mode if ->mode is not 0
2695 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2696 * returns S_IRUGO if it has only a read handler
2697 * returns S_IWUSR if it has only a write hander
2698 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002699static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002700{
Al Viroa5e7ed32011-07-26 01:55:55 -04002701 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002702
2703 if (cft->mode)
2704 return cft->mode;
2705
2706 if (cft->read || cft->read_u64 || cft->read_s64 ||
2707 cft->read_map || cft->read_seq_string)
2708 mode |= S_IRUGO;
2709
2710 if (cft->write || cft->write_u64 || cft->write_s64 ||
2711 cft->write_string || cft->trigger)
2712 mode |= S_IWUSR;
2713
2714 return mode;
2715}
2716
Tejun Heodb0416b2012-04-01 12:09:55 -07002717static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002718 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719{
Paul Menagebd89aab2007-10-18 23:40:44 -07002720 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002721 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002722 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002725 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002727
Tejun Heo93438622013-04-14 20:15:25 -07002728 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729 strcpy(name, subsys->name);
2730 strcat(name, ".");
2731 }
2732 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733
Paul Menageddbcc7e2007-10-18 23:39:30 -07002734 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002735
2736 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2737 if (!cfe)
2738 return -ENOMEM;
2739
Paul Menageddbcc7e2007-10-18 23:39:30 -07002740 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002743 goto out;
2744 }
2745
Li Zefand6cbf352013-05-14 19:44:20 +08002746 cfe->type = (void *)cft;
2747 cfe->dentry = dentry;
2748 dentry->d_fsdata = cfe;
2749 simple_xattrs_init(&cfe->xattrs);
2750
Tejun Heo05ef1d72012-04-01 12:09:56 -07002751 mode = cgroup_file_mode(cft);
2752 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2753 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002754 list_add_tail(&cfe->node, &parent->files);
2755 cfe = NULL;
2756 }
2757 dput(dentry);
2758out:
2759 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002760 return error;
2761}
2762
Tejun Heo79578622012-04-01 12:09:56 -07002763static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002764 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002765{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002766 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002767 int err, ret = 0;
2768
2769 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002770 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002771 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2772 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002773 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2774 continue;
2775 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2776 continue;
2777
Li Zefan2739d3c2013-01-21 18:18:33 +08002778 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002779 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002780 if (err)
2781 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2782 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002783 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002784 } else {
2785 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002786 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002787 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789}
2790
Tejun Heo8e3f6542012-04-01 12:09:55 -07002791static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002792 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793{
2794 /*
2795 * Thanks to the entanglement with vfs inode locking, we can't walk
2796 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002797 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2798 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002799 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002800 mutex_lock(&cgroup_mutex);
2801}
2802
2803static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002804 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002805 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002806{
2807 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002808 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002809 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002810 struct dentry *prev = NULL;
2811 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002812 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813
2814 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002815 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002816 !atomic_inc_not_zero(&sb->s_active)) {
2817 mutex_unlock(&cgroup_mutex);
2818 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002819 }
2820
Li Zefane8c82d22013-06-18 18:48:37 +08002821 /*
2822 * All cgroups which are created after we drop cgroup_mutex will
2823 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002824 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002825 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002826 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002827
Tejun Heo8e3f6542012-04-01 12:09:55 -07002828 mutex_unlock(&cgroup_mutex);
2829
Li Zefane8c82d22013-06-18 18:48:37 +08002830 /* @root always needs to be updated */
2831 inode = root->dentry->d_inode;
2832 mutex_lock(&inode->i_mutex);
2833 mutex_lock(&cgroup_mutex);
2834 cgroup_addrm_files(root, ss, cfts, is_add);
2835 mutex_unlock(&cgroup_mutex);
2836 mutex_unlock(&inode->i_mutex);
2837
2838 /* add/rm files for all cgroups created before */
2839 rcu_read_lock();
2840 cgroup_for_each_descendant_pre(cgrp, root) {
2841 if (cgroup_is_dead(cgrp))
2842 continue;
2843
2844 inode = cgrp->dentry->d_inode;
2845 dget(cgrp->dentry);
2846 rcu_read_unlock();
2847
2848 dput(prev);
2849 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002850
2851 mutex_lock(&inode->i_mutex);
2852 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002853 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002854 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002855 mutex_unlock(&cgroup_mutex);
2856 mutex_unlock(&inode->i_mutex);
2857
Li Zefane8c82d22013-06-18 18:48:37 +08002858 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859 }
Li Zefane8c82d22013-06-18 18:48:37 +08002860 rcu_read_unlock();
2861 dput(prev);
2862 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002863}
2864
2865/**
2866 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2867 * @ss: target cgroup subsystem
2868 * @cfts: zero-length name terminated array of cftypes
2869 *
2870 * Register @cfts to @ss. Files described by @cfts are created for all
2871 * existing cgroups to which @ss is attached and all future cgroups will
2872 * have them too. This function can be called anytime whether @ss is
2873 * attached or not.
2874 *
2875 * Returns 0 on successful registration, -errno on failure. Note that this
2876 * function currently returns 0 as long as @cfts registration is successful
2877 * even if some file creation attempts on existing cgroups fail.
2878 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002879int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002880{
2881 struct cftype_set *set;
2882
2883 set = kzalloc(sizeof(*set), GFP_KERNEL);
2884 if (!set)
2885 return -ENOMEM;
2886
2887 cgroup_cfts_prepare();
2888 set->cfts = cfts;
2889 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002890 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002891
2892 return 0;
2893}
2894EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2895
Li Zefana043e3b2008-02-23 15:24:09 -08002896/**
Tejun Heo79578622012-04-01 12:09:56 -07002897 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2898 * @ss: target cgroup subsystem
2899 * @cfts: zero-length name terminated array of cftypes
2900 *
2901 * Unregister @cfts from @ss. Files described by @cfts are removed from
2902 * all existing cgroups to which @ss is attached and all future cgroups
2903 * won't have them either. This function can be called anytime whether @ss
2904 * is attached or not.
2905 *
2906 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2907 * registered with @ss.
2908 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002909int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002910{
2911 struct cftype_set *set;
2912
2913 cgroup_cfts_prepare();
2914
2915 list_for_each_entry(set, &ss->cftsets, node) {
2916 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002917 list_del(&set->node);
2918 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002919 cgroup_cfts_commit(ss, cfts, false);
2920 return 0;
2921 }
2922 }
2923
2924 cgroup_cfts_commit(ss, NULL, false);
2925 return -ENOENT;
2926}
2927
2928/**
Li Zefana043e3b2008-02-23 15:24:09 -08002929 * cgroup_task_count - count the number of tasks in a cgroup.
2930 * @cgrp: the cgroup in question
2931 *
2932 * Return the number of tasks in the cgroup.
2933 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002934int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002935{
2936 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002937 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002938
Paul Menage817929e2007-10-18 23:39:36 -07002939 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002940 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2941 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002942 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002943 return count;
2944}
2945
2946/*
Paul Menage817929e2007-10-18 23:39:36 -07002947 * Advance a list_head iterator. The iterator should be positioned at
2948 * the start of a css_set
2949 */
Tejun Heo69d02062013-06-12 21:04:50 -07002950static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002951{
Tejun Heo69d02062013-06-12 21:04:50 -07002952 struct list_head *l = it->cset_link;
2953 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002954 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002955
2956 /* Advance to the next non-empty css_set */
2957 do {
2958 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002959 if (l == &cgrp->cset_links) {
2960 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002961 return;
2962 }
Tejun Heo69d02062013-06-12 21:04:50 -07002963 link = list_entry(l, struct cgrp_cset_link, cset_link);
2964 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002965 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002966 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002967 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002968}
2969
Cliff Wickman31a7df02008-02-07 00:14:42 -08002970/*
2971 * To reduce the fork() overhead for systems that are not actually
2972 * using their cgroups capability, we don't maintain the lists running
2973 * through each css_set to its tasks until we see the list actually
2974 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002975 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002976static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002977{
2978 struct task_struct *p, *g;
2979 write_lock(&css_set_lock);
2980 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002981 /*
2982 * We need tasklist_lock because RCU is not safe against
2983 * while_each_thread(). Besides, a forking task that has passed
2984 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2985 * is not guaranteed to have its child immediately visible in the
2986 * tasklist if we walk through it with RCU.
2987 */
2988 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002989 do_each_thread(g, p) {
2990 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002991 /*
2992 * We should check if the process is exiting, otherwise
2993 * it will race with cgroup_exit() in that the list
2994 * entry won't be deleted though the process has exited.
2995 */
2996 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002997 list_add(&p->cg_list, &p->cgroups->tasks);
2998 task_unlock(p);
2999 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003000 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003001 write_unlock(&css_set_lock);
3002}
3003
Tejun Heo574bd9f2012-11-09 09:12:29 -08003004/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003005 * cgroup_next_sibling - find the next sibling of a given cgroup
3006 * @pos: the current cgroup
3007 *
3008 * This function returns the next sibling of @pos and should be called
3009 * under RCU read lock. The only requirement is that @pos is accessible.
3010 * The next sibling is guaranteed to be returned regardless of @pos's
3011 * state.
3012 */
3013struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3014{
3015 struct cgroup *next;
3016
3017 WARN_ON_ONCE(!rcu_read_lock_held());
3018
3019 /*
3020 * @pos could already have been removed. Once a cgroup is removed,
3021 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003022 * changes. As CGRP_DEAD assertion is serialized and happens
3023 * before the cgroup is taken off the ->sibling list, if we see it
3024 * unasserted, it's guaranteed that the next sibling hasn't
3025 * finished its grace period even if it's already removed, and thus
3026 * safe to dereference from this RCU critical section. If
3027 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3028 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003029 */
Tejun Heo54766d42013-06-12 21:04:53 -07003030 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003031 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3032 if (&next->sibling != &pos->parent->children)
3033 return next;
3034 return NULL;
3035 }
3036
3037 /*
3038 * Can't dereference the next pointer. Each cgroup is given a
3039 * monotonically increasing unique serial number and always
3040 * appended to the sibling list, so the next one can be found by
3041 * walking the parent's children until we see a cgroup with higher
3042 * serial number than @pos's.
3043 *
3044 * While this path can be slow, it's taken only when either the
3045 * current cgroup is removed or iteration and removal race.
3046 */
3047 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3048 if (next->serial_nr > pos->serial_nr)
3049 return next;
3050 return NULL;
3051}
3052EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3053
3054/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003055 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3056 * @pos: the current position (%NULL to initiate traversal)
3057 * @cgroup: cgroup whose descendants to walk
3058 *
3059 * To be used by cgroup_for_each_descendant_pre(). Find the next
3060 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003061 *
3062 * While this function requires RCU read locking, it doesn't require the
3063 * whole traversal to be contained in a single RCU critical section. This
3064 * function will return the correct next descendant as long as both @pos
3065 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003066 */
3067struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3068 struct cgroup *cgroup)
3069{
3070 struct cgroup *next;
3071
3072 WARN_ON_ONCE(!rcu_read_lock_held());
3073
3074 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003075 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003076 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077
3078 /* visit the first child if exists */
3079 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3080 if (next)
3081 return next;
3082
3083 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003084 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003085 next = cgroup_next_sibling(pos);
3086 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003087 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003089 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090
3091 return NULL;
3092}
3093EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3094
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003095/**
3096 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3097 * @pos: cgroup of interest
3098 *
3099 * Return the rightmost descendant of @pos. If there's no descendant,
3100 * @pos is returned. This can be used during pre-order traversal to skip
3101 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003102 *
3103 * While this function requires RCU read locking, it doesn't require the
3104 * whole traversal to be contained in a single RCU critical section. This
3105 * function will return the correct rightmost descendant as long as @pos is
3106 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003107 */
3108struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3109{
3110 struct cgroup *last, *tmp;
3111
3112 WARN_ON_ONCE(!rcu_read_lock_held());
3113
3114 do {
3115 last = pos;
3116 /* ->prev isn't RCU safe, walk ->next till the end */
3117 pos = NULL;
3118 list_for_each_entry_rcu(tmp, &last->children, sibling)
3119 pos = tmp;
3120 } while (pos);
3121
3122 return last;
3123}
3124EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3125
Tejun Heo574bd9f2012-11-09 09:12:29 -08003126static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3127{
3128 struct cgroup *last;
3129
3130 do {
3131 last = pos;
3132 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3133 sibling);
3134 } while (pos);
3135
3136 return last;
3137}
3138
3139/**
3140 * cgroup_next_descendant_post - find the next descendant for post-order walk
3141 * @pos: the current position (%NULL to initiate traversal)
3142 * @cgroup: cgroup whose descendants to walk
3143 *
3144 * To be used by cgroup_for_each_descendant_post(). Find the next
3145 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003146 *
3147 * While this function requires RCU read locking, it doesn't require the
3148 * whole traversal to be contained in a single RCU critical section. This
3149 * function will return the correct next descendant as long as both @pos
3150 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003151 */
3152struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3153 struct cgroup *cgroup)
3154{
3155 struct cgroup *next;
3156
3157 WARN_ON_ONCE(!rcu_read_lock_held());
3158
3159 /* if first iteration, visit the leftmost descendant */
3160 if (!pos) {
3161 next = cgroup_leftmost_descendant(cgroup);
3162 return next != cgroup ? next : NULL;
3163 }
3164
3165 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003166 next = cgroup_next_sibling(pos);
3167 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168 return cgroup_leftmost_descendant(next);
3169
3170 /* no sibling left, visit parent */
3171 next = pos->parent;
3172 return next != cgroup ? next : NULL;
3173}
3174EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3175
Paul Menagebd89aab2007-10-18 23:40:44 -07003176void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003177 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003178{
3179 /*
3180 * The first time anyone tries to iterate across a cgroup,
3181 * we need to enable the list linking each css_set to its
3182 * tasks, and fix up all existing tasks.
3183 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003184 if (!use_task_css_set_links)
3185 cgroup_enable_task_cg_lists();
3186
Paul Menage817929e2007-10-18 23:39:36 -07003187 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003188 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003189 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003190}
3191
Paul Menagebd89aab2007-10-18 23:40:44 -07003192struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003193 struct cgroup_iter *it)
3194{
3195 struct task_struct *res;
3196 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003197 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003198
3199 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003200 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003201 return NULL;
3202 res = list_entry(l, struct task_struct, cg_list);
3203 /* Advance iterator to find next entry */
3204 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003205 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3206 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003207 /* We reached the end of this task list - move on to
3208 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003209 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003210 } else {
3211 it->task = l;
3212 }
3213 return res;
3214}
3215
Paul Menagebd89aab2007-10-18 23:40:44 -07003216void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003217 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003218{
3219 read_unlock(&css_set_lock);
3220}
3221
Cliff Wickman31a7df02008-02-07 00:14:42 -08003222static inline int started_after_time(struct task_struct *t1,
3223 struct timespec *time,
3224 struct task_struct *t2)
3225{
3226 int start_diff = timespec_compare(&t1->start_time, time);
3227 if (start_diff > 0) {
3228 return 1;
3229 } else if (start_diff < 0) {
3230 return 0;
3231 } else {
3232 /*
3233 * Arbitrarily, if two processes started at the same
3234 * time, we'll say that the lower pointer value
3235 * started first. Note that t2 may have exited by now
3236 * so this may not be a valid pointer any longer, but
3237 * that's fine - it still serves to distinguish
3238 * between two tasks started (effectively) simultaneously.
3239 */
3240 return t1 > t2;
3241 }
3242}
3243
3244/*
3245 * This function is a callback from heap_insert() and is used to order
3246 * the heap.
3247 * In this case we order the heap in descending task start time.
3248 */
3249static inline int started_after(void *p1, void *p2)
3250{
3251 struct task_struct *t1 = p1;
3252 struct task_struct *t2 = p2;
3253 return started_after_time(t1, &t2->start_time, t2);
3254}
3255
3256/**
3257 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3258 * @scan: struct cgroup_scanner containing arguments for the scan
3259 *
3260 * Arguments include pointers to callback functions test_task() and
3261 * process_task().
3262 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3263 * and if it returns true, call process_task() for it also.
3264 * The test_task pointer may be NULL, meaning always true (select all tasks).
3265 * Effectively duplicates cgroup_iter_{start,next,end}()
3266 * but does not lock css_set_lock for the call to process_task().
3267 * The struct cgroup_scanner may be embedded in any structure of the caller's
3268 * creation.
3269 * It is guaranteed that process_task() will act on every task that
3270 * is a member of the cgroup for the duration of this call. This
3271 * function may or may not call process_task() for tasks that exit
3272 * or move to a different cgroup during the call, or are forked or
3273 * move into the cgroup during the call.
3274 *
3275 * Note that test_task() may be called with locks held, and may in some
3276 * situations be called multiple times for the same task, so it should
3277 * be cheap.
3278 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3279 * pre-allocated and will be used for heap operations (and its "gt" member will
3280 * be overwritten), else a temporary heap will be used (allocation of which
3281 * may cause this function to fail).
3282 */
3283int cgroup_scan_tasks(struct cgroup_scanner *scan)
3284{
3285 int retval, i;
3286 struct cgroup_iter it;
3287 struct task_struct *p, *dropped;
3288 /* Never dereference latest_task, since it's not refcounted */
3289 struct task_struct *latest_task = NULL;
3290 struct ptr_heap tmp_heap;
3291 struct ptr_heap *heap;
3292 struct timespec latest_time = { 0, 0 };
3293
3294 if (scan->heap) {
3295 /* The caller supplied our heap and pre-allocated its memory */
3296 heap = scan->heap;
3297 heap->gt = &started_after;
3298 } else {
3299 /* We need to allocate our own heap memory */
3300 heap = &tmp_heap;
3301 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3302 if (retval)
3303 /* cannot allocate the heap */
3304 return retval;
3305 }
3306
3307 again:
3308 /*
3309 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3310 * to determine which are of interest, and using the scanner's
3311 * "process_task" callback to process any of them that need an update.
3312 * Since we don't want to hold any locks during the task updates,
3313 * gather tasks to be processed in a heap structure.
3314 * The heap is sorted by descending task start time.
3315 * If the statically-sized heap fills up, we overflow tasks that
3316 * started later, and in future iterations only consider tasks that
3317 * started after the latest task in the previous pass. This
3318 * guarantees forward progress and that we don't miss any tasks.
3319 */
3320 heap->size = 0;
3321 cgroup_iter_start(scan->cg, &it);
3322 while ((p = cgroup_iter_next(scan->cg, &it))) {
3323 /*
3324 * Only affect tasks that qualify per the caller's callback,
3325 * if he provided one
3326 */
3327 if (scan->test_task && !scan->test_task(p, scan))
3328 continue;
3329 /*
3330 * Only process tasks that started after the last task
3331 * we processed
3332 */
3333 if (!started_after_time(p, &latest_time, latest_task))
3334 continue;
3335 dropped = heap_insert(heap, p);
3336 if (dropped == NULL) {
3337 /*
3338 * The new task was inserted; the heap wasn't
3339 * previously full
3340 */
3341 get_task_struct(p);
3342 } else if (dropped != p) {
3343 /*
3344 * The new task was inserted, and pushed out a
3345 * different task
3346 */
3347 get_task_struct(p);
3348 put_task_struct(dropped);
3349 }
3350 /*
3351 * Else the new task was newer than anything already in
3352 * the heap and wasn't inserted
3353 */
3354 }
3355 cgroup_iter_end(scan->cg, &it);
3356
3357 if (heap->size) {
3358 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003359 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003360 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003361 latest_time = q->start_time;
3362 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003363 }
3364 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003365 scan->process_task(q, scan);
3366 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003367 }
3368 /*
3369 * If we had to process any tasks at all, scan again
3370 * in case some of them were in the middle of forking
3371 * children that didn't get processed.
3372 * Not the most efficient way to do it, but it avoids
3373 * having to take callback_mutex in the fork path
3374 */
3375 goto again;
3376 }
3377 if (heap == &tmp_heap)
3378 heap_free(&tmp_heap);
3379 return 0;
3380}
3381
Tejun Heo8cc99342013-04-07 09:29:50 -07003382static void cgroup_transfer_one_task(struct task_struct *task,
3383 struct cgroup_scanner *scan)
3384{
3385 struct cgroup *new_cgroup = scan->data;
3386
Tejun Heo47cfcd02013-04-07 09:29:51 -07003387 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003388 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003389 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003390}
3391
3392/**
3393 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3394 * @to: cgroup to which the tasks will be moved
3395 * @from: cgroup in which the tasks currently reside
3396 */
3397int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3398{
3399 struct cgroup_scanner scan;
3400
3401 scan.cg = from;
3402 scan.test_task = NULL; /* select all tasks in cgroup */
3403 scan.process_task = cgroup_transfer_one_task;
3404 scan.heap = NULL;
3405 scan.data = to;
3406
3407 return cgroup_scan_tasks(&scan);
3408}
3409
Paul Menage817929e2007-10-18 23:39:36 -07003410/*
Ben Blum102a7752009-09-23 15:56:26 -07003411 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003412 *
3413 * Reading this file can return large amounts of data if a cgroup has
3414 * *lots* of attached tasks. So it may need several calls to read(),
3415 * but we cannot guarantee that the information we produce is correct
3416 * unless we produce it entirely atomically.
3417 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003418 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003419
Li Zefan24528252012-01-20 11:58:43 +08003420/* which pidlist file are we talking about? */
3421enum cgroup_filetype {
3422 CGROUP_FILE_PROCS,
3423 CGROUP_FILE_TASKS,
3424};
3425
3426/*
3427 * A pidlist is a list of pids that virtually represents the contents of one
3428 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3429 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3430 * to the cgroup.
3431 */
3432struct cgroup_pidlist {
3433 /*
3434 * used to find which pidlist is wanted. doesn't change as long as
3435 * this particular list stays in the list.
3436 */
3437 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3438 /* array of xids */
3439 pid_t *list;
3440 /* how many elements the above list has */
3441 int length;
3442 /* how many files are using the current array */
3443 int use_count;
3444 /* each of these stored in a list by its cgroup */
3445 struct list_head links;
3446 /* pointer to the cgroup we belong to, for list removal purposes */
3447 struct cgroup *owner;
3448 /* protects the other fields */
3449 struct rw_semaphore mutex;
3450};
3451
Paul Menagebbcb81d2007-10-18 23:39:32 -07003452/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003453 * The following two functions "fix" the issue where there are more pids
3454 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3455 * TODO: replace with a kernel-wide solution to this problem
3456 */
3457#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3458static void *pidlist_allocate(int count)
3459{
3460 if (PIDLIST_TOO_LARGE(count))
3461 return vmalloc(count * sizeof(pid_t));
3462 else
3463 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3464}
3465static void pidlist_free(void *p)
3466{
3467 if (is_vmalloc_addr(p))
3468 vfree(p);
3469 else
3470 kfree(p);
3471}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003472
3473/*
Ben Blum102a7752009-09-23 15:56:26 -07003474 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003475 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003476 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003477static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003478{
Ben Blum102a7752009-09-23 15:56:26 -07003479 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003480
3481 /*
3482 * we presume the 0th element is unique, so i starts at 1. trivial
3483 * edge cases first; no work needs to be done for either
3484 */
3485 if (length == 0 || length == 1)
3486 return length;
3487 /* src and dest walk down the list; dest counts unique elements */
3488 for (src = 1; src < length; src++) {
3489 /* find next unique element */
3490 while (list[src] == list[src-1]) {
3491 src++;
3492 if (src == length)
3493 goto after;
3494 }
3495 /* dest always points to where the next unique element goes */
3496 list[dest] = list[src];
3497 dest++;
3498 }
3499after:
Ben Blum102a7752009-09-23 15:56:26 -07003500 return dest;
3501}
3502
3503static int cmppid(const void *a, const void *b)
3504{
3505 return *(pid_t *)a - *(pid_t *)b;
3506}
3507
3508/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003509 * find the appropriate pidlist for our purpose (given procs vs tasks)
3510 * returns with the lock on that pidlist already held, and takes care
3511 * of the use count, or returns NULL with no locks held if we're out of
3512 * memory.
3513 */
3514static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3515 enum cgroup_filetype type)
3516{
3517 struct cgroup_pidlist *l;
3518 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003519 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003520
Ben Blum72a8cb32009-09-23 15:56:27 -07003521 /*
3522 * We can't drop the pidlist_mutex before taking the l->mutex in case
3523 * the last ref-holder is trying to remove l from the list at the same
3524 * time. Holding the pidlist_mutex precludes somebody taking whichever
3525 * list we find out from under us - compare release_pid_array().
3526 */
3527 mutex_lock(&cgrp->pidlist_mutex);
3528 list_for_each_entry(l, &cgrp->pidlists, links) {
3529 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 /* make sure l doesn't vanish out from under us */
3531 down_write(&l->mutex);
3532 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003533 return l;
3534 }
3535 }
3536 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003537 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003538 if (!l) {
3539 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003540 return l;
3541 }
3542 init_rwsem(&l->mutex);
3543 down_write(&l->mutex);
3544 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003545 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003546 l->owner = cgrp;
3547 list_add(&l->links, &cgrp->pidlists);
3548 mutex_unlock(&cgrp->pidlist_mutex);
3549 return l;
3550}
3551
3552/*
Ben Blum102a7752009-09-23 15:56:26 -07003553 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3554 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003555static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3556 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003557{
3558 pid_t *array;
3559 int length;
3560 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003561 struct cgroup_iter it;
3562 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003563 struct cgroup_pidlist *l;
3564
3565 /*
3566 * If cgroup gets more users after we read count, we won't have
3567 * enough space - tough. This race is indistinguishable to the
3568 * caller from the case that the additional cgroup users didn't
3569 * show up until sometime later on.
3570 */
3571 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003572 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003573 if (!array)
3574 return -ENOMEM;
3575 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003576 cgroup_iter_start(cgrp, &it);
3577 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003578 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003579 break;
Ben Blum102a7752009-09-23 15:56:26 -07003580 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003581 if (type == CGROUP_FILE_PROCS)
3582 pid = task_tgid_vnr(tsk);
3583 else
3584 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003585 if (pid > 0) /* make sure to only use valid results */
3586 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003587 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003588 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003589 length = n;
3590 /* now sort & (if procs) strip out duplicates */
3591 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003592 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003593 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 l = cgroup_pidlist_find(cgrp, type);
3595 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003596 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003598 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003599 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003600 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003601 l->list = array;
3602 l->length = length;
3603 l->use_count++;
3604 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003605 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003606 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003607}
3608
Balbir Singh846c7bb2007-10-18 23:39:44 -07003609/**
Li Zefana043e3b2008-02-23 15:24:09 -08003610 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003611 * @stats: cgroupstats to fill information into
3612 * @dentry: A dentry entry belonging to the cgroup for which stats have
3613 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003614 *
3615 * Build and fill cgroupstats so that taskstats can export it to user
3616 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003617 */
3618int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3619{
3620 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003621 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003622 struct cgroup_iter it;
3623 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003624
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003626 * Validate dentry by checking the superblock operations,
3627 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003628 */
Li Zefan33d283b2008-11-19 15:36:48 -08003629 if (dentry->d_sb->s_op != &cgroup_ops ||
3630 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003631 goto err;
3632
3633 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003634 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003635
Paul Menagebd89aab2007-10-18 23:40:44 -07003636 cgroup_iter_start(cgrp, &it);
3637 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003638 switch (tsk->state) {
3639 case TASK_RUNNING:
3640 stats->nr_running++;
3641 break;
3642 case TASK_INTERRUPTIBLE:
3643 stats->nr_sleeping++;
3644 break;
3645 case TASK_UNINTERRUPTIBLE:
3646 stats->nr_uninterruptible++;
3647 break;
3648 case TASK_STOPPED:
3649 stats->nr_stopped++;
3650 break;
3651 default:
3652 if (delayacct_is_task_waiting_on_io(tsk))
3653 stats->nr_io_wait++;
3654 break;
3655 }
3656 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003657 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003658
Balbir Singh846c7bb2007-10-18 23:39:44 -07003659err:
3660 return ret;
3661}
3662
Paul Menage8f3ff202009-09-23 15:56:25 -07003663
Paul Menagecc31edc2008-10-18 20:28:04 -07003664/*
Ben Blum102a7752009-09-23 15:56:26 -07003665 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003666 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003667 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 */
3669
Ben Blum102a7752009-09-23 15:56:26 -07003670static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003671{
3672 /*
3673 * Initially we receive a position value that corresponds to
3674 * one more than the last pid shown (or 0 on the first call or
3675 * after a seek to the start). Use a binary-search to find the
3676 * next pid to display, if any
3677 */
Ben Blum102a7752009-09-23 15:56:26 -07003678 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003679 int index = 0, pid = *pos;
3680 int *iter;
3681
Ben Blum102a7752009-09-23 15:56:26 -07003682 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003683 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003684 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003685
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 while (index < end) {
3687 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003688 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 index = mid;
3690 break;
Ben Blum102a7752009-09-23 15:56:26 -07003691 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 index = mid + 1;
3693 else
3694 end = mid;
3695 }
3696 }
3697 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003698 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003699 return NULL;
3700 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003701 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 *pos = *iter;
3703 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003704}
3705
Ben Blum102a7752009-09-23 15:56:26 -07003706static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003707{
Ben Blum102a7752009-09-23 15:56:26 -07003708 struct cgroup_pidlist *l = s->private;
3709 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003710}
3711
Ben Blum102a7752009-09-23 15:56:26 -07003712static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003713{
Ben Blum102a7752009-09-23 15:56:26 -07003714 struct cgroup_pidlist *l = s->private;
3715 pid_t *p = v;
3716 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003717 /*
3718 * Advance to the next pid in the array. If this goes off the
3719 * end, we're done
3720 */
3721 p++;
3722 if (p >= end) {
3723 return NULL;
3724 } else {
3725 *pos = *p;
3726 return p;
3727 }
3728}
3729
Ben Blum102a7752009-09-23 15:56:26 -07003730static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003731{
3732 return seq_printf(s, "%d\n", *(int *)v);
3733}
3734
Ben Blum102a7752009-09-23 15:56:26 -07003735/*
3736 * seq_operations functions for iterating on pidlists through seq_file -
3737 * independent of whether it's tasks or procs
3738 */
3739static const struct seq_operations cgroup_pidlist_seq_operations = {
3740 .start = cgroup_pidlist_start,
3741 .stop = cgroup_pidlist_stop,
3742 .next = cgroup_pidlist_next,
3743 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003744};
3745
Ben Blum102a7752009-09-23 15:56:26 -07003746static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003747{
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 /*
3749 * the case where we're the last user of this particular pidlist will
3750 * have us remove it from the cgroup's list, which entails taking the
3751 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3752 * pidlist_mutex, we have to take pidlist_mutex first.
3753 */
3754 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003755 down_write(&l->mutex);
3756 BUG_ON(!l->use_count);
3757 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003758 /* we're the last user if refcount is 0; remove and free */
3759 list_del(&l->links);
3760 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003761 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003762 put_pid_ns(l->key.ns);
3763 up_write(&l->mutex);
3764 kfree(l);
3765 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003766 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003767 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003768 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003769}
3770
Ben Blum102a7752009-09-23 15:56:26 -07003771static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003772{
Ben Blum102a7752009-09-23 15:56:26 -07003773 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003774 if (!(file->f_mode & FMODE_READ))
3775 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003776 /*
3777 * the seq_file will only be initialized if the file was opened for
3778 * reading; hence we check if it's not null only in that case.
3779 */
3780 l = ((struct seq_file *)file->private_data)->private;
3781 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003782 return seq_release(inode, file);
3783}
3784
Ben Blum102a7752009-09-23 15:56:26 -07003785static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003786 .read = seq_read,
3787 .llseek = seq_lseek,
3788 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003789 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003790};
3791
3792/*
Ben Blum102a7752009-09-23 15:56:26 -07003793 * The following functions handle opens on a file that displays a pidlist
3794 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3795 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003796 */
Ben Blum102a7752009-09-23 15:56:26 -07003797/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003798static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003799{
3800 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003801 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003802 int retval;
3803
3804 /* Nothing to do for write-only files */
3805 if (!(file->f_mode & FMODE_READ))
3806 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003807
Ben Blum102a7752009-09-23 15:56:26 -07003808 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003809 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003810 if (retval)
3811 return retval;
3812 /* configure file information */
3813 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003814
Ben Blum102a7752009-09-23 15:56:26 -07003815 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003816 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003817 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003819 }
Ben Blum102a7752009-09-23 15:56:26 -07003820 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003821 return 0;
3822}
Ben Blum102a7752009-09-23 15:56:26 -07003823static int cgroup_tasks_open(struct inode *unused, struct file *file)
3824{
Ben Blum72a8cb32009-09-23 15:56:27 -07003825 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003826}
3827static int cgroup_procs_open(struct inode *unused, struct file *file)
3828{
Ben Blum72a8cb32009-09-23 15:56:27 -07003829 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003830}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003831
Paul Menagebd89aab2007-10-18 23:40:44 -07003832static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003833 struct cftype *cft)
3834{
Paul Menagebd89aab2007-10-18 23:40:44 -07003835 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003836}
3837
Paul Menage6379c102008-07-25 01:47:01 -07003838static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3839 struct cftype *cft,
3840 u64 val)
3841{
3842 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3843 if (val)
3844 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3845 else
3846 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3847 return 0;
3848}
3849
Paul Menagebbcb81d2007-10-18 23:39:32 -07003850/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003851 * When dput() is called asynchronously, if umount has been done and
3852 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3853 * there's a small window that vfs will see the root dentry with non-zero
3854 * refcnt and trigger BUG().
3855 *
3856 * That's why we hold a reference before dput() and drop it right after.
3857 */
3858static void cgroup_dput(struct cgroup *cgrp)
3859{
3860 struct super_block *sb = cgrp->root->sb;
3861
3862 atomic_inc(&sb->s_active);
3863 dput(cgrp->dentry);
3864 deactivate_super(sb);
3865}
3866
3867/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003868 * Unregister event and free resources.
3869 *
3870 * Gets called from workqueue.
3871 */
3872static void cgroup_event_remove(struct work_struct *work)
3873{
3874 struct cgroup_event *event = container_of(work, struct cgroup_event,
3875 remove);
3876 struct cgroup *cgrp = event->cgrp;
3877
Li Zefan810cbee2013-02-18 18:56:14 +08003878 remove_wait_queue(event->wqh, &event->wait);
3879
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003880 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3881
Li Zefan810cbee2013-02-18 18:56:14 +08003882 /* Notify userspace the event is going away. */
3883 eventfd_signal(event->eventfd, 1);
3884
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003885 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003886 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003887 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003888}
3889
3890/*
3891 * Gets called on POLLHUP on eventfd when user closes it.
3892 *
3893 * Called with wqh->lock held and interrupts disabled.
3894 */
3895static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3896 int sync, void *key)
3897{
3898 struct cgroup_event *event = container_of(wait,
3899 struct cgroup_event, wait);
3900 struct cgroup *cgrp = event->cgrp;
3901 unsigned long flags = (unsigned long)key;
3902
3903 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003904 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003905 * If the event has been detached at cgroup removal, we
3906 * can simply return knowing the other side will cleanup
3907 * for us.
3908 *
3909 * We can't race against event freeing since the other
3910 * side will require wqh->lock via remove_wait_queue(),
3911 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003912 */
Li Zefan810cbee2013-02-18 18:56:14 +08003913 spin_lock(&cgrp->event_list_lock);
3914 if (!list_empty(&event->list)) {
3915 list_del_init(&event->list);
3916 /*
3917 * We are in atomic context, but cgroup_event_remove()
3918 * may sleep, so we have to call it in workqueue.
3919 */
3920 schedule_work(&event->remove);
3921 }
3922 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923 }
3924
3925 return 0;
3926}
3927
3928static void cgroup_event_ptable_queue_proc(struct file *file,
3929 wait_queue_head_t *wqh, poll_table *pt)
3930{
3931 struct cgroup_event *event = container_of(pt,
3932 struct cgroup_event, pt);
3933
3934 event->wqh = wqh;
3935 add_wait_queue(wqh, &event->wait);
3936}
3937
3938/*
3939 * Parse input and register new cgroup event handler.
3940 *
3941 * Input must be in format '<event_fd> <control_fd> <args>'.
3942 * Interpretation of args is defined by control file implementation.
3943 */
3944static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3945 const char *buffer)
3946{
3947 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003948 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003949 unsigned int efd, cfd;
3950 struct file *efile = NULL;
3951 struct file *cfile = NULL;
3952 char *endp;
3953 int ret;
3954
3955 efd = simple_strtoul(buffer, &endp, 10);
3956 if (*endp != ' ')
3957 return -EINVAL;
3958 buffer = endp + 1;
3959
3960 cfd = simple_strtoul(buffer, &endp, 10);
3961 if ((*endp != ' ') && (*endp != '\0'))
3962 return -EINVAL;
3963 buffer = endp + 1;
3964
3965 event = kzalloc(sizeof(*event), GFP_KERNEL);
3966 if (!event)
3967 return -ENOMEM;
3968 event->cgrp = cgrp;
3969 INIT_LIST_HEAD(&event->list);
3970 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3971 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3972 INIT_WORK(&event->remove, cgroup_event_remove);
3973
3974 efile = eventfd_fget(efd);
3975 if (IS_ERR(efile)) {
3976 ret = PTR_ERR(efile);
3977 goto fail;
3978 }
3979
3980 event->eventfd = eventfd_ctx_fileget(efile);
3981 if (IS_ERR(event->eventfd)) {
3982 ret = PTR_ERR(event->eventfd);
3983 goto fail;
3984 }
3985
3986 cfile = fget(cfd);
3987 if (!cfile) {
3988 ret = -EBADF;
3989 goto fail;
3990 }
3991
3992 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003993 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003994 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003995 if (ret < 0)
3996 goto fail;
3997
3998 event->cft = __file_cft(cfile);
3999 if (IS_ERR(event->cft)) {
4000 ret = PTR_ERR(event->cft);
4001 goto fail;
4002 }
4003
Li Zefanf1690072013-02-18 14:13:35 +08004004 /*
4005 * The file to be monitored must be in the same cgroup as
4006 * cgroup.event_control is.
4007 */
4008 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4009 if (cgrp_cfile != cgrp) {
4010 ret = -EINVAL;
4011 goto fail;
4012 }
4013
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004014 if (!event->cft->register_event || !event->cft->unregister_event) {
4015 ret = -EINVAL;
4016 goto fail;
4017 }
4018
4019 ret = event->cft->register_event(cgrp, event->cft,
4020 event->eventfd, buffer);
4021 if (ret)
4022 goto fail;
4023
Li Zefan7ef70e42013-04-26 11:58:03 -07004024 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004025
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004026 /*
4027 * Events should be removed after rmdir of cgroup directory, but before
4028 * destroying subsystem state objects. Let's take reference to cgroup
4029 * directory dentry to do that.
4030 */
4031 dget(cgrp->dentry);
4032
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033 spin_lock(&cgrp->event_list_lock);
4034 list_add(&event->list, &cgrp->event_list);
4035 spin_unlock(&cgrp->event_list_lock);
4036
4037 fput(cfile);
4038 fput(efile);
4039
4040 return 0;
4041
4042fail:
4043 if (cfile)
4044 fput(cfile);
4045
4046 if (event && event->eventfd && !IS_ERR(event->eventfd))
4047 eventfd_ctx_put(event->eventfd);
4048
4049 if (!IS_ERR_OR_NULL(efile))
4050 fput(efile);
4051
4052 kfree(event);
4053
4054 return ret;
4055}
4056
Daniel Lezcano97978e62010-10-27 15:33:35 -07004057static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4058 struct cftype *cft)
4059{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004060 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004061}
4062
4063static int cgroup_clone_children_write(struct cgroup *cgrp,
4064 struct cftype *cft,
4065 u64 val)
4066{
4067 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004068 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004069 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004070 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004071 return 0;
4072}
4073
Tejun Heod5c56ce2013-06-03 19:14:34 -07004074static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004075 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004076 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004077 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004078 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004079 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004080 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004081 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004082 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004083 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004084 .write_string = cgroup_write_event_control,
4085 .mode = S_IWUGO,
4086 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004087 {
4088 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004089 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004090 .read_u64 = cgroup_clone_children_read,
4091 .write_u64 = cgroup_clone_children_write,
4092 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004093 {
Tejun Heo873fe092013-04-14 20:15:26 -07004094 .name = "cgroup.sane_behavior",
4095 .flags = CFTYPE_ONLY_ON_ROOT,
4096 .read_seq_string = cgroup_sane_behavior_show,
4097 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004098
4099 /*
4100 * Historical crazy stuff. These don't have "cgroup." prefix and
4101 * don't exist if sane_behavior. If you're depending on these, be
4102 * prepared to be burned.
4103 */
4104 {
4105 .name = "tasks",
4106 .flags = CFTYPE_INSANE, /* use "procs" instead */
4107 .open = cgroup_tasks_open,
4108 .write_u64 = cgroup_tasks_write,
4109 .release = cgroup_pidlist_release,
4110 .mode = S_IRUGO | S_IWUSR,
4111 },
4112 {
4113 .name = "notify_on_release",
4114 .flags = CFTYPE_INSANE,
4115 .read_u64 = cgroup_read_notify_on_release,
4116 .write_u64 = cgroup_write_notify_on_release,
4117 },
Tejun Heo873fe092013-04-14 20:15:26 -07004118 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004119 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004120 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004121 .read_seq_string = cgroup_release_agent_show,
4122 .write_string = cgroup_release_agent_write,
4123 .max_write_len = PATH_MAX,
4124 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004125 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004126};
4127
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004128/**
4129 * cgroup_populate_dir - selectively creation of files in a directory
4130 * @cgrp: target cgroup
4131 * @base_files: true if the base files should be added
4132 * @subsys_mask: mask of the subsystem ids whose files should be added
4133 */
4134static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4135 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136{
4137 int err;
4138 struct cgroup_subsys *ss;
4139
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004140 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004141 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004142 if (err < 0)
4143 return err;
4144 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004145
Tejun Heo8e3f6542012-04-01 12:09:55 -07004146 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004147 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004148 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004149 if (!test_bit(ss->subsys_id, &subsys_mask))
4150 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004151
Tejun Heodb0416b2012-04-01 12:09:55 -07004152 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004153 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004155
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004156 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004157 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004158 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4159 /*
4160 * Update id->css pointer and make this css visible from
4161 * CSS ID functions. This pointer will be dereferened
4162 * from RCU-read-side without locks.
4163 */
4164 if (css->id)
4165 rcu_assign_pointer(css->id->css, css);
4166 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004167
4168 return 0;
4169}
4170
Tejun Heo48ddbe12012-04-01 12:09:56 -07004171static void css_dput_fn(struct work_struct *work)
4172{
4173 struct cgroup_subsys_state *css =
4174 container_of(work, struct cgroup_subsys_state, dput_work);
4175
Li Zefan1c8158e2013-06-18 18:41:10 +08004176 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004177}
4178
Tejun Heod3daf282013-06-13 19:39:16 -07004179static void css_release(struct percpu_ref *ref)
4180{
4181 struct cgroup_subsys_state *css =
4182 container_of(ref, struct cgroup_subsys_state, refcnt);
4183
4184 schedule_work(&css->dput_work);
4185}
4186
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187static void init_cgroup_css(struct cgroup_subsys_state *css,
4188 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004189 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004190{
Paul Menagebd89aab2007-10-18 23:40:44 -07004191 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004193 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004194 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004195 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004196 BUG_ON(cgrp->subsys[ss->subsys_id]);
4197 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004198
4199 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004200 * css holds an extra ref to @cgrp->dentry which is put on the last
4201 * css_put(). dput() requires process context, which css_put() may
4202 * be called without. @css->dput_work will be used to invoke
4203 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004204 */
4205 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004206}
4207
Tejun Heob1929db2012-11-19 08:13:38 -08004208/* invoke ->post_create() on a new CSS and mark it online if successful */
4209static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210{
Tejun Heob1929db2012-11-19 08:13:38 -08004211 int ret = 0;
4212
Tejun Heoa31f2d32012-11-19 08:13:37 -08004213 lockdep_assert_held(&cgroup_mutex);
4214
Tejun Heo92fb9742012-11-19 08:13:38 -08004215 if (ss->css_online)
4216 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004217 if (!ret)
4218 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4219 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004220}
4221
4222/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4223static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4224 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4225{
4226 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4227
4228 lockdep_assert_held(&cgroup_mutex);
4229
4230 if (!(css->flags & CSS_ONLINE))
4231 return;
4232
Li Zefand7eeac12013-03-12 15:35:59 -07004233 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004234 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004235
4236 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4237}
4238
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239/*
Li Zefana043e3b2008-02-23 15:24:09 -08004240 * cgroup_create - create a cgroup
4241 * @parent: cgroup that will be parent of the new cgroup
4242 * @dentry: dentry of the new cgroup
4243 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 *
Li Zefana043e3b2008-02-23 15:24:09 -08004245 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004248 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249{
Paul Menagebd89aab2007-10-18 23:40:44 -07004250 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004251 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252 struct cgroupfs_root *root = parent->root;
4253 int err = 0;
4254 struct cgroup_subsys *ss;
4255 struct super_block *sb = root->sb;
4256
Tejun Heo0a950f62012-11-19 09:02:12 -08004257 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004258 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4259 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260 return -ENOMEM;
4261
Li Zefan65dff752013-03-01 15:01:56 +08004262 name = cgroup_alloc_name(dentry);
4263 if (!name)
4264 goto err_free_cgrp;
4265 rcu_assign_pointer(cgrp->name, name);
4266
Tejun Heo0a950f62012-11-19 09:02:12 -08004267 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4268 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004269 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004270
Tejun Heo976c06b2012-11-05 09:16:59 -08004271 /*
4272 * Only live parents can have children. Note that the liveliness
4273 * check isn't strictly necessary because cgroup_mkdir() and
4274 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4275 * anyway so that locking is contained inside cgroup proper and we
4276 * don't get nasty surprises if we ever grow another caller.
4277 */
4278 if (!cgroup_lock_live_group(parent)) {
4279 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004280 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004281 }
4282
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283 /* Grab a reference on the superblock so the hierarchy doesn't
4284 * get deleted on unmount if there are child cgroups. This
4285 * can be done outside cgroup_mutex, since the sb can't
4286 * disappear while someone has an open control file on the
4287 * fs */
4288 atomic_inc(&sb->s_active);
4289
Paul Menagecc31edc2008-10-18 20:28:04 -07004290 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291
Li Zefanfe1c06c2013-01-24 14:30:22 +08004292 dentry->d_fsdata = cgrp;
4293 cgrp->dentry = dentry;
4294
Paul Menagebd89aab2007-10-18 23:40:44 -07004295 cgrp->parent = parent;
4296 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004297
Li Zefanb6abdb02008-03-04 14:28:19 -08004298 if (notify_on_release(parent))
4299 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4300
Tejun Heo2260e7f2012-11-19 08:13:38 -08004301 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4302 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004303
Tejun Heo5549c492013-06-24 15:21:48 -07004304 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004305 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004306
Tejun Heo92fb9742012-11-19 08:13:38 -08004307 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308 if (IS_ERR(css)) {
4309 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004310 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004311 }
Tejun Heod3daf282013-06-13 19:39:16 -07004312
4313 err = percpu_ref_init(&css->refcnt, css_release);
4314 if (err)
4315 goto err_free_all;
4316
Paul Menagebd89aab2007-10-18 23:40:44 -07004317 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004318
Li Zefan4528fd02010-02-02 13:44:10 -08004319 if (ss->use_id) {
4320 err = alloc_css_id(ss, parent, cgrp);
4321 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004322 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004323 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324 }
4325
Tejun Heo4e139af2012-11-19 08:13:36 -08004326 /*
4327 * Create directory. cgroup_create_file() returns with the new
4328 * directory locked on success so that it can be populated without
4329 * dropping cgroup_mutex.
4330 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004331 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004333 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004334 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335
Tejun Heo00356bd2013-06-18 11:14:22 -07004336 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004337
Tejun Heo4e139af2012-11-19 08:13:36 -08004338 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004339 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4340 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004341
Tejun Heob1929db2012-11-19 08:13:38 -08004342 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004343 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004344 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004345
Li Zefan415cf072013-04-08 14:35:02 +08004346 /* hold a ref to the parent's dentry */
4347 dget(parent->dentry);
4348
Tejun Heob1929db2012-11-19 08:13:38 -08004349 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004350 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004351 err = online_css(ss, cgrp);
4352 if (err)
4353 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004354
4355 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4356 parent->parent) {
4357 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",
4358 current->comm, current->pid, ss->name);
4359 if (!strcmp(ss->name, "memory"))
4360 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4361 ss->warned_broken_hierarchy = true;
4362 }
Tejun Heoa8638032012-11-09 09:12:29 -08004363 }
4364
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004365 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004366 if (err)
4367 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368
4369 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004370 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371
4372 return 0;
4373
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004374err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004375 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004376 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4377
4378 if (css) {
4379 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004380 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004381 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004383 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 /* Release the reference count that we took on the superblock */
4385 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004386err_free_id:
4387 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004388err_free_name:
4389 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004390err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004391 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004393
4394err_destroy:
4395 cgroup_destroy_locked(cgrp);
4396 mutex_unlock(&cgroup_mutex);
4397 mutex_unlock(&dentry->d_inode->i_mutex);
4398 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399}
4400
Al Viro18bb1db2011-07-26 01:41:39 -04004401static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004402{
4403 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4404
4405 /* the vfs holds inode->i_mutex already */
4406 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4407}
4408
Tejun Heod3daf282013-06-13 19:39:16 -07004409static void cgroup_css_killed(struct cgroup *cgrp)
4410{
4411 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4412 return;
4413
4414 /* percpu ref's of all css's are killed, kick off the next step */
4415 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4416 schedule_work(&cgrp->destroy_work);
4417}
4418
4419static void css_ref_killed_fn(struct percpu_ref *ref)
4420{
4421 struct cgroup_subsys_state *css =
4422 container_of(ref, struct cgroup_subsys_state, refcnt);
4423
4424 cgroup_css_killed(css->cgroup);
4425}
4426
4427/**
4428 * cgroup_destroy_locked - the first stage of cgroup destruction
4429 * @cgrp: cgroup to be destroyed
4430 *
4431 * css's make use of percpu refcnts whose killing latency shouldn't be
4432 * exposed to userland and are RCU protected. Also, cgroup core needs to
4433 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4434 * invoked. To satisfy all the requirements, destruction is implemented in
4435 * the following two steps.
4436 *
4437 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4438 * userland visible parts and start killing the percpu refcnts of
4439 * css's. Set up so that the next stage will be kicked off once all
4440 * the percpu refcnts are confirmed to be killed.
4441 *
4442 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4443 * rest of destruction. Once all cgroup references are gone, the
4444 * cgroup is RCU-freed.
4445 *
4446 * This function implements s1. After this step, @cgrp is gone as far as
4447 * the userland is concerned and a new cgroup with the same name may be
4448 * created. As cgroup doesn't care about the names internally, this
4449 * doesn't cause any problem.
4450 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004451static int cgroup_destroy_locked(struct cgroup *cgrp)
4452 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004453{
Tejun Heo42809dd2012-11-19 08:13:37 -08004454 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004455 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004456 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004457 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004458
Tejun Heo42809dd2012-11-19 08:13:37 -08004459 lockdep_assert_held(&d->d_inode->i_mutex);
4460 lockdep_assert_held(&cgroup_mutex);
4461
Tejun Heoddd69142013-06-12 21:04:54 -07004462 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004463 * css_set_lock synchronizes access to ->cset_links and prevents
4464 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004465 */
4466 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004467 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004468 read_unlock(&css_set_lock);
4469 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004470 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004471
Tejun Heo1a90dd52012-11-05 09:16:59 -08004472 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004473 * Block new css_tryget() by killing css refcnts. cgroup core
4474 * guarantees that, by the time ->css_offline() is invoked, no new
4475 * css reference will be given out via css_tryget(). We can't
4476 * simply call percpu_ref_kill() and proceed to offlining css's
4477 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4478 * as killed on all CPUs on return.
4479 *
4480 * Use percpu_ref_kill_and_confirm() to get notifications as each
4481 * css is confirmed to be seen as killed on all CPUs. The
4482 * notification callback keeps track of the number of css's to be
4483 * killed and schedules cgroup_offline_fn() to perform the rest of
4484 * destruction once the percpu refs of all css's are confirmed to
4485 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004486 */
Tejun Heod3daf282013-06-13 19:39:16 -07004487 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004488 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004489 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4490
Tejun Heod3daf282013-06-13 19:39:16 -07004491 /*
4492 * Killing would put the base ref, but we need to keep it
4493 * alive until after ->css_offline.
4494 */
4495 percpu_ref_get(&css->refcnt);
4496
4497 atomic_inc(&cgrp->css_kill_cnt);
4498 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004499 }
Tejun Heod3daf282013-06-13 19:39:16 -07004500 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004501
4502 /*
4503 * Mark @cgrp dead. This prevents further task migration and child
4504 * creation by disabling cgroup_lock_live_group(). Note that
4505 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4506 * resume iteration after dropping RCU read lock. See
4507 * cgroup_next_sibling() for details.
4508 */
Tejun Heo54766d42013-06-12 21:04:53 -07004509 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004510
Tejun Heo455050d2013-06-13 19:27:41 -07004511 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4512 raw_spin_lock(&release_list_lock);
4513 if (!list_empty(&cgrp->release_list))
4514 list_del_init(&cgrp->release_list);
4515 raw_spin_unlock(&release_list_lock);
4516
4517 /*
4518 * Remove @cgrp directory. The removal puts the base ref but we
4519 * aren't quite done with @cgrp yet, so hold onto it.
4520 */
4521 dget(d);
4522 cgroup_d_remove_dir(d);
4523
4524 /*
4525 * Unregister events and notify userspace.
4526 * Notify userspace about cgroup removing only after rmdir of cgroup
4527 * directory to avoid race between userspace and kernelspace.
4528 */
4529 spin_lock(&cgrp->event_list_lock);
4530 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4531 list_del_init(&event->list);
4532 schedule_work(&event->remove);
4533 }
4534 spin_unlock(&cgrp->event_list_lock);
4535
Tejun Heoea15f8c2013-06-13 19:27:42 -07004536 return 0;
4537};
4538
Tejun Heod3daf282013-06-13 19:39:16 -07004539/**
4540 * cgroup_offline_fn - the second step of cgroup destruction
4541 * @work: cgroup->destroy_free_work
4542 *
4543 * This function is invoked from a work item for a cgroup which is being
4544 * destroyed after the percpu refcnts of all css's are guaranteed to be
4545 * seen as killed on all CPUs, and performs the rest of destruction. This
4546 * is the second step of destruction described in the comment above
4547 * cgroup_destroy_locked().
4548 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004549static void cgroup_offline_fn(struct work_struct *work)
4550{
4551 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4552 struct cgroup *parent = cgrp->parent;
4553 struct dentry *d = cgrp->dentry;
4554 struct cgroup_subsys *ss;
4555
4556 mutex_lock(&cgroup_mutex);
4557
Tejun Heod3daf282013-06-13 19:39:16 -07004558 /*
4559 * css_tryget() is guaranteed to fail now. Tell subsystems to
4560 * initate destruction.
4561 */
Tejun Heo5549c492013-06-24 15:21:48 -07004562 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004563 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004564
4565 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004566 * Put the css refs from cgroup_destroy_locked(). Each css holds
4567 * an extra reference to the cgroup's dentry and cgroup removal
4568 * proceeds regardless of css refs. On the last put of each css,
4569 * whenever that may be, the extra dentry ref is put so that dentry
4570 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004571 */
Tejun Heo5549c492013-06-24 15:21:48 -07004572 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004573 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574
Paul Menage999cd8a2009-01-07 18:08:36 -08004575 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004576 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004577
Paul Menageddbcc7e2007-10-18 23:39:30 -07004578 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004579
Paul Menagebd89aab2007-10-18 23:40:44 -07004580 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004581 check_for_release(parent);
4582
Tejun Heoea15f8c2013-06-13 19:27:42 -07004583 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004584}
4585
Tejun Heo42809dd2012-11-19 08:13:37 -08004586static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4587{
4588 int ret;
4589
4590 mutex_lock(&cgroup_mutex);
4591 ret = cgroup_destroy_locked(dentry->d_fsdata);
4592 mutex_unlock(&cgroup_mutex);
4593
4594 return ret;
4595}
4596
Tejun Heo8e3f6542012-04-01 12:09:55 -07004597static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4598{
4599 INIT_LIST_HEAD(&ss->cftsets);
4600
4601 /*
4602 * base_cftset is embedded in subsys itself, no need to worry about
4603 * deregistration.
4604 */
4605 if (ss->base_cftypes) {
4606 ss->base_cftset.cfts = ss->base_cftypes;
4607 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4608 }
4609}
4610
Li Zefan06a11922008-04-29 01:00:07 -07004611static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004612{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004614
4615 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616
Tejun Heo648bb562012-11-19 08:13:36 -08004617 mutex_lock(&cgroup_mutex);
4618
Tejun Heo8e3f6542012-04-01 12:09:55 -07004619 /* init base cftset */
4620 cgroup_init_cftsets(ss);
4621
Paul Menageddbcc7e2007-10-18 23:39:30 -07004622 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004623 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4624 ss->root = &cgroup_dummy_root;
4625 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626 /* We don't handle early failures gracefully */
4627 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004628 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004629
Li Zefane8d55fd2008-04-29 01:00:13 -07004630 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004631 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004632 * newly registered, all tasks and hence the
4633 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004634 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004635
4636 need_forkexit_callback |= ss->fork || ss->exit;
4637
Li Zefane8d55fd2008-04-29 01:00:13 -07004638 /* At system boot, before all subsystems have been
4639 * registered, no tasks have been forked, so we don't
4640 * need to invoke fork callbacks here. */
4641 BUG_ON(!list_empty(&init_task.tasks));
4642
Tejun Heo9871bf92013-06-24 15:21:47 -07004643 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004644
Tejun Heo648bb562012-11-19 08:13:36 -08004645 mutex_unlock(&cgroup_mutex);
4646
Ben Blume6a11052010-03-10 15:22:09 -08004647 /* this function shouldn't be used with modular subsystems, since they
4648 * need to register a subsys_id, among other things */
4649 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004650}
4651
4652/**
Ben Blume6a11052010-03-10 15:22:09 -08004653 * cgroup_load_subsys: load and register a modular subsystem at runtime
4654 * @ss: the subsystem to load
4655 *
4656 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004657 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004658 * up for use. If the subsystem is built-in anyway, work is delegated to the
4659 * simpler cgroup_init_subsys.
4660 */
4661int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4662{
Ben Blume6a11052010-03-10 15:22:09 -08004663 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004664 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004665 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004666 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004667 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004668
4669 /* check name and function validity */
4670 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004671 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004672 return -EINVAL;
4673
4674 /*
4675 * we don't support callbacks in modular subsystems. this check is
4676 * before the ss->module check for consistency; a subsystem that could
4677 * be a module should still have no callbacks even if the user isn't
4678 * compiling it as one.
4679 */
4680 if (ss->fork || ss->exit)
4681 return -EINVAL;
4682
4683 /*
4684 * an optionally modular subsystem is built-in: we want to do nothing,
4685 * since cgroup_init_subsys will have already taken care of it.
4686 */
4687 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004688 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004689 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004690 return 0;
4691 }
4692
Tejun Heo8e3f6542012-04-01 12:09:55 -07004693 /* init base cftset */
4694 cgroup_init_cftsets(ss);
4695
Ben Blume6a11052010-03-10 15:22:09 -08004696 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004697 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004698
4699 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004700 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004701 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004702 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004703 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004704 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004705 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004706 /* failure case - need to deassign the cgroup_subsys[] slot. */
4707 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004708 mutex_unlock(&cgroup_mutex);
4709 return PTR_ERR(css);
4710 }
4711
Tejun Heo9871bf92013-06-24 15:21:47 -07004712 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4713 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004714
4715 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004716 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004717 /* init_idr must be after init_cgroup_css because it sets css->id. */
4718 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004719 ret = cgroup_init_idr(ss, css);
4720 if (ret)
4721 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004722 }
4723
4724 /*
4725 * Now we need to entangle the css into the existing css_sets. unlike
4726 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4727 * will need a new pointer to it; done by iterating the css_set_table.
4728 * furthermore, modifying the existing css_sets will corrupt the hash
4729 * table state, so each changed css_set will need its hash recomputed.
4730 * this is all done under the css_set_lock.
4731 */
4732 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004733 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004734 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004735 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004736 continue;
4737 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004738 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004739 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004740 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004741 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004742 key = css_set_hash(cset->subsys);
4743 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004744 }
4745 write_unlock(&css_set_lock);
4746
Tejun Heo9871bf92013-06-24 15:21:47 -07004747 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004748 if (ret)
4749 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004750
Ben Blume6a11052010-03-10 15:22:09 -08004751 /* success! */
4752 mutex_unlock(&cgroup_mutex);
4753 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004754
4755err_unload:
4756 mutex_unlock(&cgroup_mutex);
4757 /* @ss can't be mounted here as try_module_get() would fail */
4758 cgroup_unload_subsys(ss);
4759 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004760}
4761EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4762
4763/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004764 * cgroup_unload_subsys: unload a modular subsystem
4765 * @ss: the subsystem to unload
4766 *
4767 * This function should be called in a modular subsystem's exitcall. When this
4768 * function is invoked, the refcount on the subsystem's module will be 0, so
4769 * the subsystem will not be attached to any hierarchy.
4770 */
4771void cgroup_unload_subsys(struct cgroup_subsys *ss)
4772{
Tejun Heo69d02062013-06-12 21:04:50 -07004773 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004774
4775 BUG_ON(ss->module == NULL);
4776
4777 /*
4778 * we shouldn't be called if the subsystem is in use, and the use of
4779 * try_module_get in parse_cgroupfs_options should ensure that it
4780 * doesn't start being used while we're killing it off.
4781 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004782 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004783
4784 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004785
Tejun Heo9871bf92013-06-24 15:21:47 -07004786 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004787
Tejun Heoc897ff62013-02-27 17:03:49 -08004788 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004789 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004790
Ben Blumcf5d5942010-03-10 15:22:09 -08004791 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004792 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004793
Tejun Heo9871bf92013-06-24 15:21:47 -07004794 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004795 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004796
4797 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004798 * disentangle the css from all css_sets attached to the dummy
4799 * top. as in loading, we need to pay our respects to the hashtable
4800 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004801 */
4802 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004803 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004804 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004805 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004806
Tejun Heo5abb8852013-06-12 21:04:49 -07004807 hash_del(&cset->hlist);
4808 cset->subsys[ss->subsys_id] = NULL;
4809 key = css_set_hash(cset->subsys);
4810 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004811 }
4812 write_unlock(&css_set_lock);
4813
4814 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004815 * remove subsystem's css from the cgroup_dummy_top and free it -
4816 * need to free before marking as null because ss->css_free needs
4817 * the cgrp->subsys pointer to find their state. note that this
4818 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004819 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004820 ss->css_free(cgroup_dummy_top);
4821 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004822
4823 mutex_unlock(&cgroup_mutex);
4824}
4825EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4826
4827/**
Li Zefana043e3b2008-02-23 15:24:09 -08004828 * cgroup_init_early - cgroup initialization at system boot
4829 *
4830 * Initialize cgroups at system boot, and initialize any
4831 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004832 */
4833int __init cgroup_init_early(void)
4834{
Tejun Heo30159ec2013-06-25 11:53:37 -07004835 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004836 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004837
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004838 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004839 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004840 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004841 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004842 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 init_cgroup_root(&cgroup_dummy_root);
4844 cgroup_root_count = 1;
Paul Menage817929e2007-10-18 23:39:36 -07004845 init_task.cgroups = &init_css_set;
4846
Tejun Heo69d02062013-06-12 21:04:50 -07004847 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004848 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4849 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004850 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004851
Tejun Heo30159ec2013-06-25 11:53:37 -07004852 /* at bootup time, we don't worry about modular subsystems */
4853 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004854 BUG_ON(!ss->name);
4855 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004856 BUG_ON(!ss->css_alloc);
4857 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004858 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004859 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860 ss->name, ss->subsys_id);
4861 BUG();
4862 }
4863
4864 if (ss->early_init)
4865 cgroup_init_subsys(ss);
4866 }
4867 return 0;
4868}
4869
4870/**
Li Zefana043e3b2008-02-23 15:24:09 -08004871 * cgroup_init - cgroup initialization
4872 *
4873 * Register cgroup filesystem and /proc file, and initialize
4874 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004875 */
4876int __init cgroup_init(void)
4877{
Tejun Heo30159ec2013-06-25 11:53:37 -07004878 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004879 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004880 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004881
4882 err = bdi_init(&cgroup_backing_dev_info);
4883 if (err)
4884 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004885
Tejun Heo30159ec2013-06-25 11:53:37 -07004886 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004887 if (!ss->early_init)
4888 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004889 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004890 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004891 }
4892
Tejun Heofa3ca072013-04-14 11:36:56 -07004893 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004894 mutex_lock(&cgroup_mutex);
4895 mutex_lock(&cgroup_root_mutex);
4896
Tejun Heo82fe9b02013-06-25 11:53:37 -07004897 /* Add init_css_set to the hash table */
4898 key = css_set_hash(init_css_set.subsys);
4899 hash_add(css_set_table, &init_css_set.hlist, key);
4900
Tejun Heo9871bf92013-06-24 15:21:47 -07004901 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root));
Greg KH676db4a2010-08-05 13:53:35 -07004902
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004903 mutex_unlock(&cgroup_root_mutex);
4904 mutex_unlock(&cgroup_mutex);
4905
Greg KH676db4a2010-08-05 13:53:35 -07004906 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4907 if (!cgroup_kobj) {
4908 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004909 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004910 }
4911
4912 err = register_filesystem(&cgroup_fs_type);
4913 if (err < 0) {
4914 kobject_put(cgroup_kobj);
4915 goto out;
4916 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004917
Li Zefan46ae2202008-04-29 01:00:08 -07004918 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004919
Paul Menageddbcc7e2007-10-18 23:39:30 -07004920out:
Paul Menagea4243162007-10-18 23:39:35 -07004921 if (err)
4922 bdi_destroy(&cgroup_backing_dev_info);
4923
Paul Menageddbcc7e2007-10-18 23:39:30 -07004924 return err;
4925}
Paul Menageb4f48b62007-10-18 23:39:33 -07004926
Paul Menagea4243162007-10-18 23:39:35 -07004927/*
4928 * proc_cgroup_show()
4929 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4930 * - Used for /proc/<pid>/cgroup.
4931 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4932 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004933 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004934 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4935 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4936 * cgroup to top_cgroup.
4937 */
4938
4939/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004940int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004941{
4942 struct pid *pid;
4943 struct task_struct *tsk;
4944 char *buf;
4945 int retval;
4946 struct cgroupfs_root *root;
4947
4948 retval = -ENOMEM;
4949 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4950 if (!buf)
4951 goto out;
4952
4953 retval = -ESRCH;
4954 pid = m->private;
4955 tsk = get_pid_task(pid, PIDTYPE_PID);
4956 if (!tsk)
4957 goto out_free;
4958
4959 retval = 0;
4960
4961 mutex_lock(&cgroup_mutex);
4962
Li Zefane5f6a862009-01-07 18:07:41 -08004963 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004964 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004965 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004966 int count = 0;
4967
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004968 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004969 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004970 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004971 if (strlen(root->name))
4972 seq_printf(m, "%sname=%s", count ? "," : "",
4973 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004974 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004975 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004976 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004977 if (retval < 0)
4978 goto out_unlock;
4979 seq_puts(m, buf);
4980 seq_putc(m, '\n');
4981 }
4982
4983out_unlock:
4984 mutex_unlock(&cgroup_mutex);
4985 put_task_struct(tsk);
4986out_free:
4987 kfree(buf);
4988out:
4989 return retval;
4990}
4991
Paul Menagea4243162007-10-18 23:39:35 -07004992/* Display information about each subsystem and each hierarchy */
4993static int proc_cgroupstats_show(struct seq_file *m, void *v)
4994{
Tejun Heo30159ec2013-06-25 11:53:37 -07004995 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004996 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004997
Paul Menage8bab8dd2008-04-04 14:29:57 -07004998 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004999 /*
5000 * ideally we don't want subsystems moving around while we do this.
5001 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5002 * subsys/hierarchy state.
5003 */
Paul Menagea4243162007-10-18 23:39:35 -07005004 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005005
5006 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005007 seq_printf(m, "%s\t%d\t%d\t%d\n",
5008 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005009 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005010
Paul Menagea4243162007-10-18 23:39:35 -07005011 mutex_unlock(&cgroup_mutex);
5012 return 0;
5013}
5014
5015static int cgroupstats_open(struct inode *inode, struct file *file)
5016{
Al Viro9dce07f2008-03-29 03:07:28 +00005017 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005018}
5019
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005020static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005021 .open = cgroupstats_open,
5022 .read = seq_read,
5023 .llseek = seq_lseek,
5024 .release = single_release,
5025};
5026
Paul Menageb4f48b62007-10-18 23:39:33 -07005027/**
5028 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005029 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005030 *
5031 * Description: A task inherits its parent's cgroup at fork().
5032 *
5033 * A pointer to the shared css_set was automatically copied in
5034 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005035 * it was not made under the protection of RCU or cgroup_mutex, so
5036 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5037 * have already changed current->cgroups, allowing the previously
5038 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005039 *
5040 * At the point that cgroup_fork() is called, 'current' is the parent
5041 * task, and the passed argument 'child' points to the child task.
5042 */
5043void cgroup_fork(struct task_struct *child)
5044{
Tejun Heo9bb71302012-10-18 17:52:07 -07005045 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005046 child->cgroups = current->cgroups;
5047 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005048 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005049 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005050}
5051
5052/**
Li Zefana043e3b2008-02-23 15:24:09 -08005053 * cgroup_post_fork - called on a new task after adding it to the task list
5054 * @child: the task in question
5055 *
Tejun Heo5edee612012-10-16 15:03:14 -07005056 * Adds the task to the list running through its css_set if necessary and
5057 * call the subsystem fork() callbacks. Has to be after the task is
5058 * visible on the task list in case we race with the first call to
5059 * cgroup_iter_start() - to guarantee that the new task ends up on its
5060 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005061 */
Paul Menage817929e2007-10-18 23:39:36 -07005062void cgroup_post_fork(struct task_struct *child)
5063{
Tejun Heo30159ec2013-06-25 11:53:37 -07005064 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005065 int i;
5066
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005067 /*
5068 * use_task_css_set_links is set to 1 before we walk the tasklist
5069 * under the tasklist_lock and we read it here after we added the child
5070 * to the tasklist under the tasklist_lock as well. If the child wasn't
5071 * yet in the tasklist when we walked through it from
5072 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5073 * should be visible now due to the paired locking and barriers implied
5074 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5075 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5076 * lock on fork.
5077 */
Paul Menage817929e2007-10-18 23:39:36 -07005078 if (use_task_css_set_links) {
5079 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005080 task_lock(child);
5081 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005082 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005083 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005084 write_unlock(&css_set_lock);
5085 }
Tejun Heo5edee612012-10-16 15:03:14 -07005086
5087 /*
5088 * Call ss->fork(). This must happen after @child is linked on
5089 * css_set; otherwise, @child might change state between ->fork()
5090 * and addition to css_set.
5091 */
5092 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005093 /*
5094 * fork/exit callbacks are supported only for builtin
5095 * subsystems, and the builtin section of the subsys
5096 * array is immutable, so we don't need to lock the
5097 * subsys array here. On the other hand, modular section
5098 * of the array can be freed at module unload, so we
5099 * can't touch that.
5100 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005101 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005102 if (ss->fork)
5103 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005104 }
Paul Menage817929e2007-10-18 23:39:36 -07005105}
Tejun Heo5edee612012-10-16 15:03:14 -07005106
Paul Menage817929e2007-10-18 23:39:36 -07005107/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005108 * cgroup_exit - detach cgroup from exiting task
5109 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005110 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005111 *
5112 * Description: Detach cgroup from @tsk and release it.
5113 *
5114 * Note that cgroups marked notify_on_release force every task in
5115 * them to take the global cgroup_mutex mutex when exiting.
5116 * This could impact scaling on very large systems. Be reluctant to
5117 * use notify_on_release cgroups where very high task exit scaling
5118 * is required on large systems.
5119 *
5120 * the_top_cgroup_hack:
5121 *
5122 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5123 *
5124 * We call cgroup_exit() while the task is still competent to
5125 * handle notify_on_release(), then leave the task attached to the
5126 * root cgroup in each hierarchy for the remainder of its exit.
5127 *
5128 * To do this properly, we would increment the reference count on
5129 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5130 * code we would add a second cgroup function call, to drop that
5131 * reference. This would just create an unnecessary hot spot on
5132 * the top_cgroup reference count, to no avail.
5133 *
5134 * Normally, holding a reference to a cgroup without bumping its
5135 * count is unsafe. The cgroup could go away, or someone could
5136 * attach us to a different cgroup, decrementing the count on
5137 * the first cgroup that we never incremented. But in this case,
5138 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005139 * which wards off any cgroup_attach_task() attempts, or task is a failed
5140 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005141 */
5142void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5143{
Tejun Heo30159ec2013-06-25 11:53:37 -07005144 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005145 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005146 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005147
5148 /*
5149 * Unlink from the css_set task list if necessary.
5150 * Optimistically check cg_list before taking
5151 * css_set_lock
5152 */
5153 if (!list_empty(&tsk->cg_list)) {
5154 write_lock(&css_set_lock);
5155 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005156 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005157 write_unlock(&css_set_lock);
5158 }
5159
Paul Menageb4f48b62007-10-18 23:39:33 -07005160 /* Reassign the task to the init_css_set. */
5161 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005162 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005163 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005164
5165 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005166 /*
5167 * fork/exit callbacks are supported only for builtin
5168 * subsystems, see cgroup_post_fork() for details.
5169 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005170 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005171 if (ss->exit) {
5172 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005173 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005174 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005175
Li Zefan761b3ef2012-01-31 13:47:36 +08005176 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005177 }
5178 }
5179 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005180 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005181
Tejun Heo5abb8852013-06-12 21:04:49 -07005182 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005183}
Paul Menage697f4162007-10-18 23:39:34 -07005184
Paul Menagebd89aab2007-10-18 23:40:44 -07005185static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005186{
Li Zefanf50daa72013-03-01 15:06:07 +08005187 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005188 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005189 /*
5190 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005191 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005192 * it now
5193 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005194 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005195
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005196 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005197 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005198 list_empty(&cgrp->release_list)) {
5199 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005200 need_schedule_work = 1;
5201 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005202 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005203 if (need_schedule_work)
5204 schedule_work(&release_agent_work);
5205 }
5206}
5207
Paul Menage81a6a5c2007-10-18 23:39:38 -07005208/*
5209 * Notify userspace when a cgroup is released, by running the
5210 * configured release agent with the name of the cgroup (path
5211 * relative to the root of cgroup file system) as the argument.
5212 *
5213 * Most likely, this user command will try to rmdir this cgroup.
5214 *
5215 * This races with the possibility that some other task will be
5216 * attached to this cgroup before it is removed, or that some other
5217 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5218 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5219 * unused, and this cgroup will be reprieved from its death sentence,
5220 * to continue to serve a useful existence. Next time it's released,
5221 * we will get notified again, if it still has 'notify_on_release' set.
5222 *
5223 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5224 * means only wait until the task is successfully execve()'d. The
5225 * separate release agent task is forked by call_usermodehelper(),
5226 * then control in this thread returns here, without waiting for the
5227 * release agent task. We don't bother to wait because the caller of
5228 * this routine has no use for the exit status of the release agent
5229 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005230 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005231static void cgroup_release_agent(struct work_struct *work)
5232{
5233 BUG_ON(work != &release_agent_work);
5234 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005235 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005236 while (!list_empty(&release_list)) {
5237 char *argv[3], *envp[3];
5238 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005239 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005240 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005241 struct cgroup,
5242 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005243 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005244 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005245 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005246 if (!pathbuf)
5247 goto continue_free;
5248 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5249 goto continue_free;
5250 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5251 if (!agentbuf)
5252 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005253
5254 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005255 argv[i++] = agentbuf;
5256 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005257 argv[i] = NULL;
5258
5259 i = 0;
5260 /* minimal command environment */
5261 envp[i++] = "HOME=/";
5262 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5263 envp[i] = NULL;
5264
5265 /* Drop the lock while we invoke the usermode helper,
5266 * since the exec could involve hitting disk and hence
5267 * be a slow process */
5268 mutex_unlock(&cgroup_mutex);
5269 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005270 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005271 continue_free:
5272 kfree(pathbuf);
5273 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005274 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005275 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005276 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005277 mutex_unlock(&cgroup_mutex);
5278}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005279
5280static int __init cgroup_disable(char *str)
5281{
Tejun Heo30159ec2013-06-25 11:53:37 -07005282 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005283 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005284 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005285
5286 while ((token = strsep(&str, ",")) != NULL) {
5287 if (!*token)
5288 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005289
Tejun Heo30159ec2013-06-25 11:53:37 -07005290 /*
5291 * cgroup_disable, being at boot time, can't know about
5292 * module subsystems, so we don't worry about them.
5293 */
5294 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005295 if (!strcmp(token, ss->name)) {
5296 ss->disabled = 1;
5297 printk(KERN_INFO "Disabling %s control group"
5298 " subsystem\n", ss->name);
5299 break;
5300 }
5301 }
5302 }
5303 return 1;
5304}
5305__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005306
5307/*
5308 * Functons for CSS ID.
5309 */
5310
Tejun Heo54766d42013-06-12 21:04:53 -07005311/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005312unsigned short css_id(struct cgroup_subsys_state *css)
5313{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005314 struct css_id *cssid;
5315
5316 /*
5317 * This css_id() can return correct value when somone has refcnt
5318 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5319 * it's unchanged until freed.
5320 */
Tejun Heod3daf282013-06-13 19:39:16 -07005321 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005322
5323 if (cssid)
5324 return cssid->id;
5325 return 0;
5326}
Ben Blum67523c42010-03-10 15:22:11 -08005327EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005328
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005329/**
5330 * css_is_ancestor - test "root" css is an ancestor of "child"
5331 * @child: the css to be tested.
5332 * @root: the css supporsed to be an ancestor of the child.
5333 *
5334 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005335 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005336 * But, considering usual usage, the csses should be valid objects after test.
5337 * Assuming that the caller will do some action to the child if this returns
5338 * returns true, the caller must take "child";s reference count.
5339 * If "child" is valid object and this returns true, "root" is valid, too.
5340 */
5341
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005342bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005343 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005345 struct css_id *child_id;
5346 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005348 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005349 if (!child_id)
5350 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005351 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005352 if (!root_id)
5353 return false;
5354 if (child_id->depth < root_id->depth)
5355 return false;
5356 if (child_id->stack[root_id->depth] != root_id->id)
5357 return false;
5358 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005359}
5360
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005361void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5362{
5363 struct css_id *id = css->id;
5364 /* When this is called before css_id initialization, id can be NULL */
5365 if (!id)
5366 return;
5367
5368 BUG_ON(!ss->use_id);
5369
5370 rcu_assign_pointer(id->css, NULL);
5371 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005372 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005374 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005375 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005376}
Ben Blum67523c42010-03-10 15:22:11 -08005377EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378
5379/*
5380 * This is called by init or create(). Then, calls to this function are
5381 * always serialized (By cgroup_mutex() at create()).
5382 */
5383
5384static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5385{
5386 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005387 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005388
5389 BUG_ON(!ss->use_id);
5390
5391 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5392 newid = kzalloc(size, GFP_KERNEL);
5393 if (!newid)
5394 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005395
5396 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005397 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005398 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005399 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005400 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005401 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005402
5403 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005404 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005405 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005406
Tejun Heod228d9e2013-02-27 17:04:54 -08005407 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408 newid->depth = depth;
5409 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410err_out:
5411 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005412 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413
5414}
5415
Ben Blume6a11052010-03-10 15:22:09 -08005416static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5417 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418{
5419 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005420
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005421 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005422 idr_init(&ss->idr);
5423
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005424 newid = get_new_cssid(ss, 0);
5425 if (IS_ERR(newid))
5426 return PTR_ERR(newid);
5427
5428 newid->stack[0] = newid->id;
5429 newid->css = rootcss;
5430 rootcss->id = newid;
5431 return 0;
5432}
5433
5434static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5435 struct cgroup *child)
5436{
5437 int subsys_id, i, depth = 0;
5438 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005439 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005440
5441 subsys_id = ss->subsys_id;
5442 parent_css = parent->subsys[subsys_id];
5443 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005444 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005445 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446
5447 child_id = get_new_cssid(ss, depth);
5448 if (IS_ERR(child_id))
5449 return PTR_ERR(child_id);
5450
5451 for (i = 0; i < depth; i++)
5452 child_id->stack[i] = parent_id->stack[i];
5453 child_id->stack[depth] = child_id->id;
5454 /*
5455 * child_id->css pointer will be set after this cgroup is available
5456 * see cgroup_populate_dir()
5457 */
5458 rcu_assign_pointer(child_css->id, child_id);
5459
5460 return 0;
5461}
5462
5463/**
5464 * css_lookup - lookup css by id
5465 * @ss: cgroup subsys to be looked into.
5466 * @id: the id
5467 *
5468 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5469 * NULL if not. Should be called under rcu_read_lock()
5470 */
5471struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5472{
5473 struct css_id *cssid = NULL;
5474
5475 BUG_ON(!ss->use_id);
5476 cssid = idr_find(&ss->idr, id);
5477
5478 if (unlikely(!cssid))
5479 return NULL;
5480
5481 return rcu_dereference(cssid->css);
5482}
Ben Blum67523c42010-03-10 15:22:11 -08005483EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005484
Stephane Eraniane5d13672011-02-14 11:20:01 +02005485/*
5486 * get corresponding css from file open on cgroupfs directory
5487 */
5488struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5489{
5490 struct cgroup *cgrp;
5491 struct inode *inode;
5492 struct cgroup_subsys_state *css;
5493
Al Viro496ad9a2013-01-23 17:07:38 -05005494 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005495 /* check in cgroup filesystem dir */
5496 if (inode->i_op != &cgroup_dir_inode_operations)
5497 return ERR_PTR(-EBADF);
5498
5499 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5500 return ERR_PTR(-EINVAL);
5501
5502 /* get cgroup */
5503 cgrp = __d_cgrp(f->f_dentry);
5504 css = cgrp->subsys[id];
5505 return css ? css : ERR_PTR(-ENOENT);
5506}
5507
Paul Menagefe693432009-09-23 15:56:20 -07005508#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005509static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005510{
5511 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5512
5513 if (!css)
5514 return ERR_PTR(-ENOMEM);
5515
5516 return css;
5517}
5518
Li Zefan03c78cb2013-06-14 11:17:19 +08005519static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005520{
Li Zefan03c78cb2013-06-14 11:17:19 +08005521 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005522}
5523
Li Zefan03c78cb2013-06-14 11:17:19 +08005524static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005525{
Li Zefan03c78cb2013-06-14 11:17:19 +08005526 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005527}
5528
Li Zefan03c78cb2013-06-14 11:17:19 +08005529static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005530{
5531 return (u64)(unsigned long)current->cgroups;
5532}
5533
Li Zefan03c78cb2013-06-14 11:17:19 +08005534static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5535 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005536{
5537 u64 count;
5538
5539 rcu_read_lock();
5540 count = atomic_read(&current->cgroups->refcount);
5541 rcu_read_unlock();
5542 return count;
5543}
5544
Li Zefan03c78cb2013-06-14 11:17:19 +08005545static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005546 struct cftype *cft,
5547 struct seq_file *seq)
5548{
Tejun Heo69d02062013-06-12 21:04:50 -07005549 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005550 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005551
5552 read_lock(&css_set_lock);
5553 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005554 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005555 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005556 struct cgroup *c = link->cgrp;
5557 const char *name;
5558
5559 if (c->dentry)
5560 name = c->dentry->d_name.name;
5561 else
5562 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005563 seq_printf(seq, "Root %d group %s\n",
5564 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005565 }
5566 rcu_read_unlock();
5567 read_unlock(&css_set_lock);
5568 return 0;
5569}
5570
5571#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005572static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005573 struct cftype *cft,
5574 struct seq_file *seq)
5575{
Tejun Heo69d02062013-06-12 21:04:50 -07005576 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005577
5578 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005579 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005580 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005581 struct task_struct *task;
5582 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005583 seq_printf(seq, "css_set %p\n", cset);
5584 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005585 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5586 seq_puts(seq, " ...\n");
5587 break;
5588 } else {
5589 seq_printf(seq, " task %d\n",
5590 task_pid_vnr(task));
5591 }
5592 }
5593 }
5594 read_unlock(&css_set_lock);
5595 return 0;
5596}
5597
Paul Menagefe693432009-09-23 15:56:20 -07005598static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5599{
5600 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5601}
5602
5603static struct cftype debug_files[] = {
5604 {
Paul Menagefe693432009-09-23 15:56:20 -07005605 .name = "taskcount",
5606 .read_u64 = debug_taskcount_read,
5607 },
5608
5609 {
5610 .name = "current_css_set",
5611 .read_u64 = current_css_set_read,
5612 },
5613
5614 {
5615 .name = "current_css_set_refcount",
5616 .read_u64 = current_css_set_refcount_read,
5617 },
5618
5619 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005620 .name = "current_css_set_cg_links",
5621 .read_seq_string = current_css_set_cg_links_read,
5622 },
5623
5624 {
5625 .name = "cgroup_css_links",
5626 .read_seq_string = cgroup_css_links_read,
5627 },
5628
5629 {
Paul Menagefe693432009-09-23 15:56:20 -07005630 .name = "releasable",
5631 .read_u64 = releasable_read,
5632 },
Paul Menagefe693432009-09-23 15:56:20 -07005633
Tejun Heo4baf6e32012-04-01 12:09:55 -07005634 { } /* terminate */
5635};
Paul Menagefe693432009-09-23 15:56:20 -07005636
5637struct cgroup_subsys debug_subsys = {
5638 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005639 .css_alloc = debug_css_alloc,
5640 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005641 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005642 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005643};
5644#endif /* CONFIG_CGROUP_DEBUG */