blob: e76698dd6c0820091c55a991cb9c98d10485656d [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}
246
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);
Al Viro00cd8dd2012-06-10 17:13:09 -0400833static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700835static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700836static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700837static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700838
839static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200840 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700841 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700842};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700843
Tejun Heo623f9262013-08-13 11:01:55 -0400844static int alloc_css_id(struct cgroup_subsys_state *child_css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700845
Al Viroa5e7ed32011-07-26 01:55:55 -0400846static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847{
848 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700849
850 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400851 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700852 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100853 inode->i_uid = current_fsuid();
854 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700855 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
856 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
857 }
858 return inode;
859}
860
Li Zefan65dff752013-03-01 15:01:56 +0800861static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
862{
863 struct cgroup_name *name;
864
865 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
866 if (!name)
867 return NULL;
868 strcpy(name->name, dentry->d_name.name);
869 return name;
870}
871
Li Zefanbe445622013-01-24 14:31:42 +0800872static void cgroup_free_fn(struct work_struct *work)
873{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700874 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800875
876 mutex_lock(&cgroup_mutex);
Li Zefanbe445622013-01-24 14:31:42 +0800877 cgrp->root->number_of_cgroups--;
878 mutex_unlock(&cgroup_mutex);
879
880 /*
Li Zefan415cf072013-04-08 14:35:02 +0800881 * We get a ref to the parent's dentry, and put the ref when
882 * this cgroup is being freed, so it's guaranteed that the
883 * parent won't be destroyed before its children.
884 */
885 dput(cgrp->parent->dentry);
886
887 /*
Li Zefanbe445622013-01-24 14:31:42 +0800888 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700889 * created the cgroup. This will free cgrp->root, if we are
890 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800891 */
892 deactivate_super(cgrp->root->sb);
893
894 /*
895 * if we're getting rid of the cgroup, refcount should ensure
896 * that there are no pidlists left.
897 */
898 BUG_ON(!list_empty(&cgrp->pidlists));
899
900 simple_xattrs_free(&cgrp->xattrs);
901
Li Zefan65dff752013-03-01 15:01:56 +0800902 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800903 kfree(cgrp);
904}
905
906static void cgroup_free_rcu(struct rcu_head *head)
907{
908 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
909
Tejun Heoea15f8c2013-06-13 19:27:42 -0700910 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
911 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800912}
913
Paul Menageddbcc7e2007-10-18 23:39:30 -0700914static void cgroup_diput(struct dentry *dentry, struct inode *inode)
915{
916 /* is dentry a directory ? if so, kfree() associated cgroup */
917 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700918 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800919
Tejun Heo54766d42013-06-12 21:04:53 -0700920 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800921 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700922 } else {
923 struct cfent *cfe = __d_cfe(dentry);
924 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
925
926 WARN_ONCE(!list_empty(&cfe->node) &&
927 cgrp != &cgrp->root->top_cgroup,
928 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700929 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700930 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700931 }
932 iput(inode);
933}
934
Al Viroc72a04e2011-01-14 05:31:45 +0000935static int cgroup_delete(const struct dentry *d)
936{
937 return 1;
938}
939
Paul Menageddbcc7e2007-10-18 23:39:30 -0700940static void remove_dir(struct dentry *d)
941{
942 struct dentry *parent = dget(d->d_parent);
943
944 d_delete(d);
945 simple_rmdir(parent->d_inode, d);
946 dput(parent);
947}
948
Li Zefan2739d3c2013-01-21 18:18:33 +0800949static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700950{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700951 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
954 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100955
Li Zefan2739d3c2013-01-21 18:18:33 +0800956 /*
957 * If we're doing cleanup due to failure of cgroup_create(),
958 * the corresponding @cfe may not exist.
959 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700960 list_for_each_entry(cfe, &cgrp->files, node) {
961 struct dentry *d = cfe->dentry;
962
963 if (cft && cfe->type != cft)
964 continue;
965
966 dget(d);
967 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700968 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700969 list_del_init(&cfe->node);
970 dput(d);
971
Li Zefan2739d3c2013-01-21 18:18:33 +0800972 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700973 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700974}
975
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400976/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700977 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700978 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400979 * @subsys_mask: mask of the subsystem ids whose files should be removed
980 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700981static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700982{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400983 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700984 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700985
Tejun Heob420ba72013-07-12 12:34:02 -0700986 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400987 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700988
989 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400990 continue;
991 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400992 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400993 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700994}
995
996/*
997 * NOTE : the dentry must have been dget()'ed
998 */
999static void cgroup_d_remove_dir(struct dentry *dentry)
1000{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001001 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001002
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001003 parent = dentry->d_parent;
1004 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001005 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001006 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001007 spin_unlock(&dentry->d_lock);
1008 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001009 remove_dir(dentry);
1010}
1011
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001012/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001013 * Call with cgroup_mutex held. Drops reference counts on modules, including
1014 * any duplicate ones that parse_cgroupfs_options took. If this function
1015 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001016 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001017static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001018 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019{
Paul Menagebd89aab2007-10-18 23:40:44 -07001020 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001021 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001022 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001023 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001024
Ben Blumaae8aab2010-03-10 15:22:07 -08001025 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001026 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001027
Paul Menageddbcc7e2007-10-18 23:39:30 -07001028 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001029 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001030 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001031 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001032
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001033 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001034 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001035 ret = -EBUSY;
1036 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001037 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001038
1039 /* pin the module */
1040 if (!try_module_get(ss->module)) {
1041 ret = -ENOENT;
1042 goto out_put;
1043 }
1044 pinned |= 1 << i;
1045 }
1046
1047 /* subsys could be missing if unloaded between parsing and here */
1048 if (added_mask != pinned) {
1049 ret = -ENOENT;
1050 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001051 }
1052
Tejun Heo31261212013-06-28 17:07:30 -07001053 ret = cgroup_populate_dir(cgrp, added_mask);
1054 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001055 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001056
1057 /*
1058 * Nothing can fail from this point on. Remove files for the
1059 * removed subsystems and rebind each subsystem.
1060 */
1061 cgroup_clear_dir(cgrp, removed_mask);
1062
Tejun Heo30159ec2013-06-25 11:53:37 -07001063 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001064 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001065
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001066 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001067 /* We're binding this subsystem to this hierarchy */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001068 BUG_ON(cgroup_css(cgrp, ss));
1069 BUG_ON(!cgroup_css(cgroup_dummy_top, ss));
1070 BUG_ON(cgroup_css(cgroup_dummy_top, ss)->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001071
Tejun Heo73e80ed2013-08-13 11:01:55 -04001072 rcu_assign_pointer(cgrp->subsys[i],
Tejun Heoca8bdca2013-08-26 18:40:56 -04001073 cgroup_css(cgroup_dummy_top, ss));
1074 cgroup_css(cgrp, ss)->cgroup = cgrp;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001075
Li Zefan33a68ac2009-01-07 18:07:42 -08001076 list_move(&ss->sibling, &root->subsys_list);
Lai Jiangshanb2aa30f2009-01-07 18:07:37 -08001077 ss->root = root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001078 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001079 ss->bind(cgroup_css(cgrp, ss));
Tejun Heoa8a648c2013-06-24 15:21:47 -07001080
Ben Blumcf5d5942010-03-10 15:22:09 -08001081 /* refcount was already taken, and we're keeping it */
Tejun Heoa8a648c2013-06-24 15:21:47 -07001082 root->subsys_mask |= bit;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001083 } else if (bit & removed_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001084 /* We're removing this subsystem */
Tejun Heoca8bdca2013-08-26 18:40:56 -04001085 BUG_ON(cgroup_css(cgrp, ss) != cgroup_css(cgroup_dummy_top, ss));
1086 BUG_ON(cgroup_css(cgrp, ss)->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001087
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 if (ss->bind)
Tejun Heoca8bdca2013-08-26 18:40:56 -04001089 ss->bind(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04001090
Tejun Heoca8bdca2013-08-26 18:40:56 -04001091 cgroup_css(cgroup_dummy_top, ss)->cgroup = cgroup_dummy_top;
Tejun Heo73e80ed2013-08-13 11:01:55 -04001092 RCU_INIT_POINTER(cgrp->subsys[i], NULL);
1093
Tejun Heo9871bf92013-06-24 15:21:47 -07001094 cgroup_subsys[i]->root = &cgroup_dummy_root;
1095 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001096
Ben Blumcf5d5942010-03-10 15:22:09 -08001097 /* subsystem is now free - drop reference on module */
1098 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001099 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100 }
1101 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001102
Tejun Heo1672d042013-06-25 18:04:54 -07001103 /*
1104 * Mark @root has finished binding subsystems. @root->subsys_mask
1105 * now matches the bound subsystems.
1106 */
1107 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1108
Paul Menageddbcc7e2007-10-18 23:39:30 -07001109 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001110
1111out_put:
1112 for_each_subsys(ss, i)
1113 if (pinned & (1 << i))
1114 module_put(ss->module);
1115 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001116}
1117
Al Viro34c80b12011-12-08 21:32:45 -05001118static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119{
Al Viro34c80b12011-12-08 21:32:45 -05001120 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001121 struct cgroup_subsys *ss;
1122
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001123 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001124 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001125 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001126 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1127 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001128 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001129 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001130 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001131 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001132 if (strlen(root->release_agent_path))
1133 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001134 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001135 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001136 if (strlen(root->name))
1137 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001138 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001139 return 0;
1140}
1141
1142struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001143 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001144 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001145 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001146 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001147 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001148 /* User explicitly requested empty subsystem */
1149 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001150
1151 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001152
Paul Menageddbcc7e2007-10-18 23:39:30 -07001153};
1154
Ben Blumaae8aab2010-03-10 15:22:07 -08001155/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001156 * Convert a hierarchy specifier into a bitmask of subsystems and
1157 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1158 * array. This function takes refcounts on subsystems to be used, unless it
1159 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001160 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001161static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001162{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001163 char *token, *o = data;
1164 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001165 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001166 struct cgroup_subsys *ss;
1167 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001168
Ben Blumaae8aab2010-03-10 15:22:07 -08001169 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1170
Li Zefanf9ab5b52009-06-17 16:26:33 -07001171#ifdef CONFIG_CPUSETS
1172 mask = ~(1UL << cpuset_subsys_id);
1173#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174
Paul Menagec6d57f32009-09-23 15:56:19 -07001175 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001176
1177 while ((token = strsep(&o, ",")) != NULL) {
1178 if (!*token)
1179 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001180 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001181 /* Explicitly have no subsystems */
1182 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001183 continue;
1184 }
1185 if (!strcmp(token, "all")) {
1186 /* Mutually exclusive option 'all' + subsystem name */
1187 if (one_ss)
1188 return -EINVAL;
1189 all_ss = true;
1190 continue;
1191 }
Tejun Heo873fe092013-04-14 20:15:26 -07001192 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1193 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1194 continue;
1195 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001197 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001198 continue;
1199 }
1200 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001201 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001202 continue;
1203 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001204 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001205 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001206 continue;
1207 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001208 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001209 /* Specifying two release agents is forbidden */
1210 if (opts->release_agent)
1211 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001212 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001213 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001214 if (!opts->release_agent)
1215 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001216 continue;
1217 }
1218 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001219 const char *name = token + 5;
1220 /* Can't specify an empty name */
1221 if (!strlen(name))
1222 return -EINVAL;
1223 /* Must match [\w.-]+ */
1224 for (i = 0; i < strlen(name); i++) {
1225 char c = name[i];
1226 if (isalnum(c))
1227 continue;
1228 if ((c == '.') || (c == '-') || (c == '_'))
1229 continue;
1230 return -EINVAL;
1231 }
1232 /* Specifying two names is forbidden */
1233 if (opts->name)
1234 return -EINVAL;
1235 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001236 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001237 GFP_KERNEL);
1238 if (!opts->name)
1239 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001240
1241 continue;
1242 }
1243
Tejun Heo30159ec2013-06-25 11:53:37 -07001244 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001245 if (strcmp(token, ss->name))
1246 continue;
1247 if (ss->disabled)
1248 continue;
1249
1250 /* Mutually exclusive option 'all' + subsystem name */
1251 if (all_ss)
1252 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001253 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001254 one_ss = true;
1255
1256 break;
1257 }
1258 if (i == CGROUP_SUBSYS_COUNT)
1259 return -ENOENT;
1260 }
1261
1262 /*
1263 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001264 * otherwise if 'none', 'name=' and a subsystem name options
1265 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001266 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001267 if (all_ss || (!one_ss && !opts->none && !opts->name))
1268 for_each_subsys(ss, i)
1269 if (!ss->disabled)
1270 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001271
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001272 /* Consistency checks */
1273
Tejun Heo873fe092013-04-14 20:15:26 -07001274 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1275 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1276
1277 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1278 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1279 return -EINVAL;
1280 }
1281
1282 if (opts->cpuset_clone_children) {
1283 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1284 return -EINVAL;
1285 }
1286 }
1287
Li Zefanf9ab5b52009-06-17 16:26:33 -07001288 /*
1289 * Option noprefix was introduced just for backward compatibility
1290 * with the old cpuset, so we allow noprefix only if mounting just
1291 * the cpuset subsystem.
1292 */
Tejun Heo93438622013-04-14 20:15:25 -07001293 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001294 return -EINVAL;
1295
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001296
1297 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001298 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001299 return -EINVAL;
1300
1301 /*
1302 * We either have to specify by name or by subsystems. (So all
1303 * empty hierarchies must have a name).
1304 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001305 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001306 return -EINVAL;
1307
1308 return 0;
1309}
1310
1311static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1312{
1313 int ret = 0;
1314 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001315 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001317 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001318
Tejun Heo873fe092013-04-14 20:15:26 -07001319 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1320 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1321 return -EINVAL;
1322 }
1323
Paul Menagebd89aab2007-10-18 23:40:44 -07001324 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001326 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001327
1328 /* See what subsystems are wanted */
1329 ret = parse_cgroupfs_options(data, &opts);
1330 if (ret)
1331 goto out_unlock;
1332
Tejun Heoa8a648c2013-06-24 15:21:47 -07001333 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001334 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1335 task_tgid_nr(current), current->comm);
1336
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001337 added_mask = opts.subsys_mask & ~root->subsys_mask;
1338 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001339
Ben Blumcf5d5942010-03-10 15:22:09 -08001340 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001341 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001342 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001343 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1344 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1345 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001346 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001347 goto out_unlock;
1348 }
1349
Tejun Heof172e672013-06-28 17:07:30 -07001350 /* remounting is not allowed for populated hierarchies */
1351 if (root->number_of_cgroups > 1) {
1352 ret = -EBUSY;
1353 goto out_unlock;
1354 }
1355
Tejun Heoa8a648c2013-06-24 15:21:47 -07001356 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001357 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001358 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001359
Paul Menage81a6a5c2007-10-18 23:39:38 -07001360 if (opts.release_agent)
1361 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001362 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001363 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001364 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001365 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001367 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001368 return ret;
1369}
1370
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001371static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001372 .statfs = simple_statfs,
1373 .drop_inode = generic_delete_inode,
1374 .show_options = cgroup_show_options,
1375 .remount_fs = cgroup_remount,
1376};
1377
Paul Menagecc31edc2008-10-18 20:28:04 -07001378static void init_cgroup_housekeeping(struct cgroup *cgrp)
1379{
1380 INIT_LIST_HEAD(&cgrp->sibling);
1381 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001382 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001383 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001384 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001385 INIT_LIST_HEAD(&cgrp->pidlists);
1386 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001387 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001388 INIT_LIST_HEAD(&cgrp->event_list);
1389 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001390 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001391}
Paul Menagec6d57f32009-09-23 15:56:19 -07001392
Paul Menageddbcc7e2007-10-18 23:39:30 -07001393static void init_cgroup_root(struct cgroupfs_root *root)
1394{
Paul Menagebd89aab2007-10-18 23:40:44 -07001395 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001396
Paul Menageddbcc7e2007-10-18 23:39:30 -07001397 INIT_LIST_HEAD(&root->subsys_list);
1398 INIT_LIST_HEAD(&root->root_list);
1399 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001400 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001401 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001402 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001403 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001404}
1405
Tejun Heofc76df72013-06-25 11:53:37 -07001406static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001407{
Tejun Heo1a574232013-04-14 11:36:58 -07001408 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001409
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001410 lockdep_assert_held(&cgroup_mutex);
1411 lockdep_assert_held(&cgroup_root_mutex);
1412
Tejun Heofc76df72013-06-25 11:53:37 -07001413 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1414 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001415 if (id < 0)
1416 return id;
1417
1418 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001419 return 0;
1420}
1421
1422static void cgroup_exit_root_id(struct cgroupfs_root *root)
1423{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001424 lockdep_assert_held(&cgroup_mutex);
1425 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001426
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001427 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001428 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001429 root->hierarchy_id = 0;
1430 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001431}
1432
Paul Menageddbcc7e2007-10-18 23:39:30 -07001433static int cgroup_test_super(struct super_block *sb, void *data)
1434{
Paul Menagec6d57f32009-09-23 15:56:19 -07001435 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001436 struct cgroupfs_root *root = sb->s_fs_info;
1437
Paul Menagec6d57f32009-09-23 15:56:19 -07001438 /* If we asked for a name then it must match */
1439 if (opts->name && strcmp(opts->name, root->name))
1440 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001441
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001442 /*
1443 * If we asked for subsystems (or explicitly for no
1444 * subsystems) then they must match
1445 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001446 if ((opts->subsys_mask || opts->none)
1447 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001448 return 0;
1449
1450 return 1;
1451}
1452
Paul Menagec6d57f32009-09-23 15:56:19 -07001453static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1454{
1455 struct cgroupfs_root *root;
1456
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001457 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001458 return NULL;
1459
1460 root = kzalloc(sizeof(*root), GFP_KERNEL);
1461 if (!root)
1462 return ERR_PTR(-ENOMEM);
1463
1464 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001465
Tejun Heo1672d042013-06-25 18:04:54 -07001466 /*
1467 * We need to set @root->subsys_mask now so that @root can be
1468 * matched by cgroup_test_super() before it finishes
1469 * initialization; otherwise, competing mounts with the same
1470 * options may try to bind the same subsystems instead of waiting
1471 * for the first one leading to unexpected mount errors.
1472 * SUBSYS_BOUND will be set once actual binding is complete.
1473 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001474 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001475 root->flags = opts->flags;
1476 if (opts->release_agent)
1477 strcpy(root->release_agent_path, opts->release_agent);
1478 if (opts->name)
1479 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001480 if (opts->cpuset_clone_children)
1481 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001482 return root;
1483}
1484
Tejun Heofa3ca072013-04-14 11:36:56 -07001485static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001486{
Tejun Heofa3ca072013-04-14 11:36:56 -07001487 if (root) {
1488 /* hierarhcy ID shoulid already have been released */
1489 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001490
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001491 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001492 kfree(root);
1493 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001494}
1495
Paul Menageddbcc7e2007-10-18 23:39:30 -07001496static int cgroup_set_super(struct super_block *sb, void *data)
1497{
1498 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001499 struct cgroup_sb_opts *opts = data;
1500
1501 /* If we don't have a new root, we can't set up a new sb */
1502 if (!opts->new_root)
1503 return -EINVAL;
1504
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001505 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001506
1507 ret = set_anon_super(sb, NULL);
1508 if (ret)
1509 return ret;
1510
Paul Menagec6d57f32009-09-23 15:56:19 -07001511 sb->s_fs_info = opts->new_root;
1512 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001513
1514 sb->s_blocksize = PAGE_CACHE_SIZE;
1515 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1516 sb->s_magic = CGROUP_SUPER_MAGIC;
1517 sb->s_op = &cgroup_ops;
1518
1519 return 0;
1520}
1521
1522static int cgroup_get_rootdir(struct super_block *sb)
1523{
Al Viro0df6a632010-12-21 13:29:29 -05001524 static const struct dentry_operations cgroup_dops = {
1525 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001526 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001527 };
1528
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529 struct inode *inode =
1530 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001531
1532 if (!inode)
1533 return -ENOMEM;
1534
Paul Menageddbcc7e2007-10-18 23:39:30 -07001535 inode->i_fop = &simple_dir_operations;
1536 inode->i_op = &cgroup_dir_inode_operations;
1537 /* directories start off with i_nlink == 2 (for "." entry) */
1538 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001539 sb->s_root = d_make_root(inode);
1540 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001541 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001542 /* for everything else we want ->d_op set */
1543 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001544 return 0;
1545}
1546
Al Virof7e83572010-07-26 13:23:11 +04001547static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001549 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001550{
1551 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001552 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001553 int ret = 0;
1554 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001555 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001556 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001557 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001558 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001559
1560 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001561 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001562 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001563 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001564 if (ret)
1565 goto out_err;
1566
1567 /*
1568 * Allocate a new cgroup root. We may not need it if we're
1569 * reusing an existing hierarchy.
1570 */
1571 new_root = cgroup_root_from_opts(&opts);
1572 if (IS_ERR(new_root)) {
1573 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001574 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001575 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001577
Paul Menagec6d57f32009-09-23 15:56:19 -07001578 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001579 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001580 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001581 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001582 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001583 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001584 }
1585
Paul Menagec6d57f32009-09-23 15:56:19 -07001586 root = sb->s_fs_info;
1587 BUG_ON(!root);
1588 if (root == opts.new_root) {
1589 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001590 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001591 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001592 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001593 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001594
1595 BUG_ON(sb->s_root != NULL);
1596
1597 ret = cgroup_get_rootdir(sb);
1598 if (ret)
1599 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001600 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601
Paul Menage817929e2007-10-18 23:39:36 -07001602 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001604 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001605
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001606 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1607 0, 1, GFP_KERNEL);
1608 if (root_cgrp->id < 0)
1609 goto unlock_drop;
1610
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001611 /* Check for name clashes with existing mounts */
1612 ret = -EBUSY;
1613 if (strlen(root->name))
1614 for_each_active_root(existing_root)
1615 if (!strcmp(existing_root->name, root->name))
1616 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001617
Paul Menage817929e2007-10-18 23:39:36 -07001618 /*
1619 * We're accessing css_set_count without locking
1620 * css_set_lock here, but that's OK - it can only be
1621 * increased by someone holding cgroup_lock, and
1622 * that's us. The worst that can happen is that we
1623 * have some link structures left over
1624 */
Tejun Heo69d02062013-06-12 21:04:50 -07001625 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001626 if (ret)
1627 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001628
Tejun Heofc76df72013-06-25 11:53:37 -07001629 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1630 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001631 if (ret)
1632 goto unlock_drop;
1633
Tejun Heo31261212013-06-28 17:07:30 -07001634 sb->s_root->d_fsdata = root_cgrp;
1635 root_cgrp->dentry = sb->s_root;
1636
1637 /*
1638 * We're inside get_sb() and will call lookup_one_len() to
1639 * create the root files, which doesn't work if SELinux is
1640 * in use. The following cred dancing somehow works around
1641 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1642 * populating new cgroupfs mount") for more details.
1643 */
1644 cred = override_creds(&init_cred);
1645
Tejun Heo2bb566c2013-08-08 20:11:23 -04001646 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001647 if (ret)
1648 goto rm_base_files;
1649
Tejun Heoa8a648c2013-06-24 15:21:47 -07001650 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001651 if (ret)
1652 goto rm_base_files;
1653
1654 revert_creds(cred);
1655
Ben Blumcf5d5942010-03-10 15:22:09 -08001656 /*
1657 * There must be no failure case after here, since rebinding
1658 * takes care of subsystems' refcounts, which are explicitly
1659 * dropped in the failure exit path.
1660 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001661
Tejun Heo9871bf92013-06-24 15:21:47 -07001662 list_add(&root->root_list, &cgroup_roots);
1663 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001664
Paul Menage817929e2007-10-18 23:39:36 -07001665 /* Link the top cgroup in this hierarchy into all
1666 * the css_set objects */
1667 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001668 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001669 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001670 write_unlock(&css_set_lock);
1671
Tejun Heo69d02062013-06-12 21:04:50 -07001672 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001673
Li Zefanc12f65d2009-01-07 18:07:42 -08001674 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001675 BUG_ON(root->number_of_cgroups != 1);
1676
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001677 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001678 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001679 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001680 } else {
1681 /*
1682 * We re-used an existing hierarchy - the new root (if
1683 * any) is not needed
1684 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001685 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001686
Tejun Heoc7ba8282013-06-29 14:06:10 -07001687 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001688 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1689 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1690 ret = -EINVAL;
1691 goto drop_new_super;
1692 } else {
1693 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1694 }
Tejun Heo873fe092013-04-14 20:15:26 -07001695 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001696 }
1697
Paul Menagec6d57f32009-09-23 15:56:19 -07001698 kfree(opts.release_agent);
1699 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001700 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001701
Tejun Heo31261212013-06-28 17:07:30 -07001702 rm_base_files:
1703 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001704 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001705 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001707 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001708 mutex_unlock(&cgroup_root_mutex);
1709 mutex_unlock(&cgroup_mutex);
1710 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001711 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001712 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001713 out_err:
1714 kfree(opts.release_agent);
1715 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001716 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001717}
1718
1719static void cgroup_kill_sb(struct super_block *sb) {
1720 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001721 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001722 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001723 int ret;
1724
1725 BUG_ON(!root);
1726
1727 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001728 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729
Tejun Heo31261212013-06-28 17:07:30 -07001730 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001732 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001733
1734 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001735 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1736 ret = rebind_subsystems(root, 0, root->subsys_mask);
1737 /* Shouldn't be able to fail ... */
1738 BUG_ON(ret);
1739 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001740
Paul Menage817929e2007-10-18 23:39:36 -07001741 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001742 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001743 * root cgroup
1744 */
1745 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001746
Tejun Heo69d02062013-06-12 21:04:50 -07001747 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1748 list_del(&link->cset_link);
1749 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001750 kfree(link);
1751 }
1752 write_unlock(&css_set_lock);
1753
Paul Menage839ec542009-01-29 14:25:22 -08001754 if (!list_empty(&root->root_list)) {
1755 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001756 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001757 }
Li Zefane5f6a862009-01-07 18:07:41 -08001758
Tejun Heofa3ca072013-04-14 11:36:56 -07001759 cgroup_exit_root_id(root);
1760
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001761 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001763 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001764
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001765 simple_xattrs_free(&cgrp->xattrs);
1766
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001768 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001769}
1770
1771static struct file_system_type cgroup_fs_type = {
1772 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001773 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001774 .kill_sb = cgroup_kill_sb,
1775};
1776
Greg KH676db4a2010-08-05 13:53:35 -07001777static struct kobject *cgroup_kobj;
1778
Li Zefana043e3b2008-02-23 15:24:09 -08001779/**
1780 * cgroup_path - generate the path of a cgroup
1781 * @cgrp: the cgroup in question
1782 * @buf: the buffer to write the path into
1783 * @buflen: the length of the buffer
1784 *
Li Zefan65dff752013-03-01 15:01:56 +08001785 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1786 *
1787 * We can't generate cgroup path using dentry->d_name, as accessing
1788 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1789 * inode's i_mutex, while on the other hand cgroup_path() can be called
1790 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001792int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793{
Li Zefan65dff752013-03-01 15:01:56 +08001794 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001795 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001796
Tejun Heoda1f2962013-04-14 10:32:19 -07001797 if (!cgrp->parent) {
1798 if (strlcpy(buf, "/", buflen) >= buflen)
1799 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001800 return 0;
1801 }
1802
Tao Ma316eb662012-11-08 21:36:38 +08001803 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001804 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001805
Li Zefan65dff752013-03-01 15:01:56 +08001806 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001807 do {
Li Zefan65dff752013-03-01 15:01:56 +08001808 const char *name = cgroup_name(cgrp);
1809 int len;
1810
1811 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001812 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001813 goto out;
1814 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001815
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001817 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001818 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001819
1820 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001821 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001822 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001823 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001824out:
1825 rcu_read_unlock();
1826 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827}
Ben Blum67523c42010-03-10 15:22:11 -08001828EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001829
Tejun Heo857a2be2013-04-14 20:50:08 -07001830/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001831 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001832 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001833 * @buf: the buffer to write the path into
1834 * @buflen: the length of the buffer
1835 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001836 * Determine @task's cgroup on the first (the one with the lowest non-zero
1837 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1838 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1839 * cgroup controller callbacks.
1840 *
1841 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001842 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001843int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001844{
1845 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001846 struct cgroup *cgrp;
1847 int hierarchy_id = 1, ret = 0;
1848
1849 if (buflen < 2)
1850 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001851
1852 mutex_lock(&cgroup_mutex);
1853
Tejun Heo913ffdb2013-07-11 16:34:48 -07001854 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1855
Tejun Heo857a2be2013-04-14 20:50:08 -07001856 if (root) {
1857 cgrp = task_cgroup_from_root(task, root);
1858 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001859 } else {
1860 /* if no hierarchy exists, everyone is in "/" */
1861 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001862 }
1863
1864 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001865 return ret;
1866}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001867EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001868
Ben Blum74a11662011-05-26 16:25:20 -07001869/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001870 * Control Group taskset
1871 */
Tejun Heo134d3372011-12-12 18:12:21 -08001872struct task_and_cgroup {
1873 struct task_struct *task;
1874 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001875 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001876};
1877
Tejun Heo2f7ee562011-12-12 18:12:21 -08001878struct cgroup_taskset {
1879 struct task_and_cgroup single;
1880 struct flex_array *tc_array;
1881 int tc_array_len;
1882 int idx;
1883 struct cgroup *cur_cgrp;
1884};
1885
1886/**
1887 * cgroup_taskset_first - reset taskset and return the first task
1888 * @tset: taskset of interest
1889 *
1890 * @tset iteration is initialized and the first task is returned.
1891 */
1892struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1893{
1894 if (tset->tc_array) {
1895 tset->idx = 0;
1896 return cgroup_taskset_next(tset);
1897 } else {
1898 tset->cur_cgrp = tset->single.cgrp;
1899 return tset->single.task;
1900 }
1901}
1902EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1903
1904/**
1905 * cgroup_taskset_next - iterate to the next task in taskset
1906 * @tset: taskset of interest
1907 *
1908 * Return the next task in @tset. Iteration must have been initialized
1909 * with cgroup_taskset_first().
1910 */
1911struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1912{
1913 struct task_and_cgroup *tc;
1914
1915 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1916 return NULL;
1917
1918 tc = flex_array_get(tset->tc_array, tset->idx++);
1919 tset->cur_cgrp = tc->cgrp;
1920 return tc->task;
1921}
1922EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1923
1924/**
Tejun Heod99c8722013-08-08 20:11:27 -04001925 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001926 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001927 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001928 *
Tejun Heod99c8722013-08-08 20:11:27 -04001929 * Return the css for the current (last returned) task of @tset for
1930 * subsystem specified by @subsys_id. This function must be preceded by
1931 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001932 */
Tejun Heod99c8722013-08-08 20:11:27 -04001933struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1934 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001935{
Tejun Heoca8bdca2013-08-26 18:40:56 -04001936 return cgroup_css(tset->cur_cgrp, cgroup_subsys[subsys_id]);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001937}
Tejun Heod99c8722013-08-08 20:11:27 -04001938EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001939
1940/**
1941 * cgroup_taskset_size - return the number of tasks in taskset
1942 * @tset: taskset of interest
1943 */
1944int cgroup_taskset_size(struct cgroup_taskset *tset)
1945{
1946 return tset->tc_array ? tset->tc_array_len : 1;
1947}
1948EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1949
1950
Ben Blum74a11662011-05-26 16:25:20 -07001951/*
1952 * cgroup_task_migrate - move a task from one cgroup to another.
1953 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001954 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001955 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001956static void cgroup_task_migrate(struct cgroup *old_cgrp,
1957 struct task_struct *tsk,
1958 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001959{
Tejun Heo5abb8852013-06-12 21:04:49 -07001960 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001961
1962 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001963 * We are synchronized through threadgroup_lock() against PF_EXITING
1964 * setting such that we can't race against cgroup_exit() changing the
1965 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001966 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001967 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001968 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001969
Ben Blum74a11662011-05-26 16:25:20 -07001970 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001971 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001972 task_unlock(tsk);
1973
1974 /* Update the css_set linked lists if we're using them */
1975 write_lock(&css_set_lock);
1976 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001977 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001978 write_unlock(&css_set_lock);
1979
1980 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001981 * We just gained a reference on old_cset by taking it from the
1982 * task. As trading it for new_cset is protected by cgroup_mutex,
1983 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001984 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001985 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1986 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001987}
1988
Li Zefana043e3b2008-02-23 15:24:09 -08001989/**
Li Zefan081aa452013-03-13 09:17:09 +08001990 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001991 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001992 * @tsk: the task or the leader of the threadgroup to be attached
1993 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001994 *
Tejun Heo257058a2011-12-12 18:12:21 -08001995 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001996 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001997 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001998static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1999 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07002000{
2001 int retval, i, group_size;
2002 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002003 struct cgroupfs_root *root = cgrp->root;
2004 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002005 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002006 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002007 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002008 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002009
2010 /*
2011 * step 0: in order to do expensive, possibly blocking operations for
2012 * every thread, we cannot iterate the thread group list, since it needs
2013 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002014 * group - group_rwsem prevents new threads from appearing, and if
2015 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002016 */
Li Zefan081aa452013-03-13 09:17:09 +08002017 if (threadgroup)
2018 group_size = get_nr_threads(tsk);
2019 else
2020 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002021 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002022 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002023 if (!group)
2024 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002025 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002026 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002027 if (retval)
2028 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002029
Ben Blum74a11662011-05-26 16:25:20 -07002030 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002031 /*
2032 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2033 * already PF_EXITING could be freed from underneath us unless we
2034 * take an rcu_read_lock.
2035 */
2036 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002037 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002038 struct task_and_cgroup ent;
2039
Tejun Heocd3d0952011-12-12 18:12:21 -08002040 /* @tsk either already exited or can't exit until the end */
2041 if (tsk->flags & PF_EXITING)
2042 continue;
2043
Ben Blum74a11662011-05-26 16:25:20 -07002044 /* as per above, nr_threads may decrease, but not increase. */
2045 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002046 ent.task = tsk;
2047 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002048 /* nothing to do if this task is already in the cgroup */
2049 if (ent.cgrp == cgrp)
2050 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002051 /*
2052 * saying GFP_ATOMIC has no effect here because we did prealloc
2053 * earlier, but it's good form to communicate our expectations.
2054 */
Tejun Heo134d3372011-12-12 18:12:21 -08002055 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002056 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002057 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002058
2059 if (!threadgroup)
2060 break;
Ben Blum74a11662011-05-26 16:25:20 -07002061 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002062 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002063 /* remember the number of threads in the array for later. */
2064 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002065 tset.tc_array = group;
2066 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002067
Tejun Heo134d3372011-12-12 18:12:21 -08002068 /* methods shouldn't be called if no task is actually migrating */
2069 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002070 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002071 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002072
Ben Blum74a11662011-05-26 16:25:20 -07002073 /*
2074 * step 1: check that we can legitimately attach to the cgroup.
2075 */
Tejun Heo5549c492013-06-24 15:21:48 -07002076 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002077 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002078
Ben Blum74a11662011-05-26 16:25:20 -07002079 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002080 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002081 if (retval) {
2082 failed_ss = ss;
2083 goto out_cancel_attach;
2084 }
2085 }
Ben Blum74a11662011-05-26 16:25:20 -07002086 }
2087
2088 /*
2089 * step 2: make sure css_sets exist for all threads to be migrated.
2090 * we use find_css_set, which allocates a new one if necessary.
2091 */
Ben Blum74a11662011-05-26 16:25:20 -07002092 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002093 struct css_set *old_cset;
2094
Tejun Heo134d3372011-12-12 18:12:21 -08002095 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002096 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002097 tc->cset = find_css_set(old_cset, cgrp);
2098 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002099 retval = -ENOMEM;
2100 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002101 }
2102 }
2103
2104 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002105 * step 3: now that we're guaranteed success wrt the css_sets,
2106 * proceed to move all tasks to the new cgroup. There are no
2107 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002108 */
Ben Blum74a11662011-05-26 16:25:20 -07002109 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002110 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002111 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002112 }
2113 /* nothing is sensitive to fork() after this point. */
2114
2115 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002116 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002117 */
Tejun Heo5549c492013-06-24 15:21:48 -07002118 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002119 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002120
Ben Blum74a11662011-05-26 16:25:20 -07002121 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002122 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002123 }
2124
2125 /*
2126 * step 5: success! and cleanup
2127 */
Ben Blum74a11662011-05-26 16:25:20 -07002128 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002129out_put_css_set_refs:
2130 if (retval) {
2131 for (i = 0; i < group_size; i++) {
2132 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002133 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002134 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002135 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002136 }
Ben Blum74a11662011-05-26 16:25:20 -07002137 }
2138out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002139 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002140 for_each_root_subsys(root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04002141 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoeb954192013-08-08 20:11:23 -04002142
Tejun Heo494c1672011-12-12 18:12:22 -08002143 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002144 break;
Ben Blum74a11662011-05-26 16:25:20 -07002145 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002146 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002147 }
2148 }
Ben Blum74a11662011-05-26 16:25:20 -07002149out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002150 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002151 return retval;
2152}
2153
2154/*
2155 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002156 * function to attach either it or all tasks in its threadgroup. Will lock
2157 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002158 */
2159static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002160{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002162 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002163 int ret;
2164
Ben Blum74a11662011-05-26 16:25:20 -07002165 if (!cgroup_lock_live_group(cgrp))
2166 return -ENODEV;
2167
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002168retry_find_task:
2169 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002170 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002171 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002172 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002173 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002174 ret= -ESRCH;
2175 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002176 }
Ben Blum74a11662011-05-26 16:25:20 -07002177 /*
2178 * even if we're attaching all tasks in the thread group, we
2179 * only need to check permissions on one of them.
2180 */
David Howellsc69e8d92008-11-14 10:39:19 +11002181 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002182 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2183 !uid_eq(cred->euid, tcred->uid) &&
2184 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002185 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002186 ret = -EACCES;
2187 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002188 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002189 } else
2190 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002191
2192 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002193 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002194
2195 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002196 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002197 * trapped in a cpuset, or RT worker may be born in a cgroup
2198 * with no rt_runtime allocated. Just say no.
2199 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002200 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002201 ret = -EINVAL;
2202 rcu_read_unlock();
2203 goto out_unlock_cgroup;
2204 }
2205
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002206 get_task_struct(tsk);
2207 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002208
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002209 threadgroup_lock(tsk);
2210 if (threadgroup) {
2211 if (!thread_group_leader(tsk)) {
2212 /*
2213 * a race with de_thread from another thread's exec()
2214 * may strip us of our leadership, if this happens,
2215 * there is no choice but to throw this task away and
2216 * try again; this is
2217 * "double-double-toil-and-trouble-check locking".
2218 */
2219 threadgroup_unlock(tsk);
2220 put_task_struct(tsk);
2221 goto retry_find_task;
2222 }
Li Zefan081aa452013-03-13 09:17:09 +08002223 }
2224
2225 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2226
Tejun Heocd3d0952011-12-12 18:12:21 -08002227 threadgroup_unlock(tsk);
2228
Paul Menagebbcb81d2007-10-18 23:39:32 -07002229 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002230out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002231 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002232 return ret;
2233}
2234
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002235/**
2236 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2237 * @from: attach to all cgroups of a given task
2238 * @tsk: the task to be attached
2239 */
2240int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2241{
2242 struct cgroupfs_root *root;
2243 int retval = 0;
2244
Tejun Heo47cfcd02013-04-07 09:29:51 -07002245 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002246 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002247 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002248
Li Zefan6f4b7e62013-07-31 16:18:36 +08002249 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002250 if (retval)
2251 break;
2252 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002253 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002254
2255 return retval;
2256}
2257EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2258
Tejun Heo182446d2013-08-08 20:11:24 -04002259static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2260 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002261{
Tejun Heo182446d2013-08-08 20:11:24 -04002262 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002263}
2264
Tejun Heo182446d2013-08-08 20:11:24 -04002265static int cgroup_procs_write(struct cgroup_subsys_state *css,
2266 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002267{
Tejun Heo182446d2013-08-08 20:11:24 -04002268 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002269}
2270
Tejun Heo182446d2013-08-08 20:11:24 -04002271static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2272 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002273{
Tejun Heo182446d2013-08-08 20:11:24 -04002274 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002275 if (strlen(buffer) >= PATH_MAX)
2276 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002277 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002278 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002279 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002280 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002281 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002282 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002283 return 0;
2284}
2285
Tejun Heo182446d2013-08-08 20:11:24 -04002286static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2287 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002288{
Tejun Heo182446d2013-08-08 20:11:24 -04002289 struct cgroup *cgrp = css->cgroup;
2290
Paul Menagee788e062008-07-25 01:46:59 -07002291 if (!cgroup_lock_live_group(cgrp))
2292 return -ENODEV;
2293 seq_puts(seq, cgrp->root->release_agent_path);
2294 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002295 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002296 return 0;
2297}
2298
Tejun Heo182446d2013-08-08 20:11:24 -04002299static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2300 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002301{
Tejun Heo182446d2013-08-08 20:11:24 -04002302 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002303 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002304}
2305
Paul Menage84eea842008-07-25 01:47:00 -07002306/* A buffer size big enough for numbers or short strings */
2307#define CGROUP_LOCAL_BUFFER_SIZE 64
2308
Tejun Heo182446d2013-08-08 20:11:24 -04002309static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2310 struct cftype *cft, struct file *file,
2311 const char __user *userbuf, size_t nbytes,
2312 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002313{
Paul Menage84eea842008-07-25 01:47:00 -07002314 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002315 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002316 char *end;
2317
2318 if (!nbytes)
2319 return -EINVAL;
2320 if (nbytes >= sizeof(buffer))
2321 return -E2BIG;
2322 if (copy_from_user(buffer, userbuf, nbytes))
2323 return -EFAULT;
2324
2325 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002326 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002327 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002328 if (*end)
2329 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002330 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002331 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002332 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002333 if (*end)
2334 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002335 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002336 }
Paul Menage355e0c42007-10-18 23:39:33 -07002337 if (!retval)
2338 retval = nbytes;
2339 return retval;
2340}
2341
Tejun Heo182446d2013-08-08 20:11:24 -04002342static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2343 struct cftype *cft, struct file *file,
2344 const char __user *userbuf, size_t nbytes,
2345 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002346{
Paul Menage84eea842008-07-25 01:47:00 -07002347 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002348 int retval = 0;
2349 size_t max_bytes = cft->max_write_len;
2350 char *buffer = local_buffer;
2351
2352 if (!max_bytes)
2353 max_bytes = sizeof(local_buffer) - 1;
2354 if (nbytes >= max_bytes)
2355 return -E2BIG;
2356 /* Allocate a dynamic buffer if we need one */
2357 if (nbytes >= sizeof(local_buffer)) {
2358 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2359 if (buffer == NULL)
2360 return -ENOMEM;
2361 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002362 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2363 retval = -EFAULT;
2364 goto out;
2365 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002366
2367 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002368 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002369 if (!retval)
2370 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002371out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002372 if (buffer != local_buffer)
2373 kfree(buffer);
2374 return retval;
2375}
2376
Paul Menageddbcc7e2007-10-18 23:39:30 -07002377static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002378 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002379{
Tejun Heo182446d2013-08-08 20:11:24 -04002380 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002381 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002382 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002383
Paul Menage355e0c42007-10-18 23:39:33 -07002384 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002385 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002386 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002387 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002388 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002389 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002390 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002391 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002392 return ret ? ret : nbytes;
2393 }
Paul Menage355e0c42007-10-18 23:39:33 -07002394 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002395}
2396
Tejun Heo182446d2013-08-08 20:11:24 -04002397static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2398 struct cftype *cft, struct file *file,
2399 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002400{
Paul Menage84eea842008-07-25 01:47:00 -07002401 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002402 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002403 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2404
2405 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2406}
2407
Tejun Heo182446d2013-08-08 20:11:24 -04002408static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2409 struct cftype *cft, struct file *file,
2410 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002411{
Paul Menage84eea842008-07-25 01:47:00 -07002412 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002413 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002414 int len = sprintf(tmp, "%lld\n", (long long) val);
2415
2416 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2417}
2418
Paul Menageddbcc7e2007-10-18 23:39:30 -07002419static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002420 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002421{
Tejun Heo182446d2013-08-08 20:11:24 -04002422 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002423 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002424 struct cgroup_subsys_state *css = cfe->css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002425
Paul Menageddbcc7e2007-10-18 23:39:30 -07002426 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002427 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002428 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002429 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002430 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002431 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432 return -EINVAL;
2433}
2434
Paul Menage91796562008-04-29 01:00:01 -07002435/*
2436 * seqfile ops/methods for returning structured data. Currently just
2437 * supports string->u64 maps, but can be extended in future.
2438 */
2439
Paul Menage91796562008-04-29 01:00:01 -07002440static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2441{
2442 struct seq_file *sf = cb->state;
2443 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2444}
2445
2446static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2447{
Li Zefane0798ce2013-07-31 17:36:25 +08002448 struct cfent *cfe = m->private;
2449 struct cftype *cft = cfe->type;
Tejun Heo105347b2013-08-13 11:01:55 -04002450 struct cgroup_subsys_state *css = cfe->css;
Li Zefane0798ce2013-07-31 17:36:25 +08002451
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002452 if (cft->read_map) {
2453 struct cgroup_map_cb cb = {
2454 .fill = cgroup_map_add,
2455 .state = m,
2456 };
Tejun Heo182446d2013-08-08 20:11:24 -04002457 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002458 }
Tejun Heo182446d2013-08-08 20:11:24 -04002459 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002460}
2461
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002462static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002463 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002464 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002465 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002466 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002467};
2468
Paul Menageddbcc7e2007-10-18 23:39:30 -07002469static int cgroup_file_open(struct inode *inode, struct file *file)
2470{
Tejun Heof7d58812013-08-08 20:11:23 -04002471 struct cfent *cfe = __d_cfe(file->f_dentry);
2472 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002473 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2474 struct cgroup_subsys_state *css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002475 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002476
2477 err = generic_file_open(inode, file);
2478 if (err)
2479 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002480
2481 /*
2482 * If the file belongs to a subsystem, pin the css. Will be
2483 * unpinned either on open failure or release. This ensures that
2484 * @css stays alive for all file operations.
2485 */
Tejun Heo105347b2013-08-13 11:01:55 -04002486 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002487 css = cgroup_css(cgrp, cft->ss);
2488 if (cft->ss && !css_tryget(css))
2489 css = NULL;
Tejun Heo105347b2013-08-13 11:01:55 -04002490 rcu_read_unlock();
2491
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002492 if (!css)
Tejun Heof7d58812013-08-08 20:11:23 -04002493 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002494
Tejun Heo0bfb4aa2013-08-15 11:42:36 -04002495 /*
2496 * @cfe->css is used by read/write/close to determine the
2497 * associated css. @file->private_data would be a better place but
2498 * that's already used by seqfile. Multiple accessors may use it
2499 * simultaneously which is okay as the association never changes.
2500 */
2501 WARN_ON_ONCE(cfe->css && cfe->css != css);
2502 cfe->css = css;
2503
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002504 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002505 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002506 err = single_open(file, cgroup_seqfile_show, cfe);
2507 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002508 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002509 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002510
Tejun Heo67f4c362013-08-08 20:11:24 -04002511 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002512 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002513 return err;
2514}
2515
2516static int cgroup_file_release(struct inode *inode, struct file *file)
2517{
Tejun Heof7d58812013-08-08 20:11:23 -04002518 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002519 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo105347b2013-08-13 11:01:55 -04002520 struct cgroup_subsys_state *css = cfe->css;
Tejun Heof7d58812013-08-08 20:11:23 -04002521 int ret = 0;
2522
Paul Menageddbcc7e2007-10-18 23:39:30 -07002523 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002524 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002525 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002526 css_put(css);
2527 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002528}
2529
2530/*
2531 * cgroup_rename - Only allow simple rename of directories in place.
2532 */
2533static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2534 struct inode *new_dir, struct dentry *new_dentry)
2535{
Li Zefan65dff752013-03-01 15:01:56 +08002536 int ret;
2537 struct cgroup_name *name, *old_name;
2538 struct cgroup *cgrp;
2539
2540 /*
2541 * It's convinient to use parent dir's i_mutex to protected
2542 * cgrp->name.
2543 */
2544 lockdep_assert_held(&old_dir->i_mutex);
2545
Paul Menageddbcc7e2007-10-18 23:39:30 -07002546 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2547 return -ENOTDIR;
2548 if (new_dentry->d_inode)
2549 return -EEXIST;
2550 if (old_dir != new_dir)
2551 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002552
2553 cgrp = __d_cgrp(old_dentry);
2554
Tejun Heo6db8e852013-06-14 11:18:22 -07002555 /*
2556 * This isn't a proper migration and its usefulness is very
2557 * limited. Disallow if sane_behavior.
2558 */
2559 if (cgroup_sane_behavior(cgrp))
2560 return -EPERM;
2561
Li Zefan65dff752013-03-01 15:01:56 +08002562 name = cgroup_alloc_name(new_dentry);
2563 if (!name)
2564 return -ENOMEM;
2565
2566 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2567 if (ret) {
2568 kfree(name);
2569 return ret;
2570 }
2571
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002572 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002573 rcu_assign_pointer(cgrp->name, name);
2574
2575 kfree_rcu(old_name, rcu_head);
2576 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002577}
2578
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002579static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2580{
2581 if (S_ISDIR(dentry->d_inode->i_mode))
2582 return &__d_cgrp(dentry)->xattrs;
2583 else
Li Zefan712317a2013-04-18 23:09:52 -07002584 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002585}
2586
2587static inline int xattr_enabled(struct dentry *dentry)
2588{
2589 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002590 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002591}
2592
2593static bool is_valid_xattr(const char *name)
2594{
2595 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2596 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2597 return true;
2598 return false;
2599}
2600
2601static int cgroup_setxattr(struct dentry *dentry, const char *name,
2602 const void *val, size_t size, int flags)
2603{
2604 if (!xattr_enabled(dentry))
2605 return -EOPNOTSUPP;
2606 if (!is_valid_xattr(name))
2607 return -EINVAL;
2608 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2609}
2610
2611static int cgroup_removexattr(struct dentry *dentry, const char *name)
2612{
2613 if (!xattr_enabled(dentry))
2614 return -EOPNOTSUPP;
2615 if (!is_valid_xattr(name))
2616 return -EINVAL;
2617 return simple_xattr_remove(__d_xattrs(dentry), name);
2618}
2619
2620static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2621 void *buf, size_t size)
2622{
2623 if (!xattr_enabled(dentry))
2624 return -EOPNOTSUPP;
2625 if (!is_valid_xattr(name))
2626 return -EINVAL;
2627 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2628}
2629
2630static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2631{
2632 if (!xattr_enabled(dentry))
2633 return -EOPNOTSUPP;
2634 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2635}
2636
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002637static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002638 .read = cgroup_file_read,
2639 .write = cgroup_file_write,
2640 .llseek = generic_file_llseek,
2641 .open = cgroup_file_open,
2642 .release = cgroup_file_release,
2643};
2644
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002645static const struct inode_operations cgroup_file_inode_operations = {
2646 .setxattr = cgroup_setxattr,
2647 .getxattr = cgroup_getxattr,
2648 .listxattr = cgroup_listxattr,
2649 .removexattr = cgroup_removexattr,
2650};
2651
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002652static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002653 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654 .mkdir = cgroup_mkdir,
2655 .rmdir = cgroup_rmdir,
2656 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002657 .setxattr = cgroup_setxattr,
2658 .getxattr = cgroup_getxattr,
2659 .listxattr = cgroup_listxattr,
2660 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002661};
2662
Al Viro00cd8dd2012-06-10 17:13:09 -04002663static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002664{
2665 if (dentry->d_name.len > NAME_MAX)
2666 return ERR_PTR(-ENAMETOOLONG);
2667 d_add(dentry, NULL);
2668 return NULL;
2669}
2670
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002671/*
2672 * Check if a file is a control file
2673 */
2674static inline struct cftype *__file_cft(struct file *file)
2675{
Al Viro496ad9a2013-01-23 17:07:38 -05002676 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002677 return ERR_PTR(-EINVAL);
2678 return __d_cft(file->f_dentry);
2679}
2680
Al Viroa5e7ed32011-07-26 01:55:55 -04002681static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002682 struct super_block *sb)
2683{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002684 struct inode *inode;
2685
2686 if (!dentry)
2687 return -ENOENT;
2688 if (dentry->d_inode)
2689 return -EEXIST;
2690
2691 inode = cgroup_new_inode(mode, sb);
2692 if (!inode)
2693 return -ENOMEM;
2694
2695 if (S_ISDIR(mode)) {
2696 inode->i_op = &cgroup_dir_inode_operations;
2697 inode->i_fop = &simple_dir_operations;
2698
2699 /* start off with i_nlink == 2 (for "." entry) */
2700 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002701 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002702
Tejun Heob8a2df62012-11-19 08:13:37 -08002703 /*
2704 * Control reaches here with cgroup_mutex held.
2705 * @inode->i_mutex should nest outside cgroup_mutex but we
2706 * want to populate it immediately without releasing
2707 * cgroup_mutex. As @inode isn't visible to anyone else
2708 * yet, trylock will always succeed without affecting
2709 * lockdep checks.
2710 */
2711 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002712 } else if (S_ISREG(mode)) {
2713 inode->i_size = 0;
2714 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002715 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002716 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002717 d_instantiate(dentry, inode);
2718 dget(dentry); /* Extra count - pin the dentry in core */
2719 return 0;
2720}
2721
Li Zefan099fca32009-04-02 16:57:29 -07002722/**
2723 * cgroup_file_mode - deduce file mode of a control file
2724 * @cft: the control file in question
2725 *
2726 * returns cft->mode if ->mode is not 0
2727 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2728 * returns S_IRUGO if it has only a read handler
2729 * returns S_IWUSR if it has only a write hander
2730 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002731static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002732{
Al Viroa5e7ed32011-07-26 01:55:55 -04002733 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002734
2735 if (cft->mode)
2736 return cft->mode;
2737
2738 if (cft->read || cft->read_u64 || cft->read_s64 ||
2739 cft->read_map || cft->read_seq_string)
2740 mode |= S_IRUGO;
2741
2742 if (cft->write || cft->write_u64 || cft->write_s64 ||
2743 cft->write_string || cft->trigger)
2744 mode |= S_IWUSR;
2745
2746 return mode;
2747}
2748
Tejun Heo2bb566c2013-08-08 20:11:23 -04002749static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750{
Paul Menagebd89aab2007-10-18 23:40:44 -07002751 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002752 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002753 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002754 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002755 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002756 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002757 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002758
Tejun Heo9fa4db32013-08-26 18:40:56 -04002759 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2760 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002761 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002762 strcat(name, ".");
2763 }
2764 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002765
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767
2768 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2769 if (!cfe)
2770 return -ENOMEM;
2771
Paul Menageddbcc7e2007-10-18 23:39:30 -07002772 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002773 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002774 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775 goto out;
2776 }
2777
Li Zefand6cbf352013-05-14 19:44:20 +08002778 cfe->type = (void *)cft;
2779 cfe->dentry = dentry;
2780 dentry->d_fsdata = cfe;
2781 simple_xattrs_init(&cfe->xattrs);
2782
Tejun Heo05ef1d72012-04-01 12:09:56 -07002783 mode = cgroup_file_mode(cft);
2784 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2785 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002786 list_add_tail(&cfe->node, &parent->files);
2787 cfe = NULL;
2788 }
2789 dput(dentry);
2790out:
2791 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002792 return error;
2793}
2794
Tejun Heob1f28d32013-06-28 16:24:10 -07002795/**
2796 * cgroup_addrm_files - add or remove files to a cgroup directory
2797 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002798 * @cfts: array of cftypes to be added
2799 * @is_add: whether to add or remove
2800 *
2801 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002802 * For removals, this function never fails. If addition fails, this
2803 * function doesn't remove files already added. The caller is responsible
2804 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002805 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002806static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2807 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002808{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002809 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002810 int ret;
2811
2812 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2813 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002814
2815 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002816 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002817 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2818 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002819 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2820 continue;
2821 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2822 continue;
2823
Li Zefan2739d3c2013-01-21 18:18:33 +08002824 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002825 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002826 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002827 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002828 cft->name, ret);
2829 return ret;
2830 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002831 } else {
2832 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002833 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002834 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002835 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002836}
2837
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002839 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002840{
2841 /*
2842 * Thanks to the entanglement with vfs inode locking, we can't walk
2843 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002844 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2845 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002846 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002847 mutex_lock(&cgroup_mutex);
2848}
2849
Tejun Heo2bb566c2013-08-08 20:11:23 -04002850static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002851 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002852{
2853 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002854 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002855 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002856 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002857 struct dentry *prev = NULL;
2858 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002859 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002860 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002861 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002862
2863 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002864 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002865 !atomic_inc_not_zero(&sb->s_active)) {
2866 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002867 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002868 }
2869
Li Zefane8c82d22013-06-18 18:48:37 +08002870 /*
2871 * All cgroups which are created after we drop cgroup_mutex will
2872 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002873 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002874 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002875 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002876
Tejun Heo8e3f6542012-04-01 12:09:55 -07002877 mutex_unlock(&cgroup_mutex);
2878
Li Zefane8c82d22013-06-18 18:48:37 +08002879 /* add/rm files for all cgroups created before */
2880 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002881 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002882 struct cgroup *cgrp = css->cgroup;
2883
Li Zefane8c82d22013-06-18 18:48:37 +08002884 if (cgroup_is_dead(cgrp))
2885 continue;
2886
2887 inode = cgrp->dentry->d_inode;
2888 dget(cgrp->dentry);
2889 rcu_read_unlock();
2890
2891 dput(prev);
2892 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002893
2894 mutex_lock(&inode->i_mutex);
2895 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002896 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002897 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002898 mutex_unlock(&cgroup_mutex);
2899 mutex_unlock(&inode->i_mutex);
2900
Li Zefane8c82d22013-06-18 18:48:37 +08002901 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002902 if (ret)
2903 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002904 }
Li Zefane8c82d22013-06-18 18:48:37 +08002905 rcu_read_unlock();
2906 dput(prev);
2907 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002908 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002909}
2910
2911/**
2912 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2913 * @ss: target cgroup subsystem
2914 * @cfts: zero-length name terminated array of cftypes
2915 *
2916 * Register @cfts to @ss. Files described by @cfts are created for all
2917 * existing cgroups to which @ss is attached and all future cgroups will
2918 * have them too. This function can be called anytime whether @ss is
2919 * attached or not.
2920 *
2921 * Returns 0 on successful registration, -errno on failure. Note that this
2922 * function currently returns 0 as long as @cfts registration is successful
2923 * even if some file creation attempts on existing cgroups fail.
2924 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002925int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002926{
2927 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002928 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002929 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930
2931 set = kzalloc(sizeof(*set), GFP_KERNEL);
2932 if (!set)
2933 return -ENOMEM;
2934
Tejun Heo2bb566c2013-08-08 20:11:23 -04002935 for (cft = cfts; cft->name[0] != '\0'; cft++)
2936 cft->ss = ss;
2937
Tejun Heo8e3f6542012-04-01 12:09:55 -07002938 cgroup_cfts_prepare();
2939 set->cfts = cfts;
2940 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002941 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002942 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002943 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002944 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002945}
2946EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2947
Li Zefana043e3b2008-02-23 15:24:09 -08002948/**
Tejun Heo79578622012-04-01 12:09:56 -07002949 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002950 * @cfts: zero-length name terminated array of cftypes
2951 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002952 * Unregister @cfts. Files described by @cfts are removed from all
2953 * existing cgroups and all future cgroups won't have them either. This
2954 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002955 *
2956 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002957 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002958 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002959int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002960{
2961 struct cftype_set *set;
2962
Tejun Heo2bb566c2013-08-08 20:11:23 -04002963 if (!cfts || !cfts[0].ss)
2964 return -ENOENT;
2965
Tejun Heo79578622012-04-01 12:09:56 -07002966 cgroup_cfts_prepare();
2967
Tejun Heo2bb566c2013-08-08 20:11:23 -04002968 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002969 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002970 list_del(&set->node);
2971 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002972 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002973 return 0;
2974 }
2975 }
2976
Tejun Heo2bb566c2013-08-08 20:11:23 -04002977 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002978 return -ENOENT;
2979}
2980
2981/**
Li Zefana043e3b2008-02-23 15:24:09 -08002982 * cgroup_task_count - count the number of tasks in a cgroup.
2983 * @cgrp: the cgroup in question
2984 *
2985 * Return the number of tasks in the cgroup.
2986 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002987int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002988{
2989 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002990 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002991
Paul Menage817929e2007-10-18 23:39:36 -07002992 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002993 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2994 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002995 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002996 return count;
2997}
2998
2999/*
Tejun Heo0942eee2013-08-08 20:11:26 -04003000 * To reduce the fork() overhead for systems that are not actually using
3001 * their cgroups capability, we don't maintain the lists running through
3002 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04003003 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003004 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003005static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003006{
3007 struct task_struct *p, *g;
3008 write_lock(&css_set_lock);
3009 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003010 /*
3011 * We need tasklist_lock because RCU is not safe against
3012 * while_each_thread(). Besides, a forking task that has passed
3013 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3014 * is not guaranteed to have its child immediately visible in the
3015 * tasklist if we walk through it with RCU.
3016 */
3017 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003018 do_each_thread(g, p) {
3019 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003020 /*
3021 * We should check if the process is exiting, otherwise
3022 * it will race with cgroup_exit() in that the list
3023 * entry won't be deleted though the process has exited.
3024 */
3025 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003026 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003027 task_unlock(p);
3028 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003029 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003030 write_unlock(&css_set_lock);
3031}
3032
Tejun Heo574bd9f2012-11-09 09:12:29 -08003033/**
Tejun Heo492eb212013-08-08 20:11:25 -04003034 * css_next_child - find the next child of a given css
3035 * @pos_css: the current position (%NULL to initiate traversal)
3036 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003037 *
Tejun Heo492eb212013-08-08 20:11:25 -04003038 * This function returns the next child of @parent_css and should be called
3039 * under RCU read lock. The only requirement is that @parent_css and
3040 * @pos_css are accessible. The next sibling is guaranteed to be returned
3041 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003042 */
Tejun Heo492eb212013-08-08 20:11:25 -04003043struct cgroup_subsys_state *
3044css_next_child(struct cgroup_subsys_state *pos_css,
3045 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003046{
Tejun Heo492eb212013-08-08 20:11:25 -04003047 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3048 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003049 struct cgroup *next;
3050
3051 WARN_ON_ONCE(!rcu_read_lock_held());
3052
3053 /*
3054 * @pos could already have been removed. Once a cgroup is removed,
3055 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003056 * changes. As CGRP_DEAD assertion is serialized and happens
3057 * before the cgroup is taken off the ->sibling list, if we see it
3058 * unasserted, it's guaranteed that the next sibling hasn't
3059 * finished its grace period even if it's already removed, and thus
3060 * safe to dereference from this RCU critical section. If
3061 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3062 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003063 *
3064 * If @pos is dead, its next pointer can't be dereferenced;
3065 * however, as each cgroup is given a monotonically increasing
3066 * unique serial number and always appended to the sibling list,
3067 * the next one can be found by walking the parent's children until
3068 * we see a cgroup with higher serial number than @pos's. While
3069 * this path can be slower, it's taken only when either the current
3070 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003071 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003072 if (!pos) {
3073 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3074 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003075 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003076 } else {
3077 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3078 if (next->serial_nr > pos->serial_nr)
3079 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003080 }
3081
Tejun Heo492eb212013-08-08 20:11:25 -04003082 if (&next->sibling == &cgrp->children)
3083 return NULL;
3084
Tejun Heoca8bdca2013-08-26 18:40:56 -04003085 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003086}
Tejun Heo492eb212013-08-08 20:11:25 -04003087EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003088
3089/**
Tejun Heo492eb212013-08-08 20:11:25 -04003090 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003091 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003092 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003093 *
Tejun Heo492eb212013-08-08 20:11:25 -04003094 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003095 * to visit for pre-order traversal of @root's descendants. @root is
3096 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003097 *
3098 * While this function requires RCU read locking, it doesn't require the
3099 * whole traversal to be contained in a single RCU critical section. This
3100 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003101 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003102 */
Tejun Heo492eb212013-08-08 20:11:25 -04003103struct cgroup_subsys_state *
3104css_next_descendant_pre(struct cgroup_subsys_state *pos,
3105 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003106{
Tejun Heo492eb212013-08-08 20:11:25 -04003107 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108
3109 WARN_ON_ONCE(!rcu_read_lock_held());
3110
Tejun Heobd8815a2013-08-08 20:11:27 -04003111 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003112 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003113 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003114
3115 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003116 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003117 if (next)
3118 return next;
3119
3120 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003121 while (pos != root) {
3122 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003123 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003124 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003125 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003126 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003127
3128 return NULL;
3129}
Tejun Heo492eb212013-08-08 20:11:25 -04003130EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003131
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003132/**
Tejun Heo492eb212013-08-08 20:11:25 -04003133 * css_rightmost_descendant - return the rightmost descendant of a css
3134 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003135 *
Tejun Heo492eb212013-08-08 20:11:25 -04003136 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3137 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003138 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003139 *
3140 * While this function requires RCU read locking, it doesn't require the
3141 * whole traversal to be contained in a single RCU critical section. This
3142 * function will return the correct rightmost descendant as long as @pos is
3143 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003144 */
Tejun Heo492eb212013-08-08 20:11:25 -04003145struct cgroup_subsys_state *
3146css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003147{
Tejun Heo492eb212013-08-08 20:11:25 -04003148 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003149
3150 WARN_ON_ONCE(!rcu_read_lock_held());
3151
3152 do {
3153 last = pos;
3154 /* ->prev isn't RCU safe, walk ->next till the end */
3155 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003156 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003157 pos = tmp;
3158 } while (pos);
3159
3160 return last;
3161}
Tejun Heo492eb212013-08-08 20:11:25 -04003162EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003163
Tejun Heo492eb212013-08-08 20:11:25 -04003164static struct cgroup_subsys_state *
3165css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003166{
Tejun Heo492eb212013-08-08 20:11:25 -04003167 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003168
3169 do {
3170 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003171 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172 } while (pos);
3173
3174 return last;
3175}
3176
3177/**
Tejun Heo492eb212013-08-08 20:11:25 -04003178 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003179 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003180 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003181 *
Tejun Heo492eb212013-08-08 20:11:25 -04003182 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003183 * to visit for post-order traversal of @root's descendants. @root is
3184 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003185 *
3186 * While this function requires RCU read locking, it doesn't require the
3187 * whole traversal to be contained in a single RCU critical section. This
3188 * function will return the correct next descendant as long as both @pos
3189 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003190 */
Tejun Heo492eb212013-08-08 20:11:25 -04003191struct cgroup_subsys_state *
3192css_next_descendant_post(struct cgroup_subsys_state *pos,
3193 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003194{
Tejun Heo492eb212013-08-08 20:11:25 -04003195 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003196
3197 WARN_ON_ONCE(!rcu_read_lock_held());
3198
3199 /* if first iteration, visit the leftmost descendant */
3200 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003201 next = css_leftmost_descendant(root);
3202 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003203 }
3204
Tejun Heobd8815a2013-08-08 20:11:27 -04003205 /* if we visited @root, we're done */
3206 if (pos == root)
3207 return NULL;
3208
Tejun Heo574bd9f2012-11-09 09:12:29 -08003209 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003210 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003211 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003212 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003213
3214 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003215 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003216}
Tejun Heo492eb212013-08-08 20:11:25 -04003217EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003218
Tejun Heo0942eee2013-08-08 20:11:26 -04003219/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003220 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003221 * @it: the iterator to advance
3222 *
3223 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003224 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003225static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003226{
3227 struct list_head *l = it->cset_link;
3228 struct cgrp_cset_link *link;
3229 struct css_set *cset;
3230
3231 /* Advance to the next non-empty css_set */
3232 do {
3233 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003234 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003235 it->cset_link = NULL;
3236 return;
3237 }
3238 link = list_entry(l, struct cgrp_cset_link, cset_link);
3239 cset = link->cset;
3240 } while (list_empty(&cset->tasks));
3241 it->cset_link = l;
3242 it->task = cset->tasks.next;
3243}
3244
Tejun Heo0942eee2013-08-08 20:11:26 -04003245/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003246 * css_task_iter_start - initiate task iteration
3247 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003248 * @it: the task iterator to use
3249 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003250 * Initiate iteration through the tasks of @css. The caller can call
3251 * css_task_iter_next() to walk through the tasks until the function
3252 * returns NULL. On completion of iteration, css_task_iter_end() must be
3253 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003254 *
3255 * Note that this function acquires a lock which is released when the
3256 * iteration finishes. The caller can't sleep while iteration is in
3257 * progress.
3258 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003259void css_task_iter_start(struct cgroup_subsys_state *css,
3260 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003261 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003262{
3263 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003264 * The first time anyone tries to iterate across a css, we need to
3265 * enable the list linking each css_set to its tasks, and fix up
3266 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003267 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003268 if (!use_task_css_set_links)
3269 cgroup_enable_task_cg_lists();
3270
Paul Menage817929e2007-10-18 23:39:36 -07003271 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003272
Tejun Heo72ec7022013-08-08 20:11:26 -04003273 it->origin_css = css;
3274 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003275
Tejun Heo72ec7022013-08-08 20:11:26 -04003276 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003277}
3278
Tejun Heo0942eee2013-08-08 20:11:26 -04003279/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003280 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003281 * @it: the task iterator being iterated
3282 *
3283 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003284 * initialized via css_task_iter_start(). Returns NULL when the iteration
3285 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003286 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003287struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003288{
3289 struct task_struct *res;
3290 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003291 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003292
3293 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003294 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003295 return NULL;
3296 res = list_entry(l, struct task_struct, cg_list);
3297 /* Advance iterator to find next entry */
3298 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003299 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3300 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003301 /*
3302 * We reached the end of this task list - move on to the
3303 * next cgrp_cset_link.
3304 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003305 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003306 } else {
3307 it->task = l;
3308 }
3309 return res;
3310}
3311
Tejun Heo0942eee2013-08-08 20:11:26 -04003312/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003313 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003314 * @it: the task iterator to finish
3315 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003316 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003317 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003318void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003319 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003320{
3321 read_unlock(&css_set_lock);
3322}
3323
Cliff Wickman31a7df02008-02-07 00:14:42 -08003324static inline int started_after_time(struct task_struct *t1,
3325 struct timespec *time,
3326 struct task_struct *t2)
3327{
3328 int start_diff = timespec_compare(&t1->start_time, time);
3329 if (start_diff > 0) {
3330 return 1;
3331 } else if (start_diff < 0) {
3332 return 0;
3333 } else {
3334 /*
3335 * Arbitrarily, if two processes started at the same
3336 * time, we'll say that the lower pointer value
3337 * started first. Note that t2 may have exited by now
3338 * so this may not be a valid pointer any longer, but
3339 * that's fine - it still serves to distinguish
3340 * between two tasks started (effectively) simultaneously.
3341 */
3342 return t1 > t2;
3343 }
3344}
3345
3346/*
3347 * This function is a callback from heap_insert() and is used to order
3348 * the heap.
3349 * In this case we order the heap in descending task start time.
3350 */
3351static inline int started_after(void *p1, void *p2)
3352{
3353 struct task_struct *t1 = p1;
3354 struct task_struct *t2 = p2;
3355 return started_after_time(t1, &t2->start_time, t2);
3356}
3357
3358/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003359 * css_scan_tasks - iterate though all the tasks in a css
3360 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003361 * @test: optional test callback
3362 * @process: process callback
3363 * @data: data passed to @test and @process
3364 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003365 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003366 * Iterate through all the tasks in @css, calling @test for each, and if it
3367 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003368 *
Tejun Heoe5358372013-08-08 20:11:26 -04003369 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003370 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003371 * lock css_set_lock for the call to @process.
3372 *
3373 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003374 * of @css for the duration of this call. This function may or may not
3375 * call @process for tasks that exit or move to a different css during the
3376 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003377 *
3378 * Note that @test may be called with locks held, and may in some
3379 * situations be called multiple times for the same task, so it should be
3380 * cheap.
3381 *
3382 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3383 * heap operations (and its "gt" member will be overwritten), else a
3384 * temporary heap will be used (allocation of which may cause this function
3385 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003386 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003387int css_scan_tasks(struct cgroup_subsys_state *css,
3388 bool (*test)(struct task_struct *, void *),
3389 void (*process)(struct task_struct *, void *),
3390 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003391{
3392 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003393 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003394 struct task_struct *p, *dropped;
3395 /* Never dereference latest_task, since it's not refcounted */
3396 struct task_struct *latest_task = NULL;
3397 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003398 struct timespec latest_time = { 0, 0 };
3399
Tejun Heoe5358372013-08-08 20:11:26 -04003400 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003401 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003402 heap->gt = &started_after;
3403 } else {
3404 /* We need to allocate our own heap memory */
3405 heap = &tmp_heap;
3406 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3407 if (retval)
3408 /* cannot allocate the heap */
3409 return retval;
3410 }
3411
3412 again:
3413 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003414 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003415 * which are of interest, and invoking @process callback on the
3416 * ones which need an update. Since we don't want to hold any
3417 * locks during the task updates, gather tasks to be processed in a
3418 * heap structure. The heap is sorted by descending task start
3419 * time. If the statically-sized heap fills up, we overflow tasks
3420 * that started later, and in future iterations only consider tasks
3421 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003422 * guarantees forward progress and that we don't miss any tasks.
3423 */
3424 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003425 css_task_iter_start(css, &it);
3426 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003427 /*
3428 * Only affect tasks that qualify per the caller's callback,
3429 * if he provided one
3430 */
Tejun Heoe5358372013-08-08 20:11:26 -04003431 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003432 continue;
3433 /*
3434 * Only process tasks that started after the last task
3435 * we processed
3436 */
3437 if (!started_after_time(p, &latest_time, latest_task))
3438 continue;
3439 dropped = heap_insert(heap, p);
3440 if (dropped == NULL) {
3441 /*
3442 * The new task was inserted; the heap wasn't
3443 * previously full
3444 */
3445 get_task_struct(p);
3446 } else if (dropped != p) {
3447 /*
3448 * The new task was inserted, and pushed out a
3449 * different task
3450 */
3451 get_task_struct(p);
3452 put_task_struct(dropped);
3453 }
3454 /*
3455 * Else the new task was newer than anything already in
3456 * the heap and wasn't inserted
3457 */
3458 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003459 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003460
3461 if (heap->size) {
3462 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003463 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003464 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003465 latest_time = q->start_time;
3466 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003467 }
3468 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003469 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003470 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003471 }
3472 /*
3473 * If we had to process any tasks at all, scan again
3474 * in case some of them were in the middle of forking
3475 * children that didn't get processed.
3476 * Not the most efficient way to do it, but it avoids
3477 * having to take callback_mutex in the fork path
3478 */
3479 goto again;
3480 }
3481 if (heap == &tmp_heap)
3482 heap_free(&tmp_heap);
3483 return 0;
3484}
3485
Tejun Heoe5358372013-08-08 20:11:26 -04003486static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003487{
Tejun Heoe5358372013-08-08 20:11:26 -04003488 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003489
Tejun Heo47cfcd02013-04-07 09:29:51 -07003490 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003491 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003492 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003493}
3494
3495/**
3496 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3497 * @to: cgroup to which the tasks will be moved
3498 * @from: cgroup in which the tasks currently reside
3499 */
3500int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3501{
Tejun Heo72ec7022013-08-08 20:11:26 -04003502 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3503 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003504}
3505
Paul Menage817929e2007-10-18 23:39:36 -07003506/*
Ben Blum102a7752009-09-23 15:56:26 -07003507 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003508 *
3509 * Reading this file can return large amounts of data if a cgroup has
3510 * *lots* of attached tasks. So it may need several calls to read(),
3511 * but we cannot guarantee that the information we produce is correct
3512 * unless we produce it entirely atomically.
3513 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003514 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003515
Li Zefan24528252012-01-20 11:58:43 +08003516/* which pidlist file are we talking about? */
3517enum cgroup_filetype {
3518 CGROUP_FILE_PROCS,
3519 CGROUP_FILE_TASKS,
3520};
3521
3522/*
3523 * A pidlist is a list of pids that virtually represents the contents of one
3524 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3525 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3526 * to the cgroup.
3527 */
3528struct cgroup_pidlist {
3529 /*
3530 * used to find which pidlist is wanted. doesn't change as long as
3531 * this particular list stays in the list.
3532 */
3533 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3534 /* array of xids */
3535 pid_t *list;
3536 /* how many elements the above list has */
3537 int length;
3538 /* how many files are using the current array */
3539 int use_count;
3540 /* each of these stored in a list by its cgroup */
3541 struct list_head links;
3542 /* pointer to the cgroup we belong to, for list removal purposes */
3543 struct cgroup *owner;
3544 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003545 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003546};
3547
Paul Menagebbcb81d2007-10-18 23:39:32 -07003548/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003549 * The following two functions "fix" the issue where there are more pids
3550 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3551 * TODO: replace with a kernel-wide solution to this problem
3552 */
3553#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3554static void *pidlist_allocate(int count)
3555{
3556 if (PIDLIST_TOO_LARGE(count))
3557 return vmalloc(count * sizeof(pid_t));
3558 else
3559 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3560}
3561static void pidlist_free(void *p)
3562{
3563 if (is_vmalloc_addr(p))
3564 vfree(p);
3565 else
3566 kfree(p);
3567}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003568
3569/*
Ben Blum102a7752009-09-23 15:56:26 -07003570 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003571 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003572 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003573static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003574{
Ben Blum102a7752009-09-23 15:56:26 -07003575 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003576
3577 /*
3578 * we presume the 0th element is unique, so i starts at 1. trivial
3579 * edge cases first; no work needs to be done for either
3580 */
3581 if (length == 0 || length == 1)
3582 return length;
3583 /* src and dest walk down the list; dest counts unique elements */
3584 for (src = 1; src < length; src++) {
3585 /* find next unique element */
3586 while (list[src] == list[src-1]) {
3587 src++;
3588 if (src == length)
3589 goto after;
3590 }
3591 /* dest always points to where the next unique element goes */
3592 list[dest] = list[src];
3593 dest++;
3594 }
3595after:
Ben Blum102a7752009-09-23 15:56:26 -07003596 return dest;
3597}
3598
3599static int cmppid(const void *a, const void *b)
3600{
3601 return *(pid_t *)a - *(pid_t *)b;
3602}
3603
3604/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003605 * find the appropriate pidlist for our purpose (given procs vs tasks)
3606 * returns with the lock on that pidlist already held, and takes care
3607 * of the use count, or returns NULL with no locks held if we're out of
3608 * memory.
3609 */
3610static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3611 enum cgroup_filetype type)
3612{
3613 struct cgroup_pidlist *l;
3614 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003615 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003616
Ben Blum72a8cb32009-09-23 15:56:27 -07003617 /*
Li Zefanb3958902013-08-01 09:52:15 +08003618 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 * the last ref-holder is trying to remove l from the list at the same
3620 * time. Holding the pidlist_mutex precludes somebody taking whichever
3621 * list we find out from under us - compare release_pid_array().
3622 */
3623 mutex_lock(&cgrp->pidlist_mutex);
3624 list_for_each_entry(l, &cgrp->pidlists, links) {
3625 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003627 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 return l;
3630 }
3631 }
3632 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003633 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 if (!l) {
3635 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 return l;
3637 }
Li Zefanb3958902013-08-01 09:52:15 +08003638 init_rwsem(&l->rwsem);
3639 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003640 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003641 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003642 l->owner = cgrp;
3643 list_add(&l->links, &cgrp->pidlists);
3644 mutex_unlock(&cgrp->pidlist_mutex);
3645 return l;
3646}
3647
3648/*
Ben Blum102a7752009-09-23 15:56:26 -07003649 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3650 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003651static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3652 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003653{
3654 pid_t *array;
3655 int length;
3656 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003657 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003658 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003659 struct cgroup_pidlist *l;
3660
3661 /*
3662 * If cgroup gets more users after we read count, we won't have
3663 * enough space - tough. This race is indistinguishable to the
3664 * caller from the case that the additional cgroup users didn't
3665 * show up until sometime later on.
3666 */
3667 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003668 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003669 if (!array)
3670 return -ENOMEM;
3671 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003672 css_task_iter_start(&cgrp->dummy_css, &it);
3673 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003674 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003675 break;
Ben Blum102a7752009-09-23 15:56:26 -07003676 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003677 if (type == CGROUP_FILE_PROCS)
3678 pid = task_tgid_vnr(tsk);
3679 else
3680 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003681 if (pid > 0) /* make sure to only use valid results */
3682 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003683 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003684 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003685 length = n;
3686 /* now sort & (if procs) strip out duplicates */
3687 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003689 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 l = cgroup_pidlist_find(cgrp, type);
3691 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003692 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003694 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003696 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003697 l->list = array;
3698 l->length = length;
3699 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003700 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003701 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003702 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003703}
3704
Balbir Singh846c7bb2007-10-18 23:39:44 -07003705/**
Li Zefana043e3b2008-02-23 15:24:09 -08003706 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003707 * @stats: cgroupstats to fill information into
3708 * @dentry: A dentry entry belonging to the cgroup for which stats have
3709 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003710 *
3711 * Build and fill cgroupstats so that taskstats can export it to user
3712 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003713 */
3714int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3715{
3716 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003717 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003718 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003720
Balbir Singh846c7bb2007-10-18 23:39:44 -07003721 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003722 * Validate dentry by checking the superblock operations,
3723 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003724 */
Li Zefan33d283b2008-11-19 15:36:48 -08003725 if (dentry->d_sb->s_op != &cgroup_ops ||
3726 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003727 goto err;
3728
3729 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003730 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003731
Tejun Heo72ec7022013-08-08 20:11:26 -04003732 css_task_iter_start(&cgrp->dummy_css, &it);
3733 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003734 switch (tsk->state) {
3735 case TASK_RUNNING:
3736 stats->nr_running++;
3737 break;
3738 case TASK_INTERRUPTIBLE:
3739 stats->nr_sleeping++;
3740 break;
3741 case TASK_UNINTERRUPTIBLE:
3742 stats->nr_uninterruptible++;
3743 break;
3744 case TASK_STOPPED:
3745 stats->nr_stopped++;
3746 break;
3747 default:
3748 if (delayacct_is_task_waiting_on_io(tsk))
3749 stats->nr_io_wait++;
3750 break;
3751 }
3752 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003753 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003754
Balbir Singh846c7bb2007-10-18 23:39:44 -07003755err:
3756 return ret;
3757}
3758
Paul Menage8f3ff202009-09-23 15:56:25 -07003759
Paul Menagecc31edc2008-10-18 20:28:04 -07003760/*
Ben Blum102a7752009-09-23 15:56:26 -07003761 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003762 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003763 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003764 */
3765
Ben Blum102a7752009-09-23 15:56:26 -07003766static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003767{
3768 /*
3769 * Initially we receive a position value that corresponds to
3770 * one more than the last pid shown (or 0 on the first call or
3771 * after a seek to the start). Use a binary-search to find the
3772 * next pid to display, if any
3773 */
Ben Blum102a7752009-09-23 15:56:26 -07003774 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003775 int index = 0, pid = *pos;
3776 int *iter;
3777
Li Zefanb3958902013-08-01 09:52:15 +08003778 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003779 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003780 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003781
Paul Menagecc31edc2008-10-18 20:28:04 -07003782 while (index < end) {
3783 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003784 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003785 index = mid;
3786 break;
Ben Blum102a7752009-09-23 15:56:26 -07003787 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003788 index = mid + 1;
3789 else
3790 end = mid;
3791 }
3792 }
3793 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003794 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003795 return NULL;
3796 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003797 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003798 *pos = *iter;
3799 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003800}
3801
Ben Blum102a7752009-09-23 15:56:26 -07003802static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003803{
Ben Blum102a7752009-09-23 15:56:26 -07003804 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003805 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003806}
3807
Ben Blum102a7752009-09-23 15:56:26 -07003808static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003809{
Ben Blum102a7752009-09-23 15:56:26 -07003810 struct cgroup_pidlist *l = s->private;
3811 pid_t *p = v;
3812 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003813 /*
3814 * Advance to the next pid in the array. If this goes off the
3815 * end, we're done
3816 */
3817 p++;
3818 if (p >= end) {
3819 return NULL;
3820 } else {
3821 *pos = *p;
3822 return p;
3823 }
3824}
3825
Ben Blum102a7752009-09-23 15:56:26 -07003826static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003827{
3828 return seq_printf(s, "%d\n", *(int *)v);
3829}
3830
Ben Blum102a7752009-09-23 15:56:26 -07003831/*
3832 * seq_operations functions for iterating on pidlists through seq_file -
3833 * independent of whether it's tasks or procs
3834 */
3835static const struct seq_operations cgroup_pidlist_seq_operations = {
3836 .start = cgroup_pidlist_start,
3837 .stop = cgroup_pidlist_stop,
3838 .next = cgroup_pidlist_next,
3839 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003840};
3841
Ben Blum102a7752009-09-23 15:56:26 -07003842static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003843{
Ben Blum72a8cb32009-09-23 15:56:27 -07003844 /*
3845 * the case where we're the last user of this particular pidlist will
3846 * have us remove it from the cgroup's list, which entails taking the
3847 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3848 * pidlist_mutex, we have to take pidlist_mutex first.
3849 */
3850 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003851 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003852 BUG_ON(!l->use_count);
3853 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003854 /* we're the last user if refcount is 0; remove and free */
3855 list_del(&l->links);
3856 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003857 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003858 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003859 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003860 kfree(l);
3861 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003862 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003863 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003864 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003865}
3866
Ben Blum102a7752009-09-23 15:56:26 -07003867static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003868{
Ben Blum102a7752009-09-23 15:56:26 -07003869 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003870 if (!(file->f_mode & FMODE_READ))
3871 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003872 /*
3873 * the seq_file will only be initialized if the file was opened for
3874 * reading; hence we check if it's not null only in that case.
3875 */
3876 l = ((struct seq_file *)file->private_data)->private;
3877 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003878 return seq_release(inode, file);
3879}
3880
Ben Blum102a7752009-09-23 15:56:26 -07003881static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003882 .read = seq_read,
3883 .llseek = seq_lseek,
3884 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003885 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003886};
3887
3888/*
Ben Blum102a7752009-09-23 15:56:26 -07003889 * The following functions handle opens on a file that displays a pidlist
3890 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3891 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003892 */
Ben Blum102a7752009-09-23 15:56:26 -07003893/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003894static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003895{
3896 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003897 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003898 int retval;
3899
3900 /* Nothing to do for write-only files */
3901 if (!(file->f_mode & FMODE_READ))
3902 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003903
Ben Blum102a7752009-09-23 15:56:26 -07003904 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003905 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003906 if (retval)
3907 return retval;
3908 /* configure file information */
3909 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003910
Ben Blum102a7752009-09-23 15:56:26 -07003911 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003912 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003913 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003914 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003915 }
Ben Blum102a7752009-09-23 15:56:26 -07003916 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003917 return 0;
3918}
Ben Blum102a7752009-09-23 15:56:26 -07003919static int cgroup_tasks_open(struct inode *unused, struct file *file)
3920{
Ben Blum72a8cb32009-09-23 15:56:27 -07003921 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003922}
3923static int cgroup_procs_open(struct inode *unused, struct file *file)
3924{
Ben Blum72a8cb32009-09-23 15:56:27 -07003925 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003926}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003927
Tejun Heo182446d2013-08-08 20:11:24 -04003928static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3929 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003930{
Tejun Heo182446d2013-08-08 20:11:24 -04003931 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003932}
3933
Tejun Heo182446d2013-08-08 20:11:24 -04003934static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3935 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003936{
Tejun Heo182446d2013-08-08 20:11:24 -04003937 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003938 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003939 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003940 else
Tejun Heo182446d2013-08-08 20:11:24 -04003941 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003942 return 0;
3943}
3944
Paul Menagebbcb81d2007-10-18 23:39:32 -07003945/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003946 * When dput() is called asynchronously, if umount has been done and
3947 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3948 * there's a small window that vfs will see the root dentry with non-zero
3949 * refcnt and trigger BUG().
3950 *
3951 * That's why we hold a reference before dput() and drop it right after.
3952 */
3953static void cgroup_dput(struct cgroup *cgrp)
3954{
3955 struct super_block *sb = cgrp->root->sb;
3956
3957 atomic_inc(&sb->s_active);
3958 dput(cgrp->dentry);
3959 deactivate_super(sb);
3960}
3961
3962/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003963 * Unregister event and free resources.
3964 *
3965 * Gets called from workqueue.
3966 */
3967static void cgroup_event_remove(struct work_struct *work)
3968{
3969 struct cgroup_event *event = container_of(work, struct cgroup_event,
3970 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003971 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972
Li Zefan810cbee2013-02-18 18:56:14 +08003973 remove_wait_queue(event->wqh, &event->wait);
3974
Tejun Heo81eeaf02013-08-08 20:11:26 -04003975 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003976
Li Zefan810cbee2013-02-18 18:56:14 +08003977 /* Notify userspace the event is going away. */
3978 eventfd_signal(event->eventfd, 1);
3979
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003980 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003981 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003982 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003983}
3984
3985/*
3986 * Gets called on POLLHUP on eventfd when user closes it.
3987 *
3988 * Called with wqh->lock held and interrupts disabled.
3989 */
3990static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3991 int sync, void *key)
3992{
3993 struct cgroup_event *event = container_of(wait,
3994 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003995 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003996 unsigned long flags = (unsigned long)key;
3997
3998 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 /*
Li Zefan810cbee2013-02-18 18:56:14 +08004000 * If the event has been detached at cgroup removal, we
4001 * can simply return knowing the other side will cleanup
4002 * for us.
4003 *
4004 * We can't race against event freeing since the other
4005 * side will require wqh->lock via remove_wait_queue(),
4006 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004007 */
Li Zefan810cbee2013-02-18 18:56:14 +08004008 spin_lock(&cgrp->event_list_lock);
4009 if (!list_empty(&event->list)) {
4010 list_del_init(&event->list);
4011 /*
4012 * We are in atomic context, but cgroup_event_remove()
4013 * may sleep, so we have to call it in workqueue.
4014 */
4015 schedule_work(&event->remove);
4016 }
4017 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004018 }
4019
4020 return 0;
4021}
4022
4023static void cgroup_event_ptable_queue_proc(struct file *file,
4024 wait_queue_head_t *wqh, poll_table *pt)
4025{
4026 struct cgroup_event *event = container_of(pt,
4027 struct cgroup_event, pt);
4028
4029 event->wqh = wqh;
4030 add_wait_queue(wqh, &event->wait);
4031}
4032
4033/*
4034 * Parse input and register new cgroup event handler.
4035 *
4036 * Input must be in format '<event_fd> <control_fd> <args>'.
4037 * Interpretation of args is defined by control file implementation.
4038 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004039static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04004040 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004041{
Tejun Heo6e6eab02013-08-15 11:43:15 -04004042 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004043 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04004044 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004045 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004046 struct file *efile;
4047 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004048 char *endp;
4049 int ret;
4050
4051 efd = simple_strtoul(buffer, &endp, 10);
4052 if (*endp != ' ')
4053 return -EINVAL;
4054 buffer = endp + 1;
4055
4056 cfd = simple_strtoul(buffer, &endp, 10);
4057 if ((*endp != ' ') && (*endp != '\0'))
4058 return -EINVAL;
4059 buffer = endp + 1;
4060
4061 event = kzalloc(sizeof(*event), GFP_KERNEL);
4062 if (!event)
4063 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004064
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004065 INIT_LIST_HEAD(&event->list);
4066 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4067 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4068 INIT_WORK(&event->remove, cgroup_event_remove);
4069
4070 efile = eventfd_fget(efd);
4071 if (IS_ERR(efile)) {
4072 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004073 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004074 }
4075
4076 event->eventfd = eventfd_ctx_fileget(efile);
4077 if (IS_ERR(event->eventfd)) {
4078 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004079 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004080 }
4081
4082 cfile = fget(cfd);
4083 if (!cfile) {
4084 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004085 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004086 }
4087
4088 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004089 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004090 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004091 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004092 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004093
4094 event->cft = __file_cft(cfile);
4095 if (IS_ERR(event->cft)) {
4096 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004097 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004098 }
4099
Tejun Heo6e6eab02013-08-15 11:43:15 -04004100 if (!event->cft->ss) {
4101 ret = -EBADF;
4102 goto out_put_cfile;
4103 }
4104
Tejun Heo7941cb02013-08-26 18:40:56 -04004105 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004106 * Determine the css of @cfile, verify it belongs to the same
4107 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004108 * Remaining events are automatically removed on cgroup destruction
4109 * but the removal is asynchronous, so take an extra ref.
4110 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004111 rcu_read_lock();
4112
4113 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004114 event->css = cgroup_css(cgrp, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004115 cfile_css = css_from_dir(cfile->f_dentry->d_parent, event->cft->ss);
4116 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004117 ret = 0;
4118
4119 rcu_read_unlock();
4120 if (ret)
4121 goto out_put_cfile;
4122
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004123 if (!event->cft->register_event || !event->cft->unregister_event) {
4124 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004125 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004126 }
4127
Tejun Heo6e6eab02013-08-15 11:43:15 -04004128 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004129 event->eventfd, buffer);
4130 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004131 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004132
Li Zefan7ef70e42013-04-26 11:58:03 -07004133 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004134
4135 spin_lock(&cgrp->event_list_lock);
4136 list_add(&event->list, &cgrp->event_list);
4137 spin_unlock(&cgrp->event_list_lock);
4138
4139 fput(cfile);
4140 fput(efile);
4141
4142 return 0;
4143
Tejun Heo7941cb02013-08-26 18:40:56 -04004144out_put_css:
4145 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004146out_put_cfile:
4147 fput(cfile);
4148out_put_eventfd:
4149 eventfd_ctx_put(event->eventfd);
4150out_put_efile:
4151 fput(efile);
4152out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004153 kfree(event);
4154
4155 return ret;
4156}
4157
Tejun Heo182446d2013-08-08 20:11:24 -04004158static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4159 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004160{
Tejun Heo182446d2013-08-08 20:11:24 -04004161 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004162}
4163
Tejun Heo182446d2013-08-08 20:11:24 -04004164static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4165 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004166{
4167 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004168 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004169 else
Tejun Heo182446d2013-08-08 20:11:24 -04004170 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004171 return 0;
4172}
4173
Tejun Heod5c56ce2013-06-03 19:14:34 -07004174static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004175 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004176 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004177 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004178 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004179 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004180 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004181 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004182 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004183 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004184 .write_string = cgroup_write_event_control,
4185 .mode = S_IWUGO,
4186 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004187 {
4188 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004189 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004190 .read_u64 = cgroup_clone_children_read,
4191 .write_u64 = cgroup_clone_children_write,
4192 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004193 {
Tejun Heo873fe092013-04-14 20:15:26 -07004194 .name = "cgroup.sane_behavior",
4195 .flags = CFTYPE_ONLY_ON_ROOT,
4196 .read_seq_string = cgroup_sane_behavior_show,
4197 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004198
4199 /*
4200 * Historical crazy stuff. These don't have "cgroup." prefix and
4201 * don't exist if sane_behavior. If you're depending on these, be
4202 * prepared to be burned.
4203 */
4204 {
4205 .name = "tasks",
4206 .flags = CFTYPE_INSANE, /* use "procs" instead */
4207 .open = cgroup_tasks_open,
4208 .write_u64 = cgroup_tasks_write,
4209 .release = cgroup_pidlist_release,
4210 .mode = S_IRUGO | S_IWUSR,
4211 },
4212 {
4213 .name = "notify_on_release",
4214 .flags = CFTYPE_INSANE,
4215 .read_u64 = cgroup_read_notify_on_release,
4216 .write_u64 = cgroup_write_notify_on_release,
4217 },
Tejun Heo873fe092013-04-14 20:15:26 -07004218 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004219 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004220 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004221 .read_seq_string = cgroup_release_agent_show,
4222 .write_string = cgroup_release_agent_write,
4223 .max_write_len = PATH_MAX,
4224 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004225 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004226};
4227
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004228/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004229 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004230 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004231 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004232 *
4233 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004234 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004235static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004236{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004237 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004238 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004239
Tejun Heo8e3f6542012-04-01 12:09:55 -07004240 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004241 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004242 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004243
4244 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004245 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004246
Tejun Heobee55092013-06-28 16:24:11 -07004247 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004248 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004249 if (ret < 0)
4250 goto err;
4251 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004252 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004253
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004254 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004255 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004256 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004257 struct css_id *id = rcu_dereference_protected(css->id, true);
4258
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004259 /*
4260 * Update id->css pointer and make this css visible from
4261 * CSS ID functions. This pointer will be dereferened
4262 * from RCU-read-side without locks.
4263 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004264 if (id)
4265 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004266 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267
4268 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004269err:
4270 cgroup_clear_dir(cgrp, subsys_mask);
4271 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004272}
4273
Tejun Heo0c21ead2013-08-13 20:22:51 -04004274/*
4275 * css destruction is four-stage process.
4276 *
4277 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4278 * Implemented in kill_css().
4279 *
4280 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4281 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4282 * by invoking offline_css(). After offlining, the base ref is put.
4283 * Implemented in css_killed_work_fn().
4284 *
4285 * 3. When the percpu_ref reaches zero, the only possible remaining
4286 * accessors are inside RCU read sections. css_release() schedules the
4287 * RCU callback.
4288 *
4289 * 4. After the grace period, the css can be freed. Implemented in
4290 * css_free_work_fn().
4291 *
4292 * It is actually hairier because both step 2 and 4 require process context
4293 * and thus involve punting to css->destroy_work adding two additional
4294 * steps to the already complex sequence.
4295 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004296static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004297{
4298 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004299 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004300 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004301
Tejun Heo0ae78e02013-08-13 11:01:54 -04004302 if (css->parent)
4303 css_put(css->parent);
4304
Tejun Heo0c21ead2013-08-13 20:22:51 -04004305 css->ss->css_free(css);
4306 cgroup_dput(cgrp);
4307}
4308
4309static void css_free_rcu_fn(struct rcu_head *rcu_head)
4310{
4311 struct cgroup_subsys_state *css =
4312 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4313
4314 /*
4315 * css holds an extra ref to @cgrp->dentry which is put on the last
4316 * css_put(). dput() requires process context which we don't have.
4317 */
4318 INIT_WORK(&css->destroy_work, css_free_work_fn);
4319 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004320}
4321
Tejun Heod3daf282013-06-13 19:39:16 -07004322static void css_release(struct percpu_ref *ref)
4323{
4324 struct cgroup_subsys_state *css =
4325 container_of(ref, struct cgroup_subsys_state, refcnt);
4326
Tejun Heo0c21ead2013-08-13 20:22:51 -04004327 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004328}
4329
Tejun Heo623f9262013-08-13 11:01:55 -04004330static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4331 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004332{
Paul Menagebd89aab2007-10-18 23:40:44 -07004333 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004334 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004335 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004336 css->id = NULL;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004337
4338 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004339 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004340 else
Tejun Heo38b53ab2012-11-19 08:13:36 -08004341 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004342
Tejun Heoca8bdca2013-08-26 18:40:56 -04004343 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344}
4345
Li Zefan2a4ac632013-07-31 16:16:40 +08004346/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004347static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004348{
Tejun Heo623f9262013-08-13 11:01:55 -04004349 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004350 int ret = 0;
4351
Tejun Heoa31f2d32012-11-19 08:13:37 -08004352 lockdep_assert_held(&cgroup_mutex);
4353
Tejun Heo92fb9742012-11-19 08:13:38 -08004354 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004355 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004356 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004357 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004358 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004359 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4360 }
Tejun Heob1929db2012-11-19 08:13:38 -08004361 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004362}
4363
Li Zefan2a4ac632013-07-31 16:16:40 +08004364/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004365static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004366{
Tejun Heo623f9262013-08-13 11:01:55 -04004367 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004368
4369 lockdep_assert_held(&cgroup_mutex);
4370
4371 if (!(css->flags & CSS_ONLINE))
4372 return;
4373
Li Zefand7eeac12013-03-12 15:35:59 -07004374 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004375 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004376
Tejun Heoeb954192013-08-08 20:11:23 -04004377 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004378 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004379 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004380}
4381
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382/*
Li Zefana043e3b2008-02-23 15:24:09 -08004383 * cgroup_create - create a cgroup
4384 * @parent: cgroup that will be parent of the new cgroup
4385 * @dentry: dentry of the new cgroup
4386 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387 *
Li Zefana043e3b2008-02-23 15:24:09 -08004388 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004389 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004390static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004391 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004392{
Tejun Heoae7f1642013-08-13 20:22:50 -04004393 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004394 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004395 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396 struct cgroupfs_root *root = parent->root;
4397 int err = 0;
4398 struct cgroup_subsys *ss;
4399 struct super_block *sb = root->sb;
4400
Tejun Heo0a950f62012-11-19 09:02:12 -08004401 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004402 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4403 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404 return -ENOMEM;
4405
Li Zefan65dff752013-03-01 15:01:56 +08004406 name = cgroup_alloc_name(dentry);
4407 if (!name)
4408 goto err_free_cgrp;
4409 rcu_assign_pointer(cgrp->name, name);
4410
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004411 /*
4412 * Temporarily set the pointer to NULL, so idr_find() won't return
4413 * a half-baked cgroup.
4414 */
4415 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004416 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004417 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004418
Tejun Heo976c06b2012-11-05 09:16:59 -08004419 /*
4420 * Only live parents can have children. Note that the liveliness
4421 * check isn't strictly necessary because cgroup_mkdir() and
4422 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4423 * anyway so that locking is contained inside cgroup proper and we
4424 * don't get nasty surprises if we ever grow another caller.
4425 */
4426 if (!cgroup_lock_live_group(parent)) {
4427 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004428 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004429 }
4430
Paul Menageddbcc7e2007-10-18 23:39:30 -07004431 /* Grab a reference on the superblock so the hierarchy doesn't
4432 * get deleted on unmount if there are child cgroups. This
4433 * can be done outside cgroup_mutex, since the sb can't
4434 * disappear while someone has an open control file on the
4435 * fs */
4436 atomic_inc(&sb->s_active);
4437
Paul Menagecc31edc2008-10-18 20:28:04 -07004438 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004439
Li Zefanfe1c06c2013-01-24 14:30:22 +08004440 dentry->d_fsdata = cgrp;
4441 cgrp->dentry = dentry;
4442
Paul Menagebd89aab2007-10-18 23:40:44 -07004443 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004444 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004445 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004446
Li Zefanb6abdb02008-03-04 14:28:19 -08004447 if (notify_on_release(parent))
4448 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4449
Tejun Heo2260e7f2012-11-19 08:13:38 -08004450 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4451 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004452
Tejun Heo5549c492013-06-24 15:21:48 -07004453 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004454 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004455
Tejun Heoca8bdca2013-08-26 18:40:56 -04004456 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004457 if (IS_ERR(css)) {
4458 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004459 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004460 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004461 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004462
4463 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004464 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004465 goto err_free_all;
4466
Tejun Heo623f9262013-08-13 11:01:55 -04004467 init_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004468
Li Zefan4528fd02010-02-02 13:44:10 -08004469 if (ss->use_id) {
Tejun Heo623f9262013-08-13 11:01:55 -04004470 err = alloc_css_id(css);
Li Zefan4528fd02010-02-02 13:44:10 -08004471 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004472 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004473 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474 }
4475
Tejun Heo4e139af2012-11-19 08:13:36 -08004476 /*
4477 * Create directory. cgroup_create_file() returns with the new
4478 * directory locked on success so that it can be populated without
4479 * dropping cgroup_mutex.
4480 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004481 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004483 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004484 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004485
Tejun Heo00356bd2013-06-18 11:14:22 -07004486 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004487
Tejun Heo4e139af2012-11-19 08:13:36 -08004488 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004489 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4490 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004491
Tejun Heo0ae78e02013-08-13 11:01:54 -04004492 /* each css holds a ref to the cgroup's dentry and the parent css */
4493 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 Heo0ae78e02013-08-13 11:01:54 -04004495
Tejun Heoed9577932012-11-05 09:16:58 -08004496 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004497 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004498 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004499
Li Zefan415cf072013-04-08 14:35:02 +08004500 /* hold a ref to the parent's dentry */
4501 dget(parent->dentry);
4502
Tejun Heob1929db2012-11-19 08:13:38 -08004503 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004504 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004505 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004506
4507 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004508 if (err)
4509 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004510
4511 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4512 parent->parent) {
4513 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",
4514 current->comm, current->pid, ss->name);
4515 if (!strcmp(ss->name, "memory"))
4516 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4517 ss->warned_broken_hierarchy = true;
4518 }
Tejun Heoa8638032012-11-09 09:12:29 -08004519 }
4520
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004521 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4522
Tejun Heo2bb566c2013-08-08 20:11:23 -04004523 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004524 if (err)
4525 goto err_destroy;
4526
4527 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004528 if (err)
4529 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004530
4531 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004532 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004533
4534 return 0;
4535
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004536err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004537 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004538 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004539
4540 if (css) {
4541 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004542 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004543 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004544 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004545 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546 /* Release the reference count that we took on the superblock */
4547 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004548err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004549 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004550err_free_name:
4551 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004552err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004553 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004554 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004555
4556err_destroy:
4557 cgroup_destroy_locked(cgrp);
4558 mutex_unlock(&cgroup_mutex);
4559 mutex_unlock(&dentry->d_inode->i_mutex);
4560 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004561}
4562
Al Viro18bb1db2011-07-26 01:41:39 -04004563static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004564{
4565 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4566
4567 /* the vfs holds inode->i_mutex already */
4568 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4569}
4570
Tejun Heo223dbc32013-08-13 20:22:50 -04004571/*
4572 * This is called when the refcnt of a css is confirmed to be killed.
4573 * css_tryget() is now guaranteed to fail.
4574 */
4575static void css_killed_work_fn(struct work_struct *work)
4576{
4577 struct cgroup_subsys_state *css =
4578 container_of(work, struct cgroup_subsys_state, destroy_work);
4579 struct cgroup *cgrp = css->cgroup;
4580
Tejun Heof20104d2013-08-13 20:22:50 -04004581 mutex_lock(&cgroup_mutex);
4582
4583 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004584 * css_tryget() is guaranteed to fail now. Tell subsystems to
4585 * initate destruction.
4586 */
4587 offline_css(css);
4588
4589 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004590 * If @cgrp is marked dead, it's waiting for refs of all css's to
4591 * be disabled before proceeding to the second phase of cgroup
4592 * destruction. If we are the last one, kick it off.
4593 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004594 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004595 cgroup_destroy_css_killed(cgrp);
4596
4597 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004598
4599 /*
4600 * Put the css refs from kill_css(). Each css holds an extra
4601 * reference to the cgroup's dentry and cgroup removal proceeds
4602 * regardless of css refs. On the last put of each css, whenever
4603 * that may be, the extra dentry ref is put so that dentry
4604 * destruction happens only after all css's are released.
4605 */
4606 css_put(css);
Tejun Heo223dbc32013-08-13 20:22:50 -04004607}
4608
4609/* css kill confirmation processing requires process context, bounce */
4610static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004611{
4612 struct cgroup_subsys_state *css =
4613 container_of(ref, struct cgroup_subsys_state, refcnt);
4614
Tejun Heo223dbc32013-08-13 20:22:50 -04004615 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4616 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004617}
4618
4619/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004620 * kill_css - destroy a css
4621 * @css: css to destroy
4622 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004623 * This function initiates destruction of @css by removing cgroup interface
4624 * files and putting its base reference. ->css_offline() will be invoked
4625 * asynchronously once css_tryget() is guaranteed to fail and when the
4626 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004627 */
4628static void kill_css(struct cgroup_subsys_state *css)
4629{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004630 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4631
Tejun Heoedae0c32013-08-13 20:22:51 -04004632 /*
4633 * Killing would put the base ref, but we need to keep it alive
4634 * until after ->css_offline().
4635 */
4636 css_get(css);
4637
4638 /*
4639 * cgroup core guarantees that, by the time ->css_offline() is
4640 * invoked, no new css reference will be given out via
4641 * css_tryget(). We can't simply call percpu_ref_kill() and
4642 * proceed to offlining css's because percpu_ref_kill() doesn't
4643 * guarantee that the ref is seen as killed on all CPUs on return.
4644 *
4645 * Use percpu_ref_kill_and_confirm() to get notifications as each
4646 * css is confirmed to be seen as killed on all CPUs.
4647 */
4648 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
4649}
4650
4651/**
Tejun Heod3daf282013-06-13 19:39:16 -07004652 * cgroup_destroy_locked - the first stage of cgroup destruction
4653 * @cgrp: cgroup to be destroyed
4654 *
4655 * css's make use of percpu refcnts whose killing latency shouldn't be
4656 * exposed to userland and are RCU protected. Also, cgroup core needs to
4657 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4658 * invoked. To satisfy all the requirements, destruction is implemented in
4659 * the following two steps.
4660 *
4661 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4662 * userland visible parts and start killing the percpu refcnts of
4663 * css's. Set up so that the next stage will be kicked off once all
4664 * the percpu refcnts are confirmed to be killed.
4665 *
4666 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4667 * rest of destruction. Once all cgroup references are gone, the
4668 * cgroup is RCU-freed.
4669 *
4670 * This function implements s1. After this step, @cgrp is gone as far as
4671 * the userland is concerned and a new cgroup with the same name may be
4672 * created. As cgroup doesn't care about the names internally, this
4673 * doesn't cause any problem.
4674 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004675static int cgroup_destroy_locked(struct cgroup *cgrp)
4676 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004677{
Tejun Heo42809dd2012-11-19 08:13:37 -08004678 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004679 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004680 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004681 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004682
Tejun Heo42809dd2012-11-19 08:13:37 -08004683 lockdep_assert_held(&d->d_inode->i_mutex);
4684 lockdep_assert_held(&cgroup_mutex);
4685
Tejun Heoddd69142013-06-12 21:04:54 -07004686 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004687 * css_set_lock synchronizes access to ->cset_links and prevents
4688 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004689 */
4690 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004691 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004692 read_unlock(&css_set_lock);
4693 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004694 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004695
Tejun Heo1a90dd52012-11-05 09:16:59 -08004696 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004697 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4698 * will be invoked to perform the rest of destruction once the
4699 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004700 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004701 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004702 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004703
4704 /*
4705 * Mark @cgrp dead. This prevents further task migration and child
4706 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004707 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004708 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004709 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004710 */
Tejun Heo54766d42013-06-12 21:04:53 -07004711 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004712
Tejun Heo455050d2013-06-13 19:27:41 -07004713 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4714 raw_spin_lock(&release_list_lock);
4715 if (!list_empty(&cgrp->release_list))
4716 list_del_init(&cgrp->release_list);
4717 raw_spin_unlock(&release_list_lock);
4718
4719 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004720 * If @cgrp has css's attached, the second stage of cgroup
4721 * destruction is kicked off from css_killed_work_fn() after the
4722 * refs of all attached css's are killed. If @cgrp doesn't have
4723 * any css, we kick it off here.
4724 */
4725 if (!cgrp->nr_css)
4726 cgroup_destroy_css_killed(cgrp);
4727
4728 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004729 * Clear the base files and remove @cgrp directory. The removal
4730 * puts the base ref but we aren't quite done with @cgrp yet, so
4731 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004732 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004733 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004734 dget(d);
4735 cgroup_d_remove_dir(d);
4736
4737 /*
4738 * Unregister events and notify userspace.
4739 * Notify userspace about cgroup removing only after rmdir of cgroup
4740 * directory to avoid race between userspace and kernelspace.
4741 */
4742 spin_lock(&cgrp->event_list_lock);
4743 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4744 list_del_init(&event->list);
4745 schedule_work(&event->remove);
4746 }
4747 spin_unlock(&cgrp->event_list_lock);
4748
Tejun Heoea15f8c2013-06-13 19:27:42 -07004749 return 0;
4750};
4751
Tejun Heod3daf282013-06-13 19:39:16 -07004752/**
Tejun Heof20104d2013-08-13 20:22:50 -04004753 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004754 * @work: cgroup->destroy_free_work
4755 *
4756 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004757 * destroyed after all css's are offlined and performs the rest of
4758 * destruction. This is the second step of destruction described in the
4759 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004760 */
Tejun Heof20104d2013-08-13 20:22:50 -04004761static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004762{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004763 struct cgroup *parent = cgrp->parent;
4764 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004765
Tejun Heof20104d2013-08-13 20:22:50 -04004766 lockdep_assert_held(&cgroup_mutex);
Tejun Heoea15f8c2013-06-13 19:27:42 -07004767
Paul Menage999cd8a2009-01-07 18:08:36 -08004768 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004769 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004770
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004771 /*
4772 * We should remove the cgroup object from idr before its grace
4773 * period starts, so we won't be looking up a cgroup while the
4774 * cgroup is being freed.
4775 */
4776 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4777 cgrp->id = -1;
4778
Paul Menageddbcc7e2007-10-18 23:39:30 -07004779 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004780
Paul Menagebd89aab2007-10-18 23:40:44 -07004781 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004782 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004783}
4784
Tejun Heo42809dd2012-11-19 08:13:37 -08004785static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4786{
4787 int ret;
4788
4789 mutex_lock(&cgroup_mutex);
4790 ret = cgroup_destroy_locked(dentry->d_fsdata);
4791 mutex_unlock(&cgroup_mutex);
4792
4793 return ret;
4794}
4795
Tejun Heo8e3f6542012-04-01 12:09:55 -07004796static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4797{
4798 INIT_LIST_HEAD(&ss->cftsets);
4799
4800 /*
4801 * base_cftset is embedded in subsys itself, no need to worry about
4802 * deregistration.
4803 */
4804 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004805 struct cftype *cft;
4806
4807 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4808 cft->ss = ss;
4809
Tejun Heo8e3f6542012-04-01 12:09:55 -07004810 ss->base_cftset.cfts = ss->base_cftypes;
4811 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4812 }
4813}
4814
Li Zefan06a11922008-04-29 01:00:07 -07004815static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004816{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004817 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004818
4819 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004820
Tejun Heo648bb562012-11-19 08:13:36 -08004821 mutex_lock(&cgroup_mutex);
4822
Tejun Heo8e3f6542012-04-01 12:09:55 -07004823 /* init base cftset */
4824 cgroup_init_cftsets(ss);
4825
Paul Menageddbcc7e2007-10-18 23:39:30 -07004826 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4828 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004829 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004830 /* We don't handle early failures gracefully */
4831 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004832 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004833
Li Zefane8d55fd2008-04-29 01:00:13 -07004834 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004835 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004836 * newly registered, all tasks and hence the
4837 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004838 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839
4840 need_forkexit_callback |= ss->fork || ss->exit;
4841
Li Zefane8d55fd2008-04-29 01:00:13 -07004842 /* At system boot, before all subsystems have been
4843 * registered, no tasks have been forked, so we don't
4844 * need to invoke fork callbacks here. */
4845 BUG_ON(!list_empty(&init_task.tasks));
4846
Tejun Heoae7f1642013-08-13 20:22:50 -04004847 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004848
Tejun Heo648bb562012-11-19 08:13:36 -08004849 mutex_unlock(&cgroup_mutex);
4850
Ben Blume6a11052010-03-10 15:22:09 -08004851 /* this function shouldn't be used with modular subsystems, since they
4852 * need to register a subsys_id, among other things */
4853 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004854}
4855
4856/**
Ben Blume6a11052010-03-10 15:22:09 -08004857 * cgroup_load_subsys: load and register a modular subsystem at runtime
4858 * @ss: the subsystem to load
4859 *
4860 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004861 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004862 * up for use. If the subsystem is built-in anyway, work is delegated to the
4863 * simpler cgroup_init_subsys.
4864 */
4865int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4866{
Ben Blume6a11052010-03-10 15:22:09 -08004867 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004868 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004869 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004870 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004871 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004872
4873 /* check name and function validity */
4874 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004875 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004876 return -EINVAL;
4877
4878 /*
4879 * we don't support callbacks in modular subsystems. this check is
4880 * before the ss->module check for consistency; a subsystem that could
4881 * be a module should still have no callbacks even if the user isn't
4882 * compiling it as one.
4883 */
4884 if (ss->fork || ss->exit)
4885 return -EINVAL;
4886
4887 /*
4888 * an optionally modular subsystem is built-in: we want to do nothing,
4889 * since cgroup_init_subsys will have already taken care of it.
4890 */
4891 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004892 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004893 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004894 return 0;
4895 }
4896
Tejun Heo8e3f6542012-04-01 12:09:55 -07004897 /* init base cftset */
4898 cgroup_init_cftsets(ss);
4899
Ben Blume6a11052010-03-10 15:22:09 -08004900 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004901 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004902
4903 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004904 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004905 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004906 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004907 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004908 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004909 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004910 /* failure case - need to deassign the cgroup_subsys[] slot. */
4911 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004912 mutex_unlock(&cgroup_mutex);
4913 return PTR_ERR(css);
4914 }
4915
Tejun Heo9871bf92013-06-24 15:21:47 -07004916 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4917 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004918
4919 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004920 init_css(css, ss, cgroup_dummy_top);
4921 /* init_idr must be after init_css() because it sets css->id. */
Ben Blume6a11052010-03-10 15:22:09 -08004922 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004923 ret = cgroup_init_idr(ss, css);
4924 if (ret)
4925 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004926 }
4927
4928 /*
4929 * Now we need to entangle the css into the existing css_sets. unlike
4930 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4931 * will need a new pointer to it; done by iterating the css_set_table.
4932 * furthermore, modifying the existing css_sets will corrupt the hash
4933 * table state, so each changed css_set will need its hash recomputed.
4934 * this is all done under the css_set_lock.
4935 */
4936 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004937 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004938 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004939 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004940 continue;
4941 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004942 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004943 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004944 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004945 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004946 key = css_set_hash(cset->subsys);
4947 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004948 }
4949 write_unlock(&css_set_lock);
4950
Tejun Heoae7f1642013-08-13 20:22:50 -04004951 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004952 if (ret)
4953 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004954
Ben Blume6a11052010-03-10 15:22:09 -08004955 /* success! */
4956 mutex_unlock(&cgroup_mutex);
4957 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004958
4959err_unload:
4960 mutex_unlock(&cgroup_mutex);
4961 /* @ss can't be mounted here as try_module_get() would fail */
4962 cgroup_unload_subsys(ss);
4963 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004964}
4965EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4966
4967/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004968 * cgroup_unload_subsys: unload a modular subsystem
4969 * @ss: the subsystem to unload
4970 *
4971 * This function should be called in a modular subsystem's exitcall. When this
4972 * function is invoked, the refcount on the subsystem's module will be 0, so
4973 * the subsystem will not be attached to any hierarchy.
4974 */
4975void cgroup_unload_subsys(struct cgroup_subsys *ss)
4976{
Tejun Heo69d02062013-06-12 21:04:50 -07004977 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004978
4979 BUG_ON(ss->module == NULL);
4980
4981 /*
4982 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004983 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004984 * doesn't start being used while we're killing it off.
4985 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004986 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004987
4988 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004989
Tejun Heoca8bdca2013-08-26 18:40:56 -04004990 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08004991
Tejun Heoc897ff62013-02-27 17:03:49 -08004992 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004993 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004994
Ben Blumcf5d5942010-03-10 15:22:09 -08004995 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004996 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004997
Tejun Heo9871bf92013-06-24 15:21:47 -07004998 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004999 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08005000
5001 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005002 * disentangle the css from all css_sets attached to the dummy
5003 * top. as in loading, we need to pay our respects to the hashtable
5004 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08005005 */
5006 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07005007 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005008 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08005009 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08005010
Tejun Heo5abb8852013-06-12 21:04:49 -07005011 hash_del(&cset->hlist);
5012 cset->subsys[ss->subsys_id] = NULL;
5013 key = css_set_hash(cset->subsys);
5014 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08005015 }
5016 write_unlock(&css_set_lock);
5017
5018 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005019 * remove subsystem's css from the cgroup_dummy_top and free it -
5020 * need to free before marking as null because ss->css_free needs
5021 * the cgrp->subsys pointer to find their state. note that this
5022 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08005023 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04005024 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04005025 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08005026
5027 mutex_unlock(&cgroup_mutex);
5028}
5029EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
5030
5031/**
Li Zefana043e3b2008-02-23 15:24:09 -08005032 * cgroup_init_early - cgroup initialization at system boot
5033 *
5034 * Initialize cgroups at system boot, and initialize any
5035 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005036 */
5037int __init cgroup_init_early(void)
5038{
Tejun Heo30159ec2013-06-25 11:53:37 -07005039 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005040 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005041
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07005042 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07005043 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07005044 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005045 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005046 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005047 init_cgroup_root(&cgroup_dummy_root);
5048 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005049 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005050
Tejun Heo69d02062013-06-12 21:04:50 -07005051 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005052 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5053 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005054 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005055
Tejun Heo30159ec2013-06-25 11:53:37 -07005056 /* at bootup time, we don't worry about modular subsystems */
5057 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005058 BUG_ON(!ss->name);
5059 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005060 BUG_ON(!ss->css_alloc);
5061 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005062 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005063 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005064 ss->name, ss->subsys_id);
5065 BUG();
5066 }
5067
5068 if (ss->early_init)
5069 cgroup_init_subsys(ss);
5070 }
5071 return 0;
5072}
5073
5074/**
Li Zefana043e3b2008-02-23 15:24:09 -08005075 * cgroup_init - cgroup initialization
5076 *
5077 * Register cgroup filesystem and /proc file, and initialize
5078 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005079 */
5080int __init cgroup_init(void)
5081{
Tejun Heo30159ec2013-06-25 11:53:37 -07005082 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005083 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005084 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005085
5086 err = bdi_init(&cgroup_backing_dev_info);
5087 if (err)
5088 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005089
Tejun Heo30159ec2013-06-25 11:53:37 -07005090 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005091 if (!ss->early_init)
5092 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005093 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005094 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005095 }
5096
Tejun Heofa3ca072013-04-14 11:36:56 -07005097 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005098 mutex_lock(&cgroup_mutex);
5099 mutex_lock(&cgroup_root_mutex);
5100
Tejun Heo82fe9b02013-06-25 11:53:37 -07005101 /* Add init_css_set to the hash table */
5102 key = css_set_hash(init_css_set.subsys);
5103 hash_add(css_set_table, &init_css_set.hlist, key);
5104
Tejun Heofc76df72013-06-25 11:53:37 -07005105 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005106
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005107 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5108 0, 1, GFP_KERNEL);
5109 BUG_ON(err < 0);
5110
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005111 mutex_unlock(&cgroup_root_mutex);
5112 mutex_unlock(&cgroup_mutex);
5113
Greg KH676db4a2010-08-05 13:53:35 -07005114 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5115 if (!cgroup_kobj) {
5116 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005117 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005118 }
5119
5120 err = register_filesystem(&cgroup_fs_type);
5121 if (err < 0) {
5122 kobject_put(cgroup_kobj);
5123 goto out;
5124 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005125
Li Zefan46ae2202008-04-29 01:00:08 -07005126 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005127
Paul Menageddbcc7e2007-10-18 23:39:30 -07005128out:
Paul Menagea4243162007-10-18 23:39:35 -07005129 if (err)
5130 bdi_destroy(&cgroup_backing_dev_info);
5131
Paul Menageddbcc7e2007-10-18 23:39:30 -07005132 return err;
5133}
Paul Menageb4f48b62007-10-18 23:39:33 -07005134
Paul Menagea4243162007-10-18 23:39:35 -07005135/*
5136 * proc_cgroup_show()
5137 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5138 * - Used for /proc/<pid>/cgroup.
5139 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5140 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005141 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005142 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5143 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5144 * cgroup to top_cgroup.
5145 */
5146
5147/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005148int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005149{
5150 struct pid *pid;
5151 struct task_struct *tsk;
5152 char *buf;
5153 int retval;
5154 struct cgroupfs_root *root;
5155
5156 retval = -ENOMEM;
5157 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5158 if (!buf)
5159 goto out;
5160
5161 retval = -ESRCH;
5162 pid = m->private;
5163 tsk = get_pid_task(pid, PIDTYPE_PID);
5164 if (!tsk)
5165 goto out_free;
5166
5167 retval = 0;
5168
5169 mutex_lock(&cgroup_mutex);
5170
Li Zefane5f6a862009-01-07 18:07:41 -08005171 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005172 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005173 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005174 int count = 0;
5175
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005176 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005177 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005178 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005179 if (strlen(root->name))
5180 seq_printf(m, "%sname=%s", count ? "," : "",
5181 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005182 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005183 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005184 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005185 if (retval < 0)
5186 goto out_unlock;
5187 seq_puts(m, buf);
5188 seq_putc(m, '\n');
5189 }
5190
5191out_unlock:
5192 mutex_unlock(&cgroup_mutex);
5193 put_task_struct(tsk);
5194out_free:
5195 kfree(buf);
5196out:
5197 return retval;
5198}
5199
Paul Menagea4243162007-10-18 23:39:35 -07005200/* Display information about each subsystem and each hierarchy */
5201static int proc_cgroupstats_show(struct seq_file *m, void *v)
5202{
Tejun Heo30159ec2013-06-25 11:53:37 -07005203 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005204 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005205
Paul Menage8bab8dd2008-04-04 14:29:57 -07005206 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005207 /*
5208 * ideally we don't want subsystems moving around while we do this.
5209 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5210 * subsys/hierarchy state.
5211 */
Paul Menagea4243162007-10-18 23:39:35 -07005212 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005213
5214 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005215 seq_printf(m, "%s\t%d\t%d\t%d\n",
5216 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005217 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005218
Paul Menagea4243162007-10-18 23:39:35 -07005219 mutex_unlock(&cgroup_mutex);
5220 return 0;
5221}
5222
5223static int cgroupstats_open(struct inode *inode, struct file *file)
5224{
Al Viro9dce07f2008-03-29 03:07:28 +00005225 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005226}
5227
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005228static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005229 .open = cgroupstats_open,
5230 .read = seq_read,
5231 .llseek = seq_lseek,
5232 .release = single_release,
5233};
5234
Paul Menageb4f48b62007-10-18 23:39:33 -07005235/**
5236 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005237 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005238 *
5239 * Description: A task inherits its parent's cgroup at fork().
5240 *
5241 * A pointer to the shared css_set was automatically copied in
5242 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005243 * it was not made under the protection of RCU or cgroup_mutex, so
5244 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5245 * have already changed current->cgroups, allowing the previously
5246 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005247 *
5248 * At the point that cgroup_fork() is called, 'current' is the parent
5249 * task, and the passed argument 'child' points to the child task.
5250 */
5251void cgroup_fork(struct task_struct *child)
5252{
Tejun Heo9bb71302012-10-18 17:52:07 -07005253 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005254 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005255 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005256 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005257 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005258}
5259
5260/**
Li Zefana043e3b2008-02-23 15:24:09 -08005261 * cgroup_post_fork - called on a new task after adding it to the task list
5262 * @child: the task in question
5263 *
Tejun Heo5edee612012-10-16 15:03:14 -07005264 * Adds the task to the list running through its css_set if necessary and
5265 * call the subsystem fork() callbacks. Has to be after the task is
5266 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005267 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005268 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005269 */
Paul Menage817929e2007-10-18 23:39:36 -07005270void cgroup_post_fork(struct task_struct *child)
5271{
Tejun Heo30159ec2013-06-25 11:53:37 -07005272 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005273 int i;
5274
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005275 /*
5276 * use_task_css_set_links is set to 1 before we walk the tasklist
5277 * under the tasklist_lock and we read it here after we added the child
5278 * to the tasklist under the tasklist_lock as well. If the child wasn't
5279 * yet in the tasklist when we walked through it from
5280 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5281 * should be visible now due to the paired locking and barriers implied
5282 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5283 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5284 * lock on fork.
5285 */
Paul Menage817929e2007-10-18 23:39:36 -07005286 if (use_task_css_set_links) {
5287 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005288 task_lock(child);
5289 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005290 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005291 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005292 write_unlock(&css_set_lock);
5293 }
Tejun Heo5edee612012-10-16 15:03:14 -07005294
5295 /*
5296 * Call ss->fork(). This must happen after @child is linked on
5297 * css_set; otherwise, @child might change state between ->fork()
5298 * and addition to css_set.
5299 */
5300 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005301 /*
5302 * fork/exit callbacks are supported only for builtin
5303 * subsystems, and the builtin section of the subsys
5304 * array is immutable, so we don't need to lock the
5305 * subsys array here. On the other hand, modular section
5306 * of the array can be freed at module unload, so we
5307 * can't touch that.
5308 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005309 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005310 if (ss->fork)
5311 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005312 }
Paul Menage817929e2007-10-18 23:39:36 -07005313}
Tejun Heo5edee612012-10-16 15:03:14 -07005314
Paul Menage817929e2007-10-18 23:39:36 -07005315/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005316 * cgroup_exit - detach cgroup from exiting task
5317 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005318 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005319 *
5320 * Description: Detach cgroup from @tsk and release it.
5321 *
5322 * Note that cgroups marked notify_on_release force every task in
5323 * them to take the global cgroup_mutex mutex when exiting.
5324 * This could impact scaling on very large systems. Be reluctant to
5325 * use notify_on_release cgroups where very high task exit scaling
5326 * is required on large systems.
5327 *
5328 * the_top_cgroup_hack:
5329 *
5330 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5331 *
5332 * We call cgroup_exit() while the task is still competent to
5333 * handle notify_on_release(), then leave the task attached to the
5334 * root cgroup in each hierarchy for the remainder of its exit.
5335 *
5336 * To do this properly, we would increment the reference count on
5337 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5338 * code we would add a second cgroup function call, to drop that
5339 * reference. This would just create an unnecessary hot spot on
5340 * the top_cgroup reference count, to no avail.
5341 *
5342 * Normally, holding a reference to a cgroup without bumping its
5343 * count is unsafe. The cgroup could go away, or someone could
5344 * attach us to a different cgroup, decrementing the count on
5345 * the first cgroup that we never incremented. But in this case,
5346 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005347 * which wards off any cgroup_attach_task() attempts, or task is a failed
5348 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005349 */
5350void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5351{
Tejun Heo30159ec2013-06-25 11:53:37 -07005352 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005353 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005354 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005355
5356 /*
5357 * Unlink from the css_set task list if necessary.
5358 * Optimistically check cg_list before taking
5359 * css_set_lock
5360 */
5361 if (!list_empty(&tsk->cg_list)) {
5362 write_lock(&css_set_lock);
5363 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005364 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005365 write_unlock(&css_set_lock);
5366 }
5367
Paul Menageb4f48b62007-10-18 23:39:33 -07005368 /* Reassign the task to the init_css_set. */
5369 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005370 cset = task_css_set(tsk);
5371 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005372
5373 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005374 /*
5375 * fork/exit callbacks are supported only for builtin
5376 * subsystems, see cgroup_post_fork() for details.
5377 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005378 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005379 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005380 struct cgroup_subsys_state *old_css = cset->subsys[i];
5381 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005382
Tejun Heoeb954192013-08-08 20:11:23 -04005383 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005384 }
5385 }
5386 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005387 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005388
Tejun Heo5abb8852013-06-12 21:04:49 -07005389 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005390}
Paul Menage697f4162007-10-18 23:39:34 -07005391
Paul Menagebd89aab2007-10-18 23:40:44 -07005392static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005393{
Li Zefanf50daa72013-03-01 15:06:07 +08005394 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005395 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005396 /*
5397 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005398 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005399 * it now
5400 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005401 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005402
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005403 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005404 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005405 list_empty(&cgrp->release_list)) {
5406 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407 need_schedule_work = 1;
5408 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005409 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005410 if (need_schedule_work)
5411 schedule_work(&release_agent_work);
5412 }
5413}
5414
Paul Menage81a6a5c2007-10-18 23:39:38 -07005415/*
5416 * Notify userspace when a cgroup is released, by running the
5417 * configured release agent with the name of the cgroup (path
5418 * relative to the root of cgroup file system) as the argument.
5419 *
5420 * Most likely, this user command will try to rmdir this cgroup.
5421 *
5422 * This races with the possibility that some other task will be
5423 * attached to this cgroup before it is removed, or that some other
5424 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5425 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5426 * unused, and this cgroup will be reprieved from its death sentence,
5427 * to continue to serve a useful existence. Next time it's released,
5428 * we will get notified again, if it still has 'notify_on_release' set.
5429 *
5430 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5431 * means only wait until the task is successfully execve()'d. The
5432 * separate release agent task is forked by call_usermodehelper(),
5433 * then control in this thread returns here, without waiting for the
5434 * release agent task. We don't bother to wait because the caller of
5435 * this routine has no use for the exit status of the release agent
5436 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005437 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005438static void cgroup_release_agent(struct work_struct *work)
5439{
5440 BUG_ON(work != &release_agent_work);
5441 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005442 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005443 while (!list_empty(&release_list)) {
5444 char *argv[3], *envp[3];
5445 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005446 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005447 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005448 struct cgroup,
5449 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005450 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005451 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005452 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005453 if (!pathbuf)
5454 goto continue_free;
5455 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5456 goto continue_free;
5457 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5458 if (!agentbuf)
5459 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005460
5461 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005462 argv[i++] = agentbuf;
5463 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005464 argv[i] = NULL;
5465
5466 i = 0;
5467 /* minimal command environment */
5468 envp[i++] = "HOME=/";
5469 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5470 envp[i] = NULL;
5471
5472 /* Drop the lock while we invoke the usermode helper,
5473 * since the exec could involve hitting disk and hence
5474 * be a slow process */
5475 mutex_unlock(&cgroup_mutex);
5476 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005477 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005478 continue_free:
5479 kfree(pathbuf);
5480 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005481 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005482 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005483 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005484 mutex_unlock(&cgroup_mutex);
5485}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005486
5487static int __init cgroup_disable(char *str)
5488{
Tejun Heo30159ec2013-06-25 11:53:37 -07005489 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005490 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005491 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005492
5493 while ((token = strsep(&str, ",")) != NULL) {
5494 if (!*token)
5495 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005496
Tejun Heo30159ec2013-06-25 11:53:37 -07005497 /*
5498 * cgroup_disable, being at boot time, can't know about
5499 * module subsystems, so we don't worry about them.
5500 */
5501 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005502 if (!strcmp(token, ss->name)) {
5503 ss->disabled = 1;
5504 printk(KERN_INFO "Disabling %s control group"
5505 " subsystem\n", ss->name);
5506 break;
5507 }
5508 }
5509 }
5510 return 1;
5511}
5512__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005513
5514/*
5515 * Functons for CSS ID.
5516 */
5517
Tejun Heo54766d42013-06-12 21:04:53 -07005518/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005519unsigned short css_id(struct cgroup_subsys_state *css)
5520{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005521 struct css_id *cssid;
5522
5523 /*
5524 * This css_id() can return correct value when somone has refcnt
5525 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5526 * it's unchanged until freed.
5527 */
Tejun Heod3daf282013-06-13 19:39:16 -07005528 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005529
5530 if (cssid)
5531 return cssid->id;
5532 return 0;
5533}
Ben Blum67523c42010-03-10 15:22:11 -08005534EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005535
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005536/**
5537 * css_is_ancestor - test "root" css is an ancestor of "child"
5538 * @child: the css to be tested.
5539 * @root: the css supporsed to be an ancestor of the child.
5540 *
5541 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005542 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005543 * But, considering usual usage, the csses should be valid objects after test.
5544 * Assuming that the caller will do some action to the child if this returns
5545 * returns true, the caller must take "child";s reference count.
5546 * If "child" is valid object and this returns true, "root" is valid, too.
5547 */
5548
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005549bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005550 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005551{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005552 struct css_id *child_id;
5553 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005554
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005555 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005556 if (!child_id)
5557 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005558 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005559 if (!root_id)
5560 return false;
5561 if (child_id->depth < root_id->depth)
5562 return false;
5563 if (child_id->stack[root_id->depth] != root_id->id)
5564 return false;
5565 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005566}
5567
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005568void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5569{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005570 struct css_id *id = rcu_dereference_protected(css->id, true);
5571
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005572 /* When this is called before css_id initialization, id can be NULL */
5573 if (!id)
5574 return;
5575
5576 BUG_ON(!ss->use_id);
5577
5578 rcu_assign_pointer(id->css, NULL);
5579 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005580 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005581 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005582 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005583 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005584}
Ben Blum67523c42010-03-10 15:22:11 -08005585EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005586
5587/*
5588 * This is called by init or create(). Then, calls to this function are
5589 * always serialized (By cgroup_mutex() at create()).
5590 */
5591
5592static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5593{
5594 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005595 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005596
5597 BUG_ON(!ss->use_id);
5598
5599 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5600 newid = kzalloc(size, GFP_KERNEL);
5601 if (!newid)
5602 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005603
5604 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005605 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005606 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005607 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005608 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005609 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005610
5611 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005612 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005613 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005614
Tejun Heod228d9e2013-02-27 17:04:54 -08005615 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005616 newid->depth = depth;
5617 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005618err_out:
5619 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005620 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005621
5622}
5623
Ben Blume6a11052010-03-10 15:22:09 -08005624static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5625 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005626{
5627 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005628
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005629 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005630 idr_init(&ss->idr);
5631
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005632 newid = get_new_cssid(ss, 0);
5633 if (IS_ERR(newid))
5634 return PTR_ERR(newid);
5635
5636 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005637 RCU_INIT_POINTER(newid->css, rootcss);
5638 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005639 return 0;
5640}
5641
Tejun Heo623f9262013-08-13 11:01:55 -04005642static int alloc_css_id(struct cgroup_subsys_state *child_css)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005643{
Tejun Heo623f9262013-08-13 11:01:55 -04005644 struct cgroup_subsys_state *parent_css = css_parent(child_css);
Li Zefanfae9c792010-04-22 17:30:00 +08005645 struct css_id *child_id, *parent_id;
Tejun Heo623f9262013-08-13 11:01:55 -04005646 int i, depth;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005647
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005648 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005649 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005650
Tejun Heo623f9262013-08-13 11:01:55 -04005651 child_id = get_new_cssid(child_css->ss, depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005652 if (IS_ERR(child_id))
5653 return PTR_ERR(child_id);
5654
5655 for (i = 0; i < depth; i++)
5656 child_id->stack[i] = parent_id->stack[i];
5657 child_id->stack[depth] = child_id->id;
5658 /*
5659 * child_id->css pointer will be set after this cgroup is available
5660 * see cgroup_populate_dir()
5661 */
5662 rcu_assign_pointer(child_css->id, child_id);
5663
5664 return 0;
5665}
5666
5667/**
5668 * css_lookup - lookup css by id
5669 * @ss: cgroup subsys to be looked into.
5670 * @id: the id
5671 *
5672 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5673 * NULL if not. Should be called under rcu_read_lock()
5674 */
5675struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5676{
5677 struct css_id *cssid = NULL;
5678
5679 BUG_ON(!ss->use_id);
5680 cssid = idr_find(&ss->idr, id);
5681
5682 if (unlikely(!cssid))
5683 return NULL;
5684
5685 return rcu_dereference(cssid->css);
5686}
Ben Blum67523c42010-03-10 15:22:11 -08005687EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005688
Tejun Heob77d7b62013-08-13 11:01:54 -04005689/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005690 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5691 * @dentry: directory dentry of interest
5692 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005693 *
5694 * Must be called under RCU read lock. The caller is responsible for
5695 * pinning the returned css if it needs to be accessed outside the RCU
5696 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005697 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005698struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5699 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005700{
5701 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005702
Tejun Heob77d7b62013-08-13 11:01:54 -04005703 WARN_ON_ONCE(!rcu_read_lock_held());
5704
Tejun Heo35cf0832013-08-26 18:40:56 -04005705 /* is @dentry a cgroup dir? */
5706 if (!dentry->d_inode ||
5707 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005708 return ERR_PTR(-EBADF);
5709
Tejun Heo35cf0832013-08-26 18:40:56 -04005710 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005711 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005712}
5713
Li Zefan1cb650b2013-08-19 10:05:24 +08005714/**
5715 * css_from_id - lookup css by id
5716 * @id: the cgroup id
5717 * @ss: cgroup subsys to be looked into
5718 *
5719 * Returns the css if there's valid one with @id, otherwise returns NULL.
5720 * Should be called under rcu_read_lock().
5721 */
5722struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5723{
5724 struct cgroup *cgrp;
5725
5726 rcu_lockdep_assert(rcu_read_lock_held() ||
5727 lockdep_is_held(&cgroup_mutex),
5728 "css_from_id() needs proper protection");
5729
5730 cgrp = idr_find(&ss->root->cgroup_idr, id);
5731 if (cgrp)
5732 return cgroup_css(cgrp, ss->subsys_id);
5733 return NULL;
5734}
5735
Paul Menagefe693432009-09-23 15:56:20 -07005736#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005737static struct cgroup_subsys_state *
5738debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005739{
5740 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5741
5742 if (!css)
5743 return ERR_PTR(-ENOMEM);
5744
5745 return css;
5746}
5747
Tejun Heoeb954192013-08-08 20:11:23 -04005748static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005749{
Tejun Heoeb954192013-08-08 20:11:23 -04005750 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005751}
5752
Tejun Heo182446d2013-08-08 20:11:24 -04005753static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5754 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005755{
Tejun Heo182446d2013-08-08 20:11:24 -04005756 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005757}
5758
Tejun Heo182446d2013-08-08 20:11:24 -04005759static u64 current_css_set_read(struct cgroup_subsys_state *css,
5760 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005761{
5762 return (u64)(unsigned long)current->cgroups;
5763}
5764
Tejun Heo182446d2013-08-08 20:11:24 -04005765static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005766 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005767{
5768 u64 count;
5769
5770 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005771 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005772 rcu_read_unlock();
5773 return count;
5774}
5775
Tejun Heo182446d2013-08-08 20:11:24 -04005776static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005777 struct cftype *cft,
5778 struct seq_file *seq)
5779{
Tejun Heo69d02062013-06-12 21:04:50 -07005780 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005781 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005782
5783 read_lock(&css_set_lock);
5784 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005785 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005786 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005787 struct cgroup *c = link->cgrp;
5788 const char *name;
5789
5790 if (c->dentry)
5791 name = c->dentry->d_name.name;
5792 else
5793 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005794 seq_printf(seq, "Root %d group %s\n",
5795 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005796 }
5797 rcu_read_unlock();
5798 read_unlock(&css_set_lock);
5799 return 0;
5800}
5801
5802#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005803static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5804 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005805{
Tejun Heo69d02062013-06-12 21:04:50 -07005806 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005807
5808 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005809 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005810 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005811 struct task_struct *task;
5812 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005813 seq_printf(seq, "css_set %p\n", cset);
5814 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005815 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5816 seq_puts(seq, " ...\n");
5817 break;
5818 } else {
5819 seq_printf(seq, " task %d\n",
5820 task_pid_vnr(task));
5821 }
5822 }
5823 }
5824 read_unlock(&css_set_lock);
5825 return 0;
5826}
5827
Tejun Heo182446d2013-08-08 20:11:24 -04005828static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005829{
Tejun Heo182446d2013-08-08 20:11:24 -04005830 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005831}
5832
5833static struct cftype debug_files[] = {
5834 {
Paul Menagefe693432009-09-23 15:56:20 -07005835 .name = "taskcount",
5836 .read_u64 = debug_taskcount_read,
5837 },
5838
5839 {
5840 .name = "current_css_set",
5841 .read_u64 = current_css_set_read,
5842 },
5843
5844 {
5845 .name = "current_css_set_refcount",
5846 .read_u64 = current_css_set_refcount_read,
5847 },
5848
5849 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005850 .name = "current_css_set_cg_links",
5851 .read_seq_string = current_css_set_cg_links_read,
5852 },
5853
5854 {
5855 .name = "cgroup_css_links",
5856 .read_seq_string = cgroup_css_links_read,
5857 },
5858
5859 {
Paul Menagefe693432009-09-23 15:56:20 -07005860 .name = "releasable",
5861 .read_u64 = releasable_read,
5862 },
Paul Menagefe693432009-09-23 15:56:20 -07005863
Tejun Heo4baf6e32012-04-01 12:09:55 -07005864 { } /* terminate */
5865};
Paul Menagefe693432009-09-23 15:56:20 -07005866
5867struct cgroup_subsys debug_subsys = {
5868 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005869 .css_alloc = debug_css_alloc,
5870 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005871 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005872 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005873};
5874#endif /* CONFIG_CGROUP_DEBUG */