blob: 605cb13a1574a41a207a329afdbd6c8538d6f38f [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
84EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for task_subsys_state_check() */
85#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heoea15f8c2013-06-13 19:27:42 -0700218static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800219static int cgroup_destroy_locked(struct cgroup *cgrp);
Gao feng879a3d92012-12-01 00:21:28 +0800220static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
221 struct cftype cfts[], bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222
Paul Menageddbcc7e2007-10-18 23:39:30 -0700223/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700224static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225{
Tejun Heo54766d42013-06-12 21:04:53 -0700226 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227}
228
Li Zefan78574cf2013-04-08 19:00:38 -0700229/**
230 * cgroup_is_descendant - test ancestry
231 * @cgrp: the cgroup to be tested
232 * @ancestor: possible ancestor of @cgrp
233 *
234 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
235 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
236 * and @ancestor are accessible.
237 */
238bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
239{
240 while (cgrp) {
241 if (cgrp == ancestor)
242 return true;
243 cgrp = cgrp->parent;
244 }
245 return false;
246}
247EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248
Adrian Bunke9685a02008-02-07 00:13:46 -0800249static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700250{
251 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700252 (1 << CGRP_RELEASABLE) |
253 (1 << CGRP_NOTIFY_ON_RELEASE);
254 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700255}
256
Adrian Bunke9685a02008-02-07 00:13:46 -0800257static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700258{
Paul Menagebd89aab2007-10-18 23:40:44 -0700259 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260}
261
Tejun Heo5549c492013-06-24 15:21:48 -0700262/* iterate each subsystem attached to a hierarchy */
263#define for_each_root_subsys(root, ss) \
264 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700265
Tejun Heo5549c492013-06-24 15:21:48 -0700266/* iterate across the active hierarchies */
267#define for_each_active_root(root) \
268 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700269
Tejun Heof6ea9372012-04-01 12:09:55 -0700270static inline struct cgroup *__d_cgrp(struct dentry *dentry)
271{
272 return dentry->d_fsdata;
273}
274
Tejun Heo05ef1d72012-04-01 12:09:56 -0700275static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700276{
277 return dentry->d_fsdata;
278}
279
Tejun Heo05ef1d72012-04-01 12:09:56 -0700280static inline struct cftype *__d_cft(struct dentry *dentry)
281{
282 return __d_cfe(dentry)->type;
283}
284
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700285/**
286 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
287 * @cgrp: the cgroup to be checked for liveness
288 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700289 * On success, returns true; the mutex should be later unlocked. On
290 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700291 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700292static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700293{
294 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700295 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700296 mutex_unlock(&cgroup_mutex);
297 return false;
298 }
299 return true;
300}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700301
Paul Menage81a6a5c2007-10-18 23:39:38 -0700302/* the list of cgroups eligible for automatic release. Protected by
303 * release_list_lock */
304static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200305static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700306static void cgroup_release_agent(struct work_struct *work);
307static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700308static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700309
Tejun Heo69d02062013-06-12 21:04:50 -0700310/*
311 * A cgroup can be associated with multiple css_sets as different tasks may
312 * belong to different cgroups on different hierarchies. In the other
313 * direction, a css_set is naturally associated with multiple cgroups.
314 * This M:N relationship is represented by the following link structure
315 * which exists for each association and allows traversing the associations
316 * from both sides.
317 */
318struct cgrp_cset_link {
319 /* the cgroup and css_set this link associates */
320 struct cgroup *cgrp;
321 struct css_set *cset;
322
323 /* list of cgrp_cset_links anchored at cgrp->cset_links */
324 struct list_head cset_link;
325
326 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
327 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700328};
329
330/* The default css_set - used by init and its children prior to any
331 * hierarchies being mounted. It contains a pointer to the root state
332 * for each subsystem. Also used to anchor the list of css_sets. Not
333 * reference-counted, to improve performance when child cgroups
334 * haven't been created.
335 */
336
337static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700338static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700339
Ben Blume6a11052010-03-10 15:22:09 -0800340static int cgroup_init_idr(struct cgroup_subsys *ss,
341 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700342
Paul Menage817929e2007-10-18 23:39:36 -0700343/* css_set_lock protects the list of css_set objects, and the
344 * chain of tasks off each css_set. Nests outside task->alloc_lock
345 * due to cgroup_iter_start() */
346static DEFINE_RWLOCK(css_set_lock);
347static int css_set_count;
348
Paul Menage7717f7b2009-09-23 15:56:22 -0700349/*
350 * hash table for cgroup groups. This improves the performance to find
351 * an existing css_set. This hash doesn't (currently) take into
352 * account cgroups in empty hierarchies.
353 */
Li Zefan472b1052008-04-29 01:00:11 -0700354#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800355static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700356
Li Zefan0ac801f2013-01-10 11:49:27 +0800357static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700358{
359 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +0800360 unsigned long key = 0UL;
Li Zefan472b1052008-04-29 01:00:11 -0700361
362 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++)
Li Zefan0ac801f2013-01-10 11:49:27 +0800363 key += (unsigned long)css[i];
364 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700365
Li Zefan0ac801f2013-01-10 11:49:27 +0800366 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700367}
368
Paul Menage817929e2007-10-18 23:39:36 -0700369/* We don't maintain the lists running through each css_set to its
370 * task until after the first call to cgroup_iter_start(). This
371 * reduces the fork()/exit() overhead for people who have cgroups
372 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700373static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700374
Tejun Heo5abb8852013-06-12 21:04:49 -0700375static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700376{
Tejun Heo69d02062013-06-12 21:04:50 -0700377 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700378
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700379 /*
380 * Ensure that the refcount doesn't hit zero while any readers
381 * can see it. Similar to atomic_dec_and_lock(), but for an
382 * rwlock
383 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700384 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700385 return;
386 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700387 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700388 write_unlock(&css_set_lock);
389 return;
390 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700391
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700392 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700393 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700394 css_set_count--;
395
Tejun Heo69d02062013-06-12 21:04:50 -0700396 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700397 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700398
Tejun Heo69d02062013-06-12 21:04:50 -0700399 list_del(&link->cset_link);
400 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800401
Tejun Heoddd69142013-06-12 21:04:54 -0700402 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700403 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700404 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700405 set_bit(CGRP_RELEASABLE, &cgrp->flags);
406 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700407 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700408
409 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700410 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700411
412 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700413 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700414}
415
416/*
417 * refcounted get/put for css_set objects
418 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700419static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700420{
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700422}
423
Tejun Heo5abb8852013-06-12 21:04:49 -0700424static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700425{
Tejun Heo5abb8852013-06-12 21:04:49 -0700426 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700427}
428
Tejun Heo5abb8852013-06-12 21:04:49 -0700429static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700430{
Tejun Heo5abb8852013-06-12 21:04:49 -0700431 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432}
433
Tejun Heob326f9d2013-06-24 15:21:48 -0700434/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700435 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700436 * @cset: candidate css_set being tested
437 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700438 * @new_cgrp: cgroup that's being entered by the task
439 * @template: desired set of css pointers in css_set (pre-calculated)
440 *
441 * Returns true if "cg" matches "old_cg" except for the hierarchy
442 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
443 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700444static bool compare_css_sets(struct css_set *cset,
445 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700446 struct cgroup *new_cgrp,
447 struct cgroup_subsys_state *template[])
448{
449 struct list_head *l1, *l2;
450
Tejun Heo5abb8852013-06-12 21:04:49 -0700451 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700452 /* Not all subsystems matched */
453 return false;
454 }
455
456 /*
457 * Compare cgroup pointers in order to distinguish between
458 * different cgroups in heirarchies with no subsystems. We
459 * could get by with just this check alone (and skip the
460 * memcmp above) but on most setups the memcmp check will
461 * avoid the need for this more expensive check on almost all
462 * candidates.
463 */
464
Tejun Heo69d02062013-06-12 21:04:50 -0700465 l1 = &cset->cgrp_links;
466 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700468 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700469 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700470
471 l1 = l1->next;
472 l2 = l2->next;
473 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700474 if (l1 == &cset->cgrp_links) {
475 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700476 break;
477 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700478 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700479 }
480 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700481 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
482 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
483 cgrp1 = link1->cgrp;
484 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700485 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700486 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700487
488 /*
489 * If this hierarchy is the hierarchy of the cgroup
490 * that's changing, then we need to check that this
491 * css_set points to the new cgroup; if it's any other
492 * hierarchy, then this css_set should point to the
493 * same cgroup as the old css_set.
494 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 if (cgrp1->root == new_cgrp->root) {
496 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 return false;
498 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700499 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700500 return false;
501 }
502 }
503 return true;
504}
505
Tejun Heob326f9d2013-06-24 15:21:48 -0700506/**
507 * find_existing_css_set - init css array and find the matching css_set
508 * @old_cset: the css_set that we're using before the cgroup transition
509 * @cgrp: the cgroup that we're moving into
510 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700511 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700512static struct css_set *find_existing_css_set(struct css_set *old_cset,
513 struct cgroup *cgrp,
514 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700515{
Paul Menagebd89aab2007-10-18 23:40:44 -0700516 struct cgroupfs_root *root = cgrp->root;
Tejun Heo5abb8852013-06-12 21:04:49 -0700517 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800518 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700519 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700520
Ben Blumaae8aab2010-03-10 15:22:07 -0800521 /*
522 * Build the set of subsystem state objects that we want to see in the
523 * new css_set. while subsystems can change globally, the entries here
524 * won't change, so no need for locking.
525 */
Paul Menage817929e2007-10-18 23:39:36 -0700526 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400527 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700528 /* Subsystem is in this hierarchy. So we want
529 * the subsystem state from the new
530 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700531 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700532 } else {
533 /* Subsystem is not in this hierarchy, so we
534 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700535 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700536 }
537 }
538
Li Zefan0ac801f2013-01-10 11:49:27 +0800539 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 hash_for_each_possible(css_set_table, cset, hlist, key) {
541 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700542 continue;
543
544 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700545 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700546 }
Paul Menage817929e2007-10-18 23:39:36 -0700547
548 /* No existing cgroup group matched */
549 return NULL;
550}
551
Tejun Heo69d02062013-06-12 21:04:50 -0700552static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700553{
Tejun Heo69d02062013-06-12 21:04:50 -0700554 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700555
Tejun Heo69d02062013-06-12 21:04:50 -0700556 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
557 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700558 kfree(link);
559 }
560}
561
Tejun Heo69d02062013-06-12 21:04:50 -0700562/**
563 * allocate_cgrp_cset_links - allocate cgrp_cset_links
564 * @count: the number of links to allocate
565 * @tmp_links: list_head the allocated links are put on
566 *
567 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
568 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700569 */
Tejun Heo69d02062013-06-12 21:04:50 -0700570static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700571{
Tejun Heo69d02062013-06-12 21:04:50 -0700572 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700573 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700574
575 INIT_LIST_HEAD(tmp_links);
576
Li Zefan36553432008-07-29 22:33:19 -0700577 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700578 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700579 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700580 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700581 return -ENOMEM;
582 }
Tejun Heo69d02062013-06-12 21:04:50 -0700583 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700584 }
585 return 0;
586}
587
Li Zefanc12f65d2009-01-07 18:07:42 -0800588/**
589 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700590 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700591 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800592 * @cgrp: the destination cgroup
593 */
Tejun Heo69d02062013-06-12 21:04:50 -0700594static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
595 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800596{
Tejun Heo69d02062013-06-12 21:04:50 -0700597 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800598
Tejun Heo69d02062013-06-12 21:04:50 -0700599 BUG_ON(list_empty(tmp_links));
600 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
601 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700602 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700603 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700604 /*
605 * Always add links to the tail of the list so that the list
606 * is sorted by order of hierarchy creation
607 */
Tejun Heo69d02062013-06-12 21:04:50 -0700608 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800609}
610
Tejun Heob326f9d2013-06-24 15:21:48 -0700611/**
612 * find_css_set - return a new css_set with one cgroup updated
613 * @old_cset: the baseline css_set
614 * @cgrp: the cgroup to be updated
615 *
616 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
617 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700618 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700619static struct css_set *find_css_set(struct css_set *old_cset,
620 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700621{
Tejun Heob326f9d2013-06-24 15:21:48 -0700622 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700623 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700624 struct list_head tmp_links;
625 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800626 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700627
Tejun Heob326f9d2013-06-24 15:21:48 -0700628 lockdep_assert_held(&cgroup_mutex);
629
Paul Menage817929e2007-10-18 23:39:36 -0700630 /* First see if we already have a cgroup group that matches
631 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700632 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700633 cset = find_existing_css_set(old_cset, cgrp, template);
634 if (cset)
635 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700636 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700637
Tejun Heo5abb8852013-06-12 21:04:49 -0700638 if (cset)
639 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700640
Tejun Heof4f4be22013-06-12 21:04:51 -0700641 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700642 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700643 return NULL;
644
Tejun Heo69d02062013-06-12 21:04:50 -0700645 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700646 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700647 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700648 return NULL;
649 }
650
Tejun Heo5abb8852013-06-12 21:04:49 -0700651 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700652 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700653 INIT_LIST_HEAD(&cset->tasks);
654 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700655
656 /* Copy the set of subsystem state objects generated in
657 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700658 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700659
660 write_lock(&css_set_lock);
661 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700662 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700663 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700664
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 if (c->root == cgrp->root)
666 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700667 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700668 }
Paul Menage817929e2007-10-18 23:39:36 -0700669
Tejun Heo69d02062013-06-12 21:04:50 -0700670 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700671
Paul Menage817929e2007-10-18 23:39:36 -0700672 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700673
674 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700675 key = css_set_hash(cset->subsys);
676 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700677
Paul Menage817929e2007-10-18 23:39:36 -0700678 write_unlock(&css_set_lock);
679
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700681}
682
Paul Menageddbcc7e2007-10-18 23:39:30 -0700683/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700684 * Return the cgroup for "task" from the given hierarchy. Must be
685 * called with cgroup_mutex held.
686 */
687static struct cgroup *task_cgroup_from_root(struct task_struct *task,
688 struct cgroupfs_root *root)
689{
Tejun Heo5abb8852013-06-12 21:04:49 -0700690 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700691 struct cgroup *res = NULL;
692
693 BUG_ON(!mutex_is_locked(&cgroup_mutex));
694 read_lock(&css_set_lock);
695 /*
696 * No need to lock the task - since we hold cgroup_mutex the
697 * task can't change groups, so the only thing that can happen
698 * is that it exits and its css is set back to init_css_set.
699 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700700 cset = task->cgroups;
701 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700702 res = &root->top_cgroup;
703 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700704 struct cgrp_cset_link *link;
705
706 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700707 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700708
Paul Menage7717f7b2009-09-23 15:56:22 -0700709 if (c->root == root) {
710 res = c;
711 break;
712 }
713 }
714 }
715 read_unlock(&css_set_lock);
716 BUG_ON(!res);
717 return res;
718}
719
720/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700721 * There is one global cgroup mutex. We also require taking
722 * task_lock() when dereferencing a task's cgroup subsys pointers.
723 * See "The task_lock() exception", at the end of this comment.
724 *
725 * A task must hold cgroup_mutex to modify cgroups.
726 *
727 * Any task can increment and decrement the count field without lock.
728 * So in general, code holding cgroup_mutex can't rely on the count
729 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800730 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700731 * means that no tasks are currently attached, therefore there is no
732 * way a task attached to that cgroup can fork (the other way to
733 * increment the count). So code holding cgroup_mutex can safely
734 * assume that if the count is zero, it will stay zero. Similarly, if
735 * a task holds cgroup_mutex on a cgroup with zero count, it
736 * knows that the cgroup won't be removed, as cgroup_rmdir()
737 * needs that mutex.
738 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700739 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
740 * (usually) take cgroup_mutex. These are the two most performance
741 * critical pieces of code here. The exception occurs on cgroup_exit(),
742 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
743 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800744 * to the release agent with the name of the cgroup (path relative to
745 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700746 *
747 * A cgroup can only be deleted if both its 'count' of using tasks
748 * is zero, and its list of 'children' cgroups is empty. Since all
749 * tasks in the system use _some_ cgroup, and since there is always at
750 * least one task in the system (init, pid == 1), therefore, top_cgroup
751 * always has either children cgroups and/or using tasks. So we don't
752 * need a special hack to ensure that top_cgroup cannot be deleted.
753 *
754 * The task_lock() exception
755 *
756 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800757 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800758 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700759 * several performance critical places that need to reference
760 * task->cgroup without the expense of grabbing a system global
761 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800762 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700763 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
764 * the task_struct routinely used for such matters.
765 *
766 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800767 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 */
769
Paul Menageddbcc7e2007-10-18 23:39:30 -0700770/*
771 * A couple of forward declarations required, due to cyclic reference loop:
772 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
773 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
774 * -> cgroup_mkdir.
775 */
776
Al Viro18bb1db2011-07-26 01:41:39 -0400777static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400778static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400780static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
781 unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700782static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700783static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700784
785static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200786 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700787 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700788};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700789
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700790static int alloc_css_id(struct cgroup_subsys *ss,
791 struct cgroup *parent, struct cgroup *child);
792
Al Viroa5e7ed32011-07-26 01:55:55 -0400793static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794{
795 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796
797 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400798 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100800 inode->i_uid = current_fsuid();
801 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
803 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
804 }
805 return inode;
806}
807
Li Zefan65dff752013-03-01 15:01:56 +0800808static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
809{
810 struct cgroup_name *name;
811
812 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
813 if (!name)
814 return NULL;
815 strcpy(name->name, dentry->d_name.name);
816 return name;
817}
818
Li Zefanbe445622013-01-24 14:31:42 +0800819static void cgroup_free_fn(struct work_struct *work)
820{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700821 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800822 struct cgroup_subsys *ss;
823
824 mutex_lock(&cgroup_mutex);
825 /*
826 * Release the subsystem state objects.
827 */
Tejun Heo5549c492013-06-24 15:21:48 -0700828 for_each_root_subsys(cgrp->root, ss)
Li Zefanbe445622013-01-24 14:31:42 +0800829 ss->css_free(cgrp);
830
831 cgrp->root->number_of_cgroups--;
832 mutex_unlock(&cgroup_mutex);
833
834 /*
Li Zefan415cf072013-04-08 14:35:02 +0800835 * We get a ref to the parent's dentry, and put the ref when
836 * this cgroup is being freed, so it's guaranteed that the
837 * parent won't be destroyed before its children.
838 */
839 dput(cgrp->parent->dentry);
840
Li Zefancc20e012013-04-26 11:58:02 -0700841 ida_simple_remove(&cgrp->root->cgroup_ida, cgrp->id);
842
Li Zefan415cf072013-04-08 14:35:02 +0800843 /*
Li Zefanbe445622013-01-24 14:31:42 +0800844 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700845 * created the cgroup. This will free cgrp->root, if we are
846 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800847 */
848 deactivate_super(cgrp->root->sb);
849
850 /*
851 * if we're getting rid of the cgroup, refcount should ensure
852 * that there are no pidlists left.
853 */
854 BUG_ON(!list_empty(&cgrp->pidlists));
855
856 simple_xattrs_free(&cgrp->xattrs);
857
Li Zefan65dff752013-03-01 15:01:56 +0800858 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800859 kfree(cgrp);
860}
861
862static void cgroup_free_rcu(struct rcu_head *head)
863{
864 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
865
Tejun Heoea15f8c2013-06-13 19:27:42 -0700866 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
867 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800868}
869
Paul Menageddbcc7e2007-10-18 23:39:30 -0700870static void cgroup_diput(struct dentry *dentry, struct inode *inode)
871{
872 /* is dentry a directory ? if so, kfree() associated cgroup */
873 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700874 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800875
Tejun Heo54766d42013-06-12 21:04:53 -0700876 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800877 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700878 } else {
879 struct cfent *cfe = __d_cfe(dentry);
880 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
881
882 WARN_ONCE(!list_empty(&cfe->node) &&
883 cgrp != &cgrp->root->top_cgroup,
884 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700885 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700886 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700887 }
888 iput(inode);
889}
890
Al Viroc72a04e2011-01-14 05:31:45 +0000891static int cgroup_delete(const struct dentry *d)
892{
893 return 1;
894}
895
Paul Menageddbcc7e2007-10-18 23:39:30 -0700896static void remove_dir(struct dentry *d)
897{
898 struct dentry *parent = dget(d->d_parent);
899
900 d_delete(d);
901 simple_rmdir(parent->d_inode, d);
902 dput(parent);
903}
904
Li Zefan2739d3c2013-01-21 18:18:33 +0800905static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700906{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700907 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700908
Tejun Heo05ef1d72012-04-01 12:09:56 -0700909 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
910 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100911
Li Zefan2739d3c2013-01-21 18:18:33 +0800912 /*
913 * If we're doing cleanup due to failure of cgroup_create(),
914 * the corresponding @cfe may not exist.
915 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916 list_for_each_entry(cfe, &cgrp->files, node) {
917 struct dentry *d = cfe->dentry;
918
919 if (cft && cfe->type != cft)
920 continue;
921
922 dget(d);
923 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700924 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700925 list_del_init(&cfe->node);
926 dput(d);
927
Li Zefan2739d3c2013-01-21 18:18:33 +0800928 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930}
931
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400932/**
933 * cgroup_clear_directory - selective removal of base and subsystem files
934 * @dir: directory containing the files
935 * @base_files: true if the base files should be removed
936 * @subsys_mask: mask of the subsystem ids whose files should be removed
937 */
938static void cgroup_clear_directory(struct dentry *dir, bool base_files,
939 unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940{
941 struct cgroup *cgrp = __d_cgrp(dir);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400942 struct cgroup_subsys *ss;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700943
Tejun Heo5549c492013-06-24 15:21:48 -0700944 for_each_root_subsys(cgrp->root, ss) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400945 struct cftype_set *set;
946 if (!test_bit(ss->subsys_id, &subsys_mask))
947 continue;
948 list_for_each_entry(set, &ss->cftsets, node)
Gao feng879a3d92012-12-01 00:21:28 +0800949 cgroup_addrm_files(cgrp, NULL, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400950 }
951 if (base_files) {
952 while (!list_empty(&cgrp->files))
953 cgroup_rm_file(cgrp, NULL);
954 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700955}
956
957/*
958 * NOTE : the dentry must have been dget()'ed
959 */
960static void cgroup_d_remove_dir(struct dentry *dentry)
961{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100962 struct dentry *parent;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400963 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100964
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400965 cgroup_clear_directory(dentry, true, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700966
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100967 parent = dentry->d_parent;
968 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800969 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700970 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100971 spin_unlock(&dentry->d_lock);
972 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 remove_dir(dentry);
974}
975
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700976/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800977 * Call with cgroup_mutex held. Drops reference counts on modules, including
978 * any duplicate ones that parse_cgroupfs_options took. If this function
979 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800980 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700981static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700982 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983{
Paul Menagebd89aab2007-10-18 23:40:44 -0700984 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985 int i;
986
Ben Blumaae8aab2010-03-10 15:22:07 -0800987 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800988 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800989
Paul Menageddbcc7e2007-10-18 23:39:30 -0700990 /* Check that any added subsystems are currently free */
991 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Li Zefan8d53d552008-02-23 15:24:11 -0800992 unsigned long bit = 1UL << i;
Tejun Heo9871bf92013-06-24 15:21:47 -0700993 struct cgroup_subsys *ss = cgroup_subsys[i];
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400994 if (!(bit & added_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 continue;
Ben Blumaae8aab2010-03-10 15:22:07 -0800996 /*
997 * Nobody should tell us to do a subsys that doesn't exist:
998 * parse_cgroupfs_options should catch that case and refcounts
999 * ensure that subsystems won't disappear once selected.
1000 */
1001 BUG_ON(ss == NULL);
Tejun Heo9871bf92013-06-24 15:21:47 -07001002 if (ss->root != &cgroup_dummy_root) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001003 /* Subsystem isn't free */
1004 return -EBUSY;
1005 }
1006 }
1007
1008 /* Currently we don't handle adding/removing subsystems when
1009 * any child cgroups exist. This is theoretically supportable
1010 * but involves complex error handling, so it's being left until
1011 * later */
Paul Menage307257c2008-12-15 13:54:22 -08001012 if (root->number_of_cgroups > 1)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 return -EBUSY;
1014
1015 /* Process each subsystem */
1016 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001017 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 unsigned long bit = 1UL << i;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001019 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 /* We're binding this subsystem to this hierarchy */
Ben Blumaae8aab2010-03-10 15:22:07 -08001021 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001022 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001023 BUG_ON(!cgroup_dummy_top->subsys[i]);
1024 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001025
Tejun Heo9871bf92013-06-24 15:21:47 -07001026 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001027 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001028 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001029 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 if (ss->bind)
Li Zefan761b3ef2012-01-31 13:47:36 +08001031 ss->bind(cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001032
Ben Blumcf5d5942010-03-10 15:22:09 -08001033 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001034 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001035 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 /* We're removing this subsystem */
Ben Blumaae8aab2010-03-10 15:22:07 -08001037 BUG_ON(ss == NULL);
Tejun Heo9871bf92013-06-24 15:21:47 -07001038 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001039 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001040
Paul Menageddbcc7e2007-10-18 23:39:30 -07001041 if (ss->bind)
Tejun Heo9871bf92013-06-24 15:21:47 -07001042 ss->bind(cgroup_dummy_top);
1043 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001044 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001045 cgroup_subsys[i]->root = &cgroup_dummy_root;
1046 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001047
Ben Blumcf5d5942010-03-10 15:22:09 -08001048 /* subsystem is now free - drop reference on module */
1049 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001050 root->subsys_mask &= ~bit;
1051 } else if (bit & root->subsys_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 /* Subsystem state should already exist */
Ben Blumaae8aab2010-03-10 15:22:07 -08001053 BUG_ON(ss == NULL);
Paul Menagebd89aab2007-10-18 23:40:44 -07001054 BUG_ON(!cgrp->subsys[i]);
Ben Blumcf5d5942010-03-10 15:22:09 -08001055 /*
1056 * a refcount was taken, but we already had one, so
1057 * drop the extra reference.
1058 */
1059 module_put(ss->module);
1060#ifdef CONFIG_MODULE_UNLOAD
1061 BUG_ON(ss->module && !module_refcount(ss->module));
1062#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 } else {
1064 /* Subsystem state shouldn't exist */
Paul Menagebd89aab2007-10-18 23:40:44 -07001065 BUG_ON(cgrp->subsys[i]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 }
1067 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001068
1069 return 0;
1070}
1071
Al Viro34c80b12011-12-08 21:32:45 -05001072static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073{
Al Viro34c80b12011-12-08 21:32:45 -05001074 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 struct cgroup_subsys *ss;
1076
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001077 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001078 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001079 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001080 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1081 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001082 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001084 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001085 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001086 if (strlen(root->release_agent_path))
1087 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001088 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001089 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001090 if (strlen(root->name))
1091 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001092 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001093 return 0;
1094}
1095
1096struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001097 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001099 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001100 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001101 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001102 /* User explicitly requested empty subsystem */
1103 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001104
1105 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001106
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107};
1108
Ben Blumaae8aab2010-03-10 15:22:07 -08001109/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001110 * Convert a hierarchy specifier into a bitmask of subsystems and
1111 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1112 * array. This function takes refcounts on subsystems to be used, unless it
1113 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001114 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001115static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001117 char *token, *o = data;
1118 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001119 unsigned long mask = (unsigned long)-1;
Ben Blumcf5d5942010-03-10 15:22:09 -08001120 int i;
1121 bool module_pin_failed = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001122
Ben Blumaae8aab2010-03-10 15:22:07 -08001123 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1124
Li Zefanf9ab5b52009-06-17 16:26:33 -07001125#ifdef CONFIG_CPUSETS
1126 mask = ~(1UL << cpuset_subsys_id);
1127#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128
Paul Menagec6d57f32009-09-23 15:56:19 -07001129 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001130
1131 while ((token = strsep(&o, ",")) != NULL) {
1132 if (!*token)
1133 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001134 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001135 /* Explicitly have no subsystems */
1136 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001137 continue;
1138 }
1139 if (!strcmp(token, "all")) {
1140 /* Mutually exclusive option 'all' + subsystem name */
1141 if (one_ss)
1142 return -EINVAL;
1143 all_ss = true;
1144 continue;
1145 }
Tejun Heo873fe092013-04-14 20:15:26 -07001146 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1147 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1148 continue;
1149 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001151 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001152 continue;
1153 }
1154 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001155 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001156 continue;
1157 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001158 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001159 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001160 continue;
1161 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001162 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001163 /* Specifying two release agents is forbidden */
1164 if (opts->release_agent)
1165 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001166 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001167 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001168 if (!opts->release_agent)
1169 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001170 continue;
1171 }
1172 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 const char *name = token + 5;
1174 /* Can't specify an empty name */
1175 if (!strlen(name))
1176 return -EINVAL;
1177 /* Must match [\w.-]+ */
1178 for (i = 0; i < strlen(name); i++) {
1179 char c = name[i];
1180 if (isalnum(c))
1181 continue;
1182 if ((c == '.') || (c == '-') || (c == '_'))
1183 continue;
1184 return -EINVAL;
1185 }
1186 /* Specifying two names is forbidden */
1187 if (opts->name)
1188 return -EINVAL;
1189 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001190 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001191 GFP_KERNEL);
1192 if (!opts->name)
1193 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194
1195 continue;
1196 }
1197
1198 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001199 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001200 if (ss == NULL)
1201 continue;
1202 if (strcmp(token, ss->name))
1203 continue;
1204 if (ss->disabled)
1205 continue;
1206
1207 /* Mutually exclusive option 'all' + subsystem name */
1208 if (all_ss)
1209 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001210 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001211 one_ss = true;
1212
1213 break;
1214 }
1215 if (i == CGROUP_SUBSYS_COUNT)
1216 return -ENOENT;
1217 }
1218
1219 /*
1220 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001221 * otherwise if 'none', 'name=' and a subsystem name options
1222 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001223 */
Li Zefan0d19ea82011-12-27 14:25:55 +08001224 if (all_ss || (!one_ss && !opts->none && !opts->name)) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001225 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07001226 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001227 if (ss == NULL)
1228 continue;
1229 if (ss->disabled)
1230 continue;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001231 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001232 }
1233 }
1234
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001235 /* Consistency checks */
1236
Tejun Heo873fe092013-04-14 20:15:26 -07001237 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1238 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1239
1240 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1241 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1242 return -EINVAL;
1243 }
1244
1245 if (opts->cpuset_clone_children) {
1246 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1247 return -EINVAL;
1248 }
1249 }
1250
Li Zefanf9ab5b52009-06-17 16:26:33 -07001251 /*
1252 * Option noprefix was introduced just for backward compatibility
1253 * with the old cpuset, so we allow noprefix only if mounting just
1254 * the cpuset subsystem.
1255 */
Tejun Heo93438622013-04-14 20:15:25 -07001256 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001257 return -EINVAL;
1258
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001259
1260 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001261 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001262 return -EINVAL;
1263
1264 /*
1265 * We either have to specify by name or by subsystems. (So all
1266 * empty hierarchies must have a name).
1267 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001268 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269 return -EINVAL;
1270
Ben Blumcf5d5942010-03-10 15:22:09 -08001271 /*
1272 * Grab references on all the modules we'll need, so the subsystems
1273 * don't dance around before rebind_subsystems attaches them. This may
1274 * take duplicate reference counts on a subsystem that's already used,
1275 * but rebind_subsystems handles this case.
1276 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001277 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001278 unsigned long bit = 1UL << i;
1279
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001280 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001281 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001282 if (!try_module_get(cgroup_subsys[i]->module)) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001283 module_pin_failed = true;
1284 break;
1285 }
1286 }
1287 if (module_pin_failed) {
1288 /*
1289 * oops, one of the modules was going away. this means that we
1290 * raced with a module_delete call, and to the user this is
1291 * essentially a "subsystem doesn't exist" case.
1292 */
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001293 for (i--; i >= 0; i--) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001294 /* drop refcounts only on the ones we took */
1295 unsigned long bit = 1UL << i;
1296
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001297 if (!(bit & opts->subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001298 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001299 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001300 }
1301 return -ENOENT;
1302 }
1303
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 return 0;
1305}
1306
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001307static void drop_parsed_module_refcounts(unsigned long subsys_mask)
Ben Blumcf5d5942010-03-10 15:22:09 -08001308{
1309 int i;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02001310 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Ben Blumcf5d5942010-03-10 15:22:09 -08001311 unsigned long bit = 1UL << i;
1312
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001313 if (!(bit & subsys_mask))
Ben Blumcf5d5942010-03-10 15:22:09 -08001314 continue;
Tejun Heo9871bf92013-06-24 15:21:47 -07001315 module_put(cgroup_subsys[i]->module);
Ben Blumcf5d5942010-03-10 15:22:09 -08001316 }
1317}
1318
Paul Menageddbcc7e2007-10-18 23:39:30 -07001319static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1320{
1321 int ret = 0;
1322 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001325 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
Tejun Heo873fe092013-04-14 20:15:26 -07001327 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1328 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1329 return -EINVAL;
1330 }
1331
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001333 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001334 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001335
1336 /* See what subsystems are wanted */
1337 ret = parse_cgroupfs_options(data, &opts);
1338 if (ret)
1339 goto out_unlock;
1340
Tejun Heoa8a648c2013-06-24 15:21:47 -07001341 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001342 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1343 task_tgid_nr(current), current->comm);
1344
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001345 added_mask = opts.subsys_mask & ~root->subsys_mask;
1346 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001347
Ben Blumcf5d5942010-03-10 15:22:09 -08001348 /* Don't allow flags or name to change at remount */
1349 if (opts.flags != root->flags ||
1350 (opts.name && strcmp(opts.name, root->name))) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351 ret = -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001352 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001353 goto out_unlock;
1354 }
1355
Gao feng7083d032012-12-03 09:28:18 +08001356 /*
1357 * Clear out the files of subsystems that should be removed, do
1358 * this before rebind_subsystems, since rebind_subsystems may
1359 * change this hierarchy's subsys_list.
1360 */
1361 cgroup_clear_directory(cgrp->dentry, false, removed_mask);
1362
Tejun Heoa8a648c2013-06-24 15:21:47 -07001363 ret = rebind_subsystems(root, added_mask, removed_mask);
Ben Blumcf5d5942010-03-10 15:22:09 -08001364 if (ret) {
Gao feng7083d032012-12-03 09:28:18 +08001365 /* rebind_subsystems failed, re-populate the removed files */
1366 cgroup_populate_dir(cgrp, false, removed_mask);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001367 drop_parsed_module_refcounts(opts.subsys_mask);
Li Zefan0670e082009-04-02 16:57:30 -07001368 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001369 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001371 /* re-populate subsystem files */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001372 cgroup_populate_dir(cgrp, false, added_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373
Paul Menage81a6a5c2007-10-18 23:39:38 -07001374 if (opts.release_agent)
1375 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001376 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001377 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001378 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001379 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001381 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 return ret;
1383}
1384
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001385static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001386 .statfs = simple_statfs,
1387 .drop_inode = generic_delete_inode,
1388 .show_options = cgroup_show_options,
1389 .remount_fs = cgroup_remount,
1390};
1391
Paul Menagecc31edc2008-10-18 20:28:04 -07001392static void init_cgroup_housekeeping(struct cgroup *cgrp)
1393{
1394 INIT_LIST_HEAD(&cgrp->sibling);
1395 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001396 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001397 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001398 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001399 INIT_LIST_HEAD(&cgrp->pidlists);
1400 mutex_init(&cgrp->pidlist_mutex);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001401 INIT_LIST_HEAD(&cgrp->event_list);
1402 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001403 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001404}
Paul Menagec6d57f32009-09-23 15:56:19 -07001405
Paul Menageddbcc7e2007-10-18 23:39:30 -07001406static void init_cgroup_root(struct cgroupfs_root *root)
1407{
Paul Menagebd89aab2007-10-18 23:40:44 -07001408 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001409
Paul Menageddbcc7e2007-10-18 23:39:30 -07001410 INIT_LIST_HEAD(&root->subsys_list);
1411 INIT_LIST_HEAD(&root->root_list);
1412 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001413 cgrp->root = root;
Li Zefan65dff752013-03-01 15:01:56 +08001414 cgrp->name = &root_cgroup_name;
Paul Menagecc31edc2008-10-18 20:28:04 -07001415 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001416}
1417
Tejun Heofa3ca072013-04-14 11:36:56 -07001418static int cgroup_init_root_id(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001419{
Tejun Heo1a574232013-04-14 11:36:58 -07001420 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001421
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001422 lockdep_assert_held(&cgroup_mutex);
1423 lockdep_assert_held(&cgroup_root_mutex);
1424
Tejun Heo1a574232013-04-14 11:36:58 -07001425 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, 2, 0, GFP_KERNEL);
1426 if (id < 0)
1427 return id;
1428
1429 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001430 return 0;
1431}
1432
1433static void cgroup_exit_root_id(struct cgroupfs_root *root)
1434{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001435 lockdep_assert_held(&cgroup_mutex);
1436 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001437
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001438 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001439 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001440 root->hierarchy_id = 0;
1441 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001442}
1443
Paul Menageddbcc7e2007-10-18 23:39:30 -07001444static int cgroup_test_super(struct super_block *sb, void *data)
1445{
Paul Menagec6d57f32009-09-23 15:56:19 -07001446 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447 struct cgroupfs_root *root = sb->s_fs_info;
1448
Paul Menagec6d57f32009-09-23 15:56:19 -07001449 /* If we asked for a name then it must match */
1450 if (opts->name && strcmp(opts->name, root->name))
1451 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001452
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001453 /*
1454 * If we asked for subsystems (or explicitly for no
1455 * subsystems) then they must match
1456 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001457 if ((opts->subsys_mask || opts->none)
1458 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001459 return 0;
1460
1461 return 1;
1462}
1463
Paul Menagec6d57f32009-09-23 15:56:19 -07001464static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1465{
1466 struct cgroupfs_root *root;
1467
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001468 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001469 return NULL;
1470
1471 root = kzalloc(sizeof(*root), GFP_KERNEL);
1472 if (!root)
1473 return ERR_PTR(-ENOMEM);
1474
1475 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001476
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001477 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001478 root->flags = opts->flags;
Tejun Heo0a950f62012-11-19 09:02:12 -08001479 ida_init(&root->cgroup_ida);
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 if (opts->release_agent)
1481 strcpy(root->release_agent_path, opts->release_agent);
1482 if (opts->name)
1483 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001484 if (opts->cpuset_clone_children)
1485 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001486 return root;
1487}
1488
Tejun Heofa3ca072013-04-14 11:36:56 -07001489static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490{
Tejun Heofa3ca072013-04-14 11:36:56 -07001491 if (root) {
1492 /* hierarhcy ID shoulid already have been released */
1493 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001494
Tejun Heofa3ca072013-04-14 11:36:56 -07001495 ida_destroy(&root->cgroup_ida);
1496 kfree(root);
1497 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001498}
1499
Paul Menageddbcc7e2007-10-18 23:39:30 -07001500static int cgroup_set_super(struct super_block *sb, void *data)
1501{
1502 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001503 struct cgroup_sb_opts *opts = data;
1504
1505 /* If we don't have a new root, we can't set up a new sb */
1506 if (!opts->new_root)
1507 return -EINVAL;
1508
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001509 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510
1511 ret = set_anon_super(sb, NULL);
1512 if (ret)
1513 return ret;
1514
Paul Menagec6d57f32009-09-23 15:56:19 -07001515 sb->s_fs_info = opts->new_root;
1516 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001517
1518 sb->s_blocksize = PAGE_CACHE_SIZE;
1519 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1520 sb->s_magic = CGROUP_SUPER_MAGIC;
1521 sb->s_op = &cgroup_ops;
1522
1523 return 0;
1524}
1525
1526static int cgroup_get_rootdir(struct super_block *sb)
1527{
Al Viro0df6a632010-12-21 13:29:29 -05001528 static const struct dentry_operations cgroup_dops = {
1529 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001530 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001531 };
1532
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 struct inode *inode =
1534 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535
1536 if (!inode)
1537 return -ENOMEM;
1538
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 inode->i_fop = &simple_dir_operations;
1540 inode->i_op = &cgroup_dir_inode_operations;
1541 /* directories start off with i_nlink == 2 (for "." entry) */
1542 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001543 sb->s_root = d_make_root(inode);
1544 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001545 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001546 /* for everything else we want ->d_op set */
1547 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548 return 0;
1549}
1550
Al Virof7e83572010-07-26 13:23:11 +04001551static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001553 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001554{
1555 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001556 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557 int ret = 0;
1558 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001559 struct cgroupfs_root *new_root;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001560 struct inode *inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561
1562 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001563 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001564 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001565 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 if (ret)
1567 goto out_err;
1568
1569 /*
1570 * Allocate a new cgroup root. We may not need it if we're
1571 * reusing an existing hierarchy.
1572 */
1573 new_root = cgroup_root_from_opts(&opts);
1574 if (IS_ERR(new_root)) {
1575 ret = PTR_ERR(new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001576 goto drop_modules;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001577 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001581 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001583 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001584 cgroup_free_root(opts.new_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08001585 goto drop_modules;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586 }
1587
Paul Menagec6d57f32009-09-23 15:56:19 -07001588 root = sb->s_fs_info;
1589 BUG_ON(!root);
1590 if (root == opts.new_root) {
1591 /* We used the new root structure, so this is a new hierarchy */
Tejun Heo69d02062013-06-12 21:04:50 -07001592 struct list_head tmp_links;
Li Zefanc12f65d2009-01-07 18:07:42 -08001593 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001594 struct cgroupfs_root *existing_root;
eparis@redhat2ce97382011-06-02 21:20:51 +10001595 const struct cred *cred;
Li Zefan28fd5df2008-04-29 01:00:13 -07001596 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001597 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001598
1599 BUG_ON(sb->s_root != NULL);
1600
1601 ret = cgroup_get_rootdir(sb);
1602 if (ret)
1603 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001604 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Paul Menage817929e2007-10-18 23:39:36 -07001606 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001608 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001609
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001610 /* Check for name clashes with existing mounts */
1611 ret = -EBUSY;
1612 if (strlen(root->name))
1613 for_each_active_root(existing_root)
1614 if (!strcmp(existing_root->name, root->name))
1615 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001616
Paul Menage817929e2007-10-18 23:39:36 -07001617 /*
1618 * We're accessing css_set_count without locking
1619 * css_set_lock here, but that's OK - it can only be
1620 * increased by someone holding cgroup_lock, and
1621 * that's us. The worst that can happen is that we
1622 * have some link structures left over
1623 */
Tejun Heo69d02062013-06-12 21:04:50 -07001624 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001625 if (ret)
1626 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001627
Tejun Heofa3ca072013-04-14 11:36:56 -07001628 ret = cgroup_init_root_id(root);
1629 if (ret)
1630 goto unlock_drop;
1631
Tejun Heoa8a648c2013-06-24 15:21:47 -07001632 ret = rebind_subsystems(root, root->subsys_mask, 0);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633 if (ret == -EBUSY) {
Tejun Heo69d02062013-06-12 21:04:50 -07001634 free_cgrp_cset_links(&tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001635 goto unlock_drop;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001636 }
Ben Blumcf5d5942010-03-10 15:22:09 -08001637 /*
1638 * There must be no failure case after here, since rebinding
1639 * takes care of subsystems' refcounts, which are explicitly
1640 * dropped in the failure exit path.
1641 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642
1643 /* EBUSY should be the only error here */
1644 BUG_ON(ret);
1645
Tejun Heo9871bf92013-06-24 15:21:47 -07001646 list_add(&root->root_list, &cgroup_roots);
1647 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001648
Li Zefanc12f65d2009-01-07 18:07:42 -08001649 sb->s_root->d_fsdata = root_cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001650 root->top_cgroup.dentry = sb->s_root;
1651
Paul Menage817929e2007-10-18 23:39:36 -07001652 /* Link the top cgroup in this hierarchy into all
1653 * the css_set objects */
1654 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001655 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001656 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001657 write_unlock(&css_set_lock);
1658
Tejun Heo69d02062013-06-12 21:04:50 -07001659 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001660
Li Zefanc12f65d2009-01-07 18:07:42 -08001661 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662 BUG_ON(root->number_of_cgroups != 1);
1663
eparis@redhat2ce97382011-06-02 21:20:51 +10001664 cred = override_creds(&init_cred);
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001665 cgroup_populate_dir(root_cgrp, true, root->subsys_mask);
eparis@redhat2ce97382011-06-02 21:20:51 +10001666 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001667 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001668 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001669 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001670 } else {
1671 /*
1672 * We re-used an existing hierarchy - the new root (if
1673 * any) is not needed
1674 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001675 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001676
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001677 if (root->flags != opts.flags) {
1678 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1679 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1680 ret = -EINVAL;
1681 goto drop_new_super;
1682 } else {
1683 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1684 }
Tejun Heo873fe092013-04-14 20:15:26 -07001685 }
1686
Ben Blumcf5d5942010-03-10 15:22:09 -08001687 /* no subsys rebinding, so refcounts don't change */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001688 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001689 }
1690
Paul Menagec6d57f32009-09-23 15:56:19 -07001691 kfree(opts.release_agent);
1692 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001693 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001695 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001696 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001697 mutex_unlock(&cgroup_root_mutex);
1698 mutex_unlock(&cgroup_mutex);
1699 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001701 deactivate_locked_super(sb);
Ben Blumcf5d5942010-03-10 15:22:09 -08001702 drop_modules:
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001703 drop_parsed_module_refcounts(opts.subsys_mask);
Paul Menagec6d57f32009-09-23 15:56:19 -07001704 out_err:
1705 kfree(opts.release_agent);
1706 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001707 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708}
1709
1710static void cgroup_kill_sb(struct super_block *sb) {
1711 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001712 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001713 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714 int ret;
1715
1716 BUG_ON(!root);
1717
1718 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001719 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001720
1721 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001722 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723
1724 /* Rebind all subsystems back to the default hierarchy */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001725 ret = rebind_subsystems(root, 0, root->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001726 /* Shouldn't be able to fail ... */
1727 BUG_ON(ret);
1728
Paul Menage817929e2007-10-18 23:39:36 -07001729 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001730 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001731 * root cgroup
1732 */
1733 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001734
Tejun Heo69d02062013-06-12 21:04:50 -07001735 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1736 list_del(&link->cset_link);
1737 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001738 kfree(link);
1739 }
1740 write_unlock(&css_set_lock);
1741
Paul Menage839ec542009-01-29 14:25:22 -08001742 if (!list_empty(&root->root_list)) {
1743 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001744 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001745 }
Li Zefane5f6a862009-01-07 18:07:41 -08001746
Tejun Heofa3ca072013-04-14 11:36:56 -07001747 cgroup_exit_root_id(root);
1748
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001749 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 mutex_unlock(&cgroup_mutex);
1751
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001752 simple_xattrs_free(&cgrp->xattrs);
1753
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001755 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001756}
1757
1758static struct file_system_type cgroup_fs_type = {
1759 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001760 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 .kill_sb = cgroup_kill_sb,
1762};
1763
Greg KH676db4a2010-08-05 13:53:35 -07001764static struct kobject *cgroup_kobj;
1765
Li Zefana043e3b2008-02-23 15:24:09 -08001766/**
1767 * cgroup_path - generate the path of a cgroup
1768 * @cgrp: the cgroup in question
1769 * @buf: the buffer to write the path into
1770 * @buflen: the length of the buffer
1771 *
Li Zefan65dff752013-03-01 15:01:56 +08001772 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1773 *
1774 * We can't generate cgroup path using dentry->d_name, as accessing
1775 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1776 * inode's i_mutex, while on the other hand cgroup_path() can be called
1777 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001779int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780{
Li Zefan65dff752013-03-01 15:01:56 +08001781 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001782 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001783
Tejun Heoda1f2962013-04-14 10:32:19 -07001784 if (!cgrp->parent) {
1785 if (strlcpy(buf, "/", buflen) >= buflen)
1786 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001787 return 0;
1788 }
1789
Tao Ma316eb662012-11-08 21:36:38 +08001790 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001791 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001792
Li Zefan65dff752013-03-01 15:01:56 +08001793 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001794 do {
Li Zefan65dff752013-03-01 15:01:56 +08001795 const char *name = cgroup_name(cgrp);
1796 int len;
1797
1798 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001800 goto out;
1801 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001802
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001804 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001805 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001806
1807 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001808 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001809 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001811out:
1812 rcu_read_unlock();
1813 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814}
Ben Blum67523c42010-03-10 15:22:11 -08001815EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816
Tejun Heo857a2be2013-04-14 20:50:08 -07001817/**
1818 * task_cgroup_path_from_hierarchy - cgroup path of a task on a hierarchy
1819 * @task: target task
1820 * @hierarchy_id: the hierarchy to look up @task's cgroup from
1821 * @buf: the buffer to write the path into
1822 * @buflen: the length of the buffer
1823 *
1824 * Determine @task's cgroup on the hierarchy specified by @hierarchy_id and
1825 * copy its path into @buf. This function grabs cgroup_mutex and shouldn't
1826 * be used inside locks used by cgroup controller callbacks.
1827 */
1828int task_cgroup_path_from_hierarchy(struct task_struct *task, int hierarchy_id,
1829 char *buf, size_t buflen)
1830{
1831 struct cgroupfs_root *root;
1832 struct cgroup *cgrp = NULL;
1833 int ret = -ENOENT;
1834
1835 mutex_lock(&cgroup_mutex);
1836
1837 root = idr_find(&cgroup_hierarchy_idr, hierarchy_id);
1838 if (root) {
1839 cgrp = task_cgroup_from_root(task, root);
1840 ret = cgroup_path(cgrp, buf, buflen);
1841 }
1842
1843 mutex_unlock(&cgroup_mutex);
1844
1845 return ret;
1846}
1847EXPORT_SYMBOL_GPL(task_cgroup_path_from_hierarchy);
1848
Ben Blum74a11662011-05-26 16:25:20 -07001849/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001850 * Control Group taskset
1851 */
Tejun Heo134d3372011-12-12 18:12:21 -08001852struct task_and_cgroup {
1853 struct task_struct *task;
1854 struct cgroup *cgrp;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001855 struct css_set *cg;
Tejun Heo134d3372011-12-12 18:12:21 -08001856};
1857
Tejun Heo2f7ee562011-12-12 18:12:21 -08001858struct cgroup_taskset {
1859 struct task_and_cgroup single;
1860 struct flex_array *tc_array;
1861 int tc_array_len;
1862 int idx;
1863 struct cgroup *cur_cgrp;
1864};
1865
1866/**
1867 * cgroup_taskset_first - reset taskset and return the first task
1868 * @tset: taskset of interest
1869 *
1870 * @tset iteration is initialized and the first task is returned.
1871 */
1872struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1873{
1874 if (tset->tc_array) {
1875 tset->idx = 0;
1876 return cgroup_taskset_next(tset);
1877 } else {
1878 tset->cur_cgrp = tset->single.cgrp;
1879 return tset->single.task;
1880 }
1881}
1882EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1883
1884/**
1885 * cgroup_taskset_next - iterate to the next task in taskset
1886 * @tset: taskset of interest
1887 *
1888 * Return the next task in @tset. Iteration must have been initialized
1889 * with cgroup_taskset_first().
1890 */
1891struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1892{
1893 struct task_and_cgroup *tc;
1894
1895 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1896 return NULL;
1897
1898 tc = flex_array_get(tset->tc_array, tset->idx++);
1899 tset->cur_cgrp = tc->cgrp;
1900 return tc->task;
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1903
1904/**
1905 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1906 * @tset: taskset of interest
1907 *
1908 * Return the cgroup for the current (last returned) task of @tset. This
1909 * function must be preceded by either cgroup_taskset_first() or
1910 * cgroup_taskset_next().
1911 */
1912struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1913{
1914 return tset->cur_cgrp;
1915}
1916EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1917
1918/**
1919 * cgroup_taskset_size - return the number of tasks in taskset
1920 * @tset: taskset of interest
1921 */
1922int cgroup_taskset_size(struct cgroup_taskset *tset)
1923{
1924 return tset->tc_array ? tset->tc_array_len : 1;
1925}
1926EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1927
1928
Ben Blum74a11662011-05-26 16:25:20 -07001929/*
1930 * cgroup_task_migrate - move a task from one cgroup to another.
1931 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001932 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001933 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001934static void cgroup_task_migrate(struct cgroup *old_cgrp,
1935 struct task_struct *tsk,
1936 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001937{
Tejun Heo5abb8852013-06-12 21:04:49 -07001938 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001939
1940 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001941 * We are synchronized through threadgroup_lock() against PF_EXITING
1942 * setting such that we can't race against cgroup_exit() changing the
1943 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001944 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001945 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heo5abb8852013-06-12 21:04:49 -07001946 old_cset = tsk->cgroups;
Ben Blum74a11662011-05-26 16:25:20 -07001947
Ben Blum74a11662011-05-26 16:25:20 -07001948 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001949 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001950 task_unlock(tsk);
1951
1952 /* Update the css_set linked lists if we're using them */
1953 write_lock(&css_set_lock);
1954 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001955 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001956 write_unlock(&css_set_lock);
1957
1958 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 * We just gained a reference on old_cset by taking it from the
1960 * task. As trading it for new_cset is protected by cgroup_mutex,
1961 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001962 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001963 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1964 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001965}
1966
Li Zefana043e3b2008-02-23 15:24:09 -08001967/**
Li Zefan081aa452013-03-13 09:17:09 +08001968 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001969 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001970 * @tsk: the task or the leader of the threadgroup to be attached
1971 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001972 *
Tejun Heo257058a2011-12-12 18:12:21 -08001973 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001974 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001975 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001976static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1977 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001978{
1979 int retval, i, group_size;
1980 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001981 struct cgroupfs_root *root = cgrp->root;
1982 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001983 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001984 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001985 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001986 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001987
1988 /*
1989 * step 0: in order to do expensive, possibly blocking operations for
1990 * every thread, we cannot iterate the thread group list, since it needs
1991 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001992 * group - group_rwsem prevents new threads from appearing, and if
1993 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001994 */
Li Zefan081aa452013-03-13 09:17:09 +08001995 if (threadgroup)
1996 group_size = get_nr_threads(tsk);
1997 else
1998 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001999 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002000 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002001 if (!group)
2002 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002003 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002004 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002005 if (retval)
2006 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002007
Ben Blum74a11662011-05-26 16:25:20 -07002008 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002009 /*
2010 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2011 * already PF_EXITING could be freed from underneath us unless we
2012 * take an rcu_read_lock.
2013 */
2014 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002015 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002016 struct task_and_cgroup ent;
2017
Tejun Heocd3d0952011-12-12 18:12:21 -08002018 /* @tsk either already exited or can't exit until the end */
2019 if (tsk->flags & PF_EXITING)
2020 continue;
2021
Ben Blum74a11662011-05-26 16:25:20 -07002022 /* as per above, nr_threads may decrease, but not increase. */
2023 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002024 ent.task = tsk;
2025 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002026 /* nothing to do if this task is already in the cgroup */
2027 if (ent.cgrp == cgrp)
2028 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002029 /*
2030 * saying GFP_ATOMIC has no effect here because we did prealloc
2031 * earlier, but it's good form to communicate our expectations.
2032 */
Tejun Heo134d3372011-12-12 18:12:21 -08002033 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002034 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002035 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002036
2037 if (!threadgroup)
2038 break;
Ben Blum74a11662011-05-26 16:25:20 -07002039 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002040 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002041 /* remember the number of threads in the array for later. */
2042 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002043 tset.tc_array = group;
2044 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002045
Tejun Heo134d3372011-12-12 18:12:21 -08002046 /* methods shouldn't be called if no task is actually migrating */
2047 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002048 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002049 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002050
Ben Blum74a11662011-05-26 16:25:20 -07002051 /*
2052 * step 1: check that we can legitimately attach to the cgroup.
2053 */
Tejun Heo5549c492013-06-24 15:21:48 -07002054 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002055 if (ss->can_attach) {
Li Zefan761b3ef2012-01-31 13:47:36 +08002056 retval = ss->can_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002057 if (retval) {
2058 failed_ss = ss;
2059 goto out_cancel_attach;
2060 }
2061 }
Ben Blum74a11662011-05-26 16:25:20 -07002062 }
2063
2064 /*
2065 * step 2: make sure css_sets exist for all threads to be migrated.
2066 * we use find_css_set, which allocates a new one if necessary.
2067 */
Ben Blum74a11662011-05-26 16:25:20 -07002068 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002069 tc = flex_array_get(group, i);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002070 tc->cg = find_css_set(tc->task->cgroups, cgrp);
2071 if (!tc->cg) {
2072 retval = -ENOMEM;
2073 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002074 }
2075 }
2076
2077 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002078 * step 3: now that we're guaranteed success wrt the css_sets,
2079 * proceed to move all tasks to the new cgroup. There are no
2080 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002081 */
Ben Blum74a11662011-05-26 16:25:20 -07002082 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002083 tc = flex_array_get(group, i);
Kevin Wilson1e2ccd12013-04-01 10:51:37 +03002084 cgroup_task_migrate(tc->cgrp, tc->task, tc->cg);
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
2086 /* nothing is sensitive to fork() after this point. */
2087
2088 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002089 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002090 */
Tejun Heo5549c492013-06-24 15:21:48 -07002091 for_each_root_subsys(root, ss) {
Ben Blum74a11662011-05-26 16:25:20 -07002092 if (ss->attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002093 ss->attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002094 }
2095
2096 /*
2097 * step 5: success! and cleanup
2098 */
Ben Blum74a11662011-05-26 16:25:20 -07002099 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002100out_put_css_set_refs:
2101 if (retval) {
2102 for (i = 0; i < group_size; i++) {
2103 tc = flex_array_get(group, i);
2104 if (!tc->cg)
2105 break;
2106 put_css_set(tc->cg);
2107 }
Ben Blum74a11662011-05-26 16:25:20 -07002108 }
2109out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002110 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002111 for_each_root_subsys(root, ss) {
Tejun Heo494c1672011-12-12 18:12:22 -08002112 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002113 break;
Ben Blum74a11662011-05-26 16:25:20 -07002114 if (ss->cancel_attach)
Li Zefan761b3ef2012-01-31 13:47:36 +08002115 ss->cancel_attach(cgrp, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002116 }
2117 }
Ben Blum74a11662011-05-26 16:25:20 -07002118out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002119 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002120 return retval;
2121}
2122
2123/*
2124 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002125 * function to attach either it or all tasks in its threadgroup. Will lock
2126 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002127 */
2128static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002129{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002130 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002131 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002132 int ret;
2133
Ben Blum74a11662011-05-26 16:25:20 -07002134 if (!cgroup_lock_live_group(cgrp))
2135 return -ENODEV;
2136
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002137retry_find_task:
2138 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002140 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002141 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002143 ret= -ESRCH;
2144 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002145 }
Ben Blum74a11662011-05-26 16:25:20 -07002146 /*
2147 * even if we're attaching all tasks in the thread group, we
2148 * only need to check permissions on one of them.
2149 */
David Howellsc69e8d92008-11-14 10:39:19 +11002150 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002151 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2152 !uid_eq(cred->euid, tcred->uid) &&
2153 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002154 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002155 ret = -EACCES;
2156 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002157 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002158 } else
2159 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002160
2161 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002162 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002163
2164 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002165 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002166 * trapped in a cpuset, or RT worker may be born in a cgroup
2167 * with no rt_runtime allocated. Just say no.
2168 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002169 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002170 ret = -EINVAL;
2171 rcu_read_unlock();
2172 goto out_unlock_cgroup;
2173 }
2174
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002175 get_task_struct(tsk);
2176 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002177
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002178 threadgroup_lock(tsk);
2179 if (threadgroup) {
2180 if (!thread_group_leader(tsk)) {
2181 /*
2182 * a race with de_thread from another thread's exec()
2183 * may strip us of our leadership, if this happens,
2184 * there is no choice but to throw this task away and
2185 * try again; this is
2186 * "double-double-toil-and-trouble-check locking".
2187 */
2188 threadgroup_unlock(tsk);
2189 put_task_struct(tsk);
2190 goto retry_find_task;
2191 }
Li Zefan081aa452013-03-13 09:17:09 +08002192 }
2193
2194 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2195
Tejun Heocd3d0952011-12-12 18:12:21 -08002196 threadgroup_unlock(tsk);
2197
Paul Menagebbcb81d2007-10-18 23:39:32 -07002198 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002199out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002200 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002201 return ret;
2202}
2203
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002204/**
2205 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2206 * @from: attach to all cgroups of a given task
2207 * @tsk: the task to be attached
2208 */
2209int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2210{
2211 struct cgroupfs_root *root;
2212 int retval = 0;
2213
Tejun Heo47cfcd02013-04-07 09:29:51 -07002214 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002215 for_each_active_root(root) {
2216 struct cgroup *from_cg = task_cgroup_from_root(from, root);
2217
2218 retval = cgroup_attach_task(from_cg, tsk, false);
2219 if (retval)
2220 break;
2221 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002222 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002223
2224 return retval;
2225}
2226EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2227
Paul Menageaf351022008-07-25 01:47:01 -07002228static int cgroup_tasks_write(struct cgroup *cgrp, struct cftype *cft, u64 pid)
2229{
Ben Blum74a11662011-05-26 16:25:20 -07002230 return attach_task_by_pid(cgrp, pid, false);
2231}
2232
2233static int cgroup_procs_write(struct cgroup *cgrp, struct cftype *cft, u64 tgid)
2234{
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002235 return attach_task_by_pid(cgrp, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002236}
2237
Paul Menagee788e062008-07-25 01:46:59 -07002238static int cgroup_release_agent_write(struct cgroup *cgrp, struct cftype *cft,
2239 const char *buffer)
2240{
2241 BUILD_BUG_ON(sizeof(cgrp->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002242 if (strlen(buffer) >= PATH_MAX)
2243 return -EINVAL;
Paul Menagee788e062008-07-25 01:46:59 -07002244 if (!cgroup_lock_live_group(cgrp))
2245 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002246 mutex_lock(&cgroup_root_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002247 strcpy(cgrp->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002248 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002249 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002250 return 0;
2251}
2252
2253static int cgroup_release_agent_show(struct cgroup *cgrp, struct cftype *cft,
2254 struct seq_file *seq)
2255{
2256 if (!cgroup_lock_live_group(cgrp))
2257 return -ENODEV;
2258 seq_puts(seq, cgrp->root->release_agent_path);
2259 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002260 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002261 return 0;
2262}
2263
Tejun Heo873fe092013-04-14 20:15:26 -07002264static int cgroup_sane_behavior_show(struct cgroup *cgrp, struct cftype *cft,
2265 struct seq_file *seq)
2266{
2267 seq_printf(seq, "%d\n", cgroup_sane_behavior(cgrp));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002268 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002269}
2270
Paul Menage84eea842008-07-25 01:47:00 -07002271/* A buffer size big enough for numbers or short strings */
2272#define CGROUP_LOCAL_BUFFER_SIZE 64
2273
Paul Menagee73d2c62008-04-29 01:00:06 -07002274static ssize_t cgroup_write_X64(struct cgroup *cgrp, struct cftype *cft,
Paul Menagef4c753b2008-04-29 00:59:56 -07002275 struct file *file,
2276 const char __user *userbuf,
2277 size_t nbytes, loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002278{
Paul Menage84eea842008-07-25 01:47:00 -07002279 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002280 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002281 char *end;
2282
2283 if (!nbytes)
2284 return -EINVAL;
2285 if (nbytes >= sizeof(buffer))
2286 return -E2BIG;
2287 if (copy_from_user(buffer, userbuf, nbytes))
2288 return -EFAULT;
2289
2290 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002291 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002292 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002293 if (*end)
2294 return -EINVAL;
2295 retval = cft->write_u64(cgrp, cft, val);
2296 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002297 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002298 if (*end)
2299 return -EINVAL;
2300 retval = cft->write_s64(cgrp, cft, val);
2301 }
Paul Menage355e0c42007-10-18 23:39:33 -07002302 if (!retval)
2303 retval = nbytes;
2304 return retval;
2305}
2306
Paul Menagedb3b1492008-07-25 01:46:58 -07002307static ssize_t cgroup_write_string(struct cgroup *cgrp, struct cftype *cft,
2308 struct file *file,
2309 const char __user *userbuf,
2310 size_t nbytes, loff_t *unused_ppos)
2311{
Paul Menage84eea842008-07-25 01:47:00 -07002312 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002313 int retval = 0;
2314 size_t max_bytes = cft->max_write_len;
2315 char *buffer = local_buffer;
2316
2317 if (!max_bytes)
2318 max_bytes = sizeof(local_buffer) - 1;
2319 if (nbytes >= max_bytes)
2320 return -E2BIG;
2321 /* Allocate a dynamic buffer if we need one */
2322 if (nbytes >= sizeof(local_buffer)) {
2323 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2324 if (buffer == NULL)
2325 return -ENOMEM;
2326 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002327 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2328 retval = -EFAULT;
2329 goto out;
2330 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002331
2332 buffer[nbytes] = 0; /* nul-terminate */
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002333 retval = cft->write_string(cgrp, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002334 if (!retval)
2335 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002336out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002337 if (buffer != local_buffer)
2338 kfree(buffer);
2339 return retval;
2340}
2341
Paul Menageddbcc7e2007-10-18 23:39:30 -07002342static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
2343 size_t nbytes, loff_t *ppos)
2344{
2345 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002346 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002347
Tejun Heo54766d42013-06-12 21:04:53 -07002348 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002349 return -ENODEV;
Paul Menage355e0c42007-10-18 23:39:33 -07002350 if (cft->write)
Paul Menagebd89aab2007-10-18 23:40:44 -07002351 return cft->write(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002352 if (cft->write_u64 || cft->write_s64)
2353 return cgroup_write_X64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002354 if (cft->write_string)
2355 return cgroup_write_string(cgrp, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002356 if (cft->trigger) {
2357 int ret = cft->trigger(cgrp, (unsigned int)cft->private);
2358 return ret ? ret : nbytes;
2359 }
Paul Menage355e0c42007-10-18 23:39:33 -07002360 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002361}
2362
Paul Menagef4c753b2008-04-29 00:59:56 -07002363static ssize_t cgroup_read_u64(struct cgroup *cgrp, struct cftype *cft,
2364 struct file *file,
2365 char __user *buf, size_t nbytes,
2366 loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367{
Paul Menage84eea842008-07-25 01:47:00 -07002368 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagef4c753b2008-04-29 00:59:56 -07002369 u64 val = cft->read_u64(cgrp, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002370 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2371
2372 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2373}
2374
Paul Menagee73d2c62008-04-29 01:00:06 -07002375static ssize_t cgroup_read_s64(struct cgroup *cgrp, struct cftype *cft,
2376 struct file *file,
2377 char __user *buf, size_t nbytes,
2378 loff_t *ppos)
2379{
Paul Menage84eea842008-07-25 01:47:00 -07002380 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagee73d2c62008-04-29 01:00:06 -07002381 s64 val = cft->read_s64(cgrp, cft);
2382 int len = sprintf(tmp, "%lld\n", (long long) val);
2383
2384 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2385}
2386
Paul Menageddbcc7e2007-10-18 23:39:30 -07002387static ssize_t cgroup_file_read(struct file *file, char __user *buf,
2388 size_t nbytes, loff_t *ppos)
2389{
2390 struct cftype *cft = __d_cft(file->f_dentry);
Paul Menagebd89aab2007-10-18 23:40:44 -07002391 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392
Tejun Heo54766d42013-06-12 21:04:53 -07002393 if (cgroup_is_dead(cgrp))
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394 return -ENODEV;
2395
2396 if (cft->read)
Paul Menagebd89aab2007-10-18 23:40:44 -07002397 return cft->read(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002398 if (cft->read_u64)
2399 return cgroup_read_u64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002400 if (cft->read_s64)
2401 return cgroup_read_s64(cgrp, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402 return -EINVAL;
2403}
2404
Paul Menage91796562008-04-29 01:00:01 -07002405/*
2406 * seqfile ops/methods for returning structured data. Currently just
2407 * supports string->u64 maps, but can be extended in future.
2408 */
2409
2410struct cgroup_seqfile_state {
2411 struct cftype *cft;
2412 struct cgroup *cgroup;
2413};
2414
2415static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2416{
2417 struct seq_file *sf = cb->state;
2418 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2419}
2420
2421static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2422{
2423 struct cgroup_seqfile_state *state = m->private;
2424 struct cftype *cft = state->cft;
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002425 if (cft->read_map) {
2426 struct cgroup_map_cb cb = {
2427 .fill = cgroup_map_add,
2428 .state = m,
2429 };
2430 return cft->read_map(state->cgroup, cft, &cb);
2431 }
2432 return cft->read_seq_string(state->cgroup, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002433}
2434
Adrian Bunk96930a62008-07-25 19:46:21 -07002435static int cgroup_seqfile_release(struct inode *inode, struct file *file)
Paul Menage91796562008-04-29 01:00:01 -07002436{
2437 struct seq_file *seq = file->private_data;
2438 kfree(seq->private);
2439 return single_release(inode, file);
2440}
2441
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002442static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002443 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002444 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002445 .llseek = seq_lseek,
2446 .release = cgroup_seqfile_release,
2447};
2448
Paul Menageddbcc7e2007-10-18 23:39:30 -07002449static int cgroup_file_open(struct inode *inode, struct file *file)
2450{
2451 int err;
2452 struct cftype *cft;
2453
2454 err = generic_file_open(inode, file);
2455 if (err)
2456 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002457 cft = __d_cft(file->f_dentry);
Li Zefan75139b82009-01-07 18:07:33 -08002458
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002459 if (cft->read_map || cft->read_seq_string) {
Tejun Heof4f4be22013-06-12 21:04:51 -07002460 struct cgroup_seqfile_state *state;
2461
2462 state = kzalloc(sizeof(*state), GFP_USER);
Paul Menage91796562008-04-29 01:00:01 -07002463 if (!state)
2464 return -ENOMEM;
Tejun Heof4f4be22013-06-12 21:04:51 -07002465
Paul Menage91796562008-04-29 01:00:01 -07002466 state->cft = cft;
2467 state->cgroup = __d_cgrp(file->f_dentry->d_parent);
2468 file->f_op = &cgroup_seqfile_operations;
2469 err = single_open(file, cgroup_seqfile_show, state);
2470 if (err < 0)
2471 kfree(state);
2472 } else if (cft->open)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002473 err = cft->open(inode, file);
2474 else
2475 err = 0;
2476
2477 return err;
2478}
2479
2480static int cgroup_file_release(struct inode *inode, struct file *file)
2481{
2482 struct cftype *cft = __d_cft(file->f_dentry);
2483 if (cft->release)
2484 return cft->release(inode, file);
2485 return 0;
2486}
2487
2488/*
2489 * cgroup_rename - Only allow simple rename of directories in place.
2490 */
2491static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2492 struct inode *new_dir, struct dentry *new_dentry)
2493{
Li Zefan65dff752013-03-01 15:01:56 +08002494 int ret;
2495 struct cgroup_name *name, *old_name;
2496 struct cgroup *cgrp;
2497
2498 /*
2499 * It's convinient to use parent dir's i_mutex to protected
2500 * cgrp->name.
2501 */
2502 lockdep_assert_held(&old_dir->i_mutex);
2503
Paul Menageddbcc7e2007-10-18 23:39:30 -07002504 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2505 return -ENOTDIR;
2506 if (new_dentry->d_inode)
2507 return -EEXIST;
2508 if (old_dir != new_dir)
2509 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002510
2511 cgrp = __d_cgrp(old_dentry);
2512
Tejun Heo6db8e852013-06-14 11:18:22 -07002513 /*
2514 * This isn't a proper migration and its usefulness is very
2515 * limited. Disallow if sane_behavior.
2516 */
2517 if (cgroup_sane_behavior(cgrp))
2518 return -EPERM;
2519
Li Zefan65dff752013-03-01 15:01:56 +08002520 name = cgroup_alloc_name(new_dentry);
2521 if (!name)
2522 return -ENOMEM;
2523
2524 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2525 if (ret) {
2526 kfree(name);
2527 return ret;
2528 }
2529
2530 old_name = cgrp->name;
2531 rcu_assign_pointer(cgrp->name, name);
2532
2533 kfree_rcu(old_name, rcu_head);
2534 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002535}
2536
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002537static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2538{
2539 if (S_ISDIR(dentry->d_inode->i_mode))
2540 return &__d_cgrp(dentry)->xattrs;
2541 else
Li Zefan712317a2013-04-18 23:09:52 -07002542 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002543}
2544
2545static inline int xattr_enabled(struct dentry *dentry)
2546{
2547 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002548 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002549}
2550
2551static bool is_valid_xattr(const char *name)
2552{
2553 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2554 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2555 return true;
2556 return false;
2557}
2558
2559static int cgroup_setxattr(struct dentry *dentry, const char *name,
2560 const void *val, size_t size, int flags)
2561{
2562 if (!xattr_enabled(dentry))
2563 return -EOPNOTSUPP;
2564 if (!is_valid_xattr(name))
2565 return -EINVAL;
2566 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2567}
2568
2569static int cgroup_removexattr(struct dentry *dentry, const char *name)
2570{
2571 if (!xattr_enabled(dentry))
2572 return -EOPNOTSUPP;
2573 if (!is_valid_xattr(name))
2574 return -EINVAL;
2575 return simple_xattr_remove(__d_xattrs(dentry), name);
2576}
2577
2578static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2579 void *buf, size_t size)
2580{
2581 if (!xattr_enabled(dentry))
2582 return -EOPNOTSUPP;
2583 if (!is_valid_xattr(name))
2584 return -EINVAL;
2585 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2586}
2587
2588static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2589{
2590 if (!xattr_enabled(dentry))
2591 return -EOPNOTSUPP;
2592 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2593}
2594
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002595static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002596 .read = cgroup_file_read,
2597 .write = cgroup_file_write,
2598 .llseek = generic_file_llseek,
2599 .open = cgroup_file_open,
2600 .release = cgroup_file_release,
2601};
2602
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002603static const struct inode_operations cgroup_file_inode_operations = {
2604 .setxattr = cgroup_setxattr,
2605 .getxattr = cgroup_getxattr,
2606 .listxattr = cgroup_listxattr,
2607 .removexattr = cgroup_removexattr,
2608};
2609
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002610static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002611 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002612 .mkdir = cgroup_mkdir,
2613 .rmdir = cgroup_rmdir,
2614 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002615 .setxattr = cgroup_setxattr,
2616 .getxattr = cgroup_getxattr,
2617 .listxattr = cgroup_listxattr,
2618 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002619};
2620
Al Viro00cd8dd2012-06-10 17:13:09 -04002621static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002622{
2623 if (dentry->d_name.len > NAME_MAX)
2624 return ERR_PTR(-ENAMETOOLONG);
2625 d_add(dentry, NULL);
2626 return NULL;
2627}
2628
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002629/*
2630 * Check if a file is a control file
2631 */
2632static inline struct cftype *__file_cft(struct file *file)
2633{
Al Viro496ad9a2013-01-23 17:07:38 -05002634 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002635 return ERR_PTR(-EINVAL);
2636 return __d_cft(file->f_dentry);
2637}
2638
Al Viroa5e7ed32011-07-26 01:55:55 -04002639static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002640 struct super_block *sb)
2641{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002642 struct inode *inode;
2643
2644 if (!dentry)
2645 return -ENOENT;
2646 if (dentry->d_inode)
2647 return -EEXIST;
2648
2649 inode = cgroup_new_inode(mode, sb);
2650 if (!inode)
2651 return -ENOMEM;
2652
2653 if (S_ISDIR(mode)) {
2654 inode->i_op = &cgroup_dir_inode_operations;
2655 inode->i_fop = &simple_dir_operations;
2656
2657 /* start off with i_nlink == 2 (for "." entry) */
2658 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002659 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660
Tejun Heob8a2df62012-11-19 08:13:37 -08002661 /*
2662 * Control reaches here with cgroup_mutex held.
2663 * @inode->i_mutex should nest outside cgroup_mutex but we
2664 * want to populate it immediately without releasing
2665 * cgroup_mutex. As @inode isn't visible to anyone else
2666 * yet, trylock will always succeed without affecting
2667 * lockdep checks.
2668 */
2669 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002670 } else if (S_ISREG(mode)) {
2671 inode->i_size = 0;
2672 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002673 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002674 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675 d_instantiate(dentry, inode);
2676 dget(dentry); /* Extra count - pin the dentry in core */
2677 return 0;
2678}
2679
Li Zefan099fca32009-04-02 16:57:29 -07002680/**
2681 * cgroup_file_mode - deduce file mode of a control file
2682 * @cft: the control file in question
2683 *
2684 * returns cft->mode if ->mode is not 0
2685 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2686 * returns S_IRUGO if it has only a read handler
2687 * returns S_IWUSR if it has only a write hander
2688 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002689static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002690{
Al Viroa5e7ed32011-07-26 01:55:55 -04002691 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002692
2693 if (cft->mode)
2694 return cft->mode;
2695
2696 if (cft->read || cft->read_u64 || cft->read_s64 ||
2697 cft->read_map || cft->read_seq_string)
2698 mode |= S_IRUGO;
2699
2700 if (cft->write || cft->write_u64 || cft->write_s64 ||
2701 cft->write_string || cft->trigger)
2702 mode |= S_IWUSR;
2703
2704 return mode;
2705}
2706
Tejun Heodb0416b2012-04-01 12:09:55 -07002707static int cgroup_add_file(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002708 struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709{
Paul Menagebd89aab2007-10-18 23:40:44 -07002710 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002711 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002713 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002714 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002715 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002717
Tejun Heo93438622013-04-14 20:15:25 -07002718 if (subsys && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002719 strcpy(name, subsys->name);
2720 strcat(name, ".");
2721 }
2722 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002723
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002725
2726 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2727 if (!cfe)
2728 return -ENOMEM;
2729
Paul Menageddbcc7e2007-10-18 23:39:30 -07002730 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002731 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002732 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002733 goto out;
2734 }
2735
Li Zefand6cbf352013-05-14 19:44:20 +08002736 cfe->type = (void *)cft;
2737 cfe->dentry = dentry;
2738 dentry->d_fsdata = cfe;
2739 simple_xattrs_init(&cfe->xattrs);
2740
Tejun Heo05ef1d72012-04-01 12:09:56 -07002741 mode = cgroup_file_mode(cft);
2742 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2743 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 list_add_tail(&cfe->node, &parent->files);
2745 cfe = NULL;
2746 }
2747 dput(dentry);
2748out:
2749 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 return error;
2751}
2752
Tejun Heo79578622012-04-01 12:09:56 -07002753static int cgroup_addrm_files(struct cgroup *cgrp, struct cgroup_subsys *subsys,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002754 struct cftype cfts[], bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002756 struct cftype *cft;
Tejun Heodb0416b2012-04-01 12:09:55 -07002757 int err, ret = 0;
2758
2759 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002760 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002761 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2762 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002763 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2764 continue;
2765 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2766 continue;
2767
Li Zefan2739d3c2013-01-21 18:18:33 +08002768 if (is_add) {
Tejun Heo79578622012-04-01 12:09:56 -07002769 err = cgroup_add_file(cgrp, subsys, cft);
Li Zefan2739d3c2013-01-21 18:18:33 +08002770 if (err)
2771 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
2772 cft->name, err);
Tejun Heodb0416b2012-04-01 12:09:55 -07002773 ret = err;
Li Zefan2739d3c2013-01-21 18:18:33 +08002774 } else {
2775 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002776 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002777 }
Tejun Heodb0416b2012-04-01 12:09:55 -07002778 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002779}
2780
Tejun Heo8e3f6542012-04-01 12:09:55 -07002781static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002782 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002783{
2784 /*
2785 * Thanks to the entanglement with vfs inode locking, we can't walk
2786 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002787 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2788 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002789 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002790 mutex_lock(&cgroup_mutex);
2791}
2792
2793static void cgroup_cfts_commit(struct cgroup_subsys *ss,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002794 struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002795 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002796{
2797 LIST_HEAD(pending);
Li Zefane8c82d22013-06-18 18:48:37 +08002798 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002799 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002800 struct dentry *prev = NULL;
2801 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002802 u64 update_before;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002803
2804 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002805 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002806 !atomic_inc_not_zero(&sb->s_active)) {
2807 mutex_unlock(&cgroup_mutex);
2808 return;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809 }
2810
Li Zefane8c82d22013-06-18 18:48:37 +08002811 /*
2812 * All cgroups which are created after we drop cgroup_mutex will
2813 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002814 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002815 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002816 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002817
Tejun Heo8e3f6542012-04-01 12:09:55 -07002818 mutex_unlock(&cgroup_mutex);
2819
Li Zefane8c82d22013-06-18 18:48:37 +08002820 /* @root always needs to be updated */
2821 inode = root->dentry->d_inode;
2822 mutex_lock(&inode->i_mutex);
2823 mutex_lock(&cgroup_mutex);
2824 cgroup_addrm_files(root, ss, cfts, is_add);
2825 mutex_unlock(&cgroup_mutex);
2826 mutex_unlock(&inode->i_mutex);
2827
2828 /* add/rm files for all cgroups created before */
2829 rcu_read_lock();
2830 cgroup_for_each_descendant_pre(cgrp, root) {
2831 if (cgroup_is_dead(cgrp))
2832 continue;
2833
2834 inode = cgrp->dentry->d_inode;
2835 dget(cgrp->dentry);
2836 rcu_read_unlock();
2837
2838 dput(prev);
2839 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840
2841 mutex_lock(&inode->i_mutex);
2842 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002843 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo79578622012-04-01 12:09:56 -07002844 cgroup_addrm_files(cgrp, ss, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002845 mutex_unlock(&cgroup_mutex);
2846 mutex_unlock(&inode->i_mutex);
2847
Li Zefane8c82d22013-06-18 18:48:37 +08002848 rcu_read_lock();
Tejun Heo8e3f6542012-04-01 12:09:55 -07002849 }
Li Zefane8c82d22013-06-18 18:48:37 +08002850 rcu_read_unlock();
2851 dput(prev);
2852 deactivate_super(sb);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853}
2854
2855/**
2856 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2857 * @ss: target cgroup subsystem
2858 * @cfts: zero-length name terminated array of cftypes
2859 *
2860 * Register @cfts to @ss. Files described by @cfts are created for all
2861 * existing cgroups to which @ss is attached and all future cgroups will
2862 * have them too. This function can be called anytime whether @ss is
2863 * attached or not.
2864 *
2865 * Returns 0 on successful registration, -errno on failure. Note that this
2866 * function currently returns 0 as long as @cfts registration is successful
2867 * even if some file creation attempts on existing cgroups fail.
2868 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002869int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002870{
2871 struct cftype_set *set;
2872
2873 set = kzalloc(sizeof(*set), GFP_KERNEL);
2874 if (!set)
2875 return -ENOMEM;
2876
2877 cgroup_cfts_prepare();
2878 set->cfts = cfts;
2879 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo79578622012-04-01 12:09:56 -07002880 cgroup_cfts_commit(ss, cfts, true);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881
2882 return 0;
2883}
2884EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2885
Li Zefana043e3b2008-02-23 15:24:09 -08002886/**
Tejun Heo79578622012-04-01 12:09:56 -07002887 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
2888 * @ss: target cgroup subsystem
2889 * @cfts: zero-length name terminated array of cftypes
2890 *
2891 * Unregister @cfts from @ss. Files described by @cfts are removed from
2892 * all existing cgroups to which @ss is attached and all future cgroups
2893 * won't have them either. This function can be called anytime whether @ss
2894 * is attached or not.
2895 *
2896 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
2897 * registered with @ss.
2898 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002899int cgroup_rm_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002900{
2901 struct cftype_set *set;
2902
2903 cgroup_cfts_prepare();
2904
2905 list_for_each_entry(set, &ss->cftsets, node) {
2906 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002907 list_del(&set->node);
2908 kfree(set);
Tejun Heo79578622012-04-01 12:09:56 -07002909 cgroup_cfts_commit(ss, cfts, false);
2910 return 0;
2911 }
2912 }
2913
2914 cgroup_cfts_commit(ss, NULL, false);
2915 return -ENOENT;
2916}
2917
2918/**
Li Zefana043e3b2008-02-23 15:24:09 -08002919 * cgroup_task_count - count the number of tasks in a cgroup.
2920 * @cgrp: the cgroup in question
2921 *
2922 * Return the number of tasks in the cgroup.
2923 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002924int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002925{
2926 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002927 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002928
Paul Menage817929e2007-10-18 23:39:36 -07002929 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002930 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2931 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002932 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002933 return count;
2934}
2935
2936/*
Paul Menage817929e2007-10-18 23:39:36 -07002937 * Advance a list_head iterator. The iterator should be positioned at
2938 * the start of a css_set
2939 */
Tejun Heo69d02062013-06-12 21:04:50 -07002940static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002941{
Tejun Heo69d02062013-06-12 21:04:50 -07002942 struct list_head *l = it->cset_link;
2943 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002944 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002945
2946 /* Advance to the next non-empty css_set */
2947 do {
2948 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002949 if (l == &cgrp->cset_links) {
2950 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002951 return;
2952 }
Tejun Heo69d02062013-06-12 21:04:50 -07002953 link = list_entry(l, struct cgrp_cset_link, cset_link);
2954 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07002955 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07002956 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07002957 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07002958}
2959
Cliff Wickman31a7df02008-02-07 00:14:42 -08002960/*
2961 * To reduce the fork() overhead for systems that are not actually
2962 * using their cgroups capability, we don't maintain the lists running
2963 * through each css_set to its tasks until we see the list actually
2964 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002965 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002966static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002967{
2968 struct task_struct *p, *g;
2969 write_lock(&css_set_lock);
2970 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002971 /*
2972 * We need tasklist_lock because RCU is not safe against
2973 * while_each_thread(). Besides, a forking task that has passed
2974 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2975 * is not guaranteed to have its child immediately visible in the
2976 * tasklist if we walk through it with RCU.
2977 */
2978 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002979 do_each_thread(g, p) {
2980 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002981 /*
2982 * We should check if the process is exiting, otherwise
2983 * it will race with cgroup_exit() in that the list
2984 * entry won't be deleted though the process has exited.
2985 */
2986 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Cliff Wickman31a7df02008-02-07 00:14:42 -08002987 list_add(&p->cg_list, &p->cgroups->tasks);
2988 task_unlock(p);
2989 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002990 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002991 write_unlock(&css_set_lock);
2992}
2993
Tejun Heo574bd9f2012-11-09 09:12:29 -08002994/**
Tejun Heo53fa5262013-05-24 10:55:38 +09002995 * cgroup_next_sibling - find the next sibling of a given cgroup
2996 * @pos: the current cgroup
2997 *
2998 * This function returns the next sibling of @pos and should be called
2999 * under RCU read lock. The only requirement is that @pos is accessible.
3000 * The next sibling is guaranteed to be returned regardless of @pos's
3001 * state.
3002 */
3003struct cgroup *cgroup_next_sibling(struct cgroup *pos)
3004{
3005 struct cgroup *next;
3006
3007 WARN_ON_ONCE(!rcu_read_lock_held());
3008
3009 /*
3010 * @pos could already have been removed. Once a cgroup is removed,
3011 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003012 * changes. As CGRP_DEAD assertion is serialized and happens
3013 * before the cgroup is taken off the ->sibling list, if we see it
3014 * unasserted, it's guaranteed that the next sibling hasn't
3015 * finished its grace period even if it's already removed, and thus
3016 * safe to dereference from this RCU critical section. If
3017 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3018 * to be visible as %true here.
Tejun Heo53fa5262013-05-24 10:55:38 +09003019 */
Tejun Heo54766d42013-06-12 21:04:53 -07003020 if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003021 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
3022 if (&next->sibling != &pos->parent->children)
3023 return next;
3024 return NULL;
3025 }
3026
3027 /*
3028 * Can't dereference the next pointer. Each cgroup is given a
3029 * monotonically increasing unique serial number and always
3030 * appended to the sibling list, so the next one can be found by
3031 * walking the parent's children until we see a cgroup with higher
3032 * serial number than @pos's.
3033 *
3034 * While this path can be slow, it's taken only when either the
3035 * current cgroup is removed or iteration and removal race.
3036 */
3037 list_for_each_entry_rcu(next, &pos->parent->children, sibling)
3038 if (next->serial_nr > pos->serial_nr)
3039 return next;
3040 return NULL;
3041}
3042EXPORT_SYMBOL_GPL(cgroup_next_sibling);
3043
3044/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003045 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3046 * @pos: the current position (%NULL to initiate traversal)
3047 * @cgroup: cgroup whose descendants to walk
3048 *
3049 * To be used by cgroup_for_each_descendant_pre(). Find the next
3050 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003051 *
3052 * While this function requires RCU read locking, it doesn't require the
3053 * whole traversal to be contained in a single RCU critical section. This
3054 * function will return the correct next descendant as long as both @pos
3055 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003056 */
3057struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3058 struct cgroup *cgroup)
3059{
3060 struct cgroup *next;
3061
3062 WARN_ON_ONCE(!rcu_read_lock_held());
3063
3064 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003065 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003066 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003067
3068 /* visit the first child if exists */
3069 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3070 if (next)
3071 return next;
3072
3073 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003074 while (pos != cgroup) {
Tejun Heo75501a62013-05-24 10:55:38 +09003075 next = cgroup_next_sibling(pos);
3076 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003077 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003078 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003079 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003080
3081 return NULL;
3082}
3083EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3084
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003085/**
3086 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3087 * @pos: cgroup of interest
3088 *
3089 * Return the rightmost descendant of @pos. If there's no descendant,
3090 * @pos is returned. This can be used during pre-order traversal to skip
3091 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003092 *
3093 * While this function requires RCU read locking, it doesn't require the
3094 * whole traversal to be contained in a single RCU critical section. This
3095 * function will return the correct rightmost descendant as long as @pos is
3096 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003097 */
3098struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3099{
3100 struct cgroup *last, *tmp;
3101
3102 WARN_ON_ONCE(!rcu_read_lock_held());
3103
3104 do {
3105 last = pos;
3106 /* ->prev isn't RCU safe, walk ->next till the end */
3107 pos = NULL;
3108 list_for_each_entry_rcu(tmp, &last->children, sibling)
3109 pos = tmp;
3110 } while (pos);
3111
3112 return last;
3113}
3114EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3115
Tejun Heo574bd9f2012-11-09 09:12:29 -08003116static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3117{
3118 struct cgroup *last;
3119
3120 do {
3121 last = pos;
3122 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3123 sibling);
3124 } while (pos);
3125
3126 return last;
3127}
3128
3129/**
3130 * cgroup_next_descendant_post - find the next descendant for post-order walk
3131 * @pos: the current position (%NULL to initiate traversal)
3132 * @cgroup: cgroup whose descendants to walk
3133 *
3134 * To be used by cgroup_for_each_descendant_post(). Find the next
3135 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003136 *
3137 * While this function requires RCU read locking, it doesn't require the
3138 * whole traversal to be contained in a single RCU critical section. This
3139 * function will return the correct next descendant as long as both @pos
3140 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003141 */
3142struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3143 struct cgroup *cgroup)
3144{
3145 struct cgroup *next;
3146
3147 WARN_ON_ONCE(!rcu_read_lock_held());
3148
3149 /* if first iteration, visit the leftmost descendant */
3150 if (!pos) {
3151 next = cgroup_leftmost_descendant(cgroup);
3152 return next != cgroup ? next : NULL;
3153 }
3154
3155 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo75501a62013-05-24 10:55:38 +09003156 next = cgroup_next_sibling(pos);
3157 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158 return cgroup_leftmost_descendant(next);
3159
3160 /* no sibling left, visit parent */
3161 next = pos->parent;
3162 return next != cgroup ? next : NULL;
3163}
3164EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3165
Paul Menagebd89aab2007-10-18 23:40:44 -07003166void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003167 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003168{
3169 /*
3170 * The first time anyone tries to iterate across a cgroup,
3171 * we need to enable the list linking each css_set to its
3172 * tasks, and fix up all existing tasks.
3173 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003174 if (!use_task_css_set_links)
3175 cgroup_enable_task_cg_lists();
3176
Paul Menage817929e2007-10-18 23:39:36 -07003177 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003178 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003179 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003180}
3181
Paul Menagebd89aab2007-10-18 23:40:44 -07003182struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003183 struct cgroup_iter *it)
3184{
3185 struct task_struct *res;
3186 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003187 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003188
3189 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003190 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003191 return NULL;
3192 res = list_entry(l, struct task_struct, cg_list);
3193 /* Advance iterator to find next entry */
3194 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003195 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3196 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003197 /* We reached the end of this task list - move on to
3198 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003199 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003200 } else {
3201 it->task = l;
3202 }
3203 return res;
3204}
3205
Paul Menagebd89aab2007-10-18 23:40:44 -07003206void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003207 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003208{
3209 read_unlock(&css_set_lock);
3210}
3211
Cliff Wickman31a7df02008-02-07 00:14:42 -08003212static inline int started_after_time(struct task_struct *t1,
3213 struct timespec *time,
3214 struct task_struct *t2)
3215{
3216 int start_diff = timespec_compare(&t1->start_time, time);
3217 if (start_diff > 0) {
3218 return 1;
3219 } else if (start_diff < 0) {
3220 return 0;
3221 } else {
3222 /*
3223 * Arbitrarily, if two processes started at the same
3224 * time, we'll say that the lower pointer value
3225 * started first. Note that t2 may have exited by now
3226 * so this may not be a valid pointer any longer, but
3227 * that's fine - it still serves to distinguish
3228 * between two tasks started (effectively) simultaneously.
3229 */
3230 return t1 > t2;
3231 }
3232}
3233
3234/*
3235 * This function is a callback from heap_insert() and is used to order
3236 * the heap.
3237 * In this case we order the heap in descending task start time.
3238 */
3239static inline int started_after(void *p1, void *p2)
3240{
3241 struct task_struct *t1 = p1;
3242 struct task_struct *t2 = p2;
3243 return started_after_time(t1, &t2->start_time, t2);
3244}
3245
3246/**
3247 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3248 * @scan: struct cgroup_scanner containing arguments for the scan
3249 *
3250 * Arguments include pointers to callback functions test_task() and
3251 * process_task().
3252 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3253 * and if it returns true, call process_task() for it also.
3254 * The test_task pointer may be NULL, meaning always true (select all tasks).
3255 * Effectively duplicates cgroup_iter_{start,next,end}()
3256 * but does not lock css_set_lock for the call to process_task().
3257 * The struct cgroup_scanner may be embedded in any structure of the caller's
3258 * creation.
3259 * It is guaranteed that process_task() will act on every task that
3260 * is a member of the cgroup for the duration of this call. This
3261 * function may or may not call process_task() for tasks that exit
3262 * or move to a different cgroup during the call, or are forked or
3263 * move into the cgroup during the call.
3264 *
3265 * Note that test_task() may be called with locks held, and may in some
3266 * situations be called multiple times for the same task, so it should
3267 * be cheap.
3268 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3269 * pre-allocated and will be used for heap operations (and its "gt" member will
3270 * be overwritten), else a temporary heap will be used (allocation of which
3271 * may cause this function to fail).
3272 */
3273int cgroup_scan_tasks(struct cgroup_scanner *scan)
3274{
3275 int retval, i;
3276 struct cgroup_iter it;
3277 struct task_struct *p, *dropped;
3278 /* Never dereference latest_task, since it's not refcounted */
3279 struct task_struct *latest_task = NULL;
3280 struct ptr_heap tmp_heap;
3281 struct ptr_heap *heap;
3282 struct timespec latest_time = { 0, 0 };
3283
3284 if (scan->heap) {
3285 /* The caller supplied our heap and pre-allocated its memory */
3286 heap = scan->heap;
3287 heap->gt = &started_after;
3288 } else {
3289 /* We need to allocate our own heap memory */
3290 heap = &tmp_heap;
3291 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3292 if (retval)
3293 /* cannot allocate the heap */
3294 return retval;
3295 }
3296
3297 again:
3298 /*
3299 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3300 * to determine which are of interest, and using the scanner's
3301 * "process_task" callback to process any of them that need an update.
3302 * Since we don't want to hold any locks during the task updates,
3303 * gather tasks to be processed in a heap structure.
3304 * The heap is sorted by descending task start time.
3305 * If the statically-sized heap fills up, we overflow tasks that
3306 * started later, and in future iterations only consider tasks that
3307 * started after the latest task in the previous pass. This
3308 * guarantees forward progress and that we don't miss any tasks.
3309 */
3310 heap->size = 0;
3311 cgroup_iter_start(scan->cg, &it);
3312 while ((p = cgroup_iter_next(scan->cg, &it))) {
3313 /*
3314 * Only affect tasks that qualify per the caller's callback,
3315 * if he provided one
3316 */
3317 if (scan->test_task && !scan->test_task(p, scan))
3318 continue;
3319 /*
3320 * Only process tasks that started after the last task
3321 * we processed
3322 */
3323 if (!started_after_time(p, &latest_time, latest_task))
3324 continue;
3325 dropped = heap_insert(heap, p);
3326 if (dropped == NULL) {
3327 /*
3328 * The new task was inserted; the heap wasn't
3329 * previously full
3330 */
3331 get_task_struct(p);
3332 } else if (dropped != p) {
3333 /*
3334 * The new task was inserted, and pushed out a
3335 * different task
3336 */
3337 get_task_struct(p);
3338 put_task_struct(dropped);
3339 }
3340 /*
3341 * Else the new task was newer than anything already in
3342 * the heap and wasn't inserted
3343 */
3344 }
3345 cgroup_iter_end(scan->cg, &it);
3346
3347 if (heap->size) {
3348 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003349 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003350 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003351 latest_time = q->start_time;
3352 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003353 }
3354 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003355 scan->process_task(q, scan);
3356 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 }
3358 /*
3359 * If we had to process any tasks at all, scan again
3360 * in case some of them were in the middle of forking
3361 * children that didn't get processed.
3362 * Not the most efficient way to do it, but it avoids
3363 * having to take callback_mutex in the fork path
3364 */
3365 goto again;
3366 }
3367 if (heap == &tmp_heap)
3368 heap_free(&tmp_heap);
3369 return 0;
3370}
3371
Tejun Heo8cc99342013-04-07 09:29:50 -07003372static void cgroup_transfer_one_task(struct task_struct *task,
3373 struct cgroup_scanner *scan)
3374{
3375 struct cgroup *new_cgroup = scan->data;
3376
Tejun Heo47cfcd02013-04-07 09:29:51 -07003377 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003378 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003379 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003380}
3381
3382/**
3383 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3384 * @to: cgroup to which the tasks will be moved
3385 * @from: cgroup in which the tasks currently reside
3386 */
3387int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3388{
3389 struct cgroup_scanner scan;
3390
3391 scan.cg = from;
3392 scan.test_task = NULL; /* select all tasks in cgroup */
3393 scan.process_task = cgroup_transfer_one_task;
3394 scan.heap = NULL;
3395 scan.data = to;
3396
3397 return cgroup_scan_tasks(&scan);
3398}
3399
Paul Menage817929e2007-10-18 23:39:36 -07003400/*
Ben Blum102a7752009-09-23 15:56:26 -07003401 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003402 *
3403 * Reading this file can return large amounts of data if a cgroup has
3404 * *lots* of attached tasks. So it may need several calls to read(),
3405 * but we cannot guarantee that the information we produce is correct
3406 * unless we produce it entirely atomically.
3407 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003408 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003409
Li Zefan24528252012-01-20 11:58:43 +08003410/* which pidlist file are we talking about? */
3411enum cgroup_filetype {
3412 CGROUP_FILE_PROCS,
3413 CGROUP_FILE_TASKS,
3414};
3415
3416/*
3417 * A pidlist is a list of pids that virtually represents the contents of one
3418 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3419 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3420 * to the cgroup.
3421 */
3422struct cgroup_pidlist {
3423 /*
3424 * used to find which pidlist is wanted. doesn't change as long as
3425 * this particular list stays in the list.
3426 */
3427 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3428 /* array of xids */
3429 pid_t *list;
3430 /* how many elements the above list has */
3431 int length;
3432 /* how many files are using the current array */
3433 int use_count;
3434 /* each of these stored in a list by its cgroup */
3435 struct list_head links;
3436 /* pointer to the cgroup we belong to, for list removal purposes */
3437 struct cgroup *owner;
3438 /* protects the other fields */
3439 struct rw_semaphore mutex;
3440};
3441
Paul Menagebbcb81d2007-10-18 23:39:32 -07003442/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003443 * The following two functions "fix" the issue where there are more pids
3444 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3445 * TODO: replace with a kernel-wide solution to this problem
3446 */
3447#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3448static void *pidlist_allocate(int count)
3449{
3450 if (PIDLIST_TOO_LARGE(count))
3451 return vmalloc(count * sizeof(pid_t));
3452 else
3453 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3454}
3455static void pidlist_free(void *p)
3456{
3457 if (is_vmalloc_addr(p))
3458 vfree(p);
3459 else
3460 kfree(p);
3461}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003462
3463/*
Ben Blum102a7752009-09-23 15:56:26 -07003464 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003465 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003466 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003467static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003468{
Ben Blum102a7752009-09-23 15:56:26 -07003469 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003470
3471 /*
3472 * we presume the 0th element is unique, so i starts at 1. trivial
3473 * edge cases first; no work needs to be done for either
3474 */
3475 if (length == 0 || length == 1)
3476 return length;
3477 /* src and dest walk down the list; dest counts unique elements */
3478 for (src = 1; src < length; src++) {
3479 /* find next unique element */
3480 while (list[src] == list[src-1]) {
3481 src++;
3482 if (src == length)
3483 goto after;
3484 }
3485 /* dest always points to where the next unique element goes */
3486 list[dest] = list[src];
3487 dest++;
3488 }
3489after:
Ben Blum102a7752009-09-23 15:56:26 -07003490 return dest;
3491}
3492
3493static int cmppid(const void *a, const void *b)
3494{
3495 return *(pid_t *)a - *(pid_t *)b;
3496}
3497
3498/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003499 * find the appropriate pidlist for our purpose (given procs vs tasks)
3500 * returns with the lock on that pidlist already held, and takes care
3501 * of the use count, or returns NULL with no locks held if we're out of
3502 * memory.
3503 */
3504static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3505 enum cgroup_filetype type)
3506{
3507 struct cgroup_pidlist *l;
3508 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003509 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003510
Ben Blum72a8cb32009-09-23 15:56:27 -07003511 /*
3512 * We can't drop the pidlist_mutex before taking the l->mutex in case
3513 * the last ref-holder is trying to remove l from the list at the same
3514 * time. Holding the pidlist_mutex precludes somebody taking whichever
3515 * list we find out from under us - compare release_pid_array().
3516 */
3517 mutex_lock(&cgrp->pidlist_mutex);
3518 list_for_each_entry(l, &cgrp->pidlists, links) {
3519 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003520 /* make sure l doesn't vanish out from under us */
3521 down_write(&l->mutex);
3522 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003523 return l;
3524 }
3525 }
3526 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003527 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003528 if (!l) {
3529 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003530 return l;
3531 }
3532 init_rwsem(&l->mutex);
3533 down_write(&l->mutex);
3534 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003535 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003536 l->owner = cgrp;
3537 list_add(&l->links, &cgrp->pidlists);
3538 mutex_unlock(&cgrp->pidlist_mutex);
3539 return l;
3540}
3541
3542/*
Ben Blum102a7752009-09-23 15:56:26 -07003543 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3544 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003545static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3546 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003547{
3548 pid_t *array;
3549 int length;
3550 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003551 struct cgroup_iter it;
3552 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003553 struct cgroup_pidlist *l;
3554
3555 /*
3556 * If cgroup gets more users after we read count, we won't have
3557 * enough space - tough. This race is indistinguishable to the
3558 * caller from the case that the additional cgroup users didn't
3559 * show up until sometime later on.
3560 */
3561 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003562 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003563 if (!array)
3564 return -ENOMEM;
3565 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003566 cgroup_iter_start(cgrp, &it);
3567 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003568 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003569 break;
Ben Blum102a7752009-09-23 15:56:26 -07003570 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003571 if (type == CGROUP_FILE_PROCS)
3572 pid = task_tgid_vnr(tsk);
3573 else
3574 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003575 if (pid > 0) /* make sure to only use valid results */
3576 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003577 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003578 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003579 length = n;
3580 /* now sort & (if procs) strip out duplicates */
3581 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003583 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003584 l = cgroup_pidlist_find(cgrp, type);
3585 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003586 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003587 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003588 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003589 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003590 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003591 l->list = array;
3592 l->length = length;
3593 l->use_count++;
3594 up_write(&l->mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003595 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003596 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003597}
3598
Balbir Singh846c7bb2007-10-18 23:39:44 -07003599/**
Li Zefana043e3b2008-02-23 15:24:09 -08003600 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003601 * @stats: cgroupstats to fill information into
3602 * @dentry: A dentry entry belonging to the cgroup for which stats have
3603 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003604 *
3605 * Build and fill cgroupstats so that taskstats can export it to user
3606 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003607 */
3608int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3609{
3610 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003611 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003612 struct cgroup_iter it;
3613 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003614
Balbir Singh846c7bb2007-10-18 23:39:44 -07003615 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003616 * Validate dentry by checking the superblock operations,
3617 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003618 */
Li Zefan33d283b2008-11-19 15:36:48 -08003619 if (dentry->d_sb->s_op != &cgroup_ops ||
3620 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003621 goto err;
3622
3623 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003624 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003625
Paul Menagebd89aab2007-10-18 23:40:44 -07003626 cgroup_iter_start(cgrp, &it);
3627 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003628 switch (tsk->state) {
3629 case TASK_RUNNING:
3630 stats->nr_running++;
3631 break;
3632 case TASK_INTERRUPTIBLE:
3633 stats->nr_sleeping++;
3634 break;
3635 case TASK_UNINTERRUPTIBLE:
3636 stats->nr_uninterruptible++;
3637 break;
3638 case TASK_STOPPED:
3639 stats->nr_stopped++;
3640 break;
3641 default:
3642 if (delayacct_is_task_waiting_on_io(tsk))
3643 stats->nr_io_wait++;
3644 break;
3645 }
3646 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003647 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003648
Balbir Singh846c7bb2007-10-18 23:39:44 -07003649err:
3650 return ret;
3651}
3652
Paul Menage8f3ff202009-09-23 15:56:25 -07003653
Paul Menagecc31edc2008-10-18 20:28:04 -07003654/*
Ben Blum102a7752009-09-23 15:56:26 -07003655 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003656 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003657 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003658 */
3659
Ben Blum102a7752009-09-23 15:56:26 -07003660static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003661{
3662 /*
3663 * Initially we receive a position value that corresponds to
3664 * one more than the last pid shown (or 0 on the first call or
3665 * after a seek to the start). Use a binary-search to find the
3666 * next pid to display, if any
3667 */
Ben Blum102a7752009-09-23 15:56:26 -07003668 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003669 int index = 0, pid = *pos;
3670 int *iter;
3671
Ben Blum102a7752009-09-23 15:56:26 -07003672 down_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003673 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003674 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003675
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 while (index < end) {
3677 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003678 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003679 index = mid;
3680 break;
Ben Blum102a7752009-09-23 15:56:26 -07003681 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003682 index = mid + 1;
3683 else
3684 end = mid;
3685 }
3686 }
3687 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003688 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 return NULL;
3690 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003691 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003692 *pos = *iter;
3693 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003694}
3695
Ben Blum102a7752009-09-23 15:56:26 -07003696static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003697{
Ben Blum102a7752009-09-23 15:56:26 -07003698 struct cgroup_pidlist *l = s->private;
3699 up_read(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003700}
3701
Ben Blum102a7752009-09-23 15:56:26 -07003702static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003703{
Ben Blum102a7752009-09-23 15:56:26 -07003704 struct cgroup_pidlist *l = s->private;
3705 pid_t *p = v;
3706 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003707 /*
3708 * Advance to the next pid in the array. If this goes off the
3709 * end, we're done
3710 */
3711 p++;
3712 if (p >= end) {
3713 return NULL;
3714 } else {
3715 *pos = *p;
3716 return p;
3717 }
3718}
3719
Ben Blum102a7752009-09-23 15:56:26 -07003720static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003721{
3722 return seq_printf(s, "%d\n", *(int *)v);
3723}
3724
Ben Blum102a7752009-09-23 15:56:26 -07003725/*
3726 * seq_operations functions for iterating on pidlists through seq_file -
3727 * independent of whether it's tasks or procs
3728 */
3729static const struct seq_operations cgroup_pidlist_seq_operations = {
3730 .start = cgroup_pidlist_start,
3731 .stop = cgroup_pidlist_stop,
3732 .next = cgroup_pidlist_next,
3733 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003734};
3735
Ben Blum102a7752009-09-23 15:56:26 -07003736static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003737{
Ben Blum72a8cb32009-09-23 15:56:27 -07003738 /*
3739 * the case where we're the last user of this particular pidlist will
3740 * have us remove it from the cgroup's list, which entails taking the
3741 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3742 * pidlist_mutex, we have to take pidlist_mutex first.
3743 */
3744 mutex_lock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003745 down_write(&l->mutex);
3746 BUG_ON(!l->use_count);
3747 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003748 /* we're the last user if refcount is 0; remove and free */
3749 list_del(&l->links);
3750 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003751 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003752 put_pid_ns(l->key.ns);
3753 up_write(&l->mutex);
3754 kfree(l);
3755 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003756 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003757 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blum102a7752009-09-23 15:56:26 -07003758 up_write(&l->mutex);
Paul Menagecc31edc2008-10-18 20:28:04 -07003759}
3760
Ben Blum102a7752009-09-23 15:56:26 -07003761static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003762{
Ben Blum102a7752009-09-23 15:56:26 -07003763 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003764 if (!(file->f_mode & FMODE_READ))
3765 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003766 /*
3767 * the seq_file will only be initialized if the file was opened for
3768 * reading; hence we check if it's not null only in that case.
3769 */
3770 l = ((struct seq_file *)file->private_data)->private;
3771 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003772 return seq_release(inode, file);
3773}
3774
Ben Blum102a7752009-09-23 15:56:26 -07003775static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 .read = seq_read,
3777 .llseek = seq_lseek,
3778 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003779 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003780};
3781
3782/*
Ben Blum102a7752009-09-23 15:56:26 -07003783 * The following functions handle opens on a file that displays a pidlist
3784 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3785 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003786 */
Ben Blum102a7752009-09-23 15:56:26 -07003787/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003788static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003789{
3790 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003791 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003792 int retval;
3793
3794 /* Nothing to do for write-only files */
3795 if (!(file->f_mode & FMODE_READ))
3796 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003797
Ben Blum102a7752009-09-23 15:56:26 -07003798 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003799 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003800 if (retval)
3801 return retval;
3802 /* configure file information */
3803 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003804
Ben Blum102a7752009-09-23 15:56:26 -07003805 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003806 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003807 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003809 }
Ben Blum102a7752009-09-23 15:56:26 -07003810 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003811 return 0;
3812}
Ben Blum102a7752009-09-23 15:56:26 -07003813static int cgroup_tasks_open(struct inode *unused, struct file *file)
3814{
Ben Blum72a8cb32009-09-23 15:56:27 -07003815 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003816}
3817static int cgroup_procs_open(struct inode *unused, struct file *file)
3818{
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003820}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003821
Paul Menagebd89aab2007-10-18 23:40:44 -07003822static u64 cgroup_read_notify_on_release(struct cgroup *cgrp,
Paul Menage81a6a5c2007-10-18 23:39:38 -07003823 struct cftype *cft)
3824{
Paul Menagebd89aab2007-10-18 23:40:44 -07003825 return notify_on_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003826}
3827
Paul Menage6379c102008-07-25 01:47:01 -07003828static int cgroup_write_notify_on_release(struct cgroup *cgrp,
3829 struct cftype *cft,
3830 u64 val)
3831{
3832 clear_bit(CGRP_RELEASABLE, &cgrp->flags);
3833 if (val)
3834 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3835 else
3836 clear_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
3837 return 0;
3838}
3839
Paul Menagebbcb81d2007-10-18 23:39:32 -07003840/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003841 * When dput() is called asynchronously, if umount has been done and
3842 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3843 * there's a small window that vfs will see the root dentry with non-zero
3844 * refcnt and trigger BUG().
3845 *
3846 * That's why we hold a reference before dput() and drop it right after.
3847 */
3848static void cgroup_dput(struct cgroup *cgrp)
3849{
3850 struct super_block *sb = cgrp->root->sb;
3851
3852 atomic_inc(&sb->s_active);
3853 dput(cgrp->dentry);
3854 deactivate_super(sb);
3855}
3856
3857/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003858 * Unregister event and free resources.
3859 *
3860 * Gets called from workqueue.
3861 */
3862static void cgroup_event_remove(struct work_struct *work)
3863{
3864 struct cgroup_event *event = container_of(work, struct cgroup_event,
3865 remove);
3866 struct cgroup *cgrp = event->cgrp;
3867
Li Zefan810cbee2013-02-18 18:56:14 +08003868 remove_wait_queue(event->wqh, &event->wait);
3869
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003870 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3871
Li Zefan810cbee2013-02-18 18:56:14 +08003872 /* Notify userspace the event is going away. */
3873 eventfd_signal(event->eventfd, 1);
3874
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003875 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003876 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003877 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003878}
3879
3880/*
3881 * Gets called on POLLHUP on eventfd when user closes it.
3882 *
3883 * Called with wqh->lock held and interrupts disabled.
3884 */
3885static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3886 int sync, void *key)
3887{
3888 struct cgroup_event *event = container_of(wait,
3889 struct cgroup_event, wait);
3890 struct cgroup *cgrp = event->cgrp;
3891 unsigned long flags = (unsigned long)key;
3892
3893 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003894 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003895 * If the event has been detached at cgroup removal, we
3896 * can simply return knowing the other side will cleanup
3897 * for us.
3898 *
3899 * We can't race against event freeing since the other
3900 * side will require wqh->lock via remove_wait_queue(),
3901 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003902 */
Li Zefan810cbee2013-02-18 18:56:14 +08003903 spin_lock(&cgrp->event_list_lock);
3904 if (!list_empty(&event->list)) {
3905 list_del_init(&event->list);
3906 /*
3907 * We are in atomic context, but cgroup_event_remove()
3908 * may sleep, so we have to call it in workqueue.
3909 */
3910 schedule_work(&event->remove);
3911 }
3912 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003913 }
3914
3915 return 0;
3916}
3917
3918static void cgroup_event_ptable_queue_proc(struct file *file,
3919 wait_queue_head_t *wqh, poll_table *pt)
3920{
3921 struct cgroup_event *event = container_of(pt,
3922 struct cgroup_event, pt);
3923
3924 event->wqh = wqh;
3925 add_wait_queue(wqh, &event->wait);
3926}
3927
3928/*
3929 * Parse input and register new cgroup event handler.
3930 *
3931 * Input must be in format '<event_fd> <control_fd> <args>'.
3932 * Interpretation of args is defined by control file implementation.
3933 */
3934static int cgroup_write_event_control(struct cgroup *cgrp, struct cftype *cft,
3935 const char *buffer)
3936{
3937 struct cgroup_event *event = NULL;
Li Zefanf1690072013-02-18 14:13:35 +08003938 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003939 unsigned int efd, cfd;
3940 struct file *efile = NULL;
3941 struct file *cfile = NULL;
3942 char *endp;
3943 int ret;
3944
3945 efd = simple_strtoul(buffer, &endp, 10);
3946 if (*endp != ' ')
3947 return -EINVAL;
3948 buffer = endp + 1;
3949
3950 cfd = simple_strtoul(buffer, &endp, 10);
3951 if ((*endp != ' ') && (*endp != '\0'))
3952 return -EINVAL;
3953 buffer = endp + 1;
3954
3955 event = kzalloc(sizeof(*event), GFP_KERNEL);
3956 if (!event)
3957 return -ENOMEM;
3958 event->cgrp = cgrp;
3959 INIT_LIST_HEAD(&event->list);
3960 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
3961 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
3962 INIT_WORK(&event->remove, cgroup_event_remove);
3963
3964 efile = eventfd_fget(efd);
3965 if (IS_ERR(efile)) {
3966 ret = PTR_ERR(efile);
3967 goto fail;
3968 }
3969
3970 event->eventfd = eventfd_ctx_fileget(efile);
3971 if (IS_ERR(event->eventfd)) {
3972 ret = PTR_ERR(event->eventfd);
3973 goto fail;
3974 }
3975
3976 cfile = fget(cfd);
3977 if (!cfile) {
3978 ret = -EBADF;
3979 goto fail;
3980 }
3981
3982 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04003983 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05003984 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 if (ret < 0)
3986 goto fail;
3987
3988 event->cft = __file_cft(cfile);
3989 if (IS_ERR(event->cft)) {
3990 ret = PTR_ERR(event->cft);
3991 goto fail;
3992 }
3993
Li Zefanf1690072013-02-18 14:13:35 +08003994 /*
3995 * The file to be monitored must be in the same cgroup as
3996 * cgroup.event_control is.
3997 */
3998 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
3999 if (cgrp_cfile != cgrp) {
4000 ret = -EINVAL;
4001 goto fail;
4002 }
4003
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004004 if (!event->cft->register_event || !event->cft->unregister_event) {
4005 ret = -EINVAL;
4006 goto fail;
4007 }
4008
4009 ret = event->cft->register_event(cgrp, event->cft,
4010 event->eventfd, buffer);
4011 if (ret)
4012 goto fail;
4013
Li Zefan7ef70e42013-04-26 11:58:03 -07004014 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004015
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004016 /*
4017 * Events should be removed after rmdir of cgroup directory, but before
4018 * destroying subsystem state objects. Let's take reference to cgroup
4019 * directory dentry to do that.
4020 */
4021 dget(cgrp->dentry);
4022
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004023 spin_lock(&cgrp->event_list_lock);
4024 list_add(&event->list, &cgrp->event_list);
4025 spin_unlock(&cgrp->event_list_lock);
4026
4027 fput(cfile);
4028 fput(efile);
4029
4030 return 0;
4031
4032fail:
4033 if (cfile)
4034 fput(cfile);
4035
4036 if (event && event->eventfd && !IS_ERR(event->eventfd))
4037 eventfd_ctx_put(event->eventfd);
4038
4039 if (!IS_ERR_OR_NULL(efile))
4040 fput(efile);
4041
4042 kfree(event);
4043
4044 return ret;
4045}
4046
Daniel Lezcano97978e62010-10-27 15:33:35 -07004047static u64 cgroup_clone_children_read(struct cgroup *cgrp,
4048 struct cftype *cft)
4049{
Tejun Heo2260e7f2012-11-19 08:13:38 -08004050 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004051}
4052
4053static int cgroup_clone_children_write(struct cgroup *cgrp,
4054 struct cftype *cft,
4055 u64 val)
4056{
4057 if (val)
Tejun Heo2260e7f2012-11-19 08:13:38 -08004058 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004059 else
Tejun Heo2260e7f2012-11-19 08:13:38 -08004060 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004061 return 0;
4062}
4063
Tejun Heod5c56ce2013-06-03 19:14:34 -07004064static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004065 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004066 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004067 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004068 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004069 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004070 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004071 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004072 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004073 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004074 .write_string = cgroup_write_event_control,
4075 .mode = S_IWUGO,
4076 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004077 {
4078 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004079 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004080 .read_u64 = cgroup_clone_children_read,
4081 .write_u64 = cgroup_clone_children_write,
4082 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004083 {
Tejun Heo873fe092013-04-14 20:15:26 -07004084 .name = "cgroup.sane_behavior",
4085 .flags = CFTYPE_ONLY_ON_ROOT,
4086 .read_seq_string = cgroup_sane_behavior_show,
4087 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004088
4089 /*
4090 * Historical crazy stuff. These don't have "cgroup." prefix and
4091 * don't exist if sane_behavior. If you're depending on these, be
4092 * prepared to be burned.
4093 */
4094 {
4095 .name = "tasks",
4096 .flags = CFTYPE_INSANE, /* use "procs" instead */
4097 .open = cgroup_tasks_open,
4098 .write_u64 = cgroup_tasks_write,
4099 .release = cgroup_pidlist_release,
4100 .mode = S_IRUGO | S_IWUSR,
4101 },
4102 {
4103 .name = "notify_on_release",
4104 .flags = CFTYPE_INSANE,
4105 .read_u64 = cgroup_read_notify_on_release,
4106 .write_u64 = cgroup_write_notify_on_release,
4107 },
Tejun Heo873fe092013-04-14 20:15:26 -07004108 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004109 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004110 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004111 .read_seq_string = cgroup_release_agent_show,
4112 .write_string = cgroup_release_agent_write,
4113 .max_write_len = PATH_MAX,
4114 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004115 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004116};
4117
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004118/**
4119 * cgroup_populate_dir - selectively creation of files in a directory
4120 * @cgrp: target cgroup
4121 * @base_files: true if the base files should be added
4122 * @subsys_mask: mask of the subsystem ids whose files should be added
4123 */
4124static int cgroup_populate_dir(struct cgroup *cgrp, bool base_files,
4125 unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004126{
4127 int err;
4128 struct cgroup_subsys *ss;
4129
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004130 if (base_files) {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004131 err = cgroup_addrm_files(cgrp, NULL, cgroup_base_files, true);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004132 if (err < 0)
4133 return err;
4134 }
Paul Menagebbcb81d2007-10-18 23:39:32 -07004135
Tejun Heo8e3f6542012-04-01 12:09:55 -07004136 /* process cftsets of each subsystem */
Tejun Heo5549c492013-06-24 15:21:48 -07004137 for_each_root_subsys(cgrp->root, ss) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004138 struct cftype_set *set;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004139 if (!test_bit(ss->subsys_id, &subsys_mask))
4140 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004141
Tejun Heodb0416b2012-04-01 12:09:55 -07004142 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo79578622012-04-01 12:09:56 -07004143 cgroup_addrm_files(cgrp, ss, set->cfts, true);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004144 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004145
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004146 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004147 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004148 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4149 /*
4150 * Update id->css pointer and make this css visible from
4151 * CSS ID functions. This pointer will be dereferened
4152 * from RCU-read-side without locks.
4153 */
4154 if (css->id)
4155 rcu_assign_pointer(css->id->css, css);
4156 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157
4158 return 0;
4159}
4160
Tejun Heo48ddbe12012-04-01 12:09:56 -07004161static void css_dput_fn(struct work_struct *work)
4162{
4163 struct cgroup_subsys_state *css =
4164 container_of(work, struct cgroup_subsys_state, dput_work);
4165
Li Zefan1c8158e2013-06-18 18:41:10 +08004166 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004167}
4168
Tejun Heod3daf282013-06-13 19:39:16 -07004169static void css_release(struct percpu_ref *ref)
4170{
4171 struct cgroup_subsys_state *css =
4172 container_of(ref, struct cgroup_subsys_state, refcnt);
4173
4174 schedule_work(&css->dput_work);
4175}
4176
Paul Menageddbcc7e2007-10-18 23:39:30 -07004177static void init_cgroup_css(struct cgroup_subsys_state *css,
4178 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004179 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004180{
Paul Menagebd89aab2007-10-18 23:40:44 -07004181 css->cgroup = cgrp;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004182 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004183 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004184 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004185 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004186 BUG_ON(cgrp->subsys[ss->subsys_id]);
4187 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004188
4189 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004190 * css holds an extra ref to @cgrp->dentry which is put on the last
4191 * css_put(). dput() requires process context, which css_put() may
4192 * be called without. @css->dput_work will be used to invoke
4193 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004194 */
4195 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004196}
4197
Tejun Heob1929db2012-11-19 08:13:38 -08004198/* invoke ->post_create() on a new CSS and mark it online if successful */
4199static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004200{
Tejun Heob1929db2012-11-19 08:13:38 -08004201 int ret = 0;
4202
Tejun Heoa31f2d32012-11-19 08:13:37 -08004203 lockdep_assert_held(&cgroup_mutex);
4204
Tejun Heo92fb9742012-11-19 08:13:38 -08004205 if (ss->css_online)
4206 ret = ss->css_online(cgrp);
Tejun Heob1929db2012-11-19 08:13:38 -08004207 if (!ret)
4208 cgrp->subsys[ss->subsys_id]->flags |= CSS_ONLINE;
4209 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004210}
4211
4212/* if the CSS is online, invoke ->pre_destory() on it and mark it offline */
4213static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
4214 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
4215{
4216 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4217
4218 lockdep_assert_held(&cgroup_mutex);
4219
4220 if (!(css->flags & CSS_ONLINE))
4221 return;
4222
Li Zefand7eeac12013-03-12 15:35:59 -07004223 if (ss->css_offline)
Tejun Heo92fb9742012-11-19 08:13:38 -08004224 ss->css_offline(cgrp);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004225
4226 cgrp->subsys[ss->subsys_id]->flags &= ~CSS_ONLINE;
4227}
4228
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229/*
Li Zefana043e3b2008-02-23 15:24:09 -08004230 * cgroup_create - create a cgroup
4231 * @parent: cgroup that will be parent of the new cgroup
4232 * @dentry: dentry of the new cgroup
4233 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234 *
Li Zefana043e3b2008-02-23 15:24:09 -08004235 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004238 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239{
Paul Menagebd89aab2007-10-18 23:40:44 -07004240 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004241 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242 struct cgroupfs_root *root = parent->root;
4243 int err = 0;
4244 struct cgroup_subsys *ss;
4245 struct super_block *sb = root->sb;
4246
Tejun Heo0a950f62012-11-19 09:02:12 -08004247 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004248 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4249 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004250 return -ENOMEM;
4251
Li Zefan65dff752013-03-01 15:01:56 +08004252 name = cgroup_alloc_name(dentry);
4253 if (!name)
4254 goto err_free_cgrp;
4255 rcu_assign_pointer(cgrp->name, name);
4256
Tejun Heo0a950f62012-11-19 09:02:12 -08004257 cgrp->id = ida_simple_get(&root->cgroup_ida, 1, 0, GFP_KERNEL);
4258 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004259 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004260
Tejun Heo976c06b2012-11-05 09:16:59 -08004261 /*
4262 * Only live parents can have children. Note that the liveliness
4263 * check isn't strictly necessary because cgroup_mkdir() and
4264 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4265 * anyway so that locking is contained inside cgroup proper and we
4266 * don't get nasty surprises if we ever grow another caller.
4267 */
4268 if (!cgroup_lock_live_group(parent)) {
4269 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004270 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004271 }
4272
Paul Menageddbcc7e2007-10-18 23:39:30 -07004273 /* Grab a reference on the superblock so the hierarchy doesn't
4274 * get deleted on unmount if there are child cgroups. This
4275 * can be done outside cgroup_mutex, since the sb can't
4276 * disappear while someone has an open control file on the
4277 * fs */
4278 atomic_inc(&sb->s_active);
4279
Paul Menagecc31edc2008-10-18 20:28:04 -07004280 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281
Li Zefanfe1c06c2013-01-24 14:30:22 +08004282 dentry->d_fsdata = cgrp;
4283 cgrp->dentry = dentry;
4284
Paul Menagebd89aab2007-10-18 23:40:44 -07004285 cgrp->parent = parent;
4286 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004287
Li Zefanb6abdb02008-03-04 14:28:19 -08004288 if (notify_on_release(parent))
4289 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4290
Tejun Heo2260e7f2012-11-19 08:13:38 -08004291 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4292 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004293
Tejun Heo5549c492013-06-24 15:21:48 -07004294 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004295 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004296
Tejun Heo92fb9742012-11-19 08:13:38 -08004297 css = ss->css_alloc(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004298 if (IS_ERR(css)) {
4299 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004300 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004301 }
Tejun Heod3daf282013-06-13 19:39:16 -07004302
4303 err = percpu_ref_init(&css->refcnt, css_release);
4304 if (err)
4305 goto err_free_all;
4306
Paul Menagebd89aab2007-10-18 23:40:44 -07004307 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004308
Li Zefan4528fd02010-02-02 13:44:10 -08004309 if (ss->use_id) {
4310 err = alloc_css_id(ss, parent, cgrp);
4311 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004312 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004313 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004314 }
4315
Tejun Heo4e139af2012-11-19 08:13:36 -08004316 /*
4317 * Create directory. cgroup_create_file() returns with the new
4318 * directory locked on success so that it can be populated without
4319 * dropping cgroup_mutex.
4320 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004321 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004322 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004323 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004324 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004325
Tejun Heo00356bd2013-06-18 11:14:22 -07004326 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004327
Tejun Heo4e139af2012-11-19 08:13:36 -08004328 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004329 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4330 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004331
Tejun Heob1929db2012-11-19 08:13:38 -08004332 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004333 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004334 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004335
Li Zefan415cf072013-04-08 14:35:02 +08004336 /* hold a ref to the parent's dentry */
4337 dget(parent->dentry);
4338
Tejun Heob1929db2012-11-19 08:13:38 -08004339 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004340 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004341 err = online_css(ss, cgrp);
4342 if (err)
4343 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004344
4345 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4346 parent->parent) {
4347 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",
4348 current->comm, current->pid, ss->name);
4349 if (!strcmp(ss->name, "memory"))
4350 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4351 ss->warned_broken_hierarchy = true;
4352 }
Tejun Heoa8638032012-11-09 09:12:29 -08004353 }
4354
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04004355 err = cgroup_populate_dir(cgrp, true, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004356 if (err)
4357 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004358
4359 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004360 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004361
4362 return 0;
4363
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004364err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004365 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004366 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4367
4368 if (css) {
4369 percpu_ref_cancel_init(&css->refcnt);
Tejun Heo92fb9742012-11-19 08:13:38 -08004370 ss->css_free(cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004371 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004372 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 /* Release the reference count that we took on the superblock */
4375 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004376err_free_id:
4377 ida_simple_remove(&root->cgroup_ida, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004378err_free_name:
4379 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004380err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004381 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004383
4384err_destroy:
4385 cgroup_destroy_locked(cgrp);
4386 mutex_unlock(&cgroup_mutex);
4387 mutex_unlock(&dentry->d_inode->i_mutex);
4388 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389}
4390
Al Viro18bb1db2011-07-26 01:41:39 -04004391static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392{
4393 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4394
4395 /* the vfs holds inode->i_mutex already */
4396 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4397}
4398
Tejun Heod3daf282013-06-13 19:39:16 -07004399static void cgroup_css_killed(struct cgroup *cgrp)
4400{
4401 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4402 return;
4403
4404 /* percpu ref's of all css's are killed, kick off the next step */
4405 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4406 schedule_work(&cgrp->destroy_work);
4407}
4408
4409static void css_ref_killed_fn(struct percpu_ref *ref)
4410{
4411 struct cgroup_subsys_state *css =
4412 container_of(ref, struct cgroup_subsys_state, refcnt);
4413
4414 cgroup_css_killed(css->cgroup);
4415}
4416
4417/**
4418 * cgroup_destroy_locked - the first stage of cgroup destruction
4419 * @cgrp: cgroup to be destroyed
4420 *
4421 * css's make use of percpu refcnts whose killing latency shouldn't be
4422 * exposed to userland and are RCU protected. Also, cgroup core needs to
4423 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4424 * invoked. To satisfy all the requirements, destruction is implemented in
4425 * the following two steps.
4426 *
4427 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4428 * userland visible parts and start killing the percpu refcnts of
4429 * css's. Set up so that the next stage will be kicked off once all
4430 * the percpu refcnts are confirmed to be killed.
4431 *
4432 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4433 * rest of destruction. Once all cgroup references are gone, the
4434 * cgroup is RCU-freed.
4435 *
4436 * This function implements s1. After this step, @cgrp is gone as far as
4437 * the userland is concerned and a new cgroup with the same name may be
4438 * created. As cgroup doesn't care about the names internally, this
4439 * doesn't cause any problem.
4440 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004441static int cgroup_destroy_locked(struct cgroup *cgrp)
4442 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004443{
Tejun Heo42809dd2012-11-19 08:13:37 -08004444 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004445 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004446 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004447 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004448
Tejun Heo42809dd2012-11-19 08:13:37 -08004449 lockdep_assert_held(&d->d_inode->i_mutex);
4450 lockdep_assert_held(&cgroup_mutex);
4451
Tejun Heoddd69142013-06-12 21:04:54 -07004452 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004453 * css_set_lock synchronizes access to ->cset_links and prevents
4454 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004455 */
4456 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004457 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004458 read_unlock(&css_set_lock);
4459 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004461
Tejun Heo1a90dd52012-11-05 09:16:59 -08004462 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004463 * Block new css_tryget() by killing css refcnts. cgroup core
4464 * guarantees that, by the time ->css_offline() is invoked, no new
4465 * css reference will be given out via css_tryget(). We can't
4466 * simply call percpu_ref_kill() and proceed to offlining css's
4467 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4468 * as killed on all CPUs on return.
4469 *
4470 * Use percpu_ref_kill_and_confirm() to get notifications as each
4471 * css is confirmed to be seen as killed on all CPUs. The
4472 * notification callback keeps track of the number of css's to be
4473 * killed and schedules cgroup_offline_fn() to perform the rest of
4474 * destruction once the percpu refs of all css's are confirmed to
4475 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004476 */
Tejun Heod3daf282013-06-13 19:39:16 -07004477 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004478 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004479 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4480
Tejun Heod3daf282013-06-13 19:39:16 -07004481 /*
4482 * Killing would put the base ref, but we need to keep it
4483 * alive until after ->css_offline.
4484 */
4485 percpu_ref_get(&css->refcnt);
4486
4487 atomic_inc(&cgrp->css_kill_cnt);
4488 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004489 }
Tejun Heod3daf282013-06-13 19:39:16 -07004490 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004491
4492 /*
4493 * Mark @cgrp dead. This prevents further task migration and child
4494 * creation by disabling cgroup_lock_live_group(). Note that
4495 * CGRP_DEAD assertion is depended upon by cgroup_next_sibling() to
4496 * resume iteration after dropping RCU read lock. See
4497 * cgroup_next_sibling() for details.
4498 */
Tejun Heo54766d42013-06-12 21:04:53 -07004499 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004500
Tejun Heo455050d2013-06-13 19:27:41 -07004501 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4502 raw_spin_lock(&release_list_lock);
4503 if (!list_empty(&cgrp->release_list))
4504 list_del_init(&cgrp->release_list);
4505 raw_spin_unlock(&release_list_lock);
4506
4507 /*
4508 * Remove @cgrp directory. The removal puts the base ref but we
4509 * aren't quite done with @cgrp yet, so hold onto it.
4510 */
4511 dget(d);
4512 cgroup_d_remove_dir(d);
4513
4514 /*
4515 * Unregister events and notify userspace.
4516 * Notify userspace about cgroup removing only after rmdir of cgroup
4517 * directory to avoid race between userspace and kernelspace.
4518 */
4519 spin_lock(&cgrp->event_list_lock);
4520 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4521 list_del_init(&event->list);
4522 schedule_work(&event->remove);
4523 }
4524 spin_unlock(&cgrp->event_list_lock);
4525
Tejun Heoea15f8c2013-06-13 19:27:42 -07004526 return 0;
4527};
4528
Tejun Heod3daf282013-06-13 19:39:16 -07004529/**
4530 * cgroup_offline_fn - the second step of cgroup destruction
4531 * @work: cgroup->destroy_free_work
4532 *
4533 * This function is invoked from a work item for a cgroup which is being
4534 * destroyed after the percpu refcnts of all css's are guaranteed to be
4535 * seen as killed on all CPUs, and performs the rest of destruction. This
4536 * is the second step of destruction described in the comment above
4537 * cgroup_destroy_locked().
4538 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004539static void cgroup_offline_fn(struct work_struct *work)
4540{
4541 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4542 struct cgroup *parent = cgrp->parent;
4543 struct dentry *d = cgrp->dentry;
4544 struct cgroup_subsys *ss;
4545
4546 mutex_lock(&cgroup_mutex);
4547
Tejun Heod3daf282013-06-13 19:39:16 -07004548 /*
4549 * css_tryget() is guaranteed to fail now. Tell subsystems to
4550 * initate destruction.
4551 */
Tejun Heo5549c492013-06-24 15:21:48 -07004552 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004553 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004554
4555 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004556 * Put the css refs from cgroup_destroy_locked(). Each css holds
4557 * an extra reference to the cgroup's dentry and cgroup removal
4558 * proceeds regardless of css refs. On the last put of each css,
4559 * whenever that may be, the extra dentry ref is put so that dentry
4560 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004561 */
Tejun Heo5549c492013-06-24 15:21:48 -07004562 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004563 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004564
Paul Menage999cd8a2009-01-07 18:08:36 -08004565 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004566 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004567
Paul Menageddbcc7e2007-10-18 23:39:30 -07004568 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569
Paul Menagebd89aab2007-10-18 23:40:44 -07004570 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004571 check_for_release(parent);
4572
Tejun Heoea15f8c2013-06-13 19:27:42 -07004573 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574}
4575
Tejun Heo42809dd2012-11-19 08:13:37 -08004576static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4577{
4578 int ret;
4579
4580 mutex_lock(&cgroup_mutex);
4581 ret = cgroup_destroy_locked(dentry->d_fsdata);
4582 mutex_unlock(&cgroup_mutex);
4583
4584 return ret;
4585}
4586
Tejun Heo8e3f6542012-04-01 12:09:55 -07004587static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4588{
4589 INIT_LIST_HEAD(&ss->cftsets);
4590
4591 /*
4592 * base_cftset is embedded in subsys itself, no need to worry about
4593 * deregistration.
4594 */
4595 if (ss->base_cftypes) {
4596 ss->base_cftset.cfts = ss->base_cftypes;
4597 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4598 }
4599}
4600
Li Zefan06a11922008-04-29 01:00:07 -07004601static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004602{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004603 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004604
4605 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004606
Tejun Heo648bb562012-11-19 08:13:36 -08004607 mutex_lock(&cgroup_mutex);
4608
Tejun Heo8e3f6542012-04-01 12:09:55 -07004609 /* init base cftset */
4610 cgroup_init_cftsets(ss);
4611
Paul Menageddbcc7e2007-10-18 23:39:30 -07004612 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004613 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4614 ss->root = &cgroup_dummy_root;
4615 css = ss->css_alloc(cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004616 /* We don't handle early failures gracefully */
4617 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004618 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004619
Li Zefane8d55fd2008-04-29 01:00:13 -07004620 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004621 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004622 * newly registered, all tasks and hence the
4623 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004624 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004625
4626 need_forkexit_callback |= ss->fork || ss->exit;
4627
Li Zefane8d55fd2008-04-29 01:00:13 -07004628 /* At system boot, before all subsystems have been
4629 * registered, no tasks have been forked, so we don't
4630 * need to invoke fork callbacks here. */
4631 BUG_ON(!list_empty(&init_task.tasks));
4632
Tejun Heo9871bf92013-06-24 15:21:47 -07004633 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004634
Tejun Heo648bb562012-11-19 08:13:36 -08004635 mutex_unlock(&cgroup_mutex);
4636
Ben Blume6a11052010-03-10 15:22:09 -08004637 /* this function shouldn't be used with modular subsystems, since they
4638 * need to register a subsys_id, among other things */
4639 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004640}
4641
4642/**
Ben Blume6a11052010-03-10 15:22:09 -08004643 * cgroup_load_subsys: load and register a modular subsystem at runtime
4644 * @ss: the subsystem to load
4645 *
4646 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004647 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004648 * up for use. If the subsystem is built-in anyway, work is delegated to the
4649 * simpler cgroup_init_subsys.
4650 */
4651int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4652{
Ben Blume6a11052010-03-10 15:22:09 -08004653 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004654 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004655 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004656 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004657 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004658
4659 /* check name and function validity */
4660 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004661 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004662 return -EINVAL;
4663
4664 /*
4665 * we don't support callbacks in modular subsystems. this check is
4666 * before the ss->module check for consistency; a subsystem that could
4667 * be a module should still have no callbacks even if the user isn't
4668 * compiling it as one.
4669 */
4670 if (ss->fork || ss->exit)
4671 return -EINVAL;
4672
4673 /*
4674 * an optionally modular subsystem is built-in: we want to do nothing,
4675 * since cgroup_init_subsys will have already taken care of it.
4676 */
4677 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004678 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004679 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004680 return 0;
4681 }
4682
Tejun Heo8e3f6542012-04-01 12:09:55 -07004683 /* init base cftset */
4684 cgroup_init_cftsets(ss);
4685
Ben Blume6a11052010-03-10 15:22:09 -08004686 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004687 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004688
4689 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004690 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004691 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004692 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004693 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004694 css = ss->css_alloc(cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004695 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004696 /* failure case - need to deassign the cgroup_subsys[] slot. */
4697 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004698 mutex_unlock(&cgroup_mutex);
4699 return PTR_ERR(css);
4700 }
4701
Tejun Heo9871bf92013-06-24 15:21:47 -07004702 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4703 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004704
4705 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004706 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004707 /* init_idr must be after init_cgroup_css because it sets css->id. */
4708 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004709 ret = cgroup_init_idr(ss, css);
4710 if (ret)
4711 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004712 }
4713
4714 /*
4715 * Now we need to entangle the css into the existing css_sets. unlike
4716 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4717 * will need a new pointer to it; done by iterating the css_set_table.
4718 * furthermore, modifying the existing css_sets will corrupt the hash
4719 * table state, so each changed css_set will need its hash recomputed.
4720 * this is all done under the css_set_lock.
4721 */
4722 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004723 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004724 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004725 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004726 continue;
4727 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004728 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004729 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004730 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004731 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004732 key = css_set_hash(cset->subsys);
4733 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004734 }
4735 write_unlock(&css_set_lock);
4736
Tejun Heo9871bf92013-06-24 15:21:47 -07004737 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004738 if (ret)
4739 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004740
Ben Blume6a11052010-03-10 15:22:09 -08004741 /* success! */
4742 mutex_unlock(&cgroup_mutex);
4743 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004744
4745err_unload:
4746 mutex_unlock(&cgroup_mutex);
4747 /* @ss can't be mounted here as try_module_get() would fail */
4748 cgroup_unload_subsys(ss);
4749 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004750}
4751EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4752
4753/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004754 * cgroup_unload_subsys: unload a modular subsystem
4755 * @ss: the subsystem to unload
4756 *
4757 * This function should be called in a modular subsystem's exitcall. When this
4758 * function is invoked, the refcount on the subsystem's module will be 0, so
4759 * the subsystem will not be attached to any hierarchy.
4760 */
4761void cgroup_unload_subsys(struct cgroup_subsys *ss)
4762{
Tejun Heo69d02062013-06-12 21:04:50 -07004763 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004764
4765 BUG_ON(ss->module == NULL);
4766
4767 /*
4768 * we shouldn't be called if the subsystem is in use, and the use of
4769 * try_module_get in parse_cgroupfs_options should ensure that it
4770 * doesn't start being used while we're killing it off.
4771 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004772 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004773
4774 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004775
Tejun Heo9871bf92013-06-24 15:21:47 -07004776 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004777
Tejun Heoc897ff62013-02-27 17:03:49 -08004778 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004779 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004780
Ben Blumcf5d5942010-03-10 15:22:09 -08004781 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004782 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004783
Tejun Heo9871bf92013-06-24 15:21:47 -07004784 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004785 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004786
4787 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004788 * disentangle the css from all css_sets attached to the dummy
4789 * top. as in loading, we need to pay our respects to the hashtable
4790 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004791 */
4792 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004793 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004794 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004795 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004796
Tejun Heo5abb8852013-06-12 21:04:49 -07004797 hash_del(&cset->hlist);
4798 cset->subsys[ss->subsys_id] = NULL;
4799 key = css_set_hash(cset->subsys);
4800 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004801 }
4802 write_unlock(&css_set_lock);
4803
4804 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004805 * remove subsystem's css from the cgroup_dummy_top and free it -
4806 * need to free before marking as null because ss->css_free needs
4807 * the cgrp->subsys pointer to find their state. note that this
4808 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004809 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004810 ss->css_free(cgroup_dummy_top);
4811 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004812
4813 mutex_unlock(&cgroup_mutex);
4814}
4815EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4816
4817/**
Li Zefana043e3b2008-02-23 15:24:09 -08004818 * cgroup_init_early - cgroup initialization at system boot
4819 *
4820 * Initialize cgroups at system boot, and initialize any
4821 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004822 */
4823int __init cgroup_init_early(void)
4824{
4825 int i;
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004826 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004827 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004828 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004829 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004830 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004831 init_cgroup_root(&cgroup_dummy_root);
4832 cgroup_root_count = 1;
Paul Menage817929e2007-10-18 23:39:36 -07004833 init_task.cgroups = &init_css_set;
4834
Tejun Heo69d02062013-06-12 21:04:50 -07004835 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004836 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4837 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004838 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004840 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004841 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menageddbcc7e2007-10-18 23:39:30 -07004842
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004843 /* at bootup time, we don't worry about modular subsystems */
4844 if (!ss || ss->module)
4845 continue;
4846
Paul Menageddbcc7e2007-10-18 23:39:30 -07004847 BUG_ON(!ss->name);
4848 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004849 BUG_ON(!ss->css_alloc);
4850 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004851 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004852 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004853 ss->name, ss->subsys_id);
4854 BUG();
4855 }
4856
4857 if (ss->early_init)
4858 cgroup_init_subsys(ss);
4859 }
4860 return 0;
4861}
4862
4863/**
Li Zefana043e3b2008-02-23 15:24:09 -08004864 * cgroup_init - cgroup initialization
4865 *
4866 * Register cgroup filesystem and /proc file, and initialize
4867 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004868 */
4869int __init cgroup_init(void)
4870{
4871 int err;
4872 int i;
Li Zefan0ac801f2013-01-10 11:49:27 +08004873 unsigned long key;
Paul Menagea4243162007-10-18 23:39:35 -07004874
4875 err = bdi_init(&cgroup_backing_dev_info);
4876 if (err)
4877 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004878
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004879 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004880 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004881
4882 /* at bootup time, we don't worry about modular subsystems */
4883 if (!ss || ss->module)
4884 continue;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004885 if (!ss->early_init)
4886 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004887 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004888 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004889 }
4890
Li Zefan472b1052008-04-29 01:00:11 -07004891 /* Add init_css_set to the hash table */
Li Zefan0ac801f2013-01-10 11:49:27 +08004892 key = css_set_hash(init_css_set.subsys);
4893 hash_add(css_set_table, &init_css_set.hlist, key);
Tejun Heofa3ca072013-04-14 11:36:56 -07004894
4895 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004896 mutex_lock(&cgroup_mutex);
4897 mutex_lock(&cgroup_root_mutex);
4898
Tejun Heo9871bf92013-06-24 15:21:47 -07004899 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root));
Greg KH676db4a2010-08-05 13:53:35 -07004900
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004901 mutex_unlock(&cgroup_root_mutex);
4902 mutex_unlock(&cgroup_mutex);
4903
Greg KH676db4a2010-08-05 13:53:35 -07004904 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4905 if (!cgroup_kobj) {
4906 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004907 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004908 }
4909
4910 err = register_filesystem(&cgroup_fs_type);
4911 if (err < 0) {
4912 kobject_put(cgroup_kobj);
4913 goto out;
4914 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004915
Li Zefan46ae2202008-04-29 01:00:08 -07004916 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004917
Paul Menageddbcc7e2007-10-18 23:39:30 -07004918out:
Paul Menagea4243162007-10-18 23:39:35 -07004919 if (err)
4920 bdi_destroy(&cgroup_backing_dev_info);
4921
Paul Menageddbcc7e2007-10-18 23:39:30 -07004922 return err;
4923}
Paul Menageb4f48b62007-10-18 23:39:33 -07004924
Paul Menagea4243162007-10-18 23:39:35 -07004925/*
4926 * proc_cgroup_show()
4927 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4928 * - Used for /proc/<pid>/cgroup.
4929 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4930 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004931 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004932 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4933 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4934 * cgroup to top_cgroup.
4935 */
4936
4937/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004938int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004939{
4940 struct pid *pid;
4941 struct task_struct *tsk;
4942 char *buf;
4943 int retval;
4944 struct cgroupfs_root *root;
4945
4946 retval = -ENOMEM;
4947 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4948 if (!buf)
4949 goto out;
4950
4951 retval = -ESRCH;
4952 pid = m->private;
4953 tsk = get_pid_task(pid, PIDTYPE_PID);
4954 if (!tsk)
4955 goto out_free;
4956
4957 retval = 0;
4958
4959 mutex_lock(&cgroup_mutex);
4960
Li Zefane5f6a862009-01-07 18:07:41 -08004961 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004962 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004963 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004964 int count = 0;
4965
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004966 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004967 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004968 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004969 if (strlen(root->name))
4970 seq_printf(m, "%sname=%s", count ? "," : "",
4971 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004972 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004973 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004974 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004975 if (retval < 0)
4976 goto out_unlock;
4977 seq_puts(m, buf);
4978 seq_putc(m, '\n');
4979 }
4980
4981out_unlock:
4982 mutex_unlock(&cgroup_mutex);
4983 put_task_struct(tsk);
4984out_free:
4985 kfree(buf);
4986out:
4987 return retval;
4988}
4989
Paul Menagea4243162007-10-18 23:39:35 -07004990/* Display information about each subsystem and each hierarchy */
4991static int proc_cgroupstats_show(struct seq_file *m, void *v)
4992{
4993 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004994
Paul Menage8bab8dd2008-04-04 14:29:57 -07004995 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004996 /*
4997 * ideally we don't want subsystems moving around while we do this.
4998 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4999 * subsys/hierarchy state.
5000 */
Paul Menagea4243162007-10-18 23:39:35 -07005001 mutex_lock(&cgroup_mutex);
Paul Menagea4243162007-10-18 23:39:35 -07005002 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005003 struct cgroup_subsys *ss = cgroup_subsys[i];
Ben Blumaae8aab2010-03-10 15:22:07 -08005004 if (ss == NULL)
5005 continue;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005006 seq_printf(m, "%s\t%d\t%d\t%d\n",
5007 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005008 ss->root->number_of_cgroups, !ss->disabled);
Paul Menagea4243162007-10-18 23:39:35 -07005009 }
5010 mutex_unlock(&cgroup_mutex);
5011 return 0;
5012}
5013
5014static int cgroupstats_open(struct inode *inode, struct file *file)
5015{
Al Viro9dce07f2008-03-29 03:07:28 +00005016 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005017}
5018
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005019static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005020 .open = cgroupstats_open,
5021 .read = seq_read,
5022 .llseek = seq_lseek,
5023 .release = single_release,
5024};
5025
Paul Menageb4f48b62007-10-18 23:39:33 -07005026/**
5027 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005028 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005029 *
5030 * Description: A task inherits its parent's cgroup at fork().
5031 *
5032 * A pointer to the shared css_set was automatically copied in
5033 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005034 * it was not made under the protection of RCU or cgroup_mutex, so
5035 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5036 * have already changed current->cgroups, allowing the previously
5037 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005038 *
5039 * At the point that cgroup_fork() is called, 'current' is the parent
5040 * task, and the passed argument 'child' points to the child task.
5041 */
5042void cgroup_fork(struct task_struct *child)
5043{
Tejun Heo9bb71302012-10-18 17:52:07 -07005044 task_lock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005045 child->cgroups = current->cgroups;
5046 get_css_set(child->cgroups);
Tejun Heo9bb71302012-10-18 17:52:07 -07005047 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005048 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005049}
5050
5051/**
Li Zefana043e3b2008-02-23 15:24:09 -08005052 * cgroup_post_fork - called on a new task after adding it to the task list
5053 * @child: the task in question
5054 *
Tejun Heo5edee612012-10-16 15:03:14 -07005055 * Adds the task to the list running through its css_set if necessary and
5056 * call the subsystem fork() callbacks. Has to be after the task is
5057 * visible on the task list in case we race with the first call to
5058 * cgroup_iter_start() - to guarantee that the new task ends up on its
5059 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005060 */
Paul Menage817929e2007-10-18 23:39:36 -07005061void cgroup_post_fork(struct task_struct *child)
5062{
Tejun Heo5edee612012-10-16 15:03:14 -07005063 int i;
5064
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005065 /*
5066 * use_task_css_set_links is set to 1 before we walk the tasklist
5067 * under the tasklist_lock and we read it here after we added the child
5068 * to the tasklist under the tasklist_lock as well. If the child wasn't
5069 * yet in the tasklist when we walked through it from
5070 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5071 * should be visible now due to the paired locking and barriers implied
5072 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5073 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5074 * lock on fork.
5075 */
Paul Menage817929e2007-10-18 23:39:36 -07005076 if (use_task_css_set_links) {
5077 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005078 task_lock(child);
5079 if (list_empty(&child->cg_list))
Paul Menage817929e2007-10-18 23:39:36 -07005080 list_add(&child->cg_list, &child->cgroups->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005081 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005082 write_unlock(&css_set_lock);
5083 }
Tejun Heo5edee612012-10-16 15:03:14 -07005084
5085 /*
5086 * Call ss->fork(). This must happen after @child is linked on
5087 * css_set; otherwise, @child might change state between ->fork()
5088 * and addition to css_set.
5089 */
5090 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005091 /*
5092 * fork/exit callbacks are supported only for builtin
5093 * subsystems, and the builtin section of the subsys
5094 * array is immutable, so we don't need to lock the
5095 * subsys array here. On the other hand, modular section
5096 * of the array can be freed at module unload, so we
5097 * can't touch that.
5098 */
5099 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005100 struct cgroup_subsys *ss = cgroup_subsys[i];
Tejun Heo5edee612012-10-16 15:03:14 -07005101
Tejun Heo5edee612012-10-16 15:03:14 -07005102 if (ss->fork)
5103 ss->fork(child);
5104 }
5105 }
Paul Menage817929e2007-10-18 23:39:36 -07005106}
Tejun Heo5edee612012-10-16 15:03:14 -07005107
Paul Menage817929e2007-10-18 23:39:36 -07005108/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005109 * cgroup_exit - detach cgroup from exiting task
5110 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005111 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005112 *
5113 * Description: Detach cgroup from @tsk and release it.
5114 *
5115 * Note that cgroups marked notify_on_release force every task in
5116 * them to take the global cgroup_mutex mutex when exiting.
5117 * This could impact scaling on very large systems. Be reluctant to
5118 * use notify_on_release cgroups where very high task exit scaling
5119 * is required on large systems.
5120 *
5121 * the_top_cgroup_hack:
5122 *
5123 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5124 *
5125 * We call cgroup_exit() while the task is still competent to
5126 * handle notify_on_release(), then leave the task attached to the
5127 * root cgroup in each hierarchy for the remainder of its exit.
5128 *
5129 * To do this properly, we would increment the reference count on
5130 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5131 * code we would add a second cgroup function call, to drop that
5132 * reference. This would just create an unnecessary hot spot on
5133 * the top_cgroup reference count, to no avail.
5134 *
5135 * Normally, holding a reference to a cgroup without bumping its
5136 * count is unsafe. The cgroup could go away, or someone could
5137 * attach us to a different cgroup, decrementing the count on
5138 * the first cgroup that we never incremented. But in this case,
5139 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005140 * which wards off any cgroup_attach_task() attempts, or task is a failed
5141 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005142 */
5143void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5144{
Tejun Heo5abb8852013-06-12 21:04:49 -07005145 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005146 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005147
5148 /*
5149 * Unlink from the css_set task list if necessary.
5150 * Optimistically check cg_list before taking
5151 * css_set_lock
5152 */
5153 if (!list_empty(&tsk->cg_list)) {
5154 write_lock(&css_set_lock);
5155 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005156 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005157 write_unlock(&css_set_lock);
5158 }
5159
Paul Menageb4f48b62007-10-18 23:39:33 -07005160 /* Reassign the task to the init_css_set. */
5161 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07005162 cset = tsk->cgroups;
Paul Menage817929e2007-10-18 23:39:36 -07005163 tsk->cgroups = &init_css_set;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005164
5165 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005166 /*
5167 * fork/exit callbacks are supported only for builtin
5168 * subsystems, see cgroup_post_fork() for details.
5169 */
5170 for (i = 0; i < CGROUP_BUILTIN_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005171 struct cgroup_subsys *ss = cgroup_subsys[i];
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005172
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005173 if (ss->exit) {
5174 struct cgroup *old_cgrp =
Tejun Heo5abb8852013-06-12 21:04:49 -07005175 rcu_dereference_raw(cset->subsys[i])->cgroup;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005176 struct cgroup *cgrp = task_cgroup(tsk, i);
Li Zefan761b3ef2012-01-31 13:47:36 +08005177 ss->exit(cgrp, old_cgrp, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005178 }
5179 }
5180 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005181 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005182
Tejun Heo5abb8852013-06-12 21:04:49 -07005183 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005184}
Paul Menage697f4162007-10-18 23:39:34 -07005185
Paul Menagebd89aab2007-10-18 23:40:44 -07005186static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005187{
Li Zefanf50daa72013-03-01 15:06:07 +08005188 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005189 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005190 /*
5191 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005192 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005193 * it now
5194 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005195 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005196
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005197 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005198 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005199 list_empty(&cgrp->release_list)) {
5200 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005201 need_schedule_work = 1;
5202 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005203 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005204 if (need_schedule_work)
5205 schedule_work(&release_agent_work);
5206 }
5207}
5208
Paul Menage81a6a5c2007-10-18 23:39:38 -07005209/*
5210 * Notify userspace when a cgroup is released, by running the
5211 * configured release agent with the name of the cgroup (path
5212 * relative to the root of cgroup file system) as the argument.
5213 *
5214 * Most likely, this user command will try to rmdir this cgroup.
5215 *
5216 * This races with the possibility that some other task will be
5217 * attached to this cgroup before it is removed, or that some other
5218 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5219 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5220 * unused, and this cgroup will be reprieved from its death sentence,
5221 * to continue to serve a useful existence. Next time it's released,
5222 * we will get notified again, if it still has 'notify_on_release' set.
5223 *
5224 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5225 * means only wait until the task is successfully execve()'d. The
5226 * separate release agent task is forked by call_usermodehelper(),
5227 * then control in this thread returns here, without waiting for the
5228 * release agent task. We don't bother to wait because the caller of
5229 * this routine has no use for the exit status of the release agent
5230 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005231 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005232static void cgroup_release_agent(struct work_struct *work)
5233{
5234 BUG_ON(work != &release_agent_work);
5235 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005236 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005237 while (!list_empty(&release_list)) {
5238 char *argv[3], *envp[3];
5239 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005240 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005241 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005242 struct cgroup,
5243 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005244 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005245 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005246 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005247 if (!pathbuf)
5248 goto continue_free;
5249 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5250 goto continue_free;
5251 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5252 if (!agentbuf)
5253 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005254
5255 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005256 argv[i++] = agentbuf;
5257 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005258 argv[i] = NULL;
5259
5260 i = 0;
5261 /* minimal command environment */
5262 envp[i++] = "HOME=/";
5263 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5264 envp[i] = NULL;
5265
5266 /* Drop the lock while we invoke the usermode helper,
5267 * since the exec could involve hitting disk and hence
5268 * be a slow process */
5269 mutex_unlock(&cgroup_mutex);
5270 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005271 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005272 continue_free:
5273 kfree(pathbuf);
5274 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005275 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005276 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005277 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005278 mutex_unlock(&cgroup_mutex);
5279}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005280
5281static int __init cgroup_disable(char *str)
5282{
5283 int i;
5284 char *token;
5285
5286 while ((token = strsep(&str, ",")) != NULL) {
5287 if (!*token)
5288 continue;
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005289 for (i = 0; i < CGROUP_SUBSYS_COUNT; i++) {
Tejun Heo9871bf92013-06-24 15:21:47 -07005290 struct cgroup_subsys *ss = cgroup_subsys[i];
Paul Menage8bab8dd2008-04-04 14:29:57 -07005291
Daniel Wagnerbe45c902012-09-13 09:50:55 +02005292 /*
5293 * cgroup_disable, being at boot time, can't
5294 * know about module subsystems, so we don't
5295 * worry about them.
5296 */
5297 if (!ss || ss->module)
5298 continue;
5299
Paul Menage8bab8dd2008-04-04 14:29:57 -07005300 if (!strcmp(token, ss->name)) {
5301 ss->disabled = 1;
5302 printk(KERN_INFO "Disabling %s control group"
5303 " subsystem\n", ss->name);
5304 break;
5305 }
5306 }
5307 }
5308 return 1;
5309}
5310__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005311
5312/*
5313 * Functons for CSS ID.
5314 */
5315
Tejun Heo54766d42013-06-12 21:04:53 -07005316/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005317unsigned short css_id(struct cgroup_subsys_state *css)
5318{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005319 struct css_id *cssid;
5320
5321 /*
5322 * This css_id() can return correct value when somone has refcnt
5323 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5324 * it's unchanged until freed.
5325 */
Tejun Heod3daf282013-06-13 19:39:16 -07005326 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005327
5328 if (cssid)
5329 return cssid->id;
5330 return 0;
5331}
Ben Blum67523c42010-03-10 15:22:11 -08005332EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005333
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005334/**
5335 * css_is_ancestor - test "root" css is an ancestor of "child"
5336 * @child: the css to be tested.
5337 * @root: the css supporsed to be an ancestor of the child.
5338 *
5339 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005340 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005341 * But, considering usual usage, the csses should be valid objects after test.
5342 * Assuming that the caller will do some action to the child if this returns
5343 * returns true, the caller must take "child";s reference count.
5344 * If "child" is valid object and this returns true, "root" is valid, too.
5345 */
5346
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005347bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005348 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005349{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005350 struct css_id *child_id;
5351 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005352
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005353 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005354 if (!child_id)
5355 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005356 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005357 if (!root_id)
5358 return false;
5359 if (child_id->depth < root_id->depth)
5360 return false;
5361 if (child_id->stack[root_id->depth] != root_id->id)
5362 return false;
5363 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005364}
5365
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005366void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5367{
5368 struct css_id *id = css->id;
5369 /* When this is called before css_id initialization, id can be NULL */
5370 if (!id)
5371 return;
5372
5373 BUG_ON(!ss->use_id);
5374
5375 rcu_assign_pointer(id->css, NULL);
5376 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005377 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005379 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005380 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005381}
Ben Blum67523c42010-03-10 15:22:11 -08005382EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005383
5384/*
5385 * This is called by init or create(). Then, calls to this function are
5386 * always serialized (By cgroup_mutex() at create()).
5387 */
5388
5389static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5390{
5391 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005392 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005393
5394 BUG_ON(!ss->use_id);
5395
5396 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5397 newid = kzalloc(size, GFP_KERNEL);
5398 if (!newid)
5399 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005400
5401 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005402 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005403 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005404 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005405 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005406 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005407
5408 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005409 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005411
Tejun Heod228d9e2013-02-27 17:04:54 -08005412 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413 newid->depth = depth;
5414 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005415err_out:
5416 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005417 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005418
5419}
5420
Ben Blume6a11052010-03-10 15:22:09 -08005421static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5422 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005423{
5424 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005425
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005426 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005427 idr_init(&ss->idr);
5428
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005429 newid = get_new_cssid(ss, 0);
5430 if (IS_ERR(newid))
5431 return PTR_ERR(newid);
5432
5433 newid->stack[0] = newid->id;
5434 newid->css = rootcss;
5435 rootcss->id = newid;
5436 return 0;
5437}
5438
5439static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5440 struct cgroup *child)
5441{
5442 int subsys_id, i, depth = 0;
5443 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005444 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005445
5446 subsys_id = ss->subsys_id;
5447 parent_css = parent->subsys[subsys_id];
5448 child_css = child->subsys[subsys_id];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005449 parent_id = parent_css->id;
Greg Thelen94b3dd02010-06-04 14:15:03 -07005450 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451
5452 child_id = get_new_cssid(ss, depth);
5453 if (IS_ERR(child_id))
5454 return PTR_ERR(child_id);
5455
5456 for (i = 0; i < depth; i++)
5457 child_id->stack[i] = parent_id->stack[i];
5458 child_id->stack[depth] = child_id->id;
5459 /*
5460 * child_id->css pointer will be set after this cgroup is available
5461 * see cgroup_populate_dir()
5462 */
5463 rcu_assign_pointer(child_css->id, child_id);
5464
5465 return 0;
5466}
5467
5468/**
5469 * css_lookup - lookup css by id
5470 * @ss: cgroup subsys to be looked into.
5471 * @id: the id
5472 *
5473 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5474 * NULL if not. Should be called under rcu_read_lock()
5475 */
5476struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5477{
5478 struct css_id *cssid = NULL;
5479
5480 BUG_ON(!ss->use_id);
5481 cssid = idr_find(&ss->idr, id);
5482
5483 if (unlikely(!cssid))
5484 return NULL;
5485
5486 return rcu_dereference(cssid->css);
5487}
Ben Blum67523c42010-03-10 15:22:11 -08005488EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005489
Stephane Eraniane5d13672011-02-14 11:20:01 +02005490/*
5491 * get corresponding css from file open on cgroupfs directory
5492 */
5493struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5494{
5495 struct cgroup *cgrp;
5496 struct inode *inode;
5497 struct cgroup_subsys_state *css;
5498
Al Viro496ad9a2013-01-23 17:07:38 -05005499 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005500 /* check in cgroup filesystem dir */
5501 if (inode->i_op != &cgroup_dir_inode_operations)
5502 return ERR_PTR(-EBADF);
5503
5504 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5505 return ERR_PTR(-EINVAL);
5506
5507 /* get cgroup */
5508 cgrp = __d_cgrp(f->f_dentry);
5509 css = cgrp->subsys[id];
5510 return css ? css : ERR_PTR(-ENOENT);
5511}
5512
Paul Menagefe693432009-09-23 15:56:20 -07005513#ifdef CONFIG_CGROUP_DEBUG
Li Zefan03c78cb2013-06-14 11:17:19 +08005514static struct cgroup_subsys_state *debug_css_alloc(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005515{
5516 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5517
5518 if (!css)
5519 return ERR_PTR(-ENOMEM);
5520
5521 return css;
5522}
5523
Li Zefan03c78cb2013-06-14 11:17:19 +08005524static void debug_css_free(struct cgroup *cgrp)
Paul Menagefe693432009-09-23 15:56:20 -07005525{
Li Zefan03c78cb2013-06-14 11:17:19 +08005526 kfree(cgrp->subsys[debug_subsys_id]);
Paul Menagefe693432009-09-23 15:56:20 -07005527}
5528
Li Zefan03c78cb2013-06-14 11:17:19 +08005529static u64 debug_taskcount_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005530{
Li Zefan03c78cb2013-06-14 11:17:19 +08005531 return cgroup_task_count(cgrp);
Paul Menagefe693432009-09-23 15:56:20 -07005532}
5533
Li Zefan03c78cb2013-06-14 11:17:19 +08005534static u64 current_css_set_read(struct cgroup *cgrp, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005535{
5536 return (u64)(unsigned long)current->cgroups;
5537}
5538
Li Zefan03c78cb2013-06-14 11:17:19 +08005539static u64 current_css_set_refcount_read(struct cgroup *cgrp,
5540 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005541{
5542 u64 count;
5543
5544 rcu_read_lock();
5545 count = atomic_read(&current->cgroups->refcount);
5546 rcu_read_unlock();
5547 return count;
5548}
5549
Li Zefan03c78cb2013-06-14 11:17:19 +08005550static int current_css_set_cg_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005551 struct cftype *cft,
5552 struct seq_file *seq)
5553{
Tejun Heo69d02062013-06-12 21:04:50 -07005554 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005555 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005556
5557 read_lock(&css_set_lock);
5558 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005559 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005560 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005561 struct cgroup *c = link->cgrp;
5562 const char *name;
5563
5564 if (c->dentry)
5565 name = c->dentry->d_name.name;
5566 else
5567 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005568 seq_printf(seq, "Root %d group %s\n",
5569 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005570 }
5571 rcu_read_unlock();
5572 read_unlock(&css_set_lock);
5573 return 0;
5574}
5575
5576#define MAX_TASKS_SHOWN_PER_CSS 25
Li Zefan03c78cb2013-06-14 11:17:19 +08005577static int cgroup_css_links_read(struct cgroup *cgrp,
Paul Menage7717f7b2009-09-23 15:56:22 -07005578 struct cftype *cft,
5579 struct seq_file *seq)
5580{
Tejun Heo69d02062013-06-12 21:04:50 -07005581 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005582
5583 read_lock(&css_set_lock);
Li Zefan03c78cb2013-06-14 11:17:19 +08005584 list_for_each_entry(link, &cgrp->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005585 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005586 struct task_struct *task;
5587 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005588 seq_printf(seq, "css_set %p\n", cset);
5589 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005590 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5591 seq_puts(seq, " ...\n");
5592 break;
5593 } else {
5594 seq_printf(seq, " task %d\n",
5595 task_pid_vnr(task));
5596 }
5597 }
5598 }
5599 read_unlock(&css_set_lock);
5600 return 0;
5601}
5602
Paul Menagefe693432009-09-23 15:56:20 -07005603static u64 releasable_read(struct cgroup *cgrp, struct cftype *cft)
5604{
5605 return test_bit(CGRP_RELEASABLE, &cgrp->flags);
5606}
5607
5608static struct cftype debug_files[] = {
5609 {
Paul Menagefe693432009-09-23 15:56:20 -07005610 .name = "taskcount",
5611 .read_u64 = debug_taskcount_read,
5612 },
5613
5614 {
5615 .name = "current_css_set",
5616 .read_u64 = current_css_set_read,
5617 },
5618
5619 {
5620 .name = "current_css_set_refcount",
5621 .read_u64 = current_css_set_refcount_read,
5622 },
5623
5624 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005625 .name = "current_css_set_cg_links",
5626 .read_seq_string = current_css_set_cg_links_read,
5627 },
5628
5629 {
5630 .name = "cgroup_css_links",
5631 .read_seq_string = cgroup_css_links_read,
5632 },
5633
5634 {
Paul Menagefe693432009-09-23 15:56:20 -07005635 .name = "releasable",
5636 .read_u64 = releasable_read,
5637 },
Paul Menagefe693432009-09-23 15:56:20 -07005638
Tejun Heo4baf6e32012-04-01 12:09:55 -07005639 { } /* terminate */
5640};
Paul Menagefe693432009-09-23 15:56:20 -07005641
5642struct cgroup_subsys debug_subsys = {
5643 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005644 .css_alloc = debug_css_alloc,
5645 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005646 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005647 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005648};
5649#endif /* CONFIG_CGROUP_DEBUG */