blob: c02a288a4e3d93783fddbedeb3aa1a8b3ae210ac [file] [log] [blame]
Paul Menageddbcc7e2007-10-18 23:39:30 -07001/*
Paul Menageddbcc7e2007-10-18 23:39:30 -07002 * Generic process-grouping system.
3 *
4 * Based originally on the cpuset system, extracted by Paul Menage
5 * Copyright (C) 2006 Google, Inc
6 *
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08007 * Notifications support
8 * Copyright (C) 2009 Nokia Corporation
9 * Author: Kirill A. Shutemov
10 *
Paul Menageddbcc7e2007-10-18 23:39:30 -070011 * Copyright notices from the original cpuset code:
12 * --------------------------------------------------
13 * Copyright (C) 2003 BULL SA.
14 * Copyright (C) 2004-2006 Silicon Graphics, Inc.
15 *
16 * Portions derived from Patrick Mochel's sysfs code.
17 * sysfs is Copyright (c) 2001-3 Patrick Mochel
18 *
19 * 2003-10-10 Written by Simon Derr.
20 * 2003-10-22 Updates by Stephen Hemminger.
21 * 2004 May-July Rework by Paul Jackson.
22 * ---------------------------------------------------
23 *
24 * This file is subject to the terms and conditions of the GNU General Public
25 * License. See the file COPYING in the main directory of the Linux
26 * distribution for more details.
27 */
28
29#include <linux/cgroup.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100030#include <linux/cred.h>
Paul Menagec6d57f32009-09-23 15:56:19 -070031#include <linux/ctype.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070032#include <linux/errno.h>
eparis@redhat2ce97382011-06-02 21:20:51 +100033#include <linux/init_task.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070034#include <linux/kernel.h>
35#include <linux/list.h>
36#include <linux/mm.h>
37#include <linux/mutex.h>
38#include <linux/mount.h>
39#include <linux/pagemap.h>
Paul Menagea4243162007-10-18 23:39:35 -070040#include <linux/proc_fs.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070041#include <linux/rcupdate.h>
42#include <linux/sched.h>
Paul Menage817929e2007-10-18 23:39:36 -070043#include <linux/backing-dev.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070044#include <linux/seq_file.h>
45#include <linux/slab.h>
46#include <linux/magic.h>
47#include <linux/spinlock.h>
48#include <linux/string.h>
Paul Menagebbcb81d2007-10-18 23:39:32 -070049#include <linux/sort.h>
Paul Menage81a6a5c2007-10-18 23:39:38 -070050#include <linux/kmod.h>
Ben Blume6a11052010-03-10 15:22:09 -080051#include <linux/module.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070052#include <linux/delayacct.h>
53#include <linux/cgroupstats.h>
Li Zefan0ac801f2013-01-10 11:49:27 +080054#include <linux/hashtable.h>
Al Viro3f8206d2008-07-26 03:46:43 -040055#include <linux/namei.h>
Li Zefan096b7fe2009-07-29 15:04:04 -070056#include <linux/pid_namespace.h>
Paul Menage2c6ab6d2009-09-23 15:56:23 -070057#include <linux/idr.h>
Ben Blumd1d9fd32009-09-23 15:56:28 -070058#include <linux/vmalloc.h> /* TODO: replace with more sophisticated array */
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -080059#include <linux/eventfd.h>
60#include <linux/poll.h>
Li Zefan081aa452013-03-13 09:17:09 +080061#include <linux/flex_array.h> /* used in cgroup_attach_task */
Mike Galbraithc4c27fb2012-04-21 09:13:46 +020062#include <linux/kthread.h>
Balbir Singh846c7bb2007-10-18 23:39:44 -070063
Arun Sharma600634972011-07-26 16:09:06 -070064#include <linux/atomic.h>
Paul Menageddbcc7e2007-10-18 23:39:30 -070065
Tejun Heoe25e2cb2011-12-12 18:12:21 -080066/*
67 * cgroup_mutex is the master lock. Any modification to cgroup or its
68 * hierarchy must be performed while holding it.
69 *
70 * cgroup_root_mutex nests inside cgroup_mutex and should be held to modify
71 * cgroupfs_root of any cgroup hierarchy - subsys list, flags,
72 * release_agent_path and so on. Modifying requires both cgroup_mutex and
73 * cgroup_root_mutex. Readers can acquire either of the two. This is to
74 * break the following locking order cycle.
75 *
76 * A. cgroup_mutex -> cred_guard_mutex -> s_type->i_mutex_key -> namespace_sem
77 * B. namespace_sem -> cgroup_mutex
78 *
79 * B happens only through cgroup_show_options() and using cgroup_root_mutex
80 * breaks it.
81 */
Tejun Heo22194492013-04-07 09:29:51 -070082#ifdef CONFIG_PROVE_RCU
83DEFINE_MUTEX(cgroup_mutex);
Tejun Heo8af01f52013-08-08 20:11:22 -040084EXPORT_SYMBOL_GPL(cgroup_mutex); /* only for lockdep */
Tejun Heo22194492013-04-07 09:29:51 -070085#else
Paul Menage81a6a5c2007-10-18 23:39:38 -070086static DEFINE_MUTEX(cgroup_mutex);
Tejun Heo22194492013-04-07 09:29:51 -070087#endif
88
Tejun Heoe25e2cb2011-12-12 18:12:21 -080089static DEFINE_MUTEX(cgroup_root_mutex);
Paul Menage81a6a5c2007-10-18 23:39:38 -070090
Ben Blumaae8aab2010-03-10 15:22:07 -080091/*
92 * Generate an array of cgroup subsystem pointers. At boot time, this is
Daniel Wagnerbe45c902012-09-13 09:50:55 +020093 * populated with the built in subsystems, and modular subsystems are
Ben Blumaae8aab2010-03-10 15:22:07 -080094 * registered after that. The mutable section of this array is protected by
95 * cgroup_mutex.
96 */
Daniel Wagner80f4c872012-09-12 16:12:06 +020097#define SUBSYS(_x) [_x ## _subsys_id] = &_x ## _subsys,
Daniel Wagner5fc0b022012-09-12 16:12:05 +020098#define IS_SUBSYS_ENABLED(option) IS_BUILTIN(option)
Tejun Heo9871bf92013-06-24 15:21:47 -070099static struct cgroup_subsys *cgroup_subsys[CGROUP_SUBSYS_COUNT] = {
Paul Menageddbcc7e2007-10-18 23:39:30 -0700100#include <linux/cgroup_subsys.h>
101};
102
Paul Menageddbcc7e2007-10-18 23:39:30 -0700103/*
Tejun Heo9871bf92013-06-24 15:21:47 -0700104 * The dummy hierarchy, reserved for the subsystems that are otherwise
105 * unattached - it never has more than a single cgroup, and all tasks are
106 * part of that cgroup.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700107 */
Tejun Heo9871bf92013-06-24 15:21:47 -0700108static struct cgroupfs_root cgroup_dummy_root;
109
110/* dummy_top is a shorthand for the dummy hierarchy's top cgroup */
111static struct cgroup * const cgroup_dummy_top = &cgroup_dummy_root.top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700112
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700113/*
Tejun Heo05ef1d72012-04-01 12:09:56 -0700114 * cgroupfs file entry, pointed to from leaf dentry->d_fsdata.
115 */
116struct cfent {
117 struct list_head node;
118 struct dentry *dentry;
119 struct cftype *type;
Li Zefan712317a2013-04-18 23:09:52 -0700120
121 /* file xattrs */
122 struct simple_xattrs xattrs;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700123};
124
125/*
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700126 * CSS ID -- ID per subsys's Cgroup Subsys State(CSS). used only when
127 * cgroup_subsys->use_id != 0.
128 */
129#define CSS_ID_MAX (65535)
130struct css_id {
131 /*
132 * The css to which this ID points. This pointer is set to valid value
133 * after cgroup is populated. If cgroup is removed, this will be NULL.
134 * This pointer is expected to be RCU-safe because destroy()
Tejun Heoe9316082012-11-05 09:16:58 -0800135 * is called after synchronize_rcu(). But for safe use, css_tryget()
136 * should be used for avoiding race.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700137 */
Arnd Bergmann2c392b82010-02-24 19:41:39 +0100138 struct cgroup_subsys_state __rcu *css;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700139 /*
140 * ID of this css.
141 */
142 unsigned short id;
143 /*
144 * Depth in hierarchy which this ID belongs to.
145 */
146 unsigned short depth;
147 /*
148 * ID is freed by RCU. (and lookup routine is RCU safe.)
149 */
150 struct rcu_head rcu_head;
151 /*
152 * Hierarchy of CSS ID belongs to.
153 */
154 unsigned short stack[0]; /* Array of Length (depth+1) */
155};
156
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800157/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300158 * cgroup_event represents events which userspace want to receive.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800159 */
160struct cgroup_event {
161 /*
Tejun Heo81eeaf02013-08-08 20:11:26 -0400162 * css which the event belongs to.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800163 */
Tejun Heo81eeaf02013-08-08 20:11:26 -0400164 struct cgroup_subsys_state *css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -0800165 /*
166 * Control file which the event associated.
167 */
168 struct cftype *cft;
169 /*
170 * eventfd to signal userspace about the event.
171 */
172 struct eventfd_ctx *eventfd;
173 /*
174 * Each of these stored in a list by the cgroup.
175 */
176 struct list_head list;
177 /*
178 * All fields below needed to unregister event when
179 * userspace closes eventfd.
180 */
181 poll_table pt;
182 wait_queue_head_t *wqh;
183 wait_queue_t wait;
184 struct work_struct remove;
185};
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700186
Paul Menageddbcc7e2007-10-18 23:39:30 -0700187/* The list of hierarchy roots */
188
Tejun Heo9871bf92013-06-24 15:21:47 -0700189static LIST_HEAD(cgroup_roots);
190static int cgroup_root_count;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700191
Tejun Heo54e7b4e2013-04-14 11:36:57 -0700192/*
193 * Hierarchy ID allocation and mapping. It follows the same exclusion
194 * rules as other root ops - both cgroup_mutex and cgroup_root_mutex for
195 * writes, either for reads.
196 */
Tejun Heo1a574232013-04-14 11:36:58 -0700197static DEFINE_IDR(cgroup_hierarchy_idr);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700198
Li Zefan65dff752013-03-01 15:01:56 +0800199static struct cgroup_name root_cgroup_name = { .name = "/" };
200
Li Zefan794611a2013-06-18 18:53:53 +0800201/*
202 * Assign a monotonically increasing serial number to cgroups. It
203 * guarantees cgroups with bigger numbers are newer than those with smaller
204 * numbers. Also, as cgroups are always appended to the parent's
205 * ->children list, it guarantees that sibling cgroups are always sorted in
Tejun Heo00356bd2013-06-18 11:14:22 -0700206 * the ascending serial number order on the list. Protected by
207 * cgroup_mutex.
Li Zefan794611a2013-06-18 18:53:53 +0800208 */
Tejun Heo00356bd2013-06-18 11:14:22 -0700209static u64 cgroup_serial_nr_next = 1;
Li Zefan794611a2013-06-18 18:53:53 +0800210
Paul Menageddbcc7e2007-10-18 23:39:30 -0700211/* This flag indicates whether tasks in the fork and exit paths should
Li Zefana043e3b2008-02-23 15:24:09 -0800212 * check for fork/exit handlers to call. This avoids us having to do
213 * extra work in the fork/exit path if none of the subsystems need to
214 * be called.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700215 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700216static int need_forkexit_callback __read_mostly;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700217
Tejun Heo628f7cd2013-06-28 16:24:11 -0700218static struct cftype cgroup_base_files[];
219
Tejun Heoea15f8c2013-06-13 19:27:42 -0700220static void cgroup_offline_fn(struct work_struct *work);
Tejun Heo42809dd2012-11-19 08:13:37 -0800221static int cgroup_destroy_locked(struct cgroup *cgrp);
Tejun Heo2bb566c2013-08-08 20:11:23 -0400222static int cgroup_addrm_files(struct cgroup *cgrp, struct cftype cfts[],
223 bool is_add);
Tejun Heo42809dd2012-11-19 08:13:37 -0800224
Tejun Heo95109b62013-08-08 20:11:27 -0400225/**
226 * cgroup_css - obtain a cgroup's css for the specified subsystem
227 * @cgrp: the cgroup of interest
228 * @subsys_id: the subsystem of interest
229 *
230 * Return @cgrp's css (cgroup_subsys_state) associated with @subsys_id.
231 */
232static struct cgroup_subsys_state *cgroup_css(struct cgroup *cgrp,
233 int subsys_id)
234{
235 return cgrp->subsys[subsys_id];
236}
237
Paul Menageddbcc7e2007-10-18 23:39:30 -0700238/* convenient tests for these bits */
Tejun Heo54766d42013-06-12 21:04:53 -0700239static inline bool cgroup_is_dead(const struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700240{
Tejun Heo54766d42013-06-12 21:04:53 -0700241 return test_bit(CGRP_DEAD, &cgrp->flags);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700242}
243
Li Zefan78574cf2013-04-08 19:00:38 -0700244/**
245 * cgroup_is_descendant - test ancestry
246 * @cgrp: the cgroup to be tested
247 * @ancestor: possible ancestor of @cgrp
248 *
249 * Test whether @cgrp is a descendant of @ancestor. It also returns %true
250 * if @cgrp == @ancestor. This function is safe to call as long as @cgrp
251 * and @ancestor are accessible.
252 */
253bool cgroup_is_descendant(struct cgroup *cgrp, struct cgroup *ancestor)
254{
255 while (cgrp) {
256 if (cgrp == ancestor)
257 return true;
258 cgrp = cgrp->parent;
259 }
260 return false;
261}
262EXPORT_SYMBOL_GPL(cgroup_is_descendant);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700263
Adrian Bunke9685a02008-02-07 00:13:46 -0800264static int cgroup_is_releasable(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700265{
266 const int bits =
Paul Menagebd89aab2007-10-18 23:40:44 -0700267 (1 << CGRP_RELEASABLE) |
268 (1 << CGRP_NOTIFY_ON_RELEASE);
269 return (cgrp->flags & bits) == bits;
Paul Menage81a6a5c2007-10-18 23:39:38 -0700270}
271
Adrian Bunke9685a02008-02-07 00:13:46 -0800272static int notify_on_release(const struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700273{
Paul Menagebd89aab2007-10-18 23:40:44 -0700274 return test_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700275}
276
Tejun Heo30159ec2013-06-25 11:53:37 -0700277/**
278 * for_each_subsys - iterate all loaded cgroup subsystems
279 * @ss: the iteration cursor
280 * @i: the index of @ss, CGROUP_SUBSYS_COUNT after reaching the end
281 *
282 * Should be called under cgroup_mutex.
283 */
284#define for_each_subsys(ss, i) \
285 for ((i) = 0; (i) < CGROUP_SUBSYS_COUNT; (i)++) \
286 if (({ lockdep_assert_held(&cgroup_mutex); \
287 !((ss) = cgroup_subsys[i]); })) { } \
288 else
289
290/**
291 * for_each_builtin_subsys - iterate all built-in cgroup subsystems
292 * @ss: the iteration cursor
293 * @i: the index of @ss, CGROUP_BUILTIN_SUBSYS_COUNT after reaching the end
294 *
295 * Bulit-in subsystems are always present and iteration itself doesn't
296 * require any synchronization.
297 */
298#define for_each_builtin_subsys(ss, i) \
299 for ((i) = 0; (i) < CGROUP_BUILTIN_SUBSYS_COUNT && \
300 (((ss) = cgroup_subsys[i]) || true); (i)++)
301
Tejun Heo5549c492013-06-24 15:21:48 -0700302/* iterate each subsystem attached to a hierarchy */
303#define for_each_root_subsys(root, ss) \
304 list_for_each_entry((ss), &(root)->subsys_list, sibling)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700305
Tejun Heo5549c492013-06-24 15:21:48 -0700306/* iterate across the active hierarchies */
307#define for_each_active_root(root) \
308 list_for_each_entry((root), &cgroup_roots, root_list)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700309
Tejun Heof6ea9372012-04-01 12:09:55 -0700310static inline struct cgroup *__d_cgrp(struct dentry *dentry)
311{
312 return dentry->d_fsdata;
313}
314
Tejun Heo05ef1d72012-04-01 12:09:56 -0700315static inline struct cfent *__d_cfe(struct dentry *dentry)
Tejun Heof6ea9372012-04-01 12:09:55 -0700316{
317 return dentry->d_fsdata;
318}
319
Tejun Heo05ef1d72012-04-01 12:09:56 -0700320static inline struct cftype *__d_cft(struct dentry *dentry)
321{
322 return __d_cfe(dentry)->type;
323}
324
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700325/**
326 * cgroup_lock_live_group - take cgroup_mutex and check that cgrp is alive.
327 * @cgrp: the cgroup to be checked for liveness
328 *
Tejun Heo47cfcd02013-04-07 09:29:51 -0700329 * On success, returns true; the mutex should be later unlocked. On
330 * failure returns false with no lock held.
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700331 */
Tejun Heob9777cf2013-04-07 09:29:51 -0700332static bool cgroup_lock_live_group(struct cgroup *cgrp)
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700333{
334 mutex_lock(&cgroup_mutex);
Tejun Heo54766d42013-06-12 21:04:53 -0700335 if (cgroup_is_dead(cgrp)) {
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700336 mutex_unlock(&cgroup_mutex);
337 return false;
338 }
339 return true;
340}
Tejun Heo7ae1bad2013-04-07 09:29:51 -0700341
Paul Menage81a6a5c2007-10-18 23:39:38 -0700342/* the list of cgroups eligible for automatic release. Protected by
343 * release_list_lock */
344static LIST_HEAD(release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +0200345static DEFINE_RAW_SPINLOCK(release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700346static void cgroup_release_agent(struct work_struct *work);
347static DECLARE_WORK(release_agent_work, cgroup_release_agent);
Paul Menagebd89aab2007-10-18 23:40:44 -0700348static void check_for_release(struct cgroup *cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700349
Tejun Heo69d02062013-06-12 21:04:50 -0700350/*
351 * A cgroup can be associated with multiple css_sets as different tasks may
352 * belong to different cgroups on different hierarchies. In the other
353 * direction, a css_set is naturally associated with multiple cgroups.
354 * This M:N relationship is represented by the following link structure
355 * which exists for each association and allows traversing the associations
356 * from both sides.
357 */
358struct cgrp_cset_link {
359 /* the cgroup and css_set this link associates */
360 struct cgroup *cgrp;
361 struct css_set *cset;
362
363 /* list of cgrp_cset_links anchored at cgrp->cset_links */
364 struct list_head cset_link;
365
366 /* list of cgrp_cset_links anchored at css_set->cgrp_links */
367 struct list_head cgrp_link;
Paul Menage817929e2007-10-18 23:39:36 -0700368};
369
370/* The default css_set - used by init and its children prior to any
371 * hierarchies being mounted. It contains a pointer to the root state
372 * for each subsystem. Also used to anchor the list of css_sets. Not
373 * reference-counted, to improve performance when child cgroups
374 * haven't been created.
375 */
376
377static struct css_set init_css_set;
Tejun Heo69d02062013-06-12 21:04:50 -0700378static struct cgrp_cset_link init_cgrp_cset_link;
Paul Menage817929e2007-10-18 23:39:36 -0700379
Ben Blume6a11052010-03-10 15:22:09 -0800380static int cgroup_init_idr(struct cgroup_subsys *ss,
381 struct cgroup_subsys_state *css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700382
Tejun Heo0942eee2013-08-08 20:11:26 -0400383/*
384 * css_set_lock protects the list of css_set objects, and the chain of
385 * tasks off each css_set. Nests outside task->alloc_lock due to
Tejun Heo72ec7022013-08-08 20:11:26 -0400386 * css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -0400387 */
Paul Menage817929e2007-10-18 23:39:36 -0700388static DEFINE_RWLOCK(css_set_lock);
389static int css_set_count;
390
Paul Menage7717f7b2009-09-23 15:56:22 -0700391/*
392 * hash table for cgroup groups. This improves the performance to find
393 * an existing css_set. This hash doesn't (currently) take into
394 * account cgroups in empty hierarchies.
395 */
Li Zefan472b1052008-04-29 01:00:11 -0700396#define CSS_SET_HASH_BITS 7
Li Zefan0ac801f2013-01-10 11:49:27 +0800397static DEFINE_HASHTABLE(css_set_table, CSS_SET_HASH_BITS);
Li Zefan472b1052008-04-29 01:00:11 -0700398
Li Zefan0ac801f2013-01-10 11:49:27 +0800399static unsigned long css_set_hash(struct cgroup_subsys_state *css[])
Li Zefan472b1052008-04-29 01:00:11 -0700400{
Li Zefan0ac801f2013-01-10 11:49:27 +0800401 unsigned long key = 0UL;
Tejun Heo30159ec2013-06-25 11:53:37 -0700402 struct cgroup_subsys *ss;
403 int i;
Li Zefan472b1052008-04-29 01:00:11 -0700404
Tejun Heo30159ec2013-06-25 11:53:37 -0700405 for_each_subsys(ss, i)
Li Zefan0ac801f2013-01-10 11:49:27 +0800406 key += (unsigned long)css[i];
407 key = (key >> 16) ^ key;
Li Zefan472b1052008-04-29 01:00:11 -0700408
Li Zefan0ac801f2013-01-10 11:49:27 +0800409 return key;
Li Zefan472b1052008-04-29 01:00:11 -0700410}
411
Tejun Heo0942eee2013-08-08 20:11:26 -0400412/*
413 * We don't maintain the lists running through each css_set to its task
Tejun Heo72ec7022013-08-08 20:11:26 -0400414 * until after the first call to css_task_iter_start(). This reduces the
415 * fork()/exit() overhead for people who have cgroups compiled into their
416 * kernel but not actually in use.
Tejun Heo0942eee2013-08-08 20:11:26 -0400417 */
Li Zefan8947f9d2008-07-25 01:46:56 -0700418static int use_task_css_set_links __read_mostly;
Paul Menage817929e2007-10-18 23:39:36 -0700419
Tejun Heo5abb8852013-06-12 21:04:49 -0700420static void __put_css_set(struct css_set *cset, int taskexit)
Paul Menageb4f48b62007-10-18 23:39:33 -0700421{
Tejun Heo69d02062013-06-12 21:04:50 -0700422 struct cgrp_cset_link *link, *tmp_link;
Tejun Heo5abb8852013-06-12 21:04:49 -0700423
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700424 /*
425 * Ensure that the refcount doesn't hit zero while any readers
426 * can see it. Similar to atomic_dec_and_lock(), but for an
427 * rwlock
428 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700429 if (atomic_add_unless(&cset->refcount, -1, 1))
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700430 return;
431 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700432 if (!atomic_dec_and_test(&cset->refcount)) {
Lai Jiangshan146aa1b2008-10-18 20:28:03 -0700433 write_unlock(&css_set_lock);
434 return;
435 }
Paul Menage81a6a5c2007-10-18 23:39:38 -0700436
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700437 /* This css_set is dead. unlink it and release cgroup refcounts */
Tejun Heo5abb8852013-06-12 21:04:49 -0700438 hash_del(&cset->hlist);
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700439 css_set_count--;
440
Tejun Heo69d02062013-06-12 21:04:50 -0700441 list_for_each_entry_safe(link, tmp_link, &cset->cgrp_links, cgrp_link) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700442 struct cgroup *cgrp = link->cgrp;
Tejun Heo5abb8852013-06-12 21:04:49 -0700443
Tejun Heo69d02062013-06-12 21:04:50 -0700444 list_del(&link->cset_link);
445 list_del(&link->cgrp_link);
Li Zefan71b57072013-01-24 14:43:28 +0800446
Tejun Heoddd69142013-06-12 21:04:54 -0700447 /* @cgrp can't go away while we're holding css_set_lock */
Tejun Heo6f3d828f02013-06-12 21:04:55 -0700448 if (list_empty(&cgrp->cset_links) && notify_on_release(cgrp)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -0700449 if (taskexit)
Paul Menagebd89aab2007-10-18 23:40:44 -0700450 set_bit(CGRP_RELEASABLE, &cgrp->flags);
451 check_for_release(cgrp);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700452 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700453
454 kfree(link);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700455 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -0700456
457 write_unlock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700458 kfree_rcu(cset, rcu_head);
Paul Menage817929e2007-10-18 23:39:36 -0700459}
460
461/*
462 * refcounted get/put for css_set objects
463 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700464static inline void get_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700465{
Tejun Heo5abb8852013-06-12 21:04:49 -0700466 atomic_inc(&cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -0700467}
468
Tejun Heo5abb8852013-06-12 21:04:49 -0700469static inline void put_css_set(struct css_set *cset)
Paul Menage817929e2007-10-18 23:39:36 -0700470{
Tejun Heo5abb8852013-06-12 21:04:49 -0700471 __put_css_set(cset, 0);
Paul Menage817929e2007-10-18 23:39:36 -0700472}
473
Tejun Heo5abb8852013-06-12 21:04:49 -0700474static inline void put_css_set_taskexit(struct css_set *cset)
Paul Menage81a6a5c2007-10-18 23:39:38 -0700475{
Tejun Heo5abb8852013-06-12 21:04:49 -0700476 __put_css_set(cset, 1);
Paul Menage81a6a5c2007-10-18 23:39:38 -0700477}
478
Tejun Heob326f9d2013-06-24 15:21:48 -0700479/**
Paul Menage7717f7b2009-09-23 15:56:22 -0700480 * compare_css_sets - helper function for find_existing_css_set().
Tejun Heo5abb8852013-06-12 21:04:49 -0700481 * @cset: candidate css_set being tested
482 * @old_cset: existing css_set for a task
Paul Menage7717f7b2009-09-23 15:56:22 -0700483 * @new_cgrp: cgroup that's being entered by the task
484 * @template: desired set of css pointers in css_set (pre-calculated)
485 *
Li Zefan6f4b7e62013-07-31 16:18:36 +0800486 * Returns true if "cset" matches "old_cset" except for the hierarchy
Paul Menage7717f7b2009-09-23 15:56:22 -0700487 * which "new_cgrp" belongs to, for which it should match "new_cgrp".
488 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700489static bool compare_css_sets(struct css_set *cset,
490 struct css_set *old_cset,
Paul Menage7717f7b2009-09-23 15:56:22 -0700491 struct cgroup *new_cgrp,
492 struct cgroup_subsys_state *template[])
493{
494 struct list_head *l1, *l2;
495
Tejun Heo5abb8852013-06-12 21:04:49 -0700496 if (memcmp(template, cset->subsys, sizeof(cset->subsys))) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700497 /* Not all subsystems matched */
498 return false;
499 }
500
501 /*
502 * Compare cgroup pointers in order to distinguish between
503 * different cgroups in heirarchies with no subsystems. We
504 * could get by with just this check alone (and skip the
505 * memcmp above) but on most setups the memcmp check will
506 * avoid the need for this more expensive check on almost all
507 * candidates.
508 */
509
Tejun Heo69d02062013-06-12 21:04:50 -0700510 l1 = &cset->cgrp_links;
511 l2 = &old_cset->cgrp_links;
Paul Menage7717f7b2009-09-23 15:56:22 -0700512 while (1) {
Tejun Heo69d02062013-06-12 21:04:50 -0700513 struct cgrp_cset_link *link1, *link2;
Tejun Heo5abb8852013-06-12 21:04:49 -0700514 struct cgroup *cgrp1, *cgrp2;
Paul Menage7717f7b2009-09-23 15:56:22 -0700515
516 l1 = l1->next;
517 l2 = l2->next;
518 /* See if we reached the end - both lists are equal length. */
Tejun Heo69d02062013-06-12 21:04:50 -0700519 if (l1 == &cset->cgrp_links) {
520 BUG_ON(l2 != &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700521 break;
522 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700523 BUG_ON(l2 == &old_cset->cgrp_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700524 }
525 /* Locate the cgroups associated with these links. */
Tejun Heo69d02062013-06-12 21:04:50 -0700526 link1 = list_entry(l1, struct cgrp_cset_link, cgrp_link);
527 link2 = list_entry(l2, struct cgrp_cset_link, cgrp_link);
528 cgrp1 = link1->cgrp;
529 cgrp2 = link2->cgrp;
Paul Menage7717f7b2009-09-23 15:56:22 -0700530 /* Hierarchies should be linked in the same order. */
Tejun Heo5abb8852013-06-12 21:04:49 -0700531 BUG_ON(cgrp1->root != cgrp2->root);
Paul Menage7717f7b2009-09-23 15:56:22 -0700532
533 /*
534 * If this hierarchy is the hierarchy of the cgroup
535 * that's changing, then we need to check that this
536 * css_set points to the new cgroup; if it's any other
537 * hierarchy, then this css_set should point to the
538 * same cgroup as the old css_set.
539 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700540 if (cgrp1->root == new_cgrp->root) {
541 if (cgrp1 != new_cgrp)
Paul Menage7717f7b2009-09-23 15:56:22 -0700542 return false;
543 } else {
Tejun Heo5abb8852013-06-12 21:04:49 -0700544 if (cgrp1 != cgrp2)
Paul Menage7717f7b2009-09-23 15:56:22 -0700545 return false;
546 }
547 }
548 return true;
549}
550
Tejun Heob326f9d2013-06-24 15:21:48 -0700551/**
552 * find_existing_css_set - init css array and find the matching css_set
553 * @old_cset: the css_set that we're using before the cgroup transition
554 * @cgrp: the cgroup that we're moving into
555 * @template: out param for the new set of csses, should be clear on entry
Paul Menage817929e2007-10-18 23:39:36 -0700556 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700557static struct css_set *find_existing_css_set(struct css_set *old_cset,
558 struct cgroup *cgrp,
559 struct cgroup_subsys_state *template[])
Paul Menage817929e2007-10-18 23:39:36 -0700560{
Paul Menagebd89aab2007-10-18 23:40:44 -0700561 struct cgroupfs_root *root = cgrp->root;
Tejun Heo30159ec2013-06-25 11:53:37 -0700562 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -0700563 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +0800564 unsigned long key;
Tejun Heob326f9d2013-06-24 15:21:48 -0700565 int i;
Paul Menage817929e2007-10-18 23:39:36 -0700566
Ben Blumaae8aab2010-03-10 15:22:07 -0800567 /*
568 * Build the set of subsystem state objects that we want to see in the
569 * new css_set. while subsystems can change globally, the entries here
570 * won't change, so no need for locking.
571 */
Tejun Heo30159ec2013-06-25 11:53:37 -0700572 for_each_subsys(ss, i) {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -0400573 if (root->subsys_mask & (1UL << i)) {
Paul Menage817929e2007-10-18 23:39:36 -0700574 /* Subsystem is in this hierarchy. So we want
575 * the subsystem state from the new
576 * cgroup */
Paul Menagebd89aab2007-10-18 23:40:44 -0700577 template[i] = cgrp->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700578 } else {
579 /* Subsystem is not in this hierarchy, so we
580 * don't want to change the subsystem state */
Tejun Heo5abb8852013-06-12 21:04:49 -0700581 template[i] = old_cset->subsys[i];
Paul Menage817929e2007-10-18 23:39:36 -0700582 }
583 }
584
Li Zefan0ac801f2013-01-10 11:49:27 +0800585 key = css_set_hash(template);
Tejun Heo5abb8852013-06-12 21:04:49 -0700586 hash_for_each_possible(css_set_table, cset, hlist, key) {
587 if (!compare_css_sets(cset, old_cset, cgrp, template))
Paul Menage7717f7b2009-09-23 15:56:22 -0700588 continue;
589
590 /* This css_set matches what we need */
Tejun Heo5abb8852013-06-12 21:04:49 -0700591 return cset;
Li Zefan472b1052008-04-29 01:00:11 -0700592 }
Paul Menage817929e2007-10-18 23:39:36 -0700593
594 /* No existing cgroup group matched */
595 return NULL;
596}
597
Tejun Heo69d02062013-06-12 21:04:50 -0700598static void free_cgrp_cset_links(struct list_head *links_to_free)
Paul Menage817929e2007-10-18 23:39:36 -0700599{
Tejun Heo69d02062013-06-12 21:04:50 -0700600 struct cgrp_cset_link *link, *tmp_link;
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -0700601
Tejun Heo69d02062013-06-12 21:04:50 -0700602 list_for_each_entry_safe(link, tmp_link, links_to_free, cset_link) {
603 list_del(&link->cset_link);
Paul Menage817929e2007-10-18 23:39:36 -0700604 kfree(link);
605 }
606}
607
Tejun Heo69d02062013-06-12 21:04:50 -0700608/**
609 * allocate_cgrp_cset_links - allocate cgrp_cset_links
610 * @count: the number of links to allocate
611 * @tmp_links: list_head the allocated links are put on
612 *
613 * Allocate @count cgrp_cset_link structures and chain them on @tmp_links
614 * through ->cset_link. Returns 0 on success or -errno.
Li Zefan36553432008-07-29 22:33:19 -0700615 */
Tejun Heo69d02062013-06-12 21:04:50 -0700616static int allocate_cgrp_cset_links(int count, struct list_head *tmp_links)
Li Zefan36553432008-07-29 22:33:19 -0700617{
Tejun Heo69d02062013-06-12 21:04:50 -0700618 struct cgrp_cset_link *link;
Li Zefan36553432008-07-29 22:33:19 -0700619 int i;
Tejun Heo69d02062013-06-12 21:04:50 -0700620
621 INIT_LIST_HEAD(tmp_links);
622
Li Zefan36553432008-07-29 22:33:19 -0700623 for (i = 0; i < count; i++) {
Tejun Heof4f4be22013-06-12 21:04:51 -0700624 link = kzalloc(sizeof(*link), GFP_KERNEL);
Li Zefan36553432008-07-29 22:33:19 -0700625 if (!link) {
Tejun Heo69d02062013-06-12 21:04:50 -0700626 free_cgrp_cset_links(tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700627 return -ENOMEM;
628 }
Tejun Heo69d02062013-06-12 21:04:50 -0700629 list_add(&link->cset_link, tmp_links);
Li Zefan36553432008-07-29 22:33:19 -0700630 }
631 return 0;
632}
633
Li Zefanc12f65d2009-01-07 18:07:42 -0800634/**
635 * link_css_set - a helper function to link a css_set to a cgroup
Tejun Heo69d02062013-06-12 21:04:50 -0700636 * @tmp_links: cgrp_cset_link objects allocated by allocate_cgrp_cset_links()
Tejun Heo5abb8852013-06-12 21:04:49 -0700637 * @cset: the css_set to be linked
Li Zefanc12f65d2009-01-07 18:07:42 -0800638 * @cgrp: the destination cgroup
639 */
Tejun Heo69d02062013-06-12 21:04:50 -0700640static void link_css_set(struct list_head *tmp_links, struct css_set *cset,
641 struct cgroup *cgrp)
Li Zefanc12f65d2009-01-07 18:07:42 -0800642{
Tejun Heo69d02062013-06-12 21:04:50 -0700643 struct cgrp_cset_link *link;
Li Zefanc12f65d2009-01-07 18:07:42 -0800644
Tejun Heo69d02062013-06-12 21:04:50 -0700645 BUG_ON(list_empty(tmp_links));
646 link = list_first_entry(tmp_links, struct cgrp_cset_link, cset_link);
647 link->cset = cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700648 link->cgrp = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700649 list_move(&link->cset_link, &cgrp->cset_links);
Paul Menage7717f7b2009-09-23 15:56:22 -0700650 /*
651 * Always add links to the tail of the list so that the list
652 * is sorted by order of hierarchy creation
653 */
Tejun Heo69d02062013-06-12 21:04:50 -0700654 list_add_tail(&link->cgrp_link, &cset->cgrp_links);
Li Zefanc12f65d2009-01-07 18:07:42 -0800655}
656
Tejun Heob326f9d2013-06-24 15:21:48 -0700657/**
658 * find_css_set - return a new css_set with one cgroup updated
659 * @old_cset: the baseline css_set
660 * @cgrp: the cgroup to be updated
661 *
662 * Return a new css_set that's equivalent to @old_cset, but with @cgrp
663 * substituted into the appropriate hierarchy.
Paul Menage817929e2007-10-18 23:39:36 -0700664 */
Tejun Heo5abb8852013-06-12 21:04:49 -0700665static struct css_set *find_css_set(struct css_set *old_cset,
666 struct cgroup *cgrp)
Paul Menage817929e2007-10-18 23:39:36 -0700667{
Tejun Heob326f9d2013-06-24 15:21:48 -0700668 struct cgroup_subsys_state *template[CGROUP_SUBSYS_COUNT] = { };
Tejun Heo5abb8852013-06-12 21:04:49 -0700669 struct css_set *cset;
Tejun Heo69d02062013-06-12 21:04:50 -0700670 struct list_head tmp_links;
671 struct cgrp_cset_link *link;
Li Zefan0ac801f2013-01-10 11:49:27 +0800672 unsigned long key;
Li Zefan472b1052008-04-29 01:00:11 -0700673
Tejun Heob326f9d2013-06-24 15:21:48 -0700674 lockdep_assert_held(&cgroup_mutex);
675
Paul Menage817929e2007-10-18 23:39:36 -0700676 /* First see if we already have a cgroup group that matches
677 * the desired set */
Li Zefan7e9abd82008-07-25 01:46:54 -0700678 read_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -0700679 cset = find_existing_css_set(old_cset, cgrp, template);
680 if (cset)
681 get_css_set(cset);
Li Zefan7e9abd82008-07-25 01:46:54 -0700682 read_unlock(&css_set_lock);
Paul Menage817929e2007-10-18 23:39:36 -0700683
Tejun Heo5abb8852013-06-12 21:04:49 -0700684 if (cset)
685 return cset;
Paul Menage817929e2007-10-18 23:39:36 -0700686
Tejun Heof4f4be22013-06-12 21:04:51 -0700687 cset = kzalloc(sizeof(*cset), GFP_KERNEL);
Tejun Heo5abb8852013-06-12 21:04:49 -0700688 if (!cset)
Paul Menage817929e2007-10-18 23:39:36 -0700689 return NULL;
690
Tejun Heo69d02062013-06-12 21:04:50 -0700691 /* Allocate all the cgrp_cset_link objects that we'll need */
Tejun Heo9871bf92013-06-24 15:21:47 -0700692 if (allocate_cgrp_cset_links(cgroup_root_count, &tmp_links) < 0) {
Tejun Heo5abb8852013-06-12 21:04:49 -0700693 kfree(cset);
Paul Menage817929e2007-10-18 23:39:36 -0700694 return NULL;
695 }
696
Tejun Heo5abb8852013-06-12 21:04:49 -0700697 atomic_set(&cset->refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -0700698 INIT_LIST_HEAD(&cset->cgrp_links);
Tejun Heo5abb8852013-06-12 21:04:49 -0700699 INIT_LIST_HEAD(&cset->tasks);
700 INIT_HLIST_NODE(&cset->hlist);
Paul Menage817929e2007-10-18 23:39:36 -0700701
702 /* Copy the set of subsystem state objects generated in
703 * find_existing_css_set() */
Tejun Heo5abb8852013-06-12 21:04:49 -0700704 memcpy(cset->subsys, template, sizeof(cset->subsys));
Paul Menage817929e2007-10-18 23:39:36 -0700705
706 write_lock(&css_set_lock);
707 /* Add reference counts and links from the new css_set. */
Tejun Heo69d02062013-06-12 21:04:50 -0700708 list_for_each_entry(link, &old_cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700709 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700710
Paul Menage7717f7b2009-09-23 15:56:22 -0700711 if (c->root == cgrp->root)
712 c = cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700713 link_css_set(&tmp_links, cset, c);
Paul Menage7717f7b2009-09-23 15:56:22 -0700714 }
Paul Menage817929e2007-10-18 23:39:36 -0700715
Tejun Heo69d02062013-06-12 21:04:50 -0700716 BUG_ON(!list_empty(&tmp_links));
Paul Menage817929e2007-10-18 23:39:36 -0700717
Paul Menage817929e2007-10-18 23:39:36 -0700718 css_set_count++;
Li Zefan472b1052008-04-29 01:00:11 -0700719
720 /* Add this cgroup group to the hash table */
Tejun Heo5abb8852013-06-12 21:04:49 -0700721 key = css_set_hash(cset->subsys);
722 hash_add(css_set_table, &cset->hlist, key);
Li Zefan472b1052008-04-29 01:00:11 -0700723
Paul Menage817929e2007-10-18 23:39:36 -0700724 write_unlock(&css_set_lock);
725
Tejun Heo5abb8852013-06-12 21:04:49 -0700726 return cset;
Paul Menageb4f48b62007-10-18 23:39:33 -0700727}
728
Paul Menageddbcc7e2007-10-18 23:39:30 -0700729/*
Paul Menage7717f7b2009-09-23 15:56:22 -0700730 * Return the cgroup for "task" from the given hierarchy. Must be
731 * called with cgroup_mutex held.
732 */
733static struct cgroup *task_cgroup_from_root(struct task_struct *task,
734 struct cgroupfs_root *root)
735{
Tejun Heo5abb8852013-06-12 21:04:49 -0700736 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -0700737 struct cgroup *res = NULL;
738
739 BUG_ON(!mutex_is_locked(&cgroup_mutex));
740 read_lock(&css_set_lock);
741 /*
742 * No need to lock the task - since we hold cgroup_mutex the
743 * task can't change groups, so the only thing that can happen
744 * is that it exits and its css is set back to init_css_set.
745 */
Tejun Heoa8ad8052013-06-21 15:52:04 -0700746 cset = task_css_set(task);
Tejun Heo5abb8852013-06-12 21:04:49 -0700747 if (cset == &init_css_set) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700748 res = &root->top_cgroup;
749 } else {
Tejun Heo69d02062013-06-12 21:04:50 -0700750 struct cgrp_cset_link *link;
751
752 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -0700753 struct cgroup *c = link->cgrp;
Tejun Heo69d02062013-06-12 21:04:50 -0700754
Paul Menage7717f7b2009-09-23 15:56:22 -0700755 if (c->root == root) {
756 res = c;
757 break;
758 }
759 }
760 }
761 read_unlock(&css_set_lock);
762 BUG_ON(!res);
763 return res;
764}
765
766/*
Paul Menageddbcc7e2007-10-18 23:39:30 -0700767 * There is one global cgroup mutex. We also require taking
768 * task_lock() when dereferencing a task's cgroup subsys pointers.
769 * See "The task_lock() exception", at the end of this comment.
770 *
771 * A task must hold cgroup_mutex to modify cgroups.
772 *
773 * Any task can increment and decrement the count field without lock.
774 * So in general, code holding cgroup_mutex can't rely on the count
775 * field not changing. However, if the count goes to zero, then only
Cliff Wickman956db3c2008-02-07 00:14:43 -0800776 * cgroup_attach_task() can increment it again. Because a count of zero
Paul Menageddbcc7e2007-10-18 23:39:30 -0700777 * means that no tasks are currently attached, therefore there is no
778 * way a task attached to that cgroup can fork (the other way to
779 * increment the count). So code holding cgroup_mutex can safely
780 * assume that if the count is zero, it will stay zero. Similarly, if
781 * a task holds cgroup_mutex on a cgroup with zero count, it
782 * knows that the cgroup won't be removed, as cgroup_rmdir()
783 * needs that mutex.
784 *
Paul Menageddbcc7e2007-10-18 23:39:30 -0700785 * The fork and exit callbacks cgroup_fork() and cgroup_exit(), don't
786 * (usually) take cgroup_mutex. These are the two most performance
787 * critical pieces of code here. The exception occurs on cgroup_exit(),
788 * when a task in a notify_on_release cgroup exits. Then cgroup_mutex
789 * is taken, and if the cgroup count is zero, a usermode call made
Li Zefana043e3b2008-02-23 15:24:09 -0800790 * to the release agent with the name of the cgroup (path relative to
791 * the root of cgroup file system) as the argument.
Paul Menageddbcc7e2007-10-18 23:39:30 -0700792 *
793 * A cgroup can only be deleted if both its 'count' of using tasks
794 * is zero, and its list of 'children' cgroups is empty. Since all
795 * tasks in the system use _some_ cgroup, and since there is always at
796 * least one task in the system (init, pid == 1), therefore, top_cgroup
797 * always has either children cgroups and/or using tasks. So we don't
798 * need a special hack to ensure that top_cgroup cannot be deleted.
799 *
800 * The task_lock() exception
801 *
802 * The need for this exception arises from the action of
Tao Mad0b2fdd2012-11-20 22:06:18 +0800803 * cgroup_attach_task(), which overwrites one task's cgroup pointer with
Li Zefana043e3b2008-02-23 15:24:09 -0800804 * another. It does so using cgroup_mutex, however there are
Paul Menageddbcc7e2007-10-18 23:39:30 -0700805 * several performance critical places that need to reference
806 * task->cgroup without the expense of grabbing a system global
807 * mutex. Therefore except as noted below, when dereferencing or, as
Tao Mad0b2fdd2012-11-20 22:06:18 +0800808 * in cgroup_attach_task(), modifying a task's cgroup pointer we use
Paul Menageddbcc7e2007-10-18 23:39:30 -0700809 * task_lock(), which acts on a spinlock (task->alloc_lock) already in
810 * the task_struct routinely used for such matters.
811 *
812 * P.S. One more locking exception. RCU is used to guard the
Cliff Wickman956db3c2008-02-07 00:14:43 -0800813 * update of a tasks cgroup pointer by cgroup_attach_task()
Paul Menageddbcc7e2007-10-18 23:39:30 -0700814 */
815
Paul Menageddbcc7e2007-10-18 23:39:30 -0700816/*
817 * A couple of forward declarations required, due to cyclic reference loop:
818 * cgroup_mkdir -> cgroup_create -> cgroup_populate_dir ->
819 * cgroup_add_file -> cgroup_create_file -> cgroup_dir_inode_operations
820 * -> cgroup_mkdir.
821 */
822
Al Viro18bb1db2011-07-26 01:41:39 -0400823static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
Al Viro00cd8dd2012-06-10 17:13:09 -0400824static struct dentry *cgroup_lookup(struct inode *, struct dentry *, unsigned int);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700825static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry);
Tejun Heo628f7cd2013-06-28 16:24:11 -0700826static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask);
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -0700827static const struct inode_operations cgroup_dir_inode_operations;
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700828static const struct file_operations proc_cgroupstats_operations;
Paul Menagea4243162007-10-18 23:39:35 -0700829
830static struct backing_dev_info cgroup_backing_dev_info = {
Jens Axboed9938312009-06-12 14:45:52 +0200831 .name = "cgroup",
Miklos Szeredie4ad08f2008-04-30 00:54:37 -0700832 .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
Paul Menagea4243162007-10-18 23:39:35 -0700833};
Paul Menageddbcc7e2007-10-18 23:39:30 -0700834
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700835static int alloc_css_id(struct cgroup_subsys *ss,
836 struct cgroup *parent, struct cgroup *child);
837
Al Viroa5e7ed32011-07-26 01:55:55 -0400838static struct inode *cgroup_new_inode(umode_t mode, struct super_block *sb)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700839{
840 struct inode *inode = new_inode(sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700841
842 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400843 inode->i_ino = get_next_ino();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700844 inode->i_mode = mode;
David Howells76aac0e2008-11-14 10:39:12 +1100845 inode->i_uid = current_fsuid();
846 inode->i_gid = current_fsgid();
Paul Menageddbcc7e2007-10-18 23:39:30 -0700847 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
848 inode->i_mapping->backing_dev_info = &cgroup_backing_dev_info;
849 }
850 return inode;
851}
852
Li Zefan65dff752013-03-01 15:01:56 +0800853static struct cgroup_name *cgroup_alloc_name(struct dentry *dentry)
854{
855 struct cgroup_name *name;
856
857 name = kmalloc(sizeof(*name) + dentry->d_name.len + 1, GFP_KERNEL);
858 if (!name)
859 return NULL;
860 strcpy(name->name, dentry->d_name.name);
861 return name;
862}
863
Li Zefanbe445622013-01-24 14:31:42 +0800864static void cgroup_free_fn(struct work_struct *work)
865{
Tejun Heoea15f8c2013-06-13 19:27:42 -0700866 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800867 struct cgroup_subsys *ss;
868
869 mutex_lock(&cgroup_mutex);
870 /*
871 * Release the subsystem state objects.
872 */
Tejun Heoeb954192013-08-08 20:11:23 -0400873 for_each_root_subsys(cgrp->root, ss) {
874 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
875
876 ss->css_free(css);
877 }
Li Zefanbe445622013-01-24 14:31:42 +0800878
879 cgrp->root->number_of_cgroups--;
880 mutex_unlock(&cgroup_mutex);
881
882 /*
Li Zefan415cf072013-04-08 14:35:02 +0800883 * We get a ref to the parent's dentry, and put the ref when
884 * this cgroup is being freed, so it's guaranteed that the
885 * parent won't be destroyed before its children.
886 */
887 dput(cgrp->parent->dentry);
888
889 /*
Li Zefanbe445622013-01-24 14:31:42 +0800890 * Drop the active superblock reference that we took when we
Li Zefancc20e012013-04-26 11:58:02 -0700891 * created the cgroup. This will free cgrp->root, if we are
892 * holding the last reference to @sb.
Li Zefanbe445622013-01-24 14:31:42 +0800893 */
894 deactivate_super(cgrp->root->sb);
895
896 /*
897 * if we're getting rid of the cgroup, refcount should ensure
898 * that there are no pidlists left.
899 */
900 BUG_ON(!list_empty(&cgrp->pidlists));
901
902 simple_xattrs_free(&cgrp->xattrs);
903
Li Zefan65dff752013-03-01 15:01:56 +0800904 kfree(rcu_dereference_raw(cgrp->name));
Li Zefanbe445622013-01-24 14:31:42 +0800905 kfree(cgrp);
906}
907
908static void cgroup_free_rcu(struct rcu_head *head)
909{
910 struct cgroup *cgrp = container_of(head, struct cgroup, rcu_head);
911
Tejun Heoea15f8c2013-06-13 19:27:42 -0700912 INIT_WORK(&cgrp->destroy_work, cgroup_free_fn);
913 schedule_work(&cgrp->destroy_work);
Li Zefanbe445622013-01-24 14:31:42 +0800914}
915
Paul Menageddbcc7e2007-10-18 23:39:30 -0700916static void cgroup_diput(struct dentry *dentry, struct inode *inode)
917{
918 /* is dentry a directory ? if so, kfree() associated cgroup */
919 if (S_ISDIR(inode->i_mode)) {
Paul Menagebd89aab2007-10-18 23:40:44 -0700920 struct cgroup *cgrp = dentry->d_fsdata;
Li Zefanbe445622013-01-24 14:31:42 +0800921
Tejun Heo54766d42013-06-12 21:04:53 -0700922 BUG_ON(!(cgroup_is_dead(cgrp)));
Li Zefanbe445622013-01-24 14:31:42 +0800923 call_rcu(&cgrp->rcu_head, cgroup_free_rcu);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700924 } else {
925 struct cfent *cfe = __d_cfe(dentry);
926 struct cgroup *cgrp = dentry->d_parent->d_fsdata;
927
928 WARN_ONCE(!list_empty(&cfe->node) &&
929 cgrp != &cgrp->root->top_cgroup,
930 "cfe still linked for %s\n", cfe->type->name);
Li Zefan712317a2013-04-18 23:09:52 -0700931 simple_xattrs_free(&cfe->xattrs);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700932 kfree(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -0700933 }
934 iput(inode);
935}
936
Al Viroc72a04e2011-01-14 05:31:45 +0000937static int cgroup_delete(const struct dentry *d)
938{
939 return 1;
940}
941
Paul Menageddbcc7e2007-10-18 23:39:30 -0700942static void remove_dir(struct dentry *d)
943{
944 struct dentry *parent = dget(d->d_parent);
945
946 d_delete(d);
947 simple_rmdir(parent->d_inode, d);
948 dput(parent);
949}
950
Li Zefan2739d3c2013-01-21 18:18:33 +0800951static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -0700952{
Tejun Heo05ef1d72012-04-01 12:09:56 -0700953 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700954
Tejun Heo05ef1d72012-04-01 12:09:56 -0700955 lockdep_assert_held(&cgrp->dentry->d_inode->i_mutex);
956 lockdep_assert_held(&cgroup_mutex);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100957
Li Zefan2739d3c2013-01-21 18:18:33 +0800958 /*
959 * If we're doing cleanup due to failure of cgroup_create(),
960 * the corresponding @cfe may not exist.
961 */
Tejun Heo05ef1d72012-04-01 12:09:56 -0700962 list_for_each_entry(cfe, &cgrp->files, node) {
963 struct dentry *d = cfe->dentry;
964
965 if (cft && cfe->type != cft)
966 continue;
967
968 dget(d);
969 d_delete(d);
Tejun Heoce27e312012-07-03 10:38:06 -0700970 simple_unlink(cgrp->dentry->d_inode, d);
Tejun Heo05ef1d72012-04-01 12:09:56 -0700971 list_del_init(&cfe->node);
972 dput(d);
973
Li Zefan2739d3c2013-01-21 18:18:33 +0800974 break;
Paul Menageddbcc7e2007-10-18 23:39:30 -0700975 }
Tejun Heo05ef1d72012-04-01 12:09:56 -0700976}
977
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400978/**
Tejun Heo628f7cd2013-06-28 16:24:11 -0700979 * cgroup_clear_dir - remove subsys files in a cgroup directory
Tejun Heo8f891402013-06-28 16:24:10 -0700980 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400981 * @subsys_mask: mask of the subsystem ids whose files should be removed
982 */
Tejun Heo628f7cd2013-06-28 16:24:11 -0700983static void cgroup_clear_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Tejun Heo05ef1d72012-04-01 12:09:56 -0700984{
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400985 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -0700986 int i;
Tejun Heo05ef1d72012-04-01 12:09:56 -0700987
Tejun Heob420ba72013-07-12 12:34:02 -0700988 for_each_subsys(ss, i) {
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400989 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -0700990
991 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400992 continue;
993 list_for_each_entry(set, &ss->cftsets, node)
Tejun Heo2bb566c2013-08-08 20:11:23 -0400994 cgroup_addrm_files(cgrp, set->cfts, false);
Aristeu Rozanski13af07d2012-08-23 16:53:29 -0400995 }
Paul Menageddbcc7e2007-10-18 23:39:30 -0700996}
997
998/*
999 * NOTE : the dentry must have been dget()'ed
1000 */
1001static void cgroup_d_remove_dir(struct dentry *dentry)
1002{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001003 struct dentry *parent;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001004
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001005 parent = dentry->d_parent;
1006 spin_lock(&parent->d_lock);
Li Zefan3ec762a2011-01-14 11:34:34 +08001007 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001008 list_del_init(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001009 spin_unlock(&dentry->d_lock);
1010 spin_unlock(&parent->d_lock);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001011 remove_dir(dentry);
1012}
1013
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07001014/*
Ben Blumcf5d5942010-03-10 15:22:09 -08001015 * Call with cgroup_mutex held. Drops reference counts on modules, including
1016 * any duplicate ones that parse_cgroupfs_options took. If this function
1017 * returns an error, no reference counts are touched.
Ben Blumaae8aab2010-03-10 15:22:07 -08001018 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001019static int rebind_subsystems(struct cgroupfs_root *root,
Tejun Heoa8a648c2013-06-24 15:21:47 -07001020 unsigned long added_mask, unsigned removed_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001021{
Paul Menagebd89aab2007-10-18 23:40:44 -07001022 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo30159ec2013-06-25 11:53:37 -07001023 struct cgroup_subsys *ss;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001024 unsigned long pinned = 0;
Tejun Heo31261212013-06-28 17:07:30 -07001025 int i, ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001026
Ben Blumaae8aab2010-03-10 15:22:07 -08001027 BUG_ON(!mutex_is_locked(&cgroup_mutex));
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001028 BUG_ON(!mutex_is_locked(&cgroup_root_mutex));
Ben Blumaae8aab2010-03-10 15:22:07 -08001029
Paul Menageddbcc7e2007-10-18 23:39:30 -07001030 /* Check that any added subsystems are currently free */
Tejun Heo30159ec2013-06-25 11:53:37 -07001031 for_each_subsys(ss, i) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001032 if (!(added_mask & (1 << i)))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001033 continue;
Tejun Heo30159ec2013-06-25 11:53:37 -07001034
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001035 /* is the subsystem mounted elsewhere? */
Tejun Heo9871bf92013-06-24 15:21:47 -07001036 if (ss->root != &cgroup_dummy_root) {
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001037 ret = -EBUSY;
1038 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001039 }
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001040
1041 /* pin the module */
1042 if (!try_module_get(ss->module)) {
1043 ret = -ENOENT;
1044 goto out_put;
1045 }
1046 pinned |= 1 << i;
1047 }
1048
1049 /* subsys could be missing if unloaded between parsing and here */
1050 if (added_mask != pinned) {
1051 ret = -ENOENT;
1052 goto out_put;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001053 }
1054
Tejun Heo31261212013-06-28 17:07:30 -07001055 ret = cgroup_populate_dir(cgrp, added_mask);
1056 if (ret)
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001057 goto out_put;
Tejun Heo31261212013-06-28 17:07:30 -07001058
1059 /*
1060 * Nothing can fail from this point on. Remove files for the
1061 * removed subsystems and rebind each subsystem.
1062 */
1063 cgroup_clear_dir(cgrp, removed_mask);
1064
Tejun Heo30159ec2013-06-25 11:53:37 -07001065 for_each_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001066 unsigned long bit = 1UL << i;
Tejun Heo30159ec2013-06-25 11:53:37 -07001067
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001068 if (bit & added_mask) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001069 /* We're binding this subsystem to this hierarchy */
Paul Menagebd89aab2007-10-18 23:40:44 -07001070 BUG_ON(cgrp->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001071 BUG_ON(!cgroup_dummy_top->subsys[i]);
1072 BUG_ON(cgroup_dummy_top->subsys[i]->cgroup != cgroup_dummy_top);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001073
Tejun Heo9871bf92013-06-24 15:21:47 -07001074 cgrp->subsys[i] = cgroup_dummy_top->subsys[i];
Paul Menagebd89aab2007-10-18 23:40:44 -07001075 cgrp->subsys[i]->cgroup = cgrp;
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 Heoeb954192013-08-08 20:11:23 -04001079 ss->bind(cgrp->subsys[i]);
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 Heo9871bf92013-06-24 15:21:47 -07001085 BUG_ON(cgrp->subsys[i] != cgroup_dummy_top->subsys[i]);
Paul Menagebd89aab2007-10-18 23:40:44 -07001086 BUG_ON(cgrp->subsys[i]->cgroup != cgrp);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001087
Paul Menageddbcc7e2007-10-18 23:39:30 -07001088 if (ss->bind)
Tejun Heoeb954192013-08-08 20:11:23 -04001089 ss->bind(cgroup_dummy_top->subsys[i]);
Tejun Heo9871bf92013-06-24 15:21:47 -07001090 cgroup_dummy_top->subsys[i]->cgroup = cgroup_dummy_top;
Paul Menagebd89aab2007-10-18 23:40:44 -07001091 cgrp->subsys[i] = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07001092 cgroup_subsys[i]->root = &cgroup_dummy_root;
1093 list_move(&ss->sibling, &cgroup_dummy_root.subsys_list);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001094
Ben Blumcf5d5942010-03-10 15:22:09 -08001095 /* subsystem is now free - drop reference on module */
1096 module_put(ss->module);
Tejun Heoa8a648c2013-06-24 15:21:47 -07001097 root->subsys_mask &= ~bit;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001098 }
1099 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001100
Tejun Heo1672d042013-06-25 18:04:54 -07001101 /*
1102 * Mark @root has finished binding subsystems. @root->subsys_mask
1103 * now matches the bound subsystems.
1104 */
1105 root->flags |= CGRP_ROOT_SUBSYS_BOUND;
1106
Paul Menageddbcc7e2007-10-18 23:39:30 -07001107 return 0;
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001108
1109out_put:
1110 for_each_subsys(ss, i)
1111 if (pinned & (1 << i))
1112 module_put(ss->module);
1113 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001114}
1115
Al Viro34c80b12011-12-08 21:32:45 -05001116static int cgroup_show_options(struct seq_file *seq, struct dentry *dentry)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001117{
Al Viro34c80b12011-12-08 21:32:45 -05001118 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001119 struct cgroup_subsys *ss;
1120
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001121 mutex_lock(&cgroup_root_mutex);
Tejun Heo5549c492013-06-24 15:21:48 -07001122 for_each_root_subsys(root, ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001123 seq_printf(seq, ",%s", ss->name);
Tejun Heo873fe092013-04-14 20:15:26 -07001124 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR)
1125 seq_puts(seq, ",sane_behavior");
Tejun Heo93438622013-04-14 20:15:25 -07001126 if (root->flags & CGRP_ROOT_NOPREFIX)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001127 seq_puts(seq, ",noprefix");
Tejun Heo93438622013-04-14 20:15:25 -07001128 if (root->flags & CGRP_ROOT_XATTR)
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001129 seq_puts(seq, ",xattr");
Paul Menage81a6a5c2007-10-18 23:39:38 -07001130 if (strlen(root->release_agent_path))
1131 seq_printf(seq, ",release_agent=%s", root->release_agent_path);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001132 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags))
Daniel Lezcano97978e62010-10-27 15:33:35 -07001133 seq_puts(seq, ",clone_children");
Paul Menagec6d57f32009-09-23 15:56:19 -07001134 if (strlen(root->name))
1135 seq_printf(seq, ",name=%s", root->name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001136 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001137 return 0;
1138}
1139
1140struct cgroup_sb_opts {
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001141 unsigned long subsys_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001142 unsigned long flags;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001143 char *release_agent;
Tejun Heo2260e7f2012-11-19 08:13:38 -08001144 bool cpuset_clone_children;
Paul Menagec6d57f32009-09-23 15:56:19 -07001145 char *name;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001146 /* User explicitly requested empty subsystem */
1147 bool none;
Paul Menagec6d57f32009-09-23 15:56:19 -07001148
1149 struct cgroupfs_root *new_root;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001150
Paul Menageddbcc7e2007-10-18 23:39:30 -07001151};
1152
Ben Blumaae8aab2010-03-10 15:22:07 -08001153/*
Tejun Heo9871bf92013-06-24 15:21:47 -07001154 * Convert a hierarchy specifier into a bitmask of subsystems and
1155 * flags. Call with cgroup_mutex held to protect the cgroup_subsys[]
1156 * array. This function takes refcounts on subsystems to be used, unless it
1157 * returns error, in which case no refcounts are taken.
Ben Blumaae8aab2010-03-10 15:22:07 -08001158 */
Ben Blumcf5d5942010-03-10 15:22:09 -08001159static int parse_cgroupfs_options(char *data, struct cgroup_sb_opts *opts)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001160{
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001161 char *token, *o = data;
1162 bool all_ss = false, one_ss = false;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001163 unsigned long mask = (unsigned long)-1;
Tejun Heo30159ec2013-06-25 11:53:37 -07001164 struct cgroup_subsys *ss;
1165 int i;
Li Zefanf9ab5b52009-06-17 16:26:33 -07001166
Ben Blumaae8aab2010-03-10 15:22:07 -08001167 BUG_ON(!mutex_is_locked(&cgroup_mutex));
1168
Li Zefanf9ab5b52009-06-17 16:26:33 -07001169#ifdef CONFIG_CPUSETS
1170 mask = ~(1UL << cpuset_subsys_id);
1171#endif
Paul Menageddbcc7e2007-10-18 23:39:30 -07001172
Paul Menagec6d57f32009-09-23 15:56:19 -07001173 memset(opts, 0, sizeof(*opts));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001174
1175 while ((token = strsep(&o, ",")) != NULL) {
1176 if (!*token)
1177 return -EINVAL;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001178 if (!strcmp(token, "none")) {
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001179 /* Explicitly have no subsystems */
1180 opts->none = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001181 continue;
1182 }
1183 if (!strcmp(token, "all")) {
1184 /* Mutually exclusive option 'all' + subsystem name */
1185 if (one_ss)
1186 return -EINVAL;
1187 all_ss = true;
1188 continue;
1189 }
Tejun Heo873fe092013-04-14 20:15:26 -07001190 if (!strcmp(token, "__DEVEL__sane_behavior")) {
1191 opts->flags |= CGRP_ROOT_SANE_BEHAVIOR;
1192 continue;
1193 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001194 if (!strcmp(token, "noprefix")) {
Tejun Heo93438622013-04-14 20:15:25 -07001195 opts->flags |= CGRP_ROOT_NOPREFIX;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001196 continue;
1197 }
1198 if (!strcmp(token, "clone_children")) {
Tejun Heo2260e7f2012-11-19 08:13:38 -08001199 opts->cpuset_clone_children = true;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001200 continue;
1201 }
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001202 if (!strcmp(token, "xattr")) {
Tejun Heo93438622013-04-14 20:15:25 -07001203 opts->flags |= CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001204 continue;
1205 }
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001206 if (!strncmp(token, "release_agent=", 14)) {
Paul Menage81a6a5c2007-10-18 23:39:38 -07001207 /* Specifying two release agents is forbidden */
1208 if (opts->release_agent)
1209 return -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001210 opts->release_agent =
Dan Carpentere400c282010-08-10 18:02:54 -07001211 kstrndup(token + 14, PATH_MAX - 1, GFP_KERNEL);
Paul Menage81a6a5c2007-10-18 23:39:38 -07001212 if (!opts->release_agent)
1213 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001214 continue;
1215 }
1216 if (!strncmp(token, "name=", 5)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001217 const char *name = token + 5;
1218 /* Can't specify an empty name */
1219 if (!strlen(name))
1220 return -EINVAL;
1221 /* Must match [\w.-]+ */
1222 for (i = 0; i < strlen(name); i++) {
1223 char c = name[i];
1224 if (isalnum(c))
1225 continue;
1226 if ((c == '.') || (c == '-') || (c == '_'))
1227 continue;
1228 return -EINVAL;
1229 }
1230 /* Specifying two names is forbidden */
1231 if (opts->name)
1232 return -EINVAL;
1233 opts->name = kstrndup(name,
Dan Carpentere400c282010-08-10 18:02:54 -07001234 MAX_CGROUP_ROOT_NAMELEN - 1,
Paul Menagec6d57f32009-09-23 15:56:19 -07001235 GFP_KERNEL);
1236 if (!opts->name)
1237 return -ENOMEM;
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001238
1239 continue;
1240 }
1241
Tejun Heo30159ec2013-06-25 11:53:37 -07001242 for_each_subsys(ss, i) {
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001243 if (strcmp(token, ss->name))
1244 continue;
1245 if (ss->disabled)
1246 continue;
1247
1248 /* Mutually exclusive option 'all' + subsystem name */
1249 if (all_ss)
1250 return -EINVAL;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001251 set_bit(i, &opts->subsys_mask);
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001252 one_ss = true;
1253
1254 break;
1255 }
1256 if (i == CGROUP_SUBSYS_COUNT)
1257 return -ENOENT;
1258 }
1259
1260 /*
1261 * If the 'all' option was specified select all the subsystems,
Li Zefan0d19ea82011-12-27 14:25:55 +08001262 * otherwise if 'none', 'name=' and a subsystem name options
1263 * were not specified, let's default to 'all'
Daniel Lezcano32a8cf22010-10-27 15:33:37 -07001264 */
Tejun Heo30159ec2013-06-25 11:53:37 -07001265 if (all_ss || (!one_ss && !opts->none && !opts->name))
1266 for_each_subsys(ss, i)
1267 if (!ss->disabled)
1268 set_bit(i, &opts->subsys_mask);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001269
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001270 /* Consistency checks */
1271
Tejun Heo873fe092013-04-14 20:15:26 -07001272 if (opts->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1273 pr_warning("cgroup: sane_behavior: this is still under development and its behaviors will change, proceed at your own risk\n");
1274
1275 if (opts->flags & CGRP_ROOT_NOPREFIX) {
1276 pr_err("cgroup: sane_behavior: noprefix is not allowed\n");
1277 return -EINVAL;
1278 }
1279
1280 if (opts->cpuset_clone_children) {
1281 pr_err("cgroup: sane_behavior: clone_children is not allowed\n");
1282 return -EINVAL;
1283 }
1284 }
1285
Li Zefanf9ab5b52009-06-17 16:26:33 -07001286 /*
1287 * Option noprefix was introduced just for backward compatibility
1288 * with the old cpuset, so we allow noprefix only if mounting just
1289 * the cpuset subsystem.
1290 */
Tejun Heo93438622013-04-14 20:15:25 -07001291 if ((opts->flags & CGRP_ROOT_NOPREFIX) && (opts->subsys_mask & mask))
Li Zefanf9ab5b52009-06-17 16:26:33 -07001292 return -EINVAL;
1293
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001294
1295 /* Can't specify "none" and some subsystems */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001296 if (opts->subsys_mask && opts->none)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001297 return -EINVAL;
1298
1299 /*
1300 * We either have to specify by name or by subsystems. (So all
1301 * empty hierarchies must have a name).
1302 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001303 if (!opts->subsys_mask && !opts->name)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001304 return -EINVAL;
1305
1306 return 0;
1307}
1308
1309static int cgroup_remount(struct super_block *sb, int *flags, char *data)
1310{
1311 int ret = 0;
1312 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001313 struct cgroup *cgrp = &root->top_cgroup;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001314 struct cgroup_sb_opts opts;
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001315 unsigned long added_mask, removed_mask;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001316
Tejun Heo873fe092013-04-14 20:15:26 -07001317 if (root->flags & CGRP_ROOT_SANE_BEHAVIOR) {
1318 pr_err("cgroup: sane_behavior: remount is not allowed\n");
1319 return -EINVAL;
1320 }
1321
Paul Menagebd89aab2007-10-18 23:40:44 -07001322 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001323 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001324 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001325
1326 /* See what subsystems are wanted */
1327 ret = parse_cgroupfs_options(data, &opts);
1328 if (ret)
1329 goto out_unlock;
1330
Tejun Heoa8a648c2013-06-24 15:21:47 -07001331 if (opts.subsys_mask != root->subsys_mask || opts.release_agent)
Tejun Heo8b5a5a92012-04-01 12:09:54 -07001332 pr_warning("cgroup: option changes via remount are deprecated (pid=%d comm=%s)\n",
1333 task_tgid_nr(current), current->comm);
1334
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001335 added_mask = opts.subsys_mask & ~root->subsys_mask;
1336 removed_mask = root->subsys_mask & ~opts.subsys_mask;
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04001337
Ben Blumcf5d5942010-03-10 15:22:09 -08001338 /* Don't allow flags or name to change at remount */
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001339 if (((opts.flags ^ root->flags) & CGRP_ROOT_OPTION_MASK) ||
Ben Blumcf5d5942010-03-10 15:22:09 -08001340 (opts.name && strcmp(opts.name, root->name))) {
Tejun Heo0ce6cba2013-06-27 19:37:26 -07001341 pr_err("cgroup: option or name mismatch, new: 0x%lx \"%s\", old: 0x%lx \"%s\"\n",
1342 opts.flags & CGRP_ROOT_OPTION_MASK, opts.name ?: "",
1343 root->flags & CGRP_ROOT_OPTION_MASK, root->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001344 ret = -EINVAL;
Paul Menagec6d57f32009-09-23 15:56:19 -07001345 goto out_unlock;
1346 }
1347
Tejun Heof172e672013-06-28 17:07:30 -07001348 /* remounting is not allowed for populated hierarchies */
1349 if (root->number_of_cgroups > 1) {
1350 ret = -EBUSY;
1351 goto out_unlock;
1352 }
1353
Tejun Heoa8a648c2013-06-24 15:21:47 -07001354 ret = rebind_subsystems(root, added_mask, removed_mask);
Tejun Heo31261212013-06-28 17:07:30 -07001355 if (ret)
Li Zefan0670e082009-04-02 16:57:30 -07001356 goto out_unlock;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001357
Paul Menage81a6a5c2007-10-18 23:39:38 -07001358 if (opts.release_agent)
1359 strcpy(root->release_agent_path, opts.release_agent);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001360 out_unlock:
Jesper Juhl66bdc9c2009-04-02 16:57:27 -07001361 kfree(opts.release_agent);
Paul Menagec6d57f32009-09-23 15:56:19 -07001362 kfree(opts.name);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001363 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001364 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07001365 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001366 return ret;
1367}
1368
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001369static const struct super_operations cgroup_ops = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07001370 .statfs = simple_statfs,
1371 .drop_inode = generic_delete_inode,
1372 .show_options = cgroup_show_options,
1373 .remount_fs = cgroup_remount,
1374};
1375
Paul Menagecc31edc2008-10-18 20:28:04 -07001376static void init_cgroup_housekeeping(struct cgroup *cgrp)
1377{
1378 INIT_LIST_HEAD(&cgrp->sibling);
1379 INIT_LIST_HEAD(&cgrp->children);
Tejun Heo05ef1d72012-04-01 12:09:56 -07001380 INIT_LIST_HEAD(&cgrp->files);
Tejun Heo69d02062013-06-12 21:04:50 -07001381 INIT_LIST_HEAD(&cgrp->cset_links);
Paul Menagecc31edc2008-10-18 20:28:04 -07001382 INIT_LIST_HEAD(&cgrp->release_list);
Ben Blum72a8cb32009-09-23 15:56:27 -07001383 INIT_LIST_HEAD(&cgrp->pidlists);
1384 mutex_init(&cgrp->pidlist_mutex);
Tejun Heo67f4c362013-08-08 20:11:24 -04001385 cgrp->dummy_css.cgroup = cgrp;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08001386 INIT_LIST_HEAD(&cgrp->event_list);
1387 spin_lock_init(&cgrp->event_list_lock);
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001388 simple_xattrs_init(&cgrp->xattrs);
Paul Menagecc31edc2008-10-18 20:28:04 -07001389}
Paul Menagec6d57f32009-09-23 15:56:19 -07001390
Paul Menageddbcc7e2007-10-18 23:39:30 -07001391static void init_cgroup_root(struct cgroupfs_root *root)
1392{
Paul Menagebd89aab2007-10-18 23:40:44 -07001393 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heob0ca5a82012-04-01 12:09:54 -07001394
Paul Menageddbcc7e2007-10-18 23:39:30 -07001395 INIT_LIST_HEAD(&root->subsys_list);
1396 INIT_LIST_HEAD(&root->root_list);
1397 root->number_of_cgroups = 1;
Paul Menagebd89aab2007-10-18 23:40:44 -07001398 cgrp->root = root;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07001399 RCU_INIT_POINTER(cgrp->name, &root_cgroup_name);
Paul Menagecc31edc2008-10-18 20:28:04 -07001400 init_cgroup_housekeeping(cgrp);
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001401 idr_init(&root->cgroup_idr);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001402}
1403
Tejun Heofc76df72013-06-25 11:53:37 -07001404static int cgroup_init_root_id(struct cgroupfs_root *root, int start, int end)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001405{
Tejun Heo1a574232013-04-14 11:36:58 -07001406 int id;
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001407
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001408 lockdep_assert_held(&cgroup_mutex);
1409 lockdep_assert_held(&cgroup_root_mutex);
1410
Tejun Heofc76df72013-06-25 11:53:37 -07001411 id = idr_alloc_cyclic(&cgroup_hierarchy_idr, root, start, end,
1412 GFP_KERNEL);
Tejun Heo1a574232013-04-14 11:36:58 -07001413 if (id < 0)
1414 return id;
1415
1416 root->hierarchy_id = id;
Tejun Heofa3ca072013-04-14 11:36:56 -07001417 return 0;
1418}
1419
1420static void cgroup_exit_root_id(struct cgroupfs_root *root)
1421{
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001422 lockdep_assert_held(&cgroup_mutex);
1423 lockdep_assert_held(&cgroup_root_mutex);
Tejun Heofa3ca072013-04-14 11:36:56 -07001424
Tejun Heo54e7b4e2013-04-14 11:36:57 -07001425 if (root->hierarchy_id) {
Tejun Heo1a574232013-04-14 11:36:58 -07001426 idr_remove(&cgroup_hierarchy_idr, root->hierarchy_id);
Tejun Heofa3ca072013-04-14 11:36:56 -07001427 root->hierarchy_id = 0;
1428 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001429}
1430
Paul Menageddbcc7e2007-10-18 23:39:30 -07001431static int cgroup_test_super(struct super_block *sb, void *data)
1432{
Paul Menagec6d57f32009-09-23 15:56:19 -07001433 struct cgroup_sb_opts *opts = data;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001434 struct cgroupfs_root *root = sb->s_fs_info;
1435
Paul Menagec6d57f32009-09-23 15:56:19 -07001436 /* If we asked for a name then it must match */
1437 if (opts->name && strcmp(opts->name, root->name))
1438 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001439
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001440 /*
1441 * If we asked for subsystems (or explicitly for no
1442 * subsystems) then they must match
1443 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001444 if ((opts->subsys_mask || opts->none)
1445 && (opts->subsys_mask != root->subsys_mask))
Paul Menageddbcc7e2007-10-18 23:39:30 -07001446 return 0;
1447
1448 return 1;
1449}
1450
Paul Menagec6d57f32009-09-23 15:56:19 -07001451static struct cgroupfs_root *cgroup_root_from_opts(struct cgroup_sb_opts *opts)
1452{
1453 struct cgroupfs_root *root;
1454
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001455 if (!opts->subsys_mask && !opts->none)
Paul Menagec6d57f32009-09-23 15:56:19 -07001456 return NULL;
1457
1458 root = kzalloc(sizeof(*root), GFP_KERNEL);
1459 if (!root)
1460 return ERR_PTR(-ENOMEM);
1461
1462 init_cgroup_root(root);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001463
Tejun Heo1672d042013-06-25 18:04:54 -07001464 /*
1465 * We need to set @root->subsys_mask now so that @root can be
1466 * matched by cgroup_test_super() before it finishes
1467 * initialization; otherwise, competing mounts with the same
1468 * options may try to bind the same subsystems instead of waiting
1469 * for the first one leading to unexpected mount errors.
1470 * SUBSYS_BOUND will be set once actual binding is complete.
1471 */
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001472 root->subsys_mask = opts->subsys_mask;
Paul Menagec6d57f32009-09-23 15:56:19 -07001473 root->flags = opts->flags;
1474 if (opts->release_agent)
1475 strcpy(root->release_agent_path, opts->release_agent);
1476 if (opts->name)
1477 strcpy(root->name, opts->name);
Tejun Heo2260e7f2012-11-19 08:13:38 -08001478 if (opts->cpuset_clone_children)
1479 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &root->top_cgroup.flags);
Paul Menagec6d57f32009-09-23 15:56:19 -07001480 return root;
1481}
1482
Tejun Heofa3ca072013-04-14 11:36:56 -07001483static void cgroup_free_root(struct cgroupfs_root *root)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001484{
Tejun Heofa3ca072013-04-14 11:36:56 -07001485 if (root) {
1486 /* hierarhcy ID shoulid already have been released */
1487 WARN_ON_ONCE(root->hierarchy_id);
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001488
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001489 idr_destroy(&root->cgroup_idr);
Tejun Heofa3ca072013-04-14 11:36:56 -07001490 kfree(root);
1491 }
Paul Menage2c6ab6d2009-09-23 15:56:23 -07001492}
1493
Paul Menageddbcc7e2007-10-18 23:39:30 -07001494static int cgroup_set_super(struct super_block *sb, void *data)
1495{
1496 int ret;
Paul Menagec6d57f32009-09-23 15:56:19 -07001497 struct cgroup_sb_opts *opts = data;
1498
1499 /* If we don't have a new root, we can't set up a new sb */
1500 if (!opts->new_root)
1501 return -EINVAL;
1502
Aristeu Rozanskia1a71b452012-08-23 16:53:31 -04001503 BUG_ON(!opts->subsys_mask && !opts->none);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001504
1505 ret = set_anon_super(sb, NULL);
1506 if (ret)
1507 return ret;
1508
Paul Menagec6d57f32009-09-23 15:56:19 -07001509 sb->s_fs_info = opts->new_root;
1510 opts->new_root->sb = sb;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001511
1512 sb->s_blocksize = PAGE_CACHE_SIZE;
1513 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1514 sb->s_magic = CGROUP_SUPER_MAGIC;
1515 sb->s_op = &cgroup_ops;
1516
1517 return 0;
1518}
1519
1520static int cgroup_get_rootdir(struct super_block *sb)
1521{
Al Viro0df6a632010-12-21 13:29:29 -05001522 static const struct dentry_operations cgroup_dops = {
1523 .d_iput = cgroup_diput,
Al Viroc72a04e2011-01-14 05:31:45 +00001524 .d_delete = cgroup_delete,
Al Viro0df6a632010-12-21 13:29:29 -05001525 };
1526
Paul Menageddbcc7e2007-10-18 23:39:30 -07001527 struct inode *inode =
1528 cgroup_new_inode(S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001529
1530 if (!inode)
1531 return -ENOMEM;
1532
Paul Menageddbcc7e2007-10-18 23:39:30 -07001533 inode->i_fop = &simple_dir_operations;
1534 inode->i_op = &cgroup_dir_inode_operations;
1535 /* directories start off with i_nlink == 2 (for "." entry) */
1536 inc_nlink(inode);
Al Viro48fde702012-01-08 22:15:13 -05001537 sb->s_root = d_make_root(inode);
1538 if (!sb->s_root)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001539 return -ENOMEM;
Al Viro0df6a632010-12-21 13:29:29 -05001540 /* for everything else we want ->d_op set */
1541 sb->s_d_op = &cgroup_dops;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001542 return 0;
1543}
1544
Al Virof7e83572010-07-26 13:23:11 +04001545static struct dentry *cgroup_mount(struct file_system_type *fs_type,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001546 int flags, const char *unused_dev_name,
Al Virof7e83572010-07-26 13:23:11 +04001547 void *data)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001548{
1549 struct cgroup_sb_opts opts;
Paul Menagec6d57f32009-09-23 15:56:19 -07001550 struct cgroupfs_root *root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001551 int ret = 0;
1552 struct super_block *sb;
Paul Menagec6d57f32009-09-23 15:56:19 -07001553 struct cgroupfs_root *new_root;
Tejun Heo31261212013-06-28 17:07:30 -07001554 struct list_head tmp_links;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001555 struct inode *inode;
Tejun Heo31261212013-06-28 17:07:30 -07001556 const struct cred *cred;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001557
1558 /* First find the desired set of subsystems */
Ben Blumaae8aab2010-03-10 15:22:07 -08001559 mutex_lock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001560 ret = parse_cgroupfs_options(data, &opts);
Ben Blumaae8aab2010-03-10 15:22:07 -08001561 mutex_unlock(&cgroup_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001562 if (ret)
1563 goto out_err;
1564
1565 /*
1566 * Allocate a new cgroup root. We may not need it if we're
1567 * reusing an existing hierarchy.
1568 */
1569 new_root = cgroup_root_from_opts(&opts);
1570 if (IS_ERR(new_root)) {
1571 ret = PTR_ERR(new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001572 goto out_err;
Paul Menage81a6a5c2007-10-18 23:39:38 -07001573 }
Paul Menagec6d57f32009-09-23 15:56:19 -07001574 opts.new_root = new_root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001575
Paul Menagec6d57f32009-09-23 15:56:19 -07001576 /* Locate an existing or new sb for this hierarchy */
David Howells9249e172012-06-25 12:55:37 +01001577 sb = sget(fs_type, cgroup_test_super, cgroup_set_super, 0, &opts);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001578 if (IS_ERR(sb)) {
Paul Menagec6d57f32009-09-23 15:56:19 -07001579 ret = PTR_ERR(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001580 cgroup_free_root(opts.new_root);
Tejun Heo1d5be6b2013-07-12 13:38:17 -07001581 goto out_err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001582 }
1583
Paul Menagec6d57f32009-09-23 15:56:19 -07001584 root = sb->s_fs_info;
1585 BUG_ON(!root);
1586 if (root == opts.new_root) {
1587 /* We used the new root structure, so this is a new hierarchy */
Li Zefanc12f65d2009-01-07 18:07:42 -08001588 struct cgroup *root_cgrp = &root->top_cgroup;
Paul Menagec6d57f32009-09-23 15:56:19 -07001589 struct cgroupfs_root *existing_root;
Li Zefan28fd5df2008-04-29 01:00:13 -07001590 int i;
Tejun Heo5abb8852013-06-12 21:04:49 -07001591 struct css_set *cset;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001592
1593 BUG_ON(sb->s_root != NULL);
1594
1595 ret = cgroup_get_rootdir(sb);
1596 if (ret)
1597 goto drop_new_super;
Paul Menage817929e2007-10-18 23:39:36 -07001598 inode = sb->s_root->d_inode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001599
Paul Menage817929e2007-10-18 23:39:36 -07001600 mutex_lock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001601 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001602 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001603
Li Zefan4e96ee8e2013-07-31 09:50:50 +08001604 root_cgrp->id = idr_alloc(&root->cgroup_idr, root_cgrp,
1605 0, 1, GFP_KERNEL);
1606 if (root_cgrp->id < 0)
1607 goto unlock_drop;
1608
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001609 /* Check for name clashes with existing mounts */
1610 ret = -EBUSY;
1611 if (strlen(root->name))
1612 for_each_active_root(existing_root)
1613 if (!strcmp(existing_root->name, root->name))
1614 goto unlock_drop;
Paul Menagec6d57f32009-09-23 15:56:19 -07001615
Paul Menage817929e2007-10-18 23:39:36 -07001616 /*
1617 * We're accessing css_set_count without locking
1618 * css_set_lock here, but that's OK - it can only be
1619 * increased by someone holding cgroup_lock, and
1620 * that's us. The worst that can happen is that we
1621 * have some link structures left over
1622 */
Tejun Heo69d02062013-06-12 21:04:50 -07001623 ret = allocate_cgrp_cset_links(css_set_count, &tmp_links);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001624 if (ret)
1625 goto unlock_drop;
Paul Menage817929e2007-10-18 23:39:36 -07001626
Tejun Heofc76df72013-06-25 11:53:37 -07001627 /* ID 0 is reserved for dummy root, 1 for unified hierarchy */
1628 ret = cgroup_init_root_id(root, 2, 0);
Tejun Heofa3ca072013-04-14 11:36:56 -07001629 if (ret)
1630 goto unlock_drop;
1631
Tejun Heo31261212013-06-28 17:07:30 -07001632 sb->s_root->d_fsdata = root_cgrp;
1633 root_cgrp->dentry = sb->s_root;
1634
1635 /*
1636 * We're inside get_sb() and will call lookup_one_len() to
1637 * create the root files, which doesn't work if SELinux is
1638 * in use. The following cred dancing somehow works around
1639 * it. See 2ce9738ba ("cgroupfs: use init_cred when
1640 * populating new cgroupfs mount") for more details.
1641 */
1642 cred = override_creds(&init_cred);
1643
Tejun Heo2bb566c2013-08-08 20:11:23 -04001644 ret = cgroup_addrm_files(root_cgrp, cgroup_base_files, true);
Tejun Heo31261212013-06-28 17:07:30 -07001645 if (ret)
1646 goto rm_base_files;
1647
Tejun Heoa8a648c2013-06-24 15:21:47 -07001648 ret = rebind_subsystems(root, root->subsys_mask, 0);
Tejun Heo31261212013-06-28 17:07:30 -07001649 if (ret)
1650 goto rm_base_files;
1651
1652 revert_creds(cred);
1653
Ben Blumcf5d5942010-03-10 15:22:09 -08001654 /*
1655 * There must be no failure case after here, since rebinding
1656 * takes care of subsystems' refcounts, which are explicitly
1657 * dropped in the failure exit path.
1658 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07001659
Tejun Heo9871bf92013-06-24 15:21:47 -07001660 list_add(&root->root_list, &cgroup_roots);
1661 cgroup_root_count++;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001662
Paul Menage817929e2007-10-18 23:39:36 -07001663 /* Link the top cgroup in this hierarchy into all
1664 * the css_set objects */
1665 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07001666 hash_for_each(css_set_table, i, cset, hlist)
Tejun Heo69d02062013-06-12 21:04:50 -07001667 link_css_set(&tmp_links, cset, root_cgrp);
Paul Menage817929e2007-10-18 23:39:36 -07001668 write_unlock(&css_set_lock);
1669
Tejun Heo69d02062013-06-12 21:04:50 -07001670 free_cgrp_cset_links(&tmp_links);
Paul Menage817929e2007-10-18 23:39:36 -07001671
Li Zefanc12f65d2009-01-07 18:07:42 -08001672 BUG_ON(!list_empty(&root_cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001673 BUG_ON(root->number_of_cgroups != 1);
1674
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001675 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001676 mutex_unlock(&cgroup_mutex);
Xiaotian Feng34f77a92009-09-23 15:56:18 -07001677 mutex_unlock(&inode->i_mutex);
Paul Menagec6d57f32009-09-23 15:56:19 -07001678 } else {
1679 /*
1680 * We re-used an existing hierarchy - the new root (if
1681 * any) is not needed
1682 */
Tejun Heofa3ca072013-04-14 11:36:56 -07001683 cgroup_free_root(opts.new_root);
Tejun Heo873fe092013-04-14 20:15:26 -07001684
Tejun Heoc7ba8282013-06-29 14:06:10 -07001685 if ((root->flags ^ opts.flags) & CGRP_ROOT_OPTION_MASK) {
Jeff Liu2a0ff3f2013-05-26 21:33:09 +08001686 if ((root->flags | opts.flags) & CGRP_ROOT_SANE_BEHAVIOR) {
1687 pr_err("cgroup: sane_behavior: new mount options should match the existing superblock\n");
1688 ret = -EINVAL;
1689 goto drop_new_super;
1690 } else {
1691 pr_warning("cgroup: new mount options do not match the existing superblock, will be ignored\n");
1692 }
Tejun Heo873fe092013-04-14 20:15:26 -07001693 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001694 }
1695
Paul Menagec6d57f32009-09-23 15:56:19 -07001696 kfree(opts.release_agent);
1697 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001698 return dget(sb->s_root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001699
Tejun Heo31261212013-06-28 17:07:30 -07001700 rm_base_files:
1701 free_cgrp_cset_links(&tmp_links);
Tejun Heo2bb566c2013-08-08 20:11:23 -04001702 cgroup_addrm_files(&root->top_cgroup, cgroup_base_files, false);
Tejun Heo31261212013-06-28 17:07:30 -07001703 revert_creds(cred);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001704 unlock_drop:
Tejun Heofa3ca072013-04-14 11:36:56 -07001705 cgroup_exit_root_id(root);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001706 mutex_unlock(&cgroup_root_mutex);
1707 mutex_unlock(&cgroup_mutex);
1708 mutex_unlock(&inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001709 drop_new_super:
Al Viro6f5bbff2009-05-06 01:34:22 -04001710 deactivate_locked_super(sb);
Paul Menagec6d57f32009-09-23 15:56:19 -07001711 out_err:
1712 kfree(opts.release_agent);
1713 kfree(opts.name);
Al Virof7e83572010-07-26 13:23:11 +04001714 return ERR_PTR(ret);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001715}
1716
1717static void cgroup_kill_sb(struct super_block *sb) {
1718 struct cgroupfs_root *root = sb->s_fs_info;
Paul Menagebd89aab2007-10-18 23:40:44 -07001719 struct cgroup *cgrp = &root->top_cgroup;
Tejun Heo69d02062013-06-12 21:04:50 -07001720 struct cgrp_cset_link *link, *tmp_link;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001721 int ret;
1722
1723 BUG_ON(!root);
1724
1725 BUG_ON(root->number_of_cgroups != 1);
Paul Menagebd89aab2007-10-18 23:40:44 -07001726 BUG_ON(!list_empty(&cgrp->children));
Paul Menageddbcc7e2007-10-18 23:39:30 -07001727
Tejun Heo31261212013-06-28 17:07:30 -07001728 mutex_lock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001729 mutex_lock(&cgroup_mutex);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001730 mutex_lock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001731
1732 /* Rebind all subsystems back to the default hierarchy */
Tejun Heo1672d042013-06-25 18:04:54 -07001733 if (root->flags & CGRP_ROOT_SUBSYS_BOUND) {
1734 ret = rebind_subsystems(root, 0, root->subsys_mask);
1735 /* Shouldn't be able to fail ... */
1736 BUG_ON(ret);
1737 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07001738
Paul Menage817929e2007-10-18 23:39:36 -07001739 /*
Tejun Heo69d02062013-06-12 21:04:50 -07001740 * Release all the links from cset_links to this hierarchy's
Paul Menage817929e2007-10-18 23:39:36 -07001741 * root cgroup
1742 */
1743 write_lock(&css_set_lock);
KOSAKI Motohiro71cbb942008-07-25 01:46:55 -07001744
Tejun Heo69d02062013-06-12 21:04:50 -07001745 list_for_each_entry_safe(link, tmp_link, &cgrp->cset_links, cset_link) {
1746 list_del(&link->cset_link);
1747 list_del(&link->cgrp_link);
Paul Menage817929e2007-10-18 23:39:36 -07001748 kfree(link);
1749 }
1750 write_unlock(&css_set_lock);
1751
Paul Menage839ec542009-01-29 14:25:22 -08001752 if (!list_empty(&root->root_list)) {
1753 list_del(&root->root_list);
Tejun Heo9871bf92013-06-24 15:21:47 -07001754 cgroup_root_count--;
Paul Menage839ec542009-01-29 14:25:22 -08001755 }
Li Zefane5f6a862009-01-07 18:07:41 -08001756
Tejun Heofa3ca072013-04-14 11:36:56 -07001757 cgroup_exit_root_id(root);
1758
Tejun Heoe25e2cb2011-12-12 18:12:21 -08001759 mutex_unlock(&cgroup_root_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001760 mutex_unlock(&cgroup_mutex);
Tejun Heo31261212013-06-28 17:07:30 -07001761 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001762
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04001763 simple_xattrs_free(&cgrp->xattrs);
1764
Paul Menageddbcc7e2007-10-18 23:39:30 -07001765 kill_litter_super(sb);
Tejun Heofa3ca072013-04-14 11:36:56 -07001766 cgroup_free_root(root);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001767}
1768
1769static struct file_system_type cgroup_fs_type = {
1770 .name = "cgroup",
Al Virof7e83572010-07-26 13:23:11 +04001771 .mount = cgroup_mount,
Paul Menageddbcc7e2007-10-18 23:39:30 -07001772 .kill_sb = cgroup_kill_sb,
1773};
1774
Greg KH676db4a2010-08-05 13:53:35 -07001775static struct kobject *cgroup_kobj;
1776
Li Zefana043e3b2008-02-23 15:24:09 -08001777/**
1778 * cgroup_path - generate the path of a cgroup
1779 * @cgrp: the cgroup in question
1780 * @buf: the buffer to write the path into
1781 * @buflen: the length of the buffer
1782 *
Li Zefan65dff752013-03-01 15:01:56 +08001783 * Writes path of cgroup into buf. Returns 0 on success, -errno on error.
1784 *
1785 * We can't generate cgroup path using dentry->d_name, as accessing
1786 * dentry->name must be protected by irq-unsafe dentry->d_lock or parent
1787 * inode's i_mutex, while on the other hand cgroup_path() can be called
1788 * with some irq-safe spinlocks held.
Paul Menageddbcc7e2007-10-18 23:39:30 -07001789 */
Paul Menagebd89aab2007-10-18 23:40:44 -07001790int cgroup_path(const struct cgroup *cgrp, char *buf, int buflen)
Paul Menageddbcc7e2007-10-18 23:39:30 -07001791{
Li Zefan65dff752013-03-01 15:01:56 +08001792 int ret = -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001793 char *start;
Tejun Heofebfcef2012-11-19 08:13:36 -08001794
Tejun Heoda1f2962013-04-14 10:32:19 -07001795 if (!cgrp->parent) {
1796 if (strlcpy(buf, "/", buflen) >= buflen)
1797 return -ENAMETOOLONG;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001798 return 0;
1799 }
1800
Tao Ma316eb662012-11-08 21:36:38 +08001801 start = buf + buflen - 1;
Tao Ma316eb662012-11-08 21:36:38 +08001802 *start = '\0';
Li Zefan9a9686b2010-04-22 17:29:24 +08001803
Li Zefan65dff752013-03-01 15:01:56 +08001804 rcu_read_lock();
Tejun Heoda1f2962013-04-14 10:32:19 -07001805 do {
Li Zefan65dff752013-03-01 15:01:56 +08001806 const char *name = cgroup_name(cgrp);
1807 int len;
1808
1809 len = strlen(name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001810 if ((start -= len) < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001811 goto out;
1812 memcpy(start, name, len);
Li Zefan9a9686b2010-04-22 17:29:24 +08001813
Paul Menageddbcc7e2007-10-18 23:39:30 -07001814 if (--start < buf)
Li Zefan65dff752013-03-01 15:01:56 +08001815 goto out;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001816 *start = '/';
Li Zefan65dff752013-03-01 15:01:56 +08001817
1818 cgrp = cgrp->parent;
Tejun Heoda1f2962013-04-14 10:32:19 -07001819 } while (cgrp->parent);
Li Zefan65dff752013-03-01 15:01:56 +08001820 ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001821 memmove(buf, start, buf + buflen - start);
Li Zefan65dff752013-03-01 15:01:56 +08001822out:
1823 rcu_read_unlock();
1824 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07001825}
Ben Blum67523c42010-03-10 15:22:11 -08001826EXPORT_SYMBOL_GPL(cgroup_path);
Paul Menageddbcc7e2007-10-18 23:39:30 -07001827
Tejun Heo857a2be2013-04-14 20:50:08 -07001828/**
Tejun Heo913ffdb2013-07-11 16:34:48 -07001829 * task_cgroup_path - cgroup path of a task in the first cgroup hierarchy
Tejun Heo857a2be2013-04-14 20:50:08 -07001830 * @task: target task
Tejun Heo857a2be2013-04-14 20:50:08 -07001831 * @buf: the buffer to write the path into
1832 * @buflen: the length of the buffer
1833 *
Tejun Heo913ffdb2013-07-11 16:34:48 -07001834 * Determine @task's cgroup on the first (the one with the lowest non-zero
1835 * hierarchy_id) cgroup hierarchy and copy its path into @buf. This
1836 * function grabs cgroup_mutex and shouldn't be used inside locks used by
1837 * cgroup controller callbacks.
1838 *
1839 * Returns 0 on success, fails with -%ENAMETOOLONG if @buflen is too short.
Tejun Heo857a2be2013-04-14 20:50:08 -07001840 */
Tejun Heo913ffdb2013-07-11 16:34:48 -07001841int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen)
Tejun Heo857a2be2013-04-14 20:50:08 -07001842{
1843 struct cgroupfs_root *root;
Tejun Heo913ffdb2013-07-11 16:34:48 -07001844 struct cgroup *cgrp;
1845 int hierarchy_id = 1, ret = 0;
1846
1847 if (buflen < 2)
1848 return -ENAMETOOLONG;
Tejun Heo857a2be2013-04-14 20:50:08 -07001849
1850 mutex_lock(&cgroup_mutex);
1851
Tejun Heo913ffdb2013-07-11 16:34:48 -07001852 root = idr_get_next(&cgroup_hierarchy_idr, &hierarchy_id);
1853
Tejun Heo857a2be2013-04-14 20:50:08 -07001854 if (root) {
1855 cgrp = task_cgroup_from_root(task, root);
1856 ret = cgroup_path(cgrp, buf, buflen);
Tejun Heo913ffdb2013-07-11 16:34:48 -07001857 } else {
1858 /* if no hierarchy exists, everyone is in "/" */
1859 memcpy(buf, "/", 2);
Tejun Heo857a2be2013-04-14 20:50:08 -07001860 }
1861
1862 mutex_unlock(&cgroup_mutex);
Tejun Heo857a2be2013-04-14 20:50:08 -07001863 return ret;
1864}
Tejun Heo913ffdb2013-07-11 16:34:48 -07001865EXPORT_SYMBOL_GPL(task_cgroup_path);
Tejun Heo857a2be2013-04-14 20:50:08 -07001866
Ben Blum74a11662011-05-26 16:25:20 -07001867/*
Tejun Heo2f7ee562011-12-12 18:12:21 -08001868 * Control Group taskset
1869 */
Tejun Heo134d3372011-12-12 18:12:21 -08001870struct task_and_cgroup {
1871 struct task_struct *task;
1872 struct cgroup *cgrp;
Li Zefan6f4b7e62013-07-31 16:18:36 +08001873 struct css_set *cset;
Tejun Heo134d3372011-12-12 18:12:21 -08001874};
1875
Tejun Heo2f7ee562011-12-12 18:12:21 -08001876struct cgroup_taskset {
1877 struct task_and_cgroup single;
1878 struct flex_array *tc_array;
1879 int tc_array_len;
1880 int idx;
1881 struct cgroup *cur_cgrp;
1882};
1883
1884/**
1885 * cgroup_taskset_first - reset taskset and return the first task
1886 * @tset: taskset of interest
1887 *
1888 * @tset iteration is initialized and the first task is returned.
1889 */
1890struct task_struct *cgroup_taskset_first(struct cgroup_taskset *tset)
1891{
1892 if (tset->tc_array) {
1893 tset->idx = 0;
1894 return cgroup_taskset_next(tset);
1895 } else {
1896 tset->cur_cgrp = tset->single.cgrp;
1897 return tset->single.task;
1898 }
1899}
1900EXPORT_SYMBOL_GPL(cgroup_taskset_first);
1901
1902/**
1903 * cgroup_taskset_next - iterate to the next task in taskset
1904 * @tset: taskset of interest
1905 *
1906 * Return the next task in @tset. Iteration must have been initialized
1907 * with cgroup_taskset_first().
1908 */
1909struct task_struct *cgroup_taskset_next(struct cgroup_taskset *tset)
1910{
1911 struct task_and_cgroup *tc;
1912
1913 if (!tset->tc_array || tset->idx >= tset->tc_array_len)
1914 return NULL;
1915
1916 tc = flex_array_get(tset->tc_array, tset->idx++);
1917 tset->cur_cgrp = tc->cgrp;
1918 return tc->task;
1919}
1920EXPORT_SYMBOL_GPL(cgroup_taskset_next);
1921
1922/**
Tejun Heod99c8722013-08-08 20:11:27 -04001923 * cgroup_taskset_cur_css - return the matching css for the current task
Tejun Heo2f7ee562011-12-12 18:12:21 -08001924 * @tset: taskset of interest
Tejun Heod99c8722013-08-08 20:11:27 -04001925 * @subsys_id: the ID of the target subsystem
Tejun Heo2f7ee562011-12-12 18:12:21 -08001926 *
Tejun Heod99c8722013-08-08 20:11:27 -04001927 * Return the css for the current (last returned) task of @tset for
1928 * subsystem specified by @subsys_id. This function must be preceded by
1929 * either cgroup_taskset_first() or cgroup_taskset_next().
Tejun Heo2f7ee562011-12-12 18:12:21 -08001930 */
Tejun Heod99c8722013-08-08 20:11:27 -04001931struct cgroup_subsys_state *cgroup_taskset_cur_css(struct cgroup_taskset *tset,
1932 int subsys_id)
Tejun Heo2f7ee562011-12-12 18:12:21 -08001933{
Tejun Heod99c8722013-08-08 20:11:27 -04001934 return cgroup_css(tset->cur_cgrp, subsys_id);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001935}
Tejun Heod99c8722013-08-08 20:11:27 -04001936EXPORT_SYMBOL_GPL(cgroup_taskset_cur_css);
Tejun Heo2f7ee562011-12-12 18:12:21 -08001937
1938/**
1939 * cgroup_taskset_size - return the number of tasks in taskset
1940 * @tset: taskset of interest
1941 */
1942int cgroup_taskset_size(struct cgroup_taskset *tset)
1943{
1944 return tset->tc_array ? tset->tc_array_len : 1;
1945}
1946EXPORT_SYMBOL_GPL(cgroup_taskset_size);
1947
1948
Ben Blum74a11662011-05-26 16:25:20 -07001949/*
1950 * cgroup_task_migrate - move a task from one cgroup to another.
1951 *
Tao Mad0b2fdd2012-11-20 22:06:18 +08001952 * Must be called with cgroup_mutex and threadgroup locked.
Ben Blum74a11662011-05-26 16:25:20 -07001953 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001954static void cgroup_task_migrate(struct cgroup *old_cgrp,
1955 struct task_struct *tsk,
1956 struct css_set *new_cset)
Ben Blum74a11662011-05-26 16:25:20 -07001957{
Tejun Heo5abb8852013-06-12 21:04:49 -07001958 struct css_set *old_cset;
Ben Blum74a11662011-05-26 16:25:20 -07001959
1960 /*
Mandeep Singh Baines026085e2011-12-21 20:18:35 -08001961 * We are synchronized through threadgroup_lock() against PF_EXITING
1962 * setting such that we can't race against cgroup_exit() changing the
1963 * css_set to init_css_set and dropping the old one.
Ben Blum74a11662011-05-26 16:25:20 -07001964 */
Frederic Weisbeckerc84cdf72011-12-21 20:03:18 +01001965 WARN_ON_ONCE(tsk->flags & PF_EXITING);
Tejun Heoa8ad8052013-06-21 15:52:04 -07001966 old_cset = task_css_set(tsk);
Ben Blum74a11662011-05-26 16:25:20 -07001967
Ben Blum74a11662011-05-26 16:25:20 -07001968 task_lock(tsk);
Tejun Heo5abb8852013-06-12 21:04:49 -07001969 rcu_assign_pointer(tsk->cgroups, new_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001970 task_unlock(tsk);
1971
1972 /* Update the css_set linked lists if we're using them */
1973 write_lock(&css_set_lock);
1974 if (!list_empty(&tsk->cg_list))
Tejun Heo5abb8852013-06-12 21:04:49 -07001975 list_move(&tsk->cg_list, &new_cset->tasks);
Ben Blum74a11662011-05-26 16:25:20 -07001976 write_unlock(&css_set_lock);
1977
1978 /*
Tejun Heo5abb8852013-06-12 21:04:49 -07001979 * We just gained a reference on old_cset by taking it from the
1980 * task. As trading it for new_cset is protected by cgroup_mutex,
1981 * we're safe to drop it here; it will be freed under RCU.
Ben Blum74a11662011-05-26 16:25:20 -07001982 */
Tejun Heo5abb8852013-06-12 21:04:49 -07001983 set_bit(CGRP_RELEASABLE, &old_cgrp->flags);
1984 put_css_set(old_cset);
Ben Blum74a11662011-05-26 16:25:20 -07001985}
1986
Li Zefana043e3b2008-02-23 15:24:09 -08001987/**
Li Zefan081aa452013-03-13 09:17:09 +08001988 * cgroup_attach_task - attach a task or a whole threadgroup to a cgroup
Ben Blum74a11662011-05-26 16:25:20 -07001989 * @cgrp: the cgroup to attach to
Li Zefan081aa452013-03-13 09:17:09 +08001990 * @tsk: the task or the leader of the threadgroup to be attached
1991 * @threadgroup: attach the whole threadgroup?
Ben Blum74a11662011-05-26 16:25:20 -07001992 *
Tejun Heo257058a2011-12-12 18:12:21 -08001993 * Call holding cgroup_mutex and the group_rwsem of the leader. Will take
Li Zefan081aa452013-03-13 09:17:09 +08001994 * task_lock of @tsk or each thread in the threadgroup individually in turn.
Ben Blum74a11662011-05-26 16:25:20 -07001995 */
Tejun Heo47cfcd02013-04-07 09:29:51 -07001996static int cgroup_attach_task(struct cgroup *cgrp, struct task_struct *tsk,
1997 bool threadgroup)
Ben Blum74a11662011-05-26 16:25:20 -07001998{
1999 int retval, i, group_size;
2000 struct cgroup_subsys *ss, *failed_ss = NULL;
Ben Blum74a11662011-05-26 16:25:20 -07002001 struct cgroupfs_root *root = cgrp->root;
2002 /* threadgroup list cursor and array */
Li Zefan081aa452013-03-13 09:17:09 +08002003 struct task_struct *leader = tsk;
Tejun Heo134d3372011-12-12 18:12:21 -08002004 struct task_and_cgroup *tc;
Ben Blumd8466872011-05-26 16:25:21 -07002005 struct flex_array *group;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002006 struct cgroup_taskset tset = { };
Ben Blum74a11662011-05-26 16:25:20 -07002007
2008 /*
2009 * step 0: in order to do expensive, possibly blocking operations for
2010 * every thread, we cannot iterate the thread group list, since it needs
2011 * rcu or tasklist locked. instead, build an array of all threads in the
Tejun Heo257058a2011-12-12 18:12:21 -08002012 * group - group_rwsem prevents new threads from appearing, and if
2013 * threads exit, this will just be an over-estimate.
Ben Blum74a11662011-05-26 16:25:20 -07002014 */
Li Zefan081aa452013-03-13 09:17:09 +08002015 if (threadgroup)
2016 group_size = get_nr_threads(tsk);
2017 else
2018 group_size = 1;
Ben Blumd8466872011-05-26 16:25:21 -07002019 /* flex_array supports very large thread-groups better than kmalloc. */
Tejun Heo134d3372011-12-12 18:12:21 -08002020 group = flex_array_alloc(sizeof(*tc), group_size, GFP_KERNEL);
Ben Blum74a11662011-05-26 16:25:20 -07002021 if (!group)
2022 return -ENOMEM;
Ben Blumd8466872011-05-26 16:25:21 -07002023 /* pre-allocate to guarantee space while iterating in rcu read-side. */
Li Zefan3ac17072013-03-12 15:36:00 -07002024 retval = flex_array_prealloc(group, 0, group_size, GFP_KERNEL);
Ben Blumd8466872011-05-26 16:25:21 -07002025 if (retval)
2026 goto out_free_group_list;
Ben Blum74a11662011-05-26 16:25:20 -07002027
Ben Blum74a11662011-05-26 16:25:20 -07002028 i = 0;
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002029 /*
2030 * Prevent freeing of tasks while we take a snapshot. Tasks that are
2031 * already PF_EXITING could be freed from underneath us unless we
2032 * take an rcu_read_lock.
2033 */
2034 rcu_read_lock();
Ben Blum74a11662011-05-26 16:25:20 -07002035 do {
Tejun Heo134d3372011-12-12 18:12:21 -08002036 struct task_and_cgroup ent;
2037
Tejun Heocd3d0952011-12-12 18:12:21 -08002038 /* @tsk either already exited or can't exit until the end */
2039 if (tsk->flags & PF_EXITING)
2040 continue;
2041
Ben Blum74a11662011-05-26 16:25:20 -07002042 /* as per above, nr_threads may decrease, but not increase. */
2043 BUG_ON(i >= group_size);
Tejun Heo134d3372011-12-12 18:12:21 -08002044 ent.task = tsk;
2045 ent.cgrp = task_cgroup_from_root(tsk, root);
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002046 /* nothing to do if this task is already in the cgroup */
2047 if (ent.cgrp == cgrp)
2048 continue;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002049 /*
2050 * saying GFP_ATOMIC has no effect here because we did prealloc
2051 * earlier, but it's good form to communicate our expectations.
2052 */
Tejun Heo134d3372011-12-12 18:12:21 -08002053 retval = flex_array_put(group, i, &ent, GFP_ATOMIC);
Ben Blumd8466872011-05-26 16:25:21 -07002054 BUG_ON(retval != 0);
Ben Blum74a11662011-05-26 16:25:20 -07002055 i++;
Li Zefan081aa452013-03-13 09:17:09 +08002056
2057 if (!threadgroup)
2058 break;
Ben Blum74a11662011-05-26 16:25:20 -07002059 } while_each_thread(leader, tsk);
Mandeep Singh Bainesfb5d2b42012-01-03 21:18:31 -08002060 rcu_read_unlock();
Ben Blum74a11662011-05-26 16:25:20 -07002061 /* remember the number of threads in the array for later. */
2062 group_size = i;
Tejun Heo2f7ee562011-12-12 18:12:21 -08002063 tset.tc_array = group;
2064 tset.tc_array_len = group_size;
Ben Blum74a11662011-05-26 16:25:20 -07002065
Tejun Heo134d3372011-12-12 18:12:21 -08002066 /* methods shouldn't be called if no task is actually migrating */
2067 retval = 0;
Mandeep Singh Baines892a2b92011-12-21 20:18:37 -08002068 if (!group_size)
Mandeep Singh Bainesb07ef772011-12-21 20:18:36 -08002069 goto out_free_group_list;
Tejun Heo134d3372011-12-12 18:12:21 -08002070
Ben Blum74a11662011-05-26 16:25:20 -07002071 /*
2072 * step 1: check that we can legitimately attach to the cgroup.
2073 */
Tejun Heo5549c492013-06-24 15:21:48 -07002074 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002075 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2076
Ben Blum74a11662011-05-26 16:25:20 -07002077 if (ss->can_attach) {
Tejun Heoeb954192013-08-08 20:11:23 -04002078 retval = ss->can_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002079 if (retval) {
2080 failed_ss = ss;
2081 goto out_cancel_attach;
2082 }
2083 }
Ben Blum74a11662011-05-26 16:25:20 -07002084 }
2085
2086 /*
2087 * step 2: make sure css_sets exist for all threads to be migrated.
2088 * we use find_css_set, which allocates a new one if necessary.
2089 */
Ben Blum74a11662011-05-26 16:25:20 -07002090 for (i = 0; i < group_size; i++) {
Tejun Heoa8ad8052013-06-21 15:52:04 -07002091 struct css_set *old_cset;
2092
Tejun Heo134d3372011-12-12 18:12:21 -08002093 tc = flex_array_get(group, i);
Tejun Heoa8ad8052013-06-21 15:52:04 -07002094 old_cset = task_css_set(tc->task);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002095 tc->cset = find_css_set(old_cset, cgrp);
2096 if (!tc->cset) {
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002097 retval = -ENOMEM;
2098 goto out_put_css_set_refs;
Ben Blum74a11662011-05-26 16:25:20 -07002099 }
2100 }
2101
2102 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002103 * step 3: now that we're guaranteed success wrt the css_sets,
2104 * proceed to move all tasks to the new cgroup. There are no
2105 * failure cases after here, so this is the commit point.
Ben Blum74a11662011-05-26 16:25:20 -07002106 */
Ben Blum74a11662011-05-26 16:25:20 -07002107 for (i = 0; i < group_size; i++) {
Tejun Heo134d3372011-12-12 18:12:21 -08002108 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002109 cgroup_task_migrate(tc->cgrp, tc->task, tc->cset);
Ben Blum74a11662011-05-26 16:25:20 -07002110 }
2111 /* nothing is sensitive to fork() after this point. */
2112
2113 /*
Tejun Heo494c1672011-12-12 18:12:22 -08002114 * step 4: do subsystem attach callbacks.
Ben Blum74a11662011-05-26 16:25:20 -07002115 */
Tejun Heo5549c492013-06-24 15:21:48 -07002116 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002117 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2118
Ben Blum74a11662011-05-26 16:25:20 -07002119 if (ss->attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002120 ss->attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002121 }
2122
2123 /*
2124 * step 5: success! and cleanup
2125 */
Ben Blum74a11662011-05-26 16:25:20 -07002126 retval = 0;
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002127out_put_css_set_refs:
2128 if (retval) {
2129 for (i = 0; i < group_size; i++) {
2130 tc = flex_array_get(group, i);
Li Zefan6f4b7e62013-07-31 16:18:36 +08002131 if (!tc->cset)
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002132 break;
Li Zefan6f4b7e62013-07-31 16:18:36 +08002133 put_css_set(tc->cset);
Mandeep Singh Baines61d1d212012-01-30 12:51:56 -08002134 }
Ben Blum74a11662011-05-26 16:25:20 -07002135 }
2136out_cancel_attach:
Ben Blum74a11662011-05-26 16:25:20 -07002137 if (retval) {
Tejun Heo5549c492013-06-24 15:21:48 -07002138 for_each_root_subsys(root, ss) {
Tejun Heoeb954192013-08-08 20:11:23 -04002139 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
2140
Tejun Heo494c1672011-12-12 18:12:22 -08002141 if (ss == failed_ss)
Ben Blum74a11662011-05-26 16:25:20 -07002142 break;
Ben Blum74a11662011-05-26 16:25:20 -07002143 if (ss->cancel_attach)
Tejun Heoeb954192013-08-08 20:11:23 -04002144 ss->cancel_attach(css, &tset);
Ben Blum74a11662011-05-26 16:25:20 -07002145 }
2146 }
Ben Blum74a11662011-05-26 16:25:20 -07002147out_free_group_list:
Ben Blumd8466872011-05-26 16:25:21 -07002148 flex_array_free(group);
Ben Blum74a11662011-05-26 16:25:20 -07002149 return retval;
2150}
2151
2152/*
2153 * Find the task_struct of the task to attach by vpid and pass it along to the
Tejun Heocd3d0952011-12-12 18:12:21 -08002154 * function to attach either it or all tasks in its threadgroup. Will lock
2155 * cgroup_mutex and threadgroup; may take task_lock of task.
Ben Blum74a11662011-05-26 16:25:20 -07002156 */
2157static int attach_task_by_pid(struct cgroup *cgrp, u64 pid, bool threadgroup)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002158{
Paul Menagebbcb81d2007-10-18 23:39:32 -07002159 struct task_struct *tsk;
David Howellsc69e8d92008-11-14 10:39:19 +11002160 const struct cred *cred = current_cred(), *tcred;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002161 int ret;
2162
Ben Blum74a11662011-05-26 16:25:20 -07002163 if (!cgroup_lock_live_group(cgrp))
2164 return -ENODEV;
2165
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002166retry_find_task:
2167 rcu_read_lock();
Paul Menagebbcb81d2007-10-18 23:39:32 -07002168 if (pid) {
Pavel Emelyanov73507f32008-02-07 00:14:47 -08002169 tsk = find_task_by_vpid(pid);
Ben Blum74a11662011-05-26 16:25:20 -07002170 if (!tsk) {
Paul Menagebbcb81d2007-10-18 23:39:32 -07002171 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002172 ret= -ESRCH;
2173 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002174 }
Ben Blum74a11662011-05-26 16:25:20 -07002175 /*
2176 * even if we're attaching all tasks in the thread group, we
2177 * only need to check permissions on one of them.
2178 */
David Howellsc69e8d92008-11-14 10:39:19 +11002179 tcred = __task_cred(tsk);
Eric W. Biederman14a590c2012-03-12 15:44:39 -07002180 if (!uid_eq(cred->euid, GLOBAL_ROOT_UID) &&
2181 !uid_eq(cred->euid, tcred->uid) &&
2182 !uid_eq(cred->euid, tcred->suid)) {
David Howellsc69e8d92008-11-14 10:39:19 +11002183 rcu_read_unlock();
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002184 ret = -EACCES;
2185 goto out_unlock_cgroup;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002186 }
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002187 } else
2188 tsk = current;
Tejun Heocd3d0952011-12-12 18:12:21 -08002189
2190 if (threadgroup)
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002191 tsk = tsk->group_leader;
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002192
2193 /*
Tejun Heo14a40ff2013-03-19 13:45:20 -07002194 * Workqueue threads may acquire PF_NO_SETAFFINITY and become
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002195 * trapped in a cpuset, or RT worker may be born in a cgroup
2196 * with no rt_runtime allocated. Just say no.
2197 */
Tejun Heo14a40ff2013-03-19 13:45:20 -07002198 if (tsk == kthreadd_task || (tsk->flags & PF_NO_SETAFFINITY)) {
Mike Galbraithc4c27fb2012-04-21 09:13:46 +02002199 ret = -EINVAL;
2200 rcu_read_unlock();
2201 goto out_unlock_cgroup;
2202 }
2203
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002204 get_task_struct(tsk);
2205 rcu_read_unlock();
Tejun Heocd3d0952011-12-12 18:12:21 -08002206
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002207 threadgroup_lock(tsk);
2208 if (threadgroup) {
2209 if (!thread_group_leader(tsk)) {
2210 /*
2211 * a race with de_thread from another thread's exec()
2212 * may strip us of our leadership, if this happens,
2213 * there is no choice but to throw this task away and
2214 * try again; this is
2215 * "double-double-toil-and-trouble-check locking".
2216 */
2217 threadgroup_unlock(tsk);
2218 put_task_struct(tsk);
2219 goto retry_find_task;
2220 }
Li Zefan081aa452013-03-13 09:17:09 +08002221 }
2222
2223 ret = cgroup_attach_task(cgrp, tsk, threadgroup);
2224
Tejun Heocd3d0952011-12-12 18:12:21 -08002225 threadgroup_unlock(tsk);
2226
Paul Menagebbcb81d2007-10-18 23:39:32 -07002227 put_task_struct(tsk);
Mandeep Singh Bainesb78949e2012-01-03 21:18:30 -08002228out_unlock_cgroup:
Tejun Heo47cfcd02013-04-07 09:29:51 -07002229 mutex_unlock(&cgroup_mutex);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002230 return ret;
2231}
2232
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002233/**
2234 * cgroup_attach_task_all - attach task 'tsk' to all cgroups of task 'from'
2235 * @from: attach to all cgroups of a given task
2236 * @tsk: the task to be attached
2237 */
2238int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
2239{
2240 struct cgroupfs_root *root;
2241 int retval = 0;
2242
Tejun Heo47cfcd02013-04-07 09:29:51 -07002243 mutex_lock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002244 for_each_active_root(root) {
Li Zefan6f4b7e62013-07-31 16:18:36 +08002245 struct cgroup *from_cgrp = task_cgroup_from_root(from, root);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002246
Li Zefan6f4b7e62013-07-31 16:18:36 +08002247 retval = cgroup_attach_task(from_cgrp, tsk, false);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002248 if (retval)
2249 break;
2250 }
Tejun Heo47cfcd02013-04-07 09:29:51 -07002251 mutex_unlock(&cgroup_mutex);
Tejun Heo7ae1bad2013-04-07 09:29:51 -07002252
2253 return retval;
2254}
2255EXPORT_SYMBOL_GPL(cgroup_attach_task_all);
2256
Tejun Heo182446d2013-08-08 20:11:24 -04002257static int cgroup_tasks_write(struct cgroup_subsys_state *css,
2258 struct cftype *cft, u64 pid)
Paul Menageaf351022008-07-25 01:47:01 -07002259{
Tejun Heo182446d2013-08-08 20:11:24 -04002260 return attach_task_by_pid(css->cgroup, pid, false);
Ben Blum74a11662011-05-26 16:25:20 -07002261}
2262
Tejun Heo182446d2013-08-08 20:11:24 -04002263static int cgroup_procs_write(struct cgroup_subsys_state *css,
2264 struct cftype *cft, u64 tgid)
Ben Blum74a11662011-05-26 16:25:20 -07002265{
Tejun Heo182446d2013-08-08 20:11:24 -04002266 return attach_task_by_pid(css->cgroup, tgid, true);
Paul Menageaf351022008-07-25 01:47:01 -07002267}
2268
Tejun Heo182446d2013-08-08 20:11:24 -04002269static int cgroup_release_agent_write(struct cgroup_subsys_state *css,
2270 struct cftype *cft, const char *buffer)
Paul Menagee788e062008-07-25 01:46:59 -07002271{
Tejun Heo182446d2013-08-08 20:11:24 -04002272 BUILD_BUG_ON(sizeof(css->cgroup->root->release_agent_path) < PATH_MAX);
Evgeny Kuznetsovf4a25892010-10-27 15:33:37 -07002273 if (strlen(buffer) >= PATH_MAX)
2274 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002275 if (!cgroup_lock_live_group(css->cgroup))
Paul Menagee788e062008-07-25 01:46:59 -07002276 return -ENODEV;
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002277 mutex_lock(&cgroup_root_mutex);
Tejun Heo182446d2013-08-08 20:11:24 -04002278 strcpy(css->cgroup->root->release_agent_path, buffer);
Tejun Heoe25e2cb2011-12-12 18:12:21 -08002279 mutex_unlock(&cgroup_root_mutex);
Tejun Heo47cfcd02013-04-07 09:29:51 -07002280 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002281 return 0;
2282}
2283
Tejun Heo182446d2013-08-08 20:11:24 -04002284static int cgroup_release_agent_show(struct cgroup_subsys_state *css,
2285 struct cftype *cft, struct seq_file *seq)
Paul Menagee788e062008-07-25 01:46:59 -07002286{
Tejun Heo182446d2013-08-08 20:11:24 -04002287 struct cgroup *cgrp = css->cgroup;
2288
Paul Menagee788e062008-07-25 01:46:59 -07002289 if (!cgroup_lock_live_group(cgrp))
2290 return -ENODEV;
2291 seq_puts(seq, cgrp->root->release_agent_path);
2292 seq_putc(seq, '\n');
Tejun Heo47cfcd02013-04-07 09:29:51 -07002293 mutex_unlock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07002294 return 0;
2295}
2296
Tejun Heo182446d2013-08-08 20:11:24 -04002297static int cgroup_sane_behavior_show(struct cgroup_subsys_state *css,
2298 struct cftype *cft, struct seq_file *seq)
Tejun Heo873fe092013-04-14 20:15:26 -07002299{
Tejun Heo182446d2013-08-08 20:11:24 -04002300 seq_printf(seq, "%d\n", cgroup_sane_behavior(css->cgroup));
Paul Menage81a6a5c2007-10-18 23:39:38 -07002301 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002302}
2303
Tejun Heof7d58812013-08-08 20:11:23 -04002304/* return the css for the given cgroup file */
2305static struct cgroup_subsys_state *cgroup_file_css(struct cfent *cfe)
2306{
2307 struct cftype *cft = cfe->type;
2308 struct cgroup *cgrp = __d_cgrp(cfe->dentry->d_parent);
2309
2310 if (cft->ss)
2311 return cgrp->subsys[cft->ss->subsys_id];
Tejun Heo67f4c362013-08-08 20:11:24 -04002312 return &cgrp->dummy_css;
Tejun Heof7d58812013-08-08 20:11:23 -04002313}
2314
Paul Menage84eea842008-07-25 01:47:00 -07002315/* A buffer size big enough for numbers or short strings */
2316#define CGROUP_LOCAL_BUFFER_SIZE 64
2317
Tejun Heo182446d2013-08-08 20:11:24 -04002318static ssize_t cgroup_write_X64(struct cgroup_subsys_state *css,
2319 struct cftype *cft, struct file *file,
2320 const char __user *userbuf, size_t nbytes,
2321 loff_t *unused_ppos)
Paul Menage355e0c42007-10-18 23:39:33 -07002322{
Paul Menage84eea842008-07-25 01:47:00 -07002323 char buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menage355e0c42007-10-18 23:39:33 -07002324 int retval = 0;
Paul Menage355e0c42007-10-18 23:39:33 -07002325 char *end;
2326
2327 if (!nbytes)
2328 return -EINVAL;
2329 if (nbytes >= sizeof(buffer))
2330 return -E2BIG;
2331 if (copy_from_user(buffer, userbuf, nbytes))
2332 return -EFAULT;
2333
2334 buffer[nbytes] = 0; /* nul-terminate */
Paul Menagee73d2c62008-04-29 01:00:06 -07002335 if (cft->write_u64) {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002336 u64 val = simple_strtoull(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002337 if (*end)
2338 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002339 retval = cft->write_u64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002340 } else {
KOSAKI Motohiro478988d2009-10-26 16:49:36 -07002341 s64 val = simple_strtoll(strstrip(buffer), &end, 0);
Paul Menagee73d2c62008-04-29 01:00:06 -07002342 if (*end)
2343 return -EINVAL;
Tejun Heo182446d2013-08-08 20:11:24 -04002344 retval = cft->write_s64(css, cft, val);
Paul Menagee73d2c62008-04-29 01:00:06 -07002345 }
Paul Menage355e0c42007-10-18 23:39:33 -07002346 if (!retval)
2347 retval = nbytes;
2348 return retval;
2349}
2350
Tejun Heo182446d2013-08-08 20:11:24 -04002351static ssize_t cgroup_write_string(struct cgroup_subsys_state *css,
2352 struct cftype *cft, struct file *file,
2353 const char __user *userbuf, size_t nbytes,
2354 loff_t *unused_ppos)
Paul Menagedb3b1492008-07-25 01:46:58 -07002355{
Paul Menage84eea842008-07-25 01:47:00 -07002356 char local_buffer[CGROUP_LOCAL_BUFFER_SIZE];
Paul Menagedb3b1492008-07-25 01:46:58 -07002357 int retval = 0;
2358 size_t max_bytes = cft->max_write_len;
2359 char *buffer = local_buffer;
2360
2361 if (!max_bytes)
2362 max_bytes = sizeof(local_buffer) - 1;
2363 if (nbytes >= max_bytes)
2364 return -E2BIG;
2365 /* Allocate a dynamic buffer if we need one */
2366 if (nbytes >= sizeof(local_buffer)) {
2367 buffer = kmalloc(nbytes + 1, GFP_KERNEL);
2368 if (buffer == NULL)
2369 return -ENOMEM;
2370 }
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002371 if (nbytes && copy_from_user(buffer, userbuf, nbytes)) {
2372 retval = -EFAULT;
2373 goto out;
2374 }
Paul Menagedb3b1492008-07-25 01:46:58 -07002375
2376 buffer[nbytes] = 0; /* nul-terminate */
Tejun Heo182446d2013-08-08 20:11:24 -04002377 retval = cft->write_string(css, cft, strstrip(buffer));
Paul Menagedb3b1492008-07-25 01:46:58 -07002378 if (!retval)
2379 retval = nbytes;
Li Zefan5a3eb9f2008-07-29 22:33:18 -07002380out:
Paul Menagedb3b1492008-07-25 01:46:58 -07002381 if (buffer != local_buffer)
2382 kfree(buffer);
2383 return retval;
2384}
2385
Paul Menageddbcc7e2007-10-18 23:39:30 -07002386static ssize_t cgroup_file_write(struct file *file, const char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002387 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002388{
Tejun Heo182446d2013-08-08 20:11:24 -04002389 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002390 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002391 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002392
Paul Menage355e0c42007-10-18 23:39:33 -07002393 if (cft->write)
Tejun Heo182446d2013-08-08 20:11:24 -04002394 return cft->write(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002395 if (cft->write_u64 || cft->write_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002396 return cgroup_write_X64(css, cft, file, buf, nbytes, ppos);
Paul Menagedb3b1492008-07-25 01:46:58 -07002397 if (cft->write_string)
Tejun Heo182446d2013-08-08 20:11:24 -04002398 return cgroup_write_string(css, cft, file, buf, nbytes, ppos);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002399 if (cft->trigger) {
Tejun Heo182446d2013-08-08 20:11:24 -04002400 int ret = cft->trigger(css, (unsigned int)cft->private);
Pavel Emelyanovd447ea22008-04-29 01:00:08 -07002401 return ret ? ret : nbytes;
2402 }
Paul Menage355e0c42007-10-18 23:39:33 -07002403 return -EINVAL;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002404}
2405
Tejun Heo182446d2013-08-08 20:11:24 -04002406static ssize_t cgroup_read_u64(struct cgroup_subsys_state *css,
2407 struct cftype *cft, struct file *file,
2408 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002409{
Paul Menage84eea842008-07-25 01:47:00 -07002410 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002411 u64 val = cft->read_u64(css, cft);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002412 int len = sprintf(tmp, "%llu\n", (unsigned long long) val);
2413
2414 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2415}
2416
Tejun Heo182446d2013-08-08 20:11:24 -04002417static ssize_t cgroup_read_s64(struct cgroup_subsys_state *css,
2418 struct cftype *cft, struct file *file,
2419 char __user *buf, size_t nbytes, loff_t *ppos)
Paul Menagee73d2c62008-04-29 01:00:06 -07002420{
Paul Menage84eea842008-07-25 01:47:00 -07002421 char tmp[CGROUP_LOCAL_BUFFER_SIZE];
Tejun Heo182446d2013-08-08 20:11:24 -04002422 s64 val = cft->read_s64(css, cft);
Paul Menagee73d2c62008-04-29 01:00:06 -07002423 int len = sprintf(tmp, "%lld\n", (long long) val);
2424
2425 return simple_read_from_buffer(buf, nbytes, ppos, tmp, len);
2426}
2427
Paul Menageddbcc7e2007-10-18 23:39:30 -07002428static ssize_t cgroup_file_read(struct file *file, char __user *buf,
Tejun Heo182446d2013-08-08 20:11:24 -04002429 size_t nbytes, loff_t *ppos)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002430{
Tejun Heo182446d2013-08-08 20:11:24 -04002431 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002432 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heo182446d2013-08-08 20:11:24 -04002433 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002434
Paul Menageddbcc7e2007-10-18 23:39:30 -07002435 if (cft->read)
Tejun Heo182446d2013-08-08 20:11:24 -04002436 return cft->read(css, cft, file, buf, nbytes, ppos);
Paul Menagef4c753b2008-04-29 00:59:56 -07002437 if (cft->read_u64)
Tejun Heo182446d2013-08-08 20:11:24 -04002438 return cgroup_read_u64(css, cft, file, buf, nbytes, ppos);
Paul Menagee73d2c62008-04-29 01:00:06 -07002439 if (cft->read_s64)
Tejun Heo182446d2013-08-08 20:11:24 -04002440 return cgroup_read_s64(css, cft, file, buf, nbytes, ppos);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002441 return -EINVAL;
2442}
2443
Paul Menage91796562008-04-29 01:00:01 -07002444/*
2445 * seqfile ops/methods for returning structured data. Currently just
2446 * supports string->u64 maps, but can be extended in future.
2447 */
2448
Paul Menage91796562008-04-29 01:00:01 -07002449static int cgroup_map_add(struct cgroup_map_cb *cb, const char *key, u64 value)
2450{
2451 struct seq_file *sf = cb->state;
2452 return seq_printf(sf, "%s %llu\n", key, (unsigned long long)value);
2453}
2454
2455static int cgroup_seqfile_show(struct seq_file *m, void *arg)
2456{
Li Zefane0798ce2013-07-31 17:36:25 +08002457 struct cfent *cfe = m->private;
2458 struct cftype *cft = cfe->type;
Tejun Heo182446d2013-08-08 20:11:24 -04002459 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Li Zefane0798ce2013-07-31 17:36:25 +08002460
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002461 if (cft->read_map) {
2462 struct cgroup_map_cb cb = {
2463 .fill = cgroup_map_add,
2464 .state = m,
2465 };
Tejun Heo182446d2013-08-08 20:11:24 -04002466 return cft->read_map(css, cft, &cb);
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002467 }
Tejun Heo182446d2013-08-08 20:11:24 -04002468 return cft->read_seq_string(css, cft, m);
Paul Menage91796562008-04-29 01:00:01 -07002469}
2470
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002471static const struct file_operations cgroup_seqfile_operations = {
Paul Menage91796562008-04-29 01:00:01 -07002472 .read = seq_read,
Paul Menagee788e062008-07-25 01:46:59 -07002473 .write = cgroup_file_write,
Paul Menage91796562008-04-29 01:00:01 -07002474 .llseek = seq_lseek,
Li Zefane0798ce2013-07-31 17:36:25 +08002475 .release = single_release,
Paul Menage91796562008-04-29 01:00:01 -07002476};
2477
Paul Menageddbcc7e2007-10-18 23:39:30 -07002478static int cgroup_file_open(struct inode *inode, struct file *file)
2479{
Tejun Heof7d58812013-08-08 20:11:23 -04002480 struct cfent *cfe = __d_cfe(file->f_dentry);
2481 struct cftype *cft = __d_cft(file->f_dentry);
2482 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002483 int err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002484
2485 err = generic_file_open(inode, file);
2486 if (err)
2487 return err;
Tejun Heof7d58812013-08-08 20:11:23 -04002488
2489 /*
2490 * If the file belongs to a subsystem, pin the css. Will be
2491 * unpinned either on open failure or release. This ensures that
2492 * @css stays alive for all file operations.
2493 */
Tejun Heo67f4c362013-08-08 20:11:24 -04002494 if (css->ss && !css_tryget(css))
Tejun Heof7d58812013-08-08 20:11:23 -04002495 return -ENODEV;
Li Zefan75139b82009-01-07 18:07:33 -08002496
Serge E. Hallyn29486df2008-04-29 01:00:14 -07002497 if (cft->read_map || cft->read_seq_string) {
Paul Menage91796562008-04-29 01:00:01 -07002498 file->f_op = &cgroup_seqfile_operations;
Li Zefane0798ce2013-07-31 17:36:25 +08002499 err = single_open(file, cgroup_seqfile_show, cfe);
2500 } else if (cft->open) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002501 err = cft->open(inode, file);
Li Zefane0798ce2013-07-31 17:36:25 +08002502 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002503
Tejun Heo67f4c362013-08-08 20:11:24 -04002504 if (css->ss && err)
Tejun Heof7d58812013-08-08 20:11:23 -04002505 css_put(css);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002506 return err;
2507}
2508
2509static int cgroup_file_release(struct inode *inode, struct file *file)
2510{
Tejun Heof7d58812013-08-08 20:11:23 -04002511 struct cfent *cfe = __d_cfe(file->f_dentry);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002512 struct cftype *cft = __d_cft(file->f_dentry);
Tejun Heof7d58812013-08-08 20:11:23 -04002513 struct cgroup_subsys_state *css = cgroup_file_css(cfe);
2514 int ret = 0;
2515
Paul Menageddbcc7e2007-10-18 23:39:30 -07002516 if (cft->release)
Tejun Heof7d58812013-08-08 20:11:23 -04002517 ret = cft->release(inode, file);
Tejun Heo67f4c362013-08-08 20:11:24 -04002518 if (css->ss)
Tejun Heof7d58812013-08-08 20:11:23 -04002519 css_put(css);
2520 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002521}
2522
2523/*
2524 * cgroup_rename - Only allow simple rename of directories in place.
2525 */
2526static int cgroup_rename(struct inode *old_dir, struct dentry *old_dentry,
2527 struct inode *new_dir, struct dentry *new_dentry)
2528{
Li Zefan65dff752013-03-01 15:01:56 +08002529 int ret;
2530 struct cgroup_name *name, *old_name;
2531 struct cgroup *cgrp;
2532
2533 /*
2534 * It's convinient to use parent dir's i_mutex to protected
2535 * cgrp->name.
2536 */
2537 lockdep_assert_held(&old_dir->i_mutex);
2538
Paul Menageddbcc7e2007-10-18 23:39:30 -07002539 if (!S_ISDIR(old_dentry->d_inode->i_mode))
2540 return -ENOTDIR;
2541 if (new_dentry->d_inode)
2542 return -EEXIST;
2543 if (old_dir != new_dir)
2544 return -EIO;
Li Zefan65dff752013-03-01 15:01:56 +08002545
2546 cgrp = __d_cgrp(old_dentry);
2547
Tejun Heo6db8e852013-06-14 11:18:22 -07002548 /*
2549 * This isn't a proper migration and its usefulness is very
2550 * limited. Disallow if sane_behavior.
2551 */
2552 if (cgroup_sane_behavior(cgrp))
2553 return -EPERM;
2554
Li Zefan65dff752013-03-01 15:01:56 +08002555 name = cgroup_alloc_name(new_dentry);
2556 if (!name)
2557 return -ENOMEM;
2558
2559 ret = simple_rename(old_dir, old_dentry, new_dir, new_dentry);
2560 if (ret) {
2561 kfree(name);
2562 return ret;
2563 }
2564
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07002565 old_name = rcu_dereference_protected(cgrp->name, true);
Li Zefan65dff752013-03-01 15:01:56 +08002566 rcu_assign_pointer(cgrp->name, name);
2567
2568 kfree_rcu(old_name, rcu_head);
2569 return 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002570}
2571
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002572static struct simple_xattrs *__d_xattrs(struct dentry *dentry)
2573{
2574 if (S_ISDIR(dentry->d_inode->i_mode))
2575 return &__d_cgrp(dentry)->xattrs;
2576 else
Li Zefan712317a2013-04-18 23:09:52 -07002577 return &__d_cfe(dentry)->xattrs;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002578}
2579
2580static inline int xattr_enabled(struct dentry *dentry)
2581{
2582 struct cgroupfs_root *root = dentry->d_sb->s_fs_info;
Tejun Heo93438622013-04-14 20:15:25 -07002583 return root->flags & CGRP_ROOT_XATTR;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002584}
2585
2586static bool is_valid_xattr(const char *name)
2587{
2588 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) ||
2589 !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN))
2590 return true;
2591 return false;
2592}
2593
2594static int cgroup_setxattr(struct dentry *dentry, const char *name,
2595 const void *val, size_t size, int flags)
2596{
2597 if (!xattr_enabled(dentry))
2598 return -EOPNOTSUPP;
2599 if (!is_valid_xattr(name))
2600 return -EINVAL;
2601 return simple_xattr_set(__d_xattrs(dentry), name, val, size, flags);
2602}
2603
2604static int cgroup_removexattr(struct dentry *dentry, const char *name)
2605{
2606 if (!xattr_enabled(dentry))
2607 return -EOPNOTSUPP;
2608 if (!is_valid_xattr(name))
2609 return -EINVAL;
2610 return simple_xattr_remove(__d_xattrs(dentry), name);
2611}
2612
2613static ssize_t cgroup_getxattr(struct dentry *dentry, const char *name,
2614 void *buf, size_t size)
2615{
2616 if (!xattr_enabled(dentry))
2617 return -EOPNOTSUPP;
2618 if (!is_valid_xattr(name))
2619 return -EINVAL;
2620 return simple_xattr_get(__d_xattrs(dentry), name, buf, size);
2621}
2622
2623static ssize_t cgroup_listxattr(struct dentry *dentry, char *buf, size_t size)
2624{
2625 if (!xattr_enabled(dentry))
2626 return -EOPNOTSUPP;
2627 return simple_xattr_list(__d_xattrs(dentry), buf, size);
2628}
2629
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002630static const struct file_operations cgroup_file_operations = {
Paul Menageddbcc7e2007-10-18 23:39:30 -07002631 .read = cgroup_file_read,
2632 .write = cgroup_file_write,
2633 .llseek = generic_file_llseek,
2634 .open = cgroup_file_open,
2635 .release = cgroup_file_release,
2636};
2637
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002638static const struct inode_operations cgroup_file_inode_operations = {
2639 .setxattr = cgroup_setxattr,
2640 .getxattr = cgroup_getxattr,
2641 .listxattr = cgroup_listxattr,
2642 .removexattr = cgroup_removexattr,
2643};
2644
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -07002645static const struct inode_operations cgroup_dir_inode_operations = {
Al Viroc72a04e2011-01-14 05:31:45 +00002646 .lookup = cgroup_lookup,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002647 .mkdir = cgroup_mkdir,
2648 .rmdir = cgroup_rmdir,
2649 .rename = cgroup_rename,
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002650 .setxattr = cgroup_setxattr,
2651 .getxattr = cgroup_getxattr,
2652 .listxattr = cgroup_listxattr,
2653 .removexattr = cgroup_removexattr,
Paul Menageddbcc7e2007-10-18 23:39:30 -07002654};
2655
Al Viro00cd8dd2012-06-10 17:13:09 -04002656static struct dentry *cgroup_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
Al Viroc72a04e2011-01-14 05:31:45 +00002657{
2658 if (dentry->d_name.len > NAME_MAX)
2659 return ERR_PTR(-ENAMETOOLONG);
2660 d_add(dentry, NULL);
2661 return NULL;
2662}
2663
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002664/*
2665 * Check if a file is a control file
2666 */
2667static inline struct cftype *__file_cft(struct file *file)
2668{
Al Viro496ad9a2013-01-23 17:07:38 -05002669 if (file_inode(file)->i_fop != &cgroup_file_operations)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08002670 return ERR_PTR(-EINVAL);
2671 return __d_cft(file->f_dentry);
2672}
2673
Al Viroa5e7ed32011-07-26 01:55:55 -04002674static int cgroup_create_file(struct dentry *dentry, umode_t mode,
Nick Piggin5adcee12011-01-07 17:49:20 +11002675 struct super_block *sb)
2676{
Paul Menageddbcc7e2007-10-18 23:39:30 -07002677 struct inode *inode;
2678
2679 if (!dentry)
2680 return -ENOENT;
2681 if (dentry->d_inode)
2682 return -EEXIST;
2683
2684 inode = cgroup_new_inode(mode, sb);
2685 if (!inode)
2686 return -ENOMEM;
2687
2688 if (S_ISDIR(mode)) {
2689 inode->i_op = &cgroup_dir_inode_operations;
2690 inode->i_fop = &simple_dir_operations;
2691
2692 /* start off with i_nlink == 2 (for "." entry) */
2693 inc_nlink(inode);
Tejun Heo28fd6f32012-11-19 08:13:36 -08002694 inc_nlink(dentry->d_parent->d_inode);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002695
Tejun Heob8a2df62012-11-19 08:13:37 -08002696 /*
2697 * Control reaches here with cgroup_mutex held.
2698 * @inode->i_mutex should nest outside cgroup_mutex but we
2699 * want to populate it immediately without releasing
2700 * cgroup_mutex. As @inode isn't visible to anyone else
2701 * yet, trylock will always succeed without affecting
2702 * lockdep checks.
2703 */
2704 WARN_ON_ONCE(!mutex_trylock(&inode->i_mutex));
Paul Menageddbcc7e2007-10-18 23:39:30 -07002705 } else if (S_ISREG(mode)) {
2706 inode->i_size = 0;
2707 inode->i_fop = &cgroup_file_operations;
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002708 inode->i_op = &cgroup_file_inode_operations;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002709 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07002710 d_instantiate(dentry, inode);
2711 dget(dentry); /* Extra count - pin the dentry in core */
2712 return 0;
2713}
2714
Li Zefan099fca32009-04-02 16:57:29 -07002715/**
2716 * cgroup_file_mode - deduce file mode of a control file
2717 * @cft: the control file in question
2718 *
2719 * returns cft->mode if ->mode is not 0
2720 * returns S_IRUGO|S_IWUSR if it has both a read and a write handler
2721 * returns S_IRUGO if it has only a read handler
2722 * returns S_IWUSR if it has only a write hander
2723 */
Al Viroa5e7ed32011-07-26 01:55:55 -04002724static umode_t cgroup_file_mode(const struct cftype *cft)
Li Zefan099fca32009-04-02 16:57:29 -07002725{
Al Viroa5e7ed32011-07-26 01:55:55 -04002726 umode_t mode = 0;
Li Zefan099fca32009-04-02 16:57:29 -07002727
2728 if (cft->mode)
2729 return cft->mode;
2730
2731 if (cft->read || cft->read_u64 || cft->read_s64 ||
2732 cft->read_map || cft->read_seq_string)
2733 mode |= S_IRUGO;
2734
2735 if (cft->write || cft->write_u64 || cft->write_s64 ||
2736 cft->write_string || cft->trigger)
2737 mode |= S_IWUSR;
2738
2739 return mode;
2740}
2741
Tejun Heo2bb566c2013-08-08 20:11:23 -04002742static int cgroup_add_file(struct cgroup *cgrp, struct cftype *cft)
Paul Menageddbcc7e2007-10-18 23:39:30 -07002743{
Paul Menagebd89aab2007-10-18 23:40:44 -07002744 struct dentry *dir = cgrp->dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002745 struct cgroup *parent = __d_cgrp(dir);
Paul Menageddbcc7e2007-10-18 23:39:30 -07002746 struct dentry *dentry;
Tejun Heo05ef1d72012-04-01 12:09:56 -07002747 struct cfent *cfe;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002748 int error;
Al Viroa5e7ed32011-07-26 01:55:55 -04002749 umode_t mode;
Paul Menageddbcc7e2007-10-18 23:39:30 -07002750 char name[MAX_CGROUP_TYPE_NAMELEN + MAX_CFTYPE_NAME + 2] = { 0 };
Tejun Heo8e3f6542012-04-01 12:09:55 -07002751
Tejun Heo2bb566c2013-08-08 20:11:23 -04002752 if (cft->ss && !(cgrp->root->flags & CGRP_ROOT_NOPREFIX)) {
2753 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 /* @root always needs to be updated */
2872 inode = root->dentry->d_inode;
2873 mutex_lock(&inode->i_mutex);
2874 mutex_lock(&cgroup_mutex);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002875 ret = cgroup_addrm_files(root, cfts, is_add);
Li Zefane8c82d22013-06-18 18:48:37 +08002876 mutex_unlock(&cgroup_mutex);
2877 mutex_unlock(&inode->i_mutex);
2878
Tejun Heo9ccece82013-06-28 16:24:11 -07002879 if (ret)
2880 goto out_deact;
2881
Li Zefane8c82d22013-06-18 18:48:37 +08002882 /* add/rm files for all cgroups created before */
2883 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002884 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2885 struct cgroup *cgrp = css->cgroup;
2886
Li Zefane8c82d22013-06-18 18:48:37 +08002887 if (cgroup_is_dead(cgrp))
2888 continue;
2889
2890 inode = cgrp->dentry->d_inode;
2891 dget(cgrp->dentry);
2892 rcu_read_unlock();
2893
2894 dput(prev);
2895 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002896
2897 mutex_lock(&inode->i_mutex);
2898 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002899 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002900 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901 mutex_unlock(&cgroup_mutex);
2902 mutex_unlock(&inode->i_mutex);
2903
Li Zefane8c82d22013-06-18 18:48:37 +08002904 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002905 if (ret)
2906 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002907 }
Li Zefane8c82d22013-06-18 18:48:37 +08002908 rcu_read_unlock();
2909 dput(prev);
Tejun Heo9ccece82013-06-28 16:24:11 -07002910out_deact:
Li Zefane8c82d22013-06-18 18:48:37 +08002911 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002912 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002913}
2914
2915/**
2916 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2917 * @ss: target cgroup subsystem
2918 * @cfts: zero-length name terminated array of cftypes
2919 *
2920 * Register @cfts to @ss. Files described by @cfts are created for all
2921 * existing cgroups to which @ss is attached and all future cgroups will
2922 * have them too. This function can be called anytime whether @ss is
2923 * attached or not.
2924 *
2925 * Returns 0 on successful registration, -errno on failure. Note that this
2926 * function currently returns 0 as long as @cfts registration is successful
2927 * even if some file creation attempts on existing cgroups fail.
2928 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002929int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930{
2931 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002932 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002933 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002934
2935 set = kzalloc(sizeof(*set), GFP_KERNEL);
2936 if (!set)
2937 return -ENOMEM;
2938
Tejun Heo2bb566c2013-08-08 20:11:23 -04002939 for (cft = cfts; cft->name[0] != '\0'; cft++)
2940 cft->ss = ss;
2941
Tejun Heo8e3f6542012-04-01 12:09:55 -07002942 cgroup_cfts_prepare();
2943 set->cfts = cfts;
2944 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002945 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002946 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002947 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002948 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002949}
2950EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2951
Li Zefana043e3b2008-02-23 15:24:09 -08002952/**
Tejun Heo79578622012-04-01 12:09:56 -07002953 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002954 * @cfts: zero-length name terminated array of cftypes
2955 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002956 * Unregister @cfts. Files described by @cfts are removed from all
2957 * existing cgroups and all future cgroups won't have them either. This
2958 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002959 *
2960 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002961 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002962 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002963int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002964{
2965 struct cftype_set *set;
2966
Tejun Heo2bb566c2013-08-08 20:11:23 -04002967 if (!cfts || !cfts[0].ss)
2968 return -ENOENT;
2969
Tejun Heo79578622012-04-01 12:09:56 -07002970 cgroup_cfts_prepare();
2971
Tejun Heo2bb566c2013-08-08 20:11:23 -04002972 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002973 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002974 list_del(&set->node);
2975 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002976 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002977 return 0;
2978 }
2979 }
2980
Tejun Heo2bb566c2013-08-08 20:11:23 -04002981 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002982 return -ENOENT;
2983}
2984
2985/**
Li Zefana043e3b2008-02-23 15:24:09 -08002986 * cgroup_task_count - count the number of tasks in a cgroup.
2987 * @cgrp: the cgroup in question
2988 *
2989 * Return the number of tasks in the cgroup.
2990 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002991int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002992{
2993 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002994 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002995
Paul Menage817929e2007-10-18 23:39:36 -07002996 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002997 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2998 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002999 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07003000 return count;
3001}
3002
3003/*
Tejun Heo0942eee2013-08-08 20:11:26 -04003004 * To reduce the fork() overhead for systems that are not actually using
3005 * their cgroups capability, we don't maintain the lists running through
3006 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04003007 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08003008 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07003009static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010{
3011 struct task_struct *p, *g;
3012 write_lock(&css_set_lock);
3013 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003014 /*
3015 * We need tasklist_lock because RCU is not safe against
3016 * while_each_thread(). Besides, a forking task that has passed
3017 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3018 * is not guaranteed to have its child immediately visible in the
3019 * tasklist if we walk through it with RCU.
3020 */
3021 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003022 do_each_thread(g, p) {
3023 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003024 /*
3025 * We should check if the process is exiting, otherwise
3026 * it will race with cgroup_exit() in that the list
3027 * entry won't be deleted though the process has exited.
3028 */
3029 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003030 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003031 task_unlock(p);
3032 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003033 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003034 write_unlock(&css_set_lock);
3035}
3036
Tejun Heo574bd9f2012-11-09 09:12:29 -08003037/**
Tejun Heo492eb212013-08-08 20:11:25 -04003038 * css_next_child - find the next child of a given css
3039 * @pos_css: the current position (%NULL to initiate traversal)
3040 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003041 *
Tejun Heo492eb212013-08-08 20:11:25 -04003042 * This function returns the next child of @parent_css and should be called
3043 * under RCU read lock. The only requirement is that @parent_css and
3044 * @pos_css are accessible. The next sibling is guaranteed to be returned
3045 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003046 */
Tejun Heo492eb212013-08-08 20:11:25 -04003047struct cgroup_subsys_state *
3048css_next_child(struct cgroup_subsys_state *pos_css,
3049 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003050{
Tejun Heo492eb212013-08-08 20:11:25 -04003051 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3052 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003053 struct cgroup *next;
3054
3055 WARN_ON_ONCE(!rcu_read_lock_held());
3056
3057 /*
3058 * @pos could already have been removed. Once a cgroup is removed,
3059 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003060 * changes. As CGRP_DEAD assertion is serialized and happens
3061 * before the cgroup is taken off the ->sibling list, if we see it
3062 * unasserted, it's guaranteed that the next sibling hasn't
3063 * finished its grace period even if it's already removed, and thus
3064 * safe to dereference from this RCU critical section. If
3065 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3066 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003067 *
3068 * If @pos is dead, its next pointer can't be dereferenced;
3069 * however, as each cgroup is given a monotonically increasing
3070 * unique serial number and always appended to the sibling list,
3071 * the next one can be found by walking the parent's children until
3072 * we see a cgroup with higher serial number than @pos's. While
3073 * this path can be slower, it's taken only when either the current
3074 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003075 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003076 if (!pos) {
3077 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3078 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003079 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003080 } else {
3081 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3082 if (next->serial_nr > pos->serial_nr)
3083 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003084 }
3085
Tejun Heo492eb212013-08-08 20:11:25 -04003086 if (&next->sibling == &cgrp->children)
3087 return NULL;
3088
3089 if (parent_css->ss)
3090 return cgroup_css(next, parent_css->ss->subsys_id);
3091 else
3092 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003093}
Tejun Heo492eb212013-08-08 20:11:25 -04003094EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003095
3096/**
Tejun Heo492eb212013-08-08 20:11:25 -04003097 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003098 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003099 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003100 *
Tejun Heo492eb212013-08-08 20:11:25 -04003101 * To be used by css_for_each_descendant_pre(). Find the next descendant
3102 * to visit for pre-order traversal of @root's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003103 *
3104 * While this function requires RCU read locking, it doesn't require the
3105 * whole traversal to be contained in a single RCU critical section. This
3106 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003107 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003108 */
Tejun Heo492eb212013-08-08 20:11:25 -04003109struct cgroup_subsys_state *
3110css_next_descendant_pre(struct cgroup_subsys_state *pos,
3111 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112{
Tejun Heo492eb212013-08-08 20:11:25 -04003113 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003114
3115 WARN_ON_ONCE(!rcu_read_lock_held());
3116
Tejun Heo492eb212013-08-08 20:11:25 -04003117 /* if first iteration, pretend we just visited @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003118 if (!pos)
Tejun Heo492eb212013-08-08 20:11:25 -04003119 pos = root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003120
3121 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003122 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003123 if (next)
3124 return next;
3125
3126 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003127 while (pos != root) {
3128 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003129 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003130 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003131 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003132 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003133
3134 return NULL;
3135}
Tejun Heo492eb212013-08-08 20:11:25 -04003136EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003137
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003138/**
Tejun Heo492eb212013-08-08 20:11:25 -04003139 * css_rightmost_descendant - return the rightmost descendant of a css
3140 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003141 *
Tejun Heo492eb212013-08-08 20:11:25 -04003142 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3143 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003144 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003145 *
3146 * While this function requires RCU read locking, it doesn't require the
3147 * whole traversal to be contained in a single RCU critical section. This
3148 * function will return the correct rightmost descendant as long as @pos is
3149 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003150 */
Tejun Heo492eb212013-08-08 20:11:25 -04003151struct cgroup_subsys_state *
3152css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003153{
Tejun Heo492eb212013-08-08 20:11:25 -04003154 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003155
3156 WARN_ON_ONCE(!rcu_read_lock_held());
3157
3158 do {
3159 last = pos;
3160 /* ->prev isn't RCU safe, walk ->next till the end */
3161 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003162 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003163 pos = tmp;
3164 } while (pos);
3165
3166 return last;
3167}
Tejun Heo492eb212013-08-08 20:11:25 -04003168EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003169
Tejun Heo492eb212013-08-08 20:11:25 -04003170static struct cgroup_subsys_state *
3171css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003172{
Tejun Heo492eb212013-08-08 20:11:25 -04003173 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174
3175 do {
3176 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003177 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003178 } while (pos);
3179
3180 return last;
3181}
3182
3183/**
Tejun Heo492eb212013-08-08 20:11:25 -04003184 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003185 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003186 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003187 *
Tejun Heo492eb212013-08-08 20:11:25 -04003188 * To be used by css_for_each_descendant_post(). Find the next descendant
3189 * to visit for post-order traversal of @root's descendants.
Tejun Heo75501a62013-05-24 10:55:38 +09003190 *
3191 * While this function requires RCU read locking, it doesn't require the
3192 * whole traversal to be contained in a single RCU critical section. This
3193 * function will return the correct next descendant as long as both @pos
3194 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003195 */
Tejun Heo492eb212013-08-08 20:11:25 -04003196struct cgroup_subsys_state *
3197css_next_descendant_post(struct cgroup_subsys_state *pos,
3198 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003199{
Tejun Heo492eb212013-08-08 20:11:25 -04003200 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003201
3202 WARN_ON_ONCE(!rcu_read_lock_held());
3203
3204 /* if first iteration, visit the leftmost descendant */
3205 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003206 next = css_leftmost_descendant(root);
3207 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003208 }
3209
3210 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003211 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003212 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003213 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003214
3215 /* no sibling left, visit parent */
Tejun Heo492eb212013-08-08 20:11:25 -04003216 next = css_parent(pos);
3217 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003218}
Tejun Heo492eb212013-08-08 20:11:25 -04003219EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003220
Tejun Heo0942eee2013-08-08 20:11:26 -04003221/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003222 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003223 * @it: the iterator to advance
3224 *
3225 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003226 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003227static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003228{
3229 struct list_head *l = it->cset_link;
3230 struct cgrp_cset_link *link;
3231 struct css_set *cset;
3232
3233 /* Advance to the next non-empty css_set */
3234 do {
3235 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003236 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003237 it->cset_link = NULL;
3238 return;
3239 }
3240 link = list_entry(l, struct cgrp_cset_link, cset_link);
3241 cset = link->cset;
3242 } while (list_empty(&cset->tasks));
3243 it->cset_link = l;
3244 it->task = cset->tasks.next;
3245}
3246
Tejun Heo0942eee2013-08-08 20:11:26 -04003247/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003248 * css_task_iter_start - initiate task iteration
3249 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003250 * @it: the task iterator to use
3251 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003252 * Initiate iteration through the tasks of @css. The caller can call
3253 * css_task_iter_next() to walk through the tasks until the function
3254 * returns NULL. On completion of iteration, css_task_iter_end() must be
3255 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003256 *
3257 * Note that this function acquires a lock which is released when the
3258 * iteration finishes. The caller can't sleep while iteration is in
3259 * progress.
3260 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003261void css_task_iter_start(struct cgroup_subsys_state *css,
3262 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003263 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003264{
3265 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003266 * The first time anyone tries to iterate across a css, we need to
3267 * enable the list linking each css_set to its tasks, and fix up
3268 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003269 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003270 if (!use_task_css_set_links)
3271 cgroup_enable_task_cg_lists();
3272
Paul Menage817929e2007-10-18 23:39:36 -07003273 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003274
Tejun Heo72ec7022013-08-08 20:11:26 -04003275 it->origin_css = css;
3276 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003277
Tejun Heo72ec7022013-08-08 20:11:26 -04003278 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003279}
3280
Tejun Heo0942eee2013-08-08 20:11:26 -04003281/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003282 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003283 * @it: the task iterator being iterated
3284 *
3285 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003286 * initialized via css_task_iter_start(). Returns NULL when the iteration
3287 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003288 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003289struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003290{
3291 struct task_struct *res;
3292 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003293 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003294
3295 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003296 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003297 return NULL;
3298 res = list_entry(l, struct task_struct, cg_list);
3299 /* Advance iterator to find next entry */
3300 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003301 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3302 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003303 /*
3304 * We reached the end of this task list - move on to the
3305 * next cgrp_cset_link.
3306 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003307 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003308 } else {
3309 it->task = l;
3310 }
3311 return res;
3312}
3313
Tejun Heo0942eee2013-08-08 20:11:26 -04003314/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003315 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003316 * @it: the task iterator to finish
3317 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003318 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003319 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003320void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003321 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003322{
3323 read_unlock(&css_set_lock);
3324}
3325
Cliff Wickman31a7df02008-02-07 00:14:42 -08003326static inline int started_after_time(struct task_struct *t1,
3327 struct timespec *time,
3328 struct task_struct *t2)
3329{
3330 int start_diff = timespec_compare(&t1->start_time, time);
3331 if (start_diff > 0) {
3332 return 1;
3333 } else if (start_diff < 0) {
3334 return 0;
3335 } else {
3336 /*
3337 * Arbitrarily, if two processes started at the same
3338 * time, we'll say that the lower pointer value
3339 * started first. Note that t2 may have exited by now
3340 * so this may not be a valid pointer any longer, but
3341 * that's fine - it still serves to distinguish
3342 * between two tasks started (effectively) simultaneously.
3343 */
3344 return t1 > t2;
3345 }
3346}
3347
3348/*
3349 * This function is a callback from heap_insert() and is used to order
3350 * the heap.
3351 * In this case we order the heap in descending task start time.
3352 */
3353static inline int started_after(void *p1, void *p2)
3354{
3355 struct task_struct *t1 = p1;
3356 struct task_struct *t2 = p2;
3357 return started_after_time(t1, &t2->start_time, t2);
3358}
3359
3360/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003361 * css_scan_tasks - iterate though all the tasks in a css
3362 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003363 * @test: optional test callback
3364 * @process: process callback
3365 * @data: data passed to @test and @process
3366 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003367 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003368 * Iterate through all the tasks in @css, calling @test for each, and if it
3369 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003370 *
Tejun Heoe5358372013-08-08 20:11:26 -04003371 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003372 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003373 * lock css_set_lock for the call to @process.
3374 *
3375 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003376 * of @css for the duration of this call. This function may or may not
3377 * call @process for tasks that exit or move to a different css during the
3378 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003379 *
3380 * Note that @test may be called with locks held, and may in some
3381 * situations be called multiple times for the same task, so it should be
3382 * cheap.
3383 *
3384 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3385 * heap operations (and its "gt" member will be overwritten), else a
3386 * temporary heap will be used (allocation of which may cause this function
3387 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003388 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003389int css_scan_tasks(struct cgroup_subsys_state *css,
3390 bool (*test)(struct task_struct *, void *),
3391 void (*process)(struct task_struct *, void *),
3392 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003393{
3394 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003395 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003396 struct task_struct *p, *dropped;
3397 /* Never dereference latest_task, since it's not refcounted */
3398 struct task_struct *latest_task = NULL;
3399 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003400 struct timespec latest_time = { 0, 0 };
3401
Tejun Heoe5358372013-08-08 20:11:26 -04003402 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003403 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003404 heap->gt = &started_after;
3405 } else {
3406 /* We need to allocate our own heap memory */
3407 heap = &tmp_heap;
3408 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3409 if (retval)
3410 /* cannot allocate the heap */
3411 return retval;
3412 }
3413
3414 again:
3415 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003416 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003417 * which are of interest, and invoking @process callback on the
3418 * ones which need an update. Since we don't want to hold any
3419 * locks during the task updates, gather tasks to be processed in a
3420 * heap structure. The heap is sorted by descending task start
3421 * time. If the statically-sized heap fills up, we overflow tasks
3422 * that started later, and in future iterations only consider tasks
3423 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003424 * guarantees forward progress and that we don't miss any tasks.
3425 */
3426 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003427 css_task_iter_start(css, &it);
3428 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003429 /*
3430 * Only affect tasks that qualify per the caller's callback,
3431 * if he provided one
3432 */
Tejun Heoe5358372013-08-08 20:11:26 -04003433 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003434 continue;
3435 /*
3436 * Only process tasks that started after the last task
3437 * we processed
3438 */
3439 if (!started_after_time(p, &latest_time, latest_task))
3440 continue;
3441 dropped = heap_insert(heap, p);
3442 if (dropped == NULL) {
3443 /*
3444 * The new task was inserted; the heap wasn't
3445 * previously full
3446 */
3447 get_task_struct(p);
3448 } else if (dropped != p) {
3449 /*
3450 * The new task was inserted, and pushed out a
3451 * different task
3452 */
3453 get_task_struct(p);
3454 put_task_struct(dropped);
3455 }
3456 /*
3457 * Else the new task was newer than anything already in
3458 * the heap and wasn't inserted
3459 */
3460 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003461 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003462
3463 if (heap->size) {
3464 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003465 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003466 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003467 latest_time = q->start_time;
3468 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003469 }
3470 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003471 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003472 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003473 }
3474 /*
3475 * If we had to process any tasks at all, scan again
3476 * in case some of them were in the middle of forking
3477 * children that didn't get processed.
3478 * Not the most efficient way to do it, but it avoids
3479 * having to take callback_mutex in the fork path
3480 */
3481 goto again;
3482 }
3483 if (heap == &tmp_heap)
3484 heap_free(&tmp_heap);
3485 return 0;
3486}
3487
Tejun Heoe5358372013-08-08 20:11:26 -04003488static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003489{
Tejun Heoe5358372013-08-08 20:11:26 -04003490 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003491
Tejun Heo47cfcd02013-04-07 09:29:51 -07003492 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003493 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003494 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003495}
3496
3497/**
3498 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3499 * @to: cgroup to which the tasks will be moved
3500 * @from: cgroup in which the tasks currently reside
3501 */
3502int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3503{
Tejun Heo72ec7022013-08-08 20:11:26 -04003504 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3505 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003506}
3507
Paul Menage817929e2007-10-18 23:39:36 -07003508/*
Ben Blum102a7752009-09-23 15:56:26 -07003509 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003510 *
3511 * Reading this file can return large amounts of data if a cgroup has
3512 * *lots* of attached tasks. So it may need several calls to read(),
3513 * but we cannot guarantee that the information we produce is correct
3514 * unless we produce it entirely atomically.
3515 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003516 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003517
Li Zefan24528252012-01-20 11:58:43 +08003518/* which pidlist file are we talking about? */
3519enum cgroup_filetype {
3520 CGROUP_FILE_PROCS,
3521 CGROUP_FILE_TASKS,
3522};
3523
3524/*
3525 * A pidlist is a list of pids that virtually represents the contents of one
3526 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3527 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3528 * to the cgroup.
3529 */
3530struct cgroup_pidlist {
3531 /*
3532 * used to find which pidlist is wanted. doesn't change as long as
3533 * this particular list stays in the list.
3534 */
3535 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3536 /* array of xids */
3537 pid_t *list;
3538 /* how many elements the above list has */
3539 int length;
3540 /* how many files are using the current array */
3541 int use_count;
3542 /* each of these stored in a list by its cgroup */
3543 struct list_head links;
3544 /* pointer to the cgroup we belong to, for list removal purposes */
3545 struct cgroup *owner;
3546 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003547 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003548};
3549
Paul Menagebbcb81d2007-10-18 23:39:32 -07003550/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003551 * The following two functions "fix" the issue where there are more pids
3552 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3553 * TODO: replace with a kernel-wide solution to this problem
3554 */
3555#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3556static void *pidlist_allocate(int count)
3557{
3558 if (PIDLIST_TOO_LARGE(count))
3559 return vmalloc(count * sizeof(pid_t));
3560 else
3561 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3562}
3563static void pidlist_free(void *p)
3564{
3565 if (is_vmalloc_addr(p))
3566 vfree(p);
3567 else
3568 kfree(p);
3569}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003570
3571/*
Ben Blum102a7752009-09-23 15:56:26 -07003572 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003573 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003574 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003575static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003576{
Ben Blum102a7752009-09-23 15:56:26 -07003577 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003578
3579 /*
3580 * we presume the 0th element is unique, so i starts at 1. trivial
3581 * edge cases first; no work needs to be done for either
3582 */
3583 if (length == 0 || length == 1)
3584 return length;
3585 /* src and dest walk down the list; dest counts unique elements */
3586 for (src = 1; src < length; src++) {
3587 /* find next unique element */
3588 while (list[src] == list[src-1]) {
3589 src++;
3590 if (src == length)
3591 goto after;
3592 }
3593 /* dest always points to where the next unique element goes */
3594 list[dest] = list[src];
3595 dest++;
3596 }
3597after:
Ben Blum102a7752009-09-23 15:56:26 -07003598 return dest;
3599}
3600
3601static int cmppid(const void *a, const void *b)
3602{
3603 return *(pid_t *)a - *(pid_t *)b;
3604}
3605
3606/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003607 * find the appropriate pidlist for our purpose (given procs vs tasks)
3608 * returns with the lock on that pidlist already held, and takes care
3609 * of the use count, or returns NULL with no locks held if we're out of
3610 * memory.
3611 */
3612static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3613 enum cgroup_filetype type)
3614{
3615 struct cgroup_pidlist *l;
3616 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003617 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003618
Ben Blum72a8cb32009-09-23 15:56:27 -07003619 /*
Li Zefanb3958902013-08-01 09:52:15 +08003620 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003621 * the last ref-holder is trying to remove l from the list at the same
3622 * time. Holding the pidlist_mutex precludes somebody taking whichever
3623 * list we find out from under us - compare release_pid_array().
3624 */
3625 mutex_lock(&cgrp->pidlist_mutex);
3626 list_for_each_entry(l, &cgrp->pidlists, links) {
3627 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003628 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003629 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003630 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003631 return l;
3632 }
3633 }
3634 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003635 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003636 if (!l) {
3637 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003638 return l;
3639 }
Li Zefanb3958902013-08-01 09:52:15 +08003640 init_rwsem(&l->rwsem);
3641 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003642 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003643 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003644 l->owner = cgrp;
3645 list_add(&l->links, &cgrp->pidlists);
3646 mutex_unlock(&cgrp->pidlist_mutex);
3647 return l;
3648}
3649
3650/*
Ben Blum102a7752009-09-23 15:56:26 -07003651 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3652 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003653static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3654 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003655{
3656 pid_t *array;
3657 int length;
3658 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003659 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003660 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003661 struct cgroup_pidlist *l;
3662
3663 /*
3664 * If cgroup gets more users after we read count, we won't have
3665 * enough space - tough. This race is indistinguishable to the
3666 * caller from the case that the additional cgroup users didn't
3667 * show up until sometime later on.
3668 */
3669 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003670 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003671 if (!array)
3672 return -ENOMEM;
3673 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003674 css_task_iter_start(&cgrp->dummy_css, &it);
3675 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003676 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003677 break;
Ben Blum102a7752009-09-23 15:56:26 -07003678 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003679 if (type == CGROUP_FILE_PROCS)
3680 pid = task_tgid_vnr(tsk);
3681 else
3682 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003683 if (pid > 0) /* make sure to only use valid results */
3684 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003685 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003686 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003687 length = n;
3688 /* now sort & (if procs) strip out duplicates */
3689 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003691 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003692 l = cgroup_pidlist_find(cgrp, type);
3693 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003694 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003695 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003696 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003697 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003698 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003699 l->list = array;
3700 l->length = length;
3701 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003702 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003703 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003704 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003705}
3706
Balbir Singh846c7bb2007-10-18 23:39:44 -07003707/**
Li Zefana043e3b2008-02-23 15:24:09 -08003708 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003709 * @stats: cgroupstats to fill information into
3710 * @dentry: A dentry entry belonging to the cgroup for which stats have
3711 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003712 *
3713 * Build and fill cgroupstats so that taskstats can export it to user
3714 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003715 */
3716int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3717{
3718 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003719 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003720 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003721 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003722
Balbir Singh846c7bb2007-10-18 23:39:44 -07003723 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003724 * Validate dentry by checking the superblock operations,
3725 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003726 */
Li Zefan33d283b2008-11-19 15:36:48 -08003727 if (dentry->d_sb->s_op != &cgroup_ops ||
3728 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003729 goto err;
3730
3731 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003732 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003733
Tejun Heo72ec7022013-08-08 20:11:26 -04003734 css_task_iter_start(&cgrp->dummy_css, &it);
3735 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003736 switch (tsk->state) {
3737 case TASK_RUNNING:
3738 stats->nr_running++;
3739 break;
3740 case TASK_INTERRUPTIBLE:
3741 stats->nr_sleeping++;
3742 break;
3743 case TASK_UNINTERRUPTIBLE:
3744 stats->nr_uninterruptible++;
3745 break;
3746 case TASK_STOPPED:
3747 stats->nr_stopped++;
3748 break;
3749 default:
3750 if (delayacct_is_task_waiting_on_io(tsk))
3751 stats->nr_io_wait++;
3752 break;
3753 }
3754 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003755 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003756
Balbir Singh846c7bb2007-10-18 23:39:44 -07003757err:
3758 return ret;
3759}
3760
Paul Menage8f3ff202009-09-23 15:56:25 -07003761
Paul Menagecc31edc2008-10-18 20:28:04 -07003762/*
Ben Blum102a7752009-09-23 15:56:26 -07003763 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003764 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003765 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003766 */
3767
Ben Blum102a7752009-09-23 15:56:26 -07003768static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003769{
3770 /*
3771 * Initially we receive a position value that corresponds to
3772 * one more than the last pid shown (or 0 on the first call or
3773 * after a seek to the start). Use a binary-search to find the
3774 * next pid to display, if any
3775 */
Ben Blum102a7752009-09-23 15:56:26 -07003776 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 int index = 0, pid = *pos;
3778 int *iter;
3779
Li Zefanb3958902013-08-01 09:52:15 +08003780 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003781 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003782 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003783
Paul Menagecc31edc2008-10-18 20:28:04 -07003784 while (index < end) {
3785 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003786 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003787 index = mid;
3788 break;
Ben Blum102a7752009-09-23 15:56:26 -07003789 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 index = mid + 1;
3791 else
3792 end = mid;
3793 }
3794 }
3795 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003796 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003797 return NULL;
3798 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003799 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003800 *pos = *iter;
3801 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003802}
3803
Ben Blum102a7752009-09-23 15:56:26 -07003804static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003805{
Ben Blum102a7752009-09-23 15:56:26 -07003806 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003807 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003808}
3809
Ben Blum102a7752009-09-23 15:56:26 -07003810static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003811{
Ben Blum102a7752009-09-23 15:56:26 -07003812 struct cgroup_pidlist *l = s->private;
3813 pid_t *p = v;
3814 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003815 /*
3816 * Advance to the next pid in the array. If this goes off the
3817 * end, we're done
3818 */
3819 p++;
3820 if (p >= end) {
3821 return NULL;
3822 } else {
3823 *pos = *p;
3824 return p;
3825 }
3826}
3827
Ben Blum102a7752009-09-23 15:56:26 -07003828static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003829{
3830 return seq_printf(s, "%d\n", *(int *)v);
3831}
3832
Ben Blum102a7752009-09-23 15:56:26 -07003833/*
3834 * seq_operations functions for iterating on pidlists through seq_file -
3835 * independent of whether it's tasks or procs
3836 */
3837static const struct seq_operations cgroup_pidlist_seq_operations = {
3838 .start = cgroup_pidlist_start,
3839 .stop = cgroup_pidlist_stop,
3840 .next = cgroup_pidlist_next,
3841 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003842};
3843
Ben Blum102a7752009-09-23 15:56:26 -07003844static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003845{
Ben Blum72a8cb32009-09-23 15:56:27 -07003846 /*
3847 * the case where we're the last user of this particular pidlist will
3848 * have us remove it from the cgroup's list, which entails taking the
3849 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3850 * pidlist_mutex, we have to take pidlist_mutex first.
3851 */
3852 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003853 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003854 BUG_ON(!l->use_count);
3855 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003856 /* we're the last user if refcount is 0; remove and free */
3857 list_del(&l->links);
3858 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003859 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003860 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003861 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003862 kfree(l);
3863 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003864 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003865 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003866 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003867}
3868
Ben Blum102a7752009-09-23 15:56:26 -07003869static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003870{
Ben Blum102a7752009-09-23 15:56:26 -07003871 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003872 if (!(file->f_mode & FMODE_READ))
3873 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003874 /*
3875 * the seq_file will only be initialized if the file was opened for
3876 * reading; hence we check if it's not null only in that case.
3877 */
3878 l = ((struct seq_file *)file->private_data)->private;
3879 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003880 return seq_release(inode, file);
3881}
3882
Ben Blum102a7752009-09-23 15:56:26 -07003883static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003884 .read = seq_read,
3885 .llseek = seq_lseek,
3886 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003887 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003888};
3889
3890/*
Ben Blum102a7752009-09-23 15:56:26 -07003891 * The following functions handle opens on a file that displays a pidlist
3892 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3893 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003894 */
Ben Blum102a7752009-09-23 15:56:26 -07003895/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003896static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003897{
3898 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003899 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003900 int retval;
3901
3902 /* Nothing to do for write-only files */
3903 if (!(file->f_mode & FMODE_READ))
3904 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003905
Ben Blum102a7752009-09-23 15:56:26 -07003906 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003907 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003908 if (retval)
3909 return retval;
3910 /* configure file information */
3911 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003912
Ben Blum102a7752009-09-23 15:56:26 -07003913 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003914 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003915 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003916 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003917 }
Ben Blum102a7752009-09-23 15:56:26 -07003918 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003919 return 0;
3920}
Ben Blum102a7752009-09-23 15:56:26 -07003921static int cgroup_tasks_open(struct inode *unused, struct file *file)
3922{
Ben Blum72a8cb32009-09-23 15:56:27 -07003923 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003924}
3925static int cgroup_procs_open(struct inode *unused, struct file *file)
3926{
Ben Blum72a8cb32009-09-23 15:56:27 -07003927 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003928}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003929
Tejun Heo182446d2013-08-08 20:11:24 -04003930static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3931 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003932{
Tejun Heo182446d2013-08-08 20:11:24 -04003933 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003934}
3935
Tejun Heo182446d2013-08-08 20:11:24 -04003936static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3937 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003938{
Tejun Heo182446d2013-08-08 20:11:24 -04003939 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003940 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003941 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003942 else
Tejun Heo182446d2013-08-08 20:11:24 -04003943 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003944 return 0;
3945}
3946
Paul Menagebbcb81d2007-10-18 23:39:32 -07003947/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003948 * When dput() is called asynchronously, if umount has been done and
3949 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3950 * there's a small window that vfs will see the root dentry with non-zero
3951 * refcnt and trigger BUG().
3952 *
3953 * That's why we hold a reference before dput() and drop it right after.
3954 */
3955static void cgroup_dput(struct cgroup *cgrp)
3956{
3957 struct super_block *sb = cgrp->root->sb;
3958
3959 atomic_inc(&sb->s_active);
3960 dput(cgrp->dentry);
3961 deactivate_super(sb);
3962}
3963
3964/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003965 * Unregister event and free resources.
3966 *
3967 * Gets called from workqueue.
3968 */
3969static void cgroup_event_remove(struct work_struct *work)
3970{
3971 struct cgroup_event *event = container_of(work, struct cgroup_event,
3972 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003973 struct cgroup_subsys_state *css = event->css;
3974 struct cgroup *cgrp = css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003975
Li Zefan810cbee2013-02-18 18:56:14 +08003976 remove_wait_queue(event->wqh, &event->wait);
3977
Tejun Heo81eeaf02013-08-08 20:11:26 -04003978 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003979
Li Zefan810cbee2013-02-18 18:56:14 +08003980 /* Notify userspace the event is going away. */
3981 eventfd_signal(event->eventfd, 1);
3982
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003983 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003984 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003985 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003986}
3987
3988/*
3989 * Gets called on POLLHUP on eventfd when user closes it.
3990 *
3991 * Called with wqh->lock held and interrupts disabled.
3992 */
3993static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3994 int sync, void *key)
3995{
3996 struct cgroup_event *event = container_of(wait,
3997 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003998 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003999 unsigned long flags = (unsigned long)key;
4000
4001 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004002 /*
Li Zefan810cbee2013-02-18 18:56:14 +08004003 * If the event has been detached at cgroup removal, we
4004 * can simply return knowing the other side will cleanup
4005 * for us.
4006 *
4007 * We can't race against event freeing since the other
4008 * side will require wqh->lock via remove_wait_queue(),
4009 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004010 */
Li Zefan810cbee2013-02-18 18:56:14 +08004011 spin_lock(&cgrp->event_list_lock);
4012 if (!list_empty(&event->list)) {
4013 list_del_init(&event->list);
4014 /*
4015 * We are in atomic context, but cgroup_event_remove()
4016 * may sleep, so we have to call it in workqueue.
4017 */
4018 schedule_work(&event->remove);
4019 }
4020 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004021 }
4022
4023 return 0;
4024}
4025
4026static void cgroup_event_ptable_queue_proc(struct file *file,
4027 wait_queue_head_t *wqh, poll_table *pt)
4028{
4029 struct cgroup_event *event = container_of(pt,
4030 struct cgroup_event, pt);
4031
4032 event->wqh = wqh;
4033 add_wait_queue(wqh, &event->wait);
4034}
4035
4036/*
4037 * Parse input and register new cgroup event handler.
4038 *
4039 * Input must be in format '<event_fd> <control_fd> <args>'.
4040 * Interpretation of args is defined by control file implementation.
4041 */
Tejun Heo182446d2013-08-08 20:11:24 -04004042static int cgroup_write_event_control(struct cgroup_subsys_state *css,
4043 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004044{
Tejun Heo182446d2013-08-08 20:11:24 -04004045 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004046 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004047 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004048 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004049 struct file *efile;
4050 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004051 char *endp;
4052 int ret;
4053
4054 efd = simple_strtoul(buffer, &endp, 10);
4055 if (*endp != ' ')
4056 return -EINVAL;
4057 buffer = endp + 1;
4058
4059 cfd = simple_strtoul(buffer, &endp, 10);
4060 if ((*endp != ' ') && (*endp != '\0'))
4061 return -EINVAL;
4062 buffer = endp + 1;
4063
4064 event = kzalloc(sizeof(*event), GFP_KERNEL);
4065 if (!event)
4066 return -ENOMEM;
Tejun Heo81eeaf02013-08-08 20:11:26 -04004067 event->css = css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004068 INIT_LIST_HEAD(&event->list);
4069 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4070 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4071 INIT_WORK(&event->remove, cgroup_event_remove);
4072
4073 efile = eventfd_fget(efd);
4074 if (IS_ERR(efile)) {
4075 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004076 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004077 }
4078
4079 event->eventfd = eventfd_ctx_fileget(efile);
4080 if (IS_ERR(event->eventfd)) {
4081 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004082 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004083 }
4084
4085 cfile = fget(cfd);
4086 if (!cfile) {
4087 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004088 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004089 }
4090
4091 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004092 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004093 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004094 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004095 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004096
4097 event->cft = __file_cft(cfile);
4098 if (IS_ERR(event->cft)) {
4099 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004100 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004101 }
4102
Li Zefanf1690072013-02-18 14:13:35 +08004103 /*
4104 * The file to be monitored must be in the same cgroup as
4105 * cgroup.event_control is.
4106 */
4107 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4108 if (cgrp_cfile != cgrp) {
4109 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004110 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004111 }
4112
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004113 if (!event->cft->register_event || !event->cft->unregister_event) {
4114 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004115 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004116 }
4117
Tejun Heo81eeaf02013-08-08 20:11:26 -04004118 ret = event->cft->register_event(css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004119 event->eventfd, buffer);
4120 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004121 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004122
Li Zefan7ef70e42013-04-26 11:58:03 -07004123 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004124
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004125 /*
4126 * Events should be removed after rmdir of cgroup directory, but before
4127 * destroying subsystem state objects. Let's take reference to cgroup
4128 * directory dentry to do that.
4129 */
4130 dget(cgrp->dentry);
4131
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004132 spin_lock(&cgrp->event_list_lock);
4133 list_add(&event->list, &cgrp->event_list);
4134 spin_unlock(&cgrp->event_list_lock);
4135
4136 fput(cfile);
4137 fput(efile);
4138
4139 return 0;
4140
Li Zefan876ede82013-08-01 09:51:47 +08004141out_put_cfile:
4142 fput(cfile);
4143out_put_eventfd:
4144 eventfd_ctx_put(event->eventfd);
4145out_put_efile:
4146 fput(efile);
4147out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004148 kfree(event);
4149
4150 return ret;
4151}
4152
Tejun Heo182446d2013-08-08 20:11:24 -04004153static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4154 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004155{
Tejun Heo182446d2013-08-08 20:11:24 -04004156 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004157}
4158
Tejun Heo182446d2013-08-08 20:11:24 -04004159static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4160 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004161{
4162 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004163 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004164 else
Tejun Heo182446d2013-08-08 20:11:24 -04004165 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004166 return 0;
4167}
4168
Tejun Heod5c56ce2013-06-03 19:14:34 -07004169static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004170 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004171 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004172 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004173 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004174 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004175 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004176 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004177 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004178 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004179 .write_string = cgroup_write_event_control,
4180 .mode = S_IWUGO,
4181 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004182 {
4183 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004184 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004185 .read_u64 = cgroup_clone_children_read,
4186 .write_u64 = cgroup_clone_children_write,
4187 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004188 {
Tejun Heo873fe092013-04-14 20:15:26 -07004189 .name = "cgroup.sane_behavior",
4190 .flags = CFTYPE_ONLY_ON_ROOT,
4191 .read_seq_string = cgroup_sane_behavior_show,
4192 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004193
4194 /*
4195 * Historical crazy stuff. These don't have "cgroup." prefix and
4196 * don't exist if sane_behavior. If you're depending on these, be
4197 * prepared to be burned.
4198 */
4199 {
4200 .name = "tasks",
4201 .flags = CFTYPE_INSANE, /* use "procs" instead */
4202 .open = cgroup_tasks_open,
4203 .write_u64 = cgroup_tasks_write,
4204 .release = cgroup_pidlist_release,
4205 .mode = S_IRUGO | S_IWUSR,
4206 },
4207 {
4208 .name = "notify_on_release",
4209 .flags = CFTYPE_INSANE,
4210 .read_u64 = cgroup_read_notify_on_release,
4211 .write_u64 = cgroup_write_notify_on_release,
4212 },
Tejun Heo873fe092013-04-14 20:15:26 -07004213 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004214 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004215 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004216 .read_seq_string = cgroup_release_agent_show,
4217 .write_string = cgroup_release_agent_write,
4218 .max_write_len = PATH_MAX,
4219 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004220 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004221};
4222
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004223/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004224 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004225 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004226 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004227 *
4228 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004229 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004230static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004231{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004232 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004233 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004234
Tejun Heo8e3f6542012-04-01 12:09:55 -07004235 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004236 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004237 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004238
4239 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004240 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004241
Tejun Heobee55092013-06-28 16:24:11 -07004242 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004243 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004244 if (ret < 0)
4245 goto err;
4246 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004247 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004248
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004249 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004250 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004251 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004252 struct css_id *id = rcu_dereference_protected(css->id, true);
4253
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004254 /*
4255 * Update id->css pointer and make this css visible from
4256 * CSS ID functions. This pointer will be dereferened
4257 * from RCU-read-side without locks.
4258 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004259 if (id)
4260 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004261 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004262
4263 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004264err:
4265 cgroup_clear_dir(cgrp, subsys_mask);
4266 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004267}
4268
Tejun Heo48ddbe12012-04-01 12:09:56 -07004269static void css_dput_fn(struct work_struct *work)
4270{
4271 struct cgroup_subsys_state *css =
4272 container_of(work, struct cgroup_subsys_state, dput_work);
4273
Li Zefan1c8158e2013-06-18 18:41:10 +08004274 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004275}
4276
Tejun Heod3daf282013-06-13 19:39:16 -07004277static void css_release(struct percpu_ref *ref)
4278{
4279 struct cgroup_subsys_state *css =
4280 container_of(ref, struct cgroup_subsys_state, refcnt);
4281
4282 schedule_work(&css->dput_work);
4283}
4284
Paul Menageddbcc7e2007-10-18 23:39:30 -07004285static void init_cgroup_css(struct cgroup_subsys_state *css,
4286 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004287 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004288{
Paul Menagebd89aab2007-10-18 23:40:44 -07004289 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004290 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004291 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004292 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004293 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004294 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004295 BUG_ON(cgrp->subsys[ss->subsys_id]);
4296 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004297
4298 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004299 * css holds an extra ref to @cgrp->dentry which is put on the last
4300 * css_put(). dput() requires process context, which css_put() may
4301 * be called without. @css->dput_work will be used to invoke
4302 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004303 */
4304 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004305}
4306
Li Zefan2a4ac632013-07-31 16:16:40 +08004307/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004308static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004309{
Tejun Heoeb954192013-08-08 20:11:23 -04004310 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heob1929db2012-11-19 08:13:38 -08004311 int ret = 0;
4312
Tejun Heoa31f2d32012-11-19 08:13:37 -08004313 lockdep_assert_held(&cgroup_mutex);
4314
Tejun Heo92fb9742012-11-19 08:13:38 -08004315 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004316 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004317 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004318 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004319 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004320}
4321
Li Zefan2a4ac632013-07-31 16:16:40 +08004322/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004323static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004324{
4325 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4326
4327 lockdep_assert_held(&cgroup_mutex);
4328
4329 if (!(css->flags & CSS_ONLINE))
4330 return;
4331
Li Zefand7eeac12013-03-12 15:35:59 -07004332 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004333 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004334
Tejun Heoeb954192013-08-08 20:11:23 -04004335 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004336}
4337
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338/*
Li Zefana043e3b2008-02-23 15:24:09 -08004339 * cgroup_create - create a cgroup
4340 * @parent: cgroup that will be parent of the new cgroup
4341 * @dentry: dentry of the new cgroup
4342 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004343 *
Li Zefana043e3b2008-02-23 15:24:09 -08004344 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004345 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004346static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004347 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004348{
Paul Menagebd89aab2007-10-18 23:40:44 -07004349 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004350 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004351 struct cgroupfs_root *root = parent->root;
4352 int err = 0;
4353 struct cgroup_subsys *ss;
4354 struct super_block *sb = root->sb;
4355
Tejun Heo0a950f62012-11-19 09:02:12 -08004356 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004357 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4358 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004359 return -ENOMEM;
4360
Li Zefan65dff752013-03-01 15:01:56 +08004361 name = cgroup_alloc_name(dentry);
4362 if (!name)
4363 goto err_free_cgrp;
4364 rcu_assign_pointer(cgrp->name, name);
4365
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004366 /*
4367 * Temporarily set the pointer to NULL, so idr_find() won't return
4368 * a half-baked cgroup.
4369 */
4370 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004371 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004372 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004373
Tejun Heo976c06b2012-11-05 09:16:59 -08004374 /*
4375 * Only live parents can have children. Note that the liveliness
4376 * check isn't strictly necessary because cgroup_mkdir() and
4377 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4378 * anyway so that locking is contained inside cgroup proper and we
4379 * don't get nasty surprises if we ever grow another caller.
4380 */
4381 if (!cgroup_lock_live_group(parent)) {
4382 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004383 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004384 }
4385
Paul Menageddbcc7e2007-10-18 23:39:30 -07004386 /* Grab a reference on the superblock so the hierarchy doesn't
4387 * get deleted on unmount if there are child cgroups. This
4388 * can be done outside cgroup_mutex, since the sb can't
4389 * disappear while someone has an open control file on the
4390 * fs */
4391 atomic_inc(&sb->s_active);
4392
Paul Menagecc31edc2008-10-18 20:28:04 -07004393 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004394
Li Zefanfe1c06c2013-01-24 14:30:22 +08004395 dentry->d_fsdata = cgrp;
4396 cgrp->dentry = dentry;
4397
Paul Menagebd89aab2007-10-18 23:40:44 -07004398 cgrp->parent = parent;
4399 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004400
Li Zefanb6abdb02008-03-04 14:28:19 -08004401 if (notify_on_release(parent))
4402 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4403
Tejun Heo2260e7f2012-11-19 08:13:38 -08004404 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4405 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004406
Tejun Heo5549c492013-06-24 15:21:48 -07004407 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004408 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004409
Tejun Heoeb954192013-08-08 20:11:23 -04004410 css = ss->css_alloc(parent->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004411 if (IS_ERR(css)) {
4412 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004413 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004414 }
Tejun Heod3daf282013-06-13 19:39:16 -07004415
4416 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004417 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004418 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004419 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004420 }
Tejun Heod3daf282013-06-13 19:39:16 -07004421
Paul Menagebd89aab2007-10-18 23:40:44 -07004422 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004423
Li Zefan4528fd02010-02-02 13:44:10 -08004424 if (ss->use_id) {
4425 err = alloc_css_id(ss, parent, cgrp);
4426 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004427 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004428 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004429 }
4430
Tejun Heo4e139af2012-11-19 08:13:36 -08004431 /*
4432 * Create directory. cgroup_create_file() returns with the new
4433 * directory locked on success so that it can be populated without
4434 * dropping cgroup_mutex.
4435 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004436 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004437 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004438 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004439 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004440
Tejun Heo00356bd2013-06-18 11:14:22 -07004441 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004442
Tejun Heo4e139af2012-11-19 08:13:36 -08004443 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004444 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4445 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004446
Tejun Heob1929db2012-11-19 08:13:38 -08004447 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004448 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004449 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004450
Li Zefan415cf072013-04-08 14:35:02 +08004451 /* hold a ref to the parent's dentry */
4452 dget(parent->dentry);
4453
Tejun Heob1929db2012-11-19 08:13:38 -08004454 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004455 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004456 err = online_css(ss, cgrp);
4457 if (err)
4458 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004459
4460 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4461 parent->parent) {
4462 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",
4463 current->comm, current->pid, ss->name);
4464 if (!strcmp(ss->name, "memory"))
4465 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4466 ss->warned_broken_hierarchy = true;
4467 }
Tejun Heoa8638032012-11-09 09:12:29 -08004468 }
4469
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004470 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4471
Tejun Heo2bb566c2013-08-08 20:11:23 -04004472 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004473 if (err)
4474 goto err_destroy;
4475
4476 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004477 if (err)
4478 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004479
4480 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004481 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004482
4483 return 0;
4484
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004485err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004486 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004487 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4488
4489 if (css) {
4490 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004491 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004492 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004493 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004494 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004495 /* Release the reference count that we took on the superblock */
4496 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004497err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004498 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004499err_free_name:
4500 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004501err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004502 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004503 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004504
4505err_destroy:
4506 cgroup_destroy_locked(cgrp);
4507 mutex_unlock(&cgroup_mutex);
4508 mutex_unlock(&dentry->d_inode->i_mutex);
4509 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004510}
4511
Al Viro18bb1db2011-07-26 01:41:39 -04004512static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004513{
4514 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4515
4516 /* the vfs holds inode->i_mutex already */
4517 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4518}
4519
Tejun Heod3daf282013-06-13 19:39:16 -07004520static void cgroup_css_killed(struct cgroup *cgrp)
4521{
4522 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4523 return;
4524
4525 /* percpu ref's of all css's are killed, kick off the next step */
4526 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4527 schedule_work(&cgrp->destroy_work);
4528}
4529
4530static void css_ref_killed_fn(struct percpu_ref *ref)
4531{
4532 struct cgroup_subsys_state *css =
4533 container_of(ref, struct cgroup_subsys_state, refcnt);
4534
4535 cgroup_css_killed(css->cgroup);
4536}
4537
4538/**
4539 * cgroup_destroy_locked - the first stage of cgroup destruction
4540 * @cgrp: cgroup to be destroyed
4541 *
4542 * css's make use of percpu refcnts whose killing latency shouldn't be
4543 * exposed to userland and are RCU protected. Also, cgroup core needs to
4544 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4545 * invoked. To satisfy all the requirements, destruction is implemented in
4546 * the following two steps.
4547 *
4548 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4549 * userland visible parts and start killing the percpu refcnts of
4550 * css's. Set up so that the next stage will be kicked off once all
4551 * the percpu refcnts are confirmed to be killed.
4552 *
4553 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4554 * rest of destruction. Once all cgroup references are gone, the
4555 * cgroup is RCU-freed.
4556 *
4557 * This function implements s1. After this step, @cgrp is gone as far as
4558 * the userland is concerned and a new cgroup with the same name may be
4559 * created. As cgroup doesn't care about the names internally, this
4560 * doesn't cause any problem.
4561 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004562static int cgroup_destroy_locked(struct cgroup *cgrp)
4563 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004564{
Tejun Heo42809dd2012-11-19 08:13:37 -08004565 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004566 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004567 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004568 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004569
Tejun Heo42809dd2012-11-19 08:13:37 -08004570 lockdep_assert_held(&d->d_inode->i_mutex);
4571 lockdep_assert_held(&cgroup_mutex);
4572
Tejun Heoddd69142013-06-12 21:04:54 -07004573 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004574 * css_set_lock synchronizes access to ->cset_links and prevents
4575 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004576 */
4577 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004578 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004579 read_unlock(&css_set_lock);
4580 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004581 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004582
Tejun Heo1a90dd52012-11-05 09:16:59 -08004583 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004584 * Block new css_tryget() by killing css refcnts. cgroup core
4585 * guarantees that, by the time ->css_offline() is invoked, no new
4586 * css reference will be given out via css_tryget(). We can't
4587 * simply call percpu_ref_kill() and proceed to offlining css's
4588 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4589 * as killed on all CPUs on return.
4590 *
4591 * Use percpu_ref_kill_and_confirm() to get notifications as each
4592 * css is confirmed to be seen as killed on all CPUs. The
4593 * notification callback keeps track of the number of css's to be
4594 * killed and schedules cgroup_offline_fn() to perform the rest of
4595 * destruction once the percpu refs of all css's are confirmed to
4596 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004597 */
Tejun Heod3daf282013-06-13 19:39:16 -07004598 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004599 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004600 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4601
Tejun Heod3daf282013-06-13 19:39:16 -07004602 /*
4603 * Killing would put the base ref, but we need to keep it
4604 * alive until after ->css_offline.
4605 */
4606 percpu_ref_get(&css->refcnt);
4607
4608 atomic_inc(&cgrp->css_kill_cnt);
4609 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004610 }
Tejun Heod3daf282013-06-13 19:39:16 -07004611 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004612
4613 /*
4614 * Mark @cgrp dead. This prevents further task migration and child
4615 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004616 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004617 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004618 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004619 */
Tejun Heo54766d42013-06-12 21:04:53 -07004620 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004621
Tejun Heo455050d2013-06-13 19:27:41 -07004622 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4623 raw_spin_lock(&release_list_lock);
4624 if (!list_empty(&cgrp->release_list))
4625 list_del_init(&cgrp->release_list);
4626 raw_spin_unlock(&release_list_lock);
4627
4628 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004629 * Clear and remove @cgrp directory. The removal puts the base ref
4630 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004631 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004632 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004633 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004634 dget(d);
4635 cgroup_d_remove_dir(d);
4636
4637 /*
4638 * Unregister events and notify userspace.
4639 * Notify userspace about cgroup removing only after rmdir of cgroup
4640 * directory to avoid race between userspace and kernelspace.
4641 */
4642 spin_lock(&cgrp->event_list_lock);
4643 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4644 list_del_init(&event->list);
4645 schedule_work(&event->remove);
4646 }
4647 spin_unlock(&cgrp->event_list_lock);
4648
Tejun Heoea15f8c2013-06-13 19:27:42 -07004649 return 0;
4650};
4651
Tejun Heod3daf282013-06-13 19:39:16 -07004652/**
4653 * cgroup_offline_fn - the second step of cgroup destruction
4654 * @work: cgroup->destroy_free_work
4655 *
4656 * This function is invoked from a work item for a cgroup which is being
4657 * destroyed after the percpu refcnts of all css's are guaranteed to be
4658 * seen as killed on all CPUs, and performs the rest of destruction. This
4659 * is the second step of destruction described in the comment above
4660 * cgroup_destroy_locked().
4661 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004662static void cgroup_offline_fn(struct work_struct *work)
4663{
4664 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4665 struct cgroup *parent = cgrp->parent;
4666 struct dentry *d = cgrp->dentry;
4667 struct cgroup_subsys *ss;
4668
4669 mutex_lock(&cgroup_mutex);
4670
Tejun Heod3daf282013-06-13 19:39:16 -07004671 /*
4672 * css_tryget() is guaranteed to fail now. Tell subsystems to
4673 * initate destruction.
4674 */
Tejun Heo5549c492013-06-24 15:21:48 -07004675 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004676 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004677
4678 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004679 * Put the css refs from cgroup_destroy_locked(). Each css holds
4680 * an extra reference to the cgroup's dentry and cgroup removal
4681 * proceeds regardless of css refs. On the last put of each css,
4682 * whenever that may be, the extra dentry ref is put so that dentry
4683 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004684 */
Tejun Heo5549c492013-06-24 15:21:48 -07004685 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004686 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004687
Paul Menage999cd8a2009-01-07 18:08:36 -08004688 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004689 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004690
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004691 /*
4692 * We should remove the cgroup object from idr before its grace
4693 * period starts, so we won't be looking up a cgroup while the
4694 * cgroup is being freed.
4695 */
4696 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4697 cgrp->id = -1;
4698
Paul Menageddbcc7e2007-10-18 23:39:30 -07004699 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004700
Paul Menagebd89aab2007-10-18 23:40:44 -07004701 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004702 check_for_release(parent);
4703
Tejun Heoea15f8c2013-06-13 19:27:42 -07004704 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004705}
4706
Tejun Heo42809dd2012-11-19 08:13:37 -08004707static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4708{
4709 int ret;
4710
4711 mutex_lock(&cgroup_mutex);
4712 ret = cgroup_destroy_locked(dentry->d_fsdata);
4713 mutex_unlock(&cgroup_mutex);
4714
4715 return ret;
4716}
4717
Tejun Heo8e3f6542012-04-01 12:09:55 -07004718static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4719{
4720 INIT_LIST_HEAD(&ss->cftsets);
4721
4722 /*
4723 * base_cftset is embedded in subsys itself, no need to worry about
4724 * deregistration.
4725 */
4726 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004727 struct cftype *cft;
4728
4729 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4730 cft->ss = ss;
4731
Tejun Heo8e3f6542012-04-01 12:09:55 -07004732 ss->base_cftset.cfts = ss->base_cftypes;
4733 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4734 }
4735}
4736
Li Zefan06a11922008-04-29 01:00:07 -07004737static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004738{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004739 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004740
4741 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004742
Tejun Heo648bb562012-11-19 08:13:36 -08004743 mutex_lock(&cgroup_mutex);
4744
Tejun Heo8e3f6542012-04-01 12:09:55 -07004745 /* init base cftset */
4746 cgroup_init_cftsets(ss);
4747
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004749 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4750 ss->root = &cgroup_dummy_root;
Tejun Heoeb954192013-08-08 20:11:23 -04004751 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004752 /* We don't handle early failures gracefully */
4753 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004754 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004755
Li Zefane8d55fd2008-04-29 01:00:13 -07004756 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004757 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004758 * newly registered, all tasks and hence the
4759 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004760 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004761
4762 need_forkexit_callback |= ss->fork || ss->exit;
4763
Li Zefane8d55fd2008-04-29 01:00:13 -07004764 /* At system boot, before all subsystems have been
4765 * registered, no tasks have been forked, so we don't
4766 * need to invoke fork callbacks here. */
4767 BUG_ON(!list_empty(&init_task.tasks));
4768
Tejun Heo9871bf92013-06-24 15:21:47 -07004769 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004770
Tejun Heo648bb562012-11-19 08:13:36 -08004771 mutex_unlock(&cgroup_mutex);
4772
Ben Blume6a11052010-03-10 15:22:09 -08004773 /* this function shouldn't be used with modular subsystems, since they
4774 * need to register a subsys_id, among other things */
4775 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004776}
4777
4778/**
Ben Blume6a11052010-03-10 15:22:09 -08004779 * cgroup_load_subsys: load and register a modular subsystem at runtime
4780 * @ss: the subsystem to load
4781 *
4782 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004783 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004784 * up for use. If the subsystem is built-in anyway, work is delegated to the
4785 * simpler cgroup_init_subsys.
4786 */
4787int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4788{
Ben Blume6a11052010-03-10 15:22:09 -08004789 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004790 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004791 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004792 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004793 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004794
4795 /* check name and function validity */
4796 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004797 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004798 return -EINVAL;
4799
4800 /*
4801 * we don't support callbacks in modular subsystems. this check is
4802 * before the ss->module check for consistency; a subsystem that could
4803 * be a module should still have no callbacks even if the user isn't
4804 * compiling it as one.
4805 */
4806 if (ss->fork || ss->exit)
4807 return -EINVAL;
4808
4809 /*
4810 * an optionally modular subsystem is built-in: we want to do nothing,
4811 * since cgroup_init_subsys will have already taken care of it.
4812 */
4813 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004814 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004815 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004816 return 0;
4817 }
4818
Tejun Heo8e3f6542012-04-01 12:09:55 -07004819 /* init base cftset */
4820 cgroup_init_cftsets(ss);
4821
Ben Blume6a11052010-03-10 15:22:09 -08004822 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004823 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004824
4825 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004826 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004827 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004828 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004829 */
Tejun Heoeb954192013-08-08 20:11:23 -04004830 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Ben Blume6a11052010-03-10 15:22:09 -08004831 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004832 /* failure case - need to deassign the cgroup_subsys[] slot. */
4833 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004834 mutex_unlock(&cgroup_mutex);
4835 return PTR_ERR(css);
4836 }
4837
Tejun Heo9871bf92013-06-24 15:21:47 -07004838 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4839 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004840
4841 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004842 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004843 /* init_idr must be after init_cgroup_css because it sets css->id. */
4844 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004845 ret = cgroup_init_idr(ss, css);
4846 if (ret)
4847 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004848 }
4849
4850 /*
4851 * Now we need to entangle the css into the existing css_sets. unlike
4852 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4853 * will need a new pointer to it; done by iterating the css_set_table.
4854 * furthermore, modifying the existing css_sets will corrupt the hash
4855 * table state, so each changed css_set will need its hash recomputed.
4856 * this is all done under the css_set_lock.
4857 */
4858 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004859 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004860 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004861 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004862 continue;
4863 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004864 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004865 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004866 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004867 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004868 key = css_set_hash(cset->subsys);
4869 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004870 }
4871 write_unlock(&css_set_lock);
4872
Tejun Heo9871bf92013-06-24 15:21:47 -07004873 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004874 if (ret)
4875 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004876
Ben Blume6a11052010-03-10 15:22:09 -08004877 /* success! */
4878 mutex_unlock(&cgroup_mutex);
4879 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004880
4881err_unload:
4882 mutex_unlock(&cgroup_mutex);
4883 /* @ss can't be mounted here as try_module_get() would fail */
4884 cgroup_unload_subsys(ss);
4885 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004886}
4887EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4888
4889/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004890 * cgroup_unload_subsys: unload a modular subsystem
4891 * @ss: the subsystem to unload
4892 *
4893 * This function should be called in a modular subsystem's exitcall. When this
4894 * function is invoked, the refcount on the subsystem's module will be 0, so
4895 * the subsystem will not be attached to any hierarchy.
4896 */
4897void cgroup_unload_subsys(struct cgroup_subsys *ss)
4898{
Tejun Heo69d02062013-06-12 21:04:50 -07004899 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004900
4901 BUG_ON(ss->module == NULL);
4902
4903 /*
4904 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004905 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004906 * doesn't start being used while we're killing it off.
4907 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004908 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004909
4910 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004911
Tejun Heo9871bf92013-06-24 15:21:47 -07004912 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004913
Tejun Heoc897ff62013-02-27 17:03:49 -08004914 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004915 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004916
Ben Blumcf5d5942010-03-10 15:22:09 -08004917 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004918 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004919
Tejun Heo9871bf92013-06-24 15:21:47 -07004920 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004921 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004922
4923 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004924 * disentangle the css from all css_sets attached to the dummy
4925 * top. as in loading, we need to pay our respects to the hashtable
4926 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004927 */
4928 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004929 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004930 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004931 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004932
Tejun Heo5abb8852013-06-12 21:04:49 -07004933 hash_del(&cset->hlist);
4934 cset->subsys[ss->subsys_id] = NULL;
4935 key = css_set_hash(cset->subsys);
4936 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004937 }
4938 write_unlock(&css_set_lock);
4939
4940 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004941 * remove subsystem's css from the cgroup_dummy_top and free it -
4942 * need to free before marking as null because ss->css_free needs
4943 * the cgrp->subsys pointer to find their state. note that this
4944 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004945 */
Tejun Heoeb954192013-08-08 20:11:23 -04004946 ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
Tejun Heo9871bf92013-06-24 15:21:47 -07004947 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004948
4949 mutex_unlock(&cgroup_mutex);
4950}
4951EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4952
4953/**
Li Zefana043e3b2008-02-23 15:24:09 -08004954 * cgroup_init_early - cgroup initialization at system boot
4955 *
4956 * Initialize cgroups at system boot, and initialize any
4957 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004958 */
4959int __init cgroup_init_early(void)
4960{
Tejun Heo30159ec2013-06-25 11:53:37 -07004961 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004962 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004963
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004964 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004965 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004966 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004967 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004968 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004969 init_cgroup_root(&cgroup_dummy_root);
4970 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004971 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004972
Tejun Heo69d02062013-06-12 21:04:50 -07004973 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004974 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4975 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004976 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004977
Tejun Heo30159ec2013-06-25 11:53:37 -07004978 /* at bootup time, we don't worry about modular subsystems */
4979 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004980 BUG_ON(!ss->name);
4981 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004982 BUG_ON(!ss->css_alloc);
4983 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004984 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004985 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004986 ss->name, ss->subsys_id);
4987 BUG();
4988 }
4989
4990 if (ss->early_init)
4991 cgroup_init_subsys(ss);
4992 }
4993 return 0;
4994}
4995
4996/**
Li Zefana043e3b2008-02-23 15:24:09 -08004997 * cgroup_init - cgroup initialization
4998 *
4999 * Register cgroup filesystem and /proc file, and initialize
5000 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07005001 */
5002int __init cgroup_init(void)
5003{
Tejun Heo30159ec2013-06-25 11:53:37 -07005004 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08005005 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07005006 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005007
5008 err = bdi_init(&cgroup_backing_dev_info);
5009 if (err)
5010 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005011
Tejun Heo30159ec2013-06-25 11:53:37 -07005012 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005013 if (!ss->early_init)
5014 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005015 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005016 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005017 }
5018
Tejun Heofa3ca072013-04-14 11:36:56 -07005019 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005020 mutex_lock(&cgroup_mutex);
5021 mutex_lock(&cgroup_root_mutex);
5022
Tejun Heo82fe9b02013-06-25 11:53:37 -07005023 /* Add init_css_set to the hash table */
5024 key = css_set_hash(init_css_set.subsys);
5025 hash_add(css_set_table, &init_css_set.hlist, key);
5026
Tejun Heofc76df72013-06-25 11:53:37 -07005027 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005028
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005029 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5030 0, 1, GFP_KERNEL);
5031 BUG_ON(err < 0);
5032
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005033 mutex_unlock(&cgroup_root_mutex);
5034 mutex_unlock(&cgroup_mutex);
5035
Greg KH676db4a2010-08-05 13:53:35 -07005036 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5037 if (!cgroup_kobj) {
5038 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005039 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005040 }
5041
5042 err = register_filesystem(&cgroup_fs_type);
5043 if (err < 0) {
5044 kobject_put(cgroup_kobj);
5045 goto out;
5046 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005047
Li Zefan46ae2202008-04-29 01:00:08 -07005048 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005049
Paul Menageddbcc7e2007-10-18 23:39:30 -07005050out:
Paul Menagea4243162007-10-18 23:39:35 -07005051 if (err)
5052 bdi_destroy(&cgroup_backing_dev_info);
5053
Paul Menageddbcc7e2007-10-18 23:39:30 -07005054 return err;
5055}
Paul Menageb4f48b62007-10-18 23:39:33 -07005056
Paul Menagea4243162007-10-18 23:39:35 -07005057/*
5058 * proc_cgroup_show()
5059 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5060 * - Used for /proc/<pid>/cgroup.
5061 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5062 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005063 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005064 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5065 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5066 * cgroup to top_cgroup.
5067 */
5068
5069/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005070int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005071{
5072 struct pid *pid;
5073 struct task_struct *tsk;
5074 char *buf;
5075 int retval;
5076 struct cgroupfs_root *root;
5077
5078 retval = -ENOMEM;
5079 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5080 if (!buf)
5081 goto out;
5082
5083 retval = -ESRCH;
5084 pid = m->private;
5085 tsk = get_pid_task(pid, PIDTYPE_PID);
5086 if (!tsk)
5087 goto out_free;
5088
5089 retval = 0;
5090
5091 mutex_lock(&cgroup_mutex);
5092
Li Zefane5f6a862009-01-07 18:07:41 -08005093 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005094 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005095 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005096 int count = 0;
5097
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005098 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005099 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005100 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005101 if (strlen(root->name))
5102 seq_printf(m, "%sname=%s", count ? "," : "",
5103 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005104 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005105 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005106 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005107 if (retval < 0)
5108 goto out_unlock;
5109 seq_puts(m, buf);
5110 seq_putc(m, '\n');
5111 }
5112
5113out_unlock:
5114 mutex_unlock(&cgroup_mutex);
5115 put_task_struct(tsk);
5116out_free:
5117 kfree(buf);
5118out:
5119 return retval;
5120}
5121
Paul Menagea4243162007-10-18 23:39:35 -07005122/* Display information about each subsystem and each hierarchy */
5123static int proc_cgroupstats_show(struct seq_file *m, void *v)
5124{
Tejun Heo30159ec2013-06-25 11:53:37 -07005125 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005126 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005127
Paul Menage8bab8dd2008-04-04 14:29:57 -07005128 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005129 /*
5130 * ideally we don't want subsystems moving around while we do this.
5131 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5132 * subsys/hierarchy state.
5133 */
Paul Menagea4243162007-10-18 23:39:35 -07005134 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005135
5136 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005137 seq_printf(m, "%s\t%d\t%d\t%d\n",
5138 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005139 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005140
Paul Menagea4243162007-10-18 23:39:35 -07005141 mutex_unlock(&cgroup_mutex);
5142 return 0;
5143}
5144
5145static int cgroupstats_open(struct inode *inode, struct file *file)
5146{
Al Viro9dce07f2008-03-29 03:07:28 +00005147 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005148}
5149
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005150static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005151 .open = cgroupstats_open,
5152 .read = seq_read,
5153 .llseek = seq_lseek,
5154 .release = single_release,
5155};
5156
Paul Menageb4f48b62007-10-18 23:39:33 -07005157/**
5158 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005159 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005160 *
5161 * Description: A task inherits its parent's cgroup at fork().
5162 *
5163 * A pointer to the shared css_set was automatically copied in
5164 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005165 * it was not made under the protection of RCU or cgroup_mutex, so
5166 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5167 * have already changed current->cgroups, allowing the previously
5168 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005169 *
5170 * At the point that cgroup_fork() is called, 'current' is the parent
5171 * task, and the passed argument 'child' points to the child task.
5172 */
5173void cgroup_fork(struct task_struct *child)
5174{
Tejun Heo9bb71302012-10-18 17:52:07 -07005175 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005176 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005177 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005178 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005179 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005180}
5181
5182/**
Li Zefana043e3b2008-02-23 15:24:09 -08005183 * cgroup_post_fork - called on a new task after adding it to the task list
5184 * @child: the task in question
5185 *
Tejun Heo5edee612012-10-16 15:03:14 -07005186 * Adds the task to the list running through its css_set if necessary and
5187 * call the subsystem fork() callbacks. Has to be after the task is
5188 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005189 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005190 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005191 */
Paul Menage817929e2007-10-18 23:39:36 -07005192void cgroup_post_fork(struct task_struct *child)
5193{
Tejun Heo30159ec2013-06-25 11:53:37 -07005194 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005195 int i;
5196
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005197 /*
5198 * use_task_css_set_links is set to 1 before we walk the tasklist
5199 * under the tasklist_lock and we read it here after we added the child
5200 * to the tasklist under the tasklist_lock as well. If the child wasn't
5201 * yet in the tasklist when we walked through it from
5202 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5203 * should be visible now due to the paired locking and barriers implied
5204 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5205 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5206 * lock on fork.
5207 */
Paul Menage817929e2007-10-18 23:39:36 -07005208 if (use_task_css_set_links) {
5209 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005210 task_lock(child);
5211 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005212 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005213 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005214 write_unlock(&css_set_lock);
5215 }
Tejun Heo5edee612012-10-16 15:03:14 -07005216
5217 /*
5218 * Call ss->fork(). This must happen after @child is linked on
5219 * css_set; otherwise, @child might change state between ->fork()
5220 * and addition to css_set.
5221 */
5222 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005223 /*
5224 * fork/exit callbacks are supported only for builtin
5225 * subsystems, and the builtin section of the subsys
5226 * array is immutable, so we don't need to lock the
5227 * subsys array here. On the other hand, modular section
5228 * of the array can be freed at module unload, so we
5229 * can't touch that.
5230 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005231 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005232 if (ss->fork)
5233 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005234 }
Paul Menage817929e2007-10-18 23:39:36 -07005235}
Tejun Heo5edee612012-10-16 15:03:14 -07005236
Paul Menage817929e2007-10-18 23:39:36 -07005237/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005238 * cgroup_exit - detach cgroup from exiting task
5239 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005240 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005241 *
5242 * Description: Detach cgroup from @tsk and release it.
5243 *
5244 * Note that cgroups marked notify_on_release force every task in
5245 * them to take the global cgroup_mutex mutex when exiting.
5246 * This could impact scaling on very large systems. Be reluctant to
5247 * use notify_on_release cgroups where very high task exit scaling
5248 * is required on large systems.
5249 *
5250 * the_top_cgroup_hack:
5251 *
5252 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5253 *
5254 * We call cgroup_exit() while the task is still competent to
5255 * handle notify_on_release(), then leave the task attached to the
5256 * root cgroup in each hierarchy for the remainder of its exit.
5257 *
5258 * To do this properly, we would increment the reference count on
5259 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5260 * code we would add a second cgroup function call, to drop that
5261 * reference. This would just create an unnecessary hot spot on
5262 * the top_cgroup reference count, to no avail.
5263 *
5264 * Normally, holding a reference to a cgroup without bumping its
5265 * count is unsafe. The cgroup could go away, or someone could
5266 * attach us to a different cgroup, decrementing the count on
5267 * the first cgroup that we never incremented. But in this case,
5268 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005269 * which wards off any cgroup_attach_task() attempts, or task is a failed
5270 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005271 */
5272void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5273{
Tejun Heo30159ec2013-06-25 11:53:37 -07005274 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005275 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005276 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005277
5278 /*
5279 * Unlink from the css_set task list if necessary.
5280 * Optimistically check cg_list before taking
5281 * css_set_lock
5282 */
5283 if (!list_empty(&tsk->cg_list)) {
5284 write_lock(&css_set_lock);
5285 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005286 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005287 write_unlock(&css_set_lock);
5288 }
5289
Paul Menageb4f48b62007-10-18 23:39:33 -07005290 /* Reassign the task to the init_css_set. */
5291 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005292 cset = task_css_set(tsk);
5293 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005294
5295 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005296 /*
5297 * fork/exit callbacks are supported only for builtin
5298 * subsystems, see cgroup_post_fork() for details.
5299 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005300 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005301 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005302 struct cgroup_subsys_state *old_css = cset->subsys[i];
5303 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005304
Tejun Heoeb954192013-08-08 20:11:23 -04005305 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005306 }
5307 }
5308 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005309 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005310
Tejun Heo5abb8852013-06-12 21:04:49 -07005311 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005312}
Paul Menage697f4162007-10-18 23:39:34 -07005313
Paul Menagebd89aab2007-10-18 23:40:44 -07005314static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005315{
Li Zefanf50daa72013-03-01 15:06:07 +08005316 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005317 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005318 /*
5319 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005320 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005321 * it now
5322 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005323 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005324
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005325 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005326 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005327 list_empty(&cgrp->release_list)) {
5328 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005329 need_schedule_work = 1;
5330 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005331 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005332 if (need_schedule_work)
5333 schedule_work(&release_agent_work);
5334 }
5335}
5336
Paul Menage81a6a5c2007-10-18 23:39:38 -07005337/*
5338 * Notify userspace when a cgroup is released, by running the
5339 * configured release agent with the name of the cgroup (path
5340 * relative to the root of cgroup file system) as the argument.
5341 *
5342 * Most likely, this user command will try to rmdir this cgroup.
5343 *
5344 * This races with the possibility that some other task will be
5345 * attached to this cgroup before it is removed, or that some other
5346 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5347 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5348 * unused, and this cgroup will be reprieved from its death sentence,
5349 * to continue to serve a useful existence. Next time it's released,
5350 * we will get notified again, if it still has 'notify_on_release' set.
5351 *
5352 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5353 * means only wait until the task is successfully execve()'d. The
5354 * separate release agent task is forked by call_usermodehelper(),
5355 * then control in this thread returns here, without waiting for the
5356 * release agent task. We don't bother to wait because the caller of
5357 * this routine has no use for the exit status of the release agent
5358 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005359 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005360static void cgroup_release_agent(struct work_struct *work)
5361{
5362 BUG_ON(work != &release_agent_work);
5363 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005364 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005365 while (!list_empty(&release_list)) {
5366 char *argv[3], *envp[3];
5367 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005368 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005369 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005370 struct cgroup,
5371 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005372 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005373 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005374 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005375 if (!pathbuf)
5376 goto continue_free;
5377 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5378 goto continue_free;
5379 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5380 if (!agentbuf)
5381 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005382
5383 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005384 argv[i++] = agentbuf;
5385 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005386 argv[i] = NULL;
5387
5388 i = 0;
5389 /* minimal command environment */
5390 envp[i++] = "HOME=/";
5391 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5392 envp[i] = NULL;
5393
5394 /* Drop the lock while we invoke the usermode helper,
5395 * since the exec could involve hitting disk and hence
5396 * be a slow process */
5397 mutex_unlock(&cgroup_mutex);
5398 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005399 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005400 continue_free:
5401 kfree(pathbuf);
5402 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005403 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005404 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005405 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005406 mutex_unlock(&cgroup_mutex);
5407}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005408
5409static int __init cgroup_disable(char *str)
5410{
Tejun Heo30159ec2013-06-25 11:53:37 -07005411 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005412 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005413 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005414
5415 while ((token = strsep(&str, ",")) != NULL) {
5416 if (!*token)
5417 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005418
Tejun Heo30159ec2013-06-25 11:53:37 -07005419 /*
5420 * cgroup_disable, being at boot time, can't know about
5421 * module subsystems, so we don't worry about them.
5422 */
5423 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005424 if (!strcmp(token, ss->name)) {
5425 ss->disabled = 1;
5426 printk(KERN_INFO "Disabling %s control group"
5427 " subsystem\n", ss->name);
5428 break;
5429 }
5430 }
5431 }
5432 return 1;
5433}
5434__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005435
5436/*
5437 * Functons for CSS ID.
5438 */
5439
Tejun Heo54766d42013-06-12 21:04:53 -07005440/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005441unsigned short css_id(struct cgroup_subsys_state *css)
5442{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005443 struct css_id *cssid;
5444
5445 /*
5446 * This css_id() can return correct value when somone has refcnt
5447 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5448 * it's unchanged until freed.
5449 */
Tejun Heod3daf282013-06-13 19:39:16 -07005450 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005451
5452 if (cssid)
5453 return cssid->id;
5454 return 0;
5455}
Ben Blum67523c42010-03-10 15:22:11 -08005456EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005457
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005458/**
5459 * css_is_ancestor - test "root" css is an ancestor of "child"
5460 * @child: the css to be tested.
5461 * @root: the css supporsed to be an ancestor of the child.
5462 *
5463 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005464 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005465 * But, considering usual usage, the csses should be valid objects after test.
5466 * Assuming that the caller will do some action to the child if this returns
5467 * returns true, the caller must take "child";s reference count.
5468 * If "child" is valid object and this returns true, "root" is valid, too.
5469 */
5470
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005471bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005472 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005473{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005474 struct css_id *child_id;
5475 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005476
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005477 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005478 if (!child_id)
5479 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005480 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005481 if (!root_id)
5482 return false;
5483 if (child_id->depth < root_id->depth)
5484 return false;
5485 if (child_id->stack[root_id->depth] != root_id->id)
5486 return false;
5487 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005488}
5489
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005490void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5491{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005492 struct css_id *id = rcu_dereference_protected(css->id, true);
5493
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005494 /* When this is called before css_id initialization, id can be NULL */
5495 if (!id)
5496 return;
5497
5498 BUG_ON(!ss->use_id);
5499
5500 rcu_assign_pointer(id->css, NULL);
5501 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005502 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005503 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005504 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005505 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005506}
Ben Blum67523c42010-03-10 15:22:11 -08005507EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005508
5509/*
5510 * This is called by init or create(). Then, calls to this function are
5511 * always serialized (By cgroup_mutex() at create()).
5512 */
5513
5514static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5515{
5516 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005517 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005518
5519 BUG_ON(!ss->use_id);
5520
5521 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5522 newid = kzalloc(size, GFP_KERNEL);
5523 if (!newid)
5524 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005525
5526 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005527 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005528 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005529 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005530 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005531 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005532
5533 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005534 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005535 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005536
Tejun Heod228d9e2013-02-27 17:04:54 -08005537 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005538 newid->depth = depth;
5539 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005540err_out:
5541 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005542 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005543
5544}
5545
Ben Blume6a11052010-03-10 15:22:09 -08005546static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5547 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005548{
5549 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005550
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005551 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005552 idr_init(&ss->idr);
5553
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005554 newid = get_new_cssid(ss, 0);
5555 if (IS_ERR(newid))
5556 return PTR_ERR(newid);
5557
5558 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005559 RCU_INIT_POINTER(newid->css, rootcss);
5560 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005561 return 0;
5562}
5563
5564static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5565 struct cgroup *child)
5566{
5567 int subsys_id, i, depth = 0;
5568 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005569 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005570
5571 subsys_id = ss->subsys_id;
5572 parent_css = parent->subsys[subsys_id];
5573 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005574 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005575 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005576
5577 child_id = get_new_cssid(ss, depth);
5578 if (IS_ERR(child_id))
5579 return PTR_ERR(child_id);
5580
5581 for (i = 0; i < depth; i++)
5582 child_id->stack[i] = parent_id->stack[i];
5583 child_id->stack[depth] = child_id->id;
5584 /*
5585 * child_id->css pointer will be set after this cgroup is available
5586 * see cgroup_populate_dir()
5587 */
5588 rcu_assign_pointer(child_css->id, child_id);
5589
5590 return 0;
5591}
5592
5593/**
5594 * css_lookup - lookup css by id
5595 * @ss: cgroup subsys to be looked into.
5596 * @id: the id
5597 *
5598 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5599 * NULL if not. Should be called under rcu_read_lock()
5600 */
5601struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5602{
5603 struct css_id *cssid = NULL;
5604
5605 BUG_ON(!ss->use_id);
5606 cssid = idr_find(&ss->idr, id);
5607
5608 if (unlikely(!cssid))
5609 return NULL;
5610
5611 return rcu_dereference(cssid->css);
5612}
Ben Blum67523c42010-03-10 15:22:11 -08005613EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005614
Stephane Eraniane5d13672011-02-14 11:20:01 +02005615/*
5616 * get corresponding css from file open on cgroupfs directory
5617 */
5618struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5619{
5620 struct cgroup *cgrp;
5621 struct inode *inode;
5622 struct cgroup_subsys_state *css;
5623
Al Viro496ad9a2013-01-23 17:07:38 -05005624 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005625 /* check in cgroup filesystem dir */
5626 if (inode->i_op != &cgroup_dir_inode_operations)
5627 return ERR_PTR(-EBADF);
5628
5629 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5630 return ERR_PTR(-EINVAL);
5631
5632 /* get cgroup */
5633 cgrp = __d_cgrp(f->f_dentry);
5634 css = cgrp->subsys[id];
5635 return css ? css : ERR_PTR(-ENOENT);
5636}
5637
Paul Menagefe693432009-09-23 15:56:20 -07005638#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005639static struct cgroup_subsys_state *
5640debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005641{
5642 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5643
5644 if (!css)
5645 return ERR_PTR(-ENOMEM);
5646
5647 return css;
5648}
5649
Tejun Heoeb954192013-08-08 20:11:23 -04005650static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005651{
Tejun Heoeb954192013-08-08 20:11:23 -04005652 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005653}
5654
Tejun Heo182446d2013-08-08 20:11:24 -04005655static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5656 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005657{
Tejun Heo182446d2013-08-08 20:11:24 -04005658 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005659}
5660
Tejun Heo182446d2013-08-08 20:11:24 -04005661static u64 current_css_set_read(struct cgroup_subsys_state *css,
5662 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005663{
5664 return (u64)(unsigned long)current->cgroups;
5665}
5666
Tejun Heo182446d2013-08-08 20:11:24 -04005667static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005668 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005669{
5670 u64 count;
5671
5672 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005673 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005674 rcu_read_unlock();
5675 return count;
5676}
5677
Tejun Heo182446d2013-08-08 20:11:24 -04005678static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005679 struct cftype *cft,
5680 struct seq_file *seq)
5681{
Tejun Heo69d02062013-06-12 21:04:50 -07005682 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005683 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005684
5685 read_lock(&css_set_lock);
5686 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005687 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005688 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005689 struct cgroup *c = link->cgrp;
5690 const char *name;
5691
5692 if (c->dentry)
5693 name = c->dentry->d_name.name;
5694 else
5695 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005696 seq_printf(seq, "Root %d group %s\n",
5697 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005698 }
5699 rcu_read_unlock();
5700 read_unlock(&css_set_lock);
5701 return 0;
5702}
5703
5704#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005705static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5706 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005707{
Tejun Heo69d02062013-06-12 21:04:50 -07005708 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005709
5710 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005711 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005712 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005713 struct task_struct *task;
5714 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005715 seq_printf(seq, "css_set %p\n", cset);
5716 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005717 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5718 seq_puts(seq, " ...\n");
5719 break;
5720 } else {
5721 seq_printf(seq, " task %d\n",
5722 task_pid_vnr(task));
5723 }
5724 }
5725 }
5726 read_unlock(&css_set_lock);
5727 return 0;
5728}
5729
Tejun Heo182446d2013-08-08 20:11:24 -04005730static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005731{
Tejun Heo182446d2013-08-08 20:11:24 -04005732 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005733}
5734
5735static struct cftype debug_files[] = {
5736 {
Paul Menagefe693432009-09-23 15:56:20 -07005737 .name = "taskcount",
5738 .read_u64 = debug_taskcount_read,
5739 },
5740
5741 {
5742 .name = "current_css_set",
5743 .read_u64 = current_css_set_read,
5744 },
5745
5746 {
5747 .name = "current_css_set_refcount",
5748 .read_u64 = current_css_set_refcount_read,
5749 },
5750
5751 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005752 .name = "current_css_set_cg_links",
5753 .read_seq_string = current_css_set_cg_links_read,
5754 },
5755
5756 {
5757 .name = "cgroup_css_links",
5758 .read_seq_string = cgroup_css_links_read,
5759 },
5760
5761 {
Paul Menagefe693432009-09-23 15:56:20 -07005762 .name = "releasable",
5763 .read_u64 = releasable_read,
5764 },
Paul Menagefe693432009-09-23 15:56:20 -07005765
Tejun Heo4baf6e32012-04-01 12:09:55 -07005766 { } /* terminate */
5767};
Paul Menagefe693432009-09-23 15:56:20 -07005768
5769struct cgroup_subsys debug_subsys = {
5770 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005771 .css_alloc = debug_css_alloc,
5772 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005773 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005774 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005775};
5776#endif /* CONFIG_CGROUP_DEBUG */