blob: f9c99abc38ab21417f0fe4f1bc691a1f8643d49e [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 Heofc76df72013-06-25 11:53:37 -07001428static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
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 Heofc76df72013-06-25 11:53:37 -07001435 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1436 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001437 if (id < 0)
1438 return id;
1439
1440 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001441 return 0;
1442}
1443
1444static void cgroup_exit_root_id(struct cgroupfs_root *root)
1445{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001446 lockdep_assert_held(&cgroup_mutex);
1447 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001448
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001449 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001450 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001451 root->hierarchy_id = 0;
1452 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453}
1454
Paul Menageddbcc7e2007-10-18 23:39:30 -07001455static int cgroup_test_super(struct super_block *sb, void *data)
1456{
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001458 struct cgroupfs_root *root = sb->s_fs_info;
1459
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 /* If we asked for a name then it must match */
1461 if (opts->name && strcmp(opts->name, root->name))
1462 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001464 /*
1465 * If we asked for subsystems (or explicitly for no
1466 * subsystems) then they must match
1467 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001468 if ((opts->subsys_mask || opts->none)
1469 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001470 return 0;
1471
1472 return 1;
1473}
1474
Paul Menagec6d57f32009-09-23 15:56:19 -07001475static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1476{
1477 struct cgroupfs_root *root;
1478
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001479 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 return NULL;
1481
1482 root = kzalloc(sizeof(*root), GFP_KERNEL);
1483 if (!root)
1484 return ERR_PTR(-ENOMEM);
1485
1486 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001487
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001488 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001489 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001490 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001491 if (opts->release_agent)
1492 strcpy(root->release_agent_path, opts->release_agent);
1493 if (opts->name)
1494 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001495 if (opts->cpuset_clone_children)
1496 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 return root;
1498}
1499
Tejun Heofa3ca072013-04-14 11:36:56 -07001500static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001501{
Tejun Heofa3ca072013-04-14 11:36:56 -07001502 if (root) {
1503 /* hierarhcy ID shoulid already have been released */
1504 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001505
Tejun Heofa3ca072013-04-14 11:36:56 -07001506 ida_destroy(&root->cgroup_ida);
1507 kfree(root);
1508 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001509}
1510
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511static int cgroup_set_super(struct super_block *sb, void *data)
1512{
1513 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001514 struct cgroup_sb_opts *opts = data;
1515
1516 /* If we don't have a new root, we can't set up a new sb */
1517 if (!opts->new_root)
1518 return -EINVAL;
1519
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001520 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001521
1522 ret = set_anon_super(sb, NULL);
1523 if (ret)
1524 return ret;
1525
Paul Menagec6d57f32009-09-23 15:56:19 -07001526 sb->s_fs_info = opts->new_root;
1527 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528
1529 sb->s_blocksize = PAGE_CACHE_SIZE;
1530 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1531 sb->s_magic = CGROUP_SUPER_MAGIC;
1532 sb->s_op = &cgroup_ops;
1533
1534 return 0;
1535}
1536
1537static int cgroup_get_rootdir(struct super_block *sb)
1538{
Al Viro0df6a632010-12-21 13:29:29 -05001539 static const struct dentry_operations cgroup_dops = {
1540 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001541 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001542 };
1543
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544 struct inode *inode =
1545 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546
1547 if (!inode)
1548 return -ENOMEM;
1549
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550 inode->i_fop = &simple_dir_operations;
1551 inode->i_op = &cgroup_dir_inode_operations;
1552 /* directories start off with i_nlink == 2 (for "." entry) */
1553 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001554 sb->s_root = d_make_root(inode);
1555 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001556 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001557 /* for everything else we want ->d_op set */
1558 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 return 0;
1560}
1561
Al Virof7e83572010-07-26 13:23:11 +04001562static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001563 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001564 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565{
1566 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001567 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568 int ret = 0;
1569 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001570 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001571 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001572
1573 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001574 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001576 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 if (ret)
1578 goto out_err;
1579
1580 /*
1581 * Allocate a new cgroup root. We may not need it if we're
1582 * reusing an existing hierarchy.
1583 */
1584 new_root = cgroup_root_from_opts(&opts);
1585 if (IS_ERR(new_root)) {
1586 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001587 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001588 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001589 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001592 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001595 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001596 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001597 }
1598
Paul Menagec6d57f32009-09-23 15:56:19 -07001599 root = sb->s_fs_info;
1600 BUG_ON(!root);
1601 if (root == opts.new_root) {
1602 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001603 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001604 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001605 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001606 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001607 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001608 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
1610 BUG_ON(sb->s_root != NULL);
1611
1612 ret = cgroup_get_rootdir(sb);
1613 if (ret)
1614 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001615 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616
Paul Menage817929e2007-10-18 23:39:36 -07001617 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001618 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001619 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001620
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001621 /* Check for name clashes with existing mounts */
1622 ret = -EBUSY;
1623 if (strlen(root->name))
1624 for_each_active_root(existing_root)
1625 if (!strcmp(existing_root->name, root->name))
1626 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001627
Paul Menage817929e2007-10-18 23:39:36 -07001628 /*
1629 * We're accessing css_set_count without locking
1630 * css_set_lock here, but that's OK - it can only be
1631 * increased by someone holding cgroup_lock, and
1632 * that's us. The worst that can happen is that we
1633 * have some link structures left over
1634 */
Tejun Heo69d02062013-06-12 21:04:50 -07001635 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001636 if (ret)
1637 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001638
Tejun Heofc76df72013-06-25 11:53:37 -07001639 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1640 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001641 if (ret)
1642 goto unlock_drop;
1643
Tejun Heoa8a648c2013-06-24 15:21:47 -07001644 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001646 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001647 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001649 /*
1650 * There must be no failure case after here, since rebinding
1651 * takes care of subsystems' refcounts, which are explicitly
1652 * dropped in the failure exit path.
1653 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001654
1655 /* EBUSY should be the only error here */
1656 BUG_ON(ret);
1657
Tejun Heo9871bf92013-06-24 15:21:47 -07001658 list_add(&root->root_list, &cgroup_roots);
1659 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660
Li Zefanc12f65d2009-01-07 18:07:42 -08001661 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 root->top_cgroup.dentry = sb->s_root;
1663
Paul Menage817929e2007-10-18 23:39:36 -07001664 /* Link the top cgroup in this hierarchy into all
1665 * the css_set objects */
1666 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001667 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001668 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001669 write_unlock(&css_set_lock);
1670
Tejun Heo69d02062013-06-12 21:04:50 -07001671 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001672
Li Zefanc12f65d2009-01-07 18:07:42 -08001673 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 BUG_ON(root->number_of_cgroups != 1);
1675
eparis@redhat2ce97382011-06-02 21:20:51 +10001676 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001677 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001678 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001679 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001680 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001681 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001682 } else {
1683 /*
1684 * We re-used an existing hierarchy - the new root (if
1685 * any) is not needed
1686 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001687 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001688
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001689 if (root->flags != opts.flags) {
1690 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1691 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1692 ret = -EINVAL;
1693 goto drop_new_super;
1694 } else {
1695 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1696 }
Tejun Heo873fe092013-04-14 20:15:26 -07001697 }
1698
Ben Blumcf5d5942010-03-10 15:22:09 -08001699 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001700 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701 }
1702
Paul Menagec6d57f32009-09-23 15:56:19 -07001703 kfree(opts.release_agent);
1704 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001705 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001708 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001709 mutex_unlock(&cgroup_root_mutex);
1710 mutex_unlock(&cgroup_mutex);
1711 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001713 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001714 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001715 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001716 out_err:
1717 kfree(opts.release_agent);
1718 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001719 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720}
1721
1722static void cgroup_kill_sb(struct super_block *sb) {
1723 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001724 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001725 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726 int ret;
1727
1728 BUG_ON(!root);
1729
1730 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001731 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001734 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001735
1736 /* Rebind all subsystems back to the default hierarchy */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001737 ret = rebind_subsystems(root, 0, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738 /* Shouldn't be able to fail ... */
1739 BUG_ON(ret);
1740
Paul Menage817929e2007-10-18 23:39:36 -07001741 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001742 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001743 * root cgroup
1744 */
1745 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001746
Tejun Heo69d02062013-06-12 21:04:50 -07001747 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1748 list_del(&link->cset_link);
1749 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001750 kfree(link);
1751 }
1752 write_unlock(&css_set_lock);
1753
Paul Menage839ec542009-01-29 14:25:22 -08001754 if (!list_empty(&root->root_list)) {
1755 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001756 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001757 }
Li Zefane5f6a862009-01-07 18:07:41 -08001758
Tejun Heofa3ca072013-04-14 11:36:56 -07001759 cgroup_exit_root_id(root);
1760
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001761 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 mutex_unlock(&cgroup_mutex);
1763
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001764 simple_xattrs_free(&cgrp->xattrs);
1765
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001767 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768}
1769
1770static struct file_system_type cgroup_fs_type = {
1771 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001772 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 .kill_sb = cgroup_kill_sb,
1774};
1775
Greg KH676db4a2010-08-05 13:53:35 -07001776static struct kobject *cgroup_kobj;
1777
Li Zefana043e3b2008-02-23 15:24:09 -08001778/**
1779 * cgroup_path - generate the path of a cgroup
1780 * @cgrp: the cgroup in question
1781 * @buf: the buffer to write the path into
1782 * @buflen: the length of the buffer
1783 *
Li Zefan65dff752013-03-01 15:01:56 +08001784 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1785 *
1786 * We can't generate cgroup path using dentry->d_name, as accessing
1787 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1788 * inode's i_mutex, while on the other hand cgroup_path() can be called
1789 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001791int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792{
Li Zefan65dff752013-03-01 15:01:56 +08001793 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001795
Tejun Heoda1f2962013-04-14 10:32:19 -07001796 if (!cgrp->parent) {
1797 if (strlcpy(buf, "/", buflen) >= buflen)
1798 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 return 0;
1800 }
1801
Tao Ma316eb662012-11-08 21:36:38 +08001802 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001803 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001804
Li Zefan65dff752013-03-01 15:01:56 +08001805 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001806 do {
Li Zefan65dff752013-03-01 15:01:56 +08001807 const char *name = cgroup_name(cgrp);
1808 int len;
1809
1810 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001812 goto out;
1813 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001814
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001816 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001818
1819 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001820 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001821 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001823out:
1824 rcu_read_unlock();
1825 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826}
Ben Blum67523c42010-03-10 15:22:11 -08001827EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828
Tejun Heo857a2be2013-04-14 20:50:08 -07001829/**
1830 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1831 * @task: target task
1832 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1833 * @buf: the buffer to write the path into
1834 * @buflen: the length of the buffer
1835 *
1836 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1837 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1838 * be used inside locks used by cgroup controller callbacks.
1839 */
1840int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1841 char *buf, size_t buflen)
1842{
1843 struct cgroupfs_root *root;
1844 struct cgroup *cgrp = NULL;
1845 int ret = -ENOENT;
1846
1847 mutex_lock(&cgroup_mutex);
1848
1849 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1850 if (root) {
1851 cgrp = task_cgroup_from_root(task, root);
1852 ret = cgroup_path(cgrp, buf, buflen);
1853 }
1854
1855 mutex_unlock(&cgroup_mutex);
1856
1857 return ret;
1858}
1859EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1860
Ben Blum74a11662011-05-26 16:25:20 -07001861/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001862 * Control Group taskset
1863 */
Tejun Heo134d3372011-12-12 18:12:21 -08001864struct task_and_cgroup {
1865 struct task_struct *task;
1866 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001867 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001868};
1869
Tejun Heo2f7ee562011-12-12 18:12:21 -08001870struct cgroup_taskset {
1871 struct task_and_cgroup single;
1872 struct flex_array *tc_array;
1873 int tc_array_len;
1874 int idx;
1875 struct cgroup *cur_cgrp;
1876};
1877
1878/**
1879 * cgroup_taskset_first - reset taskset and return the first task
1880 * @tset: taskset of interest
1881 *
1882 * @tset iteration is initialized and the first task is returned.
1883 */
1884struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1885{
1886 if (tset->tc_array) {
1887 tset->idx = 0;
1888 return cgroup_taskset_next(tset);
1889 } else {
1890 tset->cur_cgrp = tset->single.cgrp;
1891 return tset->single.task;
1892 }
1893}
1894EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1895
1896/**
1897 * cgroup_taskset_next - iterate to the next task in taskset
1898 * @tset: taskset of interest
1899 *
1900 * Return the next task in @tset. Iteration must have been initialized
1901 * with cgroup_taskset_first().
1902 */
1903struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1904{
1905 struct task_and_cgroup *tc;
1906
1907 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1908 return NULL;
1909
1910 tc = flex_array_get(tset->tc_array, tset->idx++);
1911 tset->cur_cgrp = tc->cgrp;
1912 return tc->task;
1913}
1914EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1915
1916/**
1917 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1918 * @tset: taskset of interest
1919 *
1920 * Return the cgroup for the current (last returned) task of @tset. This
1921 * function must be preceded by either cgroup_taskset_first() or
1922 * cgroup_taskset_next().
1923 */
1924struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1925{
1926 return tset->cur_cgrp;
1927}
1928EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1929
1930/**
1931 * cgroup_taskset_size - return the number of tasks in taskset
1932 * @tset: taskset of interest
1933 */
1934int cgroup_taskset_size(struct cgroup_taskset *tset)
1935{
1936 return tset->tc_array ? tset->tc_array_len : 1;
1937}
1938EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1939
1940
Ben Blum74a11662011-05-26 16:25:20 -07001941/*
1942 * cgroup_task_migrate - move a task from one cgroup to another.
1943 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001944 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001945 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001946static void cgroup_task_migrate(struct cgroup *old_cgrp,
1947 struct task_struct *tsk,
1948 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001949{
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001951
1952 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001953 * We are synchronized through threadgroup_lock() against PF_EXITING
1954 * setting such that we can't race against cgroup_exit() changing the
1955 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001956 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001957 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001958 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001959
Ben Blum74a11662011-05-26 16:25:20 -07001960 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001961 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001962 task_unlock(tsk);
1963
1964 /* Update the css_set linked lists if we're using them */
1965 write_lock(&css_set_lock);
1966 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001967 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001968 write_unlock(&css_set_lock);
1969
1970 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001971 * We just gained a reference on old_cset by taking it from the
1972 * task. As trading it for new_cset is protected by cgroup_mutex,
1973 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001974 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001975 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1976 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001977}
1978
Li Zefana043e3b2008-02-23 15:24:09 -08001979/**
Li Zefan081aa452013-03-13 09:17:09 +08001980 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001981 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001982 * @tsk: the task or the leader of the threadgroup to be attached
1983 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001984 *
Tejun Heo257058a2011-12-12 18:12:21 -08001985 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001986 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001987 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001988static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1989 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001990{
1991 int retval, i, group_size;
1992 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001993 struct cgroupfs_root *root = cgrp->root;
1994 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001995 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001996 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001997 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001998 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001999
2000 /*
2001 * step 0: in order to do expensive, possibly blocking operations for
2002 * every thread, we cannot iterate the thread group list, since it needs
2003 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002004 * group - group_rwsem prevents new threads from appearing, and if
2005 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002006 */
Li Zefan081aa452013-03-13 09:17:09 +08002007 if (threadgroup)
2008 group_size = get_nr_threads(tsk);
2009 else
2010 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002011 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002012 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002013 if (!group)
2014 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002015 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002016 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002017 if (retval)
2018 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002019
Ben Blum74a11662011-05-26 16:25:20 -07002020 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002021 /*
2022 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2023 * already PF_EXITING could be freed from underneath us unless we
2024 * take an rcu_read_lock.
2025 */
2026 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002027 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002028 struct task_and_cgroup ent;
2029
Tejun Heocd3d0952011-12-12 18:12:21 -08002030 /* @tsk either already exited or can't exit until the end */
2031 if (tsk->flags & PF_EXITING)
2032 continue;
2033
Ben Blum74a11662011-05-26 16:25:20 -07002034 /* as per above, nr_threads may decrease, but not increase. */
2035 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002036 ent.task = tsk;
2037 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002038 /* nothing to do if this task is already in the cgroup */
2039 if (ent.cgrp == cgrp)
2040 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002041 /*
2042 * saying GFP_ATOMIC has no effect here because we did prealloc
2043 * earlier, but it's good form to communicate our expectations.
2044 */
Tejun Heo134d3372011-12-12 18:12:21 -08002045 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002046 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002047 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002048
2049 if (!threadgroup)
2050 break;
Ben Blum74a11662011-05-26 16:25:20 -07002051 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002052 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002053 /* remember the number of threads in the array for later. */
2054 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002055 tset.tc_array = group;
2056 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002057
Tejun Heo134d3372011-12-12 18:12:21 -08002058 /* methods shouldn't be called if no task is actually migrating */
2059 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002060 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002061 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002062
Ben Blum74a11662011-05-26 16:25:20 -07002063 /*
2064 * step 1: check that we can legitimately attach to the cgroup.
2065 */
Tejun Heo5549c492013-06-24 15:21:48 -07002066 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002067 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002068 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002069 if (retval) {
2070 failed_ss = ss;
2071 goto out_cancel_attach;
2072 }
2073 }
Ben Blum74a11662011-05-26 16:25:20 -07002074 }
2075
2076 /*
2077 * step 2: make sure css_sets exist for all threads to be migrated.
2078 * we use find_css_set, which allocates a new one if necessary.
2079 */
Ben Blum74a11662011-05-26 16:25:20 -07002080 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002081 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002082 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2083 if (!tc->cg) {
2084 retval = -ENOMEM;
2085 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087 }
2088
2089 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002090 * step 3: now that we're guaranteed success wrt the css_sets,
2091 * proceed to move all tasks to the new cgroup. There are no
2092 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002093 */
Ben Blum74a11662011-05-26 16:25:20 -07002094 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002095 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002096 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002097 }
2098 /* nothing is sensitive to fork() after this point. */
2099
2100 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002101 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002102 */
Tejun Heo5549c492013-06-24 15:21:48 -07002103 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002105 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107
2108 /*
2109 * step 5: success! and cleanup
2110 */
Ben Blum74a11662011-05-26 16:25:20 -07002111 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002112out_put_css_set_refs:
2113 if (retval) {
2114 for (i = 0; i < group_size; i++) {
2115 tc = flex_array_get(group, i);
2116 if (!tc->cg)
2117 break;
2118 put_css_set(tc->cg);
2119 }
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002122 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002123 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002124 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002125 break;
Ben Blum74a11662011-05-26 16:25:20 -07002126 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002127 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002128 }
2129 }
Ben Blum74a11662011-05-26 16:25:20 -07002130out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002131 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002132 return retval;
2133}
2134
2135/*
2136 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002137 * function to attach either it or all tasks in its threadgroup. Will lock
2138 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002139 */
2140static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002141{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002143 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002144 int ret;
2145
Ben Blum74a11662011-05-26 16:25:20 -07002146 if (!cgroup_lock_live_group(cgrp))
2147 return -ENODEV;
2148
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002149retry_find_task:
2150 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002151 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002152 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002153 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002154 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 ret= -ESRCH;
2156 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157 }
Ben Blum74a11662011-05-26 16:25:20 -07002158 /*
2159 * even if we're attaching all tasks in the thread group, we
2160 * only need to check permissions on one of them.
2161 */
David Howellsc69e8d92008-11-14 10:39:19 +11002162 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002163 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2164 !uid_eq(cred->euid, tcred->uid) &&
2165 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002166 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167 ret = -EACCES;
2168 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002170 } else
2171 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002172
2173 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002175
2176 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002177 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002178 * trapped in a cpuset, or RT worker may be born in a cgroup
2179 * with no rt_runtime allocated. Just say no.
2180 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002181 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002182 ret = -EINVAL;
2183 rcu_read_unlock();
2184 goto out_unlock_cgroup;
2185 }
2186
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187 get_task_struct(tsk);
2188 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002189
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002190 threadgroup_lock(tsk);
2191 if (threadgroup) {
2192 if (!thread_group_leader(tsk)) {
2193 /*
2194 * a race with de_thread from another thread's exec()
2195 * may strip us of our leadership, if this happens,
2196 * there is no choice but to throw this task away and
2197 * try again; this is
2198 * "double-double-toil-and-trouble-check locking".
2199 */
2200 threadgroup_unlock(tsk);
2201 put_task_struct(tsk);
2202 goto retry_find_task;
2203 }
Li Zefan081aa452013-03-13 09:17:09 +08002204 }
2205
2206 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2207
Tejun Heocd3d0952011-12-12 18:12:21 -08002208 threadgroup_unlock(tsk);
2209
Paul Menagebbcb81d2007-10-18 23:39:32 -07002210 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002211out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002212 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002213 return ret;
2214}
2215
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002216/**
2217 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2218 * @from: attach to all cgroups of a given task
2219 * @tsk: the task to be attached
2220 */
2221int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2222{
2223 struct cgroupfs_root *root;
2224 int retval = 0;
2225
Tejun Heo47cfcd02013-04-07 09:29:51 -07002226 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002227 for_each_active_root(root) {
2228 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2229
2230 retval = cgroup_attach_task(from_cg, tsk, false);
2231 if (retval)
2232 break;
2233 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002234 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002235
2236 return retval;
2237}
2238EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2239
Paul Menageaf351022008-07-25 01:47:01 -07002240static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2241{
Ben Blum74a11662011-05-26 16:25:20 -07002242 return attach_task_by_pid(cgrp, pid, false);
2243}
2244
2245static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2246{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002247 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002248}
2249
Paul Menagee788e062008-07-25 01:46:59 -07002250static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2251 const char *buffer)
2252{
2253 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002254 if (strlen(buffer) >= PATH_MAX)
2255 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002256 if (!cgroup_lock_live_group(cgrp))
2257 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002258 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002259 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002260 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002261 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002262 return 0;
2263}
2264
2265static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2266 struct seq_file *seq)
2267{
2268 if (!cgroup_lock_live_group(cgrp))
2269 return -ENODEV;
2270 seq_puts(seq, cgrp->root->release_agent_path);
2271 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002272 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002273 return 0;
2274}
2275
Tejun Heo873fe092013-04-14 20:15:26 -07002276static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2277 struct seq_file *seq)
2278{
2279 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002280 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002281}
2282
Paul Menage84eea842008-07-25 01:47:00 -07002283/* A buffer size big enough for numbers or short strings */
2284#define CGROUP_LOCAL_BUFFER_SIZE 64
2285
Paul Menagee73d2c62008-04-29 01:00:06 -07002286static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002287 struct file *file,
2288 const char __user *userbuf,
2289 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002290{
Paul Menage84eea842008-07-25 01:47:00 -07002291 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002292 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002293 char *end;
2294
2295 if (!nbytes)
2296 return -EINVAL;
2297 if (nbytes >= sizeof(buffer))
2298 return -E2BIG;
2299 if (copy_from_user(buffer, userbuf, nbytes))
2300 return -EFAULT;
2301
2302 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002303 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002304 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002305 if (*end)
2306 return -EINVAL;
2307 retval = cft->write_u64(cgrp, cft, val);
2308 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002309 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002310 if (*end)
2311 return -EINVAL;
2312 retval = cft->write_s64(cgrp, cft, val);
2313 }
Paul Menage355e0c42007-10-18 23:39:33 -07002314 if (!retval)
2315 retval = nbytes;
2316 return retval;
2317}
2318
Paul Menagedb3b1492008-07-25 01:46:58 -07002319static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2320 struct file *file,
2321 const char __user *userbuf,
2322 size_t nbytes, loff_t *unused_ppos)
2323{
Paul Menage84eea842008-07-25 01:47:00 -07002324 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002325 int retval = 0;
2326 size_t max_bytes = cft->max_write_len;
2327 char *buffer = local_buffer;
2328
2329 if (!max_bytes)
2330 max_bytes = sizeof(local_buffer) - 1;
2331 if (nbytes >= max_bytes)
2332 return -E2BIG;
2333 /* Allocate a dynamic buffer if we need one */
2334 if (nbytes >= sizeof(local_buffer)) {
2335 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2336 if (buffer == NULL)
2337 return -ENOMEM;
2338 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002339 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2340 retval = -EFAULT;
2341 goto out;
2342 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002343
2344 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002345 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002346 if (!retval)
2347 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002348out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002349 if (buffer != local_buffer)
2350 kfree(buffer);
2351 return retval;
2352}
2353
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2355 size_t nbytes, loff_t *ppos)
2356{
2357 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002358 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002359
Tejun Heo54766d42013-06-12 21:04:53 -07002360 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002362 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002363 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002364 if (cft->write_u64 || cft->write_s64)
2365 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002366 if (cft->write_string)
2367 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002368 if (cft->trigger) {
2369 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2370 return ret ? ret : nbytes;
2371 }
Paul Menage355e0c42007-10-18 23:39:33 -07002372 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373}
2374
Paul Menagef4c753b2008-04-29 00:59:56 -07002375static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2376 struct file *file,
2377 char __user *buf, size_t nbytes,
2378 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379{
Paul Menage84eea842008-07-25 01:47:00 -07002380 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002381 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2383
2384 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2385}
2386
Paul Menagee73d2c62008-04-29 01:00:06 -07002387static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2388 struct file *file,
2389 char __user *buf, size_t nbytes,
2390 loff_t *ppos)
2391{
Paul Menage84eea842008-07-25 01:47:00 -07002392 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002393 s64 val = cft->read_s64(cgrp, cft);
2394 int len = sprintf(tmp, "%lld\n", (long long) val);
2395
2396 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2397}
2398
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2400 size_t nbytes, loff_t *ppos)
2401{
2402 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002403 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404
Tejun Heo54766d42013-06-12 21:04:53 -07002405 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002406 return -ENODEV;
2407
2408 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002409 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002410 if (cft->read_u64)
2411 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002412 if (cft->read_s64)
2413 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002414 return -EINVAL;
2415}
2416
Paul Menage91796562008-04-29 01:00:01 -07002417/*
2418 * seqfile ops/methods for returning structured data. Currently just
2419 * supports string->u64 maps, but can be extended in future.
2420 */
2421
2422struct cgroup_seqfile_state {
2423 struct cftype *cft;
2424 struct cgroup *cgroup;
2425};
2426
2427static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2428{
2429 struct seq_file *sf = cb->state;
2430 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2431}
2432
2433static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2434{
2435 struct cgroup_seqfile_state *state = m->private;
2436 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002437 if (cft->read_map) {
2438 struct cgroup_map_cb cb = {
2439 .fill = cgroup_map_add,
2440 .state = m,
2441 };
2442 return cft->read_map(state->cgroup, cft, &cb);
2443 }
2444 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002445}
2446
Adrian Bunk96930a62008-07-25 19:46:21 -07002447static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002448{
2449 struct seq_file *seq = file->private_data;
2450 kfree(seq->private);
2451 return single_release(inode, file);
2452}
2453
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002454static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002455 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002456 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002457 .llseek = seq_lseek,
2458 .release = cgroup_seqfile_release,
2459};
2460
Paul Menageddbcc7e2007-10-18 23:39:30 -07002461static int cgroup_file_open(struct inode *inode, struct file *file)
2462{
2463 int err;
2464 struct cftype *cft;
2465
2466 err = generic_file_open(inode, file);
2467 if (err)
2468 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002470
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002471 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002472 struct cgroup_seqfile_state *state;
2473
2474 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002475 if (!state)
2476 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002477
Paul Menage91796562008-04-29 01:00:01 -07002478 state->cft = cft;
2479 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2480 file->f_op = &cgroup_seqfile_operations;
2481 err = single_open(file, cgroup_seqfile_show, state);
2482 if (err < 0)
2483 kfree(state);
2484 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002485 err = cft->open(inode, file);
2486 else
2487 err = 0;
2488
2489 return err;
2490}
2491
2492static int cgroup_file_release(struct inode *inode, struct file *file)
2493{
2494 struct cftype *cft = __d_cft(file->f_dentry);
2495 if (cft->release)
2496 return cft->release(inode, file);
2497 return 0;
2498}
2499
2500/*
2501 * cgroup_rename - Only allow simple rename of directories in place.
2502 */
2503static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2504 struct inode *new_dir, struct dentry *new_dentry)
2505{
Li Zefan65dff752013-03-01 15:01:56 +08002506 int ret;
2507 struct cgroup_name *name, *old_name;
2508 struct cgroup *cgrp;
2509
2510 /*
2511 * It's convinient to use parent dir's i_mutex to protected
2512 * cgrp->name.
2513 */
2514 lockdep_assert_held(&old_dir->i_mutex);
2515
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2517 return -ENOTDIR;
2518 if (new_dentry->d_inode)
2519 return -EEXIST;
2520 if (old_dir != new_dir)
2521 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002522
2523 cgrp = __d_cgrp(old_dentry);
2524
Tejun Heo6db8e852013-06-14 11:18:22 -07002525 /*
2526 * This isn't a proper migration and its usefulness is very
2527 * limited. Disallow if sane_behavior.
2528 */
2529 if (cgroup_sane_behavior(cgrp))
2530 return -EPERM;
2531
Li Zefan65dff752013-03-01 15:01:56 +08002532 name = cgroup_alloc_name(new_dentry);
2533 if (!name)
2534 return -ENOMEM;
2535
2536 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2537 if (ret) {
2538 kfree(name);
2539 return ret;
2540 }
2541
2542 old_name = cgrp->name;
2543 rcu_assign_pointer(cgrp->name, name);
2544
2545 kfree_rcu(old_name, rcu_head);
2546 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002547}
2548
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002549static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2550{
2551 if (S_ISDIR(dentry->d_inode->i_mode))
2552 return &__d_cgrp(dentry)->xattrs;
2553 else
Li Zefan712317a2013-04-18 23:09:52 -07002554 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002555}
2556
2557static inline int xattr_enabled(struct dentry *dentry)
2558{
2559 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002560 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002561}
2562
2563static bool is_valid_xattr(const char *name)
2564{
2565 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2566 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2567 return true;
2568 return false;
2569}
2570
2571static int cgroup_setxattr(struct dentry *dentry, const char *name,
2572 const void *val, size_t size, int flags)
2573{
2574 if (!xattr_enabled(dentry))
2575 return -EOPNOTSUPP;
2576 if (!is_valid_xattr(name))
2577 return -EINVAL;
2578 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2579}
2580
2581static int cgroup_removexattr(struct dentry *dentry, const char *name)
2582{
2583 if (!xattr_enabled(dentry))
2584 return -EOPNOTSUPP;
2585 if (!is_valid_xattr(name))
2586 return -EINVAL;
2587 return simple_xattr_remove(__d_xattrs(dentry), name);
2588}
2589
2590static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2591 void *buf, size_t size)
2592{
2593 if (!xattr_enabled(dentry))
2594 return -EOPNOTSUPP;
2595 if (!is_valid_xattr(name))
2596 return -EINVAL;
2597 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2598}
2599
2600static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2601{
2602 if (!xattr_enabled(dentry))
2603 return -EOPNOTSUPP;
2604 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2605}
2606
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002607static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002608 .read = cgroup_file_read,
2609 .write = cgroup_file_write,
2610 .llseek = generic_file_llseek,
2611 .open = cgroup_file_open,
2612 .release = cgroup_file_release,
2613};
2614
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615static const struct inode_operations cgroup_file_inode_operations = {
2616 .setxattr = cgroup_setxattr,
2617 .getxattr = cgroup_getxattr,
2618 .listxattr = cgroup_listxattr,
2619 .removexattr = cgroup_removexattr,
2620};
2621
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002622static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002623 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002624 .mkdir = cgroup_mkdir,
2625 .rmdir = cgroup_rmdir,
2626 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002627 .setxattr = cgroup_setxattr,
2628 .getxattr = cgroup_getxattr,
2629 .listxattr = cgroup_listxattr,
2630 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631};
2632
Al Viro00cd8dd2012-06-10 17:13:09 -04002633static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002634{
2635 if (dentry->d_name.len > NAME_MAX)
2636 return ERR_PTR(-ENAMETOOLONG);
2637 d_add(dentry, NULL);
2638 return NULL;
2639}
2640
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002641/*
2642 * Check if a file is a control file
2643 */
2644static inline struct cftype *__file_cft(struct file *file)
2645{
Al Viro496ad9a2013-01-23 17:07:38 -05002646 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002647 return ERR_PTR(-EINVAL);
2648 return __d_cft(file->f_dentry);
2649}
2650
Al Viroa5e7ed32011-07-26 01:55:55 -04002651static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002652 struct super_block *sb)
2653{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654 struct inode *inode;
2655
2656 if (!dentry)
2657 return -ENOENT;
2658 if (dentry->d_inode)
2659 return -EEXIST;
2660
2661 inode = cgroup_new_inode(mode, sb);
2662 if (!inode)
2663 return -ENOMEM;
2664
2665 if (S_ISDIR(mode)) {
2666 inode->i_op = &cgroup_dir_inode_operations;
2667 inode->i_fop = &simple_dir_operations;
2668
2669 /* start off with i_nlink == 2 (for "." entry) */
2670 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002671 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002672
Tejun Heob8a2df62012-11-19 08:13:37 -08002673 /*
2674 * Control reaches here with cgroup_mutex held.
2675 * @inode->i_mutex should nest outside cgroup_mutex but we
2676 * want to populate it immediately without releasing
2677 * cgroup_mutex. As @inode isn't visible to anyone else
2678 * yet, trylock will always succeed without affecting
2679 * lockdep checks.
2680 */
2681 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 } else if (S_ISREG(mode)) {
2683 inode->i_size = 0;
2684 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002685 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002687 d_instantiate(dentry, inode);
2688 dget(dentry); /* Extra count - pin the dentry in core */
2689 return 0;
2690}
2691
Li Zefan099fca32009-04-02 16:57:29 -07002692/**
2693 * cgroup_file_mode - deduce file mode of a control file
2694 * @cft: the control file in question
2695 *
2696 * returns cft->mode if ->mode is not 0
2697 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2698 * returns S_IRUGO if it has only a read handler
2699 * returns S_IWUSR if it has only a write hander
2700 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002701static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002702{
Al Viroa5e7ed32011-07-26 01:55:55 -04002703 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002704
2705 if (cft->mode)
2706 return cft->mode;
2707
2708 if (cft->read || cft->read_u64 || cft->read_s64 ||
2709 cft->read_map || cft->read_seq_string)
2710 mode |= S_IRUGO;
2711
2712 if (cft->write || cft->write_u64 || cft->write_s64 ||
2713 cft->write_string || cft->trigger)
2714 mode |= S_IWUSR;
2715
2716 return mode;
2717}
2718
Tejun Heodb0416b2012-04-01 12:09:55 -07002719static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002720 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721{
Paul Menagebd89aab2007-10-18 23:40:44 -07002722 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002727 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002729
Tejun Heo93438622013-04-14 20:15:25 -07002730 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 strcpy(name, subsys->name);
2732 strcat(name, ".");
2733 }
2734 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002735
Paul Menageddbcc7e2007-10-18 23:39:30 -07002736 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002737
2738 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2739 if (!cfe)
2740 return -ENOMEM;
2741
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002743 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 goto out;
2746 }
2747
Li Zefand6cbf352013-05-14 19:44:20 +08002748 cfe->type = (void *)cft;
2749 cfe->dentry = dentry;
2750 dentry->d_fsdata = cfe;
2751 simple_xattrs_init(&cfe->xattrs);
2752
Tejun Heo05ef1d72012-04-01 12:09:56 -07002753 mode = cgroup_file_mode(cft);
2754 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2755 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002756 list_add_tail(&cfe->node, &parent->files);
2757 cfe = NULL;
2758 }
2759 dput(dentry);
2760out:
2761 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 return error;
2763}
2764
Tejun Heo79578622012-04-01 12:09:56 -07002765static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002766 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002767{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002768 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002769 int err, ret = 0;
2770
2771 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002772 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002773 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2774 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002775 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2776 continue;
2777 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2778 continue;
2779
Li Zefan2739d3c2013-01-21 18:18:33 +08002780 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002781 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002782 if (err)
2783 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2784 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002785 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002786 } else {
2787 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002788 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002789 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002790 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002791}
2792
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002794 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002795{
2796 /*
2797 * Thanks to the entanglement with vfs inode locking, we can't walk
2798 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002799 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2800 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002801 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002802 mutex_lock(&cgroup_mutex);
2803}
2804
2805static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002806 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002807 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002808{
2809 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002810 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002811 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002812 struct dentry *prev = NULL;
2813 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002814 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002815
2816 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002817 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002818 !atomic_inc_not_zero(&sb->s_active)) {
2819 mutex_unlock(&cgroup_mutex);
2820 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002821 }
2822
Li Zefane8c82d22013-06-18 18:48:37 +08002823 /*
2824 * All cgroups which are created after we drop cgroup_mutex will
2825 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002826 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002827 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002828 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002829
Tejun Heo8e3f6542012-04-01 12:09:55 -07002830 mutex_unlock(&cgroup_mutex);
2831
Li Zefane8c82d22013-06-18 18:48:37 +08002832 /* @root always needs to be updated */
2833 inode = root->dentry->d_inode;
2834 mutex_lock(&inode->i_mutex);
2835 mutex_lock(&cgroup_mutex);
2836 cgroup_addrm_files(root, ss, cfts, is_add);
2837 mutex_unlock(&cgroup_mutex);
2838 mutex_unlock(&inode->i_mutex);
2839
2840 /* add/rm files for all cgroups created before */
2841 rcu_read_lock();
2842 cgroup_for_each_descendant_pre(cgrp, root) {
2843 if (cgroup_is_dead(cgrp))
2844 continue;
2845
2846 inode = cgrp->dentry->d_inode;
2847 dget(cgrp->dentry);
2848 rcu_read_unlock();
2849
2850 dput(prev);
2851 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852
2853 mutex_lock(&inode->i_mutex);
2854 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002855 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002856 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002857 mutex_unlock(&cgroup_mutex);
2858 mutex_unlock(&inode->i_mutex);
2859
Li Zefane8c82d22013-06-18 18:48:37 +08002860 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002861 }
Li Zefane8c82d22013-06-18 18:48:37 +08002862 rcu_read_unlock();
2863 dput(prev);
2864 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002865}
2866
2867/**
2868 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2869 * @ss: target cgroup subsystem
2870 * @cfts: zero-length name terminated array of cftypes
2871 *
2872 * Register @cfts to @ss. Files described by @cfts are created for all
2873 * existing cgroups to which @ss is attached and all future cgroups will
2874 * have them too. This function can be called anytime whether @ss is
2875 * attached or not.
2876 *
2877 * Returns 0 on successful registration, -errno on failure. Note that this
2878 * function currently returns 0 as long as @cfts registration is successful
2879 * even if some file creation attempts on existing cgroups fail.
2880 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002881int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002882{
2883 struct cftype_set *set;
2884
2885 set = kzalloc(sizeof(*set), GFP_KERNEL);
2886 if (!set)
2887 return -ENOMEM;
2888
2889 cgroup_cfts_prepare();
2890 set->cfts = cfts;
2891 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002892 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002893
2894 return 0;
2895}
2896EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2897
Li Zefana043e3b2008-02-23 15:24:09 -08002898/**
Tejun Heo79578622012-04-01 12:09:56 -07002899 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2900 * @ss: target cgroup subsystem
2901 * @cfts: zero-length name terminated array of cftypes
2902 *
2903 * Unregister @cfts from @ss. Files described by @cfts are removed from
2904 * all existing cgroups to which @ss is attached and all future cgroups
2905 * won't have them either. This function can be called anytime whether @ss
2906 * is attached or not.
2907 *
2908 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2909 * registered with @ss.
2910 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002911int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002912{
2913 struct cftype_set *set;
2914
2915 cgroup_cfts_prepare();
2916
2917 list_for_each_entry(set, &ss->cftsets, node) {
2918 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002919 list_del(&set->node);
2920 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002921 cgroup_cfts_commit(ss, cfts, false);
2922 return 0;
2923 }
2924 }
2925
2926 cgroup_cfts_commit(ss, NULL, false);
2927 return -ENOENT;
2928}
2929
2930/**
Li Zefana043e3b2008-02-23 15:24:09 -08002931 * cgroup_task_count - count the number of tasks in a cgroup.
2932 * @cgrp: the cgroup in question
2933 *
2934 * Return the number of tasks in the cgroup.
2935 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002936int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002937{
2938 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002939 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002940
Paul Menage817929e2007-10-18 23:39:36 -07002941 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002942 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2943 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002944 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002945 return count;
2946}
2947
2948/*
Paul Menage817929e2007-10-18 23:39:36 -07002949 * Advance a list_head iterator. The iterator should be positioned at
2950 * the start of a css_set
2951 */
Tejun Heo69d02062013-06-12 21:04:50 -07002952static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002953{
Tejun Heo69d02062013-06-12 21:04:50 -07002954 struct list_head *l = it->cset_link;
2955 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002956 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002957
2958 /* Advance to the next non-empty css_set */
2959 do {
2960 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002961 if (l == &cgrp->cset_links) {
2962 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002963 return;
2964 }
Tejun Heo69d02062013-06-12 21:04:50 -07002965 link = list_entry(l, struct cgrp_cset_link, cset_link);
2966 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002967 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002968 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002969 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002970}
2971
Cliff Wickman31a7df02008-02-07 00:14:42 -08002972/*
2973 * To reduce the fork() overhead for systems that are not actually
2974 * using their cgroups capability, we don't maintain the lists running
2975 * through each css_set to its tasks until we see the list actually
2976 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002977 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002978static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979{
2980 struct task_struct *p, *g;
2981 write_lock(&css_set_lock);
2982 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002983 /*
2984 * We need tasklist_lock because RCU is not safe against
2985 * while_each_thread(). Besides, a forking task that has passed
2986 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2987 * is not guaranteed to have its child immediately visible in the
2988 * tasklist if we walk through it with RCU.
2989 */
2990 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991 do_each_thread(g, p) {
2992 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002993 /*
2994 * We should check if the process is exiting, otherwise
2995 * it will race with cgroup_exit() in that the list
2996 * entry won't be deleted though the process has exited.
2997 */
2998 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002999 list_add(&p->cg_list, &p->cgroups->tasks);
3000 task_unlock(p);
3001 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003002 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003003 write_unlock(&css_set_lock);
3004}
3005
Tejun Heo574bd9f2012-11-09 09:12:29 -08003006/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003007 * cgroup_next_sibling - find the next sibling of a given cgroup
3008 * @pos: the current cgroup
3009 *
3010 * This function returns the next sibling of @pos and should be called
3011 * under RCU read lock. The only requirement is that @pos is accessible.
3012 * The next sibling is guaranteed to be returned regardless of @pos's
3013 * state.
3014 */
3015struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3016{
3017 struct cgroup *next;
3018
3019 WARN_ON_ONCE(!rcu_read_lock_held());
3020
3021 /*
3022 * @pos could already have been removed. Once a cgroup is removed,
3023 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003024 * changes. As CGRP_DEAD assertion is serialized and happens
3025 * before the cgroup is taken off the ->sibling list, if we see it
3026 * unasserted, it's guaranteed that the next sibling hasn't
3027 * finished its grace period even if it's already removed, and thus
3028 * safe to dereference from this RCU critical section. If
3029 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3030 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003031 */
Tejun Heo54766d42013-06-12 21:04:53 -07003032 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003033 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3034 if (&next->sibling != &pos->parent->children)
3035 return next;
3036 return NULL;
3037 }
3038
3039 /*
3040 * Can't dereference the next pointer. Each cgroup is given a
3041 * monotonically increasing unique serial number and always
3042 * appended to the sibling list, so the next one can be found by
3043 * walking the parent's children until we see a cgroup with higher
3044 * serial number than @pos's.
3045 *
3046 * While this path can be slow, it's taken only when either the
3047 * current cgroup is removed or iteration and removal race.
3048 */
3049 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3050 if (next->serial_nr > pos->serial_nr)
3051 return next;
3052 return NULL;
3053}
3054EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3055
3056/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003057 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3058 * @pos: the current position (%NULL to initiate traversal)
3059 * @cgroup: cgroup whose descendants to walk
3060 *
3061 * To be used by cgroup_for_each_descendant_pre(). Find the next
3062 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003063 *
3064 * While this function requires RCU read locking, it doesn't require the
3065 * whole traversal to be contained in a single RCU critical section. This
3066 * function will return the correct next descendant as long as both @pos
3067 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003068 */
3069struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3070 struct cgroup *cgroup)
3071{
3072 struct cgroup *next;
3073
3074 WARN_ON_ONCE(!rcu_read_lock_held());
3075
3076 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003077 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003079
3080 /* visit the first child if exists */
3081 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3082 if (next)
3083 return next;
3084
3085 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003086 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003087 next = cgroup_next_sibling(pos);
3088 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003089 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003090 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003091 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003092
3093 return NULL;
3094}
3095EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3096
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003097/**
3098 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3099 * @pos: cgroup of interest
3100 *
3101 * Return the rightmost descendant of @pos. If there's no descendant,
3102 * @pos is returned. This can be used during pre-order traversal to skip
3103 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003104 *
3105 * While this function requires RCU read locking, it doesn't require the
3106 * whole traversal to be contained in a single RCU critical section. This
3107 * function will return the correct rightmost descendant as long as @pos is
3108 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003109 */
3110struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3111{
3112 struct cgroup *last, *tmp;
3113
3114 WARN_ON_ONCE(!rcu_read_lock_held());
3115
3116 do {
3117 last = pos;
3118 /* ->prev isn't RCU safe, walk ->next till the end */
3119 pos = NULL;
3120 list_for_each_entry_rcu(tmp, &last->children, sibling)
3121 pos = tmp;
3122 } while (pos);
3123
3124 return last;
3125}
3126EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3127
Tejun Heo574bd9f2012-11-09 09:12:29 -08003128static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3129{
3130 struct cgroup *last;
3131
3132 do {
3133 last = pos;
3134 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3135 sibling);
3136 } while (pos);
3137
3138 return last;
3139}
3140
3141/**
3142 * cgroup_next_descendant_post - find the next descendant for post-order walk
3143 * @pos: the current position (%NULL to initiate traversal)
3144 * @cgroup: cgroup whose descendants to walk
3145 *
3146 * To be used by cgroup_for_each_descendant_post(). Find the next
3147 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003148 *
3149 * While this function requires RCU read locking, it doesn't require the
3150 * whole traversal to be contained in a single RCU critical section. This
3151 * function will return the correct next descendant as long as both @pos
3152 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003153 */
3154struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3155 struct cgroup *cgroup)
3156{
3157 struct cgroup *next;
3158
3159 WARN_ON_ONCE(!rcu_read_lock_held());
3160
3161 /* if first iteration, visit the leftmost descendant */
3162 if (!pos) {
3163 next = cgroup_leftmost_descendant(cgroup);
3164 return next != cgroup ? next : NULL;
3165 }
3166
3167 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003168 next = cgroup_next_sibling(pos);
3169 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003170 return cgroup_leftmost_descendant(next);
3171
3172 /* no sibling left, visit parent */
3173 next = pos->parent;
3174 return next != cgroup ? next : NULL;
3175}
3176EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3177
Paul Menagebd89aab2007-10-18 23:40:44 -07003178void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003179 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003180{
3181 /*
3182 * The first time anyone tries to iterate across a cgroup,
3183 * we need to enable the list linking each css_set to its
3184 * tasks, and fix up all existing tasks.
3185 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003186 if (!use_task_css_set_links)
3187 cgroup_enable_task_cg_lists();
3188
Paul Menage817929e2007-10-18 23:39:36 -07003189 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003190 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003191 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003192}
3193
Paul Menagebd89aab2007-10-18 23:40:44 -07003194struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003195 struct cgroup_iter *it)
3196{
3197 struct task_struct *res;
3198 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003199 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003200
3201 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003202 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003203 return NULL;
3204 res = list_entry(l, struct task_struct, cg_list);
3205 /* Advance iterator to find next entry */
3206 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003207 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3208 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003209 /* We reached the end of this task list - move on to
3210 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003211 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003212 } else {
3213 it->task = l;
3214 }
3215 return res;
3216}
3217
Paul Menagebd89aab2007-10-18 23:40:44 -07003218void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003219 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003220{
3221 read_unlock(&css_set_lock);
3222}
3223
Cliff Wickman31a7df02008-02-07 00:14:42 -08003224static inline int started_after_time(struct task_struct *t1,
3225 struct timespec *time,
3226 struct task_struct *t2)
3227{
3228 int start_diff = timespec_compare(&t1->start_time, time);
3229 if (start_diff > 0) {
3230 return 1;
3231 } else if (start_diff < 0) {
3232 return 0;
3233 } else {
3234 /*
3235 * Arbitrarily, if two processes started at the same
3236 * time, we'll say that the lower pointer value
3237 * started first. Note that t2 may have exited by now
3238 * so this may not be a valid pointer any longer, but
3239 * that's fine - it still serves to distinguish
3240 * between two tasks started (effectively) simultaneously.
3241 */
3242 return t1 > t2;
3243 }
3244}
3245
3246/*
3247 * This function is a callback from heap_insert() and is used to order
3248 * the heap.
3249 * In this case we order the heap in descending task start time.
3250 */
3251static inline int started_after(void *p1, void *p2)
3252{
3253 struct task_struct *t1 = p1;
3254 struct task_struct *t2 = p2;
3255 return started_after_time(t1, &t2->start_time, t2);
3256}
3257
3258/**
3259 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3260 * @scan: struct cgroup_scanner containing arguments for the scan
3261 *
3262 * Arguments include pointers to callback functions test_task() and
3263 * process_task().
3264 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3265 * and if it returns true, call process_task() for it also.
3266 * The test_task pointer may be NULL, meaning always true (select all tasks).
3267 * Effectively duplicates cgroup_iter_{start,next,end}()
3268 * but does not lock css_set_lock for the call to process_task().
3269 * The struct cgroup_scanner may be embedded in any structure of the caller's
3270 * creation.
3271 * It is guaranteed that process_task() will act on every task that
3272 * is a member of the cgroup for the duration of this call. This
3273 * function may or may not call process_task() for tasks that exit
3274 * or move to a different cgroup during the call, or are forked or
3275 * move into the cgroup during the call.
3276 *
3277 * Note that test_task() may be called with locks held, and may in some
3278 * situations be called multiple times for the same task, so it should
3279 * be cheap.
3280 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3281 * pre-allocated and will be used for heap operations (and its "gt" member will
3282 * be overwritten), else a temporary heap will be used (allocation of which
3283 * may cause this function to fail).
3284 */
3285int cgroup_scan_tasks(struct cgroup_scanner *scan)
3286{
3287 int retval, i;
3288 struct cgroup_iter it;
3289 struct task_struct *p, *dropped;
3290 /* Never dereference latest_task, since it's not refcounted */
3291 struct task_struct *latest_task = NULL;
3292 struct ptr_heap tmp_heap;
3293 struct ptr_heap *heap;
3294 struct timespec latest_time = { 0, 0 };
3295
3296 if (scan->heap) {
3297 /* The caller supplied our heap and pre-allocated its memory */
3298 heap = scan->heap;
3299 heap->gt = &started_after;
3300 } else {
3301 /* We need to allocate our own heap memory */
3302 heap = &tmp_heap;
3303 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3304 if (retval)
3305 /* cannot allocate the heap */
3306 return retval;
3307 }
3308
3309 again:
3310 /*
3311 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3312 * to determine which are of interest, and using the scanner's
3313 * "process_task" callback to process any of them that need an update.
3314 * Since we don't want to hold any locks during the task updates,
3315 * gather tasks to be processed in a heap structure.
3316 * The heap is sorted by descending task start time.
3317 * If the statically-sized heap fills up, we overflow tasks that
3318 * started later, and in future iterations only consider tasks that
3319 * started after the latest task in the previous pass. This
3320 * guarantees forward progress and that we don't miss any tasks.
3321 */
3322 heap->size = 0;
3323 cgroup_iter_start(scan->cg, &it);
3324 while ((p = cgroup_iter_next(scan->cg, &it))) {
3325 /*
3326 * Only affect tasks that qualify per the caller's callback,
3327 * if he provided one
3328 */
3329 if (scan->test_task && !scan->test_task(p, scan))
3330 continue;
3331 /*
3332 * Only process tasks that started after the last task
3333 * we processed
3334 */
3335 if (!started_after_time(p, &latest_time, latest_task))
3336 continue;
3337 dropped = heap_insert(heap, p);
3338 if (dropped == NULL) {
3339 /*
3340 * The new task was inserted; the heap wasn't
3341 * previously full
3342 */
3343 get_task_struct(p);
3344 } else if (dropped != p) {
3345 /*
3346 * The new task was inserted, and pushed out a
3347 * different task
3348 */
3349 get_task_struct(p);
3350 put_task_struct(dropped);
3351 }
3352 /*
3353 * Else the new task was newer than anything already in
3354 * the heap and wasn't inserted
3355 */
3356 }
3357 cgroup_iter_end(scan->cg, &it);
3358
3359 if (heap->size) {
3360 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003361 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003362 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003363 latest_time = q->start_time;
3364 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003365 }
3366 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003367 scan->process_task(q, scan);
3368 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003369 }
3370 /*
3371 * If we had to process any tasks at all, scan again
3372 * in case some of them were in the middle of forking
3373 * children that didn't get processed.
3374 * Not the most efficient way to do it, but it avoids
3375 * having to take callback_mutex in the fork path
3376 */
3377 goto again;
3378 }
3379 if (heap == &tmp_heap)
3380 heap_free(&tmp_heap);
3381 return 0;
3382}
3383
Tejun Heo8cc99342013-04-07 09:29:50 -07003384static void cgroup_transfer_one_task(struct task_struct *task,
3385 struct cgroup_scanner *scan)
3386{
3387 struct cgroup *new_cgroup = scan->data;
3388
Tejun Heo47cfcd02013-04-07 09:29:51 -07003389 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003390 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003391 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003392}
3393
3394/**
3395 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3396 * @to: cgroup to which the tasks will be moved
3397 * @from: cgroup in which the tasks currently reside
3398 */
3399int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3400{
3401 struct cgroup_scanner scan;
3402
3403 scan.cg = from;
3404 scan.test_task = NULL; /* select all tasks in cgroup */
3405 scan.process_task = cgroup_transfer_one_task;
3406 scan.heap = NULL;
3407 scan.data = to;
3408
3409 return cgroup_scan_tasks(&scan);
3410}
3411
Paul Menage817929e2007-10-18 23:39:36 -07003412/*
Ben Blum102a7752009-09-23 15:56:26 -07003413 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003414 *
3415 * Reading this file can return large amounts of data if a cgroup has
3416 * *lots* of attached tasks. So it may need several calls to read(),
3417 * but we cannot guarantee that the information we produce is correct
3418 * unless we produce it entirely atomically.
3419 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003420 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003421
Li Zefan24528252012-01-20 11:58:43 +08003422/* which pidlist file are we talking about? */
3423enum cgroup_filetype {
3424 CGROUP_FILE_PROCS,
3425 CGROUP_FILE_TASKS,
3426};
3427
3428/*
3429 * A pidlist is a list of pids that virtually represents the contents of one
3430 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3431 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3432 * to the cgroup.
3433 */
3434struct cgroup_pidlist {
3435 /*
3436 * used to find which pidlist is wanted. doesn't change as long as
3437 * this particular list stays in the list.
3438 */
3439 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3440 /* array of xids */
3441 pid_t *list;
3442 /* how many elements the above list has */
3443 int length;
3444 /* how many files are using the current array */
3445 int use_count;
3446 /* each of these stored in a list by its cgroup */
3447 struct list_head links;
3448 /* pointer to the cgroup we belong to, for list removal purposes */
3449 struct cgroup *owner;
3450 /* protects the other fields */
3451 struct rw_semaphore mutex;
3452};
3453
Paul Menagebbcb81d2007-10-18 23:39:32 -07003454/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003455 * The following two functions "fix" the issue where there are more pids
3456 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3457 * TODO: replace with a kernel-wide solution to this problem
3458 */
3459#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3460static void *pidlist_allocate(int count)
3461{
3462 if (PIDLIST_TOO_LARGE(count))
3463 return vmalloc(count * sizeof(pid_t));
3464 else
3465 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3466}
3467static void pidlist_free(void *p)
3468{
3469 if (is_vmalloc_addr(p))
3470 vfree(p);
3471 else
3472 kfree(p);
3473}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003474
3475/*
Ben Blum102a7752009-09-23 15:56:26 -07003476 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003477 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003478 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003479static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003480{
Ben Blum102a7752009-09-23 15:56:26 -07003481 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003482
3483 /*
3484 * we presume the 0th element is unique, so i starts at 1. trivial
3485 * edge cases first; no work needs to be done for either
3486 */
3487 if (length == 0 || length == 1)
3488 return length;
3489 /* src and dest walk down the list; dest counts unique elements */
3490 for (src = 1; src < length; src++) {
3491 /* find next unique element */
3492 while (list[src] == list[src-1]) {
3493 src++;
3494 if (src == length)
3495 goto after;
3496 }
3497 /* dest always points to where the next unique element goes */
3498 list[dest] = list[src];
3499 dest++;
3500 }
3501after:
Ben Blum102a7752009-09-23 15:56:26 -07003502 return dest;
3503}
3504
3505static int cmppid(const void *a, const void *b)
3506{
3507 return *(pid_t *)a - *(pid_t *)b;
3508}
3509
3510/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003511 * find the appropriate pidlist for our purpose (given procs vs tasks)
3512 * returns with the lock on that pidlist already held, and takes care
3513 * of the use count, or returns NULL with no locks held if we're out of
3514 * memory.
3515 */
3516static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3517 enum cgroup_filetype type)
3518{
3519 struct cgroup_pidlist *l;
3520 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003521 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003522
Ben Blum72a8cb32009-09-23 15:56:27 -07003523 /*
3524 * We can't drop the pidlist_mutex before taking the l->mutex in case
3525 * the last ref-holder is trying to remove l from the list at the same
3526 * time. Holding the pidlist_mutex precludes somebody taking whichever
3527 * list we find out from under us - compare release_pid_array().
3528 */
3529 mutex_lock(&cgrp->pidlist_mutex);
3530 list_for_each_entry(l, &cgrp->pidlists, links) {
3531 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003532 /* make sure l doesn't vanish out from under us */
3533 down_write(&l->mutex);
3534 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003535 return l;
3536 }
3537 }
3538 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003539 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003540 if (!l) {
3541 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003542 return l;
3543 }
3544 init_rwsem(&l->mutex);
3545 down_write(&l->mutex);
3546 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003547 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003548 l->owner = cgrp;
3549 list_add(&l->links, &cgrp->pidlists);
3550 mutex_unlock(&cgrp->pidlist_mutex);
3551 return l;
3552}
3553
3554/*
Ben Blum102a7752009-09-23 15:56:26 -07003555 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3556 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003557static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3558 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003559{
3560 pid_t *array;
3561 int length;
3562 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003563 struct cgroup_iter it;
3564 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003565 struct cgroup_pidlist *l;
3566
3567 /*
3568 * If cgroup gets more users after we read count, we won't have
3569 * enough space - tough. This race is indistinguishable to the
3570 * caller from the case that the additional cgroup users didn't
3571 * show up until sometime later on.
3572 */
3573 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003574 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003575 if (!array)
3576 return -ENOMEM;
3577 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003578 cgroup_iter_start(cgrp, &it);
3579 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003580 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003581 break;
Ben Blum102a7752009-09-23 15:56:26 -07003582 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003583 if (type == CGROUP_FILE_PROCS)
3584 pid = task_tgid_vnr(tsk);
3585 else
3586 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003587 if (pid > 0) /* make sure to only use valid results */
3588 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003589 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003590 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003591 length = n;
3592 /* now sort & (if procs) strip out duplicates */
3593 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003595 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003596 l = cgroup_pidlist_find(cgrp, type);
3597 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003598 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003599 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003600 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003601 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003602 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003603 l->list = array;
3604 l->length = length;
3605 l->use_count++;
3606 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003607 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003608 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003609}
3610
Balbir Singh846c7bb2007-10-18 23:39:44 -07003611/**
Li Zefana043e3b2008-02-23 15:24:09 -08003612 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003613 * @stats: cgroupstats to fill information into
3614 * @dentry: A dentry entry belonging to the cgroup for which stats have
3615 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003616 *
3617 * Build and fill cgroupstats so that taskstats can export it to user
3618 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003619 */
3620int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3621{
3622 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003623 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003624 struct cgroup_iter it;
3625 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003626
Balbir Singh846c7bb2007-10-18 23:39:44 -07003627 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003628 * Validate dentry by checking the superblock operations,
3629 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003630 */
Li Zefan33d283b2008-11-19 15:36:48 -08003631 if (dentry->d_sb->s_op != &cgroup_ops ||
3632 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003633 goto err;
3634
3635 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003636 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003637
Paul Menagebd89aab2007-10-18 23:40:44 -07003638 cgroup_iter_start(cgrp, &it);
3639 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003640 switch (tsk->state) {
3641 case TASK_RUNNING:
3642 stats->nr_running++;
3643 break;
3644 case TASK_INTERRUPTIBLE:
3645 stats->nr_sleeping++;
3646 break;
3647 case TASK_UNINTERRUPTIBLE:
3648 stats->nr_uninterruptible++;
3649 break;
3650 case TASK_STOPPED:
3651 stats->nr_stopped++;
3652 break;
3653 default:
3654 if (delayacct_is_task_waiting_on_io(tsk))
3655 stats->nr_io_wait++;
3656 break;
3657 }
3658 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003659 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003660
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661err:
3662 return ret;
3663}
3664
Paul Menage8f3ff202009-09-23 15:56:25 -07003665
Paul Menagecc31edc2008-10-18 20:28:04 -07003666/*
Ben Blum102a7752009-09-23 15:56:26 -07003667 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003668 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003669 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003670 */
3671
Ben Blum102a7752009-09-23 15:56:26 -07003672static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003673{
3674 /*
3675 * Initially we receive a position value that corresponds to
3676 * one more than the last pid shown (or 0 on the first call or
3677 * after a seek to the start). Use a binary-search to find the
3678 * next pid to display, if any
3679 */
Ben Blum102a7752009-09-23 15:56:26 -07003680 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003681 int index = 0, pid = *pos;
3682 int *iter;
3683
Ben Blum102a7752009-09-23 15:56:26 -07003684 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003685 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003686 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003687
Paul Menagecc31edc2008-10-18 20:28:04 -07003688 while (index < end) {
3689 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003690 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003691 index = mid;
3692 break;
Ben Blum102a7752009-09-23 15:56:26 -07003693 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003694 index = mid + 1;
3695 else
3696 end = mid;
3697 }
3698 }
3699 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003700 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003701 return NULL;
3702 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003703 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 *pos = *iter;
3705 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003706}
3707
Ben Blum102a7752009-09-23 15:56:26 -07003708static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003709{
Ben Blum102a7752009-09-23 15:56:26 -07003710 struct cgroup_pidlist *l = s->private;
3711 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003712}
3713
Ben Blum102a7752009-09-23 15:56:26 -07003714static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003715{
Ben Blum102a7752009-09-23 15:56:26 -07003716 struct cgroup_pidlist *l = s->private;
3717 pid_t *p = v;
3718 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003719 /*
3720 * Advance to the next pid in the array. If this goes off the
3721 * end, we're done
3722 */
3723 p++;
3724 if (p >= end) {
3725 return NULL;
3726 } else {
3727 *pos = *p;
3728 return p;
3729 }
3730}
3731
Ben Blum102a7752009-09-23 15:56:26 -07003732static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003733{
3734 return seq_printf(s, "%d\n", *(int *)v);
3735}
3736
Ben Blum102a7752009-09-23 15:56:26 -07003737/*
3738 * seq_operations functions for iterating on pidlists through seq_file -
3739 * independent of whether it's tasks or procs
3740 */
3741static const struct seq_operations cgroup_pidlist_seq_operations = {
3742 .start = cgroup_pidlist_start,
3743 .stop = cgroup_pidlist_stop,
3744 .next = cgroup_pidlist_next,
3745 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003746};
3747
Ben Blum102a7752009-09-23 15:56:26 -07003748static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003749{
Ben Blum72a8cb32009-09-23 15:56:27 -07003750 /*
3751 * the case where we're the last user of this particular pidlist will
3752 * have us remove it from the cgroup's list, which entails taking the
3753 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3754 * pidlist_mutex, we have to take pidlist_mutex first.
3755 */
3756 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003757 down_write(&l->mutex);
3758 BUG_ON(!l->use_count);
3759 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003760 /* we're the last user if refcount is 0; remove and free */
3761 list_del(&l->links);
3762 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003763 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003764 put_pid_ns(l->key.ns);
3765 up_write(&l->mutex);
3766 kfree(l);
3767 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003768 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003769 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003770 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003771}
3772
Ben Blum102a7752009-09-23 15:56:26 -07003773static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003774{
Ben Blum102a7752009-09-23 15:56:26 -07003775 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003776 if (!(file->f_mode & FMODE_READ))
3777 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003778 /*
3779 * the seq_file will only be initialized if the file was opened for
3780 * reading; hence we check if it's not null only in that case.
3781 */
3782 l = ((struct seq_file *)file->private_data)->private;
3783 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 return seq_release(inode, file);
3785}
3786
Ben Blum102a7752009-09-23 15:56:26 -07003787static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003788 .read = seq_read,
3789 .llseek = seq_lseek,
3790 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003791 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003792};
3793
3794/*
Ben Blum102a7752009-09-23 15:56:26 -07003795 * The following functions handle opens on a file that displays a pidlist
3796 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3797 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 */
Ben Blum102a7752009-09-23 15:56:26 -07003799/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003800static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003801{
3802 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003803 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003804 int retval;
3805
3806 /* Nothing to do for write-only files */
3807 if (!(file->f_mode & FMODE_READ))
3808 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003809
Ben Blum102a7752009-09-23 15:56:26 -07003810 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003811 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003812 if (retval)
3813 return retval;
3814 /* configure file information */
3815 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816
Ben Blum102a7752009-09-23 15:56:26 -07003817 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003819 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003820 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003821 }
Ben Blum102a7752009-09-23 15:56:26 -07003822 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003823 return 0;
3824}
Ben Blum102a7752009-09-23 15:56:26 -07003825static int cgroup_tasks_open(struct inode *unused, struct file *file)
3826{
Ben Blum72a8cb32009-09-23 15:56:27 -07003827 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003828}
3829static int cgroup_procs_open(struct inode *unused, struct file *file)
3830{
Ben Blum72a8cb32009-09-23 15:56:27 -07003831 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003832}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003833
Paul Menagebd89aab2007-10-18 23:40:44 -07003834static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003835 struct cftype *cft)
3836{
Paul Menagebd89aab2007-10-18 23:40:44 -07003837 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003838}
3839
Paul Menage6379c102008-07-25 01:47:01 -07003840static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3841 struct cftype *cft,
3842 u64 val)
3843{
3844 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3845 if (val)
3846 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3847 else
3848 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3849 return 0;
3850}
3851
Paul Menagebbcb81d2007-10-18 23:39:32 -07003852/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003853 * When dput() is called asynchronously, if umount has been done and
3854 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3855 * there's a small window that vfs will see the root dentry with non-zero
3856 * refcnt and trigger BUG().
3857 *
3858 * That's why we hold a reference before dput() and drop it right after.
3859 */
3860static void cgroup_dput(struct cgroup *cgrp)
3861{
3862 struct super_block *sb = cgrp->root->sb;
3863
3864 atomic_inc(&sb->s_active);
3865 dput(cgrp->dentry);
3866 deactivate_super(sb);
3867}
3868
3869/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003870 * Unregister event and free resources.
3871 *
3872 * Gets called from workqueue.
3873 */
3874static void cgroup_event_remove(struct work_struct *work)
3875{
3876 struct cgroup_event *event = container_of(work, struct cgroup_event,
3877 remove);
3878 struct cgroup *cgrp = event->cgrp;
3879
Li Zefan810cbee2013-02-18 18:56:14 +08003880 remove_wait_queue(event->wqh, &event->wait);
3881
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3883
Li Zefan810cbee2013-02-18 18:56:14 +08003884 /* Notify userspace the event is going away. */
3885 eventfd_signal(event->eventfd, 1);
3886
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003887 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003888 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003889 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003890}
3891
3892/*
3893 * Gets called on POLLHUP on eventfd when user closes it.
3894 *
3895 * Called with wqh->lock held and interrupts disabled.
3896 */
3897static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3898 int sync, void *key)
3899{
3900 struct cgroup_event *event = container_of(wait,
3901 struct cgroup_event, wait);
3902 struct cgroup *cgrp = event->cgrp;
3903 unsigned long flags = (unsigned long)key;
3904
3905 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003906 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003907 * If the event has been detached at cgroup removal, we
3908 * can simply return knowing the other side will cleanup
3909 * for us.
3910 *
3911 * We can't race against event freeing since the other
3912 * side will require wqh->lock via remove_wait_queue(),
3913 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003914 */
Li Zefan810cbee2013-02-18 18:56:14 +08003915 spin_lock(&cgrp->event_list_lock);
3916 if (!list_empty(&event->list)) {
3917 list_del_init(&event->list);
3918 /*
3919 * We are in atomic context, but cgroup_event_remove()
3920 * may sleep, so we have to call it in workqueue.
3921 */
3922 schedule_work(&event->remove);
3923 }
3924 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003925 }
3926
3927 return 0;
3928}
3929
3930static void cgroup_event_ptable_queue_proc(struct file *file,
3931 wait_queue_head_t *wqh, poll_table *pt)
3932{
3933 struct cgroup_event *event = container_of(pt,
3934 struct cgroup_event, pt);
3935
3936 event->wqh = wqh;
3937 add_wait_queue(wqh, &event->wait);
3938}
3939
3940/*
3941 * Parse input and register new cgroup event handler.
3942 *
3943 * Input must be in format '<event_fd> <control_fd> <args>'.
3944 * Interpretation of args is defined by control file implementation.
3945 */
3946static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3947 const char *buffer)
3948{
3949 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003950 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003951 unsigned int efd, cfd;
3952 struct file *efile = NULL;
3953 struct file *cfile = NULL;
3954 char *endp;
3955 int ret;
3956
3957 efd = simple_strtoul(buffer, &endp, 10);
3958 if (*endp != ' ')
3959 return -EINVAL;
3960 buffer = endp + 1;
3961
3962 cfd = simple_strtoul(buffer, &endp, 10);
3963 if ((*endp != ' ') && (*endp != '\0'))
3964 return -EINVAL;
3965 buffer = endp + 1;
3966
3967 event = kzalloc(sizeof(*event), GFP_KERNEL);
3968 if (!event)
3969 return -ENOMEM;
3970 event->cgrp = cgrp;
3971 INIT_LIST_HEAD(&event->list);
3972 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3973 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3974 INIT_WORK(&event->remove, cgroup_event_remove);
3975
3976 efile = eventfd_fget(efd);
3977 if (IS_ERR(efile)) {
3978 ret = PTR_ERR(efile);
3979 goto fail;
3980 }
3981
3982 event->eventfd = eventfd_ctx_fileget(efile);
3983 if (IS_ERR(event->eventfd)) {
3984 ret = PTR_ERR(event->eventfd);
3985 goto fail;
3986 }
3987
3988 cfile = fget(cfd);
3989 if (!cfile) {
3990 ret = -EBADF;
3991 goto fail;
3992 }
3993
3994 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003995 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003996 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003997 if (ret < 0)
3998 goto fail;
3999
4000 event->cft = __file_cft(cfile);
4001 if (IS_ERR(event->cft)) {
4002 ret = PTR_ERR(event->cft);
4003 goto fail;
4004 }
4005
Li Zefanf1690072013-02-18 14:13:35 +08004006 /*
4007 * The file to be monitored must be in the same cgroup as
4008 * cgroup.event_control is.
4009 */
4010 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4011 if (cgrp_cfile != cgrp) {
4012 ret = -EINVAL;
4013 goto fail;
4014 }
4015
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004016 if (!event->cft->register_event || !event->cft->unregister_event) {
4017 ret = -EINVAL;
4018 goto fail;
4019 }
4020
4021 ret = event->cft->register_event(cgrp, event->cft,
4022 event->eventfd, buffer);
4023 if (ret)
4024 goto fail;
4025
Li Zefan7ef70e42013-04-26 11:58:03 -07004026 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004027
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004028 /*
4029 * Events should be removed after rmdir of cgroup directory, but before
4030 * destroying subsystem state objects. Let's take reference to cgroup
4031 * directory dentry to do that.
4032 */
4033 dget(cgrp->dentry);
4034
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004035 spin_lock(&cgrp->event_list_lock);
4036 list_add(&event->list, &cgrp->event_list);
4037 spin_unlock(&cgrp->event_list_lock);
4038
4039 fput(cfile);
4040 fput(efile);
4041
4042 return 0;
4043
4044fail:
4045 if (cfile)
4046 fput(cfile);
4047
4048 if (event && event->eventfd && !IS_ERR(event->eventfd))
4049 eventfd_ctx_put(event->eventfd);
4050
4051 if (!IS_ERR_OR_NULL(efile))
4052 fput(efile);
4053
4054 kfree(event);
4055
4056 return ret;
4057}
4058
Daniel Lezcano97978e62010-10-27 15:33:35 -07004059static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4060 struct cftype *cft)
4061{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004062 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004063}
4064
4065static int cgroup_clone_children_write(struct cgroup *cgrp,
4066 struct cftype *cft,
4067 u64 val)
4068{
4069 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004070 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004071 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004072 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004073 return 0;
4074}
4075
Tejun Heod5c56ce2013-06-03 19:14:34 -07004076static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004077 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004078 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004079 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004080 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004081 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004082 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004083 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004084 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004085 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004086 .write_string = cgroup_write_event_control,
4087 .mode = S_IWUGO,
4088 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004089 {
4090 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004091 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004092 .read_u64 = cgroup_clone_children_read,
4093 .write_u64 = cgroup_clone_children_write,
4094 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004095 {
Tejun Heo873fe092013-04-14 20:15:26 -07004096 .name = "cgroup.sane_behavior",
4097 .flags = CFTYPE_ONLY_ON_ROOT,
4098 .read_seq_string = cgroup_sane_behavior_show,
4099 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004100
4101 /*
4102 * Historical crazy stuff. These don't have "cgroup." prefix and
4103 * don't exist if sane_behavior. If you're depending on these, be
4104 * prepared to be burned.
4105 */
4106 {
4107 .name = "tasks",
4108 .flags = CFTYPE_INSANE, /* use "procs" instead */
4109 .open = cgroup_tasks_open,
4110 .write_u64 = cgroup_tasks_write,
4111 .release = cgroup_pidlist_release,
4112 .mode = S_IRUGO | S_IWUSR,
4113 },
4114 {
4115 .name = "notify_on_release",
4116 .flags = CFTYPE_INSANE,
4117 .read_u64 = cgroup_read_notify_on_release,
4118 .write_u64 = cgroup_write_notify_on_release,
4119 },
Tejun Heo873fe092013-04-14 20:15:26 -07004120 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004121 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004122 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004123 .read_seq_string = cgroup_release_agent_show,
4124 .write_string = cgroup_release_agent_write,
4125 .max_write_len = PATH_MAX,
4126 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004127 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004128};
4129
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004130/**
4131 * cgroup_populate_dir - selectively creation of files in a directory
4132 * @cgrp: target cgroup
4133 * @base_files: true if the base files should be added
4134 * @subsys_mask: mask of the subsystem ids whose files should be added
4135 */
4136static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4137 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004138{
4139 int err;
4140 struct cgroup_subsys *ss;
4141
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004142 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004143 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004144 if (err < 0)
4145 return err;
4146 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004147
Tejun Heo8e3f6542012-04-01 12:09:55 -07004148 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004149 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004150 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004151 if (!test_bit(ss->subsys_id, &subsys_mask))
4152 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004153
Tejun Heodb0416b2012-04-01 12:09:55 -07004154 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004155 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004156 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004157
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004158 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004159 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004160 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4161 /*
4162 * Update id->css pointer and make this css visible from
4163 * CSS ID functions. This pointer will be dereferened
4164 * from RCU-read-side without locks.
4165 */
4166 if (css->id)
4167 rcu_assign_pointer(css->id->css, css);
4168 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169
4170 return 0;
4171}
4172
Tejun Heo48ddbe12012-04-01 12:09:56 -07004173static void css_dput_fn(struct work_struct *work)
4174{
4175 struct cgroup_subsys_state *css =
4176 container_of(work, struct cgroup_subsys_state, dput_work);
4177
Li Zefan1c8158e2013-06-18 18:41:10 +08004178 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004179}
4180
Tejun Heod3daf282013-06-13 19:39:16 -07004181static void css_release(struct percpu_ref *ref)
4182{
4183 struct cgroup_subsys_state *css =
4184 container_of(ref, struct cgroup_subsys_state, refcnt);
4185
4186 schedule_work(&css->dput_work);
4187}
4188
Paul Menageddbcc7e2007-10-18 23:39:30 -07004189static void init_cgroup_css(struct cgroup_subsys_state *css,
4190 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004191 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004192{
Paul Menagebd89aab2007-10-18 23:40:44 -07004193 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004194 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004195 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004196 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004197 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004198 BUG_ON(cgrp->subsys[ss->subsys_id]);
4199 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004200
4201 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004202 * css holds an extra ref to @cgrp->dentry which is put on the last
4203 * css_put(). dput() requires process context, which css_put() may
4204 * be called without. @css->dput_work will be used to invoke
4205 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004206 */
4207 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004208}
4209
Tejun Heob1929db2012-11-19 08:13:38 -08004210/* invoke ->post_create() on a new CSS and mark it online if successful */
4211static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004212{
Tejun Heob1929db2012-11-19 08:13:38 -08004213 int ret = 0;
4214
Tejun Heoa31f2d32012-11-19 08:13:37 -08004215 lockdep_assert_held(&cgroup_mutex);
4216
Tejun Heo92fb9742012-11-19 08:13:38 -08004217 if (ss->css_online)
4218 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004219 if (!ret)
4220 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4221 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004222}
4223
4224/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4225static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4226 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4227{
4228 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4229
4230 lockdep_assert_held(&cgroup_mutex);
4231
4232 if (!(css->flags & CSS_ONLINE))
4233 return;
4234
Li Zefand7eeac12013-03-12 15:35:59 -07004235 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004236 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004237
4238 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4239}
4240
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241/*
Li Zefana043e3b2008-02-23 15:24:09 -08004242 * cgroup_create - create a cgroup
4243 * @parent: cgroup that will be parent of the new cgroup
4244 * @dentry: dentry of the new cgroup
4245 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246 *
Li Zefana043e3b2008-02-23 15:24:09 -08004247 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004248 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004250 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251{
Paul Menagebd89aab2007-10-18 23:40:44 -07004252 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004253 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004254 struct cgroupfs_root *root = parent->root;
4255 int err = 0;
4256 struct cgroup_subsys *ss;
4257 struct super_block *sb = root->sb;
4258
Tejun Heo0a950f62012-11-19 09:02:12 -08004259 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004260 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4261 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262 return -ENOMEM;
4263
Li Zefan65dff752013-03-01 15:01:56 +08004264 name = cgroup_alloc_name(dentry);
4265 if (!name)
4266 goto err_free_cgrp;
4267 rcu_assign_pointer(cgrp->name, name);
4268
Tejun Heo0a950f62012-11-19 09:02:12 -08004269 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4270 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004271 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004272
Tejun Heo976c06b2012-11-05 09:16:59 -08004273 /*
4274 * Only live parents can have children. Note that the liveliness
4275 * check isn't strictly necessary because cgroup_mkdir() and
4276 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4277 * anyway so that locking is contained inside cgroup proper and we
4278 * don't get nasty surprises if we ever grow another caller.
4279 */
4280 if (!cgroup_lock_live_group(parent)) {
4281 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004282 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004283 }
4284
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285 /* Grab a reference on the superblock so the hierarchy doesn't
4286 * get deleted on unmount if there are child cgroups. This
4287 * can be done outside cgroup_mutex, since the sb can't
4288 * disappear while someone has an open control file on the
4289 * fs */
4290 atomic_inc(&sb->s_active);
4291
Paul Menagecc31edc2008-10-18 20:28:04 -07004292 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004293
Li Zefanfe1c06c2013-01-24 14:30:22 +08004294 dentry->d_fsdata = cgrp;
4295 cgrp->dentry = dentry;
4296
Paul Menagebd89aab2007-10-18 23:40:44 -07004297 cgrp->parent = parent;
4298 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004299
Li Zefanb6abdb02008-03-04 14:28:19 -08004300 if (notify_on_release(parent))
4301 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4302
Tejun Heo2260e7f2012-11-19 08:13:38 -08004303 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4304 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004305
Tejun Heo5549c492013-06-24 15:21:48 -07004306 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004307 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004308
Tejun Heo92fb9742012-11-19 08:13:38 -08004309 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004310 if (IS_ERR(css)) {
4311 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004312 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004313 }
Tejun Heod3daf282013-06-13 19:39:16 -07004314
4315 err = percpu_ref_init(&css->refcnt, css_release);
4316 if (err)
4317 goto err_free_all;
4318
Paul Menagebd89aab2007-10-18 23:40:44 -07004319 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004320
Li Zefan4528fd02010-02-02 13:44:10 -08004321 if (ss->use_id) {
4322 err = alloc_css_id(ss, parent, cgrp);
4323 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004324 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004325 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004326 }
4327
Tejun Heo4e139af2012-11-19 08:13:36 -08004328 /*
4329 * Create directory. cgroup_create_file() returns with the new
4330 * directory locked on success so that it can be populated without
4331 * dropping cgroup_mutex.
4332 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004333 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004335 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004336 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337
Tejun Heo00356bd2013-06-18 11:14:22 -07004338 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004339
Tejun Heo4e139af2012-11-19 08:13:36 -08004340 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004341 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4342 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004343
Tejun Heob1929db2012-11-19 08:13:38 -08004344 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004345 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004346 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004347
Li Zefan415cf072013-04-08 14:35:02 +08004348 /* hold a ref to the parent's dentry */
4349 dget(parent->dentry);
4350
Tejun Heob1929db2012-11-19 08:13:38 -08004351 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004352 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004353 err = online_css(ss, cgrp);
4354 if (err)
4355 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004356
4357 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4358 parent->parent) {
4359 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",
4360 current->comm, current->pid, ss->name);
4361 if (!strcmp(ss->name, "memory"))
4362 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4363 ss->warned_broken_hierarchy = true;
4364 }
Tejun Heoa8638032012-11-09 09:12:29 -08004365 }
4366
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004367 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004368 if (err)
4369 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004370
4371 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004372 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
4374 return 0;
4375
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004376err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004377 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004378 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4379
4380 if (css) {
4381 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004382 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004383 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 /* Release the reference count that we took on the superblock */
4387 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004388err_free_id:
4389 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004390err_free_name:
4391 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004392err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004393 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004394 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004395
4396err_destroy:
4397 cgroup_destroy_locked(cgrp);
4398 mutex_unlock(&cgroup_mutex);
4399 mutex_unlock(&dentry->d_inode->i_mutex);
4400 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004401}
4402
Al Viro18bb1db2011-07-26 01:41:39 -04004403static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404{
4405 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4406
4407 /* the vfs holds inode->i_mutex already */
4408 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4409}
4410
Tejun Heod3daf282013-06-13 19:39:16 -07004411static void cgroup_css_killed(struct cgroup *cgrp)
4412{
4413 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4414 return;
4415
4416 /* percpu ref's of all css's are killed, kick off the next step */
4417 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4418 schedule_work(&cgrp->destroy_work);
4419}
4420
4421static void css_ref_killed_fn(struct percpu_ref *ref)
4422{
4423 struct cgroup_subsys_state *css =
4424 container_of(ref, struct cgroup_subsys_state, refcnt);
4425
4426 cgroup_css_killed(css->cgroup);
4427}
4428
4429/**
4430 * cgroup_destroy_locked - the first stage of cgroup destruction
4431 * @cgrp: cgroup to be destroyed
4432 *
4433 * css's make use of percpu refcnts whose killing latency shouldn't be
4434 * exposed to userland and are RCU protected. Also, cgroup core needs to
4435 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4436 * invoked. To satisfy all the requirements, destruction is implemented in
4437 * the following two steps.
4438 *
4439 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4440 * userland visible parts and start killing the percpu refcnts of
4441 * css's. Set up so that the next stage will be kicked off once all
4442 * the percpu refcnts are confirmed to be killed.
4443 *
4444 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4445 * rest of destruction. Once all cgroup references are gone, the
4446 * cgroup is RCU-freed.
4447 *
4448 * This function implements s1. After this step, @cgrp is gone as far as
4449 * the userland is concerned and a new cgroup with the same name may be
4450 * created. As cgroup doesn't care about the names internally, this
4451 * doesn't cause any problem.
4452 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004453static int cgroup_destroy_locked(struct cgroup *cgrp)
4454 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004455{
Tejun Heo42809dd2012-11-19 08:13:37 -08004456 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004457 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004458 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004459 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460
Tejun Heo42809dd2012-11-19 08:13:37 -08004461 lockdep_assert_held(&d->d_inode->i_mutex);
4462 lockdep_assert_held(&cgroup_mutex);
4463
Tejun Heoddd69142013-06-12 21:04:54 -07004464 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004465 * css_set_lock synchronizes access to ->cset_links and prevents
4466 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004467 */
4468 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004469 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004470 read_unlock(&css_set_lock);
4471 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004473
Tejun Heo1a90dd52012-11-05 09:16:59 -08004474 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004475 * Block new css_tryget() by killing css refcnts. cgroup core
4476 * guarantees that, by the time ->css_offline() is invoked, no new
4477 * css reference will be given out via css_tryget(). We can't
4478 * simply call percpu_ref_kill() and proceed to offlining css's
4479 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4480 * as killed on all CPUs on return.
4481 *
4482 * Use percpu_ref_kill_and_confirm() to get notifications as each
4483 * css is confirmed to be seen as killed on all CPUs. The
4484 * notification callback keeps track of the number of css's to be
4485 * killed and schedules cgroup_offline_fn() to perform the rest of
4486 * destruction once the percpu refs of all css's are confirmed to
4487 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004488 */
Tejun Heod3daf282013-06-13 19:39:16 -07004489 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004490 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004491 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4492
Tejun Heod3daf282013-06-13 19:39:16 -07004493 /*
4494 * Killing would put the base ref, but we need to keep it
4495 * alive until after ->css_offline.
4496 */
4497 percpu_ref_get(&css->refcnt);
4498
4499 atomic_inc(&cgrp->css_kill_cnt);
4500 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004501 }
Tejun Heod3daf282013-06-13 19:39:16 -07004502 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004503
4504 /*
4505 * Mark @cgrp dead. This prevents further task migration and child
4506 * creation by disabling cgroup_lock_live_group(). Note that
4507 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4508 * resume iteration after dropping RCU read lock. See
4509 * cgroup_next_sibling() for details.
4510 */
Tejun Heo54766d42013-06-12 21:04:53 -07004511 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004512
Tejun Heo455050d2013-06-13 19:27:41 -07004513 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4514 raw_spin_lock(&release_list_lock);
4515 if (!list_empty(&cgrp->release_list))
4516 list_del_init(&cgrp->release_list);
4517 raw_spin_unlock(&release_list_lock);
4518
4519 /*
4520 * Remove @cgrp directory. The removal puts the base ref but we
4521 * aren't quite done with @cgrp yet, so hold onto it.
4522 */
4523 dget(d);
4524 cgroup_d_remove_dir(d);
4525
4526 /*
4527 * Unregister events and notify userspace.
4528 * Notify userspace about cgroup removing only after rmdir of cgroup
4529 * directory to avoid race between userspace and kernelspace.
4530 */
4531 spin_lock(&cgrp->event_list_lock);
4532 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4533 list_del_init(&event->list);
4534 schedule_work(&event->remove);
4535 }
4536 spin_unlock(&cgrp->event_list_lock);
4537
Tejun Heoea15f8c2013-06-13 19:27:42 -07004538 return 0;
4539};
4540
Tejun Heod3daf282013-06-13 19:39:16 -07004541/**
4542 * cgroup_offline_fn - the second step of cgroup destruction
4543 * @work: cgroup->destroy_free_work
4544 *
4545 * This function is invoked from a work item for a cgroup which is being
4546 * destroyed after the percpu refcnts of all css's are guaranteed to be
4547 * seen as killed on all CPUs, and performs the rest of destruction. This
4548 * is the second step of destruction described in the comment above
4549 * cgroup_destroy_locked().
4550 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004551static void cgroup_offline_fn(struct work_struct *work)
4552{
4553 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4554 struct cgroup *parent = cgrp->parent;
4555 struct dentry *d = cgrp->dentry;
4556 struct cgroup_subsys *ss;
4557
4558 mutex_lock(&cgroup_mutex);
4559
Tejun Heod3daf282013-06-13 19:39:16 -07004560 /*
4561 * css_tryget() is guaranteed to fail now. Tell subsystems to
4562 * initate destruction.
4563 */
Tejun Heo5549c492013-06-24 15:21:48 -07004564 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004565 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004566
4567 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004568 * Put the css refs from cgroup_destroy_locked(). Each css holds
4569 * an extra reference to the cgroup's dentry and cgroup removal
4570 * proceeds regardless of css refs. On the last put of each css,
4571 * whenever that may be, the extra dentry ref is put so that dentry
4572 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004573 */
Tejun Heo5549c492013-06-24 15:21:48 -07004574 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004575 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004576
Paul Menage999cd8a2009-01-07 18:08:36 -08004577 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004578 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004579
Paul Menageddbcc7e2007-10-18 23:39:30 -07004580 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581
Paul Menagebd89aab2007-10-18 23:40:44 -07004582 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004583 check_for_release(parent);
4584
Tejun Heoea15f8c2013-06-13 19:27:42 -07004585 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004586}
4587
Tejun Heo42809dd2012-11-19 08:13:37 -08004588static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4589{
4590 int ret;
4591
4592 mutex_lock(&cgroup_mutex);
4593 ret = cgroup_destroy_locked(dentry->d_fsdata);
4594 mutex_unlock(&cgroup_mutex);
4595
4596 return ret;
4597}
4598
Tejun Heo8e3f6542012-04-01 12:09:55 -07004599static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4600{
4601 INIT_LIST_HEAD(&ss->cftsets);
4602
4603 /*
4604 * base_cftset is embedded in subsys itself, no need to worry about
4605 * deregistration.
4606 */
4607 if (ss->base_cftypes) {
4608 ss->base_cftset.cfts = ss->base_cftypes;
4609 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4610 }
4611}
4612
Li Zefan06a11922008-04-29 01:00:07 -07004613static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004614{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004615 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004616
4617 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004618
Tejun Heo648bb562012-11-19 08:13:36 -08004619 mutex_lock(&cgroup_mutex);
4620
Tejun Heo8e3f6542012-04-01 12:09:55 -07004621 /* init base cftset */
4622 cgroup_init_cftsets(ss);
4623
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004625 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4626 ss->root = &cgroup_dummy_root;
4627 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004628 /* We don't handle early failures gracefully */
4629 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004630 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004631
Li Zefane8d55fd2008-04-29 01:00:13 -07004632 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004633 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004634 * newly registered, all tasks and hence the
4635 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004636 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637
4638 need_forkexit_callback |= ss->fork || ss->exit;
4639
Li Zefane8d55fd2008-04-29 01:00:13 -07004640 /* At system boot, before all subsystems have been
4641 * registered, no tasks have been forked, so we don't
4642 * need to invoke fork callbacks here. */
4643 BUG_ON(!list_empty(&init_task.tasks));
4644
Tejun Heo9871bf92013-06-24 15:21:47 -07004645 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004646
Tejun Heo648bb562012-11-19 08:13:36 -08004647 mutex_unlock(&cgroup_mutex);
4648
Ben Blume6a11052010-03-10 15:22:09 -08004649 /* this function shouldn't be used with modular subsystems, since they
4650 * need to register a subsys_id, among other things */
4651 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004652}
4653
4654/**
Ben Blume6a11052010-03-10 15:22:09 -08004655 * cgroup_load_subsys: load and register a modular subsystem at runtime
4656 * @ss: the subsystem to load
4657 *
4658 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004659 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004660 * up for use. If the subsystem is built-in anyway, work is delegated to the
4661 * simpler cgroup_init_subsys.
4662 */
4663int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4664{
Ben Blume6a11052010-03-10 15:22:09 -08004665 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004666 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004667 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004668 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004669 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004670
4671 /* check name and function validity */
4672 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004673 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004674 return -EINVAL;
4675
4676 /*
4677 * we don't support callbacks in modular subsystems. this check is
4678 * before the ss->module check for consistency; a subsystem that could
4679 * be a module should still have no callbacks even if the user isn't
4680 * compiling it as one.
4681 */
4682 if (ss->fork || ss->exit)
4683 return -EINVAL;
4684
4685 /*
4686 * an optionally modular subsystem is built-in: we want to do nothing,
4687 * since cgroup_init_subsys will have already taken care of it.
4688 */
4689 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004690 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004691 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004692 return 0;
4693 }
4694
Tejun Heo8e3f6542012-04-01 12:09:55 -07004695 /* init base cftset */
4696 cgroup_init_cftsets(ss);
4697
Ben Blume6a11052010-03-10 15:22:09 -08004698 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004699 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004700
4701 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004702 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004703 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004704 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004705 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004706 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004707 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004708 /* failure case - need to deassign the cgroup_subsys[] slot. */
4709 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004710 mutex_unlock(&cgroup_mutex);
4711 return PTR_ERR(css);
4712 }
4713
Tejun Heo9871bf92013-06-24 15:21:47 -07004714 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4715 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004716
4717 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004718 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004719 /* init_idr must be after init_cgroup_css because it sets css->id. */
4720 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004721 ret = cgroup_init_idr(ss, css);
4722 if (ret)
4723 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004724 }
4725
4726 /*
4727 * Now we need to entangle the css into the existing css_sets. unlike
4728 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4729 * will need a new pointer to it; done by iterating the css_set_table.
4730 * furthermore, modifying the existing css_sets will corrupt the hash
4731 * table state, so each changed css_set will need its hash recomputed.
4732 * this is all done under the css_set_lock.
4733 */
4734 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004735 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004736 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004737 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004738 continue;
4739 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004740 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004741 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004742 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004743 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004744 key = css_set_hash(cset->subsys);
4745 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004746 }
4747 write_unlock(&css_set_lock);
4748
Tejun Heo9871bf92013-06-24 15:21:47 -07004749 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004750 if (ret)
4751 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004752
Ben Blume6a11052010-03-10 15:22:09 -08004753 /* success! */
4754 mutex_unlock(&cgroup_mutex);
4755 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004756
4757err_unload:
4758 mutex_unlock(&cgroup_mutex);
4759 /* @ss can't be mounted here as try_module_get() would fail */
4760 cgroup_unload_subsys(ss);
4761 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004762}
4763EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4764
4765/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004766 * cgroup_unload_subsys: unload a modular subsystem
4767 * @ss: the subsystem to unload
4768 *
4769 * This function should be called in a modular subsystem's exitcall. When this
4770 * function is invoked, the refcount on the subsystem's module will be 0, so
4771 * the subsystem will not be attached to any hierarchy.
4772 */
4773void cgroup_unload_subsys(struct cgroup_subsys *ss)
4774{
Tejun Heo69d02062013-06-12 21:04:50 -07004775 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004776
4777 BUG_ON(ss->module == NULL);
4778
4779 /*
4780 * we shouldn't be called if the subsystem is in use, and the use of
4781 * try_module_get in parse_cgroupfs_options should ensure that it
4782 * doesn't start being used while we're killing it off.
4783 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004784 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004785
4786 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004787
Tejun Heo9871bf92013-06-24 15:21:47 -07004788 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004789
Tejun Heoc897ff62013-02-27 17:03:49 -08004790 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004791 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004792
Ben Blumcf5d5942010-03-10 15:22:09 -08004793 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004794 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004795
Tejun Heo9871bf92013-06-24 15:21:47 -07004796 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004797 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004798
4799 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004800 * disentangle the css from all css_sets attached to the dummy
4801 * top. as in loading, we need to pay our respects to the hashtable
4802 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004803 */
4804 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004805 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004806 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004807 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004808
Tejun Heo5abb8852013-06-12 21:04:49 -07004809 hash_del(&cset->hlist);
4810 cset->subsys[ss->subsys_id] = NULL;
4811 key = css_set_hash(cset->subsys);
4812 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004813 }
4814 write_unlock(&css_set_lock);
4815
4816 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004817 * remove subsystem's css from the cgroup_dummy_top and free it -
4818 * need to free before marking as null because ss->css_free needs
4819 * the cgrp->subsys pointer to find their state. note that this
4820 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004821 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004822 ss->css_free(cgroup_dummy_top);
4823 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004824
4825 mutex_unlock(&cgroup_mutex);
4826}
4827EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4828
4829/**
Li Zefana043e3b2008-02-23 15:24:09 -08004830 * cgroup_init_early - cgroup initialization at system boot
4831 *
4832 * Initialize cgroups at system boot, and initialize any
4833 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004834 */
4835int __init cgroup_init_early(void)
4836{
Tejun Heo30159ec2013-06-25 11:53:37 -07004837 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004838 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004839
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004840 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004841 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004842 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004843 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004844 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004845 init_cgroup_root(&cgroup_dummy_root);
4846 cgroup_root_count = 1;
Paul Menage817929e2007-10-18 23:39:36 -07004847 init_task.cgroups = &init_css_set;
4848
Tejun Heo69d02062013-06-12 21:04:50 -07004849 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004850 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4851 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004852 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004853
Tejun Heo30159ec2013-06-25 11:53:37 -07004854 /* at bootup time, we don't worry about modular subsystems */
4855 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004856 BUG_ON(!ss->name);
4857 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004858 BUG_ON(!ss->css_alloc);
4859 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004861 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004862 ss->name, ss->subsys_id);
4863 BUG();
4864 }
4865
4866 if (ss->early_init)
4867 cgroup_init_subsys(ss);
4868 }
4869 return 0;
4870}
4871
4872/**
Li Zefana043e3b2008-02-23 15:24:09 -08004873 * cgroup_init - cgroup initialization
4874 *
4875 * Register cgroup filesystem and /proc file, and initialize
4876 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004877 */
4878int __init cgroup_init(void)
4879{
Tejun Heo30159ec2013-06-25 11:53:37 -07004880 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004881 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004882 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004883
4884 err = bdi_init(&cgroup_backing_dev_info);
4885 if (err)
4886 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004887
Tejun Heo30159ec2013-06-25 11:53:37 -07004888 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004889 if (!ss->early_init)
4890 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004891 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004892 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004893 }
4894
Tejun Heofa3ca072013-04-14 11:36:56 -07004895 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004896 mutex_lock(&cgroup_mutex);
4897 mutex_lock(&cgroup_root_mutex);
4898
Tejun Heo82fe9b02013-06-25 11:53:37 -07004899 /* Add init_css_set to the hash table */
4900 key = css_set_hash(init_css_set.subsys);
4901 hash_add(css_set_table, &init_css_set.hlist, key);
4902
Tejun Heofc76df72013-06-25 11:53:37 -07004903 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004904
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004905 mutex_unlock(&cgroup_root_mutex);
4906 mutex_unlock(&cgroup_mutex);
4907
Greg KH676db4a2010-08-05 13:53:35 -07004908 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4909 if (!cgroup_kobj) {
4910 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004911 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004912 }
4913
4914 err = register_filesystem(&cgroup_fs_type);
4915 if (err < 0) {
4916 kobject_put(cgroup_kobj);
4917 goto out;
4918 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004919
Li Zefan46ae2202008-04-29 01:00:08 -07004920 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004921
Paul Menageddbcc7e2007-10-18 23:39:30 -07004922out:
Paul Menagea4243162007-10-18 23:39:35 -07004923 if (err)
4924 bdi_destroy(&cgroup_backing_dev_info);
4925
Paul Menageddbcc7e2007-10-18 23:39:30 -07004926 return err;
4927}
Paul Menageb4f48b62007-10-18 23:39:33 -07004928
Paul Menagea4243162007-10-18 23:39:35 -07004929/*
4930 * proc_cgroup_show()
4931 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4932 * - Used for /proc/<pid>/cgroup.
4933 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4934 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004935 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004936 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4937 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4938 * cgroup to top_cgroup.
4939 */
4940
4941/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004942int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004943{
4944 struct pid *pid;
4945 struct task_struct *tsk;
4946 char *buf;
4947 int retval;
4948 struct cgroupfs_root *root;
4949
4950 retval = -ENOMEM;
4951 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4952 if (!buf)
4953 goto out;
4954
4955 retval = -ESRCH;
4956 pid = m->private;
4957 tsk = get_pid_task(pid, PIDTYPE_PID);
4958 if (!tsk)
4959 goto out_free;
4960
4961 retval = 0;
4962
4963 mutex_lock(&cgroup_mutex);
4964
Li Zefane5f6a862009-01-07 18:07:41 -08004965 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004966 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004967 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004968 int count = 0;
4969
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004970 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004971 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004972 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004973 if (strlen(root->name))
4974 seq_printf(m, "%sname=%s", count ? "," : "",
4975 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004976 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004977 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004978 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004979 if (retval < 0)
4980 goto out_unlock;
4981 seq_puts(m, buf);
4982 seq_putc(m, '\n');
4983 }
4984
4985out_unlock:
4986 mutex_unlock(&cgroup_mutex);
4987 put_task_struct(tsk);
4988out_free:
4989 kfree(buf);
4990out:
4991 return retval;
4992}
4993
Paul Menagea4243162007-10-18 23:39:35 -07004994/* Display information about each subsystem and each hierarchy */
4995static int proc_cgroupstats_show(struct seq_file *m, void *v)
4996{
Tejun Heo30159ec2013-06-25 11:53:37 -07004997 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004998 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004999
Paul Menage8bab8dd2008-04-04 14:29:57 -07005000 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005001 /*
5002 * ideally we don't want subsystems moving around while we do this.
5003 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5004 * subsys/hierarchy state.
5005 */
Paul Menagea4243162007-10-18 23:39:35 -07005006 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005007
5008 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005009 seq_printf(m, "%s\t%d\t%d\t%d\n",
5010 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005011 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005012
Paul Menagea4243162007-10-18 23:39:35 -07005013 mutex_unlock(&cgroup_mutex);
5014 return 0;
5015}
5016
5017static int cgroupstats_open(struct inode *inode, struct file *file)
5018{
Al Viro9dce07f2008-03-29 03:07:28 +00005019 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005020}
5021
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005022static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005023 .open = cgroupstats_open,
5024 .read = seq_read,
5025 .llseek = seq_lseek,
5026 .release = single_release,
5027};
5028
Paul Menageb4f48b62007-10-18 23:39:33 -07005029/**
5030 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005031 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005032 *
5033 * Description: A task inherits its parent's cgroup at fork().
5034 *
5035 * A pointer to the shared css_set was automatically copied in
5036 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005037 * it was not made under the protection of RCU or cgroup_mutex, so
5038 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5039 * have already changed current->cgroups, allowing the previously
5040 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005041 *
5042 * At the point that cgroup_fork() is called, 'current' is the parent
5043 * task, and the passed argument 'child' points to the child task.
5044 */
5045void cgroup_fork(struct task_struct *child)
5046{
Tejun Heo9bb71302012-10-18 17:52:07 -07005047 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005048 child->cgroups = current->cgroups;
5049 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005050 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005051 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005052}
5053
5054/**
Li Zefana043e3b2008-02-23 15:24:09 -08005055 * cgroup_post_fork - called on a new task after adding it to the task list
5056 * @child: the task in question
5057 *
Tejun Heo5edee612012-10-16 15:03:14 -07005058 * Adds the task to the list running through its css_set if necessary and
5059 * call the subsystem fork() callbacks. Has to be after the task is
5060 * visible on the task list in case we race with the first call to
5061 * cgroup_iter_start() - to guarantee that the new task ends up on its
5062 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005063 */
Paul Menage817929e2007-10-18 23:39:36 -07005064void cgroup_post_fork(struct task_struct *child)
5065{
Tejun Heo30159ec2013-06-25 11:53:37 -07005066 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005067 int i;
5068
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005069 /*
5070 * use_task_css_set_links is set to 1 before we walk the tasklist
5071 * under the tasklist_lock and we read it here after we added the child
5072 * to the tasklist under the tasklist_lock as well. If the child wasn't
5073 * yet in the tasklist when we walked through it from
5074 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5075 * should be visible now due to the paired locking and barriers implied
5076 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5077 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5078 * lock on fork.
5079 */
Paul Menage817929e2007-10-18 23:39:36 -07005080 if (use_task_css_set_links) {
5081 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005082 task_lock(child);
5083 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005084 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005085 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005086 write_unlock(&css_set_lock);
5087 }
Tejun Heo5edee612012-10-16 15:03:14 -07005088
5089 /*
5090 * Call ss->fork(). This must happen after @child is linked on
5091 * css_set; otherwise, @child might change state between ->fork()
5092 * and addition to css_set.
5093 */
5094 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005095 /*
5096 * fork/exit callbacks are supported only for builtin
5097 * subsystems, and the builtin section of the subsys
5098 * array is immutable, so we don't need to lock the
5099 * subsys array here. On the other hand, modular section
5100 * of the array can be freed at module unload, so we
5101 * can't touch that.
5102 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005103 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005104 if (ss->fork)
5105 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005106 }
Paul Menage817929e2007-10-18 23:39:36 -07005107}
Tejun Heo5edee612012-10-16 15:03:14 -07005108
Paul Menage817929e2007-10-18 23:39:36 -07005109/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005110 * cgroup_exit - detach cgroup from exiting task
5111 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005112 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005113 *
5114 * Description: Detach cgroup from @tsk and release it.
5115 *
5116 * Note that cgroups marked notify_on_release force every task in
5117 * them to take the global cgroup_mutex mutex when exiting.
5118 * This could impact scaling on very large systems. Be reluctant to
5119 * use notify_on_release cgroups where very high task exit scaling
5120 * is required on large systems.
5121 *
5122 * the_top_cgroup_hack:
5123 *
5124 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5125 *
5126 * We call cgroup_exit() while the task is still competent to
5127 * handle notify_on_release(), then leave the task attached to the
5128 * root cgroup in each hierarchy for the remainder of its exit.
5129 *
5130 * To do this properly, we would increment the reference count on
5131 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5132 * code we would add a second cgroup function call, to drop that
5133 * reference. This would just create an unnecessary hot spot on
5134 * the top_cgroup reference count, to no avail.
5135 *
5136 * Normally, holding a reference to a cgroup without bumping its
5137 * count is unsafe. The cgroup could go away, or someone could
5138 * attach us to a different cgroup, decrementing the count on
5139 * the first cgroup that we never incremented. But in this case,
5140 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005141 * which wards off any cgroup_attach_task() attempts, or task is a failed
5142 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005143 */
5144void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5145{
Tejun Heo30159ec2013-06-25 11:53:37 -07005146 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005147 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005148 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005149
5150 /*
5151 * Unlink from the css_set task list if necessary.
5152 * Optimistically check cg_list before taking
5153 * css_set_lock
5154 */
5155 if (!list_empty(&tsk->cg_list)) {
5156 write_lock(&css_set_lock);
5157 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005158 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005159 write_unlock(&css_set_lock);
5160 }
5161
Paul Menageb4f48b62007-10-18 23:39:33 -07005162 /* Reassign the task to the init_css_set. */
5163 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005164 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005165 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005166
5167 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005168 /*
5169 * fork/exit callbacks are supported only for builtin
5170 * subsystems, see cgroup_post_fork() for details.
5171 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005172 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005173 if (ss->exit) {
5174 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005175 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005176 struct cgroup *cgrp = task_cgroup(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005177
Li Zefan761b3ef2012-01-31 13:47:36 +08005178 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005179 }
5180 }
5181 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005182 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005183
Tejun Heo5abb8852013-06-12 21:04:49 -07005184 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005185}
Paul Menage697f4162007-10-18 23:39:34 -07005186
Paul Menagebd89aab2007-10-18 23:40:44 -07005187static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005188{
Li Zefanf50daa72013-03-01 15:06:07 +08005189 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005190 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005191 /*
5192 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005193 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005194 * it now
5195 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005196 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005197
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005198 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005199 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005200 list_empty(&cgrp->release_list)) {
5201 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005202 need_schedule_work = 1;
5203 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005204 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005205 if (need_schedule_work)
5206 schedule_work(&release_agent_work);
5207 }
5208}
5209
Paul Menage81a6a5c2007-10-18 23:39:38 -07005210/*
5211 * Notify userspace when a cgroup is released, by running the
5212 * configured release agent with the name of the cgroup (path
5213 * relative to the root of cgroup file system) as the argument.
5214 *
5215 * Most likely, this user command will try to rmdir this cgroup.
5216 *
5217 * This races with the possibility that some other task will be
5218 * attached to this cgroup before it is removed, or that some other
5219 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5220 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5221 * unused, and this cgroup will be reprieved from its death sentence,
5222 * to continue to serve a useful existence. Next time it's released,
5223 * we will get notified again, if it still has 'notify_on_release' set.
5224 *
5225 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5226 * means only wait until the task is successfully execve()'d. The
5227 * separate release agent task is forked by call_usermodehelper(),
5228 * then control in this thread returns here, without waiting for the
5229 * release agent task. We don't bother to wait because the caller of
5230 * this routine has no use for the exit status of the release agent
5231 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005232 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005233static void cgroup_release_agent(struct work_struct *work)
5234{
5235 BUG_ON(work != &release_agent_work);
5236 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005237 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005238 while (!list_empty(&release_list)) {
5239 char *argv[3], *envp[3];
5240 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005241 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005242 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005243 struct cgroup,
5244 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005245 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005246 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005247 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005248 if (!pathbuf)
5249 goto continue_free;
5250 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5251 goto continue_free;
5252 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5253 if (!agentbuf)
5254 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005255
5256 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005257 argv[i++] = agentbuf;
5258 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005259 argv[i] = NULL;
5260
5261 i = 0;
5262 /* minimal command environment */
5263 envp[i++] = "HOME=/";
5264 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5265 envp[i] = NULL;
5266
5267 /* Drop the lock while we invoke the usermode helper,
5268 * since the exec could involve hitting disk and hence
5269 * be a slow process */
5270 mutex_unlock(&cgroup_mutex);
5271 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005272 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005273 continue_free:
5274 kfree(pathbuf);
5275 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005276 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005277 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005278 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005279 mutex_unlock(&cgroup_mutex);
5280}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005281
5282static int __init cgroup_disable(char *str)
5283{
Tejun Heo30159ec2013-06-25 11:53:37 -07005284 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005285 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005286 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005287
5288 while ((token = strsep(&str, ",")) != NULL) {
5289 if (!*token)
5290 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005291
Tejun Heo30159ec2013-06-25 11:53:37 -07005292 /*
5293 * cgroup_disable, being at boot time, can't know about
5294 * module subsystems, so we don't worry about them.
5295 */
5296 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005297 if (!strcmp(token, ss->name)) {
5298 ss->disabled = 1;
5299 printk(KERN_INFO "Disabling %s control group"
5300 " subsystem\n", ss->name);
5301 break;
5302 }
5303 }
5304 }
5305 return 1;
5306}
5307__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005308
5309/*
5310 * Functons for CSS ID.
5311 */
5312
Tejun Heo54766d42013-06-12 21:04:53 -07005313/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005314unsigned short css_id(struct cgroup_subsys_state *css)
5315{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005316 struct css_id *cssid;
5317
5318 /*
5319 * This css_id() can return correct value when somone has refcnt
5320 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5321 * it's unchanged until freed.
5322 */
Tejun Heod3daf282013-06-13 19:39:16 -07005323 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005324
5325 if (cssid)
5326 return cssid->id;
5327 return 0;
5328}
Ben Blum67523c42010-03-10 15:22:11 -08005329EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005330
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005331/**
5332 * css_is_ancestor - test "root" css is an ancestor of "child"
5333 * @child: the css to be tested.
5334 * @root: the css supporsed to be an ancestor of the child.
5335 *
5336 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005337 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005338 * But, considering usual usage, the csses should be valid objects after test.
5339 * Assuming that the caller will do some action to the child if this returns
5340 * returns true, the caller must take "child";s reference count.
5341 * If "child" is valid object and this returns true, "root" is valid, too.
5342 */
5343
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005344bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005345 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005346{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005347 struct css_id *child_id;
5348 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005349
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005350 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005351 if (!child_id)
5352 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005353 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005354 if (!root_id)
5355 return false;
5356 if (child_id->depth < root_id->depth)
5357 return false;
5358 if (child_id->stack[root_id->depth] != root_id->id)
5359 return false;
5360 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005361}
5362
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005363void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5364{
5365 struct css_id *id = css->id;
5366 /* When this is called before css_id initialization, id can be NULL */
5367 if (!id)
5368 return;
5369
5370 BUG_ON(!ss->use_id);
5371
5372 rcu_assign_pointer(id->css, NULL);
5373 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005374 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005375 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005376 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005377 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378}
Ben Blum67523c42010-03-10 15:22:11 -08005379EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005380
5381/*
5382 * This is called by init or create(). Then, calls to this function are
5383 * always serialized (By cgroup_mutex() at create()).
5384 */
5385
5386static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5387{
5388 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005389 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005390
5391 BUG_ON(!ss->use_id);
5392
5393 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5394 newid = kzalloc(size, GFP_KERNEL);
5395 if (!newid)
5396 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005397
5398 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005399 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005400 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005401 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005402 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005403 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005404
5405 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005406 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005407 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408
Tejun Heod228d9e2013-02-27 17:04:54 -08005409 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410 newid->depth = depth;
5411 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005412err_out:
5413 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005414 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005415
5416}
5417
Ben Blume6a11052010-03-10 15:22:09 -08005418static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5419 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005420{
5421 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005422
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005423 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005424 idr_init(&ss->idr);
5425
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005426 newid = get_new_cssid(ss, 0);
5427 if (IS_ERR(newid))
5428 return PTR_ERR(newid);
5429
5430 newid->stack[0] = newid->id;
5431 newid->css = rootcss;
5432 rootcss->id = newid;
5433 return 0;
5434}
5435
5436static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5437 struct cgroup *child)
5438{
5439 int subsys_id, i, depth = 0;
5440 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005441 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005442
5443 subsys_id = ss->subsys_id;
5444 parent_css = parent->subsys[subsys_id];
5445 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005447 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005448
5449 child_id = get_new_cssid(ss, depth);
5450 if (IS_ERR(child_id))
5451 return PTR_ERR(child_id);
5452
5453 for (i = 0; i < depth; i++)
5454 child_id->stack[i] = parent_id->stack[i];
5455 child_id->stack[depth] = child_id->id;
5456 /*
5457 * child_id->css pointer will be set after this cgroup is available
5458 * see cgroup_populate_dir()
5459 */
5460 rcu_assign_pointer(child_css->id, child_id);
5461
5462 return 0;
5463}
5464
5465/**
5466 * css_lookup - lookup css by id
5467 * @ss: cgroup subsys to be looked into.
5468 * @id: the id
5469 *
5470 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5471 * NULL if not. Should be called under rcu_read_lock()
5472 */
5473struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5474{
5475 struct css_id *cssid = NULL;
5476
5477 BUG_ON(!ss->use_id);
5478 cssid = idr_find(&ss->idr, id);
5479
5480 if (unlikely(!cssid))
5481 return NULL;
5482
5483 return rcu_dereference(cssid->css);
5484}
Ben Blum67523c42010-03-10 15:22:11 -08005485EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005486
Stephane Eraniane5d13672011-02-14 11:20:01 +02005487/*
5488 * get corresponding css from file open on cgroupfs directory
5489 */
5490struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5491{
5492 struct cgroup *cgrp;
5493 struct inode *inode;
5494 struct cgroup_subsys_state *css;
5495
Al Viro496ad9a2013-01-23 17:07:38 -05005496 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005497 /* check in cgroup filesystem dir */
5498 if (inode->i_op != &cgroup_dir_inode_operations)
5499 return ERR_PTR(-EBADF);
5500
5501 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5502 return ERR_PTR(-EINVAL);
5503
5504 /* get cgroup */
5505 cgrp = __d_cgrp(f->f_dentry);
5506 css = cgrp->subsys[id];
5507 return css ? css : ERR_PTR(-ENOENT);
5508}
5509
Paul Menagefe693432009-09-23 15:56:20 -07005510#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005511static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005512{
5513 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5514
5515 if (!css)
5516 return ERR_PTR(-ENOMEM);
5517
5518 return css;
5519}
5520
Li Zefan03c78cb2013-06-14 11:17:19 +08005521static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005522{
Li Zefan03c78cb2013-06-14 11:17:19 +08005523 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005524}
5525
Li Zefan03c78cb2013-06-14 11:17:19 +08005526static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005527{
Li Zefan03c78cb2013-06-14 11:17:19 +08005528 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005529}
5530
Li Zefan03c78cb2013-06-14 11:17:19 +08005531static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005532{
5533 return (u64)(unsigned long)current->cgroups;
5534}
5535
Li Zefan03c78cb2013-06-14 11:17:19 +08005536static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5537 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005538{
5539 u64 count;
5540
5541 rcu_read_lock();
5542 count = atomic_read(&current->cgroups->refcount);
5543 rcu_read_unlock();
5544 return count;
5545}
5546
Li Zefan03c78cb2013-06-14 11:17:19 +08005547static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005548 struct cftype *cft,
5549 struct seq_file *seq)
5550{
Tejun Heo69d02062013-06-12 21:04:50 -07005551 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005552 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005553
5554 read_lock(&css_set_lock);
5555 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005556 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005557 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005558 struct cgroup *c = link->cgrp;
5559 const char *name;
5560
5561 if (c->dentry)
5562 name = c->dentry->d_name.name;
5563 else
5564 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005565 seq_printf(seq, "Root %d group %s\n",
5566 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005567 }
5568 rcu_read_unlock();
5569 read_unlock(&css_set_lock);
5570 return 0;
5571}
5572
5573#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005574static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005575 struct cftype *cft,
5576 struct seq_file *seq)
5577{
Tejun Heo69d02062013-06-12 21:04:50 -07005578 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005579
5580 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005581 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005582 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005583 struct task_struct *task;
5584 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005585 seq_printf(seq, "css_set %p\n", cset);
5586 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005587 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5588 seq_puts(seq, " ...\n");
5589 break;
5590 } else {
5591 seq_printf(seq, " task %d\n",
5592 task_pid_vnr(task));
5593 }
5594 }
5595 }
5596 read_unlock(&css_set_lock);
5597 return 0;
5598}
5599
Paul Menagefe693432009-09-23 15:56:20 -07005600static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5601{
5602 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5603}
5604
5605static struct cftype debug_files[] = {
5606 {
Paul Menagefe693432009-09-23 15:56:20 -07005607 .name = "taskcount",
5608 .read_u64 = debug_taskcount_read,
5609 },
5610
5611 {
5612 .name = "current_css_set",
5613 .read_u64 = current_css_set_read,
5614 },
5615
5616 {
5617 .name = "current_css_set_refcount",
5618 .read_u64 = current_css_set_refcount_read,
5619 },
5620
5621 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005622 .name = "current_css_set_cg_links",
5623 .read_seq_string = current_css_set_cg_links_read,
5624 },
5625
5626 {
5627 .name = "cgroup_css_links",
5628 .read_seq_string = cgroup_css_links_read,
5629 },
5630
5631 {
Paul Menagefe693432009-09-23 15:56:20 -07005632 .name = "releasable",
5633 .read_u64 = releasable_read,
5634 },
Paul Menagefe693432009-09-23 15:56:20 -07005635
Tejun Heo4baf6e32012-04-01 12:09:55 -07005636 { } /* terminate */
5637};
Paul Menagefe693432009-09-23 15:56:20 -07005638
5639struct cgroup_subsys debug_subsys = {
5640 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005641 .css_alloc = debug_css_alloc,
5642 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005643 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005644 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005645};
5646#endif /* CONFIG_CGROUP_DEBUG */