blob: 52f0498db946d688b3438f8ee024a25f2d0e7a44 [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 /* add/rm files for all cgroups created before */
2872 rcu_read_lock();
Tejun Heo492eb212013-08-08 20:11:25 -04002873 css_for_each_descendant_pre(css, cgroup_css(root, ss->subsys_id)) {
2874 struct cgroup *cgrp = css->cgroup;
2875
Li Zefane8c82d22013-06-18 18:48:37 +08002876 if (cgroup_is_dead(cgrp))
2877 continue;
2878
2879 inode = cgrp->dentry->d_inode;
2880 dget(cgrp->dentry);
2881 rcu_read_unlock();
2882
2883 dput(prev);
2884 prev = cgrp->dentry;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002885
2886 mutex_lock(&inode->i_mutex);
2887 mutex_lock(&cgroup_mutex);
Tejun Heo00356bd2013-06-18 11:14:22 -07002888 if (cgrp->serial_nr < update_before && !cgroup_is_dead(cgrp))
Tejun Heo2bb566c2013-08-08 20:11:23 -04002889 ret = cgroup_addrm_files(cgrp, cfts, is_add);
Tejun Heo8e3f6542012-04-01 12:09:55 -07002890 mutex_unlock(&cgroup_mutex);
2891 mutex_unlock(&inode->i_mutex);
2892
Li Zefane8c82d22013-06-18 18:48:37 +08002893 rcu_read_lock();
Tejun Heo9ccece82013-06-28 16:24:11 -07002894 if (ret)
2895 break;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002896 }
Li Zefane8c82d22013-06-18 18:48:37 +08002897 rcu_read_unlock();
2898 dput(prev);
2899 deactivate_super(sb);
Tejun Heo9ccece82013-06-28 16:24:11 -07002900 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002901}
2902
2903/**
2904 * cgroup_add_cftypes - add an array of cftypes to a subsystem
2905 * @ss: target cgroup subsystem
2906 * @cfts: zero-length name terminated array of cftypes
2907 *
2908 * Register @cfts to @ss. Files described by @cfts are created for all
2909 * existing cgroups to which @ss is attached and all future cgroups will
2910 * have them too. This function can be called anytime whether @ss is
2911 * attached or not.
2912 *
2913 * Returns 0 on successful registration, -errno on failure. Note that this
2914 * function currently returns 0 as long as @cfts registration is successful
2915 * even if some file creation attempts on existing cgroups fail.
2916 */
Aristeu Rozanski03b1cde2012-08-23 16:53:30 -04002917int cgroup_add_cftypes(struct cgroup_subsys *ss, struct cftype *cfts)
Tejun Heo8e3f6542012-04-01 12:09:55 -07002918{
2919 struct cftype_set *set;
Tejun Heo2bb566c2013-08-08 20:11:23 -04002920 struct cftype *cft;
Tejun Heo9ccece82013-06-28 16:24:11 -07002921 int ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002922
2923 set = kzalloc(sizeof(*set), GFP_KERNEL);
2924 if (!set)
2925 return -ENOMEM;
2926
Tejun Heo2bb566c2013-08-08 20:11:23 -04002927 for (cft = cfts; cft->name[0] != '\0'; cft++)
2928 cft->ss = ss;
2929
Tejun Heo8e3f6542012-04-01 12:09:55 -07002930 cgroup_cfts_prepare();
2931 set->cfts = cfts;
2932 list_add_tail(&set->node, &ss->cftsets);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002933 ret = cgroup_cfts_commit(cfts, true);
Tejun Heo9ccece82013-06-28 16:24:11 -07002934 if (ret)
Tejun Heo2bb566c2013-08-08 20:11:23 -04002935 cgroup_rm_cftypes(cfts);
Tejun Heo9ccece82013-06-28 16:24:11 -07002936 return ret;
Tejun Heo8e3f6542012-04-01 12:09:55 -07002937}
2938EXPORT_SYMBOL_GPL(cgroup_add_cftypes);
2939
Li Zefana043e3b2008-02-23 15:24:09 -08002940/**
Tejun Heo79578622012-04-01 12:09:56 -07002941 * cgroup_rm_cftypes - remove an array of cftypes from a subsystem
Tejun Heo79578622012-04-01 12:09:56 -07002942 * @cfts: zero-length name terminated array of cftypes
2943 *
Tejun Heo2bb566c2013-08-08 20:11:23 -04002944 * Unregister @cfts. Files described by @cfts are removed from all
2945 * existing cgroups and all future cgroups won't have them either. This
2946 * function can be called anytime whether @cfts' subsys is attached or not.
Tejun Heo79578622012-04-01 12:09:56 -07002947 *
2948 * Returns 0 on successful unregistration, -ENOENT if @cfts is not
Tejun Heo2bb566c2013-08-08 20:11:23 -04002949 * registered.
Tejun Heo79578622012-04-01 12:09:56 -07002950 */
Tejun Heo2bb566c2013-08-08 20:11:23 -04002951int cgroup_rm_cftypes(struct cftype *cfts)
Tejun Heo79578622012-04-01 12:09:56 -07002952{
2953 struct cftype_set *set;
2954
Tejun Heo2bb566c2013-08-08 20:11:23 -04002955 if (!cfts || !cfts[0].ss)
2956 return -ENOENT;
2957
Tejun Heo79578622012-04-01 12:09:56 -07002958 cgroup_cfts_prepare();
2959
Tejun Heo2bb566c2013-08-08 20:11:23 -04002960 list_for_each_entry(set, &cfts[0].ss->cftsets, node) {
Tejun Heo79578622012-04-01 12:09:56 -07002961 if (set->cfts == cfts) {
Li Zefanf57947d2013-06-18 18:41:53 +08002962 list_del(&set->node);
2963 kfree(set);
Tejun Heo2bb566c2013-08-08 20:11:23 -04002964 cgroup_cfts_commit(cfts, false);
Tejun Heo79578622012-04-01 12:09:56 -07002965 return 0;
2966 }
2967 }
2968
Tejun Heo2bb566c2013-08-08 20:11:23 -04002969 cgroup_cfts_commit(NULL, false);
Tejun Heo79578622012-04-01 12:09:56 -07002970 return -ENOENT;
2971}
2972
2973/**
Li Zefana043e3b2008-02-23 15:24:09 -08002974 * cgroup_task_count - count the number of tasks in a cgroup.
2975 * @cgrp: the cgroup in question
2976 *
2977 * Return the number of tasks in the cgroup.
2978 */
Paul Menagebd89aab2007-10-18 23:40:44 -07002979int cgroup_task_count(const struct cgroup *cgrp)
Paul Menagebbcb81d2007-10-18 23:39:32 -07002980{
2981 int count = 0;
Tejun Heo69d02062013-06-12 21:04:50 -07002982 struct cgrp_cset_link *link;
Paul Menagebbcb81d2007-10-18 23:39:32 -07002983
Paul Menage817929e2007-10-18 23:39:36 -07002984 read_lock(&css_set_lock);
Tejun Heo69d02062013-06-12 21:04:50 -07002985 list_for_each_entry(link, &cgrp->cset_links, cset_link)
2986 count += atomic_read(&link->cset->refcount);
Paul Menage817929e2007-10-18 23:39:36 -07002987 read_unlock(&css_set_lock);
Paul Menagebbcb81d2007-10-18 23:39:32 -07002988 return count;
2989}
2990
2991/*
Tejun Heo0942eee2013-08-08 20:11:26 -04002992 * To reduce the fork() overhead for systems that are not actually using
2993 * their cgroups capability, we don't maintain the lists running through
2994 * each css_set to its tasks until we see the list actually used - in other
Tejun Heo72ec7022013-08-08 20:11:26 -04002995 * words after the first call to css_task_iter_start().
Cliff Wickman31a7df02008-02-07 00:14:42 -08002996 */
Adrian Bunk3df91fe2008-04-29 00:59:54 -07002997static void cgroup_enable_task_cg_lists(void)
Cliff Wickman31a7df02008-02-07 00:14:42 -08002998{
2999 struct task_struct *p, *g;
3000 write_lock(&css_set_lock);
3001 use_task_css_set_links = 1;
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003002 /*
3003 * We need tasklist_lock because RCU is not safe against
3004 * while_each_thread(). Besides, a forking task that has passed
3005 * cgroup_post_fork() without seeing use_task_css_set_links = 1
3006 * is not guaranteed to have its child immediately visible in the
3007 * tasklist if we walk through it with RCU.
3008 */
3009 read_lock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003010 do_each_thread(g, p) {
3011 task_lock(p);
Li Zefan0e043882008-04-17 11:37:15 +08003012 /*
3013 * We should check if the process is exiting, otherwise
3014 * it will race with cgroup_exit() in that the list
3015 * entry won't be deleted though the process has exited.
3016 */
3017 if (!(p->flags & PF_EXITING) && list_empty(&p->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07003018 list_add(&p->cg_list, &task_css_set(p)->tasks);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003019 task_unlock(p);
3020 } while_each_thread(g, p);
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01003021 read_unlock(&tasklist_lock);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003022 write_unlock(&css_set_lock);
3023}
3024
Tejun Heo574bd9f2012-11-09 09:12:29 -08003025/**
Tejun Heo492eb212013-08-08 20:11:25 -04003026 * css_next_child - find the next child of a given css
3027 * @pos_css: the current position (%NULL to initiate traversal)
3028 * @parent_css: css whose children to walk
Tejun Heo53fa5262013-05-24 10:55:38 +09003029 *
Tejun Heo492eb212013-08-08 20:11:25 -04003030 * This function returns the next child of @parent_css and should be called
3031 * under RCU read lock. The only requirement is that @parent_css and
3032 * @pos_css are accessible. The next sibling is guaranteed to be returned
3033 * regardless of their states.
Tejun Heo53fa5262013-05-24 10:55:38 +09003034 */
Tejun Heo492eb212013-08-08 20:11:25 -04003035struct cgroup_subsys_state *
3036css_next_child(struct cgroup_subsys_state *pos_css,
3037 struct cgroup_subsys_state *parent_css)
Tejun Heo53fa5262013-05-24 10:55:38 +09003038{
Tejun Heo492eb212013-08-08 20:11:25 -04003039 struct cgroup *pos = pos_css ? pos_css->cgroup : NULL;
3040 struct cgroup *cgrp = parent_css->cgroup;
Tejun Heo53fa5262013-05-24 10:55:38 +09003041 struct cgroup *next;
3042
3043 WARN_ON_ONCE(!rcu_read_lock_held());
3044
3045 /*
3046 * @pos could already have been removed. Once a cgroup is removed,
3047 * its ->sibling.next is no longer updated when its next sibling
Tejun Heoea15f8c2013-06-13 19:27:42 -07003048 * changes. As CGRP_DEAD assertion is serialized and happens
3049 * before the cgroup is taken off the ->sibling list, if we see it
3050 * unasserted, it's guaranteed that the next sibling hasn't
3051 * finished its grace period even if it's already removed, and thus
3052 * safe to dereference from this RCU critical section. If
3053 * ->sibling.next is inaccessible, cgroup_is_dead() is guaranteed
3054 * to be visible as %true here.
Tejun Heo3b287a52013-08-08 20:11:24 -04003055 *
3056 * If @pos is dead, its next pointer can't be dereferenced;
3057 * however, as each cgroup is given a monotonically increasing
3058 * unique serial number and always appended to the sibling list,
3059 * the next one can be found by walking the parent's children until
3060 * we see a cgroup with higher serial number than @pos's. While
3061 * this path can be slower, it's taken only when either the current
3062 * cgroup is removed or iteration and removal race.
Tejun Heo53fa5262013-05-24 10:55:38 +09003063 */
Tejun Heo3b287a52013-08-08 20:11:24 -04003064 if (!pos) {
3065 next = list_entry_rcu(cgrp->children.next, struct cgroup, sibling);
3066 } else if (likely(!cgroup_is_dead(pos))) {
Tejun Heo53fa5262013-05-24 10:55:38 +09003067 next = list_entry_rcu(pos->sibling.next, struct cgroup, sibling);
Tejun Heo3b287a52013-08-08 20:11:24 -04003068 } else {
3069 list_for_each_entry_rcu(next, &cgrp->children, sibling)
3070 if (next->serial_nr > pos->serial_nr)
3071 break;
Tejun Heo53fa5262013-05-24 10:55:38 +09003072 }
3073
Tejun Heo492eb212013-08-08 20:11:25 -04003074 if (&next->sibling == &cgrp->children)
3075 return NULL;
3076
3077 if (parent_css->ss)
3078 return cgroup_css(next, parent_css->ss->subsys_id);
3079 else
3080 return &next->dummy_css;
Tejun Heo53fa5262013-05-24 10:55:38 +09003081}
Tejun Heo492eb212013-08-08 20:11:25 -04003082EXPORT_SYMBOL_GPL(css_next_child);
Tejun Heo53fa5262013-05-24 10:55:38 +09003083
3084/**
Tejun Heo492eb212013-08-08 20:11:25 -04003085 * css_next_descendant_pre - find the next descendant for pre-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003086 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003087 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003088 *
Tejun Heo492eb212013-08-08 20:11:25 -04003089 * To be used by css_for_each_descendant_pre(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003090 * to visit for pre-order traversal of @root's descendants. @root is
3091 * included in the iteration and the first node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003092 *
3093 * While this function requires RCU read locking, it doesn't require the
3094 * whole traversal to be contained in a single RCU critical section. This
3095 * function will return the correct next descendant as long as both @pos
Tejun Heo492eb212013-08-08 20:11:25 -04003096 * and @root are accessible and @pos is a descendant of @root.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003097 */
Tejun Heo492eb212013-08-08 20:11:25 -04003098struct cgroup_subsys_state *
3099css_next_descendant_pre(struct cgroup_subsys_state *pos,
3100 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003101{
Tejun Heo492eb212013-08-08 20:11:25 -04003102 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003103
3104 WARN_ON_ONCE(!rcu_read_lock_held());
3105
Tejun Heobd8815a2013-08-08 20:11:27 -04003106 /* if first iteration, visit @root */
Tejun Heo7805d002013-05-24 10:50:24 +09003107 if (!pos)
Tejun Heobd8815a2013-08-08 20:11:27 -04003108 return root;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003109
3110 /* visit the first child if exists */
Tejun Heo492eb212013-08-08 20:11:25 -04003111 next = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003112 if (next)
3113 return next;
3114
3115 /* no child, visit my or the closest ancestor's next sibling */
Tejun Heo492eb212013-08-08 20:11:25 -04003116 while (pos != root) {
3117 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003118 if (next)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003119 return next;
Tejun Heo492eb212013-08-08 20:11:25 -04003120 pos = css_parent(pos);
Tejun Heo7805d002013-05-24 10:50:24 +09003121 }
Tejun Heo574bd9f2012-11-09 09:12:29 -08003122
3123 return NULL;
3124}
Tejun Heo492eb212013-08-08 20:11:25 -04003125EXPORT_SYMBOL_GPL(css_next_descendant_pre);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003126
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003127/**
Tejun Heo492eb212013-08-08 20:11:25 -04003128 * css_rightmost_descendant - return the rightmost descendant of a css
3129 * @pos: css of interest
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003130 *
Tejun Heo492eb212013-08-08 20:11:25 -04003131 * Return the rightmost descendant of @pos. If there's no descendant, @pos
3132 * is returned. This can be used during pre-order traversal to skip
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003133 * subtree of @pos.
Tejun Heo75501a62013-05-24 10:55:38 +09003134 *
3135 * While this function requires RCU read locking, it doesn't require the
3136 * whole traversal to be contained in a single RCU critical section. This
3137 * function will return the correct rightmost descendant as long as @pos is
3138 * accessible.
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003139 */
Tejun Heo492eb212013-08-08 20:11:25 -04003140struct cgroup_subsys_state *
3141css_rightmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003142{
Tejun Heo492eb212013-08-08 20:11:25 -04003143 struct cgroup_subsys_state *last, *tmp;
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003144
3145 WARN_ON_ONCE(!rcu_read_lock_held());
3146
3147 do {
3148 last = pos;
3149 /* ->prev isn't RCU safe, walk ->next till the end */
3150 pos = NULL;
Tejun Heo492eb212013-08-08 20:11:25 -04003151 css_for_each_child(tmp, last)
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003152 pos = tmp;
3153 } while (pos);
3154
3155 return last;
3156}
Tejun Heo492eb212013-08-08 20:11:25 -04003157EXPORT_SYMBOL_GPL(css_rightmost_descendant);
Tejun Heo12a9d2f2013-01-07 08:49:33 -08003158
Tejun Heo492eb212013-08-08 20:11:25 -04003159static struct cgroup_subsys_state *
3160css_leftmost_descendant(struct cgroup_subsys_state *pos)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003161{
Tejun Heo492eb212013-08-08 20:11:25 -04003162 struct cgroup_subsys_state *last;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003163
3164 do {
3165 last = pos;
Tejun Heo492eb212013-08-08 20:11:25 -04003166 pos = css_next_child(NULL, pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003167 } while (pos);
3168
3169 return last;
3170}
3171
3172/**
Tejun Heo492eb212013-08-08 20:11:25 -04003173 * css_next_descendant_post - find the next descendant for post-order walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003174 * @pos: the current position (%NULL to initiate traversal)
Tejun Heo492eb212013-08-08 20:11:25 -04003175 * @root: css whose descendants to walk
Tejun Heo574bd9f2012-11-09 09:12:29 -08003176 *
Tejun Heo492eb212013-08-08 20:11:25 -04003177 * To be used by css_for_each_descendant_post(). Find the next descendant
Tejun Heobd8815a2013-08-08 20:11:27 -04003178 * to visit for post-order traversal of @root's descendants. @root is
3179 * included in the iteration and the last node to be visited.
Tejun Heo75501a62013-05-24 10:55:38 +09003180 *
3181 * While this function requires RCU read locking, it doesn't require the
3182 * whole traversal to be contained in a single RCU critical section. This
3183 * function will return the correct next descendant as long as both @pos
3184 * and @cgroup are accessible and @pos is a descendant of @cgroup.
Tejun Heo574bd9f2012-11-09 09:12:29 -08003185 */
Tejun Heo492eb212013-08-08 20:11:25 -04003186struct cgroup_subsys_state *
3187css_next_descendant_post(struct cgroup_subsys_state *pos,
3188 struct cgroup_subsys_state *root)
Tejun Heo574bd9f2012-11-09 09:12:29 -08003189{
Tejun Heo492eb212013-08-08 20:11:25 -04003190 struct cgroup_subsys_state *next;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003191
3192 WARN_ON_ONCE(!rcu_read_lock_held());
3193
3194 /* if first iteration, visit the leftmost descendant */
3195 if (!pos) {
Tejun Heo492eb212013-08-08 20:11:25 -04003196 next = css_leftmost_descendant(root);
3197 return next != root ? next : NULL;
Tejun Heo574bd9f2012-11-09 09:12:29 -08003198 }
3199
Tejun Heobd8815a2013-08-08 20:11:27 -04003200 /* if we visited @root, we're done */
3201 if (pos == root)
3202 return NULL;
3203
Tejun Heo574bd9f2012-11-09 09:12:29 -08003204 /* if there's an unvisited sibling, visit its leftmost descendant */
Tejun Heo492eb212013-08-08 20:11:25 -04003205 next = css_next_child(pos, css_parent(pos));
Tejun Heo75501a62013-05-24 10:55:38 +09003206 if (next)
Tejun Heo492eb212013-08-08 20:11:25 -04003207 return css_leftmost_descendant(next);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003208
3209 /* no sibling left, visit parent */
Tejun Heobd8815a2013-08-08 20:11:27 -04003210 return css_parent(pos);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003211}
Tejun Heo492eb212013-08-08 20:11:25 -04003212EXPORT_SYMBOL_GPL(css_next_descendant_post);
Tejun Heo574bd9f2012-11-09 09:12:29 -08003213
Tejun Heo0942eee2013-08-08 20:11:26 -04003214/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003215 * css_advance_task_iter - advance a task itererator to the next css_set
Tejun Heo0942eee2013-08-08 20:11:26 -04003216 * @it: the iterator to advance
3217 *
3218 * Advance @it to the next css_set to walk.
Tejun Heod5158762013-08-08 20:11:26 -04003219 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003220static void css_advance_task_iter(struct css_task_iter *it)
Tejun Heod5158762013-08-08 20:11:26 -04003221{
3222 struct list_head *l = it->cset_link;
3223 struct cgrp_cset_link *link;
3224 struct css_set *cset;
3225
3226 /* Advance to the next non-empty css_set */
3227 do {
3228 l = l->next;
Tejun Heo72ec7022013-08-08 20:11:26 -04003229 if (l == &it->origin_css->cgroup->cset_links) {
Tejun Heod5158762013-08-08 20:11:26 -04003230 it->cset_link = NULL;
3231 return;
3232 }
3233 link = list_entry(l, struct cgrp_cset_link, cset_link);
3234 cset = link->cset;
3235 } while (list_empty(&cset->tasks));
3236 it->cset_link = l;
3237 it->task = cset->tasks.next;
3238}
3239
Tejun Heo0942eee2013-08-08 20:11:26 -04003240/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003241 * css_task_iter_start - initiate task iteration
3242 * @css: the css to walk tasks of
Tejun Heo0942eee2013-08-08 20:11:26 -04003243 * @it: the task iterator to use
3244 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003245 * Initiate iteration through the tasks of @css. The caller can call
3246 * css_task_iter_next() to walk through the tasks until the function
3247 * returns NULL. On completion of iteration, css_task_iter_end() must be
3248 * called.
Tejun Heo0942eee2013-08-08 20:11:26 -04003249 *
3250 * Note that this function acquires a lock which is released when the
3251 * iteration finishes. The caller can't sleep while iteration is in
3252 * progress.
3253 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003254void css_task_iter_start(struct cgroup_subsys_state *css,
3255 struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003256 __acquires(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003257{
3258 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003259 * The first time anyone tries to iterate across a css, we need to
3260 * enable the list linking each css_set to its tasks, and fix up
3261 * all existing tasks.
Paul Menage817929e2007-10-18 23:39:36 -07003262 */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003263 if (!use_task_css_set_links)
3264 cgroup_enable_task_cg_lists();
3265
Paul Menage817929e2007-10-18 23:39:36 -07003266 read_lock(&css_set_lock);
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003267
Tejun Heo72ec7022013-08-08 20:11:26 -04003268 it->origin_css = css;
3269 it->cset_link = &css->cgroup->cset_links;
Tejun Heoc59cd3d2013-08-08 20:11:26 -04003270
Tejun Heo72ec7022013-08-08 20:11:26 -04003271 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003272}
3273
Tejun Heo0942eee2013-08-08 20:11:26 -04003274/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003275 * css_task_iter_next - return the next task for the iterator
Tejun Heo0942eee2013-08-08 20:11:26 -04003276 * @it: the task iterator being iterated
3277 *
3278 * The "next" function for task iteration. @it should have been
Tejun Heo72ec7022013-08-08 20:11:26 -04003279 * initialized via css_task_iter_start(). Returns NULL when the iteration
3280 * reaches the end.
Tejun Heo0942eee2013-08-08 20:11:26 -04003281 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003282struct task_struct *css_task_iter_next(struct css_task_iter *it)
Paul Menage817929e2007-10-18 23:39:36 -07003283{
3284 struct task_struct *res;
3285 struct list_head *l = it->task;
Tejun Heo69d02062013-06-12 21:04:50 -07003286 struct cgrp_cset_link *link;
Paul Menage817929e2007-10-18 23:39:36 -07003287
3288 /* If the iterator cg is NULL, we have no tasks */
Tejun Heo69d02062013-06-12 21:04:50 -07003289 if (!it->cset_link)
Paul Menage817929e2007-10-18 23:39:36 -07003290 return NULL;
3291 res = list_entry(l, struct task_struct, cg_list);
3292 /* Advance iterator to find next entry */
3293 l = l->next;
Tejun Heo69d02062013-06-12 21:04:50 -07003294 link = list_entry(it->cset_link, struct cgrp_cset_link, cset_link);
3295 if (l == &link->cset->tasks) {
Tejun Heo0942eee2013-08-08 20:11:26 -04003296 /*
3297 * We reached the end of this task list - move on to the
3298 * next cgrp_cset_link.
3299 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003300 css_advance_task_iter(it);
Paul Menage817929e2007-10-18 23:39:36 -07003301 } else {
3302 it->task = l;
3303 }
3304 return res;
3305}
3306
Tejun Heo0942eee2013-08-08 20:11:26 -04003307/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003308 * css_task_iter_end - finish task iteration
Tejun Heo0942eee2013-08-08 20:11:26 -04003309 * @it: the task iterator to finish
3310 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003311 * Finish task iteration started by css_task_iter_start().
Tejun Heo0942eee2013-08-08 20:11:26 -04003312 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003313void css_task_iter_end(struct css_task_iter *it)
Kirill A. Shutemovc6ca5752011-12-27 07:46:26 +02003314 __releases(css_set_lock)
Paul Menage817929e2007-10-18 23:39:36 -07003315{
3316 read_unlock(&css_set_lock);
3317}
3318
Cliff Wickman31a7df02008-02-07 00:14:42 -08003319static inline int started_after_time(struct task_struct *t1,
3320 struct timespec *time,
3321 struct task_struct *t2)
3322{
3323 int start_diff = timespec_compare(&t1->start_time, time);
3324 if (start_diff > 0) {
3325 return 1;
3326 } else if (start_diff < 0) {
3327 return 0;
3328 } else {
3329 /*
3330 * Arbitrarily, if two processes started at the same
3331 * time, we'll say that the lower pointer value
3332 * started first. Note that t2 may have exited by now
3333 * so this may not be a valid pointer any longer, but
3334 * that's fine - it still serves to distinguish
3335 * between two tasks started (effectively) simultaneously.
3336 */
3337 return t1 > t2;
3338 }
3339}
3340
3341/*
3342 * This function is a callback from heap_insert() and is used to order
3343 * the heap.
3344 * In this case we order the heap in descending task start time.
3345 */
3346static inline int started_after(void *p1, void *p2)
3347{
3348 struct task_struct *t1 = p1;
3349 struct task_struct *t2 = p2;
3350 return started_after_time(t1, &t2->start_time, t2);
3351}
3352
3353/**
Tejun Heo72ec7022013-08-08 20:11:26 -04003354 * css_scan_tasks - iterate though all the tasks in a css
3355 * @css: the css to iterate tasks of
Tejun Heoe5358372013-08-08 20:11:26 -04003356 * @test: optional test callback
3357 * @process: process callback
3358 * @data: data passed to @test and @process
3359 * @heap: optional pre-allocated heap used for task iteration
Cliff Wickman31a7df02008-02-07 00:14:42 -08003360 *
Tejun Heo72ec7022013-08-08 20:11:26 -04003361 * Iterate through all the tasks in @css, calling @test for each, and if it
3362 * returns %true, call @process for it also.
Cliff Wickman31a7df02008-02-07 00:14:42 -08003363 *
Tejun Heoe5358372013-08-08 20:11:26 -04003364 * @test may be NULL, meaning always true (select all tasks), which
Tejun Heo72ec7022013-08-08 20:11:26 -04003365 * effectively duplicates css_task_iter_{start,next,end}() but does not
Tejun Heoe5358372013-08-08 20:11:26 -04003366 * lock css_set_lock for the call to @process.
3367 *
3368 * It is guaranteed that @process will act on every task that is a member
Tejun Heo72ec7022013-08-08 20:11:26 -04003369 * of @css for the duration of this call. This function may or may not
3370 * call @process for tasks that exit or move to a different css during the
3371 * call, or are forked or move into the css during the call.
Tejun Heoe5358372013-08-08 20:11:26 -04003372 *
3373 * Note that @test may be called with locks held, and may in some
3374 * situations be called multiple times for the same task, so it should be
3375 * cheap.
3376 *
3377 * If @heap is non-NULL, a heap has been pre-allocated and will be used for
3378 * heap operations (and its "gt" member will be overwritten), else a
3379 * temporary heap will be used (allocation of which may cause this function
3380 * to fail).
Cliff Wickman31a7df02008-02-07 00:14:42 -08003381 */
Tejun Heo72ec7022013-08-08 20:11:26 -04003382int css_scan_tasks(struct cgroup_subsys_state *css,
3383 bool (*test)(struct task_struct *, void *),
3384 void (*process)(struct task_struct *, void *),
3385 void *data, struct ptr_heap *heap)
Cliff Wickman31a7df02008-02-07 00:14:42 -08003386{
3387 int retval, i;
Tejun Heo72ec7022013-08-08 20:11:26 -04003388 struct css_task_iter it;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003389 struct task_struct *p, *dropped;
3390 /* Never dereference latest_task, since it's not refcounted */
3391 struct task_struct *latest_task = NULL;
3392 struct ptr_heap tmp_heap;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003393 struct timespec latest_time = { 0, 0 };
3394
Tejun Heoe5358372013-08-08 20:11:26 -04003395 if (heap) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003396 /* The caller supplied our heap and pre-allocated its memory */
Cliff Wickman31a7df02008-02-07 00:14:42 -08003397 heap->gt = &started_after;
3398 } else {
3399 /* We need to allocate our own heap memory */
3400 heap = &tmp_heap;
3401 retval = heap_init(heap, PAGE_SIZE, GFP_KERNEL, &started_after);
3402 if (retval)
3403 /* cannot allocate the heap */
3404 return retval;
3405 }
3406
3407 again:
3408 /*
Tejun Heo72ec7022013-08-08 20:11:26 -04003409 * Scan tasks in the css, using the @test callback to determine
Tejun Heoe5358372013-08-08 20:11:26 -04003410 * which are of interest, and invoking @process callback on the
3411 * ones which need an update. Since we don't want to hold any
3412 * locks during the task updates, gather tasks to be processed in a
3413 * heap structure. The heap is sorted by descending task start
3414 * time. If the statically-sized heap fills up, we overflow tasks
3415 * that started later, and in future iterations only consider tasks
3416 * that started after the latest task in the previous pass. This
Cliff Wickman31a7df02008-02-07 00:14:42 -08003417 * guarantees forward progress and that we don't miss any tasks.
3418 */
3419 heap->size = 0;
Tejun Heo72ec7022013-08-08 20:11:26 -04003420 css_task_iter_start(css, &it);
3421 while ((p = css_task_iter_next(&it))) {
Cliff Wickman31a7df02008-02-07 00:14:42 -08003422 /*
3423 * Only affect tasks that qualify per the caller's callback,
3424 * if he provided one
3425 */
Tejun Heoe5358372013-08-08 20:11:26 -04003426 if (test && !test(p, data))
Cliff Wickman31a7df02008-02-07 00:14:42 -08003427 continue;
3428 /*
3429 * Only process tasks that started after the last task
3430 * we processed
3431 */
3432 if (!started_after_time(p, &latest_time, latest_task))
3433 continue;
3434 dropped = heap_insert(heap, p);
3435 if (dropped == NULL) {
3436 /*
3437 * The new task was inserted; the heap wasn't
3438 * previously full
3439 */
3440 get_task_struct(p);
3441 } else if (dropped != p) {
3442 /*
3443 * The new task was inserted, and pushed out a
3444 * different task
3445 */
3446 get_task_struct(p);
3447 put_task_struct(dropped);
3448 }
3449 /*
3450 * Else the new task was newer than anything already in
3451 * the heap and wasn't inserted
3452 */
3453 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003454 css_task_iter_end(&it);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003455
3456 if (heap->size) {
3457 for (i = 0; i < heap->size; i++) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003458 struct task_struct *q = heap->ptrs[i];
Cliff Wickman31a7df02008-02-07 00:14:42 -08003459 if (i == 0) {
Paul Jackson4fe91d52008-04-29 00:59:55 -07003460 latest_time = q->start_time;
3461 latest_task = q;
Cliff Wickman31a7df02008-02-07 00:14:42 -08003462 }
3463 /* Process the task per the caller's callback */
Tejun Heoe5358372013-08-08 20:11:26 -04003464 process(q, data);
Paul Jackson4fe91d52008-04-29 00:59:55 -07003465 put_task_struct(q);
Cliff Wickman31a7df02008-02-07 00:14:42 -08003466 }
3467 /*
3468 * If we had to process any tasks at all, scan again
3469 * in case some of them were in the middle of forking
3470 * children that didn't get processed.
3471 * Not the most efficient way to do it, but it avoids
3472 * having to take callback_mutex in the fork path
3473 */
3474 goto again;
3475 }
3476 if (heap == &tmp_heap)
3477 heap_free(&tmp_heap);
3478 return 0;
3479}
3480
Tejun Heoe5358372013-08-08 20:11:26 -04003481static void cgroup_transfer_one_task(struct task_struct *task, void *data)
Tejun Heo8cc99342013-04-07 09:29:50 -07003482{
Tejun Heoe5358372013-08-08 20:11:26 -04003483 struct cgroup *new_cgroup = data;
Tejun Heo8cc99342013-04-07 09:29:50 -07003484
Tejun Heo47cfcd02013-04-07 09:29:51 -07003485 mutex_lock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003486 cgroup_attach_task(new_cgroup, task, false);
Tejun Heo47cfcd02013-04-07 09:29:51 -07003487 mutex_unlock(&cgroup_mutex);
Tejun Heo8cc99342013-04-07 09:29:50 -07003488}
3489
3490/**
3491 * cgroup_trasnsfer_tasks - move tasks from one cgroup to another
3492 * @to: cgroup to which the tasks will be moved
3493 * @from: cgroup in which the tasks currently reside
3494 */
3495int cgroup_transfer_tasks(struct cgroup *to, struct cgroup *from)
3496{
Tejun Heo72ec7022013-08-08 20:11:26 -04003497 return css_scan_tasks(&from->dummy_css, NULL, cgroup_transfer_one_task,
3498 to, NULL);
Tejun Heo8cc99342013-04-07 09:29:50 -07003499}
3500
Paul Menage817929e2007-10-18 23:39:36 -07003501/*
Ben Blum102a7752009-09-23 15:56:26 -07003502 * Stuff for reading the 'tasks'/'procs' files.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003503 *
3504 * Reading this file can return large amounts of data if a cgroup has
3505 * *lots* of attached tasks. So it may need several calls to read(),
3506 * but we cannot guarantee that the information we produce is correct
3507 * unless we produce it entirely atomically.
3508 *
Paul Menagebbcb81d2007-10-18 23:39:32 -07003509 */
Paul Menagebbcb81d2007-10-18 23:39:32 -07003510
Li Zefan24528252012-01-20 11:58:43 +08003511/* which pidlist file are we talking about? */
3512enum cgroup_filetype {
3513 CGROUP_FILE_PROCS,
3514 CGROUP_FILE_TASKS,
3515};
3516
3517/*
3518 * A pidlist is a list of pids that virtually represents the contents of one
3519 * of the cgroup files ("procs" or "tasks"). We keep a list of such pidlists,
3520 * a pair (one each for procs, tasks) for each pid namespace that's relevant
3521 * to the cgroup.
3522 */
3523struct cgroup_pidlist {
3524 /*
3525 * used to find which pidlist is wanted. doesn't change as long as
3526 * this particular list stays in the list.
3527 */
3528 struct { enum cgroup_filetype type; struct pid_namespace *ns; } key;
3529 /* array of xids */
3530 pid_t *list;
3531 /* how many elements the above list has */
3532 int length;
3533 /* how many files are using the current array */
3534 int use_count;
3535 /* each of these stored in a list by its cgroup */
3536 struct list_head links;
3537 /* pointer to the cgroup we belong to, for list removal purposes */
3538 struct cgroup *owner;
3539 /* protects the other fields */
Li Zefanb3958902013-08-01 09:52:15 +08003540 struct rw_semaphore rwsem;
Li Zefan24528252012-01-20 11:58:43 +08003541};
3542
Paul Menagebbcb81d2007-10-18 23:39:32 -07003543/*
Ben Blumd1d9fd32009-09-23 15:56:28 -07003544 * The following two functions "fix" the issue where there are more pids
3545 * than kmalloc will give memory for; in such cases, we use vmalloc/vfree.
3546 * TODO: replace with a kernel-wide solution to this problem
3547 */
3548#define PIDLIST_TOO_LARGE(c) ((c) * sizeof(pid_t) > (PAGE_SIZE * 2))
3549static void *pidlist_allocate(int count)
3550{
3551 if (PIDLIST_TOO_LARGE(count))
3552 return vmalloc(count * sizeof(pid_t));
3553 else
3554 return kmalloc(count * sizeof(pid_t), GFP_KERNEL);
3555}
3556static void pidlist_free(void *p)
3557{
3558 if (is_vmalloc_addr(p))
3559 vfree(p);
3560 else
3561 kfree(p);
3562}
Ben Blumd1d9fd32009-09-23 15:56:28 -07003563
3564/*
Ben Blum102a7752009-09-23 15:56:26 -07003565 * pidlist_uniq - given a kmalloc()ed list, strip out all duplicate entries
Li Zefan6ee211a2013-03-12 15:36:00 -07003566 * Returns the number of unique elements.
Paul Menagebbcb81d2007-10-18 23:39:32 -07003567 */
Li Zefan6ee211a2013-03-12 15:36:00 -07003568static int pidlist_uniq(pid_t *list, int length)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003569{
Ben Blum102a7752009-09-23 15:56:26 -07003570 int src, dest = 1;
Ben Blum102a7752009-09-23 15:56:26 -07003571
3572 /*
3573 * we presume the 0th element is unique, so i starts at 1. trivial
3574 * edge cases first; no work needs to be done for either
3575 */
3576 if (length == 0 || length == 1)
3577 return length;
3578 /* src and dest walk down the list; dest counts unique elements */
3579 for (src = 1; src < length; src++) {
3580 /* find next unique element */
3581 while (list[src] == list[src-1]) {
3582 src++;
3583 if (src == length)
3584 goto after;
3585 }
3586 /* dest always points to where the next unique element goes */
3587 list[dest] = list[src];
3588 dest++;
3589 }
3590after:
Ben Blum102a7752009-09-23 15:56:26 -07003591 return dest;
3592}
3593
3594static int cmppid(const void *a, const void *b)
3595{
3596 return *(pid_t *)a - *(pid_t *)b;
3597}
3598
3599/*
Ben Blum72a8cb32009-09-23 15:56:27 -07003600 * find the appropriate pidlist for our purpose (given procs vs tasks)
3601 * returns with the lock on that pidlist already held, and takes care
3602 * of the use count, or returns NULL with no locks held if we're out of
3603 * memory.
3604 */
3605static struct cgroup_pidlist *cgroup_pidlist_find(struct cgroup *cgrp,
3606 enum cgroup_filetype type)
3607{
3608 struct cgroup_pidlist *l;
3609 /* don't need task_nsproxy() if we're looking at ourself */
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08003610 struct pid_namespace *ns = task_active_pid_ns(current);
Li Zefanb70cc5f2010-03-10 15:22:12 -08003611
Ben Blum72a8cb32009-09-23 15:56:27 -07003612 /*
Li Zefanb3958902013-08-01 09:52:15 +08003613 * We can't drop the pidlist_mutex before taking the l->rwsem in case
Ben Blum72a8cb32009-09-23 15:56:27 -07003614 * the last ref-holder is trying to remove l from the list at the same
3615 * time. Holding the pidlist_mutex precludes somebody taking whichever
3616 * list we find out from under us - compare release_pid_array().
3617 */
3618 mutex_lock(&cgrp->pidlist_mutex);
3619 list_for_each_entry(l, &cgrp->pidlists, links) {
3620 if (l->key.type == type && l->key.ns == ns) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003621 /* make sure l doesn't vanish out from under us */
Li Zefanb3958902013-08-01 09:52:15 +08003622 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003623 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003624 return l;
3625 }
3626 }
3627 /* entry not found; create a new one */
Tejun Heof4f4be22013-06-12 21:04:51 -07003628 l = kzalloc(sizeof(struct cgroup_pidlist), GFP_KERNEL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003629 if (!l) {
3630 mutex_unlock(&cgrp->pidlist_mutex);
Ben Blum72a8cb32009-09-23 15:56:27 -07003631 return l;
3632 }
Li Zefanb3958902013-08-01 09:52:15 +08003633 init_rwsem(&l->rwsem);
3634 down_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003635 l->key.type = type;
Li Zefanb70cc5f2010-03-10 15:22:12 -08003636 l->key.ns = get_pid_ns(ns);
Ben Blum72a8cb32009-09-23 15:56:27 -07003637 l->owner = cgrp;
3638 list_add(&l->links, &cgrp->pidlists);
3639 mutex_unlock(&cgrp->pidlist_mutex);
3640 return l;
3641}
3642
3643/*
Ben Blum102a7752009-09-23 15:56:26 -07003644 * Load a cgroup's pidarray with either procs' tgids or tasks' pids
3645 */
Ben Blum72a8cb32009-09-23 15:56:27 -07003646static int pidlist_array_load(struct cgroup *cgrp, enum cgroup_filetype type,
3647 struct cgroup_pidlist **lp)
Ben Blum102a7752009-09-23 15:56:26 -07003648{
3649 pid_t *array;
3650 int length;
3651 int pid, n = 0; /* used for populating the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003652 struct css_task_iter it;
Paul Menage817929e2007-10-18 23:39:36 -07003653 struct task_struct *tsk;
Ben Blum102a7752009-09-23 15:56:26 -07003654 struct cgroup_pidlist *l;
3655
3656 /*
3657 * If cgroup gets more users after we read count, we won't have
3658 * enough space - tough. This race is indistinguishable to the
3659 * caller from the case that the additional cgroup users didn't
3660 * show up until sometime later on.
3661 */
3662 length = cgroup_task_count(cgrp);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003663 array = pidlist_allocate(length);
Ben Blum102a7752009-09-23 15:56:26 -07003664 if (!array)
3665 return -ENOMEM;
3666 /* now, populate the array */
Tejun Heo72ec7022013-08-08 20:11:26 -04003667 css_task_iter_start(&cgrp->dummy_css, &it);
3668 while ((tsk = css_task_iter_next(&it))) {
Ben Blum102a7752009-09-23 15:56:26 -07003669 if (unlikely(n == length))
Paul Menage817929e2007-10-18 23:39:36 -07003670 break;
Ben Blum102a7752009-09-23 15:56:26 -07003671 /* get tgid or pid for procs or tasks file respectively */
Ben Blum72a8cb32009-09-23 15:56:27 -07003672 if (type == CGROUP_FILE_PROCS)
3673 pid = task_tgid_vnr(tsk);
3674 else
3675 pid = task_pid_vnr(tsk);
Ben Blum102a7752009-09-23 15:56:26 -07003676 if (pid > 0) /* make sure to only use valid results */
3677 array[n++] = pid;
Paul Menage817929e2007-10-18 23:39:36 -07003678 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003679 css_task_iter_end(&it);
Ben Blum102a7752009-09-23 15:56:26 -07003680 length = n;
3681 /* now sort & (if procs) strip out duplicates */
3682 sort(array, length, sizeof(pid_t), cmppid, NULL);
Ben Blum72a8cb32009-09-23 15:56:27 -07003683 if (type == CGROUP_FILE_PROCS)
Li Zefan6ee211a2013-03-12 15:36:00 -07003684 length = pidlist_uniq(array, length);
Ben Blum72a8cb32009-09-23 15:56:27 -07003685 l = cgroup_pidlist_find(cgrp, type);
3686 if (!l) {
Ben Blumd1d9fd32009-09-23 15:56:28 -07003687 pidlist_free(array);
Ben Blum72a8cb32009-09-23 15:56:27 -07003688 return -ENOMEM;
Ben Blum102a7752009-09-23 15:56:26 -07003689 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003690 /* store array, freeing old if necessary - lock already held */
Ben Blumd1d9fd32009-09-23 15:56:28 -07003691 pidlist_free(l->list);
Ben Blum102a7752009-09-23 15:56:26 -07003692 l->list = array;
3693 l->length = length;
3694 l->use_count++;
Li Zefanb3958902013-08-01 09:52:15 +08003695 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003696 *lp = l;
Ben Blum102a7752009-09-23 15:56:26 -07003697 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003698}
3699
Balbir Singh846c7bb2007-10-18 23:39:44 -07003700/**
Li Zefana043e3b2008-02-23 15:24:09 -08003701 * cgroupstats_build - build and fill cgroupstats
Balbir Singh846c7bb2007-10-18 23:39:44 -07003702 * @stats: cgroupstats to fill information into
3703 * @dentry: A dentry entry belonging to the cgroup for which stats have
3704 * been requested.
Li Zefana043e3b2008-02-23 15:24:09 -08003705 *
3706 * Build and fill cgroupstats so that taskstats can export it to user
3707 * space.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003708 */
3709int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry)
3710{
3711 int ret = -EINVAL;
Paul Menagebd89aab2007-10-18 23:40:44 -07003712 struct cgroup *cgrp;
Tejun Heo72ec7022013-08-08 20:11:26 -04003713 struct css_task_iter it;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003714 struct task_struct *tsk;
Li Zefan33d283b2008-11-19 15:36:48 -08003715
Balbir Singh846c7bb2007-10-18 23:39:44 -07003716 /*
Li Zefan33d283b2008-11-19 15:36:48 -08003717 * Validate dentry by checking the superblock operations,
3718 * and make sure it's a directory.
Balbir Singh846c7bb2007-10-18 23:39:44 -07003719 */
Li Zefan33d283b2008-11-19 15:36:48 -08003720 if (dentry->d_sb->s_op != &cgroup_ops ||
3721 !S_ISDIR(dentry->d_inode->i_mode))
Balbir Singh846c7bb2007-10-18 23:39:44 -07003722 goto err;
3723
3724 ret = 0;
Paul Menagebd89aab2007-10-18 23:40:44 -07003725 cgrp = dentry->d_fsdata;
Balbir Singh846c7bb2007-10-18 23:39:44 -07003726
Tejun Heo72ec7022013-08-08 20:11:26 -04003727 css_task_iter_start(&cgrp->dummy_css, &it);
3728 while ((tsk = css_task_iter_next(&it))) {
Balbir Singh846c7bb2007-10-18 23:39:44 -07003729 switch (tsk->state) {
3730 case TASK_RUNNING:
3731 stats->nr_running++;
3732 break;
3733 case TASK_INTERRUPTIBLE:
3734 stats->nr_sleeping++;
3735 break;
3736 case TASK_UNINTERRUPTIBLE:
3737 stats->nr_uninterruptible++;
3738 break;
3739 case TASK_STOPPED:
3740 stats->nr_stopped++;
3741 break;
3742 default:
3743 if (delayacct_is_task_waiting_on_io(tsk))
3744 stats->nr_io_wait++;
3745 break;
3746 }
3747 }
Tejun Heo72ec7022013-08-08 20:11:26 -04003748 css_task_iter_end(&it);
Balbir Singh846c7bb2007-10-18 23:39:44 -07003749
Balbir Singh846c7bb2007-10-18 23:39:44 -07003750err:
3751 return ret;
3752}
3753
Paul Menage8f3ff202009-09-23 15:56:25 -07003754
Paul Menagecc31edc2008-10-18 20:28:04 -07003755/*
Ben Blum102a7752009-09-23 15:56:26 -07003756 * seq_file methods for the tasks/procs files. The seq_file position is the
Paul Menagecc31edc2008-10-18 20:28:04 -07003757 * next pid to display; the seq_file iterator is a pointer to the pid
Ben Blum102a7752009-09-23 15:56:26 -07003758 * in the cgroup->l->list array.
Paul Menagecc31edc2008-10-18 20:28:04 -07003759 */
3760
Ben Blum102a7752009-09-23 15:56:26 -07003761static void *cgroup_pidlist_start(struct seq_file *s, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003762{
3763 /*
3764 * Initially we receive a position value that corresponds to
3765 * one more than the last pid shown (or 0 on the first call or
3766 * after a seek to the start). Use a binary-search to find the
3767 * next pid to display, if any
3768 */
Ben Blum102a7752009-09-23 15:56:26 -07003769 struct cgroup_pidlist *l = s->private;
Paul Menagecc31edc2008-10-18 20:28:04 -07003770 int index = 0, pid = *pos;
3771 int *iter;
3772
Li Zefanb3958902013-08-01 09:52:15 +08003773 down_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003774 if (pid) {
Ben Blum102a7752009-09-23 15:56:26 -07003775 int end = l->length;
Stephen Rothwell20777762008-10-21 16:11:20 +11003776
Paul Menagecc31edc2008-10-18 20:28:04 -07003777 while (index < end) {
3778 int mid = (index + end) / 2;
Ben Blum102a7752009-09-23 15:56:26 -07003779 if (l->list[mid] == pid) {
Paul Menagecc31edc2008-10-18 20:28:04 -07003780 index = mid;
3781 break;
Ben Blum102a7752009-09-23 15:56:26 -07003782 } else if (l->list[mid] <= pid)
Paul Menagecc31edc2008-10-18 20:28:04 -07003783 index = mid + 1;
3784 else
3785 end = mid;
3786 }
3787 }
3788 /* If we're off the end of the array, we're done */
Ben Blum102a7752009-09-23 15:56:26 -07003789 if (index >= l->length)
Paul Menagecc31edc2008-10-18 20:28:04 -07003790 return NULL;
3791 /* Update the abstract position to be the actual pid that we found */
Ben Blum102a7752009-09-23 15:56:26 -07003792 iter = l->list + index;
Paul Menagecc31edc2008-10-18 20:28:04 -07003793 *pos = *iter;
3794 return iter;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003795}
3796
Ben Blum102a7752009-09-23 15:56:26 -07003797static void cgroup_pidlist_stop(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003798{
Ben Blum102a7752009-09-23 15:56:26 -07003799 struct cgroup_pidlist *l = s->private;
Li Zefanb3958902013-08-01 09:52:15 +08003800 up_read(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003801}
3802
Ben Blum102a7752009-09-23 15:56:26 -07003803static void *cgroup_pidlist_next(struct seq_file *s, void *v, loff_t *pos)
Paul Menagecc31edc2008-10-18 20:28:04 -07003804{
Ben Blum102a7752009-09-23 15:56:26 -07003805 struct cgroup_pidlist *l = s->private;
3806 pid_t *p = v;
3807 pid_t *end = l->list + l->length;
Paul Menagecc31edc2008-10-18 20:28:04 -07003808 /*
3809 * Advance to the next pid in the array. If this goes off the
3810 * end, we're done
3811 */
3812 p++;
3813 if (p >= end) {
3814 return NULL;
3815 } else {
3816 *pos = *p;
3817 return p;
3818 }
3819}
3820
Ben Blum102a7752009-09-23 15:56:26 -07003821static int cgroup_pidlist_show(struct seq_file *s, void *v)
Paul Menagecc31edc2008-10-18 20:28:04 -07003822{
3823 return seq_printf(s, "%d\n", *(int *)v);
3824}
3825
Ben Blum102a7752009-09-23 15:56:26 -07003826/*
3827 * seq_operations functions for iterating on pidlists through seq_file -
3828 * independent of whether it's tasks or procs
3829 */
3830static const struct seq_operations cgroup_pidlist_seq_operations = {
3831 .start = cgroup_pidlist_start,
3832 .stop = cgroup_pidlist_stop,
3833 .next = cgroup_pidlist_next,
3834 .show = cgroup_pidlist_show,
Paul Menagecc31edc2008-10-18 20:28:04 -07003835};
3836
Ben Blum102a7752009-09-23 15:56:26 -07003837static void cgroup_release_pid_array(struct cgroup_pidlist *l)
Paul Menagecc31edc2008-10-18 20:28:04 -07003838{
Ben Blum72a8cb32009-09-23 15:56:27 -07003839 /*
3840 * the case where we're the last user of this particular pidlist will
3841 * have us remove it from the cgroup's list, which entails taking the
3842 * mutex. since in pidlist_find the pidlist->lock depends on cgroup->
3843 * pidlist_mutex, we have to take pidlist_mutex first.
3844 */
3845 mutex_lock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003846 down_write(&l->rwsem);
Ben Blum102a7752009-09-23 15:56:26 -07003847 BUG_ON(!l->use_count);
3848 if (!--l->use_count) {
Ben Blum72a8cb32009-09-23 15:56:27 -07003849 /* we're the last user if refcount is 0; remove and free */
3850 list_del(&l->links);
3851 mutex_unlock(&l->owner->pidlist_mutex);
Ben Blumd1d9fd32009-09-23 15:56:28 -07003852 pidlist_free(l->list);
Ben Blum72a8cb32009-09-23 15:56:27 -07003853 put_pid_ns(l->key.ns);
Li Zefanb3958902013-08-01 09:52:15 +08003854 up_write(&l->rwsem);
Ben Blum72a8cb32009-09-23 15:56:27 -07003855 kfree(l);
3856 return;
Paul Menagecc31edc2008-10-18 20:28:04 -07003857 }
Ben Blum72a8cb32009-09-23 15:56:27 -07003858 mutex_unlock(&l->owner->pidlist_mutex);
Li Zefanb3958902013-08-01 09:52:15 +08003859 up_write(&l->rwsem);
Paul Menagecc31edc2008-10-18 20:28:04 -07003860}
3861
Ben Blum102a7752009-09-23 15:56:26 -07003862static int cgroup_pidlist_release(struct inode *inode, struct file *file)
Paul Menagebbcb81d2007-10-18 23:39:32 -07003863{
Ben Blum102a7752009-09-23 15:56:26 -07003864 struct cgroup_pidlist *l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003865 if (!(file->f_mode & FMODE_READ))
3866 return 0;
Ben Blum102a7752009-09-23 15:56:26 -07003867 /*
3868 * the seq_file will only be initialized if the file was opened for
3869 * reading; hence we check if it's not null only in that case.
3870 */
3871 l = ((struct seq_file *)file->private_data)->private;
3872 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003873 return seq_release(inode, file);
3874}
3875
Ben Blum102a7752009-09-23 15:56:26 -07003876static const struct file_operations cgroup_pidlist_operations = {
Paul Menagecc31edc2008-10-18 20:28:04 -07003877 .read = seq_read,
3878 .llseek = seq_lseek,
3879 .write = cgroup_file_write,
Ben Blum102a7752009-09-23 15:56:26 -07003880 .release = cgroup_pidlist_release,
Paul Menagecc31edc2008-10-18 20:28:04 -07003881};
3882
3883/*
Ben Blum102a7752009-09-23 15:56:26 -07003884 * The following functions handle opens on a file that displays a pidlist
3885 * (tasks or procs). Prepare an array of the process/thread IDs of whoever's
3886 * in the cgroup.
Paul Menagecc31edc2008-10-18 20:28:04 -07003887 */
Ben Blum102a7752009-09-23 15:56:26 -07003888/* helper function for the two below it */
Ben Blum72a8cb32009-09-23 15:56:27 -07003889static int cgroup_pidlist_open(struct file *file, enum cgroup_filetype type)
Paul Menagecc31edc2008-10-18 20:28:04 -07003890{
3891 struct cgroup *cgrp = __d_cgrp(file->f_dentry->d_parent);
Ben Blum72a8cb32009-09-23 15:56:27 -07003892 struct cgroup_pidlist *l;
Paul Menagecc31edc2008-10-18 20:28:04 -07003893 int retval;
3894
3895 /* Nothing to do for write-only files */
3896 if (!(file->f_mode & FMODE_READ))
3897 return 0;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003898
Ben Blum102a7752009-09-23 15:56:26 -07003899 /* have the array populated */
Ben Blum72a8cb32009-09-23 15:56:27 -07003900 retval = pidlist_array_load(cgrp, type, &l);
Ben Blum102a7752009-09-23 15:56:26 -07003901 if (retval)
3902 return retval;
3903 /* configure file information */
3904 file->f_op = &cgroup_pidlist_operations;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003905
Ben Blum102a7752009-09-23 15:56:26 -07003906 retval = seq_open(file, &cgroup_pidlist_seq_operations);
Paul Menagecc31edc2008-10-18 20:28:04 -07003907 if (retval) {
Ben Blum102a7752009-09-23 15:56:26 -07003908 cgroup_release_pid_array(l);
Paul Menagecc31edc2008-10-18 20:28:04 -07003909 return retval;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003910 }
Ben Blum102a7752009-09-23 15:56:26 -07003911 ((struct seq_file *)file->private_data)->private = l;
Paul Menagebbcb81d2007-10-18 23:39:32 -07003912 return 0;
3913}
Ben Blum102a7752009-09-23 15:56:26 -07003914static int cgroup_tasks_open(struct inode *unused, struct file *file)
3915{
Ben Blum72a8cb32009-09-23 15:56:27 -07003916 return cgroup_pidlist_open(file, CGROUP_FILE_TASKS);
Ben Blum102a7752009-09-23 15:56:26 -07003917}
3918static int cgroup_procs_open(struct inode *unused, struct file *file)
3919{
Ben Blum72a8cb32009-09-23 15:56:27 -07003920 return cgroup_pidlist_open(file, CGROUP_FILE_PROCS);
Ben Blum102a7752009-09-23 15:56:26 -07003921}
Paul Menagebbcb81d2007-10-18 23:39:32 -07003922
Tejun Heo182446d2013-08-08 20:11:24 -04003923static u64 cgroup_read_notify_on_release(struct cgroup_subsys_state *css,
3924 struct cftype *cft)
Paul Menage81a6a5c2007-10-18 23:39:38 -07003925{
Tejun Heo182446d2013-08-08 20:11:24 -04003926 return notify_on_release(css->cgroup);
Paul Menage81a6a5c2007-10-18 23:39:38 -07003927}
3928
Tejun Heo182446d2013-08-08 20:11:24 -04003929static int cgroup_write_notify_on_release(struct cgroup_subsys_state *css,
3930 struct cftype *cft, u64 val)
Paul Menage6379c102008-07-25 01:47:01 -07003931{
Tejun Heo182446d2013-08-08 20:11:24 -04003932 clear_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003933 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04003934 set_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003935 else
Tejun Heo182446d2013-08-08 20:11:24 -04003936 clear_bit(CGRP_NOTIFY_ON_RELEASE, &css->cgroup->flags);
Paul Menage6379c102008-07-25 01:47:01 -07003937 return 0;
3938}
3939
Paul Menagebbcb81d2007-10-18 23:39:32 -07003940/*
Li Zefan1c8158e2013-06-18 18:41:10 +08003941 * When dput() is called asynchronously, if umount has been done and
3942 * then deactivate_super() in cgroup_free_fn() kills the superblock,
3943 * there's a small window that vfs will see the root dentry with non-zero
3944 * refcnt and trigger BUG().
3945 *
3946 * That's why we hold a reference before dput() and drop it right after.
3947 */
3948static void cgroup_dput(struct cgroup *cgrp)
3949{
3950 struct super_block *sb = cgrp->root->sb;
3951
3952 atomic_inc(&sb->s_active);
3953 dput(cgrp->dentry);
3954 deactivate_super(sb);
3955}
3956
3957/*
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003958 * Unregister event and free resources.
3959 *
3960 * Gets called from workqueue.
3961 */
3962static void cgroup_event_remove(struct work_struct *work)
3963{
3964 struct cgroup_event *event = container_of(work, struct cgroup_event,
3965 remove);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003966 struct cgroup_subsys_state *css = event->css;
3967 struct cgroup *cgrp = css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003968
Li Zefan810cbee2013-02-18 18:56:14 +08003969 remove_wait_queue(event->wqh, &event->wait);
3970
Tejun Heo81eeaf02013-08-08 20:11:26 -04003971 event->cft->unregister_event(css, event->cft, event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003972
Li Zefan810cbee2013-02-18 18:56:14 +08003973 /* Notify userspace the event is going away. */
3974 eventfd_signal(event->eventfd, 1);
3975
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003976 eventfd_ctx_put(event->eventfd);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003977 kfree(event);
Li Zefan1c8158e2013-06-18 18:41:10 +08003978 cgroup_dput(cgrp);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003979}
3980
3981/*
3982 * Gets called on POLLHUP on eventfd when user closes it.
3983 *
3984 * Called with wqh->lock held and interrupts disabled.
3985 */
3986static int cgroup_event_wake(wait_queue_t *wait, unsigned mode,
3987 int sync, void *key)
3988{
3989 struct cgroup_event *event = container_of(wait,
3990 struct cgroup_event, wait);
Tejun Heo81eeaf02013-08-08 20:11:26 -04003991 struct cgroup *cgrp = event->css->cgroup;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003992 unsigned long flags = (unsigned long)key;
3993
3994 if (flags & POLLHUP) {
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08003995 /*
Li Zefan810cbee2013-02-18 18:56:14 +08003996 * If the event has been detached at cgroup removal, we
3997 * can simply return knowing the other side will cleanup
3998 * for us.
3999 *
4000 * We can't race against event freeing since the other
4001 * side will require wqh->lock via remove_wait_queue(),
4002 * which we hold.
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004003 */
Li Zefan810cbee2013-02-18 18:56:14 +08004004 spin_lock(&cgrp->event_list_lock);
4005 if (!list_empty(&event->list)) {
4006 list_del_init(&event->list);
4007 /*
4008 * We are in atomic context, but cgroup_event_remove()
4009 * may sleep, so we have to call it in workqueue.
4010 */
4011 schedule_work(&event->remove);
4012 }
4013 spin_unlock(&cgrp->event_list_lock);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004014 }
4015
4016 return 0;
4017}
4018
4019static void cgroup_event_ptable_queue_proc(struct file *file,
4020 wait_queue_head_t *wqh, poll_table *pt)
4021{
4022 struct cgroup_event *event = container_of(pt,
4023 struct cgroup_event, pt);
4024
4025 event->wqh = wqh;
4026 add_wait_queue(wqh, &event->wait);
4027}
4028
4029/*
4030 * Parse input and register new cgroup event handler.
4031 *
4032 * Input must be in format '<event_fd> <control_fd> <args>'.
4033 * Interpretation of args is defined by control file implementation.
4034 */
Tejun Heo182446d2013-08-08 20:11:24 -04004035static int cgroup_write_event_control(struct cgroup_subsys_state *css,
4036 struct cftype *cft, const char *buffer)
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004037{
Tejun Heo182446d2013-08-08 20:11:24 -04004038 struct cgroup *cgrp = css->cgroup;
Li Zefan876ede82013-08-01 09:51:47 +08004039 struct cgroup_event *event;
Li Zefanf1690072013-02-18 14:13:35 +08004040 struct cgroup *cgrp_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004041 unsigned int efd, cfd;
Li Zefan876ede82013-08-01 09:51:47 +08004042 struct file *efile;
4043 struct file *cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004044 char *endp;
4045 int ret;
4046
4047 efd = simple_strtoul(buffer, &endp, 10);
4048 if (*endp != ' ')
4049 return -EINVAL;
4050 buffer = endp + 1;
4051
4052 cfd = simple_strtoul(buffer, &endp, 10);
4053 if ((*endp != ' ') && (*endp != '\0'))
4054 return -EINVAL;
4055 buffer = endp + 1;
4056
4057 event = kzalloc(sizeof(*event), GFP_KERNEL);
4058 if (!event)
4059 return -ENOMEM;
Tejun Heo81eeaf02013-08-08 20:11:26 -04004060 event->css = css;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004061 INIT_LIST_HEAD(&event->list);
4062 init_poll_funcptr(&event->pt, cgroup_event_ptable_queue_proc);
4063 init_waitqueue_func_entry(&event->wait, cgroup_event_wake);
4064 INIT_WORK(&event->remove, cgroup_event_remove);
4065
4066 efile = eventfd_fget(efd);
4067 if (IS_ERR(efile)) {
4068 ret = PTR_ERR(efile);
Li Zefan876ede82013-08-01 09:51:47 +08004069 goto out_kfree;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004070 }
4071
4072 event->eventfd = eventfd_ctx_fileget(efile);
4073 if (IS_ERR(event->eventfd)) {
4074 ret = PTR_ERR(event->eventfd);
Li Zefan876ede82013-08-01 09:51:47 +08004075 goto out_put_efile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004076 }
4077
4078 cfile = fget(cfd);
4079 if (!cfile) {
4080 ret = -EBADF;
Li Zefan876ede82013-08-01 09:51:47 +08004081 goto out_put_eventfd;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004082 }
4083
4084 /* the process need read permission on control file */
Al Viro3bfa7842011-06-19 12:55:10 -04004085 /* AV: shouldn't we check that it's been opened for read instead? */
Al Viro496ad9a2013-01-23 17:07:38 -05004086 ret = inode_permission(file_inode(cfile), MAY_READ);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004087 if (ret < 0)
Li Zefan876ede82013-08-01 09:51:47 +08004088 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004089
4090 event->cft = __file_cft(cfile);
4091 if (IS_ERR(event->cft)) {
4092 ret = PTR_ERR(event->cft);
Li Zefan876ede82013-08-01 09:51:47 +08004093 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004094 }
4095
Li Zefanf1690072013-02-18 14:13:35 +08004096 /*
4097 * The file to be monitored must be in the same cgroup as
4098 * cgroup.event_control is.
4099 */
4100 cgrp_cfile = __d_cgrp(cfile->f_dentry->d_parent);
4101 if (cgrp_cfile != cgrp) {
4102 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004103 goto out_put_cfile;
Li Zefanf1690072013-02-18 14:13:35 +08004104 }
4105
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004106 if (!event->cft->register_event || !event->cft->unregister_event) {
4107 ret = -EINVAL;
Li Zefan876ede82013-08-01 09:51:47 +08004108 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004109 }
4110
Tejun Heo81eeaf02013-08-08 20:11:26 -04004111 ret = event->cft->register_event(css, event->cft,
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004112 event->eventfd, buffer);
4113 if (ret)
Li Zefan876ede82013-08-01 09:51:47 +08004114 goto out_put_cfile;
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004115
Li Zefan7ef70e42013-04-26 11:58:03 -07004116 efile->f_op->poll(efile, &event->pt);
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004117
Kirill A. Shutemova0a4db52010-03-10 15:22:34 -08004118 /*
4119 * Events should be removed after rmdir of cgroup directory, but before
4120 * destroying subsystem state objects. Let's take reference to cgroup
4121 * directory dentry to do that.
4122 */
4123 dget(cgrp->dentry);
4124
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004125 spin_lock(&cgrp->event_list_lock);
4126 list_add(&event->list, &cgrp->event_list);
4127 spin_unlock(&cgrp->event_list_lock);
4128
4129 fput(cfile);
4130 fput(efile);
4131
4132 return 0;
4133
Li Zefan876ede82013-08-01 09:51:47 +08004134out_put_cfile:
4135 fput(cfile);
4136out_put_eventfd:
4137 eventfd_ctx_put(event->eventfd);
4138out_put_efile:
4139 fput(efile);
4140out_kfree:
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004141 kfree(event);
4142
4143 return ret;
4144}
4145
Tejun Heo182446d2013-08-08 20:11:24 -04004146static u64 cgroup_clone_children_read(struct cgroup_subsys_state *css,
4147 struct cftype *cft)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004148{
Tejun Heo182446d2013-08-08 20:11:24 -04004149 return test_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004150}
4151
Tejun Heo182446d2013-08-08 20:11:24 -04004152static int cgroup_clone_children_write(struct cgroup_subsys_state *css,
4153 struct cftype *cft, u64 val)
Daniel Lezcano97978e62010-10-27 15:33:35 -07004154{
4155 if (val)
Tejun Heo182446d2013-08-08 20:11:24 -04004156 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004157 else
Tejun Heo182446d2013-08-08 20:11:24 -04004158 clear_bit(CGRP_CPUSET_CLONE_CHILDREN, &css->cgroup->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004159 return 0;
4160}
4161
Tejun Heod5c56ce2013-06-03 19:14:34 -07004162static struct cftype cgroup_base_files[] = {
Paul Menage81a6a5c2007-10-18 23:39:38 -07004163 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004164 .name = "cgroup.procs",
Ben Blum102a7752009-09-23 15:56:26 -07004165 .open = cgroup_procs_open,
Ben Blum74a11662011-05-26 16:25:20 -07004166 .write_u64 = cgroup_procs_write,
Ben Blum102a7752009-09-23 15:56:26 -07004167 .release = cgroup_pidlist_release,
Ben Blum74a11662011-05-26 16:25:20 -07004168 .mode = S_IRUGO | S_IWUSR,
Ben Blum102a7752009-09-23 15:56:26 -07004169 },
Paul Menage81a6a5c2007-10-18 23:39:38 -07004170 {
Tejun Heod5c56ce2013-06-03 19:14:34 -07004171 .name = "cgroup.event_control",
Kirill A. Shutemov0dea1162010-03-10 15:22:20 -08004172 .write_string = cgroup_write_event_control,
4173 .mode = S_IWUGO,
4174 },
Daniel Lezcano97978e62010-10-27 15:33:35 -07004175 {
4176 .name = "cgroup.clone_children",
Tejun Heo873fe092013-04-14 20:15:26 -07004177 .flags = CFTYPE_INSANE,
Daniel Lezcano97978e62010-10-27 15:33:35 -07004178 .read_u64 = cgroup_clone_children_read,
4179 .write_u64 = cgroup_clone_children_write,
4180 },
Tejun Heo6e6ff252012-04-01 12:09:55 -07004181 {
Tejun Heo873fe092013-04-14 20:15:26 -07004182 .name = "cgroup.sane_behavior",
4183 .flags = CFTYPE_ONLY_ON_ROOT,
4184 .read_seq_string = cgroup_sane_behavior_show,
4185 },
Tejun Heod5c56ce2013-06-03 19:14:34 -07004186
4187 /*
4188 * Historical crazy stuff. These don't have "cgroup." prefix and
4189 * don't exist if sane_behavior. If you're depending on these, be
4190 * prepared to be burned.
4191 */
4192 {
4193 .name = "tasks",
4194 .flags = CFTYPE_INSANE, /* use "procs" instead */
4195 .open = cgroup_tasks_open,
4196 .write_u64 = cgroup_tasks_write,
4197 .release = cgroup_pidlist_release,
4198 .mode = S_IRUGO | S_IWUSR,
4199 },
4200 {
4201 .name = "notify_on_release",
4202 .flags = CFTYPE_INSANE,
4203 .read_u64 = cgroup_read_notify_on_release,
4204 .write_u64 = cgroup_write_notify_on_release,
4205 },
Tejun Heo873fe092013-04-14 20:15:26 -07004206 {
Tejun Heo6e6ff252012-04-01 12:09:55 -07004207 .name = "release_agent",
Tejun Heocc5943a2013-06-03 19:13:55 -07004208 .flags = CFTYPE_INSANE | CFTYPE_ONLY_ON_ROOT,
Tejun Heo6e6ff252012-04-01 12:09:55 -07004209 .read_seq_string = cgroup_release_agent_show,
4210 .write_string = cgroup_release_agent_write,
4211 .max_write_len = PATH_MAX,
4212 },
Tejun Heodb0416b2012-04-01 12:09:55 -07004213 { } /* terminate */
Paul Menagebbcb81d2007-10-18 23:39:32 -07004214};
4215
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004216/**
Tejun Heo628f7cd2013-06-28 16:24:11 -07004217 * cgroup_populate_dir - create subsys files in a cgroup directory
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004218 * @cgrp: target cgroup
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004219 * @subsys_mask: mask of the subsystem ids whose files should be added
Tejun Heobee55092013-06-28 16:24:11 -07004220 *
4221 * On failure, no file is added.
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004222 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004223static int cgroup_populate_dir(struct cgroup *cgrp, unsigned long subsys_mask)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004224{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004225 struct cgroup_subsys *ss;
Tejun Heob420ba72013-07-12 12:34:02 -07004226 int i, ret = 0;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004227
Tejun Heo8e3f6542012-04-01 12:09:55 -07004228 /* process cftsets of each subsystem */
Tejun Heob420ba72013-07-12 12:34:02 -07004229 for_each_subsys(ss, i) {
Tejun Heo8e3f6542012-04-01 12:09:55 -07004230 struct cftype_set *set;
Tejun Heob420ba72013-07-12 12:34:02 -07004231
4232 if (!test_bit(i, &subsys_mask))
Aristeu Rozanski13af07d2012-08-23 16:53:29 -04004233 continue;
Tejun Heo8e3f6542012-04-01 12:09:55 -07004234
Tejun Heobee55092013-06-28 16:24:11 -07004235 list_for_each_entry(set, &ss->cftsets, node) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004236 ret = cgroup_addrm_files(cgrp, set->cfts, true);
Tejun Heobee55092013-06-28 16:24:11 -07004237 if (ret < 0)
4238 goto err;
4239 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004240 }
Tejun Heo8e3f6542012-04-01 12:09:55 -07004241
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004242 /* This cgroup is ready now */
Tejun Heo5549c492013-06-24 15:21:48 -07004243 for_each_root_subsys(cgrp->root, ss) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004244 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004245 struct css_id *id = rcu_dereference_protected(css->id, true);
4246
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004247 /*
4248 * Update id->css pointer and make this css visible from
4249 * CSS ID functions. This pointer will be dereferened
4250 * from RCU-read-side without locks.
4251 */
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004252 if (id)
4253 rcu_assign_pointer(id->css, css);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004254 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004255
4256 return 0;
Tejun Heobee55092013-06-28 16:24:11 -07004257err:
4258 cgroup_clear_dir(cgrp, subsys_mask);
4259 return ret;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004260}
4261
Tejun Heo48ddbe12012-04-01 12:09:56 -07004262static void css_dput_fn(struct work_struct *work)
4263{
4264 struct cgroup_subsys_state *css =
4265 container_of(work, struct cgroup_subsys_state, dput_work);
4266
Li Zefan1c8158e2013-06-18 18:41:10 +08004267 cgroup_dput(css->cgroup);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004268}
4269
Tejun Heod3daf282013-06-13 19:39:16 -07004270static void css_release(struct percpu_ref *ref)
4271{
4272 struct cgroup_subsys_state *css =
4273 container_of(ref, struct cgroup_subsys_state, refcnt);
4274
4275 schedule_work(&css->dput_work);
4276}
4277
Paul Menageddbcc7e2007-10-18 23:39:30 -07004278static void init_cgroup_css(struct cgroup_subsys_state *css,
4279 struct cgroup_subsys *ss,
Paul Menagebd89aab2007-10-18 23:40:44 -07004280 struct cgroup *cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004281{
Paul Menagebd89aab2007-10-18 23:40:44 -07004282 css->cgroup = cgrp;
Tejun Heo72c97e52013-08-08 20:11:22 -04004283 css->ss = ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004284 css->flags = 0;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07004285 css->id = NULL;
Tejun Heo9871bf92013-06-24 15:21:47 -07004286 if (cgrp == cgroup_dummy_top)
Tejun Heo38b53ab2012-11-19 08:13:36 -08004287 css->flags |= CSS_ROOT;
Paul Menagebd89aab2007-10-18 23:40:44 -07004288 BUG_ON(cgrp->subsys[ss->subsys_id]);
4289 cgrp->subsys[ss->subsys_id] = css;
Tejun Heo48ddbe12012-04-01 12:09:56 -07004290
4291 /*
Tejun Heoed9577932012-11-05 09:16:58 -08004292 * css holds an extra ref to @cgrp->dentry which is put on the last
4293 * css_put(). dput() requires process context, which css_put() may
4294 * be called without. @css->dput_work will be used to invoke
4295 * dput() asynchronously from css_put().
Tejun Heo48ddbe12012-04-01 12:09:56 -07004296 */
4297 INIT_WORK(&css->dput_work, css_dput_fn);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004298}
4299
Li Zefan2a4ac632013-07-31 16:16:40 +08004300/* invoke ->css_online() on a new CSS and mark it online if successful */
Tejun Heob1929db2012-11-19 08:13:38 -08004301static int online_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004302{
Tejun Heoeb954192013-08-08 20:11:23 -04004303 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
Tejun Heob1929db2012-11-19 08:13:38 -08004304 int ret = 0;
4305
Tejun Heoa31f2d32012-11-19 08:13:37 -08004306 lockdep_assert_held(&cgroup_mutex);
4307
Tejun Heo92fb9742012-11-19 08:13:38 -08004308 if (ss->css_online)
Tejun Heoeb954192013-08-08 20:11:23 -04004309 ret = ss->css_online(css);
Tejun Heob1929db2012-11-19 08:13:38 -08004310 if (!ret)
Tejun Heoeb954192013-08-08 20:11:23 -04004311 css->flags |= CSS_ONLINE;
Tejun Heob1929db2012-11-19 08:13:38 -08004312 return ret;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004313}
4314
Li Zefan2a4ac632013-07-31 16:16:40 +08004315/* if the CSS is online, invoke ->css_offline() on it and mark it offline */
Tejun Heoa31f2d32012-11-19 08:13:37 -08004316static void offline_css(struct cgroup_subsys *ss, struct cgroup *cgrp)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004317{
4318 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4319
4320 lockdep_assert_held(&cgroup_mutex);
4321
4322 if (!(css->flags & CSS_ONLINE))
4323 return;
4324
Li Zefand7eeac12013-03-12 15:35:59 -07004325 if (ss->css_offline)
Tejun Heoeb954192013-08-08 20:11:23 -04004326 ss->css_offline(css);
Tejun Heoa31f2d32012-11-19 08:13:37 -08004327
Tejun Heoeb954192013-08-08 20:11:23 -04004328 css->flags &= ~CSS_ONLINE;
Tejun Heoa31f2d32012-11-19 08:13:37 -08004329}
4330
Paul Menageddbcc7e2007-10-18 23:39:30 -07004331/*
Li Zefana043e3b2008-02-23 15:24:09 -08004332 * cgroup_create - create a cgroup
4333 * @parent: cgroup that will be parent of the new cgroup
4334 * @dentry: dentry of the new cgroup
4335 * @mode: mode to set on new inode
Paul Menageddbcc7e2007-10-18 23:39:30 -07004336 *
Li Zefana043e3b2008-02-23 15:24:09 -08004337 * Must be called with the mutex on the parent inode held
Paul Menageddbcc7e2007-10-18 23:39:30 -07004338 */
Paul Menageddbcc7e2007-10-18 23:39:30 -07004339static long cgroup_create(struct cgroup *parent, struct dentry *dentry,
Al Viroa5e7ed32011-07-26 01:55:55 -04004340 umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004341{
Paul Menagebd89aab2007-10-18 23:40:44 -07004342 struct cgroup *cgrp;
Li Zefan65dff752013-03-01 15:01:56 +08004343 struct cgroup_name *name;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004344 struct cgroupfs_root *root = parent->root;
4345 int err = 0;
4346 struct cgroup_subsys *ss;
4347 struct super_block *sb = root->sb;
4348
Tejun Heo0a950f62012-11-19 09:02:12 -08004349 /* allocate the cgroup and its ID, 0 is reserved for the root */
Paul Menagebd89aab2007-10-18 23:40:44 -07004350 cgrp = kzalloc(sizeof(*cgrp), GFP_KERNEL);
4351 if (!cgrp)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004352 return -ENOMEM;
4353
Li Zefan65dff752013-03-01 15:01:56 +08004354 name = cgroup_alloc_name(dentry);
4355 if (!name)
4356 goto err_free_cgrp;
4357 rcu_assign_pointer(cgrp->name, name);
4358
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004359 /*
4360 * Temporarily set the pointer to NULL, so idr_find() won't return
4361 * a half-baked cgroup.
4362 */
4363 cgrp->id = idr_alloc(&root->cgroup_idr, NULL, 1, 0, GFP_KERNEL);
Tejun Heo0a950f62012-11-19 09:02:12 -08004364 if (cgrp->id < 0)
Li Zefan65dff752013-03-01 15:01:56 +08004365 goto err_free_name;
Tejun Heo0a950f62012-11-19 09:02:12 -08004366
Tejun Heo976c06b2012-11-05 09:16:59 -08004367 /*
4368 * Only live parents can have children. Note that the liveliness
4369 * check isn't strictly necessary because cgroup_mkdir() and
4370 * cgroup_rmdir() are fully synchronized by i_mutex; however, do it
4371 * anyway so that locking is contained inside cgroup proper and we
4372 * don't get nasty surprises if we ever grow another caller.
4373 */
4374 if (!cgroup_lock_live_group(parent)) {
4375 err = -ENODEV;
Tejun Heo0a950f62012-11-19 09:02:12 -08004376 goto err_free_id;
Tejun Heo976c06b2012-11-05 09:16:59 -08004377 }
4378
Paul Menageddbcc7e2007-10-18 23:39:30 -07004379 /* Grab a reference on the superblock so the hierarchy doesn't
4380 * get deleted on unmount if there are child cgroups. This
4381 * can be done outside cgroup_mutex, since the sb can't
4382 * disappear while someone has an open control file on the
4383 * fs */
4384 atomic_inc(&sb->s_active);
4385
Paul Menagecc31edc2008-10-18 20:28:04 -07004386 init_cgroup_housekeeping(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004387
Li Zefanfe1c06c2013-01-24 14:30:22 +08004388 dentry->d_fsdata = cgrp;
4389 cgrp->dentry = dentry;
4390
Paul Menagebd89aab2007-10-18 23:40:44 -07004391 cgrp->parent = parent;
4392 cgrp->root = parent->root;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004393
Li Zefanb6abdb02008-03-04 14:28:19 -08004394 if (notify_on_release(parent))
4395 set_bit(CGRP_NOTIFY_ON_RELEASE, &cgrp->flags);
4396
Tejun Heo2260e7f2012-11-19 08:13:38 -08004397 if (test_bit(CGRP_CPUSET_CLONE_CHILDREN, &parent->flags))
4398 set_bit(CGRP_CPUSET_CLONE_CHILDREN, &cgrp->flags);
Daniel Lezcano97978e62010-10-27 15:33:35 -07004399
Tejun Heo5549c492013-06-24 15:21:48 -07004400 for_each_root_subsys(root, ss) {
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07004401 struct cgroup_subsys_state *css;
Li Zefan4528fd02010-02-02 13:44:10 -08004402
Tejun Heoeb954192013-08-08 20:11:23 -04004403 css = ss->css_alloc(parent->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004404 if (IS_ERR(css)) {
4405 err = PTR_ERR(css);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004406 goto err_free_all;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004407 }
Tejun Heod3daf282013-06-13 19:39:16 -07004408
4409 err = percpu_ref_init(&css->refcnt, css_release);
Li Zefanda0a12c2013-07-31 16:16:28 +08004410 if (err) {
Tejun Heoeb954192013-08-08 20:11:23 -04004411 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004412 goto err_free_all;
Li Zefanda0a12c2013-07-31 16:16:28 +08004413 }
Tejun Heod3daf282013-06-13 19:39:16 -07004414
Paul Menagebd89aab2007-10-18 23:40:44 -07004415 init_cgroup_css(css, ss, cgrp);
Tejun Heod3daf282013-06-13 19:39:16 -07004416
Li Zefan4528fd02010-02-02 13:44:10 -08004417 if (ss->use_id) {
4418 err = alloc_css_id(ss, parent, cgrp);
4419 if (err)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004420 goto err_free_all;
Li Zefan4528fd02010-02-02 13:44:10 -08004421 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004422 }
4423
Tejun Heo4e139af2012-11-19 08:13:36 -08004424 /*
4425 * Create directory. cgroup_create_file() returns with the new
4426 * directory locked on success so that it can be populated without
4427 * dropping cgroup_mutex.
4428 */
Tejun Heo28fd6f32012-11-19 08:13:36 -08004429 err = cgroup_create_file(dentry, S_IFDIR | mode, sb);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004430 if (err < 0)
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004431 goto err_free_all;
Tejun Heo4e139af2012-11-19 08:13:36 -08004432 lockdep_assert_held(&dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004433
Tejun Heo00356bd2013-06-18 11:14:22 -07004434 cgrp->serial_nr = cgroup_serial_nr_next++;
Tejun Heo53fa5262013-05-24 10:55:38 +09004435
Tejun Heo4e139af2012-11-19 08:13:36 -08004436 /* allocation complete, commit to creation */
Tejun Heo4e139af2012-11-19 08:13:36 -08004437 list_add_tail_rcu(&cgrp->sibling, &cgrp->parent->children);
4438 root->number_of_cgroups++;
Tejun Heo28fd6f32012-11-19 08:13:36 -08004439
Tejun Heob1929db2012-11-19 08:13:38 -08004440 /* each css holds a ref to the cgroup's dentry */
Tejun Heo5549c492013-06-24 15:21:48 -07004441 for_each_root_subsys(root, ss)
Tejun Heoed9577932012-11-05 09:16:58 -08004442 dget(dentry);
Tejun Heo48ddbe12012-04-01 12:09:56 -07004443
Li Zefan415cf072013-04-08 14:35:02 +08004444 /* hold a ref to the parent's dentry */
4445 dget(parent->dentry);
4446
Tejun Heob1929db2012-11-19 08:13:38 -08004447 /* creation succeeded, notify subsystems */
Tejun Heo5549c492013-06-24 15:21:48 -07004448 for_each_root_subsys(root, ss) {
Tejun Heob1929db2012-11-19 08:13:38 -08004449 err = online_css(ss, cgrp);
4450 if (err)
4451 goto err_destroy;
Glauber Costa1f869e82012-11-30 17:31:23 +04004452
4453 if (ss->broken_hierarchy && !ss->warned_broken_hierarchy &&
4454 parent->parent) {
4455 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",
4456 current->comm, current->pid, ss->name);
4457 if (!strcmp(ss->name, "memory"))
4458 pr_warning("cgroup: \"memory\" requires setting use_hierarchy to 1 on the root.\n");
4459 ss->warned_broken_hierarchy = true;
4460 }
Tejun Heoa8638032012-11-09 09:12:29 -08004461 }
4462
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004463 idr_replace(&root->cgroup_idr, cgrp, cgrp->id);
4464
Tejun Heo2bb566c2013-08-08 20:11:23 -04004465 err = cgroup_addrm_files(cgrp, cgroup_base_files, true);
Tejun Heo628f7cd2013-06-28 16:24:11 -07004466 if (err)
4467 goto err_destroy;
4468
4469 err = cgroup_populate_dir(cgrp, root->subsys_mask);
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004470 if (err)
4471 goto err_destroy;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004472
4473 mutex_unlock(&cgroup_mutex);
Paul Menagebd89aab2007-10-18 23:40:44 -07004474 mutex_unlock(&cgrp->dentry->d_inode->i_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004475
4476 return 0;
4477
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004478err_free_all:
Tejun Heo5549c492013-06-24 15:21:48 -07004479 for_each_root_subsys(root, ss) {
Tejun Heod3daf282013-06-13 19:39:16 -07004480 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4481
4482 if (css) {
4483 percpu_ref_cancel_init(&css->refcnt);
Tejun Heoeb954192013-08-08 20:11:23 -04004484 ss->css_free(css);
Tejun Heod3daf282013-06-13 19:39:16 -07004485 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004486 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07004487 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004488 /* Release the reference count that we took on the superblock */
4489 deactivate_super(sb);
Tejun Heo0a950f62012-11-19 09:02:12 -08004490err_free_id:
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004491 idr_remove(&root->cgroup_idr, cgrp->id);
Li Zefan65dff752013-03-01 15:01:56 +08004492err_free_name:
4493 kfree(rcu_dereference_raw(cgrp->name));
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004494err_free_cgrp:
Paul Menagebd89aab2007-10-18 23:40:44 -07004495 kfree(cgrp);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004496 return err;
Tejun Heo4b8b47eb2012-11-19 08:13:38 -08004497
4498err_destroy:
4499 cgroup_destroy_locked(cgrp);
4500 mutex_unlock(&cgroup_mutex);
4501 mutex_unlock(&dentry->d_inode->i_mutex);
4502 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004503}
4504
Al Viro18bb1db2011-07-26 01:41:39 -04004505static int cgroup_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004506{
4507 struct cgroup *c_parent = dentry->d_parent->d_fsdata;
4508
4509 /* the vfs holds inode->i_mutex already */
4510 return cgroup_create(c_parent, dentry, mode | S_IFDIR);
4511}
4512
Tejun Heod3daf282013-06-13 19:39:16 -07004513static void cgroup_css_killed(struct cgroup *cgrp)
4514{
4515 if (!atomic_dec_and_test(&cgrp->css_kill_cnt))
4516 return;
4517
4518 /* percpu ref's of all css's are killed, kick off the next step */
4519 INIT_WORK(&cgrp->destroy_work, cgroup_offline_fn);
4520 schedule_work(&cgrp->destroy_work);
4521}
4522
4523static void css_ref_killed_fn(struct percpu_ref *ref)
4524{
4525 struct cgroup_subsys_state *css =
4526 container_of(ref, struct cgroup_subsys_state, refcnt);
4527
4528 cgroup_css_killed(css->cgroup);
4529}
4530
4531/**
4532 * cgroup_destroy_locked - the first stage of cgroup destruction
4533 * @cgrp: cgroup to be destroyed
4534 *
4535 * css's make use of percpu refcnts whose killing latency shouldn't be
4536 * exposed to userland and are RCU protected. Also, cgroup core needs to
4537 * guarantee that css_tryget() won't succeed by the time ->css_offline() is
4538 * invoked. To satisfy all the requirements, destruction is implemented in
4539 * the following two steps.
4540 *
4541 * s1. Verify @cgrp can be destroyed and mark it dying. Remove all
4542 * userland visible parts and start killing the percpu refcnts of
4543 * css's. Set up so that the next stage will be kicked off once all
4544 * the percpu refcnts are confirmed to be killed.
4545 *
4546 * s2. Invoke ->css_offline(), mark the cgroup dead and proceed with the
4547 * rest of destruction. Once all cgroup references are gone, the
4548 * cgroup is RCU-freed.
4549 *
4550 * This function implements s1. After this step, @cgrp is gone as far as
4551 * the userland is concerned and a new cgroup with the same name may be
4552 * created. As cgroup doesn't care about the names internally, this
4553 * doesn't cause any problem.
4554 */
Tejun Heo42809dd2012-11-19 08:13:37 -08004555static int cgroup_destroy_locked(struct cgroup *cgrp)
4556 __releases(&cgroup_mutex) __acquires(&cgroup_mutex)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004557{
Tejun Heo42809dd2012-11-19 08:13:37 -08004558 struct dentry *d = cgrp->dentry;
Kirill A. Shutemov4ab78682010-03-10 15:22:34 -08004559 struct cgroup_event *event, *tmp;
Tejun Heoed9577932012-11-05 09:16:58 -08004560 struct cgroup_subsys *ss;
Tejun Heoddd69142013-06-12 21:04:54 -07004561 bool empty;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004562
Tejun Heo42809dd2012-11-19 08:13:37 -08004563 lockdep_assert_held(&d->d_inode->i_mutex);
4564 lockdep_assert_held(&cgroup_mutex);
4565
Tejun Heoddd69142013-06-12 21:04:54 -07004566 /*
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004567 * css_set_lock synchronizes access to ->cset_links and prevents
4568 * @cgrp from being removed while __put_css_set() is in progress.
Tejun Heoddd69142013-06-12 21:04:54 -07004569 */
4570 read_lock(&css_set_lock);
Tejun Heo6f3d828f02013-06-12 21:04:55 -07004571 empty = list_empty(&cgrp->cset_links) && list_empty(&cgrp->children);
Tejun Heoddd69142013-06-12 21:04:54 -07004572 read_unlock(&css_set_lock);
4573 if (!empty)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004574 return -EBUSY;
Tejun Heoed9577932012-11-05 09:16:58 -08004575
Tejun Heo1a90dd52012-11-05 09:16:59 -08004576 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004577 * Block new css_tryget() by killing css refcnts. cgroup core
4578 * guarantees that, by the time ->css_offline() is invoked, no new
4579 * css reference will be given out via css_tryget(). We can't
4580 * simply call percpu_ref_kill() and proceed to offlining css's
4581 * because percpu_ref_kill() doesn't guarantee that the ref is seen
4582 * as killed on all CPUs on return.
4583 *
4584 * Use percpu_ref_kill_and_confirm() to get notifications as each
4585 * css is confirmed to be seen as killed on all CPUs. The
4586 * notification callback keeps track of the number of css's to be
4587 * killed and schedules cgroup_offline_fn() to perform the rest of
4588 * destruction once the percpu refs of all css's are confirmed to
4589 * be killed.
Tejun Heo1a90dd52012-11-05 09:16:59 -08004590 */
Tejun Heod3daf282013-06-13 19:39:16 -07004591 atomic_set(&cgrp->css_kill_cnt, 1);
Tejun Heo5549c492013-06-24 15:21:48 -07004592 for_each_root_subsys(cgrp->root, ss) {
Tejun Heoed9577932012-11-05 09:16:58 -08004593 struct cgroup_subsys_state *css = cgrp->subsys[ss->subsys_id];
4594
Tejun Heod3daf282013-06-13 19:39:16 -07004595 /*
4596 * Killing would put the base ref, but we need to keep it
4597 * alive until after ->css_offline.
4598 */
4599 percpu_ref_get(&css->refcnt);
4600
4601 atomic_inc(&cgrp->css_kill_cnt);
4602 percpu_ref_kill_and_confirm(&css->refcnt, css_ref_killed_fn);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004603 }
Tejun Heod3daf282013-06-13 19:39:16 -07004604 cgroup_css_killed(cgrp);
Tejun Heo455050d2013-06-13 19:27:41 -07004605
4606 /*
4607 * Mark @cgrp dead. This prevents further task migration and child
4608 * creation by disabling cgroup_lock_live_group(). Note that
Tejun Heo492eb212013-08-08 20:11:25 -04004609 * CGRP_DEAD assertion is depended upon by css_next_child() to
Tejun Heo455050d2013-06-13 19:27:41 -07004610 * resume iteration after dropping RCU read lock. See
Tejun Heo492eb212013-08-08 20:11:25 -04004611 * css_next_child() for details.
Tejun Heo455050d2013-06-13 19:27:41 -07004612 */
Tejun Heo54766d42013-06-12 21:04:53 -07004613 set_bit(CGRP_DEAD, &cgrp->flags);
Tejun Heo1a90dd52012-11-05 09:16:59 -08004614
Tejun Heo455050d2013-06-13 19:27:41 -07004615 /* CGRP_DEAD is set, remove from ->release_list for the last time */
4616 raw_spin_lock(&release_list_lock);
4617 if (!list_empty(&cgrp->release_list))
4618 list_del_init(&cgrp->release_list);
4619 raw_spin_unlock(&release_list_lock);
4620
4621 /*
Tejun Heo8f891402013-06-28 16:24:10 -07004622 * Clear and remove @cgrp directory. The removal puts the base ref
4623 * but we aren't quite done with @cgrp yet, so hold onto it.
Tejun Heo455050d2013-06-13 19:27:41 -07004624 */
Tejun Heo628f7cd2013-06-28 16:24:11 -07004625 cgroup_clear_dir(cgrp, cgrp->root->subsys_mask);
Tejun Heo2bb566c2013-08-08 20:11:23 -04004626 cgroup_addrm_files(cgrp, cgroup_base_files, false);
Tejun Heo455050d2013-06-13 19:27:41 -07004627 dget(d);
4628 cgroup_d_remove_dir(d);
4629
4630 /*
4631 * Unregister events and notify userspace.
4632 * Notify userspace about cgroup removing only after rmdir of cgroup
4633 * directory to avoid race between userspace and kernelspace.
4634 */
4635 spin_lock(&cgrp->event_list_lock);
4636 list_for_each_entry_safe(event, tmp, &cgrp->event_list, list) {
4637 list_del_init(&event->list);
4638 schedule_work(&event->remove);
4639 }
4640 spin_unlock(&cgrp->event_list_lock);
4641
Tejun Heoea15f8c2013-06-13 19:27:42 -07004642 return 0;
4643};
4644
Tejun Heod3daf282013-06-13 19:39:16 -07004645/**
4646 * cgroup_offline_fn - the second step of cgroup destruction
4647 * @work: cgroup->destroy_free_work
4648 *
4649 * This function is invoked from a work item for a cgroup which is being
4650 * destroyed after the percpu refcnts of all css's are guaranteed to be
4651 * seen as killed on all CPUs, and performs the rest of destruction. This
4652 * is the second step of destruction described in the comment above
4653 * cgroup_destroy_locked().
4654 */
Tejun Heoea15f8c2013-06-13 19:27:42 -07004655static void cgroup_offline_fn(struct work_struct *work)
4656{
4657 struct cgroup *cgrp = container_of(work, struct cgroup, destroy_work);
4658 struct cgroup *parent = cgrp->parent;
4659 struct dentry *d = cgrp->dentry;
4660 struct cgroup_subsys *ss;
4661
4662 mutex_lock(&cgroup_mutex);
4663
Tejun Heod3daf282013-06-13 19:39:16 -07004664 /*
4665 * css_tryget() is guaranteed to fail now. Tell subsystems to
4666 * initate destruction.
4667 */
Tejun Heo5549c492013-06-24 15:21:48 -07004668 for_each_root_subsys(cgrp->root, ss)
Tejun Heoa31f2d32012-11-19 08:13:37 -08004669 offline_css(ss, cgrp);
Tejun Heoed9577932012-11-05 09:16:58 -08004670
4671 /*
Tejun Heod3daf282013-06-13 19:39:16 -07004672 * Put the css refs from cgroup_destroy_locked(). Each css holds
4673 * an extra reference to the cgroup's dentry and cgroup removal
4674 * proceeds regardless of css refs. On the last put of each css,
4675 * whenever that may be, the extra dentry ref is put so that dentry
4676 * destruction happens only after all css's are released.
Tejun Heoed9577932012-11-05 09:16:58 -08004677 */
Tejun Heo5549c492013-06-24 15:21:48 -07004678 for_each_root_subsys(cgrp->root, ss)
Tejun Heoe9316082012-11-05 09:16:58 -08004679 css_put(cgrp->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004680
Paul Menage999cd8a2009-01-07 18:08:36 -08004681 /* delete this cgroup from parent->children */
Tejun Heoeb6fd502012-11-09 09:12:29 -08004682 list_del_rcu(&cgrp->sibling);
Tejun Heob0ca5a82012-04-01 12:09:54 -07004683
Li Zefan4e96ee8e2013-07-31 09:50:50 +08004684 /*
4685 * We should remove the cgroup object from idr before its grace
4686 * period starts, so we won't be looking up a cgroup while the
4687 * cgroup is being freed.
4688 */
4689 idr_remove(&cgrp->root->cgroup_idr, cgrp->id);
4690 cgrp->id = -1;
4691
Paul Menageddbcc7e2007-10-18 23:39:30 -07004692 dput(d);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004693
Paul Menagebd89aab2007-10-18 23:40:44 -07004694 set_bit(CGRP_RELEASABLE, &parent->flags);
Paul Menage81a6a5c2007-10-18 23:39:38 -07004695 check_for_release(parent);
4696
Tejun Heoea15f8c2013-06-13 19:27:42 -07004697 mutex_unlock(&cgroup_mutex);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004698}
4699
Tejun Heo42809dd2012-11-19 08:13:37 -08004700static int cgroup_rmdir(struct inode *unused_dir, struct dentry *dentry)
4701{
4702 int ret;
4703
4704 mutex_lock(&cgroup_mutex);
4705 ret = cgroup_destroy_locked(dentry->d_fsdata);
4706 mutex_unlock(&cgroup_mutex);
4707
4708 return ret;
4709}
4710
Tejun Heo8e3f6542012-04-01 12:09:55 -07004711static void __init_or_module cgroup_init_cftsets(struct cgroup_subsys *ss)
4712{
4713 INIT_LIST_HEAD(&ss->cftsets);
4714
4715 /*
4716 * base_cftset is embedded in subsys itself, no need to worry about
4717 * deregistration.
4718 */
4719 if (ss->base_cftypes) {
Tejun Heo2bb566c2013-08-08 20:11:23 -04004720 struct cftype *cft;
4721
4722 for (cft = ss->base_cftypes; cft->name[0] != '\0'; cft++)
4723 cft->ss = ss;
4724
Tejun Heo8e3f6542012-04-01 12:09:55 -07004725 ss->base_cftset.cfts = ss->base_cftypes;
4726 list_add_tail(&ss->base_cftset.node, &ss->cftsets);
4727 }
4728}
4729
Li Zefan06a11922008-04-29 01:00:07 -07004730static void __init cgroup_init_subsys(struct cgroup_subsys *ss)
Paul Menageddbcc7e2007-10-18 23:39:30 -07004731{
Paul Menageddbcc7e2007-10-18 23:39:30 -07004732 struct cgroup_subsys_state *css;
Diego Callejacfe36bd2007-11-14 16:58:54 -08004733
4734 printk(KERN_INFO "Initializing cgroup subsys %s\n", ss->name);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004735
Tejun Heo648bb562012-11-19 08:13:36 -08004736 mutex_lock(&cgroup_mutex);
4737
Tejun Heo8e3f6542012-04-01 12:09:55 -07004738 /* init base cftset */
4739 cgroup_init_cftsets(ss);
4740
Paul Menageddbcc7e2007-10-18 23:39:30 -07004741 /* Create the top cgroup state for this subsystem */
Tejun Heo9871bf92013-06-24 15:21:47 -07004742 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4743 ss->root = &cgroup_dummy_root;
Tejun Heoeb954192013-08-08 20:11:23 -04004744 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004745 /* We don't handle early failures gracefully */
4746 BUG_ON(IS_ERR(css));
Tejun Heo9871bf92013-06-24 15:21:47 -07004747 init_cgroup_css(css, ss, cgroup_dummy_top);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004748
Li Zefane8d55fd2008-04-29 01:00:13 -07004749 /* Update the init_css_set to contain a subsys
Paul Menage817929e2007-10-18 23:39:36 -07004750 * pointer to this state - since the subsystem is
Li Zefane8d55fd2008-04-29 01:00:13 -07004751 * newly registered, all tasks and hence the
4752 * init_css_set is in the subsystem's top cgroup. */
Tejun Heob48c6a82012-11-19 08:13:36 -08004753 init_css_set.subsys[ss->subsys_id] = css;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004754
4755 need_forkexit_callback |= ss->fork || ss->exit;
4756
Li Zefane8d55fd2008-04-29 01:00:13 -07004757 /* At system boot, before all subsystems have been
4758 * registered, no tasks have been forked, so we don't
4759 * need to invoke fork callbacks here. */
4760 BUG_ON(!list_empty(&init_task.tasks));
4761
Tejun Heo9871bf92013-06-24 15:21:47 -07004762 BUG_ON(online_css(ss, cgroup_dummy_top));
Tejun Heoa8638032012-11-09 09:12:29 -08004763
Tejun Heo648bb562012-11-19 08:13:36 -08004764 mutex_unlock(&cgroup_mutex);
4765
Ben Blume6a11052010-03-10 15:22:09 -08004766 /* this function shouldn't be used with modular subsystems, since they
4767 * need to register a subsys_id, among other things */
4768 BUG_ON(ss->module);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004769}
4770
4771/**
Ben Blume6a11052010-03-10 15:22:09 -08004772 * cgroup_load_subsys: load and register a modular subsystem at runtime
4773 * @ss: the subsystem to load
4774 *
4775 * This function should be called in a modular subsystem's initcall. If the
Thomas Weber88393162010-03-16 11:47:56 +01004776 * subsystem is built as a module, it will be assigned a new subsys_id and set
Ben Blume6a11052010-03-10 15:22:09 -08004777 * up for use. If the subsystem is built-in anyway, work is delegated to the
4778 * simpler cgroup_init_subsys.
4779 */
4780int __init_or_module cgroup_load_subsys(struct cgroup_subsys *ss)
4781{
Ben Blume6a11052010-03-10 15:22:09 -08004782 struct cgroup_subsys_state *css;
Tejun Heod19e19d2012-11-19 08:13:37 -08004783 int i, ret;
Sasha Levinb67bfe02013-02-27 17:06:00 -08004784 struct hlist_node *tmp;
Tejun Heo5abb8852013-06-12 21:04:49 -07004785 struct css_set *cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004786 unsigned long key;
Ben Blume6a11052010-03-10 15:22:09 -08004787
4788 /* check name and function validity */
4789 if (ss->name == NULL || strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN ||
Tejun Heo92fb9742012-11-19 08:13:38 -08004790 ss->css_alloc == NULL || ss->css_free == NULL)
Ben Blume6a11052010-03-10 15:22:09 -08004791 return -EINVAL;
4792
4793 /*
4794 * we don't support callbacks in modular subsystems. this check is
4795 * before the ss->module check for consistency; a subsystem that could
4796 * be a module should still have no callbacks even if the user isn't
4797 * compiling it as one.
4798 */
4799 if (ss->fork || ss->exit)
4800 return -EINVAL;
4801
4802 /*
4803 * an optionally modular subsystem is built-in: we want to do nothing,
4804 * since cgroup_init_subsys will have already taken care of it.
4805 */
4806 if (ss->module == NULL) {
Daniel Wagnerbe45c902012-09-13 09:50:55 +02004807 /* a sanity check */
Tejun Heo9871bf92013-06-24 15:21:47 -07004808 BUG_ON(cgroup_subsys[ss->subsys_id] != ss);
Ben Blume6a11052010-03-10 15:22:09 -08004809 return 0;
4810 }
4811
Tejun Heo8e3f6542012-04-01 12:09:55 -07004812 /* init base cftset */
4813 cgroup_init_cftsets(ss);
4814
Ben Blume6a11052010-03-10 15:22:09 -08004815 mutex_lock(&cgroup_mutex);
Tejun Heo9871bf92013-06-24 15:21:47 -07004816 cgroup_subsys[ss->subsys_id] = ss;
Ben Blume6a11052010-03-10 15:22:09 -08004817
4818 /*
Tejun Heo92fb9742012-11-19 08:13:38 -08004819 * no ss->css_alloc seems to need anything important in the ss
Tejun Heo9871bf92013-06-24 15:21:47 -07004820 * struct, so this can happen first (i.e. before the dummy root
Tejun Heo92fb9742012-11-19 08:13:38 -08004821 * attachment).
Ben Blume6a11052010-03-10 15:22:09 -08004822 */
Tejun Heoeb954192013-08-08 20:11:23 -04004823 css = ss->css_alloc(cgroup_dummy_top->subsys[ss->subsys_id]);
Ben Blume6a11052010-03-10 15:22:09 -08004824 if (IS_ERR(css)) {
Tejun Heo9871bf92013-06-24 15:21:47 -07004825 /* failure case - need to deassign the cgroup_subsys[] slot. */
4826 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blume6a11052010-03-10 15:22:09 -08004827 mutex_unlock(&cgroup_mutex);
4828 return PTR_ERR(css);
4829 }
4830
Tejun Heo9871bf92013-06-24 15:21:47 -07004831 list_add(&ss->sibling, &cgroup_dummy_root.subsys_list);
4832 ss->root = &cgroup_dummy_root;
Ben Blume6a11052010-03-10 15:22:09 -08004833
4834 /* our new subsystem will be attached to the dummy hierarchy. */
Tejun Heo9871bf92013-06-24 15:21:47 -07004835 init_cgroup_css(css, ss, cgroup_dummy_top);
Ben Blume6a11052010-03-10 15:22:09 -08004836 /* init_idr must be after init_cgroup_css because it sets css->id. */
4837 if (ss->use_id) {
Tejun Heod19e19d2012-11-19 08:13:37 -08004838 ret = cgroup_init_idr(ss, css);
4839 if (ret)
4840 goto err_unload;
Ben Blume6a11052010-03-10 15:22:09 -08004841 }
4842
4843 /*
4844 * Now we need to entangle the css into the existing css_sets. unlike
4845 * in cgroup_init_subsys, there are now multiple css_sets, so each one
4846 * will need a new pointer to it; done by iterating the css_set_table.
4847 * furthermore, modifying the existing css_sets will corrupt the hash
4848 * table state, so each changed css_set will need its hash recomputed.
4849 * this is all done under the css_set_lock.
4850 */
4851 write_lock(&css_set_lock);
Tejun Heo5abb8852013-06-12 21:04:49 -07004852 hash_for_each_safe(css_set_table, i, tmp, cset, hlist) {
Li Zefan0ac801f2013-01-10 11:49:27 +08004853 /* skip entries that we already rehashed */
Tejun Heo5abb8852013-06-12 21:04:49 -07004854 if (cset->subsys[ss->subsys_id])
Li Zefan0ac801f2013-01-10 11:49:27 +08004855 continue;
4856 /* remove existing entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004857 hash_del(&cset->hlist);
Li Zefan0ac801f2013-01-10 11:49:27 +08004858 /* set new value */
Tejun Heo5abb8852013-06-12 21:04:49 -07004859 cset->subsys[ss->subsys_id] = css;
Li Zefan0ac801f2013-01-10 11:49:27 +08004860 /* recompute hash and restore entry */
Tejun Heo5abb8852013-06-12 21:04:49 -07004861 key = css_set_hash(cset->subsys);
4862 hash_add(css_set_table, &cset->hlist, key);
Ben Blume6a11052010-03-10 15:22:09 -08004863 }
4864 write_unlock(&css_set_lock);
4865
Tejun Heo9871bf92013-06-24 15:21:47 -07004866 ret = online_css(ss, cgroup_dummy_top);
Tejun Heob1929db2012-11-19 08:13:38 -08004867 if (ret)
4868 goto err_unload;
Tejun Heoa8638032012-11-09 09:12:29 -08004869
Ben Blume6a11052010-03-10 15:22:09 -08004870 /* success! */
4871 mutex_unlock(&cgroup_mutex);
4872 return 0;
Tejun Heod19e19d2012-11-19 08:13:37 -08004873
4874err_unload:
4875 mutex_unlock(&cgroup_mutex);
4876 /* @ss can't be mounted here as try_module_get() would fail */
4877 cgroup_unload_subsys(ss);
4878 return ret;
Ben Blume6a11052010-03-10 15:22:09 -08004879}
4880EXPORT_SYMBOL_GPL(cgroup_load_subsys);
4881
4882/**
Ben Blumcf5d5942010-03-10 15:22:09 -08004883 * cgroup_unload_subsys: unload a modular subsystem
4884 * @ss: the subsystem to unload
4885 *
4886 * This function should be called in a modular subsystem's exitcall. When this
4887 * function is invoked, the refcount on the subsystem's module will be 0, so
4888 * the subsystem will not be attached to any hierarchy.
4889 */
4890void cgroup_unload_subsys(struct cgroup_subsys *ss)
4891{
Tejun Heo69d02062013-06-12 21:04:50 -07004892 struct cgrp_cset_link *link;
Ben Blumcf5d5942010-03-10 15:22:09 -08004893
4894 BUG_ON(ss->module == NULL);
4895
4896 /*
4897 * we shouldn't be called if the subsystem is in use, and the use of
Tejun Heo1d5be6b2013-07-12 13:38:17 -07004898 * try_module_get() in rebind_subsystems() should ensure that it
Ben Blumcf5d5942010-03-10 15:22:09 -08004899 * doesn't start being used while we're killing it off.
4900 */
Tejun Heo9871bf92013-06-24 15:21:47 -07004901 BUG_ON(ss->root != &cgroup_dummy_root);
Ben Blumcf5d5942010-03-10 15:22:09 -08004902
4903 mutex_lock(&cgroup_mutex);
Tejun Heo02ae7482012-11-19 08:13:37 -08004904
Tejun Heo9871bf92013-06-24 15:21:47 -07004905 offline_css(ss, cgroup_dummy_top);
Tejun Heo02ae7482012-11-19 08:13:37 -08004906
Tejun Heoc897ff62013-02-27 17:03:49 -08004907 if (ss->use_id)
Tejun Heo02ae7482012-11-19 08:13:37 -08004908 idr_destroy(&ss->idr);
Tejun Heo02ae7482012-11-19 08:13:37 -08004909
Ben Blumcf5d5942010-03-10 15:22:09 -08004910 /* deassign the subsys_id */
Tejun Heo9871bf92013-06-24 15:21:47 -07004911 cgroup_subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004912
Tejun Heo9871bf92013-06-24 15:21:47 -07004913 /* remove subsystem from the dummy root's list of subsystems */
Phil Carmody8d258792011-03-22 16:30:13 -07004914 list_del_init(&ss->sibling);
Ben Blumcf5d5942010-03-10 15:22:09 -08004915
4916 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004917 * disentangle the css from all css_sets attached to the dummy
4918 * top. as in loading, we need to pay our respects to the hashtable
4919 * gods.
Ben Blumcf5d5942010-03-10 15:22:09 -08004920 */
4921 write_lock(&css_set_lock);
Tejun Heo9871bf92013-06-24 15:21:47 -07004922 list_for_each_entry(link, &cgroup_dummy_top->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07004923 struct css_set *cset = link->cset;
Li Zefan0ac801f2013-01-10 11:49:27 +08004924 unsigned long key;
Ben Blumcf5d5942010-03-10 15:22:09 -08004925
Tejun Heo5abb8852013-06-12 21:04:49 -07004926 hash_del(&cset->hlist);
4927 cset->subsys[ss->subsys_id] = NULL;
4928 key = css_set_hash(cset->subsys);
4929 hash_add(css_set_table, &cset->hlist, key);
Ben Blumcf5d5942010-03-10 15:22:09 -08004930 }
4931 write_unlock(&css_set_lock);
4932
4933 /*
Tejun Heo9871bf92013-06-24 15:21:47 -07004934 * remove subsystem's css from the cgroup_dummy_top and free it -
4935 * need to free before marking as null because ss->css_free needs
4936 * the cgrp->subsys pointer to find their state. note that this
4937 * also takes care of freeing the css_id.
Ben Blumcf5d5942010-03-10 15:22:09 -08004938 */
Tejun Heoeb954192013-08-08 20:11:23 -04004939 ss->css_free(cgroup_dummy_top->subsys[ss->subsys_id]);
Tejun Heo9871bf92013-06-24 15:21:47 -07004940 cgroup_dummy_top->subsys[ss->subsys_id] = NULL;
Ben Blumcf5d5942010-03-10 15:22:09 -08004941
4942 mutex_unlock(&cgroup_mutex);
4943}
4944EXPORT_SYMBOL_GPL(cgroup_unload_subsys);
4945
4946/**
Li Zefana043e3b2008-02-23 15:24:09 -08004947 * cgroup_init_early - cgroup initialization at system boot
4948 *
4949 * Initialize cgroups at system boot, and initialize any
4950 * subsystems that request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004951 */
4952int __init cgroup_init_early(void)
4953{
Tejun Heo30159ec2013-06-25 11:53:37 -07004954 struct cgroup_subsys *ss;
Paul Menageddbcc7e2007-10-18 23:39:30 -07004955 int i;
Tejun Heo30159ec2013-06-25 11:53:37 -07004956
Lai Jiangshan146aa1b2008-10-18 20:28:03 -07004957 atomic_set(&init_css_set.refcount, 1);
Tejun Heo69d02062013-06-12 21:04:50 -07004958 INIT_LIST_HEAD(&init_css_set.cgrp_links);
Paul Menage817929e2007-10-18 23:39:36 -07004959 INIT_LIST_HEAD(&init_css_set.tasks);
Li Zefan472b1052008-04-29 01:00:11 -07004960 INIT_HLIST_NODE(&init_css_set.hlist);
Paul Menage817929e2007-10-18 23:39:36 -07004961 css_set_count = 1;
Tejun Heo9871bf92013-06-24 15:21:47 -07004962 init_cgroup_root(&cgroup_dummy_root);
4963 cgroup_root_count = 1;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07004964 RCU_INIT_POINTER(init_task.cgroups, &init_css_set);
Paul Menage817929e2007-10-18 23:39:36 -07004965
Tejun Heo69d02062013-06-12 21:04:50 -07004966 init_cgrp_cset_link.cset = &init_css_set;
Tejun Heo9871bf92013-06-24 15:21:47 -07004967 init_cgrp_cset_link.cgrp = cgroup_dummy_top;
4968 list_add(&init_cgrp_cset_link.cset_link, &cgroup_dummy_top->cset_links);
Tejun Heo69d02062013-06-12 21:04:50 -07004969 list_add(&init_cgrp_cset_link.cgrp_link, &init_css_set.cgrp_links);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004970
Tejun Heo30159ec2013-06-25 11:53:37 -07004971 /* at bootup time, we don't worry about modular subsystems */
4972 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07004973 BUG_ON(!ss->name);
4974 BUG_ON(strlen(ss->name) > MAX_CGROUP_TYPE_NAMELEN);
Tejun Heo92fb9742012-11-19 08:13:38 -08004975 BUG_ON(!ss->css_alloc);
4976 BUG_ON(!ss->css_free);
Paul Menageddbcc7e2007-10-18 23:39:30 -07004977 if (ss->subsys_id != i) {
Diego Callejacfe36bd2007-11-14 16:58:54 -08004978 printk(KERN_ERR "cgroup: Subsys %s id == %d\n",
Paul Menageddbcc7e2007-10-18 23:39:30 -07004979 ss->name, ss->subsys_id);
4980 BUG();
4981 }
4982
4983 if (ss->early_init)
4984 cgroup_init_subsys(ss);
4985 }
4986 return 0;
4987}
4988
4989/**
Li Zefana043e3b2008-02-23 15:24:09 -08004990 * cgroup_init - cgroup initialization
4991 *
4992 * Register cgroup filesystem and /proc file, and initialize
4993 * any subsystems that didn't request early init.
Paul Menageddbcc7e2007-10-18 23:39:30 -07004994 */
4995int __init cgroup_init(void)
4996{
Tejun Heo30159ec2013-06-25 11:53:37 -07004997 struct cgroup_subsys *ss;
Li Zefan0ac801f2013-01-10 11:49:27 +08004998 unsigned long key;
Tejun Heo30159ec2013-06-25 11:53:37 -07004999 int i, err;
Paul Menagea4243162007-10-18 23:39:35 -07005000
5001 err = bdi_init(&cgroup_backing_dev_info);
5002 if (err)
5003 return err;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005004
Tejun Heo30159ec2013-06-25 11:53:37 -07005005 for_each_builtin_subsys(ss, i) {
Paul Menageddbcc7e2007-10-18 23:39:30 -07005006 if (!ss->early_init)
5007 cgroup_init_subsys(ss);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005008 if (ss->use_id)
Ben Blume6a11052010-03-10 15:22:09 -08005009 cgroup_init_idr(ss, init_css_set.subsys[ss->subsys_id]);
Paul Menageddbcc7e2007-10-18 23:39:30 -07005010 }
5011
Tejun Heofa3ca072013-04-14 11:36:56 -07005012 /* allocate id for the dummy hierarchy */
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005013 mutex_lock(&cgroup_mutex);
5014 mutex_lock(&cgroup_root_mutex);
5015
Tejun Heo82fe9b02013-06-25 11:53:37 -07005016 /* Add init_css_set to the hash table */
5017 key = css_set_hash(init_css_set.subsys);
5018 hash_add(css_set_table, &init_css_set.hlist, key);
5019
Tejun Heofc76df72013-06-25 11:53:37 -07005020 BUG_ON(cgroup_init_root_id(&cgroup_dummy_root, 0, 1));
Greg KH676db4a2010-08-05 13:53:35 -07005021
Li Zefan4e96ee8e2013-07-31 09:50:50 +08005022 err = idr_alloc(&cgroup_dummy_root.cgroup_idr, cgroup_dummy_top,
5023 0, 1, GFP_KERNEL);
5024 BUG_ON(err < 0);
5025
Tejun Heo54e7b4e2013-04-14 11:36:57 -07005026 mutex_unlock(&cgroup_root_mutex);
5027 mutex_unlock(&cgroup_mutex);
5028
Greg KH676db4a2010-08-05 13:53:35 -07005029 cgroup_kobj = kobject_create_and_add("cgroup", fs_kobj);
5030 if (!cgroup_kobj) {
5031 err = -ENOMEM;
Paul Menageddbcc7e2007-10-18 23:39:30 -07005032 goto out;
Greg KH676db4a2010-08-05 13:53:35 -07005033 }
5034
5035 err = register_filesystem(&cgroup_fs_type);
5036 if (err < 0) {
5037 kobject_put(cgroup_kobj);
5038 goto out;
5039 }
Paul Menageddbcc7e2007-10-18 23:39:30 -07005040
Li Zefan46ae2202008-04-29 01:00:08 -07005041 proc_create("cgroups", 0, NULL, &proc_cgroupstats_operations);
Paul Menagea4243162007-10-18 23:39:35 -07005042
Paul Menageddbcc7e2007-10-18 23:39:30 -07005043out:
Paul Menagea4243162007-10-18 23:39:35 -07005044 if (err)
5045 bdi_destroy(&cgroup_backing_dev_info);
5046
Paul Menageddbcc7e2007-10-18 23:39:30 -07005047 return err;
5048}
Paul Menageb4f48b62007-10-18 23:39:33 -07005049
Paul Menagea4243162007-10-18 23:39:35 -07005050/*
5051 * proc_cgroup_show()
5052 * - Print task's cgroup paths into seq_file, one line for each hierarchy
5053 * - Used for /proc/<pid>/cgroup.
5054 * - No need to task_lock(tsk) on this tsk->cgroup reference, as it
5055 * doesn't really matter if tsk->cgroup changes after we read it,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005056 * and we take cgroup_mutex, keeping cgroup_attach_task() from changing it
Paul Menagea4243162007-10-18 23:39:35 -07005057 * anyway. No need to check that tsk->cgroup != NULL, thanks to
5058 * the_top_cgroup_hack in cgroup_exit(), which sets an exiting tasks
5059 * cgroup to top_cgroup.
5060 */
5061
5062/* TODO: Use a proper seq_file iterator */
Al Viro8d8b97b2013-04-19 23:11:24 -04005063int proc_cgroup_show(struct seq_file *m, void *v)
Paul Menagea4243162007-10-18 23:39:35 -07005064{
5065 struct pid *pid;
5066 struct task_struct *tsk;
5067 char *buf;
5068 int retval;
5069 struct cgroupfs_root *root;
5070
5071 retval = -ENOMEM;
5072 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
5073 if (!buf)
5074 goto out;
5075
5076 retval = -ESRCH;
5077 pid = m->private;
5078 tsk = get_pid_task(pid, PIDTYPE_PID);
5079 if (!tsk)
5080 goto out_free;
5081
5082 retval = 0;
5083
5084 mutex_lock(&cgroup_mutex);
5085
Li Zefane5f6a862009-01-07 18:07:41 -08005086 for_each_active_root(root) {
Paul Menagea4243162007-10-18 23:39:35 -07005087 struct cgroup_subsys *ss;
Paul Menagebd89aab2007-10-18 23:40:44 -07005088 struct cgroup *cgrp;
Paul Menagea4243162007-10-18 23:39:35 -07005089 int count = 0;
5090
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005091 seq_printf(m, "%d:", root->hierarchy_id);
Tejun Heo5549c492013-06-24 15:21:48 -07005092 for_each_root_subsys(root, ss)
Paul Menagea4243162007-10-18 23:39:35 -07005093 seq_printf(m, "%s%s", count++ ? "," : "", ss->name);
Paul Menagec6d57f32009-09-23 15:56:19 -07005094 if (strlen(root->name))
5095 seq_printf(m, "%sname=%s", count ? "," : "",
5096 root->name);
Paul Menagea4243162007-10-18 23:39:35 -07005097 seq_putc(m, ':');
Paul Menage7717f7b2009-09-23 15:56:22 -07005098 cgrp = task_cgroup_from_root(tsk, root);
Paul Menagebd89aab2007-10-18 23:40:44 -07005099 retval = cgroup_path(cgrp, buf, PAGE_SIZE);
Paul Menagea4243162007-10-18 23:39:35 -07005100 if (retval < 0)
5101 goto out_unlock;
5102 seq_puts(m, buf);
5103 seq_putc(m, '\n');
5104 }
5105
5106out_unlock:
5107 mutex_unlock(&cgroup_mutex);
5108 put_task_struct(tsk);
5109out_free:
5110 kfree(buf);
5111out:
5112 return retval;
5113}
5114
Paul Menagea4243162007-10-18 23:39:35 -07005115/* Display information about each subsystem and each hierarchy */
5116static int proc_cgroupstats_show(struct seq_file *m, void *v)
5117{
Tejun Heo30159ec2013-06-25 11:53:37 -07005118 struct cgroup_subsys *ss;
Paul Menagea4243162007-10-18 23:39:35 -07005119 int i;
Paul Menagea4243162007-10-18 23:39:35 -07005120
Paul Menage8bab8dd2008-04-04 14:29:57 -07005121 seq_puts(m, "#subsys_name\thierarchy\tnum_cgroups\tenabled\n");
Ben Blumaae8aab2010-03-10 15:22:07 -08005122 /*
5123 * ideally we don't want subsystems moving around while we do this.
5124 * cgroup_mutex is also necessary to guarantee an atomic snapshot of
5125 * subsys/hierarchy state.
5126 */
Paul Menagea4243162007-10-18 23:39:35 -07005127 mutex_lock(&cgroup_mutex);
Tejun Heo30159ec2013-06-25 11:53:37 -07005128
5129 for_each_subsys(ss, i)
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005130 seq_printf(m, "%s\t%d\t%d\t%d\n",
5131 ss->name, ss->root->hierarchy_id,
Paul Menage8bab8dd2008-04-04 14:29:57 -07005132 ss->root->number_of_cgroups, !ss->disabled);
Tejun Heo30159ec2013-06-25 11:53:37 -07005133
Paul Menagea4243162007-10-18 23:39:35 -07005134 mutex_unlock(&cgroup_mutex);
5135 return 0;
5136}
5137
5138static int cgroupstats_open(struct inode *inode, struct file *file)
5139{
Al Viro9dce07f2008-03-29 03:07:28 +00005140 return single_open(file, proc_cgroupstats_show, NULL);
Paul Menagea4243162007-10-18 23:39:35 -07005141}
5142
Alexey Dobriyan828c0952009-10-01 15:43:56 -07005143static const struct file_operations proc_cgroupstats_operations = {
Paul Menagea4243162007-10-18 23:39:35 -07005144 .open = cgroupstats_open,
5145 .read = seq_read,
5146 .llseek = seq_lseek,
5147 .release = single_release,
5148};
5149
Paul Menageb4f48b62007-10-18 23:39:33 -07005150/**
5151 * cgroup_fork - attach newly forked task to its parents cgroup.
Li Zefana043e3b2008-02-23 15:24:09 -08005152 * @child: pointer to task_struct of forking parent process.
Paul Menageb4f48b62007-10-18 23:39:33 -07005153 *
5154 * Description: A task inherits its parent's cgroup at fork().
5155 *
5156 * A pointer to the shared css_set was automatically copied in
5157 * fork.c by dup_task_struct(). However, we ignore that copy, since
Tejun Heo9bb71302012-10-18 17:52:07 -07005158 * it was not made under the protection of RCU or cgroup_mutex, so
5159 * might no longer be a valid cgroup pointer. cgroup_attach_task() might
5160 * have already changed current->cgroups, allowing the previously
5161 * referenced cgroup group to be removed and freed.
Paul Menageb4f48b62007-10-18 23:39:33 -07005162 *
5163 * At the point that cgroup_fork() is called, 'current' is the parent
5164 * task, and the passed argument 'child' points to the child task.
5165 */
5166void cgroup_fork(struct task_struct *child)
5167{
Tejun Heo9bb71302012-10-18 17:52:07 -07005168 task_lock(current);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005169 get_css_set(task_css_set(current));
Paul Menage817929e2007-10-18 23:39:36 -07005170 child->cgroups = current->cgroups;
Tejun Heo9bb71302012-10-18 17:52:07 -07005171 task_unlock(current);
Paul Menage817929e2007-10-18 23:39:36 -07005172 INIT_LIST_HEAD(&child->cg_list);
Paul Menageb4f48b62007-10-18 23:39:33 -07005173}
5174
5175/**
Li Zefana043e3b2008-02-23 15:24:09 -08005176 * cgroup_post_fork - called on a new task after adding it to the task list
5177 * @child: the task in question
5178 *
Tejun Heo5edee612012-10-16 15:03:14 -07005179 * Adds the task to the list running through its css_set if necessary and
5180 * call the subsystem fork() callbacks. Has to be after the task is
5181 * visible on the task list in case we race with the first call to
Tejun Heo0942eee2013-08-08 20:11:26 -04005182 * cgroup_task_iter_start() - to guarantee that the new task ends up on its
Tejun Heo5edee612012-10-16 15:03:14 -07005183 * list.
Li Zefana043e3b2008-02-23 15:24:09 -08005184 */
Paul Menage817929e2007-10-18 23:39:36 -07005185void cgroup_post_fork(struct task_struct *child)
5186{
Tejun Heo30159ec2013-06-25 11:53:37 -07005187 struct cgroup_subsys *ss;
Tejun Heo5edee612012-10-16 15:03:14 -07005188 int i;
5189
Frederic Weisbecker3ce32302012-02-08 03:37:27 +01005190 /*
5191 * use_task_css_set_links is set to 1 before we walk the tasklist
5192 * under the tasklist_lock and we read it here after we added the child
5193 * to the tasklist under the tasklist_lock as well. If the child wasn't
5194 * yet in the tasklist when we walked through it from
5195 * cgroup_enable_task_cg_lists(), then use_task_css_set_links value
5196 * should be visible now due to the paired locking and barriers implied
5197 * by LOCK/UNLOCK: it is written before the tasklist_lock unlock
5198 * in cgroup_enable_task_cg_lists() and read here after the tasklist_lock
5199 * lock on fork.
5200 */
Paul Menage817929e2007-10-18 23:39:36 -07005201 if (use_task_css_set_links) {
5202 write_lock(&css_set_lock);
Tejun Heod8783832012-10-18 17:40:30 -07005203 task_lock(child);
5204 if (list_empty(&child->cg_list))
Tejun Heoa8ad8052013-06-21 15:52:04 -07005205 list_add(&child->cg_list, &task_css_set(child)->tasks);
Tejun Heod8783832012-10-18 17:40:30 -07005206 task_unlock(child);
Paul Menage817929e2007-10-18 23:39:36 -07005207 write_unlock(&css_set_lock);
5208 }
Tejun Heo5edee612012-10-16 15:03:14 -07005209
5210 /*
5211 * Call ss->fork(). This must happen after @child is linked on
5212 * css_set; otherwise, @child might change state between ->fork()
5213 * and addition to css_set.
5214 */
5215 if (need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005216 /*
5217 * fork/exit callbacks are supported only for builtin
5218 * subsystems, and the builtin section of the subsys
5219 * array is immutable, so we don't need to lock the
5220 * subsys array here. On the other hand, modular section
5221 * of the array can be freed at module unload, so we
5222 * can't touch that.
5223 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005224 for_each_builtin_subsys(ss, i)
Tejun Heo5edee612012-10-16 15:03:14 -07005225 if (ss->fork)
5226 ss->fork(child);
Tejun Heo5edee612012-10-16 15:03:14 -07005227 }
Paul Menage817929e2007-10-18 23:39:36 -07005228}
Tejun Heo5edee612012-10-16 15:03:14 -07005229
Paul Menage817929e2007-10-18 23:39:36 -07005230/**
Paul Menageb4f48b62007-10-18 23:39:33 -07005231 * cgroup_exit - detach cgroup from exiting task
5232 * @tsk: pointer to task_struct of exiting process
Li Zefana043e3b2008-02-23 15:24:09 -08005233 * @run_callback: run exit callbacks?
Paul Menageb4f48b62007-10-18 23:39:33 -07005234 *
5235 * Description: Detach cgroup from @tsk and release it.
5236 *
5237 * Note that cgroups marked notify_on_release force every task in
5238 * them to take the global cgroup_mutex mutex when exiting.
5239 * This could impact scaling on very large systems. Be reluctant to
5240 * use notify_on_release cgroups where very high task exit scaling
5241 * is required on large systems.
5242 *
5243 * the_top_cgroup_hack:
5244 *
5245 * Set the exiting tasks cgroup to the root cgroup (top_cgroup).
5246 *
5247 * We call cgroup_exit() while the task is still competent to
5248 * handle notify_on_release(), then leave the task attached to the
5249 * root cgroup in each hierarchy for the remainder of its exit.
5250 *
5251 * To do this properly, we would increment the reference count on
5252 * top_cgroup, and near the very end of the kernel/exit.c do_exit()
5253 * code we would add a second cgroup function call, to drop that
5254 * reference. This would just create an unnecessary hot spot on
5255 * the top_cgroup reference count, to no avail.
5256 *
5257 * Normally, holding a reference to a cgroup without bumping its
5258 * count is unsafe. The cgroup could go away, or someone could
5259 * attach us to a different cgroup, decrementing the count on
5260 * the first cgroup that we never incremented. But in this case,
5261 * top_cgroup isn't going away, and either task has PF_EXITING set,
Cliff Wickman956db3c2008-02-07 00:14:43 -08005262 * which wards off any cgroup_attach_task() attempts, or task is a failed
5263 * fork, never visible to cgroup_attach_task.
Paul Menageb4f48b62007-10-18 23:39:33 -07005264 */
5265void cgroup_exit(struct task_struct *tsk, int run_callbacks)
5266{
Tejun Heo30159ec2013-06-25 11:53:37 -07005267 struct cgroup_subsys *ss;
Tejun Heo5abb8852013-06-12 21:04:49 -07005268 struct css_set *cset;
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005269 int i;
Paul Menage817929e2007-10-18 23:39:36 -07005270
5271 /*
5272 * Unlink from the css_set task list if necessary.
5273 * Optimistically check cg_list before taking
5274 * css_set_lock
5275 */
5276 if (!list_empty(&tsk->cg_list)) {
5277 write_lock(&css_set_lock);
5278 if (!list_empty(&tsk->cg_list))
Phil Carmody8d258792011-03-22 16:30:13 -07005279 list_del_init(&tsk->cg_list);
Paul Menage817929e2007-10-18 23:39:36 -07005280 write_unlock(&css_set_lock);
5281 }
5282
Paul Menageb4f48b62007-10-18 23:39:33 -07005283 /* Reassign the task to the init_css_set. */
5284 task_lock(tsk);
Tejun Heoa8ad8052013-06-21 15:52:04 -07005285 cset = task_css_set(tsk);
5286 RCU_INIT_POINTER(tsk->cgroups, &init_css_set);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005287
5288 if (run_callbacks && need_forkexit_callback) {
Li Zefan7d8e0bf2013-03-05 10:57:03 +08005289 /*
5290 * fork/exit callbacks are supported only for builtin
5291 * subsystems, see cgroup_post_fork() for details.
5292 */
Tejun Heo30159ec2013-06-25 11:53:37 -07005293 for_each_builtin_subsys(ss, i) {
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005294 if (ss->exit) {
Tejun Heoeb954192013-08-08 20:11:23 -04005295 struct cgroup_subsys_state *old_css = cset->subsys[i];
5296 struct cgroup_subsys_state *css = task_css(tsk, i);
Tejun Heo30159ec2013-06-25 11:53:37 -07005297
Tejun Heoeb954192013-08-08 20:11:23 -04005298 ss->exit(css, old_css, tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005299 }
5300 }
5301 }
Paul Menageb4f48b62007-10-18 23:39:33 -07005302 task_unlock(tsk);
Peter Zijlstrad41d5a02011-02-07 17:02:20 +01005303
Tejun Heo5abb8852013-06-12 21:04:49 -07005304 put_css_set_taskexit(cset);
Paul Menageb4f48b62007-10-18 23:39:33 -07005305}
Paul Menage697f4162007-10-18 23:39:34 -07005306
Paul Menagebd89aab2007-10-18 23:40:44 -07005307static void check_for_release(struct cgroup *cgrp)
Paul Menage81a6a5c2007-10-18 23:39:38 -07005308{
Li Zefanf50daa72013-03-01 15:06:07 +08005309 if (cgroup_is_releasable(cgrp) &&
Tejun Heo6f3d828f02013-06-12 21:04:55 -07005310 list_empty(&cgrp->cset_links) && list_empty(&cgrp->children)) {
Li Zefanf50daa72013-03-01 15:06:07 +08005311 /*
5312 * Control Group is currently removeable. If it's not
Paul Menage81a6a5c2007-10-18 23:39:38 -07005313 * already queued for a userspace notification, queue
Li Zefanf50daa72013-03-01 15:06:07 +08005314 * it now
5315 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005316 int need_schedule_work = 0;
Li Zefanf50daa72013-03-01 15:06:07 +08005317
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005318 raw_spin_lock(&release_list_lock);
Tejun Heo54766d42013-06-12 21:04:53 -07005319 if (!cgroup_is_dead(cgrp) &&
Paul Menagebd89aab2007-10-18 23:40:44 -07005320 list_empty(&cgrp->release_list)) {
5321 list_add(&cgrp->release_list, &release_list);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005322 need_schedule_work = 1;
5323 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005324 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005325 if (need_schedule_work)
5326 schedule_work(&release_agent_work);
5327 }
5328}
5329
Paul Menage81a6a5c2007-10-18 23:39:38 -07005330/*
5331 * Notify userspace when a cgroup is released, by running the
5332 * configured release agent with the name of the cgroup (path
5333 * relative to the root of cgroup file system) as the argument.
5334 *
5335 * Most likely, this user command will try to rmdir this cgroup.
5336 *
5337 * This races with the possibility that some other task will be
5338 * attached to this cgroup before it is removed, or that some other
5339 * user task will 'mkdir' a child cgroup of this cgroup. That's ok.
5340 * The presumed 'rmdir' will fail quietly if this cgroup is no longer
5341 * unused, and this cgroup will be reprieved from its death sentence,
5342 * to continue to serve a useful existence. Next time it's released,
5343 * we will get notified again, if it still has 'notify_on_release' set.
5344 *
5345 * The final arg to call_usermodehelper() is UMH_WAIT_EXEC, which
5346 * means only wait until the task is successfully execve()'d. The
5347 * separate release agent task is forked by call_usermodehelper(),
5348 * then control in this thread returns here, without waiting for the
5349 * release agent task. We don't bother to wait because the caller of
5350 * this routine has no use for the exit status of the release agent
5351 * task, so no sense holding our caller up for that.
Paul Menage81a6a5c2007-10-18 23:39:38 -07005352 */
Paul Menage81a6a5c2007-10-18 23:39:38 -07005353static void cgroup_release_agent(struct work_struct *work)
5354{
5355 BUG_ON(work != &release_agent_work);
5356 mutex_lock(&cgroup_mutex);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005357 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005358 while (!list_empty(&release_list)) {
5359 char *argv[3], *envp[3];
5360 int i;
Paul Menagee788e062008-07-25 01:46:59 -07005361 char *pathbuf = NULL, *agentbuf = NULL;
Paul Menagebd89aab2007-10-18 23:40:44 -07005362 struct cgroup *cgrp = list_entry(release_list.next,
Paul Menage81a6a5c2007-10-18 23:39:38 -07005363 struct cgroup,
5364 release_list);
Paul Menagebd89aab2007-10-18 23:40:44 -07005365 list_del_init(&cgrp->release_list);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005366 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005367 pathbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Paul Menagee788e062008-07-25 01:46:59 -07005368 if (!pathbuf)
5369 goto continue_free;
5370 if (cgroup_path(cgrp, pathbuf, PAGE_SIZE) < 0)
5371 goto continue_free;
5372 agentbuf = kstrdup(cgrp->root->release_agent_path, GFP_KERNEL);
5373 if (!agentbuf)
5374 goto continue_free;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005375
5376 i = 0;
Paul Menagee788e062008-07-25 01:46:59 -07005377 argv[i++] = agentbuf;
5378 argv[i++] = pathbuf;
Paul Menage81a6a5c2007-10-18 23:39:38 -07005379 argv[i] = NULL;
5380
5381 i = 0;
5382 /* minimal command environment */
5383 envp[i++] = "HOME=/";
5384 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
5385 envp[i] = NULL;
5386
5387 /* Drop the lock while we invoke the usermode helper,
5388 * since the exec could involve hitting disk and hence
5389 * be a slow process */
5390 mutex_unlock(&cgroup_mutex);
5391 call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005392 mutex_lock(&cgroup_mutex);
Paul Menagee788e062008-07-25 01:46:59 -07005393 continue_free:
5394 kfree(pathbuf);
5395 kfree(agentbuf);
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005396 raw_spin_lock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005397 }
Thomas Gleixnercdcc1362009-07-25 16:47:45 +02005398 raw_spin_unlock(&release_list_lock);
Paul Menage81a6a5c2007-10-18 23:39:38 -07005399 mutex_unlock(&cgroup_mutex);
5400}
Paul Menage8bab8dd2008-04-04 14:29:57 -07005401
5402static int __init cgroup_disable(char *str)
5403{
Tejun Heo30159ec2013-06-25 11:53:37 -07005404 struct cgroup_subsys *ss;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005405 char *token;
Tejun Heo30159ec2013-06-25 11:53:37 -07005406 int i;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005407
5408 while ((token = strsep(&str, ",")) != NULL) {
5409 if (!*token)
5410 continue;
Paul Menage8bab8dd2008-04-04 14:29:57 -07005411
Tejun Heo30159ec2013-06-25 11:53:37 -07005412 /*
5413 * cgroup_disable, being at boot time, can't know about
5414 * module subsystems, so we don't worry about them.
5415 */
5416 for_each_builtin_subsys(ss, i) {
Paul Menage8bab8dd2008-04-04 14:29:57 -07005417 if (!strcmp(token, ss->name)) {
5418 ss->disabled = 1;
5419 printk(KERN_INFO "Disabling %s control group"
5420 " subsystem\n", ss->name);
5421 break;
5422 }
5423 }
5424 }
5425 return 1;
5426}
5427__setup("cgroup_disable=", cgroup_disable);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005428
5429/*
5430 * Functons for CSS ID.
5431 */
5432
Tejun Heo54766d42013-06-12 21:04:53 -07005433/* to get ID other than 0, this should be called when !cgroup_is_dead() */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005434unsigned short css_id(struct cgroup_subsys_state *css)
5435{
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005436 struct css_id *cssid;
5437
5438 /*
5439 * This css_id() can return correct value when somone has refcnt
5440 * on this or this is under rcu_read_lock(). Once css->id is allocated,
5441 * it's unchanged until freed.
5442 */
Tejun Heod3daf282013-06-13 19:39:16 -07005443 cssid = rcu_dereference_raw(css->id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005444
5445 if (cssid)
5446 return cssid->id;
5447 return 0;
5448}
Ben Blum67523c42010-03-10 15:22:11 -08005449EXPORT_SYMBOL_GPL(css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005450
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005451/**
5452 * css_is_ancestor - test "root" css is an ancestor of "child"
5453 * @child: the css to be tested.
5454 * @root: the css supporsed to be an ancestor of the child.
5455 *
5456 * Returns true if "root" is an ancestor of "child" in its hierarchy. Because
Johannes Weiner91c637342012-05-29 15:06:24 -07005457 * this function reads css->id, the caller must hold rcu_read_lock().
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005458 * But, considering usual usage, the csses should be valid objects after test.
5459 * Assuming that the caller will do some action to the child if this returns
5460 * returns true, the caller must take "child";s reference count.
5461 * If "child" is valid object and this returns true, "root" is valid, too.
5462 */
5463
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005464bool css_is_ancestor(struct cgroup_subsys_state *child,
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07005465 const struct cgroup_subsys_state *root)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005466{
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005467 struct css_id *child_id;
5468 struct css_id *root_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005469
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005470 child_id = rcu_dereference(child->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005471 if (!child_id)
5472 return false;
KAMEZAWA Hiroyuki747388d2010-05-11 14:06:59 -07005473 root_id = rcu_dereference(root->id);
Johannes Weiner91c637342012-05-29 15:06:24 -07005474 if (!root_id)
5475 return false;
5476 if (child_id->depth < root_id->depth)
5477 return false;
5478 if (child_id->stack[root_id->depth] != root_id->id)
5479 return false;
5480 return true;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005481}
5482
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005483void free_css_id(struct cgroup_subsys *ss, struct cgroup_subsys_state *css)
5484{
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005485 struct css_id *id = rcu_dereference_protected(css->id, true);
5486
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005487 /* When this is called before css_id initialization, id can be NULL */
5488 if (!id)
5489 return;
5490
5491 BUG_ON(!ss->use_id);
5492
5493 rcu_assign_pointer(id->css, NULL);
5494 rcu_assign_pointer(css->id, NULL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005495 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005496 idr_remove(&ss->idr, id->id);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005497 spin_unlock(&ss->id_lock);
Lai Jiangshan025cea92011-03-15 17:56:10 +08005498 kfree_rcu(id, rcu_head);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005499}
Ben Blum67523c42010-03-10 15:22:11 -08005500EXPORT_SYMBOL_GPL(free_css_id);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005501
5502/*
5503 * This is called by init or create(). Then, calls to this function are
5504 * always serialized (By cgroup_mutex() at create()).
5505 */
5506
5507static struct css_id *get_new_cssid(struct cgroup_subsys *ss, int depth)
5508{
5509 struct css_id *newid;
Tejun Heod228d9e2013-02-27 17:04:54 -08005510 int ret, size;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005511
5512 BUG_ON(!ss->use_id);
5513
5514 size = sizeof(*newid) + sizeof(unsigned short) * (depth + 1);
5515 newid = kzalloc(size, GFP_KERNEL);
5516 if (!newid)
5517 return ERR_PTR(-ENOMEM);
Tejun Heod228d9e2013-02-27 17:04:54 -08005518
5519 idr_preload(GFP_KERNEL);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005520 spin_lock(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005521 /* Don't use 0. allocates an ID of 1-65535 */
Tejun Heod228d9e2013-02-27 17:04:54 -08005522 ret = idr_alloc(&ss->idr, newid, 1, CSS_ID_MAX + 1, GFP_NOWAIT);
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005523 spin_unlock(&ss->id_lock);
Tejun Heod228d9e2013-02-27 17:04:54 -08005524 idr_preload_end();
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005525
5526 /* Returns error when there are no free spaces for new ID.*/
Tejun Heod228d9e2013-02-27 17:04:54 -08005527 if (ret < 0)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005528 goto err_out;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005529
Tejun Heod228d9e2013-02-27 17:04:54 -08005530 newid->id = ret;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005531 newid->depth = depth;
5532 return newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005533err_out:
5534 kfree(newid);
Tejun Heod228d9e2013-02-27 17:04:54 -08005535 return ERR_PTR(ret);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005536
5537}
5538
Ben Blume6a11052010-03-10 15:22:09 -08005539static int __init_or_module cgroup_init_idr(struct cgroup_subsys *ss,
5540 struct cgroup_subsys_state *rootcss)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005541{
5542 struct css_id *newid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005543
Hugh Dickins42aee6c2012-03-21 16:34:21 -07005544 spin_lock_init(&ss->id_lock);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005545 idr_init(&ss->idr);
5546
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005547 newid = get_new_cssid(ss, 0);
5548 if (IS_ERR(newid))
5549 return PTR_ERR(newid);
5550
5551 newid->stack[0] = newid->id;
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005552 RCU_INIT_POINTER(newid->css, rootcss);
5553 RCU_INIT_POINTER(rootcss->id, newid);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005554 return 0;
5555}
5556
5557static int alloc_css_id(struct cgroup_subsys *ss, struct cgroup *parent,
5558 struct cgroup *child)
5559{
5560 int subsys_id, i, depth = 0;
5561 struct cgroup_subsys_state *parent_css, *child_css;
Li Zefanfae9c792010-04-22 17:30:00 +08005562 struct css_id *child_id, *parent_id;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005563
5564 subsys_id = ss->subsys_id;
5565 parent_css = parent->subsys[subsys_id];
5566 child_css = child->subsys[subsys_id];
Tejun Heoa4ea1cc2013-06-21 15:52:33 -07005567 parent_id = rcu_dereference_protected(parent_css->id, true);
Greg Thelen94b3dd02010-06-04 14:15:03 -07005568 depth = parent_id->depth + 1;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005569
5570 child_id = get_new_cssid(ss, depth);
5571 if (IS_ERR(child_id))
5572 return PTR_ERR(child_id);
5573
5574 for (i = 0; i < depth; i++)
5575 child_id->stack[i] = parent_id->stack[i];
5576 child_id->stack[depth] = child_id->id;
5577 /*
5578 * child_id->css pointer will be set after this cgroup is available
5579 * see cgroup_populate_dir()
5580 */
5581 rcu_assign_pointer(child_css->id, child_id);
5582
5583 return 0;
5584}
5585
5586/**
5587 * css_lookup - lookup css by id
5588 * @ss: cgroup subsys to be looked into.
5589 * @id: the id
5590 *
5591 * Returns pointer to cgroup_subsys_state if there is valid one with id.
5592 * NULL if not. Should be called under rcu_read_lock()
5593 */
5594struct cgroup_subsys_state *css_lookup(struct cgroup_subsys *ss, int id)
5595{
5596 struct css_id *cssid = NULL;
5597
5598 BUG_ON(!ss->use_id);
5599 cssid = idr_find(&ss->idr, id);
5600
5601 if (unlikely(!cssid))
5602 return NULL;
5603
5604 return rcu_dereference(cssid->css);
5605}
Ben Blum67523c42010-03-10 15:22:11 -08005606EXPORT_SYMBOL_GPL(css_lookup);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -07005607
Stephane Eraniane5d13672011-02-14 11:20:01 +02005608/*
5609 * get corresponding css from file open on cgroupfs directory
5610 */
5611struct cgroup_subsys_state *cgroup_css_from_dir(struct file *f, int id)
5612{
5613 struct cgroup *cgrp;
5614 struct inode *inode;
5615 struct cgroup_subsys_state *css;
5616
Al Viro496ad9a2013-01-23 17:07:38 -05005617 inode = file_inode(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005618 /* check in cgroup filesystem dir */
5619 if (inode->i_op != &cgroup_dir_inode_operations)
5620 return ERR_PTR(-EBADF);
5621
5622 if (id < 0 || id >= CGROUP_SUBSYS_COUNT)
5623 return ERR_PTR(-EINVAL);
5624
5625 /* get cgroup */
5626 cgrp = __d_cgrp(f->f_dentry);
5627 css = cgrp->subsys[id];
5628 return css ? css : ERR_PTR(-ENOENT);
5629}
5630
Paul Menagefe693432009-09-23 15:56:20 -07005631#ifdef CONFIG_CGROUP_DEBUG
Tejun Heoeb954192013-08-08 20:11:23 -04005632static struct cgroup_subsys_state *
5633debug_css_alloc(struct cgroup_subsys_state *parent_css)
Paul Menagefe693432009-09-23 15:56:20 -07005634{
5635 struct cgroup_subsys_state *css = kzalloc(sizeof(*css), GFP_KERNEL);
5636
5637 if (!css)
5638 return ERR_PTR(-ENOMEM);
5639
5640 return css;
5641}
5642
Tejun Heoeb954192013-08-08 20:11:23 -04005643static void debug_css_free(struct cgroup_subsys_state *css)
Paul Menagefe693432009-09-23 15:56:20 -07005644{
Tejun Heoeb954192013-08-08 20:11:23 -04005645 kfree(css);
Paul Menagefe693432009-09-23 15:56:20 -07005646}
5647
Tejun Heo182446d2013-08-08 20:11:24 -04005648static u64 debug_taskcount_read(struct cgroup_subsys_state *css,
5649 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005650{
Tejun Heo182446d2013-08-08 20:11:24 -04005651 return cgroup_task_count(css->cgroup);
Paul Menagefe693432009-09-23 15:56:20 -07005652}
5653
Tejun Heo182446d2013-08-08 20:11:24 -04005654static u64 current_css_set_read(struct cgroup_subsys_state *css,
5655 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005656{
5657 return (u64)(unsigned long)current->cgroups;
5658}
5659
Tejun Heo182446d2013-08-08 20:11:24 -04005660static u64 current_css_set_refcount_read(struct cgroup_subsys_state *css,
Li Zefan03c78cb2013-06-14 11:17:19 +08005661 struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005662{
5663 u64 count;
5664
5665 rcu_read_lock();
Tejun Heoa8ad8052013-06-21 15:52:04 -07005666 count = atomic_read(&task_css_set(current)->refcount);
Paul Menagefe693432009-09-23 15:56:20 -07005667 rcu_read_unlock();
5668 return count;
5669}
5670
Tejun Heo182446d2013-08-08 20:11:24 -04005671static int current_css_set_cg_links_read(struct cgroup_subsys_state *css,
Paul Menage7717f7b2009-09-23 15:56:22 -07005672 struct cftype *cft,
5673 struct seq_file *seq)
5674{
Tejun Heo69d02062013-06-12 21:04:50 -07005675 struct cgrp_cset_link *link;
Tejun Heo5abb8852013-06-12 21:04:49 -07005676 struct css_set *cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005677
5678 read_lock(&css_set_lock);
5679 rcu_read_lock();
Tejun Heo5abb8852013-06-12 21:04:49 -07005680 cset = rcu_dereference(current->cgroups);
Tejun Heo69d02062013-06-12 21:04:50 -07005681 list_for_each_entry(link, &cset->cgrp_links, cgrp_link) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005682 struct cgroup *c = link->cgrp;
5683 const char *name;
5684
5685 if (c->dentry)
5686 name = c->dentry->d_name.name;
5687 else
5688 name = "?";
Paul Menage2c6ab6d2009-09-23 15:56:23 -07005689 seq_printf(seq, "Root %d group %s\n",
5690 c->root->hierarchy_id, name);
Paul Menage7717f7b2009-09-23 15:56:22 -07005691 }
5692 rcu_read_unlock();
5693 read_unlock(&css_set_lock);
5694 return 0;
5695}
5696
5697#define MAX_TASKS_SHOWN_PER_CSS 25
Tejun Heo182446d2013-08-08 20:11:24 -04005698static int cgroup_css_links_read(struct cgroup_subsys_state *css,
5699 struct cftype *cft, struct seq_file *seq)
Paul Menage7717f7b2009-09-23 15:56:22 -07005700{
Tejun Heo69d02062013-06-12 21:04:50 -07005701 struct cgrp_cset_link *link;
Paul Menage7717f7b2009-09-23 15:56:22 -07005702
5703 read_lock(&css_set_lock);
Tejun Heo182446d2013-08-08 20:11:24 -04005704 list_for_each_entry(link, &css->cgroup->cset_links, cset_link) {
Tejun Heo69d02062013-06-12 21:04:50 -07005705 struct css_set *cset = link->cset;
Paul Menage7717f7b2009-09-23 15:56:22 -07005706 struct task_struct *task;
5707 int count = 0;
Tejun Heo5abb8852013-06-12 21:04:49 -07005708 seq_printf(seq, "css_set %p\n", cset);
5709 list_for_each_entry(task, &cset->tasks, cg_list) {
Paul Menage7717f7b2009-09-23 15:56:22 -07005710 if (count++ > MAX_TASKS_SHOWN_PER_CSS) {
5711 seq_puts(seq, " ...\n");
5712 break;
5713 } else {
5714 seq_printf(seq, " task %d\n",
5715 task_pid_vnr(task));
5716 }
5717 }
5718 }
5719 read_unlock(&css_set_lock);
5720 return 0;
5721}
5722
Tejun Heo182446d2013-08-08 20:11:24 -04005723static u64 releasable_read(struct cgroup_subsys_state *css, struct cftype *cft)
Paul Menagefe693432009-09-23 15:56:20 -07005724{
Tejun Heo182446d2013-08-08 20:11:24 -04005725 return test_bit(CGRP_RELEASABLE, &css->cgroup->flags);
Paul Menagefe693432009-09-23 15:56:20 -07005726}
5727
5728static struct cftype debug_files[] = {
5729 {
Paul Menagefe693432009-09-23 15:56:20 -07005730 .name = "taskcount",
5731 .read_u64 = debug_taskcount_read,
5732 },
5733
5734 {
5735 .name = "current_css_set",
5736 .read_u64 = current_css_set_read,
5737 },
5738
5739 {
5740 .name = "current_css_set_refcount",
5741 .read_u64 = current_css_set_refcount_read,
5742 },
5743
5744 {
Paul Menage7717f7b2009-09-23 15:56:22 -07005745 .name = "current_css_set_cg_links",
5746 .read_seq_string = current_css_set_cg_links_read,
5747 },
5748
5749 {
5750 .name = "cgroup_css_links",
5751 .read_seq_string = cgroup_css_links_read,
5752 },
5753
5754 {
Paul Menagefe693432009-09-23 15:56:20 -07005755 .name = "releasable",
5756 .read_u64 = releasable_read,
5757 },
Paul Menagefe693432009-09-23 15:56:20 -07005758
Tejun Heo4baf6e32012-04-01 12:09:55 -07005759 { } /* terminate */
5760};
Paul Menagefe693432009-09-23 15:56:20 -07005761
5762struct cgroup_subsys debug_subsys = {
5763 .name = "debug",
Tejun Heo92fb9742012-11-19 08:13:38 -08005764 .css_alloc = debug_css_alloc,
5765 .css_free = debug_css_free,
Paul Menagefe693432009-09-23 15:56:20 -07005766 .subsys_id = debug_subsys_id,
Tejun Heo4baf6e32012-04-01 12:09:55 -07005767 .base_cftypes = debug_files,
Paul Menagefe693432009-09-23 15:56:20 -07005768};
5769#endif /* CONFIG_CGROUP_DEBUG */