blob: abc62ea1303c3f73ee89a19a81994768d668a774 [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040084EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070085#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
162 * Cgroup which the event belongs to.
163 */
164 struct cgroup *cgrp;
165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218static struct cftype cgroup_base_files[];
219
Tejun Heoea15f8c2013-06-13 19:27:42 -0700220static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400222static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
223 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Paul Menageddbcc7e2007-10-18 23:39:30 -0700225/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700226static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700227{
Tejun Heo54766d42013-06-12 21:04:53 -0700228 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700229}
230
Li Zefan78574cf2013-04-08 19:00:38 -0700231/**
232 * cgroup_is_descendant - test ancestry
233 * @cgrp: the cgroup to be tested
234 * @ancestor: possible ancestor of @cgrp
235 *
236 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
237 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
238 * and @ancestor are accessible.
239 */
240bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
241{
242 while (cgrp) {
243 if (cgrp == ancestor)
244 return true;
245 cgrp = cgrp->parent;
246 }
247 return false;
248}
249EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250
Adrian Bunke9685a02008-02-07 00:13:46 -0800251static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700252{
253 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700254 (1 << CGRP_RELEASABLE) |
255 (1 << CGRP_NOTIFY_ON_RELEASE);
256 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700257}
258
Adrian Bunke9685a02008-02-07 00:13:46 -0800259static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700260{
Paul Menagebd89aab2007-10-18 23:40:44 -0700261 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700262}
263
Tejun Heo30159ec2013-06-25 11:53:37 -0700264/**
265 * for_each_subsys - iterate all loaded cgroup subsystems
266 * @ss: the iteration cursor
267 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
268 *
269 * Should be called under cgroup_mutex.
270 */
271#define for_each_subsys(ss, i) \
272 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
273 if (({ lockdep_assert_held(&cgroup_mutex); \
274 !((ss) = cgroup_subsys[i]); })) { } \
275 else
276
277/**
278 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
281 *
282 * Bulit-in subsystems are always present and iteration itself doesn't
283 * require any synchronization.
284 */
285#define for_each_builtin_subsys(ss, i) \
286 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
287 (((ss) = cgroup_subsys[i]) || true); (i)++)
288
Tejun Heo5549c492013-06-24 15:21:48 -0700289/* iterate each subsystem attached to a hierarchy */
290#define for_each_root_subsys(root, ss) \
291 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700292
Tejun Heo5549c492013-06-24 15:21:48 -0700293/* iterate across the active hierarchies */
294#define for_each_active_root(root) \
295 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700296
Tejun Heof6ea9372012-04-01 12:09:55 -0700297static inline struct cgroup *__d_cgrp(struct dentry *dentry)
298{
299 return dentry->d_fsdata;
300}
301
Tejun Heo05ef1d72012-04-01 12:09:56 -0700302static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700303{
304 return dentry->d_fsdata;
305}
306
Tejun Heo05ef1d72012-04-01 12:09:56 -0700307static inline struct cftype *__d_cft(struct dentry *dentry)
308{
309 return __d_cfe(dentry)->type;
310}
311
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700312/**
313 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
314 * @cgrp: the cgroup to be checked for liveness
315 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700316 * On success, returns true; the mutex should be later unlocked. On
317 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700318 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700319static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700320{
321 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700322 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700323 mutex_unlock(&cgroup_mutex);
324 return false;
325 }
326 return true;
327}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700328
Paul Menage81a6a5c2007-10-18 23:39:38 -0700329/* the list of cgroups eligible for automatic release. Protected by
330 * release_list_lock */
331static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200332static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700333static void cgroup_release_agent(struct work_struct *work);
334static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700335static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700336
Tejun Heo69d02062013-06-12 21:04:50 -0700337/*
338 * A cgroup can be associated with multiple css_sets as different tasks may
339 * belong to different cgroups on different hierarchies. In the other
340 * direction, a css_set is naturally associated with multiple cgroups.
341 * This M:N relationship is represented by the following link structure
342 * which exists for each association and allows traversing the associations
343 * from both sides.
344 */
345struct cgrp_cset_link {
346 /* the cgroup and css_set this link associates */
347 struct cgroup *cgrp;
348 struct css_set *cset;
349
350 /* list of cgrp_cset_links anchored at cgrp->cset_links */
351 struct list_head cset_link;
352
353 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
354 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700355};
356
357/* The default css_set - used by init and its children prior to any
358 * hierarchies being mounted. It contains a pointer to the root state
359 * for each subsystem. Also used to anchor the list of css_sets. Not
360 * reference-counted, to improve performance when child cgroups
361 * haven't been created.
362 */
363
364static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700365static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700366
Ben Blume6a11052010-03-10 15:22:09 -0800367static int cgroup_init_idr(struct cgroup_subsys *ss,
368 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700369
Tejun Heo0942eee2013-08-08 20:11:26 -0400370/*
371 * css_set_lock protects the list of css_set objects, and the chain of
372 * tasks off each css_set. Nests outside task->alloc_lock due to
373 * cgroup_task_iter_start().
374 */
Paul Menage817929e2007-10-18 23:39:36 -0700375static DEFINE_RWLOCK(css_set_lock);
376static int css_set_count;
377
Paul Menage7717f7b2009-09-23 15:56:22 -0700378/*
379 * hash table for cgroup groups. This improves the performance to find
380 * an existing css_set. This hash doesn't (currently) take into
381 * account cgroups in empty hierarchies.
382 */
Li Zefan472b1052008-04-29 01:00:11 -0700383#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700385
Li Zefan0ac801f2013-01-10 11:49:27 +0800386static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700387{
Li Zefan0ac801f2013-01-10 11:49:27 +0800388 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700389 struct cgroup_subsys *ss;
390 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700391
Tejun Heo30159ec2013-06-25 11:53:37 -0700392 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800393 key += (unsigned long)css[i];
394 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700395
Li Zefan0ac801f2013-01-10 11:49:27 +0800396 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700397}
398
Tejun Heo0942eee2013-08-08 20:11:26 -0400399/*
400 * We don't maintain the lists running through each css_set to its task
401 * until after the first call to cgroup_task_iter_start(). This reduces
402 * the fork()/exit() overhead for people who have cgroups compiled into
403 * their kernel but not actually in use.
404 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700405static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700406
Tejun Heo5abb8852013-06-12 21:04:49 -0700407static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700408{
Tejun Heo69d02062013-06-12 21:04:50 -0700409 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700410
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700411 /*
412 * Ensure that the refcount doesn't hit zero while any readers
413 * can see it. Similar to atomic_dec_and_lock(), but for an
414 * rwlock
415 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700416 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700417 return;
418 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700419 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700420 write_unlock(&css_set_lock);
421 return;
422 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700423
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700424 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700425 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700426 css_set_count--;
427
Tejun Heo69d02062013-06-12 21:04:50 -0700428 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700429 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700430
Tejun Heo69d02062013-06-12 21:04:50 -0700431 list_del(&link->cset_link);
432 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800433
Tejun Heoddd69142013-06-12 21:04:54 -0700434 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d8282013-06-12 21:04:55 -0700435 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700437 set_bit(CGRP_RELEASABLE, &cgrp->flags);
438 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700439 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700440
441 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700442 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700443
444 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700445 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700446}
447
448/*
449 * refcounted get/put for css_set objects
450 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700451static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700452{
Tejun Heo5abb8852013-06-12 21:04:49 -0700453 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700454}
455
Tejun Heo5abb8852013-06-12 21:04:49 -0700456static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700457{
Tejun Heo5abb8852013-06-12 21:04:49 -0700458 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700459}
460
Tejun Heo5abb8852013-06-12 21:04:49 -0700461static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700462{
Tejun Heo5abb8852013-06-12 21:04:49 -0700463 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700464}
465
Tejun Heob326f9d2013-06-24 15:21:48 -0700466/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700467 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700468 * @cset: candidate css_set being tested
469 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 * @new_cgrp: cgroup that's being entered by the task
471 * @template: desired set of css pointers in css_set (pre-calculated)
472 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800473 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
475 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700476static bool compare_css_sets(struct css_set *cset,
477 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700478 struct cgroup *new_cgrp,
479 struct cgroup_subsys_state *template[])
480{
481 struct list_head *l1, *l2;
482
Tejun Heo5abb8852013-06-12 21:04:49 -0700483 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700484 /* Not all subsystems matched */
485 return false;
486 }
487
488 /*
489 * Compare cgroup pointers in order to distinguish between
490 * different cgroups in heirarchies with no subsystems. We
491 * could get by with just this check alone (and skip the
492 * memcmp above) but on most setups the memcmp check will
493 * avoid the need for this more expensive check on almost all
494 * candidates.
495 */
496
Tejun Heo69d02062013-06-12 21:04:50 -0700497 l1 = &cset->cgrp_links;
498 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700499 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700500 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700501 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700502
503 l1 = l1->next;
504 l2 = l2->next;
505 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700506 if (l1 == &cset->cgrp_links) {
507 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700508 break;
509 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700510 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700511 }
512 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700513 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
514 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
515 cgrp1 = link1->cgrp;
516 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700517 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700518 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700519
520 /*
521 * If this hierarchy is the hierarchy of the cgroup
522 * that's changing, then we need to check that this
523 * css_set points to the new cgroup; if it's any other
524 * hierarchy, then this css_set should point to the
525 * same cgroup as the old css_set.
526 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1->root == new_cgrp->root) {
528 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700529 return false;
530 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700531 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700532 return false;
533 }
534 }
535 return true;
536}
537
Tejun Heob326f9d2013-06-24 15:21:48 -0700538/**
539 * find_existing_css_set - init css array and find the matching css_set
540 * @old_cset: the css_set that we're using before the cgroup transition
541 * @cgrp: the cgroup that we're moving into
542 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700543 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700544static struct css_set *find_existing_css_set(struct css_set *old_cset,
545 struct cgroup *cgrp,
546 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700547{
Paul Menagebd89aab2007-10-18 23:40:44 -0700548 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700549 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800551 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700552 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700553
Ben Blumaae8aab2010-03-10 15:22:07 -0800554 /*
555 * Build the set of subsystem state objects that we want to see in the
556 * new css_set. while subsystems can change globally, the entries here
557 * won't change, so no need for locking.
558 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700559 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400560 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700561 /* Subsystem is in this hierarchy. So we want
562 * the subsystem state from the new
563 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700564 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 } else {
566 /* Subsystem is not in this hierarchy, so we
567 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700568 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700569 }
570 }
571
Li Zefan0ac801f2013-01-10 11:49:27 +0800572 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700573 hash_for_each_possible(css_set_table, cset, hlist, key) {
574 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700575 continue;
576
577 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700578 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700579 }
Paul Menage817929e2007-10-18 23:39:36 -0700580
581 /* No existing cgroup group matched */
582 return NULL;
583}
584
Tejun Heo69d02062013-06-12 21:04:50 -0700585static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700586{
Tejun Heo69d02062013-06-12 21:04:50 -0700587 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700588
Tejun Heo69d02062013-06-12 21:04:50 -0700589 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
590 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700591 kfree(link);
592 }
593}
594
Tejun Heo69d02062013-06-12 21:04:50 -0700595/**
596 * allocate_cgrp_cset_links - allocate cgrp_cset_links
597 * @count: the number of links to allocate
598 * @tmp_links: list_head the allocated links are put on
599 *
600 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
601 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700602 */
Tejun Heo69d02062013-06-12 21:04:50 -0700603static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700604{
Tejun Heo69d02062013-06-12 21:04:50 -0700605 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700606 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700607
608 INIT_LIST_HEAD(tmp_links);
609
Li Zefan36553432008-07-29 22:33:19 -0700610 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700611 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700612 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700613 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700614 return -ENOMEM;
615 }
Tejun Heo69d02062013-06-12 21:04:50 -0700616 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700617 }
618 return 0;
619}
620
Li Zefanc12f65d2009-01-07 18:07:42 -0800621/**
622 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700623 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700624 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800625 * @cgrp: the destination cgroup
626 */
Tejun Heo69d02062013-06-12 21:04:50 -0700627static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
628 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800629{
Tejun Heo69d02062013-06-12 21:04:50 -0700630 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800631
Tejun Heo69d02062013-06-12 21:04:50 -0700632 BUG_ON(list_empty(tmp_links));
633 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
634 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700635 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700636 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700637 /*
638 * Always add links to the tail of the list so that the list
639 * is sorted by order of hierarchy creation
640 */
Tejun Heo69d02062013-06-12 21:04:50 -0700641 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800642}
643
Tejun Heob326f9d2013-06-24 15:21:48 -0700644/**
645 * find_css_set - return a new css_set with one cgroup updated
646 * @old_cset: the baseline css_set
647 * @cgrp: the cgroup to be updated
648 *
649 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
650 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700651 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700652static struct css_set *find_css_set(struct css_set *old_cset,
653 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700654{
Tejun Heob326f9d2013-06-24 15:21:48 -0700655 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700656 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700657 struct list_head tmp_links;
658 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800659 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700660
Tejun Heob326f9d2013-06-24 15:21:48 -0700661 lockdep_assert_held(&cgroup_mutex);
662
Paul Menage817929e2007-10-18 23:39:36 -0700663 /* First see if we already have a cgroup group that matches
664 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700666 cset = find_existing_css_set(old_cset, cgrp, template);
667 if (cset)
668 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700669 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700670
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 if (cset)
672 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700673
Tejun Heof4f4be22013-06-12 21:04:51 -0700674 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700675 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700676 return NULL;
677
Tejun Heo69d02062013-06-12 21:04:50 -0700678 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700679 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700681 return NULL;
682 }
683
Tejun Heo5abb8852013-06-12 21:04:49 -0700684 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700685 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700686 INIT_LIST_HEAD(&cset->tasks);
687 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 /* Copy the set of subsystem state objects generated in
690 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700691 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700692
693 write_lock(&css_set_lock);
694 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700695 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700696 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700697
Paul Menage7717f7b2009-09-23 15:56:22 -0700698 if (c->root == cgrp->root)
699 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700700 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700701 }
Paul Menage817929e2007-10-18 23:39:36 -0700702
Tejun Heo69d02062013-06-12 21:04:50 -0700703 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700704
Paul Menage817929e2007-10-18 23:39:36 -0700705 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700706
707 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700708 key = css_set_hash(cset->subsys);
709 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700710
Paul Menage817929e2007-10-18 23:39:36 -0700711 write_unlock(&css_set_lock);
712
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700714}
715
Paul Menageddbcc7e2007-10-18 23:39:30 -0700716/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700717 * Return the cgroup for "task" from the given hierarchy. Must be
718 * called with cgroup_mutex held.
719 */
720static struct cgroup *task_cgroup_from_root(struct task_struct *task,
721 struct cgroupfs_root *root)
722{
Tejun Heo5abb8852013-06-12 21:04:49 -0700723 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700724 struct cgroup *res = NULL;
725
726 BUG_ON(!mutex_is_locked(&cgroup_mutex));
727 read_lock(&css_set_lock);
728 /*
729 * No need to lock the task - since we hold cgroup_mutex the
730 * task can't change groups, so the only thing that can happen
731 * is that it exits and its css is set back to init_css_set.
732 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700733 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700734 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700735 res = &root->top_cgroup;
736 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700737 struct cgrp_cset_link *link;
738
739 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700740 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700741
Paul Menage7717f7b2009-09-23 15:56:22 -0700742 if (c->root == root) {
743 res = c;
744 break;
745 }
746 }
747 }
748 read_unlock(&css_set_lock);
749 BUG_ON(!res);
750 return res;
751}
752
753/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700754 * There is one global cgroup mutex. We also require taking
755 * task_lock() when dereferencing a task's cgroup subsys pointers.
756 * See "The task_lock() exception", at the end of this comment.
757 *
758 * A task must hold cgroup_mutex to modify cgroups.
759 *
760 * Any task can increment and decrement the count field without lock.
761 * So in general, code holding cgroup_mutex can't rely on the count
762 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800763 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700764 * means that no tasks are currently attached, therefore there is no
765 * way a task attached to that cgroup can fork (the other way to
766 * increment the count). So code holding cgroup_mutex can safely
767 * assume that if the count is zero, it will stay zero. Similarly, if
768 * a task holds cgroup_mutex on a cgroup with zero count, it
769 * knows that the cgroup won't be removed, as cgroup_rmdir()
770 * needs that mutex.
771 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700772 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
773 * (usually) take cgroup_mutex. These are the two most performance
774 * critical pieces of code here. The exception occurs on cgroup_exit(),
775 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
776 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800777 * to the release agent with the name of the cgroup (path relative to
778 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700779 *
780 * A cgroup can only be deleted if both its 'count' of using tasks
781 * is zero, and its list of 'children' cgroups is empty. Since all
782 * tasks in the system use _some_ cgroup, and since there is always at
783 * least one task in the system (init, pid == 1), therefore, top_cgroup
784 * always has either children cgroups and/or using tasks. So we don't
785 * need a special hack to ensure that top_cgroup cannot be deleted.
786 *
787 * The task_lock() exception
788 *
789 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800790 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800791 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * several performance critical places that need to reference
793 * task->cgroup without the expense of grabbing a system global
794 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800795 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700796 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
797 * the task_struct routinely used for such matters.
798 *
799 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800800 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801 */
802
Paul Menageddbcc7e2007-10-18 23:39:30 -0700803/*
804 * A couple of forward declarations required, due to cyclic reference loop:
805 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
806 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
807 * -> cgroup_mkdir.
808 */
809
Al Viro18bb1db2011-07-26 01:41:39 -0400810static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400811static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700812static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700813static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700814static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700815static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700816
817static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200818 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700819 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700820};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700821
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700822static int alloc_css_id(struct cgroup_subsys *ss,
823 struct cgroup *parent, struct cgroup *child);
824
Al Viroa5e7ed32011-07-26 01:55:55 -0400825static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826{
827 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700828
829 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400830 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700831 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100832 inode->i_uid = current_fsuid();
833 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
835 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
836 }
837 return inode;
838}
839
Li Zefan65dff752013-03-01 15:01:56 +0800840static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
841{
842 struct cgroup_name *name;
843
844 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
845 if (!name)
846 return NULL;
847 strcpy(name->name, dentry->d_name.name);
848 return name;
849}
850
Li Zefanbe445622013-01-24 14:31:42 +0800851static void cgroup_free_fn(struct work_struct *work)
852{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700853 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800854 struct cgroup_subsys *ss;
855
856 mutex_lock(&cgroup_mutex);
857 /*
858 * Release the subsystem state objects.
859 */
Tejun Heoeb954192013-08-08 20:11:23 -0400860 for_each_root_subsys(cgrp->root, ss) {
861 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
862
863 ss->css_free(css);
864 }
Li Zefanbe445622013-01-24 14:31:42 +0800865
866 cgrp->root->number_of_cgroups--;
867 mutex_unlock(&cgroup_mutex);
868
869 /*
Li Zefan415cf072013-04-08 14:35:02 +0800870 * We get a ref to the parent's dentry, and put the ref when
871 * this cgroup is being freed, so it's guaranteed that the
872 * parent won't be destroyed before its children.
873 */
874 dput(cgrp->parent->dentry);
875
876 /*
Li Zefanbe445622013-01-24 14:31:42 +0800877 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700878 * created the cgroup. This will free cgrp->root, if we are
879 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800880 */
881 deactivate_super(cgrp->root->sb);
882
883 /*
884 * if we're getting rid of the cgroup, refcount should ensure
885 * that there are no pidlists left.
886 */
887 BUG_ON(!list_empty(&cgrp->pidlists));
888
889 simple_xattrs_free(&cgrp->xattrs);
890
Li Zefan65dff752013-03-01 15:01:56 +0800891 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800892 kfree(cgrp);
893}
894
895static void cgroup_free_rcu(struct rcu_head *head)
896{
897 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
898
Tejun Heoea15f8c2013-06-13 19:27:42 -0700899 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
900 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800901}
902
Paul Menageddbcc7e2007-10-18 23:39:30 -0700903static void cgroup_diput(struct dentry *dentry, struct inode *inode)
904{
905 /* is dentry a directory ? if so, kfree() associated cgroup */
906 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700907 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800908
Tejun Heo54766d42013-06-12 21:04:53 -0700909 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800910 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700911 } else {
912 struct cfent *cfe = __d_cfe(dentry);
913 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
914
915 WARN_ONCE(!list_empty(&cfe->node) &&
916 cgrp != &cgrp->root->top_cgroup,
917 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700918 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700919 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700920 }
921 iput(inode);
922}
923
Al Viroc72a04e2011-01-14 05:31:45 +0000924static int cgroup_delete(const struct dentry *d)
925{
926 return 1;
927}
928
Paul Menageddbcc7e2007-10-18 23:39:30 -0700929static void remove_dir(struct dentry *d)
930{
931 struct dentry *parent = dget(d->d_parent);
932
933 d_delete(d);
934 simple_rmdir(parent->d_inode, d);
935 dput(parent);
936}
937
Li Zefan2739d3c2013-01-21 18:18:33 +0800938static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700940 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700941
Tejun Heo05ef1d72012-04-01 12:09:56 -0700942 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
943 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100944
Li Zefan2739d3c2013-01-21 18:18:33 +0800945 /*
946 * If we're doing cleanup due to failure of cgroup_create(),
947 * the corresponding @cfe may not exist.
948 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700949 list_for_each_entry(cfe, &cgrp->files, node) {
950 struct dentry *d = cfe->dentry;
951
952 if (cft && cfe->type != cft)
953 continue;
954
955 dget(d);
956 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700957 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700958 list_del_init(&cfe->node);
959 dput(d);
960
Li Zefan2739d3c2013-01-21 18:18:33 +0800961 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700962 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700963}
964
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400965/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700966 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700967 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400968 * @subsys_mask: mask of the subsystem ids whose files should be removed
969 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700970static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700973 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700974
Tejun Heob420ba72013-07-12 12:34:02 -0700975 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700977
978 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 continue;
980 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400981 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400982 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700983}
984
985/*
986 * NOTE : the dentry must have been dget()'ed
987 */
988static void cgroup_d_remove_dir(struct dentry *dentry)
989{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100990 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100992 parent = dentry->d_parent;
993 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800994 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700995 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100996 spin_unlock(&dentry->d_lock);
997 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700998 remove_dir(dentry);
999}
1000
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001001/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001002 * Call with cgroup_mutex held. Drops reference counts on modules, including
1003 * any duplicate ones that parse_cgroupfs_options took. If this function
1004 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001005 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001007 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008{
Paul Menagebd89aab2007-10-18 23:40:44 -07001009 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001010 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001011 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001012 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013
Ben Blumaae8aab2010-03-10 15:22:07 -08001014 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001015 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001016
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001018 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001019 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001020 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001021
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001022 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001023 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001024 ret = -EBUSY;
1025 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001027
1028 /* pin the module */
1029 if (!try_module_get(ss->module)) {
1030 ret = -ENOENT;
1031 goto out_put;
1032 }
1033 pinned |= 1 << i;
1034 }
1035
1036 /* subsys could be missing if unloaded between parsing and here */
1037 if (added_mask != pinned) {
1038 ret = -ENOENT;
1039 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001040 }
1041
Tejun Heo31261212013-06-28 17:07:30 -07001042 ret = cgroup_populate_dir(cgrp, added_mask);
1043 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001044 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001045
1046 /*
1047 * Nothing can fail from this point on. Remove files for the
1048 * removed subsystems and rebind each subsystem.
1049 */
1050 cgroup_clear_dir(cgrp, removed_mask);
1051
Tejun Heo30159ec2013-06-25 11:53:37 -07001052 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001054
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001055 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001056 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001057 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001058 BUG_ON(!cgroup_dummy_top->subsys[i]);
1059 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001060
Tejun Heo9871bf92013-06-24 15:21:47 -07001061 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001062 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001063 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001064 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001065 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001066 ss->bind(cgrp->subsys[i]);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001067
Ben Blumcf5d5942010-03-10 15:22:09 -08001068 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001069 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001070 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001072 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001073 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001074
Paul Menageddbcc7e2007-10-18 23:39:30 -07001075 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001076 ss->bind(cgroup_dummy_top->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001077 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001078 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001079 cgroup_subsys[i]->root = &cgroup_dummy_root;
1080 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001081
Ben Blumcf5d5942010-03-10 15:22:09 -08001082 /* subsystem is now free - drop reference on module */
1083 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001084 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001085 }
1086 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087
Tejun Heo1672d042013-06-25 18:04:54 -07001088 /*
1089 * Mark @root has finished binding subsystems. @root->subsys_mask
1090 * now matches the bound subsystems.
1091 */
1092 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1093
Paul Menageddbcc7e2007-10-18 23:39:30 -07001094 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001095
1096out_put:
1097 for_each_subsys(ss, i)
1098 if (pinned & (1 << i))
1099 module_put(ss->module);
1100 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101}
1102
Al Viro34c80b12011-12-08 21:32:45 -05001103static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001104{
Al Viro34c80b12011-12-08 21:32:45 -05001105 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 struct cgroup_subsys *ss;
1107
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001108 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001109 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001111 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1112 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001113 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001115 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001116 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001117 if (strlen(root->release_agent_path))
1118 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001119 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001120 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001121 if (strlen(root->name))
1122 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001123 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 return 0;
1125}
1126
1127struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001128 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001130 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001131 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001132 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001133 /* User explicitly requested empty subsystem */
1134 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001135
1136 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001137
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138};
1139
Ben Blumaae8aab2010-03-10 15:22:07 -08001140/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001141 * Convert a hierarchy specifier into a bitmask of subsystems and
1142 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1143 * array. This function takes refcounts on subsystems to be used, unless it
1144 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001145 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001146static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001147{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001148 char *token, *o = data;
1149 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001150 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001151 struct cgroup_subsys *ss;
1152 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001153
Ben Blumaae8aab2010-03-10 15:22:07 -08001154 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1155
Li Zefanf9ab5b52009-06-17 16:26:33 -07001156#ifdef CONFIG_CPUSETS
1157 mask = ~(1UL << cpuset_subsys_id);
1158#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001159
Paul Menagec6d57f32009-09-23 15:56:19 -07001160 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161
1162 while ((token = strsep(&o, ",")) != NULL) {
1163 if (!*token)
1164 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001165 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001166 /* Explicitly have no subsystems */
1167 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001168 continue;
1169 }
1170 if (!strcmp(token, "all")) {
1171 /* Mutually exclusive option 'all' + subsystem name */
1172 if (one_ss)
1173 return -EINVAL;
1174 all_ss = true;
1175 continue;
1176 }
Tejun Heo873fe092013-04-14 20:15:26 -07001177 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1178 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1179 continue;
1180 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001182 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
1185 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001186 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001187 continue;
1188 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001189 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001190 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001191 continue;
1192 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001193 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001194 /* Specifying two release agents is forbidden */
1195 if (opts->release_agent)
1196 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001197 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001198 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001199 if (!opts->release_agent)
1200 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201 continue;
1202 }
1203 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001204 const char *name = token + 5;
1205 /* Can't specify an empty name */
1206 if (!strlen(name))
1207 return -EINVAL;
1208 /* Must match [\w.-]+ */
1209 for (i = 0; i < strlen(name); i++) {
1210 char c = name[i];
1211 if (isalnum(c))
1212 continue;
1213 if ((c == '.') || (c == '-') || (c == '_'))
1214 continue;
1215 return -EINVAL;
1216 }
1217 /* Specifying two names is forbidden */
1218 if (opts->name)
1219 return -EINVAL;
1220 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001221 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001222 GFP_KERNEL);
1223 if (!opts->name)
1224 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001225
1226 continue;
1227 }
1228
Tejun Heo30159ec2013-06-25 11:53:37 -07001229 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001230 if (strcmp(token, ss->name))
1231 continue;
1232 if (ss->disabled)
1233 continue;
1234
1235 /* Mutually exclusive option 'all' + subsystem name */
1236 if (all_ss)
1237 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001238 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001239 one_ss = true;
1240
1241 break;
1242 }
1243 if (i == CGROUP_SUBSYS_COUNT)
1244 return -ENOENT;
1245 }
1246
1247 /*
1248 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001249 * otherwise if 'none', 'name=' and a subsystem name options
1250 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001251 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001252 if (all_ss || (!one_ss && !opts->none && !opts->name))
1253 for_each_subsys(ss, i)
1254 if (!ss->disabled)
1255 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001256
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001257 /* Consistency checks */
1258
Tejun Heo873fe092013-04-14 20:15:26 -07001259 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1260 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1261
1262 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1263 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1264 return -EINVAL;
1265 }
1266
1267 if (opts->cpuset_clone_children) {
1268 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1269 return -EINVAL;
1270 }
1271 }
1272
Li Zefanf9ab5b52009-06-17 16:26:33 -07001273 /*
1274 * Option noprefix was introduced just for backward compatibility
1275 * with the old cpuset, so we allow noprefix only if mounting just
1276 * the cpuset subsystem.
1277 */
Tejun Heo93438622013-04-14 20:15:25 -07001278 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001279 return -EINVAL;
1280
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001281
1282 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001283 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001284 return -EINVAL;
1285
1286 /*
1287 * We either have to specify by name or by subsystems. (So all
1288 * empty hierarchies must have a name).
1289 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001290 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001291 return -EINVAL;
1292
1293 return 0;
1294}
1295
1296static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1297{
1298 int ret = 0;
1299 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001300 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001301 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001302 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001303
Tejun Heo873fe092013-04-14 20:15:26 -07001304 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1305 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1306 return -EINVAL;
1307 }
1308
Paul Menagebd89aab2007-10-18 23:40:44 -07001309 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001310 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001311 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001312
1313 /* See what subsystems are wanted */
1314 ret = parse_cgroupfs_options(data, &opts);
1315 if (ret)
1316 goto out_unlock;
1317
Tejun Heoa8a648c2013-06-24 15:21:47 -07001318 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001319 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1320 task_tgid_nr(current), current->comm);
1321
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001322 added_mask = opts.subsys_mask & ~root->subsys_mask;
1323 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001324
Ben Blumcf5d5942010-03-10 15:22:09 -08001325 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001326 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001327 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001328 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1329 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1330 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001331 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001332 goto out_unlock;
1333 }
1334
Tejun Heof172e672013-06-28 17:07:30 -07001335 /* remounting is not allowed for populated hierarchies */
1336 if (root->number_of_cgroups > 1) {
1337 ret = -EBUSY;
1338 goto out_unlock;
1339 }
1340
Tejun Heoa8a648c2013-06-24 15:21:47 -07001341 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001342 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001343 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344
Paul Menage81a6a5c2007-10-18 23:39:38 -07001345 if (opts.release_agent)
1346 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001348 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001349 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001350 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001351 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001352 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 return ret;
1354}
1355
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001356static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357 .statfs = simple_statfs,
1358 .drop_inode = generic_delete_inode,
1359 .show_options = cgroup_show_options,
1360 .remount_fs = cgroup_remount,
1361};
1362
Paul Menagecc31edc2008-10-18 20:28:04 -07001363static void init_cgroup_housekeeping(struct cgroup *cgrp)
1364{
1365 INIT_LIST_HEAD(&cgrp->sibling);
1366 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001367 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001368 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001369 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001370 INIT_LIST_HEAD(&cgrp->pidlists);
1371 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001372 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001373 INIT_LIST_HEAD(&cgrp->event_list);
1374 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001375 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001376}
Paul Menagec6d57f32009-09-23 15:56:19 -07001377
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378static void init_cgroup_root(struct cgroupfs_root *root)
1379{
Paul Menagebd89aab2007-10-18 23:40:44 -07001380 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001381
Paul Menageddbcc7e2007-10-18 23:39:30 -07001382 INIT_LIST_HEAD(&root->subsys_list);
1383 INIT_LIST_HEAD(&root->root_list);
1384 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001385 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001386 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001387 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001388 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001389}
1390
Tejun Heofc76df72013-06-25 11:53:37 -07001391static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001392{
Tejun Heo1a574232013-04-14 11:36:58 -07001393 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001394
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001395 lockdep_assert_held(&cgroup_mutex);
1396 lockdep_assert_held(&cgroup_root_mutex);
1397
Tejun Heofc76df72013-06-25 11:53:37 -07001398 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1399 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001400 if (id < 0)
1401 return id;
1402
1403 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001404 return 0;
1405}
1406
1407static void cgroup_exit_root_id(struct cgroupfs_root *root)
1408{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001409 lockdep_assert_held(&cgroup_mutex);
1410 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001411
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001412 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001413 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001414 root->hierarchy_id = 0;
1415 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001416}
1417
Paul Menageddbcc7e2007-10-18 23:39:30 -07001418static int cgroup_test_super(struct super_block *sb, void *data)
1419{
Paul Menagec6d57f32009-09-23 15:56:19 -07001420 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001421 struct cgroupfs_root *root = sb->s_fs_info;
1422
Paul Menagec6d57f32009-09-23 15:56:19 -07001423 /* If we asked for a name then it must match */
1424 if (opts->name && strcmp(opts->name, root->name))
1425 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001426
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001427 /*
1428 * If we asked for subsystems (or explicitly for no
1429 * subsystems) then they must match
1430 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001431 if ((opts->subsys_mask || opts->none)
1432 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433 return 0;
1434
1435 return 1;
1436}
1437
Paul Menagec6d57f32009-09-23 15:56:19 -07001438static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1439{
1440 struct cgroupfs_root *root;
1441
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001442 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001443 return NULL;
1444
1445 root = kzalloc(sizeof(*root), GFP_KERNEL);
1446 if (!root)
1447 return ERR_PTR(-ENOMEM);
1448
1449 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001450
Tejun Heo1672d042013-06-25 18:04:54 -07001451 /*
1452 * We need to set @root->subsys_mask now so that @root can be
1453 * matched by cgroup_test_super() before it finishes
1454 * initialization; otherwise, competing mounts with the same
1455 * options may try to bind the same subsystems instead of waiting
1456 * for the first one leading to unexpected mount errors.
1457 * SUBSYS_BOUND will be set once actual binding is complete.
1458 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001459 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001460 root->flags = opts->flags;
1461 if (opts->release_agent)
1462 strcpy(root->release_agent_path, opts->release_agent);
1463 if (opts->name)
1464 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001465 if (opts->cpuset_clone_children)
1466 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001467 return root;
1468}
1469
Tejun Heofa3ca072013-04-14 11:36:56 -07001470static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001471{
Tejun Heofa3ca072013-04-14 11:36:56 -07001472 if (root) {
1473 /* hierarhcy ID shoulid already have been released */
1474 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001475
Li Zefan4e96ee82013-07-31 09:50:50 +08001476 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001477 kfree(root);
1478 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001479}
1480
Paul Menageddbcc7e2007-10-18 23:39:30 -07001481static int cgroup_set_super(struct super_block *sb, void *data)
1482{
1483 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001484 struct cgroup_sb_opts *opts = data;
1485
1486 /* If we don't have a new root, we can't set up a new sb */
1487 if (!opts->new_root)
1488 return -EINVAL;
1489
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001490 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001491
1492 ret = set_anon_super(sb, NULL);
1493 if (ret)
1494 return ret;
1495
Paul Menagec6d57f32009-09-23 15:56:19 -07001496 sb->s_fs_info = opts->new_root;
1497 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001498
1499 sb->s_blocksize = PAGE_CACHE_SIZE;
1500 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1501 sb->s_magic = CGROUP_SUPER_MAGIC;
1502 sb->s_op = &cgroup_ops;
1503
1504 return 0;
1505}
1506
1507static int cgroup_get_rootdir(struct super_block *sb)
1508{
Al Viro0df6a632010-12-21 13:29:29 -05001509 static const struct dentry_operations cgroup_dops = {
1510 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001511 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001512 };
1513
Paul Menageddbcc7e2007-10-18 23:39:30 -07001514 struct inode *inode =
1515 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516
1517 if (!inode)
1518 return -ENOMEM;
1519
Paul Menageddbcc7e2007-10-18 23:39:30 -07001520 inode->i_fop = &simple_dir_operations;
1521 inode->i_op = &cgroup_dir_inode_operations;
1522 /* directories start off with i_nlink == 2 (for "." entry) */
1523 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001524 sb->s_root = d_make_root(inode);
1525 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001526 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001527 /* for everything else we want ->d_op set */
1528 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 return 0;
1530}
1531
Al Virof7e83572010-07-26 13:23:11 +04001532static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001534 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535{
1536 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001537 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001538 int ret = 0;
1539 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001540 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001541 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001542 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001543 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544
1545 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001546 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001548 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001549 if (ret)
1550 goto out_err;
1551
1552 /*
1553 * Allocate a new cgroup root. We may not need it if we're
1554 * reusing an existing hierarchy.
1555 */
1556 new_root = cgroup_root_from_opts(&opts);
1557 if (IS_ERR(new_root)) {
1558 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001559 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001560 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001561 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001564 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001566 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001567 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001568 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001569 }
1570
Paul Menagec6d57f32009-09-23 15:56:19 -07001571 root = sb->s_fs_info;
1572 BUG_ON(!root);
1573 if (root == opts.new_root) {
1574 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001575 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001577 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001578 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579
1580 BUG_ON(sb->s_root != NULL);
1581
1582 ret = cgroup_get_rootdir(sb);
1583 if (ret)
1584 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001585 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
Paul Menage817929e2007-10-18 23:39:36 -07001587 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001588 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001589 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001590
Li Zefan4e96ee82013-07-31 09:50:50 +08001591 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1592 0, 1, GFP_KERNEL);
1593 if (root_cgrp->id < 0)
1594 goto unlock_drop;
1595
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001596 /* Check for name clashes with existing mounts */
1597 ret = -EBUSY;
1598 if (strlen(root->name))
1599 for_each_active_root(existing_root)
1600 if (!strcmp(existing_root->name, root->name))
1601 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001602
Paul Menage817929e2007-10-18 23:39:36 -07001603 /*
1604 * We're accessing css_set_count without locking
1605 * css_set_lock here, but that's OK - it can only be
1606 * increased by someone holding cgroup_lock, and
1607 * that's us. The worst that can happen is that we
1608 * have some link structures left over
1609 */
Tejun Heo69d02062013-06-12 21:04:50 -07001610 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 if (ret)
1612 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001613
Tejun Heofc76df72013-06-25 11:53:37 -07001614 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1615 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001616 if (ret)
1617 goto unlock_drop;
1618
Tejun Heo31261212013-06-28 17:07:30 -07001619 sb->s_root->d_fsdata = root_cgrp;
1620 root_cgrp->dentry = sb->s_root;
1621
1622 /*
1623 * We're inside get_sb() and will call lookup_one_len() to
1624 * create the root files, which doesn't work if SELinux is
1625 * in use. The following cred dancing somehow works around
1626 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1627 * populating new cgroupfs mount") for more details.
1628 */
1629 cred = override_creds(&init_cred);
1630
Tejun Heo2bb566c2013-08-08 20:11:23 -04001631 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001632 if (ret)
1633 goto rm_base_files;
1634
Tejun Heoa8a648c2013-06-24 15:21:47 -07001635 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001636 if (ret)
1637 goto rm_base_files;
1638
1639 revert_creds(cred);
1640
Ben Blumcf5d5942010-03-10 15:22:09 -08001641 /*
1642 * There must be no failure case after here, since rebinding
1643 * takes care of subsystems' refcounts, which are explicitly
1644 * dropped in the failure exit path.
1645 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001646
Tejun Heo9871bf92013-06-24 15:21:47 -07001647 list_add(&root->root_list, &cgroup_roots);
1648 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001649
Paul Menage817929e2007-10-18 23:39:36 -07001650 /* Link the top cgroup in this hierarchy into all
1651 * the css_set objects */
1652 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001653 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001654 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001655 write_unlock(&css_set_lock);
1656
Tejun Heo69d02062013-06-12 21:04:50 -07001657 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001658
Li Zefanc12f65d2009-01-07 18:07:42 -08001659 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660 BUG_ON(root->number_of_cgroups != 1);
1661
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001662 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001664 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001665 } else {
1666 /*
1667 * We re-used an existing hierarchy - the new root (if
1668 * any) is not needed
1669 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001670 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001671
Tejun Heoc7ba8282013-06-29 14:06:10 -07001672 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001673 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1674 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1675 ret = -EINVAL;
1676 goto drop_new_super;
1677 } else {
1678 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1679 }
Tejun Heo873fe092013-04-14 20:15:26 -07001680 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001681 }
1682
Paul Menagec6d57f32009-09-23 15:56:19 -07001683 kfree(opts.release_agent);
1684 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001685 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001686
Tejun Heo31261212013-06-28 17:07:30 -07001687 rm_base_files:
1688 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001689 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001690 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001691 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001692 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001693 mutex_unlock(&cgroup_root_mutex);
1694 mutex_unlock(&cgroup_mutex);
1695 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001697 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001698 out_err:
1699 kfree(opts.release_agent);
1700 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001701 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001702}
1703
1704static void cgroup_kill_sb(struct super_block *sb) {
1705 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001706 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001707 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001708 int ret;
1709
1710 BUG_ON(!root);
1711
1712 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001713 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
Tejun Heo31261212013-06-28 17:07:30 -07001715 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001717 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001718
1719 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001720 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1721 ret = rebind_subsystems(root, 0, root->subsys_mask);
1722 /* Shouldn't be able to fail ... */
1723 BUG_ON(ret);
1724 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001725
Paul Menage817929e2007-10-18 23:39:36 -07001726 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001727 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001728 * root cgroup
1729 */
1730 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001731
Tejun Heo69d02062013-06-12 21:04:50 -07001732 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1733 list_del(&link->cset_link);
1734 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001735 kfree(link);
1736 }
1737 write_unlock(&css_set_lock);
1738
Paul Menage839ec542009-01-29 14:25:22 -08001739 if (!list_empty(&root->root_list)) {
1740 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001741 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001742 }
Li Zefane5f6a862009-01-07 18:07:41 -08001743
Tejun Heofa3ca072013-04-14 11:36:56 -07001744 cgroup_exit_root_id(root);
1745
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001746 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001747 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001748 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001749
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001750 simple_xattrs_free(&cgrp->xattrs);
1751
Paul Menageddbcc7e2007-10-18 23:39:30 -07001752 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001753 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001754}
1755
1756static struct file_system_type cgroup_fs_type = {
1757 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001758 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001759 .kill_sb = cgroup_kill_sb,
1760};
1761
Greg KH676db4a2010-08-05 13:53:35 -07001762static struct kobject *cgroup_kobj;
1763
Li Zefana043e3b2008-02-23 15:24:09 -08001764/**
1765 * cgroup_path - generate the path of a cgroup
1766 * @cgrp: the cgroup in question
1767 * @buf: the buffer to write the path into
1768 * @buflen: the length of the buffer
1769 *
Li Zefan65dff752013-03-01 15:01:56 +08001770 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1771 *
1772 * We can't generate cgroup path using dentry->d_name, as accessing
1773 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1774 * inode's i_mutex, while on the other hand cgroup_path() can be called
1775 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001777int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001778{
Li Zefan65dff752013-03-01 15:01:56 +08001779 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001780 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001781
Tejun Heoda1f2962013-04-14 10:32:19 -07001782 if (!cgrp->parent) {
1783 if (strlcpy(buf, "/", buflen) >= buflen)
1784 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001785 return 0;
1786 }
1787
Tao Ma316eb662012-11-08 21:36:38 +08001788 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001789 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001790
Li Zefan65dff752013-03-01 15:01:56 +08001791 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001792 do {
Li Zefan65dff752013-03-01 15:01:56 +08001793 const char *name = cgroup_name(cgrp);
1794 int len;
1795
1796 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001798 goto out;
1799 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001800
Paul Menageddbcc7e2007-10-18 23:39:30 -07001801 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001802 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001803 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001804
1805 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001806 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001807 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001809out:
1810 rcu_read_unlock();
1811 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812}
Ben Blum67523c42010-03-10 15:22:11 -08001813EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814
Tejun Heo857a2be2013-04-14 20:50:08 -07001815/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001816 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001817 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001818 * @buf: the buffer to write the path into
1819 * @buflen: the length of the buffer
1820 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001821 * Determine @task's cgroup on the first (the one with the lowest non-zero
1822 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1823 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1824 * cgroup controller callbacks.
1825 *
1826 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001827 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001828int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001829{
1830 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001831 struct cgroup *cgrp;
1832 int hierarchy_id = 1, ret = 0;
1833
1834 if (buflen < 2)
1835 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001836
1837 mutex_lock(&cgroup_mutex);
1838
Tejun Heo913ffdb2013-07-11 16:34:48 -07001839 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1840
Tejun Heo857a2be2013-04-14 20:50:08 -07001841 if (root) {
1842 cgrp = task_cgroup_from_root(task, root);
1843 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001844 } else {
1845 /* if no hierarchy exists, everyone is in "/" */
1846 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001847 }
1848
1849 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001850 return ret;
1851}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001852EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001853
Ben Blum74a11662011-05-26 16:25:20 -07001854/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001855 * Control Group taskset
1856 */
Tejun Heo134d3372011-12-12 18:12:21 -08001857struct task_and_cgroup {
1858 struct task_struct *task;
1859 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001860 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001861};
1862
Tejun Heo2f7ee562011-12-12 18:12:21 -08001863struct cgroup_taskset {
1864 struct task_and_cgroup single;
1865 struct flex_array *tc_array;
1866 int tc_array_len;
1867 int idx;
1868 struct cgroup *cur_cgrp;
1869};
1870
1871/**
1872 * cgroup_taskset_first - reset taskset and return the first task
1873 * @tset: taskset of interest
1874 *
1875 * @tset iteration is initialized and the first task is returned.
1876 */
1877struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1878{
1879 if (tset->tc_array) {
1880 tset->idx = 0;
1881 return cgroup_taskset_next(tset);
1882 } else {
1883 tset->cur_cgrp = tset->single.cgrp;
1884 return tset->single.task;
1885 }
1886}
1887EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1888
1889/**
1890 * cgroup_taskset_next - iterate to the next task in taskset
1891 * @tset: taskset of interest
1892 *
1893 * Return the next task in @tset. Iteration must have been initialized
1894 * with cgroup_taskset_first().
1895 */
1896struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1897{
1898 struct task_and_cgroup *tc;
1899
1900 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1901 return NULL;
1902
1903 tc = flex_array_get(tset->tc_array, tset->idx++);
1904 tset->cur_cgrp = tc->cgrp;
1905 return tc->task;
1906}
1907EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1908
1909/**
1910 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1911 * @tset: taskset of interest
1912 *
1913 * Return the cgroup for the current (last returned) task of @tset. This
1914 * function must be preceded by either cgroup_taskset_first() or
1915 * cgroup_taskset_next().
1916 */
1917struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1918{
1919 return tset->cur_cgrp;
1920}
1921EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1922
1923/**
1924 * cgroup_taskset_size - return the number of tasks in taskset
1925 * @tset: taskset of interest
1926 */
1927int cgroup_taskset_size(struct cgroup_taskset *tset)
1928{
1929 return tset->tc_array ? tset->tc_array_len : 1;
1930}
1931EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1932
1933
Ben Blum74a11662011-05-26 16:25:20 -07001934/*
1935 * cgroup_task_migrate - move a task from one cgroup to another.
1936 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001937 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001938 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001939static void cgroup_task_migrate(struct cgroup *old_cgrp,
1940 struct task_struct *tsk,
1941 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001942{
Tejun Heo5abb8852013-06-12 21:04:49 -07001943 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001944
1945 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001946 * We are synchronized through threadgroup_lock() against PF_EXITING
1947 * setting such that we can't race against cgroup_exit() changing the
1948 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001949 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001950 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001951 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001952
Ben Blum74a11662011-05-26 16:25:20 -07001953 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001954 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001955 task_unlock(tsk);
1956
1957 /* Update the css_set linked lists if we're using them */
1958 write_lock(&css_set_lock);
1959 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001960 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001961 write_unlock(&css_set_lock);
1962
1963 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001964 * We just gained a reference on old_cset by taking it from the
1965 * task. As trading it for new_cset is protected by cgroup_mutex,
1966 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001967 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001968 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1969 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001970}
1971
Li Zefana043e3b2008-02-23 15:24:09 -08001972/**
Li Zefan081aa452013-03-13 09:17:09 +08001973 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001974 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001975 * @tsk: the task or the leader of the threadgroup to be attached
1976 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001977 *
Tejun Heo257058a2011-12-12 18:12:21 -08001978 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001979 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001980 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001981static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1982 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001983{
1984 int retval, i, group_size;
1985 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001986 struct cgroupfs_root *root = cgrp->root;
1987 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001988 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001989 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001990 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001991 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001992
1993 /*
1994 * step 0: in order to do expensive, possibly blocking operations for
1995 * every thread, we cannot iterate the thread group list, since it needs
1996 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001997 * group - group_rwsem prevents new threads from appearing, and if
1998 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001999 */
Li Zefan081aa452013-03-13 09:17:09 +08002000 if (threadgroup)
2001 group_size = get_nr_threads(tsk);
2002 else
2003 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002004 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002005 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002006 if (!group)
2007 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002008 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002009 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002010 if (retval)
2011 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002012
Ben Blum74a11662011-05-26 16:25:20 -07002013 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002014 /*
2015 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2016 * already PF_EXITING could be freed from underneath us unless we
2017 * take an rcu_read_lock.
2018 */
2019 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002020 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002021 struct task_and_cgroup ent;
2022
Tejun Heocd3d0952011-12-12 18:12:21 -08002023 /* @tsk either already exited or can't exit until the end */
2024 if (tsk->flags & PF_EXITING)
2025 continue;
2026
Ben Blum74a11662011-05-26 16:25:20 -07002027 /* as per above, nr_threads may decrease, but not increase. */
2028 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002029 ent.task = tsk;
2030 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002031 /* nothing to do if this task is already in the cgroup */
2032 if (ent.cgrp == cgrp)
2033 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002034 /*
2035 * saying GFP_ATOMIC has no effect here because we did prealloc
2036 * earlier, but it's good form to communicate our expectations.
2037 */
Tejun Heo134d3372011-12-12 18:12:21 -08002038 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002039 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002040 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002041
2042 if (!threadgroup)
2043 break;
Ben Blum74a11662011-05-26 16:25:20 -07002044 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002045 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002046 /* remember the number of threads in the array for later. */
2047 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002048 tset.tc_array = group;
2049 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002050
Tejun Heo134d3372011-12-12 18:12:21 -08002051 /* methods shouldn't be called if no task is actually migrating */
2052 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002053 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002054 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002055
Ben Blum74a11662011-05-26 16:25:20 -07002056 /*
2057 * step 1: check that we can legitimately attach to the cgroup.
2058 */
Tejun Heo5549c492013-06-24 15:21:48 -07002059 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002060 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2061
Ben Blum74a11662011-05-26 16:25:20 -07002062 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002063 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002064 if (retval) {
2065 failed_ss = ss;
2066 goto out_cancel_attach;
2067 }
2068 }
Ben Blum74a11662011-05-26 16:25:20 -07002069 }
2070
2071 /*
2072 * step 2: make sure css_sets exist for all threads to be migrated.
2073 * we use find_css_set, which allocates a new one if necessary.
2074 */
Ben Blum74a11662011-05-26 16:25:20 -07002075 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002076 struct css_set *old_cset;
2077
Tejun Heo134d3372011-12-12 18:12:21 -08002078 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002079 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002080 tc->cset = find_css_set(old_cset, cgrp);
2081 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002082 retval = -ENOMEM;
2083 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085 }
2086
2087 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002088 * step 3: now that we're guaranteed success wrt the css_sets,
2089 * proceed to move all tasks to the new cgroup. There are no
2090 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002091 */
Ben Blum74a11662011-05-26 16:25:20 -07002092 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002093 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002094 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002095 }
2096 /* nothing is sensitive to fork() after this point. */
2097
2098 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002099 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002100 */
Tejun Heo5549c492013-06-24 15:21:48 -07002101 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002102 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2103
Ben Blum74a11662011-05-26 16:25:20 -07002104 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002105 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002106 }
2107
2108 /*
2109 * step 5: success! and cleanup
2110 */
Ben Blum74a11662011-05-26 16:25:20 -07002111 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002112out_put_css_set_refs:
2113 if (retval) {
2114 for (i = 0; i < group_size; i++) {
2115 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002116 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002117 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002118 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002119 }
Ben Blum74a11662011-05-26 16:25:20 -07002120 }
2121out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002122 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002123 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002124 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2125
Tejun Heo494c1672011-12-12 18:12:22 -08002126 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002127 break;
Ben Blum74a11662011-05-26 16:25:20 -07002128 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002129 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002130 }
2131 }
Ben Blum74a11662011-05-26 16:25:20 -07002132out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002133 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002134 return retval;
2135}
2136
2137/*
2138 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002139 * function to attach either it or all tasks in its threadgroup. Will lock
2140 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002141 */
2142static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002143{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002144 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002145 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002146 int ret;
2147
Ben Blum74a11662011-05-26 16:25:20 -07002148 if (!cgroup_lock_live_group(cgrp))
2149 return -ENODEV;
2150
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002151retry_find_task:
2152 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002153 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002154 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002155 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002156 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002157 ret= -ESRCH;
2158 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002159 }
Ben Blum74a11662011-05-26 16:25:20 -07002160 /*
2161 * even if we're attaching all tasks in the thread group, we
2162 * only need to check permissions on one of them.
2163 */
David Howellsc69e8d92008-11-14 10:39:19 +11002164 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002165 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2166 !uid_eq(cred->euid, tcred->uid) &&
2167 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002168 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002169 ret = -EACCES;
2170 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 } else
2173 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002174
2175 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002176 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002177
2178 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002179 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002180 * trapped in a cpuset, or RT worker may be born in a cgroup
2181 * with no rt_runtime allocated. Just say no.
2182 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002183 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002184 ret = -EINVAL;
2185 rcu_read_unlock();
2186 goto out_unlock_cgroup;
2187 }
2188
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189 get_task_struct(tsk);
2190 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002191
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002192 threadgroup_lock(tsk);
2193 if (threadgroup) {
2194 if (!thread_group_leader(tsk)) {
2195 /*
2196 * a race with de_thread from another thread's exec()
2197 * may strip us of our leadership, if this happens,
2198 * there is no choice but to throw this task away and
2199 * try again; this is
2200 * "double-double-toil-and-trouble-check locking".
2201 */
2202 threadgroup_unlock(tsk);
2203 put_task_struct(tsk);
2204 goto retry_find_task;
2205 }
Li Zefan081aa452013-03-13 09:17:09 +08002206 }
2207
2208 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2209
Tejun Heocd3d0952011-12-12 18:12:21 -08002210 threadgroup_unlock(tsk);
2211
Paul Menagebbcb81d2007-10-18 23:39:32 -07002212 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002213out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002214 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002215 return ret;
2216}
2217
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002218/**
2219 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2220 * @from: attach to all cgroups of a given task
2221 * @tsk: the task to be attached
2222 */
2223int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2224{
2225 struct cgroupfs_root *root;
2226 int retval = 0;
2227
Tejun Heo47cfcd02013-04-07 09:29:51 -07002228 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002229 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002230 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002231
Li Zefan6f4b7e62013-07-31 16:18:36 +08002232 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233 if (retval)
2234 break;
2235 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002236 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002237
2238 return retval;
2239}
2240EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2241
Tejun Heo182446d2013-08-08 20:11:24 -04002242static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2243 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002244{
Tejun Heo182446d2013-08-08 20:11:24 -04002245 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002246}
2247
Tejun Heo182446d2013-08-08 20:11:24 -04002248static int cgroup_procs_write(struct cgroup_subsys_state *css,
2249 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002250{
Tejun Heo182446d2013-08-08 20:11:24 -04002251 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002252}
2253
Tejun Heo182446d2013-08-08 20:11:24 -04002254static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2255 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002256{
Tejun Heo182446d2013-08-08 20:11:24 -04002257 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002258 if (strlen(buffer) >= PATH_MAX)
2259 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002260 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002261 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002262 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002263 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002264 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002265 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002266 return 0;
2267}
2268
Tejun Heo182446d2013-08-08 20:11:24 -04002269static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2270 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002271{
Tejun Heo182446d2013-08-08 20:11:24 -04002272 struct cgroup *cgrp = css->cgroup;
2273
Paul Menagee788e062008-07-25 01:46:59 -07002274 if (!cgroup_lock_live_group(cgrp))
2275 return -ENODEV;
2276 seq_puts(seq, cgrp->root->release_agent_path);
2277 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002278 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002279 return 0;
2280}
2281
Tejun Heo182446d2013-08-08 20:11:24 -04002282static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2283 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002284{
Tejun Heo182446d2013-08-08 20:11:24 -04002285 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002286 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002287}
2288
Tejun Heof7d58812013-08-08 20:11:23 -04002289/* return the css for the given cgroup file */
2290static struct cgroup_subsys_state *cgroup_file_css(struct cfent *cfe)
2291{
2292 struct cftype *cft = cfe->type;
2293 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2294
2295 if (cft->ss)
2296 return cgrp->subsys[cft->ss->subsys_id];
Tejun Heo67f4c362013-08-08 20:11:24 -04002297 return &cgrp->dummy_css;
Tejun Heof7d58812013-08-08 20:11:23 -04002298}
2299
Paul Menage84eea842008-07-25 01:47:00 -07002300/* A buffer size big enough for numbers or short strings */
2301#define CGROUP_LOCAL_BUFFER_SIZE 64
2302
Tejun Heo182446d2013-08-08 20:11:24 -04002303static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2304 struct cftype *cft, struct file *file,
2305 const char __user *userbuf, size_t nbytes,
2306 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002307{
Paul Menage84eea842008-07-25 01:47:00 -07002308 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002309 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002310 char *end;
2311
2312 if (!nbytes)
2313 return -EINVAL;
2314 if (nbytes >= sizeof(buffer))
2315 return -E2BIG;
2316 if (copy_from_user(buffer, userbuf, nbytes))
2317 return -EFAULT;
2318
2319 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002320 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002321 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002322 if (*end)
2323 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002324 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002325 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002326 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (*end)
2328 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002329 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002330 }
Paul Menage355e0c42007-10-18 23:39:33 -07002331 if (!retval)
2332 retval = nbytes;
2333 return retval;
2334}
2335
Tejun Heo182446d2013-08-08 20:11:24 -04002336static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2337 struct cftype *cft, struct file *file,
2338 const char __user *userbuf, size_t nbytes,
2339 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002340{
Paul Menage84eea842008-07-25 01:47:00 -07002341 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002342 int retval = 0;
2343 size_t max_bytes = cft->max_write_len;
2344 char *buffer = local_buffer;
2345
2346 if (!max_bytes)
2347 max_bytes = sizeof(local_buffer) - 1;
2348 if (nbytes >= max_bytes)
2349 return -E2BIG;
2350 /* Allocate a dynamic buffer if we need one */
2351 if (nbytes >= sizeof(local_buffer)) {
2352 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2353 if (buffer == NULL)
2354 return -ENOMEM;
2355 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002356 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2357 retval = -EFAULT;
2358 goto out;
2359 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002360
2361 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002362 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002363 if (!retval)
2364 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002365out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002366 if (buffer != local_buffer)
2367 kfree(buffer);
2368 return retval;
2369}
2370
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002372 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373{
Tejun Heo182446d2013-08-08 20:11:24 -04002374 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002375 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002376 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377
Paul Menage355e0c42007-10-18 23:39:33 -07002378 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002379 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002380 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002381 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002382 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002383 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002384 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002385 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002386 return ret ? ret : nbytes;
2387 }
Paul Menage355e0c42007-10-18 23:39:33 -07002388 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002389}
2390
Tejun Heo182446d2013-08-08 20:11:24 -04002391static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2392 struct cftype *cft, struct file *file,
2393 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394{
Paul Menage84eea842008-07-25 01:47:00 -07002395 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002396 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002397 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2398
2399 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2400}
2401
Tejun Heo182446d2013-08-08 20:11:24 -04002402static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2403 struct cftype *cft, struct file *file,
2404 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002405{
Paul Menage84eea842008-07-25 01:47:00 -07002406 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002407 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002408 int len = sprintf(tmp, "%lld\n", (long long) val);
2409
2410 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2411}
2412
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002414 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415{
Tejun Heo182446d2013-08-08 20:11:24 -04002416 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002417 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002418 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002419
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002421 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002422 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002423 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002424 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002425 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426 return -EINVAL;
2427}
2428
Paul Menage91796562008-04-29 01:00:01 -07002429/*
2430 * seqfile ops/methods for returning structured data. Currently just
2431 * supports string->u64 maps, but can be extended in future.
2432 */
2433
Paul Menage91796562008-04-29 01:00:01 -07002434static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2435{
2436 struct seq_file *sf = cb->state;
2437 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2438}
2439
2440static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2441{
Li Zefane0798ce2013-07-31 17:36:25 +08002442 struct cfent *cfe = m->private;
2443 struct cftype *cft = cfe->type;
Tejun Heo182446d2013-08-08 20:11:24 -04002444 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Li Zefane0798ce2013-07-31 17:36:25 +08002445
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002446 if (cft->read_map) {
2447 struct cgroup_map_cb cb = {
2448 .fill = cgroup_map_add,
2449 .state = m,
2450 };
Tejun Heo182446d2013-08-08 20:11:24 -04002451 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002452 }
Tejun Heo182446d2013-08-08 20:11:24 -04002453 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002454}
2455
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002456static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002457 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002458 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002459 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002460 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002461};
2462
Paul Menageddbcc7e2007-10-18 23:39:30 -07002463static int cgroup_file_open(struct inode *inode, struct file *file)
2464{
Tejun Heof7d58812013-08-08 20:11:23 -04002465 struct cfent *cfe = __d_cfe(file->f_dentry);
2466 struct cftype *cft = __d_cft(file->f_dentry);
2467 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002468 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469
2470 err = generic_file_open(inode, file);
2471 if (err)
2472 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002473
2474 /*
2475 * If the file belongs to a subsystem, pin the css. Will be
2476 * unpinned either on open failure or release. This ensures that
2477 * @css stays alive for all file operations.
2478 */
Tejun Heo67f4c362013-08-08 20:11:24 -04002479 if (css->ss && !css_tryget(css))
Tejun Heof7d58812013-08-08 20:11:23 -04002480 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002481
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002482 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002483 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002484 err = single_open(file, cgroup_seqfile_show, cfe);
2485 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002486 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002487 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002488
Tejun Heo67f4c362013-08-08 20:11:24 -04002489 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002490 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002491 return err;
2492}
2493
2494static int cgroup_file_release(struct inode *inode, struct file *file)
2495{
Tejun Heof7d58812013-08-08 20:11:23 -04002496 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heof7d58812013-08-08 20:11:23 -04002498 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
2499 int ret = 0;
2500
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002502 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002503 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002504 css_put(css);
2505 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002506}
2507
2508/*
2509 * cgroup_rename - Only allow simple rename of directories in place.
2510 */
2511static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2512 struct inode *new_dir, struct dentry *new_dentry)
2513{
Li Zefan65dff752013-03-01 15:01:56 +08002514 int ret;
2515 struct cgroup_name *name, *old_name;
2516 struct cgroup *cgrp;
2517
2518 /*
2519 * It's convinient to use parent dir's i_mutex to protected
2520 * cgrp->name.
2521 */
2522 lockdep_assert_held(&old_dir->i_mutex);
2523
Paul Menageddbcc7e2007-10-18 23:39:30 -07002524 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2525 return -ENOTDIR;
2526 if (new_dentry->d_inode)
2527 return -EEXIST;
2528 if (old_dir != new_dir)
2529 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002530
2531 cgrp = __d_cgrp(old_dentry);
2532
Tejun Heo6db8e852013-06-14 11:18:22 -07002533 /*
2534 * This isn't a proper migration and its usefulness is very
2535 * limited. Disallow if sane_behavior.
2536 */
2537 if (cgroup_sane_behavior(cgrp))
2538 return -EPERM;
2539
Li Zefan65dff752013-03-01 15:01:56 +08002540 name = cgroup_alloc_name(new_dentry);
2541 if (!name)
2542 return -ENOMEM;
2543
2544 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2545 if (ret) {
2546 kfree(name);
2547 return ret;
2548 }
2549
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002550 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002551 rcu_assign_pointer(cgrp->name, name);
2552
2553 kfree_rcu(old_name, rcu_head);
2554 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002555}
2556
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002557static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2558{
2559 if (S_ISDIR(dentry->d_inode->i_mode))
2560 return &__d_cgrp(dentry)->xattrs;
2561 else
Li Zefan712317a2013-04-18 23:09:52 -07002562 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002563}
2564
2565static inline int xattr_enabled(struct dentry *dentry)
2566{
2567 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002568 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002569}
2570
2571static bool is_valid_xattr(const char *name)
2572{
2573 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2574 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2575 return true;
2576 return false;
2577}
2578
2579static int cgroup_setxattr(struct dentry *dentry, const char *name,
2580 const void *val, size_t size, int flags)
2581{
2582 if (!xattr_enabled(dentry))
2583 return -EOPNOTSUPP;
2584 if (!is_valid_xattr(name))
2585 return -EINVAL;
2586 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2587}
2588
2589static int cgroup_removexattr(struct dentry *dentry, const char *name)
2590{
2591 if (!xattr_enabled(dentry))
2592 return -EOPNOTSUPP;
2593 if (!is_valid_xattr(name))
2594 return -EINVAL;
2595 return simple_xattr_remove(__d_xattrs(dentry), name);
2596}
2597
2598static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2599 void *buf, size_t size)
2600{
2601 if (!xattr_enabled(dentry))
2602 return -EOPNOTSUPP;
2603 if (!is_valid_xattr(name))
2604 return -EINVAL;
2605 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2606}
2607
2608static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2609{
2610 if (!xattr_enabled(dentry))
2611 return -EOPNOTSUPP;
2612 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2613}
2614
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002615static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002616 .read = cgroup_file_read,
2617 .write = cgroup_file_write,
2618 .llseek = generic_file_llseek,
2619 .open = cgroup_file_open,
2620 .release = cgroup_file_release,
2621};
2622
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002623static const struct inode_operations cgroup_file_inode_operations = {
2624 .setxattr = cgroup_setxattr,
2625 .getxattr = cgroup_getxattr,
2626 .listxattr = cgroup_listxattr,
2627 .removexattr = cgroup_removexattr,
2628};
2629
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002630static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002631 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002632 .mkdir = cgroup_mkdir,
2633 .rmdir = cgroup_rmdir,
2634 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002635 .setxattr = cgroup_setxattr,
2636 .getxattr = cgroup_getxattr,
2637 .listxattr = cgroup_listxattr,
2638 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002639};
2640
Al Viro00cd8dd2012-06-10 17:13:09 -04002641static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002642{
2643 if (dentry->d_name.len > NAME_MAX)
2644 return ERR_PTR(-ENAMETOOLONG);
2645 d_add(dentry, NULL);
2646 return NULL;
2647}
2648
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002649/*
2650 * Check if a file is a control file
2651 */
2652static inline struct cftype *__file_cft(struct file *file)
2653{
Al Viro496ad9a2013-01-23 17:07:38 -05002654 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002655 return ERR_PTR(-EINVAL);
2656 return __d_cft(file->f_dentry);
2657}
2658
Al Viroa5e7ed32011-07-26 01:55:55 -04002659static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002660 struct super_block *sb)
2661{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002662 struct inode *inode;
2663
2664 if (!dentry)
2665 return -ENOENT;
2666 if (dentry->d_inode)
2667 return -EEXIST;
2668
2669 inode = cgroup_new_inode(mode, sb);
2670 if (!inode)
2671 return -ENOMEM;
2672
2673 if (S_ISDIR(mode)) {
2674 inode->i_op = &cgroup_dir_inode_operations;
2675 inode->i_fop = &simple_dir_operations;
2676
2677 /* start off with i_nlink == 2 (for "." entry) */
2678 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002679 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002680
Tejun Heob8a2df62012-11-19 08:13:37 -08002681 /*
2682 * Control reaches here with cgroup_mutex held.
2683 * @inode->i_mutex should nest outside cgroup_mutex but we
2684 * want to populate it immediately without releasing
2685 * cgroup_mutex. As @inode isn't visible to anyone else
2686 * yet, trylock will always succeed without affecting
2687 * lockdep checks.
2688 */
2689 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 } else if (S_ISREG(mode)) {
2691 inode->i_size = 0;
2692 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002693 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002694 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695 d_instantiate(dentry, inode);
2696 dget(dentry); /* Extra count - pin the dentry in core */
2697 return 0;
2698}
2699
Li Zefan099fca32009-04-02 16:57:29 -07002700/**
2701 * cgroup_file_mode - deduce file mode of a control file
2702 * @cft: the control file in question
2703 *
2704 * returns cft->mode if ->mode is not 0
2705 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2706 * returns S_IRUGO if it has only a read handler
2707 * returns S_IWUSR if it has only a write hander
2708 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002709static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002710{
Al Viroa5e7ed32011-07-26 01:55:55 -04002711 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002712
2713 if (cft->mode)
2714 return cft->mode;
2715
2716 if (cft->read || cft->read_u64 || cft->read_s64 ||
2717 cft->read_map || cft->read_seq_string)
2718 mode |= S_IRUGO;
2719
2720 if (cft->write || cft->write_u64 || cft->write_s64 ||
2721 cft->write_string || cft->trigger)
2722 mode |= S_IWUSR;
2723
2724 return mode;
2725}
2726
Tejun Heo2bb566c2013-08-08 20:11:23 -04002727static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002728{
Paul Menagebd89aab2007-10-18 23:40:44 -07002729 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002730 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002732 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002733 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002734 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002736
Tejun Heo2bb566c2013-08-08 20:11:23 -04002737 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2738 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 strcat(name, ".");
2740 }
2741 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002742
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744
2745 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2746 if (!cfe)
2747 return -ENOMEM;
2748
Paul Menageddbcc7e2007-10-18 23:39:30 -07002749 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002750 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002751 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002752 goto out;
2753 }
2754
Li Zefand6cbf352013-05-14 19:44:20 +08002755 cfe->type = (void *)cft;
2756 cfe->dentry = dentry;
2757 dentry->d_fsdata = cfe;
2758 simple_xattrs_init(&cfe->xattrs);
2759
Tejun Heo05ef1d72012-04-01 12:09:56 -07002760 mode = cgroup_file_mode(cft);
2761 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2762 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002763 list_add_tail(&cfe->node, &parent->files);
2764 cfe = NULL;
2765 }
2766 dput(dentry);
2767out:
2768 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002769 return error;
2770}
2771
Tejun Heob1f28d32013-06-28 16:24:10 -07002772/**
2773 * cgroup_addrm_files - add or remove files to a cgroup directory
2774 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002775 * @cfts: array of cftypes to be added
2776 * @is_add: whether to add or remove
2777 *
2778 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002779 * For removals, this function never fails. If addition fails, this
2780 * function doesn't remove files already added. The caller is responsible
2781 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002782 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002783static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2784 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002785{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002786 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002787 int ret;
2788
2789 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2790 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002791
2792 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002793 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002794 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2795 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002796 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2797 continue;
2798 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2799 continue;
2800
Li Zefan2739d3c2013-01-21 18:18:33 +08002801 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002802 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002803 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002804 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002805 cft->name, ret);
2806 return ret;
2807 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002808 } else {
2809 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002810 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002811 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002812 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002813}
2814
Tejun Heo8e3f6542012-04-01 12:09:55 -07002815static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002816 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002817{
2818 /*
2819 * Thanks to the entanglement with vfs inode locking, we can't walk
2820 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002821 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2822 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002823 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002824 mutex_lock(&cgroup_mutex);
2825}
2826
Tejun Heo2bb566c2013-08-08 20:11:23 -04002827static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002828 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829{
2830 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002831 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002832 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002833 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002834 struct dentry *prev = NULL;
2835 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002836 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002837 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002838 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002839
2840 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002841 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002842 !atomic_inc_not_zero(&sb->s_active)) {
2843 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002844 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002845 }
2846
Li Zefane8c82d22013-06-18 18:48:37 +08002847 /*
2848 * All cgroups which are created after we drop cgroup_mutex will
2849 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002850 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002851 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002852 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002853
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854 mutex_unlock(&cgroup_mutex);
2855
Li Zefane8c82d22013-06-18 18:48:37 +08002856 /* @root always needs to be updated */
2857 inode = root->dentry->d_inode;
2858 mutex_lock(&inode->i_mutex);
2859 mutex_lock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002860 ret = cgroup_addrm_files(root, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002861 mutex_unlock(&cgroup_mutex);
2862 mutex_unlock(&inode->i_mutex);
2863
Tejun Heo9ccece82013-06-28 16:24:11 -07002864 if (ret)
2865 goto out_deact;
2866
Li Zefane8c82d22013-06-18 18:48:37 +08002867 /* add/rm files for all cgroups created before */
2868 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002869 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2870 struct cgroup *cgrp = css->cgroup;
2871
Li Zefane8c82d22013-06-18 18:48:37 +08002872 if (cgroup_is_dead(cgrp))
2873 continue;
2874
2875 inode = cgrp->dentry->d_inode;
2876 dget(cgrp->dentry);
2877 rcu_read_unlock();
2878
2879 dput(prev);
2880 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002881
2882 mutex_lock(&inode->i_mutex);
2883 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002884 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002885 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002886 mutex_unlock(&cgroup_mutex);
2887 mutex_unlock(&inode->i_mutex);
2888
Li Zefane8c82d22013-06-18 18:48:37 +08002889 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002890 if (ret)
2891 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002892 }
Li Zefane8c82d22013-06-18 18:48:37 +08002893 rcu_read_unlock();
2894 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002895out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002896 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002897 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002898}
2899
2900/**
2901 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2902 * @ss: target cgroup subsystem
2903 * @cfts: zero-length name terminated array of cftypes
2904 *
2905 * Register @cfts to @ss. Files described by @cfts are created for all
2906 * existing cgroups to which @ss is attached and all future cgroups will
2907 * have them too. This function can be called anytime whether @ss is
2908 * attached or not.
2909 *
2910 * Returns 0 on successful registration, -errno on failure. Note that this
2911 * function currently returns 0 as long as @cfts registration is successful
2912 * even if some file creation attempts on existing cgroups fail.
2913 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002914int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002915{
2916 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002917 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002918 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002919
2920 set = kzalloc(sizeof(*set), GFP_KERNEL);
2921 if (!set)
2922 return -ENOMEM;
2923
Tejun Heo2bb566c2013-08-08 20:11:23 -04002924 for (cft = cfts; cft->name[0] != '\0'; cft++)
2925 cft->ss = ss;
2926
Tejun Heo8e3f6542012-04-01 12:09:55 -07002927 cgroup_cfts_prepare();
2928 set->cfts = cfts;
2929 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002930 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002931 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002932 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002933 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002934}
2935EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2936
Li Zefana043e3b2008-02-23 15:24:09 -08002937/**
Tejun Heo79578622012-04-01 12:09:56 -07002938 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002939 * @cfts: zero-length name terminated array of cftypes
2940 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002941 * Unregister @cfts. Files described by @cfts are removed from all
2942 * existing cgroups and all future cgroups won't have them either. This
2943 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002944 *
2945 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002946 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002947 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002948int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002949{
2950 struct cftype_set *set;
2951
Tejun Heo2bb566c2013-08-08 20:11:23 -04002952 if (!cfts || !cfts[0].ss)
2953 return -ENOENT;
2954
Tejun Heo79578622012-04-01 12:09:56 -07002955 cgroup_cfts_prepare();
2956
Tejun Heo2bb566c2013-08-08 20:11:23 -04002957 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002958 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002959 list_del(&set->node);
2960 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002961 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002962 return 0;
2963 }
2964 }
2965
Tejun Heo2bb566c2013-08-08 20:11:23 -04002966 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002967 return -ENOENT;
2968}
2969
2970/**
Li Zefana043e3b2008-02-23 15:24:09 -08002971 * cgroup_task_count - count the number of tasks in a cgroup.
2972 * @cgrp: the cgroup in question
2973 *
2974 * Return the number of tasks in the cgroup.
2975 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002976int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002977{
2978 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002979 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002980
Paul Menage817929e2007-10-18 23:39:36 -07002981 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002982 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2983 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002984 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002985 return count;
2986}
2987
2988/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002989 * To reduce the fork() overhead for systems that are not actually using
2990 * their cgroups capability, we don't maintain the lists running through
2991 * each css_set to its tasks until we see the list actually used - in other
2992 * words after the first call to cgroup_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002993 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002994static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002995{
2996 struct task_struct *p, *g;
2997 write_lock(&css_set_lock);
2998 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01002999 /*
3000 * We need tasklist_lock because RCU is not safe against
3001 * while_each_thread(). Besides, a forking task that has passed
3002 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3003 * is not guaranteed to have its child immediately visible in the
3004 * tasklist if we walk through it with RCU.
3005 */
3006 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003007 do_each_thread(g, p) {
3008 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003009 /*
3010 * We should check if the process is exiting, otherwise
3011 * it will race with cgroup_exit() in that the list
3012 * entry won't be deleted though the process has exited.
3013 */
3014 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003015 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003016 task_unlock(p);
3017 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003018 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 write_unlock(&css_set_lock);
3020}
3021
Tejun Heo574bd9f2012-11-09 09:12:29 -08003022/**
Tejun Heo492eb212013-08-08 20:11:25 -04003023 * css_next_child - find the next child of a given css
3024 * @pos_css: the current position (%NULL to initiate traversal)
3025 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003026 *
Tejun Heo492eb212013-08-08 20:11:25 -04003027 * This function returns the next child of @parent_css and should be called
3028 * under RCU read lock. The only requirement is that @parent_css and
3029 * @pos_css are accessible. The next sibling is guaranteed to be returned
3030 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003031 */
Tejun Heo492eb212013-08-08 20:11:25 -04003032struct cgroup_subsys_state *
3033css_next_child(struct cgroup_subsys_state *pos_css,
3034 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003035{
Tejun Heo492eb212013-08-08 20:11:25 -04003036 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3037 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003038 struct cgroup *next;
3039
3040 WARN_ON_ONCE(!rcu_read_lock_held());
3041
3042 /*
3043 * @pos could already have been removed. Once a cgroup is removed,
3044 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003045 * changes. As CGRP_DEAD assertion is serialized and happens
3046 * before the cgroup is taken off the ->sibling list, if we see it
3047 * unasserted, it's guaranteed that the next sibling hasn't
3048 * finished its grace period even if it's already removed, and thus
3049 * safe to dereference from this RCU critical section. If
3050 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3051 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003052 *
3053 * If @pos is dead, its next pointer can't be dereferenced;
3054 * however, as each cgroup is given a monotonically increasing
3055 * unique serial number and always appended to the sibling list,
3056 * the next one can be found by walking the parent's children until
3057 * we see a cgroup with higher serial number than @pos's. While
3058 * this path can be slower, it's taken only when either the current
3059 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003060 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003061 if (!pos) {
3062 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3063 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003064 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003065 } else {
3066 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3067 if (next->serial_nr > pos->serial_nr)
3068 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003069 }
3070
Tejun Heo492eb212013-08-08 20:11:25 -04003071 if (&next->sibling == &cgrp->children)
3072 return NULL;
3073
3074 if (parent_css->ss)
3075 return cgroup_css(next, parent_css->ss->subsys_id);
3076 else
3077 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003078}
Tejun Heo492eb212013-08-08 20:11:25 -04003079EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003080
3081/**
Tejun Heo492eb212013-08-08 20:11:25 -04003082 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003083 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003084 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 *
Tejun Heo492eb212013-08-08 20:11:25 -04003086 * To be used by css_for_each_descendant_pre(). Find the next descendant
3087 * to visit for pre-order traversal of @root's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003088 *
3089 * While this function requires RCU read locking, it doesn't require the
3090 * whole traversal to be contained in a single RCU critical section. This
3091 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003092 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003093 */
Tejun Heo492eb212013-08-08 20:11:25 -04003094struct cgroup_subsys_state *
3095css_next_descendant_pre(struct cgroup_subsys_state *pos,
3096 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097{
Tejun Heo492eb212013-08-08 20:11:25 -04003098 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099
3100 WARN_ON_ONCE(!rcu_read_lock_held());
3101
Tejun Heo492eb212013-08-08 20:11:25 -04003102 /* if first iteration, pretend we just visited @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003103 if (!pos)
Tejun Heo492eb212013-08-08 20:11:25 -04003104 pos = root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003105
3106 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003107 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 if (next)
3109 return next;
3110
3111 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003112 while (pos != root) {
3113 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003114 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003115 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003116 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003117 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118
3119 return NULL;
3120}
Tejun Heo492eb212013-08-08 20:11:25 -04003121EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003122
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003123/**
Tejun Heo492eb212013-08-08 20:11:25 -04003124 * css_rightmost_descendant - return the rightmost descendant of a css
3125 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003126 *
Tejun Heo492eb212013-08-08 20:11:25 -04003127 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3128 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003129 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003130 *
3131 * While this function requires RCU read locking, it doesn't require the
3132 * whole traversal to be contained in a single RCU critical section. This
3133 * function will return the correct rightmost descendant as long as @pos is
3134 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003135 */
Tejun Heo492eb212013-08-08 20:11:25 -04003136struct cgroup_subsys_state *
3137css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003138{
Tejun Heo492eb212013-08-08 20:11:25 -04003139 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003140
3141 WARN_ON_ONCE(!rcu_read_lock_held());
3142
3143 do {
3144 last = pos;
3145 /* ->prev isn't RCU safe, walk ->next till the end */
3146 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003147 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003148 pos = tmp;
3149 } while (pos);
3150
3151 return last;
3152}
Tejun Heo492eb212013-08-08 20:11:25 -04003153EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003154
Tejun Heo492eb212013-08-08 20:11:25 -04003155static struct cgroup_subsys_state *
3156css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003157{
Tejun Heo492eb212013-08-08 20:11:25 -04003158 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003159
3160 do {
3161 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003162 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003163 } while (pos);
3164
3165 return last;
3166}
3167
3168/**
Tejun Heo492eb212013-08-08 20:11:25 -04003169 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003170 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003171 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 *
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * To be used by css_for_each_descendant_post(). Find the next descendant
3174 * to visit for post-order traversal of @root's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003175 *
3176 * While this function requires RCU read locking, it doesn't require the
3177 * whole traversal to be contained in a single RCU critical section. This
3178 * function will return the correct next descendant as long as both @pos
3179 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003180 */
Tejun Heo492eb212013-08-08 20:11:25 -04003181struct cgroup_subsys_state *
3182css_next_descendant_post(struct cgroup_subsys_state *pos,
3183 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003184{
Tejun Heo492eb212013-08-08 20:11:25 -04003185 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186
3187 WARN_ON_ONCE(!rcu_read_lock_held());
3188
3189 /* if first iteration, visit the leftmost descendant */
3190 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003191 next = css_leftmost_descendant(root);
3192 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193 }
3194
3195 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003196 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003197 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003198 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003199
3200 /* no sibling left, visit parent */
Tejun Heo492eb212013-08-08 20:11:25 -04003201 next = css_parent(pos);
3202 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203}
Tejun Heo492eb212013-08-08 20:11:25 -04003204EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003205
Tejun Heo0942eee2013-08-08 20:11:26 -04003206/**
3207 * cgroup_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003208 * @it: the iterator to advance
3209 *
3210 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003211 */
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003212static void cgroup_advance_task_iter(struct cgroup_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003213{
3214 struct list_head *l = it->cset_link;
3215 struct cgrp_cset_link *link;
3216 struct css_set *cset;
3217
3218 /* Advance to the next non-empty css_set */
3219 do {
3220 l = l->next;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003221 if (l == &it->origin_cgrp->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003222 it->cset_link = NULL;
3223 return;
3224 }
3225 link = list_entry(l, struct cgrp_cset_link, cset_link);
3226 cset = link->cset;
3227 } while (list_empty(&cset->tasks));
3228 it->cset_link = l;
3229 it->task = cset->tasks.next;
3230}
3231
Tejun Heo0942eee2013-08-08 20:11:26 -04003232/**
3233 * cgroup_task_iter_start - initiate task iteration
3234 * @cgrp: the cgroup to walk tasks of
3235 * @it: the task iterator to use
3236 *
3237 * Initiate iteration through the tasks of @cgrp. The caller can call
3238 * cgroup_task_iter_next() to walk through the tasks until the function
3239 * returns NULL. On completion of iteration, cgroup_task_iter_end() must
3240 * be called.
3241 *
3242 * Note that this function acquires a lock which is released when the
3243 * iteration finishes. The caller can't sleep while iteration is in
3244 * progress.
3245 */
3246void cgroup_task_iter_start(struct cgroup *cgrp, struct cgroup_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003247 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003248{
3249 /*
3250 * The first time anyone tries to iterate across a cgroup,
3251 * we need to enable the list linking each css_set to its
3252 * tasks, and fix up all existing tasks.
3253 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003254 if (!use_task_css_set_links)
3255 cgroup_enable_task_cg_lists();
3256
Paul Menage817929e2007-10-18 23:39:36 -07003257 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003258
3259 it->origin_cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -07003260 it->cset_link = &cgrp->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003261
3262 cgroup_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003263}
3264
Tejun Heo0942eee2013-08-08 20:11:26 -04003265/**
3266 * cgroup_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003267 * @it: the task iterator being iterated
3268 *
3269 * The "next" function for task iteration. @it should have been
3270 * initialized via cgroup_task_iter_start(). Returns NULL when the
3271 * iteration reaches the end.
3272 */
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003273struct task_struct *cgroup_task_iter_next(struct cgroup_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003274{
3275 struct task_struct *res;
3276 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003277 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003278
3279 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003280 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003281 return NULL;
3282 res = list_entry(l, struct task_struct, cg_list);
3283 /* Advance iterator to find next entry */
3284 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003285 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3286 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003287 /*
3288 * We reached the end of this task list - move on to the
3289 * next cgrp_cset_link.
3290 */
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003291 cgroup_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003292 } else {
3293 it->task = l;
3294 }
3295 return res;
3296}
3297
Tejun Heo0942eee2013-08-08 20:11:26 -04003298/**
3299 * cgroup_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003300 * @it: the task iterator to finish
3301 *
3302 * Finish task iteration started by cgroup_task_iter_start().
3303 */
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003304void cgroup_task_iter_end(struct cgroup_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003305 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003306{
3307 read_unlock(&css_set_lock);
3308}
3309
Cliff Wickman31a7df02008-02-07 00:14:42 -08003310static inline int started_after_time(struct task_struct *t1,
3311 struct timespec *time,
3312 struct task_struct *t2)
3313{
3314 int start_diff = timespec_compare(&t1->start_time, time);
3315 if (start_diff > 0) {
3316 return 1;
3317 } else if (start_diff < 0) {
3318 return 0;
3319 } else {
3320 /*
3321 * Arbitrarily, if two processes started at the same
3322 * time, we'll say that the lower pointer value
3323 * started first. Note that t2 may have exited by now
3324 * so this may not be a valid pointer any longer, but
3325 * that's fine - it still serves to distinguish
3326 * between two tasks started (effectively) simultaneously.
3327 */
3328 return t1 > t2;
3329 }
3330}
3331
3332/*
3333 * This function is a callback from heap_insert() and is used to order
3334 * the heap.
3335 * In this case we order the heap in descending task start time.
3336 */
3337static inline int started_after(void *p1, void *p2)
3338{
3339 struct task_struct *t1 = p1;
3340 struct task_struct *t2 = p2;
3341 return started_after_time(t1, &t2->start_time, t2);
3342}
3343
3344/**
3345 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3346 * @scan: struct cgroup_scanner containing arguments for the scan
3347 *
3348 * Arguments include pointers to callback functions test_task() and
3349 * process_task().
3350 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3351 * and if it returns true, call process_task() for it also.
3352 * The test_task pointer may be NULL, meaning always true (select all tasks).
Tejun Heo0942eee2013-08-08 20:11:26 -04003353 * Effectively duplicates cgroup_task_iter_{start,next,end}()
Cliff Wickman31a7df02008-02-07 00:14:42 -08003354 * but does not lock css_set_lock for the call to process_task().
3355 * The struct cgroup_scanner may be embedded in any structure of the caller's
3356 * creation.
3357 * It is guaranteed that process_task() will act on every task that
3358 * is a member of the cgroup for the duration of this call. This
3359 * function may or may not call process_task() for tasks that exit
3360 * or move to a different cgroup during the call, or are forked or
3361 * move into the cgroup during the call.
3362 *
3363 * Note that test_task() may be called with locks held, and may in some
3364 * situations be called multiple times for the same task, so it should
3365 * be cheap.
3366 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3367 * pre-allocated and will be used for heap operations (and its "gt" member will
3368 * be overwritten), else a temporary heap will be used (allocation of which
3369 * may cause this function to fail).
3370 */
3371int cgroup_scan_tasks(struct cgroup_scanner *scan)
3372{
3373 int retval, i;
Tejun Heo0942eee2013-08-08 20:11:26 -04003374 struct cgroup_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003375 struct task_struct *p, *dropped;
3376 /* Never dereference latest_task, since it's not refcounted */
3377 struct task_struct *latest_task = NULL;
3378 struct ptr_heap tmp_heap;
3379 struct ptr_heap *heap;
3380 struct timespec latest_time = { 0, 0 };
3381
3382 if (scan->heap) {
3383 /* The caller supplied our heap and pre-allocated its memory */
3384 heap = scan->heap;
3385 heap->gt = &started_after;
3386 } else {
3387 /* We need to allocate our own heap memory */
3388 heap = &tmp_heap;
3389 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3390 if (retval)
3391 /* cannot allocate the heap */
3392 return retval;
3393 }
3394
3395 again:
3396 /*
3397 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3398 * to determine which are of interest, and using the scanner's
3399 * "process_task" callback to process any of them that need an update.
3400 * Since we don't want to hold any locks during the task updates,
3401 * gather tasks to be processed in a heap structure.
3402 * The heap is sorted by descending task start time.
3403 * If the statically-sized heap fills up, we overflow tasks that
3404 * started later, and in future iterations only consider tasks that
3405 * started after the latest task in the previous pass. This
3406 * guarantees forward progress and that we don't miss any tasks.
3407 */
3408 heap->size = 0;
Tejun Heo0942eee2013-08-08 20:11:26 -04003409 cgroup_task_iter_start(scan->cgrp, &it);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003410 while ((p = cgroup_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003411 /*
3412 * Only affect tasks that qualify per the caller's callback,
3413 * if he provided one
3414 */
3415 if (scan->test_task && !scan->test_task(p, scan))
3416 continue;
3417 /*
3418 * Only process tasks that started after the last task
3419 * we processed
3420 */
3421 if (!started_after_time(p, &latest_time, latest_task))
3422 continue;
3423 dropped = heap_insert(heap, p);
3424 if (dropped == NULL) {
3425 /*
3426 * The new task was inserted; the heap wasn't
3427 * previously full
3428 */
3429 get_task_struct(p);
3430 } else if (dropped != p) {
3431 /*
3432 * The new task was inserted, and pushed out a
3433 * different task
3434 */
3435 get_task_struct(p);
3436 put_task_struct(dropped);
3437 }
3438 /*
3439 * Else the new task was newer than anything already in
3440 * the heap and wasn't inserted
3441 */
3442 }
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003443 cgroup_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003444
3445 if (heap->size) {
3446 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003447 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003448 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003449 latest_time = q->start_time;
3450 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003451 }
3452 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003453 scan->process_task(q, scan);
3454 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003455 }
3456 /*
3457 * If we had to process any tasks at all, scan again
3458 * in case some of them were in the middle of forking
3459 * children that didn't get processed.
3460 * Not the most efficient way to do it, but it avoids
3461 * having to take callback_mutex in the fork path
3462 */
3463 goto again;
3464 }
3465 if (heap == &tmp_heap)
3466 heap_free(&tmp_heap);
3467 return 0;
3468}
3469
Tejun Heo8cc99342013-04-07 09:29:50 -07003470static void cgroup_transfer_one_task(struct task_struct *task,
3471 struct cgroup_scanner *scan)
3472{
3473 struct cgroup *new_cgroup = scan->data;
3474
Tejun Heo47cfcd02013-04-07 09:29:51 -07003475 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003476 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003477 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003478}
3479
3480/**
3481 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3482 * @to: cgroup to which the tasks will be moved
3483 * @from: cgroup in which the tasks currently reside
3484 */
3485int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3486{
3487 struct cgroup_scanner scan;
3488
Li Zefan6f4b7e62013-07-31 16:18:36 +08003489 scan.cgrp = from;
Tejun Heo8cc99342013-04-07 09:29:50 -07003490 scan.test_task = NULL; /* select all tasks in cgroup */
3491 scan.process_task = cgroup_transfer_one_task;
3492 scan.heap = NULL;
3493 scan.data = to;
3494
3495 return cgroup_scan_tasks(&scan);
3496}
3497
Paul Menage817929e2007-10-18 23:39:36 -07003498/*
Ben Blum102a7752009-09-23 15:56:26 -07003499 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003500 *
3501 * Reading this file can return large amounts of data if a cgroup has
3502 * *lots* of attached tasks. So it may need several calls to read(),
3503 * but we cannot guarantee that the information we produce is correct
3504 * unless we produce it entirely atomically.
3505 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003506 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507
Li Zefan24528252012-01-20 11:58:43 +08003508/* which pidlist file are we talking about? */
3509enum cgroup_filetype {
3510 CGROUP_FILE_PROCS,
3511 CGROUP_FILE_TASKS,
3512};
3513
3514/*
3515 * A pidlist is a list of pids that virtually represents the contents of one
3516 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3517 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3518 * to the cgroup.
3519 */
3520struct cgroup_pidlist {
3521 /*
3522 * used to find which pidlist is wanted. doesn't change as long as
3523 * this particular list stays in the list.
3524 */
3525 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3526 /* array of xids */
3527 pid_t *list;
3528 /* how many elements the above list has */
3529 int length;
3530 /* how many files are using the current array */
3531 int use_count;
3532 /* each of these stored in a list by its cgroup */
3533 struct list_head links;
3534 /* pointer to the cgroup we belong to, for list removal purposes */
3535 struct cgroup *owner;
3536 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003537 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003538};
3539
Paul Menagebbcb81d2007-10-18 23:39:32 -07003540/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003541 * The following two functions "fix" the issue where there are more pids
3542 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3543 * TODO: replace with a kernel-wide solution to this problem
3544 */
3545#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3546static void *pidlist_allocate(int count)
3547{
3548 if (PIDLIST_TOO_LARGE(count))
3549 return vmalloc(count * sizeof(pid_t));
3550 else
3551 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3552}
3553static void pidlist_free(void *p)
3554{
3555 if (is_vmalloc_addr(p))
3556 vfree(p);
3557 else
3558 kfree(p);
3559}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003560
3561/*
Ben Blum102a7752009-09-23 15:56:26 -07003562 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003563 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003564 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003565static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003566{
Ben Blum102a7752009-09-23 15:56:26 -07003567 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003568
3569 /*
3570 * we presume the 0th element is unique, so i starts at 1. trivial
3571 * edge cases first; no work needs to be done for either
3572 */
3573 if (length == 0 || length == 1)
3574 return length;
3575 /* src and dest walk down the list; dest counts unique elements */
3576 for (src = 1; src < length; src++) {
3577 /* find next unique element */
3578 while (list[src] == list[src-1]) {
3579 src++;
3580 if (src == length)
3581 goto after;
3582 }
3583 /* dest always points to where the next unique element goes */
3584 list[dest] = list[src];
3585 dest++;
3586 }
3587after:
Ben Blum102a7752009-09-23 15:56:26 -07003588 return dest;
3589}
3590
3591static int cmppid(const void *a, const void *b)
3592{
3593 return *(pid_t *)a - *(pid_t *)b;
3594}
3595
3596/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 * find the appropriate pidlist for our purpose (given procs vs tasks)
3598 * returns with the lock on that pidlist already held, and takes care
3599 * of the use count, or returns NULL with no locks held if we're out of
3600 * memory.
3601 */
3602static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3603 enum cgroup_filetype type)
3604{
3605 struct cgroup_pidlist *l;
3606 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003607 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003608
Ben Blum72a8cb32009-09-23 15:56:27 -07003609 /*
Li Zefanb3958902013-08-01 09:52:15 +08003610 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003611 * the last ref-holder is trying to remove l from the list at the same
3612 * time. Holding the pidlist_mutex precludes somebody taking whichever
3613 * list we find out from under us - compare release_pid_array().
3614 */
3615 mutex_lock(&cgrp->pidlist_mutex);
3616 list_for_each_entry(l, &cgrp->pidlists, links) {
3617 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003618 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003619 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003620 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003621 return l;
3622 }
3623 }
3624 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003625 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 if (!l) {
3627 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 return l;
3629 }
Li Zefanb3958902013-08-01 09:52:15 +08003630 init_rwsem(&l->rwsem);
3631 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003632 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003633 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 l->owner = cgrp;
3635 list_add(&l->links, &cgrp->pidlists);
3636 mutex_unlock(&cgrp->pidlist_mutex);
3637 return l;
3638}
3639
3640/*
Ben Blum102a7752009-09-23 15:56:26 -07003641 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3642 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003643static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3644 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003645{
3646 pid_t *array;
3647 int length;
3648 int pid, n = 0; /* used for populating the array */
Tejun Heo0942eee2013-08-08 20:11:26 -04003649 struct cgroup_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003650 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003651 struct cgroup_pidlist *l;
3652
3653 /*
3654 * If cgroup gets more users after we read count, we won't have
3655 * enough space - tough. This race is indistinguishable to the
3656 * caller from the case that the additional cgroup users didn't
3657 * show up until sometime later on.
3658 */
3659 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003660 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003661 if (!array)
3662 return -ENOMEM;
3663 /* now, populate the array */
Tejun Heo0942eee2013-08-08 20:11:26 -04003664 cgroup_task_iter_start(cgrp, &it);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003665 while ((tsk = cgroup_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003666 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003667 break;
Ben Blum102a7752009-09-23 15:56:26 -07003668 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003669 if (type == CGROUP_FILE_PROCS)
3670 pid = task_tgid_vnr(tsk);
3671 else
3672 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003673 if (pid > 0) /* make sure to only use valid results */
3674 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003675 }
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003676 cgroup_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003677 length = n;
3678 /* now sort & (if procs) strip out duplicates */
3679 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003680 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003681 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 l = cgroup_pidlist_find(cgrp, type);
3683 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003684 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003686 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003688 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003689 l->list = array;
3690 l->length = length;
3691 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003692 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003694 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003695}
3696
Balbir Singh846c7bb2007-10-18 23:39:44 -07003697/**
Li Zefana043e3b2008-02-23 15:24:09 -08003698 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003699 * @stats: cgroupstats to fill information into
3700 * @dentry: A dentry entry belonging to the cgroup for which stats have
3701 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003702 *
3703 * Build and fill cgroupstats so that taskstats can export it to user
3704 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003705 */
3706int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3707{
3708 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003709 struct cgroup *cgrp;
Tejun Heo0942eee2013-08-08 20:11:26 -04003710 struct cgroup_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003711 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003712
Balbir Singh846c7bb2007-10-18 23:39:44 -07003713 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003714 * Validate dentry by checking the superblock operations,
3715 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003716 */
Li Zefan33d283b2008-11-19 15:36:48 -08003717 if (dentry->d_sb->s_op != &cgroup_ops ||
3718 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719 goto err;
3720
3721 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003722 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003723
Tejun Heo0942eee2013-08-08 20:11:26 -04003724 cgroup_task_iter_start(cgrp, &it);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003725 while ((tsk = cgroup_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003726 switch (tsk->state) {
3727 case TASK_RUNNING:
3728 stats->nr_running++;
3729 break;
3730 case TASK_INTERRUPTIBLE:
3731 stats->nr_sleeping++;
3732 break;
3733 case TASK_UNINTERRUPTIBLE:
3734 stats->nr_uninterruptible++;
3735 break;
3736 case TASK_STOPPED:
3737 stats->nr_stopped++;
3738 break;
3739 default:
3740 if (delayacct_is_task_waiting_on_io(tsk))
3741 stats->nr_io_wait++;
3742 break;
3743 }
3744 }
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003745 cgroup_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003746
Balbir Singh846c7bb2007-10-18 23:39:44 -07003747err:
3748 return ret;
3749}
3750
Paul Menage8f3ff202009-09-23 15:56:25 -07003751
Paul Menagecc31edc2008-10-18 20:28:04 -07003752/*
Ben Blum102a7752009-09-23 15:56:26 -07003753 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003754 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003755 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003756 */
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003759{
3760 /*
3761 * Initially we receive a position value that corresponds to
3762 * one more than the last pid shown (or 0 on the first call or
3763 * after a seek to the start). Use a binary-search to find the
3764 * next pid to display, if any
3765 */
Ben Blum102a7752009-09-23 15:56:26 -07003766 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 int index = 0, pid = *pos;
3768 int *iter;
3769
Li Zefanb3958902013-08-01 09:52:15 +08003770 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003772 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003773
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 while (index < end) {
3775 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003776 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 index = mid;
3778 break;
Ben Blum102a7752009-09-23 15:56:26 -07003779 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 index = mid + 1;
3781 else
3782 end = mid;
3783 }
3784 }
3785 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003786 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 return NULL;
3788 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003789 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 *pos = *iter;
3791 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003792}
3793
Ben Blum102a7752009-09-23 15:56:26 -07003794static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003795{
Ben Blum102a7752009-09-23 15:56:26 -07003796 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003797 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003798}
3799
Ben Blum102a7752009-09-23 15:56:26 -07003800static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003801{
Ben Blum102a7752009-09-23 15:56:26 -07003802 struct cgroup_pidlist *l = s->private;
3803 pid_t *p = v;
3804 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003805 /*
3806 * Advance to the next pid in the array. If this goes off the
3807 * end, we're done
3808 */
3809 p++;
3810 if (p >= end) {
3811 return NULL;
3812 } else {
3813 *pos = *p;
3814 return p;
3815 }
3816}
3817
Ben Blum102a7752009-09-23 15:56:26 -07003818static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003819{
3820 return seq_printf(s, "%d\n", *(int *)v);
3821}
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823/*
3824 * seq_operations functions for iterating on pidlists through seq_file -
3825 * independent of whether it's tasks or procs
3826 */
3827static const struct seq_operations cgroup_pidlist_seq_operations = {
3828 .start = cgroup_pidlist_start,
3829 .stop = cgroup_pidlist_stop,
3830 .next = cgroup_pidlist_next,
3831 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003832};
3833
Ben Blum102a7752009-09-23 15:56:26 -07003834static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003835{
Ben Blum72a8cb32009-09-23 15:56:27 -07003836 /*
3837 * the case where we're the last user of this particular pidlist will
3838 * have us remove it from the cgroup's list, which entails taking the
3839 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3840 * pidlist_mutex, we have to take pidlist_mutex first.
3841 */
3842 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003843 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003844 BUG_ON(!l->use_count);
3845 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003846 /* we're the last user if refcount is 0; remove and free */
3847 list_del(&l->links);
3848 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003849 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003850 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003851 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003852 kfree(l);
3853 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003854 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003855 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003856 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003857}
3858
Ben Blum102a7752009-09-23 15:56:26 -07003859static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003860{
Ben Blum102a7752009-09-23 15:56:26 -07003861 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003862 if (!(file->f_mode & FMODE_READ))
3863 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003864 /*
3865 * the seq_file will only be initialized if the file was opened for
3866 * reading; hence we check if it's not null only in that case.
3867 */
3868 l = ((struct seq_file *)file->private_data)->private;
3869 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 return seq_release(inode, file);
3871}
3872
Ben Blum102a7752009-09-23 15:56:26 -07003873static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003874 .read = seq_read,
3875 .llseek = seq_lseek,
3876 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003877 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003878};
3879
3880/*
Ben Blum102a7752009-09-23 15:56:26 -07003881 * The following functions handle opens on a file that displays a pidlist
3882 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3883 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003884 */
Ben Blum102a7752009-09-23 15:56:26 -07003885/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003886static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003887{
3888 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003889 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003890 int retval;
3891
3892 /* Nothing to do for write-only files */
3893 if (!(file->f_mode & FMODE_READ))
3894 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003895
Ben Blum102a7752009-09-23 15:56:26 -07003896 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003897 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003898 if (retval)
3899 return retval;
3900 /* configure file information */
3901 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003902
Ben Blum102a7752009-09-23 15:56:26 -07003903 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003904 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003905 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003906 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003907 }
Ben Blum102a7752009-09-23 15:56:26 -07003908 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003909 return 0;
3910}
Ben Blum102a7752009-09-23 15:56:26 -07003911static int cgroup_tasks_open(struct inode *unused, struct file *file)
3912{
Ben Blum72a8cb32009-09-23 15:56:27 -07003913 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003914}
3915static int cgroup_procs_open(struct inode *unused, struct file *file)
3916{
Ben Blum72a8cb32009-09-23 15:56:27 -07003917 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003918}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003919
Tejun Heo182446d2013-08-08 20:11:24 -04003920static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3921 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003922{
Tejun Heo182446d2013-08-08 20:11:24 -04003923 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003924}
3925
Tejun Heo182446d2013-08-08 20:11:24 -04003926static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3927 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003928{
Tejun Heo182446d2013-08-08 20:11:24 -04003929 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003930 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003931 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003932 else
Tejun Heo182446d2013-08-08 20:11:24 -04003933 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003934 return 0;
3935}
3936
Paul Menagebbcb81d2007-10-18 23:39:32 -07003937/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003938 * When dput() is called asynchronously, if umount has been done and
3939 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3940 * there's a small window that vfs will see the root dentry with non-zero
3941 * refcnt and trigger BUG().
3942 *
3943 * That's why we hold a reference before dput() and drop it right after.
3944 */
3945static void cgroup_dput(struct cgroup *cgrp)
3946{
3947 struct super_block *sb = cgrp->root->sb;
3948
3949 atomic_inc(&sb->s_active);
3950 dput(cgrp->dentry);
3951 deactivate_super(sb);
3952}
3953
3954/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 * Unregister event and free resources.
3956 *
3957 * Gets called from workqueue.
3958 */
3959static void cgroup_event_remove(struct work_struct *work)
3960{
3961 struct cgroup_event *event = container_of(work, struct cgroup_event,
3962 remove);
3963 struct cgroup *cgrp = event->cgrp;
3964
Li Zefan810cbee2013-02-18 18:56:14 +08003965 remove_wait_queue(event->wqh, &event->wait);
3966
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003967 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3968
Li Zefan810cbee2013-02-18 18:56:14 +08003969 /* Notify userspace the event is going away. */
3970 eventfd_signal(event->eventfd, 1);
3971
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003973 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003974 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003975}
3976
3977/*
3978 * Gets called on POLLHUP on eventfd when user closes it.
3979 *
3980 * Called with wqh->lock held and interrupts disabled.
3981 */
3982static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3983 int sync, void *key)
3984{
3985 struct cgroup_event *event = container_of(wait,
3986 struct cgroup_event, wait);
3987 struct cgroup *cgrp = event->cgrp;
3988 unsigned long flags = (unsigned long)key;
3989
3990 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003991 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003992 * If the event has been detached at cgroup removal, we
3993 * can simply return knowing the other side will cleanup
3994 * for us.
3995 *
3996 * We can't race against event freeing since the other
3997 * side will require wqh->lock via remove_wait_queue(),
3998 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 */
Li Zefan810cbee2013-02-18 18:56:14 +08004000 spin_lock(&cgrp->event_list_lock);
4001 if (!list_empty(&event->list)) {
4002 list_del_init(&event->list);
4003 /*
4004 * We are in atomic context, but cgroup_event_remove()
4005 * may sleep, so we have to call it in workqueue.
4006 */
4007 schedule_work(&event->remove);
4008 }
4009 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004010 }
4011
4012 return 0;
4013}
4014
4015static void cgroup_event_ptable_queue_proc(struct file *file,
4016 wait_queue_head_t *wqh, poll_table *pt)
4017{
4018 struct cgroup_event *event = container_of(pt,
4019 struct cgroup_event, pt);
4020
4021 event->wqh = wqh;
4022 add_wait_queue(wqh, &event->wait);
4023}
4024
4025/*
4026 * Parse input and register new cgroup event handler.
4027 *
4028 * Input must be in format '<event_fd> <control_fd> <args>'.
4029 * Interpretation of args is defined by control file implementation.
4030 */
Tejun Heo182446d2013-08-08 20:11:24 -04004031static int cgroup_write_event_control(struct cgroup_subsys_state *css,
4032 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033{
Tejun Heo182446d2013-08-08 20:11:24 -04004034 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004035 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004036 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004038 struct file *efile;
4039 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004040 char *endp;
4041 int ret;
4042
4043 efd = simple_strtoul(buffer, &endp, 10);
4044 if (*endp != ' ')
4045 return -EINVAL;
4046 buffer = endp + 1;
4047
4048 cfd = simple_strtoul(buffer, &endp, 10);
4049 if ((*endp != ' ') && (*endp != '\0'))
4050 return -EINVAL;
4051 buffer = endp + 1;
4052
4053 event = kzalloc(sizeof(*event), GFP_KERNEL);
4054 if (!event)
4055 return -ENOMEM;
4056 event->cgrp = cgrp;
4057 INIT_LIST_HEAD(&event->list);
4058 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4059 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4060 INIT_WORK(&event->remove, cgroup_event_remove);
4061
4062 efile = eventfd_fget(efd);
4063 if (IS_ERR(efile)) {
4064 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004065 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004066 }
4067
4068 event->eventfd = eventfd_ctx_fileget(efile);
4069 if (IS_ERR(event->eventfd)) {
4070 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004071 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004072 }
4073
4074 cfile = fget(cfd);
4075 if (!cfile) {
4076 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004077 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004078 }
4079
4080 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004081 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004082 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004083 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004084 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085
4086 event->cft = __file_cft(cfile);
4087 if (IS_ERR(event->cft)) {
4088 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004089 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004090 }
4091
Li Zefanf1690072013-02-18 14:13:35 +08004092 /*
4093 * The file to be monitored must be in the same cgroup as
4094 * cgroup.event_control is.
4095 */
4096 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4097 if (cgrp_cfile != cgrp) {
4098 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004099 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004100 }
4101
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004102 if (!event->cft->register_event || !event->cft->unregister_event) {
4103 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004104 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004105 }
4106
4107 ret = event->cft->register_event(cgrp, event->cft,
4108 event->eventfd, buffer);
4109 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004110 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004111
Li Zefan7ef70e42013-04-26 11:58:03 -07004112 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004113
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004114 /*
4115 * Events should be removed after rmdir of cgroup directory, but before
4116 * destroying subsystem state objects. Let's take reference to cgroup
4117 * directory dentry to do that.
4118 */
4119 dget(cgrp->dentry);
4120
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004121 spin_lock(&cgrp->event_list_lock);
4122 list_add(&event->list, &cgrp->event_list);
4123 spin_unlock(&cgrp->event_list_lock);
4124
4125 fput(cfile);
4126 fput(efile);
4127
4128 return 0;
4129
Li Zefan876ede82013-08-01 09:51:47 +08004130out_put_cfile:
4131 fput(cfile);
4132out_put_eventfd:
4133 eventfd_ctx_put(event->eventfd);
4134out_put_efile:
4135 fput(efile);
4136out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004137 kfree(event);
4138
4139 return ret;
4140}
4141
Tejun Heo182446d2013-08-08 20:11:24 -04004142static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4143 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004144{
Tejun Heo182446d2013-08-08 20:11:24 -04004145 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004146}
4147
Tejun Heo182446d2013-08-08 20:11:24 -04004148static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4149 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004150{
4151 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004152 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004153 else
Tejun Heo182446d2013-08-08 20:11:24 -04004154 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004155 return 0;
4156}
4157
Tejun Heod5c56ce2013-06-03 19:14:34 -07004158static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004159 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004160 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004161 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004162 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004163 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004164 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004165 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004166 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004167 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004168 .write_string = cgroup_write_event_control,
4169 .mode = S_IWUGO,
4170 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004171 {
4172 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004173 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004174 .read_u64 = cgroup_clone_children_read,
4175 .write_u64 = cgroup_clone_children_write,
4176 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004177 {
Tejun Heo873fe092013-04-14 20:15:26 -07004178 .name = "cgroup.sane_behavior",
4179 .flags = CFTYPE_ONLY_ON_ROOT,
4180 .read_seq_string = cgroup_sane_behavior_show,
4181 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004182
4183 /*
4184 * Historical crazy stuff. These don't have "cgroup." prefix and
4185 * don't exist if sane_behavior. If you're depending on these, be
4186 * prepared to be burned.
4187 */
4188 {
4189 .name = "tasks",
4190 .flags = CFTYPE_INSANE, /* use "procs" instead */
4191 .open = cgroup_tasks_open,
4192 .write_u64 = cgroup_tasks_write,
4193 .release = cgroup_pidlist_release,
4194 .mode = S_IRUGO | S_IWUSR,
4195 },
4196 {
4197 .name = "notify_on_release",
4198 .flags = CFTYPE_INSANE,
4199 .read_u64 = cgroup_read_notify_on_release,
4200 .write_u64 = cgroup_write_notify_on_release,
4201 },
Tejun Heo873fe092013-04-14 20:15:26 -07004202 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004203 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004204 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004205 .read_seq_string = cgroup_release_agent_show,
4206 .write_string = cgroup_release_agent_write,
4207 .max_write_len = PATH_MAX,
4208 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004209 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004210};
4211
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004212/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004213 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004214 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004215 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004216 *
4217 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004218 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004219static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004220{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004221 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004222 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004223
Tejun Heo8e3f6542012-04-01 12:09:55 -07004224 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004225 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004226 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004227
4228 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004229 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004230
Tejun Heobee55092013-06-28 16:24:11 -07004231 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004232 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004233 if (ret < 0)
4234 goto err;
4235 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004237
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004238 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004239 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004240 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004241 struct css_id *id = rcu_dereference_protected(css->id, true);
4242
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004243 /*
4244 * Update id->css pointer and make this css visible from
4245 * CSS ID functions. This pointer will be dereferened
4246 * from RCU-read-side without locks.
4247 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004248 if (id)
4249 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004250 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004251
4252 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004253err:
4254 cgroup_clear_dir(cgrp, subsys_mask);
4255 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256}
4257
Tejun Heo48ddbe12012-04-01 12:09:56 -07004258static void css_dput_fn(struct work_struct *work)
4259{
4260 struct cgroup_subsys_state *css =
4261 container_of(work, struct cgroup_subsys_state, dput_work);
4262
Li Zefan1c8158e2013-06-18 18:41:10 +08004263 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004264}
4265
Tejun Heod3daf282013-06-13 19:39:16 -07004266static void css_release(struct percpu_ref *ref)
4267{
4268 struct cgroup_subsys_state *css =
4269 container_of(ref, struct cgroup_subsys_state, refcnt);
4270
4271 schedule_work(&css->dput_work);
4272}
4273
Paul Menageddbcc7e2007-10-18 23:39:30 -07004274static void init_cgroup_css(struct cgroup_subsys_state *css,
4275 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004276 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004277{
Paul Menagebd89aab2007-10-18 23:40:44 -07004278 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004279 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004281 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004282 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004283 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004284 BUG_ON(cgrp->subsys[ss->subsys_id]);
4285 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004286
4287 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004288 * css holds an extra ref to @cgrp->dentry which is put on the last
4289 * css_put(). dput() requires process context, which css_put() may
4290 * be called without. @css->dput_work will be used to invoke
4291 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004292 */
4293 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004294}
4295
Li Zefan2a4ac632013-07-31 16:16:40 +08004296/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004297static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004298{
Tejun Heoeb954192013-08-08 20:11:23 -04004299 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heob1929db2012-11-19 08:13:38 -08004300 int ret = 0;
4301
Tejun Heoa31f2d32012-11-19 08:13:37 -08004302 lockdep_assert_held(&cgroup_mutex);
4303
Tejun Heo92fb9742012-11-19 08:13:38 -08004304 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004305 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004306 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004307 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004308 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004309}
4310
Li Zefan2a4ac632013-07-31 16:16:40 +08004311/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004312static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004313{
4314 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4315
4316 lockdep_assert_held(&cgroup_mutex);
4317
4318 if (!(css->flags & CSS_ONLINE))
4319 return;
4320
Li Zefand7eeac12013-03-12 15:35:59 -07004321 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004322 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004323
Tejun Heoeb954192013-08-08 20:11:23 -04004324 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004325}
4326
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327/*
Li Zefana043e3b2008-02-23 15:24:09 -08004328 * cgroup_create - create a cgroup
4329 * @parent: cgroup that will be parent of the new cgroup
4330 * @dentry: dentry of the new cgroup
4331 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332 *
Li Zefana043e3b2008-02-23 15:24:09 -08004333 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004334 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004336 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337{
Paul Menagebd89aab2007-10-18 23:40:44 -07004338 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004339 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004340 struct cgroupfs_root *root = parent->root;
4341 int err = 0;
4342 struct cgroup_subsys *ss;
4343 struct super_block *sb = root->sb;
4344
Tejun Heo0a950f62012-11-19 09:02:12 -08004345 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004346 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4347 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 return -ENOMEM;
4349
Li Zefan65dff752013-03-01 15:01:56 +08004350 name = cgroup_alloc_name(dentry);
4351 if (!name)
4352 goto err_free_cgrp;
4353 rcu_assign_pointer(cgrp->name, name);
4354
Li Zefan4e96ee82013-07-31 09:50:50 +08004355 /*
4356 * Temporarily set the pointer to NULL, so idr_find() won't return
4357 * a half-baked cgroup.
4358 */
4359 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004360 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004361 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004362
Tejun Heo976c06b2012-11-05 09:16:59 -08004363 /*
4364 * Only live parents can have children. Note that the liveliness
4365 * check isn't strictly necessary because cgroup_mkdir() and
4366 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4367 * anyway so that locking is contained inside cgroup proper and we
4368 * don't get nasty surprises if we ever grow another caller.
4369 */
4370 if (!cgroup_lock_live_group(parent)) {
4371 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004372 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004373 }
4374
Paul Menageddbcc7e2007-10-18 23:39:30 -07004375 /* Grab a reference on the superblock so the hierarchy doesn't
4376 * get deleted on unmount if there are child cgroups. This
4377 * can be done outside cgroup_mutex, since the sb can't
4378 * disappear while someone has an open control file on the
4379 * fs */
4380 atomic_inc(&sb->s_active);
4381
Paul Menagecc31edc2008-10-18 20:28:04 -07004382 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004383
Li Zefanfe1c06c2013-01-24 14:30:22 +08004384 dentry->d_fsdata = cgrp;
4385 cgrp->dentry = dentry;
4386
Paul Menagebd89aab2007-10-18 23:40:44 -07004387 cgrp->parent = parent;
4388 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389
Li Zefanb6abdb02008-03-04 14:28:19 -08004390 if (notify_on_release(parent))
4391 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4392
Tejun Heo2260e7f2012-11-19 08:13:38 -08004393 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4394 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004395
Tejun Heo5549c492013-06-24 15:21:48 -07004396 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004397 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004398
Tejun Heoeb954192013-08-08 20:11:23 -04004399 css = ss->css_alloc(parent->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400 if (IS_ERR(css)) {
4401 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004402 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004403 }
Tejun Heod3daf282013-06-13 19:39:16 -07004404
4405 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004406 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004407 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004408 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004409 }
Tejun Heod3daf282013-06-13 19:39:16 -07004410
Paul Menagebd89aab2007-10-18 23:40:44 -07004411 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004412
Li Zefan4528fd02010-02-02 13:44:10 -08004413 if (ss->use_id) {
4414 err = alloc_css_id(ss, parent, cgrp);
4415 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004416 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004417 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004418 }
4419
Tejun Heo4e139af2012-11-19 08:13:36 -08004420 /*
4421 * Create directory. cgroup_create_file() returns with the new
4422 * directory locked on success so that it can be populated without
4423 * dropping cgroup_mutex.
4424 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004425 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004426 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004427 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004428 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004429
Tejun Heo00356bd2013-06-18 11:14:22 -07004430 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004431
Tejun Heo4e139af2012-11-19 08:13:36 -08004432 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004433 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4434 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004435
Tejun Heob1929db2012-11-19 08:13:38 -08004436 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004437 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004438 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004439
Li Zefan415cf072013-04-08 14:35:02 +08004440 /* hold a ref to the parent's dentry */
4441 dget(parent->dentry);
4442
Tejun Heob1929db2012-11-19 08:13:38 -08004443 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004444 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004445 err = online_css(ss, cgrp);
4446 if (err)
4447 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004448
4449 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4450 parent->parent) {
4451 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",
4452 current->comm, current->pid, ss->name);
4453 if (!strcmp(ss->name, "memory"))
4454 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4455 ss->warned_broken_hierarchy = true;
4456 }
Tejun Heoa8638032012-11-09 09:12:29 -08004457 }
4458
Li Zefan4e96ee82013-07-31 09:50:50 +08004459 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4460
Tejun Heo2bb566c2013-08-08 20:11:23 -04004461 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004462 if (err)
4463 goto err_destroy;
4464
4465 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004466 if (err)
4467 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004468
4469 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004470 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004471
4472 return 0;
4473
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004474err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004475 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004476 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4477
4478 if (css) {
4479 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004480 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004481 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004483 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004484 /* Release the reference count that we took on the superblock */
4485 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004486err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004487 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004488err_free_name:
4489 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004490err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004491 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004492 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004493
4494err_destroy:
4495 cgroup_destroy_locked(cgrp);
4496 mutex_unlock(&cgroup_mutex);
4497 mutex_unlock(&dentry->d_inode->i_mutex);
4498 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004499}
4500
Al Viro18bb1db2011-07-26 01:41:39 -04004501static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004502{
4503 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4504
4505 /* the vfs holds inode->i_mutex already */
4506 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4507}
4508
Tejun Heod3daf282013-06-13 19:39:16 -07004509static void cgroup_css_killed(struct cgroup *cgrp)
4510{
4511 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4512 return;
4513
4514 /* percpu ref's of all css's are killed, kick off the next step */
4515 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4516 schedule_work(&cgrp->destroy_work);
4517}
4518
4519static void css_ref_killed_fn(struct percpu_ref *ref)
4520{
4521 struct cgroup_subsys_state *css =
4522 container_of(ref, struct cgroup_subsys_state, refcnt);
4523
4524 cgroup_css_killed(css->cgroup);
4525}
4526
4527/**
4528 * cgroup_destroy_locked - the first stage of cgroup destruction
4529 * @cgrp: cgroup to be destroyed
4530 *
4531 * css's make use of percpu refcnts whose killing latency shouldn't be
4532 * exposed to userland and are RCU protected. Also, cgroup core needs to
4533 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4534 * invoked. To satisfy all the requirements, destruction is implemented in
4535 * the following two steps.
4536 *
4537 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4538 * userland visible parts and start killing the percpu refcnts of
4539 * css's. Set up so that the next stage will be kicked off once all
4540 * the percpu refcnts are confirmed to be killed.
4541 *
4542 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4543 * rest of destruction. Once all cgroup references are gone, the
4544 * cgroup is RCU-freed.
4545 *
4546 * This function implements s1. After this step, @cgrp is gone as far as
4547 * the userland is concerned and a new cgroup with the same name may be
4548 * created. As cgroup doesn't care about the names internally, this
4549 * doesn't cause any problem.
4550 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004551static int cgroup_destroy_locked(struct cgroup *cgrp)
4552 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004553{
Tejun Heo42809dd2012-11-19 08:13:37 -08004554 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004555 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004556 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004557 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004558
Tejun Heo42809dd2012-11-19 08:13:37 -08004559 lockdep_assert_held(&d->d_inode->i_mutex);
4560 lockdep_assert_held(&cgroup_mutex);
4561
Tejun Heoddd69142013-06-12 21:04:54 -07004562 /*
Tejun Heo6f3d8282013-06-12 21:04:55 -07004563 * css_set_lock synchronizes access to ->cset_links and prevents
4564 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004565 */
4566 read_lock(&css_set_lock);
Tejun Heo6f3d8282013-06-12 21:04:55 -07004567 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004568 read_unlock(&css_set_lock);
4569 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004570 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004571
Tejun Heo1a90dd52012-11-05 09:16:59 -08004572 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004573 * Block new css_tryget() by killing css refcnts. cgroup core
4574 * guarantees that, by the time ->css_offline() is invoked, no new
4575 * css reference will be given out via css_tryget(). We can't
4576 * simply call percpu_ref_kill() and proceed to offlining css's
4577 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4578 * as killed on all CPUs on return.
4579 *
4580 * Use percpu_ref_kill_and_confirm() to get notifications as each
4581 * css is confirmed to be seen as killed on all CPUs. The
4582 * notification callback keeps track of the number of css's to be
4583 * killed and schedules cgroup_offline_fn() to perform the rest of
4584 * destruction once the percpu refs of all css's are confirmed to
4585 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004586 */
Tejun Heod3daf282013-06-13 19:39:16 -07004587 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004588 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004589 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4590
Tejun Heod3daf282013-06-13 19:39:16 -07004591 /*
4592 * Killing would put the base ref, but we need to keep it
4593 * alive until after ->css_offline.
4594 */
4595 percpu_ref_get(&css->refcnt);
4596
4597 atomic_inc(&cgrp->css_kill_cnt);
4598 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004599 }
Tejun Heod3daf282013-06-13 19:39:16 -07004600 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004601
4602 /*
4603 * Mark @cgrp dead. This prevents further task migration and child
4604 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004605 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004606 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004607 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004608 */
Tejun Heo54766d42013-06-12 21:04:53 -07004609 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004610
Tejun Heo455050d2013-06-13 19:27:41 -07004611 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4612 raw_spin_lock(&release_list_lock);
4613 if (!list_empty(&cgrp->release_list))
4614 list_del_init(&cgrp->release_list);
4615 raw_spin_unlock(&release_list_lock);
4616
4617 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004618 * Clear and remove @cgrp directory. The removal puts the base ref
4619 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004620 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004621 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004622 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004623 dget(d);
4624 cgroup_d_remove_dir(d);
4625
4626 /*
4627 * Unregister events and notify userspace.
4628 * Notify userspace about cgroup removing only after rmdir of cgroup
4629 * directory to avoid race between userspace and kernelspace.
4630 */
4631 spin_lock(&cgrp->event_list_lock);
4632 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4633 list_del_init(&event->list);
4634 schedule_work(&event->remove);
4635 }
4636 spin_unlock(&cgrp->event_list_lock);
4637
Tejun Heoea15f8c2013-06-13 19:27:42 -07004638 return 0;
4639};
4640
Tejun Heod3daf282013-06-13 19:39:16 -07004641/**
4642 * cgroup_offline_fn - the second step of cgroup destruction
4643 * @work: cgroup->destroy_free_work
4644 *
4645 * This function is invoked from a work item for a cgroup which is being
4646 * destroyed after the percpu refcnts of all css's are guaranteed to be
4647 * seen as killed on all CPUs, and performs the rest of destruction. This
4648 * is the second step of destruction described in the comment above
4649 * cgroup_destroy_locked().
4650 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004651static void cgroup_offline_fn(struct work_struct *work)
4652{
4653 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4654 struct cgroup *parent = cgrp->parent;
4655 struct dentry *d = cgrp->dentry;
4656 struct cgroup_subsys *ss;
4657
4658 mutex_lock(&cgroup_mutex);
4659
Tejun Heod3daf282013-06-13 19:39:16 -07004660 /*
4661 * css_tryget() is guaranteed to fail now. Tell subsystems to
4662 * initate destruction.
4663 */
Tejun Heo5549c492013-06-24 15:21:48 -07004664 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004665 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004666
4667 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004668 * Put the css refs from cgroup_destroy_locked(). Each css holds
4669 * an extra reference to the cgroup's dentry and cgroup removal
4670 * proceeds regardless of css refs. On the last put of each css,
4671 * whenever that may be, the extra dentry ref is put so that dentry
4672 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004673 */
Tejun Heo5549c492013-06-24 15:21:48 -07004674 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004675 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676
Paul Menage999cd8a2009-01-07 18:08:36 -08004677 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004678 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004679
Li Zefan4e96ee82013-07-31 09:50:50 +08004680 /*
4681 * We should remove the cgroup object from idr before its grace
4682 * period starts, so we won't be looking up a cgroup while the
4683 * cgroup is being freed.
4684 */
4685 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4686 cgrp->id = -1;
4687
Paul Menageddbcc7e2007-10-18 23:39:30 -07004688 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689
Paul Menagebd89aab2007-10-18 23:40:44 -07004690 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004691 check_for_release(parent);
4692
Tejun Heoea15f8c2013-06-13 19:27:42 -07004693 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694}
4695
Tejun Heo42809dd2012-11-19 08:13:37 -08004696static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4697{
4698 int ret;
4699
4700 mutex_lock(&cgroup_mutex);
4701 ret = cgroup_destroy_locked(dentry->d_fsdata);
4702 mutex_unlock(&cgroup_mutex);
4703
4704 return ret;
4705}
4706
Tejun Heo8e3f6542012-04-01 12:09:55 -07004707static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4708{
4709 INIT_LIST_HEAD(&ss->cftsets);
4710
4711 /*
4712 * base_cftset is embedded in subsys itself, no need to worry about
4713 * deregistration.
4714 */
4715 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004716 struct cftype *cft;
4717
4718 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4719 cft->ss = ss;
4720
Tejun Heo8e3f6542012-04-01 12:09:55 -07004721 ss->base_cftset.cfts = ss->base_cftypes;
4722 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4723 }
4724}
4725
Li Zefan06a11922008-04-29 01:00:07 -07004726static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004727{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004728 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004729
4730 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731
Tejun Heo648bb562012-11-19 08:13:36 -08004732 mutex_lock(&cgroup_mutex);
4733
Tejun Heo8e3f6542012-04-01 12:09:55 -07004734 /* init base cftset */
4735 cgroup_init_cftsets(ss);
4736
Paul Menageddbcc7e2007-10-18 23:39:30 -07004737 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004738 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4739 ss->root = &cgroup_dummy_root;
Tejun Heoeb954192013-08-08 20:11:23 -04004740 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004741 /* We don't handle early failures gracefully */
4742 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004743 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004744
Li Zefane8d55fd2008-04-29 01:00:13 -07004745 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004746 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004747 * newly registered, all tasks and hence the
4748 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004749 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004750
4751 need_forkexit_callback |= ss->fork || ss->exit;
4752
Li Zefane8d55fd2008-04-29 01:00:13 -07004753 /* At system boot, before all subsystems have been
4754 * registered, no tasks have been forked, so we don't
4755 * need to invoke fork callbacks here. */
4756 BUG_ON(!list_empty(&init_task.tasks));
4757
Tejun Heo9871bf92013-06-24 15:21:47 -07004758 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004759
Tejun Heo648bb562012-11-19 08:13:36 -08004760 mutex_unlock(&cgroup_mutex);
4761
Ben Blume6a11052010-03-10 15:22:09 -08004762 /* this function shouldn't be used with modular subsystems, since they
4763 * need to register a subsys_id, among other things */
4764 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004765}
4766
4767/**
Ben Blume6a11052010-03-10 15:22:09 -08004768 * cgroup_load_subsys: load and register a modular subsystem at runtime
4769 * @ss: the subsystem to load
4770 *
4771 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004772 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004773 * up for use. If the subsystem is built-in anyway, work is delegated to the
4774 * simpler cgroup_init_subsys.
4775 */
4776int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4777{
Ben Blume6a11052010-03-10 15:22:09 -08004778 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004779 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004780 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004781 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004782 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004783
4784 /* check name and function validity */
4785 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004786 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004787 return -EINVAL;
4788
4789 /*
4790 * we don't support callbacks in modular subsystems. this check is
4791 * before the ss->module check for consistency; a subsystem that could
4792 * be a module should still have no callbacks even if the user isn't
4793 * compiling it as one.
4794 */
4795 if (ss->fork || ss->exit)
4796 return -EINVAL;
4797
4798 /*
4799 * an optionally modular subsystem is built-in: we want to do nothing,
4800 * since cgroup_init_subsys will have already taken care of it.
4801 */
4802 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004803 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004804 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004805 return 0;
4806 }
4807
Tejun Heo8e3f6542012-04-01 12:09:55 -07004808 /* init base cftset */
4809 cgroup_init_cftsets(ss);
4810
Ben Blume6a11052010-03-10 15:22:09 -08004811 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004812 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004813
4814 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004815 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004816 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004817 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004818 */
Tejun Heoeb954192013-08-08 20:11:23 -04004819 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Ben Blume6a11052010-03-10 15:22:09 -08004820 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004821 /* failure case - need to deassign the cgroup_subsys[] slot. */
4822 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004823 mutex_unlock(&cgroup_mutex);
4824 return PTR_ERR(css);
4825 }
4826
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4828 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004829
4830 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004831 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004832 /* init_idr must be after init_cgroup_css because it sets css->id. */
4833 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004834 ret = cgroup_init_idr(ss, css);
4835 if (ret)
4836 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004837 }
4838
4839 /*
4840 * Now we need to entangle the css into the existing css_sets. unlike
4841 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4842 * will need a new pointer to it; done by iterating the css_set_table.
4843 * furthermore, modifying the existing css_sets will corrupt the hash
4844 * table state, so each changed css_set will need its hash recomputed.
4845 * this is all done under the css_set_lock.
4846 */
4847 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004848 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004849 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004850 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004851 continue;
4852 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004853 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004854 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004855 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004856 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004857 key = css_set_hash(cset->subsys);
4858 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004859 }
4860 write_unlock(&css_set_lock);
4861
Tejun Heo9871bf92013-06-24 15:21:47 -07004862 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004863 if (ret)
4864 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004865
Ben Blume6a11052010-03-10 15:22:09 -08004866 /* success! */
4867 mutex_unlock(&cgroup_mutex);
4868 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004869
4870err_unload:
4871 mutex_unlock(&cgroup_mutex);
4872 /* @ss can't be mounted here as try_module_get() would fail */
4873 cgroup_unload_subsys(ss);
4874 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004875}
4876EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4877
4878/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004879 * cgroup_unload_subsys: unload a modular subsystem
4880 * @ss: the subsystem to unload
4881 *
4882 * This function should be called in a modular subsystem's exitcall. When this
4883 * function is invoked, the refcount on the subsystem's module will be 0, so
4884 * the subsystem will not be attached to any hierarchy.
4885 */
4886void cgroup_unload_subsys(struct cgroup_subsys *ss)
4887{
Tejun Heo69d02062013-06-12 21:04:50 -07004888 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004889
4890 BUG_ON(ss->module == NULL);
4891
4892 /*
4893 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004894 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004895 * doesn't start being used while we're killing it off.
4896 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004897 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004898
4899 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004900
Tejun Heo9871bf92013-06-24 15:21:47 -07004901 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004902
Tejun Heoc897ff62013-02-27 17:03:49 -08004903 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004904 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004905
Ben Blumcf5d5942010-03-10 15:22:09 -08004906 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004907 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004908
Tejun Heo9871bf92013-06-24 15:21:47 -07004909 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004910 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004911
4912 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004913 * disentangle the css from all css_sets attached to the dummy
4914 * top. as in loading, we need to pay our respects to the hashtable
4915 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004916 */
4917 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004918 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004919 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004920 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004921
Tejun Heo5abb8852013-06-12 21:04:49 -07004922 hash_del(&cset->hlist);
4923 cset->subsys[ss->subsys_id] = NULL;
4924 key = css_set_hash(cset->subsys);
4925 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004926 }
4927 write_unlock(&css_set_lock);
4928
4929 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004930 * remove subsystem's css from the cgroup_dummy_top and free it -
4931 * need to free before marking as null because ss->css_free needs
4932 * the cgrp->subsys pointer to find their state. note that this
4933 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004934 */
Tejun Heoeb954192013-08-08 20:11:23 -04004935 ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
Tejun Heo9871bf92013-06-24 15:21:47 -07004936 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004937
4938 mutex_unlock(&cgroup_mutex);
4939}
4940EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4941
4942/**
Li Zefana043e3b2008-02-23 15:24:09 -08004943 * cgroup_init_early - cgroup initialization at system boot
4944 *
4945 * Initialize cgroups at system boot, and initialize any
4946 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004947 */
4948int __init cgroup_init_early(void)
4949{
Tejun Heo30159ec2013-06-25 11:53:37 -07004950 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004951 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004952
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004953 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004954 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004955 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004956 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004957 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004958 init_cgroup_root(&cgroup_dummy_root);
4959 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004960 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004961
Tejun Heo69d02062013-06-12 21:04:50 -07004962 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004963 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4964 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004965 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004966
Tejun Heo30159ec2013-06-25 11:53:37 -07004967 /* at bootup time, we don't worry about modular subsystems */
4968 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004969 BUG_ON(!ss->name);
4970 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004971 BUG_ON(!ss->css_alloc);
4972 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004973 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004974 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004975 ss->name, ss->subsys_id);
4976 BUG();
4977 }
4978
4979 if (ss->early_init)
4980 cgroup_init_subsys(ss);
4981 }
4982 return 0;
4983}
4984
4985/**
Li Zefana043e3b2008-02-23 15:24:09 -08004986 * cgroup_init - cgroup initialization
4987 *
4988 * Register cgroup filesystem and /proc file, and initialize
4989 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004990 */
4991int __init cgroup_init(void)
4992{
Tejun Heo30159ec2013-06-25 11:53:37 -07004993 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004994 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004995 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004996
4997 err = bdi_init(&cgroup_backing_dev_info);
4998 if (err)
4999 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005000
Tejun Heo30159ec2013-06-25 11:53:37 -07005001 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005002 if (!ss->early_init)
5003 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005004 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005005 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005006 }
5007
Tejun Heofa3ca072013-04-14 11:36:56 -07005008 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005009 mutex_lock(&cgroup_mutex);
5010 mutex_lock(&cgroup_root_mutex);
5011
Tejun Heo82fe9b02013-06-25 11:53:37 -07005012 /* Add init_css_set to the hash table */
5013 key = css_set_hash(init_css_set.subsys);
5014 hash_add(css_set_table, &init_css_set.hlist, key);
5015
Tejun Heofc76df72013-06-25 11:53:37 -07005016 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005017
Li Zefan4e96ee82013-07-31 09:50:50 +08005018 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5019 0, 1, GFP_KERNEL);
5020 BUG_ON(err < 0);
5021
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005022 mutex_unlock(&cgroup_root_mutex);
5023 mutex_unlock(&cgroup_mutex);
5024
Greg KH676db4a2010-08-05 13:53:35 -07005025 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5026 if (!cgroup_kobj) {
5027 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005028 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005029 }
5030
5031 err = register_filesystem(&cgroup_fs_type);
5032 if (err < 0) {
5033 kobject_put(cgroup_kobj);
5034 goto out;
5035 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005036
Li Zefan46ae2202008-04-29 01:00:08 -07005037 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005038
Paul Menageddbcc7e2007-10-18 23:39:30 -07005039out:
Paul Menagea4243162007-10-18 23:39:35 -07005040 if (err)
5041 bdi_destroy(&cgroup_backing_dev_info);
5042
Paul Menageddbcc7e2007-10-18 23:39:30 -07005043 return err;
5044}
Paul Menageb4f48b62007-10-18 23:39:33 -07005045
Paul Menagea4243162007-10-18 23:39:35 -07005046/*
5047 * proc_cgroup_show()
5048 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5049 * - Used for /proc/<pid>/cgroup.
5050 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5051 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005052 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005053 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5054 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5055 * cgroup to top_cgroup.
5056 */
5057
5058/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005059int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005060{
5061 struct pid *pid;
5062 struct task_struct *tsk;
5063 char *buf;
5064 int retval;
5065 struct cgroupfs_root *root;
5066
5067 retval = -ENOMEM;
5068 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5069 if (!buf)
5070 goto out;
5071
5072 retval = -ESRCH;
5073 pid = m->private;
5074 tsk = get_pid_task(pid, PIDTYPE_PID);
5075 if (!tsk)
5076 goto out_free;
5077
5078 retval = 0;
5079
5080 mutex_lock(&cgroup_mutex);
5081
Li Zefane5f6a862009-01-07 18:07:41 -08005082 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005083 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005084 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005085 int count = 0;
5086
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005087 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005088 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005089 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005090 if (strlen(root->name))
5091 seq_printf(m, "%sname=%s", count ? "," : "",
5092 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005093 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005094 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005095 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005096 if (retval < 0)
5097 goto out_unlock;
5098 seq_puts(m, buf);
5099 seq_putc(m, '\n');
5100 }
5101
5102out_unlock:
5103 mutex_unlock(&cgroup_mutex);
5104 put_task_struct(tsk);
5105out_free:
5106 kfree(buf);
5107out:
5108 return retval;
5109}
5110
Paul Menagea4243162007-10-18 23:39:35 -07005111/* Display information about each subsystem and each hierarchy */
5112static int proc_cgroupstats_show(struct seq_file *m, void *v)
5113{
Tejun Heo30159ec2013-06-25 11:53:37 -07005114 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005115 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005116
Paul Menage8bab8dd2008-04-04 14:29:57 -07005117 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005118 /*
5119 * ideally we don't want subsystems moving around while we do this.
5120 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5121 * subsys/hierarchy state.
5122 */
Paul Menagea4243162007-10-18 23:39:35 -07005123 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005124
5125 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005126 seq_printf(m, "%s\t%d\t%d\t%d\n",
5127 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005128 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005129
Paul Menagea4243162007-10-18 23:39:35 -07005130 mutex_unlock(&cgroup_mutex);
5131 return 0;
5132}
5133
5134static int cgroupstats_open(struct inode *inode, struct file *file)
5135{
Al Viro9dce07f2008-03-29 03:07:28 +00005136 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005137}
5138
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005139static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005140 .open = cgroupstats_open,
5141 .read = seq_read,
5142 .llseek = seq_lseek,
5143 .release = single_release,
5144};
5145
Paul Menageb4f48b62007-10-18 23:39:33 -07005146/**
5147 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005148 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005149 *
5150 * Description: A task inherits its parent's cgroup at fork().
5151 *
5152 * A pointer to the shared css_set was automatically copied in
5153 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005154 * it was not made under the protection of RCU or cgroup_mutex, so
5155 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5156 * have already changed current->cgroups, allowing the previously
5157 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005158 *
5159 * At the point that cgroup_fork() is called, 'current' is the parent
5160 * task, and the passed argument 'child' points to the child task.
5161 */
5162void cgroup_fork(struct task_struct *child)
5163{
Tejun Heo9bb71302012-10-18 17:52:07 -07005164 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005165 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005166 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005167 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005168 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005169}
5170
5171/**
Li Zefana043e3b2008-02-23 15:24:09 -08005172 * cgroup_post_fork - called on a new task after adding it to the task list
5173 * @child: the task in question
5174 *
Tejun Heo5edee612012-10-16 15:03:14 -07005175 * Adds the task to the list running through its css_set if necessary and
5176 * call the subsystem fork() callbacks. Has to be after the task is
5177 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005178 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005179 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005180 */
Paul Menage817929e2007-10-18 23:39:36 -07005181void cgroup_post_fork(struct task_struct *child)
5182{
Tejun Heo30159ec2013-06-25 11:53:37 -07005183 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005184 int i;
5185
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005186 /*
5187 * use_task_css_set_links is set to 1 before we walk the tasklist
5188 * under the tasklist_lock and we read it here after we added the child
5189 * to the tasklist under the tasklist_lock as well. If the child wasn't
5190 * yet in the tasklist when we walked through it from
5191 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5192 * should be visible now due to the paired locking and barriers implied
5193 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5194 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5195 * lock on fork.
5196 */
Paul Menage817929e2007-10-18 23:39:36 -07005197 if (use_task_css_set_links) {
5198 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005199 task_lock(child);
5200 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005201 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005202 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005203 write_unlock(&css_set_lock);
5204 }
Tejun Heo5edee612012-10-16 15:03:14 -07005205
5206 /*
5207 * Call ss->fork(). This must happen after @child is linked on
5208 * css_set; otherwise, @child might change state between ->fork()
5209 * and addition to css_set.
5210 */
5211 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005212 /*
5213 * fork/exit callbacks are supported only for builtin
5214 * subsystems, and the builtin section of the subsys
5215 * array is immutable, so we don't need to lock the
5216 * subsys array here. On the other hand, modular section
5217 * of the array can be freed at module unload, so we
5218 * can't touch that.
5219 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005220 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005221 if (ss->fork)
5222 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005223 }
Paul Menage817929e2007-10-18 23:39:36 -07005224}
Tejun Heo5edee612012-10-16 15:03:14 -07005225
Paul Menage817929e2007-10-18 23:39:36 -07005226/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005227 * cgroup_exit - detach cgroup from exiting task
5228 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005229 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005230 *
5231 * Description: Detach cgroup from @tsk and release it.
5232 *
5233 * Note that cgroups marked notify_on_release force every task in
5234 * them to take the global cgroup_mutex mutex when exiting.
5235 * This could impact scaling on very large systems. Be reluctant to
5236 * use notify_on_release cgroups where very high task exit scaling
5237 * is required on large systems.
5238 *
5239 * the_top_cgroup_hack:
5240 *
5241 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5242 *
5243 * We call cgroup_exit() while the task is still competent to
5244 * handle notify_on_release(), then leave the task attached to the
5245 * root cgroup in each hierarchy for the remainder of its exit.
5246 *
5247 * To do this properly, we would increment the reference count on
5248 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5249 * code we would add a second cgroup function call, to drop that
5250 * reference. This would just create an unnecessary hot spot on
5251 * the top_cgroup reference count, to no avail.
5252 *
5253 * Normally, holding a reference to a cgroup without bumping its
5254 * count is unsafe. The cgroup could go away, or someone could
5255 * attach us to a different cgroup, decrementing the count on
5256 * the first cgroup that we never incremented. But in this case,
5257 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005258 * which wards off any cgroup_attach_task() attempts, or task is a failed
5259 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005260 */
5261void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5262{
Tejun Heo30159ec2013-06-25 11:53:37 -07005263 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005264 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005265 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005266
5267 /*
5268 * Unlink from the css_set task list if necessary.
5269 * Optimistically check cg_list before taking
5270 * css_set_lock
5271 */
5272 if (!list_empty(&tsk->cg_list)) {
5273 write_lock(&css_set_lock);
5274 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005275 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005276 write_unlock(&css_set_lock);
5277 }
5278
Paul Menageb4f48b62007-10-18 23:39:33 -07005279 /* Reassign the task to the init_css_set. */
5280 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005281 cset = task_css_set(tsk);
5282 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005283
5284 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005285 /*
5286 * fork/exit callbacks are supported only for builtin
5287 * subsystems, see cgroup_post_fork() for details.
5288 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005289 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005290 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005291 struct cgroup_subsys_state *old_css = cset->subsys[i];
5292 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005293
Tejun Heoeb954192013-08-08 20:11:23 -04005294 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005295 }
5296 }
5297 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005298 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005299
Tejun Heo5abb8852013-06-12 21:04:49 -07005300 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005301}
Paul Menage697f4162007-10-18 23:39:34 -07005302
Paul Menagebd89aab2007-10-18 23:40:44 -07005303static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005304{
Li Zefanf50daa72013-03-01 15:06:07 +08005305 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d8282013-06-12 21:04:55 -07005306 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005307 /*
5308 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005309 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005310 * it now
5311 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005312 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005313
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005314 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005315 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005316 list_empty(&cgrp->release_list)) {
5317 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005318 need_schedule_work = 1;
5319 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005320 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005321 if (need_schedule_work)
5322 schedule_work(&release_agent_work);
5323 }
5324}
5325
Paul Menage81a6a5c2007-10-18 23:39:38 -07005326/*
5327 * Notify userspace when a cgroup is released, by running the
5328 * configured release agent with the name of the cgroup (path
5329 * relative to the root of cgroup file system) as the argument.
5330 *
5331 * Most likely, this user command will try to rmdir this cgroup.
5332 *
5333 * This races with the possibility that some other task will be
5334 * attached to this cgroup before it is removed, or that some other
5335 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5336 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5337 * unused, and this cgroup will be reprieved from its death sentence,
5338 * to continue to serve a useful existence. Next time it's released,
5339 * we will get notified again, if it still has 'notify_on_release' set.
5340 *
5341 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5342 * means only wait until the task is successfully execve()'d. The
5343 * separate release agent task is forked by call_usermodehelper(),
5344 * then control in this thread returns here, without waiting for the
5345 * release agent task. We don't bother to wait because the caller of
5346 * this routine has no use for the exit status of the release agent
5347 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005348 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005349static void cgroup_release_agent(struct work_struct *work)
5350{
5351 BUG_ON(work != &release_agent_work);
5352 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005353 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005354 while (!list_empty(&release_list)) {
5355 char *argv[3], *envp[3];
5356 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005357 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005358 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005359 struct cgroup,
5360 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005361 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005362 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005363 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005364 if (!pathbuf)
5365 goto continue_free;
5366 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5367 goto continue_free;
5368 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5369 if (!agentbuf)
5370 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005371
5372 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005373 argv[i++] = agentbuf;
5374 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005375 argv[i] = NULL;
5376
5377 i = 0;
5378 /* minimal command environment */
5379 envp[i++] = "HOME=/";
5380 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5381 envp[i] = NULL;
5382
5383 /* Drop the lock while we invoke the usermode helper,
5384 * since the exec could involve hitting disk and hence
5385 * be a slow process */
5386 mutex_unlock(&cgroup_mutex);
5387 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005388 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005389 continue_free:
5390 kfree(pathbuf);
5391 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005392 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005393 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005394 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005395 mutex_unlock(&cgroup_mutex);
5396}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005397
5398static int __init cgroup_disable(char *str)
5399{
Tejun Heo30159ec2013-06-25 11:53:37 -07005400 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005401 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005402 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005403
5404 while ((token = strsep(&str, ",")) != NULL) {
5405 if (!*token)
5406 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005407
Tejun Heo30159ec2013-06-25 11:53:37 -07005408 /*
5409 * cgroup_disable, being at boot time, can't know about
5410 * module subsystems, so we don't worry about them.
5411 */
5412 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005413 if (!strcmp(token, ss->name)) {
5414 ss->disabled = 1;
5415 printk(KERN_INFO "Disabling %s control group"
5416 " subsystem\n", ss->name);
5417 break;
5418 }
5419 }
5420 }
5421 return 1;
5422}
5423__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005424
5425/*
5426 * Functons for CSS ID.
5427 */
5428
Tejun Heo54766d42013-06-12 21:04:53 -07005429/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005430unsigned short css_id(struct cgroup_subsys_state *css)
5431{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005432 struct css_id *cssid;
5433
5434 /*
5435 * This css_id() can return correct value when somone has refcnt
5436 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5437 * it's unchanged until freed.
5438 */
Tejun Heod3daf282013-06-13 19:39:16 -07005439 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005440
5441 if (cssid)
5442 return cssid->id;
5443 return 0;
5444}
Ben Blum67523c42010-03-10 15:22:11 -08005445EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005446
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005447/**
5448 * css_is_ancestor - test "root" css is an ancestor of "child"
5449 * @child: the css to be tested.
5450 * @root: the css supporsed to be an ancestor of the child.
5451 *
5452 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005453 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005454 * But, considering usual usage, the csses should be valid objects after test.
5455 * Assuming that the caller will do some action to the child if this returns
5456 * returns true, the caller must take "child";s reference count.
5457 * If "child" is valid object and this returns true, "root" is valid, too.
5458 */
5459
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005460bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005461 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005462{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005463 struct css_id *child_id;
5464 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005465
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005466 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005467 if (!child_id)
5468 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005469 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005470 if (!root_id)
5471 return false;
5472 if (child_id->depth < root_id->depth)
5473 return false;
5474 if (child_id->stack[root_id->depth] != root_id->id)
5475 return false;
5476 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005477}
5478
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005479void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5480{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005481 struct css_id *id = rcu_dereference_protected(css->id, true);
5482
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005483 /* When this is called before css_id initialization, id can be NULL */
5484 if (!id)
5485 return;
5486
5487 BUG_ON(!ss->use_id);
5488
5489 rcu_assign_pointer(id->css, NULL);
5490 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005491 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005492 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005493 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005494 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005495}
Ben Blum67523c42010-03-10 15:22:11 -08005496EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005497
5498/*
5499 * This is called by init or create(). Then, calls to this function are
5500 * always serialized (By cgroup_mutex() at create()).
5501 */
5502
5503static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5504{
5505 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005506 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005507
5508 BUG_ON(!ss->use_id);
5509
5510 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5511 newid = kzalloc(size, GFP_KERNEL);
5512 if (!newid)
5513 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005514
5515 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005516 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005517 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005518 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005519 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005520 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005521
5522 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005523 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005524 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005525
Tejun Heod228d9e2013-02-27 17:04:54 -08005526 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005527 newid->depth = depth;
5528 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005529err_out:
5530 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005531 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005532
5533}
5534
Ben Blume6a11052010-03-10 15:22:09 -08005535static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5536 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005537{
5538 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005539
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005540 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005541 idr_init(&ss->idr);
5542
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005543 newid = get_new_cssid(ss, 0);
5544 if (IS_ERR(newid))
5545 return PTR_ERR(newid);
5546
5547 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005548 RCU_INIT_POINTER(newid->css, rootcss);
5549 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005550 return 0;
5551}
5552
5553static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5554 struct cgroup *child)
5555{
5556 int subsys_id, i, depth = 0;
5557 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005558 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005559
5560 subsys_id = ss->subsys_id;
5561 parent_css = parent->subsys[subsys_id];
5562 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005563 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005564 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005565
5566 child_id = get_new_cssid(ss, depth);
5567 if (IS_ERR(child_id))
5568 return PTR_ERR(child_id);
5569
5570 for (i = 0; i < depth; i++)
5571 child_id->stack[i] = parent_id->stack[i];
5572 child_id->stack[depth] = child_id->id;
5573 /*
5574 * child_id->css pointer will be set after this cgroup is available
5575 * see cgroup_populate_dir()
5576 */
5577 rcu_assign_pointer(child_css->id, child_id);
5578
5579 return 0;
5580}
5581
5582/**
5583 * css_lookup - lookup css by id
5584 * @ss: cgroup subsys to be looked into.
5585 * @id: the id
5586 *
5587 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5588 * NULL if not. Should be called under rcu_read_lock()
5589 */
5590struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5591{
5592 struct css_id *cssid = NULL;
5593
5594 BUG_ON(!ss->use_id);
5595 cssid = idr_find(&ss->idr, id);
5596
5597 if (unlikely(!cssid))
5598 return NULL;
5599
5600 return rcu_dereference(cssid->css);
5601}
Ben Blum67523c42010-03-10 15:22:11 -08005602EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005603
Stephane Eraniane5d13672011-02-14 11:20:01 +02005604/*
5605 * get corresponding css from file open on cgroupfs directory
5606 */
5607struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5608{
5609 struct cgroup *cgrp;
5610 struct inode *inode;
5611 struct cgroup_subsys_state *css;
5612
Al Viro496ad9a2013-01-23 17:07:38 -05005613 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005614 /* check in cgroup filesystem dir */
5615 if (inode->i_op != &cgroup_dir_inode_operations)
5616 return ERR_PTR(-EBADF);
5617
5618 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5619 return ERR_PTR(-EINVAL);
5620
5621 /* get cgroup */
5622 cgrp = __d_cgrp(f->f_dentry);
5623 css = cgrp->subsys[id];
5624 return css ? css : ERR_PTR(-ENOENT);
5625}
5626
Paul Menagefe693432009-09-23 15:56:20 -07005627#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005628static struct cgroup_subsys_state *
5629debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005630{
5631 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5632
5633 if (!css)
5634 return ERR_PTR(-ENOMEM);
5635
5636 return css;
5637}
5638
Tejun Heoeb954192013-08-08 20:11:23 -04005639static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005640{
Tejun Heoeb954192013-08-08 20:11:23 -04005641 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005642}
5643
Tejun Heo182446d2013-08-08 20:11:24 -04005644static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5645 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005646{
Tejun Heo182446d2013-08-08 20:11:24 -04005647 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005648}
5649
Tejun Heo182446d2013-08-08 20:11:24 -04005650static u64 current_css_set_read(struct cgroup_subsys_state *css,
5651 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005652{
5653 return (u64)(unsigned long)current->cgroups;
5654}
5655
Tejun Heo182446d2013-08-08 20:11:24 -04005656static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005657 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005658{
5659 u64 count;
5660
5661 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005662 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005663 rcu_read_unlock();
5664 return count;
5665}
5666
Tejun Heo182446d2013-08-08 20:11:24 -04005667static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005668 struct cftype *cft,
5669 struct seq_file *seq)
5670{
Tejun Heo69d02062013-06-12 21:04:50 -07005671 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005672 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005673
5674 read_lock(&css_set_lock);
5675 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005676 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005677 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005678 struct cgroup *c = link->cgrp;
5679 const char *name;
5680
5681 if (c->dentry)
5682 name = c->dentry->d_name.name;
5683 else
5684 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005685 seq_printf(seq, "Root %d group %s\n",
5686 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005687 }
5688 rcu_read_unlock();
5689 read_unlock(&css_set_lock);
5690 return 0;
5691}
5692
5693#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005694static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5695 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005696{
Tejun Heo69d02062013-06-12 21:04:50 -07005697 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005698
5699 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005700 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005701 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005702 struct task_struct *task;
5703 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005704 seq_printf(seq, "css_set %p\n", cset);
5705 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005706 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5707 seq_puts(seq, " ...\n");
5708 break;
5709 } else {
5710 seq_printf(seq, " task %d\n",
5711 task_pid_vnr(task));
5712 }
5713 }
5714 }
5715 read_unlock(&css_set_lock);
5716 return 0;
5717}
5718
Tejun Heo182446d2013-08-08 20:11:24 -04005719static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005720{
Tejun Heo182446d2013-08-08 20:11:24 -04005721 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005722}
5723
5724static struct cftype debug_files[] = {
5725 {
Paul Menagefe693432009-09-23 15:56:20 -07005726 .name = "taskcount",
5727 .read_u64 = debug_taskcount_read,
5728 },
5729
5730 {
5731 .name = "current_css_set",
5732 .read_u64 = current_css_set_read,
5733 },
5734
5735 {
5736 .name = "current_css_set_refcount",
5737 .read_u64 = current_css_set_refcount_read,
5738 },
5739
5740 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005741 .name = "current_css_set_cg_links",
5742 .read_seq_string = current_css_set_cg_links_read,
5743 },
5744
5745 {
5746 .name = "cgroup_css_links",
5747 .read_seq_string = cgroup_css_links_read,
5748 },
5749
5750 {
Paul Menagefe693432009-09-23 15:56:20 -07005751 .name = "releasable",
5752 .read_u64 = releasable_read,
5753 },
Paul Menagefe693432009-09-23 15:56:20 -07005754
Tejun Heo4baf6e32012-04-01 12:09:55 -07005755 { } /* terminate */
5756};
Paul Menagefe693432009-09-23 15:56:20 -07005757
5758struct cgroup_subsys debug_subsys = {
5759 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005760 .css_alloc = debug_css_alloc,
5761 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005762 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005763 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005764};
5765#endif /* CONFIG_CGROUP_DEBUG */