blob: 2418b6e71a854e187573ec12746481ce863e721a [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>
Al Viro4e10f3c2013-08-30 12:29:49 -040063#include <linux/file.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070064
Arun Sharma600634972011-07-26 16:09:06 -070065#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070066
Tejun Heoe25e2cb2011-12-12 18:12:21 -080067/*
68 * cgroup_mutex is the master lock. Any modification to cgroup or its
69 * hierarchy must be performed while holding it.
70 *
71 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
72 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
73 * release_agent_path and so on. Modifying requires both cgroup_mutex and
74 * cgroup_root_mutex. Readers can acquire either of the two. This is to
75 * break the following locking order cycle.
76 *
77 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
78 * B. namespace_sem -> cgroup_mutex
79 *
80 * B happens only through cgroup_show_options() and using cgroup_root_mutex
81 * breaks it.
82 */
Tejun Heo22194492013-04-07 09:29:51 -070083#ifdef CONFIG_PROVE_RCU
84DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040085EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070086#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070087static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070088#endif
89
Tejun Heoe25e2cb2011-12-12 18:12:21 -080090static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070091
Ben Blumaae8aab2010-03-10 15:22:07 -080092/*
93 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020094 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080095 * registered after that. The mutable section of this array is protected by
96 * cgroup_mutex.
97 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020098#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020099#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -0700100static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700101#include <linux/cgroup_subsys.h>
102};
103
Paul Menageddbcc7e2007-10-18 23:39:30 -0700104/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700105 * The dummy hierarchy, reserved for the subsystems that are otherwise
106 * unattached - it never has more than a single cgroup, and all tasks are
107 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700108 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700109static struct cgroupfs_root cgroup_dummy_root;
110
111/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
112static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700113
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700114/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700115 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
116 */
117struct cfent {
118 struct list_head node;
119 struct dentry *dentry;
120 struct cftype *type;
Tejun Heo105347b2013-08-13 11:01:55 -0400121 struct cgroup_subsys_state *css;
Li Zefan712317a2013-04-18 23:09:52 -0700122
123 /* file xattrs */
124 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700125};
126
127/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700128 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
129 * cgroup_subsys->use_id != 0.
130 */
131#define CSS_ID_MAX (65535)
132struct css_id {
133 /*
134 * The css to which this ID points. This pointer is set to valid value
135 * after cgroup is populated. If cgroup is removed, this will be NULL.
136 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800137 * is called after synchronize_rcu(). But for safe use, css_tryget()
138 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100140 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700141 /*
142 * ID of this css.
143 */
144 unsigned short id;
145 /*
146 * Depth in hierarchy which this ID belongs to.
147 */
148 unsigned short depth;
149 /*
150 * ID is freed by RCU. (and lookup routine is RCU safe.)
151 */
152 struct rcu_head rcu_head;
153 /*
154 * Hierarchy of CSS ID belongs to.
155 */
156 unsigned short stack[0]; /* Array of Length (depth+1) */
157};
158
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300160 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800161 */
162struct cgroup_event {
163 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400164 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800165 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400166 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800167 /*
168 * Control file which the event associated.
169 */
170 struct cftype *cft;
171 /*
172 * eventfd to signal userspace about the event.
173 */
174 struct eventfd_ctx *eventfd;
175 /*
176 * Each of these stored in a list by the cgroup.
177 */
178 struct list_head list;
179 /*
180 * All fields below needed to unregister event when
181 * userspace closes eventfd.
182 */
183 poll_table pt;
184 wait_queue_head_t *wqh;
185 wait_queue_t wait;
186 struct work_struct remove;
187};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700188
Paul Menageddbcc7e2007-10-18 23:39:30 -0700189/* The list of hierarchy roots */
190
Tejun Heo9871bf92013-06-24 15:21:47 -0700191static LIST_HEAD(cgroup_roots);
192static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700193
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700194/*
195 * Hierarchy ID allocation and mapping. It follows the same exclusion
196 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
197 * writes, either for reads.
198 */
Tejun Heo1a574232013-04-14 11:36:58 -0700199static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700200
Li Zefan65dff752013-03-01 15:01:56 +0800201static struct cgroup_name root_cgroup_name = { .name = "/" };
202
Li Zefan794611a2013-06-18 18:53:53 +0800203/*
204 * Assign a monotonically increasing serial number to cgroups. It
205 * guarantees cgroups with bigger numbers are newer than those with smaller
206 * numbers. Also, as cgroups are always appended to the parent's
207 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700208 * the ascending serial number order on the list. Protected by
209 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800210 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700211static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800212
Paul Menageddbcc7e2007-10-18 23:39:30 -0700213/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800214 * check for fork/exit handlers to call. This avoids us having to do
215 * extra work in the fork/exit path if none of the subsystems need to
216 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700218static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700219
Tejun Heo628f7cd2013-06-28 16:24:11 -0700220static struct cftype cgroup_base_files[];
221
Tejun Heof20104d2013-08-13 20:22:50 -0400222static void cgroup_destroy_css_killed(struct cgroup *cgrp);
Tejun Heo42809dd2012-11-19 08:13:37 -0800223static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400224static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
225 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800226
Tejun Heo95109b62013-08-08 20:11:27 -0400227/**
228 * cgroup_css - obtain a cgroup's css for the specified subsystem
229 * @cgrp: the cgroup of interest
Tejun Heoca8bdca2013-08-26 18:40:56 -0400230 * @ss: the subsystem of interest (%NULL returns the dummy_css)
Tejun Heo95109b62013-08-08 20:11:27 -0400231 *
Tejun Heoca8bdca2013-08-26 18:40:56 -0400232 * Return @cgrp's css (cgroup_subsys_state) associated with @ss. This
233 * function must be called either under cgroup_mutex or rcu_read_lock() and
234 * the caller is responsible for pinning the returned css if it wants to
235 * keep accessing it outside the said locks. This function may return
236 * %NULL if @cgrp doesn't have @subsys_id enabled.
Tejun Heo95109b62013-08-08 20:11:27 -0400237 */
238static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
Tejun Heoca8bdca2013-08-26 18:40:56 -0400239 struct cgroup_subsys *ss)
Tejun Heo95109b62013-08-08 20:11:27 -0400240{
Tejun Heoca8bdca2013-08-26 18:40:56 -0400241 if (ss)
242 return rcu_dereference_check(cgrp->subsys[ss->subsys_id],
243 lockdep_is_held(&cgroup_mutex));
244 else
245 return &cgrp->dummy_css;
Tejun Heo95109b62013-08-08 20:11:27 -0400246}
Paul Menageddbcc7e2007-10-18 23:39:30 -0700247
Paul Menageddbcc7e2007-10-18 23:39:30 -0700248/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700249static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700250{
Tejun Heo54766d42013-06-12 21:04:53 -0700251 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700252}
253
Li Zefan78574cf2013-04-08 19:00:38 -0700254/**
255 * cgroup_is_descendant - test ancestry
256 * @cgrp: the cgroup to be tested
257 * @ancestor: possible ancestor of @cgrp
258 *
259 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
260 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
261 * and @ancestor are accessible.
262 */
263bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
264{
265 while (cgrp) {
266 if (cgrp == ancestor)
267 return true;
268 cgrp = cgrp->parent;
269 }
270 return false;
271}
272EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700273
Adrian Bunke9685a02008-02-07 00:13:46 -0800274static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700275{
276 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700277 (1 << CGRP_RELEASABLE) |
278 (1 << CGRP_NOTIFY_ON_RELEASE);
279 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700280}
281
Adrian Bunke9685a02008-02-07 00:13:46 -0800282static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700283{
Paul Menagebd89aab2007-10-18 23:40:44 -0700284 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700285}
286
Tejun Heo30159ec2013-06-25 11:53:37 -0700287/**
288 * for_each_subsys - iterate all loaded cgroup subsystems
289 * @ss: the iteration cursor
290 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
291 *
292 * Should be called under cgroup_mutex.
293 */
294#define for_each_subsys(ss, i) \
295 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
296 if (({ lockdep_assert_held(&cgroup_mutex); \
297 !((ss) = cgroup_subsys[i]); })) { } \
298 else
299
300/**
301 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
302 * @ss: the iteration cursor
303 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
304 *
305 * Bulit-in subsystems are always present and iteration itself doesn't
306 * require any synchronization.
307 */
308#define for_each_builtin_subsys(ss, i) \
309 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
310 (((ss) = cgroup_subsys[i]) || true); (i)++)
311
Tejun Heo5549c492013-06-24 15:21:48 -0700312/* iterate each subsystem attached to a hierarchy */
313#define for_each_root_subsys(root, ss) \
314 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700315
Tejun Heo5549c492013-06-24 15:21:48 -0700316/* iterate across the active hierarchies */
317#define for_each_active_root(root) \
318 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700319
Tejun Heof6ea9372012-04-01 12:09:55 -0700320static inline struct cgroup *__d_cgrp(struct dentry *dentry)
321{
322 return dentry->d_fsdata;
323}
324
Tejun Heo05ef1d72012-04-01 12:09:56 -0700325static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700326{
327 return dentry->d_fsdata;
328}
329
Tejun Heo05ef1d72012-04-01 12:09:56 -0700330static inline struct cftype *__d_cft(struct dentry *dentry)
331{
332 return __d_cfe(dentry)->type;
333}
334
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700335/**
336 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
337 * @cgrp: the cgroup to be checked for liveness
338 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700339 * On success, returns true; the mutex should be later unlocked. On
340 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700341 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700342static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700343{
344 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700345 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700346 mutex_unlock(&cgroup_mutex);
347 return false;
348 }
349 return true;
350}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700351
Paul Menage81a6a5c2007-10-18 23:39:38 -0700352/* the list of cgroups eligible for automatic release. Protected by
353 * release_list_lock */
354static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200355static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700356static void cgroup_release_agent(struct work_struct *work);
357static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700358static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700359
Tejun Heo69d02062013-06-12 21:04:50 -0700360/*
361 * A cgroup can be associated with multiple css_sets as different tasks may
362 * belong to different cgroups on different hierarchies. In the other
363 * direction, a css_set is naturally associated with multiple cgroups.
364 * This M:N relationship is represented by the following link structure
365 * which exists for each association and allows traversing the associations
366 * from both sides.
367 */
368struct cgrp_cset_link {
369 /* the cgroup and css_set this link associates */
370 struct cgroup *cgrp;
371 struct css_set *cset;
372
373 /* list of cgrp_cset_links anchored at cgrp->cset_links */
374 struct list_head cset_link;
375
376 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
377 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700378};
379
380/* The default css_set - used by init and its children prior to any
381 * hierarchies being mounted. It contains a pointer to the root state
382 * for each subsystem. Also used to anchor the list of css_sets. Not
383 * reference-counted, to improve performance when child cgroups
384 * haven't been created.
385 */
386
387static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700388static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700389
Ben Blume6a11052010-03-10 15:22:09 -0800390static int cgroup_init_idr(struct cgroup_subsys *ss,
391 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700392
Tejun Heo0942eee2013-08-08 20:11:26 -0400393/*
394 * css_set_lock protects the list of css_set objects, and the chain of
395 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400396 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400397 */
Paul Menage817929e2007-10-18 23:39:36 -0700398static DEFINE_RWLOCK(css_set_lock);
399static int css_set_count;
400
Paul Menage7717f7b2009-09-23 15:56:22 -0700401/*
402 * hash table for cgroup groups. This improves the performance to find
403 * an existing css_set. This hash doesn't (currently) take into
404 * account cgroups in empty hierarchies.
405 */
Li Zefan472b1052008-04-29 01:00:11 -0700406#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800407static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700408
Li Zefan0ac801f2013-01-10 11:49:27 +0800409static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700410{
Li Zefan0ac801f2013-01-10 11:49:27 +0800411 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700412 struct cgroup_subsys *ss;
413 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700414
Tejun Heo30159ec2013-06-25 11:53:37 -0700415 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800416 key += (unsigned long)css[i];
417 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700418
Li Zefan0ac801f2013-01-10 11:49:27 +0800419 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700420}
421
Tejun Heo0942eee2013-08-08 20:11:26 -0400422/*
423 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400424 * until after the first call to css_task_iter_start(). This reduces the
425 * fork()/exit() overhead for people who have cgroups compiled into their
426 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400427 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700428static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700429
Tejun Heo5abb8852013-06-12 21:04:49 -0700430static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700431{
Tejun Heo69d02062013-06-12 21:04:50 -0700432 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700433
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700434 /*
435 * Ensure that the refcount doesn't hit zero while any readers
436 * can see it. Similar to atomic_dec_and_lock(), but for an
437 * rwlock
438 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700439 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700440 return;
441 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700442 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700443 write_unlock(&css_set_lock);
444 return;
445 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700446
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700447 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700448 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700449 css_set_count--;
450
Tejun Heo69d02062013-06-12 21:04:50 -0700451 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700452 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700453
Tejun Heo69d02062013-06-12 21:04:50 -0700454 list_del(&link->cset_link);
455 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800456
Tejun Heoddd69142013-06-12 21:04:54 -0700457 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700458 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700459 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700460 set_bit(CGRP_RELEASABLE, &cgrp->flags);
461 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700462 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700463
464 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700465 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700466
467 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700468 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700469}
470
471/*
472 * refcounted get/put for css_set objects
473 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700474static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700475{
Tejun Heo5abb8852013-06-12 21:04:49 -0700476 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700477}
478
Tejun Heo5abb8852013-06-12 21:04:49 -0700479static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700480{
Tejun Heo5abb8852013-06-12 21:04:49 -0700481 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700482}
483
Tejun Heo5abb8852013-06-12 21:04:49 -0700484static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700485{
Tejun Heo5abb8852013-06-12 21:04:49 -0700486 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700487}
488
Tejun Heob326f9d2013-06-24 15:21:48 -0700489/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700490 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700491 * @cset: candidate css_set being tested
492 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700493 * @new_cgrp: cgroup that's being entered by the task
494 * @template: desired set of css pointers in css_set (pre-calculated)
495 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800496 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
498 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700499static bool compare_css_sets(struct css_set *cset,
500 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700501 struct cgroup *new_cgrp,
502 struct cgroup_subsys_state *template[])
503{
504 struct list_head *l1, *l2;
505
Tejun Heo5abb8852013-06-12 21:04:49 -0700506 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700507 /* Not all subsystems matched */
508 return false;
509 }
510
511 /*
512 * Compare cgroup pointers in order to distinguish between
513 * different cgroups in heirarchies with no subsystems. We
514 * could get by with just this check alone (and skip the
515 * memcmp above) but on most setups the memcmp check will
516 * avoid the need for this more expensive check on almost all
517 * candidates.
518 */
519
Tejun Heo69d02062013-06-12 21:04:50 -0700520 l1 = &cset->cgrp_links;
521 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700522 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700523 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700524 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700525
526 l1 = l1->next;
527 l2 = l2->next;
528 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700529 if (l1 == &cset->cgrp_links) {
530 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700531 break;
532 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700533 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700534 }
535 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700536 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
537 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
538 cgrp1 = link1->cgrp;
539 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700540 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700541 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700542
543 /*
544 * If this hierarchy is the hierarchy of the cgroup
545 * that's changing, then we need to check that this
546 * css_set points to the new cgroup; if it's any other
547 * hierarchy, then this css_set should point to the
548 * same cgroup as the old css_set.
549 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700550 if (cgrp1->root == new_cgrp->root) {
551 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700552 return false;
553 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700554 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700555 return false;
556 }
557 }
558 return true;
559}
560
Tejun Heob326f9d2013-06-24 15:21:48 -0700561/**
562 * find_existing_css_set - init css array and find the matching css_set
563 * @old_cset: the css_set that we're using before the cgroup transition
564 * @cgrp: the cgroup that we're moving into
565 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700566 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700567static struct css_set *find_existing_css_set(struct css_set *old_cset,
568 struct cgroup *cgrp,
569 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700570{
Paul Menagebd89aab2007-10-18 23:40:44 -0700571 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700572 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700573 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800574 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700575 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700576
Ben Blumaae8aab2010-03-10 15:22:07 -0800577 /*
578 * Build the set of subsystem state objects that we want to see in the
579 * new css_set. while subsystems can change globally, the entries here
580 * won't change, so no need for locking.
581 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700582 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400583 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700584 /* Subsystem is in this hierarchy. So we want
585 * the subsystem state from the new
586 * cgroup */
Tejun Heoca8bdca2013-08-26 18:40:56 -0400587 template[i] = cgroup_css(cgrp, ss);
Paul Menage817929e2007-10-18 23:39:36 -0700588 } else {
589 /* Subsystem is not in this hierarchy, so we
590 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700591 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700592 }
593 }
594
Li Zefan0ac801f2013-01-10 11:49:27 +0800595 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700596 hash_for_each_possible(css_set_table, cset, hlist, key) {
597 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700598 continue;
599
600 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700601 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700602 }
Paul Menage817929e2007-10-18 23:39:36 -0700603
604 /* No existing cgroup group matched */
605 return NULL;
606}
607
Tejun Heo69d02062013-06-12 21:04:50 -0700608static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700609{
Tejun Heo69d02062013-06-12 21:04:50 -0700610 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700611
Tejun Heo69d02062013-06-12 21:04:50 -0700612 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
613 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700614 kfree(link);
615 }
616}
617
Tejun Heo69d02062013-06-12 21:04:50 -0700618/**
619 * allocate_cgrp_cset_links - allocate cgrp_cset_links
620 * @count: the number of links to allocate
621 * @tmp_links: list_head the allocated links are put on
622 *
623 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
624 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700625 */
Tejun Heo69d02062013-06-12 21:04:50 -0700626static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700627{
Tejun Heo69d02062013-06-12 21:04:50 -0700628 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700629 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700630
631 INIT_LIST_HEAD(tmp_links);
632
Li Zefan36553432008-07-29 22:33:19 -0700633 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700634 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700635 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700636 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700637 return -ENOMEM;
638 }
Tejun Heo69d02062013-06-12 21:04:50 -0700639 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700640 }
641 return 0;
642}
643
Li Zefanc12f65d2009-01-07 18:07:42 -0800644/**
645 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700646 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700647 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800648 * @cgrp: the destination cgroup
649 */
Tejun Heo69d02062013-06-12 21:04:50 -0700650static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
651 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800652{
Tejun Heo69d02062013-06-12 21:04:50 -0700653 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800654
Tejun Heo69d02062013-06-12 21:04:50 -0700655 BUG_ON(list_empty(tmp_links));
656 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
657 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700658 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700659 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700660 /*
661 * Always add links to the tail of the list so that the list
662 * is sorted by order of hierarchy creation
663 */
Tejun Heo69d02062013-06-12 21:04:50 -0700664 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800665}
666
Tejun Heob326f9d2013-06-24 15:21:48 -0700667/**
668 * find_css_set - return a new css_set with one cgroup updated
669 * @old_cset: the baseline css_set
670 * @cgrp: the cgroup to be updated
671 *
672 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
673 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700674 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700675static struct css_set *find_css_set(struct css_set *old_cset,
676 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700677{
Tejun Heob326f9d2013-06-24 15:21:48 -0700678 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700679 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700680 struct list_head tmp_links;
681 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800682 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700683
Tejun Heob326f9d2013-06-24 15:21:48 -0700684 lockdep_assert_held(&cgroup_mutex);
685
Paul Menage817929e2007-10-18 23:39:36 -0700686 /* First see if we already have a cgroup group that matches
687 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700688 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700689 cset = find_existing_css_set(old_cset, cgrp, template);
690 if (cset)
691 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700692 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700693
Tejun Heo5abb8852013-06-12 21:04:49 -0700694 if (cset)
695 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700696
Tejun Heof4f4be22013-06-12 21:04:51 -0700697 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700698 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700699 return NULL;
700
Tejun Heo69d02062013-06-12 21:04:50 -0700701 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700702 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700703 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700704 return NULL;
705 }
706
Tejun Heo5abb8852013-06-12 21:04:49 -0700707 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700708 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700709 INIT_LIST_HEAD(&cset->tasks);
710 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700711
712 /* Copy the set of subsystem state objects generated in
713 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700714 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700715
716 write_lock(&css_set_lock);
717 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700718 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700719 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700720
Paul Menage7717f7b2009-09-23 15:56:22 -0700721 if (c->root == cgrp->root)
722 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700723 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700724 }
Paul Menage817929e2007-10-18 23:39:36 -0700725
Tejun Heo69d02062013-06-12 21:04:50 -0700726 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700727
Paul Menage817929e2007-10-18 23:39:36 -0700728 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700729
730 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700731 key = css_set_hash(cset->subsys);
732 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700733
Paul Menage817929e2007-10-18 23:39:36 -0700734 write_unlock(&css_set_lock);
735
Tejun Heo5abb8852013-06-12 21:04:49 -0700736 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700737}
738
Paul Menageddbcc7e2007-10-18 23:39:30 -0700739/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700740 * Return the cgroup for "task" from the given hierarchy. Must be
741 * called with cgroup_mutex held.
742 */
743static struct cgroup *task_cgroup_from_root(struct task_struct *task,
744 struct cgroupfs_root *root)
745{
Tejun Heo5abb8852013-06-12 21:04:49 -0700746 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700747 struct cgroup *res = NULL;
748
749 BUG_ON(!mutex_is_locked(&cgroup_mutex));
750 read_lock(&css_set_lock);
751 /*
752 * No need to lock the task - since we hold cgroup_mutex the
753 * task can't change groups, so the only thing that can happen
754 * is that it exits and its css is set back to init_css_set.
755 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700756 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700757 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700758 res = &root->top_cgroup;
759 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700760 struct cgrp_cset_link *link;
761
762 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700763 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700764
Paul Menage7717f7b2009-09-23 15:56:22 -0700765 if (c->root == root) {
766 res = c;
767 break;
768 }
769 }
770 }
771 read_unlock(&css_set_lock);
772 BUG_ON(!res);
773 return res;
774}
775
776/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777 * There is one global cgroup mutex. We also require taking
778 * task_lock() when dereferencing a task's cgroup subsys pointers.
779 * See "The task_lock() exception", at the end of this comment.
780 *
781 * A task must hold cgroup_mutex to modify cgroups.
782 *
783 * Any task can increment and decrement the count field without lock.
784 * So in general, code holding cgroup_mutex can't rely on the count
785 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800786 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700787 * means that no tasks are currently attached, therefore there is no
788 * way a task attached to that cgroup can fork (the other way to
789 * increment the count). So code holding cgroup_mutex can safely
790 * assume that if the count is zero, it will stay zero. Similarly, if
791 * a task holds cgroup_mutex on a cgroup with zero count, it
792 * knows that the cgroup won't be removed, as cgroup_rmdir()
793 * needs that mutex.
794 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700795 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
796 * (usually) take cgroup_mutex. These are the two most performance
797 * critical pieces of code here. The exception occurs on cgroup_exit(),
798 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
799 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800800 * to the release agent with the name of the cgroup (path relative to
801 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700802 *
803 * A cgroup can only be deleted if both its 'count' of using tasks
804 * is zero, and its list of 'children' cgroups is empty. Since all
805 * tasks in the system use _some_ cgroup, and since there is always at
806 * least one task in the system (init, pid == 1), therefore, top_cgroup
807 * always has either children cgroups and/or using tasks. So we don't
808 * need a special hack to ensure that top_cgroup cannot be deleted.
809 *
810 * The task_lock() exception
811 *
812 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800813 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800814 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700815 * several performance critical places that need to reference
816 * task->cgroup without the expense of grabbing a system global
817 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800818 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700819 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
820 * the task_struct routinely used for such matters.
821 *
822 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800823 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700824 */
825
Paul Menageddbcc7e2007-10-18 23:39:30 -0700826/*
827 * A couple of forward declarations required, due to cyclic reference loop:
828 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
829 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
830 * -> cgroup_mkdir.
831 */
832
Al Viro18bb1db2011-07-26 01:41:39 -0400833static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
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;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001045 }
1046
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001047 /* 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 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001052
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;
Li Zefan0670e082009-04-02 16:57:30 -07001353 goto out_unlock;
Ben Blumcf5d5942010-03-10 15:22:09 -08001354 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001355
Paul Menageddbcc7e2007-10-18 23:39:30 -07001356 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001357 if (ret)
Paul Menageddbcc7e2007-10-18 23:39: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
2426 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;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002503
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 Viro786e1442013-07-14 17:50:23 +04002653 .lookup = simple_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
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002663/*
2664 * Check if a file is a control file
2665 */
2666static inline struct cftype *__file_cft(struct file *file)
2667{
Al Viro496ad9a2013-01-23 17:07:38 -05002668 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002669 return ERR_PTR(-EINVAL);
2670 return __d_cft(file->f_dentry);
2671}
2672
Al Viroa5e7ed32011-07-26 01:55:55 -04002673static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002674 struct super_block *sb)
2675{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002676 struct inode *inode;
2677
2678 if (!dentry)
2679 return -ENOENT;
2680 if (dentry->d_inode)
2681 return -EEXIST;
2682
2683 inode = cgroup_new_inode(mode, sb);
2684 if (!inode)
2685 return -ENOMEM;
2686
2687 if (S_ISDIR(mode)) {
2688 inode->i_op = &cgroup_dir_inode_operations;
2689 inode->i_fop = &simple_dir_operations;
2690
2691 /* start off with i_nlink == 2 (for "." entry) */
2692 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002693 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002694
Tejun Heob8a2df62012-11-19 08:13:37 -08002695 /*
2696 * Control reaches here with cgroup_mutex held.
2697 * @inode->i_mutex should nest outside cgroup_mutex but we
2698 * want to populate it immediately without releasing
2699 * cgroup_mutex. As @inode isn't visible to anyone else
2700 * yet, trylock will always succeed without affecting
2701 * lockdep checks.
2702 */
2703 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002704 } else if (S_ISREG(mode)) {
2705 inode->i_size = 0;
2706 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002707 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002708 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 d_instantiate(dentry, inode);
2710 dget(dentry); /* Extra count - pin the dentry in core */
2711 return 0;
2712}
2713
Li Zefan099fca32009-04-02 16:57:29 -07002714/**
2715 * cgroup_file_mode - deduce file mode of a control file
2716 * @cft: the control file in question
2717 *
2718 * returns cft->mode if ->mode is not 0
2719 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2720 * returns S_IRUGO if it has only a read handler
2721 * returns S_IWUSR if it has only a write hander
2722 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002723static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002724{
Al Viroa5e7ed32011-07-26 01:55:55 -04002725 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002726
2727 if (cft->mode)
2728 return cft->mode;
2729
2730 if (cft->read || cft->read_u64 || cft->read_s64 ||
2731 cft->read_map || cft->read_seq_string)
2732 mode |= S_IRUGO;
2733
2734 if (cft->write || cft->write_u64 || cft->write_s64 ||
2735 cft->write_string || cft->trigger)
2736 mode |= S_IWUSR;
2737
2738 return mode;
2739}
2740
Tejun Heo2bb566c2013-08-08 20:11:23 -04002741static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002742{
Paul Menagebd89aab2007-10-18 23:40:44 -07002743 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002744 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002745 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002746 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002747 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002748 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002749 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002750
Tejun Heo9fa4db32013-08-26 18:40:56 -04002751 if (cft->ss && !(cft->flags & CFTYPE_NO_PREFIX) &&
2752 !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002753 strcpy(name, cft->ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002754 strcat(name, ".");
2755 }
2756 strcat(name, cft->name);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002757
Paul Menageddbcc7e2007-10-18 23:39:30 -07002758 BUG_ON(!mutex_is_locked(&dir->d_inode->i_mutex));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002759
2760 cfe = kzalloc(sizeof(*cfe), GFP_KERNEL);
2761 if (!cfe)
2762 return -ENOMEM;
2763
Paul Menageddbcc7e2007-10-18 23:39:30 -07002764 dentry = lookup_one_len(name, dir, strlen(name));
Tejun Heo05ef1d72012-04-01 12:09:56 -07002765 if (IS_ERR(dentry)) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002766 error = PTR_ERR(dentry);
Tejun Heo05ef1d72012-04-01 12:09:56 -07002767 goto out;
2768 }
2769
Li Zefand6cbf352013-05-14 19:44:20 +08002770 cfe->type = (void *)cft;
2771 cfe->dentry = dentry;
2772 dentry->d_fsdata = cfe;
2773 simple_xattrs_init(&cfe->xattrs);
2774
Tejun Heo05ef1d72012-04-01 12:09:56 -07002775 mode = cgroup_file_mode(cft);
2776 error = cgroup_create_file(dentry, mode | S_IFREG, cgrp->root->sb);
2777 if (!error) {
Tejun Heo05ef1d72012-04-01 12:09:56 -07002778 list_add_tail(&cfe->node, &parent->files);
2779 cfe = NULL;
2780 }
2781 dput(dentry);
2782out:
2783 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002784 return error;
2785}
2786
Tejun Heob1f28d32013-06-28 16:24:10 -07002787/**
2788 * cgroup_addrm_files - add or remove files to a cgroup directory
2789 * @cgrp: the target cgroup
Tejun Heob1f28d32013-06-28 16:24:10 -07002790 * @cfts: array of cftypes to be added
2791 * @is_add: whether to add or remove
2792 *
2793 * Depending on @is_add, add or remove files defined by @cfts on @cgrp.
Tejun Heo2bb566c2013-08-08 20:11:23 -04002794 * For removals, this function never fails. If addition fails, this
2795 * function doesn't remove files already added. The caller is responsible
2796 * for cleaning up.
Tejun Heob1f28d32013-06-28 16:24:10 -07002797 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002798static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
2799 bool is_add)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002800{
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002801 struct cftype *cft;
Tejun Heob1f28d32013-06-28 16:24:10 -07002802 int ret;
2803
2804 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
2805 lockdep_assert_held(&cgroup_mutex);
Tejun Heodb0416b2012-04-01 12:09:55 -07002806
2807 for (cft = cfts; cft->name[0] != '\0'; cft++) {
Gao fengf33fddc2012-12-06 14:38:57 +08002808 /* does cft->flags tell us to skip this file on @cgrp? */
Tejun Heo873fe092013-04-14 20:15:26 -07002809 if ((cft->flags & CFTYPE_INSANE) && cgroup_sane_behavior(cgrp))
2810 continue;
Gao fengf33fddc2012-12-06 14:38:57 +08002811 if ((cft->flags & CFTYPE_NOT_ON_ROOT) && !cgrp->parent)
2812 continue;
2813 if ((cft->flags & CFTYPE_ONLY_ON_ROOT) && cgrp->parent)
2814 continue;
2815
Li Zefan2739d3c2013-01-21 18:18:33 +08002816 if (is_add) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04002817 ret = cgroup_add_file(cgrp, cft);
Tejun Heob1f28d32013-06-28 16:24:10 -07002818 if (ret) {
Li Zefan2739d3c2013-01-21 18:18:33 +08002819 pr_warn("cgroup_addrm_files: failed to add %s, err=%d\n",
Tejun Heob1f28d32013-06-28 16:24:10 -07002820 cft->name, ret);
2821 return ret;
2822 }
Li Zefan2739d3c2013-01-21 18:18:33 +08002823 } else {
2824 cgroup_rm_file(cgrp, cft);
Tejun Heodb0416b2012-04-01 12:09:55 -07002825 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002826 }
Tejun Heob1f28d32013-06-28 16:24:10 -07002827 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002828}
2829
Tejun Heo8e3f6542012-04-01 12:09:55 -07002830static void cgroup_cfts_prepare(void)
Li Zefane8c82d22013-06-18 18:48:37 +08002831 __acquires(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002832{
2833 /*
2834 * Thanks to the entanglement with vfs inode locking, we can't walk
2835 * the existing cgroups under cgroup_mutex and create files.
Tejun Heo492eb212013-08-08 20:11:25 -04002836 * Instead, we use css_for_each_descendant_pre() and drop RCU read
2837 * lock before calling cgroup_addrm_files().
Tejun Heo8e3f6542012-04-01 12:09:55 -07002838 */
Tejun Heo8e3f6542012-04-01 12:09:55 -07002839 mutex_lock(&cgroup_mutex);
2840}
2841
Tejun Heo2bb566c2013-08-08 20:11:23 -04002842static int cgroup_cfts_commit(struct cftype *cfts, bool is_add)
Li Zefane8c82d22013-06-18 18:48:37 +08002843 __releases(&cgroup_mutex)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002844{
2845 LIST_HEAD(pending);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002846 struct cgroup_subsys *ss = cfts[0].ss;
Tejun Heo492eb212013-08-08 20:11:25 -04002847 struct cgroup *root = &ss->root->top_cgroup;
Li Zefan084457f2013-06-18 18:40:19 +08002848 struct super_block *sb = ss->root->sb;
Li Zefane8c82d22013-06-18 18:48:37 +08002849 struct dentry *prev = NULL;
2850 struct inode *inode;
Tejun Heo492eb212013-08-08 20:11:25 -04002851 struct cgroup_subsys_state *css;
Tejun Heo00356bd2013-06-18 11:14:22 -07002852 u64 update_before;
Tejun Heo9ccece82013-06-28 16:24:11 -07002853 int ret = 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002854
2855 /* %NULL @cfts indicates abort and don't bother if @ss isn't attached */
Tejun Heo9871bf92013-06-24 15:21:47 -07002856 if (!cfts || ss->root == &cgroup_dummy_root ||
Li Zefane8c82d22013-06-18 18:48:37 +08002857 !atomic_inc_not_zero(&sb->s_active)) {
2858 mutex_unlock(&cgroup_mutex);
Tejun Heo9ccece82013-06-28 16:24:11 -07002859 return 0;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002860 }
2861
Li Zefane8c82d22013-06-18 18:48:37 +08002862 /*
2863 * All cgroups which are created after we drop cgroup_mutex will
2864 * have the updated set of files, so we only need to update the
Tejun Heo00356bd2013-06-18 11:14:22 -07002865 * cgroups created before the current @cgroup_serial_nr_next.
Li Zefane8c82d22013-06-18 18:48:37 +08002866 */
Tejun Heo00356bd2013-06-18 11:14:22 -07002867 update_before = cgroup_serial_nr_next;
Li Zefane8c82d22013-06-18 18:48:37 +08002868
Tejun Heo8e3f6542012-04-01 12:09:55 -07002869 mutex_unlock(&cgroup_mutex);
2870
Li Zefane8c82d22013-06-18 18:48:37 +08002871 /* add/rm files for all cgroups created before */
2872 rcu_read_lock();
Tejun Heoca8bdca2013-08-26 18:40:56 -04002873 css_for_each_descendant_pre(css, cgroup_css(root, ss)) {
Tejun Heo492eb212013-08-08 20:11:25 -04002874 struct cgroup *cgrp = css->cgroup;
2875
Li Zefane8c82d22013-06-18 18:48:37 +08002876 if (cgroup_is_dead(cgrp))
2877 continue;
2878
2879 inode = cgrp->dentry->d_inode;
2880 dget(cgrp->dentry);
2881 rcu_read_unlock();
2882
2883 dput(prev);
2884 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885
2886 mutex_lock(&inode->i_mutex);
2887 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002888 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002889 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002890 mutex_unlock(&cgroup_mutex);
2891 mutex_unlock(&inode->i_mutex);
2892
Li Zefane8c82d22013-06-18 18:48:37 +08002893 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002894 if (ret)
2895 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002896 }
Li Zefane8c82d22013-06-18 18:48:37 +08002897 rcu_read_unlock();
2898 dput(prev);
2899 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002900 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901}
2902
2903/**
2904 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2905 * @ss: target cgroup subsystem
2906 * @cfts: zero-length name terminated array of cftypes
2907 *
2908 * Register @cfts to @ss. Files described by @cfts are created for all
2909 * existing cgroups to which @ss is attached and all future cgroups will
2910 * have them too. This function can be called anytime whether @ss is
2911 * attached or not.
2912 *
2913 * Returns 0 on successful registration, -errno on failure. Note that this
2914 * function currently returns 0 as long as @cfts registration is successful
2915 * even if some file creation attempts on existing cgroups fail.
2916 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002917int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002918{
2919 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002920 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002921 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002922
2923 set = kzalloc(sizeof(*set), GFP_KERNEL);
2924 if (!set)
2925 return -ENOMEM;
2926
Tejun Heo2bb566c2013-08-08 20:11:23 -04002927 for (cft = cfts; cft->name[0] != '\0'; cft++)
2928 cft->ss = ss;
2929
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930 cgroup_cfts_prepare();
2931 set->cfts = cfts;
2932 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002934 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002935 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002936 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002937}
2938EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2939
Li Zefana043e3b2008-02-23 15:24:09 -08002940/**
Tejun Heo79578622012-04-01 12:09:56 -07002941 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002942 * @cfts: zero-length name terminated array of cftypes
2943 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002944 * Unregister @cfts. Files described by @cfts are removed from all
2945 * existing cgroups and all future cgroups won't have them either. This
2946 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002947 *
2948 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002949 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002950 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002951int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002952{
2953 struct cftype_set *set;
2954
Tejun Heo2bb566c2013-08-08 20:11:23 -04002955 if (!cfts || !cfts[0].ss)
2956 return -ENOENT;
2957
Tejun Heo79578622012-04-01 12:09:56 -07002958 cgroup_cfts_prepare();
2959
Tejun Heo2bb566c2013-08-08 20:11:23 -04002960 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002961 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002962 list_del(&set->node);
2963 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002964 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002965 return 0;
2966 }
2967 }
2968
Tejun Heo2bb566c2013-08-08 20:11:23 -04002969 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002970 return -ENOENT;
2971}
2972
2973/**
Li Zefana043e3b2008-02-23 15:24:09 -08002974 * cgroup_task_count - count the number of tasks in a cgroup.
2975 * @cgrp: the cgroup in question
2976 *
2977 * Return the number of tasks in the cgroup.
2978 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002979int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002980{
2981 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002982 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002983
Paul Menage817929e2007-10-18 23:39:36 -07002984 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002985 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2986 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002987 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002988 return count;
2989}
2990
2991/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002992 * To reduce the fork() overhead for systems that are not actually using
2993 * their cgroups capability, we don't maintain the lists running through
2994 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002995 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002996 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002997static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002998{
2999 struct task_struct *p, *g;
3000 write_lock(&css_set_lock);
3001 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003002 /*
3003 * We need tasklist_lock because RCU is not safe against
3004 * while_each_thread(). Besides, a forking task that has passed
3005 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3006 * is not guaranteed to have its child immediately visible in the
3007 * tasklist if we walk through it with RCU.
3008 */
3009 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010 do_each_thread(g, p) {
3011 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003012 /*
3013 * We should check if the process is exiting, otherwise
3014 * it will race with cgroup_exit() in that the list
3015 * entry won't be deleted though the process has exited.
3016 */
3017 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003018 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 task_unlock(p);
3020 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003021 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003022 write_unlock(&css_set_lock);
3023}
3024
Tejun Heo574bd9f2012-11-09 09:12:29 -08003025/**
Tejun Heo492eb212013-08-08 20:11:25 -04003026 * css_next_child - find the next child of a given css
3027 * @pos_css: the current position (%NULL to initiate traversal)
3028 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003029 *
Tejun Heo492eb212013-08-08 20:11:25 -04003030 * This function returns the next child of @parent_css and should be called
3031 * under RCU read lock. The only requirement is that @parent_css and
3032 * @pos_css are accessible. The next sibling is guaranteed to be returned
3033 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003034 */
Tejun Heo492eb212013-08-08 20:11:25 -04003035struct cgroup_subsys_state *
3036css_next_child(struct cgroup_subsys_state *pos_css,
3037 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003038{
Tejun Heo492eb212013-08-08 20:11:25 -04003039 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3040 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003041 struct cgroup *next;
3042
3043 WARN_ON_ONCE(!rcu_read_lock_held());
3044
3045 /*
3046 * @pos could already have been removed. Once a cgroup is removed,
3047 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003048 * changes. As CGRP_DEAD assertion is serialized and happens
3049 * before the cgroup is taken off the ->sibling list, if we see it
3050 * unasserted, it's guaranteed that the next sibling hasn't
3051 * finished its grace period even if it's already removed, and thus
3052 * safe to dereference from this RCU critical section. If
3053 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3054 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003055 *
3056 * If @pos is dead, its next pointer can't be dereferenced;
3057 * however, as each cgroup is given a monotonically increasing
3058 * unique serial number and always appended to the sibling list,
3059 * the next one can be found by walking the parent's children until
3060 * we see a cgroup with higher serial number than @pos's. While
3061 * this path can be slower, it's taken only when either the current
3062 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003063 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003064 if (!pos) {
3065 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3066 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003067 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003068 } else {
3069 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3070 if (next->serial_nr > pos->serial_nr)
3071 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003072 }
3073
Tejun Heo492eb212013-08-08 20:11:25 -04003074 if (&next->sibling == &cgrp->children)
3075 return NULL;
3076
Tejun Heoca8bdca2013-08-26 18:40:56 -04003077 return cgroup_css(next, parent_css->ss);
Tejun Heo53fa5262013-05-24 10:55:38 +09003078}
Tejun Heo492eb212013-08-08 20:11:25 -04003079EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003080
3081/**
Tejun Heo492eb212013-08-08 20:11:25 -04003082 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003083 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003084 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003085 *
Tejun Heo492eb212013-08-08 20:11:25 -04003086 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003087 * to visit for pre-order traversal of @root's descendants. @root is
3088 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003089 *
3090 * While this function requires RCU read locking, it doesn't require the
3091 * whole traversal to be contained in a single RCU critical section. This
3092 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003093 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003094 */
Tejun Heo492eb212013-08-08 20:11:25 -04003095struct cgroup_subsys_state *
3096css_next_descendant_pre(struct cgroup_subsys_state *pos,
3097 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003098{
Tejun Heo492eb212013-08-08 20:11:25 -04003099 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003100
3101 WARN_ON_ONCE(!rcu_read_lock_held());
3102
Tejun Heobd8815a2013-08-08 20:11:27 -04003103 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003104 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003105 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003106
3107 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003108 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109 if (next)
3110 return next;
3111
3112 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003113 while (pos != root) {
3114 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003115 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003116 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003117 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003118 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003119
3120 return NULL;
3121}
Tejun Heo492eb212013-08-08 20:11:25 -04003122EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003124/**
Tejun Heo492eb212013-08-08 20:11:25 -04003125 * css_rightmost_descendant - return the rightmost descendant of a css
3126 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003127 *
Tejun Heo492eb212013-08-08 20:11:25 -04003128 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3129 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003130 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003131 *
3132 * While this function requires RCU read locking, it doesn't require the
3133 * whole traversal to be contained in a single RCU critical section. This
3134 * function will return the correct rightmost descendant as long as @pos is
3135 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003136 */
Tejun Heo492eb212013-08-08 20:11:25 -04003137struct cgroup_subsys_state *
3138css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003139{
Tejun Heo492eb212013-08-08 20:11:25 -04003140 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003141
3142 WARN_ON_ONCE(!rcu_read_lock_held());
3143
3144 do {
3145 last = pos;
3146 /* ->prev isn't RCU safe, walk ->next till the end */
3147 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003148 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003149 pos = tmp;
3150 } while (pos);
3151
3152 return last;
3153}
Tejun Heo492eb212013-08-08 20:11:25 -04003154EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003155
Tejun Heo492eb212013-08-08 20:11:25 -04003156static struct cgroup_subsys_state *
3157css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003158{
Tejun Heo492eb212013-08-08 20:11:25 -04003159 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003160
3161 do {
3162 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003163 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003164 } while (pos);
3165
3166 return last;
3167}
3168
3169/**
Tejun Heo492eb212013-08-08 20:11:25 -04003170 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003171 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003172 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003173 *
Tejun Heo492eb212013-08-08 20:11:25 -04003174 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003175 * to visit for post-order traversal of @root's descendants. @root is
3176 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003177 *
3178 * While this function requires RCU read locking, it doesn't require the
3179 * whole traversal to be contained in a single RCU critical section. This
3180 * function will return the correct next descendant as long as both @pos
3181 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003182 */
Tejun Heo492eb212013-08-08 20:11:25 -04003183struct cgroup_subsys_state *
3184css_next_descendant_post(struct cgroup_subsys_state *pos,
3185 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003186{
Tejun Heo492eb212013-08-08 20:11:25 -04003187 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003188
3189 WARN_ON_ONCE(!rcu_read_lock_held());
3190
3191 /* if first iteration, visit the leftmost descendant */
3192 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003193 next = css_leftmost_descendant(root);
3194 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003195 }
3196
Tejun Heobd8815a2013-08-08 20:11:27 -04003197 /* if we visited @root, we're done */
3198 if (pos == root)
3199 return NULL;
3200
Tejun Heo574bd9f2012-11-09 09:12:29 -08003201 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003202 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003203 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003204 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003205
3206 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003207 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003208}
Tejun Heo492eb212013-08-08 20:11:25 -04003209EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003210
Tejun Heo0942eee2013-08-08 20:11:26 -04003211/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003212 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003213 * @it: the iterator to advance
3214 *
3215 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003216 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003217static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003218{
3219 struct list_head *l = it->cset_link;
3220 struct cgrp_cset_link *link;
3221 struct css_set *cset;
3222
3223 /* Advance to the next non-empty css_set */
3224 do {
3225 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003226 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003227 it->cset_link = NULL;
3228 return;
3229 }
3230 link = list_entry(l, struct cgrp_cset_link, cset_link);
3231 cset = link->cset;
3232 } while (list_empty(&cset->tasks));
3233 it->cset_link = l;
3234 it->task = cset->tasks.next;
3235}
3236
Tejun Heo0942eee2013-08-08 20:11:26 -04003237/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003238 * css_task_iter_start - initiate task iteration
3239 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003240 * @it: the task iterator to use
3241 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003242 * Initiate iteration through the tasks of @css. The caller can call
3243 * css_task_iter_next() to walk through the tasks until the function
3244 * returns NULL. On completion of iteration, css_task_iter_end() must be
3245 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003246 *
3247 * Note that this function acquires a lock which is released when the
3248 * iteration finishes. The caller can't sleep while iteration is in
3249 * progress.
3250 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003251void css_task_iter_start(struct cgroup_subsys_state *css,
3252 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003253 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003254{
3255 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003256 * The first time anyone tries to iterate across a css, we need to
3257 * enable the list linking each css_set to its tasks, and fix up
3258 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003259 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003260 if (!use_task_css_set_links)
3261 cgroup_enable_task_cg_lists();
3262
Paul Menage817929e2007-10-18 23:39:36 -07003263 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003264
Tejun Heo72ec7022013-08-08 20:11:26 -04003265 it->origin_css = css;
3266 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003267
Tejun Heo72ec7022013-08-08 20:11:26 -04003268 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003269}
3270
Tejun Heo0942eee2013-08-08 20:11:26 -04003271/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003272 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003273 * @it: the task iterator being iterated
3274 *
3275 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003276 * initialized via css_task_iter_start(). Returns NULL when the iteration
3277 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003278 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003279struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003280{
3281 struct task_struct *res;
3282 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003283 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003284
3285 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003286 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003287 return NULL;
3288 res = list_entry(l, struct task_struct, cg_list);
3289 /* Advance iterator to find next entry */
3290 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003291 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3292 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003293 /*
3294 * We reached the end of this task list - move on to the
3295 * next cgrp_cset_link.
3296 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003297 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003298 } else {
3299 it->task = l;
3300 }
3301 return res;
3302}
3303
Tejun Heo0942eee2013-08-08 20:11:26 -04003304/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003305 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003306 * @it: the task iterator to finish
3307 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003308 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003309 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003310void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003311 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003312{
3313 read_unlock(&css_set_lock);
3314}
3315
Cliff Wickman31a7df02008-02-07 00:14:42 -08003316static inline int started_after_time(struct task_struct *t1,
3317 struct timespec *time,
3318 struct task_struct *t2)
3319{
3320 int start_diff = timespec_compare(&t1->start_time, time);
3321 if (start_diff > 0) {
3322 return 1;
3323 } else if (start_diff < 0) {
3324 return 0;
3325 } else {
3326 /*
3327 * Arbitrarily, if two processes started at the same
3328 * time, we'll say that the lower pointer value
3329 * started first. Note that t2 may have exited by now
3330 * so this may not be a valid pointer any longer, but
3331 * that's fine - it still serves to distinguish
3332 * between two tasks started (effectively) simultaneously.
3333 */
3334 return t1 > t2;
3335 }
3336}
3337
3338/*
3339 * This function is a callback from heap_insert() and is used to order
3340 * the heap.
3341 * In this case we order the heap in descending task start time.
3342 */
3343static inline int started_after(void *p1, void *p2)
3344{
3345 struct task_struct *t1 = p1;
3346 struct task_struct *t2 = p2;
3347 return started_after_time(t1, &t2->start_time, t2);
3348}
3349
3350/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003351 * css_scan_tasks - iterate though all the tasks in a css
3352 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003353 * @test: optional test callback
3354 * @process: process callback
3355 * @data: data passed to @test and @process
3356 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003357 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003358 * Iterate through all the tasks in @css, calling @test for each, and if it
3359 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003360 *
Tejun Heoe5358372013-08-08 20:11:26 -04003361 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003362 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003363 * lock css_set_lock for the call to @process.
3364 *
3365 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003366 * of @css for the duration of this call. This function may or may not
3367 * call @process for tasks that exit or move to a different css during the
3368 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003369 *
3370 * Note that @test may be called with locks held, and may in some
3371 * situations be called multiple times for the same task, so it should be
3372 * cheap.
3373 *
3374 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3375 * heap operations (and its "gt" member will be overwritten), else a
3376 * temporary heap will be used (allocation of which may cause this function
3377 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003378 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003379int css_scan_tasks(struct cgroup_subsys_state *css,
3380 bool (*test)(struct task_struct *, void *),
3381 void (*process)(struct task_struct *, void *),
3382 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003383{
3384 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003385 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003386 struct task_struct *p, *dropped;
3387 /* Never dereference latest_task, since it's not refcounted */
3388 struct task_struct *latest_task = NULL;
3389 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003390 struct timespec latest_time = { 0, 0 };
3391
Tejun Heoe5358372013-08-08 20:11:26 -04003392 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003393 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003394 heap->gt = &started_after;
3395 } else {
3396 /* We need to allocate our own heap memory */
3397 heap = &tmp_heap;
3398 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3399 if (retval)
3400 /* cannot allocate the heap */
3401 return retval;
3402 }
3403
3404 again:
3405 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003406 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003407 * which are of interest, and invoking @process callback on the
3408 * ones which need an update. Since we don't want to hold any
3409 * locks during the task updates, gather tasks to be processed in a
3410 * heap structure. The heap is sorted by descending task start
3411 * time. If the statically-sized heap fills up, we overflow tasks
3412 * that started later, and in future iterations only consider tasks
3413 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003414 * guarantees forward progress and that we don't miss any tasks.
3415 */
3416 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003417 css_task_iter_start(css, &it);
3418 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003419 /*
3420 * Only affect tasks that qualify per the caller's callback,
3421 * if he provided one
3422 */
Tejun Heoe5358372013-08-08 20:11:26 -04003423 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003424 continue;
3425 /*
3426 * Only process tasks that started after the last task
3427 * we processed
3428 */
3429 if (!started_after_time(p, &latest_time, latest_task))
3430 continue;
3431 dropped = heap_insert(heap, p);
3432 if (dropped == NULL) {
3433 /*
3434 * The new task was inserted; the heap wasn't
3435 * previously full
3436 */
3437 get_task_struct(p);
3438 } else if (dropped != p) {
3439 /*
3440 * The new task was inserted, and pushed out a
3441 * different task
3442 */
3443 get_task_struct(p);
3444 put_task_struct(dropped);
3445 }
3446 /*
3447 * Else the new task was newer than anything already in
3448 * the heap and wasn't inserted
3449 */
3450 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003451 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003452
3453 if (heap->size) {
3454 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003455 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003456 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003457 latest_time = q->start_time;
3458 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003459 }
3460 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003461 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003462 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003463 }
3464 /*
3465 * If we had to process any tasks at all, scan again
3466 * in case some of them were in the middle of forking
3467 * children that didn't get processed.
3468 * Not the most efficient way to do it, but it avoids
3469 * having to take callback_mutex in the fork path
3470 */
3471 goto again;
3472 }
3473 if (heap == &tmp_heap)
3474 heap_free(&tmp_heap);
3475 return 0;
3476}
3477
Tejun Heoe5358372013-08-08 20:11:26 -04003478static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003479{
Tejun Heoe5358372013-08-08 20:11:26 -04003480 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003481
Tejun Heo47cfcd02013-04-07 09:29:51 -07003482 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003483 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003484 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003485}
3486
3487/**
3488 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3489 * @to: cgroup to which the tasks will be moved
3490 * @from: cgroup in which the tasks currently reside
3491 */
3492int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3493{
Tejun Heo72ec7022013-08-08 20:11:26 -04003494 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3495 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003496}
3497
Paul Menage817929e2007-10-18 23:39:36 -07003498/*
Ben Blum102a7752009-09-23 15:56:26 -07003499 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003500 *
3501 * Reading this file can return large amounts of data if a cgroup has
3502 * *lots* of attached tasks. So it may need several calls to read(),
3503 * but we cannot guarantee that the information we produce is correct
3504 * unless we produce it entirely atomically.
3505 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003506 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003507
Li Zefan24528252012-01-20 11:58:43 +08003508/* which pidlist file are we talking about? */
3509enum cgroup_filetype {
3510 CGROUP_FILE_PROCS,
3511 CGROUP_FILE_TASKS,
3512};
3513
3514/*
3515 * A pidlist is a list of pids that virtually represents the contents of one
3516 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3517 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3518 * to the cgroup.
3519 */
3520struct cgroup_pidlist {
3521 /*
3522 * used to find which pidlist is wanted. doesn't change as long as
3523 * this particular list stays in the list.
3524 */
3525 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3526 /* array of xids */
3527 pid_t *list;
3528 /* how many elements the above list has */
3529 int length;
3530 /* how many files are using the current array */
3531 int use_count;
3532 /* each of these stored in a list by its cgroup */
3533 struct list_head links;
3534 /* pointer to the cgroup we belong to, for list removal purposes */
3535 struct cgroup *owner;
3536 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003537 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003538};
3539
Paul Menagebbcb81d2007-10-18 23:39:32 -07003540/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003541 * The following two functions "fix" the issue where there are more pids
3542 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3543 * TODO: replace with a kernel-wide solution to this problem
3544 */
3545#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3546static void *pidlist_allocate(int count)
3547{
3548 if (PIDLIST_TOO_LARGE(count))
3549 return vmalloc(count * sizeof(pid_t));
3550 else
3551 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3552}
3553static void pidlist_free(void *p)
3554{
3555 if (is_vmalloc_addr(p))
3556 vfree(p);
3557 else
3558 kfree(p);
3559}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003560
3561/*
Ben Blum102a7752009-09-23 15:56:26 -07003562 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003563 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003564 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003565static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003566{
Ben Blum102a7752009-09-23 15:56:26 -07003567 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003568
3569 /*
3570 * we presume the 0th element is unique, so i starts at 1. trivial
3571 * edge cases first; no work needs to be done for either
3572 */
3573 if (length == 0 || length == 1)
3574 return length;
3575 /* src and dest walk down the list; dest counts unique elements */
3576 for (src = 1; src < length; src++) {
3577 /* find next unique element */
3578 while (list[src] == list[src-1]) {
3579 src++;
3580 if (src == length)
3581 goto after;
3582 }
3583 /* dest always points to where the next unique element goes */
3584 list[dest] = list[src];
3585 dest++;
3586 }
3587after:
Ben Blum102a7752009-09-23 15:56:26 -07003588 return dest;
3589}
3590
3591static int cmppid(const void *a, const void *b)
3592{
3593 return *(pid_t *)a - *(pid_t *)b;
3594}
3595
3596/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003597 * find the appropriate pidlist for our purpose (given procs vs tasks)
3598 * returns with the lock on that pidlist already held, and takes care
3599 * of the use count, or returns NULL with no locks held if we're out of
3600 * memory.
3601 */
3602static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3603 enum cgroup_filetype type)
3604{
3605 struct cgroup_pidlist *l;
3606 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003607 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003608
Ben Blum72a8cb32009-09-23 15:56:27 -07003609 /*
Li Zefanb3958902013-08-01 09:52:15 +08003610 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003611 * the last ref-holder is trying to remove l from the list at the same
3612 * time. Holding the pidlist_mutex precludes somebody taking whichever
3613 * list we find out from under us - compare release_pid_array().
3614 */
3615 mutex_lock(&cgrp->pidlist_mutex);
3616 list_for_each_entry(l, &cgrp->pidlists, links) {
3617 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003618 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003619 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003620 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003621 return l;
3622 }
3623 }
3624 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003625 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003626 if (!l) {
3627 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 return l;
3629 }
Li Zefanb3958902013-08-01 09:52:15 +08003630 init_rwsem(&l->rwsem);
3631 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003632 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003633 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003634 l->owner = cgrp;
3635 list_add(&l->links, &cgrp->pidlists);
3636 mutex_unlock(&cgrp->pidlist_mutex);
3637 return l;
3638}
3639
3640/*
Ben Blum102a7752009-09-23 15:56:26 -07003641 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3642 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003643static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3644 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003645{
3646 pid_t *array;
3647 int length;
3648 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003649 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003650 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003651 struct cgroup_pidlist *l;
3652
3653 /*
3654 * If cgroup gets more users after we read count, we won't have
3655 * enough space - tough. This race is indistinguishable to the
3656 * caller from the case that the additional cgroup users didn't
3657 * show up until sometime later on.
3658 */
3659 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003660 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003661 if (!array)
3662 return -ENOMEM;
3663 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003664 css_task_iter_start(&cgrp->dummy_css, &it);
3665 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003666 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003667 break;
Ben Blum102a7752009-09-23 15:56:26 -07003668 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003669 if (type == CGROUP_FILE_PROCS)
3670 pid = task_tgid_vnr(tsk);
3671 else
3672 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003673 if (pid > 0) /* make sure to only use valid results */
3674 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003675 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003676 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003677 length = n;
3678 /* now sort & (if procs) strip out duplicates */
3679 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003680 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003681 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003682 l = cgroup_pidlist_find(cgrp, type);
3683 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003684 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003686 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003687 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003688 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003689 l->list = array;
3690 l->length = length;
3691 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003692 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003693 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003694 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003695}
3696
Balbir Singh846c7bb2007-10-18 23:39:44 -07003697/**
Li Zefana043e3b2008-02-23 15:24:09 -08003698 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003699 * @stats: cgroupstats to fill information into
3700 * @dentry: A dentry entry belonging to the cgroup for which stats have
3701 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003702 *
3703 * Build and fill cgroupstats so that taskstats can export it to user
3704 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003705 */
3706int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3707{
3708 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003709 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003710 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003711 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003712
Balbir Singh846c7bb2007-10-18 23:39:44 -07003713 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003714 * Validate dentry by checking the superblock operations,
3715 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003716 */
Li Zefan33d283b2008-11-19 15:36:48 -08003717 if (dentry->d_sb->s_op != &cgroup_ops ||
3718 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719 goto err;
3720
3721 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003722 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003723
Tejun Heo72ec7022013-08-08 20:11:26 -04003724 css_task_iter_start(&cgrp->dummy_css, &it);
3725 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003726 switch (tsk->state) {
3727 case TASK_RUNNING:
3728 stats->nr_running++;
3729 break;
3730 case TASK_INTERRUPTIBLE:
3731 stats->nr_sleeping++;
3732 break;
3733 case TASK_UNINTERRUPTIBLE:
3734 stats->nr_uninterruptible++;
3735 break;
3736 case TASK_STOPPED:
3737 stats->nr_stopped++;
3738 break;
3739 default:
3740 if (delayacct_is_task_waiting_on_io(tsk))
3741 stats->nr_io_wait++;
3742 break;
3743 }
3744 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003745 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003746
Balbir Singh846c7bb2007-10-18 23:39:44 -07003747err:
3748 return ret;
3749}
3750
Paul Menage8f3ff202009-09-23 15:56:25 -07003751
Paul Menagecc31edc2008-10-18 20:28:04 -07003752/*
Ben Blum102a7752009-09-23 15:56:26 -07003753 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003754 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003755 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003756 */
3757
Ben Blum102a7752009-09-23 15:56:26 -07003758static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003759{
3760 /*
3761 * Initially we receive a position value that corresponds to
3762 * one more than the last pid shown (or 0 on the first call or
3763 * after a seek to the start). Use a binary-search to find the
3764 * next pid to display, if any
3765 */
Ben Blum102a7752009-09-23 15:56:26 -07003766 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003767 int index = 0, pid = *pos;
3768 int *iter;
3769
Li Zefanb3958902013-08-01 09:52:15 +08003770 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003771 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003772 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003773
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 while (index < end) {
3775 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003776 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 index = mid;
3778 break;
Ben Blum102a7752009-09-23 15:56:26 -07003779 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 index = mid + 1;
3781 else
3782 end = mid;
3783 }
3784 }
3785 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003786 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 return NULL;
3788 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003789 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 *pos = *iter;
3791 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003792}
3793
Ben Blum102a7752009-09-23 15:56:26 -07003794static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003795{
Ben Blum102a7752009-09-23 15:56:26 -07003796 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003797 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003798}
3799
Ben Blum102a7752009-09-23 15:56:26 -07003800static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003801{
Ben Blum102a7752009-09-23 15:56:26 -07003802 struct cgroup_pidlist *l = s->private;
3803 pid_t *p = v;
3804 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003805 /*
3806 * Advance to the next pid in the array. If this goes off the
3807 * end, we're done
3808 */
3809 p++;
3810 if (p >= end) {
3811 return NULL;
3812 } else {
3813 *pos = *p;
3814 return p;
3815 }
3816}
3817
Ben Blum102a7752009-09-23 15:56:26 -07003818static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003819{
3820 return seq_printf(s, "%d\n", *(int *)v);
3821}
3822
Ben Blum102a7752009-09-23 15:56:26 -07003823/*
3824 * seq_operations functions for iterating on pidlists through seq_file -
3825 * independent of whether it's tasks or procs
3826 */
3827static const struct seq_operations cgroup_pidlist_seq_operations = {
3828 .start = cgroup_pidlist_start,
3829 .stop = cgroup_pidlist_stop,
3830 .next = cgroup_pidlist_next,
3831 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003832};
3833
Ben Blum102a7752009-09-23 15:56:26 -07003834static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003835{
Ben Blum72a8cb32009-09-23 15:56:27 -07003836 /*
3837 * the case where we're the last user of this particular pidlist will
3838 * have us remove it from the cgroup's list, which entails taking the
3839 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3840 * pidlist_mutex, we have to take pidlist_mutex first.
3841 */
3842 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003843 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003844 BUG_ON(!l->use_count);
3845 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003846 /* we're the last user if refcount is 0; remove and free */
3847 list_del(&l->links);
3848 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003849 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003850 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003851 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003852 kfree(l);
3853 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003854 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003855 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003856 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003857}
3858
Ben Blum102a7752009-09-23 15:56:26 -07003859static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003860{
Ben Blum102a7752009-09-23 15:56:26 -07003861 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003862 if (!(file->f_mode & FMODE_READ))
3863 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003864 /*
3865 * the seq_file will only be initialized if the file was opened for
3866 * reading; hence we check if it's not null only in that case.
3867 */
3868 l = ((struct seq_file *)file->private_data)->private;
3869 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003870 return seq_release(inode, file);
3871}
3872
Ben Blum102a7752009-09-23 15:56:26 -07003873static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003874 .read = seq_read,
3875 .llseek = seq_lseek,
3876 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003877 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003878};
3879
3880/*
Ben Blum102a7752009-09-23 15:56:26 -07003881 * The following functions handle opens on a file that displays a pidlist
3882 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3883 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003884 */
Ben Blum102a7752009-09-23 15:56:26 -07003885/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003886static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003887{
3888 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003889 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003890 int retval;
3891
3892 /* Nothing to do for write-only files */
3893 if (!(file->f_mode & FMODE_READ))
3894 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003895
Ben Blum102a7752009-09-23 15:56:26 -07003896 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003897 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003898 if (retval)
3899 return retval;
3900 /* configure file information */
3901 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003902
Ben Blum102a7752009-09-23 15:56:26 -07003903 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003904 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003905 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003906 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003907 }
Ben Blum102a7752009-09-23 15:56:26 -07003908 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003909 return 0;
3910}
Ben Blum102a7752009-09-23 15:56:26 -07003911static int cgroup_tasks_open(struct inode *unused, struct file *file)
3912{
Ben Blum72a8cb32009-09-23 15:56:27 -07003913 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003914}
3915static int cgroup_procs_open(struct inode *unused, struct file *file)
3916{
Ben Blum72a8cb32009-09-23 15:56:27 -07003917 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003918}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003919
Tejun Heo182446d2013-08-08 20:11:24 -04003920static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3921 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003922{
Tejun Heo182446d2013-08-08 20:11:24 -04003923 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003924}
3925
Tejun Heo182446d2013-08-08 20:11:24 -04003926static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3927 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003928{
Tejun Heo182446d2013-08-08 20:11:24 -04003929 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003930 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003931 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003932 else
Tejun Heo182446d2013-08-08 20:11:24 -04003933 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003934 return 0;
3935}
3936
Paul Menagebbcb81d2007-10-18 23:39:32 -07003937/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003938 * When dput() is called asynchronously, if umount has been done and
3939 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3940 * there's a small window that vfs will see the root dentry with non-zero
3941 * refcnt and trigger BUG().
3942 *
3943 * That's why we hold a reference before dput() and drop it right after.
3944 */
3945static void cgroup_dput(struct cgroup *cgrp)
3946{
3947 struct super_block *sb = cgrp->root->sb;
3948
3949 atomic_inc(&sb->s_active);
3950 dput(cgrp->dentry);
3951 deactivate_super(sb);
3952}
3953
3954/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003955 * Unregister event and free resources.
3956 *
3957 * Gets called from workqueue.
3958 */
3959static void cgroup_event_remove(struct work_struct *work)
3960{
3961 struct cgroup_event *event = container_of(work, struct cgroup_event,
3962 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003963 struct cgroup_subsys_state *css = event->css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003964
Li Zefan810cbee2013-02-18 18:56:14 +08003965 remove_wait_queue(event->wqh, &event->wait);
3966
Tejun Heo81eeaf02013-08-08 20:11:26 -04003967 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003968
Li Zefan810cbee2013-02-18 18:56:14 +08003969 /* Notify userspace the event is going away. */
3970 eventfd_signal(event->eventfd, 1);
3971
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003973 kfree(event);
Tejun Heo7941cb02013-08-26 18:40:56 -04003974 css_put(css);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003975}
3976
3977/*
3978 * Gets called on POLLHUP on eventfd when user closes it.
3979 *
3980 * Called with wqh->lock held and interrupts disabled.
3981 */
3982static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3983 int sync, void *key)
3984{
3985 struct cgroup_event *event = container_of(wait,
3986 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003987 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003988 unsigned long flags = (unsigned long)key;
3989
3990 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003991 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003992 * If the event has been detached at cgroup removal, we
3993 * can simply return knowing the other side will cleanup
3994 * for us.
3995 *
3996 * We can't race against event freeing since the other
3997 * side will require wqh->lock via remove_wait_queue(),
3998 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 */
Li Zefan810cbee2013-02-18 18:56:14 +08004000 spin_lock(&cgrp->event_list_lock);
4001 if (!list_empty(&event->list)) {
4002 list_del_init(&event->list);
4003 /*
4004 * We are in atomic context, but cgroup_event_remove()
4005 * may sleep, so we have to call it in workqueue.
4006 */
4007 schedule_work(&event->remove);
4008 }
4009 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004010 }
4011
4012 return 0;
4013}
4014
4015static void cgroup_event_ptable_queue_proc(struct file *file,
4016 wait_queue_head_t *wqh, poll_table *pt)
4017{
4018 struct cgroup_event *event = container_of(pt,
4019 struct cgroup_event, pt);
4020
4021 event->wqh = wqh;
4022 add_wait_queue(wqh, &event->wait);
4023}
4024
4025/*
4026 * Parse input and register new cgroup event handler.
4027 *
4028 * Input must be in format '<event_fd> <control_fd> <args>'.
4029 * Interpretation of args is defined by control file implementation.
4030 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004031static int cgroup_write_event_control(struct cgroup_subsys_state *dummy_css,
Tejun Heo182446d2013-08-08 20:11:24 -04004032 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004033{
Tejun Heo6e6eab02013-08-15 11:43:15 -04004034 struct cgroup *cgrp = dummy_css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004035 struct cgroup_event *event;
Tejun Heo7c918cb2013-08-26 18:40:56 -04004036 struct cgroup_subsys_state *cfile_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037 unsigned int efd, cfd;
Al Viro4e10f3c2013-08-30 12:29:49 -04004038 struct fd efile;
4039 struct fd cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004040 char *endp;
4041 int ret;
4042
4043 efd = simple_strtoul(buffer, &endp, 10);
4044 if (*endp != ' ')
4045 return -EINVAL;
4046 buffer = endp + 1;
4047
4048 cfd = simple_strtoul(buffer, &endp, 10);
4049 if ((*endp != ' ') && (*endp != '\0'))
4050 return -EINVAL;
4051 buffer = endp + 1;
4052
4053 event = kzalloc(sizeof(*event), GFP_KERNEL);
4054 if (!event)
4055 return -ENOMEM;
Tejun Heo6e6eab02013-08-15 11:43:15 -04004056
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004057 INIT_LIST_HEAD(&event->list);
4058 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4059 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4060 INIT_WORK(&event->remove, cgroup_event_remove);
4061
Al Viro4e10f3c2013-08-30 12:29:49 -04004062 efile = fdget(efd);
4063 if (!efile.file) {
4064 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004065 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004066 }
4067
Al Viro4e10f3c2013-08-30 12:29:49 -04004068 event->eventfd = eventfd_ctx_fileget(efile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004069 if (IS_ERR(event->eventfd)) {
4070 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004071 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004072 }
4073
Al Viro4e10f3c2013-08-30 12:29:49 -04004074 cfile = fdget(cfd);
4075 if (!cfile.file) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004076 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004077 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004078 }
4079
4080 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004081 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro4e10f3c2013-08-30 12:29:49 -04004082 ret = inode_permission(file_inode(cfile.file), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004083 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004084 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004085
Al Viro4e10f3c2013-08-30 12:29:49 -04004086 event->cft = __file_cft(cfile.file);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004087 if (IS_ERR(event->cft)) {
4088 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004089 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004090 }
4091
Tejun Heo6e6eab02013-08-15 11:43:15 -04004092 if (!event->cft->ss) {
4093 ret = -EBADF;
4094 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004095 }
4096
Li Zefanf1690072013-02-18 14:13:35 +08004097 /*
Tejun Heo7c918cb2013-08-26 18:40:56 -04004098 * Determine the css of @cfile, verify it belongs to the same
4099 * cgroup as cgroup.event_control, and associate @event with it.
Tejun Heo7941cb02013-08-26 18:40:56 -04004100 * Remaining events are automatically removed on cgroup destruction
4101 * but the removal is asynchronous, so take an extra ref.
Li Zefanf1690072013-02-18 14:13:35 +08004102 */
Tejun Heo6e6eab02013-08-15 11:43:15 -04004103 rcu_read_lock();
4104
4105 ret = -EINVAL;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004106 event->css = cgroup_css(cgrp, event->cft->ss);
Al Viro4e10f3c2013-08-30 12:29:49 -04004107 cfile_css = css_from_dir(cfile.file->f_dentry->d_parent, event->cft->ss);
Tejun Heo7c918cb2013-08-26 18:40:56 -04004108 if (event->css && event->css == cfile_css && css_tryget(event->css))
Tejun Heo6e6eab02013-08-15 11:43:15 -04004109 ret = 0;
4110
4111 rcu_read_unlock();
4112 if (ret)
4113 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004114
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004115 if (!event->cft->register_event || !event->cft->unregister_event) {
4116 ret = -EINVAL;
Tejun Heo7941cb02013-08-26 18:40:56 -04004117 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004118 }
4119
Tejun Heo6e6eab02013-08-15 11:43:15 -04004120 ret = event->cft->register_event(event->css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004121 event->eventfd, buffer);
4122 if (ret)
Tejun Heo7941cb02013-08-26 18:40:56 -04004123 goto out_put_css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004124
Al Viro4e10f3c2013-08-30 12:29:49 -04004125 efile.file->f_op->poll(efile.file, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004126
4127 spin_lock(&cgrp->event_list_lock);
4128 list_add(&event->list, &cgrp->event_list);
4129 spin_unlock(&cgrp->event_list_lock);
4130
Al Viro4e10f3c2013-08-30 12:29:49 -04004131 fdput(cfile);
4132 fdput(efile);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004133
4134 return 0;
4135
Tejun Heo7941cb02013-08-26 18:40:56 -04004136out_put_css:
4137 css_put(event->css);
Li Zefan876ede82013-08-01 09:51:47 +08004138out_put_cfile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004139 fdput(cfile);
Li Zefan876ede82013-08-01 09:51:47 +08004140out_put_eventfd:
4141 eventfd_ctx_put(event->eventfd);
4142out_put_efile:
Al Viro4e10f3c2013-08-30 12:29:49 -04004143 fdput(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004144out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004145 kfree(event);
4146
4147 return ret;
4148}
4149
Tejun Heo182446d2013-08-08 20:11:24 -04004150static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4151 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004152{
Tejun Heo182446d2013-08-08 20:11:24 -04004153 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004154}
4155
Tejun Heo182446d2013-08-08 20:11:24 -04004156static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4157 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004158{
4159 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004160 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004161 else
Tejun Heo182446d2013-08-08 20:11:24 -04004162 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004163 return 0;
4164}
4165
Tejun Heod5c56ce2013-06-03 19:14:34 -07004166static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004167 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004168 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004169 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004170 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004171 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004172 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004173 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004174 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004175 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004176 .write_string = cgroup_write_event_control,
4177 .mode = S_IWUGO,
4178 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004179 {
4180 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004181 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004182 .read_u64 = cgroup_clone_children_read,
4183 .write_u64 = cgroup_clone_children_write,
4184 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004185 {
Tejun Heo873fe092013-04-14 20:15:26 -07004186 .name = "cgroup.sane_behavior",
4187 .flags = CFTYPE_ONLY_ON_ROOT,
4188 .read_seq_string = cgroup_sane_behavior_show,
4189 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004190
4191 /*
4192 * Historical crazy stuff. These don't have "cgroup." prefix and
4193 * don't exist if sane_behavior. If you're depending on these, be
4194 * prepared to be burned.
4195 */
4196 {
4197 .name = "tasks",
4198 .flags = CFTYPE_INSANE, /* use "procs" instead */
4199 .open = cgroup_tasks_open,
4200 .write_u64 = cgroup_tasks_write,
4201 .release = cgroup_pidlist_release,
4202 .mode = S_IRUGO | S_IWUSR,
4203 },
4204 {
4205 .name = "notify_on_release",
4206 .flags = CFTYPE_INSANE,
4207 .read_u64 = cgroup_read_notify_on_release,
4208 .write_u64 = cgroup_write_notify_on_release,
4209 },
Tejun Heo873fe092013-04-14 20:15:26 -07004210 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004211 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004212 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004213 .read_seq_string = cgroup_release_agent_show,
4214 .write_string = cgroup_release_agent_write,
4215 .max_write_len = PATH_MAX,
4216 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004217 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004218};
4219
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004220/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004221 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004222 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004223 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004224 *
4225 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004226 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004227static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004228{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004229 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004230 int i, ret = 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07004231
Tejun Heo8e3f6542012-04-01 12:09:55 -07004232 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004233 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004234 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004235
4236 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004237 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004238
Tejun Heobee55092013-06-28 16:24:11 -07004239 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004240 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004241 if (ret < 0)
4242 goto err;
4243 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004244 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004245
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004246 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004247 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoca8bdca2013-08-26 18:40:56 -04004248 struct cgroup_subsys_state *css = cgroup_css(cgrp, ss);
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004249 struct css_id *id = rcu_dereference_protected(css->id, true);
4250
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004251 /*
4252 * Update id->css pointer and make this css visible from
4253 * CSS ID functions. This pointer will be dereferened
4254 * from RCU-read-side without locks.
4255 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004256 if (id)
4257 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004258 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004259
4260 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004261err:
4262 cgroup_clear_dir(cgrp, subsys_mask);
4263 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004264}
4265
Tejun Heo0c21ead2013-08-13 20:22:51 -04004266/*
4267 * css destruction is four-stage process.
4268 *
4269 * 1. Destruction starts. Killing of the percpu_ref is initiated.
4270 * Implemented in kill_css().
4271 *
4272 * 2. When the percpu_ref is confirmed to be visible as killed on all CPUs
4273 * and thus css_tryget() is guaranteed to fail, the css can be offlined
4274 * by invoking offline_css(). After offlining, the base ref is put.
4275 * Implemented in css_killed_work_fn().
4276 *
4277 * 3. When the percpu_ref reaches zero, the only possible remaining
4278 * accessors are inside RCU read sections. css_release() schedules the
4279 * RCU callback.
4280 *
4281 * 4. After the grace period, the css can be freed. Implemented in
4282 * css_free_work_fn().
4283 *
4284 * It is actually hairier because both step 2 and 4 require process context
4285 * and thus involve punting to css->destroy_work adding two additional
4286 * steps to the already complex sequence.
4287 */
Tejun Heo35ef10d2013-08-13 11:01:54 -04004288static void css_free_work_fn(struct work_struct *work)
Tejun Heo48ddbe12012-04-01 12:09:56 -07004289{
4290 struct cgroup_subsys_state *css =
Tejun Heo35ef10d2013-08-13 11:01:54 -04004291 container_of(work, struct cgroup_subsys_state, destroy_work);
Tejun Heo0c21ead2013-08-13 20:22:51 -04004292 struct cgroup *cgrp = css->cgroup;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004293
Tejun Heo0ae78e02013-08-13 11:01:54 -04004294 if (css->parent)
4295 css_put(css->parent);
4296
Tejun Heo0c21ead2013-08-13 20:22:51 -04004297 css->ss->css_free(css);
4298 cgroup_dput(cgrp);
4299}
4300
4301static void css_free_rcu_fn(struct rcu_head *rcu_head)
4302{
4303 struct cgroup_subsys_state *css =
4304 container_of(rcu_head, struct cgroup_subsys_state, rcu_head);
4305
4306 /*
4307 * css holds an extra ref to @cgrp->dentry which is put on the last
4308 * css_put(). dput() requires process context which we don't have.
4309 */
4310 INIT_WORK(&css->destroy_work, css_free_work_fn);
4311 schedule_work(&css->destroy_work);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004312}
4313
Tejun Heod3daf282013-06-13 19:39:16 -07004314static void css_release(struct percpu_ref *ref)
4315{
4316 struct cgroup_subsys_state *css =
4317 container_of(ref, struct cgroup_subsys_state, refcnt);
4318
Tejun Heo0c21ead2013-08-13 20:22:51 -04004319 call_rcu(&css->rcu_head, css_free_rcu_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004320}
4321
Tejun Heo623f9262013-08-13 11:01:55 -04004322static void init_css(struct cgroup_subsys_state *css, struct cgroup_subsys *ss,
4323 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004324{
Paul Menagebd89aab2007-10-18 23:40:44 -07004325 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004326 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004327 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004328 css->id = NULL;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004329
Tejun Heo0ae78e02013-08-13 11:01:54 -04004330 if (cgrp->parent)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004331 css->parent = cgroup_css(cgrp->parent, ss);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004332 else
Paul Menageddbcc7e2007-10-18 23:39:30 -07004333 css->flags |= CSS_ROOT;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004334
Tejun Heoca8bdca2013-08-26 18:40:56 -04004335 BUG_ON(cgroup_css(cgrp, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336}
4337
Li Zefan2a4ac632013-07-31 16:16:40 +08004338/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heo623f9262013-08-13 11:01:55 -04004339static int online_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004340{
Tejun Heo623f9262013-08-13 11:01:55 -04004341 struct cgroup_subsys *ss = css->ss;
Tejun Heob1929db2012-11-19 08:13:38 -08004342 int ret = 0;
4343
Tejun Heoa31f2d32012-11-19 08:13:37 -08004344 lockdep_assert_held(&cgroup_mutex);
4345
Tejun Heo92fb9742012-11-19 08:13:38 -08004346 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004347 ret = ss->css_online(css);
Tejun Heoae7f1642013-08-13 20:22:50 -04004348 if (!ret) {
Tejun Heoeb954192013-08-08 20:11:23 -04004349 css->flags |= CSS_ONLINE;
Tejun Heof20104d2013-08-13 20:22:50 -04004350 css->cgroup->nr_css++;
Tejun Heoae7f1642013-08-13 20:22:50 -04004351 rcu_assign_pointer(css->cgroup->subsys[ss->subsys_id], css);
4352 }
Tejun Heob1929db2012-11-19 08:13:38 -08004353 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004354}
4355
Li Zefan2a4ac632013-07-31 16:16:40 +08004356/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heo623f9262013-08-13 11:01:55 -04004357static void offline_css(struct cgroup_subsys_state *css)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004358{
Tejun Heo623f9262013-08-13 11:01:55 -04004359 struct cgroup_subsys *ss = css->ss;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004360
4361 lockdep_assert_held(&cgroup_mutex);
4362
4363 if (!(css->flags & CSS_ONLINE))
4364 return;
4365
Li Zefand7eeac12013-03-12 15:35:59 -07004366 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004367 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004368
Tejun Heoeb954192013-08-08 20:11:23 -04004369 css->flags &= ~CSS_ONLINE;
Tejun Heo09a503ea2013-08-13 20:22:50 -04004370 css->cgroup->nr_css--;
Tejun Heo0c21ead2013-08-13 20:22:51 -04004371 RCU_INIT_POINTER(css->cgroup->subsys[ss->subsys_id], css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004372}
4373
Paul Menageddbcc7e2007-10-18 23:39:30 -07004374/*
Li Zefana043e3b2008-02-23 15:24:09 -08004375 * cgroup_create - create a cgroup
4376 * @parent: cgroup that will be parent of the new cgroup
4377 * @dentry: dentry of the new cgroup
4378 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 *
Li Zefana043e3b2008-02-23 15:24:09 -08004380 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004381 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004382static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004383 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004384{
Tejun Heoae7f1642013-08-13 20:22:50 -04004385 struct cgroup_subsys_state *css_ar[CGROUP_SUBSYS_COUNT] = { };
Paul Menagebd89aab2007-10-18 23:40:44 -07004386 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004387 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004388 struct cgroupfs_root *root = parent->root;
4389 int err = 0;
4390 struct cgroup_subsys *ss;
4391 struct super_block *sb = root->sb;
4392
Tejun Heo0a950f62012-11-19 09:02:12 -08004393 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004394 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4395 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004396 return -ENOMEM;
4397
Li Zefan65dff752013-03-01 15:01:56 +08004398 name = cgroup_alloc_name(dentry);
4399 if (!name)
4400 goto err_free_cgrp;
4401 rcu_assign_pointer(cgrp->name, name);
4402
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004403 /*
4404 * Temporarily set the pointer to NULL, so idr_find() won't return
4405 * a half-baked cgroup.
4406 */
4407 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004408 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004409 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004410
Tejun Heo976c06b2012-11-05 09:16:59 -08004411 /*
4412 * Only live parents can have children. Note that the liveliness
4413 * check isn't strictly necessary because cgroup_mkdir() and
4414 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4415 * anyway so that locking is contained inside cgroup proper and we
4416 * don't get nasty surprises if we ever grow another caller.
4417 */
4418 if (!cgroup_lock_live_group(parent)) {
4419 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004420 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004421 }
4422
Paul Menageddbcc7e2007-10-18 23:39:30 -07004423 /* Grab a reference on the superblock so the hierarchy doesn't
4424 * get deleted on unmount if there are child cgroups. This
4425 * can be done outside cgroup_mutex, since the sb can't
4426 * disappear while someone has an open control file on the
4427 * fs */
4428 atomic_inc(&sb->s_active);
4429
Paul Menagecc31edc2008-10-18 20:28:04 -07004430 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004431
Li Zefanfe1c06c2013-01-24 14:30:22 +08004432 dentry->d_fsdata = cgrp;
4433 cgrp->dentry = dentry;
4434
Paul Menagebd89aab2007-10-18 23:40:44 -07004435 cgrp->parent = parent;
Tejun Heo0ae78e02013-08-13 11:01:54 -04004436 cgrp->dummy_css.parent = &parent->dummy_css;
Paul Menagebd89aab2007-10-18 23:40:44 -07004437 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004438
Li Zefanb6abdb02008-03-04 14:28:19 -08004439 if (notify_on_release(parent))
4440 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4441
Tejun Heo2260e7f2012-11-19 08:13:38 -08004442 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4443 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004444
Tejun Heo5549c492013-06-24 15:21:48 -07004445 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004446 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004447
Tejun Heoca8bdca2013-08-26 18:40:56 -04004448 css = ss->css_alloc(cgroup_css(parent, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004449 if (IS_ERR(css)) {
4450 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004451 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004452 }
Tejun Heoae7f1642013-08-13 20:22:50 -04004453 css_ar[ss->subsys_id] = css;
Tejun Heod3daf282013-06-13 19:39:16 -07004454
4455 err = percpu_ref_init(&css->refcnt, css_release);
Tejun Heoae7f1642013-08-13 20:22:50 -04004456 if (err)
Tejun Heod3daf282013-06-13 19:39:16 -07004457 goto err_free_all;
4458
Tejun Heo623f9262013-08-13 11:01:55 -04004459 init_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004460
Li Zefan4528fd02010-02-02 13:44:10 -08004461 if (ss->use_id) {
Tejun Heo623f9262013-08-13 11:01:55 -04004462 err = alloc_css_id(css);
Li Zefan4528fd02010-02-02 13:44:10 -08004463 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004464 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004465 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004466 }
4467
Tejun Heo4e139af2012-11-19 08:13:36 -08004468 /*
4469 * Create directory. cgroup_create_file() returns with the new
4470 * directory locked on success so that it can be populated without
4471 * dropping cgroup_mutex.
4472 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004473 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004474 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004475 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004476 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004477
Tejun Heo00356bd2013-06-18 11:14:22 -07004478 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004479
Tejun Heo4e139af2012-11-19 08:13:36 -08004480 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004481 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4482 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004483
Tejun Heo0ae78e02013-08-13 11:01:54 -04004484 /* each css holds a ref to the cgroup's dentry and the parent css */
4485 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004486 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo0ae78e02013-08-13 11:01:54 -04004487
Tejun Heoed9577932012-11-05 09:16:58 -08004488 dget(dentry);
Li Zhong930913a2013-08-16 17:57:14 +08004489 css_get(css->parent);
Tejun Heo0ae78e02013-08-13 11:01:54 -04004490 }
Tejun Heo48ddbe12012-04-01 12:09:56 -07004491
Li Zefan415cf072013-04-08 14:35:02 +08004492 /* hold a ref to the parent's dentry */
4493 dget(parent->dentry);
4494
Tejun Heob1929db2012-11-19 08:13:38 -08004495 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004496 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004497 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heo623f9262013-08-13 11:01:55 -04004498
4499 err = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004500 if (err)
4501 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004502
4503 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4504 parent->parent) {
4505 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",
4506 current->comm, current->pid, ss->name);
4507 if (!strcmp(ss->name, "memory"))
4508 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4509 ss->warned_broken_hierarchy = true;
4510 }
Tejun Heoa8638032012-11-09 09:12:29 -08004511 }
4512
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004513 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4514
Tejun Heo2bb566c2013-08-08 20:11:23 -04004515 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004516 if (err)
4517 goto err_destroy;
4518
4519 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004520 if (err)
4521 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004522
4523 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004524 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004525
4526 return 0;
4527
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004528err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004529 for_each_root_subsys(root, ss) {
Tejun Heoae7f1642013-08-13 20:22:50 -04004530 struct cgroup_subsys_state *css = css_ar[ss->subsys_id];
Tejun Heod3daf282013-06-13 19:39:16 -07004531
4532 if (css) {
4533 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004534 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004535 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004536 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004537 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004538 /* Release the reference count that we took on the superblock */
4539 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004540err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004541 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004542err_free_name:
4543 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004544err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004545 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004546 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004547
4548err_destroy:
4549 cgroup_destroy_locked(cgrp);
4550 mutex_unlock(&cgroup_mutex);
4551 mutex_unlock(&dentry->d_inode->i_mutex);
4552 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004553}
4554
Al Viro18bb1db2011-07-26 01:41:39 -04004555static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004556{
4557 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4558
4559 /* the vfs holds inode->i_mutex already */
4560 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4561}
4562
Tejun Heo223dbc32013-08-13 20:22:50 -04004563/*
4564 * This is called when the refcnt of a css is confirmed to be killed.
4565 * css_tryget() is now guaranteed to fail.
4566 */
4567static void css_killed_work_fn(struct work_struct *work)
Tejun Heod3daf282013-06-13 19:39:16 -07004568{
Tejun Heo223dbc32013-08-13 20:22:50 -04004569 struct cgroup_subsys_state *css =
4570 container_of(work, struct cgroup_subsys_state, destroy_work);
4571 struct cgroup *cgrp = css->cgroup;
Tejun Heod3daf282013-06-13 19:39:16 -07004572
Tejun Heof20104d2013-08-13 20:22:50 -04004573 mutex_lock(&cgroup_mutex);
4574
4575 /*
Tejun Heo09a503ea2013-08-13 20:22:50 -04004576 * css_tryget() is guaranteed to fail now. Tell subsystems to
4577 * initate destruction.
4578 */
4579 offline_css(css);
4580
4581 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004582 * If @cgrp is marked dead, it's waiting for refs of all css's to
4583 * be disabled before proceeding to the second phase of cgroup
4584 * destruction. If we are the last one, kick it off.
4585 */
Tejun Heo09a503ea2013-08-13 20:22:50 -04004586 if (!cgrp->nr_css && cgroup_is_dead(cgrp))
Tejun Heof20104d2013-08-13 20:22:50 -04004587 cgroup_destroy_css_killed(cgrp);
4588
4589 mutex_unlock(&cgroup_mutex);
Tejun Heo09a503ea2013-08-13 20:22:50 -04004590
4591 /*
4592 * Put the css refs from kill_css(). Each css holds an extra
4593 * reference to the cgroup's dentry and cgroup removal proceeds
4594 * regardless of css refs. On the last put of each css, whenever
4595 * that may be, the extra dentry ref is put so that dentry
4596 * destruction happens only after all css's are released.
4597 */
4598 css_put(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004599}
4600
Tejun Heo223dbc32013-08-13 20:22:50 -04004601/* css kill confirmation processing requires process context, bounce */
4602static void css_killed_ref_fn(struct percpu_ref *ref)
Tejun Heod3daf282013-06-13 19:39:16 -07004603{
4604 struct cgroup_subsys_state *css =
4605 container_of(ref, struct cgroup_subsys_state, refcnt);
4606
Tejun Heo223dbc32013-08-13 20:22:50 -04004607 INIT_WORK(&css->destroy_work, css_killed_work_fn);
4608 schedule_work(&css->destroy_work);
Tejun Heod3daf282013-06-13 19:39:16 -07004609}
4610
4611/**
Tejun Heoedae0c32013-08-13 20:22:51 -04004612 * kill_css - destroy a css
4613 * @css: css to destroy
4614 *
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004615 * This function initiates destruction of @css by removing cgroup interface
4616 * files and putting its base reference. ->css_offline() will be invoked
4617 * asynchronously once css_tryget() is guaranteed to fail and when the
4618 * reference count reaches zero, @css will be released.
Tejun Heoedae0c32013-08-13 20:22:51 -04004619 */
4620static void kill_css(struct cgroup_subsys_state *css)
4621{
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004622 cgroup_clear_dir(css->cgroup, 1 << css->ss->subsys_id);
4623
Tejun Heoedae0c32013-08-13 20:22:51 -04004624 /*
4625 * Killing would put the base ref, but we need to keep it alive
4626 * until after ->css_offline().
4627 */
4628 css_get(css);
4629
4630 /*
4631 * cgroup core guarantees that, by the time ->css_offline() is
4632 * invoked, no new css reference will be given out via
4633 * css_tryget(). We can't simply call percpu_ref_kill() and
4634 * proceed to offlining css's because percpu_ref_kill() doesn't
4635 * guarantee that the ref is seen as killed on all CPUs on return.
4636 *
4637 * Use percpu_ref_kill_and_confirm() to get notifications as each
4638 * css is confirmed to be seen as killed on all CPUs.
4639 */
4640 percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn);
Tejun Heod3daf282013-06-13 19:39:16 -07004641}
4642
4643/**
4644 * cgroup_destroy_locked - the first stage of cgroup destruction
4645 * @cgrp: cgroup to be destroyed
4646 *
4647 * css's make use of percpu refcnts whose killing latency shouldn't be
4648 * exposed to userland and are RCU protected. Also, cgroup core needs to
4649 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4650 * invoked. To satisfy all the requirements, destruction is implemented in
4651 * the following two steps.
4652 *
4653 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4654 * userland visible parts and start killing the percpu refcnts of
4655 * css's. Set up so that the next stage will be kicked off once all
4656 * the percpu refcnts are confirmed to be killed.
4657 *
4658 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4659 * rest of destruction. Once all cgroup references are gone, the
4660 * cgroup is RCU-freed.
4661 *
4662 * This function implements s1. After this step, @cgrp is gone as far as
4663 * the userland is concerned and a new cgroup with the same name may be
4664 * created. As cgroup doesn't care about the names internally, this
4665 * doesn't cause any problem.
4666 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004667static int cgroup_destroy_locked(struct cgroup *cgrp)
4668 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004669{
Tejun Heo42809dd2012-11-19 08:13:37 -08004670 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004671 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004672 struct cgroup_subsys *ss;
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004673 struct cgroup *child;
Tejun Heoddd69142013-06-12 21:04:54 -07004674 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004675
Tejun Heo42809dd2012-11-19 08:13:37 -08004676 lockdep_assert_held(&d->d_inode->i_mutex);
4677 lockdep_assert_held(&cgroup_mutex);
4678
Tejun Heoddd69142013-06-12 21:04:54 -07004679 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004680 * css_set_lock synchronizes access to ->cset_links and prevents
4681 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004682 */
4683 read_lock(&css_set_lock);
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004684 empty = list_empty(&cgrp->cset_links);
Tejun Heoddd69142013-06-12 21:04:54 -07004685 read_unlock(&css_set_lock);
4686 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004688
Tejun Heo1a90dd52012-11-05 09:16:59 -08004689 /*
Hugh Dickinsbb78a922013-08-28 16:31:23 -07004690 * Make sure there's no live children. We can't test ->children
4691 * emptiness as dead children linger on it while being destroyed;
4692 * otherwise, "rmdir parent/child parent" may fail with -EBUSY.
4693 */
4694 empty = true;
4695 rcu_read_lock();
4696 list_for_each_entry_rcu(child, &cgrp->children, sibling) {
4697 empty = cgroup_is_dead(child);
4698 if (!empty)
4699 break;
4700 }
4701 rcu_read_unlock();
4702 if (!empty)
4703 return -EBUSY;
4704
4705 /*
Tejun Heoedae0c32013-08-13 20:22:51 -04004706 * Initiate massacre of all css's. cgroup_destroy_css_killed()
4707 * will be invoked to perform the rest of destruction once the
4708 * percpu refs of all css's are confirmed to be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004709 */
Tejun Heoedae0c32013-08-13 20:22:51 -04004710 for_each_root_subsys(cgrp->root, ss)
Tejun Heoca8bdca2013-08-26 18:40:56 -04004711 kill_css(cgroup_css(cgrp, ss));
Tejun Heo455050d2013-06-13 19:27:41 -07004712
4713 /*
4714 * Mark @cgrp dead. This prevents further task migration and child
4715 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004716 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004717 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004718 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004719 */
Tejun Heo54766d42013-06-12 21:04:53 -07004720 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004721
Tejun Heo455050d2013-06-13 19:27:41 -07004722 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4723 raw_spin_lock(&release_list_lock);
4724 if (!list_empty(&cgrp->release_list))
4725 list_del_init(&cgrp->release_list);
4726 raw_spin_unlock(&release_list_lock);
4727
4728 /*
Tejun Heof20104d2013-08-13 20:22:50 -04004729 * If @cgrp has css's attached, the second stage of cgroup
4730 * destruction is kicked off from css_killed_work_fn() after the
4731 * refs of all attached css's are killed. If @cgrp doesn't have
4732 * any css, we kick it off here.
Tejun Heo455050d2013-06-13 19:27:41 -07004733 */
Tejun Heof20104d2013-08-13 20:22:50 -04004734 if (!cgrp->nr_css)
4735 cgroup_destroy_css_killed(cgrp);
4736
4737 /*
Tejun Heo3c14f8b2013-08-13 20:22:51 -04004738 * Clear the base files and remove @cgrp directory. The removal
4739 * puts the base ref but we aren't quite done with @cgrp yet, so
4740 * hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004741 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04004742 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004743 dget(d);
4744 cgroup_d_remove_dir(d);
4745
4746 /*
4747 * Unregister events and notify userspace.
4748 * Notify userspace about cgroup removing only after rmdir of cgroup
4749 * directory to avoid race between userspace and kernelspace.
4750 */
4751 spin_lock(&cgrp->event_list_lock);
4752 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4753 list_del_init(&event->list);
4754 schedule_work(&event->remove);
4755 }
4756 spin_unlock(&cgrp->event_list_lock);
4757
Tejun Heoea15f8c2013-06-13 19:27:42 -07004758 return 0;
4759};
4760
Tejun Heod3daf282013-06-13 19:39:16 -07004761/**
Tejun Heof20104d2013-08-13 20:22:50 -04004762 * cgroup_destroy_css_killed - the second step of cgroup destruction
Tejun Heod3daf282013-06-13 19:39:16 -07004763 * @work: cgroup->destroy_free_work
4764 *
4765 * This function is invoked from a work item for a cgroup which is being
Tejun Heo09a503ea2013-08-13 20:22:50 -04004766 * destroyed after all css's are offlined and performs the rest of
4767 * destruction. This is the second step of destruction described in the
4768 * comment above cgroup_destroy_locked().
Tejun Heod3daf282013-06-13 19:39:16 -07004769 */
Tejun Heof20104d2013-08-13 20:22:50 -04004770static void cgroup_destroy_css_killed(struct cgroup *cgrp)
Tejun Heoea15f8c2013-06-13 19:27:42 -07004771{
Tejun Heoea15f8c2013-06-13 19:27:42 -07004772 struct cgroup *parent = cgrp->parent;
4773 struct dentry *d = cgrp->dentry;
Tejun Heoea15f8c2013-06-13 19:27:42 -07004774
Tejun Heof20104d2013-08-13 20:22:50 -04004775 lockdep_assert_held(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776
Paul Menage999cd8a2009-01-07 18:08:36 -08004777 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004778 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004779
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004780 /*
4781 * We should remove the cgroup object from idr before its grace
4782 * period starts, so we won't be looking up a cgroup while the
4783 * cgroup is being freed.
4784 */
4785 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4786 cgrp->id = -1;
4787
Paul Menageddbcc7e2007-10-18 23:39:30 -07004788 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004789
Paul Menagebd89aab2007-10-18 23:40:44 -07004790 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004791 check_for_release(parent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004792}
4793
Tejun Heo42809dd2012-11-19 08:13:37 -08004794static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4795{
4796 int ret;
4797
4798 mutex_lock(&cgroup_mutex);
4799 ret = cgroup_destroy_locked(dentry->d_fsdata);
4800 mutex_unlock(&cgroup_mutex);
4801
4802 return ret;
4803}
4804
Tejun Heo8e3f6542012-04-01 12:09:55 -07004805static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4806{
4807 INIT_LIST_HEAD(&ss->cftsets);
4808
4809 /*
4810 * base_cftset is embedded in subsys itself, no need to worry about
4811 * deregistration.
4812 */
4813 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004814 struct cftype *cft;
4815
4816 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4817 cft->ss = ss;
4818
Tejun Heo8e3f6542012-04-01 12:09:55 -07004819 ss->base_cftset.cfts = ss->base_cftypes;
4820 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4821 }
4822}
4823
Li Zefan06a11922008-04-29 01:00:07 -07004824static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004825{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004826 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004827
4828 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004829
Tejun Heo648bb562012-11-19 08:13:36 -08004830 mutex_lock(&cgroup_mutex);
4831
Tejun Heo8e3f6542012-04-01 12:09:55 -07004832 /* init base cftset */
4833 cgroup_init_cftsets(ss);
4834
Paul Menageddbcc7e2007-10-18 23:39:30 -07004835 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004836 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4837 ss->root = &cgroup_dummy_root;
Tejun Heoca8bdca2013-08-26 18:40:56 -04004838 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Paul Menageddbcc7e2007-10-18 23:39:30 -07004839 /* We don't handle early failures gracefully */
4840 BUG_ON(IS_ERR(css));
Tejun Heo623f9262013-08-13 11:01:55 -04004841 init_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004842
Li Zefane8d55fd2008-04-29 01:00:13 -07004843 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004844 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004845 * newly registered, all tasks and hence the
4846 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004847 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004848
4849 need_forkexit_callback |= ss->fork || ss->exit;
4850
Li Zefane8d55fd2008-04-29 01:00:13 -07004851 /* At system boot, before all subsystems have been
4852 * registered, no tasks have been forked, so we don't
4853 * need to invoke fork callbacks here. */
4854 BUG_ON(!list_empty(&init_task.tasks));
4855
Tejun Heoae7f1642013-08-13 20:22:50 -04004856 BUG_ON(online_css(css));
Tejun Heoa8638032012-11-09 09:12:29 -08004857
Tejun Heo648bb562012-11-19 08:13:36 -08004858 mutex_unlock(&cgroup_mutex);
4859
Ben Blume6a11052010-03-10 15:22:09 -08004860 /* this function shouldn't be used with modular subsystems, since they
4861 * need to register a subsys_id, among other things */
4862 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004863}
4864
4865/**
Ben Blume6a11052010-03-10 15:22:09 -08004866 * cgroup_load_subsys: load and register a modular subsystem at runtime
4867 * @ss: the subsystem to load
4868 *
4869 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004870 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004871 * up for use. If the subsystem is built-in anyway, work is delegated to the
4872 * simpler cgroup_init_subsys.
4873 */
4874int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4875{
Ben Blume6a11052010-03-10 15:22:09 -08004876 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004877 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004878 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004879 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004880 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004881
4882 /* check name and function validity */
4883 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004884 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004885 return -EINVAL;
4886
4887 /*
4888 * we don't support callbacks in modular subsystems. this check is
4889 * before the ss->module check for consistency; a subsystem that could
4890 * be a module should still have no callbacks even if the user isn't
4891 * compiling it as one.
4892 */
4893 if (ss->fork || ss->exit)
4894 return -EINVAL;
4895
4896 /*
4897 * an optionally modular subsystem is built-in: we want to do nothing,
4898 * since cgroup_init_subsys will have already taken care of it.
4899 */
4900 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004901 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004902 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004903 return 0;
4904 }
4905
Tejun Heo8e3f6542012-04-01 12:09:55 -07004906 /* init base cftset */
4907 cgroup_init_cftsets(ss);
4908
Ben Blume6a11052010-03-10 15:22:09 -08004909 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004910 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004911
4912 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004913 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004914 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004915 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004916 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04004917 css = ss->css_alloc(cgroup_css(cgroup_dummy_top, ss));
Ben Blume6a11052010-03-10 15:22:09 -08004918 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004919 /* failure case - need to deassign the cgroup_subsys[] slot. */
4920 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004921 mutex_unlock(&cgroup_mutex);
4922 return PTR_ERR(css);
4923 }
4924
Tejun Heo9871bf92013-06-24 15:21:47 -07004925 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4926 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004927
4928 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo623f9262013-08-13 11:01:55 -04004929 init_css(css, ss, cgroup_dummy_top);
4930 /* init_idr must be after init_css() because it sets css->id. */
Ben Blume6a11052010-03-10 15:22:09 -08004931 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004932 ret = cgroup_init_idr(ss, css);
4933 if (ret)
4934 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004935 }
4936
4937 /*
4938 * Now we need to entangle the css into the existing css_sets. unlike
4939 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4940 * will need a new pointer to it; done by iterating the css_set_table.
4941 * furthermore, modifying the existing css_sets will corrupt the hash
4942 * table state, so each changed css_set will need its hash recomputed.
4943 * this is all done under the css_set_lock.
4944 */
4945 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004946 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004947 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004948 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004949 continue;
4950 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004951 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004952 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004953 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004954 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004955 key = css_set_hash(cset->subsys);
4956 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004957 }
4958 write_unlock(&css_set_lock);
4959
Tejun Heoae7f1642013-08-13 20:22:50 -04004960 ret = online_css(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004961 if (ret)
4962 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004963
Ben Blume6a11052010-03-10 15:22:09 -08004964 /* success! */
4965 mutex_unlock(&cgroup_mutex);
4966 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004967
4968err_unload:
4969 mutex_unlock(&cgroup_mutex);
4970 /* @ss can't be mounted here as try_module_get() would fail */
4971 cgroup_unload_subsys(ss);
4972 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004973}
4974EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4975
4976/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004977 * cgroup_unload_subsys: unload a modular subsystem
4978 * @ss: the subsystem to unload
4979 *
4980 * This function should be called in a modular subsystem's exitcall. When this
4981 * function is invoked, the refcount on the subsystem's module will be 0, so
4982 * the subsystem will not be attached to any hierarchy.
4983 */
4984void cgroup_unload_subsys(struct cgroup_subsys *ss)
4985{
Tejun Heo69d02062013-06-12 21:04:50 -07004986 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004987
4988 BUG_ON(ss->module == NULL);
4989
4990 /*
4991 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004992 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004993 * doesn't start being used while we're killing it off.
4994 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004995 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004996
4997 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004998
Tejun Heoca8bdca2013-08-26 18:40:56 -04004999 offline_css(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo02ae7482012-11-19 08:13:37 -08005000
Tejun Heoc897ff62013-02-27 17:03:49 -08005001 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08005002 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08005003
Ben Blumcf5d5942010-03-10 15:22:09 -08005004 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07005005 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08005006
Tejun Heo9871bf92013-06-24 15:21:47 -07005007 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07005008 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08005009
5010 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005011 * disentangle the css from all css_sets attached to the dummy
5012 * top. as in loading, we need to pay our respects to the hashtable
5013 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08005014 */
5015 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07005016 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005017 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08005018 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08005019
Tejun Heo5abb8852013-06-12 21:04:49 -07005020 hash_del(&cset->hlist);
5021 cset->subsys[ss->subsys_id] = NULL;
5022 key = css_set_hash(cset->subsys);
5023 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08005024 }
5025 write_unlock(&css_set_lock);
5026
5027 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07005028 * remove subsystem's css from the cgroup_dummy_top and free it -
5029 * need to free before marking as null because ss->css_free needs
5030 * the cgrp->subsys pointer to find their state. note that this
5031 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08005032 */
Tejun Heoca8bdca2013-08-26 18:40:56 -04005033 ss->css_free(cgroup_css(cgroup_dummy_top, ss));
Tejun Heo73e80ed2013-08-13 11:01:55 -04005034 RCU_INIT_POINTER(cgroup_dummy_top->subsys[ss->subsys_id], NULL);
Ben Blumcf5d5942010-03-10 15:22:09 -08005035
5036 mutex_unlock(&cgroup_mutex);
5037}
5038EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
5039
5040/**
Li Zefana043e3b2008-02-23 15:24:09 -08005041 * cgroup_init_early - cgroup initialization at system boot
5042 *
5043 * Initialize cgroups at system boot, and initialize any
5044 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005045 */
5046int __init cgroup_init_early(void)
5047{
Tejun Heo30159ec2013-06-25 11:53:37 -07005048 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005049 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07005050
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07005051 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07005052 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07005053 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07005054 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07005055 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07005056 init_cgroup_root(&cgroup_dummy_root);
5057 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005058 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07005059
Tejun Heo69d02062013-06-12 21:04:50 -07005060 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07005061 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
5062 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07005063 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005064
Tejun Heo30159ec2013-06-25 11:53:37 -07005065 /* at bootup time, we don't worry about modular subsystems */
5066 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005067 BUG_ON(!ss->name);
5068 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08005069 BUG_ON(!ss->css_alloc);
5070 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005071 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08005072 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07005073 ss->name, ss->subsys_id);
5074 BUG();
5075 }
5076
5077 if (ss->early_init)
5078 cgroup_init_subsys(ss);
5079 }
5080 return 0;
5081}
5082
5083/**
Li Zefana043e3b2008-02-23 15:24:09 -08005084 * cgroup_init - cgroup initialization
5085 *
5086 * Register cgroup filesystem and /proc file, and initialize
5087 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005088 */
5089int __init cgroup_init(void)
5090{
Tejun Heo30159ec2013-06-25 11:53:37 -07005091 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005092 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005093 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005094
5095 err = bdi_init(&cgroup_backing_dev_info);
5096 if (err)
5097 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005098
Tejun Heo30159ec2013-06-25 11:53:37 -07005099 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005100 if (!ss->early_init)
5101 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005102 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005103 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005104 }
5105
Tejun Heofa3ca072013-04-14 11:36:56 -07005106 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005107 mutex_lock(&cgroup_mutex);
5108 mutex_lock(&cgroup_root_mutex);
5109
Tejun Heo82fe9b02013-06-25 11:53:37 -07005110 /* Add init_css_set to the hash table */
5111 key = css_set_hash(init_css_set.subsys);
5112 hash_add(css_set_table, &init_css_set.hlist, key);
5113
Tejun Heofc76df72013-06-25 11:53:37 -07005114 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005115
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005116 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5117 0, 1, GFP_KERNEL);
5118 BUG_ON(err < 0);
5119
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005120 mutex_unlock(&cgroup_root_mutex);
5121 mutex_unlock(&cgroup_mutex);
5122
Greg KH676db4a2010-08-05 13:53:35 -07005123 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5124 if (!cgroup_kobj) {
5125 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005126 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005127 }
5128
5129 err = register_filesystem(&cgroup_fs_type);
5130 if (err < 0) {
5131 kobject_put(cgroup_kobj);
5132 goto out;
5133 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005134
Li Zefan46ae2202008-04-29 01:00:08 -07005135 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005136
Paul Menageddbcc7e2007-10-18 23:39:30 -07005137out:
Paul Menagea4243162007-10-18 23:39:35 -07005138 if (err)
5139 bdi_destroy(&cgroup_backing_dev_info);
5140
Paul Menageddbcc7e2007-10-18 23:39:30 -07005141 return err;
5142}
Paul Menageb4f48b62007-10-18 23:39:33 -07005143
Paul Menagea4243162007-10-18 23:39:35 -07005144/*
5145 * proc_cgroup_show()
5146 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5147 * - Used for /proc/<pid>/cgroup.
5148 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5149 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005150 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005151 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5152 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5153 * cgroup to top_cgroup.
5154 */
5155
5156/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005157int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005158{
5159 struct pid *pid;
5160 struct task_struct *tsk;
5161 char *buf;
5162 int retval;
5163 struct cgroupfs_root *root;
5164
5165 retval = -ENOMEM;
5166 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5167 if (!buf)
5168 goto out;
5169
5170 retval = -ESRCH;
5171 pid = m->private;
5172 tsk = get_pid_task(pid, PIDTYPE_PID);
5173 if (!tsk)
5174 goto out_free;
5175
5176 retval = 0;
5177
5178 mutex_lock(&cgroup_mutex);
5179
Li Zefane5f6a862009-01-07 18:07:41 -08005180 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005181 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005182 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005183 int count = 0;
5184
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005185 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005186 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005187 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005188 if (strlen(root->name))
5189 seq_printf(m, "%sname=%s", count ? "," : "",
5190 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005191 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005192 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005193 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005194 if (retval < 0)
5195 goto out_unlock;
5196 seq_puts(m, buf);
5197 seq_putc(m, '\n');
5198 }
5199
5200out_unlock:
5201 mutex_unlock(&cgroup_mutex);
5202 put_task_struct(tsk);
5203out_free:
5204 kfree(buf);
5205out:
5206 return retval;
5207}
5208
Paul Menagea4243162007-10-18 23:39:35 -07005209/* Display information about each subsystem and each hierarchy */
5210static int proc_cgroupstats_show(struct seq_file *m, void *v)
5211{
Tejun Heo30159ec2013-06-25 11:53:37 -07005212 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005213 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005214
Paul Menage8bab8dd2008-04-04 14:29:57 -07005215 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005216 /*
5217 * ideally we don't want subsystems moving around while we do this.
5218 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5219 * subsys/hierarchy state.
5220 */
Paul Menagea4243162007-10-18 23:39:35 -07005221 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005222
5223 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005224 seq_printf(m, "%s\t%d\t%d\t%d\n",
5225 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005226 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005227
Paul Menagea4243162007-10-18 23:39:35 -07005228 mutex_unlock(&cgroup_mutex);
5229 return 0;
5230}
5231
5232static int cgroupstats_open(struct inode *inode, struct file *file)
5233{
Al Viro9dce07f2008-03-29 03:07:28 +00005234 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005235}
5236
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005237static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005238 .open = cgroupstats_open,
5239 .read = seq_read,
5240 .llseek = seq_lseek,
5241 .release = single_release,
5242};
5243
Paul Menageb4f48b62007-10-18 23:39:33 -07005244/**
5245 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005246 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005247 *
5248 * Description: A task inherits its parent's cgroup at fork().
5249 *
5250 * A pointer to the shared css_set was automatically copied in
5251 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005252 * it was not made under the protection of RCU or cgroup_mutex, so
5253 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5254 * have already changed current->cgroups, allowing the previously
5255 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005256 *
5257 * At the point that cgroup_fork() is called, 'current' is the parent
5258 * task, and the passed argument 'child' points to the child task.
5259 */
5260void cgroup_fork(struct task_struct *child)
5261{
Tejun Heo9bb71302012-10-18 17:52:07 -07005262 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005263 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005264 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005265 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005266 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005267}
5268
5269/**
Li Zefana043e3b2008-02-23 15:24:09 -08005270 * cgroup_post_fork - called on a new task after adding it to the task list
5271 * @child: the task in question
5272 *
Tejun Heo5edee612012-10-16 15:03:14 -07005273 * Adds the task to the list running through its css_set if necessary and
5274 * call the subsystem fork() callbacks. Has to be after the task is
5275 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005276 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005277 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005278 */
Paul Menage817929e2007-10-18 23:39:36 -07005279void cgroup_post_fork(struct task_struct *child)
5280{
Tejun Heo30159ec2013-06-25 11:53:37 -07005281 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005282 int i;
5283
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005284 /*
5285 * use_task_css_set_links is set to 1 before we walk the tasklist
5286 * under the tasklist_lock and we read it here after we added the child
5287 * to the tasklist under the tasklist_lock as well. If the child wasn't
5288 * yet in the tasklist when we walked through it from
5289 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5290 * should be visible now due to the paired locking and barriers implied
5291 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5292 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5293 * lock on fork.
5294 */
Paul Menage817929e2007-10-18 23:39:36 -07005295 if (use_task_css_set_links) {
5296 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005297 task_lock(child);
5298 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005299 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005300 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005301 write_unlock(&css_set_lock);
5302 }
Tejun Heo5edee612012-10-16 15:03:14 -07005303
5304 /*
5305 * Call ss->fork(). This must happen after @child is linked on
5306 * css_set; otherwise, @child might change state between ->fork()
5307 * and addition to css_set.
5308 */
5309 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005310 /*
5311 * fork/exit callbacks are supported only for builtin
5312 * subsystems, and the builtin section of the subsys
5313 * array is immutable, so we don't need to lock the
5314 * subsys array here. On the other hand, modular section
5315 * of the array can be freed at module unload, so we
5316 * can't touch that.
5317 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005318 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005319 if (ss->fork)
5320 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005321 }
Paul Menage817929e2007-10-18 23:39:36 -07005322}
Tejun Heo5edee612012-10-16 15:03:14 -07005323
Paul Menage817929e2007-10-18 23:39:36 -07005324/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005325 * cgroup_exit - detach cgroup from exiting task
5326 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005327 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005328 *
5329 * Description: Detach cgroup from @tsk and release it.
5330 *
5331 * Note that cgroups marked notify_on_release force every task in
5332 * them to take the global cgroup_mutex mutex when exiting.
5333 * This could impact scaling on very large systems. Be reluctant to
5334 * use notify_on_release cgroups where very high task exit scaling
5335 * is required on large systems.
5336 *
5337 * the_top_cgroup_hack:
5338 *
5339 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5340 *
5341 * We call cgroup_exit() while the task is still competent to
5342 * handle notify_on_release(), then leave the task attached to the
5343 * root cgroup in each hierarchy for the remainder of its exit.
5344 *
5345 * To do this properly, we would increment the reference count on
5346 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5347 * code we would add a second cgroup function call, to drop that
5348 * reference. This would just create an unnecessary hot spot on
5349 * the top_cgroup reference count, to no avail.
5350 *
5351 * Normally, holding a reference to a cgroup without bumping its
5352 * count is unsafe. The cgroup could go away, or someone could
5353 * attach us to a different cgroup, decrementing the count on
5354 * the first cgroup that we never incremented. But in this case,
5355 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005356 * which wards off any cgroup_attach_task() attempts, or task is a failed
5357 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005358 */
5359void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5360{
Tejun Heo30159ec2013-06-25 11:53:37 -07005361 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005362 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005363 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005364
5365 /*
5366 * Unlink from the css_set task list if necessary.
5367 * Optimistically check cg_list before taking
5368 * css_set_lock
5369 */
5370 if (!list_empty(&tsk->cg_list)) {
5371 write_lock(&css_set_lock);
5372 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005373 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005374 write_unlock(&css_set_lock);
5375 }
5376
Paul Menageb4f48b62007-10-18 23:39:33 -07005377 /* Reassign the task to the init_css_set. */
5378 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005379 cset = task_css_set(tsk);
5380 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005381
5382 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005383 /*
5384 * fork/exit callbacks are supported only for builtin
5385 * subsystems, see cgroup_post_fork() for details.
5386 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005387 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005388 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005389 struct cgroup_subsys_state *old_css = cset->subsys[i];
5390 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005391
Tejun Heoeb954192013-08-08 20:11:23 -04005392 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005393 }
5394 }
5395 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005396 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005397
Tejun Heo5abb8852013-06-12 21:04:49 -07005398 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005399}
Paul Menage697f4162007-10-18 23:39:34 -07005400
Paul Menagebd89aab2007-10-18 23:40:44 -07005401static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005402{
Li Zefanf50daa72013-03-01 15:06:07 +08005403 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005404 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005405 /*
5406 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005407 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005408 * it now
5409 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005410 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005411
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005412 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005413 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005414 list_empty(&cgrp->release_list)) {
5415 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005416 need_schedule_work = 1;
5417 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005418 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005419 if (need_schedule_work)
5420 schedule_work(&release_agent_work);
5421 }
5422}
5423
Paul Menage81a6a5c2007-10-18 23:39:38 -07005424/*
5425 * Notify userspace when a cgroup is released, by running the
5426 * configured release agent with the name of the cgroup (path
5427 * relative to the root of cgroup file system) as the argument.
5428 *
5429 * Most likely, this user command will try to rmdir this cgroup.
5430 *
5431 * This races with the possibility that some other task will be
5432 * attached to this cgroup before it is removed, or that some other
5433 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5434 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5435 * unused, and this cgroup will be reprieved from its death sentence,
5436 * to continue to serve a useful existence. Next time it's released,
5437 * we will get notified again, if it still has 'notify_on_release' set.
5438 *
5439 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5440 * means only wait until the task is successfully execve()'d. The
5441 * separate release agent task is forked by call_usermodehelper(),
5442 * then control in this thread returns here, without waiting for the
5443 * release agent task. We don't bother to wait because the caller of
5444 * this routine has no use for the exit status of the release agent
5445 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005446 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005447static void cgroup_release_agent(struct work_struct *work)
5448{
5449 BUG_ON(work != &release_agent_work);
5450 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005451 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005452 while (!list_empty(&release_list)) {
5453 char *argv[3], *envp[3];
5454 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005455 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005456 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005457 struct cgroup,
5458 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005459 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005460 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005461 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005462 if (!pathbuf)
5463 goto continue_free;
5464 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5465 goto continue_free;
5466 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5467 if (!agentbuf)
5468 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005469
5470 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005471 argv[i++] = agentbuf;
5472 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005473 argv[i] = NULL;
5474
5475 i = 0;
5476 /* minimal command environment */
5477 envp[i++] = "HOME=/";
5478 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5479 envp[i] = NULL;
5480
5481 /* Drop the lock while we invoke the usermode helper,
5482 * since the exec could involve hitting disk and hence
5483 * be a slow process */
5484 mutex_unlock(&cgroup_mutex);
5485 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005486 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005487 continue_free:
5488 kfree(pathbuf);
5489 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005490 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005491 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005492 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005493 mutex_unlock(&cgroup_mutex);
5494}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005495
5496static int __init cgroup_disable(char *str)
5497{
Tejun Heo30159ec2013-06-25 11:53:37 -07005498 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005499 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005500 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005501
5502 while ((token = strsep(&str, ",")) != NULL) {
5503 if (!*token)
5504 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005505
Tejun Heo30159ec2013-06-25 11:53:37 -07005506 /*
5507 * cgroup_disable, being at boot time, can't know about
5508 * module subsystems, so we don't worry about them.
5509 */
5510 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005511 if (!strcmp(token, ss->name)) {
5512 ss->disabled = 1;
5513 printk(KERN_INFO "Disabling %s control group"
5514 " subsystem\n", ss->name);
5515 break;
5516 }
5517 }
5518 }
5519 return 1;
5520}
5521__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005522
5523/*
5524 * Functons for CSS ID.
5525 */
5526
Tejun Heo54766d42013-06-12 21:04:53 -07005527/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005528unsigned short css_id(struct cgroup_subsys_state *css)
5529{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005530 struct css_id *cssid;
5531
5532 /*
5533 * This css_id() can return correct value when somone has refcnt
5534 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5535 * it's unchanged until freed.
5536 */
Tejun Heod3daf282013-06-13 19:39:16 -07005537 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005538
5539 if (cssid)
5540 return cssid->id;
5541 return 0;
5542}
Ben Blum67523c42010-03-10 15:22:11 -08005543EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005544
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005545/**
5546 * css_is_ancestor - test "root" css is an ancestor of "child"
5547 * @child: the css to be tested.
5548 * @root: the css supporsed to be an ancestor of the child.
5549 *
5550 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005551 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005552 * But, considering usual usage, the csses should be valid objects after test.
5553 * Assuming that the caller will do some action to the child if this returns
5554 * returns true, the caller must take "child";s reference count.
5555 * If "child" is valid object and this returns true, "root" is valid, too.
5556 */
5557
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005558bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005559 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005560{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005561 struct css_id *child_id;
5562 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005563
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005564 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005565 if (!child_id)
5566 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005567 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005568 if (!root_id)
5569 return false;
5570 if (child_id->depth < root_id->depth)
5571 return false;
5572 if (child_id->stack[root_id->depth] != root_id->id)
5573 return false;
5574 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005575}
5576
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005577void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5578{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005579 struct css_id *id = rcu_dereference_protected(css->id, true);
5580
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005581 /* When this is called before css_id initialization, id can be NULL */
5582 if (!id)
5583 return;
5584
5585 BUG_ON(!ss->use_id);
5586
5587 rcu_assign_pointer(id->css, NULL);
5588 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005589 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005590 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005591 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005592 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005593}
Ben Blum67523c42010-03-10 15:22:11 -08005594EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005595
5596/*
5597 * This is called by init or create(). Then, calls to this function are
5598 * always serialized (By cgroup_mutex() at create()).
5599 */
5600
5601static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5602{
5603 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005604 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005605
5606 BUG_ON(!ss->use_id);
5607
5608 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5609 newid = kzalloc(size, GFP_KERNEL);
5610 if (!newid)
5611 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005612
5613 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005614 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005615 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005616 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005617 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005618 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005619
5620 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005621 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005622 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005623
Tejun Heod228d9e2013-02-27 17:04:54 -08005624 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005625 newid->depth = depth;
5626 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005627err_out:
5628 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005629 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005630
5631}
5632
Ben Blume6a11052010-03-10 15:22:09 -08005633static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5634 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005635{
5636 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005637
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005638 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005639 idr_init(&ss->idr);
5640
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005641 newid = get_new_cssid(ss, 0);
5642 if (IS_ERR(newid))
5643 return PTR_ERR(newid);
5644
5645 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005646 RCU_INIT_POINTER(newid->css, rootcss);
5647 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005648 return 0;
5649}
5650
Tejun Heo623f9262013-08-13 11:01:55 -04005651static int alloc_css_id(struct cgroup_subsys_state *child_css)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005652{
Tejun Heo623f9262013-08-13 11:01:55 -04005653 struct cgroup_subsys_state *parent_css = css_parent(child_css);
Li Zefanfae9c792010-04-22 17:30:00 +08005654 struct css_id *child_id, *parent_id;
Tejun Heo623f9262013-08-13 11:01:55 -04005655 int i, depth;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005656
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005657 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005658 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005659
Tejun Heo623f9262013-08-13 11:01:55 -04005660 child_id = get_new_cssid(child_css->ss, depth);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005661 if (IS_ERR(child_id))
5662 return PTR_ERR(child_id);
5663
5664 for (i = 0; i < depth; i++)
5665 child_id->stack[i] = parent_id->stack[i];
5666 child_id->stack[depth] = child_id->id;
5667 /*
5668 * child_id->css pointer will be set after this cgroup is available
5669 * see cgroup_populate_dir()
5670 */
5671 rcu_assign_pointer(child_css->id, child_id);
5672
5673 return 0;
5674}
5675
5676/**
5677 * css_lookup - lookup css by id
5678 * @ss: cgroup subsys to be looked into.
5679 * @id: the id
5680 *
5681 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5682 * NULL if not. Should be called under rcu_read_lock()
5683 */
5684struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5685{
5686 struct css_id *cssid = NULL;
5687
5688 BUG_ON(!ss->use_id);
5689 cssid = idr_find(&ss->idr, id);
5690
5691 if (unlikely(!cssid))
5692 return NULL;
5693
5694 return rcu_dereference(cssid->css);
5695}
Ben Blum67523c42010-03-10 15:22:11 -08005696EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005697
Tejun Heob77d7b62013-08-13 11:01:54 -04005698/**
Tejun Heo35cf0832013-08-26 18:40:56 -04005699 * css_from_dir - get corresponding css from the dentry of a cgroup dir
5700 * @dentry: directory dentry of interest
5701 * @ss: subsystem of interest
Tejun Heob77d7b62013-08-13 11:01:54 -04005702 *
5703 * Must be called under RCU read lock. The caller is responsible for
5704 * pinning the returned css if it needs to be accessed outside the RCU
5705 * critical section.
Stephane Eraniane5d13672011-02-14 11:20:01 +02005706 */
Tejun Heo35cf0832013-08-26 18:40:56 -04005707struct cgroup_subsys_state *css_from_dir(struct dentry *dentry,
5708 struct cgroup_subsys *ss)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005709{
5710 struct cgroup *cgrp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005711
Tejun Heob77d7b62013-08-13 11:01:54 -04005712 WARN_ON_ONCE(!rcu_read_lock_held());
5713
Tejun Heo35cf0832013-08-26 18:40:56 -04005714 /* is @dentry a cgroup dir? */
5715 if (!dentry->d_inode ||
5716 dentry->d_inode->i_op != &cgroup_dir_inode_operations)
Stephane Eraniane5d13672011-02-14 11:20:01 +02005717 return ERR_PTR(-EBADF);
5718
Tejun Heo35cf0832013-08-26 18:40:56 -04005719 cgrp = __d_cgrp(dentry);
Tejun Heoca8bdca2013-08-26 18:40:56 -04005720 return cgroup_css(cgrp, ss) ?: ERR_PTR(-ENOENT);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005721}
Stephane Eraniane5d13672011-02-14 11:20:01 +02005722
Li Zefan1cb650b2013-08-19 10:05:24 +08005723/**
5724 * css_from_id - lookup css by id
5725 * @id: the cgroup id
5726 * @ss: cgroup subsys to be looked into
5727 *
5728 * Returns the css if there's valid one with @id, otherwise returns NULL.
5729 * Should be called under rcu_read_lock().
5730 */
5731struct cgroup_subsys_state *css_from_id(int id, struct cgroup_subsys *ss)
5732{
5733 struct cgroup *cgrp;
5734
5735 rcu_lockdep_assert(rcu_read_lock_held() ||
5736 lockdep_is_held(&cgroup_mutex),
5737 "css_from_id() needs proper protection");
5738
5739 cgrp = idr_find(&ss->root->cgroup_idr, id);
5740 if (cgrp)
Tejun Heod1625962013-08-27 14:27:23 -04005741 return cgroup_css(cgrp, ss);
Li Zefan1cb650b2013-08-19 10:05:24 +08005742 return NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +02005743}
5744
Paul Menagefe693432009-09-23 15:56:20 -07005745#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005746static struct cgroup_subsys_state *
5747debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005748{
5749 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5750
5751 if (!css)
5752 return ERR_PTR(-ENOMEM);
5753
5754 return css;
5755}
5756
Tejun Heoeb954192013-08-08 20:11:23 -04005757static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005758{
Tejun Heoeb954192013-08-08 20:11:23 -04005759 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005760}
5761
Tejun Heo182446d2013-08-08 20:11:24 -04005762static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5763 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005764{
Tejun Heo182446d2013-08-08 20:11:24 -04005765 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005766}
5767
Tejun Heo182446d2013-08-08 20:11:24 -04005768static u64 current_css_set_read(struct cgroup_subsys_state *css,
5769 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005770{
5771 return (u64)(unsigned long)current->cgroups;
5772}
5773
Tejun Heo182446d2013-08-08 20:11:24 -04005774static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005775 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005776{
5777 u64 count;
5778
5779 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005780 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005781 rcu_read_unlock();
5782 return count;
5783}
5784
Tejun Heo182446d2013-08-08 20:11:24 -04005785static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005786 struct cftype *cft,
5787 struct seq_file *seq)
5788{
Tejun Heo69d02062013-06-12 21:04:50 -07005789 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005790 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005791
5792 read_lock(&css_set_lock);
5793 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005794 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005795 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005796 struct cgroup *c = link->cgrp;
5797 const char *name;
5798
5799 if (c->dentry)
5800 name = c->dentry->d_name.name;
5801 else
5802 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005803 seq_printf(seq, "Root %d group %s\n",
5804 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005805 }
5806 rcu_read_unlock();
5807 read_unlock(&css_set_lock);
5808 return 0;
5809}
5810
5811#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005812static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5813 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005814{
Tejun Heo69d02062013-06-12 21:04:50 -07005815 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005816
5817 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005818 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005819 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005820 struct task_struct *task;
5821 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005822 seq_printf(seq, "css_set %p\n", cset);
5823 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005824 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5825 seq_puts(seq, " ...\n");
5826 break;
5827 } else {
5828 seq_printf(seq, " task %d\n",
5829 task_pid_vnr(task));
5830 }
5831 }
5832 }
5833 read_unlock(&css_set_lock);
5834 return 0;
5835}
5836
Tejun Heo182446d2013-08-08 20:11:24 -04005837static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005838{
Tejun Heo182446d2013-08-08 20:11:24 -04005839 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005840}
5841
5842static struct cftype debug_files[] = {
5843 {
Paul Menagefe693432009-09-23 15:56:20 -07005844 .name = "taskcount",
5845 .read_u64 = debug_taskcount_read,
5846 },
5847
5848 {
5849 .name = "current_css_set",
5850 .read_u64 = current_css_set_read,
5851 },
5852
5853 {
5854 .name = "current_css_set_refcount",
5855 .read_u64 = current_css_set_refcount_read,
5856 },
5857
5858 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005859 .name = "current_css_set_cg_links",
5860 .read_seq_string = current_css_set_cg_links_read,
5861 },
5862
5863 {
5864 .name = "cgroup_css_links",
5865 .read_seq_string = cgroup_css_links_read,
5866 },
5867
5868 {
Paul Menagefe693432009-09-23 15:56:20 -07005869 .name = "releasable",
5870 .read_u64 = releasable_read,
5871 },
Paul Menagefe693432009-09-23 15:56:20 -07005872
Tejun Heo4baf6e32012-04-01 12:09:55 -07005873 { } /* terminate */
5874};
Paul Menagefe693432009-09-23 15:56:20 -07005875
5876struct cgroup_subsys debug_subsys = {
5877 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005878 .css_alloc = debug_css_alloc,
5879 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005880 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005881 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005882};
5883#endif /* CONFIG_CGROUP_DEBUG */