blob: dd55244952bdb45a0c50f9b711c6845f022dcffd [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
Paul Menage817929e2007-10-18 23:39:36 -0700370/* css_set_lock protects the list of css_set objects, and the
371 * chain of tasks off each css_set. Nests outside task->alloc_lock
372 * due to cgroup_iter_start() */
373static DEFINE_RWLOCK(css_set_lock);
374static int css_set_count;
375
Paul Menage7717f7b2009-09-23 15:56:22 -0700376/*
377 * hash table for cgroup groups. This improves the performance to find
378 * an existing css_set. This hash doesn't (currently) take into
379 * account cgroups in empty hierarchies.
380 */
Li Zefan472b1052008-04-29 01:00:11 -0700381#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800382static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700383
Li Zefan0ac801f2013-01-10 11:49:27 +0800384static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700385{
Li Zefan0ac801f2013-01-10 11:49:27 +0800386 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700387 struct cgroup_subsys *ss;
388 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700389
Tejun Heo30159ec2013-06-25 11:53:37 -0700390 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800391 key += (unsigned long)css[i];
392 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700393
Li Zefan0ac801f2013-01-10 11:49:27 +0800394 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700395}
396
Paul Menage817929e2007-10-18 23:39:36 -0700397/* We don't maintain the lists running through each css_set to its
398 * task until after the first call to cgroup_iter_start(). This
399 * reduces the fork()/exit() overhead for people who have cgroups
400 * compiled into their kernel but not actually in use */
Li Zefan8947f9d2008-07-25 01:46:56 -0700401static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700402
Tejun Heo5abb8852013-06-12 21:04:49 -0700403static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700404{
Tejun Heo69d02062013-06-12 21:04:50 -0700405 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700406
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700407 /*
408 * Ensure that the refcount doesn't hit zero while any readers
409 * can see it. Similar to atomic_dec_and_lock(), but for an
410 * rwlock
411 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700412 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700413 return;
414 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700415 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700416 write_unlock(&css_set_lock);
417 return;
418 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700419
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700420 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700421 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700422 css_set_count--;
423
Tejun Heo69d02062013-06-12 21:04:50 -0700424 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700425 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700426
Tejun Heo69d02062013-06-12 21:04:50 -0700427 list_del(&link->cset_link);
428 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800429
Tejun Heoddd69142013-06-12 21:04:54 -0700430 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d8282013-06-12 21:04:55 -0700431 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700432 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700433 set_bit(CGRP_RELEASABLE, &cgrp->flags);
434 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700435 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700436
437 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700438 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439
440 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700442}
443
444/*
445 * refcounted get/put for css_set objects
446 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700448{
Tejun Heo5abb8852013-06-12 21:04:49 -0700449 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700450}
451
Tejun Heo5abb8852013-06-12 21:04:49 -0700452static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700453{
Tejun Heo5abb8852013-06-12 21:04:49 -0700454 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700455}
456
Tejun Heo5abb8852013-06-12 21:04:49 -0700457static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458{
Tejun Heo5abb8852013-06-12 21:04:49 -0700459 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700460}
461
Tejun Heob326f9d2013-06-24 15:21:48 -0700462/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700463 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700464 * @cset: candidate css_set being tested
465 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700466 * @new_cgrp: cgroup that's being entered by the task
467 * @template: desired set of css pointers in css_set (pre-calculated)
468 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800469 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700470 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
471 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700472static bool compare_css_sets(struct css_set *cset,
473 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700474 struct cgroup *new_cgrp,
475 struct cgroup_subsys_state *template[])
476{
477 struct list_head *l1, *l2;
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 /* Not all subsystems matched */
481 return false;
482 }
483
484 /*
485 * Compare cgroup pointers in order to distinguish between
486 * different cgroups in heirarchies with no subsystems. We
487 * could get by with just this check alone (and skip the
488 * memcmp above) but on most setups the memcmp check will
489 * avoid the need for this more expensive check on almost all
490 * candidates.
491 */
492
Tejun Heo69d02062013-06-12 21:04:50 -0700493 l1 = &cset->cgrp_links;
494 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700495 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700496 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700497 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700498
499 l1 = l1->next;
500 l2 = l2->next;
501 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700502 if (l1 == &cset->cgrp_links) {
503 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700504 break;
505 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700506 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 }
508 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700509 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
510 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
511 cgrp1 = link1->cgrp;
512 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700513 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 /*
517 * If this hierarchy is the hierarchy of the cgroup
518 * that's changing, then we need to check that this
519 * css_set points to the new cgroup; if it's any other
520 * hierarchy, then this css_set should point to the
521 * same cgroup as the old css_set.
522 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 if (cgrp1->root == new_cgrp->root) {
524 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700525 return false;
526 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700527 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700528 return false;
529 }
530 }
531 return true;
532}
533
Tejun Heob326f9d2013-06-24 15:21:48 -0700534/**
535 * find_existing_css_set - init css array and find the matching css_set
536 * @old_cset: the css_set that we're using before the cgroup transition
537 * @cgrp: the cgroup that we're moving into
538 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540static struct css_set *find_existing_css_set(struct css_set *old_cset,
541 struct cgroup *cgrp,
542 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700543{
Paul Menagebd89aab2007-10-18 23:40:44 -0700544 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700545 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700546 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800547 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700548 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700549
Ben Blumaae8aab2010-03-10 15:22:07 -0800550 /*
551 * Build the set of subsystem state objects that we want to see in the
552 * new css_set. while subsystems can change globally, the entries here
553 * won't change, so no need for locking.
554 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700555 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -0400556 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700557 /* Subsystem is in this hierarchy. So we want
558 * the subsystem state from the new
559 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700560 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700561 } else {
562 /* Subsystem is not in this hierarchy, so we
563 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700564 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700565 }
566 }
567
Li Zefan0ac801f2013-01-10 11:49:27 +0800568 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700569 hash_for_each_possible(css_set_table, cset, hlist, key) {
570 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700571 continue;
572
573 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700574 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700575 }
Paul Menage817929e2007-10-18 23:39:36 -0700576
577 /* No existing cgroup group matched */
578 return NULL;
579}
580
Tejun Heo69d02062013-06-12 21:04:50 -0700581static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700582{
Tejun Heo69d02062013-06-12 21:04:50 -0700583 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700584
Tejun Heo69d02062013-06-12 21:04:50 -0700585 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
586 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700587 kfree(link);
588 }
589}
590
Tejun Heo69d02062013-06-12 21:04:50 -0700591/**
592 * allocate_cgrp_cset_links - allocate cgrp_cset_links
593 * @count: the number of links to allocate
594 * @tmp_links: list_head the allocated links are put on
595 *
596 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
597 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700598 */
Tejun Heo69d02062013-06-12 21:04:50 -0700599static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700600{
Tejun Heo69d02062013-06-12 21:04:50 -0700601 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700602 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700603
604 INIT_LIST_HEAD(tmp_links);
605
Li Zefan36553432008-07-29 22:33:19 -0700606 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700607 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700608 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700609 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700610 return -ENOMEM;
611 }
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700613 }
614 return 0;
615}
616
Li Zefanc12f65d2009-01-07 18:07:42 -0800617/**
618 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700619 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700620 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800621 * @cgrp: the destination cgroup
622 */
Tejun Heo69d02062013-06-12 21:04:50 -0700623static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
624 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800625{
Tejun Heo69d02062013-06-12 21:04:50 -0700626 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800627
Tejun Heo69d02062013-06-12 21:04:50 -0700628 BUG_ON(list_empty(tmp_links));
629 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
630 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700631 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700632 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700633 /*
634 * Always add links to the tail of the list so that the list
635 * is sorted by order of hierarchy creation
636 */
Tejun Heo69d02062013-06-12 21:04:50 -0700637 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800638}
639
Tejun Heob326f9d2013-06-24 15:21:48 -0700640/**
641 * find_css_set - return a new css_set with one cgroup updated
642 * @old_cset: the baseline css_set
643 * @cgrp: the cgroup to be updated
644 *
645 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
646 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700647 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700648static struct css_set *find_css_set(struct css_set *old_cset,
649 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700650{
Tejun Heob326f9d2013-06-24 15:21:48 -0700651 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700652 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700653 struct list_head tmp_links;
654 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800655 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700656
Tejun Heob326f9d2013-06-24 15:21:48 -0700657 lockdep_assert_held(&cgroup_mutex);
658
Paul Menage817929e2007-10-18 23:39:36 -0700659 /* First see if we already have a cgroup group that matches
660 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700661 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700662 cset = find_existing_css_set(old_cset, cgrp, template);
663 if (cset)
664 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700665 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700666
Tejun Heo5abb8852013-06-12 21:04:49 -0700667 if (cset)
668 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700669
Tejun Heof4f4be22013-06-12 21:04:51 -0700670 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700671 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700672 return NULL;
673
Tejun Heo69d02062013-06-12 21:04:50 -0700674 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700675 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700676 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700677 return NULL;
678 }
679
Tejun Heo5abb8852013-06-12 21:04:49 -0700680 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700681 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700682 INIT_LIST_HEAD(&cset->tasks);
683 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700684
685 /* Copy the set of subsystem state objects generated in
686 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700687 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700688
689 write_lock(&css_set_lock);
690 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700691 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700692 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700693
Paul Menage7717f7b2009-09-23 15:56:22 -0700694 if (c->root == cgrp->root)
695 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700696 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700697 }
Paul Menage817929e2007-10-18 23:39:36 -0700698
Tejun Heo69d02062013-06-12 21:04:50 -0700699 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700700
Paul Menage817929e2007-10-18 23:39:36 -0700701 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700702
703 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 key = css_set_hash(cset->subsys);
705 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700706
Paul Menage817929e2007-10-18 23:39:36 -0700707 write_unlock(&css_set_lock);
708
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700710}
711
Paul Menageddbcc7e2007-10-18 23:39:30 -0700712/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700713 * Return the cgroup for "task" from the given hierarchy. Must be
714 * called with cgroup_mutex held.
715 */
716static struct cgroup *task_cgroup_from_root(struct task_struct *task,
717 struct cgroupfs_root *root)
718{
Tejun Heo5abb8852013-06-12 21:04:49 -0700719 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 struct cgroup *res = NULL;
721
722 BUG_ON(!mutex_is_locked(&cgroup_mutex));
723 read_lock(&css_set_lock);
724 /*
725 * No need to lock the task - since we hold cgroup_mutex the
726 * task can't change groups, so the only thing that can happen
727 * is that it exits and its css is set back to init_css_set.
728 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700729 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700730 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700731 res = &root->top_cgroup;
732 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700733 struct cgrp_cset_link *link;
734
735 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700736 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700737
Paul Menage7717f7b2009-09-23 15:56:22 -0700738 if (c->root == root) {
739 res = c;
740 break;
741 }
742 }
743 }
744 read_unlock(&css_set_lock);
745 BUG_ON(!res);
746 return res;
747}
748
749/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700750 * There is one global cgroup mutex. We also require taking
751 * task_lock() when dereferencing a task's cgroup subsys pointers.
752 * See "The task_lock() exception", at the end of this comment.
753 *
754 * A task must hold cgroup_mutex to modify cgroups.
755 *
756 * Any task can increment and decrement the count field without lock.
757 * So in general, code holding cgroup_mutex can't rely on the count
758 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800759 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700760 * means that no tasks are currently attached, therefore there is no
761 * way a task attached to that cgroup can fork (the other way to
762 * increment the count). So code holding cgroup_mutex can safely
763 * assume that if the count is zero, it will stay zero. Similarly, if
764 * a task holds cgroup_mutex on a cgroup with zero count, it
765 * knows that the cgroup won't be removed, as cgroup_rmdir()
766 * needs that mutex.
767 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700768 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
769 * (usually) take cgroup_mutex. These are the two most performance
770 * critical pieces of code here. The exception occurs on cgroup_exit(),
771 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
772 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800773 * to the release agent with the name of the cgroup (path relative to
774 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700775 *
776 * A cgroup can only be deleted if both its 'count' of using tasks
777 * is zero, and its list of 'children' cgroups is empty. Since all
778 * tasks in the system use _some_ cgroup, and since there is always at
779 * least one task in the system (init, pid == 1), therefore, top_cgroup
780 * always has either children cgroups and/or using tasks. So we don't
781 * need a special hack to ensure that top_cgroup cannot be deleted.
782 *
783 * The task_lock() exception
784 *
785 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800786 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800787 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700788 * several performance critical places that need to reference
789 * task->cgroup without the expense of grabbing a system global
790 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800791 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
793 * the task_struct routinely used for such matters.
794 *
795 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800796 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700797 */
798
Paul Menageddbcc7e2007-10-18 23:39:30 -0700799/*
800 * A couple of forward declarations required, due to cyclic reference loop:
801 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
802 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
803 * -> cgroup_mkdir.
804 */
805
Al Viro18bb1db2011-07-26 01:41:39 -0400806static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400807static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700808static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700809static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700810static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700811static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700812
813static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200814 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700815 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700816};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700817
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700818static int alloc_css_id(struct cgroup_subsys *ss,
819 struct cgroup *parent, struct cgroup *child);
820
Al Viroa5e7ed32011-07-26 01:55:55 -0400821static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700822{
823 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824
825 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400826 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700827 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100828 inode->i_uid = current_fsuid();
829 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700830 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
831 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
832 }
833 return inode;
834}
835
Li Zefan65dff752013-03-01 15:01:56 +0800836static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
837{
838 struct cgroup_name *name;
839
840 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
841 if (!name)
842 return NULL;
843 strcpy(name->name, dentry->d_name.name);
844 return name;
845}
846
Li Zefanbe445622013-01-24 14:31:42 +0800847static void cgroup_free_fn(struct work_struct *work)
848{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700849 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800850 struct cgroup_subsys *ss;
851
852 mutex_lock(&cgroup_mutex);
853 /*
854 * Release the subsystem state objects.
855 */
Tejun Heoeb954192013-08-08 20:11:23 -0400856 for_each_root_subsys(cgrp->root, ss) {
857 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
858
859 ss->css_free(css);
860 }
Li Zefanbe445622013-01-24 14:31:42 +0800861
862 cgrp->root->number_of_cgroups--;
863 mutex_unlock(&cgroup_mutex);
864
865 /*
Li Zefan415cf072013-04-08 14:35:02 +0800866 * We get a ref to the parent's dentry, and put the ref when
867 * this cgroup is being freed, so it's guaranteed that the
868 * parent won't be destroyed before its children.
869 */
870 dput(cgrp->parent->dentry);
871
872 /*
Li Zefanbe445622013-01-24 14:31:42 +0800873 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700874 * created the cgroup. This will free cgrp->root, if we are
875 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800876 */
877 deactivate_super(cgrp->root->sb);
878
879 /*
880 * if we're getting rid of the cgroup, refcount should ensure
881 * that there are no pidlists left.
882 */
883 BUG_ON(!list_empty(&cgrp->pidlists));
884
885 simple_xattrs_free(&cgrp->xattrs);
886
Li Zefan65dff752013-03-01 15:01:56 +0800887 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800888 kfree(cgrp);
889}
890
891static void cgroup_free_rcu(struct rcu_head *head)
892{
893 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
894
Tejun Heoea15f8c2013-06-13 19:27:42 -0700895 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
896 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800897}
898
Paul Menageddbcc7e2007-10-18 23:39:30 -0700899static void cgroup_diput(struct dentry *dentry, struct inode *inode)
900{
901 /* is dentry a directory ? if so, kfree() associated cgroup */
902 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700903 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800904
Tejun Heo54766d42013-06-12 21:04:53 -0700905 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800906 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700907 } else {
908 struct cfent *cfe = __d_cfe(dentry);
909 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
910
911 WARN_ONCE(!list_empty(&cfe->node) &&
912 cgrp != &cgrp->root->top_cgroup,
913 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700914 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700915 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916 }
917 iput(inode);
918}
919
Al Viroc72a04e2011-01-14 05:31:45 +0000920static int cgroup_delete(const struct dentry *d)
921{
922 return 1;
923}
924
Paul Menageddbcc7e2007-10-18 23:39:30 -0700925static void remove_dir(struct dentry *d)
926{
927 struct dentry *parent = dget(d->d_parent);
928
929 d_delete(d);
930 simple_rmdir(parent->d_inode, d);
931 dput(parent);
932}
933
Li Zefan2739d3c2013-01-21 18:18:33 +0800934static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700935{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700936 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700937
Tejun Heo05ef1d72012-04-01 12:09:56 -0700938 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
939 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100940
Li Zefan2739d3c2013-01-21 18:18:33 +0800941 /*
942 * If we're doing cleanup due to failure of cgroup_create(),
943 * the corresponding @cfe may not exist.
944 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700945 list_for_each_entry(cfe, &cgrp->files, node) {
946 struct dentry *d = cfe->dentry;
947
948 if (cft && cfe->type != cft)
949 continue;
950
951 dget(d);
952 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700953 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700954 list_del_init(&cfe->node);
955 dput(d);
956
Li Zefan2739d3c2013-01-21 18:18:33 +0800957 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700958 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959}
960
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400961/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700962 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700963 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400964 * @subsys_mask: mask of the subsystem ids whose files should be removed
965 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700966static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700967{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400968 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700969 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700970
Tejun Heob420ba72013-07-12 12:34:02 -0700971 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400972 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700973
974 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975 continue;
976 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400977 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700979}
980
981/*
982 * NOTE : the dentry must have been dget()'ed
983 */
984static void cgroup_d_remove_dir(struct dentry *dentry)
985{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100986 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700987
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100988 parent = dentry->d_parent;
989 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +0800990 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700991 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100992 spin_unlock(&dentry->d_lock);
993 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994 remove_dir(dentry);
995}
996
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -0700997/*
Ben Blumcf5d5942010-03-10 15:22:09 -0800998 * Call with cgroup_mutex held. Drops reference counts on modules, including
999 * any duplicate ones that parse_cgroupfs_options took. If this function
1000 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001001 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001003 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004{
Paul Menagebd89aab2007-10-18 23:40:44 -07001005 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001006 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001007 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001008 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009
Ben Blumaae8aab2010-03-10 15:22:07 -08001010 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001011 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001012
Paul Menageddbcc7e2007-10-18 23:39:30 -07001013 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001014 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001015 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001017
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001018 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001019 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001020 ret = -EBUSY;
1021 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001022 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001023
1024 /* pin the module */
1025 if (!try_module_get(ss->module)) {
1026 ret = -ENOENT;
1027 goto out_put;
1028 }
1029 pinned |= 1 << i;
1030 }
1031
1032 /* subsys could be missing if unloaded between parsing and here */
1033 if (added_mask != pinned) {
1034 ret = -ENOENT;
1035 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 }
1037
Tejun Heo31261212013-06-28 17:07:30 -07001038 ret = cgroup_populate_dir(cgrp, added_mask);
1039 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001040 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001041
1042 /*
1043 * Nothing can fail from this point on. Remove files for the
1044 * removed subsystems and rebind each subsystem.
1045 */
1046 cgroup_clear_dir(cgrp, removed_mask);
1047
Tejun Heo30159ec2013-06-25 11:53:37 -07001048 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001049 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001050
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001051 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001053 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001054 BUG_ON(!cgroup_dummy_top->subsys[i]);
1055 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001056
Tejun Heo9871bf92013-06-24 15:21:47 -07001057 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001058 cgrp->subsys[i]->cgroup = cgrp;
Li Zefan33a68ac2009-01-07 18:07:42 -08001059 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001060 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001061 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001062 ss->bind(cgrp->subsys[i]);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001063
Ben Blumcf5d5942010-03-10 15:22:09 -08001064 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001065 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001066 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 /* We're removing this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07001068 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001069 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001070
Paul Menageddbcc7e2007-10-18 23:39:30 -07001071 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001072 ss->bind(cgroup_dummy_top->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001073 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001074 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001075 cgroup_subsys[i]->root = &cgroup_dummy_root;
1076 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001077
Ben Blumcf5d5942010-03-10 15:22:09 -08001078 /* subsystem is now free - drop reference on module */
1079 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001080 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001081 }
1082 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083
Tejun Heo1672d042013-06-25 18:04:54 -07001084 /*
1085 * Mark @root has finished binding subsystems. @root->subsys_mask
1086 * now matches the bound subsystems.
1087 */
1088 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1089
Paul Menageddbcc7e2007-10-18 23:39:30 -07001090 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001091
1092out_put:
1093 for_each_subsys(ss, i)
1094 if (pinned & (1 << i))
1095 module_put(ss->module);
1096 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001097}
1098
Al Viro34c80b12011-12-08 21:32:45 -05001099static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100{
Al Viro34c80b12011-12-08 21:32:45 -05001101 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102 struct cgroup_subsys *ss;
1103
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001104 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001105 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001106 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001107 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1108 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001109 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001110 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001111 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001112 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001113 if (strlen(root->release_agent_path))
1114 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001115 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001116 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001117 if (strlen(root->name))
1118 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001119 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120 return 0;
1121}
1122
1123struct cgroup_sb_opts {
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001124 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001126 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001127 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001128 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001129 /* User explicitly requested empty subsystem */
1130 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001131
1132 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001133
Paul Menageddbcc7e2007-10-18 23:39:30 -07001134};
1135
Ben Blumaae8aab2010-03-10 15:22:07 -08001136/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001137 * Convert a hierarchy specifier into a bitmask of subsystems and
1138 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1139 * array. This function takes refcounts on subsystems to be used, unless it
1140 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001141 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001142static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001144 char *token, *o = data;
1145 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001146 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001147 struct cgroup_subsys *ss;
1148 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001149
Ben Blumaae8aab2010-03-10 15:22:07 -08001150 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1151
Li Zefanf9ab5b52009-06-17 16:26:33 -07001152#ifdef CONFIG_CPUSETS
1153 mask = ~(1UL << cpuset_subsys_id);
1154#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001155
Paul Menagec6d57f32009-09-23 15:56:19 -07001156 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001157
1158 while ((token = strsep(&o, ",")) != NULL) {
1159 if (!*token)
1160 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001162 /* Explicitly have no subsystems */
1163 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001164 continue;
1165 }
1166 if (!strcmp(token, "all")) {
1167 /* Mutually exclusive option 'all' + subsystem name */
1168 if (one_ss)
1169 return -EINVAL;
1170 all_ss = true;
1171 continue;
1172 }
Tejun Heo873fe092013-04-14 20:15:26 -07001173 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1174 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1175 continue;
1176 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001177 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001178 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 continue;
1180 }
1181 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001182 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001185 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001186 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001187 continue;
1188 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001189 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001190 /* Specifying two release agents is forbidden */
1191 if (opts->release_agent)
1192 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001193 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001194 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001195 if (!opts->release_agent)
1196 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 continue;
1198 }
1199 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001200 const char *name = token + 5;
1201 /* Can't specify an empty name */
1202 if (!strlen(name))
1203 return -EINVAL;
1204 /* Must match [\w.-]+ */
1205 for (i = 0; i < strlen(name); i++) {
1206 char c = name[i];
1207 if (isalnum(c))
1208 continue;
1209 if ((c == '.') || (c == '-') || (c == '_'))
1210 continue;
1211 return -EINVAL;
1212 }
1213 /* Specifying two names is forbidden */
1214 if (opts->name)
1215 return -EINVAL;
1216 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001217 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001218 GFP_KERNEL);
1219 if (!opts->name)
1220 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001221
1222 continue;
1223 }
1224
Tejun Heo30159ec2013-06-25 11:53:37 -07001225 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001226 if (strcmp(token, ss->name))
1227 continue;
1228 if (ss->disabled)
1229 continue;
1230
1231 /* Mutually exclusive option 'all' + subsystem name */
1232 if (all_ss)
1233 return -EINVAL;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001234 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001235 one_ss = true;
1236
1237 break;
1238 }
1239 if (i == CGROUP_SUBSYS_COUNT)
1240 return -ENOENT;
1241 }
1242
1243 /*
1244 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001245 * otherwise if 'none', 'name=' and a subsystem name options
1246 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001247 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001248 if (all_ss || (!one_ss && !opts->none && !opts->name))
1249 for_each_subsys(ss, i)
1250 if (!ss->disabled)
1251 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001252
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001253 /* Consistency checks */
1254
Tejun Heo873fe092013-04-14 20:15:26 -07001255 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1256 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1257
1258 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1259 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1260 return -EINVAL;
1261 }
1262
1263 if (opts->cpuset_clone_children) {
1264 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1265 return -EINVAL;
1266 }
1267 }
1268
Li Zefanf9ab5b52009-06-17 16:26:33 -07001269 /*
1270 * Option noprefix was introduced just for backward compatibility
1271 * with the old cpuset, so we allow noprefix only if mounting just
1272 * the cpuset subsystem.
1273 */
Tejun Heo93438622013-04-14 20:15:25 -07001274 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001275 return -EINVAL;
1276
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001277
1278 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001279 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001280 return -EINVAL;
1281
1282 /*
1283 * We either have to specify by name or by subsystems. (So all
1284 * empty hierarchies must have a name).
1285 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001286 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001287 return -EINVAL;
1288
1289 return 0;
1290}
1291
1292static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1293{
1294 int ret = 0;
1295 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001296 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001297 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001298 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001299
Tejun Heo873fe092013-04-14 20:15:26 -07001300 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1301 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1302 return -EINVAL;
1303 }
1304
Paul Menagebd89aab2007-10-18 23:40:44 -07001305 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001307 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001308
1309 /* See what subsystems are wanted */
1310 ret = parse_cgroupfs_options(data, &opts);
1311 if (ret)
1312 goto out_unlock;
1313
Tejun Heoa8a648c2013-06-24 15:21:47 -07001314 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001315 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1316 task_tgid_nr(current), current->comm);
1317
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001318 added_mask = opts.subsys_mask & ~root->subsys_mask;
1319 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001320
Ben Blumcf5d5942010-03-10 15:22:09 -08001321 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001322 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001323 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001324 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1325 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1326 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001328 goto out_unlock;
1329 }
1330
Tejun Heof172e672013-06-28 17:07:30 -07001331 /* remounting is not allowed for populated hierarchies */
1332 if (root->number_of_cgroups > 1) {
1333 ret = -EBUSY;
1334 goto out_unlock;
1335 }
1336
Tejun Heoa8a648c2013-06-24 15:21:47 -07001337 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001338 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001339 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001340
Paul Menage81a6a5c2007-10-18 23:39:38 -07001341 if (opts.release_agent)
1342 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001343 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001344 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001345 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001346 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001347 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001348 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001349 return ret;
1350}
1351
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001352static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001353 .statfs = simple_statfs,
1354 .drop_inode = generic_delete_inode,
1355 .show_options = cgroup_show_options,
1356 .remount_fs = cgroup_remount,
1357};
1358
Paul Menagecc31edc2008-10-18 20:28:04 -07001359static void init_cgroup_housekeeping(struct cgroup *cgrp)
1360{
1361 INIT_LIST_HEAD(&cgrp->sibling);
1362 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001363 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001364 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001365 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001366 INIT_LIST_HEAD(&cgrp->pidlists);
1367 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001368 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001369 INIT_LIST_HEAD(&cgrp->event_list);
1370 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001371 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001372}
Paul Menagec6d57f32009-09-23 15:56:19 -07001373
Paul Menageddbcc7e2007-10-18 23:39:30 -07001374static void init_cgroup_root(struct cgroupfs_root *root)
1375{
Paul Menagebd89aab2007-10-18 23:40:44 -07001376 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001377
Paul Menageddbcc7e2007-10-18 23:39:30 -07001378 INIT_LIST_HEAD(&root->subsys_list);
1379 INIT_LIST_HEAD(&root->root_list);
1380 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001381 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001382 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001383 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee82013-07-31 09:50:50 +08001384 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001385}
1386
Tejun Heofc76df72013-06-25 11:53:37 -07001387static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001388{
Tejun Heo1a574232013-04-14 11:36:58 -07001389 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001390
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001391 lockdep_assert_held(&cgroup_mutex);
1392 lockdep_assert_held(&cgroup_root_mutex);
1393
Tejun Heofc76df72013-06-25 11:53:37 -07001394 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1395 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001396 if (id < 0)
1397 return id;
1398
1399 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001400 return 0;
1401}
1402
1403static void cgroup_exit_root_id(struct cgroupfs_root *root)
1404{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001405 lockdep_assert_held(&cgroup_mutex);
1406 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001407
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001408 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001409 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001410 root->hierarchy_id = 0;
1411 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001412}
1413
Paul Menageddbcc7e2007-10-18 23:39:30 -07001414static int cgroup_test_super(struct super_block *sb, void *data)
1415{
Paul Menagec6d57f32009-09-23 15:56:19 -07001416 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001417 struct cgroupfs_root *root = sb->s_fs_info;
1418
Paul Menagec6d57f32009-09-23 15:56:19 -07001419 /* If we asked for a name then it must match */
1420 if (opts->name && strcmp(opts->name, root->name))
1421 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001422
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001423 /*
1424 * If we asked for subsystems (or explicitly for no
1425 * subsystems) then they must match
1426 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001427 if ((opts->subsys_mask || opts->none)
1428 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001429 return 0;
1430
1431 return 1;
1432}
1433
Paul Menagec6d57f32009-09-23 15:56:19 -07001434static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1435{
1436 struct cgroupfs_root *root;
1437
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001438 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001439 return NULL;
1440
1441 root = kzalloc(sizeof(*root), GFP_KERNEL);
1442 if (!root)
1443 return ERR_PTR(-ENOMEM);
1444
1445 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001446
Tejun Heo1672d042013-06-25 18:04:54 -07001447 /*
1448 * We need to set @root->subsys_mask now so that @root can be
1449 * matched by cgroup_test_super() before it finishes
1450 * initialization; otherwise, competing mounts with the same
1451 * options may try to bind the same subsystems instead of waiting
1452 * for the first one leading to unexpected mount errors.
1453 * SUBSYS_BOUND will be set once actual binding is complete.
1454 */
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001455 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 root->flags = opts->flags;
1457 if (opts->release_agent)
1458 strcpy(root->release_agent_path, opts->release_agent);
1459 if (opts->name)
1460 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001461 if (opts->cpuset_clone_children)
1462 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001463 return root;
1464}
1465
Tejun Heofa3ca072013-04-14 11:36:56 -07001466static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001467{
Tejun Heofa3ca072013-04-14 11:36:56 -07001468 if (root) {
1469 /* hierarhcy ID shoulid already have been released */
1470 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001471
Li Zefan4e96ee82013-07-31 09:50:50 +08001472 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001473 kfree(root);
1474 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001475}
1476
Paul Menageddbcc7e2007-10-18 23:39:30 -07001477static int cgroup_set_super(struct super_block *sb, void *data)
1478{
1479 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 struct cgroup_sb_opts *opts = data;
1481
1482 /* If we don't have a new root, we can't set up a new sb */
1483 if (!opts->new_root)
1484 return -EINVAL;
1485
Aristeu Rozanskia1a71b42012-08-23 16:53:31 -04001486 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001487
1488 ret = set_anon_super(sb, NULL);
1489 if (ret)
1490 return ret;
1491
Paul Menagec6d57f32009-09-23 15:56:19 -07001492 sb->s_fs_info = opts->new_root;
1493 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494
1495 sb->s_blocksize = PAGE_CACHE_SIZE;
1496 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1497 sb->s_magic = CGROUP_SUPER_MAGIC;
1498 sb->s_op = &cgroup_ops;
1499
1500 return 0;
1501}
1502
1503static int cgroup_get_rootdir(struct super_block *sb)
1504{
Al Viro0df6a632010-12-21 13:29:29 -05001505 static const struct dentry_operations cgroup_dops = {
1506 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001507 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001508 };
1509
Paul Menageddbcc7e2007-10-18 23:39:30 -07001510 struct inode *inode =
1511 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
1513 if (!inode)
1514 return -ENOMEM;
1515
Paul Menageddbcc7e2007-10-18 23:39:30 -07001516 inode->i_fop = &simple_dir_operations;
1517 inode->i_op = &cgroup_dir_inode_operations;
1518 /* directories start off with i_nlink == 2 (for "." entry) */
1519 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001520 sb->s_root = d_make_root(inode);
1521 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001522 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001523 /* for everything else we want ->d_op set */
1524 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001525 return 0;
1526}
1527
Al Virof7e83572010-07-26 13:23:11 +04001528static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001530 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531{
1532 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001533 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 int ret = 0;
1535 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001536 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001537 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001538 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001539 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540
1541 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001542 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001544 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001545 if (ret)
1546 goto out_err;
1547
1548 /*
1549 * Allocate a new cgroup root. We may not need it if we're
1550 * reusing an existing hierarchy.
1551 */
1552 new_root = cgroup_root_from_opts(&opts);
1553 if (IS_ERR(new_root)) {
1554 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001555 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001556 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001557 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558
Paul Menagec6d57f32009-09-23 15:56:19 -07001559 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001560 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001562 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001563 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001564 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001565 }
1566
Paul Menagec6d57f32009-09-23 15:56:19 -07001567 root = sb->s_fs_info;
1568 BUG_ON(!root);
1569 if (root == opts.new_root) {
1570 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001571 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001572 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001573 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001574 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
1576 BUG_ON(sb->s_root != NULL);
1577
1578 ret = cgroup_get_rootdir(sb);
1579 if (ret)
1580 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001581 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582
Paul Menage817929e2007-10-18 23:39:36 -07001583 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001585 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001586
Li Zefan4e96ee82013-07-31 09:50:50 +08001587 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1588 0, 1, GFP_KERNEL);
1589 if (root_cgrp->id < 0)
1590 goto unlock_drop;
1591
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001592 /* Check for name clashes with existing mounts */
1593 ret = -EBUSY;
1594 if (strlen(root->name))
1595 for_each_active_root(existing_root)
1596 if (!strcmp(existing_root->name, root->name))
1597 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001598
Paul Menage817929e2007-10-18 23:39:36 -07001599 /*
1600 * We're accessing css_set_count without locking
1601 * css_set_lock here, but that's OK - it can only be
1602 * increased by someone holding cgroup_lock, and
1603 * that's us. The worst that can happen is that we
1604 * have some link structures left over
1605 */
Tejun Heo69d02062013-06-12 21:04:50 -07001606 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001607 if (ret)
1608 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001609
Tejun Heofc76df72013-06-25 11:53:37 -07001610 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1611 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001612 if (ret)
1613 goto unlock_drop;
1614
Tejun Heo31261212013-06-28 17:07:30 -07001615 sb->s_root->d_fsdata = root_cgrp;
1616 root_cgrp->dentry = sb->s_root;
1617
1618 /*
1619 * We're inside get_sb() and will call lookup_one_len() to
1620 * create the root files, which doesn't work if SELinux is
1621 * in use. The following cred dancing somehow works around
1622 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1623 * populating new cgroupfs mount") for more details.
1624 */
1625 cred = override_creds(&init_cred);
1626
Tejun Heo2bb566c2013-08-08 20:11:23 -04001627 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001628 if (ret)
1629 goto rm_base_files;
1630
Tejun Heoa8a648c2013-06-24 15:21:47 -07001631 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001632 if (ret)
1633 goto rm_base_files;
1634
1635 revert_creds(cred);
1636
Ben Blumcf5d5942010-03-10 15:22:09 -08001637 /*
1638 * There must be no failure case after here, since rebinding
1639 * takes care of subsystems' refcounts, which are explicitly
1640 * dropped in the failure exit path.
1641 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001642
Tejun Heo9871bf92013-06-24 15:21:47 -07001643 list_add(&root->root_list, &cgroup_roots);
1644 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001645
Paul Menage817929e2007-10-18 23:39:36 -07001646 /* Link the top cgroup in this hierarchy into all
1647 * the css_set objects */
1648 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001649 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001650 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001651 write_unlock(&css_set_lock);
1652
Tejun Heo69d02062013-06-12 21:04:50 -07001653 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001654
Li Zefanc12f65d2009-01-07 18:07:42 -08001655 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001656 BUG_ON(root->number_of_cgroups != 1);
1657
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001658 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001660 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001661 } else {
1662 /*
1663 * We re-used an existing hierarchy - the new root (if
1664 * any) is not needed
1665 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001666 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001667
Tejun Heoc7ba8282013-06-29 14:06:10 -07001668 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001669 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1670 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1671 ret = -EINVAL;
1672 goto drop_new_super;
1673 } else {
1674 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1675 }
Tejun Heo873fe092013-04-14 20:15:26 -07001676 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 }
1678
Paul Menagec6d57f32009-09-23 15:56:19 -07001679 kfree(opts.release_agent);
1680 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001681 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001682
Tejun Heo31261212013-06-28 17:07:30 -07001683 rm_base_files:
1684 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001685 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001686 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001687 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001688 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001689 mutex_unlock(&cgroup_root_mutex);
1690 mutex_unlock(&cgroup_mutex);
1691 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001692 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001693 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001694 out_err:
1695 kfree(opts.release_agent);
1696 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001697 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001698}
1699
1700static void cgroup_kill_sb(struct super_block *sb) {
1701 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001702 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001703 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001704 int ret;
1705
1706 BUG_ON(!root);
1707
1708 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001709 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710
Tejun Heo31261212013-06-28 17:07:30 -07001711 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001712 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001713 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001714
1715 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001716 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1717 ret = rebind_subsystems(root, 0, root->subsys_mask);
1718 /* Shouldn't be able to fail ... */
1719 BUG_ON(ret);
1720 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721
Paul Menage817929e2007-10-18 23:39:36 -07001722 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001723 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001724 * root cgroup
1725 */
1726 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001727
Tejun Heo69d02062013-06-12 21:04:50 -07001728 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1729 list_del(&link->cset_link);
1730 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001731 kfree(link);
1732 }
1733 write_unlock(&css_set_lock);
1734
Paul Menage839ec542009-01-29 14:25:22 -08001735 if (!list_empty(&root->root_list)) {
1736 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001737 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001738 }
Li Zefane5f6a862009-01-07 18:07:41 -08001739
Tejun Heofa3ca072013-04-14 11:36:56 -07001740 cgroup_exit_root_id(root);
1741
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001742 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001743 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001744 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001745
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001746 simple_xattrs_free(&cgrp->xattrs);
1747
Paul Menageddbcc7e2007-10-18 23:39:30 -07001748 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001749 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001750}
1751
1752static struct file_system_type cgroup_fs_type = {
1753 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001754 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001755 .kill_sb = cgroup_kill_sb,
1756};
1757
Greg KH676db4a2010-08-05 13:53:35 -07001758static struct kobject *cgroup_kobj;
1759
Li Zefana043e3b2008-02-23 15:24:09 -08001760/**
1761 * cgroup_path - generate the path of a cgroup
1762 * @cgrp: the cgroup in question
1763 * @buf: the buffer to write the path into
1764 * @buflen: the length of the buffer
1765 *
Li Zefan65dff752013-03-01 15:01:56 +08001766 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1767 *
1768 * We can't generate cgroup path using dentry->d_name, as accessing
1769 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1770 * inode's i_mutex, while on the other hand cgroup_path() can be called
1771 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001773int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774{
Li Zefan65dff752013-03-01 15:01:56 +08001775 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001776 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001777
Tejun Heoda1f2962013-04-14 10:32:19 -07001778 if (!cgrp->parent) {
1779 if (strlcpy(buf, "/", buflen) >= buflen)
1780 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001781 return 0;
1782 }
1783
Tao Ma316eb662012-11-08 21:36:38 +08001784 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001785 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001786
Li Zefan65dff752013-03-01 15:01:56 +08001787 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001788 do {
Li Zefan65dff752013-03-01 15:01:56 +08001789 const char *name = cgroup_name(cgrp);
1790 int len;
1791
1792 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001794 goto out;
1795 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001796
Paul Menageddbcc7e2007-10-18 23:39:30 -07001797 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001798 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001800
1801 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001802 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001803 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001804 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001805out:
1806 rcu_read_unlock();
1807 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001808}
Ben Blum67523c42010-03-10 15:22:11 -08001809EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810
Tejun Heo857a2be2013-04-14 20:50:08 -07001811/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001812 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001813 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001814 * @buf: the buffer to write the path into
1815 * @buflen: the length of the buffer
1816 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001817 * Determine @task's cgroup on the first (the one with the lowest non-zero
1818 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1819 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1820 * cgroup controller callbacks.
1821 *
1822 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001823 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001824int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001825{
1826 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001827 struct cgroup *cgrp;
1828 int hierarchy_id = 1, ret = 0;
1829
1830 if (buflen < 2)
1831 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001832
1833 mutex_lock(&cgroup_mutex);
1834
Tejun Heo913ffdb2013-07-11 16:34:48 -07001835 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1836
Tejun Heo857a2be2013-04-14 20:50:08 -07001837 if (root) {
1838 cgrp = task_cgroup_from_root(task, root);
1839 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001840 } else {
1841 /* if no hierarchy exists, everyone is in "/" */
1842 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001843 }
1844
1845 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001846 return ret;
1847}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001848EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001849
Ben Blum74a11662011-05-26 16:25:20 -07001850/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001851 * Control Group taskset
1852 */
Tejun Heo134d3372011-12-12 18:12:21 -08001853struct task_and_cgroup {
1854 struct task_struct *task;
1855 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001856 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001857};
1858
Tejun Heo2f7ee562011-12-12 18:12:21 -08001859struct cgroup_taskset {
1860 struct task_and_cgroup single;
1861 struct flex_array *tc_array;
1862 int tc_array_len;
1863 int idx;
1864 struct cgroup *cur_cgrp;
1865};
1866
1867/**
1868 * cgroup_taskset_first - reset taskset and return the first task
1869 * @tset: taskset of interest
1870 *
1871 * @tset iteration is initialized and the first task is returned.
1872 */
1873struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1874{
1875 if (tset->tc_array) {
1876 tset->idx = 0;
1877 return cgroup_taskset_next(tset);
1878 } else {
1879 tset->cur_cgrp = tset->single.cgrp;
1880 return tset->single.task;
1881 }
1882}
1883EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1884
1885/**
1886 * cgroup_taskset_next - iterate to the next task in taskset
1887 * @tset: taskset of interest
1888 *
1889 * Return the next task in @tset. Iteration must have been initialized
1890 * with cgroup_taskset_first().
1891 */
1892struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1893{
1894 struct task_and_cgroup *tc;
1895
1896 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1897 return NULL;
1898
1899 tc = flex_array_get(tset->tc_array, tset->idx++);
1900 tset->cur_cgrp = tc->cgrp;
1901 return tc->task;
1902}
1903EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1904
1905/**
1906 * cgroup_taskset_cur_cgroup - return the matching cgroup for the current task
1907 * @tset: taskset of interest
1908 *
1909 * Return the cgroup for the current (last returned) task of @tset. This
1910 * function must be preceded by either cgroup_taskset_first() or
1911 * cgroup_taskset_next().
1912 */
1913struct cgroup *cgroup_taskset_cur_cgroup(struct cgroup_taskset *tset)
1914{
1915 return tset->cur_cgrp;
1916}
1917EXPORT_SYMBOL_GPL(cgroup_taskset_cur_cgroup);
1918
1919/**
1920 * cgroup_taskset_size - return the number of tasks in taskset
1921 * @tset: taskset of interest
1922 */
1923int cgroup_taskset_size(struct cgroup_taskset *tset)
1924{
1925 return tset->tc_array ? tset->tc_array_len : 1;
1926}
1927EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1928
1929
Ben Blum74a11662011-05-26 16:25:20 -07001930/*
1931 * cgroup_task_migrate - move a task from one cgroup to another.
1932 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001933 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001934 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001935static void cgroup_task_migrate(struct cgroup *old_cgrp,
1936 struct task_struct *tsk,
1937 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001938{
Tejun Heo5abb8852013-06-12 21:04:49 -07001939 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001940
1941 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001942 * We are synchronized through threadgroup_lock() against PF_EXITING
1943 * setting such that we can't race against cgroup_exit() changing the
1944 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001945 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001946 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001947 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001948
Ben Blum74a11662011-05-26 16:25:20 -07001949 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001950 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001951 task_unlock(tsk);
1952
1953 /* Update the css_set linked lists if we're using them */
1954 write_lock(&css_set_lock);
1955 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001956 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001957 write_unlock(&css_set_lock);
1958
1959 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001960 * We just gained a reference on old_cset by taking it from the
1961 * task. As trading it for new_cset is protected by cgroup_mutex,
1962 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001963 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001964 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1965 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001966}
1967
Li Zefana043e3b2008-02-23 15:24:09 -08001968/**
Li Zefan081aa452013-03-13 09:17:09 +08001969 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001970 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001971 * @tsk: the task or the leader of the threadgroup to be attached
1972 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001973 *
Tejun Heo257058a2011-12-12 18:12:21 -08001974 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001975 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001976 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001977static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1978 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001979{
1980 int retval, i, group_size;
1981 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07001982 struct cgroupfs_root *root = cgrp->root;
1983 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08001984 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08001985 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07001986 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08001987 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07001988
1989 /*
1990 * step 0: in order to do expensive, possibly blocking operations for
1991 * every thread, we cannot iterate the thread group list, since it needs
1992 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08001993 * group - group_rwsem prevents new threads from appearing, and if
1994 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07001995 */
Li Zefan081aa452013-03-13 09:17:09 +08001996 if (threadgroup)
1997 group_size = get_nr_threads(tsk);
1998 else
1999 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002000 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002001 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002002 if (!group)
2003 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002004 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002005 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002006 if (retval)
2007 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002008
Ben Blum74a11662011-05-26 16:25:20 -07002009 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002010 /*
2011 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2012 * already PF_EXITING could be freed from underneath us unless we
2013 * take an rcu_read_lock.
2014 */
2015 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002016 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002017 struct task_and_cgroup ent;
2018
Tejun Heocd3d0952011-12-12 18:12:21 -08002019 /* @tsk either already exited or can't exit until the end */
2020 if (tsk->flags & PF_EXITING)
2021 continue;
2022
Ben Blum74a11662011-05-26 16:25:20 -07002023 /* as per above, nr_threads may decrease, but not increase. */
2024 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002025 ent.task = tsk;
2026 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002027 /* nothing to do if this task is already in the cgroup */
2028 if (ent.cgrp == cgrp)
2029 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002030 /*
2031 * saying GFP_ATOMIC has no effect here because we did prealloc
2032 * earlier, but it's good form to communicate our expectations.
2033 */
Tejun Heo134d3372011-12-12 18:12:21 -08002034 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002035 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002036 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002037
2038 if (!threadgroup)
2039 break;
Ben Blum74a11662011-05-26 16:25:20 -07002040 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002041 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002042 /* remember the number of threads in the array for later. */
2043 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002044 tset.tc_array = group;
2045 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002046
Tejun Heo134d3372011-12-12 18:12:21 -08002047 /* methods shouldn't be called if no task is actually migrating */
2048 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002049 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002050 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002051
Ben Blum74a11662011-05-26 16:25:20 -07002052 /*
2053 * step 1: check that we can legitimately attach to the cgroup.
2054 */
Tejun Heo5549c492013-06-24 15:21:48 -07002055 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002056 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2057
Ben Blum74a11662011-05-26 16:25:20 -07002058 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002059 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002060 if (retval) {
2061 failed_ss = ss;
2062 goto out_cancel_attach;
2063 }
2064 }
Ben Blum74a11662011-05-26 16:25:20 -07002065 }
2066
2067 /*
2068 * step 2: make sure css_sets exist for all threads to be migrated.
2069 * we use find_css_set, which allocates a new one if necessary.
2070 */
Ben Blum74a11662011-05-26 16:25:20 -07002071 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002072 struct css_set *old_cset;
2073
Tejun Heo134d3372011-12-12 18:12:21 -08002074 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002075 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002076 tc->cset = find_css_set(old_cset, cgrp);
2077 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002078 retval = -ENOMEM;
2079 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002080 }
2081 }
2082
2083 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002084 * step 3: now that we're guaranteed success wrt the css_sets,
2085 * proceed to move all tasks to the new cgroup. There are no
2086 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002087 */
Ben Blum74a11662011-05-26 16:25:20 -07002088 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002089 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002090 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002091 }
2092 /* nothing is sensitive to fork() after this point. */
2093
2094 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002095 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002096 */
Tejun Heo5549c492013-06-24 15:21:48 -07002097 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002098 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2099
Ben Blum74a11662011-05-26 16:25:20 -07002100 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002101 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002102 }
2103
2104 /*
2105 * step 5: success! and cleanup
2106 */
Ben Blum74a11662011-05-26 16:25:20 -07002107 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002108out_put_css_set_refs:
2109 if (retval) {
2110 for (i = 0; i < group_size; i++) {
2111 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002112 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002113 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002114 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002115 }
Ben Blum74a11662011-05-26 16:25:20 -07002116 }
2117out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002118 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002119 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002120 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2121
Tejun Heo494c1672011-12-12 18:12:22 -08002122 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002123 break;
Ben Blum74a11662011-05-26 16:25:20 -07002124 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002125 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002126 }
2127 }
Ben Blum74a11662011-05-26 16:25:20 -07002128out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002129 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002130 return retval;
2131}
2132
2133/*
2134 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002135 * function to attach either it or all tasks in its threadgroup. Will lock
2136 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002137 */
2138static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002139{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002140 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002141 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002142 int ret;
2143
Ben Blum74a11662011-05-26 16:25:20 -07002144 if (!cgroup_lock_live_group(cgrp))
2145 return -ENODEV;
2146
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002147retry_find_task:
2148 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002149 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002150 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002151 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002152 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002153 ret= -ESRCH;
2154 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002155 }
Ben Blum74a11662011-05-26 16:25:20 -07002156 /*
2157 * even if we're attaching all tasks in the thread group, we
2158 * only need to check permissions on one of them.
2159 */
David Howellsc69e8d92008-11-14 10:39:19 +11002160 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002161 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2162 !uid_eq(cred->euid, tcred->uid) &&
2163 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002164 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002165 ret = -EACCES;
2166 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002167 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168 } else
2169 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002170
2171 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002173
2174 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002175 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002176 * trapped in a cpuset, or RT worker may be born in a cgroup
2177 * with no rt_runtime allocated. Just say no.
2178 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002179 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002180 ret = -EINVAL;
2181 rcu_read_unlock();
2182 goto out_unlock_cgroup;
2183 }
2184
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185 get_task_struct(tsk);
2186 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002187
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 threadgroup_lock(tsk);
2189 if (threadgroup) {
2190 if (!thread_group_leader(tsk)) {
2191 /*
2192 * a race with de_thread from another thread's exec()
2193 * may strip us of our leadership, if this happens,
2194 * there is no choice but to throw this task away and
2195 * try again; this is
2196 * "double-double-toil-and-trouble-check locking".
2197 */
2198 threadgroup_unlock(tsk);
2199 put_task_struct(tsk);
2200 goto retry_find_task;
2201 }
Li Zefan081aa452013-03-13 09:17:09 +08002202 }
2203
2204 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2205
Tejun Heocd3d0952011-12-12 18:12:21 -08002206 threadgroup_unlock(tsk);
2207
Paul Menagebbcb81d2007-10-18 23:39:32 -07002208 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002209out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002210 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002211 return ret;
2212}
2213
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002214/**
2215 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2216 * @from: attach to all cgroups of a given task
2217 * @tsk: the task to be attached
2218 */
2219int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2220{
2221 struct cgroupfs_root *root;
2222 int retval = 0;
2223
Tejun Heo47cfcd02013-04-07 09:29:51 -07002224 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002225 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002226 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002227
Li Zefan6f4b7e62013-07-31 16:18:36 +08002228 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002229 if (retval)
2230 break;
2231 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002232 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233
2234 return retval;
2235}
2236EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2237
Tejun Heo182446d2013-08-08 20:11:24 -04002238static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2239 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002240{
Tejun Heo182446d2013-08-08 20:11:24 -04002241 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002242}
2243
Tejun Heo182446d2013-08-08 20:11:24 -04002244static int cgroup_procs_write(struct cgroup_subsys_state *css,
2245 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002246{
Tejun Heo182446d2013-08-08 20:11:24 -04002247 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002248}
2249
Tejun Heo182446d2013-08-08 20:11:24 -04002250static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2251 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002252{
Tejun Heo182446d2013-08-08 20:11:24 -04002253 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002254 if (strlen(buffer) >= PATH_MAX)
2255 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002256 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002257 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002258 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002259 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002260 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002261 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002262 return 0;
2263}
2264
Tejun Heo182446d2013-08-08 20:11:24 -04002265static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2266 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002267{
Tejun Heo182446d2013-08-08 20:11:24 -04002268 struct cgroup *cgrp = css->cgroup;
2269
Paul Menagee788e062008-07-25 01:46:59 -07002270 if (!cgroup_lock_live_group(cgrp))
2271 return -ENODEV;
2272 seq_puts(seq, cgrp->root->release_agent_path);
2273 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002274 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002275 return 0;
2276}
2277
Tejun Heo182446d2013-08-08 20:11:24 -04002278static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2279 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002280{
Tejun Heo182446d2013-08-08 20:11:24 -04002281 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002282 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002283}
2284
Tejun Heof7d58812013-08-08 20:11:23 -04002285/* return the css for the given cgroup file */
2286static struct cgroup_subsys_state *cgroup_file_css(struct cfent *cfe)
2287{
2288 struct cftype *cft = cfe->type;
2289 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2290
2291 if (cft->ss)
2292 return cgrp->subsys[cft->ss->subsys_id];
Tejun Heo67f4c362013-08-08 20:11:24 -04002293 return &cgrp->dummy_css;
Tejun Heof7d58812013-08-08 20:11:23 -04002294}
2295
Paul Menage84eea842008-07-25 01:47:00 -07002296/* A buffer size big enough for numbers or short strings */
2297#define CGROUP_LOCAL_BUFFER_SIZE 64
2298
Tejun Heo182446d2013-08-08 20:11:24 -04002299static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2300 struct cftype *cft, struct file *file,
2301 const char __user *userbuf, size_t nbytes,
2302 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002303{
Paul Menage84eea842008-07-25 01:47:00 -07002304 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002305 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002306 char *end;
2307
2308 if (!nbytes)
2309 return -EINVAL;
2310 if (nbytes >= sizeof(buffer))
2311 return -E2BIG;
2312 if (copy_from_user(buffer, userbuf, nbytes))
2313 return -EFAULT;
2314
2315 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002316 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002317 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002318 if (*end)
2319 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002320 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002321 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002322 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002323 if (*end)
2324 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002325 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002326 }
Paul Menage355e0c42007-10-18 23:39:33 -07002327 if (!retval)
2328 retval = nbytes;
2329 return retval;
2330}
2331
Tejun Heo182446d2013-08-08 20:11:24 -04002332static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2333 struct cftype *cft, struct file *file,
2334 const char __user *userbuf, size_t nbytes,
2335 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002336{
Paul Menage84eea842008-07-25 01:47:00 -07002337 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002338 int retval = 0;
2339 size_t max_bytes = cft->max_write_len;
2340 char *buffer = local_buffer;
2341
2342 if (!max_bytes)
2343 max_bytes = sizeof(local_buffer) - 1;
2344 if (nbytes >= max_bytes)
2345 return -E2BIG;
2346 /* Allocate a dynamic buffer if we need one */
2347 if (nbytes >= sizeof(local_buffer)) {
2348 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2349 if (buffer == NULL)
2350 return -ENOMEM;
2351 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002352 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2353 retval = -EFAULT;
2354 goto out;
2355 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002356
2357 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002358 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002359 if (!retval)
2360 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002361out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002362 if (buffer != local_buffer)
2363 kfree(buffer);
2364 return retval;
2365}
2366
Paul Menageddbcc7e2007-10-18 23:39:30 -07002367static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002368 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002369{
Tejun Heo182446d2013-08-08 20:11:24 -04002370 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002371 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002372 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002373
Paul Menage355e0c42007-10-18 23:39:33 -07002374 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002375 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002376 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002377 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002378 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002379 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002380 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002381 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002382 return ret ? ret : nbytes;
2383 }
Paul Menage355e0c42007-10-18 23:39:33 -07002384 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002385}
2386
Tejun Heo182446d2013-08-08 20:11:24 -04002387static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2388 struct cftype *cft, struct file *file,
2389 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390{
Paul Menage84eea842008-07-25 01:47:00 -07002391 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002392 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002393 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2394
2395 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2396}
2397
Tejun Heo182446d2013-08-08 20:11:24 -04002398static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2399 struct cftype *cft, struct file *file,
2400 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002401{
Paul Menage84eea842008-07-25 01:47:00 -07002402 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002403 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002404 int len = sprintf(tmp, "%lld\n", (long long) val);
2405
2406 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2407}
2408
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002410 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002411{
Tejun Heo182446d2013-08-08 20:11:24 -04002412 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002413 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002414 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002415
Paul Menageddbcc7e2007-10-18 23:39:30 -07002416 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002417 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002418 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002419 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002420 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002421 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422 return -EINVAL;
2423}
2424
Paul Menage91796562008-04-29 01:00:01 -07002425/*
2426 * seqfile ops/methods for returning structured data. Currently just
2427 * supports string->u64 maps, but can be extended in future.
2428 */
2429
Paul Menage91796562008-04-29 01:00:01 -07002430static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2431{
2432 struct seq_file *sf = cb->state;
2433 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2434}
2435
2436static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2437{
Li Zefane0798ce2013-07-31 17:36:25 +08002438 struct cfent *cfe = m->private;
2439 struct cftype *cft = cfe->type;
Tejun Heo182446d2013-08-08 20:11:24 -04002440 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Li Zefane0798ce2013-07-31 17:36:25 +08002441
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002442 if (cft->read_map) {
2443 struct cgroup_map_cb cb = {
2444 .fill = cgroup_map_add,
2445 .state = m,
2446 };
Tejun Heo182446d2013-08-08 20:11:24 -04002447 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002448 }
Tejun Heo182446d2013-08-08 20:11:24 -04002449 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002450}
2451
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002452static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002453 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002454 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002455 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002456 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002457};
2458
Paul Menageddbcc7e2007-10-18 23:39:30 -07002459static int cgroup_file_open(struct inode *inode, struct file *file)
2460{
Tejun Heof7d58812013-08-08 20:11:23 -04002461 struct cfent *cfe = __d_cfe(file->f_dentry);
2462 struct cftype *cft = __d_cft(file->f_dentry);
2463 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002464 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002465
2466 err = generic_file_open(inode, file);
2467 if (err)
2468 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002469
2470 /*
2471 * If the file belongs to a subsystem, pin the css. Will be
2472 * unpinned either on open failure or release. This ensures that
2473 * @css stays alive for all file operations.
2474 */
Tejun Heo67f4c362013-08-08 20:11:24 -04002475 if (css->ss && !css_tryget(css))
Tejun Heof7d58812013-08-08 20:11:23 -04002476 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002477
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002478 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002479 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002480 err = single_open(file, cgroup_seqfile_show, cfe);
2481 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002482 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002483 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484
Tejun Heo67f4c362013-08-08 20:11:24 -04002485 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002486 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002487 return err;
2488}
2489
2490static int cgroup_file_release(struct inode *inode, struct file *file)
2491{
Tejun Heof7d58812013-08-08 20:11:23 -04002492 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002493 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heof7d58812013-08-08 20:11:23 -04002494 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
2495 int ret = 0;
2496
Paul Menageddbcc7e2007-10-18 23:39:30 -07002497 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002498 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002499 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002500 css_put(css);
2501 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502}
2503
2504/*
2505 * cgroup_rename - Only allow simple rename of directories in place.
2506 */
2507static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2508 struct inode *new_dir, struct dentry *new_dentry)
2509{
Li Zefan65dff752013-03-01 15:01:56 +08002510 int ret;
2511 struct cgroup_name *name, *old_name;
2512 struct cgroup *cgrp;
2513
2514 /*
2515 * It's convinient to use parent dir's i_mutex to protected
2516 * cgrp->name.
2517 */
2518 lockdep_assert_held(&old_dir->i_mutex);
2519
Paul Menageddbcc7e2007-10-18 23:39:30 -07002520 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2521 return -ENOTDIR;
2522 if (new_dentry->d_inode)
2523 return -EEXIST;
2524 if (old_dir != new_dir)
2525 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002526
2527 cgrp = __d_cgrp(old_dentry);
2528
Tejun Heo6db8e852013-06-14 11:18:22 -07002529 /*
2530 * This isn't a proper migration and its usefulness is very
2531 * limited. Disallow if sane_behavior.
2532 */
2533 if (cgroup_sane_behavior(cgrp))
2534 return -EPERM;
2535
Li Zefan65dff752013-03-01 15:01:56 +08002536 name = cgroup_alloc_name(new_dentry);
2537 if (!name)
2538 return -ENOMEM;
2539
2540 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2541 if (ret) {
2542 kfree(name);
2543 return ret;
2544 }
2545
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002546 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002547 rcu_assign_pointer(cgrp->name, name);
2548
2549 kfree_rcu(old_name, rcu_head);
2550 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002551}
2552
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002553static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2554{
2555 if (S_ISDIR(dentry->d_inode->i_mode))
2556 return &__d_cgrp(dentry)->xattrs;
2557 else
Li Zefan712317a2013-04-18 23:09:52 -07002558 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002559}
2560
2561static inline int xattr_enabled(struct dentry *dentry)
2562{
2563 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002564 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002565}
2566
2567static bool is_valid_xattr(const char *name)
2568{
2569 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2570 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2571 return true;
2572 return false;
2573}
2574
2575static int cgroup_setxattr(struct dentry *dentry, const char *name,
2576 const void *val, size_t size, int flags)
2577{
2578 if (!xattr_enabled(dentry))
2579 return -EOPNOTSUPP;
2580 if (!is_valid_xattr(name))
2581 return -EINVAL;
2582 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2583}
2584
2585static int cgroup_removexattr(struct dentry *dentry, const char *name)
2586{
2587 if (!xattr_enabled(dentry))
2588 return -EOPNOTSUPP;
2589 if (!is_valid_xattr(name))
2590 return -EINVAL;
2591 return simple_xattr_remove(__d_xattrs(dentry), name);
2592}
2593
2594static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2595 void *buf, size_t size)
2596{
2597 if (!xattr_enabled(dentry))
2598 return -EOPNOTSUPP;
2599 if (!is_valid_xattr(name))
2600 return -EINVAL;
2601 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2602}
2603
2604static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2605{
2606 if (!xattr_enabled(dentry))
2607 return -EOPNOTSUPP;
2608 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2609}
2610
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002611static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002612 .read = cgroup_file_read,
2613 .write = cgroup_file_write,
2614 .llseek = generic_file_llseek,
2615 .open = cgroup_file_open,
2616 .release = cgroup_file_release,
2617};
2618
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002619static const struct inode_operations cgroup_file_inode_operations = {
2620 .setxattr = cgroup_setxattr,
2621 .getxattr = cgroup_getxattr,
2622 .listxattr = cgroup_listxattr,
2623 .removexattr = cgroup_removexattr,
2624};
2625
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002626static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002627 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002628 .mkdir = cgroup_mkdir,
2629 .rmdir = cgroup_rmdir,
2630 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002631 .setxattr = cgroup_setxattr,
2632 .getxattr = cgroup_getxattr,
2633 .listxattr = cgroup_listxattr,
2634 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002635};
2636
Al Viro00cd8dd2012-06-10 17:13:09 -04002637static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002638{
2639 if (dentry->d_name.len > NAME_MAX)
2640 return ERR_PTR(-ENAMETOOLONG);
2641 d_add(dentry, NULL);
2642 return NULL;
2643}
2644
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002645/*
2646 * Check if a file is a control file
2647 */
2648static inline struct cftype *__file_cft(struct file *file)
2649{
Al Viro496ad9a2013-01-23 17:07:38 -05002650 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002651 return ERR_PTR(-EINVAL);
2652 return __d_cft(file->f_dentry);
2653}
2654
Al Viroa5e7ed32011-07-26 01:55:55 -04002655static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002656 struct super_block *sb)
2657{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002658 struct inode *inode;
2659
2660 if (!dentry)
2661 return -ENOENT;
2662 if (dentry->d_inode)
2663 return -EEXIST;
2664
2665 inode = cgroup_new_inode(mode, sb);
2666 if (!inode)
2667 return -ENOMEM;
2668
2669 if (S_ISDIR(mode)) {
2670 inode->i_op = &cgroup_dir_inode_operations;
2671 inode->i_fop = &simple_dir_operations;
2672
2673 /* start off with i_nlink == 2 (for "." entry) */
2674 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002675 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676
Tejun Heob8a2df62012-11-19 08:13:37 -08002677 /*
2678 * Control reaches here with cgroup_mutex held.
2679 * @inode->i_mutex should nest outside cgroup_mutex but we
2680 * want to populate it immediately without releasing
2681 * cgroup_mutex. As @inode isn't visible to anyone else
2682 * yet, trylock will always succeed without affecting
2683 * lockdep checks.
2684 */
2685 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002686 } else if (S_ISREG(mode)) {
2687 inode->i_size = 0;
2688 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002689 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002690 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002691 d_instantiate(dentry, inode);
2692 dget(dentry); /* Extra count - pin the dentry in core */
2693 return 0;
2694}
2695
Li Zefan099fca32009-04-02 16:57:29 -07002696/**
2697 * cgroup_file_mode - deduce file mode of a control file
2698 * @cft: the control file in question
2699 *
2700 * returns cft->mode if ->mode is not 0
2701 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2702 * returns S_IRUGO if it has only a read handler
2703 * returns S_IWUSR if it has only a write hander
2704 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002705static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002706{
Al Viroa5e7ed32011-07-26 01:55:55 -04002707 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002708
2709 if (cft->mode)
2710 return cft->mode;
2711
2712 if (cft->read || cft->read_u64 || cft->read_s64 ||
2713 cft->read_map || cft->read_seq_string)
2714 mode |= S_IRUGO;
2715
2716 if (cft->write || cft->write_u64 || cft->write_s64 ||
2717 cft->write_string || cft->trigger)
2718 mode |= S_IWUSR;
2719
2720 return mode;
2721}
2722
Tejun Heo2bb566c2013-08-08 20:11:23 -04002723static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002724{
Paul Menagebd89aab2007-10-18 23:40:44 -07002725 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002726 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002727 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002728 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002729 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002730 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002731 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002732
Tejun Heo2bb566c2013-08-08 20:11:23 -04002733 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2734 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002735 strcat(name, ".");
2736 }
2737 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002738
Paul Menageddbcc7e2007-10-18 23:39:30 -07002739 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002740
2741 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2742 if (!cfe)
2743 return -ENOMEM;
2744
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002746 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002748 goto out;
2749 }
2750
Li Zefand6cbf352013-05-14 19:44:20 +08002751 cfe->type = (void *)cft;
2752 cfe->dentry = dentry;
2753 dentry->d_fsdata = cfe;
2754 simple_xattrs_init(&cfe->xattrs);
2755
Tejun Heo05ef1d72012-04-01 12:09:56 -07002756 mode = cgroup_file_mode(cft);
2757 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2758 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002759 list_add_tail(&cfe->node, &parent->files);
2760 cfe = NULL;
2761 }
2762 dput(dentry);
2763out:
2764 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002765 return error;
2766}
2767
Tejun Heob1f28d32013-06-28 16:24:10 -07002768/**
2769 * cgroup_addrm_files - add or remove files to a cgroup directory
2770 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002771 * @cfts: array of cftypes to be added
2772 * @is_add: whether to add or remove
2773 *
2774 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002775 * For removals, this function never fails. If addition fails, this
2776 * function doesn't remove files already added. The caller is responsible
2777 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002778 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002779static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2780 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002781{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002782 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002783 int ret;
2784
2785 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2786 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002787
2788 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002789 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002790 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2791 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002792 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2793 continue;
2794 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2795 continue;
2796
Li Zefan2739d3c2013-01-21 18:18:33 +08002797 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002798 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002799 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002800 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002801 cft->name, ret);
2802 return ret;
2803 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002804 } else {
2805 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002806 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002807 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002808 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002809}
2810
Tejun Heo8e3f6542012-04-01 12:09:55 -07002811static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002812 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002813{
2814 /*
2815 * Thanks to the entanglement with vfs inode locking, we can't walk
2816 * the existing cgroups under cgroup_mutex and create files.
Li Zefane8c82d22013-06-18 18:48:37 +08002817 * Instead, we use cgroup_for_each_descendant_pre() and drop RCU
2818 * read lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002819 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002820 mutex_lock(&cgroup_mutex);
2821}
2822
Tejun Heo2bb566c2013-08-08 20:11:23 -04002823static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002824 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002825{
2826 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002827 struct cgroup_subsys *ss = cfts[0].ss;
Li Zefane8c82d22013-06-18 18:48:37 +08002828 struct cgroup *cgrp, *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002829 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002830 struct dentry *prev = NULL;
2831 struct inode *inode;
Tejun Heo00356bd2013-06-18 11:14:22 -07002832 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002833 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002834
2835 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002836 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002837 !atomic_inc_not_zero(&sb->s_active)) {
2838 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002839 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840 }
2841
Li Zefane8c82d22013-06-18 18:48:37 +08002842 /*
2843 * All cgroups which are created after we drop cgroup_mutex will
2844 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002845 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002846 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002847 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002848
Tejun Heo8e3f6542012-04-01 12:09:55 -07002849 mutex_unlock(&cgroup_mutex);
2850
Li Zefane8c82d22013-06-18 18:48:37 +08002851 /* @root always needs to be updated */
2852 inode = root->dentry->d_inode;
2853 mutex_lock(&inode->i_mutex);
2854 mutex_lock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002855 ret = cgroup_addrm_files(root, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002856 mutex_unlock(&cgroup_mutex);
2857 mutex_unlock(&inode->i_mutex);
2858
Tejun Heo9ccece82013-06-28 16:24:11 -07002859 if (ret)
2860 goto out_deact;
2861
Li Zefane8c82d22013-06-18 18:48:37 +08002862 /* add/rm files for all cgroups created before */
2863 rcu_read_lock();
2864 cgroup_for_each_descendant_pre(cgrp, root) {
2865 if (cgroup_is_dead(cgrp))
2866 continue;
2867
2868 inode = cgrp->dentry->d_inode;
2869 dget(cgrp->dentry);
2870 rcu_read_unlock();
2871
2872 dput(prev);
2873 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002874
2875 mutex_lock(&inode->i_mutex);
2876 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002877 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002878 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002879 mutex_unlock(&cgroup_mutex);
2880 mutex_unlock(&inode->i_mutex);
2881
Li Zefane8c82d22013-06-18 18:48:37 +08002882 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002883 if (ret)
2884 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885 }
Li Zefane8c82d22013-06-18 18:48:37 +08002886 rcu_read_unlock();
2887 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002888out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002889 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002890 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002891}
2892
2893/**
2894 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2895 * @ss: target cgroup subsystem
2896 * @cfts: zero-length name terminated array of cftypes
2897 *
2898 * Register @cfts to @ss. Files described by @cfts are created for all
2899 * existing cgroups to which @ss is attached and all future cgroups will
2900 * have them too. This function can be called anytime whether @ss is
2901 * attached or not.
2902 *
2903 * Returns 0 on successful registration, -errno on failure. Note that this
2904 * function currently returns 0 as long as @cfts registration is successful
2905 * even if some file creation attempts on existing cgroups fail.
2906 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002907int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002908{
2909 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002910 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002911 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002912
2913 set = kzalloc(sizeof(*set), GFP_KERNEL);
2914 if (!set)
2915 return -ENOMEM;
2916
Tejun Heo2bb566c2013-08-08 20:11:23 -04002917 for (cft = cfts; cft->name[0] != '\0'; cft++)
2918 cft->ss = ss;
2919
Tejun Heo8e3f6542012-04-01 12:09:55 -07002920 cgroup_cfts_prepare();
2921 set->cfts = cfts;
2922 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002923 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002924 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002925 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002926 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002927}
2928EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2929
Li Zefana043e3b2008-02-23 15:24:09 -08002930/**
Tejun Heo79578622012-04-01 12:09:56 -07002931 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002932 * @cfts: zero-length name terminated array of cftypes
2933 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002934 * Unregister @cfts. Files described by @cfts are removed from all
2935 * existing cgroups and all future cgroups won't have them either. This
2936 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002937 *
2938 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002939 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002940 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002941int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002942{
2943 struct cftype_set *set;
2944
Tejun Heo2bb566c2013-08-08 20:11:23 -04002945 if (!cfts || !cfts[0].ss)
2946 return -ENOENT;
2947
Tejun Heo79578622012-04-01 12:09:56 -07002948 cgroup_cfts_prepare();
2949
Tejun Heo2bb566c2013-08-08 20:11:23 -04002950 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002951 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002952 list_del(&set->node);
2953 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002954 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002955 return 0;
2956 }
2957 }
2958
Tejun Heo2bb566c2013-08-08 20:11:23 -04002959 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002960 return -ENOENT;
2961}
2962
2963/**
Li Zefana043e3b2008-02-23 15:24:09 -08002964 * cgroup_task_count - count the number of tasks in a cgroup.
2965 * @cgrp: the cgroup in question
2966 *
2967 * Return the number of tasks in the cgroup.
2968 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002969int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002970{
2971 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002972 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002973
Paul Menage817929e2007-10-18 23:39:36 -07002974 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002975 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2976 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002977 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002978 return count;
2979}
2980
2981/*
Paul Menage817929e2007-10-18 23:39:36 -07002982 * Advance a list_head iterator. The iterator should be positioned at
2983 * the start of a css_set
2984 */
Tejun Heo69d02062013-06-12 21:04:50 -07002985static void cgroup_advance_iter(struct cgroup *cgrp, struct cgroup_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07002986{
Tejun Heo69d02062013-06-12 21:04:50 -07002987 struct list_head *l = it->cset_link;
2988 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07002989 struct css_set *cset;
Paul Menage817929e2007-10-18 23:39:36 -07002990
2991 /* Advance to the next non-empty css_set */
2992 do {
2993 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07002994 if (l == &cgrp->cset_links) {
2995 it->cset_link = NULL;
Paul Menage817929e2007-10-18 23:39:36 -07002996 return;
2997 }
Tejun Heo69d02062013-06-12 21:04:50 -07002998 link = list_entry(l, struct cgrp_cset_link, cset_link);
2999 cset = link->cset;
Tejun Heo5abb8852013-06-12 21:04:49 -07003000 } while (list_empty(&cset->tasks));
Tejun Heo69d02062013-06-12 21:04:50 -07003001 it->cset_link = l;
Tejun Heo5abb8852013-06-12 21:04:49 -07003002 it->task = cset->tasks.next;
Paul Menage817929e2007-10-18 23:39:36 -07003003}
3004
Cliff Wickman31a7df02008-02-07 00:14:42 -08003005/*
3006 * To reduce the fork() overhead for systems that are not actually
3007 * using their cgroups capability, we don't maintain the lists running
3008 * through each css_set to its tasks until we see the list actually
3009 * used - in other words after the first call to cgroup_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003011static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003012{
3013 struct task_struct *p, *g;
3014 write_lock(&css_set_lock);
3015 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003016 /*
3017 * We need tasklist_lock because RCU is not safe against
3018 * while_each_thread(). Besides, a forking task that has passed
3019 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3020 * is not guaranteed to have its child immediately visible in the
3021 * tasklist if we walk through it with RCU.
3022 */
3023 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003024 do_each_thread(g, p) {
3025 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003026 /*
3027 * We should check if the process is exiting, otherwise
3028 * it will race with cgroup_exit() in that the list
3029 * entry won't be deleted though the process has exited.
3030 */
3031 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003032 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003033 task_unlock(p);
3034 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003035 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003036 write_unlock(&css_set_lock);
3037}
3038
Tejun Heo574bd9f2012-11-09 09:12:29 -08003039/**
Tejun Heo3b287a52013-08-08 20:11:24 -04003040 * cgroup_next_child - find the next child of a given cgroup
3041 * @pos: the current position (%NULL to initiate traversal)
3042 * @cgrp: cgroup whose descendants to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003043 *
Tejun Heo3b287a52013-08-08 20:11:24 -04003044 * This function returns the next child of @cgrp and should be called under
3045 * RCU read lock. The only requirement is that @cgrp and @pos are
3046 * accessible. The next sibling is guaranteed to be returned regardless of
3047 * their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003048 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003049struct cgroup *cgroup_next_child(struct cgroup *pos, struct cgroup *cgrp)
Tejun Heo53fa5262013-05-24 10:55:38 +09003050{
3051 struct cgroup *next;
3052
3053 WARN_ON_ONCE(!rcu_read_lock_held());
3054
3055 /*
3056 * @pos could already have been removed. Once a cgroup is removed,
3057 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003058 * changes. As CGRP_DEAD assertion is serialized and happens
3059 * before the cgroup is taken off the ->sibling list, if we see it
3060 * unasserted, it's guaranteed that the next sibling hasn't
3061 * finished its grace period even if it's already removed, and thus
3062 * safe to dereference from this RCU critical section. If
3063 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3064 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003065 *
3066 * If @pos is dead, its next pointer can't be dereferenced;
3067 * however, as each cgroup is given a monotonically increasing
3068 * unique serial number and always appended to the sibling list,
3069 * the next one can be found by walking the parent's children until
3070 * we see a cgroup with higher serial number than @pos's. While
3071 * this path can be slower, it's taken only when either the current
3072 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003073 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003074 if (!pos) {
3075 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3076 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003077 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003078 } else {
3079 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3080 if (next->serial_nr > pos->serial_nr)
3081 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003082 }
3083
Tejun Heo3b287a52013-08-08 20:11:24 -04003084 if (&next->sibling != &cgrp->children)
3085 return next;
Tejun Heo53fa5262013-05-24 10:55:38 +09003086 return NULL;
3087}
Tejun Heo3b287a52013-08-08 20:11:24 -04003088EXPORT_SYMBOL_GPL(cgroup_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003089
3090/**
Tejun Heo574bd9f2012-11-09 09:12:29 -08003091 * cgroup_next_descendant_pre - find the next descendant for pre-order walk
3092 * @pos: the current position (%NULL to initiate traversal)
3093 * @cgroup: cgroup whose descendants to walk
3094 *
3095 * To be used by cgroup_for_each_descendant_pre(). Find the next
3096 * descendant to visit for pre-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003097 *
3098 * While this function requires RCU read locking, it doesn't require the
3099 * whole traversal to be contained in a single RCU critical section. This
3100 * function will return the correct next descendant as long as both @pos
3101 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003102 */
3103struct cgroup *cgroup_next_descendant_pre(struct cgroup *pos,
3104 struct cgroup *cgroup)
3105{
3106 struct cgroup *next;
3107
3108 WARN_ON_ONCE(!rcu_read_lock_held());
3109
3110 /* if first iteration, pretend we just visited @cgroup */
Tejun Heo7805d002013-05-24 10:50:24 +09003111 if (!pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112 pos = cgroup;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003113
3114 /* visit the first child if exists */
3115 next = list_first_or_null_rcu(&pos->children, struct cgroup, sibling);
3116 if (next)
3117 return next;
3118
3119 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo7805d002013-05-24 10:50:24 +09003120 while (pos != cgroup) {
Tejun Heo3b287a52013-08-08 20:11:24 -04003121 next = cgroup_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003122 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123 return next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003124 pos = pos->parent;
Tejun Heo7805d002013-05-24 10:50:24 +09003125 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003126
3127 return NULL;
3128}
3129EXPORT_SYMBOL_GPL(cgroup_next_descendant_pre);
3130
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003131/**
3132 * cgroup_rightmost_descendant - return the rightmost descendant of a cgroup
3133 * @pos: cgroup of interest
3134 *
3135 * Return the rightmost descendant of @pos. If there's no descendant,
3136 * @pos is returned. This can be used during pre-order traversal to skip
3137 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003138 *
3139 * While this function requires RCU read locking, it doesn't require the
3140 * whole traversal to be contained in a single RCU critical section. This
3141 * function will return the correct rightmost descendant as long as @pos is
3142 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003143 */
3144struct cgroup *cgroup_rightmost_descendant(struct cgroup *pos)
3145{
3146 struct cgroup *last, *tmp;
3147
3148 WARN_ON_ONCE(!rcu_read_lock_held());
3149
3150 do {
3151 last = pos;
3152 /* ->prev isn't RCU safe, walk ->next till the end */
3153 pos = NULL;
3154 list_for_each_entry_rcu(tmp, &last->children, sibling)
3155 pos = tmp;
3156 } while (pos);
3157
3158 return last;
3159}
3160EXPORT_SYMBOL_GPL(cgroup_rightmost_descendant);
3161
Tejun Heo574bd9f2012-11-09 09:12:29 -08003162static struct cgroup *cgroup_leftmost_descendant(struct cgroup *pos)
3163{
3164 struct cgroup *last;
3165
3166 do {
3167 last = pos;
3168 pos = list_first_or_null_rcu(&pos->children, struct cgroup,
3169 sibling);
3170 } while (pos);
3171
3172 return last;
3173}
3174
3175/**
3176 * cgroup_next_descendant_post - find the next descendant for post-order walk
3177 * @pos: the current position (%NULL to initiate traversal)
3178 * @cgroup: cgroup whose descendants to walk
3179 *
3180 * To be used by cgroup_for_each_descendant_post(). Find the next
3181 * descendant to visit for post-order traversal of @cgroup's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003182 *
3183 * While this function requires RCU read locking, it doesn't require the
3184 * whole traversal to be contained in a single RCU critical section. This
3185 * function will return the correct next descendant as long as both @pos
3186 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003187 */
3188struct cgroup *cgroup_next_descendant_post(struct cgroup *pos,
3189 struct cgroup *cgroup)
3190{
3191 struct cgroup *next;
3192
3193 WARN_ON_ONCE(!rcu_read_lock_held());
3194
3195 /* if first iteration, visit the leftmost descendant */
3196 if (!pos) {
3197 next = cgroup_leftmost_descendant(cgroup);
3198 return next != cgroup ? next : NULL;
3199 }
3200
3201 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo3b287a52013-08-08 20:11:24 -04003202 next = cgroup_next_child(pos, pos->parent);
Tejun Heo75501a62013-05-24 10:55:38 +09003203 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003204 return cgroup_leftmost_descendant(next);
3205
3206 /* no sibling left, visit parent */
3207 next = pos->parent;
3208 return next != cgroup ? next : NULL;
3209}
3210EXPORT_SYMBOL_GPL(cgroup_next_descendant_post);
3211
Paul Menagebd89aab2007-10-18 23:40:44 -07003212void cgroup_iter_start(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003213 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003214{
3215 /*
3216 * The first time anyone tries to iterate across a cgroup,
3217 * we need to enable the list linking each css_set to its
3218 * tasks, and fix up all existing tasks.
3219 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003220 if (!use_task_css_set_links)
3221 cgroup_enable_task_cg_lists();
3222
Paul Menage817929e2007-10-18 23:39:36 -07003223 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07003224 it->cset_link = &cgrp->cset_links;
Paul Menagebd89aab2007-10-18 23:40:44 -07003225 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003226}
3227
Paul Menagebd89aab2007-10-18 23:40:44 -07003228struct task_struct *cgroup_iter_next(struct cgroup *cgrp,
Paul Menage817929e2007-10-18 23:39:36 -07003229 struct cgroup_iter *it)
3230{
3231 struct task_struct *res;
3232 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003233 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003234
3235 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003236 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003237 return NULL;
3238 res = list_entry(l, struct task_struct, cg_list);
3239 /* Advance iterator to find next entry */
3240 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003241 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3242 if (l == &link->cset->tasks) {
Paul Menage817929e2007-10-18 23:39:36 -07003243 /* We reached the end of this task list - move on to
3244 * the next cg_cgroup_link */
Paul Menagebd89aab2007-10-18 23:40:44 -07003245 cgroup_advance_iter(cgrp, it);
Paul Menage817929e2007-10-18 23:39:36 -07003246 } else {
3247 it->task = l;
3248 }
3249 return res;
3250}
3251
Paul Menagebd89aab2007-10-18 23:40:44 -07003252void cgroup_iter_end(struct cgroup *cgrp, struct cgroup_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003253 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003254{
3255 read_unlock(&css_set_lock);
3256}
3257
Cliff Wickman31a7df02008-02-07 00:14:42 -08003258static inline int started_after_time(struct task_struct *t1,
3259 struct timespec *time,
3260 struct task_struct *t2)
3261{
3262 int start_diff = timespec_compare(&t1->start_time, time);
3263 if (start_diff > 0) {
3264 return 1;
3265 } else if (start_diff < 0) {
3266 return 0;
3267 } else {
3268 /*
3269 * Arbitrarily, if two processes started at the same
3270 * time, we'll say that the lower pointer value
3271 * started first. Note that t2 may have exited by now
3272 * so this may not be a valid pointer any longer, but
3273 * that's fine - it still serves to distinguish
3274 * between two tasks started (effectively) simultaneously.
3275 */
3276 return t1 > t2;
3277 }
3278}
3279
3280/*
3281 * This function is a callback from heap_insert() and is used to order
3282 * the heap.
3283 * In this case we order the heap in descending task start time.
3284 */
3285static inline int started_after(void *p1, void *p2)
3286{
3287 struct task_struct *t1 = p1;
3288 struct task_struct *t2 = p2;
3289 return started_after_time(t1, &t2->start_time, t2);
3290}
3291
3292/**
3293 * cgroup_scan_tasks - iterate though all the tasks in a cgroup
3294 * @scan: struct cgroup_scanner containing arguments for the scan
3295 *
3296 * Arguments include pointers to callback functions test_task() and
3297 * process_task().
3298 * Iterate through all the tasks in a cgroup, calling test_task() for each,
3299 * and if it returns true, call process_task() for it also.
3300 * The test_task pointer may be NULL, meaning always true (select all tasks).
3301 * Effectively duplicates cgroup_iter_{start,next,end}()
3302 * but does not lock css_set_lock for the call to process_task().
3303 * The struct cgroup_scanner may be embedded in any structure of the caller's
3304 * creation.
3305 * It is guaranteed that process_task() will act on every task that
3306 * is a member of the cgroup for the duration of this call. This
3307 * function may or may not call process_task() for tasks that exit
3308 * or move to a different cgroup during the call, or are forked or
3309 * move into the cgroup during the call.
3310 *
3311 * Note that test_task() may be called with locks held, and may in some
3312 * situations be called multiple times for the same task, so it should
3313 * be cheap.
3314 * If the heap pointer in the struct cgroup_scanner is non-NULL, a heap has been
3315 * pre-allocated and will be used for heap operations (and its "gt" member will
3316 * be overwritten), else a temporary heap will be used (allocation of which
3317 * may cause this function to fail).
3318 */
3319int cgroup_scan_tasks(struct cgroup_scanner *scan)
3320{
3321 int retval, i;
3322 struct cgroup_iter it;
3323 struct task_struct *p, *dropped;
3324 /* Never dereference latest_task, since it's not refcounted */
3325 struct task_struct *latest_task = NULL;
3326 struct ptr_heap tmp_heap;
3327 struct ptr_heap *heap;
3328 struct timespec latest_time = { 0, 0 };
3329
3330 if (scan->heap) {
3331 /* The caller supplied our heap and pre-allocated its memory */
3332 heap = scan->heap;
3333 heap->gt = &started_after;
3334 } else {
3335 /* We need to allocate our own heap memory */
3336 heap = &tmp_heap;
3337 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3338 if (retval)
3339 /* cannot allocate the heap */
3340 return retval;
3341 }
3342
3343 again:
3344 /*
3345 * Scan tasks in the cgroup, using the scanner's "test_task" callback
3346 * to determine which are of interest, and using the scanner's
3347 * "process_task" callback to process any of them that need an update.
3348 * Since we don't want to hold any locks during the task updates,
3349 * gather tasks to be processed in a heap structure.
3350 * The heap is sorted by descending task start time.
3351 * If the statically-sized heap fills up, we overflow tasks that
3352 * started later, and in future iterations only consider tasks that
3353 * started after the latest task in the previous pass. This
3354 * guarantees forward progress and that we don't miss any tasks.
3355 */
3356 heap->size = 0;
Li Zefan6f4b7e62013-07-31 16:18:36 +08003357 cgroup_iter_start(scan->cgrp, &it);
3358 while ((p = cgroup_iter_next(scan->cgrp, &it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003359 /*
3360 * Only affect tasks that qualify per the caller's callback,
3361 * if he provided one
3362 */
3363 if (scan->test_task && !scan->test_task(p, scan))
3364 continue;
3365 /*
3366 * Only process tasks that started after the last task
3367 * we processed
3368 */
3369 if (!started_after_time(p, &latest_time, latest_task))
3370 continue;
3371 dropped = heap_insert(heap, p);
3372 if (dropped == NULL) {
3373 /*
3374 * The new task was inserted; the heap wasn't
3375 * previously full
3376 */
3377 get_task_struct(p);
3378 } else if (dropped != p) {
3379 /*
3380 * The new task was inserted, and pushed out a
3381 * different task
3382 */
3383 get_task_struct(p);
3384 put_task_struct(dropped);
3385 }
3386 /*
3387 * Else the new task was newer than anything already in
3388 * the heap and wasn't inserted
3389 */
3390 }
Li Zefan6f4b7e62013-07-31 16:18:36 +08003391 cgroup_iter_end(scan->cgrp, &it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003392
3393 if (heap->size) {
3394 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003395 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003396 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003397 latest_time = q->start_time;
3398 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003399 }
3400 /* Process the task per the caller's callback */
Paul Jackson4fe91d52008-04-29 00:59:55 -07003401 scan->process_task(q, scan);
3402 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003403 }
3404 /*
3405 * If we had to process any tasks at all, scan again
3406 * in case some of them were in the middle of forking
3407 * children that didn't get processed.
3408 * Not the most efficient way to do it, but it avoids
3409 * having to take callback_mutex in the fork path
3410 */
3411 goto again;
3412 }
3413 if (heap == &tmp_heap)
3414 heap_free(&tmp_heap);
3415 return 0;
3416}
3417
Tejun Heo8cc99342013-04-07 09:29:50 -07003418static void cgroup_transfer_one_task(struct task_struct *task,
3419 struct cgroup_scanner *scan)
3420{
3421 struct cgroup *new_cgroup = scan->data;
3422
Tejun Heo47cfcd02013-04-07 09:29:51 -07003423 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003424 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003425 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003426}
3427
3428/**
3429 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3430 * @to: cgroup to which the tasks will be moved
3431 * @from: cgroup in which the tasks currently reside
3432 */
3433int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3434{
3435 struct cgroup_scanner scan;
3436
Li Zefan6f4b7e62013-07-31 16:18:36 +08003437 scan.cgrp = from;
Tejun Heo8cc99342013-04-07 09:29:50 -07003438 scan.test_task = NULL; /* select all tasks in cgroup */
3439 scan.process_task = cgroup_transfer_one_task;
3440 scan.heap = NULL;
3441 scan.data = to;
3442
3443 return cgroup_scan_tasks(&scan);
3444}
3445
Paul Menage817929e2007-10-18 23:39:36 -07003446/*
Ben Blum102a7752009-09-23 15:56:26 -07003447 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003448 *
3449 * Reading this file can return large amounts of data if a cgroup has
3450 * *lots* of attached tasks. So it may need several calls to read(),
3451 * but we cannot guarantee that the information we produce is correct
3452 * unless we produce it entirely atomically.
3453 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003454 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003455
Li Zefan24528252012-01-20 11:58:43 +08003456/* which pidlist file are we talking about? */
3457enum cgroup_filetype {
3458 CGROUP_FILE_PROCS,
3459 CGROUP_FILE_TASKS,
3460};
3461
3462/*
3463 * A pidlist is a list of pids that virtually represents the contents of one
3464 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3465 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3466 * to the cgroup.
3467 */
3468struct cgroup_pidlist {
3469 /*
3470 * used to find which pidlist is wanted. doesn't change as long as
3471 * this particular list stays in the list.
3472 */
3473 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3474 /* array of xids */
3475 pid_t *list;
3476 /* how many elements the above list has */
3477 int length;
3478 /* how many files are using the current array */
3479 int use_count;
3480 /* each of these stored in a list by its cgroup */
3481 struct list_head links;
3482 /* pointer to the cgroup we belong to, for list removal purposes */
3483 struct cgroup *owner;
3484 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003485 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003486};
3487
Paul Menagebbcb81d2007-10-18 23:39:32 -07003488/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003489 * The following two functions "fix" the issue where there are more pids
3490 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3491 * TODO: replace with a kernel-wide solution to this problem
3492 */
3493#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3494static void *pidlist_allocate(int count)
3495{
3496 if (PIDLIST_TOO_LARGE(count))
3497 return vmalloc(count * sizeof(pid_t));
3498 else
3499 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3500}
3501static void pidlist_free(void *p)
3502{
3503 if (is_vmalloc_addr(p))
3504 vfree(p);
3505 else
3506 kfree(p);
3507}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003508
3509/*
Ben Blum102a7752009-09-23 15:56:26 -07003510 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003511 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003512 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003513static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003514{
Ben Blum102a7752009-09-23 15:56:26 -07003515 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003516
3517 /*
3518 * we presume the 0th element is unique, so i starts at 1. trivial
3519 * edge cases first; no work needs to be done for either
3520 */
3521 if (length == 0 || length == 1)
3522 return length;
3523 /* src and dest walk down the list; dest counts unique elements */
3524 for (src = 1; src < length; src++) {
3525 /* find next unique element */
3526 while (list[src] == list[src-1]) {
3527 src++;
3528 if (src == length)
3529 goto after;
3530 }
3531 /* dest always points to where the next unique element goes */
3532 list[dest] = list[src];
3533 dest++;
3534 }
3535after:
Ben Blum102a7752009-09-23 15:56:26 -07003536 return dest;
3537}
3538
3539static int cmppid(const void *a, const void *b)
3540{
3541 return *(pid_t *)a - *(pid_t *)b;
3542}
3543
3544/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003545 * find the appropriate pidlist for our purpose (given procs vs tasks)
3546 * returns with the lock on that pidlist already held, and takes care
3547 * of the use count, or returns NULL with no locks held if we're out of
3548 * memory.
3549 */
3550static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3551 enum cgroup_filetype type)
3552{
3553 struct cgroup_pidlist *l;
3554 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003555 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003556
Ben Blum72a8cb32009-09-23 15:56:27 -07003557 /*
Li Zefanb3958902013-08-01 09:52:15 +08003558 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003559 * the last ref-holder is trying to remove l from the list at the same
3560 * time. Holding the pidlist_mutex precludes somebody taking whichever
3561 * list we find out from under us - compare release_pid_array().
3562 */
3563 mutex_lock(&cgrp->pidlist_mutex);
3564 list_for_each_entry(l, &cgrp->pidlists, links) {
3565 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003566 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003567 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003568 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003569 return l;
3570 }
3571 }
3572 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003573 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003574 if (!l) {
3575 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003576 return l;
3577 }
Li Zefanb3958902013-08-01 09:52:15 +08003578 init_rwsem(&l->rwsem);
3579 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003580 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003581 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003582 l->owner = cgrp;
3583 list_add(&l->links, &cgrp->pidlists);
3584 mutex_unlock(&cgrp->pidlist_mutex);
3585 return l;
3586}
3587
3588/*
Ben Blum102a7752009-09-23 15:56:26 -07003589 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3590 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003591static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3592 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003593{
3594 pid_t *array;
3595 int length;
3596 int pid, n = 0; /* used for populating the array */
Paul Menage817929e2007-10-18 23:39:36 -07003597 struct cgroup_iter it;
3598 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003599 struct cgroup_pidlist *l;
3600
3601 /*
3602 * If cgroup gets more users after we read count, we won't have
3603 * enough space - tough. This race is indistinguishable to the
3604 * caller from the case that the additional cgroup users didn't
3605 * show up until sometime later on.
3606 */
3607 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003608 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003609 if (!array)
3610 return -ENOMEM;
3611 /* now, populate the array */
Paul Menagebd89aab2007-10-18 23:40:44 -07003612 cgroup_iter_start(cgrp, &it);
3613 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003614 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003615 break;
Ben Blum102a7752009-09-23 15:56:26 -07003616 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003617 if (type == CGROUP_FILE_PROCS)
3618 pid = task_tgid_vnr(tsk);
3619 else
3620 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003621 if (pid > 0) /* make sure to only use valid results */
3622 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003623 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003624 cgroup_iter_end(cgrp, &it);
Ben Blum102a7752009-09-23 15:56:26 -07003625 length = n;
3626 /* now sort & (if procs) strip out duplicates */
3627 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003629 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003630 l = cgroup_pidlist_find(cgrp, type);
3631 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003632 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003633 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003634 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003635 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003636 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003637 l->list = array;
3638 l->length = length;
3639 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003640 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003641 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003642 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003643}
3644
Balbir Singh846c7bb2007-10-18 23:39:44 -07003645/**
Li Zefana043e3b2008-02-23 15:24:09 -08003646 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003647 * @stats: cgroupstats to fill information into
3648 * @dentry: A dentry entry belonging to the cgroup for which stats have
3649 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003650 *
3651 * Build and fill cgroupstats so that taskstats can export it to user
3652 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003653 */
3654int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3655{
3656 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003657 struct cgroup *cgrp;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003658 struct cgroup_iter it;
3659 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003660
Balbir Singh846c7bb2007-10-18 23:39:44 -07003661 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003662 * Validate dentry by checking the superblock operations,
3663 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003664 */
Li Zefan33d283b2008-11-19 15:36:48 -08003665 if (dentry->d_sb->s_op != &cgroup_ops ||
3666 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003667 goto err;
3668
3669 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003670 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003671
Paul Menagebd89aab2007-10-18 23:40:44 -07003672 cgroup_iter_start(cgrp, &it);
3673 while ((tsk = cgroup_iter_next(cgrp, &it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003674 switch (tsk->state) {
3675 case TASK_RUNNING:
3676 stats->nr_running++;
3677 break;
3678 case TASK_INTERRUPTIBLE:
3679 stats->nr_sleeping++;
3680 break;
3681 case TASK_UNINTERRUPTIBLE:
3682 stats->nr_uninterruptible++;
3683 break;
3684 case TASK_STOPPED:
3685 stats->nr_stopped++;
3686 break;
3687 default:
3688 if (delayacct_is_task_waiting_on_io(tsk))
3689 stats->nr_io_wait++;
3690 break;
3691 }
3692 }
Paul Menagebd89aab2007-10-18 23:40:44 -07003693 cgroup_iter_end(cgrp, &it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003694
Balbir Singh846c7bb2007-10-18 23:39:44 -07003695err:
3696 return ret;
3697}
3698
Paul Menage8f3ff202009-09-23 15:56:25 -07003699
Paul Menagecc31edc2008-10-18 20:28:04 -07003700/*
Ben Blum102a7752009-09-23 15:56:26 -07003701 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003702 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003703 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003704 */
3705
Ben Blum102a7752009-09-23 15:56:26 -07003706static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003707{
3708 /*
3709 * Initially we receive a position value that corresponds to
3710 * one more than the last pid shown (or 0 on the first call or
3711 * after a seek to the start). Use a binary-search to find the
3712 * next pid to display, if any
3713 */
Ben Blum102a7752009-09-23 15:56:26 -07003714 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003715 int index = 0, pid = *pos;
3716 int *iter;
3717
Li Zefanb3958902013-08-01 09:52:15 +08003718 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003719 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003720 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003721
Paul Menagecc31edc2008-10-18 20:28:04 -07003722 while (index < end) {
3723 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003724 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003725 index = mid;
3726 break;
Ben Blum102a7752009-09-23 15:56:26 -07003727 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003728 index = mid + 1;
3729 else
3730 end = mid;
3731 }
3732 }
3733 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003734 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003735 return NULL;
3736 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003737 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003738 *pos = *iter;
3739 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003740}
3741
Ben Blum102a7752009-09-23 15:56:26 -07003742static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003743{
Ben Blum102a7752009-09-23 15:56:26 -07003744 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003745 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003746}
3747
Ben Blum102a7752009-09-23 15:56:26 -07003748static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003749{
Ben Blum102a7752009-09-23 15:56:26 -07003750 struct cgroup_pidlist *l = s->private;
3751 pid_t *p = v;
3752 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 /*
3754 * Advance to the next pid in the array. If this goes off the
3755 * end, we're done
3756 */
3757 p++;
3758 if (p >= end) {
3759 return NULL;
3760 } else {
3761 *pos = *p;
3762 return p;
3763 }
3764}
3765
Ben Blum102a7752009-09-23 15:56:26 -07003766static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003767{
3768 return seq_printf(s, "%d\n", *(int *)v);
3769}
3770
Ben Blum102a7752009-09-23 15:56:26 -07003771/*
3772 * seq_operations functions for iterating on pidlists through seq_file -
3773 * independent of whether it's tasks or procs
3774 */
3775static const struct seq_operations cgroup_pidlist_seq_operations = {
3776 .start = cgroup_pidlist_start,
3777 .stop = cgroup_pidlist_stop,
3778 .next = cgroup_pidlist_next,
3779 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003780};
3781
Ben Blum102a7752009-09-23 15:56:26 -07003782static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783{
Ben Blum72a8cb32009-09-23 15:56:27 -07003784 /*
3785 * the case where we're the last user of this particular pidlist will
3786 * have us remove it from the cgroup's list, which entails taking the
3787 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3788 * pidlist_mutex, we have to take pidlist_mutex first.
3789 */
3790 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003791 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003792 BUG_ON(!l->use_count);
3793 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003794 /* we're the last user if refcount is 0; remove and free */
3795 list_del(&l->links);
3796 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003797 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003798 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003799 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003800 kfree(l);
3801 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003802 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003803 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003804 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003805}
3806
Ben Blum102a7752009-09-23 15:56:26 -07003807static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003808{
Ben Blum102a7752009-09-23 15:56:26 -07003809 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003810 if (!(file->f_mode & FMODE_READ))
3811 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003812 /*
3813 * the seq_file will only be initialized if the file was opened for
3814 * reading; hence we check if it's not null only in that case.
3815 */
3816 l = ((struct seq_file *)file->private_data)->private;
3817 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003818 return seq_release(inode, file);
3819}
3820
Ben Blum102a7752009-09-23 15:56:26 -07003821static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003822 .read = seq_read,
3823 .llseek = seq_lseek,
3824 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003825 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003826};
3827
3828/*
Ben Blum102a7752009-09-23 15:56:26 -07003829 * The following functions handle opens on a file that displays a pidlist
3830 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3831 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003832 */
Ben Blum102a7752009-09-23 15:56:26 -07003833/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003834static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003835{
3836 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003837 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003838 int retval;
3839
3840 /* Nothing to do for write-only files */
3841 if (!(file->f_mode & FMODE_READ))
3842 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003843
Ben Blum102a7752009-09-23 15:56:26 -07003844 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003845 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003846 if (retval)
3847 return retval;
3848 /* configure file information */
3849 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003850
Ben Blum102a7752009-09-23 15:56:26 -07003851 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003852 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003853 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003854 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003855 }
Ben Blum102a7752009-09-23 15:56:26 -07003856 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003857 return 0;
3858}
Ben Blum102a7752009-09-23 15:56:26 -07003859static int cgroup_tasks_open(struct inode *unused, struct file *file)
3860{
Ben Blum72a8cb32009-09-23 15:56:27 -07003861 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003862}
3863static int cgroup_procs_open(struct inode *unused, struct file *file)
3864{
Ben Blum72a8cb32009-09-23 15:56:27 -07003865 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003866}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003867
Tejun Heo182446d2013-08-08 20:11:24 -04003868static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3869 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003870{
Tejun Heo182446d2013-08-08 20:11:24 -04003871 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003872}
3873
Tejun Heo182446d2013-08-08 20:11:24 -04003874static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3875 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003876{
Tejun Heo182446d2013-08-08 20:11:24 -04003877 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003878 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003879 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003880 else
Tejun Heo182446d2013-08-08 20:11:24 -04003881 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003882 return 0;
3883}
3884
Paul Menagebbcb81d2007-10-18 23:39:32 -07003885/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003886 * When dput() is called asynchronously, if umount has been done and
3887 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3888 * there's a small window that vfs will see the root dentry with non-zero
3889 * refcnt and trigger BUG().
3890 *
3891 * That's why we hold a reference before dput() and drop it right after.
3892 */
3893static void cgroup_dput(struct cgroup *cgrp)
3894{
3895 struct super_block *sb = cgrp->root->sb;
3896
3897 atomic_inc(&sb->s_active);
3898 dput(cgrp->dentry);
3899 deactivate_super(sb);
3900}
3901
3902/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003903 * Unregister event and free resources.
3904 *
3905 * Gets called from workqueue.
3906 */
3907static void cgroup_event_remove(struct work_struct *work)
3908{
3909 struct cgroup_event *event = container_of(work, struct cgroup_event,
3910 remove);
3911 struct cgroup *cgrp = event->cgrp;
3912
Li Zefan810cbee2013-02-18 18:56:14 +08003913 remove_wait_queue(event->wqh, &event->wait);
3914
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003915 event->cft->unregister_event(cgrp, event->cft, event->eventfd);
3916
Li Zefan810cbee2013-02-18 18:56:14 +08003917 /* Notify userspace the event is going away. */
3918 eventfd_signal(event->eventfd, 1);
3919
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003920 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003921 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003922 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003923}
3924
3925/*
3926 * Gets called on POLLHUP on eventfd when user closes it.
3927 *
3928 * Called with wqh->lock held and interrupts disabled.
3929 */
3930static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3931 int sync, void *key)
3932{
3933 struct cgroup_event *event = container_of(wait,
3934 struct cgroup_event, wait);
3935 struct cgroup *cgrp = event->cgrp;
3936 unsigned long flags = (unsigned long)key;
3937
3938 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003939 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003940 * If the event has been detached at cgroup removal, we
3941 * can simply return knowing the other side will cleanup
3942 * for us.
3943 *
3944 * We can't race against event freeing since the other
3945 * side will require wqh->lock via remove_wait_queue(),
3946 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003947 */
Li Zefan810cbee2013-02-18 18:56:14 +08003948 spin_lock(&cgrp->event_list_lock);
3949 if (!list_empty(&event->list)) {
3950 list_del_init(&event->list);
3951 /*
3952 * We are in atomic context, but cgroup_event_remove()
3953 * may sleep, so we have to call it in workqueue.
3954 */
3955 schedule_work(&event->remove);
3956 }
3957 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003958 }
3959
3960 return 0;
3961}
3962
3963static void cgroup_event_ptable_queue_proc(struct file *file,
3964 wait_queue_head_t *wqh, poll_table *pt)
3965{
3966 struct cgroup_event *event = container_of(pt,
3967 struct cgroup_event, pt);
3968
3969 event->wqh = wqh;
3970 add_wait_queue(wqh, &event->wait);
3971}
3972
3973/*
3974 * Parse input and register new cgroup event handler.
3975 *
3976 * Input must be in format '<event_fd> <control_fd> <args>'.
3977 * Interpretation of args is defined by control file implementation.
3978 */
Tejun Heo182446d2013-08-08 20:11:24 -04003979static int cgroup_write_event_control(struct cgroup_subsys_state *css,
3980 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003981{
Tejun Heo182446d2013-08-08 20:11:24 -04003982 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08003983 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08003984 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08003986 struct file *efile;
3987 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003988 char *endp;
3989 int ret;
3990
3991 efd = simple_strtoul(buffer, &endp, 10);
3992 if (*endp != ' ')
3993 return -EINVAL;
3994 buffer = endp + 1;
3995
3996 cfd = simple_strtoul(buffer, &endp, 10);
3997 if ((*endp != ' ') && (*endp != '\0'))
3998 return -EINVAL;
3999 buffer = endp + 1;
4000
4001 event = kzalloc(sizeof(*event), GFP_KERNEL);
4002 if (!event)
4003 return -ENOMEM;
4004 event->cgrp = cgrp;
4005 INIT_LIST_HEAD(&event->list);
4006 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4007 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4008 INIT_WORK(&event->remove, cgroup_event_remove);
4009
4010 efile = eventfd_fget(efd);
4011 if (IS_ERR(efile)) {
4012 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004013 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004014 }
4015
4016 event->eventfd = eventfd_ctx_fileget(efile);
4017 if (IS_ERR(event->eventfd)) {
4018 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004019 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004020 }
4021
4022 cfile = fget(cfd);
4023 if (!cfile) {
4024 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004025 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004026 }
4027
4028 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004029 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004030 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004031 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004032 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033
4034 event->cft = __file_cft(cfile);
4035 if (IS_ERR(event->cft)) {
4036 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004037 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004038 }
4039
Li Zefanf1690072013-02-18 14:13:35 +08004040 /*
4041 * The file to be monitored must be in the same cgroup as
4042 * cgroup.event_control is.
4043 */
4044 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4045 if (cgrp_cfile != cgrp) {
4046 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004047 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004048 }
4049
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004050 if (!event->cft->register_event || !event->cft->unregister_event) {
4051 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004052 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004053 }
4054
4055 ret = event->cft->register_event(cgrp, event->cft,
4056 event->eventfd, buffer);
4057 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004058 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004059
Li Zefan7ef70e42013-04-26 11:58:03 -07004060 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004061
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004062 /*
4063 * Events should be removed after rmdir of cgroup directory, but before
4064 * destroying subsystem state objects. Let's take reference to cgroup
4065 * directory dentry to do that.
4066 */
4067 dget(cgrp->dentry);
4068
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004069 spin_lock(&cgrp->event_list_lock);
4070 list_add(&event->list, &cgrp->event_list);
4071 spin_unlock(&cgrp->event_list_lock);
4072
4073 fput(cfile);
4074 fput(efile);
4075
4076 return 0;
4077
Li Zefan876ede82013-08-01 09:51:47 +08004078out_put_cfile:
4079 fput(cfile);
4080out_put_eventfd:
4081 eventfd_ctx_put(event->eventfd);
4082out_put_efile:
4083 fput(efile);
4084out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085 kfree(event);
4086
4087 return ret;
4088}
4089
Tejun Heo182446d2013-08-08 20:11:24 -04004090static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4091 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004092{
Tejun Heo182446d2013-08-08 20:11:24 -04004093 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004094}
4095
Tejun Heo182446d2013-08-08 20:11:24 -04004096static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4097 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004098{
4099 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004100 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004101 else
Tejun Heo182446d2013-08-08 20:11:24 -04004102 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004103 return 0;
4104}
4105
Tejun Heod5c56ce2013-06-03 19:14:34 -07004106static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004107 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004108 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004109 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004110 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004111 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004112 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004113 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004114 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004115 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004116 .write_string = cgroup_write_event_control,
4117 .mode = S_IWUGO,
4118 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004119 {
4120 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004121 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004122 .read_u64 = cgroup_clone_children_read,
4123 .write_u64 = cgroup_clone_children_write,
4124 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004125 {
Tejun Heo873fe092013-04-14 20:15:26 -07004126 .name = "cgroup.sane_behavior",
4127 .flags = CFTYPE_ONLY_ON_ROOT,
4128 .read_seq_string = cgroup_sane_behavior_show,
4129 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004130
4131 /*
4132 * Historical crazy stuff. These don't have "cgroup." prefix and
4133 * don't exist if sane_behavior. If you're depending on these, be
4134 * prepared to be burned.
4135 */
4136 {
4137 .name = "tasks",
4138 .flags = CFTYPE_INSANE, /* use "procs" instead */
4139 .open = cgroup_tasks_open,
4140 .write_u64 = cgroup_tasks_write,
4141 .release = cgroup_pidlist_release,
4142 .mode = S_IRUGO | S_IWUSR,
4143 },
4144 {
4145 .name = "notify_on_release",
4146 .flags = CFTYPE_INSANE,
4147 .read_u64 = cgroup_read_notify_on_release,
4148 .write_u64 = cgroup_write_notify_on_release,
4149 },
Tejun Heo873fe092013-04-14 20:15:26 -07004150 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004151 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004152 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004153 .read_seq_string = cgroup_release_agent_show,
4154 .write_string = cgroup_release_agent_write,
4155 .max_write_len = PATH_MAX,
4156 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004157 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004158};
4159
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004160/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004161 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004162 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004163 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004164 *
4165 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004166 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004167static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004168{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004169 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004170 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004171
Tejun Heo8e3f6542012-04-01 12:09:55 -07004172 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004173 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004174 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004175
4176 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004177 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004178
Tejun Heobee55092013-06-28 16:24:11 -07004179 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004180 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004181 if (ret < 0)
4182 goto err;
4183 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004184 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004185
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004186 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004187 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004188 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004189 struct css_id *id = rcu_dereference_protected(css->id, true);
4190
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004191 /*
4192 * Update id->css pointer and make this css visible from
4193 * CSS ID functions. This pointer will be dereferened
4194 * from RCU-read-side without locks.
4195 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004196 if (id)
4197 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004198 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004199
4200 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004201err:
4202 cgroup_clear_dir(cgrp, subsys_mask);
4203 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004204}
4205
Tejun Heo48ddbe12012-04-01 12:09:56 -07004206static void css_dput_fn(struct work_struct *work)
4207{
4208 struct cgroup_subsys_state *css =
4209 container_of(work, struct cgroup_subsys_state, dput_work);
4210
Li Zefan1c8158e2013-06-18 18:41:10 +08004211 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004212}
4213
Tejun Heod3daf282013-06-13 19:39:16 -07004214static void css_release(struct percpu_ref *ref)
4215{
4216 struct cgroup_subsys_state *css =
4217 container_of(ref, struct cgroup_subsys_state, refcnt);
4218
4219 schedule_work(&css->dput_work);
4220}
4221
Paul Menageddbcc7e2007-10-18 23:39:30 -07004222static void init_cgroup_css(struct cgroup_subsys_state *css,
4223 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004224 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225{
Paul Menagebd89aab2007-10-18 23:40:44 -07004226 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004227 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004229 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004230 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004231 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004232 BUG_ON(cgrp->subsys[ss->subsys_id]);
4233 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004234
4235 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004236 * css holds an extra ref to @cgrp->dentry which is put on the last
4237 * css_put(). dput() requires process context, which css_put() may
4238 * be called without. @css->dput_work will be used to invoke
4239 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004240 */
4241 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004242}
4243
Li Zefan2a4ac632013-07-31 16:16:40 +08004244/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004245static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004246{
Tejun Heoeb954192013-08-08 20:11:23 -04004247 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heob1929db2012-11-19 08:13:38 -08004248 int ret = 0;
4249
Tejun Heoa31f2d32012-11-19 08:13:37 -08004250 lockdep_assert_held(&cgroup_mutex);
4251
Tejun Heo92fb9742012-11-19 08:13:38 -08004252 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004253 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004254 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004255 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004256 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004257}
4258
Li Zefan2a4ac632013-07-31 16:16:40 +08004259/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004260static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004261{
4262 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4263
4264 lockdep_assert_held(&cgroup_mutex);
4265
4266 if (!(css->flags & CSS_ONLINE))
4267 return;
4268
Li Zefand7eeac12013-03-12 15:35:59 -07004269 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004270 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004271
Tejun Heoeb954192013-08-08 20:11:23 -04004272 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004273}
4274
Paul Menageddbcc7e2007-10-18 23:39:30 -07004275/*
Li Zefana043e3b2008-02-23 15:24:09 -08004276 * cgroup_create - create a cgroup
4277 * @parent: cgroup that will be parent of the new cgroup
4278 * @dentry: dentry of the new cgroup
4279 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004280 *
Li Zefana043e3b2008-02-23 15:24:09 -08004281 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004282 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004283static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004284 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285{
Paul Menagebd89aab2007-10-18 23:40:44 -07004286 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004287 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004288 struct cgroupfs_root *root = parent->root;
4289 int err = 0;
4290 struct cgroup_subsys *ss;
4291 struct super_block *sb = root->sb;
4292
Tejun Heo0a950f62012-11-19 09:02:12 -08004293 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004294 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4295 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004296 return -ENOMEM;
4297
Li Zefan65dff752013-03-01 15:01:56 +08004298 name = cgroup_alloc_name(dentry);
4299 if (!name)
4300 goto err_free_cgrp;
4301 rcu_assign_pointer(cgrp->name, name);
4302
Li Zefan4e96ee82013-07-31 09:50:50 +08004303 /*
4304 * Temporarily set the pointer to NULL, so idr_find() won't return
4305 * a half-baked cgroup.
4306 */
4307 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004308 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004309 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004310
Tejun Heo976c06b2012-11-05 09:16:59 -08004311 /*
4312 * Only live parents can have children. Note that the liveliness
4313 * check isn't strictly necessary because cgroup_mkdir() and
4314 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4315 * anyway so that locking is contained inside cgroup proper and we
4316 * don't get nasty surprises if we ever grow another caller.
4317 */
4318 if (!cgroup_lock_live_group(parent)) {
4319 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004320 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004321 }
4322
Paul Menageddbcc7e2007-10-18 23:39:30 -07004323 /* Grab a reference on the superblock so the hierarchy doesn't
4324 * get deleted on unmount if there are child cgroups. This
4325 * can be done outside cgroup_mutex, since the sb can't
4326 * disappear while someone has an open control file on the
4327 * fs */
4328 atomic_inc(&sb->s_active);
4329
Paul Menagecc31edc2008-10-18 20:28:04 -07004330 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331
Li Zefanfe1c06c2013-01-24 14:30:22 +08004332 dentry->d_fsdata = cgrp;
4333 cgrp->dentry = dentry;
4334
Paul Menagebd89aab2007-10-18 23:40:44 -07004335 cgrp->parent = parent;
4336 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004337
Li Zefanb6abdb02008-03-04 14:28:19 -08004338 if (notify_on_release(parent))
4339 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4340
Tejun Heo2260e7f2012-11-19 08:13:38 -08004341 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4342 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004343
Tejun Heo5549c492013-06-24 15:21:48 -07004344 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004345 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004346
Tejun Heoeb954192013-08-08 20:11:23 -04004347 css = ss->css_alloc(parent->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348 if (IS_ERR(css)) {
4349 err = PTR_ERR(css);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004350 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351 }
Tejun Heod3daf282013-06-13 19:39:16 -07004352
4353 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004354 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004355 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004356 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004357 }
Tejun Heod3daf282013-06-13 19:39:16 -07004358
Paul Menagebd89aab2007-10-18 23:40:44 -07004359 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004360
Li Zefan4528fd02010-02-02 13:44:10 -08004361 if (ss->use_id) {
4362 err = alloc_css_id(ss, parent, cgrp);
4363 if (err)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004364 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004365 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004366 }
4367
Tejun Heo4e139af2012-11-19 08:13:36 -08004368 /*
4369 * Create directory. cgroup_create_file() returns with the new
4370 * directory locked on success so that it can be populated without
4371 * dropping cgroup_mutex.
4372 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004373 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374 if (err < 0)
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004375 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004376 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004377
Tejun Heo00356bd2013-06-18 11:14:22 -07004378 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004379
Tejun Heo4e139af2012-11-19 08:13:36 -08004380 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004381 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4382 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004383
Tejun Heob1929db2012-11-19 08:13:38 -08004384 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004385 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004386 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004387
Li Zefan415cf072013-04-08 14:35:02 +08004388 /* hold a ref to the parent's dentry */
4389 dget(parent->dentry);
4390
Tejun Heob1929db2012-11-19 08:13:38 -08004391 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004392 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004393 err = online_css(ss, cgrp);
4394 if (err)
4395 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004396
4397 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4398 parent->parent) {
4399 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",
4400 current->comm, current->pid, ss->name);
4401 if (!strcmp(ss->name, "memory"))
4402 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4403 ss->warned_broken_hierarchy = true;
4404 }
Tejun Heoa8638032012-11-09 09:12:29 -08004405 }
4406
Li Zefan4e96ee82013-07-31 09:50:50 +08004407 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4408
Tejun Heo2bb566c2013-08-08 20:11:23 -04004409 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004410 if (err)
4411 goto err_destroy;
4412
4413 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004414 if (err)
4415 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004416
4417 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004418 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004419
4420 return 0;
4421
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004422err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004423 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004424 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4425
4426 if (css) {
4427 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004428 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004429 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004430 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004431 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004432 /* Release the reference count that we took on the superblock */
4433 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004434err_free_id:
Li Zefan4e96ee82013-07-31 09:50:50 +08004435 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004436err_free_name:
4437 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004438err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004439 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440 return err;
Tejun Heo4b8b47e2012-11-19 08:13:38 -08004441
4442err_destroy:
4443 cgroup_destroy_locked(cgrp);
4444 mutex_unlock(&cgroup_mutex);
4445 mutex_unlock(&dentry->d_inode->i_mutex);
4446 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004447}
4448
Al Viro18bb1db2011-07-26 01:41:39 -04004449static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004450{
4451 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4452
4453 /* the vfs holds inode->i_mutex already */
4454 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4455}
4456
Tejun Heod3daf282013-06-13 19:39:16 -07004457static void cgroup_css_killed(struct cgroup *cgrp)
4458{
4459 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4460 return;
4461
4462 /* percpu ref's of all css's are killed, kick off the next step */
4463 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4464 schedule_work(&cgrp->destroy_work);
4465}
4466
4467static void css_ref_killed_fn(struct percpu_ref *ref)
4468{
4469 struct cgroup_subsys_state *css =
4470 container_of(ref, struct cgroup_subsys_state, refcnt);
4471
4472 cgroup_css_killed(css->cgroup);
4473}
4474
4475/**
4476 * cgroup_destroy_locked - the first stage of cgroup destruction
4477 * @cgrp: cgroup to be destroyed
4478 *
4479 * css's make use of percpu refcnts whose killing latency shouldn't be
4480 * exposed to userland and are RCU protected. Also, cgroup core needs to
4481 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4482 * invoked. To satisfy all the requirements, destruction is implemented in
4483 * the following two steps.
4484 *
4485 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4486 * userland visible parts and start killing the percpu refcnts of
4487 * css's. Set up so that the next stage will be kicked off once all
4488 * the percpu refcnts are confirmed to be killed.
4489 *
4490 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4491 * rest of destruction. Once all cgroup references are gone, the
4492 * cgroup is RCU-freed.
4493 *
4494 * This function implements s1. After this step, @cgrp is gone as far as
4495 * the userland is concerned and a new cgroup with the same name may be
4496 * created. As cgroup doesn't care about the names internally, this
4497 * doesn't cause any problem.
4498 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004499static int cgroup_destroy_locked(struct cgroup *cgrp)
4500 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004501{
Tejun Heo42809dd2012-11-19 08:13:37 -08004502 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004503 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004504 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004505 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004506
Tejun Heo42809dd2012-11-19 08:13:37 -08004507 lockdep_assert_held(&d->d_inode->i_mutex);
4508 lockdep_assert_held(&cgroup_mutex);
4509
Tejun Heoddd69142013-06-12 21:04:54 -07004510 /*
Tejun Heo6f3d8282013-06-12 21:04:55 -07004511 * css_set_lock synchronizes access to ->cset_links and prevents
4512 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004513 */
4514 read_lock(&css_set_lock);
Tejun Heo6f3d8282013-06-12 21:04:55 -07004515 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004516 read_unlock(&css_set_lock);
4517 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004518 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004519
Tejun Heo1a90dd52012-11-05 09:16:59 -08004520 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004521 * Block new css_tryget() by killing css refcnts. cgroup core
4522 * guarantees that, by the time ->css_offline() is invoked, no new
4523 * css reference will be given out via css_tryget(). We can't
4524 * simply call percpu_ref_kill() and proceed to offlining css's
4525 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4526 * as killed on all CPUs on return.
4527 *
4528 * Use percpu_ref_kill_and_confirm() to get notifications as each
4529 * css is confirmed to be seen as killed on all CPUs. The
4530 * notification callback keeps track of the number of css's to be
4531 * killed and schedules cgroup_offline_fn() to perform the rest of
4532 * destruction once the percpu refs of all css's are confirmed to
4533 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004534 */
Tejun Heod3daf282013-06-13 19:39:16 -07004535 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004536 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004537 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4538
Tejun Heod3daf282013-06-13 19:39:16 -07004539 /*
4540 * Killing would put the base ref, but we need to keep it
4541 * alive until after ->css_offline.
4542 */
4543 percpu_ref_get(&css->refcnt);
4544
4545 atomic_inc(&cgrp->css_kill_cnt);
4546 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004547 }
Tejun Heod3daf282013-06-13 19:39:16 -07004548 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004549
4550 /*
4551 * Mark @cgrp dead. This prevents further task migration and child
4552 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo3b287a52013-08-08 20:11:24 -04004553 * CGRP_DEAD assertion is depended upon by cgroup_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004554 * resume iteration after dropping RCU read lock. See
Tejun Heo3b287a52013-08-08 20:11:24 -04004555 * cgroup_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004556 */
Tejun Heo54766d42013-06-12 21:04:53 -07004557 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004558
Tejun Heo455050d2013-06-13 19:27:41 -07004559 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4560 raw_spin_lock(&release_list_lock);
4561 if (!list_empty(&cgrp->release_list))
4562 list_del_init(&cgrp->release_list);
4563 raw_spin_unlock(&release_list_lock);
4564
4565 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004566 * Clear and remove @cgrp directory. The removal puts the base ref
4567 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004568 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004569 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004570 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004571 dget(d);
4572 cgroup_d_remove_dir(d);
4573
4574 /*
4575 * Unregister events and notify userspace.
4576 * Notify userspace about cgroup removing only after rmdir of cgroup
4577 * directory to avoid race between userspace and kernelspace.
4578 */
4579 spin_lock(&cgrp->event_list_lock);
4580 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4581 list_del_init(&event->list);
4582 schedule_work(&event->remove);
4583 }
4584 spin_unlock(&cgrp->event_list_lock);
4585
Tejun Heoea15f8c2013-06-13 19:27:42 -07004586 return 0;
4587};
4588
Tejun Heod3daf282013-06-13 19:39:16 -07004589/**
4590 * cgroup_offline_fn - the second step of cgroup destruction
4591 * @work: cgroup->destroy_free_work
4592 *
4593 * This function is invoked from a work item for a cgroup which is being
4594 * destroyed after the percpu refcnts of all css's are guaranteed to be
4595 * seen as killed on all CPUs, and performs the rest of destruction. This
4596 * is the second step of destruction described in the comment above
4597 * cgroup_destroy_locked().
4598 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004599static void cgroup_offline_fn(struct work_struct *work)
4600{
4601 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4602 struct cgroup *parent = cgrp->parent;
4603 struct dentry *d = cgrp->dentry;
4604 struct cgroup_subsys *ss;
4605
4606 mutex_lock(&cgroup_mutex);
4607
Tejun Heod3daf282013-06-13 19:39:16 -07004608 /*
4609 * css_tryget() is guaranteed to fail now. Tell subsystems to
4610 * initate destruction.
4611 */
Tejun Heo5549c492013-06-24 15:21:48 -07004612 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004613 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004614
4615 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004616 * Put the css refs from cgroup_destroy_locked(). Each css holds
4617 * an extra reference to the cgroup's dentry and cgroup removal
4618 * proceeds regardless of css refs. On the last put of each css,
4619 * whenever that may be, the extra dentry ref is put so that dentry
4620 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004621 */
Tejun Heo5549c492013-06-24 15:21:48 -07004622 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004623 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004624
Paul Menage999cd8a2009-01-07 18:08:36 -08004625 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004626 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004627
Li Zefan4e96ee82013-07-31 09:50:50 +08004628 /*
4629 * We should remove the cgroup object from idr before its grace
4630 * period starts, so we won't be looking up a cgroup while the
4631 * cgroup is being freed.
4632 */
4633 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4634 cgrp->id = -1;
4635
Paul Menageddbcc7e2007-10-18 23:39:30 -07004636 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004637
Paul Menagebd89aab2007-10-18 23:40:44 -07004638 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004639 check_for_release(parent);
4640
Tejun Heoea15f8c2013-06-13 19:27:42 -07004641 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004642}
4643
Tejun Heo42809dd2012-11-19 08:13:37 -08004644static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4645{
4646 int ret;
4647
4648 mutex_lock(&cgroup_mutex);
4649 ret = cgroup_destroy_locked(dentry->d_fsdata);
4650 mutex_unlock(&cgroup_mutex);
4651
4652 return ret;
4653}
4654
Tejun Heo8e3f6542012-04-01 12:09:55 -07004655static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4656{
4657 INIT_LIST_HEAD(&ss->cftsets);
4658
4659 /*
4660 * base_cftset is embedded in subsys itself, no need to worry about
4661 * deregistration.
4662 */
4663 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004664 struct cftype *cft;
4665
4666 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4667 cft->ss = ss;
4668
Tejun Heo8e3f6542012-04-01 12:09:55 -07004669 ss->base_cftset.cfts = ss->base_cftypes;
4670 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4671 }
4672}
4673
Li Zefan06a11922008-04-29 01:00:07 -07004674static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004676 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004677
4678 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004679
Tejun Heo648bb562012-11-19 08:13:36 -08004680 mutex_lock(&cgroup_mutex);
4681
Tejun Heo8e3f6542012-04-01 12:09:55 -07004682 /* init base cftset */
4683 cgroup_init_cftsets(ss);
4684
Paul Menageddbcc7e2007-10-18 23:39:30 -07004685 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004686 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4687 ss->root = &cgroup_dummy_root;
Tejun Heoeb954192013-08-08 20:11:23 -04004688 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004689 /* We don't handle early failures gracefully */
4690 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004691 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004692
Li Zefane8d55fd2008-04-29 01:00:13 -07004693 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004694 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004695 * newly registered, all tasks and hence the
4696 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004697 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004698
4699 need_forkexit_callback |= ss->fork || ss->exit;
4700
Li Zefane8d55fd2008-04-29 01:00:13 -07004701 /* At system boot, before all subsystems have been
4702 * registered, no tasks have been forked, so we don't
4703 * need to invoke fork callbacks here. */
4704 BUG_ON(!list_empty(&init_task.tasks));
4705
Tejun Heo9871bf92013-06-24 15:21:47 -07004706 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004707
Tejun Heo648bb562012-11-19 08:13:36 -08004708 mutex_unlock(&cgroup_mutex);
4709
Ben Blume6a11052010-03-10 15:22:09 -08004710 /* this function shouldn't be used with modular subsystems, since they
4711 * need to register a subsys_id, among other things */
4712 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004713}
4714
4715/**
Ben Blume6a11052010-03-10 15:22:09 -08004716 * cgroup_load_subsys: load and register a modular subsystem at runtime
4717 * @ss: the subsystem to load
4718 *
4719 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004720 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004721 * up for use. If the subsystem is built-in anyway, work is delegated to the
4722 * simpler cgroup_init_subsys.
4723 */
4724int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4725{
Ben Blume6a11052010-03-10 15:22:09 -08004726 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004727 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004728 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004729 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004730 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004731
4732 /* check name and function validity */
4733 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004734 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004735 return -EINVAL;
4736
4737 /*
4738 * we don't support callbacks in modular subsystems. this check is
4739 * before the ss->module check for consistency; a subsystem that could
4740 * be a module should still have no callbacks even if the user isn't
4741 * compiling it as one.
4742 */
4743 if (ss->fork || ss->exit)
4744 return -EINVAL;
4745
4746 /*
4747 * an optionally modular subsystem is built-in: we want to do nothing,
4748 * since cgroup_init_subsys will have already taken care of it.
4749 */
4750 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004751 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004752 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004753 return 0;
4754 }
4755
Tejun Heo8e3f6542012-04-01 12:09:55 -07004756 /* init base cftset */
4757 cgroup_init_cftsets(ss);
4758
Ben Blume6a11052010-03-10 15:22:09 -08004759 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004760 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004761
4762 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004763 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004764 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004765 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004766 */
Tejun Heoeb954192013-08-08 20:11:23 -04004767 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Ben Blume6a11052010-03-10 15:22:09 -08004768 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004769 /* failure case - need to deassign the cgroup_subsys[] slot. */
4770 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004771 mutex_unlock(&cgroup_mutex);
4772 return PTR_ERR(css);
4773 }
4774
Tejun Heo9871bf92013-06-24 15:21:47 -07004775 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4776 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004777
4778 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004779 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004780 /* init_idr must be after init_cgroup_css because it sets css->id. */
4781 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004782 ret = cgroup_init_idr(ss, css);
4783 if (ret)
4784 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004785 }
4786
4787 /*
4788 * Now we need to entangle the css into the existing css_sets. unlike
4789 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4790 * will need a new pointer to it; done by iterating the css_set_table.
4791 * furthermore, modifying the existing css_sets will corrupt the hash
4792 * table state, so each changed css_set will need its hash recomputed.
4793 * this is all done under the css_set_lock.
4794 */
4795 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004796 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004797 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004798 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004799 continue;
4800 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004801 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004802 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004803 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004804 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004805 key = css_set_hash(cset->subsys);
4806 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004807 }
4808 write_unlock(&css_set_lock);
4809
Tejun Heo9871bf92013-06-24 15:21:47 -07004810 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004811 if (ret)
4812 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004813
Ben Blume6a11052010-03-10 15:22:09 -08004814 /* success! */
4815 mutex_unlock(&cgroup_mutex);
4816 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004817
4818err_unload:
4819 mutex_unlock(&cgroup_mutex);
4820 /* @ss can't be mounted here as try_module_get() would fail */
4821 cgroup_unload_subsys(ss);
4822 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004823}
4824EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4825
4826/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004827 * cgroup_unload_subsys: unload a modular subsystem
4828 * @ss: the subsystem to unload
4829 *
4830 * This function should be called in a modular subsystem's exitcall. When this
4831 * function is invoked, the refcount on the subsystem's module will be 0, so
4832 * the subsystem will not be attached to any hierarchy.
4833 */
4834void cgroup_unload_subsys(struct cgroup_subsys *ss)
4835{
Tejun Heo69d02062013-06-12 21:04:50 -07004836 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004837
4838 BUG_ON(ss->module == NULL);
4839
4840 /*
4841 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004842 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004843 * doesn't start being used while we're killing it off.
4844 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004845 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004846
4847 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004848
Tejun Heo9871bf92013-06-24 15:21:47 -07004849 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004850
Tejun Heoc897ff62013-02-27 17:03:49 -08004851 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004852 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004853
Ben Blumcf5d5942010-03-10 15:22:09 -08004854 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004855 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004856
Tejun Heo9871bf92013-06-24 15:21:47 -07004857 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004858 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004859
4860 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004861 * disentangle the css from all css_sets attached to the dummy
4862 * top. as in loading, we need to pay our respects to the hashtable
4863 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004864 */
4865 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004866 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004867 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004868 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004869
Tejun Heo5abb8852013-06-12 21:04:49 -07004870 hash_del(&cset->hlist);
4871 cset->subsys[ss->subsys_id] = NULL;
4872 key = css_set_hash(cset->subsys);
4873 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004874 }
4875 write_unlock(&css_set_lock);
4876
4877 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004878 * remove subsystem's css from the cgroup_dummy_top and free it -
4879 * need to free before marking as null because ss->css_free needs
4880 * the cgrp->subsys pointer to find their state. note that this
4881 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004882 */
Tejun Heoeb954192013-08-08 20:11:23 -04004883 ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
Tejun Heo9871bf92013-06-24 15:21:47 -07004884 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004885
4886 mutex_unlock(&cgroup_mutex);
4887}
4888EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4889
4890/**
Li Zefana043e3b2008-02-23 15:24:09 -08004891 * cgroup_init_early - cgroup initialization at system boot
4892 *
4893 * Initialize cgroups at system boot, and initialize any
4894 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004895 */
4896int __init cgroup_init_early(void)
4897{
Tejun Heo30159ec2013-06-25 11:53:37 -07004898 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004899 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004900
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004901 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004902 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004903 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004904 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004905 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004906 init_cgroup_root(&cgroup_dummy_root);
4907 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004908 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004909
Tejun Heo69d02062013-06-12 21:04:50 -07004910 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004911 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4912 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004913 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004914
Tejun Heo30159ec2013-06-25 11:53:37 -07004915 /* at bootup time, we don't worry about modular subsystems */
4916 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004917 BUG_ON(!ss->name);
4918 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004919 BUG_ON(!ss->css_alloc);
4920 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004921 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004922 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004923 ss->name, ss->subsys_id);
4924 BUG();
4925 }
4926
4927 if (ss->early_init)
4928 cgroup_init_subsys(ss);
4929 }
4930 return 0;
4931}
4932
4933/**
Li Zefana043e3b2008-02-23 15:24:09 -08004934 * cgroup_init - cgroup initialization
4935 *
4936 * Register cgroup filesystem and /proc file, and initialize
4937 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004938 */
4939int __init cgroup_init(void)
4940{
Tejun Heo30159ec2013-06-25 11:53:37 -07004941 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004942 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004943 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07004944
4945 err = bdi_init(&cgroup_backing_dev_info);
4946 if (err)
4947 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004948
Tejun Heo30159ec2013-06-25 11:53:37 -07004949 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004950 if (!ss->early_init)
4951 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004952 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08004953 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004954 }
4955
Tejun Heofa3ca072013-04-14 11:36:56 -07004956 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004957 mutex_lock(&cgroup_mutex);
4958 mutex_lock(&cgroup_root_mutex);
4959
Tejun Heo82fe9b02013-06-25 11:53:37 -07004960 /* Add init_css_set to the hash table */
4961 key = css_set_hash(init_css_set.subsys);
4962 hash_add(css_set_table, &init_css_set.hlist, key);
4963
Tejun Heofc76df72013-06-25 11:53:37 -07004964 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07004965
Li Zefan4e96ee82013-07-31 09:50:50 +08004966 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
4967 0, 1, GFP_KERNEL);
4968 BUG_ON(err < 0);
4969
Tejun Heo54e7b4e2013-04-14 11:36:57 -07004970 mutex_unlock(&cgroup_root_mutex);
4971 mutex_unlock(&cgroup_mutex);
4972
Greg KH676db4a2010-08-05 13:53:35 -07004973 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
4974 if (!cgroup_kobj) {
4975 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004976 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07004977 }
4978
4979 err = register_filesystem(&cgroup_fs_type);
4980 if (err < 0) {
4981 kobject_put(cgroup_kobj);
4982 goto out;
4983 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004984
Li Zefan46ae2202008-04-29 01:00:08 -07004985 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07004986
Paul Menageddbcc7e2007-10-18 23:39:30 -07004987out:
Paul Menagea4243162007-10-18 23:39:35 -07004988 if (err)
4989 bdi_destroy(&cgroup_backing_dev_info);
4990
Paul Menageddbcc7e2007-10-18 23:39:30 -07004991 return err;
4992}
Paul Menageb4f48b62007-10-18 23:39:33 -07004993
Paul Menagea4243162007-10-18 23:39:35 -07004994/*
4995 * proc_cgroup_show()
4996 * - Print task's cgroup paths into seq_file, one line for each hierarchy
4997 * - Used for /proc/<pid>/cgroup.
4998 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
4999 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005000 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005001 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5002 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5003 * cgroup to top_cgroup.
5004 */
5005
5006/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005007int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005008{
5009 struct pid *pid;
5010 struct task_struct *tsk;
5011 char *buf;
5012 int retval;
5013 struct cgroupfs_root *root;
5014
5015 retval = -ENOMEM;
5016 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5017 if (!buf)
5018 goto out;
5019
5020 retval = -ESRCH;
5021 pid = m->private;
5022 tsk = get_pid_task(pid, PIDTYPE_PID);
5023 if (!tsk)
5024 goto out_free;
5025
5026 retval = 0;
5027
5028 mutex_lock(&cgroup_mutex);
5029
Li Zefane5f6a862009-01-07 18:07:41 -08005030 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005031 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005032 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005033 int count = 0;
5034
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005035 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005036 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005037 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005038 if (strlen(root->name))
5039 seq_printf(m, "%sname=%s", count ? "," : "",
5040 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005041 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005042 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005043 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005044 if (retval < 0)
5045 goto out_unlock;
5046 seq_puts(m, buf);
5047 seq_putc(m, '\n');
5048 }
5049
5050out_unlock:
5051 mutex_unlock(&cgroup_mutex);
5052 put_task_struct(tsk);
5053out_free:
5054 kfree(buf);
5055out:
5056 return retval;
5057}
5058
Paul Menagea4243162007-10-18 23:39:35 -07005059/* Display information about each subsystem and each hierarchy */
5060static int proc_cgroupstats_show(struct seq_file *m, void *v)
5061{
Tejun Heo30159ec2013-06-25 11:53:37 -07005062 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005063 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005064
Paul Menage8bab8dd2008-04-04 14:29:57 -07005065 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005066 /*
5067 * ideally we don't want subsystems moving around while we do this.
5068 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5069 * subsys/hierarchy state.
5070 */
Paul Menagea4243162007-10-18 23:39:35 -07005071 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005072
5073 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005074 seq_printf(m, "%s\t%d\t%d\t%d\n",
5075 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005076 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005077
Paul Menagea4243162007-10-18 23:39:35 -07005078 mutex_unlock(&cgroup_mutex);
5079 return 0;
5080}
5081
5082static int cgroupstats_open(struct inode *inode, struct file *file)
5083{
Al Viro9dce07f2008-03-29 03:07:28 +00005084 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005085}
5086
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005087static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005088 .open = cgroupstats_open,
5089 .read = seq_read,
5090 .llseek = seq_lseek,
5091 .release = single_release,
5092};
5093
Paul Menageb4f48b62007-10-18 23:39:33 -07005094/**
5095 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005096 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005097 *
5098 * Description: A task inherits its parent's cgroup at fork().
5099 *
5100 * A pointer to the shared css_set was automatically copied in
5101 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005102 * it was not made under the protection of RCU or cgroup_mutex, so
5103 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5104 * have already changed current->cgroups, allowing the previously
5105 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005106 *
5107 * At the point that cgroup_fork() is called, 'current' is the parent
5108 * task, and the passed argument 'child' points to the child task.
5109 */
5110void cgroup_fork(struct task_struct *child)
5111{
Tejun Heo9bb71302012-10-18 17:52:07 -07005112 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005113 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005114 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005115 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005116 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005117}
5118
5119/**
Li Zefana043e3b2008-02-23 15:24:09 -08005120 * cgroup_post_fork - called on a new task after adding it to the task list
5121 * @child: the task in question
5122 *
Tejun Heo5edee612012-10-16 15:03:14 -07005123 * Adds the task to the list running through its css_set if necessary and
5124 * call the subsystem fork() callbacks. Has to be after the task is
5125 * visible on the task list in case we race with the first call to
5126 * cgroup_iter_start() - to guarantee that the new task ends up on its
5127 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005128 */
Paul Menage817929e2007-10-18 23:39:36 -07005129void cgroup_post_fork(struct task_struct *child)
5130{
Tejun Heo30159ec2013-06-25 11:53:37 -07005131 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005132 int i;
5133
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005134 /*
5135 * use_task_css_set_links is set to 1 before we walk the tasklist
5136 * under the tasklist_lock and we read it here after we added the child
5137 * to the tasklist under the tasklist_lock as well. If the child wasn't
5138 * yet in the tasklist when we walked through it from
5139 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5140 * should be visible now due to the paired locking and barriers implied
5141 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5142 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5143 * lock on fork.
5144 */
Paul Menage817929e2007-10-18 23:39:36 -07005145 if (use_task_css_set_links) {
5146 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005147 task_lock(child);
5148 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005149 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005150 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005151 write_unlock(&css_set_lock);
5152 }
Tejun Heo5edee612012-10-16 15:03:14 -07005153
5154 /*
5155 * Call ss->fork(). This must happen after @child is linked on
5156 * css_set; otherwise, @child might change state between ->fork()
5157 * and addition to css_set.
5158 */
5159 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005160 /*
5161 * fork/exit callbacks are supported only for builtin
5162 * subsystems, and the builtin section of the subsys
5163 * array is immutable, so we don't need to lock the
5164 * subsys array here. On the other hand, modular section
5165 * of the array can be freed at module unload, so we
5166 * can't touch that.
5167 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005168 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005169 if (ss->fork)
5170 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005171 }
Paul Menage817929e2007-10-18 23:39:36 -07005172}
Tejun Heo5edee612012-10-16 15:03:14 -07005173
Paul Menage817929e2007-10-18 23:39:36 -07005174/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005175 * cgroup_exit - detach cgroup from exiting task
5176 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005177 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005178 *
5179 * Description: Detach cgroup from @tsk and release it.
5180 *
5181 * Note that cgroups marked notify_on_release force every task in
5182 * them to take the global cgroup_mutex mutex when exiting.
5183 * This could impact scaling on very large systems. Be reluctant to
5184 * use notify_on_release cgroups where very high task exit scaling
5185 * is required on large systems.
5186 *
5187 * the_top_cgroup_hack:
5188 *
5189 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5190 *
5191 * We call cgroup_exit() while the task is still competent to
5192 * handle notify_on_release(), then leave the task attached to the
5193 * root cgroup in each hierarchy for the remainder of its exit.
5194 *
5195 * To do this properly, we would increment the reference count on
5196 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5197 * code we would add a second cgroup function call, to drop that
5198 * reference. This would just create an unnecessary hot spot on
5199 * the top_cgroup reference count, to no avail.
5200 *
5201 * Normally, holding a reference to a cgroup without bumping its
5202 * count is unsafe. The cgroup could go away, or someone could
5203 * attach us to a different cgroup, decrementing the count on
5204 * the first cgroup that we never incremented. But in this case,
5205 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005206 * which wards off any cgroup_attach_task() attempts, or task is a failed
5207 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005208 */
5209void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5210{
Tejun Heo30159ec2013-06-25 11:53:37 -07005211 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005212 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005213 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005214
5215 /*
5216 * Unlink from the css_set task list if necessary.
5217 * Optimistically check cg_list before taking
5218 * css_set_lock
5219 */
5220 if (!list_empty(&tsk->cg_list)) {
5221 write_lock(&css_set_lock);
5222 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005223 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005224 write_unlock(&css_set_lock);
5225 }
5226
Paul Menageb4f48b62007-10-18 23:39:33 -07005227 /* Reassign the task to the init_css_set. */
5228 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005229 cset = task_css_set(tsk);
5230 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005231
5232 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005233 /*
5234 * fork/exit callbacks are supported only for builtin
5235 * subsystems, see cgroup_post_fork() for details.
5236 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005237 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005238 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005239 struct cgroup_subsys_state *old_css = cset->subsys[i];
5240 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005241
Tejun Heoeb954192013-08-08 20:11:23 -04005242 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005243 }
5244 }
5245 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005246 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005247
Tejun Heo5abb8852013-06-12 21:04:49 -07005248 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005249}
Paul Menage697f4162007-10-18 23:39:34 -07005250
Paul Menagebd89aab2007-10-18 23:40:44 -07005251static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005252{
Li Zefanf50daa72013-03-01 15:06:07 +08005253 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d8282013-06-12 21:04:55 -07005254 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005255 /*
5256 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005257 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005258 * it now
5259 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005260 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005261
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005262 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005263 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005264 list_empty(&cgrp->release_list)) {
5265 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005266 need_schedule_work = 1;
5267 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005268 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005269 if (need_schedule_work)
5270 schedule_work(&release_agent_work);
5271 }
5272}
5273
Paul Menage81a6a5c2007-10-18 23:39:38 -07005274/*
5275 * Notify userspace when a cgroup is released, by running the
5276 * configured release agent with the name of the cgroup (path
5277 * relative to the root of cgroup file system) as the argument.
5278 *
5279 * Most likely, this user command will try to rmdir this cgroup.
5280 *
5281 * This races with the possibility that some other task will be
5282 * attached to this cgroup before it is removed, or that some other
5283 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5284 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5285 * unused, and this cgroup will be reprieved from its death sentence,
5286 * to continue to serve a useful existence. Next time it's released,
5287 * we will get notified again, if it still has 'notify_on_release' set.
5288 *
5289 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5290 * means only wait until the task is successfully execve()'d. The
5291 * separate release agent task is forked by call_usermodehelper(),
5292 * then control in this thread returns here, without waiting for the
5293 * release agent task. We don't bother to wait because the caller of
5294 * this routine has no use for the exit status of the release agent
5295 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005296 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005297static void cgroup_release_agent(struct work_struct *work)
5298{
5299 BUG_ON(work != &release_agent_work);
5300 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005301 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005302 while (!list_empty(&release_list)) {
5303 char *argv[3], *envp[3];
5304 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005305 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005306 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005307 struct cgroup,
5308 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005309 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005310 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005311 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005312 if (!pathbuf)
5313 goto continue_free;
5314 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5315 goto continue_free;
5316 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5317 if (!agentbuf)
5318 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005319
5320 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005321 argv[i++] = agentbuf;
5322 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005323 argv[i] = NULL;
5324
5325 i = 0;
5326 /* minimal command environment */
5327 envp[i++] = "HOME=/";
5328 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5329 envp[i] = NULL;
5330
5331 /* Drop the lock while we invoke the usermode helper,
5332 * since the exec could involve hitting disk and hence
5333 * be a slow process */
5334 mutex_unlock(&cgroup_mutex);
5335 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005336 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005337 continue_free:
5338 kfree(pathbuf);
5339 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005340 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005341 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005342 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005343 mutex_unlock(&cgroup_mutex);
5344}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005345
5346static int __init cgroup_disable(char *str)
5347{
Tejun Heo30159ec2013-06-25 11:53:37 -07005348 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005349 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005350 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005351
5352 while ((token = strsep(&str, ",")) != NULL) {
5353 if (!*token)
5354 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005355
Tejun Heo30159ec2013-06-25 11:53:37 -07005356 /*
5357 * cgroup_disable, being at boot time, can't know about
5358 * module subsystems, so we don't worry about them.
5359 */
5360 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005361 if (!strcmp(token, ss->name)) {
5362 ss->disabled = 1;
5363 printk(KERN_INFO "Disabling %s control group"
5364 " subsystem\n", ss->name);
5365 break;
5366 }
5367 }
5368 }
5369 return 1;
5370}
5371__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005372
5373/*
5374 * Functons for CSS ID.
5375 */
5376
Tejun Heo54766d42013-06-12 21:04:53 -07005377/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005378unsigned short css_id(struct cgroup_subsys_state *css)
5379{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005380 struct css_id *cssid;
5381
5382 /*
5383 * This css_id() can return correct value when somone has refcnt
5384 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5385 * it's unchanged until freed.
5386 */
Tejun Heod3daf282013-06-13 19:39:16 -07005387 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005388
5389 if (cssid)
5390 return cssid->id;
5391 return 0;
5392}
Ben Blum67523c42010-03-10 15:22:11 -08005393EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005394
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005395/**
5396 * css_is_ancestor - test "root" css is an ancestor of "child"
5397 * @child: the css to be tested.
5398 * @root: the css supporsed to be an ancestor of the child.
5399 *
5400 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005401 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005402 * But, considering usual usage, the csses should be valid objects after test.
5403 * Assuming that the caller will do some action to the child if this returns
5404 * returns true, the caller must take "child";s reference count.
5405 * If "child" is valid object and this returns true, "root" is valid, too.
5406 */
5407
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005408bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005409 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005410{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005411 struct css_id *child_id;
5412 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005413
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005414 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005415 if (!child_id)
5416 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005417 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005418 if (!root_id)
5419 return false;
5420 if (child_id->depth < root_id->depth)
5421 return false;
5422 if (child_id->stack[root_id->depth] != root_id->id)
5423 return false;
5424 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005425}
5426
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005427void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5428{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005429 struct css_id *id = rcu_dereference_protected(css->id, true);
5430
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005431 /* When this is called before css_id initialization, id can be NULL */
5432 if (!id)
5433 return;
5434
5435 BUG_ON(!ss->use_id);
5436
5437 rcu_assign_pointer(id->css, NULL);
5438 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005439 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005440 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005441 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005442 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005443}
Ben Blum67523c42010-03-10 15:22:11 -08005444EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005445
5446/*
5447 * This is called by init or create(). Then, calls to this function are
5448 * always serialized (By cgroup_mutex() at create()).
5449 */
5450
5451static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5452{
5453 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005454 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005455
5456 BUG_ON(!ss->use_id);
5457
5458 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5459 newid = kzalloc(size, GFP_KERNEL);
5460 if (!newid)
5461 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005462
5463 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005464 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005465 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005466 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005467 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005468 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005469
5470 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005471 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005472 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005473
Tejun Heod228d9e2013-02-27 17:04:54 -08005474 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005475 newid->depth = depth;
5476 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005477err_out:
5478 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005479 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005480
5481}
5482
Ben Blume6a11052010-03-10 15:22:09 -08005483static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5484 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005485{
5486 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005487
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005488 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005489 idr_init(&ss->idr);
5490
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005491 newid = get_new_cssid(ss, 0);
5492 if (IS_ERR(newid))
5493 return PTR_ERR(newid);
5494
5495 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005496 RCU_INIT_POINTER(newid->css, rootcss);
5497 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005498 return 0;
5499}
5500
5501static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5502 struct cgroup *child)
5503{
5504 int subsys_id, i, depth = 0;
5505 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005506 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005507
5508 subsys_id = ss->subsys_id;
5509 parent_css = parent->subsys[subsys_id];
5510 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005511 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005512 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005513
5514 child_id = get_new_cssid(ss, depth);
5515 if (IS_ERR(child_id))
5516 return PTR_ERR(child_id);
5517
5518 for (i = 0; i < depth; i++)
5519 child_id->stack[i] = parent_id->stack[i];
5520 child_id->stack[depth] = child_id->id;
5521 /*
5522 * child_id->css pointer will be set after this cgroup is available
5523 * see cgroup_populate_dir()
5524 */
5525 rcu_assign_pointer(child_css->id, child_id);
5526
5527 return 0;
5528}
5529
5530/**
5531 * css_lookup - lookup css by id
5532 * @ss: cgroup subsys to be looked into.
5533 * @id: the id
5534 *
5535 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5536 * NULL if not. Should be called under rcu_read_lock()
5537 */
5538struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5539{
5540 struct css_id *cssid = NULL;
5541
5542 BUG_ON(!ss->use_id);
5543 cssid = idr_find(&ss->idr, id);
5544
5545 if (unlikely(!cssid))
5546 return NULL;
5547
5548 return rcu_dereference(cssid->css);
5549}
Ben Blum67523c42010-03-10 15:22:11 -08005550EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005551
Stephane Eraniane5d13672011-02-14 11:20:01 +02005552/*
5553 * get corresponding css from file open on cgroupfs directory
5554 */
5555struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5556{
5557 struct cgroup *cgrp;
5558 struct inode *inode;
5559 struct cgroup_subsys_state *css;
5560
Al Viro496ad9a2013-01-23 17:07:38 -05005561 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005562 /* check in cgroup filesystem dir */
5563 if (inode->i_op != &cgroup_dir_inode_operations)
5564 return ERR_PTR(-EBADF);
5565
5566 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5567 return ERR_PTR(-EINVAL);
5568
5569 /* get cgroup */
5570 cgrp = __d_cgrp(f->f_dentry);
5571 css = cgrp->subsys[id];
5572 return css ? css : ERR_PTR(-ENOENT);
5573}
5574
Paul Menagefe693432009-09-23 15:56:20 -07005575#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005576static struct cgroup_subsys_state *
5577debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005578{
5579 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5580
5581 if (!css)
5582 return ERR_PTR(-ENOMEM);
5583
5584 return css;
5585}
5586
Tejun Heoeb954192013-08-08 20:11:23 -04005587static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005588{
Tejun Heoeb954192013-08-08 20:11:23 -04005589 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005590}
5591
Tejun Heo182446d2013-08-08 20:11:24 -04005592static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5593 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005594{
Tejun Heo182446d2013-08-08 20:11:24 -04005595 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005596}
5597
Tejun Heo182446d2013-08-08 20:11:24 -04005598static u64 current_css_set_read(struct cgroup_subsys_state *css,
5599 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005600{
5601 return (u64)(unsigned long)current->cgroups;
5602}
5603
Tejun Heo182446d2013-08-08 20:11:24 -04005604static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005605 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005606{
5607 u64 count;
5608
5609 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005610 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005611 rcu_read_unlock();
5612 return count;
5613}
5614
Tejun Heo182446d2013-08-08 20:11:24 -04005615static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005616 struct cftype *cft,
5617 struct seq_file *seq)
5618{
Tejun Heo69d02062013-06-12 21:04:50 -07005619 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005620 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005621
5622 read_lock(&css_set_lock);
5623 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005624 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005625 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005626 struct cgroup *c = link->cgrp;
5627 const char *name;
5628
5629 if (c->dentry)
5630 name = c->dentry->d_name.name;
5631 else
5632 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005633 seq_printf(seq, "Root %d group %s\n",
5634 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005635 }
5636 rcu_read_unlock();
5637 read_unlock(&css_set_lock);
5638 return 0;
5639}
5640
5641#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005642static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5643 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005644{
Tejun Heo69d02062013-06-12 21:04:50 -07005645 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005646
5647 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005648 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005649 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005650 struct task_struct *task;
5651 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005652 seq_printf(seq, "css_set %p\n", cset);
5653 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005654 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5655 seq_puts(seq, " ...\n");
5656 break;
5657 } else {
5658 seq_printf(seq, " task %d\n",
5659 task_pid_vnr(task));
5660 }
5661 }
5662 }
5663 read_unlock(&css_set_lock);
5664 return 0;
5665}
5666
Tejun Heo182446d2013-08-08 20:11:24 -04005667static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005668{
Tejun Heo182446d2013-08-08 20:11:24 -04005669 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005670}
5671
5672static struct cftype debug_files[] = {
5673 {
Paul Menagefe693432009-09-23 15:56:20 -07005674 .name = "taskcount",
5675 .read_u64 = debug_taskcount_read,
5676 },
5677
5678 {
5679 .name = "current_css_set",
5680 .read_u64 = current_css_set_read,
5681 },
5682
5683 {
5684 .name = "current_css_set_refcount",
5685 .read_u64 = current_css_set_refcount_read,
5686 },
5687
5688 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005689 .name = "current_css_set_cg_links",
5690 .read_seq_string = current_css_set_cg_links_read,
5691 },
5692
5693 {
5694 .name = "cgroup_css_links",
5695 .read_seq_string = cgroup_css_links_read,
5696 },
5697
5698 {
Paul Menagefe693432009-09-23 15:56:20 -07005699 .name = "releasable",
5700 .read_u64 = releasable_read,
5701 },
Paul Menagefe693432009-09-23 15:56:20 -07005702
Tejun Heo4baf6e32012-04-01 12:09:55 -07005703 { } /* terminate */
5704};
Paul Menagefe693432009-09-23 15:56:20 -07005705
5706struct cgroup_subsys debug_subsys = {
5707 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005708 .css_alloc = debug_css_alloc,
5709 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005710 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005711 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005712};
5713#endif /* CONFIG_CGROUP_DEBUG */