blob: 1bf4f7a12703eb765c8fac76b3ba0406997eea7d [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;
Tejun Heo105347b2013-08-13 11:01:55 -0400120 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700121
122 /* file xattrs */
123 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700124};
125
126/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700127 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
128 * cgroup_subsys->use_id != 0.
129 */
130#define CSS_ID_MAX (65535)
131struct css_id {
132 /*
133 * The css to which this ID points. This pointer is set to valid value
134 * after cgroup is populated. If cgroup is removed, this will be NULL.
135 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800136 * is called after synchronize_rcu(). But for safe use, css_tryget()
137 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700138 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100139 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700140 /*
141 * ID of this css.
142 */
143 unsigned short id;
144 /*
145 * Depth in hierarchy which this ID belongs to.
146 */
147 unsigned short depth;
148 /*
149 * ID is freed by RCU. (and lookup routine is RCU safe.)
150 */
151 struct rcu_head rcu_head;
152 /*
153 * Hierarchy of CSS ID belongs to.
154 */
155 unsigned short stack[0]; /* Array of Length (depth+1) */
156};
157
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800158/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300159 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800160 */
161struct cgroup_event {
162 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400163 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800164 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400165 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800166 /*
167 * Control file which the event associated.
168 */
169 struct cftype *cft;
170 /*
171 * eventfd to signal userspace about the event.
172 */
173 struct eventfd_ctx *eventfd;
174 /*
175 * Each of these stored in a list by the cgroup.
176 */
177 struct list_head list;
178 /*
179 * All fields below needed to unregister event when
180 * userspace closes eventfd.
181 */
182 poll_table pt;
183 wait_queue_head_t *wqh;
184 wait_queue_t wait;
185 struct work_struct remove;
186};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700187
Paul Menageddbcc7e2007-10-18 23:39:30 -0700188/* The list of hierarchy roots */
189
Tejun Heo9871bf92013-06-24 15:21:47 -0700190static LIST_HEAD(cgroup_roots);
191static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700192
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700193/*
194 * Hierarchy ID allocation and mapping. It follows the same exclusion
195 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
196 * writes, either for reads.
197 */
Tejun Heo1a574232013-04-14 11:36:58 -0700198static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700199
Li Zefan65dff752013-03-01 15:01:56 +0800200static struct cgroup_name root_cgroup_name = { .name = "/" };
201
Li Zefan794611a2013-06-18 18:53:53 +0800202/*
203 * Assign a monotonically increasing serial number to cgroups. It
204 * guarantees cgroups with bigger numbers are newer than those with smaller
205 * numbers. Also, as cgroups are always appended to the parent's
206 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700207 * the ascending serial number order on the list. Protected by
208 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800209 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700210static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800211
Paul Menageddbcc7e2007-10-18 23:39:30 -0700212/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800213 * check for fork/exit handlers to call. This avoids us having to do
214 * extra work in the fork/exit path if none of the subsystems need to
215 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700216 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700217static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700218
Tejun Heo628f7cd2013-06-28 16:24:11 -0700219static struct cftype cgroup_base_files[];
220
Tejun Heof20104d2013-08-13 20:22:50 -0400221static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800222static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400223static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
224 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800225
Tejun Heo95109b62013-08-08 20:11:27 -0400226/**
227 * cgroup_css - obtain a cgroup's css for the specified subsystem
228 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400229 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400230 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400231 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
232 * function must be called either under cgroup_mutex or rcu_read_lock() and
233 * the caller is responsible for pinning the returned css if it wants to
234 * keep accessing it outside the said locks. This function may return
235 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400236 */
237static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400238 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400239{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400240 if (ss)
241 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
242 lockdep_is_held(&cgroup_mutex));
243 else
244 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400245}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700246
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700248static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700249{
Tejun Heo54766d42013-06-12 21:04:53 -0700250 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700251}
252
Li Zefan78574cf2013-04-08 19:00:38 -0700253/**
254 * cgroup_is_descendant - test ancestry
255 * @cgrp: the cgroup to be tested
256 * @ancestor: possible ancestor of @cgrp
257 *
258 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
259 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
260 * and @ancestor are accessible.
261 */
262bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
263{
264 while (cgrp) {
265 if (cgrp == ancestor)
266 return true;
267 cgrp = cgrp->parent;
268 }
269 return false;
270}
271EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700272
Adrian Bunke9685a02008-02-07 00:13:46 -0800273static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700274{
275 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700276 (1 << CGRP_RELEASABLE) |
277 (1 << CGRP_NOTIFY_ON_RELEASE);
278 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700279}
280
Adrian Bunke9685a02008-02-07 00:13:46 -0800281static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700282{
Paul Menagebd89aab2007-10-18 23:40:44 -0700283 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700284}
285
Tejun Heo30159ec2013-06-25 11:53:37 -0700286/**
287 * for_each_subsys - iterate all loaded cgroup subsystems
288 * @ss: the iteration cursor
289 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
290 *
291 * Should be called under cgroup_mutex.
292 */
293#define for_each_subsys(ss, i) \
294 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
295 if (({ lockdep_assert_held(&cgroup_mutex); \
296 !((ss) = cgroup_subsys[i]); })) { } \
297 else
298
299/**
300 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
301 * @ss: the iteration cursor
302 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
303 *
304 * Bulit-in subsystems are always present and iteration itself doesn't
305 * require any synchronization.
306 */
307#define for_each_builtin_subsys(ss, i) \
308 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
309 (((ss) = cgroup_subsys[i]) || true); (i)++)
310
Tejun Heo5549c492013-06-24 15:21:48 -0700311/* iterate each subsystem attached to a hierarchy */
312#define for_each_root_subsys(root, ss) \
313 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700314
Tejun Heo5549c492013-06-24 15:21:48 -0700315/* iterate across the active hierarchies */
316#define for_each_active_root(root) \
317 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700318
Tejun Heof6ea9372012-04-01 12:09:55 -0700319static inline struct cgroup *__d_cgrp(struct dentry *dentry)
320{
321 return dentry->d_fsdata;
322}
323
Tejun Heo05ef1d72012-04-01 12:09:56 -0700324static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700325{
326 return dentry->d_fsdata;
327}
328
Tejun Heo05ef1d72012-04-01 12:09:56 -0700329static inline struct cftype *__d_cft(struct dentry *dentry)
330{
331 return __d_cfe(dentry)->type;
332}
333
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700334/**
335 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
336 * @cgrp: the cgroup to be checked for liveness
337 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700338 * On success, returns true; the mutex should be later unlocked. On
339 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700340 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700341static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700342{
343 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700344 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700345 mutex_unlock(&cgroup_mutex);
346 return false;
347 }
348 return true;
349}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700350
Paul Menage81a6a5c2007-10-18 23:39:38 -0700351/* the list of cgroups eligible for automatic release. Protected by
352 * release_list_lock */
353static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200354static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700355static void cgroup_release_agent(struct work_struct *work);
356static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700357static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700358
Tejun Heo69d02062013-06-12 21:04:50 -0700359/*
360 * A cgroup can be associated with multiple css_sets as different tasks may
361 * belong to different cgroups on different hierarchies. In the other
362 * direction, a css_set is naturally associated with multiple cgroups.
363 * This M:N relationship is represented by the following link structure
364 * which exists for each association and allows traversing the associations
365 * from both sides.
366 */
367struct cgrp_cset_link {
368 /* the cgroup and css_set this link associates */
369 struct cgroup *cgrp;
370 struct css_set *cset;
371
372 /* list of cgrp_cset_links anchored at cgrp->cset_links */
373 struct list_head cset_link;
374
375 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
376 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700377};
378
379/* The default css_set - used by init and its children prior to any
380 * hierarchies being mounted. It contains a pointer to the root state
381 * for each subsystem. Also used to anchor the list of css_sets. Not
382 * reference-counted, to improve performance when child cgroups
383 * haven't been created.
384 */
385
386static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700387static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700388
Ben Blume6a11052010-03-10 15:22:09 -0800389static int cgroup_init_idr(struct cgroup_subsys *ss,
390 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700391
Tejun Heo0942eee2013-08-08 20:11:26 -0400392/*
393 * css_set_lock protects the list of css_set objects, and the chain of
394 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400395 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400396 */
Paul Menage817929e2007-10-18 23:39:36 -0700397static DEFINE_RWLOCK(css_set_lock);
398static int css_set_count;
399
Paul Menage7717f7b2009-09-23 15:56:22 -0700400/*
401 * hash table for cgroup groups. This improves the performance to find
402 * an existing css_set. This hash doesn't (currently) take into
403 * account cgroups in empty hierarchies.
404 */
Li Zefan472b1052008-04-29 01:00:11 -0700405#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800406static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700407
Li Zefan0ac801f2013-01-10 11:49:27 +0800408static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700409{
Li Zefan0ac801f2013-01-10 11:49:27 +0800410 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700411 struct cgroup_subsys *ss;
412 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700413
Tejun Heo30159ec2013-06-25 11:53:37 -0700414 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800415 key += (unsigned long)css[i];
416 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700417
Li Zefan0ac801f2013-01-10 11:49:27 +0800418 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700419}
420
Tejun Heo0942eee2013-08-08 20:11:26 -0400421/*
422 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400423 * until after the first call to css_task_iter_start(). This reduces the
424 * fork()/exit() overhead for people who have cgroups compiled into their
425 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400426 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700427static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700428
Tejun Heo5abb8852013-06-12 21:04:49 -0700429static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700430{
Tejun Heo69d02062013-06-12 21:04:50 -0700431 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700432
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700433 /*
434 * Ensure that the refcount doesn't hit zero while any readers
435 * can see it. Similar to atomic_dec_and_lock(), but for an
436 * rwlock
437 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700439 return;
440 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700441 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700442 write_unlock(&css_set_lock);
443 return;
444 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700445
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700446 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700447 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700448 css_set_count--;
449
Tejun Heo69d02062013-06-12 21:04:50 -0700450 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700451 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700452
Tejun Heo69d02062013-06-12 21:04:50 -0700453 list_del(&link->cset_link);
454 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800455
Tejun Heoddd69142013-06-12 21:04:54 -0700456 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700457 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700458 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700459 set_bit(CGRP_RELEASABLE, &cgrp->flags);
460 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700461 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700462
463 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700464 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700465
466 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700467 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700468}
469
470/*
471 * refcounted get/put for css_set objects
472 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700473static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700474{
Tejun Heo5abb8852013-06-12 21:04:49 -0700475 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700476}
477
Tejun Heo5abb8852013-06-12 21:04:49 -0700478static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700479{
Tejun Heo5abb8852013-06-12 21:04:49 -0700480 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700481}
482
Tejun Heo5abb8852013-06-12 21:04:49 -0700483static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700484{
Tejun Heo5abb8852013-06-12 21:04:49 -0700485 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700486}
487
Tejun Heob326f9d2013-06-24 15:21:48 -0700488/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700489 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700490 * @cset: candidate css_set being tested
491 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700492 * @new_cgrp: cgroup that's being entered by the task
493 * @template: desired set of css pointers in css_set (pre-calculated)
494 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800495 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700496 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
497 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700498static bool compare_css_sets(struct css_set *cset,
499 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700500 struct cgroup *new_cgrp,
501 struct cgroup_subsys_state *template[])
502{
503 struct list_head *l1, *l2;
504
Tejun Heo5abb8852013-06-12 21:04:49 -0700505 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700506 /* Not all subsystems matched */
507 return false;
508 }
509
510 /*
511 * Compare cgroup pointers in order to distinguish between
512 * different cgroups in heirarchies with no subsystems. We
513 * could get by with just this check alone (and skip the
514 * memcmp above) but on most setups the memcmp check will
515 * avoid the need for this more expensive check on almost all
516 * candidates.
517 */
518
Tejun Heo69d02062013-06-12 21:04:50 -0700519 l1 = &cset->cgrp_links;
520 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700521 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700522 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700523 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700524
525 l1 = l1->next;
526 l2 = l2->next;
527 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700528 if (l1 == &cset->cgrp_links) {
529 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700530 break;
531 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700532 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700533 }
534 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700535 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
536 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
537 cgrp1 = link1->cgrp;
538 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700539 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700541
542 /*
543 * If this hierarchy is the hierarchy of the cgroup
544 * that's changing, then we need to check that this
545 * css_set points to the new cgroup; if it's any other
546 * hierarchy, then this css_set should point to the
547 * same cgroup as the old css_set.
548 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700549 if (cgrp1->root == new_cgrp->root) {
550 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700551 return false;
552 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700553 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700554 return false;
555 }
556 }
557 return true;
558}
559
Tejun Heob326f9d2013-06-24 15:21:48 -0700560/**
561 * find_existing_css_set - init css array and find the matching css_set
562 * @old_cset: the css_set that we're using before the cgroup transition
563 * @cgrp: the cgroup that we're moving into
564 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700565 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700566static struct css_set *find_existing_css_set(struct css_set *old_cset,
567 struct cgroup *cgrp,
568 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700569{
Paul Menagebd89aab2007-10-18 23:40:44 -0700570 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700571 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700572 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800573 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700574 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700575
Ben Blumaae8aab2010-03-10 15:22:07 -0800576 /*
577 * Build the set of subsystem state objects that we want to see in the
578 * new css_set. while subsystems can change globally, the entries here
579 * won't change, so no need for locking.
580 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700581 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400582 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700583 /* Subsystem is in this hierarchy. So we want
584 * the subsystem state from the new
585 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400586 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700587 } else {
588 /* Subsystem is not in this hierarchy, so we
589 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700590 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700591 }
592 }
593
Li Zefan0ac801f2013-01-10 11:49:27 +0800594 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700595 hash_for_each_possible(css_set_table, cset, hlist, key) {
596 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700597 continue;
598
599 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700600 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700601 }
Paul Menage817929e2007-10-18 23:39:36 -0700602
603 /* No existing cgroup group matched */
604 return NULL;
605}
606
Tejun Heo69d02062013-06-12 21:04:50 -0700607static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700608{
Tejun Heo69d02062013-06-12 21:04:50 -0700609 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700610
Tejun Heo69d02062013-06-12 21:04:50 -0700611 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
612 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700613 kfree(link);
614 }
615}
616
Tejun Heo69d02062013-06-12 21:04:50 -0700617/**
618 * allocate_cgrp_cset_links - allocate cgrp_cset_links
619 * @count: the number of links to allocate
620 * @tmp_links: list_head the allocated links are put on
621 *
622 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
623 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700624 */
Tejun Heo69d02062013-06-12 21:04:50 -0700625static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700626{
Tejun Heo69d02062013-06-12 21:04:50 -0700627 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700628 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700629
630 INIT_LIST_HEAD(tmp_links);
631
Li Zefan36553432008-07-29 22:33:19 -0700632 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700633 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700634 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700635 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700636 return -ENOMEM;
637 }
Tejun Heo69d02062013-06-12 21:04:50 -0700638 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700639 }
640 return 0;
641}
642
Li Zefanc12f65d2009-01-07 18:07:42 -0800643/**
644 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700645 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700646 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800647 * @cgrp: the destination cgroup
648 */
Tejun Heo69d02062013-06-12 21:04:50 -0700649static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
650 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800651{
Tejun Heo69d02062013-06-12 21:04:50 -0700652 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800653
Tejun Heo69d02062013-06-12 21:04:50 -0700654 BUG_ON(list_empty(tmp_links));
655 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
656 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700657 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700658 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700659 /*
660 * Always add links to the tail of the list so that the list
661 * is sorted by order of hierarchy creation
662 */
Tejun Heo69d02062013-06-12 21:04:50 -0700663 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800664}
665
Tejun Heob326f9d2013-06-24 15:21:48 -0700666/**
667 * find_css_set - return a new css_set with one cgroup updated
668 * @old_cset: the baseline css_set
669 * @cgrp: the cgroup to be updated
670 *
671 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
672 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700673 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700674static struct css_set *find_css_set(struct css_set *old_cset,
675 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700676{
Tejun Heob326f9d2013-06-24 15:21:48 -0700677 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700678 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700679 struct list_head tmp_links;
680 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800681 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700682
Tejun Heob326f9d2013-06-24 15:21:48 -0700683 lockdep_assert_held(&cgroup_mutex);
684
Paul Menage817929e2007-10-18 23:39:36 -0700685 /* First see if we already have a cgroup group that matches
686 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700687 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 cset = find_existing_css_set(old_cset, cgrp, template);
689 if (cset)
690 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700691 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700692
Tejun Heo5abb8852013-06-12 21:04:49 -0700693 if (cset)
694 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700695
Tejun Heof4f4be22013-06-12 21:04:51 -0700696 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700698 return NULL;
699
Tejun Heo69d02062013-06-12 21:04:50 -0700700 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700701 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700702 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700703 return NULL;
704 }
705
Tejun Heo5abb8852013-06-12 21:04:49 -0700706 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700707 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700708 INIT_LIST_HEAD(&cset->tasks);
709 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700710
711 /* Copy the set of subsystem state objects generated in
712 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700713 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700714
715 write_lock(&css_set_lock);
716 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700717 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700718 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700719
Paul Menage7717f7b2009-09-23 15:56:22 -0700720 if (c->root == cgrp->root)
721 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700722 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700723 }
Paul Menage817929e2007-10-18 23:39:36 -0700724
Tejun Heo69d02062013-06-12 21:04:50 -0700725 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700726
Paul Menage817929e2007-10-18 23:39:36 -0700727 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700728
729 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700730 key = css_set_hash(cset->subsys);
731 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700732
Paul Menage817929e2007-10-18 23:39:36 -0700733 write_unlock(&css_set_lock);
734
Tejun Heo5abb8852013-06-12 21:04:49 -0700735 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700736}
737
Paul Menageddbcc7e2007-10-18 23:39:30 -0700738/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700739 * Return the cgroup for "task" from the given hierarchy. Must be
740 * called with cgroup_mutex held.
741 */
742static struct cgroup *task_cgroup_from_root(struct task_struct *task,
743 struct cgroupfs_root *root)
744{
Tejun Heo5abb8852013-06-12 21:04:49 -0700745 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700746 struct cgroup *res = NULL;
747
748 BUG_ON(!mutex_is_locked(&cgroup_mutex));
749 read_lock(&css_set_lock);
750 /*
751 * No need to lock the task - since we hold cgroup_mutex the
752 * task can't change groups, so the only thing that can happen
753 * is that it exits and its css is set back to init_css_set.
754 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700755 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700756 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700757 res = &root->top_cgroup;
758 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700759 struct cgrp_cset_link *link;
760
761 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700762 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700763
Paul Menage7717f7b2009-09-23 15:56:22 -0700764 if (c->root == root) {
765 res = c;
766 break;
767 }
768 }
769 }
770 read_unlock(&css_set_lock);
771 BUG_ON(!res);
772 return res;
773}
774
775/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700776 * There is one global cgroup mutex. We also require taking
777 * task_lock() when dereferencing a task's cgroup subsys pointers.
778 * See "The task_lock() exception", at the end of this comment.
779 *
780 * A task must hold cgroup_mutex to modify cgroups.
781 *
782 * Any task can increment and decrement the count field without lock.
783 * So in general, code holding cgroup_mutex can't rely on the count
784 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800785 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700786 * means that no tasks are currently attached, therefore there is no
787 * way a task attached to that cgroup can fork (the other way to
788 * increment the count). So code holding cgroup_mutex can safely
789 * assume that if the count is zero, it will stay zero. Similarly, if
790 * a task holds cgroup_mutex on a cgroup with zero count, it
791 * knows that the cgroup won't be removed, as cgroup_rmdir()
792 * needs that mutex.
793 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700794 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
795 * (usually) take cgroup_mutex. These are the two most performance
796 * critical pieces of code here. The exception occurs on cgroup_exit(),
797 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
798 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800799 * to the release agent with the name of the cgroup (path relative to
800 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700801 *
802 * A cgroup can only be deleted if both its 'count' of using tasks
803 * is zero, and its list of 'children' cgroups is empty. Since all
804 * tasks in the system use _some_ cgroup, and since there is always at
805 * least one task in the system (init, pid == 1), therefore, top_cgroup
806 * always has either children cgroups and/or using tasks. So we don't
807 * need a special hack to ensure that top_cgroup cannot be deleted.
808 *
809 * The task_lock() exception
810 *
811 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800812 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800813 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814 * several performance critical places that need to reference
815 * task->cgroup without the expense of grabbing a system global
816 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800817 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700818 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
819 * the task_struct routinely used for such matters.
820 *
821 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800822 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700823 */
824
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825/*
826 * A couple of forward declarations required, due to cyclic reference loop:
827 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
828 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
829 * -> cgroup_mkdir.
830 */
831
Al Viro18bb1db2011-07-26 01:41:39 -0400832static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700833static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700834static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700835static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700836static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700837
838static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200839 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700840 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700841};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700842
Tejun Heo623f9262013-08-13 11:01:55 -0400843static int alloc_css_id(struct cgroup_subsys_state *child_css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700844
Al Viroa5e7ed32011-07-26 01:55:55 -0400845static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700846{
847 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700848
849 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400850 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700851 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100852 inode->i_uid = current_fsuid();
853 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700854 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
855 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
856 }
857 return inode;
858}
859
Li Zefan65dff752013-03-01 15:01:56 +0800860static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
861{
862 struct cgroup_name *name;
863
864 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
865 if (!name)
866 return NULL;
867 strcpy(name->name, dentry->d_name.name);
868 return name;
869}
870
Li Zefanbe445622013-01-24 14:31:42 +0800871static void cgroup_free_fn(struct work_struct *work)
872{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700873 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800874
875 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800876 cgrp->root->number_of_cgroups--;
877 mutex_unlock(&cgroup_mutex);
878
879 /*
Li Zefan415cf072013-04-08 14:35:02 +0800880 * We get a ref to the parent's dentry, and put the ref when
881 * this cgroup is being freed, so it's guaranteed that the
882 * parent won't be destroyed before its children.
883 */
884 dput(cgrp->parent->dentry);
885
886 /*
Li Zefanbe445622013-01-24 14:31:42 +0800887 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700888 * created the cgroup. This will free cgrp->root, if we are
889 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800890 */
891 deactivate_super(cgrp->root->sb);
892
893 /*
894 * if we're getting rid of the cgroup, refcount should ensure
895 * that there are no pidlists left.
896 */
897 BUG_ON(!list_empty(&cgrp->pidlists));
898
899 simple_xattrs_free(&cgrp->xattrs);
900
Li Zefan65dff752013-03-01 15:01:56 +0800901 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800902 kfree(cgrp);
903}
904
905static void cgroup_free_rcu(struct rcu_head *head)
906{
907 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
908
Tejun Heoea15f8c2013-06-13 19:27:42 -0700909 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
910 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800911}
912
Paul Menageddbcc7e2007-10-18 23:39:30 -0700913static void cgroup_diput(struct dentry *dentry, struct inode *inode)
914{
915 /* is dentry a directory ? if so, kfree() associated cgroup */
916 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700917 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800918
Tejun Heo54766d42013-06-12 21:04:53 -0700919 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800920 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700921 } else {
922 struct cfent *cfe = __d_cfe(dentry);
923 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
924
925 WARN_ONCE(!list_empty(&cfe->node) &&
926 cgrp != &cgrp->root->top_cgroup,
927 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700928 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700929 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700930 }
931 iput(inode);
932}
933
Al Viroc72a04e2011-01-14 05:31:45 +0000934static int cgroup_delete(const struct dentry *d)
935{
936 return 1;
937}
938
Paul Menageddbcc7e2007-10-18 23:39:30 -0700939static void remove_dir(struct dentry *d)
940{
941 struct dentry *parent = dget(d->d_parent);
942
943 d_delete(d);
944 simple_rmdir(parent->d_inode, d);
945 dput(parent);
946}
947
Li Zefan2739d3c2013-01-21 18:18:33 +0800948static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700949{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700950 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700951
Tejun Heo05ef1d72012-04-01 12:09:56 -0700952 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
953 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100954
Li Zefan2739d3c2013-01-21 18:18:33 +0800955 /*
956 * If we're doing cleanup due to failure of cgroup_create(),
957 * the corresponding @cfe may not exist.
958 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700959 list_for_each_entry(cfe, &cgrp->files, node) {
960 struct dentry *d = cfe->dentry;
961
962 if (cft && cfe->type != cft)
963 continue;
964
965 dget(d);
966 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700967 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700968 list_del_init(&cfe->node);
969 dput(d);
970
Li Zefan2739d3c2013-01-21 18:18:33 +0800971 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700972 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700973}
974
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400975/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700976 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700977 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978 * @subsys_mask: mask of the subsystem ids whose files should be removed
979 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700980static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700981{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400982 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700983 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700984
Tejun Heob420ba72013-07-12 12:34:02 -0700985 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400986 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700987
988 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400989 continue;
990 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400991 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400992 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700993}
994
995/*
996 * NOTE : the dentry must have been dget()'ed
997 */
998static void cgroup_d_remove_dir(struct dentry *dentry)
999{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001000 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001001
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001002 parent = dentry->d_parent;
1003 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001004 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001005 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001006 spin_unlock(&dentry->d_lock);
1007 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 remove_dir(dentry);
1009}
1010
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001011/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001012 * Call with cgroup_mutex held. Drops reference counts on modules, including
1013 * any duplicate ones that parse_cgroupfs_options took. If this function
1014 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001015 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001016static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001017 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001018{
Paul Menagebd89aab2007-10-18 23:40:44 -07001019 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001020 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001021 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001022 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001023
Ben Blumaae8aab2010-03-10 15:22:07 -08001024 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001025 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001026
Paul Menageddbcc7e2007-10-18 23:39:30 -07001027 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001028 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001029 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001031
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001032 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001033 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001034 ret = -EBUSY;
1035 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001036 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001037
1038 /* pin the module */
1039 if (!try_module_get(ss->module)) {
1040 ret = -ENOENT;
1041 goto out_put;
1042 }
1043 pinned |= 1 << i;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001044 }
1045
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001046 /* subsys could be missing if unloaded between parsing and here */
1047 if (added_mask != pinned) {
1048 ret = -ENOENT;
1049 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001050 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051
Tejun Heo31261212013-06-28 17:07:30 -07001052 ret = cgroup_populate_dir(cgrp, added_mask);
1053 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001054 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001055
1056 /*
1057 * Nothing can fail from this point on. Remove files for the
1058 * removed subsystems and rebind each subsystem.
1059 */
1060 cgroup_clear_dir(cgrp, removed_mask);
1061
Tejun Heo30159ec2013-06-25 11:53:37 -07001062 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001063 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001064
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001065 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001067 BUG_ON(cgroup_css(cgrp, ss));
1068 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1069 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001070
Tejun Heo73e80ed2013-08-13 11:01:55 -04001071 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001072 cgroup_css(cgroup_dummy_top, ss));
1073 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001074
Li Zefan33a68ac2009-01-07 18:07:42 -08001075 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001076 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001077 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001078 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001079
Ben Blumcf5d5942010-03-10 15:22:09 -08001080 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001081 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001082 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001083 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001084 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1085 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001086
Paul Menageddbcc7e2007-10-18 23:39:30 -07001087 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001088 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001089
Tejun Heoca8bdca2013-08-26 18:40:56 -04001090 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001091 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1092
Tejun Heo9871bf92013-06-24 15:21:47 -07001093 cgroup_subsys[i]->root = &cgroup_dummy_root;
1094 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001095
Ben Blumcf5d5942010-03-10 15:22:09 -08001096 /* subsystem is now free - drop reference on module */
1097 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001098 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001099 }
1100 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001101
Tejun Heo1672d042013-06-25 18:04:54 -07001102 /*
1103 * Mark @root has finished binding subsystems. @root->subsys_mask
1104 * now matches the bound subsystems.
1105 */
1106 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1107
Paul Menageddbcc7e2007-10-18 23:39:30 -07001108 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001109
1110out_put:
1111 for_each_subsys(ss, i)
1112 if (pinned & (1 << i))
1113 module_put(ss->module);
1114 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001115}
1116
Al Viro34c80b12011-12-08 21:32:45 -05001117static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001118{
Al Viro34c80b12011-12-08 21:32:45 -05001119 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001120 struct cgroup_subsys *ss;
1121
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001122 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001123 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001124 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001125 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1126 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001127 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001128 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001129 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001130 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001131 if (strlen(root->release_agent_path))
1132 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001133 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001134 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001135 if (strlen(root->name))
1136 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001137 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001138 return 0;
1139}
1140
1141struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001142 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001143 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001144 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001145 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001146 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001147 /* User explicitly requested empty subsystem */
1148 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001149
1150 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001151
Paul Menageddbcc7e2007-10-18 23:39:30 -07001152};
1153
Ben Blumaae8aab2010-03-10 15:22:07 -08001154/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001155 * Convert a hierarchy specifier into a bitmask of subsystems and
1156 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1157 * array. This function takes refcounts on subsystems to be used, unless it
1158 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001159 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001160static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001161{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001162 char *token, *o = data;
1163 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001164 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001165 struct cgroup_subsys *ss;
1166 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001167
Ben Blumaae8aab2010-03-10 15:22:07 -08001168 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1169
Li Zefanf9ab5b52009-06-17 16:26:33 -07001170#ifdef CONFIG_CPUSETS
1171 mask = ~(1UL << cpuset_subsys_id);
1172#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001173
Paul Menagec6d57f32009-09-23 15:56:19 -07001174 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001175
1176 while ((token = strsep(&o, ",")) != NULL) {
1177 if (!*token)
1178 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001179 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001180 /* Explicitly have no subsystems */
1181 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001182 continue;
1183 }
1184 if (!strcmp(token, "all")) {
1185 /* Mutually exclusive option 'all' + subsystem name */
1186 if (one_ss)
1187 return -EINVAL;
1188 all_ss = true;
1189 continue;
1190 }
Tejun Heo873fe092013-04-14 20:15:26 -07001191 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1192 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1193 continue;
1194 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001195 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001196 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001197 continue;
1198 }
1199 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001200 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001201 continue;
1202 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001203 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001204 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001205 continue;
1206 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001207 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001208 /* Specifying two release agents is forbidden */
1209 if (opts->release_agent)
1210 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001211 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001212 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001213 if (!opts->release_agent)
1214 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001215 continue;
1216 }
1217 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001218 const char *name = token + 5;
1219 /* Can't specify an empty name */
1220 if (!strlen(name))
1221 return -EINVAL;
1222 /* Must match [\w.-]+ */
1223 for (i = 0; i < strlen(name); i++) {
1224 char c = name[i];
1225 if (isalnum(c))
1226 continue;
1227 if ((c == '.') || (c == '-') || (c == '_'))
1228 continue;
1229 return -EINVAL;
1230 }
1231 /* Specifying two names is forbidden */
1232 if (opts->name)
1233 return -EINVAL;
1234 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001235 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001236 GFP_KERNEL);
1237 if (!opts->name)
1238 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001239
1240 continue;
1241 }
1242
Tejun Heo30159ec2013-06-25 11:53:37 -07001243 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001244 if (strcmp(token, ss->name))
1245 continue;
1246 if (ss->disabled)
1247 continue;
1248
1249 /* Mutually exclusive option 'all' + subsystem name */
1250 if (all_ss)
1251 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001252 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001253 one_ss = true;
1254
1255 break;
1256 }
1257 if (i == CGROUP_SUBSYS_COUNT)
1258 return -ENOENT;
1259 }
1260
1261 /*
1262 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001263 * otherwise if 'none', 'name=' and a subsystem name options
1264 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001265 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001266 if (all_ss || (!one_ss && !opts->none && !opts->name))
1267 for_each_subsys(ss, i)
1268 if (!ss->disabled)
1269 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001270
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001271 /* Consistency checks */
1272
Tejun Heo873fe092013-04-14 20:15:26 -07001273 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1274 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1275
1276 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1277 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1278 return -EINVAL;
1279 }
1280
1281 if (opts->cpuset_clone_children) {
1282 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1283 return -EINVAL;
1284 }
1285 }
1286
Li Zefanf9ab5b52009-06-17 16:26:33 -07001287 /*
1288 * Option noprefix was introduced just for backward compatibility
1289 * with the old cpuset, so we allow noprefix only if mounting just
1290 * the cpuset subsystem.
1291 */
Tejun Heo93438622013-04-14 20:15:25 -07001292 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001293 return -EINVAL;
1294
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001295
1296 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001297 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001298 return -EINVAL;
1299
1300 /*
1301 * We either have to specify by name or by subsystems. (So all
1302 * empty hierarchies must have a name).
1303 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001304 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001305 return -EINVAL;
1306
1307 return 0;
1308}
1309
1310static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1311{
1312 int ret = 0;
1313 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001314 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001315 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001316 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001317
Tejun Heo873fe092013-04-14 20:15:26 -07001318 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1319 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1320 return -EINVAL;
1321 }
1322
Paul Menagebd89aab2007-10-18 23:40:44 -07001323 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001324 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001325 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001326
1327 /* See what subsystems are wanted */
1328 ret = parse_cgroupfs_options(data, &opts);
1329 if (ret)
1330 goto out_unlock;
1331
Tejun Heoa8a648c2013-06-24 15:21:47 -07001332 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001333 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1334 task_tgid_nr(current), current->comm);
1335
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001336 added_mask = opts.subsys_mask & ~root->subsys_mask;
1337 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001338
Ben Blumcf5d5942010-03-10 15:22:09 -08001339 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001340 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001341 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001342 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1343 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1344 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001345 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001346 goto out_unlock;
1347 }
1348
Tejun Heof172e672013-06-28 17:07:30 -07001349 /* remounting is not allowed for populated hierarchies */
1350 if (root->number_of_cgroups > 1) {
1351 ret = -EBUSY;
Li Zefan0670e082009-04-02 16:57:30 -07001352 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001353 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001354
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001356 if (ret)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001358
Paul Menage81a6a5c2007-10-18 23:39:38 -07001359 if (opts.release_agent)
1360 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001361 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001362 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001363 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001364 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001365 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001366 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001367 return ret;
1368}
1369
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001370static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001371 .statfs = simple_statfs,
1372 .drop_inode = generic_delete_inode,
1373 .show_options = cgroup_show_options,
1374 .remount_fs = cgroup_remount,
1375};
1376
Paul Menagecc31edc2008-10-18 20:28:04 -07001377static void init_cgroup_housekeeping(struct cgroup *cgrp)
1378{
1379 INIT_LIST_HEAD(&cgrp->sibling);
1380 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001381 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001382 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001383 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001384 INIT_LIST_HEAD(&cgrp->pidlists);
1385 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001386 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001387 INIT_LIST_HEAD(&cgrp->event_list);
1388 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001389 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001390}
Paul Menagec6d57f32009-09-23 15:56:19 -07001391
Paul Menageddbcc7e2007-10-18 23:39:30 -07001392static void init_cgroup_root(struct cgroupfs_root *root)
1393{
Paul Menagebd89aab2007-10-18 23:40:44 -07001394 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001395
Paul Menageddbcc7e2007-10-18 23:39:30 -07001396 INIT_LIST_HEAD(&root->subsys_list);
1397 INIT_LIST_HEAD(&root->root_list);
1398 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001399 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001400 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001401 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001402 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001403}
1404
Tejun Heofc76df72013-06-25 11:53:37 -07001405static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001406{
Tejun Heo1a574232013-04-14 11:36:58 -07001407 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001408
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001409 lockdep_assert_held(&cgroup_mutex);
1410 lockdep_assert_held(&cgroup_root_mutex);
1411
Tejun Heofc76df72013-06-25 11:53:37 -07001412 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1413 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001414 if (id < 0)
1415 return id;
1416
1417 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001418 return 0;
1419}
1420
1421static void cgroup_exit_root_id(struct cgroupfs_root *root)
1422{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001423 lockdep_assert_held(&cgroup_mutex);
1424 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001425
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001426 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001427 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001428 root->hierarchy_id = 0;
1429 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001430}
1431
Paul Menageddbcc7e2007-10-18 23:39:30 -07001432static int cgroup_test_super(struct super_block *sb, void *data)
1433{
Paul Menagec6d57f32009-09-23 15:56:19 -07001434 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001435 struct cgroupfs_root *root = sb->s_fs_info;
1436
Paul Menagec6d57f32009-09-23 15:56:19 -07001437 /* If we asked for a name then it must match */
1438 if (opts->name && strcmp(opts->name, root->name))
1439 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001440
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001441 /*
1442 * If we asked for subsystems (or explicitly for no
1443 * subsystems) then they must match
1444 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001445 if ((opts->subsys_mask || opts->none)
1446 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001447 return 0;
1448
1449 return 1;
1450}
1451
Paul Menagec6d57f32009-09-23 15:56:19 -07001452static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1453{
1454 struct cgroupfs_root *root;
1455
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001456 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001457 return NULL;
1458
1459 root = kzalloc(sizeof(*root), GFP_KERNEL);
1460 if (!root)
1461 return ERR_PTR(-ENOMEM);
1462
1463 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001464
Tejun Heo1672d042013-06-25 18:04:54 -07001465 /*
1466 * We need to set @root->subsys_mask now so that @root can be
1467 * matched by cgroup_test_super() before it finishes
1468 * initialization; otherwise, competing mounts with the same
1469 * options may try to bind the same subsystems instead of waiting
1470 * for the first one leading to unexpected mount errors.
1471 * SUBSYS_BOUND will be set once actual binding is complete.
1472 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001473 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001474 root->flags = opts->flags;
1475 if (opts->release_agent)
1476 strcpy(root->release_agent_path, opts->release_agent);
1477 if (opts->name)
1478 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001479 if (opts->cpuset_clone_children)
1480 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001481 return root;
1482}
1483
Tejun Heofa3ca072013-04-14 11:36:56 -07001484static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001485{
Tejun Heofa3ca072013-04-14 11:36:56 -07001486 if (root) {
1487 /* hierarhcy ID shoulid already have been released */
1488 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001489
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001490 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001491 kfree(root);
1492 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001493}
1494
Paul Menageddbcc7e2007-10-18 23:39:30 -07001495static int cgroup_set_super(struct super_block *sb, void *data)
1496{
1497 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001498 struct cgroup_sb_opts *opts = data;
1499
1500 /* If we don't have a new root, we can't set up a new sb */
1501 if (!opts->new_root)
1502 return -EINVAL;
1503
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001504 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001505
1506 ret = set_anon_super(sb, NULL);
1507 if (ret)
1508 return ret;
1509
Paul Menagec6d57f32009-09-23 15:56:19 -07001510 sb->s_fs_info = opts->new_root;
1511 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001512
1513 sb->s_blocksize = PAGE_CACHE_SIZE;
1514 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1515 sb->s_magic = CGROUP_SUPER_MAGIC;
1516 sb->s_op = &cgroup_ops;
1517
1518 return 0;
1519}
1520
1521static int cgroup_get_rootdir(struct super_block *sb)
1522{
Al Viro0df6a632010-12-21 13:29:29 -05001523 static const struct dentry_operations cgroup_dops = {
1524 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001525 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001526 };
1527
Paul Menageddbcc7e2007-10-18 23:39:30 -07001528 struct inode *inode =
1529 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001530
1531 if (!inode)
1532 return -ENOMEM;
1533
Paul Menageddbcc7e2007-10-18 23:39:30 -07001534 inode->i_fop = &simple_dir_operations;
1535 inode->i_op = &cgroup_dir_inode_operations;
1536 /* directories start off with i_nlink == 2 (for "." entry) */
1537 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001538 sb->s_root = d_make_root(inode);
1539 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001540 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001541 /* for everything else we want ->d_op set */
1542 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001543 return 0;
1544}
1545
Al Virof7e83572010-07-26 13:23:11 +04001546static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001547 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001548 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001549{
1550 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001551 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001552 int ret = 0;
1553 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001554 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001555 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001556 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001557 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001558
1559 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001560 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001561 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001562 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001563 if (ret)
1564 goto out_err;
1565
1566 /*
1567 * Allocate a new cgroup root. We may not need it if we're
1568 * reusing an existing hierarchy.
1569 */
1570 new_root = cgroup_root_from_opts(&opts);
1571 if (IS_ERR(new_root)) {
1572 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001573 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001574 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001575 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001576
Paul Menagec6d57f32009-09-23 15:56:19 -07001577 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001578 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001579 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001580 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001581 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001582 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001583 }
1584
Paul Menagec6d57f32009-09-23 15:56:19 -07001585 root = sb->s_fs_info;
1586 BUG_ON(!root);
1587 if (root == opts.new_root) {
1588 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001589 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001590 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001591 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001592 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001593
1594 BUG_ON(sb->s_root != NULL);
1595
1596 ret = cgroup_get_rootdir(sb);
1597 if (ret)
1598 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001599 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001600
Paul Menage817929e2007-10-18 23:39:36 -07001601 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001602 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001603 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001604
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001605 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1606 0, 1, GFP_KERNEL);
1607 if (root_cgrp->id < 0)
1608 goto unlock_drop;
1609
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001610 /* Check for name clashes with existing mounts */
1611 ret = -EBUSY;
1612 if (strlen(root->name))
1613 for_each_active_root(existing_root)
1614 if (!strcmp(existing_root->name, root->name))
1615 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001616
Paul Menage817929e2007-10-18 23:39:36 -07001617 /*
1618 * We're accessing css_set_count without locking
1619 * css_set_lock here, but that's OK - it can only be
1620 * increased by someone holding cgroup_lock, and
1621 * that's us. The worst that can happen is that we
1622 * have some link structures left over
1623 */
Tejun Heo69d02062013-06-12 21:04:50 -07001624 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001625 if (ret)
1626 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001627
Tejun Heofc76df72013-06-25 11:53:37 -07001628 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1629 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001630 if (ret)
1631 goto unlock_drop;
1632
Tejun Heo31261212013-06-28 17:07:30 -07001633 sb->s_root->d_fsdata = root_cgrp;
1634 root_cgrp->dentry = sb->s_root;
1635
1636 /*
1637 * We're inside get_sb() and will call lookup_one_len() to
1638 * create the root files, which doesn't work if SELinux is
1639 * in use. The following cred dancing somehow works around
1640 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1641 * populating new cgroupfs mount") for more details.
1642 */
1643 cred = override_creds(&init_cred);
1644
Tejun Heo2bb566c2013-08-08 20:11:23 -04001645 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001646 if (ret)
1647 goto rm_base_files;
1648
Tejun Heoa8a648c2013-06-24 15:21:47 -07001649 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001650 if (ret)
1651 goto rm_base_files;
1652
1653 revert_creds(cred);
1654
Ben Blumcf5d5942010-03-10 15:22:09 -08001655 /*
1656 * There must be no failure case after here, since rebinding
1657 * takes care of subsystems' refcounts, which are explicitly
1658 * dropped in the failure exit path.
1659 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001660
Tejun Heo9871bf92013-06-24 15:21:47 -07001661 list_add(&root->root_list, &cgroup_roots);
1662 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001663
Paul Menage817929e2007-10-18 23:39:36 -07001664 /* Link the top cgroup in this hierarchy into all
1665 * the css_set objects */
1666 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001667 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001668 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001669 write_unlock(&css_set_lock);
1670
Tejun Heo69d02062013-06-12 21:04:50 -07001671 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001672
Li Zefanc12f65d2009-01-07 18:07:42 -08001673 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001674 BUG_ON(root->number_of_cgroups != 1);
1675
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001676 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001677 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001678 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001679 } else {
1680 /*
1681 * We re-used an existing hierarchy - the new root (if
1682 * any) is not needed
1683 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001684 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001685
Tejun Heoc7ba8282013-06-29 14:06:10 -07001686 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001687 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1688 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1689 ret = -EINVAL;
1690 goto drop_new_super;
1691 } else {
1692 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1693 }
Tejun Heo873fe092013-04-14 20:15:26 -07001694 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001695 }
1696
Paul Menagec6d57f32009-09-23 15:56:19 -07001697 kfree(opts.release_agent);
1698 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001699 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001700
Tejun Heo31261212013-06-28 17:07:30 -07001701 rm_base_files:
1702 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001703 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001704 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001705 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001706 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001707 mutex_unlock(&cgroup_root_mutex);
1708 mutex_unlock(&cgroup_mutex);
1709 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001710 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001711 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001712 out_err:
1713 kfree(opts.release_agent);
1714 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001715 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001716}
1717
1718static void cgroup_kill_sb(struct super_block *sb) {
1719 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001720 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001721 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001722 int ret;
1723
1724 BUG_ON(!root);
1725
1726 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001727 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001728
Tejun Heo31261212013-06-28 17:07:30 -07001729 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001730 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001731 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001732
1733 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001734 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1735 ret = rebind_subsystems(root, 0, root->subsys_mask);
1736 /* Shouldn't be able to fail ... */
1737 BUG_ON(ret);
1738 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001739
Paul Menage817929e2007-10-18 23:39:36 -07001740 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001741 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001742 * root cgroup
1743 */
1744 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001745
Tejun Heo69d02062013-06-12 21:04:50 -07001746 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1747 list_del(&link->cset_link);
1748 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001749 kfree(link);
1750 }
1751 write_unlock(&css_set_lock);
1752
Paul Menage839ec542009-01-29 14:25:22 -08001753 if (!list_empty(&root->root_list)) {
1754 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001755 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001756 }
Li Zefane5f6a862009-01-07 18:07:41 -08001757
Tejun Heofa3ca072013-04-14 11:36:56 -07001758 cgroup_exit_root_id(root);
1759
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001760 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001761 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001762 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001763
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001764 simple_xattrs_free(&cgrp->xattrs);
1765
Paul Menageddbcc7e2007-10-18 23:39:30 -07001766 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001767 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001768}
1769
1770static struct file_system_type cgroup_fs_type = {
1771 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001772 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001773 .kill_sb = cgroup_kill_sb,
1774};
1775
Greg KH676db4a2010-08-05 13:53:35 -07001776static struct kobject *cgroup_kobj;
1777
Li Zefana043e3b2008-02-23 15:24:09 -08001778/**
1779 * cgroup_path - generate the path of a cgroup
1780 * @cgrp: the cgroup in question
1781 * @buf: the buffer to write the path into
1782 * @buflen: the length of the buffer
1783 *
Li Zefan65dff752013-03-01 15:01:56 +08001784 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1785 *
1786 * We can't generate cgroup path using dentry->d_name, as accessing
1787 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1788 * inode's i_mutex, while on the other hand cgroup_path() can be called
1789 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001790 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001791int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001792{
Li Zefan65dff752013-03-01 15:01:56 +08001793 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001794 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001795
Tejun Heoda1f2962013-04-14 10:32:19 -07001796 if (!cgrp->parent) {
1797 if (strlcpy(buf, "/", buflen) >= buflen)
1798 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001799 return 0;
1800 }
1801
Tao Ma316eb662012-11-08 21:36:38 +08001802 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001803 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001804
Li Zefan65dff752013-03-01 15:01:56 +08001805 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001806 do {
Li Zefan65dff752013-03-01 15:01:56 +08001807 const char *name = cgroup_name(cgrp);
1808 int len;
1809
1810 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001811 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001812 goto out;
1813 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001814
Paul Menageddbcc7e2007-10-18 23:39:30 -07001815 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001816 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001817 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001818
1819 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001820 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001821 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001822 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001823out:
1824 rcu_read_unlock();
1825 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001826}
Ben Blum67523c42010-03-10 15:22:11 -08001827EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001828
Tejun Heo857a2be2013-04-14 20:50:08 -07001829/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001830 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001831 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 * @buf: the buffer to write the path into
1833 * @buflen: the length of the buffer
1834 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001835 * Determine @task's cgroup on the first (the one with the lowest non-zero
1836 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1837 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1838 * cgroup controller callbacks.
1839 *
1840 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001841 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001842int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001843{
1844 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001845 struct cgroup *cgrp;
1846 int hierarchy_id = 1, ret = 0;
1847
1848 if (buflen < 2)
1849 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001850
1851 mutex_lock(&cgroup_mutex);
1852
Tejun Heo913ffdb2013-07-11 16:34:48 -07001853 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1854
Tejun Heo857a2be2013-04-14 20:50:08 -07001855 if (root) {
1856 cgrp = task_cgroup_from_root(task, root);
1857 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001858 } else {
1859 /* if no hierarchy exists, everyone is in "/" */
1860 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001861 }
1862
1863 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001864 return ret;
1865}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001866EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001867
Ben Blum74a11662011-05-26 16:25:20 -07001868/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001869 * Control Group taskset
1870 */
Tejun Heo134d3372011-12-12 18:12:21 -08001871struct task_and_cgroup {
1872 struct task_struct *task;
1873 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001874 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001875};
1876
Tejun Heo2f7ee562011-12-12 18:12:21 -08001877struct cgroup_taskset {
1878 struct task_and_cgroup single;
1879 struct flex_array *tc_array;
1880 int tc_array_len;
1881 int idx;
1882 struct cgroup *cur_cgrp;
1883};
1884
1885/**
1886 * cgroup_taskset_first - reset taskset and return the first task
1887 * @tset: taskset of interest
1888 *
1889 * @tset iteration is initialized and the first task is returned.
1890 */
1891struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1892{
1893 if (tset->tc_array) {
1894 tset->idx = 0;
1895 return cgroup_taskset_next(tset);
1896 } else {
1897 tset->cur_cgrp = tset->single.cgrp;
1898 return tset->single.task;
1899 }
1900}
1901EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1902
1903/**
1904 * cgroup_taskset_next - iterate to the next task in taskset
1905 * @tset: taskset of interest
1906 *
1907 * Return the next task in @tset. Iteration must have been initialized
1908 * with cgroup_taskset_first().
1909 */
1910struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1911{
1912 struct task_and_cgroup *tc;
1913
1914 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1915 return NULL;
1916
1917 tc = flex_array_get(tset->tc_array, tset->idx++);
1918 tset->cur_cgrp = tc->cgrp;
1919 return tc->task;
1920}
1921EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1922
1923/**
Tejun Heod99c8722013-08-08 20:11:27 -04001924 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001925 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001926 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001927 *
Tejun Heod99c8722013-08-08 20:11:27 -04001928 * Return the css for the current (last returned) task of @tset for
1929 * subsystem specified by @subsys_id. This function must be preceded by
1930 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001931 */
Tejun Heod99c8722013-08-08 20:11:27 -04001932struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1933 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001934{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001935 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001936}
Tejun Heod99c8722013-08-08 20:11:27 -04001937EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001938
1939/**
1940 * cgroup_taskset_size - return the number of tasks in taskset
1941 * @tset: taskset of interest
1942 */
1943int cgroup_taskset_size(struct cgroup_taskset *tset)
1944{
1945 return tset->tc_array ? tset->tc_array_len : 1;
1946}
1947EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1948
1949
Ben Blum74a11662011-05-26 16:25:20 -07001950/*
1951 * cgroup_task_migrate - move a task from one cgroup to another.
1952 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001953 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001954 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001955static void cgroup_task_migrate(struct cgroup *old_cgrp,
1956 struct task_struct *tsk,
1957 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001958{
Tejun Heo5abb8852013-06-12 21:04:49 -07001959 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001960
1961 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001962 * We are synchronized through threadgroup_lock() against PF_EXITING
1963 * setting such that we can't race against cgroup_exit() changing the
1964 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001965 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001966 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001967 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001968
Ben Blum74a11662011-05-26 16:25:20 -07001969 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001970 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001971 task_unlock(tsk);
1972
1973 /* Update the css_set linked lists if we're using them */
1974 write_lock(&css_set_lock);
1975 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001976 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001977 write_unlock(&css_set_lock);
1978
1979 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001980 * We just gained a reference on old_cset by taking it from the
1981 * task. As trading it for new_cset is protected by cgroup_mutex,
1982 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001983 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001984 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1985 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001986}
1987
Li Zefana043e3b2008-02-23 15:24:09 -08001988/**
Li Zefan081aa452013-03-13 09:17:09 +08001989 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001990 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001991 * @tsk: the task or the leader of the threadgroup to be attached
1992 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001993 *
Tejun Heo257058a2011-12-12 18:12:21 -08001994 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001995 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001996 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001997static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1998 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001999{
2000 int retval, i, group_size;
2001 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002002 struct cgroupfs_root *root = cgrp->root;
2003 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002004 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002005 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002006 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002007 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002008
2009 /*
2010 * step 0: in order to do expensive, possibly blocking operations for
2011 * every thread, we cannot iterate the thread group list, since it needs
2012 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002013 * group - group_rwsem prevents new threads from appearing, and if
2014 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002015 */
Li Zefan081aa452013-03-13 09:17:09 +08002016 if (threadgroup)
2017 group_size = get_nr_threads(tsk);
2018 else
2019 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002020 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002021 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002022 if (!group)
2023 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002024 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002025 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002026 if (retval)
2027 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002028
Ben Blum74a11662011-05-26 16:25:20 -07002029 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002030 /*
2031 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2032 * already PF_EXITING could be freed from underneath us unless we
2033 * take an rcu_read_lock.
2034 */
2035 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002036 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002037 struct task_and_cgroup ent;
2038
Tejun Heocd3d0952011-12-12 18:12:21 -08002039 /* @tsk either already exited or can't exit until the end */
2040 if (tsk->flags & PF_EXITING)
Anjana V Kumarea847532013-10-12 10:59:17 +08002041 goto next;
Tejun Heocd3d0952011-12-12 18:12:21 -08002042
Ben Blum74a11662011-05-26 16:25:20 -07002043 /* as per above, nr_threads may decrease, but not increase. */
2044 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002045 ent.task = tsk;
2046 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002047 /* nothing to do if this task is already in the cgroup */
2048 if (ent.cgrp == cgrp)
Anjana V Kumarea847532013-10-12 10:59:17 +08002049 goto next;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002050 /*
2051 * saying GFP_ATOMIC has no effect here because we did prealloc
2052 * earlier, but it's good form to communicate our expectations.
2053 */
Tejun Heo134d3372011-12-12 18:12:21 -08002054 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002055 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002056 i++;
Anjana V Kumarea847532013-10-12 10:59:17 +08002057 next:
Li Zefan081aa452013-03-13 09:17:09 +08002058 if (!threadgroup)
2059 break;
Ben Blum74a11662011-05-26 16:25:20 -07002060 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002061 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002062 /* remember the number of threads in the array for later. */
2063 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002064 tset.tc_array = group;
2065 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002066
Tejun Heo134d3372011-12-12 18:12:21 -08002067 /* methods shouldn't be called if no task is actually migrating */
2068 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002069 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002070 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002071
Ben Blum74a11662011-05-26 16:25:20 -07002072 /*
2073 * step 1: check that we can legitimately attach to the cgroup.
2074 */
Tejun Heo5549c492013-06-24 15:21:48 -07002075 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002076 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002077
Ben Blum74a11662011-05-26 16:25:20 -07002078 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002079 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002080 if (retval) {
2081 failed_ss = ss;
2082 goto out_cancel_attach;
2083 }
2084 }
Ben Blum74a11662011-05-26 16:25:20 -07002085 }
2086
2087 /*
2088 * step 2: make sure css_sets exist for all threads to be migrated.
2089 * we use find_css_set, which allocates a new one if necessary.
2090 */
Ben Blum74a11662011-05-26 16:25:20 -07002091 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002092 struct css_set *old_cset;
2093
Tejun Heo134d3372011-12-12 18:12:21 -08002094 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002095 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002096 tc->cset = find_css_set(old_cset, cgrp);
2097 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002098 retval = -ENOMEM;
2099 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002100 }
2101 }
2102
2103 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002104 * step 3: now that we're guaranteed success wrt the css_sets,
2105 * proceed to move all tasks to the new cgroup. There are no
2106 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002107 */
Ben Blum74a11662011-05-26 16:25:20 -07002108 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002109 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002110 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002111 }
2112 /* nothing is sensitive to fork() after this point. */
2113
2114 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002115 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002116 */
Tejun Heo5549c492013-06-24 15:21:48 -07002117 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002118 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002119
Ben Blum74a11662011-05-26 16:25:20 -07002120 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002121 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002122 }
2123
2124 /*
2125 * step 5: success! and cleanup
2126 */
Ben Blum74a11662011-05-26 16:25:20 -07002127 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002128out_put_css_set_refs:
2129 if (retval) {
2130 for (i = 0; i < group_size; i++) {
2131 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002132 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002133 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002134 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002135 }
Ben Blum74a11662011-05-26 16:25:20 -07002136 }
2137out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002138 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002139 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002140 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002141
Tejun Heo494c1672011-12-12 18:12:22 -08002142 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002143 break;
Ben Blum74a11662011-05-26 16:25:20 -07002144 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002145 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002146 }
2147 }
Ben Blum74a11662011-05-26 16:25:20 -07002148out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002149 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002150 return retval;
2151}
2152
2153/*
2154 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002155 * function to attach either it or all tasks in its threadgroup. Will lock
2156 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002157 */
2158static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002159{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002161 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002162 int ret;
2163
Ben Blum74a11662011-05-26 16:25:20 -07002164 if (!cgroup_lock_live_group(cgrp))
2165 return -ENODEV;
2166
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002167retry_find_task:
2168 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002169 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002170 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002171 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002172 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002173 ret= -ESRCH;
2174 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002175 }
Ben Blum74a11662011-05-26 16:25:20 -07002176 /*
2177 * even if we're attaching all tasks in the thread group, we
2178 * only need to check permissions on one of them.
2179 */
David Howellsc69e8d92008-11-14 10:39:19 +11002180 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002181 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2182 !uid_eq(cred->euid, tcred->uid) &&
2183 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002184 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002185 ret = -EACCES;
2186 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002187 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002188 } else
2189 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002190
2191 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002192 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002193
2194 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002195 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002196 * trapped in a cpuset, or RT worker may be born in a cgroup
2197 * with no rt_runtime allocated. Just say no.
2198 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002199 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002200 ret = -EINVAL;
2201 rcu_read_unlock();
2202 goto out_unlock_cgroup;
2203 }
2204
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002205 get_task_struct(tsk);
2206 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002207
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002208 threadgroup_lock(tsk);
2209 if (threadgroup) {
2210 if (!thread_group_leader(tsk)) {
2211 /*
2212 * a race with de_thread from another thread's exec()
2213 * may strip us of our leadership, if this happens,
2214 * there is no choice but to throw this task away and
2215 * try again; this is
2216 * "double-double-toil-and-trouble-check locking".
2217 */
2218 threadgroup_unlock(tsk);
2219 put_task_struct(tsk);
2220 goto retry_find_task;
2221 }
Li Zefan081aa452013-03-13 09:17:09 +08002222 }
2223
2224 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2225
Tejun Heocd3d0952011-12-12 18:12:21 -08002226 threadgroup_unlock(tsk);
2227
Paul Menagebbcb81d2007-10-18 23:39:32 -07002228 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002229out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002230 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002231 return ret;
2232}
2233
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002234/**
2235 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2236 * @from: attach to all cgroups of a given task
2237 * @tsk: the task to be attached
2238 */
2239int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2240{
2241 struct cgroupfs_root *root;
2242 int retval = 0;
2243
Tejun Heo47cfcd02013-04-07 09:29:51 -07002244 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002245 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002246 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002247
Li Zefan6f4b7e62013-07-31 16:18:36 +08002248 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002249 if (retval)
2250 break;
2251 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002252 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002253
2254 return retval;
2255}
2256EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2257
Tejun Heo182446d2013-08-08 20:11:24 -04002258static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2259 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002260{
Tejun Heo182446d2013-08-08 20:11:24 -04002261 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002262}
2263
Tejun Heo182446d2013-08-08 20:11:24 -04002264static int cgroup_procs_write(struct cgroup_subsys_state *css,
2265 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002266{
Tejun Heo182446d2013-08-08 20:11:24 -04002267 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002268}
2269
Tejun Heo182446d2013-08-08 20:11:24 -04002270static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2271 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002272{
Tejun Heo182446d2013-08-08 20:11:24 -04002273 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002274 if (strlen(buffer) >= PATH_MAX)
2275 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002276 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002277 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002278 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002279 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002280 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002281 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002282 return 0;
2283}
2284
Tejun Heo182446d2013-08-08 20:11:24 -04002285static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2286 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002287{
Tejun Heo182446d2013-08-08 20:11:24 -04002288 struct cgroup *cgrp = css->cgroup;
2289
Paul Menagee788e062008-07-25 01:46:59 -07002290 if (!cgroup_lock_live_group(cgrp))
2291 return -ENODEV;
2292 seq_puts(seq, cgrp->root->release_agent_path);
2293 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002294 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002295 return 0;
2296}
2297
Tejun Heo182446d2013-08-08 20:11:24 -04002298static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2299 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002300{
Tejun Heo182446d2013-08-08 20:11:24 -04002301 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002302 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002303}
2304
Paul Menage84eea842008-07-25 01:47:00 -07002305/* A buffer size big enough for numbers or short strings */
2306#define CGROUP_LOCAL_BUFFER_SIZE 64
2307
Tejun Heo182446d2013-08-08 20:11:24 -04002308static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2309 struct cftype *cft, struct file *file,
2310 const char __user *userbuf, size_t nbytes,
2311 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002312{
Paul Menage84eea842008-07-25 01:47:00 -07002313 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002314 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002315 char *end;
2316
2317 if (!nbytes)
2318 return -EINVAL;
2319 if (nbytes >= sizeof(buffer))
2320 return -E2BIG;
2321 if (copy_from_user(buffer, userbuf, nbytes))
2322 return -EFAULT;
2323
2324 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002325 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002326 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002327 if (*end)
2328 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002329 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002330 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002331 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002332 if (*end)
2333 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002334 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002335 }
Paul Menage355e0c42007-10-18 23:39:33 -07002336 if (!retval)
2337 retval = nbytes;
2338 return retval;
2339}
2340
Tejun Heo182446d2013-08-08 20:11:24 -04002341static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2342 struct cftype *cft, struct file *file,
2343 const char __user *userbuf, size_t nbytes,
2344 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002345{
Paul Menage84eea842008-07-25 01:47:00 -07002346 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002347 int retval = 0;
2348 size_t max_bytes = cft->max_write_len;
2349 char *buffer = local_buffer;
2350
2351 if (!max_bytes)
2352 max_bytes = sizeof(local_buffer) - 1;
2353 if (nbytes >= max_bytes)
2354 return -E2BIG;
2355 /* Allocate a dynamic buffer if we need one */
2356 if (nbytes >= sizeof(local_buffer)) {
2357 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2358 if (buffer == NULL)
2359 return -ENOMEM;
2360 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002361 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2362 retval = -EFAULT;
2363 goto out;
2364 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002365
2366 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002367 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002368 if (!retval)
2369 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002370out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002371 if (buffer != local_buffer)
2372 kfree(buffer);
2373 return retval;
2374}
2375
Paul Menageddbcc7e2007-10-18 23:39:30 -07002376static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002377 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002378{
Tejun Heo182446d2013-08-08 20:11:24 -04002379 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002380 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002381 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002382
Paul Menage355e0c42007-10-18 23:39:33 -07002383 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002384 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002385 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002386 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002387 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002388 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002389 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002390 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002391 return ret ? ret : nbytes;
2392 }
Paul Menage355e0c42007-10-18 23:39:33 -07002393 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002394}
2395
Tejun Heo182446d2013-08-08 20:11:24 -04002396static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2397 struct cftype *cft, struct file *file,
2398 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002399{
Paul Menage84eea842008-07-25 01:47:00 -07002400 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002401 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002402 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2403
2404 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2405}
2406
Tejun Heo182446d2013-08-08 20:11:24 -04002407static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2408 struct cftype *cft, struct file *file,
2409 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002410{
Paul Menage84eea842008-07-25 01:47:00 -07002411 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002412 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002413 int len = sprintf(tmp, "%lld\n", (long long) val);
2414
2415 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2416}
2417
Paul Menageddbcc7e2007-10-18 23:39:30 -07002418static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002419 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002420{
Tejun Heo182446d2013-08-08 20:11:24 -04002421 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002422 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002423 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002424
2425 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002426 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002427 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002428 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002429 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002430 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002431 return -EINVAL;
2432}
2433
Paul Menage91796562008-04-29 01:00:01 -07002434/*
2435 * seqfile ops/methods for returning structured data. Currently just
2436 * supports string->u64 maps, but can be extended in future.
2437 */
2438
Paul Menage91796562008-04-29 01:00:01 -07002439static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2440{
2441 struct seq_file *sf = cb->state;
2442 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2443}
2444
2445static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2446{
Li Zefane0798ce2013-07-31 17:36:25 +08002447 struct cfent *cfe = m->private;
2448 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002449 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002450
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002451 if (cft->read_map) {
2452 struct cgroup_map_cb cb = {
2453 .fill = cgroup_map_add,
2454 .state = m,
2455 };
Tejun Heo182446d2013-08-08 20:11:24 -04002456 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002457 }
Tejun Heo182446d2013-08-08 20:11:24 -04002458 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002459}
2460
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002461static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002462 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002463 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002464 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002465 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002466};
2467
Paul Menageddbcc7e2007-10-18 23:39:30 -07002468static int cgroup_file_open(struct inode *inode, struct file *file)
2469{
Tejun Heof7d58812013-08-08 20:11:23 -04002470 struct cfent *cfe = __d_cfe(file->f_dentry);
2471 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002472 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2473 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002474 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475
2476 err = generic_file_open(inode, file);
2477 if (err)
2478 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002479
2480 /*
2481 * If the file belongs to a subsystem, pin the css. Will be
2482 * unpinned either on open failure or release. This ensures that
2483 * @css stays alive for all file operations.
2484 */
Tejun Heo105347b2013-08-13 11:01:55 -04002485 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002486 css = cgroup_css(cgrp, cft->ss);
2487 if (cft->ss && !css_tryget(css))
2488 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002489 rcu_read_unlock();
2490
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002491 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002492 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002493
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002494 /*
2495 * @cfe->css is used by read/write/close to determine the
2496 * associated css. @file->private_data would be a better place but
2497 * that's already used by seqfile. Multiple accessors may use it
2498 * simultaneously which is okay as the association never changes.
2499 */
2500 WARN_ON_ONCE(cfe->css && cfe->css != css);
2501 cfe->css = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002502
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002503 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002504 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002505 err = single_open(file, cgroup_seqfile_show, cfe);
2506 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002507 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002508 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002509
Tejun Heo67f4c362013-08-08 20:11:24 -04002510 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002511 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512 return err;
2513}
2514
2515static int cgroup_file_release(struct inode *inode, struct file *file)
2516{
Tejun Heof7d58812013-08-08 20:11:23 -04002517 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002518 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002519 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002520 int ret = 0;
2521
Paul Menageddbcc7e2007-10-18 23:39:30 -07002522 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002523 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002524 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002525 css_put(css);
2526 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002527}
2528
2529/*
2530 * cgroup_rename - Only allow simple rename of directories in place.
2531 */
2532static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2533 struct inode *new_dir, struct dentry *new_dentry)
2534{
Li Zefan65dff752013-03-01 15:01:56 +08002535 int ret;
2536 struct cgroup_name *name, *old_name;
2537 struct cgroup *cgrp;
2538
2539 /*
2540 * It's convinient to use parent dir's i_mutex to protected
2541 * cgrp->name.
2542 */
2543 lockdep_assert_held(&old_dir->i_mutex);
2544
Paul Menageddbcc7e2007-10-18 23:39:30 -07002545 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2546 return -ENOTDIR;
2547 if (new_dentry->d_inode)
2548 return -EEXIST;
2549 if (old_dir != new_dir)
2550 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002551
2552 cgrp = __d_cgrp(old_dentry);
2553
Tejun Heo6db8e852013-06-14 11:18:22 -07002554 /*
2555 * This isn't a proper migration and its usefulness is very
2556 * limited. Disallow if sane_behavior.
2557 */
2558 if (cgroup_sane_behavior(cgrp))
2559 return -EPERM;
2560
Li Zefan65dff752013-03-01 15:01:56 +08002561 name = cgroup_alloc_name(new_dentry);
2562 if (!name)
2563 return -ENOMEM;
2564
2565 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2566 if (ret) {
2567 kfree(name);
2568 return ret;
2569 }
2570
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002571 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002572 rcu_assign_pointer(cgrp->name, name);
2573
2574 kfree_rcu(old_name, rcu_head);
2575 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002576}
2577
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002578static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2579{
2580 if (S_ISDIR(dentry->d_inode->i_mode))
2581 return &__d_cgrp(dentry)->xattrs;
2582 else
Li Zefan712317a2013-04-18 23:09:52 -07002583 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002584}
2585
2586static inline int xattr_enabled(struct dentry *dentry)
2587{
2588 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002589 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002590}
2591
2592static bool is_valid_xattr(const char *name)
2593{
2594 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2595 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2596 return true;
2597 return false;
2598}
2599
2600static int cgroup_setxattr(struct dentry *dentry, const char *name,
2601 const void *val, size_t size, int flags)
2602{
2603 if (!xattr_enabled(dentry))
2604 return -EOPNOTSUPP;
2605 if (!is_valid_xattr(name))
2606 return -EINVAL;
2607 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2608}
2609
2610static int cgroup_removexattr(struct dentry *dentry, const char *name)
2611{
2612 if (!xattr_enabled(dentry))
2613 return -EOPNOTSUPP;
2614 if (!is_valid_xattr(name))
2615 return -EINVAL;
2616 return simple_xattr_remove(__d_xattrs(dentry), name);
2617}
2618
2619static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2620 void *buf, size_t size)
2621{
2622 if (!xattr_enabled(dentry))
2623 return -EOPNOTSUPP;
2624 if (!is_valid_xattr(name))
2625 return -EINVAL;
2626 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2627}
2628
2629static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2630{
2631 if (!xattr_enabled(dentry))
2632 return -EOPNOTSUPP;
2633 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2634}
2635
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002636static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002637 .read = cgroup_file_read,
2638 .write = cgroup_file_write,
2639 .llseek = generic_file_llseek,
2640 .open = cgroup_file_open,
2641 .release = cgroup_file_release,
2642};
2643
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002644static const struct inode_operations cgroup_file_inode_operations = {
2645 .setxattr = cgroup_setxattr,
2646 .getxattr = cgroup_getxattr,
2647 .listxattr = cgroup_listxattr,
2648 .removexattr = cgroup_removexattr,
2649};
2650
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002651static const struct inode_operations cgroup_dir_inode_operations = {
Al Viro786e1442013-07-14 17:50:23 +04002652 .lookup = simple_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002653 .mkdir = cgroup_mkdir,
2654 .rmdir = cgroup_rmdir,
2655 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002656 .setxattr = cgroup_setxattr,
2657 .getxattr = cgroup_getxattr,
2658 .listxattr = cgroup_listxattr,
2659 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002660};
2661
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002662/*
2663 * Check if a file is a control file
2664 */
2665static inline struct cftype *__file_cft(struct file *file)
2666{
Al Viro496ad9a2013-01-23 17:07:38 -05002667 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002668 return ERR_PTR(-EINVAL);
2669 return __d_cft(file->f_dentry);
2670}
2671
Al Viroa5e7ed32011-07-26 01:55:55 -04002672static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002673 struct super_block *sb)
2674{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002675 struct inode *inode;
2676
2677 if (!dentry)
2678 return -ENOENT;
2679 if (dentry->d_inode)
2680 return -EEXIST;
2681
2682 inode = cgroup_new_inode(mode, sb);
2683 if (!inode)
2684 return -ENOMEM;
2685
2686 if (S_ISDIR(mode)) {
2687 inode->i_op = &cgroup_dir_inode_operations;
2688 inode->i_fop = &simple_dir_operations;
2689
2690 /* start off with i_nlink == 2 (for "." entry) */
2691 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002692 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002693
Tejun Heob8a2df62012-11-19 08:13:37 -08002694 /*
2695 * Control reaches here with cgroup_mutex held.
2696 * @inode->i_mutex should nest outside cgroup_mutex but we
2697 * want to populate it immediately without releasing
2698 * cgroup_mutex. As @inode isn't visible to anyone else
2699 * yet, trylock will always succeed without affecting
2700 * lockdep checks.
2701 */
2702 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002703 } else if (S_ISREG(mode)) {
2704 inode->i_size = 0;
2705 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002706 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002707 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 d_instantiate(dentry, inode);
2709 dget(dentry); /* Extra count - pin the dentry in core */
2710 return 0;
2711}
2712
Li Zefan099fca32009-04-02 16:57:29 -07002713/**
2714 * cgroup_file_mode - deduce file mode of a control file
2715 * @cft: the control file in question
2716 *
2717 * returns cft->mode if ->mode is not 0
2718 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2719 * returns S_IRUGO if it has only a read handler
2720 * returns S_IWUSR if it has only a write hander
2721 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002722static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002723{
Al Viroa5e7ed32011-07-26 01:55:55 -04002724 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002725
2726 if (cft->mode)
2727 return cft->mode;
2728
2729 if (cft->read || cft->read_u64 || cft->read_s64 ||
2730 cft->read_map || cft->read_seq_string)
2731 mode |= S_IRUGO;
2732
2733 if (cft->write || cft->write_u64 || cft->write_s64 ||
2734 cft->write_string || cft->trigger)
2735 mode |= S_IWUSR;
2736
2737 return mode;
2738}
2739
Tejun Heo2bb566c2013-08-08 20:11:23 -04002740static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002741{
Paul Menagebd89aab2007-10-18 23:40:44 -07002742 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002743 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002744 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002747 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002749
Tejun Heo9fa4db32013-08-26 18:40:56 -04002750 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2751 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002752 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753 strcat(name, ".");
2754 }
2755 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002756
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002758
2759 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2760 if (!cfe)
2761 return -ENOMEM;
2762
Paul Menageddbcc7e2007-10-18 23:39:30 -07002763 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002764 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002765 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002766 goto out;
2767 }
2768
Li Zefand6cbf352013-05-14 19:44:20 +08002769 cfe->type = (void *)cft;
2770 cfe->dentry = dentry;
2771 dentry->d_fsdata = cfe;
2772 simple_xattrs_init(&cfe->xattrs);
2773
Tejun Heo05ef1d72012-04-01 12:09:56 -07002774 mode = cgroup_file_mode(cft);
2775 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2776 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002777 list_add_tail(&cfe->node, &parent->files);
2778 cfe = NULL;
2779 }
2780 dput(dentry);
2781out:
2782 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002783 return error;
2784}
2785
Tejun Heob1f28d32013-06-28 16:24:10 -07002786/**
2787 * cgroup_addrm_files - add or remove files to a cgroup directory
2788 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002789 * @cfts: array of cftypes to be added
2790 * @is_add: whether to add or remove
2791 *
2792 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002793 * For removals, this function never fails. If addition fails, this
2794 * function doesn't remove files already added. The caller is responsible
2795 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002796 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002797static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2798 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002799{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002800 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002801 int ret;
2802
2803 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2804 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002805
2806 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002807 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002808 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2809 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002810 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2811 continue;
2812 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2813 continue;
2814
Li Zefan2739d3c2013-01-21 18:18:33 +08002815 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002816 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002817 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002818 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002819 cft->name, ret);
2820 return ret;
2821 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002822 } else {
2823 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002824 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002825 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002826 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002827}
2828
Tejun Heo8e3f6542012-04-01 12:09:55 -07002829static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002830 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002831{
2832 /*
2833 * Thanks to the entanglement with vfs inode locking, we can't walk
2834 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002835 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2836 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002837 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838 mutex_lock(&cgroup_mutex);
2839}
2840
Tejun Heo2bb566c2013-08-08 20:11:23 -04002841static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002842 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002843{
2844 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002845 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002846 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002847 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002848 struct dentry *prev = NULL;
2849 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002850 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002851 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002852 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002853
2854 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002855 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002856 !atomic_inc_not_zero(&sb->s_active)) {
2857 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002858 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002859 }
2860
Li Zefane8c82d22013-06-18 18:48:37 +08002861 /*
2862 * All cgroups which are created after we drop cgroup_mutex will
2863 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002864 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002865 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002866 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002867
Tejun Heo8e3f6542012-04-01 12:09:55 -07002868 mutex_unlock(&cgroup_mutex);
2869
Li Zefane8c82d22013-06-18 18:48:37 +08002870 /* add/rm files for all cgroups created before */
2871 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002872 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002873 struct cgroup *cgrp = css->cgroup;
2874
Li Zefane8c82d22013-06-18 18:48:37 +08002875 if (cgroup_is_dead(cgrp))
2876 continue;
2877
2878 inode = cgrp->dentry->d_inode;
2879 dget(cgrp->dentry);
2880 rcu_read_unlock();
2881
2882 dput(prev);
2883 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002884
2885 mutex_lock(&inode->i_mutex);
2886 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002887 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002888 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002889 mutex_unlock(&cgroup_mutex);
2890 mutex_unlock(&inode->i_mutex);
2891
Li Zefane8c82d22013-06-18 18:48:37 +08002892 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002893 if (ret)
2894 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002895 }
Li Zefane8c82d22013-06-18 18:48:37 +08002896 rcu_read_unlock();
2897 dput(prev);
2898 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002899 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002900}
2901
2902/**
2903 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2904 * @ss: target cgroup subsystem
2905 * @cfts: zero-length name terminated array of cftypes
2906 *
2907 * Register @cfts to @ss. Files described by @cfts are created for all
2908 * existing cgroups to which @ss is attached and all future cgroups will
2909 * have them too. This function can be called anytime whether @ss is
2910 * attached or not.
2911 *
2912 * Returns 0 on successful registration, -errno on failure. Note that this
2913 * function currently returns 0 as long as @cfts registration is successful
2914 * even if some file creation attempts on existing cgroups fail.
2915 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002916int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002917{
2918 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002919 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002920 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002921
2922 set = kzalloc(sizeof(*set), GFP_KERNEL);
2923 if (!set)
2924 return -ENOMEM;
2925
Tejun Heo2bb566c2013-08-08 20:11:23 -04002926 for (cft = cfts; cft->name[0] != '\0'; cft++)
2927 cft->ss = ss;
2928
Tejun Heo8e3f6542012-04-01 12:09:55 -07002929 cgroup_cfts_prepare();
2930 set->cfts = cfts;
2931 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002932 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002933 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002934 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002935 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002936}
2937EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2938
Li Zefana043e3b2008-02-23 15:24:09 -08002939/**
Tejun Heo79578622012-04-01 12:09:56 -07002940 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002941 * @cfts: zero-length name terminated array of cftypes
2942 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002943 * Unregister @cfts. Files described by @cfts are removed from all
2944 * existing cgroups and all future cgroups won't have them either. This
2945 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002946 *
2947 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002948 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002949 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002950int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002951{
2952 struct cftype_set *set;
2953
Tejun Heo2bb566c2013-08-08 20:11:23 -04002954 if (!cfts || !cfts[0].ss)
2955 return -ENOENT;
2956
Tejun Heo79578622012-04-01 12:09:56 -07002957 cgroup_cfts_prepare();
2958
Tejun Heo2bb566c2013-08-08 20:11:23 -04002959 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002960 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002961 list_del(&set->node);
2962 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002963 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002964 return 0;
2965 }
2966 }
2967
Tejun Heo2bb566c2013-08-08 20:11:23 -04002968 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002969 return -ENOENT;
2970}
2971
2972/**
Li Zefana043e3b2008-02-23 15:24:09 -08002973 * cgroup_task_count - count the number of tasks in a cgroup.
2974 * @cgrp: the cgroup in question
2975 *
2976 * Return the number of tasks in the cgroup.
2977 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002978int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002979{
2980 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002981 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002982
Paul Menage817929e2007-10-18 23:39:36 -07002983 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002984 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2985 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002986 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002987 return count;
2988}
2989
2990/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002991 * To reduce the fork() overhead for systems that are not actually using
2992 * their cgroups capability, we don't maintain the lists running through
2993 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002994 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002995 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002996static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002997{
2998 struct task_struct *p, *g;
2999 write_lock(&css_set_lock);
3000 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003001 /*
3002 * We need tasklist_lock because RCU is not safe against
3003 * while_each_thread(). Besides, a forking task that has passed
3004 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3005 * is not guaranteed to have its child immediately visible in the
3006 * tasklist if we walk through it with RCU.
3007 */
3008 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003009 do_each_thread(g, p) {
3010 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003011 /*
3012 * We should check if the process is exiting, otherwise
3013 * it will race with cgroup_exit() in that the list
3014 * entry won't be deleted though the process has exited.
3015 */
3016 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003017 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003018 task_unlock(p);
3019 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003020 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003021 write_unlock(&css_set_lock);
3022}
3023
Tejun Heo574bd9f2012-11-09 09:12:29 -08003024/**
Tejun Heo492eb212013-08-08 20:11:25 -04003025 * css_next_child - find the next child of a given css
3026 * @pos_css: the current position (%NULL to initiate traversal)
3027 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003028 *
Tejun Heo492eb212013-08-08 20:11:25 -04003029 * This function returns the next child of @parent_css and should be called
3030 * under RCU read lock. The only requirement is that @parent_css and
3031 * @pos_css are accessible. The next sibling is guaranteed to be returned
3032 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003033 */
Tejun Heo492eb212013-08-08 20:11:25 -04003034struct cgroup_subsys_state *
3035css_next_child(struct cgroup_subsys_state *pos_css,
3036 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003037{
Tejun Heo492eb212013-08-08 20:11:25 -04003038 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3039 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003040 struct cgroup *next;
3041
3042 WARN_ON_ONCE(!rcu_read_lock_held());
3043
3044 /*
3045 * @pos could already have been removed. Once a cgroup is removed,
3046 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003047 * changes. As CGRP_DEAD assertion is serialized and happens
3048 * before the cgroup is taken off the ->sibling list, if we see it
3049 * unasserted, it's guaranteed that the next sibling hasn't
3050 * finished its grace period even if it's already removed, and thus
3051 * safe to dereference from this RCU critical section. If
3052 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3053 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003054 *
3055 * If @pos is dead, its next pointer can't be dereferenced;
3056 * however, as each cgroup is given a monotonically increasing
3057 * unique serial number and always appended to the sibling list,
3058 * the next one can be found by walking the parent's children until
3059 * we see a cgroup with higher serial number than @pos's. While
3060 * this path can be slower, it's taken only when either the current
3061 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003062 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003063 if (!pos) {
3064 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3065 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003066 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003067 } else {
3068 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3069 if (next->serial_nr > pos->serial_nr)
3070 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003071 }
3072
Tejun Heo492eb212013-08-08 20:11:25 -04003073 if (&next->sibling == &cgrp->children)
3074 return NULL;
3075
Tejun Heoca8bdca2013-08-26 18:40:56 -04003076 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003077}
Tejun Heo492eb212013-08-08 20:11:25 -04003078EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003079
3080/**
Tejun Heo492eb212013-08-08 20:11:25 -04003081 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003082 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003083 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003084 *
Tejun Heo492eb212013-08-08 20:11:25 -04003085 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003086 * to visit for pre-order traversal of @root's descendants. @root is
3087 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003088 *
3089 * While this function requires RCU read locking, it doesn't require the
3090 * whole traversal to be contained in a single RCU critical section. This
3091 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003092 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003093 */
Tejun Heo492eb212013-08-08 20:11:25 -04003094struct cgroup_subsys_state *
3095css_next_descendant_pre(struct cgroup_subsys_state *pos,
3096 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097{
Tejun Heo492eb212013-08-08 20:11:25 -04003098 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003099
3100 WARN_ON_ONCE(!rcu_read_lock_held());
3101
Tejun Heobd8815a2013-08-08 20:11:27 -04003102 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003103 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003104 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003105
3106 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003107 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 if (next)
3109 return next;
3110
3111 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003112 while (pos != root) {
3113 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003114 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003115 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003116 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003117 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003118
3119 return NULL;
3120}
Tejun Heo492eb212013-08-08 20:11:25 -04003121EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003122
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003123/**
Tejun Heo492eb212013-08-08 20:11:25 -04003124 * css_rightmost_descendant - return the rightmost descendant of a css
3125 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003126 *
Tejun Heo492eb212013-08-08 20:11:25 -04003127 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3128 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003129 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003130 *
3131 * While this function requires RCU read locking, it doesn't require the
3132 * whole traversal to be contained in a single RCU critical section. This
3133 * function will return the correct rightmost descendant as long as @pos is
3134 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003135 */
Tejun Heo492eb212013-08-08 20:11:25 -04003136struct cgroup_subsys_state *
3137css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003138{
Tejun Heo492eb212013-08-08 20:11:25 -04003139 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003140
3141 WARN_ON_ONCE(!rcu_read_lock_held());
3142
3143 do {
3144 last = pos;
3145 /* ->prev isn't RCU safe, walk ->next till the end */
3146 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003147 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003148 pos = tmp;
3149 } while (pos);
3150
3151 return last;
3152}
Tejun Heo492eb212013-08-08 20:11:25 -04003153EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003154
Tejun Heo492eb212013-08-08 20:11:25 -04003155static struct cgroup_subsys_state *
3156css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003157{
Tejun Heo492eb212013-08-08 20:11:25 -04003158 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003159
3160 do {
3161 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003162 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003163 } while (pos);
3164
3165 return last;
3166}
3167
3168/**
Tejun Heo492eb212013-08-08 20:11:25 -04003169 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003170 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003171 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 *
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003174 * to visit for post-order traversal of @root's descendants. @root is
3175 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003176 *
3177 * While this function requires RCU read locking, it doesn't require the
3178 * whole traversal to be contained in a single RCU critical section. This
3179 * function will return the correct next descendant as long as both @pos
3180 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003181 */
Tejun Heo492eb212013-08-08 20:11:25 -04003182struct cgroup_subsys_state *
3183css_next_descendant_post(struct cgroup_subsys_state *pos,
3184 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003185{
Tejun Heo492eb212013-08-08 20:11:25 -04003186 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003187
3188 WARN_ON_ONCE(!rcu_read_lock_held());
3189
Tejun Heo58b79a92013-09-06 15:31:08 -04003190 /* if first iteration, visit leftmost descendant which may be @root */
3191 if (!pos)
3192 return css_leftmost_descendant(root);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003193
Tejun Heobd8815a2013-08-08 20:11:27 -04003194 /* if we visited @root, we're done */
3195 if (pos == root)
3196 return NULL;
3197
Tejun Heo574bd9f2012-11-09 09:12:29 -08003198 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003199 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003200 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003201 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003202
3203 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003204 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003205}
Tejun Heo492eb212013-08-08 20:11:25 -04003206EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003207
Tejun Heo0942eee2013-08-08 20:11:26 -04003208/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003209 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003210 * @it: the iterator to advance
3211 *
3212 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003213 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003214static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003215{
3216 struct list_head *l = it->cset_link;
3217 struct cgrp_cset_link *link;
3218 struct css_set *cset;
3219
3220 /* Advance to the next non-empty css_set */
3221 do {
3222 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003223 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003224 it->cset_link = NULL;
3225 return;
3226 }
3227 link = list_entry(l, struct cgrp_cset_link, cset_link);
3228 cset = link->cset;
3229 } while (list_empty(&cset->tasks));
3230 it->cset_link = l;
3231 it->task = cset->tasks.next;
3232}
3233
Tejun Heo0942eee2013-08-08 20:11:26 -04003234/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003235 * css_task_iter_start - initiate task iteration
3236 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003237 * @it: the task iterator to use
3238 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003239 * Initiate iteration through the tasks of @css. The caller can call
3240 * css_task_iter_next() to walk through the tasks until the function
3241 * returns NULL. On completion of iteration, css_task_iter_end() must be
3242 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003243 *
3244 * Note that this function acquires a lock which is released when the
3245 * iteration finishes. The caller can't sleep while iteration is in
3246 * progress.
3247 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003248void css_task_iter_start(struct cgroup_subsys_state *css,
3249 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003250 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003251{
3252 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003253 * The first time anyone tries to iterate across a css, we need to
3254 * enable the list linking each css_set to its tasks, and fix up
3255 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003256 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003257 if (!use_task_css_set_links)
3258 cgroup_enable_task_cg_lists();
3259
Paul Menage817929e2007-10-18 23:39:36 -07003260 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003261
Tejun Heo72ec7022013-08-08 20:11:26 -04003262 it->origin_css = css;
3263 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003264
Tejun Heo72ec7022013-08-08 20:11:26 -04003265 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003266}
3267
Tejun Heo0942eee2013-08-08 20:11:26 -04003268/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003269 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003270 * @it: the task iterator being iterated
3271 *
3272 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003273 * initialized via css_task_iter_start(). Returns NULL when the iteration
3274 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003275 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003276struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003277{
3278 struct task_struct *res;
3279 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003280 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003281
3282 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003283 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003284 return NULL;
3285 res = list_entry(l, struct task_struct, cg_list);
3286 /* Advance iterator to find next entry */
3287 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003288 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3289 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003290 /*
3291 * We reached the end of this task list - move on to the
3292 * next cgrp_cset_link.
3293 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003294 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003295 } else {
3296 it->task = l;
3297 }
3298 return res;
3299}
3300
Tejun Heo0942eee2013-08-08 20:11:26 -04003301/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003302 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003303 * @it: the task iterator to finish
3304 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003305 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003306 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003307void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003308 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003309{
3310 read_unlock(&css_set_lock);
3311}
3312
Cliff Wickman31a7df02008-02-07 00:14:42 -08003313static inline int started_after_time(struct task_struct *t1,
3314 struct timespec *time,
3315 struct task_struct *t2)
3316{
3317 int start_diff = timespec_compare(&t1->start_time, time);
3318 if (start_diff > 0) {
3319 return 1;
3320 } else if (start_diff < 0) {
3321 return 0;
3322 } else {
3323 /*
3324 * Arbitrarily, if two processes started at the same
3325 * time, we'll say that the lower pointer value
3326 * started first. Note that t2 may have exited by now
3327 * so this may not be a valid pointer any longer, but
3328 * that's fine - it still serves to distinguish
3329 * between two tasks started (effectively) simultaneously.
3330 */
3331 return t1 > t2;
3332 }
3333}
3334
3335/*
3336 * This function is a callback from heap_insert() and is used to order
3337 * the heap.
3338 * In this case we order the heap in descending task start time.
3339 */
3340static inline int started_after(void *p1, void *p2)
3341{
3342 struct task_struct *t1 = p1;
3343 struct task_struct *t2 = p2;
3344 return started_after_time(t1, &t2->start_time, t2);
3345}
3346
3347/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003348 * css_scan_tasks - iterate though all the tasks in a css
3349 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003350 * @test: optional test callback
3351 * @process: process callback
3352 * @data: data passed to @test and @process
3353 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003354 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003355 * Iterate through all the tasks in @css, calling @test for each, and if it
3356 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 *
Tejun Heoe5358372013-08-08 20:11:26 -04003358 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003359 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003360 * lock css_set_lock for the call to @process.
3361 *
3362 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003363 * of @css for the duration of this call. This function may or may not
3364 * call @process for tasks that exit or move to a different css during the
3365 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003366 *
3367 * Note that @test may be called with locks held, and may in some
3368 * situations be called multiple times for the same task, so it should be
3369 * cheap.
3370 *
3371 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3372 * heap operations (and its "gt" member will be overwritten), else a
3373 * temporary heap will be used (allocation of which may cause this function
3374 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003375 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003376int css_scan_tasks(struct cgroup_subsys_state *css,
3377 bool (*test)(struct task_struct *, void *),
3378 void (*process)(struct task_struct *, void *),
3379 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003380{
3381 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003382 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003383 struct task_struct *p, *dropped;
3384 /* Never dereference latest_task, since it's not refcounted */
3385 struct task_struct *latest_task = NULL;
3386 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003387 struct timespec latest_time = { 0, 0 };
3388
Tejun Heoe5358372013-08-08 20:11:26 -04003389 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003390 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003391 heap->gt = &started_after;
3392 } else {
3393 /* We need to allocate our own heap memory */
3394 heap = &tmp_heap;
3395 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3396 if (retval)
3397 /* cannot allocate the heap */
3398 return retval;
3399 }
3400
3401 again:
3402 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003403 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003404 * which are of interest, and invoking @process callback on the
3405 * ones which need an update. Since we don't want to hold any
3406 * locks during the task updates, gather tasks to be processed in a
3407 * heap structure. The heap is sorted by descending task start
3408 * time. If the statically-sized heap fills up, we overflow tasks
3409 * that started later, and in future iterations only consider tasks
3410 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003411 * guarantees forward progress and that we don't miss any tasks.
3412 */
3413 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003414 css_task_iter_start(css, &it);
3415 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003416 /*
3417 * Only affect tasks that qualify per the caller's callback,
3418 * if he provided one
3419 */
Tejun Heoe5358372013-08-08 20:11:26 -04003420 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003421 continue;
3422 /*
3423 * Only process tasks that started after the last task
3424 * we processed
3425 */
3426 if (!started_after_time(p, &latest_time, latest_task))
3427 continue;
3428 dropped = heap_insert(heap, p);
3429 if (dropped == NULL) {
3430 /*
3431 * The new task was inserted; the heap wasn't
3432 * previously full
3433 */
3434 get_task_struct(p);
3435 } else if (dropped != p) {
3436 /*
3437 * The new task was inserted, and pushed out a
3438 * different task
3439 */
3440 get_task_struct(p);
3441 put_task_struct(dropped);
3442 }
3443 /*
3444 * Else the new task was newer than anything already in
3445 * the heap and wasn't inserted
3446 */
3447 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003448 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003449
3450 if (heap->size) {
3451 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003452 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003453 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003454 latest_time = q->start_time;
3455 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003456 }
3457 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003458 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003459 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003460 }
3461 /*
3462 * If we had to process any tasks at all, scan again
3463 * in case some of them were in the middle of forking
3464 * children that didn't get processed.
3465 * Not the most efficient way to do it, but it avoids
3466 * having to take callback_mutex in the fork path
3467 */
3468 goto again;
3469 }
3470 if (heap == &tmp_heap)
3471 heap_free(&tmp_heap);
3472 return 0;
3473}
3474
Tejun Heoe5358372013-08-08 20:11:26 -04003475static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003476{
Tejun Heoe5358372013-08-08 20:11:26 -04003477 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003478
Tejun Heo47cfcd02013-04-07 09:29:51 -07003479 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003480 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003481 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003482}
3483
3484/**
3485 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3486 * @to: cgroup to which the tasks will be moved
3487 * @from: cgroup in which the tasks currently reside
3488 */
3489int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3490{
Tejun Heo72ec7022013-08-08 20:11:26 -04003491 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3492 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003493}
3494
Paul Menage817929e2007-10-18 23:39:36 -07003495/*
Ben Blum102a7752009-09-23 15:56:26 -07003496 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003497 *
3498 * Reading this file can return large amounts of data if a cgroup has
3499 * *lots* of attached tasks. So it may need several calls to read(),
3500 * but we cannot guarantee that the information we produce is correct
3501 * unless we produce it entirely atomically.
3502 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003503 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003504
Li Zefan24528252012-01-20 11:58:43 +08003505/* which pidlist file are we talking about? */
3506enum cgroup_filetype {
3507 CGROUP_FILE_PROCS,
3508 CGROUP_FILE_TASKS,
3509};
3510
3511/*
3512 * A pidlist is a list of pids that virtually represents the contents of one
3513 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3514 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3515 * to the cgroup.
3516 */
3517struct cgroup_pidlist {
3518 /*
3519 * used to find which pidlist is wanted. doesn't change as long as
3520 * this particular list stays in the list.
3521 */
3522 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3523 /* array of xids */
3524 pid_t *list;
3525 /* how many elements the above list has */
3526 int length;
3527 /* how many files are using the current array */
3528 int use_count;
3529 /* each of these stored in a list by its cgroup */
3530 struct list_head links;
3531 /* pointer to the cgroup we belong to, for list removal purposes */
3532 struct cgroup *owner;
3533 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003534 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003535};
3536
Paul Menagebbcb81d2007-10-18 23:39:32 -07003537/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003538 * The following two functions "fix" the issue where there are more pids
3539 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3540 * TODO: replace with a kernel-wide solution to this problem
3541 */
3542#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3543static void *pidlist_allocate(int count)
3544{
3545 if (PIDLIST_TOO_LARGE(count))
3546 return vmalloc(count * sizeof(pid_t));
3547 else
3548 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3549}
3550static void pidlist_free(void *p)
3551{
3552 if (is_vmalloc_addr(p))
3553 vfree(p);
3554 else
3555 kfree(p);
3556}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003557
3558/*
Ben Blum102a7752009-09-23 15:56:26 -07003559 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003560 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003561 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003562static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003563{
Ben Blum102a7752009-09-23 15:56:26 -07003564 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003565
3566 /*
3567 * we presume the 0th element is unique, so i starts at 1. trivial
3568 * edge cases first; no work needs to be done for either
3569 */
3570 if (length == 0 || length == 1)
3571 return length;
3572 /* src and dest walk down the list; dest counts unique elements */
3573 for (src = 1; src < length; src++) {
3574 /* find next unique element */
3575 while (list[src] == list[src-1]) {
3576 src++;
3577 if (src == length)
3578 goto after;
3579 }
3580 /* dest always points to where the next unique element goes */
3581 list[dest] = list[src];
3582 dest++;
3583 }
3584after:
Ben Blum102a7752009-09-23 15:56:26 -07003585 return dest;
3586}
3587
3588static int cmppid(const void *a, const void *b)
3589{
3590 return *(pid_t *)a - *(pid_t *)b;
3591}
3592
3593/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003594 * find the appropriate pidlist for our purpose (given procs vs tasks)
3595 * returns with the lock on that pidlist already held, and takes care
3596 * of the use count, or returns NULL with no locks held if we're out of
3597 * memory.
3598 */
3599static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3600 enum cgroup_filetype type)
3601{
3602 struct cgroup_pidlist *l;
3603 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003604 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003605
Ben Blum72a8cb32009-09-23 15:56:27 -07003606 /*
Li Zefanb3958902013-08-01 09:52:15 +08003607 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003608 * the last ref-holder is trying to remove l from the list at the same
3609 * time. Holding the pidlist_mutex precludes somebody taking whichever
3610 * list we find out from under us - compare release_pid_array().
3611 */
3612 mutex_lock(&cgrp->pidlist_mutex);
3613 list_for_each_entry(l, &cgrp->pidlists, links) {
3614 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003615 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003616 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003617 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003618 return l;
3619 }
3620 }
3621 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003622 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003623 if (!l) {
3624 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003625 return l;
3626 }
Li Zefanb3958902013-08-01 09:52:15 +08003627 init_rwsem(&l->rwsem);
3628 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003630 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003631 l->owner = cgrp;
3632 list_add(&l->links, &cgrp->pidlists);
3633 mutex_unlock(&cgrp->pidlist_mutex);
3634 return l;
3635}
3636
3637/*
Ben Blum102a7752009-09-23 15:56:26 -07003638 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3639 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003640static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3641 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003642{
3643 pid_t *array;
3644 int length;
3645 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003646 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003647 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003648 struct cgroup_pidlist *l;
3649
3650 /*
3651 * If cgroup gets more users after we read count, we won't have
3652 * enough space - tough. This race is indistinguishable to the
3653 * caller from the case that the additional cgroup users didn't
3654 * show up until sometime later on.
3655 */
3656 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003657 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003658 if (!array)
3659 return -ENOMEM;
3660 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003661 css_task_iter_start(&cgrp->dummy_css, &it);
3662 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003663 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003664 break;
Ben Blum102a7752009-09-23 15:56:26 -07003665 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003666 if (type == CGROUP_FILE_PROCS)
3667 pid = task_tgid_vnr(tsk);
3668 else
3669 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003670 if (pid > 0) /* make sure to only use valid results */
3671 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003672 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003673 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003674 length = n;
3675 /* now sort & (if procs) strip out duplicates */
3676 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003677 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003678 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 l = cgroup_pidlist_find(cgrp, type);
3680 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003681 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003683 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003684 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003685 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003686 l->list = array;
3687 l->length = length;
3688 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003689 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003691 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003692}
3693
Balbir Singh846c7bb2007-10-18 23:39:44 -07003694/**
Li Zefana043e3b2008-02-23 15:24:09 -08003695 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003696 * @stats: cgroupstats to fill information into
3697 * @dentry: A dentry entry belonging to the cgroup for which stats have
3698 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003699 *
3700 * Build and fill cgroupstats so that taskstats can export it to user
3701 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003702 */
3703int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3704{
3705 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003706 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003707 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003708 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003709
Balbir Singh846c7bb2007-10-18 23:39:44 -07003710 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003711 * Validate dentry by checking the superblock operations,
3712 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003713 */
Li Zefan33d283b2008-11-19 15:36:48 -08003714 if (dentry->d_sb->s_op != &cgroup_ops ||
3715 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003716 goto err;
3717
3718 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003719 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003720
Tejun Heo72ec7022013-08-08 20:11:26 -04003721 css_task_iter_start(&cgrp->dummy_css, &it);
3722 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003723 switch (tsk->state) {
3724 case TASK_RUNNING:
3725 stats->nr_running++;
3726 break;
3727 case TASK_INTERRUPTIBLE:
3728 stats->nr_sleeping++;
3729 break;
3730 case TASK_UNINTERRUPTIBLE:
3731 stats->nr_uninterruptible++;
3732 break;
3733 case TASK_STOPPED:
3734 stats->nr_stopped++;
3735 break;
3736 default:
3737 if (delayacct_is_task_waiting_on_io(tsk))
3738 stats->nr_io_wait++;
3739 break;
3740 }
3741 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003742 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003743
Balbir Singh846c7bb2007-10-18 23:39:44 -07003744err:
3745 return ret;
3746}
3747
Paul Menage8f3ff202009-09-23 15:56:25 -07003748
Paul Menagecc31edc2008-10-18 20:28:04 -07003749/*
Ben Blum102a7752009-09-23 15:56:26 -07003750 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003751 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003752 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003753 */
3754
Ben Blum102a7752009-09-23 15:56:26 -07003755static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003756{
3757 /*
3758 * Initially we receive a position value that corresponds to
3759 * one more than the last pid shown (or 0 on the first call or
3760 * after a seek to the start). Use a binary-search to find the
3761 * next pid to display, if any
3762 */
Ben Blum102a7752009-09-23 15:56:26 -07003763 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003764 int index = 0, pid = *pos;
3765 int *iter;
3766
Li Zefanb3958902013-08-01 09:52:15 +08003767 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003768 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003769 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003770
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 while (index < end) {
3772 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003773 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 index = mid;
3775 break;
Ben Blum102a7752009-09-23 15:56:26 -07003776 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 index = mid + 1;
3778 else
3779 end = mid;
3780 }
3781 }
3782 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003783 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 return NULL;
3785 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003786 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 *pos = *iter;
3788 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003789}
3790
Ben Blum102a7752009-09-23 15:56:26 -07003791static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003792{
Ben Blum102a7752009-09-23 15:56:26 -07003793 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003794 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003795}
3796
Ben Blum102a7752009-09-23 15:56:26 -07003797static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003798{
Ben Blum102a7752009-09-23 15:56:26 -07003799 struct cgroup_pidlist *l = s->private;
3800 pid_t *p = v;
3801 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003802 /*
3803 * Advance to the next pid in the array. If this goes off the
3804 * end, we're done
3805 */
3806 p++;
3807 if (p >= end) {
3808 return NULL;
3809 } else {
3810 *pos = *p;
3811 return p;
3812 }
3813}
3814
Ben Blum102a7752009-09-23 15:56:26 -07003815static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003816{
3817 return seq_printf(s, "%d\n", *(int *)v);
3818}
3819
Ben Blum102a7752009-09-23 15:56:26 -07003820/*
3821 * seq_operations functions for iterating on pidlists through seq_file -
3822 * independent of whether it's tasks or procs
3823 */
3824static const struct seq_operations cgroup_pidlist_seq_operations = {
3825 .start = cgroup_pidlist_start,
3826 .stop = cgroup_pidlist_stop,
3827 .next = cgroup_pidlist_next,
3828 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003829};
3830
Ben Blum102a7752009-09-23 15:56:26 -07003831static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003832{
Ben Blum72a8cb32009-09-23 15:56:27 -07003833 /*
3834 * the case where we're the last user of this particular pidlist will
3835 * have us remove it from the cgroup's list, which entails taking the
3836 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3837 * pidlist_mutex, we have to take pidlist_mutex first.
3838 */
3839 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003840 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003841 BUG_ON(!l->use_count);
3842 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003843 /* we're the last user if refcount is 0; remove and free */
3844 list_del(&l->links);
3845 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003846 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003847 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003848 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003849 kfree(l);
3850 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003851 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003852 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003853 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003854}
3855
Ben Blum102a7752009-09-23 15:56:26 -07003856static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003857{
Ben Blum102a7752009-09-23 15:56:26 -07003858 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003859 if (!(file->f_mode & FMODE_READ))
3860 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003861 /*
3862 * the seq_file will only be initialized if the file was opened for
3863 * reading; hence we check if it's not null only in that case.
3864 */
3865 l = ((struct seq_file *)file->private_data)->private;
3866 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003867 return seq_release(inode, file);
3868}
3869
Ben Blum102a7752009-09-23 15:56:26 -07003870static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003871 .read = seq_read,
3872 .llseek = seq_lseek,
3873 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003874 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003875};
3876
3877/*
Ben Blum102a7752009-09-23 15:56:26 -07003878 * The following functions handle opens on a file that displays a pidlist
3879 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3880 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003881 */
Ben Blum102a7752009-09-23 15:56:26 -07003882/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003883static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003884{
3885 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003886 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003887 int retval;
3888
3889 /* Nothing to do for write-only files */
3890 if (!(file->f_mode & FMODE_READ))
3891 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003892
Ben Blum102a7752009-09-23 15:56:26 -07003893 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003894 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003895 if (retval)
3896 return retval;
3897 /* configure file information */
3898 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003899
Ben Blum102a7752009-09-23 15:56:26 -07003900 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003901 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003902 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003903 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003904 }
Ben Blum102a7752009-09-23 15:56:26 -07003905 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003906 return 0;
3907}
Ben Blum102a7752009-09-23 15:56:26 -07003908static int cgroup_tasks_open(struct inode *unused, struct file *file)
3909{
Ben Blum72a8cb32009-09-23 15:56:27 -07003910 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003911}
3912static int cgroup_procs_open(struct inode *unused, struct file *file)
3913{
Ben Blum72a8cb32009-09-23 15:56:27 -07003914 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003915}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003916
Tejun Heo182446d2013-08-08 20:11:24 -04003917static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3918 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003919{
Tejun Heo182446d2013-08-08 20:11:24 -04003920 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003921}
3922
Tejun Heo182446d2013-08-08 20:11:24 -04003923static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3924 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003925{
Tejun Heo182446d2013-08-08 20:11:24 -04003926 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003927 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003928 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003929 else
Tejun Heo182446d2013-08-08 20:11:24 -04003930 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003931 return 0;
3932}
3933
Paul Menagebbcb81d2007-10-18 23:39:32 -07003934/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003935 * When dput() is called asynchronously, if umount has been done and
3936 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3937 * there's a small window that vfs will see the root dentry with non-zero
3938 * refcnt and trigger BUG().
3939 *
3940 * That's why we hold a reference before dput() and drop it right after.
3941 */
3942static void cgroup_dput(struct cgroup *cgrp)
3943{
3944 struct super_block *sb = cgrp->root->sb;
3945
3946 atomic_inc(&sb->s_active);
3947 dput(cgrp->dentry);
3948 deactivate_super(sb);
3949}
3950
3951/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003952 * Unregister event and free resources.
3953 *
3954 * Gets called from workqueue.
3955 */
3956static void cgroup_event_remove(struct work_struct *work)
3957{
3958 struct cgroup_event *event = container_of(work, struct cgroup_event,
3959 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003960 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003961
Li Zefan810cbee2013-02-18 18:56:14 +08003962 remove_wait_queue(event->wqh, &event->wait);
3963
Tejun Heo81eeaf02013-08-08 20:11:26 -04003964 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003965
Li Zefan810cbee2013-02-18 18:56:14 +08003966 /* Notify userspace the event is going away. */
3967 eventfd_signal(event->eventfd, 1);
3968
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003969 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003970 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003971 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972}
3973
3974/*
3975 * Gets called on POLLHUP on eventfd when user closes it.
3976 *
3977 * Called with wqh->lock held and interrupts disabled.
3978 */
3979static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3980 int sync, void *key)
3981{
3982 struct cgroup_event *event = container_of(wait,
3983 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003984 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003985 unsigned long flags = (unsigned long)key;
3986
3987 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003988 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003989 * If the event has been detached at cgroup removal, we
3990 * can simply return knowing the other side will cleanup
3991 * for us.
3992 *
3993 * We can't race against event freeing since the other
3994 * side will require wqh->lock via remove_wait_queue(),
3995 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003996 */
Li Zefan810cbee2013-02-18 18:56:14 +08003997 spin_lock(&cgrp->event_list_lock);
3998 if (!list_empty(&event->list)) {
3999 list_del_init(&event->list);
4000 /*
4001 * We are in atomic context, but cgroup_event_remove()
4002 * may sleep, so we have to call it in workqueue.
4003 */
4004 schedule_work(&event->remove);
4005 }
4006 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004007 }
4008
4009 return 0;
4010}
4011
4012static void cgroup_event_ptable_queue_proc(struct file *file,
4013 wait_queue_head_t *wqh, poll_table *pt)
4014{
4015 struct cgroup_event *event = container_of(pt,
4016 struct cgroup_event, pt);
4017
4018 event->wqh = wqh;
4019 add_wait_queue(wqh, &event->wait);
4020}
4021
4022/*
4023 * Parse input and register new cgroup event handler.
4024 *
4025 * Input must be in format '<event_fd> <control_fd> <args>'.
4026 * Interpretation of args is defined by control file implementation.
4027 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004028static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04004029 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004030{
Tejun Heo6e6eab02013-08-15 11:43:15 -04004031 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004032 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04004033 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004034 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004035 struct file *efile;
4036 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037 char *endp;
4038 int ret;
4039
4040 efd = simple_strtoul(buffer, &endp, 10);
4041 if (*endp != ' ')
4042 return -EINVAL;
4043 buffer = endp + 1;
4044
4045 cfd = simple_strtoul(buffer, &endp, 10);
4046 if ((*endp != ' ') && (*endp != '\0'))
4047 return -EINVAL;
4048 buffer = endp + 1;
4049
4050 event = kzalloc(sizeof(*event), GFP_KERNEL);
4051 if (!event)
4052 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004053
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004054 INIT_LIST_HEAD(&event->list);
4055 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4056 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4057 INIT_WORK(&event->remove, cgroup_event_remove);
4058
4059 efile = eventfd_fget(efd);
4060 if (IS_ERR(efile)) {
4061 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004062 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004063 }
4064
4065 event->eventfd = eventfd_ctx_fileget(efile);
4066 if (IS_ERR(event->eventfd)) {
4067 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004068 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004069 }
4070
4071 cfile = fget(cfd);
4072 if (!cfile) {
4073 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004074 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004075 }
4076
4077 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004078 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004079 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004080 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004081 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004082
4083 event->cft = __file_cft(cfile);
4084 if (IS_ERR(event->cft)) {
4085 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004086 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004087 }
4088
Tejun Heo6e6eab02013-08-15 11:43:15 -04004089 if (!event->cft->ss) {
4090 ret = -EBADF;
4091 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004092 }
4093
Li Zefanf1690072013-02-18 14:13:35 +08004094 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004095 * Determine the css of @cfile, verify it belongs to the same
4096 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004097 * Remaining events are automatically removed on cgroup destruction
4098 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004099 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004100 rcu_read_lock();
4101
4102 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004103 event->css = cgroup_css(cgrp, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004104 cfile_css = css_from_dir(cfile->f_dentry->d_parent, event->cft->ss);
4105 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004106 ret = 0;
4107
4108 rcu_read_unlock();
4109 if (ret)
4110 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004111
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004112 if (!event->cft->register_event || !event->cft->unregister_event) {
4113 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004114 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004115 }
4116
Tejun Heo6e6eab02013-08-15 11:43:15 -04004117 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004118 event->eventfd, buffer);
4119 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004120 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004121
Li Zefan7ef70e42013-04-26 11:58:03 -07004122 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004123
4124 spin_lock(&cgrp->event_list_lock);
4125 list_add(&event->list, &cgrp->event_list);
4126 spin_unlock(&cgrp->event_list_lock);
4127
4128 fput(cfile);
4129 fput(efile);
4130
4131 return 0;
4132
Tejun Heo7941cb02013-08-26 18:40:56 -04004133out_put_css:
4134 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004135out_put_cfile:
4136 fput(cfile);
4137out_put_eventfd:
4138 eventfd_ctx_put(event->eventfd);
4139out_put_efile:
4140 fput(efile);
4141out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004142 kfree(event);
4143
4144 return ret;
4145}
4146
Tejun Heo182446d2013-08-08 20:11:24 -04004147static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4148 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004149{
Tejun Heo182446d2013-08-08 20:11:24 -04004150 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004151}
4152
Tejun Heo182446d2013-08-08 20:11:24 -04004153static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4154 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004155{
4156 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004157 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004158 else
Tejun Heo182446d2013-08-08 20:11:24 -04004159 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004160 return 0;
4161}
4162
Tejun Heod5c56ce2013-06-03 19:14:34 -07004163static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004164 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004165 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004166 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004167 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004168 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004169 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004170 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004171 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004172 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004173 .write_string = cgroup_write_event_control,
4174 .mode = S_IWUGO,
4175 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004176 {
4177 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004178 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004179 .read_u64 = cgroup_clone_children_read,
4180 .write_u64 = cgroup_clone_children_write,
4181 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004182 {
Tejun Heo873fe092013-04-14 20:15:26 -07004183 .name = "cgroup.sane_behavior",
4184 .flags = CFTYPE_ONLY_ON_ROOT,
4185 .read_seq_string = cgroup_sane_behavior_show,
4186 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004187
4188 /*
4189 * Historical crazy stuff. These don't have "cgroup." prefix and
4190 * don't exist if sane_behavior. If you're depending on these, be
4191 * prepared to be burned.
4192 */
4193 {
4194 .name = "tasks",
4195 .flags = CFTYPE_INSANE, /* use "procs" instead */
4196 .open = cgroup_tasks_open,
4197 .write_u64 = cgroup_tasks_write,
4198 .release = cgroup_pidlist_release,
4199 .mode = S_IRUGO | S_IWUSR,
4200 },
4201 {
4202 .name = "notify_on_release",
4203 .flags = CFTYPE_INSANE,
4204 .read_u64 = cgroup_read_notify_on_release,
4205 .write_u64 = cgroup_write_notify_on_release,
4206 },
Tejun Heo873fe092013-04-14 20:15:26 -07004207 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004208 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004209 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004210 .read_seq_string = cgroup_release_agent_show,
4211 .write_string = cgroup_release_agent_write,
4212 .max_write_len = PATH_MAX,
4213 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004214 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004215};
4216
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004217/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004218 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004219 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004220 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004221 *
4222 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004223 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004224static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004226 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004227 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004228
Tejun Heo8e3f6542012-04-01 12:09:55 -07004229 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004230 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004231 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004232
4233 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004234 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004235
Tejun Heobee55092013-06-28 16:24:11 -07004236 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004237 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004238 if (ret < 0)
4239 goto err;
4240 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004241 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004242
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004243 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004244 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004245 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004246 struct css_id *id = rcu_dereference_protected(css->id, true);
4247
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004248 /*
4249 * Update id->css pointer and make this css visible from
4250 * CSS ID functions. This pointer will be dereferened
4251 * from RCU-read-side without locks.
4252 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004253 if (id)
4254 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004255 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004256
4257 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004258err:
4259 cgroup_clear_dir(cgrp, subsys_mask);
4260 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004261}
4262
Tejun Heo0c21ead2013-08-13 20:22:51 -04004263/*
4264 * css destruction is four-stage process.
4265 *
4266 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4267 * Implemented in kill_css().
4268 *
4269 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4270 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4271 * by invoking offline_css(). After offlining, the base ref is put.
4272 * Implemented in css_killed_work_fn().
4273 *
4274 * 3. When the percpu_ref reaches zero, the only possible remaining
4275 * accessors are inside RCU read sections. css_release() schedules the
4276 * RCU callback.
4277 *
4278 * 4. After the grace period, the css can be freed. Implemented in
4279 * css_free_work_fn().
4280 *
4281 * It is actually hairier because both step 2 and 4 require process context
4282 * and thus involve punting to css->destroy_work adding two additional
4283 * steps to the already complex sequence.
4284 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004285static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004286{
4287 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004288 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004289 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004290
Tejun Heo0ae78e02013-08-13 11:01:54 -04004291 if (css->parent)
4292 css_put(css->parent);
4293
Tejun Heo0c21ead2013-08-13 20:22:51 -04004294 css->ss->css_free(css);
4295 cgroup_dput(cgrp);
4296}
4297
4298static void css_free_rcu_fn(struct rcu_head *rcu_head)
4299{
4300 struct cgroup_subsys_state *css =
4301 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4302
4303 /*
4304 * css holds an extra ref to @cgrp->dentry which is put on the last
4305 * css_put(). dput() requires process context which we don't have.
4306 */
4307 INIT_WORK(&css->destroy_work, css_free_work_fn);
4308 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004309}
4310
Tejun Heod3daf282013-06-13 19:39:16 -07004311static void css_release(struct percpu_ref *ref)
4312{
4313 struct cgroup_subsys_state *css =
4314 container_of(ref, struct cgroup_subsys_state, refcnt);
4315
Tejun Heo0c21ead2013-08-13 20:22:51 -04004316 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004317}
4318
Tejun Heo623f9262013-08-13 11:01:55 -04004319static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4320 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004321{
Paul Menagebd89aab2007-10-18 23:40:44 -07004322 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004323 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004325 css->id = NULL;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004326
Tejun Heo0ae78e02013-08-13 11:01:54 -04004327 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004328 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004329 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004330 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004331
Tejun Heoca8bdca2013-08-26 18:40:56 -04004332 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333}
4334
Li Zefan2a4ac632013-07-31 16:16:40 +08004335/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004336static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004337{
Tejun Heo623f9262013-08-13 11:01:55 -04004338 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004339 int ret = 0;
4340
Tejun Heoa31f2d32012-11-19 08:13:37 -08004341 lockdep_assert_held(&cgroup_mutex);
4342
Tejun Heo92fb9742012-11-19 08:13:38 -08004343 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004344 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004345 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004346 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004347 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004348 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4349 }
Tejun Heob1929db2012-11-19 08:13:38 -08004350 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004351}
4352
Li Zefan2a4ac632013-07-31 16:16:40 +08004353/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004354static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004355{
Tejun Heo623f9262013-08-13 11:01:55 -04004356 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004357
4358 lockdep_assert_held(&cgroup_mutex);
4359
4360 if (!(css->flags & CSS_ONLINE))
4361 return;
4362
Li Zefand7eeac12013-03-12 15:35:59 -07004363 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004364 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004365
Tejun Heoeb954192013-08-08 20:11:23 -04004366 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004367 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004368 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004369}
4370
Paul Menageddbcc7e2007-10-18 23:39:30 -07004371/*
Li Zefana043e3b2008-02-23 15:24:09 -08004372 * cgroup_create - create a cgroup
4373 * @parent: cgroup that will be parent of the new cgroup
4374 * @dentry: dentry of the new cgroup
4375 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004376 *
Li Zefana043e3b2008-02-23 15:24:09 -08004377 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004378 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004380 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381{
Tejun Heoae7f1642013-08-13 20:22:50 -04004382 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004383 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004384 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004385 struct cgroupfs_root *root = parent->root;
4386 int err = 0;
4387 struct cgroup_subsys *ss;
4388 struct super_block *sb = root->sb;
4389
Tejun Heo0a950f62012-11-19 09:02:12 -08004390 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004391 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4392 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393 return -ENOMEM;
4394
Li Zefan65dff752013-03-01 15:01:56 +08004395 name = cgroup_alloc_name(dentry);
4396 if (!name)
4397 goto err_free_cgrp;
4398 rcu_assign_pointer(cgrp->name, name);
4399
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004400 /*
4401 * Temporarily set the pointer to NULL, so idr_find() won't return
4402 * a half-baked cgroup.
4403 */
4404 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004405 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004406 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004407
Tejun Heo976c06b2012-11-05 09:16:59 -08004408 /*
4409 * Only live parents can have children. Note that the liveliness
4410 * check isn't strictly necessary because cgroup_mkdir() and
4411 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4412 * anyway so that locking is contained inside cgroup proper and we
4413 * don't get nasty surprises if we ever grow another caller.
4414 */
4415 if (!cgroup_lock_live_group(parent)) {
4416 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004417 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004418 }
4419
Paul Menageddbcc7e2007-10-18 23:39:30 -07004420 /* Grab a reference on the superblock so the hierarchy doesn't
4421 * get deleted on unmount if there are child cgroups. This
4422 * can be done outside cgroup_mutex, since the sb can't
4423 * disappear while someone has an open control file on the
4424 * fs */
4425 atomic_inc(&sb->s_active);
4426
Paul Menagecc31edc2008-10-18 20:28:04 -07004427 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004428
Li Zefanfe1c06c2013-01-24 14:30:22 +08004429 dentry->d_fsdata = cgrp;
4430 cgrp->dentry = dentry;
4431
Paul Menagebd89aab2007-10-18 23:40:44 -07004432 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004433 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004434 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004435
Li Zefanb6abdb02008-03-04 14:28:19 -08004436 if (notify_on_release(parent))
4437 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4438
Tejun Heo2260e7f2012-11-19 08:13:38 -08004439 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4440 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004441
Tejun Heo5549c492013-06-24 15:21:48 -07004442 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004443 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004444
Tejun Heoca8bdca2013-08-26 18:40:56 -04004445 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004446 if (IS_ERR(css)) {
4447 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004448 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004449 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004450 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004451
4452 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004453 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004454 goto err_free_all;
4455
Tejun Heo623f9262013-08-13 11:01:55 -04004456 init_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004457
Li Zefan4528fd02010-02-02 13:44:10 -08004458 if (ss->use_id) {
Tejun Heo623f9262013-08-13 11:01:55 -04004459 err = alloc_css_id(css);
Li Zefan4528fd02010-02-02 13:44:10 -08004460 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004461 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004462 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004463 }
4464
Tejun Heo4e139af2012-11-19 08:13:36 -08004465 /*
4466 * Create directory. cgroup_create_file() returns with the new
4467 * directory locked on success so that it can be populated without
4468 * dropping cgroup_mutex.
4469 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004470 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004471 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004472 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004473 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474
Tejun Heo00356bd2013-06-18 11:14:22 -07004475 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004476
Tejun Heo4e139af2012-11-19 08:13:36 -08004477 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004478 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4479 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004480
Tejun Heo0ae78e02013-08-13 11:01:54 -04004481 /* each css holds a ref to the cgroup's dentry and the parent css */
4482 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004483 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004484
Tejun Heoed9577932012-11-05 09:16:58 -08004485 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004486 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004487 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004488
Li Zefan415cf072013-04-08 14:35:02 +08004489 /* hold a ref to the parent's dentry */
4490 dget(parent->dentry);
4491
Tejun Heob1929db2012-11-19 08:13:38 -08004492 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004493 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004494 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004495
4496 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004497 if (err)
4498 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004499
4500 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4501 parent->parent) {
4502 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",
4503 current->comm, current->pid, ss->name);
4504 if (!strcmp(ss->name, "memory"))
4505 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4506 ss->warned_broken_hierarchy = true;
4507 }
Tejun Heoa8638032012-11-09 09:12:29 -08004508 }
4509
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004510 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4511
Tejun Heo2bb566c2013-08-08 20:11:23 -04004512 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004513 if (err)
4514 goto err_destroy;
4515
4516 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004517 if (err)
4518 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004519
4520 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004521 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522
4523 return 0;
4524
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004525err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004526 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004527 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004528
4529 if (css) {
4530 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004531 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004532 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004533 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004534 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004535 /* Release the reference count that we took on the superblock */
4536 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004537err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004538 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004539err_free_name:
4540 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004541err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004542 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004543 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004544
4545err_destroy:
4546 cgroup_destroy_locked(cgrp);
4547 mutex_unlock(&cgroup_mutex);
4548 mutex_unlock(&dentry->d_inode->i_mutex);
4549 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004550}
4551
Al Viro18bb1db2011-07-26 01:41:39 -04004552static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004553{
4554 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4555
4556 /* the vfs holds inode->i_mutex already */
4557 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4558}
4559
Tejun Heo223dbc32013-08-13 20:22:50 -04004560/*
4561 * This is called when the refcnt of a css is confirmed to be killed.
4562 * css_tryget() is now guaranteed to fail.
4563 */
4564static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004565{
Tejun Heo223dbc32013-08-13 20:22:50 -04004566 struct cgroup_subsys_state *css =
4567 container_of(work, struct cgroup_subsys_state, destroy_work);
4568 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004569
Tejun Heof20104d2013-08-13 20:22:50 -04004570 mutex_lock(&cgroup_mutex);
4571
4572 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004573 * css_tryget() is guaranteed to fail now. Tell subsystems to
4574 * initate destruction.
4575 */
4576 offline_css(css);
4577
4578 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004579 * If @cgrp is marked dead, it's waiting for refs of all css's to
4580 * be disabled before proceeding to the second phase of cgroup
4581 * destruction. If we are the last one, kick it off.
4582 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004583 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004584 cgroup_destroy_css_killed(cgrp);
4585
4586 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004587
4588 /*
4589 * Put the css refs from kill_css(). Each css holds an extra
4590 * reference to the cgroup's dentry and cgroup removal proceeds
4591 * regardless of css refs. On the last put of each css, whenever
4592 * that may be, the extra dentry ref is put so that dentry
4593 * destruction happens only after all css's are released.
4594 */
4595 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004596}
4597
Tejun Heo223dbc32013-08-13 20:22:50 -04004598/* css kill confirmation processing requires process context, bounce */
4599static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004600{
4601 struct cgroup_subsys_state *css =
4602 container_of(ref, struct cgroup_subsys_state, refcnt);
4603
Tejun Heo223dbc32013-08-13 20:22:50 -04004604 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4605 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004606}
4607
4608/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004609 * kill_css - destroy a css
4610 * @css: css to destroy
4611 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004612 * This function initiates destruction of @css by removing cgroup interface
4613 * files and putting its base reference. ->css_offline() will be invoked
4614 * asynchronously once css_tryget() is guaranteed to fail and when the
4615 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004616 */
4617static void kill_css(struct cgroup_subsys_state *css)
4618{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004619 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4620
Tejun Heoedae0c32013-08-13 20:22:51 -04004621 /*
4622 * Killing would put the base ref, but we need to keep it alive
4623 * until after ->css_offline().
4624 */
4625 css_get(css);
4626
4627 /*
4628 * cgroup core guarantees that, by the time ->css_offline() is
4629 * invoked, no new css reference will be given out via
4630 * css_tryget(). We can't simply call percpu_ref_kill() and
4631 * proceed to offlining css's because percpu_ref_kill() doesn't
4632 * guarantee that the ref is seen as killed on all CPUs on return.
4633 *
4634 * Use percpu_ref_kill_and_confirm() to get notifications as each
4635 * css is confirmed to be seen as killed on all CPUs.
4636 */
4637 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004638}
4639
4640/**
4641 * cgroup_destroy_locked - the first stage of cgroup destruction
4642 * @cgrp: cgroup to be destroyed
4643 *
4644 * css's make use of percpu refcnts whose killing latency shouldn't be
4645 * exposed to userland and are RCU protected. Also, cgroup core needs to
4646 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4647 * invoked. To satisfy all the requirements, destruction is implemented in
4648 * the following two steps.
4649 *
4650 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4651 * userland visible parts and start killing the percpu refcnts of
4652 * css's. Set up so that the next stage will be kicked off once all
4653 * the percpu refcnts are confirmed to be killed.
4654 *
4655 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4656 * rest of destruction. Once all cgroup references are gone, the
4657 * cgroup is RCU-freed.
4658 *
4659 * This function implements s1. After this step, @cgrp is gone as far as
4660 * the userland is concerned and a new cgroup with the same name may be
4661 * created. As cgroup doesn't care about the names internally, this
4662 * doesn't cause any problem.
4663 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004664static int cgroup_destroy_locked(struct cgroup *cgrp)
4665 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004666{
Tejun Heo42809dd2012-11-19 08:13:37 -08004667 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004668 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004669 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004670 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004671 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004672
Tejun Heo42809dd2012-11-19 08:13:37 -08004673 lockdep_assert_held(&d->d_inode->i_mutex);
4674 lockdep_assert_held(&cgroup_mutex);
4675
Tejun Heoddd69142013-06-12 21:04:54 -07004676 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004677 * css_set_lock synchronizes access to ->cset_links and prevents
4678 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004679 */
4680 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004681 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004682 read_unlock(&css_set_lock);
4683 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004684 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004685
Tejun Heo1a90dd52012-11-05 09:16:59 -08004686 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004687 * Make sure there's no live children. We can't test ->children
4688 * emptiness as dead children linger on it while being destroyed;
4689 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4690 */
4691 empty = true;
4692 rcu_read_lock();
4693 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4694 empty = cgroup_is_dead(child);
4695 if (!empty)
4696 break;
4697 }
4698 rcu_read_unlock();
4699 if (!empty)
4700 return -EBUSY;
4701
4702 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004703 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4704 * will be invoked to perform the rest of destruction once the
4705 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004706 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004707 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004708 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004709
4710 /*
4711 * Mark @cgrp dead. This prevents further task migration and child
4712 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004713 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004714 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004715 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004716 */
Tejun Heo54766d42013-06-12 21:04:53 -07004717 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004718
Tejun Heo455050d2013-06-13 19:27:41 -07004719 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4720 raw_spin_lock(&release_list_lock);
4721 if (!list_empty(&cgrp->release_list))
4722 list_del_init(&cgrp->release_list);
4723 raw_spin_unlock(&release_list_lock);
4724
4725 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004726 * If @cgrp has css's attached, the second stage of cgroup
4727 * destruction is kicked off from css_killed_work_fn() after the
4728 * refs of all attached css's are killed. If @cgrp doesn't have
4729 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004730 */
Tejun Heof20104d2013-08-13 20:22:50 -04004731 if (!cgrp->nr_css)
4732 cgroup_destroy_css_killed(cgrp);
4733
4734 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004735 * Clear the base files and remove @cgrp directory. The removal
4736 * puts the base ref but we aren't quite done with @cgrp yet, so
4737 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004738 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004739 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004740 dget(d);
4741 cgroup_d_remove_dir(d);
4742
4743 /*
4744 * Unregister events and notify userspace.
4745 * Notify userspace about cgroup removing only after rmdir of cgroup
4746 * directory to avoid race between userspace and kernelspace.
4747 */
4748 spin_lock(&cgrp->event_list_lock);
4749 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4750 list_del_init(&event->list);
4751 schedule_work(&event->remove);
4752 }
4753 spin_unlock(&cgrp->event_list_lock);
4754
Tejun Heoea15f8c2013-06-13 19:27:42 -07004755 return 0;
4756};
4757
Tejun Heod3daf282013-06-13 19:39:16 -07004758/**
Tejun Heof20104d2013-08-13 20:22:50 -04004759 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004760 * @work: cgroup->destroy_free_work
4761 *
4762 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004763 * destroyed after all css's are offlined and performs the rest of
4764 * destruction. This is the second step of destruction described in the
4765 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004766 */
Tejun Heof20104d2013-08-13 20:22:50 -04004767static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004768{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004769 struct cgroup *parent = cgrp->parent;
4770 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004771
Tejun Heof20104d2013-08-13 20:22:50 -04004772 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004773
Paul Menage999cd8a2009-01-07 18:08:36 -08004774 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004775 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004776
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004777 /*
4778 * We should remove the cgroup object from idr before its grace
4779 * period starts, so we won't be looking up a cgroup while the
4780 * cgroup is being freed.
4781 */
4782 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4783 cgrp->id = -1;
4784
Paul Menageddbcc7e2007-10-18 23:39:30 -07004785 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004786
Paul Menagebd89aab2007-10-18 23:40:44 -07004787 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004788 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789}
4790
Tejun Heo42809dd2012-11-19 08:13:37 -08004791static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4792{
4793 int ret;
4794
4795 mutex_lock(&cgroup_mutex);
4796 ret = cgroup_destroy_locked(dentry->d_fsdata);
4797 mutex_unlock(&cgroup_mutex);
4798
4799 return ret;
4800}
4801
Tejun Heo8e3f6542012-04-01 12:09:55 -07004802static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4803{
4804 INIT_LIST_HEAD(&ss->cftsets);
4805
4806 /*
4807 * base_cftset is embedded in subsys itself, no need to worry about
4808 * deregistration.
4809 */
4810 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004811 struct cftype *cft;
4812
4813 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4814 cft->ss = ss;
4815
Tejun Heo8e3f6542012-04-01 12:09:55 -07004816 ss->base_cftset.cfts = ss->base_cftypes;
4817 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4818 }
4819}
4820
Li Zefan06a11922008-04-29 01:00:07 -07004821static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004822{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004823 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004824
4825 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004826
Tejun Heo648bb562012-11-19 08:13:36 -08004827 mutex_lock(&cgroup_mutex);
4828
Tejun Heo8e3f6542012-04-01 12:09:55 -07004829 /* init base cftset */
4830 cgroup_init_cftsets(ss);
4831
Paul Menageddbcc7e2007-10-18 23:39:30 -07004832 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004833 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4834 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004835 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004836 /* We don't handle early failures gracefully */
4837 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004838 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839
Li Zefane8d55fd2008-04-29 01:00:13 -07004840 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004841 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004842 * newly registered, all tasks and hence the
4843 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004844 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004845
4846 need_forkexit_callback |= ss->fork || ss->exit;
4847
Li Zefane8d55fd2008-04-29 01:00:13 -07004848 /* At system boot, before all subsystems have been
4849 * registered, no tasks have been forked, so we don't
4850 * need to invoke fork callbacks here. */
4851 BUG_ON(!list_empty(&init_task.tasks));
4852
Tejun Heoae7f1642013-08-13 20:22:50 -04004853 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004854
Tejun Heo648bb562012-11-19 08:13:36 -08004855 mutex_unlock(&cgroup_mutex);
4856
Ben Blume6a11052010-03-10 15:22:09 -08004857 /* this function shouldn't be used with modular subsystems, since they
4858 * need to register a subsys_id, among other things */
4859 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004860}
4861
4862/**
Ben Blume6a11052010-03-10 15:22:09 -08004863 * cgroup_load_subsys: load and register a modular subsystem at runtime
4864 * @ss: the subsystem to load
4865 *
4866 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004867 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004868 * up for use. If the subsystem is built-in anyway, work is delegated to the
4869 * simpler cgroup_init_subsys.
4870 */
4871int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4872{
Ben Blume6a11052010-03-10 15:22:09 -08004873 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004874 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004875 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004876 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004877 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004878
4879 /* check name and function validity */
4880 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004881 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004882 return -EINVAL;
4883
4884 /*
4885 * we don't support callbacks in modular subsystems. this check is
4886 * before the ss->module check for consistency; a subsystem that could
4887 * be a module should still have no callbacks even if the user isn't
4888 * compiling it as one.
4889 */
4890 if (ss->fork || ss->exit)
4891 return -EINVAL;
4892
4893 /*
4894 * an optionally modular subsystem is built-in: we want to do nothing,
4895 * since cgroup_init_subsys will have already taken care of it.
4896 */
4897 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004898 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004899 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004900 return 0;
4901 }
4902
Tejun Heo8e3f6542012-04-01 12:09:55 -07004903 /* init base cftset */
4904 cgroup_init_cftsets(ss);
4905
Ben Blume6a11052010-03-10 15:22:09 -08004906 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004907 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004908
4909 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004910 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004911 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004912 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004913 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004914 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004915 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004916 /* failure case - need to deassign the cgroup_subsys[] slot. */
4917 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004918 mutex_unlock(&cgroup_mutex);
4919 return PTR_ERR(css);
4920 }
4921
Tejun Heo9871bf92013-06-24 15:21:47 -07004922 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4923 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004924
4925 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004926 init_css(css, ss, cgroup_dummy_top);
4927 /* init_idr must be after init_css() because it sets css->id. */
Ben Blume6a11052010-03-10 15:22:09 -08004928 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004929 ret = cgroup_init_idr(ss, css);
4930 if (ret)
4931 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004932 }
4933
4934 /*
4935 * Now we need to entangle the css into the existing css_sets. unlike
4936 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4937 * will need a new pointer to it; done by iterating the css_set_table.
4938 * furthermore, modifying the existing css_sets will corrupt the hash
4939 * table state, so each changed css_set will need its hash recomputed.
4940 * this is all done under the css_set_lock.
4941 */
4942 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004943 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004944 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004945 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004946 continue;
4947 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004948 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004949 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004950 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004951 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004952 key = css_set_hash(cset->subsys);
4953 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004954 }
4955 write_unlock(&css_set_lock);
4956
Tejun Heoae7f1642013-08-13 20:22:50 -04004957 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004958 if (ret)
4959 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004960
Ben Blume6a11052010-03-10 15:22:09 -08004961 /* success! */
4962 mutex_unlock(&cgroup_mutex);
4963 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004964
4965err_unload:
4966 mutex_unlock(&cgroup_mutex);
4967 /* @ss can't be mounted here as try_module_get() would fail */
4968 cgroup_unload_subsys(ss);
4969 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004970}
4971EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4972
4973/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004974 * cgroup_unload_subsys: unload a modular subsystem
4975 * @ss: the subsystem to unload
4976 *
4977 * This function should be called in a modular subsystem's exitcall. When this
4978 * function is invoked, the refcount on the subsystem's module will be 0, so
4979 * the subsystem will not be attached to any hierarchy.
4980 */
4981void cgroup_unload_subsys(struct cgroup_subsys *ss)
4982{
Tejun Heo69d02062013-06-12 21:04:50 -07004983 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004984
4985 BUG_ON(ss->module == NULL);
4986
4987 /*
4988 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004989 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004990 * doesn't start being used while we're killing it off.
4991 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004992 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004993
4994 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004995
Tejun Heoca8bdca2013-08-26 18:40:56 -04004996 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004997
Tejun Heoc897ff62013-02-27 17:03:49 -08004998 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004999 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08005000
Ben Blumcf5d5942010-03-10 15:22:09 -08005001 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07005002 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08005003
Tejun Heo9871bf92013-06-24 15:21:47 -07005004 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07005005 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08005006
5007 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005008 * disentangle the css from all css_sets attached to the dummy
5009 * top. as in loading, we need to pay our respects to the hashtable
5010 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08005011 */
5012 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07005013 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005014 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08005015 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08005016
Tejun Heo5abb8852013-06-12 21:04:49 -07005017 hash_del(&cset->hlist);
5018 cset->subsys[ss->subsys_id] = NULL;
5019 key = css_set_hash(cset->subsys);
5020 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08005021 }
5022 write_unlock(&css_set_lock);
5023
5024 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005025 * remove subsystem's css from the cgroup_dummy_top and free it -
5026 * need to free before marking as null because ss->css_free needs
5027 * the cgrp->subsys pointer to find their state. note that this
5028 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08005029 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04005030 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04005031 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08005032
5033 mutex_unlock(&cgroup_mutex);
5034}
5035EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
5036
5037/**
Li Zefana043e3b2008-02-23 15:24:09 -08005038 * cgroup_init_early - cgroup initialization at system boot
5039 *
5040 * Initialize cgroups at system boot, and initialize any
5041 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005042 */
5043int __init cgroup_init_early(void)
5044{
Tejun Heo30159ec2013-06-25 11:53:37 -07005045 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005046 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005047
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07005048 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07005049 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07005050 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005051 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005052 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005053 init_cgroup_root(&cgroup_dummy_root);
5054 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005055 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005056
Tejun Heo69d02062013-06-12 21:04:50 -07005057 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005058 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5059 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005060 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005061
Tejun Heo30159ec2013-06-25 11:53:37 -07005062 /* at bootup time, we don't worry about modular subsystems */
5063 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005064 BUG_ON(!ss->name);
5065 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005066 BUG_ON(!ss->css_alloc);
5067 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005068 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005069 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005070 ss->name, ss->subsys_id);
5071 BUG();
5072 }
5073
5074 if (ss->early_init)
5075 cgroup_init_subsys(ss);
5076 }
5077 return 0;
5078}
5079
5080/**
Li Zefana043e3b2008-02-23 15:24:09 -08005081 * cgroup_init - cgroup initialization
5082 *
5083 * Register cgroup filesystem and /proc file, and initialize
5084 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005085 */
5086int __init cgroup_init(void)
5087{
Tejun Heo30159ec2013-06-25 11:53:37 -07005088 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005089 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005090 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005091
5092 err = bdi_init(&cgroup_backing_dev_info);
5093 if (err)
5094 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005095
Tejun Heo30159ec2013-06-25 11:53:37 -07005096 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005097 if (!ss->early_init)
5098 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005099 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005100 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005101 }
5102
Tejun Heofa3ca072013-04-14 11:36:56 -07005103 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005104 mutex_lock(&cgroup_mutex);
5105 mutex_lock(&cgroup_root_mutex);
5106
Tejun Heo82fe9b02013-06-25 11:53:37 -07005107 /* Add init_css_set to the hash table */
5108 key = css_set_hash(init_css_set.subsys);
5109 hash_add(css_set_table, &init_css_set.hlist, key);
5110
Tejun Heofc76df72013-06-25 11:53:37 -07005111 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005112
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005113 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5114 0, 1, GFP_KERNEL);
5115 BUG_ON(err < 0);
5116
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005117 mutex_unlock(&cgroup_root_mutex);
5118 mutex_unlock(&cgroup_mutex);
5119
Greg KH676db4a2010-08-05 13:53:35 -07005120 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5121 if (!cgroup_kobj) {
5122 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005123 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005124 }
5125
5126 err = register_filesystem(&cgroup_fs_type);
5127 if (err < 0) {
5128 kobject_put(cgroup_kobj);
5129 goto out;
5130 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005131
Li Zefan46ae2202008-04-29 01:00:08 -07005132 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005133
Paul Menageddbcc7e2007-10-18 23:39:30 -07005134out:
Paul Menagea4243162007-10-18 23:39:35 -07005135 if (err)
5136 bdi_destroy(&cgroup_backing_dev_info);
5137
Paul Menageddbcc7e2007-10-18 23:39:30 -07005138 return err;
5139}
Paul Menageb4f48b62007-10-18 23:39:33 -07005140
Paul Menagea4243162007-10-18 23:39:35 -07005141/*
5142 * proc_cgroup_show()
5143 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5144 * - Used for /proc/<pid>/cgroup.
5145 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5146 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005147 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005148 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5149 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5150 * cgroup to top_cgroup.
5151 */
5152
5153/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005154int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005155{
5156 struct pid *pid;
5157 struct task_struct *tsk;
5158 char *buf;
5159 int retval;
5160 struct cgroupfs_root *root;
5161
5162 retval = -ENOMEM;
5163 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5164 if (!buf)
5165 goto out;
5166
5167 retval = -ESRCH;
5168 pid = m->private;
5169 tsk = get_pid_task(pid, PIDTYPE_PID);
5170 if (!tsk)
5171 goto out_free;
5172
5173 retval = 0;
5174
5175 mutex_lock(&cgroup_mutex);
5176
Li Zefane5f6a862009-01-07 18:07:41 -08005177 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005178 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005179 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005180 int count = 0;
5181
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005182 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005183 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005184 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005185 if (strlen(root->name))
5186 seq_printf(m, "%sname=%s", count ? "," : "",
5187 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005188 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005189 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005190 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005191 if (retval < 0)
5192 goto out_unlock;
5193 seq_puts(m, buf);
5194 seq_putc(m, '\n');
5195 }
5196
5197out_unlock:
5198 mutex_unlock(&cgroup_mutex);
5199 put_task_struct(tsk);
5200out_free:
5201 kfree(buf);
5202out:
5203 return retval;
5204}
5205
Paul Menagea4243162007-10-18 23:39:35 -07005206/* Display information about each subsystem and each hierarchy */
5207static int proc_cgroupstats_show(struct seq_file *m, void *v)
5208{
Tejun Heo30159ec2013-06-25 11:53:37 -07005209 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005210 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005211
Paul Menage8bab8dd2008-04-04 14:29:57 -07005212 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005213 /*
5214 * ideally we don't want subsystems moving around while we do this.
5215 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5216 * subsys/hierarchy state.
5217 */
Paul Menagea4243162007-10-18 23:39:35 -07005218 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005219
5220 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005221 seq_printf(m, "%s\t%d\t%d\t%d\n",
5222 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005223 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005224
Paul Menagea4243162007-10-18 23:39:35 -07005225 mutex_unlock(&cgroup_mutex);
5226 return 0;
5227}
5228
5229static int cgroupstats_open(struct inode *inode, struct file *file)
5230{
Al Viro9dce07f2008-03-29 03:07:28 +00005231 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005232}
5233
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005234static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005235 .open = cgroupstats_open,
5236 .read = seq_read,
5237 .llseek = seq_lseek,
5238 .release = single_release,
5239};
5240
Paul Menageb4f48b62007-10-18 23:39:33 -07005241/**
5242 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005243 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005244 *
5245 * Description: A task inherits its parent's cgroup at fork().
5246 *
5247 * A pointer to the shared css_set was automatically copied in
5248 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005249 * it was not made under the protection of RCU or cgroup_mutex, so
5250 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5251 * have already changed current->cgroups, allowing the previously
5252 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005253 *
5254 * At the point that cgroup_fork() is called, 'current' is the parent
5255 * task, and the passed argument 'child' points to the child task.
5256 */
5257void cgroup_fork(struct task_struct *child)
5258{
Tejun Heo9bb71302012-10-18 17:52:07 -07005259 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005260 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005261 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005262 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005263 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005264}
5265
5266/**
Li Zefana043e3b2008-02-23 15:24:09 -08005267 * cgroup_post_fork - called on a new task after adding it to the task list
5268 * @child: the task in question
5269 *
Tejun Heo5edee612012-10-16 15:03:14 -07005270 * Adds the task to the list running through its css_set if necessary and
5271 * call the subsystem fork() callbacks. Has to be after the task is
5272 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005273 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005274 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005275 */
Paul Menage817929e2007-10-18 23:39:36 -07005276void cgroup_post_fork(struct task_struct *child)
5277{
Tejun Heo30159ec2013-06-25 11:53:37 -07005278 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005279 int i;
5280
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005281 /*
5282 * use_task_css_set_links is set to 1 before we walk the tasklist
5283 * under the tasklist_lock and we read it here after we added the child
5284 * to the tasklist under the tasklist_lock as well. If the child wasn't
5285 * yet in the tasklist when we walked through it from
5286 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5287 * should be visible now due to the paired locking and barriers implied
5288 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5289 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5290 * lock on fork.
5291 */
Paul Menage817929e2007-10-18 23:39:36 -07005292 if (use_task_css_set_links) {
5293 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005294 task_lock(child);
5295 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005296 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005297 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005298 write_unlock(&css_set_lock);
5299 }
Tejun Heo5edee612012-10-16 15:03:14 -07005300
5301 /*
5302 * Call ss->fork(). This must happen after @child is linked on
5303 * css_set; otherwise, @child might change state between ->fork()
5304 * and addition to css_set.
5305 */
5306 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005307 /*
5308 * fork/exit callbacks are supported only for builtin
5309 * subsystems, and the builtin section of the subsys
5310 * array is immutable, so we don't need to lock the
5311 * subsys array here. On the other hand, modular section
5312 * of the array can be freed at module unload, so we
5313 * can't touch that.
5314 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005315 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005316 if (ss->fork)
5317 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005318 }
Paul Menage817929e2007-10-18 23:39:36 -07005319}
Tejun Heo5edee612012-10-16 15:03:14 -07005320
Paul Menage817929e2007-10-18 23:39:36 -07005321/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005322 * cgroup_exit - detach cgroup from exiting task
5323 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005324 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005325 *
5326 * Description: Detach cgroup from @tsk and release it.
5327 *
5328 * Note that cgroups marked notify_on_release force every task in
5329 * them to take the global cgroup_mutex mutex when exiting.
5330 * This could impact scaling on very large systems. Be reluctant to
5331 * use notify_on_release cgroups where very high task exit scaling
5332 * is required on large systems.
5333 *
5334 * the_top_cgroup_hack:
5335 *
5336 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5337 *
5338 * We call cgroup_exit() while the task is still competent to
5339 * handle notify_on_release(), then leave the task attached to the
5340 * root cgroup in each hierarchy for the remainder of its exit.
5341 *
5342 * To do this properly, we would increment the reference count on
5343 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5344 * code we would add a second cgroup function call, to drop that
5345 * reference. This would just create an unnecessary hot spot on
5346 * the top_cgroup reference count, to no avail.
5347 *
5348 * Normally, holding a reference to a cgroup without bumping its
5349 * count is unsafe. The cgroup could go away, or someone could
5350 * attach us to a different cgroup, decrementing the count on
5351 * the first cgroup that we never incremented. But in this case,
5352 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005353 * which wards off any cgroup_attach_task() attempts, or task is a failed
5354 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005355 */
5356void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5357{
Tejun Heo30159ec2013-06-25 11:53:37 -07005358 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005359 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005360 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005361
5362 /*
5363 * Unlink from the css_set task list if necessary.
5364 * Optimistically check cg_list before taking
5365 * css_set_lock
5366 */
5367 if (!list_empty(&tsk->cg_list)) {
5368 write_lock(&css_set_lock);
5369 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005370 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005371 write_unlock(&css_set_lock);
5372 }
5373
Paul Menageb4f48b62007-10-18 23:39:33 -07005374 /* Reassign the task to the init_css_set. */
5375 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005376 cset = task_css_set(tsk);
5377 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005378
5379 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005380 /*
5381 * fork/exit callbacks are supported only for builtin
5382 * subsystems, see cgroup_post_fork() for details.
5383 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005384 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005385 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005386 struct cgroup_subsys_state *old_css = cset->subsys[i];
5387 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005388
Tejun Heoeb954192013-08-08 20:11:23 -04005389 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005390 }
5391 }
5392 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005393 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005394
Tejun Heo5abb8852013-06-12 21:04:49 -07005395 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005396}
Paul Menage697f4162007-10-18 23:39:34 -07005397
Paul Menagebd89aab2007-10-18 23:40:44 -07005398static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005399{
Li Zefanf50daa72013-03-01 15:06:07 +08005400 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005401 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005402 /*
5403 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005404 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005405 * it now
5406 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005408
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005409 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005410 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005411 list_empty(&cgrp->release_list)) {
5412 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005413 need_schedule_work = 1;
5414 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005415 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005416 if (need_schedule_work)
5417 schedule_work(&release_agent_work);
5418 }
5419}
5420
Paul Menage81a6a5c2007-10-18 23:39:38 -07005421/*
5422 * Notify userspace when a cgroup is released, by running the
5423 * configured release agent with the name of the cgroup (path
5424 * relative to the root of cgroup file system) as the argument.
5425 *
5426 * Most likely, this user command will try to rmdir this cgroup.
5427 *
5428 * This races with the possibility that some other task will be
5429 * attached to this cgroup before it is removed, or that some other
5430 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5431 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5432 * unused, and this cgroup will be reprieved from its death sentence,
5433 * to continue to serve a useful existence. Next time it's released,
5434 * we will get notified again, if it still has 'notify_on_release' set.
5435 *
5436 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5437 * means only wait until the task is successfully execve()'d. The
5438 * separate release agent task is forked by call_usermodehelper(),
5439 * then control in this thread returns here, without waiting for the
5440 * release agent task. We don't bother to wait because the caller of
5441 * this routine has no use for the exit status of the release agent
5442 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005443 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005444static void cgroup_release_agent(struct work_struct *work)
5445{
5446 BUG_ON(work != &release_agent_work);
5447 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005448 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005449 while (!list_empty(&release_list)) {
5450 char *argv[3], *envp[3];
5451 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005452 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005453 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005454 struct cgroup,
5455 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005456 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005457 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005458 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005459 if (!pathbuf)
5460 goto continue_free;
5461 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5462 goto continue_free;
5463 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5464 if (!agentbuf)
5465 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005466
5467 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005468 argv[i++] = agentbuf;
5469 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005470 argv[i] = NULL;
5471
5472 i = 0;
5473 /* minimal command environment */
5474 envp[i++] = "HOME=/";
5475 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5476 envp[i] = NULL;
5477
5478 /* Drop the lock while we invoke the usermode helper,
5479 * since the exec could involve hitting disk and hence
5480 * be a slow process */
5481 mutex_unlock(&cgroup_mutex);
5482 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005483 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005484 continue_free:
5485 kfree(pathbuf);
5486 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005487 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005488 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005489 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005490 mutex_unlock(&cgroup_mutex);
5491}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005492
5493static int __init cgroup_disable(char *str)
5494{
Tejun Heo30159ec2013-06-25 11:53:37 -07005495 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005496 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005497 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005498
5499 while ((token = strsep(&str, ",")) != NULL) {
5500 if (!*token)
5501 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005502
Tejun Heo30159ec2013-06-25 11:53:37 -07005503 /*
5504 * cgroup_disable, being at boot time, can't know about
5505 * module subsystems, so we don't worry about them.
5506 */
5507 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005508 if (!strcmp(token, ss->name)) {
5509 ss->disabled = 1;
5510 printk(KERN_INFO "Disabling %s control group"
5511 " subsystem\n", ss->name);
5512 break;
5513 }
5514 }
5515 }
5516 return 1;
5517}
5518__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005519
5520/*
5521 * Functons for CSS ID.
5522 */
5523
Tejun Heo54766d42013-06-12 21:04:53 -07005524/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005525unsigned short css_id(struct cgroup_subsys_state *css)
5526{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005527 struct css_id *cssid;
5528
5529 /*
5530 * This css_id() can return correct value when somone has refcnt
5531 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5532 * it's unchanged until freed.
5533 */
Tejun Heod3daf282013-06-13 19:39:16 -07005534 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005535
5536 if (cssid)
5537 return cssid->id;
5538 return 0;
5539}
Ben Blum67523c42010-03-10 15:22:11 -08005540EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005541
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005542/**
5543 * css_is_ancestor - test "root" css is an ancestor of "child"
5544 * @child: the css to be tested.
5545 * @root: the css supporsed to be an ancestor of the child.
5546 *
5547 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005548 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005549 * But, considering usual usage, the csses should be valid objects after test.
5550 * Assuming that the caller will do some action to the child if this returns
5551 * returns true, the caller must take "child";s reference count.
5552 * If "child" is valid object and this returns true, "root" is valid, too.
5553 */
5554
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005555bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005556 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005557{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005558 struct css_id *child_id;
5559 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005560
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005561 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005562 if (!child_id)
5563 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005564 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005565 if (!root_id)
5566 return false;
5567 if (child_id->depth < root_id->depth)
5568 return false;
5569 if (child_id->stack[root_id->depth] != root_id->id)
5570 return false;
5571 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005572}
5573
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005574void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5575{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005576 struct css_id *id = rcu_dereference_protected(css->id, true);
5577
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005578 /* When this is called before css_id initialization, id can be NULL */
5579 if (!id)
5580 return;
5581
5582 BUG_ON(!ss->use_id);
5583
5584 rcu_assign_pointer(id->css, NULL);
5585 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005586 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005587 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005588 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005589 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005590}
Ben Blum67523c42010-03-10 15:22:11 -08005591EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005592
5593/*
5594 * This is called by init or create(). Then, calls to this function are
5595 * always serialized (By cgroup_mutex() at create()).
5596 */
5597
5598static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5599{
5600 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005601 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005602
5603 BUG_ON(!ss->use_id);
5604
5605 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5606 newid = kzalloc(size, GFP_KERNEL);
5607 if (!newid)
5608 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005609
5610 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005611 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005612 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005613 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005614 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005615 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005616
5617 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005618 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005619 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005620
Tejun Heod228d9e2013-02-27 17:04:54 -08005621 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005622 newid->depth = depth;
5623 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005624err_out:
5625 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005626 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005627
5628}
5629
Ben Blume6a11052010-03-10 15:22:09 -08005630static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5631 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005632{
5633 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005634
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005635 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005636 idr_init(&ss->idr);
5637
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005638 newid = get_new_cssid(ss, 0);
5639 if (IS_ERR(newid))
5640 return PTR_ERR(newid);
5641
5642 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005643 RCU_INIT_POINTER(newid->css, rootcss);
5644 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005645 return 0;
5646}
5647
Tejun Heo623f9262013-08-13 11:01:55 -04005648static int alloc_css_id(struct cgroup_subsys_state *child_css)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005649{
Tejun Heo623f9262013-08-13 11:01:55 -04005650 struct cgroup_subsys_state *parent_css = css_parent(child_css);
Li Zefanfae9c792010-04-22 17:30:00 +08005651 struct css_id *child_id, *parent_id;
Tejun Heo623f9262013-08-13 11:01:55 -04005652 int i, depth;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005653
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005654 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005655 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005656
Tejun Heo623f9262013-08-13 11:01:55 -04005657 child_id = get_new_cssid(child_css->ss, depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005658 if (IS_ERR(child_id))
5659 return PTR_ERR(child_id);
5660
5661 for (i = 0; i < depth; i++)
5662 child_id->stack[i] = parent_id->stack[i];
5663 child_id->stack[depth] = child_id->id;
5664 /*
5665 * child_id->css pointer will be set after this cgroup is available
5666 * see cgroup_populate_dir()
5667 */
5668 rcu_assign_pointer(child_css->id, child_id);
5669
5670 return 0;
5671}
5672
5673/**
5674 * css_lookup - lookup css by id
5675 * @ss: cgroup subsys to be looked into.
5676 * @id: the id
5677 *
5678 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5679 * NULL if not. Should be called under rcu_read_lock()
5680 */
5681struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5682{
5683 struct css_id *cssid = NULL;
5684
5685 BUG_ON(!ss->use_id);
5686 cssid = idr_find(&ss->idr, id);
5687
5688 if (unlikely(!cssid))
5689 return NULL;
5690
5691 return rcu_dereference(cssid->css);
5692}
Ben Blum67523c42010-03-10 15:22:11 -08005693EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005694
Tejun Heob77d7b62013-08-13 11:01:54 -04005695/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005696 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5697 * @dentry: directory dentry of interest
5698 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005699 *
5700 * Must be called under RCU read lock. The caller is responsible for
5701 * pinning the returned css if it needs to be accessed outside the RCU
5702 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005703 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005704struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5705 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005706{
5707 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005708
Tejun Heob77d7b62013-08-13 11:01:54 -04005709 WARN_ON_ONCE(!rcu_read_lock_held());
5710
Tejun Heo35cf0832013-08-26 18:40:56 -04005711 /* is @dentry a cgroup dir? */
5712 if (!dentry->d_inode ||
5713 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005714 return ERR_PTR(-EBADF);
5715
Tejun Heo35cf0832013-08-26 18:40:56 -04005716 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005717 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005718}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005719
Li Zefan1cb650b2013-08-19 10:05:24 +08005720/**
5721 * css_from_id - lookup css by id
5722 * @id: the cgroup id
5723 * @ss: cgroup subsys to be looked into
5724 *
5725 * Returns the css if there's valid one with @id, otherwise returns NULL.
5726 * Should be called under rcu_read_lock().
5727 */
5728struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5729{
5730 struct cgroup *cgrp;
5731
5732 rcu_lockdep_assert(rcu_read_lock_held() ||
5733 lockdep_is_held(&cgroup_mutex),
5734 "css_from_id() needs proper protection");
5735
5736 cgrp = idr_find(&ss->root->cgroup_idr, id);
5737 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005738 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005739 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005740}
5741
Paul Menagefe693432009-09-23 15:56:20 -07005742#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005743static struct cgroup_subsys_state *
5744debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005745{
5746 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5747
5748 if (!css)
5749 return ERR_PTR(-ENOMEM);
5750
5751 return css;
5752}
5753
Tejun Heoeb954192013-08-08 20:11:23 -04005754static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005755{
Tejun Heoeb954192013-08-08 20:11:23 -04005756 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005757}
5758
Tejun Heo182446d2013-08-08 20:11:24 -04005759static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5760 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005761{
Tejun Heo182446d2013-08-08 20:11:24 -04005762 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005763}
5764
Tejun Heo182446d2013-08-08 20:11:24 -04005765static u64 current_css_set_read(struct cgroup_subsys_state *css,
5766 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005767{
5768 return (u64)(unsigned long)current->cgroups;
5769}
5770
Tejun Heo182446d2013-08-08 20:11:24 -04005771static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005772 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005773{
5774 u64 count;
5775
5776 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005777 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005778 rcu_read_unlock();
5779 return count;
5780}
5781
Tejun Heo182446d2013-08-08 20:11:24 -04005782static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005783 struct cftype *cft,
5784 struct seq_file *seq)
5785{
Tejun Heo69d02062013-06-12 21:04:50 -07005786 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005787 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005788
5789 read_lock(&css_set_lock);
5790 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005791 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005792 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005793 struct cgroup *c = link->cgrp;
5794 const char *name;
5795
5796 if (c->dentry)
5797 name = c->dentry->d_name.name;
5798 else
5799 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005800 seq_printf(seq, "Root %d group %s\n",
5801 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005802 }
5803 rcu_read_unlock();
5804 read_unlock(&css_set_lock);
5805 return 0;
5806}
5807
5808#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005809static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5810 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005811{
Tejun Heo69d02062013-06-12 21:04:50 -07005812 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005813
5814 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005815 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005816 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005817 struct task_struct *task;
5818 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005819 seq_printf(seq, "css_set %p\n", cset);
5820 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005821 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5822 seq_puts(seq, " ...\n");
5823 break;
5824 } else {
5825 seq_printf(seq, " task %d\n",
5826 task_pid_vnr(task));
5827 }
5828 }
5829 }
5830 read_unlock(&css_set_lock);
5831 return 0;
5832}
5833
Tejun Heo182446d2013-08-08 20:11:24 -04005834static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005835{
Tejun Heo182446d2013-08-08 20:11:24 -04005836 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005837}
5838
5839static struct cftype debug_files[] = {
5840 {
Paul Menagefe693432009-09-23 15:56:20 -07005841 .name = "taskcount",
5842 .read_u64 = debug_taskcount_read,
5843 },
5844
5845 {
5846 .name = "current_css_set",
5847 .read_u64 = current_css_set_read,
5848 },
5849
5850 {
5851 .name = "current_css_set_refcount",
5852 .read_u64 = current_css_set_refcount_read,
5853 },
5854
5855 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005856 .name = "current_css_set_cg_links",
5857 .read_seq_string = current_css_set_cg_links_read,
5858 },
5859
5860 {
5861 .name = "cgroup_css_links",
5862 .read_seq_string = cgroup_css_links_read,
5863 },
5864
5865 {
Paul Menagefe693432009-09-23 15:56:20 -07005866 .name = "releasable",
5867 .read_u64 = releasable_read,
5868 },
Paul Menagefe693432009-09-23 15:56:20 -07005869
Tejun Heo4baf6e32012-04-01 12:09:55 -07005870 { } /* terminate */
5871};
Paul Menagefe693432009-09-23 15:56:20 -07005872
5873struct cgroup_subsys debug_subsys = {
5874 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005875 .css_alloc = debug_css_alloc,
5876 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005877 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005878 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005879};
5880#endif /* CONFIG_CGROUP_DEBUG */