blob: 67fc953c816a9a233f9a969e8272089565a1c8c8 [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
Paul Menageddbcc7e2007-10-18 23:39:30 -0700262/*
263 * for_each_subsys() allows you to iterate on each subsystem attached to
264 * an active hierarchy
265 */
266#define for_each_subsys(_root, _ss) \
267list_for_each_entry(_ss, &_root->subsys_list, sibling)
268
Li Zefane5f6a862009-01-07 18:07:41 -0800269/* for_each_active_root() allows you to iterate across the active hierarchies */
270#define for_each_active_root(_root) \
Tejun Heo9871bf92013-06-24 15:21:47 -0700271list_for_each_entry(_root, &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700272
Tejun Heof6ea9372012-04-01 12:09:55 -0700273static inline struct cgroup *__d_cgrp(struct dentry *dentry)
274{
275 return dentry->d_fsdata;
276}
277
Tejun Heo05ef1d72012-04-01 12:09:56 -0700278static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700279{
280 return dentry->d_fsdata;
281}
282
Tejun Heo05ef1d72012-04-01 12:09:56 -0700283static inline struct cftype *__d_cft(struct dentry *dentry)
284{
285 return __d_cfe(dentry)->type;
286}
287
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700288/**
289 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
290 * @cgrp: the cgroup to be checked for liveness
291 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700292 * On success, returns true; the mutex should be later unlocked. On
293 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700294 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700295static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700296{
297 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700298 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700299 mutex_unlock(&cgroup_mutex);
300 return false;
301 }
302 return true;
303}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700304
Paul Menage81a6a5c2007-10-18 23:39:38 -0700305/* the list of cgroups eligible for automatic release. Protected by
306 * release_list_lock */
307static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200308static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309static void cgroup_release_agent(struct work_struct *work);
310static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700311static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700312
Tejun Heo69d02062013-06-12 21:04:50 -0700313/*
314 * A cgroup can be associated with multiple css_sets as different tasks may
315 * belong to different cgroups on different hierarchies. In the other
316 * direction, a css_set is naturally associated with multiple cgroups.
317 * This M:N relationship is represented by the following link structure
318 * which exists for each association and allows traversing the associations
319 * from both sides.
320 */
321struct cgrp_cset_link {
322 /* the cgroup and css_set this link associates */
323 struct cgroup *cgrp;
324 struct css_set *cset;
325
326 /* list of cgrp_cset_links anchored at cgrp->cset_links */
327 struct list_head cset_link;
328
329 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
330 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700331};
332
333/* The default css_set - used by init and its children prior to any
334 * hierarchies being mounted. It contains a pointer to the root state
335 * for each subsystem. Also used to anchor the list of css_sets. Not
336 * reference-counted, to improve performance when child cgroups
337 * haven't been created.
338 */
339
340static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700341static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700342
Ben Blume6a11052010-03-10 15:22:09 -0800343static int cgroup_init_idr(struct cgroup_subsys *ss,
344 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700345
Paul Menage817929e2007-10-18 23:39:36 -0700346/* css_set_lock protects the list of css_set objects, and the
347 * chain of tasks off each css_set. Nests outside task->alloc_lock
348 * due to cgroup_iter_start() */
349static DEFINE_RWLOCK(css_set_lock);
350static int css_set_count;
351
Paul Menage7717f7b2009-09-23 15:56:22 -0700352/*
353 * hash table for cgroup groups. This improves the performance to find
354 * an existing css_set. This hash doesn't (currently) take into
355 * account cgroups in empty hierarchies.
356 */
Li Zefan472b1052008-04-29 01:00:11 -0700357#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800358static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700359
Li Zefan0ac801f2013-01-10 11:49:27 +0800360static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700361{
362 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800363 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700364
365 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800366 key += (unsigned long)css[i];
367 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700368
Li Zefan0ac801f2013-01-10 11:49:27 +0800369 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700370}
371
Paul Menage817929e2007-10-18 23:39:36 -0700372/* We don't maintain the lists running through each css_set to its
373 * task until after the first call to cgroup_iter_start(). This
374 * reduces the fork()/exit() overhead for people who have cgroups
375 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700376static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700377
Tejun Heo5abb8852013-06-12 21:04:49 -0700378static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700379{
Tejun Heo69d02062013-06-12 21:04:50 -0700380 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700381
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700382 /*
383 * Ensure that the refcount doesn't hit zero while any readers
384 * can see it. Similar to atomic_dec_and_lock(), but for an
385 * rwlock
386 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700387 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 return;
389 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700390 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700391 write_unlock(&css_set_lock);
392 return;
393 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700394
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700395 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700396 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397 css_set_count--;
398
Tejun Heo69d02062013-06-12 21:04:50 -0700399 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700400 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700401
Tejun Heo69d02062013-06-12 21:04:50 -0700402 list_del(&link->cset_link);
403 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800404
Tejun Heoddd69142013-06-12 21:04:54 -0700405 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700406 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700408 set_bit(CGRP_RELEASABLE, &cgrp->flags);
409 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700411
412 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700413 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700414
415 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700416 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700417}
418
419/*
420 * refcounted get/put for css_set objects
421 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700422static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700423{
Tejun Heo5abb8852013-06-12 21:04:49 -0700424 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700425}
426
Tejun Heo5abb8852013-06-12 21:04:49 -0700427static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700428{
Tejun Heo5abb8852013-06-12 21:04:49 -0700429 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700430}
431
Tejun Heo5abb8852013-06-12 21:04:49 -0700432static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700433{
Tejun Heo5abb8852013-06-12 21:04:49 -0700434 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435}
436
Paul Menage817929e2007-10-18 23:39:36 -0700437/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700438 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 * @cset: candidate css_set being tested
440 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700441 * @new_cgrp: cgroup that's being entered by the task
442 * @template: desired set of css pointers in css_set (pre-calculated)
443 *
444 * Returns true if "cg" matches "old_cg" except for the hierarchy
445 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static bool compare_css_sets(struct css_set *cset,
448 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700449 struct cgroup *new_cgrp,
450 struct cgroup_subsys_state *template[])
451{
452 struct list_head *l1, *l2;
453
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700455 /* Not all subsystems matched */
456 return false;
457 }
458
459 /*
460 * Compare cgroup pointers in order to distinguish between
461 * different cgroups in heirarchies with no subsystems. We
462 * could get by with just this check alone (and skip the
463 * memcmp above) but on most setups the memcmp check will
464 * avoid the need for this more expensive check on almost all
465 * candidates.
466 */
467
Tejun Heo69d02062013-06-12 21:04:50 -0700468 l1 = &cset->cgrp_links;
469 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700471 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700472 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700473
474 l1 = l1->next;
475 l2 = l2->next;
476 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700477 if (l1 == &cset->cgrp_links) {
478 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700479 break;
480 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700481 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700482 }
483 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700484 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
485 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
486 cgrp1 = link1->cgrp;
487 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700488 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700489 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700490
491 /*
492 * If this hierarchy is the hierarchy of the cgroup
493 * that's changing, then we need to check that this
494 * css_set points to the new cgroup; if it's any other
495 * hierarchy, then this css_set should point to the
496 * same cgroup as the old css_set.
497 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700498 if (cgrp1->root == new_cgrp->root) {
499 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700500 return false;
501 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700502 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700503 return false;
504 }
505 }
506 return true;
507}
508
509/*
Paul Menage817929e2007-10-18 23:39:36 -0700510 * find_existing_css_set() is a helper for
511 * find_css_set(), and checks to see whether an existing
Li Zefan472b1052008-04-29 01:00:11 -0700512 * css_set is suitable.
Paul Menage817929e2007-10-18 23:39:36 -0700513 *
514 * oldcg: the cgroup group that we're using before the cgroup
515 * transition
516 *
Paul Menagebd89aab2007-10-18 23:40:44 -0700517 * cgrp: the cgroup that we're moving into
Paul Menage817929e2007-10-18 23:39:36 -0700518 *
519 * template: location in which to build the desired set of subsystem
520 * state objects for the new cgroup group
521 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700522static struct css_set *find_existing_css_set(struct css_set *old_cset,
523 struct cgroup *cgrp,
524 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700525{
526 int i;
Paul Menagebd89aab2007-10-18 23:40:44 -0700527 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700528 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800529 unsigned long key;
Paul Menage817929e2007-10-18 23:39:36 -0700530
Ben Blumaae8aab2010-03-10 15:22:07 -0800531 /*
532 * Build the set of subsystem state objects that we want to see in the
533 * new css_set. while subsystems can change globally, the entries here
534 * won't change, so no need for locking.
535 */
Paul Menage817929e2007-10-18 23:39:36 -0700536 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400537 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700538 /* Subsystem is in this hierarchy. So we want
539 * the subsystem state from the new
540 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700541 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700542 } else {
543 /* Subsystem is not in this hierarchy, so we
544 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700546 }
547 }
548
Li Zefan0ac801f2013-01-10 11:49:27 +0800549 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 hash_for_each_possible(css_set_table, cset, hlist, key) {
551 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700552 continue;
553
554 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700555 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700556 }
Paul Menage817929e2007-10-18 23:39:36 -0700557
558 /* No existing cgroup group matched */
559 return NULL;
560}
561
Tejun Heo69d02062013-06-12 21:04:50 -0700562static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700563{
Tejun Heo69d02062013-06-12 21:04:50 -0700564 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700565
Tejun Heo69d02062013-06-12 21:04:50 -0700566 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
567 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700568 kfree(link);
569 }
570}
571
Tejun Heo69d02062013-06-12 21:04:50 -0700572/**
573 * allocate_cgrp_cset_links - allocate cgrp_cset_links
574 * @count: the number of links to allocate
575 * @tmp_links: list_head the allocated links are put on
576 *
577 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
578 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700579 */
Tejun Heo69d02062013-06-12 21:04:50 -0700580static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700581{
Tejun Heo69d02062013-06-12 21:04:50 -0700582 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700583 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700584
585 INIT_LIST_HEAD(tmp_links);
586
Li Zefan36553432008-07-29 22:33:19 -0700587 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700588 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700589 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700590 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700591 return -ENOMEM;
592 }
Tejun Heo69d02062013-06-12 21:04:50 -0700593 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700594 }
595 return 0;
596}
597
Li Zefanc12f65d2009-01-07 18:07:42 -0800598/**
599 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700600 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800602 * @cgrp: the destination cgroup
603 */
Tejun Heo69d02062013-06-12 21:04:50 -0700604static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
605 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800606{
Tejun Heo69d02062013-06-12 21:04:50 -0700607 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800608
Tejun Heo69d02062013-06-12 21:04:50 -0700609 BUG_ON(list_empty(tmp_links));
610 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
611 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700612 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700613 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700614 /*
615 * Always add links to the tail of the list so that the list
616 * is sorted by order of hierarchy creation
617 */
Tejun Heo69d02062013-06-12 21:04:50 -0700618 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800619}
620
Li Zefan36553432008-07-29 22:33:19 -0700621/*
Paul Menage817929e2007-10-18 23:39:36 -0700622 * find_css_set() takes an existing cgroup group and a
623 * cgroup object, and returns a css_set object that's
624 * equivalent to the old group, but with the given cgroup
625 * substituted into the appropriate hierarchy. Must be called with
626 * cgroup_mutex held
627 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700628static struct css_set *find_css_set(struct css_set *old_cset,
629 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700630{
Tejun Heo5abb8852013-06-12 21:04:49 -0700631 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -0700632 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT];
Tejun Heo69d02062013-06-12 21:04:50 -0700633 struct list_head tmp_links;
634 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800635 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700636
Paul Menage817929e2007-10-18 23:39:36 -0700637 /* First see if we already have a cgroup group that matches
638 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700639 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700640 cset = find_existing_css_set(old_cset, cgrp, template);
641 if (cset)
642 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700643 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700644
Tejun Heo5abb8852013-06-12 21:04:49 -0700645 if (cset)
646 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700647
Tejun Heof4f4be22013-06-12 21:04:51 -0700648 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700649 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700650 return NULL;
651
Tejun Heo69d02062013-06-12 21:04:50 -0700652 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700653 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700654 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700655 return NULL;
656 }
657
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700659 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700660 INIT_LIST_HEAD(&cset->tasks);
661 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700662
663 /* Copy the set of subsystem state objects generated in
664 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700665 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700666
667 write_lock(&css_set_lock);
668 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700669 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700670 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700671
Paul Menage7717f7b2009-09-23 15:56:22 -0700672 if (c->root == cgrp->root)
673 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700674 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700675 }
Paul Menage817929e2007-10-18 23:39:36 -0700676
Tejun Heo69d02062013-06-12 21:04:50 -0700677 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700678
Paul Menage817929e2007-10-18 23:39:36 -0700679 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700680
681 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 key = css_set_hash(cset->subsys);
683 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700684
Paul Menage817929e2007-10-18 23:39:36 -0700685 write_unlock(&css_set_lock);
686
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700688}
689
Paul Menageddbcc7e2007-10-18 23:39:30 -0700690/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700691 * Return the cgroup for "task" from the given hierarchy. Must be
692 * called with cgroup_mutex held.
693 */
694static struct cgroup *task_cgroup_from_root(struct task_struct *task,
695 struct cgroupfs_root *root)
696{
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700698 struct cgroup *res = NULL;
699
700 BUG_ON(!mutex_is_locked(&cgroup_mutex));
701 read_lock(&css_set_lock);
702 /*
703 * No need to lock the task - since we hold cgroup_mutex the
704 * task can't change groups, so the only thing that can happen
705 * is that it exits and its css is set back to init_css_set.
706 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 cset = task->cgroups;
708 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700709 res = &root->top_cgroup;
710 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700711 struct cgrp_cset_link *link;
712
713 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700715
Paul Menage7717f7b2009-09-23 15:56:22 -0700716 if (c->root == root) {
717 res = c;
718 break;
719 }
720 }
721 }
722 read_unlock(&css_set_lock);
723 BUG_ON(!res);
724 return res;
725}
726
727/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 * There is one global cgroup mutex. We also require taking
729 * task_lock() when dereferencing a task's cgroup subsys pointers.
730 * See "The task_lock() exception", at the end of this comment.
731 *
732 * A task must hold cgroup_mutex to modify cgroups.
733 *
734 * Any task can increment and decrement the count field without lock.
735 * So in general, code holding cgroup_mutex can't rely on the count
736 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800737 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738 * means that no tasks are currently attached, therefore there is no
739 * way a task attached to that cgroup can fork (the other way to
740 * increment the count). So code holding cgroup_mutex can safely
741 * assume that if the count is zero, it will stay zero. Similarly, if
742 * a task holds cgroup_mutex on a cgroup with zero count, it
743 * knows that the cgroup won't be removed, as cgroup_rmdir()
744 * needs that mutex.
745 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
747 * (usually) take cgroup_mutex. These are the two most performance
748 * critical pieces of code here. The exception occurs on cgroup_exit(),
749 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
750 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800751 * to the release agent with the name of the cgroup (path relative to
752 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700753 *
754 * A cgroup can only be deleted if both its 'count' of using tasks
755 * is zero, and its list of 'children' cgroups is empty. Since all
756 * tasks in the system use _some_ cgroup, and since there is always at
757 * least one task in the system (init, pid == 1), therefore, top_cgroup
758 * always has either children cgroups and/or using tasks. So we don't
759 * need a special hack to ensure that top_cgroup cannot be deleted.
760 *
761 * The task_lock() exception
762 *
763 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800764 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800765 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700766 * several performance critical places that need to reference
767 * task->cgroup without the expense of grabbing a system global
768 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800769 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700770 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
771 * the task_struct routinely used for such matters.
772 *
773 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800774 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 */
776
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777/*
778 * A couple of forward declarations required, due to cyclic reference loop:
779 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
780 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
781 * -> cgroup_mkdir.
782 */
783
Al Viro18bb1db2011-07-26 01:41:39 -0400784static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400785static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400787static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
788 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700789static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700790static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700791
792static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200793 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700794 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700795};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700797static int alloc_css_id(struct cgroup_subsys *ss,
798 struct cgroup *parent, struct cgroup *child);
799
Al Viroa5e7ed32011-07-26 01:55:55 -0400800static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801{
802 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803
804 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400805 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700806 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100807 inode->i_uid = current_fsuid();
808 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
810 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
811 }
812 return inode;
813}
814
Li Zefan65dff752013-03-01 15:01:56 +0800815static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
816{
817 struct cgroup_name *name;
818
819 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
820 if (!name)
821 return NULL;
822 strcpy(name->name, dentry->d_name.name);
823 return name;
824}
825
Li Zefanbe445622013-01-24 14:31:42 +0800826static void cgroup_free_fn(struct work_struct *work)
827{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700828 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800829 struct cgroup_subsys *ss;
830
831 mutex_lock(&cgroup_mutex);
832 /*
833 * Release the subsystem state objects.
834 */
835 for_each_subsys(cgrp->root, ss)
836 ss->css_free(cgrp);
837
838 cgrp->root->number_of_cgroups--;
839 mutex_unlock(&cgroup_mutex);
840
841 /*
Li Zefan415cf072013-04-08 14:35:02 +0800842 * We get a ref to the parent's dentry, and put the ref when
843 * this cgroup is being freed, so it's guaranteed that the
844 * parent won't be destroyed before its children.
845 */
846 dput(cgrp->parent->dentry);
847
Li Zefancc20e012013-04-26 11:58:02 -0700848 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
849
Li Zefan415cf072013-04-08 14:35:02 +0800850 /*
Li Zefanbe445622013-01-24 14:31:42 +0800851 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700852 * created the cgroup. This will free cgrp->root, if we are
853 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800854 */
855 deactivate_super(cgrp->root->sb);
856
857 /*
858 * if we're getting rid of the cgroup, refcount should ensure
859 * that there are no pidlists left.
860 */
861 BUG_ON(!list_empty(&cgrp->pidlists));
862
863 simple_xattrs_free(&cgrp->xattrs);
864
Li Zefan65dff752013-03-01 15:01:56 +0800865 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800866 kfree(cgrp);
867}
868
869static void cgroup_free_rcu(struct rcu_head *head)
870{
871 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
872
Tejun Heoea15f8c2013-06-13 19:27:42 -0700873 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
874 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800875}
876
Paul Menageddbcc7e2007-10-18 23:39:30 -0700877static void cgroup_diput(struct dentry *dentry, struct inode *inode)
878{
879 /* is dentry a directory ? if so, kfree() associated cgroup */
880 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700881 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800882
Tejun Heo54766d42013-06-12 21:04:53 -0700883 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800884 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700885 } else {
886 struct cfent *cfe = __d_cfe(dentry);
887 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
888
889 WARN_ONCE(!list_empty(&cfe->node) &&
890 cgrp != &cgrp->root->top_cgroup,
891 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700892 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700893 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700894 }
895 iput(inode);
896}
897
Al Viroc72a04e2011-01-14 05:31:45 +0000898static int cgroup_delete(const struct dentry *d)
899{
900 return 1;
901}
902
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903static void remove_dir(struct dentry *d)
904{
905 struct dentry *parent = dget(d->d_parent);
906
907 d_delete(d);
908 simple_rmdir(parent->d_inode, d);
909 dput(parent);
910}
911
Li Zefan2739d3c2013-01-21 18:18:33 +0800912static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700914 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700915
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
917 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100918
Li Zefan2739d3c2013-01-21 18:18:33 +0800919 /*
920 * If we're doing cleanup due to failure of cgroup_create(),
921 * the corresponding @cfe may not exist.
922 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700923 list_for_each_entry(cfe, &cgrp->files, node) {
924 struct dentry *d = cfe->dentry;
925
926 if (cft && cfe->type != cft)
927 continue;
928
929 dget(d);
930 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700931 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 list_del_init(&cfe->node);
933 dput(d);
934
Li Zefan2739d3c2013-01-21 18:18:33 +0800935 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700937}
938
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400939/**
940 * cgroup_clear_directory - selective removal of base and subsystem files
941 * @dir: directory containing the files
942 * @base_files: true if the base files should be removed
943 * @subsys_mask: mask of the subsystem ids whose files should be removed
944 */
945static void cgroup_clear_directory(struct dentry *dir, bool base_files,
946 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700947{
948 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400949 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400951 for_each_subsys(cgrp->root, ss) {
952 struct cftype_set *set;
953 if (!test_bit(ss->subsys_id, &subsys_mask))
954 continue;
955 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800956 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400957 }
958 if (base_files) {
959 while (!list_empty(&cgrp->files))
960 cgroup_rm_file(cgrp, NULL);
961 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962}
963
964/*
965 * NOTE : the dentry must have been dget()'ed
966 */
967static void cgroup_d_remove_dir(struct dentry *dentry)
968{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100969 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400970 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100971
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400972 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100974 parent = dentry->d_parent;
975 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800976 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700977 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100978 spin_unlock(&dentry->d_lock);
979 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700980 remove_dir(dentry);
981}
982
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700983/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800984 * Call with cgroup_mutex held. Drops reference counts on modules, including
985 * any duplicate ones that parse_cgroupfs_options took. If this function
986 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800987 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700988static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700989 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990{
Paul Menagebd89aab2007-10-18 23:40:44 -0700991 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700992 int i;
993
Ben Blumaae8aab2010-03-10 15:22:07 -0800994 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800995 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800996
Paul Menageddbcc7e2007-10-18 23:39:30 -0700997 /* Check that any added subsystems are currently free */
998 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800999 unsigned long bit = 1UL << i;
Tejun Heo9871bf92013-06-24 15:21:47 -07001000 struct cgroup_subsys *ss = cgroup_subsys[i];
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001001 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -08001003 /*
1004 * Nobody should tell us to do a subsys that doesn't exist:
1005 * parse_cgroupfs_options should catch that case and refcounts
1006 * ensure that subsystems won't disappear once selected.
1007 */
1008 BUG_ON(ss == NULL);
Tejun Heo9871bf92013-06-24 15:21:47 -07001009 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001010 /* Subsystem isn't free */
1011 return -EBUSY;
1012 }
1013 }
1014
1015 /* Currently we don't handle adding/removing subsystems when
1016 * any child cgroups exist. This is theoretically supportable
1017 * but involves complex error handling, so it's being left until
1018 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001019 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 return -EBUSY;
1021
1022 /* Process each subsystem */
1023 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001024 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001025 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001026 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001028 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001029 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001030 BUG_ON(!cgroup_dummy_top->subsys[i]);
1031 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001032
Tejun Heo9871bf92013-06-24 15:21:47 -07001033 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001034 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001035 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001036 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001038 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001039
Ben Blumcf5d5942010-03-10 15:22:09 -08001040 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001041 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001042 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001044 BUG_ON(ss == NULL);
Tejun Heo9871bf92013-06-24 15:21:47 -07001045 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001046 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Paul Menageddbcc7e2007-10-18 23:39:30 -07001048 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001049 ss->bind(cgroup_dummy_top);
1050 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001051 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001052 cgroup_subsys[i]->root = &cgroup_dummy_root;
1053 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001054
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /* subsystem is now free - drop reference on module */
1056 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001057 root->subsys_mask &= ~bit;
1058 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001060 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001061 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001062 /*
1063 * a refcount was taken, but we already had one, so
1064 * drop the extra reference.
1065 */
1066 module_put(ss->module);
1067#ifdef CONFIG_MODULE_UNLOAD
1068 BUG_ON(ss->module && !module_refcount(ss->module));
1069#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001070 } else {
1071 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001072 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 }
1074 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075
1076 return 0;
1077}
1078
Al Viro34c80b12011-12-08 21:32:45 -05001079static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001080{
Al Viro34c80b12011-12-08 21:32:45 -05001081 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001082 struct cgroup_subsys *ss;
1083
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001084 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 for_each_subsys(root, ss)
1086 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001087 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1088 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001089 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001091 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001092 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001093 if (strlen(root->release_agent_path))
1094 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001095 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001096 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001097 if (strlen(root->name))
1098 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001099 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 return 0;
1101}
1102
1103struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001104 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001105 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001106 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001107 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001108 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001109 /* User explicitly requested empty subsystem */
1110 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001111
1112 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001113
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114};
1115
Ben Blumaae8aab2010-03-10 15:22:07 -08001116/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001117 * Convert a hierarchy specifier into a bitmask of subsystems and
1118 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1119 * array. This function takes refcounts on subsystems to be used, unless it
1120 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001121 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001122static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001124 char *token, *o = data;
1125 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001126 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001127 int i;
1128 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001129
Ben Blumaae8aab2010-03-10 15:22:07 -08001130 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1131
Li Zefanf9ab5b52009-06-17 16:26:33 -07001132#ifdef CONFIG_CPUSETS
1133 mask = ~(1UL << cpuset_subsys_id);
1134#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001135
Paul Menagec6d57f32009-09-23 15:56:19 -07001136 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137
1138 while ((token = strsep(&o, ",")) != NULL) {
1139 if (!*token)
1140 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001141 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001142 /* Explicitly have no subsystems */
1143 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 continue;
1145 }
1146 if (!strcmp(token, "all")) {
1147 /* Mutually exclusive option 'all' + subsystem name */
1148 if (one_ss)
1149 return -EINVAL;
1150 all_ss = true;
1151 continue;
1152 }
Tejun Heo873fe092013-04-14 20:15:26 -07001153 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1154 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1155 continue;
1156 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001157 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001158 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001159 continue;
1160 }
1161 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001162 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 continue;
1164 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001165 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001166 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001167 continue;
1168 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001169 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001170 /* Specifying two release agents is forbidden */
1171 if (opts->release_agent)
1172 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001174 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001175 if (!opts->release_agent)
1176 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 continue;
1178 }
1179 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001180 const char *name = token + 5;
1181 /* Can't specify an empty name */
1182 if (!strlen(name))
1183 return -EINVAL;
1184 /* Must match [\w.-]+ */
1185 for (i = 0; i < strlen(name); i++) {
1186 char c = name[i];
1187 if (isalnum(c))
1188 continue;
1189 if ((c == '.') || (c == '-') || (c == '_'))
1190 continue;
1191 return -EINVAL;
1192 }
1193 /* Specifying two names is forbidden */
1194 if (opts->name)
1195 return -EINVAL;
1196 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001197 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001198 GFP_KERNEL);
1199 if (!opts->name)
1200 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201
1202 continue;
1203 }
1204
1205 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001206 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001207 if (ss == NULL)
1208 continue;
1209 if (strcmp(token, ss->name))
1210 continue;
1211 if (ss->disabled)
1212 continue;
1213
1214 /* Mutually exclusive option 'all' + subsystem name */
1215 if (all_ss)
1216 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001217 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001218 one_ss = true;
1219
1220 break;
1221 }
1222 if (i == CGROUP_SUBSYS_COUNT)
1223 return -ENOENT;
1224 }
1225
1226 /*
1227 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001228 * otherwise if 'none', 'name=' and a subsystem name options
1229 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001231 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001232 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001233 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001234 if (ss == NULL)
1235 continue;
1236 if (ss->disabled)
1237 continue;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001238 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001239 }
1240 }
1241
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001242 /* Consistency checks */
1243
Tejun Heo873fe092013-04-14 20:15:26 -07001244 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1245 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1246
1247 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1248 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1249 return -EINVAL;
1250 }
1251
1252 if (opts->cpuset_clone_children) {
1253 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1254 return -EINVAL;
1255 }
1256 }
1257
Li Zefanf9ab5b52009-06-17 16:26:33 -07001258 /*
1259 * Option noprefix was introduced just for backward compatibility
1260 * with the old cpuset, so we allow noprefix only if mounting just
1261 * the cpuset subsystem.
1262 */
Tejun Heo93438622013-04-14 20:15:25 -07001263 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001264 return -EINVAL;
1265
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001266
1267 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001268 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001269 return -EINVAL;
1270
1271 /*
1272 * We either have to specify by name or by subsystems. (So all
1273 * empty hierarchies must have a name).
1274 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001275 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001276 return -EINVAL;
1277
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 /*
1279 * Grab references on all the modules we'll need, so the subsystems
1280 * don't dance around before rebind_subsystems attaches them. This may
1281 * take duplicate reference counts on a subsystem that's already used,
1282 * but rebind_subsystems handles this case.
1283 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001284 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001285 unsigned long bit = 1UL << i;
1286
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001287 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001289 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001290 module_pin_failed = true;
1291 break;
1292 }
1293 }
1294 if (module_pin_failed) {
1295 /*
1296 * oops, one of the modules was going away. this means that we
1297 * raced with a module_delete call, and to the user this is
1298 * essentially a "subsystem doesn't exist" case.
1299 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001300 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001301 /* drop refcounts only on the ones we took */
1302 unsigned long bit = 1UL << i;
1303
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001304 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001305 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001306 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001307 }
1308 return -ENOENT;
1309 }
1310
Paul Menageddbcc7e2007-10-18 23:39:30 -07001311 return 0;
1312}
1313
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001314static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001315{
1316 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001317 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001318 unsigned long bit = 1UL << i;
1319
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001320 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001322 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001323 }
1324}
1325
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1327{
1328 int ret = 0;
1329 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001330 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001332 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333
Tejun Heo873fe092013-04-14 20:15:26 -07001334 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1335 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1336 return -EINVAL;
1337 }
1338
Paul Menagebd89aab2007-10-18 23:40:44 -07001339 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001341 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001342
1343 /* See what subsystems are wanted */
1344 ret = parse_cgroupfs_options(data, &opts);
1345 if (ret)
1346 goto out_unlock;
1347
Tejun Heoa8a648c2013-06-24 15:21:47 -07001348 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001349 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1350 task_tgid_nr(current), current->comm);
1351
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001352 added_mask = opts.subsys_mask & ~root->subsys_mask;
1353 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001354
Ben Blumcf5d5942010-03-10 15:22:09 -08001355 /* Don't allow flags or name to change at remount */
1356 if (opts.flags != root->flags ||
1357 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358 ret = -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001359 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001360 goto out_unlock;
1361 }
1362
Gao feng7083d032012-12-03 09:28:18 +08001363 /*
1364 * Clear out the files of subsystems that should be removed, do
1365 * this before rebind_subsystems, since rebind_subsystems may
1366 * change this hierarchy's subsys_list.
1367 */
1368 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1369
Tejun Heoa8a648c2013-06-24 15:21:47 -07001370 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001371 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001372 /* rebind_subsystems failed, re-populate the removed files */
1373 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001374 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001375 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001376 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001377
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001378 /* re-populate subsystem files */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001379 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380
Paul Menage81a6a5c2007-10-18 23:39:38 -07001381 if (opts.release_agent)
1382 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001383 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001384 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001385 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001386 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001387 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001388 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389 return ret;
1390}
1391
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001392static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393 .statfs = simple_statfs,
1394 .drop_inode = generic_delete_inode,
1395 .show_options = cgroup_show_options,
1396 .remount_fs = cgroup_remount,
1397};
1398
Paul Menagecc31edc2008-10-18 20:28:04 -07001399static void init_cgroup_housekeeping(struct cgroup *cgrp)
1400{
1401 INIT_LIST_HEAD(&cgrp->sibling);
1402 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001403 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001404 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001405 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001406 INIT_LIST_HEAD(&cgrp->pidlists);
1407 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001408 INIT_LIST_HEAD(&cgrp->event_list);
1409 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001410 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001411}
Paul Menagec6d57f32009-09-23 15:56:19 -07001412
Paul Menageddbcc7e2007-10-18 23:39:30 -07001413static void init_cgroup_root(struct cgroupfs_root *root)
1414{
Paul Menagebd89aab2007-10-18 23:40:44 -07001415 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001416
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417 INIT_LIST_HEAD(&root->subsys_list);
1418 INIT_LIST_HEAD(&root->root_list);
1419 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001420 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001421 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001422 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001423}
1424
Tejun Heofa3ca072013-04-14 11:36:56 -07001425static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001426{
Tejun Heo1a574232013-04-14 11:36:58 -07001427 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001428
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001429 lockdep_assert_held(&cgroup_mutex);
1430 lockdep_assert_held(&cgroup_root_mutex);
1431
Tejun Heo1a574232013-04-14 11:36:58 -07001432 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1433 if (id < 0)
1434 return id;
1435
1436 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001437 return 0;
1438}
1439
1440static void cgroup_exit_root_id(struct cgroupfs_root *root)
1441{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001442 lockdep_assert_held(&cgroup_mutex);
1443 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001444
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001445 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001446 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001447 root->hierarchy_id = 0;
1448 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001449}
1450
Paul Menageddbcc7e2007-10-18 23:39:30 -07001451static int cgroup_test_super(struct super_block *sb, void *data)
1452{
Paul Menagec6d57f32009-09-23 15:56:19 -07001453 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001454 struct cgroupfs_root *root = sb->s_fs_info;
1455
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 /* If we asked for a name then it must match */
1457 if (opts->name && strcmp(opts->name, root->name))
1458 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001460 /*
1461 * If we asked for subsystems (or explicitly for no
1462 * subsystems) then they must match
1463 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001464 if ((opts->subsys_mask || opts->none)
1465 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001466 return 0;
1467
1468 return 1;
1469}
1470
Paul Menagec6d57f32009-09-23 15:56:19 -07001471static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1472{
1473 struct cgroupfs_root *root;
1474
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001475 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001476 return NULL;
1477
1478 root = kzalloc(sizeof(*root), GFP_KERNEL);
1479 if (!root)
1480 return ERR_PTR(-ENOMEM);
1481
1482 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001483
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001484 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001485 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001486 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 if (opts->release_agent)
1488 strcpy(root->release_agent_path, opts->release_agent);
1489 if (opts->name)
1490 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001491 if (opts->cpuset_clone_children)
1492 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001493 return root;
1494}
1495
Tejun Heofa3ca072013-04-14 11:36:56 -07001496static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001497{
Tejun Heofa3ca072013-04-14 11:36:56 -07001498 if (root) {
1499 /* hierarhcy ID shoulid already have been released */
1500 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001501
Tejun Heofa3ca072013-04-14 11:36:56 -07001502 ida_destroy(&root->cgroup_ida);
1503 kfree(root);
1504 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001505}
1506
Paul Menageddbcc7e2007-10-18 23:39:30 -07001507static int cgroup_set_super(struct super_block *sb, void *data)
1508{
1509 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 struct cgroup_sb_opts *opts = data;
1511
1512 /* If we don't have a new root, we can't set up a new sb */
1513 if (!opts->new_root)
1514 return -EINVAL;
1515
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001516 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517
1518 ret = set_anon_super(sb, NULL);
1519 if (ret)
1520 return ret;
1521
Paul Menagec6d57f32009-09-23 15:56:19 -07001522 sb->s_fs_info = opts->new_root;
1523 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001524
1525 sb->s_blocksize = PAGE_CACHE_SIZE;
1526 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1527 sb->s_magic = CGROUP_SUPER_MAGIC;
1528 sb->s_op = &cgroup_ops;
1529
1530 return 0;
1531}
1532
1533static int cgroup_get_rootdir(struct super_block *sb)
1534{
Al Viro0df6a632010-12-21 13:29:29 -05001535 static const struct dentry_operations cgroup_dops = {
1536 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001537 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001538 };
1539
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540 struct inode *inode =
1541 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542
1543 if (!inode)
1544 return -ENOMEM;
1545
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 inode->i_fop = &simple_dir_operations;
1547 inode->i_op = &cgroup_dir_inode_operations;
1548 /* directories start off with i_nlink == 2 (for "." entry) */
1549 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001550 sb->s_root = d_make_root(inode);
1551 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001553 /* for everything else we want ->d_op set */
1554 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001555 return 0;
1556}
1557
Al Virof7e83572010-07-26 13:23:11 +04001558static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001560 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561{
1562 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 int ret = 0;
1565 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001567 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001568
1569 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001570 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001571 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001572 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001573 if (ret)
1574 goto out_err;
1575
1576 /*
1577 * Allocate a new cgroup root. We may not need it if we're
1578 * reusing an existing hierarchy.
1579 */
1580 new_root = cgroup_root_from_opts(&opts);
1581 if (IS_ERR(new_root)) {
1582 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001583 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001584 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
Paul Menagec6d57f32009-09-23 15:56:19 -07001587 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001588 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001589 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001591 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001592 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593 }
1594
Paul Menagec6d57f32009-09-23 15:56:19 -07001595 root = sb->s_fs_info;
1596 BUG_ON(!root);
1597 if (root == opts.new_root) {
1598 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001599 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001600 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001601 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001602 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001603 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001604 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
1606 BUG_ON(sb->s_root != NULL);
1607
1608 ret = cgroup_get_rootdir(sb);
1609 if (ret)
1610 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001611 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001612
Paul Menage817929e2007-10-18 23:39:36 -07001613 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001614 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001615 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001616
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001617 /* Check for name clashes with existing mounts */
1618 ret = -EBUSY;
1619 if (strlen(root->name))
1620 for_each_active_root(existing_root)
1621 if (!strcmp(existing_root->name, root->name))
1622 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001623
Paul Menage817929e2007-10-18 23:39:36 -07001624 /*
1625 * We're accessing css_set_count without locking
1626 * css_set_lock here, but that's OK - it can only be
1627 * increased by someone holding cgroup_lock, and
1628 * that's us. The worst that can happen is that we
1629 * have some link structures left over
1630 */
Tejun Heo69d02062013-06-12 21:04:50 -07001631 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001632 if (ret)
1633 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001634
Tejun Heofa3ca072013-04-14 11:36:56 -07001635 ret = cgroup_init_root_id(root);
1636 if (ret)
1637 goto unlock_drop;
1638
Tejun Heoa8a648c2013-06-24 15:21:47 -07001639 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001640 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001641 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001642 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001644 /*
1645 * There must be no failure case after here, since rebinding
1646 * takes care of subsystems' refcounts, which are explicitly
1647 * dropped in the failure exit path.
1648 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649
1650 /* EBUSY should be the only error here */
1651 BUG_ON(ret);
1652
Tejun Heo9871bf92013-06-24 15:21:47 -07001653 list_add(&root->root_list, &cgroup_roots);
1654 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655
Li Zefanc12f65d2009-01-07 18:07:42 -08001656 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001657 root->top_cgroup.dentry = sb->s_root;
1658
Paul Menage817929e2007-10-18 23:39:36 -07001659 /* Link the top cgroup in this hierarchy into all
1660 * the css_set objects */
1661 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001662 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001663 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001664 write_unlock(&css_set_lock);
1665
Tejun Heo69d02062013-06-12 21:04:50 -07001666 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001667
Li Zefanc12f65d2009-01-07 18:07:42 -08001668 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001669 BUG_ON(root->number_of_cgroups != 1);
1670
eparis@redhat2ce97382011-06-02 21:20:51 +10001671 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001672 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001673 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001674 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001676 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001677 } else {
1678 /*
1679 * We re-used an existing hierarchy - the new root (if
1680 * any) is not needed
1681 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001682 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001683
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001684 if (root->flags != opts.flags) {
1685 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1686 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1687 ret = -EINVAL;
1688 goto drop_new_super;
1689 } else {
1690 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1691 }
Tejun Heo873fe092013-04-14 20:15:26 -07001692 }
1693
Ben Blumcf5d5942010-03-10 15:22:09 -08001694 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001695 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 }
1697
Paul Menagec6d57f32009-09-23 15:56:19 -07001698 kfree(opts.release_agent);
1699 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001700 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001702 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001703 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001704 mutex_unlock(&cgroup_root_mutex);
1705 mutex_unlock(&cgroup_mutex);
1706 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001707 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001708 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001709 drop_modules:
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001710 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001711 out_err:
1712 kfree(opts.release_agent);
1713 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001714 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715}
1716
1717static void cgroup_kill_sb(struct super_block *sb) {
1718 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001719 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001720 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 int ret;
1722
1723 BUG_ON(!root);
1724
1725 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001726 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
1728 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001729 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730
1731 /* Rebind all subsystems back to the default hierarchy */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001732 ret = rebind_subsystems(root, 0, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733 /* Shouldn't be able to fail ... */
1734 BUG_ON(ret);
1735
Paul Menage817929e2007-10-18 23:39:36 -07001736 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001737 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001738 * root cgroup
1739 */
1740 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001741
Tejun Heo69d02062013-06-12 21:04:50 -07001742 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1743 list_del(&link->cset_link);
1744 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001745 kfree(link);
1746 }
1747 write_unlock(&css_set_lock);
1748
Paul Menage839ec542009-01-29 14:25:22 -08001749 if (!list_empty(&root->root_list)) {
1750 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001751 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001752 }
Li Zefane5f6a862009-01-07 18:07:41 -08001753
Tejun Heofa3ca072013-04-14 11:36:56 -07001754 cgroup_exit_root_id(root);
1755
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001756 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001757 mutex_unlock(&cgroup_mutex);
1758
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001759 simple_xattrs_free(&cgrp->xattrs);
1760
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001762 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763}
1764
1765static struct file_system_type cgroup_fs_type = {
1766 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001767 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768 .kill_sb = cgroup_kill_sb,
1769};
1770
Greg KH676db4a2010-08-05 13:53:35 -07001771static struct kobject *cgroup_kobj;
1772
Li Zefana043e3b2008-02-23 15:24:09 -08001773/**
1774 * cgroup_path - generate the path of a cgroup
1775 * @cgrp: the cgroup in question
1776 * @buf: the buffer to write the path into
1777 * @buflen: the length of the buffer
1778 *
Li Zefan65dff752013-03-01 15:01:56 +08001779 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1780 *
1781 * We can't generate cgroup path using dentry->d_name, as accessing
1782 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1783 * inode's i_mutex, while on the other hand cgroup_path() can be called
1784 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001786int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787{
Li Zefan65dff752013-03-01 15:01:56 +08001788 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001790
Tejun Heoda1f2962013-04-14 10:32:19 -07001791 if (!cgrp->parent) {
1792 if (strlcpy(buf, "/", buflen) >= buflen)
1793 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 return 0;
1795 }
1796
Tao Ma316eb662012-11-08 21:36:38 +08001797 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001798 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001799
Li Zefan65dff752013-03-01 15:01:56 +08001800 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001801 do {
Li Zefan65dff752013-03-01 15:01:56 +08001802 const char *name = cgroup_name(cgrp);
1803 int len;
1804
1805 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001806 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001807 goto out;
1808 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001809
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001811 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001813
1814 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001815 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001816 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001818out:
1819 rcu_read_unlock();
1820 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821}
Ben Blum67523c42010-03-10 15:22:11 -08001822EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823
Tejun Heo857a2be2013-04-14 20:50:08 -07001824/**
1825 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1826 * @task: target task
1827 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1828 * @buf: the buffer to write the path into
1829 * @buflen: the length of the buffer
1830 *
1831 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1832 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1833 * be used inside locks used by cgroup controller callbacks.
1834 */
1835int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1836 char *buf, size_t buflen)
1837{
1838 struct cgroupfs_root *root;
1839 struct cgroup *cgrp = NULL;
1840 int ret = -ENOENT;
1841
1842 mutex_lock(&cgroup_mutex);
1843
1844 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1845 if (root) {
1846 cgrp = task_cgroup_from_root(task, root);
1847 ret = cgroup_path(cgrp, buf, buflen);
1848 }
1849
1850 mutex_unlock(&cgroup_mutex);
1851
1852 return ret;
1853}
1854EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1855
Ben Blum74a11662011-05-26 16:25:20 -07001856/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001857 * Control Group taskset
1858 */
Tejun Heo134d3372011-12-12 18:12:21 -08001859struct task_and_cgroup {
1860 struct task_struct *task;
1861 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001862 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001863};
1864
Tejun Heo2f7ee562011-12-12 18:12:21 -08001865struct cgroup_taskset {
1866 struct task_and_cgroup single;
1867 struct flex_array *tc_array;
1868 int tc_array_len;
1869 int idx;
1870 struct cgroup *cur_cgrp;
1871};
1872
1873/**
1874 * cgroup_taskset_first - reset taskset and return the first task
1875 * @tset: taskset of interest
1876 *
1877 * @tset iteration is initialized and the first task is returned.
1878 */
1879struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1880{
1881 if (tset->tc_array) {
1882 tset->idx = 0;
1883 return cgroup_taskset_next(tset);
1884 } else {
1885 tset->cur_cgrp = tset->single.cgrp;
1886 return tset->single.task;
1887 }
1888}
1889EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1890
1891/**
1892 * cgroup_taskset_next - iterate to the next task in taskset
1893 * @tset: taskset of interest
1894 *
1895 * Return the next task in @tset. Iteration must have been initialized
1896 * with cgroup_taskset_first().
1897 */
1898struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1899{
1900 struct task_and_cgroup *tc;
1901
1902 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1903 return NULL;
1904
1905 tc = flex_array_get(tset->tc_array, tset->idx++);
1906 tset->cur_cgrp = tc->cgrp;
1907 return tc->task;
1908}
1909EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1910
1911/**
1912 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1913 * @tset: taskset of interest
1914 *
1915 * Return the cgroup for the current (last returned) task of @tset. This
1916 * function must be preceded by either cgroup_taskset_first() or
1917 * cgroup_taskset_next().
1918 */
1919struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1920{
1921 return tset->cur_cgrp;
1922}
1923EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1924
1925/**
1926 * cgroup_taskset_size - return the number of tasks in taskset
1927 * @tset: taskset of interest
1928 */
1929int cgroup_taskset_size(struct cgroup_taskset *tset)
1930{
1931 return tset->tc_array ? tset->tc_array_len : 1;
1932}
1933EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1934
1935
Ben Blum74a11662011-05-26 16:25:20 -07001936/*
1937 * cgroup_task_migrate - move a task from one cgroup to another.
1938 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001939 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001940 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001941static void cgroup_task_migrate(struct cgroup *old_cgrp,
1942 struct task_struct *tsk,
1943 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001944{
Tejun Heo5abb8852013-06-12 21:04:49 -07001945 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001946
1947 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001948 * We are synchronized through threadgroup_lock() against PF_EXITING
1949 * setting such that we can't race against cgroup_exit() changing the
1950 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001951 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001952 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001953 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001954
Ben Blum74a11662011-05-26 16:25:20 -07001955 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001956 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001957 task_unlock(tsk);
1958
1959 /* Update the css_set linked lists if we're using them */
1960 write_lock(&css_set_lock);
1961 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001962 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001963 write_unlock(&css_set_lock);
1964
1965 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001966 * We just gained a reference on old_cset by taking it from the
1967 * task. As trading it for new_cset is protected by cgroup_mutex,
1968 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001969 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001970 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1971 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001972}
1973
Li Zefana043e3b2008-02-23 15:24:09 -08001974/**
Li Zefan081aa452013-03-13 09:17:09 +08001975 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001976 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001977 * @tsk: the task or the leader of the threadgroup to be attached
1978 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001979 *
Tejun Heo257058a2011-12-12 18:12:21 -08001980 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001981 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001982 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001983static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1984 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001985{
1986 int retval, i, group_size;
1987 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001988 struct cgroupfs_root *root = cgrp->root;
1989 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001990 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001991 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001992 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001993 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001994
1995 /*
1996 * step 0: in order to do expensive, possibly blocking operations for
1997 * every thread, we cannot iterate the thread group list, since it needs
1998 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001999 * group - group_rwsem prevents new threads from appearing, and if
2000 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002001 */
Li Zefan081aa452013-03-13 09:17:09 +08002002 if (threadgroup)
2003 group_size = get_nr_threads(tsk);
2004 else
2005 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002006 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002007 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002008 if (!group)
2009 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002010 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002011 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002012 if (retval)
2013 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002014
Ben Blum74a11662011-05-26 16:25:20 -07002015 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002016 /*
2017 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2018 * already PF_EXITING could be freed from underneath us unless we
2019 * take an rcu_read_lock.
2020 */
2021 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002022 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002023 struct task_and_cgroup ent;
2024
Tejun Heocd3d0952011-12-12 18:12:21 -08002025 /* @tsk either already exited or can't exit until the end */
2026 if (tsk->flags & PF_EXITING)
2027 continue;
2028
Ben Blum74a11662011-05-26 16:25:20 -07002029 /* as per above, nr_threads may decrease, but not increase. */
2030 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002031 ent.task = tsk;
2032 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002033 /* nothing to do if this task is already in the cgroup */
2034 if (ent.cgrp == cgrp)
2035 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002036 /*
2037 * saying GFP_ATOMIC has no effect here because we did prealloc
2038 * earlier, but it's good form to communicate our expectations.
2039 */
Tejun Heo134d3372011-12-12 18:12:21 -08002040 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002041 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002042 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002043
2044 if (!threadgroup)
2045 break;
Ben Blum74a11662011-05-26 16:25:20 -07002046 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002047 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002048 /* remember the number of threads in the array for later. */
2049 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002050 tset.tc_array = group;
2051 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002052
Tejun Heo134d3372011-12-12 18:12:21 -08002053 /* methods shouldn't be called if no task is actually migrating */
2054 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002055 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002056 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002057
Ben Blum74a11662011-05-26 16:25:20 -07002058 /*
2059 * step 1: check that we can legitimately attach to the cgroup.
2060 */
2061 for_each_subsys(root, ss) {
2062 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002063 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002064 if (retval) {
2065 failed_ss = ss;
2066 goto out_cancel_attach;
2067 }
2068 }
Ben Blum74a11662011-05-26 16:25:20 -07002069 }
2070
2071 /*
2072 * step 2: make sure css_sets exist for all threads to be migrated.
2073 * we use find_css_set, which allocates a new one if necessary.
2074 */
Ben Blum74a11662011-05-26 16:25:20 -07002075 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002076 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002077 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2078 if (!tc->cg) {
2079 retval = -ENOMEM;
2080 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002081 }
2082 }
2083
2084 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002085 * step 3: now that we're guaranteed success wrt the css_sets,
2086 * proceed to move all tasks to the new cgroup. There are no
2087 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002088 */
Ben Blum74a11662011-05-26 16:25:20 -07002089 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002090 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002091 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002092 }
2093 /* nothing is sensitive to fork() after this point. */
2094
2095 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002096 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002097 */
2098 for_each_subsys(root, ss) {
2099 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002100 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002101 }
2102
2103 /*
2104 * step 5: success! and cleanup
2105 */
Ben Blum74a11662011-05-26 16:25:20 -07002106 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002107out_put_css_set_refs:
2108 if (retval) {
2109 for (i = 0; i < group_size; i++) {
2110 tc = flex_array_get(group, i);
2111 if (!tc->cg)
2112 break;
2113 put_css_set(tc->cg);
2114 }
Ben Blum74a11662011-05-26 16:25:20 -07002115 }
2116out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002117 if (retval) {
2118 for_each_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002119 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002120 break;
Ben Blum74a11662011-05-26 16:25:20 -07002121 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002122 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002123 }
2124 }
Ben Blum74a11662011-05-26 16:25:20 -07002125out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002126 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002127 return retval;
2128}
2129
2130/*
2131 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002132 * function to attach either it or all tasks in its threadgroup. Will lock
2133 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002134 */
2135static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002136{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002137 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002138 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 int ret;
2140
Ben Blum74a11662011-05-26 16:25:20 -07002141 if (!cgroup_lock_live_group(cgrp))
2142 return -ENODEV;
2143
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002144retry_find_task:
2145 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002146 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002147 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002148 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002150 ret= -ESRCH;
2151 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002152 }
Ben Blum74a11662011-05-26 16:25:20 -07002153 /*
2154 * even if we're attaching all tasks in the thread group, we
2155 * only need to check permissions on one of them.
2156 */
David Howellsc69e8d92008-11-14 10:39:19 +11002157 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002158 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2159 !uid_eq(cred->euid, tcred->uid) &&
2160 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002161 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002162 ret = -EACCES;
2163 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002164 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 } else
2166 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002167
2168 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002170
2171 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002172 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002173 * trapped in a cpuset, or RT worker may be born in a cgroup
2174 * with no rt_runtime allocated. Just say no.
2175 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002176 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002177 ret = -EINVAL;
2178 rcu_read_unlock();
2179 goto out_unlock_cgroup;
2180 }
2181
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002182 get_task_struct(tsk);
2183 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002184
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185 threadgroup_lock(tsk);
2186 if (threadgroup) {
2187 if (!thread_group_leader(tsk)) {
2188 /*
2189 * a race with de_thread from another thread's exec()
2190 * may strip us of our leadership, if this happens,
2191 * there is no choice but to throw this task away and
2192 * try again; this is
2193 * "double-double-toil-and-trouble-check locking".
2194 */
2195 threadgroup_unlock(tsk);
2196 put_task_struct(tsk);
2197 goto retry_find_task;
2198 }
Li Zefan081aa452013-03-13 09:17:09 +08002199 }
2200
2201 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2202
Tejun Heocd3d0952011-12-12 18:12:21 -08002203 threadgroup_unlock(tsk);
2204
Paul Menagebbcb81d2007-10-18 23:39:32 -07002205 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002207 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002208 return ret;
2209}
2210
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002211/**
2212 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2213 * @from: attach to all cgroups of a given task
2214 * @tsk: the task to be attached
2215 */
2216int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2217{
2218 struct cgroupfs_root *root;
2219 int retval = 0;
2220
Tejun Heo47cfcd02013-04-07 09:29:51 -07002221 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002222 for_each_active_root(root) {
2223 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2224
2225 retval = cgroup_attach_task(from_cg, tsk, false);
2226 if (retval)
2227 break;
2228 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002229 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002230
2231 return retval;
2232}
2233EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2234
Paul Menageaf351022008-07-25 01:47:01 -07002235static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2236{
Ben Blum74a11662011-05-26 16:25:20 -07002237 return attach_task_by_pid(cgrp, pid, false);
2238}
2239
2240static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2241{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002242 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002243}
2244
Paul Menagee788e062008-07-25 01:46:59 -07002245static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2246 const char *buffer)
2247{
2248 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002249 if (strlen(buffer) >= PATH_MAX)
2250 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002251 if (!cgroup_lock_live_group(cgrp))
2252 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002253 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002254 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002255 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002256 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002257 return 0;
2258}
2259
2260static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2261 struct seq_file *seq)
2262{
2263 if (!cgroup_lock_live_group(cgrp))
2264 return -ENODEV;
2265 seq_puts(seq, cgrp->root->release_agent_path);
2266 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002267 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002268 return 0;
2269}
2270
Tejun Heo873fe092013-04-14 20:15:26 -07002271static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2272 struct seq_file *seq)
2273{
2274 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002275 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002276}
2277
Paul Menage84eea842008-07-25 01:47:00 -07002278/* A buffer size big enough for numbers or short strings */
2279#define CGROUP_LOCAL_BUFFER_SIZE 64
2280
Paul Menagee73d2c62008-04-29 01:00:06 -07002281static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002282 struct file *file,
2283 const char __user *userbuf,
2284 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002285{
Paul Menage84eea842008-07-25 01:47:00 -07002286 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002287 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002288 char *end;
2289
2290 if (!nbytes)
2291 return -EINVAL;
2292 if (nbytes >= sizeof(buffer))
2293 return -E2BIG;
2294 if (copy_from_user(buffer, userbuf, nbytes))
2295 return -EFAULT;
2296
2297 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002298 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002299 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002300 if (*end)
2301 return -EINVAL;
2302 retval = cft->write_u64(cgrp, cft, val);
2303 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002304 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002305 if (*end)
2306 return -EINVAL;
2307 retval = cft->write_s64(cgrp, cft, val);
2308 }
Paul Menage355e0c42007-10-18 23:39:33 -07002309 if (!retval)
2310 retval = nbytes;
2311 return retval;
2312}
2313
Paul Menagedb3b1492008-07-25 01:46:58 -07002314static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2315 struct file *file,
2316 const char __user *userbuf,
2317 size_t nbytes, loff_t *unused_ppos)
2318{
Paul Menage84eea842008-07-25 01:47:00 -07002319 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002320 int retval = 0;
2321 size_t max_bytes = cft->max_write_len;
2322 char *buffer = local_buffer;
2323
2324 if (!max_bytes)
2325 max_bytes = sizeof(local_buffer) - 1;
2326 if (nbytes >= max_bytes)
2327 return -E2BIG;
2328 /* Allocate a dynamic buffer if we need one */
2329 if (nbytes >= sizeof(local_buffer)) {
2330 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2331 if (buffer == NULL)
2332 return -ENOMEM;
2333 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002334 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2335 retval = -EFAULT;
2336 goto out;
2337 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002338
2339 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002340 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002341 if (!retval)
2342 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002343out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002344 if (buffer != local_buffer)
2345 kfree(buffer);
2346 return retval;
2347}
2348
Paul Menageddbcc7e2007-10-18 23:39:30 -07002349static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2350 size_t nbytes, loff_t *ppos)
2351{
2352 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002353 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002354
Tejun Heo54766d42013-06-12 21:04:53 -07002355 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002356 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002357 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002358 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002359 if (cft->write_u64 || cft->write_s64)
2360 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002361 if (cft->write_string)
2362 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002363 if (cft->trigger) {
2364 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2365 return ret ? ret : nbytes;
2366 }
Paul Menage355e0c42007-10-18 23:39:33 -07002367 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002368}
2369
Paul Menagef4c753b2008-04-29 00:59:56 -07002370static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2371 struct file *file,
2372 char __user *buf, size_t nbytes,
2373 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002374{
Paul Menage84eea842008-07-25 01:47:00 -07002375 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002376 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2378
2379 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2380}
2381
Paul Menagee73d2c62008-04-29 01:00:06 -07002382static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2383 struct file *file,
2384 char __user *buf, size_t nbytes,
2385 loff_t *ppos)
2386{
Paul Menage84eea842008-07-25 01:47:00 -07002387 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002388 s64 val = cft->read_s64(cgrp, cft);
2389 int len = sprintf(tmp, "%lld\n", (long long) val);
2390
2391 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2392}
2393
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2395 size_t nbytes, loff_t *ppos)
2396{
2397 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002398 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399
Tejun Heo54766d42013-06-12 21:04:53 -07002400 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401 return -ENODEV;
2402
2403 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002404 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002405 if (cft->read_u64)
2406 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002407 if (cft->read_s64)
2408 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409 return -EINVAL;
2410}
2411
Paul Menage91796562008-04-29 01:00:01 -07002412/*
2413 * seqfile ops/methods for returning structured data. Currently just
2414 * supports string->u64 maps, but can be extended in future.
2415 */
2416
2417struct cgroup_seqfile_state {
2418 struct cftype *cft;
2419 struct cgroup *cgroup;
2420};
2421
2422static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2423{
2424 struct seq_file *sf = cb->state;
2425 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2426}
2427
2428static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2429{
2430 struct cgroup_seqfile_state *state = m->private;
2431 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002432 if (cft->read_map) {
2433 struct cgroup_map_cb cb = {
2434 .fill = cgroup_map_add,
2435 .state = m,
2436 };
2437 return cft->read_map(state->cgroup, cft, &cb);
2438 }
2439 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002440}
2441
Adrian Bunk96930a62008-07-25 19:46:21 -07002442static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002443{
2444 struct seq_file *seq = file->private_data;
2445 kfree(seq->private);
2446 return single_release(inode, file);
2447}
2448
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002449static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002450 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002451 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002452 .llseek = seq_lseek,
2453 .release = cgroup_seqfile_release,
2454};
2455
Paul Menageddbcc7e2007-10-18 23:39:30 -07002456static int cgroup_file_open(struct inode *inode, struct file *file)
2457{
2458 int err;
2459 struct cftype *cft;
2460
2461 err = generic_file_open(inode, file);
2462 if (err)
2463 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002465
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002466 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002467 struct cgroup_seqfile_state *state;
2468
2469 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002470 if (!state)
2471 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002472
Paul Menage91796562008-04-29 01:00:01 -07002473 state->cft = cft;
2474 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2475 file->f_op = &cgroup_seqfile_operations;
2476 err = single_open(file, cgroup_seqfile_show, state);
2477 if (err < 0)
2478 kfree(state);
2479 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 err = cft->open(inode, file);
2481 else
2482 err = 0;
2483
2484 return err;
2485}
2486
2487static int cgroup_file_release(struct inode *inode, struct file *file)
2488{
2489 struct cftype *cft = __d_cft(file->f_dentry);
2490 if (cft->release)
2491 return cft->release(inode, file);
2492 return 0;
2493}
2494
2495/*
2496 * cgroup_rename - Only allow simple rename of directories in place.
2497 */
2498static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2499 struct inode *new_dir, struct dentry *new_dentry)
2500{
Li Zefan65dff752013-03-01 15:01:56 +08002501 int ret;
2502 struct cgroup_name *name, *old_name;
2503 struct cgroup *cgrp;
2504
2505 /*
2506 * It's convinient to use parent dir's i_mutex to protected
2507 * cgrp->name.
2508 */
2509 lockdep_assert_held(&old_dir->i_mutex);
2510
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2512 return -ENOTDIR;
2513 if (new_dentry->d_inode)
2514 return -EEXIST;
2515 if (old_dir != new_dir)
2516 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002517
2518 cgrp = __d_cgrp(old_dentry);
2519
Tejun Heo6db8e852013-06-14 11:18:22 -07002520 /*
2521 * This isn't a proper migration and its usefulness is very
2522 * limited. Disallow if sane_behavior.
2523 */
2524 if (cgroup_sane_behavior(cgrp))
2525 return -EPERM;
2526
Li Zefan65dff752013-03-01 15:01:56 +08002527 name = cgroup_alloc_name(new_dentry);
2528 if (!name)
2529 return -ENOMEM;
2530
2531 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2532 if (ret) {
2533 kfree(name);
2534 return ret;
2535 }
2536
2537 old_name = cgrp->name;
2538 rcu_assign_pointer(cgrp->name, name);
2539
2540 kfree_rcu(old_name, rcu_head);
2541 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002542}
2543
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002544static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2545{
2546 if (S_ISDIR(dentry->d_inode->i_mode))
2547 return &__d_cgrp(dentry)->xattrs;
2548 else
Li Zefan712317a2013-04-18 23:09:52 -07002549 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002550}
2551
2552static inline int xattr_enabled(struct dentry *dentry)
2553{
2554 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002555 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002556}
2557
2558static bool is_valid_xattr(const char *name)
2559{
2560 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2561 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2562 return true;
2563 return false;
2564}
2565
2566static int cgroup_setxattr(struct dentry *dentry, const char *name,
2567 const void *val, size_t size, int flags)
2568{
2569 if (!xattr_enabled(dentry))
2570 return -EOPNOTSUPP;
2571 if (!is_valid_xattr(name))
2572 return -EINVAL;
2573 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2574}
2575
2576static int cgroup_removexattr(struct dentry *dentry, const char *name)
2577{
2578 if (!xattr_enabled(dentry))
2579 return -EOPNOTSUPP;
2580 if (!is_valid_xattr(name))
2581 return -EINVAL;
2582 return simple_xattr_remove(__d_xattrs(dentry), name);
2583}
2584
2585static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2586 void *buf, size_t size)
2587{
2588 if (!xattr_enabled(dentry))
2589 return -EOPNOTSUPP;
2590 if (!is_valid_xattr(name))
2591 return -EINVAL;
2592 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2593}
2594
2595static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2596{
2597 if (!xattr_enabled(dentry))
2598 return -EOPNOTSUPP;
2599 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2600}
2601
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002602static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002603 .read = cgroup_file_read,
2604 .write = cgroup_file_write,
2605 .llseek = generic_file_llseek,
2606 .open = cgroup_file_open,
2607 .release = cgroup_file_release,
2608};
2609
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002610static const struct inode_operations cgroup_file_inode_operations = {
2611 .setxattr = cgroup_setxattr,
2612 .getxattr = cgroup_getxattr,
2613 .listxattr = cgroup_listxattr,
2614 .removexattr = cgroup_removexattr,
2615};
2616
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002617static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002618 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619 .mkdir = cgroup_mkdir,
2620 .rmdir = cgroup_rmdir,
2621 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002622 .setxattr = cgroup_setxattr,
2623 .getxattr = cgroup_getxattr,
2624 .listxattr = cgroup_listxattr,
2625 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002626};
2627
Al Viro00cd8dd2012-06-10 17:13:09 -04002628static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002629{
2630 if (dentry->d_name.len > NAME_MAX)
2631 return ERR_PTR(-ENAMETOOLONG);
2632 d_add(dentry, NULL);
2633 return NULL;
2634}
2635
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002636/*
2637 * Check if a file is a control file
2638 */
2639static inline struct cftype *__file_cft(struct file *file)
2640{
Al Viro496ad9a2013-01-23 17:07:38 -05002641 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002642 return ERR_PTR(-EINVAL);
2643 return __d_cft(file->f_dentry);
2644}
2645
Al Viroa5e7ed32011-07-26 01:55:55 -04002646static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002647 struct super_block *sb)
2648{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002649 struct inode *inode;
2650
2651 if (!dentry)
2652 return -ENOENT;
2653 if (dentry->d_inode)
2654 return -EEXIST;
2655
2656 inode = cgroup_new_inode(mode, sb);
2657 if (!inode)
2658 return -ENOMEM;
2659
2660 if (S_ISDIR(mode)) {
2661 inode->i_op = &cgroup_dir_inode_operations;
2662 inode->i_fop = &simple_dir_operations;
2663
2664 /* start off with i_nlink == 2 (for "." entry) */
2665 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002666 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002667
Tejun Heob8a2df62012-11-19 08:13:37 -08002668 /*
2669 * Control reaches here with cgroup_mutex held.
2670 * @inode->i_mutex should nest outside cgroup_mutex but we
2671 * want to populate it immediately without releasing
2672 * cgroup_mutex. As @inode isn't visible to anyone else
2673 * yet, trylock will always succeed without affecting
2674 * lockdep checks.
2675 */
2676 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 } else if (S_ISREG(mode)) {
2678 inode->i_size = 0;
2679 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002680 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002681 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 d_instantiate(dentry, inode);
2683 dget(dentry); /* Extra count - pin the dentry in core */
2684 return 0;
2685}
2686
Li Zefan099fca32009-04-02 16:57:29 -07002687/**
2688 * cgroup_file_mode - deduce file mode of a control file
2689 * @cft: the control file in question
2690 *
2691 * returns cft->mode if ->mode is not 0
2692 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2693 * returns S_IRUGO if it has only a read handler
2694 * returns S_IWUSR if it has only a write hander
2695 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002696static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002697{
Al Viroa5e7ed32011-07-26 01:55:55 -04002698 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002699
2700 if (cft->mode)
2701 return cft->mode;
2702
2703 if (cft->read || cft->read_u64 || cft->read_s64 ||
2704 cft->read_map || cft->read_seq_string)
2705 mode |= S_IRUGO;
2706
2707 if (cft->write || cft->write_u64 || cft->write_s64 ||
2708 cft->write_string || cft->trigger)
2709 mode |= S_IWUSR;
2710
2711 return mode;
2712}
2713
Tejun Heodb0416b2012-04-01 12:09:55 -07002714static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002715 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716{
Paul Menagebd89aab2007-10-18 23:40:44 -07002717 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002718 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002720 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002721 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002722 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002723 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002724
Tejun Heo93438622013-04-14 20:15:25 -07002725 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002726 strcpy(name, subsys->name);
2727 strcat(name, ".");
2728 }
2729 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002732
2733 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2734 if (!cfe)
2735 return -ENOMEM;
2736
Paul Menageddbcc7e2007-10-18 23:39:30 -07002737 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002738 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002740 goto out;
2741 }
2742
Li Zefand6cbf352013-05-14 19:44:20 +08002743 cfe->type = (void *)cft;
2744 cfe->dentry = dentry;
2745 dentry->d_fsdata = cfe;
2746 simple_xattrs_init(&cfe->xattrs);
2747
Tejun Heo05ef1d72012-04-01 12:09:56 -07002748 mode = cgroup_file_mode(cft);
2749 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2750 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002751 list_add_tail(&cfe->node, &parent->files);
2752 cfe = NULL;
2753 }
2754 dput(dentry);
2755out:
2756 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 return error;
2758}
2759
Tejun Heo79578622012-04-01 12:09:56 -07002760static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002761 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002763 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002764 int err, ret = 0;
2765
2766 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002767 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002768 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2769 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002770 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2771 continue;
2772 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2773 continue;
2774
Li Zefan2739d3c2013-01-21 18:18:33 +08002775 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002776 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002777 if (err)
2778 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2779 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002780 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002781 } else {
2782 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002783 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002785 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002786}
2787
Tejun Heo8e3f6542012-04-01 12:09:55 -07002788static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002789 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002790{
2791 /*
2792 * Thanks to the entanglement with vfs inode locking, we can't walk
2793 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002794 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2795 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002797 mutex_lock(&cgroup_mutex);
2798}
2799
2800static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002801 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002802 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002803{
2804 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002805 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002806 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002807 struct dentry *prev = NULL;
2808 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002809 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002810
2811 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002812 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002813 !atomic_inc_not_zero(&sb->s_active)) {
2814 mutex_unlock(&cgroup_mutex);
2815 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002816 }
2817
Li Zefane8c82d22013-06-18 18:48:37 +08002818 /*
2819 * All cgroups which are created after we drop cgroup_mutex will
2820 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002821 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002822 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002823 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002824
Tejun Heo8e3f6542012-04-01 12:09:55 -07002825 mutex_unlock(&cgroup_mutex);
2826
Li Zefane8c82d22013-06-18 18:48:37 +08002827 /* @root always needs to be updated */
2828 inode = root->dentry->d_inode;
2829 mutex_lock(&inode->i_mutex);
2830 mutex_lock(&cgroup_mutex);
2831 cgroup_addrm_files(root, ss, cfts, is_add);
2832 mutex_unlock(&cgroup_mutex);
2833 mutex_unlock(&inode->i_mutex);
2834
2835 /* add/rm files for all cgroups created before */
2836 rcu_read_lock();
2837 cgroup_for_each_descendant_pre(cgrp, root) {
2838 if (cgroup_is_dead(cgrp))
2839 continue;
2840
2841 inode = cgrp->dentry->d_inode;
2842 dget(cgrp->dentry);
2843 rcu_read_unlock();
2844
2845 dput(prev);
2846 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002847
2848 mutex_lock(&inode->i_mutex);
2849 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002850 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002851 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852 mutex_unlock(&cgroup_mutex);
2853 mutex_unlock(&inode->i_mutex);
2854
Li Zefane8c82d22013-06-18 18:48:37 +08002855 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002856 }
Li Zefane8c82d22013-06-18 18:48:37 +08002857 rcu_read_unlock();
2858 dput(prev);
2859 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002860}
2861
2862/**
2863 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2864 * @ss: target cgroup subsystem
2865 * @cfts: zero-length name terminated array of cftypes
2866 *
2867 * Register @cfts to @ss. Files described by @cfts are created for all
2868 * existing cgroups to which @ss is attached and all future cgroups will
2869 * have them too. This function can be called anytime whether @ss is
2870 * attached or not.
2871 *
2872 * Returns 0 on successful registration, -errno on failure. Note that this
2873 * function currently returns 0 as long as @cfts registration is successful
2874 * even if some file creation attempts on existing cgroups fail.
2875 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002876int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002877{
2878 struct cftype_set *set;
2879
2880 set = kzalloc(sizeof(*set), GFP_KERNEL);
2881 if (!set)
2882 return -ENOMEM;
2883
2884 cgroup_cfts_prepare();
2885 set->cfts = cfts;
2886 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002887 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002888
2889 return 0;
2890}
2891EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2892
Li Zefana043e3b2008-02-23 15:24:09 -08002893/**
Tejun Heo79578622012-04-01 12:09:56 -07002894 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2895 * @ss: target cgroup subsystem
2896 * @cfts: zero-length name terminated array of cftypes
2897 *
2898 * Unregister @cfts from @ss. Files described by @cfts are removed from
2899 * all existing cgroups to which @ss is attached and all future cgroups
2900 * won't have them either. This function can be called anytime whether @ss
2901 * is attached or not.
2902 *
2903 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2904 * registered with @ss.
2905 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002906int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002907{
2908 struct cftype_set *set;
2909
2910 cgroup_cfts_prepare();
2911
2912 list_for_each_entry(set, &ss->cftsets, node) {
2913 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002914 list_del(&set->node);
2915 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002916 cgroup_cfts_commit(ss, cfts, false);
2917 return 0;
2918 }
2919 }
2920
2921 cgroup_cfts_commit(ss, NULL, false);
2922 return -ENOENT;
2923}
2924
2925/**
Li Zefana043e3b2008-02-23 15:24:09 -08002926 * cgroup_task_count - count the number of tasks in a cgroup.
2927 * @cgrp: the cgroup in question
2928 *
2929 * Return the number of tasks in the cgroup.
2930 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002931int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002932{
2933 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002934 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002935
Paul Menage817929e2007-10-18 23:39:36 -07002936 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002937 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2938 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002939 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002940 return count;
2941}
2942
2943/*
Paul Menage817929e2007-10-18 23:39:36 -07002944 * Advance a list_head iterator. The iterator should be positioned at
2945 * the start of a css_set
2946 */
Tejun Heo69d02062013-06-12 21:04:50 -07002947static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002948{
Tejun Heo69d02062013-06-12 21:04:50 -07002949 struct list_head *l = it->cset_link;
2950 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002951 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002952
2953 /* Advance to the next non-empty css_set */
2954 do {
2955 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002956 if (l == &cgrp->cset_links) {
2957 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002958 return;
2959 }
Tejun Heo69d02062013-06-12 21:04:50 -07002960 link = list_entry(l, struct cgrp_cset_link, cset_link);
2961 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002962 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002963 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002964 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002965}
2966
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967/*
2968 * To reduce the fork() overhead for systems that are not actually
2969 * using their cgroups capability, we don't maintain the lists running
2970 * through each css_set to its tasks until we see the list actually
2971 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002972 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002973static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002974{
2975 struct task_struct *p, *g;
2976 write_lock(&css_set_lock);
2977 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002978 /*
2979 * We need tasklist_lock because RCU is not safe against
2980 * while_each_thread(). Besides, a forking task that has passed
2981 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2982 * is not guaranteed to have its child immediately visible in the
2983 * tasklist if we walk through it with RCU.
2984 */
2985 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002986 do_each_thread(g, p) {
2987 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002988 /*
2989 * We should check if the process is exiting, otherwise
2990 * it will race with cgroup_exit() in that the list
2991 * entry won't be deleted though the process has exited.
2992 */
2993 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002994 list_add(&p->cg_list, &p->cgroups->tasks);
2995 task_unlock(p);
2996 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002997 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002998 write_unlock(&css_set_lock);
2999}
3000
Tejun Heo574bd9f2012-11-09 09:12:29 -08003001/**
Tejun Heo53fa5262013-05-24 10:55:38 +09003002 * cgroup_next_sibling - find the next sibling of a given cgroup
3003 * @pos: the current cgroup
3004 *
3005 * This function returns the next sibling of @pos and should be called
3006 * under RCU read lock. The only requirement is that @pos is accessible.
3007 * The next sibling is guaranteed to be returned regardless of @pos's
3008 * state.
3009 */
3010struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3011{
3012 struct cgroup *next;
3013
3014 WARN_ON_ONCE(!rcu_read_lock_held());
3015
3016 /*
3017 * @pos could already have been removed. Once a cgroup is removed,
3018 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003019 * changes. As CGRP_DEAD assertion is serialized and happens
3020 * before the cgroup is taken off the ->sibling list, if we see it
3021 * unasserted, it's guaranteed that the next sibling hasn't
3022 * finished its grace period even if it's already removed, and thus
3023 * safe to dereference from this RCU critical section. If
3024 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3025 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003026 */
Tejun Heo54766d42013-06-12 21:04:53 -07003027 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003028 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3029 if (&next->sibling != &pos->parent->children)
3030 return next;
3031 return NULL;
3032 }
3033
3034 /*
3035 * Can't dereference the next pointer. Each cgroup is given a
3036 * monotonically increasing unique serial number and always
3037 * appended to the sibling list, so the next one can be found by
3038 * walking the parent's children until we see a cgroup with higher
3039 * serial number than @pos's.
3040 *
3041 * While this path can be slow, it's taken only when either the
3042 * current cgroup is removed or iteration and removal race.
3043 */
3044 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3045 if (next->serial_nr > pos->serial_nr)
3046 return next;
3047 return NULL;
3048}
3049EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3050
3051/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003052 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3053 * @pos: the current position (%NULL to initiate traversal)
3054 * @cgroup: cgroup whose descendants to walk
3055 *
3056 * To be used by cgroup_for_each_descendant_pre(). Find the next
3057 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003058 *
3059 * While this function requires RCU read locking, it doesn't require the
3060 * whole traversal to be contained in a single RCU critical section. This
3061 * function will return the correct next descendant as long as both @pos
3062 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003063 */
3064struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3065 struct cgroup *cgroup)
3066{
3067 struct cgroup *next;
3068
3069 WARN_ON_ONCE(!rcu_read_lock_held());
3070
3071 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003072 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003073 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003074
3075 /* visit the first child if exists */
3076 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3077 if (next)
3078 return next;
3079
3080 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003081 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003082 next = cgroup_next_sibling(pos);
3083 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003084 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003086 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003087
3088 return NULL;
3089}
3090EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3091
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003092/**
3093 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3094 * @pos: cgroup of interest
3095 *
3096 * Return the rightmost descendant of @pos. If there's no descendant,
3097 * @pos is returned. This can be used during pre-order traversal to skip
3098 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003099 *
3100 * While this function requires RCU read locking, it doesn't require the
3101 * whole traversal to be contained in a single RCU critical section. This
3102 * function will return the correct rightmost descendant as long as @pos is
3103 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003104 */
3105struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3106{
3107 struct cgroup *last, *tmp;
3108
3109 WARN_ON_ONCE(!rcu_read_lock_held());
3110
3111 do {
3112 last = pos;
3113 /* ->prev isn't RCU safe, walk ->next till the end */
3114 pos = NULL;
3115 list_for_each_entry_rcu(tmp, &last->children, sibling)
3116 pos = tmp;
3117 } while (pos);
3118
3119 return last;
3120}
3121EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3122
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3124{
3125 struct cgroup *last;
3126
3127 do {
3128 last = pos;
3129 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3130 sibling);
3131 } while (pos);
3132
3133 return last;
3134}
3135
3136/**
3137 * cgroup_next_descendant_post - find the next descendant for post-order walk
3138 * @pos: the current position (%NULL to initiate traversal)
3139 * @cgroup: cgroup whose descendants to walk
3140 *
3141 * To be used by cgroup_for_each_descendant_post(). Find the next
3142 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003143 *
3144 * While this function requires RCU read locking, it doesn't require the
3145 * whole traversal to be contained in a single RCU critical section. This
3146 * function will return the correct next descendant as long as both @pos
3147 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003148 */
3149struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3150 struct cgroup *cgroup)
3151{
3152 struct cgroup *next;
3153
3154 WARN_ON_ONCE(!rcu_read_lock_held());
3155
3156 /* if first iteration, visit the leftmost descendant */
3157 if (!pos) {
3158 next = cgroup_leftmost_descendant(cgroup);
3159 return next != cgroup ? next : NULL;
3160 }
3161
3162 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003163 next = cgroup_next_sibling(pos);
3164 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003165 return cgroup_leftmost_descendant(next);
3166
3167 /* no sibling left, visit parent */
3168 next = pos->parent;
3169 return next != cgroup ? next : NULL;
3170}
3171EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3172
Paul Menagebd89aab2007-10-18 23:40:44 -07003173void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003174 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003175{
3176 /*
3177 * The first time anyone tries to iterate across a cgroup,
3178 * we need to enable the list linking each css_set to its
3179 * tasks, and fix up all existing tasks.
3180 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003181 if (!use_task_css_set_links)
3182 cgroup_enable_task_cg_lists();
3183
Paul Menage817929e2007-10-18 23:39:36 -07003184 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003185 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003186 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003187}
3188
Paul Menagebd89aab2007-10-18 23:40:44 -07003189struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003190 struct cgroup_iter *it)
3191{
3192 struct task_struct *res;
3193 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003194 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003195
3196 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003197 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003198 return NULL;
3199 res = list_entry(l, struct task_struct, cg_list);
3200 /* Advance iterator to find next entry */
3201 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003202 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3203 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003204 /* We reached the end of this task list - move on to
3205 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003206 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003207 } else {
3208 it->task = l;
3209 }
3210 return res;
3211}
3212
Paul Menagebd89aab2007-10-18 23:40:44 -07003213void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003214 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003215{
3216 read_unlock(&css_set_lock);
3217}
3218
Cliff Wickman31a7df02008-02-07 00:14:42 -08003219static inline int started_after_time(struct task_struct *t1,
3220 struct timespec *time,
3221 struct task_struct *t2)
3222{
3223 int start_diff = timespec_compare(&t1->start_time, time);
3224 if (start_diff > 0) {
3225 return 1;
3226 } else if (start_diff < 0) {
3227 return 0;
3228 } else {
3229 /*
3230 * Arbitrarily, if two processes started at the same
3231 * time, we'll say that the lower pointer value
3232 * started first. Note that t2 may have exited by now
3233 * so this may not be a valid pointer any longer, but
3234 * that's fine - it still serves to distinguish
3235 * between two tasks started (effectively) simultaneously.
3236 */
3237 return t1 > t2;
3238 }
3239}
3240
3241/*
3242 * This function is a callback from heap_insert() and is used to order
3243 * the heap.
3244 * In this case we order the heap in descending task start time.
3245 */
3246static inline int started_after(void *p1, void *p2)
3247{
3248 struct task_struct *t1 = p1;
3249 struct task_struct *t2 = p2;
3250 return started_after_time(t1, &t2->start_time, t2);
3251}
3252
3253/**
3254 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3255 * @scan: struct cgroup_scanner containing arguments for the scan
3256 *
3257 * Arguments include pointers to callback functions test_task() and
3258 * process_task().
3259 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3260 * and if it returns true, call process_task() for it also.
3261 * The test_task pointer may be NULL, meaning always true (select all tasks).
3262 * Effectively duplicates cgroup_iter_{start,next,end}()
3263 * but does not lock css_set_lock for the call to process_task().
3264 * The struct cgroup_scanner may be embedded in any structure of the caller's
3265 * creation.
3266 * It is guaranteed that process_task() will act on every task that
3267 * is a member of the cgroup for the duration of this call. This
3268 * function may or may not call process_task() for tasks that exit
3269 * or move to a different cgroup during the call, or are forked or
3270 * move into the cgroup during the call.
3271 *
3272 * Note that test_task() may be called with locks held, and may in some
3273 * situations be called multiple times for the same task, so it should
3274 * be cheap.
3275 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3276 * pre-allocated and will be used for heap operations (and its "gt" member will
3277 * be overwritten), else a temporary heap will be used (allocation of which
3278 * may cause this function to fail).
3279 */
3280int cgroup_scan_tasks(struct cgroup_scanner *scan)
3281{
3282 int retval, i;
3283 struct cgroup_iter it;
3284 struct task_struct *p, *dropped;
3285 /* Never dereference latest_task, since it's not refcounted */
3286 struct task_struct *latest_task = NULL;
3287 struct ptr_heap tmp_heap;
3288 struct ptr_heap *heap;
3289 struct timespec latest_time = { 0, 0 };
3290
3291 if (scan->heap) {
3292 /* The caller supplied our heap and pre-allocated its memory */
3293 heap = scan->heap;
3294 heap->gt = &started_after;
3295 } else {
3296 /* We need to allocate our own heap memory */
3297 heap = &tmp_heap;
3298 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3299 if (retval)
3300 /* cannot allocate the heap */
3301 return retval;
3302 }
3303
3304 again:
3305 /*
3306 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3307 * to determine which are of interest, and using the scanner's
3308 * "process_task" callback to process any of them that need an update.
3309 * Since we don't want to hold any locks during the task updates,
3310 * gather tasks to be processed in a heap structure.
3311 * The heap is sorted by descending task start time.
3312 * If the statically-sized heap fills up, we overflow tasks that
3313 * started later, and in future iterations only consider tasks that
3314 * started after the latest task in the previous pass. This
3315 * guarantees forward progress and that we don't miss any tasks.
3316 */
3317 heap->size = 0;
3318 cgroup_iter_start(scan->cg, &it);
3319 while ((p = cgroup_iter_next(scan->cg, &it))) {
3320 /*
3321 * Only affect tasks that qualify per the caller's callback,
3322 * if he provided one
3323 */
3324 if (scan->test_task && !scan->test_task(p, scan))
3325 continue;
3326 /*
3327 * Only process tasks that started after the last task
3328 * we processed
3329 */
3330 if (!started_after_time(p, &latest_time, latest_task))
3331 continue;
3332 dropped = heap_insert(heap, p);
3333 if (dropped == NULL) {
3334 /*
3335 * The new task was inserted; the heap wasn't
3336 * previously full
3337 */
3338 get_task_struct(p);
3339 } else if (dropped != p) {
3340 /*
3341 * The new task was inserted, and pushed out a
3342 * different task
3343 */
3344 get_task_struct(p);
3345 put_task_struct(dropped);
3346 }
3347 /*
3348 * Else the new task was newer than anything already in
3349 * the heap and wasn't inserted
3350 */
3351 }
3352 cgroup_iter_end(scan->cg, &it);
3353
3354 if (heap->size) {
3355 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003356 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003358 latest_time = q->start_time;
3359 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003360 }
3361 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003362 scan->process_task(q, scan);
3363 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003364 }
3365 /*
3366 * If we had to process any tasks at all, scan again
3367 * in case some of them were in the middle of forking
3368 * children that didn't get processed.
3369 * Not the most efficient way to do it, but it avoids
3370 * having to take callback_mutex in the fork path
3371 */
3372 goto again;
3373 }
3374 if (heap == &tmp_heap)
3375 heap_free(&tmp_heap);
3376 return 0;
3377}
3378
Tejun Heo8cc99342013-04-07 09:29:50 -07003379static void cgroup_transfer_one_task(struct task_struct *task,
3380 struct cgroup_scanner *scan)
3381{
3382 struct cgroup *new_cgroup = scan->data;
3383
Tejun Heo47cfcd02013-04-07 09:29:51 -07003384 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003385 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003386 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003387}
3388
3389/**
3390 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3391 * @to: cgroup to which the tasks will be moved
3392 * @from: cgroup in which the tasks currently reside
3393 */
3394int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3395{
3396 struct cgroup_scanner scan;
3397
3398 scan.cg = from;
3399 scan.test_task = NULL; /* select all tasks in cgroup */
3400 scan.process_task = cgroup_transfer_one_task;
3401 scan.heap = NULL;
3402 scan.data = to;
3403
3404 return cgroup_scan_tasks(&scan);
3405}
3406
Paul Menage817929e2007-10-18 23:39:36 -07003407/*
Ben Blum102a7752009-09-23 15:56:26 -07003408 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003409 *
3410 * Reading this file can return large amounts of data if a cgroup has
3411 * *lots* of attached tasks. So it may need several calls to read(),
3412 * but we cannot guarantee that the information we produce is correct
3413 * unless we produce it entirely atomically.
3414 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003415 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003416
Li Zefan24528252012-01-20 11:58:43 +08003417/* which pidlist file are we talking about? */
3418enum cgroup_filetype {
3419 CGROUP_FILE_PROCS,
3420 CGROUP_FILE_TASKS,
3421};
3422
3423/*
3424 * A pidlist is a list of pids that virtually represents the contents of one
3425 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3426 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3427 * to the cgroup.
3428 */
3429struct cgroup_pidlist {
3430 /*
3431 * used to find which pidlist is wanted. doesn't change as long as
3432 * this particular list stays in the list.
3433 */
3434 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3435 /* array of xids */
3436 pid_t *list;
3437 /* how many elements the above list has */
3438 int length;
3439 /* how many files are using the current array */
3440 int use_count;
3441 /* each of these stored in a list by its cgroup */
3442 struct list_head links;
3443 /* pointer to the cgroup we belong to, for list removal purposes */
3444 struct cgroup *owner;
3445 /* protects the other fields */
3446 struct rw_semaphore mutex;
3447};
3448
Paul Menagebbcb81d2007-10-18 23:39:32 -07003449/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003450 * The following two functions "fix" the issue where there are more pids
3451 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3452 * TODO: replace with a kernel-wide solution to this problem
3453 */
3454#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3455static void *pidlist_allocate(int count)
3456{
3457 if (PIDLIST_TOO_LARGE(count))
3458 return vmalloc(count * sizeof(pid_t));
3459 else
3460 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3461}
3462static void pidlist_free(void *p)
3463{
3464 if (is_vmalloc_addr(p))
3465 vfree(p);
3466 else
3467 kfree(p);
3468}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003469
3470/*
Ben Blum102a7752009-09-23 15:56:26 -07003471 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003472 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003473 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003474static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003475{
Ben Blum102a7752009-09-23 15:56:26 -07003476 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003477
3478 /*
3479 * we presume the 0th element is unique, so i starts at 1. trivial
3480 * edge cases first; no work needs to be done for either
3481 */
3482 if (length == 0 || length == 1)
3483 return length;
3484 /* src and dest walk down the list; dest counts unique elements */
3485 for (src = 1; src < length; src++) {
3486 /* find next unique element */
3487 while (list[src] == list[src-1]) {
3488 src++;
3489 if (src == length)
3490 goto after;
3491 }
3492 /* dest always points to where the next unique element goes */
3493 list[dest] = list[src];
3494 dest++;
3495 }
3496after:
Ben Blum102a7752009-09-23 15:56:26 -07003497 return dest;
3498}
3499
3500static int cmppid(const void *a, const void *b)
3501{
3502 return *(pid_t *)a - *(pid_t *)b;
3503}
3504
3505/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003506 * find the appropriate pidlist for our purpose (given procs vs tasks)
3507 * returns with the lock on that pidlist already held, and takes care
3508 * of the use count, or returns NULL with no locks held if we're out of
3509 * memory.
3510 */
3511static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3512 enum cgroup_filetype type)
3513{
3514 struct cgroup_pidlist *l;
3515 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003516 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003517
Ben Blum72a8cb32009-09-23 15:56:27 -07003518 /*
3519 * We can't drop the pidlist_mutex before taking the l->mutex in case
3520 * the last ref-holder is trying to remove l from the list at the same
3521 * time. Holding the pidlist_mutex precludes somebody taking whichever
3522 * list we find out from under us - compare release_pid_array().
3523 */
3524 mutex_lock(&cgrp->pidlist_mutex);
3525 list_for_each_entry(l, &cgrp->pidlists, links) {
3526 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003527 /* make sure l doesn't vanish out from under us */
3528 down_write(&l->mutex);
3529 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 return l;
3531 }
3532 }
3533 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003534 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003535 if (!l) {
3536 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003537 return l;
3538 }
3539 init_rwsem(&l->mutex);
3540 down_write(&l->mutex);
3541 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003542 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003543 l->owner = cgrp;
3544 list_add(&l->links, &cgrp->pidlists);
3545 mutex_unlock(&cgrp->pidlist_mutex);
3546 return l;
3547}
3548
3549/*
Ben Blum102a7752009-09-23 15:56:26 -07003550 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3551 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003552static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3553 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003554{
3555 pid_t *array;
3556 int length;
3557 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003558 struct cgroup_iter it;
3559 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003560 struct cgroup_pidlist *l;
3561
3562 /*
3563 * If cgroup gets more users after we read count, we won't have
3564 * enough space - tough. This race is indistinguishable to the
3565 * caller from the case that the additional cgroup users didn't
3566 * show up until sometime later on.
3567 */
3568 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003569 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003570 if (!array)
3571 return -ENOMEM;
3572 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003573 cgroup_iter_start(cgrp, &it);
3574 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003575 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003576 break;
Ben Blum102a7752009-09-23 15:56:26 -07003577 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003578 if (type == CGROUP_FILE_PROCS)
3579 pid = task_tgid_vnr(tsk);
3580 else
3581 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003582 if (pid > 0) /* make sure to only use valid results */
3583 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003584 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003585 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003586 length = n;
3587 /* now sort & (if procs) strip out duplicates */
3588 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003589 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003590 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 l = cgroup_pidlist_find(cgrp, type);
3592 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003593 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003595 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003596 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003597 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003598 l->list = array;
3599 l->length = length;
3600 l->use_count++;
3601 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003603 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003604}
3605
Balbir Singh846c7bb2007-10-18 23:39:44 -07003606/**
Li Zefana043e3b2008-02-23 15:24:09 -08003607 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003608 * @stats: cgroupstats to fill information into
3609 * @dentry: A dentry entry belonging to the cgroup for which stats have
3610 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003611 *
3612 * Build and fill cgroupstats so that taskstats can export it to user
3613 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003614 */
3615int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3616{
3617 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003618 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003619 struct cgroup_iter it;
3620 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003621
Balbir Singh846c7bb2007-10-18 23:39:44 -07003622 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003623 * Validate dentry by checking the superblock operations,
3624 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625 */
Li Zefan33d283b2008-11-19 15:36:48 -08003626 if (dentry->d_sb->s_op != &cgroup_ops ||
3627 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003628 goto err;
3629
3630 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003631 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003632
Paul Menagebd89aab2007-10-18 23:40:44 -07003633 cgroup_iter_start(cgrp, &it);
3634 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003635 switch (tsk->state) {
3636 case TASK_RUNNING:
3637 stats->nr_running++;
3638 break;
3639 case TASK_INTERRUPTIBLE:
3640 stats->nr_sleeping++;
3641 break;
3642 case TASK_UNINTERRUPTIBLE:
3643 stats->nr_uninterruptible++;
3644 break;
3645 case TASK_STOPPED:
3646 stats->nr_stopped++;
3647 break;
3648 default:
3649 if (delayacct_is_task_waiting_on_io(tsk))
3650 stats->nr_io_wait++;
3651 break;
3652 }
3653 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003654 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003655
Balbir Singh846c7bb2007-10-18 23:39:44 -07003656err:
3657 return ret;
3658}
3659
Paul Menage8f3ff202009-09-23 15:56:25 -07003660
Paul Menagecc31edc2008-10-18 20:28:04 -07003661/*
Ben Blum102a7752009-09-23 15:56:26 -07003662 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003663 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003664 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003665 */
3666
Ben Blum102a7752009-09-23 15:56:26 -07003667static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003668{
3669 /*
3670 * Initially we receive a position value that corresponds to
3671 * one more than the last pid shown (or 0 on the first call or
3672 * after a seek to the start). Use a binary-search to find the
3673 * next pid to display, if any
3674 */
Ben Blum102a7752009-09-23 15:56:26 -07003675 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 int index = 0, pid = *pos;
3677 int *iter;
3678
Ben Blum102a7752009-09-23 15:56:26 -07003679 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003680 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003681 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003682
Paul Menagecc31edc2008-10-18 20:28:04 -07003683 while (index < end) {
3684 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003685 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003686 index = mid;
3687 break;
Ben Blum102a7752009-09-23 15:56:26 -07003688 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 index = mid + 1;
3690 else
3691 end = mid;
3692 }
3693 }
3694 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003695 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 return NULL;
3697 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003698 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003699 *pos = *iter;
3700 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003701}
3702
Ben Blum102a7752009-09-23 15:56:26 -07003703static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003704{
Ben Blum102a7752009-09-23 15:56:26 -07003705 struct cgroup_pidlist *l = s->private;
3706 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003707}
3708
Ben Blum102a7752009-09-23 15:56:26 -07003709static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003710{
Ben Blum102a7752009-09-23 15:56:26 -07003711 struct cgroup_pidlist *l = s->private;
3712 pid_t *p = v;
3713 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003714 /*
3715 * Advance to the next pid in the array. If this goes off the
3716 * end, we're done
3717 */
3718 p++;
3719 if (p >= end) {
3720 return NULL;
3721 } else {
3722 *pos = *p;
3723 return p;
3724 }
3725}
3726
Ben Blum102a7752009-09-23 15:56:26 -07003727static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003728{
3729 return seq_printf(s, "%d\n", *(int *)v);
3730}
3731
Ben Blum102a7752009-09-23 15:56:26 -07003732/*
3733 * seq_operations functions for iterating on pidlists through seq_file -
3734 * independent of whether it's tasks or procs
3735 */
3736static const struct seq_operations cgroup_pidlist_seq_operations = {
3737 .start = cgroup_pidlist_start,
3738 .stop = cgroup_pidlist_stop,
3739 .next = cgroup_pidlist_next,
3740 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003741};
3742
Ben Blum102a7752009-09-23 15:56:26 -07003743static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003744{
Ben Blum72a8cb32009-09-23 15:56:27 -07003745 /*
3746 * the case where we're the last user of this particular pidlist will
3747 * have us remove it from the cgroup's list, which entails taking the
3748 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3749 * pidlist_mutex, we have to take pidlist_mutex first.
3750 */
3751 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003752 down_write(&l->mutex);
3753 BUG_ON(!l->use_count);
3754 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003755 /* we're the last user if refcount is 0; remove and free */
3756 list_del(&l->links);
3757 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003758 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003759 put_pid_ns(l->key.ns);
3760 up_write(&l->mutex);
3761 kfree(l);
3762 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003763 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003764 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003765 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003766}
3767
Ben Blum102a7752009-09-23 15:56:26 -07003768static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003769{
Ben Blum102a7752009-09-23 15:56:26 -07003770 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003771 if (!(file->f_mode & FMODE_READ))
3772 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003773 /*
3774 * the seq_file will only be initialized if the file was opened for
3775 * reading; hence we check if it's not null only in that case.
3776 */
3777 l = ((struct seq_file *)file->private_data)->private;
3778 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003779 return seq_release(inode, file);
3780}
3781
Ben Blum102a7752009-09-23 15:56:26 -07003782static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003783 .read = seq_read,
3784 .llseek = seq_lseek,
3785 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003786 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003787};
3788
3789/*
Ben Blum102a7752009-09-23 15:56:26 -07003790 * The following functions handle opens on a file that displays a pidlist
3791 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3792 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003793 */
Ben Blum102a7752009-09-23 15:56:26 -07003794/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003795static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003796{
3797 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003798 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003799 int retval;
3800
3801 /* Nothing to do for write-only files */
3802 if (!(file->f_mode & FMODE_READ))
3803 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003804
Ben Blum102a7752009-09-23 15:56:26 -07003805 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003806 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003807 if (retval)
3808 return retval;
3809 /* configure file information */
3810 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003811
Ben Blum102a7752009-09-23 15:56:26 -07003812 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003813 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003814 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003815 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003816 }
Ben Blum102a7752009-09-23 15:56:26 -07003817 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003818 return 0;
3819}
Ben Blum102a7752009-09-23 15:56:26 -07003820static int cgroup_tasks_open(struct inode *unused, struct file *file)
3821{
Ben Blum72a8cb32009-09-23 15:56:27 -07003822 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003823}
3824static int cgroup_procs_open(struct inode *unused, struct file *file)
3825{
Ben Blum72a8cb32009-09-23 15:56:27 -07003826 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003827}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003828
Paul Menagebd89aab2007-10-18 23:40:44 -07003829static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003830 struct cftype *cft)
3831{
Paul Menagebd89aab2007-10-18 23:40:44 -07003832 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003833}
3834
Paul Menage6379c102008-07-25 01:47:01 -07003835static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3836 struct cftype *cft,
3837 u64 val)
3838{
3839 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3840 if (val)
3841 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3842 else
3843 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3844 return 0;
3845}
3846
Paul Menagebbcb81d2007-10-18 23:39:32 -07003847/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003848 * When dput() is called asynchronously, if umount has been done and
3849 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3850 * there's a small window that vfs will see the root dentry with non-zero
3851 * refcnt and trigger BUG().
3852 *
3853 * That's why we hold a reference before dput() and drop it right after.
3854 */
3855static void cgroup_dput(struct cgroup *cgrp)
3856{
3857 struct super_block *sb = cgrp->root->sb;
3858
3859 atomic_inc(&sb->s_active);
3860 dput(cgrp->dentry);
3861 deactivate_super(sb);
3862}
3863
3864/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003865 * Unregister event and free resources.
3866 *
3867 * Gets called from workqueue.
3868 */
3869static void cgroup_event_remove(struct work_struct *work)
3870{
3871 struct cgroup_event *event = container_of(work, struct cgroup_event,
3872 remove);
3873 struct cgroup *cgrp = event->cgrp;
3874
Li Zefan810cbee2013-02-18 18:56:14 +08003875 remove_wait_queue(event->wqh, &event->wait);
3876
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003877 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3878
Li Zefan810cbee2013-02-18 18:56:14 +08003879 /* Notify userspace the event is going away. */
3880 eventfd_signal(event->eventfd, 1);
3881
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003882 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003883 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003884 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003885}
3886
3887/*
3888 * Gets called on POLLHUP on eventfd when user closes it.
3889 *
3890 * Called with wqh->lock held and interrupts disabled.
3891 */
3892static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3893 int sync, void *key)
3894{
3895 struct cgroup_event *event = container_of(wait,
3896 struct cgroup_event, wait);
3897 struct cgroup *cgrp = event->cgrp;
3898 unsigned long flags = (unsigned long)key;
3899
3900 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003901 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003902 * If the event has been detached at cgroup removal, we
3903 * can simply return knowing the other side will cleanup
3904 * for us.
3905 *
3906 * We can't race against event freeing since the other
3907 * side will require wqh->lock via remove_wait_queue(),
3908 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003909 */
Li Zefan810cbee2013-02-18 18:56:14 +08003910 spin_lock(&cgrp->event_list_lock);
3911 if (!list_empty(&event->list)) {
3912 list_del_init(&event->list);
3913 /*
3914 * We are in atomic context, but cgroup_event_remove()
3915 * may sleep, so we have to call it in workqueue.
3916 */
3917 schedule_work(&event->remove);
3918 }
3919 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003920 }
3921
3922 return 0;
3923}
3924
3925static void cgroup_event_ptable_queue_proc(struct file *file,
3926 wait_queue_head_t *wqh, poll_table *pt)
3927{
3928 struct cgroup_event *event = container_of(pt,
3929 struct cgroup_event, pt);
3930
3931 event->wqh = wqh;
3932 add_wait_queue(wqh, &event->wait);
3933}
3934
3935/*
3936 * Parse input and register new cgroup event handler.
3937 *
3938 * Input must be in format '<event_fd> <control_fd> <args>'.
3939 * Interpretation of args is defined by control file implementation.
3940 */
3941static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3942 const char *buffer)
3943{
3944 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003945 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003946 unsigned int efd, cfd;
3947 struct file *efile = NULL;
3948 struct file *cfile = NULL;
3949 char *endp;
3950 int ret;
3951
3952 efd = simple_strtoul(buffer, &endp, 10);
3953 if (*endp != ' ')
3954 return -EINVAL;
3955 buffer = endp + 1;
3956
3957 cfd = simple_strtoul(buffer, &endp, 10);
3958 if ((*endp != ' ') && (*endp != '\0'))
3959 return -EINVAL;
3960 buffer = endp + 1;
3961
3962 event = kzalloc(sizeof(*event), GFP_KERNEL);
3963 if (!event)
3964 return -ENOMEM;
3965 event->cgrp = cgrp;
3966 INIT_LIST_HEAD(&event->list);
3967 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3968 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3969 INIT_WORK(&event->remove, cgroup_event_remove);
3970
3971 efile = eventfd_fget(efd);
3972 if (IS_ERR(efile)) {
3973 ret = PTR_ERR(efile);
3974 goto fail;
3975 }
3976
3977 event->eventfd = eventfd_ctx_fileget(efile);
3978 if (IS_ERR(event->eventfd)) {
3979 ret = PTR_ERR(event->eventfd);
3980 goto fail;
3981 }
3982
3983 cfile = fget(cfd);
3984 if (!cfile) {
3985 ret = -EBADF;
3986 goto fail;
3987 }
3988
3989 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003990 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003991 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003992 if (ret < 0)
3993 goto fail;
3994
3995 event->cft = __file_cft(cfile);
3996 if (IS_ERR(event->cft)) {
3997 ret = PTR_ERR(event->cft);
3998 goto fail;
3999 }
4000
Li Zefanf1690072013-02-18 14:13:35 +08004001 /*
4002 * The file to be monitored must be in the same cgroup as
4003 * cgroup.event_control is.
4004 */
4005 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4006 if (cgrp_cfile != cgrp) {
4007 ret = -EINVAL;
4008 goto fail;
4009 }
4010
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004011 if (!event->cft->register_event || !event->cft->unregister_event) {
4012 ret = -EINVAL;
4013 goto fail;
4014 }
4015
4016 ret = event->cft->register_event(cgrp, event->cft,
4017 event->eventfd, buffer);
4018 if (ret)
4019 goto fail;
4020
Li Zefan7ef70e42013-04-26 11:58:03 -07004021 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004022
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004023 /*
4024 * Events should be removed after rmdir of cgroup directory, but before
4025 * destroying subsystem state objects. Let's take reference to cgroup
4026 * directory dentry to do that.
4027 */
4028 dget(cgrp->dentry);
4029
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004030 spin_lock(&cgrp->event_list_lock);
4031 list_add(&event->list, &cgrp->event_list);
4032 spin_unlock(&cgrp->event_list_lock);
4033
4034 fput(cfile);
4035 fput(efile);
4036
4037 return 0;
4038
4039fail:
4040 if (cfile)
4041 fput(cfile);
4042
4043 if (event && event->eventfd && !IS_ERR(event->eventfd))
4044 eventfd_ctx_put(event->eventfd);
4045
4046 if (!IS_ERR_OR_NULL(efile))
4047 fput(efile);
4048
4049 kfree(event);
4050
4051 return ret;
4052}
4053
Daniel Lezcano97978e62010-10-27 15:33:35 -07004054static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4055 struct cftype *cft)
4056{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004057 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004058}
4059
4060static int cgroup_clone_children_write(struct cgroup *cgrp,
4061 struct cftype *cft,
4062 u64 val)
4063{
4064 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004065 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004066 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004067 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004068 return 0;
4069}
4070
Tejun Heod5c56ce2013-06-03 19:14:34 -07004071static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004072 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004073 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004074 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004075 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004076 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004077 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004078 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004079 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004080 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004081 .write_string = cgroup_write_event_control,
4082 .mode = S_IWUGO,
4083 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004084 {
4085 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004086 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004087 .read_u64 = cgroup_clone_children_read,
4088 .write_u64 = cgroup_clone_children_write,
4089 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004090 {
Tejun Heo873fe092013-04-14 20:15:26 -07004091 .name = "cgroup.sane_behavior",
4092 .flags = CFTYPE_ONLY_ON_ROOT,
4093 .read_seq_string = cgroup_sane_behavior_show,
4094 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004095
4096 /*
4097 * Historical crazy stuff. These don't have "cgroup." prefix and
4098 * don't exist if sane_behavior. If you're depending on these, be
4099 * prepared to be burned.
4100 */
4101 {
4102 .name = "tasks",
4103 .flags = CFTYPE_INSANE, /* use "procs" instead */
4104 .open = cgroup_tasks_open,
4105 .write_u64 = cgroup_tasks_write,
4106 .release = cgroup_pidlist_release,
4107 .mode = S_IRUGO | S_IWUSR,
4108 },
4109 {
4110 .name = "notify_on_release",
4111 .flags = CFTYPE_INSANE,
4112 .read_u64 = cgroup_read_notify_on_release,
4113 .write_u64 = cgroup_write_notify_on_release,
4114 },
Tejun Heo873fe092013-04-14 20:15:26 -07004115 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004116 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004117 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004118 .read_seq_string = cgroup_release_agent_show,
4119 .write_string = cgroup_release_agent_write,
4120 .max_write_len = PATH_MAX,
4121 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004122 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004123};
4124
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004125/**
4126 * cgroup_populate_dir - selectively creation of files in a directory
4127 * @cgrp: target cgroup
4128 * @base_files: true if the base files should be added
4129 * @subsys_mask: mask of the subsystem ids whose files should be added
4130 */
4131static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4132 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004133{
4134 int err;
4135 struct cgroup_subsys *ss;
4136
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004137 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004138 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004139 if (err < 0)
4140 return err;
4141 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004142
Tejun Heo8e3f6542012-04-01 12:09:55 -07004143 /* process cftsets of each subsystem */
Paul Menagebd89aab2007-10-18 23:40:44 -07004144 for_each_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004145 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004146 if (!test_bit(ss->subsys_id, &subsys_mask))
4147 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004148
Tejun Heodb0416b2012-04-01 12:09:55 -07004149 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004150 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004151 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004152
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004153 /* This cgroup is ready now */
4154 for_each_subsys(cgrp->root, ss) {
4155 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4156 /*
4157 * Update id->css pointer and make this css visible from
4158 * CSS ID functions. This pointer will be dereferened
4159 * from RCU-read-side without locks.
4160 */
4161 if (css->id)
4162 rcu_assign_pointer(css->id->css, css);
4163 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004164
4165 return 0;
4166}
4167
Tejun Heo48ddbe12012-04-01 12:09:56 -07004168static void css_dput_fn(struct work_struct *work)
4169{
4170 struct cgroup_subsys_state *css =
4171 container_of(work, struct cgroup_subsys_state, dput_work);
4172
Li Zefan1c8158e2013-06-18 18:41:10 +08004173 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004174}
4175
Tejun Heod3daf282013-06-13 19:39:16 -07004176static void css_release(struct percpu_ref *ref)
4177{
4178 struct cgroup_subsys_state *css =
4179 container_of(ref, struct cgroup_subsys_state, refcnt);
4180
4181 schedule_work(&css->dput_work);
4182}
4183
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184static void init_cgroup_css(struct cgroup_subsys_state *css,
4185 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004186 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004187{
Paul Menagebd89aab2007-10-18 23:40:44 -07004188 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004189 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004190 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004191 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004192 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004193 BUG_ON(cgrp->subsys[ss->subsys_id]);
4194 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004195
4196 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004197 * css holds an extra ref to @cgrp->dentry which is put on the last
4198 * css_put(). dput() requires process context, which css_put() may
4199 * be called without. @css->dput_work will be used to invoke
4200 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004201 */
4202 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004203}
4204
Tejun Heob1929db2012-11-19 08:13:38 -08004205/* invoke ->post_create() on a new CSS and mark it online if successful */
4206static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004207{
Tejun Heob1929db2012-11-19 08:13:38 -08004208 int ret = 0;
4209
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210 lockdep_assert_held(&cgroup_mutex);
4211
Tejun Heo92fb9742012-11-19 08:13:38 -08004212 if (ss->css_online)
4213 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004214 if (!ret)
4215 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4216 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004217}
4218
4219/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4220static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4221 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4222{
4223 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4224
4225 lockdep_assert_held(&cgroup_mutex);
4226
4227 if (!(css->flags & CSS_ONLINE))
4228 return;
4229
Li Zefand7eeac12013-03-12 15:35:59 -07004230 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004231 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004232
4233 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4234}
4235
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236/*
Li Zefana043e3b2008-02-23 15:24:09 -08004237 * cgroup_create - create a cgroup
4238 * @parent: cgroup that will be parent of the new cgroup
4239 * @dentry: dentry of the new cgroup
4240 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241 *
Li Zefana043e3b2008-02-23 15:24:09 -08004242 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004243 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004245 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004246{
Paul Menagebd89aab2007-10-18 23:40:44 -07004247 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004248 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004249 struct cgroupfs_root *root = parent->root;
4250 int err = 0;
4251 struct cgroup_subsys *ss;
4252 struct super_block *sb = root->sb;
4253
Tejun Heo0a950f62012-11-19 09:02:12 -08004254 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004255 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4256 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004257 return -ENOMEM;
4258
Li Zefan65dff752013-03-01 15:01:56 +08004259 name = cgroup_alloc_name(dentry);
4260 if (!name)
4261 goto err_free_cgrp;
4262 rcu_assign_pointer(cgrp->name, name);
4263
Tejun Heo0a950f62012-11-19 09:02:12 -08004264 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4265 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004266 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004267
Tejun Heo976c06b2012-11-05 09:16:59 -08004268 /*
4269 * Only live parents can have children. Note that the liveliness
4270 * check isn't strictly necessary because cgroup_mkdir() and
4271 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4272 * anyway so that locking is contained inside cgroup proper and we
4273 * don't get nasty surprises if we ever grow another caller.
4274 */
4275 if (!cgroup_lock_live_group(parent)) {
4276 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004277 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004278 }
4279
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 /* Grab a reference on the superblock so the hierarchy doesn't
4281 * get deleted on unmount if there are child cgroups. This
4282 * can be done outside cgroup_mutex, since the sb can't
4283 * disappear while someone has an open control file on the
4284 * fs */
4285 atomic_inc(&sb->s_active);
4286
Paul Menagecc31edc2008-10-18 20:28:04 -07004287 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004288
Li Zefanfe1c06c2013-01-24 14:30:22 +08004289 dentry->d_fsdata = cgrp;
4290 cgrp->dentry = dentry;
4291
Paul Menagebd89aab2007-10-18 23:40:44 -07004292 cgrp->parent = parent;
4293 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294
Li Zefanb6abdb02008-03-04 14:28:19 -08004295 if (notify_on_release(parent))
4296 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4297
Tejun Heo2260e7f2012-11-19 08:13:38 -08004298 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4299 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004300
Paul Menageddbcc7e2007-10-18 23:39:30 -07004301 for_each_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004302 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004303
Tejun Heo92fb9742012-11-19 08:13:38 -08004304 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305 if (IS_ERR(css)) {
4306 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004307 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004308 }
Tejun Heod3daf282013-06-13 19:39:16 -07004309
4310 err = percpu_ref_init(&css->refcnt, css_release);
4311 if (err)
4312 goto err_free_all;
4313
Paul Menagebd89aab2007-10-18 23:40:44 -07004314 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004315
Li Zefan4528fd02010-02-02 13:44:10 -08004316 if (ss->use_id) {
4317 err = alloc_css_id(ss, parent, cgrp);
4318 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004319 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004320 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321 }
4322
Tejun Heo4e139af2012-11-19 08:13:36 -08004323 /*
4324 * Create directory. cgroup_create_file() returns with the new
4325 * directory locked on success so that it can be populated without
4326 * dropping cgroup_mutex.
4327 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004328 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004329 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004330 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004331 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332
Tejun Heo00356bd2013-06-18 11:14:22 -07004333 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004334
Tejun Heo4e139af2012-11-19 08:13:36 -08004335 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004336 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4337 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004338
Tejun Heob1929db2012-11-19 08:13:38 -08004339 /* each css holds a ref to the cgroup's dentry */
4340 for_each_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004341 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004342
Li Zefan415cf072013-04-08 14:35:02 +08004343 /* hold a ref to the parent's dentry */
4344 dget(parent->dentry);
4345
Tejun Heob1929db2012-11-19 08:13:38 -08004346 /* creation succeeded, notify subsystems */
4347 for_each_subsys(root, ss) {
4348 err = online_css(ss, cgrp);
4349 if (err)
4350 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004351
4352 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4353 parent->parent) {
4354 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",
4355 current->comm, current->pid, ss->name);
4356 if (!strcmp(ss->name, "memory"))
4357 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4358 ss->warned_broken_hierarchy = true;
4359 }
Tejun Heoa8638032012-11-09 09:12:29 -08004360 }
4361
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04004362 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004363 if (err)
4364 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004365
4366 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004367 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368
4369 return 0;
4370
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004371err_free_all:
Paul Menageddbcc7e2007-10-18 23:39:30 -07004372 for_each_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004373 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4374
4375 if (css) {
4376 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004377 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004378 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004380 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381 /* Release the reference count that we took on the superblock */
4382 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004383err_free_id:
4384 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004385err_free_name:
4386 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004387err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004388 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004390
4391err_destroy:
4392 cgroup_destroy_locked(cgrp);
4393 mutex_unlock(&cgroup_mutex);
4394 mutex_unlock(&dentry->d_inode->i_mutex);
4395 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396}
4397
Al Viro18bb1db2011-07-26 01:41:39 -04004398static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004399{
4400 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4401
4402 /* the vfs holds inode->i_mutex already */
4403 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4404}
4405
Tejun Heod3daf282013-06-13 19:39:16 -07004406static void cgroup_css_killed(struct cgroup *cgrp)
4407{
4408 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4409 return;
4410
4411 /* percpu ref's of all css's are killed, kick off the next step */
4412 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4413 schedule_work(&cgrp->destroy_work);
4414}
4415
4416static void css_ref_killed_fn(struct percpu_ref *ref)
4417{
4418 struct cgroup_subsys_state *css =
4419 container_of(ref, struct cgroup_subsys_state, refcnt);
4420
4421 cgroup_css_killed(css->cgroup);
4422}
4423
4424/**
4425 * cgroup_destroy_locked - the first stage of cgroup destruction
4426 * @cgrp: cgroup to be destroyed
4427 *
4428 * css's make use of percpu refcnts whose killing latency shouldn't be
4429 * exposed to userland and are RCU protected. Also, cgroup core needs to
4430 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4431 * invoked. To satisfy all the requirements, destruction is implemented in
4432 * the following two steps.
4433 *
4434 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4435 * userland visible parts and start killing the percpu refcnts of
4436 * css's. Set up so that the next stage will be kicked off once all
4437 * the percpu refcnts are confirmed to be killed.
4438 *
4439 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4440 * rest of destruction. Once all cgroup references are gone, the
4441 * cgroup is RCU-freed.
4442 *
4443 * This function implements s1. After this step, @cgrp is gone as far as
4444 * the userland is concerned and a new cgroup with the same name may be
4445 * created. As cgroup doesn't care about the names internally, this
4446 * doesn't cause any problem.
4447 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004448static int cgroup_destroy_locked(struct cgroup *cgrp)
4449 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004450{
Tejun Heo42809dd2012-11-19 08:13:37 -08004451 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004452 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004453 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004454 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004455
Tejun Heo42809dd2012-11-19 08:13:37 -08004456 lockdep_assert_held(&d->d_inode->i_mutex);
4457 lockdep_assert_held(&cgroup_mutex);
4458
Tejun Heoddd69142013-06-12 21:04:54 -07004459 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004460 * css_set_lock synchronizes access to ->cset_links and prevents
4461 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004462 */
4463 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004464 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004465 read_unlock(&css_set_lock);
4466 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004467 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004468
Tejun Heo1a90dd52012-11-05 09:16:59 -08004469 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004470 * Block new css_tryget() by killing css refcnts. cgroup core
4471 * guarantees that, by the time ->css_offline() is invoked, no new
4472 * css reference will be given out via css_tryget(). We can't
4473 * simply call percpu_ref_kill() and proceed to offlining css's
4474 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4475 * as killed on all CPUs on return.
4476 *
4477 * Use percpu_ref_kill_and_confirm() to get notifications as each
4478 * css is confirmed to be seen as killed on all CPUs. The
4479 * notification callback keeps track of the number of css's to be
4480 * killed and schedules cgroup_offline_fn() to perform the rest of
4481 * destruction once the percpu refs of all css's are confirmed to
4482 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004483 */
Tejun Heod3daf282013-06-13 19:39:16 -07004484 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heoed9577932012-11-05 09:16:58 -08004485 for_each_subsys(cgrp->root, ss) {
4486 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4487
Tejun Heod3daf282013-06-13 19:39:16 -07004488 /*
4489 * Killing would put the base ref, but we need to keep it
4490 * alive until after ->css_offline.
4491 */
4492 percpu_ref_get(&css->refcnt);
4493
4494 atomic_inc(&cgrp->css_kill_cnt);
4495 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004496 }
Tejun Heod3daf282013-06-13 19:39:16 -07004497 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004498
4499 /*
4500 * Mark @cgrp dead. This prevents further task migration and child
4501 * creation by disabling cgroup_lock_live_group(). Note that
4502 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4503 * resume iteration after dropping RCU read lock. See
4504 * cgroup_next_sibling() for details.
4505 */
Tejun Heo54766d42013-06-12 21:04:53 -07004506 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004507
Tejun Heo455050d2013-06-13 19:27:41 -07004508 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4509 raw_spin_lock(&release_list_lock);
4510 if (!list_empty(&cgrp->release_list))
4511 list_del_init(&cgrp->release_list);
4512 raw_spin_unlock(&release_list_lock);
4513
4514 /*
4515 * Remove @cgrp directory. The removal puts the base ref but we
4516 * aren't quite done with @cgrp yet, so hold onto it.
4517 */
4518 dget(d);
4519 cgroup_d_remove_dir(d);
4520
4521 /*
4522 * Unregister events and notify userspace.
4523 * Notify userspace about cgroup removing only after rmdir of cgroup
4524 * directory to avoid race between userspace and kernelspace.
4525 */
4526 spin_lock(&cgrp->event_list_lock);
4527 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4528 list_del_init(&event->list);
4529 schedule_work(&event->remove);
4530 }
4531 spin_unlock(&cgrp->event_list_lock);
4532
Tejun Heoea15f8c2013-06-13 19:27:42 -07004533 return 0;
4534};
4535
Tejun Heod3daf282013-06-13 19:39:16 -07004536/**
4537 * cgroup_offline_fn - the second step of cgroup destruction
4538 * @work: cgroup->destroy_free_work
4539 *
4540 * This function is invoked from a work item for a cgroup which is being
4541 * destroyed after the percpu refcnts of all css's are guaranteed to be
4542 * seen as killed on all CPUs, and performs the rest of destruction. This
4543 * is the second step of destruction described in the comment above
4544 * cgroup_destroy_locked().
4545 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004546static void cgroup_offline_fn(struct work_struct *work)
4547{
4548 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4549 struct cgroup *parent = cgrp->parent;
4550 struct dentry *d = cgrp->dentry;
4551 struct cgroup_subsys *ss;
4552
4553 mutex_lock(&cgroup_mutex);
4554
Tejun Heod3daf282013-06-13 19:39:16 -07004555 /*
4556 * css_tryget() is guaranteed to fail now. Tell subsystems to
4557 * initate destruction.
4558 */
Tejun Heo1a90dd52012-11-05 09:16:59 -08004559 for_each_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004560 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004561
4562 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004563 * Put the css refs from cgroup_destroy_locked(). Each css holds
4564 * an extra reference to the cgroup's dentry and cgroup removal
4565 * proceeds regardless of css refs. On the last put of each css,
4566 * whenever that may be, the extra dentry ref is put so that dentry
4567 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004568 */
Tejun Heoe9316082012-11-05 09:16:58 -08004569 for_each_subsys(cgrp->root, ss)
4570 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004571
Paul Menage999cd8a2009-01-07 18:08:36 -08004572 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004573 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004574
Paul Menageddbcc7e2007-10-18 23:39:30 -07004575 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004576
Paul Menagebd89aab2007-10-18 23:40:44 -07004577 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004578 check_for_release(parent);
4579
Tejun Heoea15f8c2013-06-13 19:27:42 -07004580 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581}
4582
Tejun Heo42809dd2012-11-19 08:13:37 -08004583static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4584{
4585 int ret;
4586
4587 mutex_lock(&cgroup_mutex);
4588 ret = cgroup_destroy_locked(dentry->d_fsdata);
4589 mutex_unlock(&cgroup_mutex);
4590
4591 return ret;
4592}
4593
Tejun Heo8e3f6542012-04-01 12:09:55 -07004594static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4595{
4596 INIT_LIST_HEAD(&ss->cftsets);
4597
4598 /*
4599 * base_cftset is embedded in subsys itself, no need to worry about
4600 * deregistration.
4601 */
4602 if (ss->base_cftypes) {
4603 ss->base_cftset.cfts = ss->base_cftypes;
4604 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4605 }
4606}
4607
Li Zefan06a11922008-04-29 01:00:07 -07004608static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004609{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004610 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004611
4612 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004613
Tejun Heo648bb562012-11-19 08:13:36 -08004614 mutex_lock(&cgroup_mutex);
4615
Tejun Heo8e3f6542012-04-01 12:09:55 -07004616 /* init base cftset */
4617 cgroup_init_cftsets(ss);
4618
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004620 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4621 ss->root = &cgroup_dummy_root;
4622 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004623 /* We don't handle early failures gracefully */
4624 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004625 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004626
Li Zefane8d55fd2008-04-29 01:00:13 -07004627 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004628 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004629 * newly registered, all tasks and hence the
4630 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004631 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004632
4633 need_forkexit_callback |= ss->fork || ss->exit;
4634
Li Zefane8d55fd2008-04-29 01:00:13 -07004635 /* At system boot, before all subsystems have been
4636 * registered, no tasks have been forked, so we don't
4637 * need to invoke fork callbacks here. */
4638 BUG_ON(!list_empty(&init_task.tasks));
4639
Tejun Heo9871bf92013-06-24 15:21:47 -07004640 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004641
Tejun Heo648bb562012-11-19 08:13:36 -08004642 mutex_unlock(&cgroup_mutex);
4643
Ben Blume6a11052010-03-10 15:22:09 -08004644 /* this function shouldn't be used with modular subsystems, since they
4645 * need to register a subsys_id, among other things */
4646 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004647}
4648
4649/**
Ben Blume6a11052010-03-10 15:22:09 -08004650 * cgroup_load_subsys: load and register a modular subsystem at runtime
4651 * @ss: the subsystem to load
4652 *
4653 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004654 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004655 * up for use. If the subsystem is built-in anyway, work is delegated to the
4656 * simpler cgroup_init_subsys.
4657 */
4658int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4659{
Ben Blume6a11052010-03-10 15:22:09 -08004660 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004661 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004662 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004663 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004664 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004665
4666 /* check name and function validity */
4667 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004668 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004669 return -EINVAL;
4670
4671 /*
4672 * we don't support callbacks in modular subsystems. this check is
4673 * before the ss->module check for consistency; a subsystem that could
4674 * be a module should still have no callbacks even if the user isn't
4675 * compiling it as one.
4676 */
4677 if (ss->fork || ss->exit)
4678 return -EINVAL;
4679
4680 /*
4681 * an optionally modular subsystem is built-in: we want to do nothing,
4682 * since cgroup_init_subsys will have already taken care of it.
4683 */
4684 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004685 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004686 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004687 return 0;
4688 }
4689
Tejun Heo8e3f6542012-04-01 12:09:55 -07004690 /* init base cftset */
4691 cgroup_init_cftsets(ss);
4692
Ben Blume6a11052010-03-10 15:22:09 -08004693 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004694 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004695
4696 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004697 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004698 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004699 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004700 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004701 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004702 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004703 /* failure case - need to deassign the cgroup_subsys[] slot. */
4704 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004705 mutex_unlock(&cgroup_mutex);
4706 return PTR_ERR(css);
4707 }
4708
Tejun Heo9871bf92013-06-24 15:21:47 -07004709 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4710 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004711
4712 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004713 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004714 /* init_idr must be after init_cgroup_css because it sets css->id. */
4715 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004716 ret = cgroup_init_idr(ss, css);
4717 if (ret)
4718 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004719 }
4720
4721 /*
4722 * Now we need to entangle the css into the existing css_sets. unlike
4723 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4724 * will need a new pointer to it; done by iterating the css_set_table.
4725 * furthermore, modifying the existing css_sets will corrupt the hash
4726 * table state, so each changed css_set will need its hash recomputed.
4727 * this is all done under the css_set_lock.
4728 */
4729 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004730 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004731 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004732 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004733 continue;
4734 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004735 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004736 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004737 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004738 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004739 key = css_set_hash(cset->subsys);
4740 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004741 }
4742 write_unlock(&css_set_lock);
4743
Tejun Heo9871bf92013-06-24 15:21:47 -07004744 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004745 if (ret)
4746 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004747
Ben Blume6a11052010-03-10 15:22:09 -08004748 /* success! */
4749 mutex_unlock(&cgroup_mutex);
4750 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004751
4752err_unload:
4753 mutex_unlock(&cgroup_mutex);
4754 /* @ss can't be mounted here as try_module_get() would fail */
4755 cgroup_unload_subsys(ss);
4756 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004757}
4758EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4759
4760/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004761 * cgroup_unload_subsys: unload a modular subsystem
4762 * @ss: the subsystem to unload
4763 *
4764 * This function should be called in a modular subsystem's exitcall. When this
4765 * function is invoked, the refcount on the subsystem's module will be 0, so
4766 * the subsystem will not be attached to any hierarchy.
4767 */
4768void cgroup_unload_subsys(struct cgroup_subsys *ss)
4769{
Tejun Heo69d02062013-06-12 21:04:50 -07004770 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004771
4772 BUG_ON(ss->module == NULL);
4773
4774 /*
4775 * we shouldn't be called if the subsystem is in use, and the use of
4776 * try_module_get in parse_cgroupfs_options should ensure that it
4777 * doesn't start being used while we're killing it off.
4778 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004779 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004780
4781 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004782
Tejun Heo9871bf92013-06-24 15:21:47 -07004783 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004784
Tejun Heoc897ff62013-02-27 17:03:49 -08004785 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004786 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004787
Ben Blumcf5d5942010-03-10 15:22:09 -08004788 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004789 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004790
Tejun Heo9871bf92013-06-24 15:21:47 -07004791 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004792 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004793
4794 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004795 * disentangle the css from all css_sets attached to the dummy
4796 * top. as in loading, we need to pay our respects to the hashtable
4797 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004798 */
4799 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004800 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004801 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004802 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004803
Tejun Heo5abb8852013-06-12 21:04:49 -07004804 hash_del(&cset->hlist);
4805 cset->subsys[ss->subsys_id] = NULL;
4806 key = css_set_hash(cset->subsys);
4807 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004808 }
4809 write_unlock(&css_set_lock);
4810
4811 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004812 * remove subsystem's css from the cgroup_dummy_top and free it -
4813 * need to free before marking as null because ss->css_free needs
4814 * the cgrp->subsys pointer to find their state. note that this
4815 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004816 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004817 ss->css_free(cgroup_dummy_top);
4818 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004819
4820 mutex_unlock(&cgroup_mutex);
4821}
4822EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4823
4824/**
Li Zefana043e3b2008-02-23 15:24:09 -08004825 * cgroup_init_early - cgroup initialization at system boot
4826 *
4827 * Initialize cgroups at system boot, and initialize any
4828 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004829 */
4830int __init cgroup_init_early(void)
4831{
4832 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004833 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004834 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004835 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004836 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004837 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004838 init_cgroup_root(&cgroup_dummy_root);
4839 cgroup_root_count = 1;
Paul Menage817929e2007-10-18 23:39:36 -07004840 init_task.cgroups = &init_css_set;
4841
Tejun Heo69d02062013-06-12 21:04:50 -07004842 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004843 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4844 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004845 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004846
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004847 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004848 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004849
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004850 /* at bootup time, we don't worry about modular subsystems */
4851 if (!ss || ss->module)
4852 continue;
4853
Paul Menageddbcc7e2007-10-18 23:39:30 -07004854 BUG_ON(!ss->name);
4855 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004856 BUG_ON(!ss->css_alloc);
4857 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004858 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004859 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860 ss->name, ss->subsys_id);
4861 BUG();
4862 }
4863
4864 if (ss->early_init)
4865 cgroup_init_subsys(ss);
4866 }
4867 return 0;
4868}
4869
4870/**
Li Zefana043e3b2008-02-23 15:24:09 -08004871 * cgroup_init - cgroup initialization
4872 *
4873 * Register cgroup filesystem and /proc file, and initialize
4874 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004875 */
4876int __init cgroup_init(void)
4877{
4878 int err;
4879 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004880 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004881
4882 err = bdi_init(&cgroup_backing_dev_info);
4883 if (err)
4884 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004885
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004886 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004887 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004888
4889 /* at bootup time, we don't worry about modular subsystems */
4890 if (!ss || ss->module)
4891 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004892 if (!ss->early_init)
4893 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004894 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004895 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004896 }
4897
Li Zefan472b1052008-04-29 01:00:11 -07004898 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004899 key = css_set_hash(init_css_set.subsys);
4900 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004901
4902 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004903 mutex_lock(&cgroup_mutex);
4904 mutex_lock(&cgroup_root_mutex);
4905
Tejun Heo9871bf92013-06-24 15:21:47 -07004906 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root));
Greg KH676db4a2010-08-05 13:53:35 -07004907
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004908 mutex_unlock(&cgroup_root_mutex);
4909 mutex_unlock(&cgroup_mutex);
4910
Greg KH676db4a2010-08-05 13:53:35 -07004911 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4912 if (!cgroup_kobj) {
4913 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004915 }
4916
4917 err = register_filesystem(&cgroup_fs_type);
4918 if (err < 0) {
4919 kobject_put(cgroup_kobj);
4920 goto out;
4921 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004922
Li Zefan46ae2202008-04-29 01:00:08 -07004923 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004924
Paul Menageddbcc7e2007-10-18 23:39:30 -07004925out:
Paul Menagea4243162007-10-18 23:39:35 -07004926 if (err)
4927 bdi_destroy(&cgroup_backing_dev_info);
4928
Paul Menageddbcc7e2007-10-18 23:39:30 -07004929 return err;
4930}
Paul Menageb4f48b62007-10-18 23:39:33 -07004931
Paul Menagea4243162007-10-18 23:39:35 -07004932/*
4933 * proc_cgroup_show()
4934 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4935 * - Used for /proc/<pid>/cgroup.
4936 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4937 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004938 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004939 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4940 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4941 * cgroup to top_cgroup.
4942 */
4943
4944/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004945int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004946{
4947 struct pid *pid;
4948 struct task_struct *tsk;
4949 char *buf;
4950 int retval;
4951 struct cgroupfs_root *root;
4952
4953 retval = -ENOMEM;
4954 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4955 if (!buf)
4956 goto out;
4957
4958 retval = -ESRCH;
4959 pid = m->private;
4960 tsk = get_pid_task(pid, PIDTYPE_PID);
4961 if (!tsk)
4962 goto out_free;
4963
4964 retval = 0;
4965
4966 mutex_lock(&cgroup_mutex);
4967
Li Zefane5f6a862009-01-07 18:07:41 -08004968 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004969 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004970 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004971 int count = 0;
4972
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004973 seq_printf(m, "%d:", root->hierarchy_id);
Paul Menagea4243162007-10-18 23:39:35 -07004974 for_each_subsys(root, ss)
4975 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004976 if (strlen(root->name))
4977 seq_printf(m, "%sname=%s", count ? "," : "",
4978 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004979 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004980 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004981 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004982 if (retval < 0)
4983 goto out_unlock;
4984 seq_puts(m, buf);
4985 seq_putc(m, '\n');
4986 }
4987
4988out_unlock:
4989 mutex_unlock(&cgroup_mutex);
4990 put_task_struct(tsk);
4991out_free:
4992 kfree(buf);
4993out:
4994 return retval;
4995}
4996
Paul Menagea4243162007-10-18 23:39:35 -07004997/* Display information about each subsystem and each hierarchy */
4998static int proc_cgroupstats_show(struct seq_file *m, void *v)
4999{
5000 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005001
Paul Menage8bab8dd2008-04-04 14:29:57 -07005002 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005003 /*
5004 * ideally we don't want subsystems moving around while we do this.
5005 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5006 * subsys/hierarchy state.
5007 */
Paul Menagea4243162007-10-18 23:39:35 -07005008 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005009 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005010 struct cgroup_subsys *ss = cgroup_subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08005011 if (ss == NULL)
5012 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005013 seq_printf(m, "%s\t%d\t%d\t%d\n",
5014 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005015 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07005016 }
5017 mutex_unlock(&cgroup_mutex);
5018 return 0;
5019}
5020
5021static int cgroupstats_open(struct inode *inode, struct file *file)
5022{
Al Viro9dce07f2008-03-29 03:07:28 +00005023 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005024}
5025
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005026static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005027 .open = cgroupstats_open,
5028 .read = seq_read,
5029 .llseek = seq_lseek,
5030 .release = single_release,
5031};
5032
Paul Menageb4f48b62007-10-18 23:39:33 -07005033/**
5034 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005035 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005036 *
5037 * Description: A task inherits its parent's cgroup at fork().
5038 *
5039 * A pointer to the shared css_set was automatically copied in
5040 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005041 * it was not made under the protection of RCU or cgroup_mutex, so
5042 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5043 * have already changed current->cgroups, allowing the previously
5044 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005045 *
5046 * At the point that cgroup_fork() is called, 'current' is the parent
5047 * task, and the passed argument 'child' points to the child task.
5048 */
5049void cgroup_fork(struct task_struct *child)
5050{
Tejun Heo9bb71302012-10-18 17:52:07 -07005051 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005052 child->cgroups = current->cgroups;
5053 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005054 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005055 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005056}
5057
5058/**
Li Zefana043e3b2008-02-23 15:24:09 -08005059 * cgroup_post_fork - called on a new task after adding it to the task list
5060 * @child: the task in question
5061 *
Tejun Heo5edee612012-10-16 15:03:14 -07005062 * Adds the task to the list running through its css_set if necessary and
5063 * call the subsystem fork() callbacks. Has to be after the task is
5064 * visible on the task list in case we race with the first call to
5065 * cgroup_iter_start() - to guarantee that the new task ends up on its
5066 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005067 */
Paul Menage817929e2007-10-18 23:39:36 -07005068void cgroup_post_fork(struct task_struct *child)
5069{
Tejun Heo5edee612012-10-16 15:03:14 -07005070 int i;
5071
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005072 /*
5073 * use_task_css_set_links is set to 1 before we walk the tasklist
5074 * under the tasklist_lock and we read it here after we added the child
5075 * to the tasklist under the tasklist_lock as well. If the child wasn't
5076 * yet in the tasklist when we walked through it from
5077 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5078 * should be visible now due to the paired locking and barriers implied
5079 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5080 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5081 * lock on fork.
5082 */
Paul Menage817929e2007-10-18 23:39:36 -07005083 if (use_task_css_set_links) {
5084 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005085 task_lock(child);
5086 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005087 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005088 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005089 write_unlock(&css_set_lock);
5090 }
Tejun Heo5edee612012-10-16 15:03:14 -07005091
5092 /*
5093 * Call ss->fork(). This must happen after @child is linked on
5094 * css_set; otherwise, @child might change state between ->fork()
5095 * and addition to css_set.
5096 */
5097 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005098 /*
5099 * fork/exit callbacks are supported only for builtin
5100 * subsystems, and the builtin section of the subsys
5101 * array is immutable, so we don't need to lock the
5102 * subsys array here. On the other hand, modular section
5103 * of the array can be freed at module unload, so we
5104 * can't touch that.
5105 */
5106 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005107 struct cgroup_subsys *ss = cgroup_subsys[i];
Tejun Heo5edee612012-10-16 15:03:14 -07005108
Tejun Heo5edee612012-10-16 15:03:14 -07005109 if (ss->fork)
5110 ss->fork(child);
5111 }
5112 }
Paul Menage817929e2007-10-18 23:39:36 -07005113}
Tejun Heo5edee612012-10-16 15:03:14 -07005114
Paul Menage817929e2007-10-18 23:39:36 -07005115/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005116 * cgroup_exit - detach cgroup from exiting task
5117 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005118 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005119 *
5120 * Description: Detach cgroup from @tsk and release it.
5121 *
5122 * Note that cgroups marked notify_on_release force every task in
5123 * them to take the global cgroup_mutex mutex when exiting.
5124 * This could impact scaling on very large systems. Be reluctant to
5125 * use notify_on_release cgroups where very high task exit scaling
5126 * is required on large systems.
5127 *
5128 * the_top_cgroup_hack:
5129 *
5130 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5131 *
5132 * We call cgroup_exit() while the task is still competent to
5133 * handle notify_on_release(), then leave the task attached to the
5134 * root cgroup in each hierarchy for the remainder of its exit.
5135 *
5136 * To do this properly, we would increment the reference count on
5137 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5138 * code we would add a second cgroup function call, to drop that
5139 * reference. This would just create an unnecessary hot spot on
5140 * the top_cgroup reference count, to no avail.
5141 *
5142 * Normally, holding a reference to a cgroup without bumping its
5143 * count is unsafe. The cgroup could go away, or someone could
5144 * attach us to a different cgroup, decrementing the count on
5145 * the first cgroup that we never incremented. But in this case,
5146 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005147 * which wards off any cgroup_attach_task() attempts, or task is a failed
5148 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005149 */
5150void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5151{
Tejun Heo5abb8852013-06-12 21:04:49 -07005152 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005153 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005154
5155 /*
5156 * Unlink from the css_set task list if necessary.
5157 * Optimistically check cg_list before taking
5158 * css_set_lock
5159 */
5160 if (!list_empty(&tsk->cg_list)) {
5161 write_lock(&css_set_lock);
5162 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005163 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005164 write_unlock(&css_set_lock);
5165 }
5166
Paul Menageb4f48b62007-10-18 23:39:33 -07005167 /* Reassign the task to the init_css_set. */
5168 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005169 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005170 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005171
5172 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005173 /*
5174 * fork/exit callbacks are supported only for builtin
5175 * subsystems, see cgroup_post_fork() for details.
5176 */
5177 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005178 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005179
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005180 if (ss->exit) {
5181 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005182 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005183 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005184 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005185 }
5186 }
5187 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005188 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005189
Tejun Heo5abb8852013-06-12 21:04:49 -07005190 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005191}
Paul Menage697f4162007-10-18 23:39:34 -07005192
Paul Menagebd89aab2007-10-18 23:40:44 -07005193static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005194{
Li Zefanf50daa72013-03-01 15:06:07 +08005195 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005196 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005197 /*
5198 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005199 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005200 * it now
5201 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005202 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005203
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005204 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005205 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005206 list_empty(&cgrp->release_list)) {
5207 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005208 need_schedule_work = 1;
5209 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005210 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005211 if (need_schedule_work)
5212 schedule_work(&release_agent_work);
5213 }
5214}
5215
Paul Menage81a6a5c2007-10-18 23:39:38 -07005216/*
5217 * Notify userspace when a cgroup is released, by running the
5218 * configured release agent with the name of the cgroup (path
5219 * relative to the root of cgroup file system) as the argument.
5220 *
5221 * Most likely, this user command will try to rmdir this cgroup.
5222 *
5223 * This races with the possibility that some other task will be
5224 * attached to this cgroup before it is removed, or that some other
5225 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5226 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5227 * unused, and this cgroup will be reprieved from its death sentence,
5228 * to continue to serve a useful existence. Next time it's released,
5229 * we will get notified again, if it still has 'notify_on_release' set.
5230 *
5231 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5232 * means only wait until the task is successfully execve()'d. The
5233 * separate release agent task is forked by call_usermodehelper(),
5234 * then control in this thread returns here, without waiting for the
5235 * release agent task. We don't bother to wait because the caller of
5236 * this routine has no use for the exit status of the release agent
5237 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005238 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005239static void cgroup_release_agent(struct work_struct *work)
5240{
5241 BUG_ON(work != &release_agent_work);
5242 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005243 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005244 while (!list_empty(&release_list)) {
5245 char *argv[3], *envp[3];
5246 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005247 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005248 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005249 struct cgroup,
5250 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005251 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005252 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005253 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005254 if (!pathbuf)
5255 goto continue_free;
5256 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5257 goto continue_free;
5258 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5259 if (!agentbuf)
5260 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005261
5262 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005263 argv[i++] = agentbuf;
5264 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005265 argv[i] = NULL;
5266
5267 i = 0;
5268 /* minimal command environment */
5269 envp[i++] = "HOME=/";
5270 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5271 envp[i] = NULL;
5272
5273 /* Drop the lock while we invoke the usermode helper,
5274 * since the exec could involve hitting disk and hence
5275 * be a slow process */
5276 mutex_unlock(&cgroup_mutex);
5277 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005278 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005279 continue_free:
5280 kfree(pathbuf);
5281 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005282 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005283 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005284 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005285 mutex_unlock(&cgroup_mutex);
5286}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005287
5288static int __init cgroup_disable(char *str)
5289{
5290 int i;
5291 char *token;
5292
5293 while ((token = strsep(&str, ",")) != NULL) {
5294 if (!*token)
5295 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005296 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005297 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07005298
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005299 /*
5300 * cgroup_disable, being at boot time, can't
5301 * know about module subsystems, so we don't
5302 * worry about them.
5303 */
5304 if (!ss || ss->module)
5305 continue;
5306
Paul Menage8bab8dd2008-04-04 14:29:57 -07005307 if (!strcmp(token, ss->name)) {
5308 ss->disabled = 1;
5309 printk(KERN_INFO "Disabling %s control group"
5310 " subsystem\n", ss->name);
5311 break;
5312 }
5313 }
5314 }
5315 return 1;
5316}
5317__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005318
5319/*
5320 * Functons for CSS ID.
5321 */
5322
Tejun Heo54766d42013-06-12 21:04:53 -07005323/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005324unsigned short css_id(struct cgroup_subsys_state *css)
5325{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005326 struct css_id *cssid;
5327
5328 /*
5329 * This css_id() can return correct value when somone has refcnt
5330 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5331 * it's unchanged until freed.
5332 */
Tejun Heod3daf282013-06-13 19:39:16 -07005333 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005334
5335 if (cssid)
5336 return cssid->id;
5337 return 0;
5338}
Ben Blum67523c42010-03-10 15:22:11 -08005339EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005340
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005341/**
5342 * css_is_ancestor - test "root" css is an ancestor of "child"
5343 * @child: the css to be tested.
5344 * @root: the css supporsed to be an ancestor of the child.
5345 *
5346 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005347 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005348 * But, considering usual usage, the csses should be valid objects after test.
5349 * Assuming that the caller will do some action to the child if this returns
5350 * returns true, the caller must take "child";s reference count.
5351 * If "child" is valid object and this returns true, "root" is valid, too.
5352 */
5353
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005354bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005355 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005356{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005357 struct css_id *child_id;
5358 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005359
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005360 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005361 if (!child_id)
5362 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005363 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005364 if (!root_id)
5365 return false;
5366 if (child_id->depth < root_id->depth)
5367 return false;
5368 if (child_id->stack[root_id->depth] != root_id->id)
5369 return false;
5370 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005371}
5372
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005373void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5374{
5375 struct css_id *id = css->id;
5376 /* When this is called before css_id initialization, id can be NULL */
5377 if (!id)
5378 return;
5379
5380 BUG_ON(!ss->use_id);
5381
5382 rcu_assign_pointer(id->css, NULL);
5383 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005384 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005385 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005386 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005387 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005388}
Ben Blum67523c42010-03-10 15:22:11 -08005389EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005390
5391/*
5392 * This is called by init or create(). Then, calls to this function are
5393 * always serialized (By cgroup_mutex() at create()).
5394 */
5395
5396static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5397{
5398 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005399 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005400
5401 BUG_ON(!ss->use_id);
5402
5403 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5404 newid = kzalloc(size, GFP_KERNEL);
5405 if (!newid)
5406 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005407
5408 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005409 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005411 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005412 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005413 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005414
5415 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005416 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005417 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418
Tejun Heod228d9e2013-02-27 17:04:54 -08005419 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005420 newid->depth = depth;
5421 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005422err_out:
5423 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005424 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005425
5426}
5427
Ben Blume6a11052010-03-10 15:22:09 -08005428static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5429 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005430{
5431 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005432
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005433 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005434 idr_init(&ss->idr);
5435
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005436 newid = get_new_cssid(ss, 0);
5437 if (IS_ERR(newid))
5438 return PTR_ERR(newid);
5439
5440 newid->stack[0] = newid->id;
5441 newid->css = rootcss;
5442 rootcss->id = newid;
5443 return 0;
5444}
5445
5446static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5447 struct cgroup *child)
5448{
5449 int subsys_id, i, depth = 0;
5450 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005451 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005452
5453 subsys_id = ss->subsys_id;
5454 parent_css = parent->subsys[subsys_id];
5455 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005456 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005457 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005458
5459 child_id = get_new_cssid(ss, depth);
5460 if (IS_ERR(child_id))
5461 return PTR_ERR(child_id);
5462
5463 for (i = 0; i < depth; i++)
5464 child_id->stack[i] = parent_id->stack[i];
5465 child_id->stack[depth] = child_id->id;
5466 /*
5467 * child_id->css pointer will be set after this cgroup is available
5468 * see cgroup_populate_dir()
5469 */
5470 rcu_assign_pointer(child_css->id, child_id);
5471
5472 return 0;
5473}
5474
5475/**
5476 * css_lookup - lookup css by id
5477 * @ss: cgroup subsys to be looked into.
5478 * @id: the id
5479 *
5480 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5481 * NULL if not. Should be called under rcu_read_lock()
5482 */
5483struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5484{
5485 struct css_id *cssid = NULL;
5486
5487 BUG_ON(!ss->use_id);
5488 cssid = idr_find(&ss->idr, id);
5489
5490 if (unlikely(!cssid))
5491 return NULL;
5492
5493 return rcu_dereference(cssid->css);
5494}
Ben Blum67523c42010-03-10 15:22:11 -08005495EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005496
Stephane Eraniane5d13672011-02-14 11:20:01 +02005497/*
5498 * get corresponding css from file open on cgroupfs directory
5499 */
5500struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5501{
5502 struct cgroup *cgrp;
5503 struct inode *inode;
5504 struct cgroup_subsys_state *css;
5505
Al Viro496ad9a2013-01-23 17:07:38 -05005506 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005507 /* check in cgroup filesystem dir */
5508 if (inode->i_op != &cgroup_dir_inode_operations)
5509 return ERR_PTR(-EBADF);
5510
5511 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5512 return ERR_PTR(-EINVAL);
5513
5514 /* get cgroup */
5515 cgrp = __d_cgrp(f->f_dentry);
5516 css = cgrp->subsys[id];
5517 return css ? css : ERR_PTR(-ENOENT);
5518}
5519
Paul Menagefe693432009-09-23 15:56:20 -07005520#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005521static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005522{
5523 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5524
5525 if (!css)
5526 return ERR_PTR(-ENOMEM);
5527
5528 return css;
5529}
5530
Li Zefan03c78cb2013-06-14 11:17:19 +08005531static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005532{
Li Zefan03c78cb2013-06-14 11:17:19 +08005533 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005534}
5535
Li Zefan03c78cb2013-06-14 11:17:19 +08005536static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005537{
Li Zefan03c78cb2013-06-14 11:17:19 +08005538 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005539}
5540
Li Zefan03c78cb2013-06-14 11:17:19 +08005541static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005542{
5543 return (u64)(unsigned long)current->cgroups;
5544}
5545
Li Zefan03c78cb2013-06-14 11:17:19 +08005546static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5547 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005548{
5549 u64 count;
5550
5551 rcu_read_lock();
5552 count = atomic_read(&current->cgroups->refcount);
5553 rcu_read_unlock();
5554 return count;
5555}
5556
Li Zefan03c78cb2013-06-14 11:17:19 +08005557static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005558 struct cftype *cft,
5559 struct seq_file *seq)
5560{
Tejun Heo69d02062013-06-12 21:04:50 -07005561 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005562 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005563
5564 read_lock(&css_set_lock);
5565 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005566 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005567 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005568 struct cgroup *c = link->cgrp;
5569 const char *name;
5570
5571 if (c->dentry)
5572 name = c->dentry->d_name.name;
5573 else
5574 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005575 seq_printf(seq, "Root %d group %s\n",
5576 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005577 }
5578 rcu_read_unlock();
5579 read_unlock(&css_set_lock);
5580 return 0;
5581}
5582
5583#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005584static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005585 struct cftype *cft,
5586 struct seq_file *seq)
5587{
Tejun Heo69d02062013-06-12 21:04:50 -07005588 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005589
5590 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005591 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005592 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005593 struct task_struct *task;
5594 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005595 seq_printf(seq, "css_set %p\n", cset);
5596 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005597 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5598 seq_puts(seq, " ...\n");
5599 break;
5600 } else {
5601 seq_printf(seq, " task %d\n",
5602 task_pid_vnr(task));
5603 }
5604 }
5605 }
5606 read_unlock(&css_set_lock);
5607 return 0;
5608}
5609
Paul Menagefe693432009-09-23 15:56:20 -07005610static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5611{
5612 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5613}
5614
5615static struct cftype debug_files[] = {
5616 {
Paul Menagefe693432009-09-23 15:56:20 -07005617 .name = "taskcount",
5618 .read_u64 = debug_taskcount_read,
5619 },
5620
5621 {
5622 .name = "current_css_set",
5623 .read_u64 = current_css_set_read,
5624 },
5625
5626 {
5627 .name = "current_css_set_refcount",
5628 .read_u64 = current_css_set_refcount_read,
5629 },
5630
5631 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005632 .name = "current_css_set_cg_links",
5633 .read_seq_string = current_css_set_cg_links_read,
5634 },
5635
5636 {
5637 .name = "cgroup_css_links",
5638 .read_seq_string = cgroup_css_links_read,
5639 },
5640
5641 {
Paul Menagefe693432009-09-23 15:56:20 -07005642 .name = "releasable",
5643 .read_u64 = releasable_read,
5644 },
Paul Menagefe693432009-09-23 15:56:20 -07005645
Tejun Heo4baf6e32012-04-01 12:09:55 -07005646 { } /* terminate */
5647};
Paul Menagefe693432009-09-23 15:56:20 -07005648
5649struct cgroup_subsys debug_subsys = {
5650 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005651 .css_alloc = debug_css_alloc,
5652 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005653 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005654 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005655};
5656#endif /* CONFIG_CGROUP_DEBUG */