blob: 34fd1be0d9bf89d8d271c11f11c593d41b350d84 [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 */
Li Zefan081aa452013-03-13 09:17:09 +080059#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020060#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070061
Arun Sharma600634972011-07-26 16:09:06 -070062#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070063
Tejun Heoe25e2cb2011-12-12 18:12:21 -080064/*
65 * cgroup_mutex is the master lock. Any modification to cgroup or its
66 * hierarchy must be performed while holding it.
67 *
68 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
69 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
70 * release_agent_path and so on. Modifying requires both cgroup_mutex and
71 * cgroup_root_mutex. Readers can acquire either of the two. This is to
72 * break the following locking order cycle.
73 *
74 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
75 * B. namespace_sem -> cgroup_mutex
76 *
77 * B happens only through cgroup_show_options() and using cgroup_root_mutex
78 * breaks it.
79 */
Tejun Heo22194492013-04-07 09:29:51 -070080#ifdef CONFIG_PROVE_RCU
81DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040082EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070083#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070084static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070085#endif
86
Tejun Heoe25e2cb2011-12-12 18:12:21 -080087static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070088
Ben Blumaae8aab2010-03-10 15:22:07 -080089/*
Tejun Heoe5fca242013-11-22 17:14:39 -050090 * cgroup destruction makes heavy use of work items and there can be a lot
91 * of concurrent destructions. Use a separate workqueue so that cgroup
92 * destruction work items don't end up filling up max_active of system_wq
93 * which may lead to deadlock.
94 */
95static struct workqueue_struct *cgroup_destroy_wq;
96
97/*
Ben Blumaae8aab2010-03-10 15:22:07 -080098 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020099 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -0800100 * registered after that. The mutable section of this array is protected by
101 * cgroup_mutex.
102 */
Daniel Wagner80f4c872012-09-12 16:12:06 +0200103#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +0200104#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700105static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700106#include <linux/cgroup_subsys.h>
107};
108
Paul Menageddbcc7e2007-10-18 23:39:30 -0700109/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700110 * The dummy hierarchy, reserved for the subsystems that are otherwise
111 * unattached - it never has more than a single cgroup, and all tasks are
112 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700113 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700114static struct cgroupfs_root cgroup_dummy_root;
115
116/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
117static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700118
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700119/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700120 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
121 */
122struct cfent {
123 struct list_head node;
124 struct dentry *dentry;
125 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400126 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700127
128 /* file xattrs */
129 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700130};
131
Paul Menageddbcc7e2007-10-18 23:39:30 -0700132/* The list of hierarchy roots */
133
Tejun Heo9871bf92013-06-24 15:21:47 -0700134static LIST_HEAD(cgroup_roots);
135static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700136
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700137/*
138 * Hierarchy ID allocation and mapping. It follows the same exclusion
139 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
140 * writes, either for reads.
141 */
Tejun Heo1a574232013-04-14 11:36:58 -0700142static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700143
Li Zefan65dff752013-03-01 15:01:56 +0800144static struct cgroup_name root_cgroup_name = { .name = "/" };
145
Li Zefan794611a2013-06-18 18:53:53 +0800146/*
147 * Assign a monotonically increasing serial number to cgroups. It
148 * guarantees cgroups with bigger numbers are newer than those with smaller
149 * numbers. Also, as cgroups are always appended to the parent's
150 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700151 * the ascending serial number order on the list. Protected by
152 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800153 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700154static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800155
Paul Menageddbcc7e2007-10-18 23:39:30 -0700156/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800157 * check for fork/exit handlers to call. This avoids us having to do
158 * extra work in the fork/exit path if none of the subsystems need to
159 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700160 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700161static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700162
Tejun Heo628f7cd2013-06-28 16:24:11 -0700163static struct cftype cgroup_base_files[];
164
Tejun Heof20104d2013-08-13 20:22:50 -0400165static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800166static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400167static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
168 bool is_add);
Tejun Heoe605b362013-11-27 18:16:21 -0500169static int cgroup_file_release(struct inode *inode, struct file *file);
Tejun Heo42809dd2012-11-19 08:13:37 -0800170
Tejun Heo95109b62013-08-08 20:11:27 -0400171/**
172 * cgroup_css - obtain a cgroup's css for the specified subsystem
173 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400174 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400175 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400176 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
177 * function must be called either under cgroup_mutex or rcu_read_lock() and
178 * the caller is responsible for pinning the returned css if it wants to
179 * keep accessing it outside the said locks. This function may return
180 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400181 */
182static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400183 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400184{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400185 if (ss)
186 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
187 lockdep_is_held(&cgroup_mutex));
188 else
189 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400190}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700193static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700194{
Tejun Heo54766d42013-06-12 21:04:53 -0700195 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700196}
197
Li Zefan78574cf2013-04-08 19:00:38 -0700198/**
199 * cgroup_is_descendant - test ancestry
200 * @cgrp: the cgroup to be tested
201 * @ancestor: possible ancestor of @cgrp
202 *
203 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
204 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
205 * and @ancestor are accessible.
206 */
207bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
208{
209 while (cgrp) {
210 if (cgrp == ancestor)
211 return true;
212 cgrp = cgrp->parent;
213 }
214 return false;
215}
216EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Adrian Bunke9685a02008-02-07 00:13:46 -0800218static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700219{
220 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700221 (1 << CGRP_RELEASABLE) |
222 (1 << CGRP_NOTIFY_ON_RELEASE);
223 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700224}
225
Adrian Bunke9685a02008-02-07 00:13:46 -0800226static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700227{
Paul Menagebd89aab2007-10-18 23:40:44 -0700228 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700229}
230
Tejun Heo30159ec2013-06-25 11:53:37 -0700231/**
232 * for_each_subsys - iterate all loaded cgroup subsystems
233 * @ss: the iteration cursor
234 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
235 *
236 * Should be called under cgroup_mutex.
237 */
238#define for_each_subsys(ss, i) \
239 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
240 if (({ lockdep_assert_held(&cgroup_mutex); \
241 !((ss) = cgroup_subsys[i]); })) { } \
242 else
243
244/**
245 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
246 * @ss: the iteration cursor
247 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
248 *
249 * Bulit-in subsystems are always present and iteration itself doesn't
250 * require any synchronization.
251 */
252#define for_each_builtin_subsys(ss, i) \
253 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
254 (((ss) = cgroup_subsys[i]) || true); (i)++)
255
Tejun Heo5549c492013-06-24 15:21:48 -0700256/* iterate each subsystem attached to a hierarchy */
257#define for_each_root_subsys(root, ss) \
258 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700259
Tejun Heo5549c492013-06-24 15:21:48 -0700260/* iterate across the active hierarchies */
261#define for_each_active_root(root) \
262 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700263
Tejun Heof6ea9372012-04-01 12:09:55 -0700264static inline struct cgroup *__d_cgrp(struct dentry *dentry)
265{
266 return dentry->d_fsdata;
267}
268
Tejun Heo05ef1d72012-04-01 12:09:56 -0700269static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700270{
271 return dentry->d_fsdata;
272}
273
Tejun Heo05ef1d72012-04-01 12:09:56 -0700274static inline struct cftype *__d_cft(struct dentry *dentry)
275{
276 return __d_cfe(dentry)->type;
277}
278
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700279/**
280 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
281 * @cgrp: the cgroup to be checked for liveness
282 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700283 * On success, returns true; the mutex should be later unlocked. On
284 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700285 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700286static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700287{
288 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700289 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700290 mutex_unlock(&cgroup_mutex);
291 return false;
292 }
293 return true;
294}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700295
Paul Menage81a6a5c2007-10-18 23:39:38 -0700296/* the list of cgroups eligible for automatic release. Protected by
297 * release_list_lock */
298static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200299static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700300static void cgroup_release_agent(struct work_struct *work);
301static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700302static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700303
Tejun Heo69d02062013-06-12 21:04:50 -0700304/*
305 * A cgroup can be associated with multiple css_sets as different tasks may
306 * belong to different cgroups on different hierarchies. In the other
307 * direction, a css_set is naturally associated with multiple cgroups.
308 * This M:N relationship is represented by the following link structure
309 * which exists for each association and allows traversing the associations
310 * from both sides.
311 */
312struct cgrp_cset_link {
313 /* the cgroup and css_set this link associates */
314 struct cgroup *cgrp;
315 struct css_set *cset;
316
317 /* list of cgrp_cset_links anchored at cgrp->cset_links */
318 struct list_head cset_link;
319
320 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
321 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700322};
323
324/* The default css_set - used by init and its children prior to any
325 * hierarchies being mounted. It contains a pointer to the root state
326 * for each subsystem. Also used to anchor the list of css_sets. Not
327 * reference-counted, to improve performance when child cgroups
328 * haven't been created.
329 */
330
331static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700332static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700333
Tejun Heo0942eee2013-08-08 20:11:26 -0400334/*
335 * css_set_lock protects the list of css_set objects, and the chain of
336 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400337 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400338 */
Paul Menage817929e2007-10-18 23:39:36 -0700339static DEFINE_RWLOCK(css_set_lock);
340static int css_set_count;
341
Paul Menage7717f7b2009-09-23 15:56:22 -0700342/*
343 * hash table for cgroup groups. This improves the performance to find
344 * an existing css_set. This hash doesn't (currently) take into
345 * account cgroups in empty hierarchies.
346 */
Li Zefan472b1052008-04-29 01:00:11 -0700347#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800348static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700349
Li Zefan0ac801f2013-01-10 11:49:27 +0800350static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700351{
Li Zefan0ac801f2013-01-10 11:49:27 +0800352 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700353 struct cgroup_subsys *ss;
354 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700355
Tejun Heo30159ec2013-06-25 11:53:37 -0700356 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800357 key += (unsigned long)css[i];
358 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700359
Li Zefan0ac801f2013-01-10 11:49:27 +0800360 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700361}
362
Tejun Heo0942eee2013-08-08 20:11:26 -0400363/*
364 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400365 * until after the first call to css_task_iter_start(). This reduces the
366 * fork()/exit() overhead for people who have cgroups compiled into their
367 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400368 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700369static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700370
Tejun Heo5abb8852013-06-12 21:04:49 -0700371static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700372{
Tejun Heo69d02062013-06-12 21:04:50 -0700373 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700374
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700375 /*
376 * Ensure that the refcount doesn't hit zero while any readers
377 * can see it. Similar to atomic_dec_and_lock(), but for an
378 * rwlock
379 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700380 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700381 return;
382 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700383 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700384 write_unlock(&css_set_lock);
385 return;
386 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700387
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700388 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700389 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700390 css_set_count--;
391
Tejun Heo69d02062013-06-12 21:04:50 -0700392 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700393 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700394
Tejun Heo69d02062013-06-12 21:04:50 -0700395 list_del(&link->cset_link);
396 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800397
Tejun Heoddd69142013-06-12 21:04:54 -0700398 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700399 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700400 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700401 set_bit(CGRP_RELEASABLE, &cgrp->flags);
402 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700403 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700404
405 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700406 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700407
408 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700409 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700410}
411
412/*
413 * refcounted get/put for css_set objects
414 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700415static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700416{
Tejun Heo5abb8852013-06-12 21:04:49 -0700417 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700418}
419
Tejun Heo5abb8852013-06-12 21:04:49 -0700420static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700421{
Tejun Heo5abb8852013-06-12 21:04:49 -0700422 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700423}
424
Tejun Heo5abb8852013-06-12 21:04:49 -0700425static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700426{
Tejun Heo5abb8852013-06-12 21:04:49 -0700427 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700428}
429
Tejun Heob326f9d2013-06-24 15:21:48 -0700430/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700431 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700432 * @cset: candidate css_set being tested
433 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700434 * @new_cgrp: cgroup that's being entered by the task
435 * @template: desired set of css pointers in css_set (pre-calculated)
436 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800437 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700438 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
439 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700440static bool compare_css_sets(struct css_set *cset,
441 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700442 struct cgroup *new_cgrp,
443 struct cgroup_subsys_state *template[])
444{
445 struct list_head *l1, *l2;
446
Tejun Heo5abb8852013-06-12 21:04:49 -0700447 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700448 /* Not all subsystems matched */
449 return false;
450 }
451
452 /*
453 * Compare cgroup pointers in order to distinguish between
454 * different cgroups in heirarchies with no subsystems. We
455 * could get by with just this check alone (and skip the
456 * memcmp above) but on most setups the memcmp check will
457 * avoid the need for this more expensive check on almost all
458 * candidates.
459 */
460
Tejun Heo69d02062013-06-12 21:04:50 -0700461 l1 = &cset->cgrp_links;
462 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700464 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700465 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700466
467 l1 = l1->next;
468 l2 = l2->next;
469 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700470 if (l1 == &cset->cgrp_links) {
471 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700472 break;
473 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700474 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700475 }
476 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700477 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
478 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
479 cgrp1 = link1->cgrp;
480 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700481 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700482 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700483
484 /*
485 * If this hierarchy is the hierarchy of the cgroup
486 * that's changing, then we need to check that this
487 * css_set points to the new cgroup; if it's any other
488 * hierarchy, then this css_set should point to the
489 * same cgroup as the old css_set.
490 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700491 if (cgrp1->root == new_cgrp->root) {
492 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 return false;
494 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700495 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 return false;
497 }
498 }
499 return true;
500}
501
Tejun Heob326f9d2013-06-24 15:21:48 -0700502/**
503 * find_existing_css_set - init css array and find the matching css_set
504 * @old_cset: the css_set that we're using before the cgroup transition
505 * @cgrp: the cgroup that we're moving into
506 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700507 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700508static struct css_set *find_existing_css_set(struct css_set *old_cset,
509 struct cgroup *cgrp,
510 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700511{
Paul Menagebd89aab2007-10-18 23:40:44 -0700512 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700513 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800515 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700516 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700517
Ben Blumaae8aab2010-03-10 15:22:07 -0800518 /*
519 * Build the set of subsystem state objects that we want to see in the
520 * new css_set. while subsystems can change globally, the entries here
521 * won't change, so no need for locking.
522 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700523 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400524 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700525 /* Subsystem is in this hierarchy. So we want
526 * the subsystem state from the new
527 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400528 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700529 } else {
530 /* Subsystem is not in this hierarchy, so we
531 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700532 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700533 }
534 }
535
Li Zefan0ac801f2013-01-10 11:49:27 +0800536 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700537 hash_for_each_possible(css_set_table, cset, hlist, key) {
538 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700539 continue;
540
541 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700542 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700543 }
Paul Menage817929e2007-10-18 23:39:36 -0700544
545 /* No existing cgroup group matched */
546 return NULL;
547}
548
Tejun Heo69d02062013-06-12 21:04:50 -0700549static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700550{
Tejun Heo69d02062013-06-12 21:04:50 -0700551 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700552
Tejun Heo69d02062013-06-12 21:04:50 -0700553 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
554 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700555 kfree(link);
556 }
557}
558
Tejun Heo69d02062013-06-12 21:04:50 -0700559/**
560 * allocate_cgrp_cset_links - allocate cgrp_cset_links
561 * @count: the number of links to allocate
562 * @tmp_links: list_head the allocated links are put on
563 *
564 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
565 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700566 */
Tejun Heo69d02062013-06-12 21:04:50 -0700567static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700568{
Tejun Heo69d02062013-06-12 21:04:50 -0700569 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700570 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700571
572 INIT_LIST_HEAD(tmp_links);
573
Li Zefan36553432008-07-29 22:33:19 -0700574 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700575 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700576 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700577 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700578 return -ENOMEM;
579 }
Tejun Heo69d02062013-06-12 21:04:50 -0700580 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700581 }
582 return 0;
583}
584
Li Zefanc12f65d2009-01-07 18:07:42 -0800585/**
586 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700587 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700588 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800589 * @cgrp: the destination cgroup
590 */
Tejun Heo69d02062013-06-12 21:04:50 -0700591static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
592 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800593{
Tejun Heo69d02062013-06-12 21:04:50 -0700594 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800595
Tejun Heo69d02062013-06-12 21:04:50 -0700596 BUG_ON(list_empty(tmp_links));
597 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
598 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700599 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700600 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700601 /*
602 * Always add links to the tail of the list so that the list
603 * is sorted by order of hierarchy creation
604 */
Tejun Heo69d02062013-06-12 21:04:50 -0700605 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800606}
607
Tejun Heob326f9d2013-06-24 15:21:48 -0700608/**
609 * find_css_set - return a new css_set with one cgroup updated
610 * @old_cset: the baseline css_set
611 * @cgrp: the cgroup to be updated
612 *
613 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
614 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700615 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700616static struct css_set *find_css_set(struct css_set *old_cset,
617 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700618{
Tejun Heob326f9d2013-06-24 15:21:48 -0700619 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700621 struct list_head tmp_links;
622 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800623 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700624
Tejun Heob326f9d2013-06-24 15:21:48 -0700625 lockdep_assert_held(&cgroup_mutex);
626
Paul Menage817929e2007-10-18 23:39:36 -0700627 /* First see if we already have a cgroup group that matches
628 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700629 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700630 cset = find_existing_css_set(old_cset, cgrp, template);
631 if (cset)
632 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700633 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700634
Tejun Heo5abb8852013-06-12 21:04:49 -0700635 if (cset)
636 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700637
Tejun Heof4f4be22013-06-12 21:04:51 -0700638 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700639 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700640 return NULL;
641
Tejun Heo69d02062013-06-12 21:04:50 -0700642 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700643 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700644 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700645 return NULL;
646 }
647
Tejun Heo5abb8852013-06-12 21:04:49 -0700648 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700649 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700650 INIT_LIST_HEAD(&cset->tasks);
651 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700652
653 /* Copy the set of subsystem state objects generated in
654 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700655 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700656
657 write_lock(&css_set_lock);
658 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700659 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700660 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700661
Paul Menage7717f7b2009-09-23 15:56:22 -0700662 if (c->root == cgrp->root)
663 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700664 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700665 }
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo69d02062013-06-12 21:04:50 -0700667 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700668
Paul Menage817929e2007-10-18 23:39:36 -0700669 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700670
671 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700672 key = css_set_hash(cset->subsys);
673 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700674
Paul Menage817929e2007-10-18 23:39:36 -0700675 write_unlock(&css_set_lock);
676
Tejun Heo5abb8852013-06-12 21:04:49 -0700677 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700678}
679
Paul Menageddbcc7e2007-10-18 23:39:30 -0700680/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700681 * Return the cgroup for "task" from the given hierarchy. Must be
682 * called with cgroup_mutex held.
683 */
684static struct cgroup *task_cgroup_from_root(struct task_struct *task,
685 struct cgroupfs_root *root)
686{
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700688 struct cgroup *res = NULL;
689
690 BUG_ON(!mutex_is_locked(&cgroup_mutex));
691 read_lock(&css_set_lock);
692 /*
693 * No need to lock the task - since we hold cgroup_mutex the
694 * task can't change groups, so the only thing that can happen
695 * is that it exits and its css is set back to init_css_set.
696 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700697 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700698 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700699 res = &root->top_cgroup;
700 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700701 struct cgrp_cset_link *link;
702
703 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700704 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700705
Paul Menage7717f7b2009-09-23 15:56:22 -0700706 if (c->root == root) {
707 res = c;
708 break;
709 }
710 }
711 }
712 read_unlock(&css_set_lock);
713 BUG_ON(!res);
714 return res;
715}
716
717/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700718 * There is one global cgroup mutex. We also require taking
719 * task_lock() when dereferencing a task's cgroup subsys pointers.
720 * See "The task_lock() exception", at the end of this comment.
721 *
722 * A task must hold cgroup_mutex to modify cgroups.
723 *
724 * Any task can increment and decrement the count field without lock.
725 * So in general, code holding cgroup_mutex can't rely on the count
726 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800727 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700728 * means that no tasks are currently attached, therefore there is no
729 * way a task attached to that cgroup can fork (the other way to
730 * increment the count). So code holding cgroup_mutex can safely
731 * assume that if the count is zero, it will stay zero. Similarly, if
732 * a task holds cgroup_mutex on a cgroup with zero count, it
733 * knows that the cgroup won't be removed, as cgroup_rmdir()
734 * needs that mutex.
735 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700736 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
737 * (usually) take cgroup_mutex. These are the two most performance
738 * critical pieces of code here. The exception occurs on cgroup_exit(),
739 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
740 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800741 * to the release agent with the name of the cgroup (path relative to
742 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700743 *
744 * A cgroup can only be deleted if both its 'count' of using tasks
745 * is zero, and its list of 'children' cgroups is empty. Since all
746 * tasks in the system use _some_ cgroup, and since there is always at
747 * least one task in the system (init, pid == 1), therefore, top_cgroup
748 * always has either children cgroups and/or using tasks. So we don't
749 * need a special hack to ensure that top_cgroup cannot be deleted.
750 *
751 * The task_lock() exception
752 *
753 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800754 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800755 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700756 * several performance critical places that need to reference
757 * task->cgroup without the expense of grabbing a system global
758 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800759 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
761 * the task_struct routinely used for such matters.
762 *
763 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800764 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700765 */
766
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767/*
768 * A couple of forward declarations required, due to cyclic reference loop:
769 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
770 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
771 * -> cgroup_mkdir.
772 */
773
Al Viro18bb1db2011-07-26 01:41:39 -0400774static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700776static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700777static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700778static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700779
780static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200781 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700782 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700783};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700784
Al Viroa5e7ed32011-07-26 01:55:55 -0400785static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786{
787 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788
789 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400790 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700791 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100792 inode->i_uid = current_fsuid();
793 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
795 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
796 }
797 return inode;
798}
799
Li Zefan65dff752013-03-01 15:01:56 +0800800static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
801{
802 struct cgroup_name *name;
803
804 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
805 if (!name)
806 return NULL;
807 strcpy(name->name, dentry->d_name.name);
808 return name;
809}
810
Li Zefanbe445622013-01-24 14:31:42 +0800811static void cgroup_free_fn(struct work_struct *work)
812{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700813 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800814
815 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800816 cgrp->root->number_of_cgroups--;
817 mutex_unlock(&cgroup_mutex);
818
819 /*
Li Zefan415cf072013-04-08 14:35:02 +0800820 * We get a ref to the parent's dentry, and put the ref when
821 * this cgroup is being freed, so it's guaranteed that the
822 * parent won't be destroyed before its children.
823 */
824 dput(cgrp->parent->dentry);
825
826 /*
Li Zefanbe445622013-01-24 14:31:42 +0800827 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700828 * created the cgroup. This will free cgrp->root, if we are
829 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800830 */
831 deactivate_super(cgrp->root->sb);
832
833 /*
834 * if we're getting rid of the cgroup, refcount should ensure
835 * that there are no pidlists left.
836 */
837 BUG_ON(!list_empty(&cgrp->pidlists));
838
839 simple_xattrs_free(&cgrp->xattrs);
840
Li Zefan65dff752013-03-01 15:01:56 +0800841 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800842 kfree(cgrp);
843}
844
845static void cgroup_free_rcu(struct rcu_head *head)
846{
847 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
848
Tejun Heoea15f8c2013-06-13 19:27:42 -0700849 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -0500850 queue_work(cgroup_destroy_wq, &cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800851}
852
Paul Menageddbcc7e2007-10-18 23:39:30 -0700853static void cgroup_diput(struct dentry *dentry, struct inode *inode)
854{
855 /* is dentry a directory ? if so, kfree() associated cgroup */
856 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700857 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800858
Tejun Heo54766d42013-06-12 21:04:53 -0700859 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800860 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700861 } else {
862 struct cfent *cfe = __d_cfe(dentry);
863 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
864
865 WARN_ONCE(!list_empty(&cfe->node) &&
866 cgrp != &cgrp->root->top_cgroup,
867 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700868 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700869 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700870 }
871 iput(inode);
872}
873
874static void remove_dir(struct dentry *d)
875{
876 struct dentry *parent = dget(d->d_parent);
877
878 d_delete(d);
879 simple_rmdir(parent->d_inode, d);
880 dput(parent);
881}
882
Li Zefan2739d3c2013-01-21 18:18:33 +0800883static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700884{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700885 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700886
Tejun Heo05ef1d72012-04-01 12:09:56 -0700887 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
888 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100889
Li Zefan2739d3c2013-01-21 18:18:33 +0800890 /*
891 * If we're doing cleanup due to failure of cgroup_create(),
892 * the corresponding @cfe may not exist.
893 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700894 list_for_each_entry(cfe, &cgrp->files, node) {
895 struct dentry *d = cfe->dentry;
896
897 if (cft && cfe->type != cft)
898 continue;
899
900 dget(d);
901 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700902 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700903 list_del_init(&cfe->node);
904 dput(d);
905
Li Zefan2739d3c2013-01-21 18:18:33 +0800906 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700907 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700908}
909
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400910/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700911 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700912 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400913 * @subsys_mask: mask of the subsystem ids whose files should be removed
914 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700915static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700916{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400917 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700918 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919
Tejun Heob420ba72013-07-12 12:34:02 -0700920 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400921 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700922
923 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400924 continue;
925 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400926 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400927 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700928}
929
930/*
931 * NOTE : the dentry must have been dget()'ed
932 */
933static void cgroup_d_remove_dir(struct dentry *dentry)
934{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100935 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700936
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100937 parent = dentry->d_parent;
938 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800939 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100941 spin_unlock(&dentry->d_lock);
942 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700943 remove_dir(dentry);
944}
945
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700946/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800947 * Call with cgroup_mutex held. Drops reference counts on modules, including
948 * any duplicate ones that parse_cgroupfs_options took. If this function
949 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -0800950 */
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -0700952 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700953{
Paul Menagebd89aab2007-10-18 23:40:44 -0700954 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -0700955 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700956 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -0700957 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958
Ben Blumaae8aab2010-03-10 15:22:07 -0800959 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -0800960 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -0800961
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -0700963 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700964 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -0700965 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -0700966
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700967 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -0700968 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700969 ret = -EBUSY;
970 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700971 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700972
973 /* pin the module */
974 if (!try_module_get(ss->module)) {
975 ret = -ENOENT;
976 goto out_put;
977 }
978 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979 }
980
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700981 /* subsys could be missing if unloaded between parsing and here */
982 if (added_mask != pinned) {
983 ret = -ENOENT;
984 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700985 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700986
Tejun Heo31261212013-06-28 17:07:30 -0700987 ret = cgroup_populate_dir(cgrp, added_mask);
988 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -0700989 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -0700990
991 /*
992 * Nothing can fail from this point on. Remove files for the
993 * removed subsystems and rebind each subsystem.
994 */
995 cgroup_clear_dir(cgrp, removed_mask);
996
Tejun Heo30159ec2013-06-25 11:53:37 -0700997 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -0700999
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001000 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001002 BUG_ON(cgroup_css(cgrp, ss));
1003 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1004 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001005
Tejun Heo73e80ed2013-08-13 11:01:55 -04001006 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001007 cgroup_css(cgroup_dummy_top, ss));
1008 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001009
Li Zefan33a68ac2009-01-07 18:07:42 -08001010 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001011 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001012 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001013 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001014
Ben Blumcf5d5942010-03-10 15:22:09 -08001015 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001016 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001017 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001019 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1020 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001021
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001023 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001024
Tejun Heoca8bdca2013-08-26 18:40:56 -04001025 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001026 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1027
Tejun Heo9871bf92013-06-24 15:21:47 -07001028 cgroup_subsys[i]->root = &cgroup_dummy_root;
1029 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001030
Ben Blumcf5d5942010-03-10 15:22:09 -08001031 /* subsystem is now free - drop reference on module */
1032 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001033 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001034 }
1035 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036
Tejun Heo1672d042013-06-25 18:04:54 -07001037 /*
1038 * Mark @root has finished binding subsystems. @root->subsys_mask
1039 * now matches the bound subsystems.
1040 */
1041 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1042
Paul Menageddbcc7e2007-10-18 23:39:30 -07001043 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001044
1045out_put:
1046 for_each_subsys(ss, i)
1047 if (pinned & (1 << i))
1048 module_put(ss->module);
1049 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050}
1051
Al Viro34c80b12011-12-08 21:32:45 -05001052static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053{
Al Viro34c80b12011-12-08 21:32:45 -05001054 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001055 struct cgroup_subsys *ss;
1056
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001057 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001058 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001059 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001060 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1061 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001062 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001064 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001065 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001066 if (strlen(root->release_agent_path))
1067 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001068 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001069 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001070 if (strlen(root->name))
1071 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001072 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001073 return 0;
1074}
1075
1076struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001077 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001079 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001080 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001081 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001082 /* User explicitly requested empty subsystem */
1083 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001084
1085 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001086
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087};
1088
Ben Blumaae8aab2010-03-10 15:22:07 -08001089/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001090 * Convert a hierarchy specifier into a bitmask of subsystems and
1091 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1092 * array. This function takes refcounts on subsystems to be used, unless it
1093 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001094 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001095static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001096{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001097 char *token, *o = data;
1098 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001099 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001100 struct cgroup_subsys *ss;
1101 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001102
Ben Blumaae8aab2010-03-10 15:22:07 -08001103 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1104
Li Zefanf9ab5b52009-06-17 16:26:33 -07001105#ifdef CONFIG_CPUSETS
1106 mask = ~(1UL << cpuset_subsys_id);
1107#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108
Paul Menagec6d57f32009-09-23 15:56:19 -07001109 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110
1111 while ((token = strsep(&o, ",")) != NULL) {
1112 if (!*token)
1113 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001114 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001115 /* Explicitly have no subsystems */
1116 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001117 continue;
1118 }
1119 if (!strcmp(token, "all")) {
1120 /* Mutually exclusive option 'all' + subsystem name */
1121 if (one_ss)
1122 return -EINVAL;
1123 all_ss = true;
1124 continue;
1125 }
Tejun Heo873fe092013-04-14 20:15:26 -07001126 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1127 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1128 continue;
1129 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001130 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001131 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001132 continue;
1133 }
1134 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001135 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001136 continue;
1137 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001138 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001139 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001140 continue;
1141 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001142 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001143 /* Specifying two release agents is forbidden */
1144 if (opts->release_agent)
1145 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001147 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001148 if (!opts->release_agent)
1149 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001150 continue;
1151 }
1152 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001153 const char *name = token + 5;
1154 /* Can't specify an empty name */
1155 if (!strlen(name))
1156 return -EINVAL;
1157 /* Must match [\w.-]+ */
1158 for (i = 0; i < strlen(name); i++) {
1159 char c = name[i];
1160 if (isalnum(c))
1161 continue;
1162 if ((c == '.') || (c == '-') || (c == '_'))
1163 continue;
1164 return -EINVAL;
1165 }
1166 /* Specifying two names is forbidden */
1167 if (opts->name)
1168 return -EINVAL;
1169 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001170 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001171 GFP_KERNEL);
1172 if (!opts->name)
1173 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001174
1175 continue;
1176 }
1177
Tejun Heo30159ec2013-06-25 11:53:37 -07001178 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 if (strcmp(token, ss->name))
1180 continue;
1181 if (ss->disabled)
1182 continue;
1183
1184 /* Mutually exclusive option 'all' + subsystem name */
1185 if (all_ss)
1186 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001187 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001188 one_ss = true;
1189
1190 break;
1191 }
1192 if (i == CGROUP_SUBSYS_COUNT)
1193 return -ENOENT;
1194 }
1195
1196 /*
1197 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001198 * otherwise if 'none', 'name=' and a subsystem name options
1199 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001200 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001201 if (all_ss || (!one_ss && !opts->none && !opts->name))
1202 for_each_subsys(ss, i)
1203 if (!ss->disabled)
1204 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001205
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001206 /* Consistency checks */
1207
Tejun Heo873fe092013-04-14 20:15:26 -07001208 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1209 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1210
1211 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1212 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1213 return -EINVAL;
1214 }
1215
1216 if (opts->cpuset_clone_children) {
1217 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1218 return -EINVAL;
1219 }
1220 }
1221
Li Zefanf9ab5b52009-06-17 16:26:33 -07001222 /*
1223 * Option noprefix was introduced just for backward compatibility
1224 * with the old cpuset, so we allow noprefix only if mounting just
1225 * the cpuset subsystem.
1226 */
Tejun Heo93438622013-04-14 20:15:25 -07001227 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001228 return -EINVAL;
1229
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001230
1231 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001232 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001233 return -EINVAL;
1234
1235 /*
1236 * We either have to specify by name or by subsystems. (So all
1237 * empty hierarchies must have a name).
1238 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001239 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001240 return -EINVAL;
1241
1242 return 0;
1243}
1244
1245static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1246{
1247 int ret = 0;
1248 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001249 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001250 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001251 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Tejun Heo873fe092013-04-14 20:15:26 -07001253 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1254 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1255 return -EINVAL;
1256 }
1257
Paul Menagebd89aab2007-10-18 23:40:44 -07001258 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001259 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001260 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001261
1262 /* See what subsystems are wanted */
1263 ret = parse_cgroupfs_options(data, &opts);
1264 if (ret)
1265 goto out_unlock;
1266
Tejun Heoa8a648c2013-06-24 15:21:47 -07001267 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001268 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1269 task_tgid_nr(current), current->comm);
1270
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001271 added_mask = opts.subsys_mask & ~root->subsys_mask;
1272 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001273
Ben Blumcf5d5942010-03-10 15:22:09 -08001274 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001275 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001276 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001277 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1278 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1279 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001280 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001281 goto out_unlock;
1282 }
1283
Tejun Heof172e672013-06-28 17:07:30 -07001284 /* remounting is not allowed for populated hierarchies */
1285 if (root->number_of_cgroups > 1) {
1286 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001287 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001288 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001289
Paul Menageddbcc7e2007-10-18 23:39:30 -07001290 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001291 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001292 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001293
Paul Menage81a6a5c2007-10-18 23:39:38 -07001294 if (opts.release_agent)
1295 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001296 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001297 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001298 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001299 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001300 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001301 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001302 return ret;
1303}
1304
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001305static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 .statfs = simple_statfs,
1307 .drop_inode = generic_delete_inode,
1308 .show_options = cgroup_show_options,
1309 .remount_fs = cgroup_remount,
1310};
1311
Paul Menagecc31edc2008-10-18 20:28:04 -07001312static void init_cgroup_housekeeping(struct cgroup *cgrp)
1313{
1314 INIT_LIST_HEAD(&cgrp->sibling);
1315 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001316 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001317 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001318 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001319 INIT_LIST_HEAD(&cgrp->pidlists);
1320 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001321 cgrp->dummy_css.cgroup = cgrp;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001322 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001323}
Paul Menagec6d57f32009-09-23 15:56:19 -07001324
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325static void init_cgroup_root(struct cgroupfs_root *root)
1326{
Paul Menagebd89aab2007-10-18 23:40:44 -07001327 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001328
Paul Menageddbcc7e2007-10-18 23:39:30 -07001329 INIT_LIST_HEAD(&root->subsys_list);
1330 INIT_LIST_HEAD(&root->root_list);
1331 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001332 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001333 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001334 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001335 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001336}
1337
Tejun Heofc76df72013-06-25 11:53:37 -07001338static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001339{
Tejun Heo1a574232013-04-14 11:36:58 -07001340 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001341
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001342 lockdep_assert_held(&cgroup_mutex);
1343 lockdep_assert_held(&cgroup_root_mutex);
1344
Tejun Heofc76df72013-06-25 11:53:37 -07001345 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1346 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001347 if (id < 0)
1348 return id;
1349
1350 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001351 return 0;
1352}
1353
1354static void cgroup_exit_root_id(struct cgroupfs_root *root)
1355{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001356 lockdep_assert_held(&cgroup_mutex);
1357 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001358
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001359 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001360 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001361 root->hierarchy_id = 0;
1362 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001363}
1364
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365static int cgroup_test_super(struct super_block *sb, void *data)
1366{
Paul Menagec6d57f32009-09-23 15:56:19 -07001367 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 struct cgroupfs_root *root = sb->s_fs_info;
1369
Paul Menagec6d57f32009-09-23 15:56:19 -07001370 /* If we asked for a name then it must match */
1371 if (opts->name && strcmp(opts->name, root->name))
1372 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001373
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001374 /*
1375 * If we asked for subsystems (or explicitly for no
1376 * subsystems) then they must match
1377 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001378 if ((opts->subsys_mask || opts->none)
1379 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001380 return 0;
1381
1382 return 1;
1383}
1384
Paul Menagec6d57f32009-09-23 15:56:19 -07001385static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1386{
1387 struct cgroupfs_root *root;
1388
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001389 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001390 return NULL;
1391
1392 root = kzalloc(sizeof(*root), GFP_KERNEL);
1393 if (!root)
1394 return ERR_PTR(-ENOMEM);
1395
1396 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001397
Tejun Heo1672d042013-06-25 18:04:54 -07001398 /*
1399 * We need to set @root->subsys_mask now so that @root can be
1400 * matched by cgroup_test_super() before it finishes
1401 * initialization; otherwise, competing mounts with the same
1402 * options may try to bind the same subsystems instead of waiting
1403 * for the first one leading to unexpected mount errors.
1404 * SUBSYS_BOUND will be set once actual binding is complete.
1405 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001406 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001407 root->flags = opts->flags;
1408 if (opts->release_agent)
1409 strcpy(root->release_agent_path, opts->release_agent);
1410 if (opts->name)
1411 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001412 if (opts->cpuset_clone_children)
1413 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001414 return root;
1415}
1416
Tejun Heofa3ca072013-04-14 11:36:56 -07001417static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001418{
Tejun Heofa3ca072013-04-14 11:36:56 -07001419 if (root) {
1420 /* hierarhcy ID shoulid already have been released */
1421 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001422
Li Zefan4e96ee82013-07-31 09:50:50 +08001423 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001424 kfree(root);
1425 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001426}
1427
Paul Menageddbcc7e2007-10-18 23:39:30 -07001428static int cgroup_set_super(struct super_block *sb, void *data)
1429{
1430 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001431 struct cgroup_sb_opts *opts = data;
1432
1433 /* If we don't have a new root, we can't set up a new sb */
1434 if (!opts->new_root)
1435 return -EINVAL;
1436
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001437 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001438
1439 ret = set_anon_super(sb, NULL);
1440 if (ret)
1441 return ret;
1442
Paul Menagec6d57f32009-09-23 15:56:19 -07001443 sb->s_fs_info = opts->new_root;
1444 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001445
1446 sb->s_blocksize = PAGE_CACHE_SIZE;
1447 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1448 sb->s_magic = CGROUP_SUPER_MAGIC;
1449 sb->s_op = &cgroup_ops;
1450
1451 return 0;
1452}
1453
1454static int cgroup_get_rootdir(struct super_block *sb)
1455{
Al Viro0df6a632010-12-21 13:29:29 -05001456 static const struct dentry_operations cgroup_dops = {
1457 .d_iput = cgroup_diput,
Al Virob26d4cd2013-10-25 18:47:37 -04001458 .d_delete = always_delete_dentry,
Al Viro0df6a632010-12-21 13:29:29 -05001459 };
1460
Paul Menageddbcc7e2007-10-18 23:39:30 -07001461 struct inode *inode =
1462 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001463
1464 if (!inode)
1465 return -ENOMEM;
1466
Paul Menageddbcc7e2007-10-18 23:39:30 -07001467 inode->i_fop = &simple_dir_operations;
1468 inode->i_op = &cgroup_dir_inode_operations;
1469 /* directories start off with i_nlink == 2 (for "." entry) */
1470 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001471 sb->s_root = d_make_root(inode);
1472 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001473 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001474 /* for everything else we want ->d_op set */
1475 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001476 return 0;
1477}
1478
Al Virof7e83572010-07-26 13:23:11 +04001479static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001480 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001481 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001482{
1483 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001484 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001485 int ret = 0;
1486 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001487 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001488 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001489 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001490 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491
1492 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001493 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001495 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 if (ret)
1497 goto out_err;
1498
1499 /*
1500 * Allocate a new cgroup root. We may not need it if we're
1501 * reusing an existing hierarchy.
1502 */
1503 new_root = cgroup_root_from_opts(&opts);
1504 if (IS_ERR(new_root)) {
1505 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001506 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001507 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001508 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001509
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001511 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001513 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001514 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001515 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516 }
1517
Paul Menagec6d57f32009-09-23 15:56:19 -07001518 root = sb->s_fs_info;
1519 BUG_ON(!root);
1520 if (root == opts.new_root) {
1521 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001522 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001523 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001524 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001525 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526
1527 BUG_ON(sb->s_root != NULL);
1528
1529 ret = cgroup_get_rootdir(sb);
1530 if (ret)
1531 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001532 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533
Paul Menage817929e2007-10-18 23:39:36 -07001534 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001536 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001537
Li Zefan4e96ee82013-07-31 09:50:50 +08001538 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1539 0, 1, GFP_KERNEL);
1540 if (root_cgrp->id < 0)
1541 goto unlock_drop;
1542
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001543 /* Check for name clashes with existing mounts */
1544 ret = -EBUSY;
1545 if (strlen(root->name))
1546 for_each_active_root(existing_root)
1547 if (!strcmp(existing_root->name, root->name))
1548 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001549
Paul Menage817929e2007-10-18 23:39:36 -07001550 /*
1551 * We're accessing css_set_count without locking
1552 * css_set_lock here, but that's OK - it can only be
1553 * increased by someone holding cgroup_lock, and
1554 * that's us. The worst that can happen is that we
1555 * have some link structures left over
1556 */
Tejun Heo69d02062013-06-12 21:04:50 -07001557 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001558 if (ret)
1559 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001560
Tejun Heofc76df72013-06-25 11:53:37 -07001561 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1562 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001563 if (ret)
1564 goto unlock_drop;
1565
Tejun Heo31261212013-06-28 17:07:30 -07001566 sb->s_root->d_fsdata = root_cgrp;
1567 root_cgrp->dentry = sb->s_root;
1568
1569 /*
1570 * We're inside get_sb() and will call lookup_one_len() to
1571 * create the root files, which doesn't work if SELinux is
1572 * in use. The following cred dancing somehow works around
1573 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1574 * populating new cgroupfs mount") for more details.
1575 */
1576 cred = override_creds(&init_cred);
1577
Tejun Heo2bb566c2013-08-08 20:11:23 -04001578 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001579 if (ret)
1580 goto rm_base_files;
1581
Tejun Heoa8a648c2013-06-24 15:21:47 -07001582 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001583 if (ret)
1584 goto rm_base_files;
1585
1586 revert_creds(cred);
1587
Ben Blumcf5d5942010-03-10 15:22:09 -08001588 /*
1589 * There must be no failure case after here, since rebinding
1590 * takes care of subsystems' refcounts, which are explicitly
1591 * dropped in the failure exit path.
1592 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
Tejun Heo9871bf92013-06-24 15:21:47 -07001594 list_add(&root->root_list, &cgroup_roots);
1595 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001596
Paul Menage817929e2007-10-18 23:39:36 -07001597 /* Link the top cgroup in this hierarchy into all
1598 * the css_set objects */
1599 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001600 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001601 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001602 write_unlock(&css_set_lock);
1603
Tejun Heo69d02062013-06-12 21:04:50 -07001604 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001605
Li Zefanc12f65d2009-01-07 18:07:42 -08001606 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001607 BUG_ON(root->number_of_cgroups != 1);
1608
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001609 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001610 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001611 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001612 } else {
1613 /*
1614 * We re-used an existing hierarchy - the new root (if
1615 * any) is not needed
1616 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001617 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001618
Tejun Heoc7ba8282013-06-29 14:06:10 -07001619 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001620 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1621 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1622 ret = -EINVAL;
1623 goto drop_new_super;
1624 } else {
1625 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1626 }
Tejun Heo873fe092013-04-14 20:15:26 -07001627 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001628 }
1629
Paul Menagec6d57f32009-09-23 15:56:19 -07001630 kfree(opts.release_agent);
1631 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001632 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001633
Tejun Heo31261212013-06-28 17:07:30 -07001634 rm_base_files:
1635 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001636 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001637 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001638 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001639 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001640 mutex_unlock(&cgroup_root_mutex);
1641 mutex_unlock(&cgroup_mutex);
1642 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001643 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001644 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001645 out_err:
1646 kfree(opts.release_agent);
1647 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001648 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649}
1650
1651static void cgroup_kill_sb(struct super_block *sb) {
1652 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001653 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001654 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001655 int ret;
1656
1657 BUG_ON(!root);
1658
1659 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001660 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661
Tejun Heo31261212013-06-28 17:07:30 -07001662 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001664 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001665
1666 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001667 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1668 ret = rebind_subsystems(root, 0, root->subsys_mask);
1669 /* Shouldn't be able to fail ... */
1670 BUG_ON(ret);
1671 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001672
Paul Menage817929e2007-10-18 23:39:36 -07001673 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001674 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001675 * root cgroup
1676 */
1677 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001678
Tejun Heo69d02062013-06-12 21:04:50 -07001679 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1680 list_del(&link->cset_link);
1681 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001682 kfree(link);
1683 }
1684 write_unlock(&css_set_lock);
1685
Paul Menage839ec542009-01-29 14:25:22 -08001686 if (!list_empty(&root->root_list)) {
1687 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001688 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001689 }
Li Zefane5f6a862009-01-07 18:07:41 -08001690
Tejun Heofa3ca072013-04-14 11:36:56 -07001691 cgroup_exit_root_id(root);
1692
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001693 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001695 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001697 simple_xattrs_free(&cgrp->xattrs);
1698
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001700 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701}
1702
1703static struct file_system_type cgroup_fs_type = {
1704 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001705 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001706 .kill_sb = cgroup_kill_sb,
1707};
1708
Greg KH676db4a2010-08-05 13:53:35 -07001709static struct kobject *cgroup_kobj;
1710
Li Zefana043e3b2008-02-23 15:24:09 -08001711/**
1712 * cgroup_path - generate the path of a cgroup
1713 * @cgrp: the cgroup in question
1714 * @buf: the buffer to write the path into
1715 * @buflen: the length of the buffer
1716 *
Li Zefan65dff752013-03-01 15:01:56 +08001717 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1718 *
1719 * We can't generate cgroup path using dentry->d_name, as accessing
1720 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1721 * inode's i_mutex, while on the other hand cgroup_path() can be called
1722 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001724int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725{
Li Zefan65dff752013-03-01 15:01:56 +08001726 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001728
Tejun Heoda1f2962013-04-14 10:32:19 -07001729 if (!cgrp->parent) {
1730 if (strlcpy(buf, "/", buflen) >= buflen)
1731 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732 return 0;
1733 }
1734
Tao Ma316eb662012-11-08 21:36:38 +08001735 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001736 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001737
Li Zefan65dff752013-03-01 15:01:56 +08001738 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001739 do {
Li Zefan65dff752013-03-01 15:01:56 +08001740 const char *name = cgroup_name(cgrp);
1741 int len;
1742
1743 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001744 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001745 goto out;
1746 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001747
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001749 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001751
1752 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001753 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001754 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001756out:
1757 rcu_read_unlock();
1758 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759}
Ben Blum67523c42010-03-10 15:22:11 -08001760EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761
Tejun Heo857a2be2013-04-14 20:50:08 -07001762/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001763 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001764 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001765 * @buf: the buffer to write the path into
1766 * @buflen: the length of the buffer
1767 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001768 * Determine @task's cgroup on the first (the one with the lowest non-zero
1769 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1770 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1771 * cgroup controller callbacks.
1772 *
1773 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001774 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001775int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001776{
1777 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001778 struct cgroup *cgrp;
1779 int hierarchy_id = 1, ret = 0;
1780
1781 if (buflen < 2)
1782 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001783
1784 mutex_lock(&cgroup_mutex);
1785
Tejun Heo913ffdb2013-07-11 16:34:48 -07001786 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1787
Tejun Heo857a2be2013-04-14 20:50:08 -07001788 if (root) {
1789 cgrp = task_cgroup_from_root(task, root);
1790 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001791 } else {
1792 /* if no hierarchy exists, everyone is in "/" */
1793 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001794 }
1795
1796 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001797 return ret;
1798}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001799EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001800
Ben Blum74a11662011-05-26 16:25:20 -07001801/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001802 * Control Group taskset
1803 */
Tejun Heo134d3372011-12-12 18:12:21 -08001804struct task_and_cgroup {
1805 struct task_struct *task;
1806 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001807 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001808};
1809
Tejun Heo2f7ee562011-12-12 18:12:21 -08001810struct cgroup_taskset {
1811 struct task_and_cgroup single;
1812 struct flex_array *tc_array;
1813 int tc_array_len;
1814 int idx;
1815 struct cgroup *cur_cgrp;
1816};
1817
1818/**
1819 * cgroup_taskset_first - reset taskset and return the first task
1820 * @tset: taskset of interest
1821 *
1822 * @tset iteration is initialized and the first task is returned.
1823 */
1824struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1825{
1826 if (tset->tc_array) {
1827 tset->idx = 0;
1828 return cgroup_taskset_next(tset);
1829 } else {
1830 tset->cur_cgrp = tset->single.cgrp;
1831 return tset->single.task;
1832 }
1833}
1834EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1835
1836/**
1837 * cgroup_taskset_next - iterate to the next task in taskset
1838 * @tset: taskset of interest
1839 *
1840 * Return the next task in @tset. Iteration must have been initialized
1841 * with cgroup_taskset_first().
1842 */
1843struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1844{
1845 struct task_and_cgroup *tc;
1846
1847 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1848 return NULL;
1849
1850 tc = flex_array_get(tset->tc_array, tset->idx++);
1851 tset->cur_cgrp = tc->cgrp;
1852 return tc->task;
1853}
1854EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1855
1856/**
Tejun Heod99c8722013-08-08 20:11:27 -04001857 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001858 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001859 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001860 *
Tejun Heod99c8722013-08-08 20:11:27 -04001861 * Return the css for the current (last returned) task of @tset for
1862 * subsystem specified by @subsys_id. This function must be preceded by
1863 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001864 */
Tejun Heod99c8722013-08-08 20:11:27 -04001865struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1866 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001867{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001868 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869}
Tejun Heod99c8722013-08-08 20:11:27 -04001870EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001871
1872/**
1873 * cgroup_taskset_size - return the number of tasks in taskset
1874 * @tset: taskset of interest
1875 */
1876int cgroup_taskset_size(struct cgroup_taskset *tset)
1877{
1878 return tset->tc_array ? tset->tc_array_len : 1;
1879}
1880EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1881
1882
Ben Blum74a11662011-05-26 16:25:20 -07001883/*
1884 * cgroup_task_migrate - move a task from one cgroup to another.
1885 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001886 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001887 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001888static void cgroup_task_migrate(struct cgroup *old_cgrp,
1889 struct task_struct *tsk,
1890 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001891{
Tejun Heo5abb8852013-06-12 21:04:49 -07001892 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001893
1894 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001895 * We are synchronized through threadgroup_lock() against PF_EXITING
1896 * setting such that we can't race against cgroup_exit() changing the
1897 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001898 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001899 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001900 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001901
Ben Blum74a11662011-05-26 16:25:20 -07001902 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001903 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001904 task_unlock(tsk);
1905
1906 /* Update the css_set linked lists if we're using them */
1907 write_lock(&css_set_lock);
1908 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001909 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001910 write_unlock(&css_set_lock);
1911
1912 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001913 * We just gained a reference on old_cset by taking it from the
1914 * task. As trading it for new_cset is protected by cgroup_mutex,
1915 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001916 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001917 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1918 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001919}
1920
Li Zefana043e3b2008-02-23 15:24:09 -08001921/**
Li Zefan081aa452013-03-13 09:17:09 +08001922 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001923 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001924 * @tsk: the task or the leader of the threadgroup to be attached
1925 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001926 *
Tejun Heo257058a2011-12-12 18:12:21 -08001927 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001928 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001929 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001930static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1931 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001932{
1933 int retval, i, group_size;
1934 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001935 struct cgroupfs_root *root = cgrp->root;
1936 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001937 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001938 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001939 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001940 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001941
1942 /*
1943 * step 0: in order to do expensive, possibly blocking operations for
1944 * every thread, we cannot iterate the thread group list, since it needs
1945 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001946 * group - group_rwsem prevents new threads from appearing, and if
1947 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001948 */
Li Zefan081aa452013-03-13 09:17:09 +08001949 if (threadgroup)
1950 group_size = get_nr_threads(tsk);
1951 else
1952 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07001953 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08001954 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07001955 if (!group)
1956 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07001957 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07001958 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07001959 if (retval)
1960 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07001961
Ben Blum74a11662011-05-26 16:25:20 -07001962 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001963 /*
1964 * Prevent freeing of tasks while we take a snapshot. Tasks that are
1965 * already PF_EXITING could be freed from underneath us unless we
1966 * take an rcu_read_lock.
1967 */
1968 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07001969 do {
Tejun Heo134d3372011-12-12 18:12:21 -08001970 struct task_and_cgroup ent;
1971
Tejun Heocd3d0952011-12-12 18:12:21 -08001972 /* @tsk either already exited or can't exit until the end */
1973 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08001974 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08001975
Ben Blum74a11662011-05-26 16:25:20 -07001976 /* as per above, nr_threads may decrease, but not increase. */
1977 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08001978 ent.task = tsk;
1979 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08001980 /* nothing to do if this task is already in the cgroup */
1981 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08001982 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08001983 /*
1984 * saying GFP_ATOMIC has no effect here because we did prealloc
1985 * earlier, but it's good form to communicate our expectations.
1986 */
Tejun Heo134d3372011-12-12 18:12:21 -08001987 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07001988 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07001989 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08001990 next:
Li Zefan081aa452013-03-13 09:17:09 +08001991 if (!threadgroup)
1992 break;
Ben Blum74a11662011-05-26 16:25:20 -07001993 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08001994 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07001995 /* remember the number of threads in the array for later. */
1996 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001997 tset.tc_array = group;
1998 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07001999
Tejun Heo134d3372011-12-12 18:12:21 -08002000 /* methods shouldn't be called if no task is actually migrating */
2001 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002002 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002003 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002004
Ben Blum74a11662011-05-26 16:25:20 -07002005 /*
2006 * step 1: check that we can legitimately attach to the cgroup.
2007 */
Tejun Heo5549c492013-06-24 15:21:48 -07002008 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002009 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002010
Ben Blum74a11662011-05-26 16:25:20 -07002011 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002012 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002013 if (retval) {
2014 failed_ss = ss;
2015 goto out_cancel_attach;
2016 }
2017 }
Ben Blum74a11662011-05-26 16:25:20 -07002018 }
2019
2020 /*
2021 * step 2: make sure css_sets exist for all threads to be migrated.
2022 * we use find_css_set, which allocates a new one if necessary.
2023 */
Ben Blum74a11662011-05-26 16:25:20 -07002024 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002025 struct css_set *old_cset;
2026
Tejun Heo134d3372011-12-12 18:12:21 -08002027 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002028 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002029 tc->cset = find_css_set(old_cset, cgrp);
2030 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002031 retval = -ENOMEM;
2032 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002033 }
2034 }
2035
2036 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002037 * step 3: now that we're guaranteed success wrt the css_sets,
2038 * proceed to move all tasks to the new cgroup. There are no
2039 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002040 */
Ben Blum74a11662011-05-26 16:25:20 -07002041 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002042 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002043 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002044 }
2045 /* nothing is sensitive to fork() after this point. */
2046
2047 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002048 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002049 */
Tejun Heo5549c492013-06-24 15:21:48 -07002050 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002051 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002052
Ben Blum74a11662011-05-26 16:25:20 -07002053 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002054 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002055 }
2056
2057 /*
2058 * step 5: success! and cleanup
2059 */
Ben Blum74a11662011-05-26 16:25:20 -07002060 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002061out_put_css_set_refs:
2062 if (retval) {
2063 for (i = 0; i < group_size; i++) {
2064 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002065 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002066 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002067 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002068 }
Ben Blum74a11662011-05-26 16:25:20 -07002069 }
2070out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002071 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002072 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002073 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002074
Tejun Heo494c1672011-12-12 18:12:22 -08002075 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002076 break;
Ben Blum74a11662011-05-26 16:25:20 -07002077 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002078 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002079 }
2080 }
Ben Blum74a11662011-05-26 16:25:20 -07002081out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002082 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002083 return retval;
2084}
2085
2086/*
2087 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002088 * function to attach either it or all tasks in its threadgroup. Will lock
2089 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002090 */
2091static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002092{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002093 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002094 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002095 int ret;
2096
Ben Blum74a11662011-05-26 16:25:20 -07002097 if (!cgroup_lock_live_group(cgrp))
2098 return -ENODEV;
2099
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002100retry_find_task:
2101 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002102 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002103 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002105 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002106 ret= -ESRCH;
2107 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002108 }
Ben Blum74a11662011-05-26 16:25:20 -07002109 /*
2110 * even if we're attaching all tasks in the thread group, we
2111 * only need to check permissions on one of them.
2112 */
David Howellsc69e8d92008-11-14 10:39:19 +11002113 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002114 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2115 !uid_eq(cred->euid, tcred->uid) &&
2116 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002117 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002118 ret = -EACCES;
2119 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002120 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002121 } else
2122 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002123
2124 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002125 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002126
2127 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002128 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002129 * trapped in a cpuset, or RT worker may be born in a cgroup
2130 * with no rt_runtime allocated. Just say no.
2131 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002132 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002133 ret = -EINVAL;
2134 rcu_read_unlock();
2135 goto out_unlock_cgroup;
2136 }
2137
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002138 get_task_struct(tsk);
2139 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002140
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002141 threadgroup_lock(tsk);
2142 if (threadgroup) {
2143 if (!thread_group_leader(tsk)) {
2144 /*
2145 * a race with de_thread from another thread's exec()
2146 * may strip us of our leadership, if this happens,
2147 * there is no choice but to throw this task away and
2148 * try again; this is
2149 * "double-double-toil-and-trouble-check locking".
2150 */
2151 threadgroup_unlock(tsk);
2152 put_task_struct(tsk);
2153 goto retry_find_task;
2154 }
Li Zefan081aa452013-03-13 09:17:09 +08002155 }
2156
2157 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2158
Tejun Heocd3d0952011-12-12 18:12:21 -08002159 threadgroup_unlock(tsk);
2160
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002162out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002163 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002164 return ret;
2165}
2166
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002167/**
2168 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2169 * @from: attach to all cgroups of a given task
2170 * @tsk: the task to be attached
2171 */
2172int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2173{
2174 struct cgroupfs_root *root;
2175 int retval = 0;
2176
Tejun Heo47cfcd02013-04-07 09:29:51 -07002177 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002178 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002179 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002180
Li Zefan6f4b7e62013-07-31 16:18:36 +08002181 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002182 if (retval)
2183 break;
2184 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002185 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002186
2187 return retval;
2188}
2189EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2190
Tejun Heo182446d2013-08-08 20:11:24 -04002191static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2192 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002193{
Tejun Heo182446d2013-08-08 20:11:24 -04002194 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002195}
2196
Tejun Heo182446d2013-08-08 20:11:24 -04002197static int cgroup_procs_write(struct cgroup_subsys_state *css,
2198 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002199{
Tejun Heo182446d2013-08-08 20:11:24 -04002200 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002201}
2202
Tejun Heo182446d2013-08-08 20:11:24 -04002203static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2204 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002205{
Tejun Heo182446d2013-08-08 20:11:24 -04002206 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002207 if (strlen(buffer) >= PATH_MAX)
2208 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002209 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002210 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002211 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002212 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002213 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002214 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002215 return 0;
2216}
2217
Tejun Heo182446d2013-08-08 20:11:24 -04002218static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2219 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002220{
Tejun Heo182446d2013-08-08 20:11:24 -04002221 struct cgroup *cgrp = css->cgroup;
2222
Paul Menagee788e062008-07-25 01:46:59 -07002223 if (!cgroup_lock_live_group(cgrp))
2224 return -ENODEV;
2225 seq_puts(seq, cgrp->root->release_agent_path);
2226 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002227 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002228 return 0;
2229}
2230
Tejun Heo182446d2013-08-08 20:11:24 -04002231static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2232 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002233{
Tejun Heo182446d2013-08-08 20:11:24 -04002234 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002235 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002236}
2237
Paul Menage84eea842008-07-25 01:47:00 -07002238/* A buffer size big enough for numbers or short strings */
2239#define CGROUP_LOCAL_BUFFER_SIZE 64
2240
Tejun Heo182446d2013-08-08 20:11:24 -04002241static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2242 struct cftype *cft, struct file *file,
2243 const char __user *userbuf, size_t nbytes,
2244 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002245{
Paul Menage84eea842008-07-25 01:47:00 -07002246 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002247 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002248 char *end;
2249
2250 if (!nbytes)
2251 return -EINVAL;
2252 if (nbytes >= sizeof(buffer))
2253 return -E2BIG;
2254 if (copy_from_user(buffer, userbuf, nbytes))
2255 return -EFAULT;
2256
2257 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002258 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002259 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002260 if (*end)
2261 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002262 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002263 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002264 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002265 if (*end)
2266 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002267 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002268 }
Paul Menage355e0c42007-10-18 23:39:33 -07002269 if (!retval)
2270 retval = nbytes;
2271 return retval;
2272}
2273
Tejun Heo182446d2013-08-08 20:11:24 -04002274static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2275 struct cftype *cft, struct file *file,
2276 const char __user *userbuf, size_t nbytes,
2277 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002278{
Paul Menage84eea842008-07-25 01:47:00 -07002279 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002280 int retval = 0;
2281 size_t max_bytes = cft->max_write_len;
2282 char *buffer = local_buffer;
2283
2284 if (!max_bytes)
2285 max_bytes = sizeof(local_buffer) - 1;
2286 if (nbytes >= max_bytes)
2287 return -E2BIG;
2288 /* Allocate a dynamic buffer if we need one */
2289 if (nbytes >= sizeof(local_buffer)) {
2290 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2291 if (buffer == NULL)
2292 return -ENOMEM;
2293 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002294 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2295 retval = -EFAULT;
2296 goto out;
2297 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002298
2299 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002300 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002301 if (!retval)
2302 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002303out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002304 if (buffer != local_buffer)
2305 kfree(buffer);
2306 return retval;
2307}
2308
Paul Menageddbcc7e2007-10-18 23:39:30 -07002309static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002310 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002311{
Tejun Heo182446d2013-08-08 20:11:24 -04002312 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002313 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002314 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002315
Paul Menage355e0c42007-10-18 23:39:33 -07002316 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002317 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002318 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002319 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002320 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002321 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002322 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002323 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002324 return ret ? ret : nbytes;
2325 }
Paul Menage355e0c42007-10-18 23:39:33 -07002326 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002327}
2328
Tejun Heo182446d2013-08-08 20:11:24 -04002329static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2330 struct cftype *cft, struct file *file,
2331 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002332{
Paul Menage84eea842008-07-25 01:47:00 -07002333 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002334 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002335 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2336
2337 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2338}
2339
Tejun Heo182446d2013-08-08 20:11:24 -04002340static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2341 struct cftype *cft, struct file *file,
2342 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002343{
Paul Menage84eea842008-07-25 01:47:00 -07002344 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002345 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002346 int len = sprintf(tmp, "%lld\n", (long long) val);
2347
2348 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2349}
2350
Paul Menageddbcc7e2007-10-18 23:39:30 -07002351static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002352 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002353{
Tejun Heo182446d2013-08-08 20:11:24 -04002354 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002355 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002356 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002357
2358 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002359 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002360 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002361 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002362 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002363 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002364 return -EINVAL;
2365}
2366
Paul Menage91796562008-04-29 01:00:01 -07002367/*
2368 * seqfile ops/methods for returning structured data. Currently just
2369 * supports string->u64 maps, but can be extended in future.
2370 */
2371
Paul Menage91796562008-04-29 01:00:01 -07002372static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2373{
2374 struct seq_file *sf = cb->state;
2375 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2376}
2377
2378static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2379{
Li Zefane0798ce2013-07-31 17:36:25 +08002380 struct cfent *cfe = m->private;
2381 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002382 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002383
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002384 if (cft->read_map) {
2385 struct cgroup_map_cb cb = {
2386 .fill = cgroup_map_add,
2387 .state = m,
2388 };
Tejun Heo182446d2013-08-08 20:11:24 -04002389 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002390 }
Tejun Heo182446d2013-08-08 20:11:24 -04002391 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002392}
2393
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002394static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002395 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002396 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002397 .llseek = seq_lseek,
Tejun Heoe605b362013-11-27 18:16:21 -05002398 .release = cgroup_file_release,
Paul Menage91796562008-04-29 01:00:01 -07002399};
2400
Paul Menageddbcc7e2007-10-18 23:39:30 -07002401static int cgroup_file_open(struct inode *inode, struct file *file)
2402{
Tejun Heof7d58812013-08-08 20:11:23 -04002403 struct cfent *cfe = __d_cfe(file->f_dentry);
2404 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002405 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2406 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002407 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002408
2409 err = generic_file_open(inode, file);
2410 if (err)
2411 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002412
2413 /*
2414 * If the file belongs to a subsystem, pin the css. Will be
2415 * unpinned either on open failure or release. This ensures that
2416 * @css stays alive for all file operations.
2417 */
Tejun Heo105347b2013-08-13 11:01:55 -04002418 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002419 css = cgroup_css(cgrp, cft->ss);
2420 if (cft->ss && !css_tryget(css))
2421 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002422 rcu_read_unlock();
2423
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002424 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002425 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002426
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002427 /*
2428 * @cfe->css is used by read/write/close to determine the
2429 * associated css. @file->private_data would be a better place but
2430 * that's already used by seqfile. Multiple accessors may use it
2431 * simultaneously which is okay as the association never changes.
2432 */
2433 WARN_ON_ONCE(cfe->css && cfe->css != css);
2434 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002436 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002437 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002438 err = single_open(file, cgroup_seqfile_show, cfe);
2439 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002440 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002441 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002442
Tejun Heo67f4c362013-08-08 20:11:24 -04002443 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002444 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002445 return err;
2446}
2447
2448static int cgroup_file_release(struct inode *inode, struct file *file)
2449{
Tejun Heof7d58812013-08-08 20:11:23 -04002450 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002451 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002452 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002453 int ret = 0;
2454
Paul Menageddbcc7e2007-10-18 23:39:30 -07002455 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002456 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002457 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002458 css_put(css);
Tejun Heoe605b362013-11-27 18:16:21 -05002459 if (file->f_op == &cgroup_seqfile_operations)
2460 single_release(inode, file);
Tejun Heof7d58812013-08-08 20:11:23 -04002461 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002462}
2463
2464/*
2465 * cgroup_rename - Only allow simple rename of directories in place.
2466 */
2467static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2468 struct inode *new_dir, struct dentry *new_dentry)
2469{
Li Zefan65dff752013-03-01 15:01:56 +08002470 int ret;
2471 struct cgroup_name *name, *old_name;
2472 struct cgroup *cgrp;
2473
2474 /*
2475 * It's convinient to use parent dir's i_mutex to protected
2476 * cgrp->name.
2477 */
2478 lockdep_assert_held(&old_dir->i_mutex);
2479
Paul Menageddbcc7e2007-10-18 23:39:30 -07002480 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2481 return -ENOTDIR;
2482 if (new_dentry->d_inode)
2483 return -EEXIST;
2484 if (old_dir != new_dir)
2485 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002486
2487 cgrp = __d_cgrp(old_dentry);
2488
Tejun Heo6db8e852013-06-14 11:18:22 -07002489 /*
2490 * This isn't a proper migration and its usefulness is very
2491 * limited. Disallow if sane_behavior.
2492 */
2493 if (cgroup_sane_behavior(cgrp))
2494 return -EPERM;
2495
Li Zefan65dff752013-03-01 15:01:56 +08002496 name = cgroup_alloc_name(new_dentry);
2497 if (!name)
2498 return -ENOMEM;
2499
2500 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2501 if (ret) {
2502 kfree(name);
2503 return ret;
2504 }
2505
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002506 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002507 rcu_assign_pointer(cgrp->name, name);
2508
2509 kfree_rcu(old_name, rcu_head);
2510 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002511}
2512
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002513static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2514{
2515 if (S_ISDIR(dentry->d_inode->i_mode))
2516 return &__d_cgrp(dentry)->xattrs;
2517 else
Li Zefan712317a2013-04-18 23:09:52 -07002518 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002519}
2520
2521static inline int xattr_enabled(struct dentry *dentry)
2522{
2523 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002524 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002525}
2526
2527static bool is_valid_xattr(const char *name)
2528{
2529 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2530 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2531 return true;
2532 return false;
2533}
2534
2535static int cgroup_setxattr(struct dentry *dentry, const char *name,
2536 const void *val, size_t size, int flags)
2537{
2538 if (!xattr_enabled(dentry))
2539 return -EOPNOTSUPP;
2540 if (!is_valid_xattr(name))
2541 return -EINVAL;
2542 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2543}
2544
2545static int cgroup_removexattr(struct dentry *dentry, const char *name)
2546{
2547 if (!xattr_enabled(dentry))
2548 return -EOPNOTSUPP;
2549 if (!is_valid_xattr(name))
2550 return -EINVAL;
2551 return simple_xattr_remove(__d_xattrs(dentry), name);
2552}
2553
2554static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2555 void *buf, size_t size)
2556{
2557 if (!xattr_enabled(dentry))
2558 return -EOPNOTSUPP;
2559 if (!is_valid_xattr(name))
2560 return -EINVAL;
2561 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2562}
2563
2564static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2565{
2566 if (!xattr_enabled(dentry))
2567 return -EOPNOTSUPP;
2568 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2569}
2570
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002571static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002572 .read = cgroup_file_read,
2573 .write = cgroup_file_write,
2574 .llseek = generic_file_llseek,
2575 .open = cgroup_file_open,
2576 .release = cgroup_file_release,
2577};
2578
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002579static const struct inode_operations cgroup_file_inode_operations = {
2580 .setxattr = cgroup_setxattr,
2581 .getxattr = cgroup_getxattr,
2582 .listxattr = cgroup_listxattr,
2583 .removexattr = cgroup_removexattr,
2584};
2585
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002586static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002587 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002588 .mkdir = cgroup_mkdir,
2589 .rmdir = cgroup_rmdir,
2590 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002591 .setxattr = cgroup_setxattr,
2592 .getxattr = cgroup_getxattr,
2593 .listxattr = cgroup_listxattr,
2594 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002595};
2596
Al Viroa5e7ed32011-07-26 01:55:55 -04002597static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002598 struct super_block *sb)
2599{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002600 struct inode *inode;
2601
2602 if (!dentry)
2603 return -ENOENT;
2604 if (dentry->d_inode)
2605 return -EEXIST;
2606
2607 inode = cgroup_new_inode(mode, sb);
2608 if (!inode)
2609 return -ENOMEM;
2610
2611 if (S_ISDIR(mode)) {
2612 inode->i_op = &cgroup_dir_inode_operations;
2613 inode->i_fop = &simple_dir_operations;
2614
2615 /* start off with i_nlink == 2 (for "." entry) */
2616 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002617 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002618
Tejun Heob8a2df62012-11-19 08:13:37 -08002619 /*
2620 * Control reaches here with cgroup_mutex held.
2621 * @inode->i_mutex should nest outside cgroup_mutex but we
2622 * want to populate it immediately without releasing
2623 * cgroup_mutex. As @inode isn't visible to anyone else
2624 * yet, trylock will always succeed without affecting
2625 * lockdep checks.
2626 */
2627 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628 } else if (S_ISREG(mode)) {
2629 inode->i_size = 0;
2630 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002631 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002633 d_instantiate(dentry, inode);
2634 dget(dentry); /* Extra count - pin the dentry in core */
2635 return 0;
2636}
2637
Li Zefan099fca32009-04-02 16:57:29 -07002638/**
2639 * cgroup_file_mode - deduce file mode of a control file
2640 * @cft: the control file in question
2641 *
2642 * returns cft->mode if ->mode is not 0
2643 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2644 * returns S_IRUGO if it has only a read handler
2645 * returns S_IWUSR if it has only a write hander
2646 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002647static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002648{
Al Viroa5e7ed32011-07-26 01:55:55 -04002649 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002650
2651 if (cft->mode)
2652 return cft->mode;
2653
2654 if (cft->read || cft->read_u64 || cft->read_s64 ||
2655 cft->read_map || cft->read_seq_string)
2656 mode |= S_IRUGO;
2657
2658 if (cft->write || cft->write_u64 || cft->write_s64 ||
2659 cft->write_string || cft->trigger)
2660 mode |= S_IWUSR;
2661
2662 return mode;
2663}
2664
Tejun Heo2bb566c2013-08-08 20:11:23 -04002665static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002666{
Paul Menagebd89aab2007-10-18 23:40:44 -07002667 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002668 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002669 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002670 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002671 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002672 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002673 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002674
Tejun Heo9fa4db32013-08-26 18:40:56 -04002675 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2676 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002677 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002678 strcat(name, ".");
2679 }
2680 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002681
Paul Menageddbcc7e2007-10-18 23:39:30 -07002682 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002683
2684 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2685 if (!cfe)
2686 return -ENOMEM;
2687
Paul Menageddbcc7e2007-10-18 23:39:30 -07002688 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002689 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002691 goto out;
2692 }
2693
Li Zefand6cbf352013-05-14 19:44:20 +08002694 cfe->type = (void *)cft;
2695 cfe->dentry = dentry;
2696 dentry->d_fsdata = cfe;
2697 simple_xattrs_init(&cfe->xattrs);
2698
Tejun Heo05ef1d72012-04-01 12:09:56 -07002699 mode = cgroup_file_mode(cft);
2700 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2701 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002702 list_add_tail(&cfe->node, &parent->files);
2703 cfe = NULL;
2704 }
2705 dput(dentry);
2706out:
2707 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 return error;
2709}
2710
Tejun Heob1f28d32013-06-28 16:24:10 -07002711/**
2712 * cgroup_addrm_files - add or remove files to a cgroup directory
2713 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002714 * @cfts: array of cftypes to be added
2715 * @is_add: whether to add or remove
2716 *
2717 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002718 * For removals, this function never fails. If addition fails, this
2719 * function doesn't remove files already added. The caller is responsible
2720 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002721 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002722static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2723 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002725 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002726 int ret;
2727
2728 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2729 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002730
2731 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002732 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002733 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2734 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002735 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2736 continue;
2737 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2738 continue;
2739
Li Zefan2739d3c2013-01-21 18:18:33 +08002740 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002741 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002742 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002743 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002744 cft->name, ret);
2745 return ret;
2746 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002747 } else {
2748 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002749 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002751 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002752}
2753
Tejun Heo8e3f6542012-04-01 12:09:55 -07002754static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002755 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002756{
2757 /*
2758 * Thanks to the entanglement with vfs inode locking, we can't walk
2759 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002760 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2761 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002762 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002763 mutex_lock(&cgroup_mutex);
2764}
2765
Tejun Heo2bb566c2013-08-08 20:11:23 -04002766static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002767 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002768{
2769 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002770 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002771 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002772 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002773 struct dentry *prev = NULL;
2774 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002775 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002776 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002777 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002778
2779 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002780 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002781 !atomic_inc_not_zero(&sb->s_active)) {
2782 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002783 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002784 }
2785
Li Zefane8c82d22013-06-18 18:48:37 +08002786 /*
2787 * All cgroups which are created after we drop cgroup_mutex will
2788 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002789 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002790 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002791 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002792
Tejun Heo8e3f6542012-04-01 12:09:55 -07002793 mutex_unlock(&cgroup_mutex);
2794
Li Zefane8c82d22013-06-18 18:48:37 +08002795 /* add/rm files for all cgroups created before */
2796 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002797 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002798 struct cgroup *cgrp = css->cgroup;
2799
Li Zefane8c82d22013-06-18 18:48:37 +08002800 if (cgroup_is_dead(cgrp))
2801 continue;
2802
2803 inode = cgrp->dentry->d_inode;
2804 dget(cgrp->dentry);
2805 rcu_read_unlock();
2806
2807 dput(prev);
2808 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002809
2810 mutex_lock(&inode->i_mutex);
2811 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002812 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002813 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002814 mutex_unlock(&cgroup_mutex);
2815 mutex_unlock(&inode->i_mutex);
2816
Li Zefane8c82d22013-06-18 18:48:37 +08002817 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002818 if (ret)
2819 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002820 }
Li Zefane8c82d22013-06-18 18:48:37 +08002821 rcu_read_unlock();
2822 dput(prev);
2823 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002824 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002825}
2826
2827/**
2828 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2829 * @ss: target cgroup subsystem
2830 * @cfts: zero-length name terminated array of cftypes
2831 *
2832 * Register @cfts to @ss. Files described by @cfts are created for all
2833 * existing cgroups to which @ss is attached and all future cgroups will
2834 * have them too. This function can be called anytime whether @ss is
2835 * attached or not.
2836 *
2837 * Returns 0 on successful registration, -errno on failure. Note that this
2838 * function currently returns 0 as long as @cfts registration is successful
2839 * even if some file creation attempts on existing cgroups fail.
2840 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002841int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002842{
2843 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002844 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002845 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002846
2847 set = kzalloc(sizeof(*set), GFP_KERNEL);
2848 if (!set)
2849 return -ENOMEM;
2850
Tejun Heo2bb566c2013-08-08 20:11:23 -04002851 for (cft = cfts; cft->name[0] != '\0'; cft++)
2852 cft->ss = ss;
2853
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854 cgroup_cfts_prepare();
2855 set->cfts = cfts;
2856 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002857 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002858 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002859 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002860 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002861}
2862EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2863
Li Zefana043e3b2008-02-23 15:24:09 -08002864/**
Tejun Heo79578622012-04-01 12:09:56 -07002865 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002866 * @cfts: zero-length name terminated array of cftypes
2867 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002868 * Unregister @cfts. Files described by @cfts are removed from all
2869 * existing cgroups and all future cgroups won't have them either. This
2870 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002871 *
2872 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002873 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002874 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002875int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002876{
2877 struct cftype_set *set;
2878
Tejun Heo2bb566c2013-08-08 20:11:23 -04002879 if (!cfts || !cfts[0].ss)
2880 return -ENOENT;
2881
Tejun Heo79578622012-04-01 12:09:56 -07002882 cgroup_cfts_prepare();
2883
Tejun Heo2bb566c2013-08-08 20:11:23 -04002884 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002885 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002886 list_del(&set->node);
2887 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002888 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002889 return 0;
2890 }
2891 }
2892
Tejun Heo2bb566c2013-08-08 20:11:23 -04002893 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002894 return -ENOENT;
2895}
2896
2897/**
Li Zefana043e3b2008-02-23 15:24:09 -08002898 * cgroup_task_count - count the number of tasks in a cgroup.
2899 * @cgrp: the cgroup in question
2900 *
2901 * Return the number of tasks in the cgroup.
2902 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002903int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002904{
2905 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002906 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002907
Paul Menage817929e2007-10-18 23:39:36 -07002908 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002909 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2910 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002911 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002912 return count;
2913}
2914
2915/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002916 * To reduce the fork() overhead for systems that are not actually using
2917 * their cgroups capability, we don't maintain the lists running through
2918 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002919 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002920 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002921static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002922{
2923 struct task_struct *p, *g;
2924 write_lock(&css_set_lock);
2925 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002926 /*
2927 * We need tasklist_lock because RCU is not safe against
2928 * while_each_thread(). Besides, a forking task that has passed
2929 * cgroup_post_fork() without seeing use_task_css_set_links = 1
2930 * is not guaranteed to have its child immediately visible in the
2931 * tasklist if we walk through it with RCU.
2932 */
2933 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002934 do_each_thread(g, p) {
2935 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08002936 /*
2937 * We should check if the process is exiting, otherwise
2938 * it will race with cgroup_exit() in that the list
2939 * entry won't be deleted though the process has exited.
2940 */
2941 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07002942 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002943 task_unlock(p);
2944 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002945 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08002946 write_unlock(&css_set_lock);
2947}
2948
Tejun Heo574bd9f2012-11-09 09:12:29 -08002949/**
Tejun Heo492eb212013-08-08 20:11:25 -04002950 * css_next_child - find the next child of a given css
2951 * @pos_css: the current position (%NULL to initiate traversal)
2952 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09002953 *
Tejun Heo492eb212013-08-08 20:11:25 -04002954 * This function returns the next child of @parent_css and should be called
2955 * under RCU read lock. The only requirement is that @parent_css and
2956 * @pos_css are accessible. The next sibling is guaranteed to be returned
2957 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09002958 */
Tejun Heo492eb212013-08-08 20:11:25 -04002959struct cgroup_subsys_state *
2960css_next_child(struct cgroup_subsys_state *pos_css,
2961 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09002962{
Tejun Heo492eb212013-08-08 20:11:25 -04002963 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
2964 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09002965 struct cgroup *next;
2966
2967 WARN_ON_ONCE(!rcu_read_lock_held());
2968
2969 /*
2970 * @pos could already have been removed. Once a cgroup is removed,
2971 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07002972 * changes. As CGRP_DEAD assertion is serialized and happens
2973 * before the cgroup is taken off the ->sibling list, if we see it
2974 * unasserted, it's guaranteed that the next sibling hasn't
2975 * finished its grace period even if it's already removed, and thus
2976 * safe to dereference from this RCU critical section. If
2977 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
2978 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04002979 *
2980 * If @pos is dead, its next pointer can't be dereferenced;
2981 * however, as each cgroup is given a monotonically increasing
2982 * unique serial number and always appended to the sibling list,
2983 * the next one can be found by walking the parent's children until
2984 * we see a cgroup with higher serial number than @pos's. While
2985 * this path can be slower, it's taken only when either the current
2986 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09002987 */
Tejun Heo3b287a52013-08-08 20:11:24 -04002988 if (!pos) {
2989 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
2990 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09002991 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04002992 } else {
2993 list_for_each_entry_rcu(next, &cgrp->children, sibling)
2994 if (next->serial_nr > pos->serial_nr)
2995 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09002996 }
2997
Tejun Heo492eb212013-08-08 20:11:25 -04002998 if (&next->sibling == &cgrp->children)
2999 return NULL;
3000
Tejun Heoca8bdca2013-08-26 18:40:56 -04003001 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003002}
Tejun Heo492eb212013-08-08 20:11:25 -04003003EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003004
3005/**
Tejun Heo492eb212013-08-08 20:11:25 -04003006 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003007 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003008 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003009 *
Tejun Heo492eb212013-08-08 20:11:25 -04003010 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003011 * to visit for pre-order traversal of @root's descendants. @root is
3012 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003013 *
3014 * While this function requires RCU read locking, it doesn't require the
3015 * whole traversal to be contained in a single RCU critical section. This
3016 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003017 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003018 */
Tejun Heo492eb212013-08-08 20:11:25 -04003019struct cgroup_subsys_state *
3020css_next_descendant_pre(struct cgroup_subsys_state *pos,
3021 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003022{
Tejun Heo492eb212013-08-08 20:11:25 -04003023 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003024
3025 WARN_ON_ONCE(!rcu_read_lock_held());
3026
Tejun Heobd8815a2013-08-08 20:11:27 -04003027 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003028 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003029 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003030
3031 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003032 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003033 if (next)
3034 return next;
3035
3036 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003037 while (pos != root) {
3038 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003039 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003040 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003041 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003042 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003043
3044 return NULL;
3045}
Tejun Heo492eb212013-08-08 20:11:25 -04003046EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003047
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003048/**
Tejun Heo492eb212013-08-08 20:11:25 -04003049 * css_rightmost_descendant - return the rightmost descendant of a css
3050 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003051 *
Tejun Heo492eb212013-08-08 20:11:25 -04003052 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3053 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003054 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003055 *
3056 * While this function requires RCU read locking, it doesn't require the
3057 * whole traversal to be contained in a single RCU critical section. This
3058 * function will return the correct rightmost descendant as long as @pos is
3059 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003060 */
Tejun Heo492eb212013-08-08 20:11:25 -04003061struct cgroup_subsys_state *
3062css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003063{
Tejun Heo492eb212013-08-08 20:11:25 -04003064 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003065
3066 WARN_ON_ONCE(!rcu_read_lock_held());
3067
3068 do {
3069 last = pos;
3070 /* ->prev isn't RCU safe, walk ->next till the end */
3071 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003072 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003073 pos = tmp;
3074 } while (pos);
3075
3076 return last;
3077}
Tejun Heo492eb212013-08-08 20:11:25 -04003078EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003079
Tejun Heo492eb212013-08-08 20:11:25 -04003080static struct cgroup_subsys_state *
3081css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082{
Tejun Heo492eb212013-08-08 20:11:25 -04003083 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003084
3085 do {
3086 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003087 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088 } while (pos);
3089
3090 return last;
3091}
3092
3093/**
Tejun Heo492eb212013-08-08 20:11:25 -04003094 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003095 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003096 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 *
Tejun Heo492eb212013-08-08 20:11:25 -04003098 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003099 * to visit for post-order traversal of @root's descendants. @root is
3100 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003101 *
3102 * While this function requires RCU read locking, it doesn't require the
3103 * whole traversal to be contained in a single RCU critical section. This
3104 * function will return the correct next descendant as long as both @pos
3105 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003106 */
Tejun Heo492eb212013-08-08 20:11:25 -04003107struct cgroup_subsys_state *
3108css_next_descendant_post(struct cgroup_subsys_state *pos,
3109 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003110{
Tejun Heo492eb212013-08-08 20:11:25 -04003111 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112
3113 WARN_ON_ONCE(!rcu_read_lock_held());
3114
Tejun Heo58b79a92013-09-06 15:31:08 -04003115 /* if first iteration, visit leftmost descendant which may be @root */
3116 if (!pos)
3117 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118
Tejun Heobd8815a2013-08-08 20:11:27 -04003119 /* if we visited @root, we're done */
3120 if (pos == root)
3121 return NULL;
3122
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003124 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003125 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003126 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003127
3128 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003129 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003130}
Tejun Heo492eb212013-08-08 20:11:25 -04003131EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003132
Tejun Heo0942eee2013-08-08 20:11:26 -04003133/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003134 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003135 * @it: the iterator to advance
3136 *
3137 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003138 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003139static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003140{
3141 struct list_head *l = it->cset_link;
3142 struct cgrp_cset_link *link;
3143 struct css_set *cset;
3144
3145 /* Advance to the next non-empty css_set */
3146 do {
3147 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003148 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003149 it->cset_link = NULL;
3150 return;
3151 }
3152 link = list_entry(l, struct cgrp_cset_link, cset_link);
3153 cset = link->cset;
3154 } while (list_empty(&cset->tasks));
3155 it->cset_link = l;
3156 it->task = cset->tasks.next;
3157}
3158
Tejun Heo0942eee2013-08-08 20:11:26 -04003159/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003160 * css_task_iter_start - initiate task iteration
3161 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003162 * @it: the task iterator to use
3163 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003164 * Initiate iteration through the tasks of @css. The caller can call
3165 * css_task_iter_next() to walk through the tasks until the function
3166 * returns NULL. On completion of iteration, css_task_iter_end() must be
3167 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003168 *
3169 * Note that this function acquires a lock which is released when the
3170 * iteration finishes. The caller can't sleep while iteration is in
3171 * progress.
3172 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003173void css_task_iter_start(struct cgroup_subsys_state *css,
3174 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003175 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003176{
3177 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003178 * The first time anyone tries to iterate across a css, we need to
3179 * enable the list linking each css_set to its tasks, and fix up
3180 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003181 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003182 if (!use_task_css_set_links)
3183 cgroup_enable_task_cg_lists();
3184
Paul Menage817929e2007-10-18 23:39:36 -07003185 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003186
Tejun Heo72ec7022013-08-08 20:11:26 -04003187 it->origin_css = css;
3188 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003189
Tejun Heo72ec7022013-08-08 20:11:26 -04003190 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003191}
3192
Tejun Heo0942eee2013-08-08 20:11:26 -04003193/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003194 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003195 * @it: the task iterator being iterated
3196 *
3197 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003198 * initialized via css_task_iter_start(). Returns NULL when the iteration
3199 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003200 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003201struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003202{
3203 struct task_struct *res;
3204 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003205 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003206
3207 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003208 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003209 return NULL;
3210 res = list_entry(l, struct task_struct, cg_list);
3211 /* Advance iterator to find next entry */
3212 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003213 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3214 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003215 /*
3216 * We reached the end of this task list - move on to the
3217 * next cgrp_cset_link.
3218 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003219 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003220 } else {
3221 it->task = l;
3222 }
3223 return res;
3224}
3225
Tejun Heo0942eee2013-08-08 20:11:26 -04003226/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003227 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003228 * @it: the task iterator to finish
3229 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003230 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003231 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003232void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003233 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003234{
3235 read_unlock(&css_set_lock);
3236}
3237
Cliff Wickman31a7df02008-02-07 00:14:42 -08003238static inline int started_after_time(struct task_struct *t1,
3239 struct timespec *time,
3240 struct task_struct *t2)
3241{
3242 int start_diff = timespec_compare(&t1->start_time, time);
3243 if (start_diff > 0) {
3244 return 1;
3245 } else if (start_diff < 0) {
3246 return 0;
3247 } else {
3248 /*
3249 * Arbitrarily, if two processes started at the same
3250 * time, we'll say that the lower pointer value
3251 * started first. Note that t2 may have exited by now
3252 * so this may not be a valid pointer any longer, but
3253 * that's fine - it still serves to distinguish
3254 * between two tasks started (effectively) simultaneously.
3255 */
3256 return t1 > t2;
3257 }
3258}
3259
3260/*
3261 * This function is a callback from heap_insert() and is used to order
3262 * the heap.
3263 * In this case we order the heap in descending task start time.
3264 */
3265static inline int started_after(void *p1, void *p2)
3266{
3267 struct task_struct *t1 = p1;
3268 struct task_struct *t2 = p2;
3269 return started_after_time(t1, &t2->start_time, t2);
3270}
3271
3272/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003273 * css_scan_tasks - iterate though all the tasks in a css
3274 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003275 * @test: optional test callback
3276 * @process: process callback
3277 * @data: data passed to @test and @process
3278 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003279 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003280 * Iterate through all the tasks in @css, calling @test for each, and if it
3281 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003282 *
Tejun Heoe5358372013-08-08 20:11:26 -04003283 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003284 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003285 * lock css_set_lock for the call to @process.
3286 *
3287 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003288 * of @css for the duration of this call. This function may or may not
3289 * call @process for tasks that exit or move to a different css during the
3290 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003291 *
3292 * Note that @test may be called with locks held, and may in some
3293 * situations be called multiple times for the same task, so it should be
3294 * cheap.
3295 *
3296 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3297 * heap operations (and its "gt" member will be overwritten), else a
3298 * temporary heap will be used (allocation of which may cause this function
3299 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003300 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003301int css_scan_tasks(struct cgroup_subsys_state *css,
3302 bool (*test)(struct task_struct *, void *),
3303 void (*process)(struct task_struct *, void *),
3304 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003305{
3306 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003307 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003308 struct task_struct *p, *dropped;
3309 /* Never dereference latest_task, since it's not refcounted */
3310 struct task_struct *latest_task = NULL;
3311 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003312 struct timespec latest_time = { 0, 0 };
3313
Tejun Heoe5358372013-08-08 20:11:26 -04003314 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003315 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003316 heap->gt = &started_after;
3317 } else {
3318 /* We need to allocate our own heap memory */
3319 heap = &tmp_heap;
3320 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3321 if (retval)
3322 /* cannot allocate the heap */
3323 return retval;
3324 }
3325
3326 again:
3327 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003328 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003329 * which are of interest, and invoking @process callback on the
3330 * ones which need an update. Since we don't want to hold any
3331 * locks during the task updates, gather tasks to be processed in a
3332 * heap structure. The heap is sorted by descending task start
3333 * time. If the statically-sized heap fills up, we overflow tasks
3334 * that started later, and in future iterations only consider tasks
3335 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003336 * guarantees forward progress and that we don't miss any tasks.
3337 */
3338 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003339 css_task_iter_start(css, &it);
3340 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003341 /*
3342 * Only affect tasks that qualify per the caller's callback,
3343 * if he provided one
3344 */
Tejun Heoe5358372013-08-08 20:11:26 -04003345 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003346 continue;
3347 /*
3348 * Only process tasks that started after the last task
3349 * we processed
3350 */
3351 if (!started_after_time(p, &latest_time, latest_task))
3352 continue;
3353 dropped = heap_insert(heap, p);
3354 if (dropped == NULL) {
3355 /*
3356 * The new task was inserted; the heap wasn't
3357 * previously full
3358 */
3359 get_task_struct(p);
3360 } else if (dropped != p) {
3361 /*
3362 * The new task was inserted, and pushed out a
3363 * different task
3364 */
3365 get_task_struct(p);
3366 put_task_struct(dropped);
3367 }
3368 /*
3369 * Else the new task was newer than anything already in
3370 * the heap and wasn't inserted
3371 */
3372 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003373 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003374
3375 if (heap->size) {
3376 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003377 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003378 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003379 latest_time = q->start_time;
3380 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003381 }
3382 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003383 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003384 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003385 }
3386 /*
3387 * If we had to process any tasks at all, scan again
3388 * in case some of them were in the middle of forking
3389 * children that didn't get processed.
3390 * Not the most efficient way to do it, but it avoids
3391 * having to take callback_mutex in the fork path
3392 */
3393 goto again;
3394 }
3395 if (heap == &tmp_heap)
3396 heap_free(&tmp_heap);
3397 return 0;
3398}
3399
Tejun Heoe5358372013-08-08 20:11:26 -04003400static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003401{
Tejun Heoe5358372013-08-08 20:11:26 -04003402 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003403
Tejun Heo47cfcd02013-04-07 09:29:51 -07003404 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003405 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003406 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003407}
3408
3409/**
3410 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3411 * @to: cgroup to which the tasks will be moved
3412 * @from: cgroup in which the tasks currently reside
3413 */
3414int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3415{
Tejun Heo72ec7022013-08-08 20:11:26 -04003416 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3417 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003418}
3419
Paul Menage817929e2007-10-18 23:39:36 -07003420/*
Ben Blum102a7752009-09-23 15:56:26 -07003421 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003422 *
3423 * Reading this file can return large amounts of data if a cgroup has
3424 * *lots* of attached tasks. So it may need several calls to read(),
3425 * but we cannot guarantee that the information we produce is correct
3426 * unless we produce it entirely atomically.
3427 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003428 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003429
Li Zefan24528252012-01-20 11:58:43 +08003430/* which pidlist file are we talking about? */
3431enum cgroup_filetype {
3432 CGROUP_FILE_PROCS,
3433 CGROUP_FILE_TASKS,
3434};
3435
3436/*
3437 * A pidlist is a list of pids that virtually represents the contents of one
3438 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3439 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3440 * to the cgroup.
3441 */
3442struct cgroup_pidlist {
3443 /*
3444 * used to find which pidlist is wanted. doesn't change as long as
3445 * this particular list stays in the list.
3446 */
3447 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3448 /* array of xids */
3449 pid_t *list;
3450 /* how many elements the above list has */
3451 int length;
3452 /* how many files are using the current array */
3453 int use_count;
3454 /* each of these stored in a list by its cgroup */
3455 struct list_head links;
3456 /* pointer to the cgroup we belong to, for list removal purposes */
3457 struct cgroup *owner;
3458 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003459 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003460};
3461
Paul Menagebbcb81d2007-10-18 23:39:32 -07003462/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003463 * The following two functions "fix" the issue where there are more pids
3464 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3465 * TODO: replace with a kernel-wide solution to this problem
3466 */
3467#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3468static void *pidlist_allocate(int count)
3469{
3470 if (PIDLIST_TOO_LARGE(count))
3471 return vmalloc(count * sizeof(pid_t));
3472 else
3473 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3474}
3475static void pidlist_free(void *p)
3476{
3477 if (is_vmalloc_addr(p))
3478 vfree(p);
3479 else
3480 kfree(p);
3481}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003482
3483/*
Ben Blum102a7752009-09-23 15:56:26 -07003484 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003485 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003486 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003487static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003488{
Ben Blum102a7752009-09-23 15:56:26 -07003489 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003490
3491 /*
3492 * we presume the 0th element is unique, so i starts at 1. trivial
3493 * edge cases first; no work needs to be done for either
3494 */
3495 if (length == 0 || length == 1)
3496 return length;
3497 /* src and dest walk down the list; dest counts unique elements */
3498 for (src = 1; src < length; src++) {
3499 /* find next unique element */
3500 while (list[src] == list[src-1]) {
3501 src++;
3502 if (src == length)
3503 goto after;
3504 }
3505 /* dest always points to where the next unique element goes */
3506 list[dest] = list[src];
3507 dest++;
3508 }
3509after:
Ben Blum102a7752009-09-23 15:56:26 -07003510 return dest;
3511}
3512
3513static int cmppid(const void *a, const void *b)
3514{
3515 return *(pid_t *)a - *(pid_t *)b;
3516}
3517
3518/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003519 * find the appropriate pidlist for our purpose (given procs vs tasks)
3520 * returns with the lock on that pidlist already held, and takes care
3521 * of the use count, or returns NULL with no locks held if we're out of
3522 * memory.
3523 */
3524static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3525 enum cgroup_filetype type)
3526{
3527 struct cgroup_pidlist *l;
3528 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003529 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003530
Ben Blum72a8cb32009-09-23 15:56:27 -07003531 /*
Li Zefanb3958902013-08-01 09:52:15 +08003532 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003533 * the last ref-holder is trying to remove l from the list at the same
3534 * time. Holding the pidlist_mutex precludes somebody taking whichever
3535 * list we find out from under us - compare release_pid_array().
3536 */
3537 mutex_lock(&cgrp->pidlist_mutex);
3538 list_for_each_entry(l, &cgrp->pidlists, links) {
3539 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003540 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003541 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003542 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003543 return l;
3544 }
3545 }
3546 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003547 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003548 if (!l) {
3549 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003550 return l;
3551 }
Li Zefanb3958902013-08-01 09:52:15 +08003552 init_rwsem(&l->rwsem);
3553 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003554 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003555 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003556 l->owner = cgrp;
3557 list_add(&l->links, &cgrp->pidlists);
3558 mutex_unlock(&cgrp->pidlist_mutex);
3559 return l;
3560}
3561
3562/*
Ben Blum102a7752009-09-23 15:56:26 -07003563 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3564 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003565static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3566 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003567{
3568 pid_t *array;
3569 int length;
3570 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003571 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003572 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003573 struct cgroup_pidlist *l;
3574
3575 /*
3576 * If cgroup gets more users after we read count, we won't have
3577 * enough space - tough. This race is indistinguishable to the
3578 * caller from the case that the additional cgroup users didn't
3579 * show up until sometime later on.
3580 */
3581 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003582 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003583 if (!array)
3584 return -ENOMEM;
3585 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003586 css_task_iter_start(&cgrp->dummy_css, &it);
3587 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003588 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003589 break;
Ben Blum102a7752009-09-23 15:56:26 -07003590 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003591 if (type == CGROUP_FILE_PROCS)
3592 pid = task_tgid_vnr(tsk);
3593 else
3594 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003595 if (pid > 0) /* make sure to only use valid results */
3596 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003597 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003598 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003599 length = n;
3600 /* now sort & (if procs) strip out duplicates */
3601 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003602 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003603 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003604 l = cgroup_pidlist_find(cgrp, type);
3605 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003606 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003607 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003608 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003609 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003610 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003611 l->list = array;
3612 l->length = length;
3613 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003614 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003615 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003616 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003617}
3618
Balbir Singh846c7bb2007-10-18 23:39:44 -07003619/**
Li Zefana043e3b2008-02-23 15:24:09 -08003620 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003621 * @stats: cgroupstats to fill information into
3622 * @dentry: A dentry entry belonging to the cgroup for which stats have
3623 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003624 *
3625 * Build and fill cgroupstats so that taskstats can export it to user
3626 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003627 */
3628int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3629{
3630 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003631 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003632 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003633 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003634
Balbir Singh846c7bb2007-10-18 23:39:44 -07003635 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003636 * Validate dentry by checking the superblock operations,
3637 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003638 */
Li Zefan33d283b2008-11-19 15:36:48 -08003639 if (dentry->d_sb->s_op != &cgroup_ops ||
3640 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003641 goto err;
3642
3643 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003644 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003645
Tejun Heo72ec7022013-08-08 20:11:26 -04003646 css_task_iter_start(&cgrp->dummy_css, &it);
3647 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003648 switch (tsk->state) {
3649 case TASK_RUNNING:
3650 stats->nr_running++;
3651 break;
3652 case TASK_INTERRUPTIBLE:
3653 stats->nr_sleeping++;
3654 break;
3655 case TASK_UNINTERRUPTIBLE:
3656 stats->nr_uninterruptible++;
3657 break;
3658 case TASK_STOPPED:
3659 stats->nr_stopped++;
3660 break;
3661 default:
3662 if (delayacct_is_task_waiting_on_io(tsk))
3663 stats->nr_io_wait++;
3664 break;
3665 }
3666 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003667 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003668
Balbir Singh846c7bb2007-10-18 23:39:44 -07003669err:
3670 return ret;
3671}
3672
Paul Menage8f3ff202009-09-23 15:56:25 -07003673
Paul Menagecc31edc2008-10-18 20:28:04 -07003674/*
Ben Blum102a7752009-09-23 15:56:26 -07003675 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003676 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003677 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003678 */
3679
Ben Blum102a7752009-09-23 15:56:26 -07003680static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003681{
3682 /*
3683 * Initially we receive a position value that corresponds to
3684 * one more than the last pid shown (or 0 on the first call or
3685 * after a seek to the start). Use a binary-search to find the
3686 * next pid to display, if any
3687 */
Ben Blum102a7752009-09-23 15:56:26 -07003688 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003689 int index = 0, pid = *pos;
3690 int *iter;
3691
Li Zefanb3958902013-08-01 09:52:15 +08003692 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003693 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003694 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003695
Paul Menagecc31edc2008-10-18 20:28:04 -07003696 while (index < end) {
3697 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003698 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003699 index = mid;
3700 break;
Ben Blum102a7752009-09-23 15:56:26 -07003701 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 index = mid + 1;
3703 else
3704 end = mid;
3705 }
3706 }
3707 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003708 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003709 return NULL;
3710 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003711 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003712 *pos = *iter;
3713 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003714}
3715
Ben Blum102a7752009-09-23 15:56:26 -07003716static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003717{
Ben Blum102a7752009-09-23 15:56:26 -07003718 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003719 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003720}
3721
Ben Blum102a7752009-09-23 15:56:26 -07003722static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003723{
Ben Blum102a7752009-09-23 15:56:26 -07003724 struct cgroup_pidlist *l = s->private;
3725 pid_t *p = v;
3726 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003727 /*
3728 * Advance to the next pid in the array. If this goes off the
3729 * end, we're done
3730 */
3731 p++;
3732 if (p >= end) {
3733 return NULL;
3734 } else {
3735 *pos = *p;
3736 return p;
3737 }
3738}
3739
Ben Blum102a7752009-09-23 15:56:26 -07003740static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003741{
3742 return seq_printf(s, "%d\n", *(int *)v);
3743}
3744
Ben Blum102a7752009-09-23 15:56:26 -07003745/*
3746 * seq_operations functions for iterating on pidlists through seq_file -
3747 * independent of whether it's tasks or procs
3748 */
3749static const struct seq_operations cgroup_pidlist_seq_operations = {
3750 .start = cgroup_pidlist_start,
3751 .stop = cgroup_pidlist_stop,
3752 .next = cgroup_pidlist_next,
3753 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003754};
3755
Ben Blum102a7752009-09-23 15:56:26 -07003756static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003757{
Ben Blum72a8cb32009-09-23 15:56:27 -07003758 /*
3759 * the case where we're the last user of this particular pidlist will
3760 * have us remove it from the cgroup's list, which entails taking the
3761 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3762 * pidlist_mutex, we have to take pidlist_mutex first.
3763 */
3764 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003765 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003766 BUG_ON(!l->use_count);
3767 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003768 /* we're the last user if refcount is 0; remove and free */
3769 list_del(&l->links);
3770 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003771 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003772 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003773 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003774 kfree(l);
3775 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003776 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003777 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003778 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003779}
3780
Ben Blum102a7752009-09-23 15:56:26 -07003781static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003782{
Ben Blum102a7752009-09-23 15:56:26 -07003783 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003784 if (!(file->f_mode & FMODE_READ))
3785 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003786 /*
3787 * the seq_file will only be initialized if the file was opened for
3788 * reading; hence we check if it's not null only in that case.
3789 */
3790 l = ((struct seq_file *)file->private_data)->private;
3791 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003792 return seq_release(inode, file);
3793}
3794
Ben Blum102a7752009-09-23 15:56:26 -07003795static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003796 .read = seq_read,
3797 .llseek = seq_lseek,
3798 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003799 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003800};
3801
3802/*
Ben Blum102a7752009-09-23 15:56:26 -07003803 * The following functions handle opens on a file that displays a pidlist
3804 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3805 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003806 */
Ben Blum102a7752009-09-23 15:56:26 -07003807/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003808static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003809{
3810 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003811 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003812 int retval;
3813
3814 /* Nothing to do for write-only files */
3815 if (!(file->f_mode & FMODE_READ))
3816 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003817
Ben Blum102a7752009-09-23 15:56:26 -07003818 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003819 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003820 if (retval)
3821 return retval;
3822 /* configure file information */
3823 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003824
Ben Blum102a7752009-09-23 15:56:26 -07003825 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003826 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003827 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003828 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003829 }
Ben Blum102a7752009-09-23 15:56:26 -07003830 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003831 return 0;
3832}
Ben Blum102a7752009-09-23 15:56:26 -07003833static int cgroup_tasks_open(struct inode *unused, struct file *file)
3834{
Ben Blum72a8cb32009-09-23 15:56:27 -07003835 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003836}
3837static int cgroup_procs_open(struct inode *unused, struct file *file)
3838{
Ben Blum72a8cb32009-09-23 15:56:27 -07003839 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003840}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003841
Tejun Heo182446d2013-08-08 20:11:24 -04003842static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3843 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003844{
Tejun Heo182446d2013-08-08 20:11:24 -04003845 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003846}
3847
Tejun Heo182446d2013-08-08 20:11:24 -04003848static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3849 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003850{
Tejun Heo182446d2013-08-08 20:11:24 -04003851 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003852 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003853 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003854 else
Tejun Heo182446d2013-08-08 20:11:24 -04003855 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003856 return 0;
3857}
3858
Paul Menagebbcb81d2007-10-18 23:39:32 -07003859/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003860 * When dput() is called asynchronously, if umount has been done and
3861 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3862 * there's a small window that vfs will see the root dentry with non-zero
3863 * refcnt and trigger BUG().
3864 *
3865 * That's why we hold a reference before dput() and drop it right after.
3866 */
3867static void cgroup_dput(struct cgroup *cgrp)
3868{
3869 struct super_block *sb = cgrp->root->sb;
3870
3871 atomic_inc(&sb->s_active);
3872 dput(cgrp->dentry);
3873 deactivate_super(sb);
3874}
3875
Tejun Heo182446d2013-08-08 20:11:24 -04003876static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
3877 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003878{
Tejun Heo182446d2013-08-08 20:11:24 -04003879 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003880}
3881
Tejun Heo182446d2013-08-08 20:11:24 -04003882static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
3883 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07003884{
3885 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003886 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003887 else
Tejun Heo182446d2013-08-08 20:11:24 -04003888 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07003889 return 0;
3890}
3891
Tejun Heod5c56ce2013-06-03 19:14:34 -07003892static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07003893 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07003894 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07003895 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07003896 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07003897 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07003898 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07003899 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07003900 {
Daniel Lezcano97978e62010-10-27 15:33:35 -07003901 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07003902 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07003903 .read_u64 = cgroup_clone_children_read,
3904 .write_u64 = cgroup_clone_children_write,
3905 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07003906 {
Tejun Heo873fe092013-04-14 20:15:26 -07003907 .name = "cgroup.sane_behavior",
3908 .flags = CFTYPE_ONLY_ON_ROOT,
3909 .read_seq_string = cgroup_sane_behavior_show,
3910 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07003911
3912 /*
3913 * Historical crazy stuff. These don't have "cgroup." prefix and
3914 * don't exist if sane_behavior. If you're depending on these, be
3915 * prepared to be burned.
3916 */
3917 {
3918 .name = "tasks",
3919 .flags = CFTYPE_INSANE, /* use "procs" instead */
3920 .open = cgroup_tasks_open,
3921 .write_u64 = cgroup_tasks_write,
3922 .release = cgroup_pidlist_release,
3923 .mode = S_IRUGO | S_IWUSR,
3924 },
3925 {
3926 .name = "notify_on_release",
3927 .flags = CFTYPE_INSANE,
3928 .read_u64 = cgroup_read_notify_on_release,
3929 .write_u64 = cgroup_write_notify_on_release,
3930 },
Tejun Heo873fe092013-04-14 20:15:26 -07003931 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07003932 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07003933 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07003934 .read_seq_string = cgroup_release_agent_show,
3935 .write_string = cgroup_release_agent_write,
3936 .max_write_len = PATH_MAX,
3937 },
Tejun Heodb0416b2012-04-01 12:09:55 -07003938 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003939};
3940
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003941/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07003942 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003943 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003944 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07003945 *
3946 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003947 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07003948static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07003949{
Paul Menageddbcc7e2007-10-18 23:39:30 -07003950 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07003951 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003952
Tejun Heo8e3f6542012-04-01 12:09:55 -07003953 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07003954 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07003955 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07003956
3957 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04003958 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07003959
Tejun Heobee55092013-06-28 16:24:11 -07003960 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04003961 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07003962 if (ret < 0)
3963 goto err;
3964 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003965 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07003966 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07003967err:
3968 cgroup_clear_dir(cgrp, subsys_mask);
3969 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07003970}
3971
Tejun Heo0c21ead2013-08-13 20:22:51 -04003972/*
3973 * css destruction is four-stage process.
3974 *
3975 * 1. Destruction starts. Killing of the percpu_ref is initiated.
3976 * Implemented in kill_css().
3977 *
3978 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
3979 * and thus css_tryget() is guaranteed to fail, the css can be offlined
3980 * by invoking offline_css(). After offlining, the base ref is put.
3981 * Implemented in css_killed_work_fn().
3982 *
3983 * 3. When the percpu_ref reaches zero, the only possible remaining
3984 * accessors are inside RCU read sections. css_release() schedules the
3985 * RCU callback.
3986 *
3987 * 4. After the grace period, the css can be freed. Implemented in
3988 * css_free_work_fn().
3989 *
3990 * It is actually hairier because both step 2 and 4 require process context
3991 * and thus involve punting to css->destroy_work adding two additional
3992 * steps to the already complex sequence.
3993 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04003994static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07003995{
3996 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04003997 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04003998 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07003999
Tejun Heo0ae78e02013-08-13 11:01:54 -04004000 if (css->parent)
4001 css_put(css->parent);
4002
Tejun Heo0c21ead2013-08-13 20:22:51 -04004003 css->ss->css_free(css);
4004 cgroup_dput(cgrp);
4005}
4006
4007static void css_free_rcu_fn(struct rcu_head *rcu_head)
4008{
4009 struct cgroup_subsys_state *css =
4010 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4011
4012 /*
4013 * css holds an extra ref to @cgrp->dentry which is put on the last
4014 * css_put(). dput() requires process context which we don't have.
4015 */
4016 INIT_WORK(&css->destroy_work, css_free_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004017 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004018}
4019
Tejun Heod3daf282013-06-13 19:39:16 -07004020static void css_release(struct percpu_ref *ref)
4021{
4022 struct cgroup_subsys_state *css =
4023 container_of(ref, struct cgroup_subsys_state, refcnt);
4024
Tejun Heo0c21ead2013-08-13 20:22:51 -04004025 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004026}
4027
Tejun Heo623f9262013-08-13 11:01:55 -04004028static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4029 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004030{
Paul Menagebd89aab2007-10-18 23:40:44 -07004031 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004032 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004033 css->flags = 0;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004034
Tejun Heo0ae78e02013-08-13 11:01:54 -04004035 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004036 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004037 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004038 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004039
Tejun Heoca8bdca2013-08-26 18:40:56 -04004040 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004041}
4042
Li Zefan2a4ac632013-07-31 16:16:40 +08004043/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004044static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004045{
Tejun Heo623f9262013-08-13 11:01:55 -04004046 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004047 int ret = 0;
4048
Tejun Heoa31f2d32012-11-19 08:13:37 -08004049 lockdep_assert_held(&cgroup_mutex);
4050
Tejun Heo92fb9742012-11-19 08:13:38 -08004051 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004052 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004053 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004054 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004055 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004056 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4057 }
Tejun Heob1929db2012-11-19 08:13:38 -08004058 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004059}
4060
Li Zefan2a4ac632013-07-31 16:16:40 +08004061/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004062static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004063{
Tejun Heo623f9262013-08-13 11:01:55 -04004064 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004065
4066 lockdep_assert_held(&cgroup_mutex);
4067
4068 if (!(css->flags & CSS_ONLINE))
4069 return;
4070
Li Zefand7eeac12013-03-12 15:35:59 -07004071 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004072 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004073
Tejun Heoeb954192013-08-08 20:11:23 -04004074 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004075 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004076 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004077}
4078
Paul Menageddbcc7e2007-10-18 23:39:30 -07004079/*
Li Zefana043e3b2008-02-23 15:24:09 -08004080 * cgroup_create - create a cgroup
4081 * @parent: cgroup that will be parent of the new cgroup
4082 * @dentry: dentry of the new cgroup
4083 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004084 *
Li Zefana043e3b2008-02-23 15:24:09 -08004085 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004086 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004087static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004088 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004089{
Tejun Heoae7f1642013-08-13 20:22:50 -04004090 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004091 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004092 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004093 struct cgroupfs_root *root = parent->root;
4094 int err = 0;
4095 struct cgroup_subsys *ss;
4096 struct super_block *sb = root->sb;
4097
Tejun Heo0a950f62012-11-19 09:02:12 -08004098 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004099 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4100 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004101 return -ENOMEM;
4102
Li Zefan65dff752013-03-01 15:01:56 +08004103 name = cgroup_alloc_name(dentry);
4104 if (!name)
4105 goto err_free_cgrp;
4106 rcu_assign_pointer(cgrp->name, name);
4107
Li Zefan4e96ee82013-07-31 09:50:50 +08004108 /*
4109 * Temporarily set the pointer to NULL, so idr_find() won't return
4110 * a half-baked cgroup.
4111 */
4112 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004113 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004114 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004115
Tejun Heo976c06b2012-11-05 09:16:59 -08004116 /*
4117 * Only live parents can have children. Note that the liveliness
4118 * check isn't strictly necessary because cgroup_mkdir() and
4119 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4120 * anyway so that locking is contained inside cgroup proper and we
4121 * don't get nasty surprises if we ever grow another caller.
4122 */
4123 if (!cgroup_lock_live_group(parent)) {
4124 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004125 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004126 }
4127
Paul Menageddbcc7e2007-10-18 23:39:30 -07004128 /* Grab a reference on the superblock so the hierarchy doesn't
4129 * get deleted on unmount if there are child cgroups. This
4130 * can be done outside cgroup_mutex, since the sb can't
4131 * disappear while someone has an open control file on the
4132 * fs */
4133 atomic_inc(&sb->s_active);
4134
Paul Menagecc31edc2008-10-18 20:28:04 -07004135 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004136
Li Zefanfe1c06c2013-01-24 14:30:22 +08004137 dentry->d_fsdata = cgrp;
4138 cgrp->dentry = dentry;
4139
Paul Menagebd89aab2007-10-18 23:40:44 -07004140 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004141 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004142 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004143
Li Zefanb6abdb02008-03-04 14:28:19 -08004144 if (notify_on_release(parent))
4145 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4146
Tejun Heo2260e7f2012-11-19 08:13:38 -08004147 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4148 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004149
Tejun Heo5549c492013-06-24 15:21:48 -07004150 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004151 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004152
Tejun Heoca8bdca2013-08-26 18:40:56 -04004153 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004154 if (IS_ERR(css)) {
4155 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004156 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004157 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004158 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004159
4160 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004161 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004162 goto err_free_all;
4163
Tejun Heo623f9262013-08-13 11:01:55 -04004164 init_css(css, ss, cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004165 }
4166
Tejun Heo4e139af2012-11-19 08:13:36 -08004167 /*
4168 * Create directory. cgroup_create_file() returns with the new
4169 * directory locked on success so that it can be populated without
4170 * dropping cgroup_mutex.
4171 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004172 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004173 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004174 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004175 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004176
Tejun Heo00356bd2013-06-18 11:14:22 -07004177 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004178
Tejun Heo4e139af2012-11-19 08:13:36 -08004179 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004180 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4181 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004182
Tejun Heo0ae78e02013-08-13 11:01:54 -04004183 /* each css holds a ref to the cgroup's dentry and the parent css */
4184 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004185 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004186
Tejun Heoed9577932012-11-05 09:16:58 -08004187 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004188 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004189 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004190
Li Zefan415cf072013-04-08 14:35:02 +08004191 /* hold a ref to the parent's dentry */
4192 dget(parent->dentry);
4193
Tejun Heob1929db2012-11-19 08:13:38 -08004194 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004195 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004196 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004197
4198 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004199 if (err)
4200 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004201
4202 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4203 parent->parent) {
4204 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",
4205 current->comm, current->pid, ss->name);
4206 if (!strcmp(ss->name, "memory"))
4207 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4208 ss->warned_broken_hierarchy = true;
4209 }
Tejun Heoa8638032012-11-09 09:12:29 -08004210 }
4211
Li Zefan4e96ee82013-07-31 09:50:50 +08004212 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4213
Tejun Heo2bb566c2013-08-08 20:11:23 -04004214 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004215 if (err)
4216 goto err_destroy;
4217
4218 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004219 if (err)
4220 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221
4222 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004223 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224
4225 return 0;
4226
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004227err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004228 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004229 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004230
4231 if (css) {
4232 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004233 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004234 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004235 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237 /* Release the reference count that we took on the superblock */
4238 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004239err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004240 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004241err_free_name:
4242 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004243err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004244 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004245 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004246
4247err_destroy:
4248 cgroup_destroy_locked(cgrp);
4249 mutex_unlock(&cgroup_mutex);
4250 mutex_unlock(&dentry->d_inode->i_mutex);
4251 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252}
4253
Al Viro18bb1db2011-07-26 01:41:39 -04004254static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255{
4256 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4257
4258 /* the vfs holds inode->i_mutex already */
4259 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4260}
4261
Tejun Heo223dbc32013-08-13 20:22:50 -04004262/*
4263 * This is called when the refcnt of a css is confirmed to be killed.
4264 * css_tryget() is now guaranteed to fail.
4265 */
4266static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004267{
Tejun Heo223dbc32013-08-13 20:22:50 -04004268 struct cgroup_subsys_state *css =
4269 container_of(work, struct cgroup_subsys_state, destroy_work);
4270 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004271
Tejun Heof20104d2013-08-13 20:22:50 -04004272 mutex_lock(&cgroup_mutex);
4273
4274 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004275 * css_tryget() is guaranteed to fail now. Tell subsystems to
4276 * initate destruction.
4277 */
4278 offline_css(css);
4279
4280 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004281 * If @cgrp is marked dead, it's waiting for refs of all css's to
4282 * be disabled before proceeding to the second phase of cgroup
4283 * destruction. If we are the last one, kick it off.
4284 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004285 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004286 cgroup_destroy_css_killed(cgrp);
4287
4288 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004289
4290 /*
4291 * Put the css refs from kill_css(). Each css holds an extra
4292 * reference to the cgroup's dentry and cgroup removal proceeds
4293 * regardless of css refs. On the last put of each css, whenever
4294 * that may be, the extra dentry ref is put so that dentry
4295 * destruction happens only after all css's are released.
4296 */
4297 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004298}
4299
Tejun Heo223dbc32013-08-13 20:22:50 -04004300/* css kill confirmation processing requires process context, bounce */
4301static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004302{
4303 struct cgroup_subsys_state *css =
4304 container_of(ref, struct cgroup_subsys_state, refcnt);
4305
Tejun Heo223dbc32013-08-13 20:22:50 -04004306 INIT_WORK(&css->destroy_work, css_killed_work_fn);
Tejun Heoe5fca242013-11-22 17:14:39 -05004307 queue_work(cgroup_destroy_wq, &css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004308}
4309
4310/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004311 * kill_css - destroy a css
4312 * @css: css to destroy
4313 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004314 * This function initiates destruction of @css by removing cgroup interface
4315 * files and putting its base reference. ->css_offline() will be invoked
4316 * asynchronously once css_tryget() is guaranteed to fail and when the
4317 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004318 */
4319static void kill_css(struct cgroup_subsys_state *css)
4320{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004321 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4322
Tejun Heoedae0c32013-08-13 20:22:51 -04004323 /*
4324 * Killing would put the base ref, but we need to keep it alive
4325 * until after ->css_offline().
4326 */
4327 css_get(css);
4328
4329 /*
4330 * cgroup core guarantees that, by the time ->css_offline() is
4331 * invoked, no new css reference will be given out via
4332 * css_tryget(). We can't simply call percpu_ref_kill() and
4333 * proceed to offlining css's because percpu_ref_kill() doesn't
4334 * guarantee that the ref is seen as killed on all CPUs on return.
4335 *
4336 * Use percpu_ref_kill_and_confirm() to get notifications as each
4337 * css is confirmed to be seen as killed on all CPUs.
4338 */
4339 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004340}
4341
4342/**
4343 * cgroup_destroy_locked - the first stage of cgroup destruction
4344 * @cgrp: cgroup to be destroyed
4345 *
4346 * css's make use of percpu refcnts whose killing latency shouldn't be
4347 * exposed to userland and are RCU protected. Also, cgroup core needs to
4348 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4349 * invoked. To satisfy all the requirements, destruction is implemented in
4350 * the following two steps.
4351 *
4352 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4353 * userland visible parts and start killing the percpu refcnts of
4354 * css's. Set up so that the next stage will be kicked off once all
4355 * the percpu refcnts are confirmed to be killed.
4356 *
4357 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4358 * rest of destruction. Once all cgroup references are gone, the
4359 * cgroup is RCU-freed.
4360 *
4361 * This function implements s1. After this step, @cgrp is gone as far as
4362 * the userland is concerned and a new cgroup with the same name may be
4363 * created. As cgroup doesn't care about the names internally, this
4364 * doesn't cause any problem.
4365 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004366static int cgroup_destroy_locked(struct cgroup *cgrp)
4367 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004368{
Tejun Heo42809dd2012-11-19 08:13:37 -08004369 struct dentry *d = cgrp->dentry;
Tejun Heoed9577932012-11-05 09:16:58 -08004370 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004371 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004372 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004373
Tejun Heo42809dd2012-11-19 08:13:37 -08004374 lockdep_assert_held(&d->d_inode->i_mutex);
4375 lockdep_assert_held(&cgroup_mutex);
4376
Tejun Heoddd69142013-06-12 21:04:54 -07004377 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004378 * css_set_lock synchronizes access to ->cset_links and prevents
4379 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004380 */
4381 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004382 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004383 read_unlock(&css_set_lock);
4384 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004386
Tejun Heo1a90dd52012-11-05 09:16:59 -08004387 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004388 * Make sure there's no live children. We can't test ->children
4389 * emptiness as dead children linger on it while being destroyed;
4390 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4391 */
4392 empty = true;
4393 rcu_read_lock();
4394 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4395 empty = cgroup_is_dead(child);
4396 if (!empty)
4397 break;
4398 }
4399 rcu_read_unlock();
4400 if (!empty)
4401 return -EBUSY;
4402
4403 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004404 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4405 * will be invoked to perform the rest of destruction once the
4406 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004407 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004408 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004409 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004410
4411 /*
4412 * Mark @cgrp dead. This prevents further task migration and child
4413 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004414 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004415 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004416 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004417 */
Tejun Heo54766d42013-06-12 21:04:53 -07004418 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004419
Tejun Heo455050d2013-06-13 19:27:41 -07004420 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4421 raw_spin_lock(&release_list_lock);
4422 if (!list_empty(&cgrp->release_list))
4423 list_del_init(&cgrp->release_list);
4424 raw_spin_unlock(&release_list_lock);
4425
4426 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004427 * If @cgrp has css's attached, the second stage of cgroup
4428 * destruction is kicked off from css_killed_work_fn() after the
4429 * refs of all attached css's are killed. If @cgrp doesn't have
4430 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004431 */
Tejun Heof20104d2013-08-13 20:22:50 -04004432 if (!cgrp->nr_css)
4433 cgroup_destroy_css_killed(cgrp);
4434
4435 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004436 * Clear the base files and remove @cgrp directory. The removal
4437 * puts the base ref but we aren't quite done with @cgrp yet, so
4438 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004439 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004440 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004441 dget(d);
4442 cgroup_d_remove_dir(d);
4443
Tejun Heoea15f8c2013-06-13 19:27:42 -07004444 return 0;
4445};
4446
Tejun Heod3daf282013-06-13 19:39:16 -07004447/**
Tejun Heof20104d2013-08-13 20:22:50 -04004448 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004449 * @work: cgroup->destroy_free_work
4450 *
4451 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004452 * destroyed after all css's are offlined and performs the rest of
4453 * destruction. This is the second step of destruction described in the
4454 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004455 */
Tejun Heof20104d2013-08-13 20:22:50 -04004456static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004457{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004458 struct cgroup *parent = cgrp->parent;
4459 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004460
Tejun Heof20104d2013-08-13 20:22:50 -04004461 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004462
Paul Menage999cd8a2009-01-07 18:08:36 -08004463 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004464 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004465
Li Zefan4e96ee82013-07-31 09:50:50 +08004466 /*
4467 * We should remove the cgroup object from idr before its grace
4468 * period starts, so we won't be looking up a cgroup while the
4469 * cgroup is being freed.
4470 */
4471 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4472 cgrp->id = -1;
4473
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004475
Paul Menagebd89aab2007-10-18 23:40:44 -07004476 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004477 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004478}
4479
Tejun Heo42809dd2012-11-19 08:13:37 -08004480static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4481{
4482 int ret;
4483
4484 mutex_lock(&cgroup_mutex);
4485 ret = cgroup_destroy_locked(dentry->d_fsdata);
4486 mutex_unlock(&cgroup_mutex);
4487
4488 return ret;
4489}
4490
Tejun Heo8e3f6542012-04-01 12:09:55 -07004491static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4492{
4493 INIT_LIST_HEAD(&ss->cftsets);
4494
4495 /*
4496 * base_cftset is embedded in subsys itself, no need to worry about
4497 * deregistration.
4498 */
4499 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004500 struct cftype *cft;
4501
4502 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4503 cft->ss = ss;
4504
Tejun Heo8e3f6542012-04-01 12:09:55 -07004505 ss->base_cftset.cfts = ss->base_cftypes;
4506 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4507 }
4508}
4509
Li Zefan06a11922008-04-29 01:00:07 -07004510static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004511{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004512 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004513
4514 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004515
Tejun Heo648bb562012-11-19 08:13:36 -08004516 mutex_lock(&cgroup_mutex);
4517
Tejun Heo8e3f6542012-04-01 12:09:55 -07004518 /* init base cftset */
4519 cgroup_init_cftsets(ss);
4520
Paul Menageddbcc7e2007-10-18 23:39:30 -07004521 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004522 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4523 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004524 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004525 /* We don't handle early failures gracefully */
4526 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004527 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004528
Li Zefane8d55fd2008-04-29 01:00:13 -07004529 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004530 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004531 * newly registered, all tasks and hence the
4532 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004533 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004534
4535 need_forkexit_callback |= ss->fork || ss->exit;
4536
Li Zefane8d55fd2008-04-29 01:00:13 -07004537 /* At system boot, before all subsystems have been
4538 * registered, no tasks have been forked, so we don't
4539 * need to invoke fork callbacks here. */
4540 BUG_ON(!list_empty(&init_task.tasks));
4541
Tejun Heoae7f1642013-08-13 20:22:50 -04004542 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004543
Tejun Heo648bb562012-11-19 08:13:36 -08004544 mutex_unlock(&cgroup_mutex);
4545
Ben Blume6a11052010-03-10 15:22:09 -08004546 /* this function shouldn't be used with modular subsystems, since they
4547 * need to register a subsys_id, among other things */
4548 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004549}
4550
4551/**
Ben Blume6a11052010-03-10 15:22:09 -08004552 * cgroup_load_subsys: load and register a modular subsystem at runtime
4553 * @ss: the subsystem to load
4554 *
4555 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004556 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004557 * up for use. If the subsystem is built-in anyway, work is delegated to the
4558 * simpler cgroup_init_subsys.
4559 */
4560int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4561{
Ben Blume6a11052010-03-10 15:22:09 -08004562 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004563 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004564 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004565 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004566 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004567
4568 /* check name and function validity */
4569 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004570 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004571 return -EINVAL;
4572
4573 /*
4574 * we don't support callbacks in modular subsystems. this check is
4575 * before the ss->module check for consistency; a subsystem that could
4576 * be a module should still have no callbacks even if the user isn't
4577 * compiling it as one.
4578 */
4579 if (ss->fork || ss->exit)
4580 return -EINVAL;
4581
4582 /*
4583 * an optionally modular subsystem is built-in: we want to do nothing,
4584 * since cgroup_init_subsys will have already taken care of it.
4585 */
4586 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004587 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004588 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004589 return 0;
4590 }
4591
Tejun Heo8e3f6542012-04-01 12:09:55 -07004592 /* init base cftset */
4593 cgroup_init_cftsets(ss);
4594
Ben Blume6a11052010-03-10 15:22:09 -08004595 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004596 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004597
4598 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004599 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004600 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004601 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004602 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004603 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004604 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004605 /* failure case - need to deassign the cgroup_subsys[] slot. */
4606 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004607 mutex_unlock(&cgroup_mutex);
4608 return PTR_ERR(css);
4609 }
4610
Tejun Heo9871bf92013-06-24 15:21:47 -07004611 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4612 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004613
4614 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004615 init_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004616
4617 /*
4618 * Now we need to entangle the css into the existing css_sets. unlike
4619 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4620 * will need a new pointer to it; done by iterating the css_set_table.
4621 * furthermore, modifying the existing css_sets will corrupt the hash
4622 * table state, so each changed css_set will need its hash recomputed.
4623 * this is all done under the css_set_lock.
4624 */
4625 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004626 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004627 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004628 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004629 continue;
4630 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004631 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004632 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004633 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004634 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004635 key = css_set_hash(cset->subsys);
4636 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004637 }
4638 write_unlock(&css_set_lock);
4639
Tejun Heoae7f1642013-08-13 20:22:50 -04004640 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004641 if (ret)
4642 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004643
Ben Blume6a11052010-03-10 15:22:09 -08004644 /* success! */
4645 mutex_unlock(&cgroup_mutex);
4646 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004647
4648err_unload:
4649 mutex_unlock(&cgroup_mutex);
4650 /* @ss can't be mounted here as try_module_get() would fail */
4651 cgroup_unload_subsys(ss);
4652 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004653}
4654EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4655
4656/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004657 * cgroup_unload_subsys: unload a modular subsystem
4658 * @ss: the subsystem to unload
4659 *
4660 * This function should be called in a modular subsystem's exitcall. When this
4661 * function is invoked, the refcount on the subsystem's module will be 0, so
4662 * the subsystem will not be attached to any hierarchy.
4663 */
4664void cgroup_unload_subsys(struct cgroup_subsys *ss)
4665{
Tejun Heo69d02062013-06-12 21:04:50 -07004666 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004667
4668 BUG_ON(ss->module == NULL);
4669
4670 /*
4671 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004672 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004673 * doesn't start being used while we're killing it off.
4674 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004675 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004676
4677 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004678
Tejun Heoca8bdca2013-08-26 18:40:56 -04004679 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004680
Ben Blumcf5d5942010-03-10 15:22:09 -08004681 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004682 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004683
Tejun Heo9871bf92013-06-24 15:21:47 -07004684 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004685 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004686
4687 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004688 * disentangle the css from all css_sets attached to the dummy
4689 * top. as in loading, we need to pay our respects to the hashtable
4690 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004691 */
4692 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004693 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004694 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004695 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004696
Tejun Heo5abb8852013-06-12 21:04:49 -07004697 hash_del(&cset->hlist);
4698 cset->subsys[ss->subsys_id] = NULL;
4699 key = css_set_hash(cset->subsys);
4700 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004701 }
4702 write_unlock(&css_set_lock);
4703
4704 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004705 * remove subsystem's css from the cgroup_dummy_top and free it -
4706 * need to free before marking as null because ss->css_free needs
Li Zefan2ff2a7d2013-09-23 16:57:03 +08004707 * the cgrp->subsys pointer to find their state.
Ben Blumcf5d5942010-03-10 15:22:09 -08004708 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004709 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04004710 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08004711
4712 mutex_unlock(&cgroup_mutex);
4713}
4714EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4715
4716/**
Li Zefana043e3b2008-02-23 15:24:09 -08004717 * cgroup_init_early - cgroup initialization at system boot
4718 *
4719 * Initialize cgroups at system boot, and initialize any
4720 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004721 */
4722int __init cgroup_init_early(void)
4723{
Tejun Heo30159ec2013-06-25 11:53:37 -07004724 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004725 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004726
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004727 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004728 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004729 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004730 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004731 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004732 init_cgroup_root(&cgroup_dummy_root);
4733 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004734 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004735
Tejun Heo69d02062013-06-12 21:04:50 -07004736 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004737 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4738 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004739 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004740
Tejun Heo30159ec2013-06-25 11:53:37 -07004741 /* at bootup time, we don't worry about modular subsystems */
4742 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004743 BUG_ON(!ss->name);
4744 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004745 BUG_ON(!ss->css_alloc);
4746 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004747 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004748 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004749 ss->name, ss->subsys_id);
4750 BUG();
4751 }
4752
4753 if (ss->early_init)
4754 cgroup_init_subsys(ss);
4755 }
4756 return 0;
4757}
4758
4759/**
Li Zefana043e3b2008-02-23 15:24:09 -08004760 * cgroup_init - cgroup initialization
4761 *
4762 * Register cgroup filesystem and /proc file, and initialize
4763 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004764 */
4765int __init cgroup_init(void)
4766{
Tejun Heo30159ec2013-06-25 11:53:37 -07004767 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004768 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004769 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004770
4771 err = bdi_init(&cgroup_backing_dev_info);
4772 if (err)
4773 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004774
Tejun Heo30159ec2013-06-25 11:53:37 -07004775 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776 if (!ss->early_init)
4777 cgroup_init_subsys(ss);
4778 }
4779
Tejun Heofa3ca072013-04-14 11:36:56 -07004780 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004781 mutex_lock(&cgroup_mutex);
4782 mutex_lock(&cgroup_root_mutex);
4783
Tejun Heo82fe9b02013-06-25 11:53:37 -07004784 /* Add init_css_set to the hash table */
4785 key = css_set_hash(init_css_set.subsys);
4786 hash_add(css_set_table, &init_css_set.hlist, key);
4787
Tejun Heofc76df72013-06-25 11:53:37 -07004788 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004789
Li Zefan4e96ee82013-07-31 09:50:50 +08004790 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4791 0, 1, GFP_KERNEL);
4792 BUG_ON(err < 0);
4793
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004794 mutex_unlock(&cgroup_root_mutex);
4795 mutex_unlock(&cgroup_mutex);
4796
Greg KH676db4a2010-08-05 13:53:35 -07004797 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4798 if (!cgroup_kobj) {
4799 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004800 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004801 }
4802
4803 err = register_filesystem(&cgroup_fs_type);
4804 if (err < 0) {
4805 kobject_put(cgroup_kobj);
4806 goto out;
4807 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004808
Li Zefan46ae2202008-04-29 01:00:08 -07004809 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004810
Paul Menageddbcc7e2007-10-18 23:39:30 -07004811out:
Paul Menagea4243162007-10-18 23:39:35 -07004812 if (err)
4813 bdi_destroy(&cgroup_backing_dev_info);
4814
Paul Menageddbcc7e2007-10-18 23:39:30 -07004815 return err;
4816}
Paul Menageb4f48b62007-10-18 23:39:33 -07004817
Tejun Heoe5fca242013-11-22 17:14:39 -05004818static int __init cgroup_wq_init(void)
4819{
4820 /*
4821 * There isn't much point in executing destruction path in
4822 * parallel. Good chunk is serialized with cgroup_mutex anyway.
4823 * Use 1 for @max_active.
4824 *
4825 * We would prefer to do this in cgroup_init() above, but that
4826 * is called before init_workqueues(): so leave this until after.
4827 */
4828 cgroup_destroy_wq = alloc_workqueue("cgroup_destroy", 0, 1);
4829 BUG_ON(!cgroup_destroy_wq);
4830 return 0;
4831}
4832core_initcall(cgroup_wq_init);
4833
Paul Menagea4243162007-10-18 23:39:35 -07004834/*
4835 * proc_cgroup_show()
4836 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4837 * - Used for /proc/<pid>/cgroup.
4838 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4839 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08004840 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07004841 * anyway. No need to check that tsk->cgroup != NULL, thanks to
4842 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
4843 * cgroup to top_cgroup.
4844 */
4845
4846/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04004847int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07004848{
4849 struct pid *pid;
4850 struct task_struct *tsk;
4851 char *buf;
4852 int retval;
4853 struct cgroupfs_root *root;
4854
4855 retval = -ENOMEM;
4856 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4857 if (!buf)
4858 goto out;
4859
4860 retval = -ESRCH;
4861 pid = m->private;
4862 tsk = get_pid_task(pid, PIDTYPE_PID);
4863 if (!tsk)
4864 goto out_free;
4865
4866 retval = 0;
4867
4868 mutex_lock(&cgroup_mutex);
4869
Li Zefane5f6a862009-01-07 18:07:41 -08004870 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07004871 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07004872 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07004873 int count = 0;
4874
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004875 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07004876 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07004877 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07004878 if (strlen(root->name))
4879 seq_printf(m, "%sname=%s", count ? "," : "",
4880 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07004881 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07004882 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07004883 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07004884 if (retval < 0)
4885 goto out_unlock;
4886 seq_puts(m, buf);
4887 seq_putc(m, '\n');
4888 }
4889
4890out_unlock:
4891 mutex_unlock(&cgroup_mutex);
4892 put_task_struct(tsk);
4893out_free:
4894 kfree(buf);
4895out:
4896 return retval;
4897}
4898
Paul Menagea4243162007-10-18 23:39:35 -07004899/* Display information about each subsystem and each hierarchy */
4900static int proc_cgroupstats_show(struct seq_file *m, void *v)
4901{
Tejun Heo30159ec2013-06-25 11:53:37 -07004902 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07004903 int i;
Paul Menagea4243162007-10-18 23:39:35 -07004904
Paul Menage8bab8dd2008-04-04 14:29:57 -07004905 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08004906 /*
4907 * ideally we don't want subsystems moving around while we do this.
4908 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
4909 * subsys/hierarchy state.
4910 */
Paul Menagea4243162007-10-18 23:39:35 -07004911 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07004912
4913 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07004914 seq_printf(m, "%s\t%d\t%d\t%d\n",
4915 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07004916 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07004917
Paul Menagea4243162007-10-18 23:39:35 -07004918 mutex_unlock(&cgroup_mutex);
4919 return 0;
4920}
4921
4922static int cgroupstats_open(struct inode *inode, struct file *file)
4923{
Al Viro9dce07f2008-03-29 03:07:28 +00004924 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07004925}
4926
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004927static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07004928 .open = cgroupstats_open,
4929 .read = seq_read,
4930 .llseek = seq_lseek,
4931 .release = single_release,
4932};
4933
Paul Menageb4f48b62007-10-18 23:39:33 -07004934/**
4935 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08004936 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07004937 *
4938 * Description: A task inherits its parent's cgroup at fork().
4939 *
4940 * A pointer to the shared css_set was automatically copied in
4941 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07004942 * it was not made under the protection of RCU or cgroup_mutex, so
4943 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
4944 * have already changed current->cgroups, allowing the previously
4945 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07004946 *
4947 * At the point that cgroup_fork() is called, 'current' is the parent
4948 * task, and the passed argument 'child' points to the child task.
4949 */
4950void cgroup_fork(struct task_struct *child)
4951{
Tejun Heo9bb71302012-10-18 17:52:07 -07004952 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07004953 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07004954 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07004955 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07004956 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07004957}
4958
4959/**
Li Zefana043e3b2008-02-23 15:24:09 -08004960 * cgroup_post_fork - called on a new task after adding it to the task list
4961 * @child: the task in question
4962 *
Tejun Heo5edee612012-10-16 15:03:14 -07004963 * Adds the task to the list running through its css_set if necessary and
4964 * call the subsystem fork() callbacks. Has to be after the task is
4965 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04004966 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07004967 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08004968 */
Paul Menage817929e2007-10-18 23:39:36 -07004969void cgroup_post_fork(struct task_struct *child)
4970{
Tejun Heo30159ec2013-06-25 11:53:37 -07004971 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07004972 int i;
4973
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01004974 /*
4975 * use_task_css_set_links is set to 1 before we walk the tasklist
4976 * under the tasklist_lock and we read it here after we added the child
4977 * to the tasklist under the tasklist_lock as well. If the child wasn't
4978 * yet in the tasklist when we walked through it from
4979 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
4980 * should be visible now due to the paired locking and barriers implied
4981 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
4982 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
4983 * lock on fork.
4984 */
Paul Menage817929e2007-10-18 23:39:36 -07004985 if (use_task_css_set_links) {
4986 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07004987 task_lock(child);
4988 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07004989 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07004990 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07004991 write_unlock(&css_set_lock);
4992 }
Tejun Heo5edee612012-10-16 15:03:14 -07004993
4994 /*
4995 * Call ss->fork(). This must happen after @child is linked on
4996 * css_set; otherwise, @child might change state between ->fork()
4997 * and addition to css_set.
4998 */
4999 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005000 /*
5001 * fork/exit callbacks are supported only for builtin
5002 * subsystems, and the builtin section of the subsys
5003 * array is immutable, so we don't need to lock the
5004 * subsys array here. On the other hand, modular section
5005 * of the array can be freed at module unload, so we
5006 * can't touch that.
5007 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005008 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005009 if (ss->fork)
5010 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005011 }
Paul Menage817929e2007-10-18 23:39:36 -07005012}
Tejun Heo5edee612012-10-16 15:03:14 -07005013
Paul Menage817929e2007-10-18 23:39:36 -07005014/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005015 * cgroup_exit - detach cgroup from exiting task
5016 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005017 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005018 *
5019 * Description: Detach cgroup from @tsk and release it.
5020 *
5021 * Note that cgroups marked notify_on_release force every task in
5022 * them to take the global cgroup_mutex mutex when exiting.
5023 * This could impact scaling on very large systems. Be reluctant to
5024 * use notify_on_release cgroups where very high task exit scaling
5025 * is required on large systems.
5026 *
5027 * the_top_cgroup_hack:
5028 *
5029 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5030 *
5031 * We call cgroup_exit() while the task is still competent to
5032 * handle notify_on_release(), then leave the task attached to the
5033 * root cgroup in each hierarchy for the remainder of its exit.
5034 *
5035 * To do this properly, we would increment the reference count on
5036 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5037 * code we would add a second cgroup function call, to drop that
5038 * reference. This would just create an unnecessary hot spot on
5039 * the top_cgroup reference count, to no avail.
5040 *
5041 * Normally, holding a reference to a cgroup without bumping its
5042 * count is unsafe. The cgroup could go away, or someone could
5043 * attach us to a different cgroup, decrementing the count on
5044 * the first cgroup that we never incremented. But in this case,
5045 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005046 * which wards off any cgroup_attach_task() attempts, or task is a failed
5047 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005048 */
5049void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5050{
Tejun Heo30159ec2013-06-25 11:53:37 -07005051 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005052 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005053 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005054
5055 /*
5056 * Unlink from the css_set task list if necessary.
5057 * Optimistically check cg_list before taking
5058 * css_set_lock
5059 */
5060 if (!list_empty(&tsk->cg_list)) {
5061 write_lock(&css_set_lock);
5062 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005063 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005064 write_unlock(&css_set_lock);
5065 }
5066
Paul Menageb4f48b62007-10-18 23:39:33 -07005067 /* Reassign the task to the init_css_set. */
5068 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005069 cset = task_css_set(tsk);
5070 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005071
5072 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005073 /*
5074 * fork/exit callbacks are supported only for builtin
5075 * subsystems, see cgroup_post_fork() for details.
5076 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005077 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005078 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005079 struct cgroup_subsys_state *old_css = cset->subsys[i];
5080 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005081
Tejun Heoeb954192013-08-08 20:11:23 -04005082 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005083 }
5084 }
5085 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005086 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005087
Tejun Heo5abb8852013-06-12 21:04:49 -07005088 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005089}
Paul Menage697f4162007-10-18 23:39:34 -07005090
Paul Menagebd89aab2007-10-18 23:40:44 -07005091static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005092{
Li Zefanf50daa72013-03-01 15:06:07 +08005093 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005094 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005095 /*
5096 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005097 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005098 * it now
5099 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005100 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005101
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005102 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005103 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005104 list_empty(&cgrp->release_list)) {
5105 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005106 need_schedule_work = 1;
5107 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005108 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005109 if (need_schedule_work)
5110 schedule_work(&release_agent_work);
5111 }
5112}
5113
Paul Menage81a6a5c2007-10-18 23:39:38 -07005114/*
5115 * Notify userspace when a cgroup is released, by running the
5116 * configured release agent with the name of the cgroup (path
5117 * relative to the root of cgroup file system) as the argument.
5118 *
5119 * Most likely, this user command will try to rmdir this cgroup.
5120 *
5121 * This races with the possibility that some other task will be
5122 * attached to this cgroup before it is removed, or that some other
5123 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5124 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5125 * unused, and this cgroup will be reprieved from its death sentence,
5126 * to continue to serve a useful existence. Next time it's released,
5127 * we will get notified again, if it still has 'notify_on_release' set.
5128 *
5129 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5130 * means only wait until the task is successfully execve()'d. The
5131 * separate release agent task is forked by call_usermodehelper(),
5132 * then control in this thread returns here, without waiting for the
5133 * release agent task. We don't bother to wait because the caller of
5134 * this routine has no use for the exit status of the release agent
5135 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005136 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005137static void cgroup_release_agent(struct work_struct *work)
5138{
5139 BUG_ON(work != &release_agent_work);
5140 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005141 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005142 while (!list_empty(&release_list)) {
5143 char *argv[3], *envp[3];
5144 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005145 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005146 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005147 struct cgroup,
5148 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005149 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005150 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005151 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005152 if (!pathbuf)
5153 goto continue_free;
5154 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5155 goto continue_free;
5156 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5157 if (!agentbuf)
5158 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005159
5160 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005161 argv[i++] = agentbuf;
5162 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005163 argv[i] = NULL;
5164
5165 i = 0;
5166 /* minimal command environment */
5167 envp[i++] = "HOME=/";
5168 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5169 envp[i] = NULL;
5170
5171 /* Drop the lock while we invoke the usermode helper,
5172 * since the exec could involve hitting disk and hence
5173 * be a slow process */
5174 mutex_unlock(&cgroup_mutex);
5175 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005176 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005177 continue_free:
5178 kfree(pathbuf);
5179 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005180 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005181 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005182 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005183 mutex_unlock(&cgroup_mutex);
5184}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005185
5186static int __init cgroup_disable(char *str)
5187{
Tejun Heo30159ec2013-06-25 11:53:37 -07005188 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005189 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005190 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005191
5192 while ((token = strsep(&str, ",")) != NULL) {
5193 if (!*token)
5194 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005195
Tejun Heo30159ec2013-06-25 11:53:37 -07005196 /*
5197 * cgroup_disable, being at boot time, can't know about
5198 * module subsystems, so we don't worry about them.
5199 */
5200 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005201 if (!strcmp(token, ss->name)) {
5202 ss->disabled = 1;
5203 printk(KERN_INFO "Disabling %s control group"
5204 " subsystem\n", ss->name);
5205 break;
5206 }
5207 }
5208 }
5209 return 1;
5210}
5211__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005212
Tejun Heob77d7b62013-08-13 11:01:54 -04005213/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005214 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5215 * @dentry: directory dentry of interest
5216 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005217 *
5218 * Must be called under RCU read lock. The caller is responsible for
5219 * pinning the returned css if it needs to be accessed outside the RCU
5220 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005221 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005222struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5223 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005224{
5225 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005226
Tejun Heob77d7b62013-08-13 11:01:54 -04005227 WARN_ON_ONCE(!rcu_read_lock_held());
5228
Tejun Heo35cf0832013-08-26 18:40:56 -04005229 /* is @dentry a cgroup dir? */
5230 if (!dentry->d_inode ||
5231 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005232 return ERR_PTR(-EBADF);
5233
Tejun Heo35cf0832013-08-26 18:40:56 -04005234 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005235 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005236}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005237
Li Zefan1cb650b2013-08-19 10:05:24 +08005238/**
5239 * css_from_id - lookup css by id
5240 * @id: the cgroup id
5241 * @ss: cgroup subsys to be looked into
5242 *
5243 * Returns the css if there's valid one with @id, otherwise returns NULL.
5244 * Should be called under rcu_read_lock().
5245 */
5246struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5247{
5248 struct cgroup *cgrp;
5249
5250 rcu_lockdep_assert(rcu_read_lock_held() ||
5251 lockdep_is_held(&cgroup_mutex),
5252 "css_from_id() needs proper protection");
5253
5254 cgrp = idr_find(&ss->root->cgroup_idr, id);
5255 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005256 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005257 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005258}
5259
Paul Menagefe693432009-09-23 15:56:20 -07005260#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005261static struct cgroup_subsys_state *
5262debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005263{
5264 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5265
5266 if (!css)
5267 return ERR_PTR(-ENOMEM);
5268
5269 return css;
5270}
5271
Tejun Heoeb954192013-08-08 20:11:23 -04005272static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005273{
Tejun Heoeb954192013-08-08 20:11:23 -04005274 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005275}
5276
Tejun Heo182446d2013-08-08 20:11:24 -04005277static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5278 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005279{
Tejun Heo182446d2013-08-08 20:11:24 -04005280 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005281}
5282
Tejun Heo182446d2013-08-08 20:11:24 -04005283static u64 current_css_set_read(struct cgroup_subsys_state *css,
5284 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005285{
5286 return (u64)(unsigned long)current->cgroups;
5287}
5288
Tejun Heo182446d2013-08-08 20:11:24 -04005289static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005290 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005291{
5292 u64 count;
5293
5294 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005295 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005296 rcu_read_unlock();
5297 return count;
5298}
5299
Tejun Heo182446d2013-08-08 20:11:24 -04005300static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005301 struct cftype *cft,
5302 struct seq_file *seq)
5303{
Tejun Heo69d02062013-06-12 21:04:50 -07005304 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005305 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005306
5307 read_lock(&css_set_lock);
5308 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005309 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005310 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005311 struct cgroup *c = link->cgrp;
5312 const char *name;
5313
5314 if (c->dentry)
5315 name = c->dentry->d_name.name;
5316 else
5317 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005318 seq_printf(seq, "Root %d group %s\n",
5319 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005320 }
5321 rcu_read_unlock();
5322 read_unlock(&css_set_lock);
5323 return 0;
5324}
5325
5326#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005327static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5328 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005329{
Tejun Heo69d02062013-06-12 21:04:50 -07005330 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005331
5332 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005333 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005334 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005335 struct task_struct *task;
5336 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005337 seq_printf(seq, "css_set %p\n", cset);
5338 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005339 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5340 seq_puts(seq, " ...\n");
5341 break;
5342 } else {
5343 seq_printf(seq, " task %d\n",
5344 task_pid_vnr(task));
5345 }
5346 }
5347 }
5348 read_unlock(&css_set_lock);
5349 return 0;
5350}
5351
Tejun Heo182446d2013-08-08 20:11:24 -04005352static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005353{
Tejun Heo182446d2013-08-08 20:11:24 -04005354 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005355}
5356
5357static struct cftype debug_files[] = {
5358 {
Paul Menagefe693432009-09-23 15:56:20 -07005359 .name = "taskcount",
5360 .read_u64 = debug_taskcount_read,
5361 },
5362
5363 {
5364 .name = "current_css_set",
5365 .read_u64 = current_css_set_read,
5366 },
5367
5368 {
5369 .name = "current_css_set_refcount",
5370 .read_u64 = current_css_set_refcount_read,
5371 },
5372
5373 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005374 .name = "current_css_set_cg_links",
5375 .read_seq_string = current_css_set_cg_links_read,
5376 },
5377
5378 {
5379 .name = "cgroup_css_links",
5380 .read_seq_string = cgroup_css_links_read,
5381 },
5382
5383 {
Paul Menagefe693432009-09-23 15:56:20 -07005384 .name = "releasable",
5385 .read_u64 = releasable_read,
5386 },
Paul Menagefe693432009-09-23 15:56:20 -07005387
Tejun Heo4baf6e32012-04-01 12:09:55 -07005388 { } /* terminate */
5389};
Paul Menagefe693432009-09-23 15:56:20 -07005390
5391struct cgroup_subsys debug_subsys = {
5392 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005393 .css_alloc = debug_css_alloc,
5394 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005395 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005396 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005397};
5398#endif /* CONFIG_CGROUP_DEBUG */